ov5640: Fix missing parentesis
[omap4-v4l2-camera:sandusandus-sandusandus-omap4-v4l2-camera.git] / drivers / media / video / ov5640.c
1 /*
2  * OmniVision OV5640 sensor driver
3  *
4  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/videodev2.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/log2.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22
23 #include <linux/gpio.h>
24 #include <linux/clk.h>
25 #include <linux/regulator/consumer.h>
26
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/v4l2-ctrls.h>
30
31 #include <media/ov5640.h>
32
33 /* OV5640 has only one fixed colorspace per pixelcode */
34 struct ov5640_datafmt {
35         enum v4l2_mbus_pixelcode        code;
36         enum v4l2_colorspace            colorspace;
37 };
38
39 struct ov5640_timing_cfg {
40         u16 x_addr_start;
41         u16 y_addr_start;
42         u16 x_addr_end;
43         u16 y_addr_end;
44         u16 h_output_size;
45         u16 v_output_size;
46         u16 h_total_size;
47         u16 v_total_size;
48         u16 isp_h_offset;
49         u16 isp_v_offset;
50         u8 h_odd_ss_inc;
51         u8 h_even_ss_inc;
52         u8 v_odd_ss_inc;
53         u8 v_even_ss_inc;
54 };
55
56 enum ov5640_size {
57         OV5640_SIZE_QVGA,
58         OV5640_SIZE_VGA,
59         OV5640_SIZE_720P,
60         OV5640_SIZE_1080P,
61         OV5640_SIZE_5MP,
62         OV5640_SIZE_LAST,
63 };
64
65 static const struct v4l2_frmsize_discrete ov5640_frmsizes[OV5640_SIZE_LAST] = {
66         {  320,  240 },
67         {  640,  480 },
68         { 1280,  720 },
69         { 1920, 1080 },
70         { 2592, 1944 },
71 };
72
73 /* Find a frame size in an array */
74 static int ov5640_find_framesize(u32 width, u32 height)
75 {
76         int i;
77
78         for (i = 0; i < OV5640_SIZE_LAST; i++) {
79                 if ((ov5640_frmsizes[i].width >= width) &&
80                     (ov5640_frmsizes[i].height >= height))
81                         break;
82         }
83
84         /* If not found, select biggest */
85         if (i >= OV5640_SIZE_LAST)
86                 i = OV5640_SIZE_LAST - 1;
87
88         return i;
89 }
90
91 struct ov5640 {
92         struct v4l2_subdev subdev;
93         struct media_pad pad;
94         struct v4l2_mbus_framefmt format;
95
96         struct v4l2_ctrl_handler ctrl_handler;
97
98         const struct ov5640_platform_data *pdata;
99
100         struct v4l2_ctrl *pixel_rate;
101
102         /* HW control */
103         struct clk *xvclk;
104         struct regulator *avdd;
105         struct regulator *dovdd;
106 };
107
108 static inline struct ov5640 *to_ov5640(struct v4l2_subdev *sd)
109 {
110         return container_of(sd, struct ov5640, subdev);
111 }
112
113 /**
114  * struct ov5640_reg - ov5640 register format
115  * @reg: 16-bit offset to register
116  * @val: 8/16/32-bit register value
117  * @length: length of the register
118  *
119  * Define a structure for OV5640 register initialization values
120  */
121 struct ov5640_reg {
122         u16     reg;
123         u8      val;
124 };
125
126 /* TODO: Divide this properly */
127 static const struct ov5640_reg configscript_common1[] = {
128         { 0x3103, 0x03 },
129         { 0x3017, 0x00 },
130         { 0x3018, 0x00 },
131         { 0x3630, 0x2e },
132         { 0x3632, 0xe2 },
133         { 0x3633, 0x23 },
134         { 0x3634, 0x44 },
135         { 0x3621, 0xe0 },
136         { 0x3704, 0xa0 },
137         { 0x3703, 0x5a },
138         { 0x3715, 0x78 },
139         { 0x3717, 0x01 },
140         { 0x370b, 0x60 },
141         { 0x3705, 0x1a },
142         { 0x3905, 0x02 },
143         { 0x3906, 0x10 },
144         { 0x3901, 0x0a },
145         { 0x3731, 0x12 },
146         { 0x3600, 0x04 },
147         { 0x3601, 0x22 },
148         { 0x471c, 0x50 },
149         { 0x3002, 0x1c },
150         { 0x3006, 0xc3 },
151         { 0x300e, 0x05 },
152         { 0x302e, 0x08 },
153         { 0x3612, 0x4b },
154         { 0x3618, 0x04 },
155         { 0x3034, 0x18 },
156         { 0x3035, 0x11 },
157         { 0x3036, 0x54 },
158         { 0x3037, 0x13 },
159         { 0x3708, 0x21 },
160         { 0x3709, 0x12 },
161         { 0x370c, 0x00 },
162 };
163
164 /* TODO: Divide this properly */
165 static const struct ov5640_reg configscript_common2[] = {
166         { 0x3a02, 0x01 },
167         { 0x3a03, 0xec },
168         { 0x3a08, 0x01 },
169         { 0x3a09, 0x27 },
170         { 0x3a0a, 0x00 },
171         { 0x3a0b, 0xf6 },
172         { 0x3a0e, 0x06 },
173         { 0x3a0d, 0x08 },
174         { 0x3a14, 0x01 },
175         { 0x3a15, 0xec },
176         { 0x4001, 0x02 },
177         { 0x4004, 0x06 },
178         { 0x460b, 0x37 },
179         { 0x4750, 0x00 },
180         { 0x4751, 0x00 },
181         { 0x4800, 0x24 },
182         { 0x5a00, 0x08 },
183         { 0x5a21, 0x00 },
184         { 0x5a24, 0x00 },
185         { 0x5000, 0x27 },
186         { 0x5001, 0x87 },
187         { 0x3820, 0x40 },
188         { 0x3821, 0x06 },
189         { 0x3824, 0x01 },
190         { 0x5481, 0x08 },
191         { 0x5482, 0x14 },
192         { 0x5483, 0x28 },
193         { 0x5484, 0x51 },
194         { 0x5485, 0x65 },
195         { 0x5486, 0x71 },
196         { 0x5487, 0x7d },
197         { 0x5488, 0x87 },
198         { 0x5489, 0x91 },
199         { 0x548a, 0x9a },
200         { 0x548b, 0xaa },
201         { 0x548c, 0xb8 },
202         { 0x548d, 0xcd },
203         { 0x548e, 0xdd },
204         { 0x548f, 0xea },
205         { 0x5490, 0x1d },
206         { 0x5381, 0x20 },
207         { 0x5382, 0x64 },
208         { 0x5383, 0x08 },
209         { 0x5384, 0x20 },
210         { 0x5385, 0x80 },
211         { 0x5386, 0xa0 },
212         { 0x5387, 0xa2 },
213         { 0x5388, 0xa0 },
214         { 0x5389, 0x02 },
215         { 0x538a, 0x01 },
216         { 0x538b, 0x98 },
217         { 0x5300, 0x08 },
218         { 0x5301, 0x30 },
219         { 0x5302, 0x10 },
220         { 0x5303, 0x00 },
221         { 0x5304, 0x08 },
222         { 0x5305, 0x30 },
223         { 0x5306, 0x08 },
224         { 0x5307, 0x16 },
225         { 0x5580, 0x00 },
226         { 0x5587, 0x00 },
227         { 0x5588, 0x00 },
228         { 0x5583, 0x40 },
229         { 0x5584, 0x10 },
230         { 0x5589, 0x10 },
231         { 0x558a, 0x00 },
232         { 0x558b, 0xf8 },
233         { 0x3a0f, 0x36 },
234         { 0x3a10, 0x2e },
235         { 0x3a1b, 0x38 },
236         { 0x3a1e, 0x2c },
237         { 0x3a11, 0x70 },
238         { 0x3a1f, 0x18 },
239         { 0x3a18, 0x00 },
240         { 0x3a19, 0xf8 },
241         { 0x3003, 0x03 },
242         { 0x3003, 0x01 },
243 };
244
245 static const struct ov5640_timing_cfg timing_cfg[OV5640_SIZE_LAST] = {
246         [OV5640_SIZE_QVGA] = {
247                 .x_addr_start = 0,
248                 .y_addr_start = 0,
249                 .x_addr_end = 2623,
250                 .y_addr_end = 1951,
251                 .h_output_size = 320,
252                 .v_output_size = 240,
253                 .h_total_size = 2844,
254                 .v_total_size = 1968,
255                 .isp_h_offset = 16,
256                 .isp_v_offset = 6,
257                 .h_odd_ss_inc = 1,
258                 .h_even_ss_inc = 1,
259                 .v_odd_ss_inc = 1,
260                 .v_even_ss_inc = 1,
261         },
262         [OV5640_SIZE_VGA] = {
263                 .x_addr_start = 0,
264                 .y_addr_start = 0,
265                 .x_addr_end = 2623,
266                 .y_addr_end = 1951,
267                 .h_output_size = 640,
268                 .v_output_size = 480,
269                 .h_total_size = 2844,
270                 .v_total_size = 1968,
271                 .isp_h_offset = 16,
272                 .isp_v_offset = 6,
273                 .h_odd_ss_inc = 1,
274                 .h_even_ss_inc = 1,
275                 .v_odd_ss_inc = 1,
276                 .v_even_ss_inc = 1,
277         },
278         [OV5640_SIZE_720P] = {
279                 .x_addr_start = 336,
280                 .y_addr_start = 434,
281                 .x_addr_end = 2287,
282                 .y_addr_end = 1522,
283                 .h_output_size = 1280,
284                 .v_output_size = 720,
285                 .h_total_size = 2500,
286                 .v_total_size = 1120,
287                 .isp_h_offset = 16,
288                 .isp_v_offset = 4,
289                 .h_odd_ss_inc = 1,
290                 .h_even_ss_inc = 1,
291                 .v_odd_ss_inc = 1,
292                 .v_even_ss_inc = 1,
293         },
294         [OV5640_SIZE_1080P] = {
295                 .x_addr_start = 336,
296                 .y_addr_start = 434,
297                 .x_addr_end = 2287,
298                 .y_addr_end = 1522,
299                 .h_output_size = 1920,
300                 .v_output_size = 1080,
301                 .h_total_size = 2500,
302                 .v_total_size = 1120,
303                 .isp_h_offset = 16,
304                 .isp_v_offset = 4,
305                 .h_odd_ss_inc = 1,
306                 .h_even_ss_inc = 1,
307                 .v_odd_ss_inc = 1,
308                 .v_even_ss_inc = 1,
309         },
310         [OV5640_SIZE_5MP] = {
311                 .x_addr_start = 0,
312                 .y_addr_start = 0,
313                 .x_addr_end = 2623,
314                 .y_addr_end = 1951,
315                 .h_output_size = 2592,
316                 .v_output_size = 1944,
317                 .h_total_size = 2844,
318                 .v_total_size = 1968,
319                 .isp_h_offset = 16,
320                 .isp_v_offset = 6,
321                 .h_odd_ss_inc = 1,
322                 .h_even_ss_inc = 1,
323                 .v_odd_ss_inc = 1,
324                 .v_even_ss_inc = 1,
325         },
326 };
327
328 /**
329  * ov5640_reg_read - Read a value from a register in an ov5640 sensor device
330  * @client: i2c driver client structure
331  * @reg: register address / offset
332  * @val: stores the value that gets read
333  *
334  * Read a value from a register in an ov5640 sensor device.
335  * The value is returned in 'val'.
336  * Returns zero if successful, or non-zero otherwise.
337  */
338 static int ov5640_reg_read(struct i2c_client *client, u16 reg, u8 *val)
339 {
340         int ret;
341         u8 data[2] = {0};
342         struct i2c_msg msg = {
343                 .addr   = client->addr,
344                 .flags  = 0,
345                 .len    = 2,
346                 .buf    = data,
347         };
348
349         data[0] = (u8)(reg >> 8);
350         data[1] = (u8)(reg & 0xff);
351
352         ret = i2c_transfer(client->adapter, &msg, 1);
353         if (ret < 0)
354                 goto err;
355
356         msg.flags = I2C_M_RD;
357         msg.len = 1;
358         ret = i2c_transfer(client->adapter, &msg, 1);
359         if (ret < 0)
360                 goto err;
361
362         *val = data[0];
363         return 0;
364
365 err:
366         dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
367         return ret;
368 }
369
370 /**
371  * Write a value to a register in ov5640 sensor device.
372  * @client: i2c driver client structure.
373  * @reg: Address of the register to read value from.
374  * @val: Value to be written to a specific register.
375  * Returns zero if successful, or non-zero otherwise.
376  */
377 static int ov5640_reg_write(struct i2c_client *client, u16 reg, u8 val)
378 {
379         int ret;
380         unsigned char data[3] = { (u8)(reg >> 8), (u8)(reg & 0xff), val };
381         struct i2c_msg msg = {
382                 .addr   = client->addr,
383                 .flags  = 0,
384                 .len    = 3,
385                 .buf    = data,
386         };
387
388         ret = i2c_transfer(client->adapter, &msg, 1);
389         if (ret < 0) {
390                 dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
391                 return ret;
392         }
393
394         return 0;
395 }
396
397 /**
398  * Initialize a list of ov5640 registers.
399  * The list of registers is terminated by the pair of values
400  * @client: i2c driver client structure.
401  * @reglist[]: List of address of the registers to write data.
402  * Returns zero if successful, or non-zero otherwise.
403  */
404 static int ov5640_reg_writes(struct i2c_client *client,
405                              const struct ov5640_reg reglist[],
406                              int size)
407 {
408         int err = 0, i;
409
410         for (i = 0; i < size; i++) {
411                 err = ov5640_reg_write(client, reglist[i].reg,
412                                 reglist[i].val);
413                 if (err)
414                         return err;
415         }
416         return 0;
417 }
418
419 static int ov5640_reg_set(struct i2c_client *client, u16 reg, u8 val)
420 {
421         int ret;
422         u8 tmpval = 0;
423
424         ret = ov5640_reg_read(client, reg, &tmpval);
425         if (ret)
426                 return ret;
427
428         return ov5640_reg_write(client, reg, tmpval | val);
429 }
430
431 static int ov5640_reg_clr(struct i2c_client *client, u16 reg, u8 val)
432 {
433         int ret;
434         u8 tmpval = 0;
435
436         ret = ov5640_reg_read(client, reg, &tmpval);
437         if (ret)
438                 return ret;
439
440         return ov5640_reg_write(client, reg, tmpval & ~val);
441 }
442
443 static int ov5640_config_timing(struct v4l2_subdev *sd)
444 {
445         struct i2c_client *client = v4l2_get_subdevdata(sd);
446         struct ov5640 *ov5640 = to_ov5640(sd);
447         int ret, i;
448
449         i = ov5640_find_framesize(ov5640->format.width, ov5640->format.height);
450
451         ret = ov5640_reg_write(client,
452                         0x3800,
453                         (timing_cfg[i].x_addr_start & 0xFF00) >> 8);
454         if (ret)
455                 return ret;
456
457         ret = ov5640_reg_write(client,
458                         0x3801,
459                         timing_cfg[i].x_addr_start & 0xFF);
460         if (ret)
461                 return ret;
462
463         ret = ov5640_reg_write(client,
464                         0x3802,
465                         (timing_cfg[i].y_addr_start & 0xFF00) >> 8);
466         if (ret)
467                 return ret;
468
469         ret = ov5640_reg_write(client,
470                         0x3803,
471                         timing_cfg[i].y_addr_start & 0xFF);
472         if (ret)
473                 return ret;
474
475         ret = ov5640_reg_write(client,
476                         0x3804,
477                         (timing_cfg[i].x_addr_end & 0xFF00) >> 8);
478         if (ret)
479                 return ret;
480
481         ret = ov5640_reg_write(client,
482                         0x3805,
483                         timing_cfg[i].x_addr_end & 0xFF);
484         if (ret)
485                 return ret;
486
487         ret = ov5640_reg_write(client,
488                         0x3806,
489                         (timing_cfg[i].y_addr_end & 0xFF00) >> 8);
490         if (ret)
491                 return ret;
492
493         ret = ov5640_reg_write(client,
494                         0x3807,
495                         timing_cfg[i].y_addr_end & 0xFF);
496         if (ret)
497                 return ret;
498
499         ret = ov5640_reg_write(client,
500                         0x3808,
501                         (timing_cfg[i].h_output_size & 0xFF00) >> 8);
502         if (ret)
503                 return ret;
504
505         ret = ov5640_reg_write(client,
506                         0x3809,
507                         timing_cfg[i].h_output_size & 0xFF);
508         if (ret)
509                 return ret;
510
511         ret = ov5640_reg_write(client,
512                         0x380A,
513                         (timing_cfg[i].v_output_size & 0xFF00) >> 8);
514         if (ret)
515                 return ret;
516
517         ret = ov5640_reg_write(client,
518                         0x380B,
519                         timing_cfg[i].v_output_size & 0xFF);
520         if (ret)
521                 return ret;
522
523         ret = ov5640_reg_write(client,
524                         0x380C,
525                         (timing_cfg[i].h_total_size & 0xFF00) >> 8);
526         if (ret)
527                 return ret;
528
529         ret = ov5640_reg_write(client,
530                         0x380D,
531                         timing_cfg[i].h_total_size & 0xFF);
532         if (ret)
533                 return ret;
534
535         ret = ov5640_reg_write(client,
536                         0x380E,
537                         (timing_cfg[i].v_total_size & 0xFF00) >> 8);
538         if (ret)
539                 return ret;
540
541         ret = ov5640_reg_write(client,
542                         0x380F,
543                         timing_cfg[i].v_total_size & 0xFF);
544         if (ret)
545                 return ret;
546
547         ret = ov5640_reg_write(client,
548                         0x3810,
549                         (timing_cfg[i].isp_h_offset & 0xFF00) >> 8);
550         if (ret)
551                 return ret;
552
553         ret = ov5640_reg_write(client,
554                         0x3811,
555                         timing_cfg[i].isp_h_offset & 0xFF);
556         if (ret)
557                 return ret;
558
559         ret = ov5640_reg_write(client,
560                         0x3812,
561                         (timing_cfg[i].isp_v_offset & 0xFF00) >> 8);
562         if (ret)
563                 return ret;
564
565         ret = ov5640_reg_write(client,
566                         0x3813,
567                         timing_cfg[i].isp_v_offset & 0xFF);
568         if (ret)
569                 return ret;
570
571         ret = ov5640_reg_write(client,
572                         0x3814,
573                         ((timing_cfg[i].h_odd_ss_inc & 0xF) << 4) |
574                         (timing_cfg[i].h_even_ss_inc & 0xF));
575         if (ret)
576                 return ret;
577
578         ret = ov5640_reg_write(client,
579                         0x3815,
580                         ((timing_cfg[i].v_odd_ss_inc & 0xF) << 4) |
581                         (timing_cfg[i].v_even_ss_inc & 0xF));
582
583         return ret;
584 }
585
586 static struct v4l2_mbus_framefmt *
587 __ov5640_get_pad_format(struct ov5640 *ov5640, struct v4l2_subdev_fh *fh,
588                          unsigned int pad, enum v4l2_subdev_format_whence which)
589 {
590         switch (which) {
591         case V4L2_SUBDEV_FORMAT_TRY:
592                 return v4l2_subdev_get_try_format(fh, pad);
593         case V4L2_SUBDEV_FORMAT_ACTIVE:
594                 return &ov5640->format;
595         default:
596                 return NULL;
597         }
598 }
599
600 /* -----------------------------------------------------------------------------
601  * V4L2 subdev internal operations
602  */
603
604 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
605 {
606         struct ov5640 *ov5640 = to_ov5640(sd);
607         struct i2c_client *client = v4l2_get_subdevdata(sd);
608         struct device *dev = &client->dev;
609
610         if (on) {
611                 int ret;
612
613                 if (ov5640->pdata->pre_poweron) {
614                         ret = ov5640->pdata->pre_poweron(sd);
615                         if (ret) {
616                                 dev_err(dev,
617                                         "Error in pre_poweron (%d)\n", ret);
618                                 return ret;
619                         }
620                 }
621
622                 if (ov5640->dovdd) {
623                         ret = regulator_enable(ov5640->dovdd);
624                         if (ret) {
625                                 dev_err(dev,
626                                         "Error in enabling DOVDD (%d)\n", ret);
627                                 if (ov5640->pdata->post_poweroff)
628                                         ov5640->pdata->post_poweroff(sd);
629                                 return ret;
630                         }
631                 }
632
633                 if (ov5640->avdd) {
634                         ret = regulator_enable(ov5640->avdd);
635                         if (ret) {
636                                 dev_err(dev,
637                                         "Error in enabling AVDD (%d)\n", ret);
638                                 if (ov5640->dovdd)
639                                         regulator_disable(ov5640->dovdd);
640                                 if (ov5640->pdata->post_poweroff)
641                                         ov5640->pdata->post_poweroff(sd);
642                                 return ret;
643                         }
644                         usleep_range(5000, 5000);
645                 }
646
647                 ret = clk_enable(ov5640->xvclk);
648                 if (ret) {
649                         dev_err(dev, "Error in enabling XVCLK (%d)\n", ret);
650                         if (ov5640->avdd)
651                                 regulator_disable(ov5640->avdd);
652                         if (ov5640->dovdd)
653                                 regulator_disable(ov5640->dovdd);
654                         if (ov5640->pdata->post_poweroff)
655                                 ov5640->pdata->post_poweroff(sd);
656                         return ret;
657                 }
658                 if (gpio_is_valid(ov5640->pdata->gpio_pwdn)) {
659                         gpio_set_value(ov5640->pdata->gpio_pwdn,
660                                        ov5640->pdata->is_gpio_pwdn_acthi ?
661                                        1 : 0);
662                 }
663                 usleep_range(2000, 2000);
664         } else {
665                 if (gpio_is_valid(ov5640->pdata->gpio_pwdn)) {
666                         gpio_set_value(ov5640->pdata->gpio_pwdn,
667                                        ov5640->pdata->is_gpio_pwdn_acthi ?
668                                        0 : 1);
669                 }
670                 clk_disable(ov5640->xvclk);
671                 if (ov5640->avdd)
672                         regulator_disable(ov5640->avdd);
673                 if (ov5640->dovdd)
674                         regulator_disable(ov5640->dovdd);
675                 if (ov5640->pdata->post_poweroff)
676                         ov5640->pdata->post_poweroff(sd);
677         }
678
679         return 0;
680 }
681
682 static struct v4l2_subdev_core_ops ov5640_subdev_core_ops = {
683         .s_power        = ov5640_s_power,
684 };
685
686 static int ov5640_g_fmt(struct v4l2_subdev *sd,
687                         struct v4l2_subdev_fh *fh,
688                         struct v4l2_subdev_format *format)
689 {
690         struct ov5640 *ov5640 = to_ov5640(sd);
691
692         format->format = *__ov5640_get_pad_format(ov5640, fh, format->pad,
693                                                    format->which);
694
695         return 0;
696 }
697
698 static int ov5640_s_fmt(struct v4l2_subdev *sd,
699                         struct v4l2_subdev_fh *fh,
700                         struct v4l2_subdev_format *format)
701 {
702         struct ov5640 *ov5640 = to_ov5640(sd);
703         struct v4l2_mbus_framefmt *__format;
704
705         __format = __ov5640_get_pad_format(ov5640, fh, format->pad,
706                                             format->which);
707
708         *__format = format->format;
709
710         /* NOTE: This is always true for now, revisit later. */
711         ov5640->pixel_rate->cur.val64 = 42000000;
712
713         return 0;
714 }
715
716 static int ov5640_enum_fmt(struct v4l2_subdev *subdev,
717                            struct v4l2_subdev_fh *fh,
718                            struct v4l2_subdev_mbus_code_enum *code)
719 {
720         if (code->index >= 2)
721                 return -EINVAL;
722
723         switch (code->index) {
724         case 0:
725                 code->code = V4L2_MBUS_FMT_UYVY8_1X16;
726                 break;
727         case 1:
728                 code->code = V4L2_MBUS_FMT_YUYV8_1X16;
729                 break;
730         }
731         return 0;
732 }
733
734 static int ov5640_enum_framesizes(struct v4l2_subdev *subdev,
735                                    struct v4l2_subdev_fh *fh,
736                                    struct v4l2_subdev_frame_size_enum *fse)
737 {
738         if ((fse->index >= OV5640_SIZE_LAST) ||
739             (fse->code != V4L2_MBUS_FMT_UYVY8_1X16 &&
740              fse->code != V4L2_MBUS_FMT_YUYV8_1X16))
741                 return -EINVAL;
742
743         fse->min_width = ov5640_frmsizes[fse->index].width;
744         fse->max_width = fse->min_width;
745         fse->min_height = ov5640_frmsizes[fse->index].height;
746         fse->max_height = fse->min_height;
747
748         return 0;
749 }
750
751 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
752 {
753         struct ov5640 *ov5640 = to_ov5640(sd);
754         struct i2c_client *client = v4l2_get_subdevdata(sd);
755         int ret = 0;
756
757         if (enable) {
758                 u8 fmtreg = 0, fmtmuxreg = 0;
759                 int i;
760
761                 switch ((u32)ov5640->format.code) {
762                 case V4L2_MBUS_FMT_UYVY8_1X16:
763                         fmtreg = 0x32;
764                         fmtmuxreg = 0;
765                         break;
766                 case V4L2_MBUS_FMT_YUYV8_1X16:
767                         fmtreg = 0x30;
768                         fmtmuxreg = 0;
769                         break;
770                 default:
771                         /* This shouldn't happen */
772                         ret = -EINVAL;
773                         return ret;
774                 }
775
776                 ret = ov5640_reg_write(client, 0x4300, fmtreg);
777                 if (ret)
778                         return ret;
779
780                 ret = ov5640_reg_write(client, 0x501F, fmtmuxreg);
781                 if (ret)
782                         return ret;
783
784                 ret = ov5640_config_timing(sd);
785                 if (ret)
786                         return ret;
787
788                 i = ov5640_find_framesize(ov5640->format.width, ov5640->format.height);
789                 if ((i == OV5640_SIZE_QVGA) ||
790                     (i == OV5640_SIZE_VGA) ||
791                     (i == OV5640_SIZE_720P)) {
792                         ret = ov5640_reg_write(client, 0x3108,
793                                         (i == OV5640_SIZE_720P) ? 0x1 : 0);
794                         if (ret)
795                                 return ret;
796                         ret = ov5640_reg_set(client, 0x5001, 0x20);
797                 } else {
798                         ret = ov5640_reg_clr(client, 0x5001, 0x20);
799                         if (ret)
800                                 return ret;
801                         ret = ov5640_reg_write(client, 0x3108, 0x2);
802                 }
803
804                 ret = ov5640_reg_clr(client, 0x3008, 0x40);
805                 if (ret)
806                         goto out;
807         } else {
808                 u8 tmpreg = 0;
809
810                 ret = ov5640_reg_read(client, 0x3008, &tmpreg);
811                 if (ret)
812                         goto out;
813
814                 ret = ov5640_reg_write(client, 0x3008, tmpreg | 0x40);
815                 if (ret)
816                         goto out;
817         }
818
819 out:
820         return ret;
821 }
822
823 static struct v4l2_subdev_video_ops ov5640_subdev_video_ops = {
824         .s_stream       = ov5640_s_stream,
825 };
826
827 static struct v4l2_subdev_pad_ops ov5640_subdev_pad_ops = {
828         .enum_mbus_code = ov5640_enum_fmt,
829         .enum_frame_size = ov5640_enum_framesizes,
830         .get_fmt = ov5640_g_fmt,
831         .set_fmt = ov5640_s_fmt,
832 };
833
834 static int ov5640_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
835 {
836         /* Quantity of initial bad frames to skip. Revisit. */
837         *frames = 3;
838
839         return 0;
840 }
841
842 static struct v4l2_subdev_sensor_ops ov5640_subdev_sensor_ops = {
843         .g_skip_frames  = ov5640_g_skip_frames,
844 };
845
846 static struct v4l2_subdev_ops ov5640_subdev_ops = {
847         .core   = &ov5640_subdev_core_ops,
848         .video  = &ov5640_subdev_video_ops,
849         .pad    = &ov5640_subdev_pad_ops,
850         .sensor = &ov5640_subdev_sensor_ops,
851 };
852
853 static int ov5640_registered(struct v4l2_subdev *subdev)
854 {
855         struct i2c_client *client = v4l2_get_subdevdata(subdev);
856         struct ov5640 *ov5640 = to_ov5640(subdev);
857         int ret = 0;
858         u8 revision = 0;
859
860         ret = ov5640_s_power(subdev, 1);
861         if (ret < 0) {
862                 dev_err(&client->dev, "OV5640 power up failed\n");
863                 return ret;
864         }
865
866         ret = ov5640_reg_read(client, 0x302A, &revision);
867         if (ret) {
868                 dev_err(&client->dev, "Failure to detect OV5640 chip\n");
869                 goto out;
870         }
871
872         revision &= 0xF;
873
874         dev_info(&client->dev, "Detected a OV5640 chip, revision %x\n",
875                  revision);
876
877         /* SW Reset */
878         ret = ov5640_reg_set(client, 0x3008, 0x80);
879         if (ret)
880                 goto out;
881
882         msleep(2);
883
884         ret = ov5640_reg_clr(client, 0x3008, 0x80);
885         if (ret)
886                 goto out;
887
888         /* SW Powerdown */
889         ret = ov5640_reg_set(client, 0x3008, 0x40);
890         if (ret)
891                 goto out;
892
893         ret = ov5640_reg_writes(client, configscript_common1,
894                         ARRAY_SIZE(configscript_common1));
895         if (ret)
896                 goto out;
897
898         ret = ov5640_reg_writes(client, configscript_common2,
899                         ARRAY_SIZE(configscript_common2));
900         if (ret)
901                 goto out;
902
903         /* Init controls */
904         ret = v4l2_ctrl_handler_init(&ov5640->ctrl_handler, 1);
905         if (ret)
906                 goto out;
907
908         ov5640->pixel_rate = v4l2_ctrl_new_std(
909                                 &ov5640->ctrl_handler, NULL,
910                                 V4L2_CID_PIXEL_RATE,
911                                 0, 0, 1, 0);
912
913         subdev->ctrl_handler = &ov5640->ctrl_handler;
914 out:
915         ov5640_s_power(subdev, 0);
916
917         return ret;
918 }
919
920 static int ov5640_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
921 {
922         struct v4l2_mbus_framefmt *format;
923
924         format = v4l2_subdev_get_try_format(fh, 0);
925         format->code = V4L2_MBUS_FMT_UYVY8_1X16;
926         format->width = ov5640_frmsizes[OV5640_SIZE_VGA].width;
927         format->height = ov5640_frmsizes[OV5640_SIZE_VGA].height;
928         format->field = V4L2_FIELD_NONE;
929         format->colorspace = V4L2_COLORSPACE_JPEG;
930
931         return 0;
932 }
933
934 static int ov5640_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
935 {
936         return 0;
937 }
938
939 static struct v4l2_subdev_internal_ops ov5640_subdev_internal_ops = {
940         .registered = ov5640_registered,
941         .open = ov5640_open,
942         .close = ov5640_close,
943 };
944
945 static int ov5640_get_resources(struct ov5640 *ov5640, struct device *dev)
946 {
947         const struct ov5640_platform_data *pdata = ov5640->pdata;
948         int ret = 0;
949
950         ov5640->xvclk = clk_get(dev, pdata->clk_xvclk);
951         if (IS_ERR(ov5640->xvclk)) {
952                 dev_err(dev, "Unable to get XVCLK (%s)\n", pdata->clk_xvclk);
953                 return -ENODEV;
954         }
955
956         if (clk_round_rate(ov5640->xvclk, 24000000) != 24000000)
957                 dev_warn(dev, "XVCLK set to rounded aproximate (%lu Hz)",
958                          clk_round_rate(ov5640->xvclk, 24000000));
959
960         if (clk_set_rate(ov5640->xvclk,
961                          clk_round_rate(ov5640->xvclk, 24000000))) {
962                 dev_err(dev, "Unable to change XVCLK (%s) rate!\n",
963                         pdata->clk_xvclk);
964                 ret = -EINVAL;
965                 goto err_clk_set_rate;
966         }
967
968         if (!pdata->reg_avdd)
969                 goto get_reg_dovdd;
970
971         ov5640->avdd = devm_regulator_get(dev, pdata->reg_avdd);
972         if (IS_ERR(ov5640->avdd)) {
973                 dev_err(dev, "Unable to get AVDD (%s) regulator\n",
974                         pdata->reg_avdd);
975                 ret = -ENODEV;
976                 goto err_reg_avdd;
977         }
978
979         if (regulator_set_voltage(ov5640->avdd, 2800000, 2800000)) {
980                 dev_err(dev, "Unable to set valid AVDD (%s) regulator"
981                         " voltage to: 2.8V\n", pdata->reg_avdd);
982                 ret = -ENODEV;
983                 goto err_reg_avdd;
984         }
985
986 get_reg_dovdd:
987         if (!pdata->reg_dovdd)
988                 goto get_gpio_pwdn;
989
990         ov5640->dovdd = devm_regulator_get(dev, pdata->reg_dovdd);
991         if (IS_ERR(ov5640->dovdd)) {
992                 dev_err(dev, "Unable to get DOVDD (%s) regulator\n",
993                         pdata->reg_dovdd);
994                 ret = -ENODEV;
995                 goto err_reg_dovdd;
996         }
997
998         if (regulator_set_voltage(ov5640->dovdd, 1800000, 1800000)) {
999                 dev_err(dev, "Unable to set valid DOVDD (%s) regulator"
1000                         " voltage to: 1.8V\n", pdata->reg_dovdd);
1001                 ret = -ENODEV;
1002                 goto err_reg_dovdd;
1003         }
1004
1005 get_gpio_pwdn:
1006         if (!gpio_is_valid(pdata->gpio_pwdn))
1007                 goto get_gpio_resetb;
1008
1009         if (gpio_request_one(pdata->gpio_pwdn,
1010                              pdata->is_gpio_pwdn_acthi ?
1011                              GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
1012                              "OV5640_PWDN")) {
1013                 dev_err(dev, "Cannot request GPIO %d\n", pdata->gpio_pwdn);
1014                 ret = -ENODEV;
1015                 goto err_gpio_pwdn;
1016         }
1017
1018 get_gpio_resetb:
1019         if (!gpio_is_valid(pdata->gpio_resetb))
1020                 goto out;
1021
1022         if (gpio_request_one(pdata->gpio_resetb,
1023                              pdata->is_gpio_resetb_acthi ?
1024                              GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
1025                              "OV5640_RESETB")) {
1026                 dev_err(dev, "Cannot request GPIO %d\n", pdata->gpio_resetb);
1027                 ret = -ENODEV;
1028                 goto err_gpio_resetb;
1029         }
1030
1031 out:
1032         return 0;
1033
1034 err_gpio_resetb:
1035         if (gpio_is_valid(pdata->gpio_pwdn))
1036                 gpio_free(pdata->gpio_pwdn);
1037 err_gpio_pwdn:
1038 err_reg_dovdd:
1039 err_reg_avdd:
1040 err_clk_set_rate:
1041         clk_put(ov5640->xvclk);
1042
1043         return ret;
1044 }
1045
1046 static void ov5640_put_resources(struct ov5640 *ov5640)
1047 {
1048         if (gpio_is_valid(ov5640->pdata->gpio_resetb))
1049                 gpio_free(ov5640->pdata->gpio_resetb);
1050         if (gpio_is_valid(ov5640->pdata->gpio_pwdn))
1051                 gpio_free(ov5640->pdata->gpio_pwdn);
1052         clk_put(ov5640->xvclk);
1053 }
1054
1055 static int ov5640_probe(struct i2c_client *client,
1056                          const struct i2c_device_id *did)
1057 {
1058         struct ov5640 *ov5640;
1059         int ret;
1060
1061         if (!client->dev.platform_data) {
1062                 dev_err(&client->dev, "No platform data!!\n");
1063                 return -ENODEV;
1064         }
1065
1066         ov5640 = kzalloc(sizeof(*ov5640), GFP_KERNEL);
1067         if (!ov5640)
1068                 return -ENOMEM;
1069
1070         ov5640->pdata = client->dev.platform_data;
1071
1072         ret = ov5640_get_resources(ov5640, &client->dev);
1073         if (ret) {
1074                 kfree(ov5640);
1075                 return ret;
1076         }
1077
1078         ov5640->format.code = V4L2_MBUS_FMT_UYVY8_1X16;
1079         ov5640->format.width = ov5640_frmsizes[OV5640_SIZE_VGA].width;
1080         ov5640->format.height = ov5640_frmsizes[OV5640_SIZE_VGA].height;
1081         ov5640->format.field = V4L2_FIELD_NONE;
1082         ov5640->format.colorspace = V4L2_COLORSPACE_JPEG;
1083
1084         v4l2_i2c_subdev_init(&ov5640->subdev, client, &ov5640_subdev_ops);
1085         ov5640->subdev.internal_ops = &ov5640_subdev_internal_ops;
1086         ov5640->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1087         ov5640->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1088
1089         ov5640->pad.flags = MEDIA_PAD_FL_SOURCE;
1090         ret = media_entity_init(&ov5640->subdev.entity, 1, &ov5640->pad, 0);
1091         if (ret < 0) {
1092                 media_entity_cleanup(&ov5640->subdev.entity);
1093                 ov5640_put_resources(ov5640);
1094                 kfree(ov5640);
1095         }
1096
1097         return ret;
1098 }
1099
1100 static int ov5640_remove(struct i2c_client *client)
1101 {
1102         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1103         struct ov5640 *ov5640 = to_ov5640(subdev);
1104
1105         v4l2_ctrl_handler_free(&ov5640->ctrl_handler);
1106         media_entity_cleanup(&subdev->entity);
1107         v4l2_device_unregister_subdev(subdev);
1108         ov5640_put_resources(ov5640);
1109         kfree(ov5640);
1110         return 0;
1111 }
1112
1113 static const struct i2c_device_id ov5640_id[] = {
1114         { "ov5640", 0 },
1115         { }
1116 };
1117 MODULE_DEVICE_TABLE(i2c, ov5640_id);
1118
1119 static struct i2c_driver ov5640_i2c_driver = {
1120         .driver = {
1121                 .name = "ov5640",
1122         },
1123         .probe          = ov5640_probe,
1124         .remove         = ov5640_remove,
1125         .id_table       = ov5640_id,
1126 };
1127
1128 static int __init ov5640_mod_init(void)
1129 {
1130         return i2c_add_driver(&ov5640_i2c_driver);
1131 }
1132
1133 static void __exit ov5640_mod_exit(void)
1134 {
1135         i2c_del_driver(&ov5640_i2c_driver);
1136 }
1137
1138 module_init(ov5640_mod_init);
1139 module_exit(ov5640_mod_exit);
1140
1141 MODULE_DESCRIPTION("OmniVision OV5640 Camera driver");
1142 MODULE_AUTHOR("Sergio Aguirre <saaguirre@ti.com>");
1143 MODULE_LICENSE("GPL v2");