[media] V4L: mt9t112: support the new mbus-config subdev ops
[pandora-kernel.git] / drivers / media / video / mt9t112.c
1 /*
2  * mt9t112 Camera Driver
3  *
4  * Copyright (C) 2009 Renesas Solutions Corp.
5  * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6  *
7  * Based on ov772x driver, mt9m111 driver,
8  *
9  * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
10  * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
11  * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
12  * Copyright (C) 2008 Magnus Damm
13  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License version 2 as
17  * published by the Free Software Foundation.
18  */
19
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/videodev2.h>
26
27 #include <media/mt9t112.h>
28 #include <media/soc_camera.h>
29 #include <media/soc_mediabus.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <media/v4l2-common.h>
32
33 /* you can check PLL/clock info */
34 /* #define EXT_CLOCK 24000000 */
35
36 /************************************************************************
37                         macro
38 ************************************************************************/
39 /*
40  * frame size
41  */
42 #define MAX_WIDTH   2048
43 #define MAX_HEIGHT  1536
44
45 #define VGA_WIDTH   640
46 #define VGA_HEIGHT  480
47
48 /*
49  * macro of read/write
50  */
51 #define ECHECKER(ret, x)                \
52         do {                            \
53                 (ret) = (x);            \
54                 if ((ret) < 0)          \
55                         return (ret);   \
56         } while (0)
57
58 #define mt9t112_reg_write(ret, client, a, b) \
59         ECHECKER(ret, __mt9t112_reg_write(client, a, b))
60 #define mt9t112_mcu_write(ret, client, a, b) \
61         ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
62
63 #define mt9t112_reg_mask_set(ret, client, a, b, c) \
64         ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
65 #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
66         ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
67
68 #define mt9t112_reg_read(ret, client, a) \
69         ECHECKER(ret, __mt9t112_reg_read(client, a))
70
71 /*
72  * Logical address
73  */
74 #define _VAR(id, offset, base)  (base | (id & 0x1f) << 10 | (offset & 0x3ff))
75 #define VAR(id, offset)  _VAR(id, offset, 0x0000)
76 #define VAR8(id, offset) _VAR(id, offset, 0x8000)
77
78 /************************************************************************
79                         struct
80 ************************************************************************/
81 struct mt9t112_frame_size {
82         u16 width;
83         u16 height;
84 };
85
86 struct mt9t112_format {
87         enum v4l2_mbus_pixelcode code;
88         enum v4l2_colorspace colorspace;
89         u16 fmt;
90         u16 order;
91 };
92
93 struct mt9t112_priv {
94         struct v4l2_subdev               subdev;
95         struct mt9t112_camera_info      *info;
96         struct i2c_client               *client;
97         struct soc_camera_device         icd;
98         struct mt9t112_frame_size        frame;
99         const struct mt9t112_format     *format;
100         int                              model;
101         u32                              flags;
102 /* for flags */
103 #define INIT_DONE       (1 << 0)
104 #define PCLK_RISING     (1 << 1)
105 };
106
107 /************************************************************************
108                         supported format
109 ************************************************************************/
110
111 static const struct mt9t112_format mt9t112_cfmts[] = {
112         {
113                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
114                 .colorspace     = V4L2_COLORSPACE_JPEG,
115                 .fmt            = 1,
116                 .order          = 0,
117         }, {
118                 .code           = V4L2_MBUS_FMT_VYUY8_2X8,
119                 .colorspace     = V4L2_COLORSPACE_JPEG,
120                 .fmt            = 1,
121                 .order          = 1,
122         }, {
123                 .code           = V4L2_MBUS_FMT_YUYV8_2X8,
124                 .colorspace     = V4L2_COLORSPACE_JPEG,
125                 .fmt            = 1,
126                 .order          = 2,
127         }, {
128                 .code           = V4L2_MBUS_FMT_YVYU8_2X8,
129                 .colorspace     = V4L2_COLORSPACE_JPEG,
130                 .fmt            = 1,
131                 .order          = 3,
132         }, {
133                 .code           = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
134                 .colorspace     = V4L2_COLORSPACE_SRGB,
135                 .fmt            = 8,
136                 .order          = 2,
137         }, {
138                 .code           = V4L2_MBUS_FMT_RGB565_2X8_LE,
139                 .colorspace     = V4L2_COLORSPACE_SRGB,
140                 .fmt            = 4,
141                 .order          = 2,
142         },
143 };
144
145 /************************************************************************
146                         general function
147 ************************************************************************/
148 static struct mt9t112_priv *to_mt9t112(const struct i2c_client *client)
149 {
150         return container_of(i2c_get_clientdata(client),
151                             struct mt9t112_priv,
152                             subdev);
153 }
154
155 static int __mt9t112_reg_read(const struct i2c_client *client, u16 command)
156 {
157         struct i2c_msg msg[2];
158         u8 buf[2];
159         int ret;
160
161         command = swab16(command);
162
163         msg[0].addr  = client->addr;
164         msg[0].flags = 0;
165         msg[0].len   = 2;
166         msg[0].buf   = (u8 *)&command;
167
168         msg[1].addr  = client->addr;
169         msg[1].flags = I2C_M_RD;
170         msg[1].len   = 2;
171         msg[1].buf   = buf;
172
173         /*
174          * if return value of this function is < 0,
175          * it mean error.
176          * else, under 16bit is valid data.
177          */
178         ret = i2c_transfer(client->adapter, msg, 2);
179         if (ret < 0)
180                 return ret;
181
182         memcpy(&ret, buf, 2);
183         return swab16(ret);
184 }
185
186 static int __mt9t112_reg_write(const struct i2c_client *client,
187                                u16 command, u16 data)
188 {
189         struct i2c_msg msg;
190         u8 buf[4];
191         int ret;
192
193         command = swab16(command);
194         data = swab16(data);
195
196         memcpy(buf + 0, &command, 2);
197         memcpy(buf + 2, &data,    2);
198
199         msg.addr  = client->addr;
200         msg.flags = 0;
201         msg.len   = 4;
202         msg.buf   = buf;
203
204         /*
205          * i2c_transfer return message length,
206          * but this function should return 0 if correct case
207          */
208         ret = i2c_transfer(client->adapter, &msg, 1);
209         if (ret >= 0)
210                 ret = 0;
211
212         return ret;
213 }
214
215 static int __mt9t112_reg_mask_set(const struct i2c_client *client,
216                                   u16  command,
217                                   u16  mask,
218                                   u16  set)
219 {
220         int val = __mt9t112_reg_read(client, command);
221         if (val < 0)
222                 return val;
223
224         val &= ~mask;
225         val |= set & mask;
226
227         return __mt9t112_reg_write(client, command, val);
228 }
229
230 /* mcu access */
231 static int __mt9t112_mcu_read(const struct i2c_client *client, u16 command)
232 {
233         int ret;
234
235         ret = __mt9t112_reg_write(client, 0x098E, command);
236         if (ret < 0)
237                 return ret;
238
239         return __mt9t112_reg_read(client, 0x0990);
240 }
241
242 static int __mt9t112_mcu_write(const struct i2c_client *client,
243                                u16 command, u16 data)
244 {
245         int ret;
246
247         ret = __mt9t112_reg_write(client, 0x098E, command);
248         if (ret < 0)
249                 return ret;
250
251         return __mt9t112_reg_write(client, 0x0990, data);
252 }
253
254 static int __mt9t112_mcu_mask_set(const struct i2c_client *client,
255                                   u16  command,
256                                   u16  mask,
257                                   u16  set)
258 {
259         int val = __mt9t112_mcu_read(client, command);
260         if (val < 0)
261                 return val;
262
263         val &= ~mask;
264         val |= set & mask;
265
266         return __mt9t112_mcu_write(client, command, val);
267 }
268
269 static int mt9t112_reset(const struct i2c_client *client)
270 {
271         int ret;
272
273         mt9t112_reg_mask_set(ret, client, 0x001a, 0x0001, 0x0001);
274         msleep(1);
275         mt9t112_reg_mask_set(ret, client, 0x001a, 0x0001, 0x0000);
276
277         return ret;
278 }
279
280 #ifndef EXT_CLOCK
281 #define CLOCK_INFO(a, b)
282 #else
283 #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
284 static int mt9t112_clock_info(const struct i2c_client *client, u32 ext)
285 {
286         int m, n, p1, p2, p3, p4, p5, p6, p7;
287         u32 vco, clk;
288         char *enable;
289
290         ext /= 1000; /* kbyte order */
291
292         mt9t112_reg_read(n, client, 0x0012);
293         p1 = n & 0x000f;
294         n = n >> 4;
295         p2 = n & 0x000f;
296         n = n >> 4;
297         p3 = n & 0x000f;
298
299         mt9t112_reg_read(n, client, 0x002a);
300         p4 = n & 0x000f;
301         n = n >> 4;
302         p5 = n & 0x000f;
303         n = n >> 4;
304         p6 = n & 0x000f;
305
306         mt9t112_reg_read(n, client, 0x002c);
307         p7 = n & 0x000f;
308
309         mt9t112_reg_read(n, client, 0x0010);
310         m = n & 0x00ff;
311         n = (n >> 8) & 0x003f;
312
313         enable = ((6000 > ext) || (54000 < ext)) ? "X" : "";
314         dev_info(&client->dev, "EXTCLK          : %10u K %s\n", ext, enable);
315
316         vco = 2 * m * ext / (n+1);
317         enable = ((384000 > vco) || (768000 < vco)) ? "X" : "";
318         dev_info(&client->dev, "VCO             : %10u K %s\n", vco, enable);
319
320         clk = vco / (p1+1) / (p2+1);
321         enable = (96000 < clk) ? "X" : "";
322         dev_info(&client->dev, "PIXCLK          : %10u K %s\n", clk, enable);
323
324         clk = vco / (p3+1);
325         enable = (768000 < clk) ? "X" : "";
326         dev_info(&client->dev, "MIPICLK         : %10u K %s\n", clk, enable);
327
328         clk = vco / (p6+1);
329         enable = (96000 < clk) ? "X" : "";
330         dev_info(&client->dev, "MCU CLK         : %10u K %s\n", clk, enable);
331
332         clk = vco / (p5+1);
333         enable = (54000 < clk) ? "X" : "";
334         dev_info(&client->dev, "SOC CLK         : %10u K %s\n", clk, enable);
335
336         clk = vco / (p4+1);
337         enable = (70000 < clk) ? "X" : "";
338         dev_info(&client->dev, "Sensor CLK      : %10u K %s\n", clk, enable);
339
340         clk = vco / (p7+1);
341         dev_info(&client->dev, "External sensor : %10u K\n", clk);
342
343         clk = ext / (n+1);
344         enable = ((2000 > clk) || (24000 < clk)) ? "X" : "";
345         dev_info(&client->dev, "PFD             : %10u K %s\n", clk, enable);
346
347         return 0;
348 }
349 #endif
350
351 static void mt9t112_frame_check(u32 *width, u32 *height)
352 {
353         if (*width > MAX_WIDTH)
354                 *width = MAX_WIDTH;
355
356         if (*height > MAX_HEIGHT)
357                 *height = MAX_HEIGHT;
358 }
359
360 static int mt9t112_set_a_frame_size(const struct i2c_client *client,
361                                    u16 width,
362                                    u16 height)
363 {
364         int ret;
365         u16 wstart = (MAX_WIDTH - width) / 2;
366         u16 hstart = (MAX_HEIGHT - height) / 2;
367
368         /* (Context A) Image Width/Height */
369         mt9t112_mcu_write(ret, client, VAR(26, 0), width);
370         mt9t112_mcu_write(ret, client, VAR(26, 2), height);
371
372         /* (Context A) Output Width/Height */
373         mt9t112_mcu_write(ret, client, VAR(18, 43), 8 + width);
374         mt9t112_mcu_write(ret, client, VAR(18, 45), 8 + height);
375
376         /* (Context A) Start Row/Column */
377         mt9t112_mcu_write(ret, client, VAR(18, 2), 4 + hstart);
378         mt9t112_mcu_write(ret, client, VAR(18, 4), 4 + wstart);
379
380         /* (Context A) End Row/Column */
381         mt9t112_mcu_write(ret, client, VAR(18, 6), 11 + height + hstart);
382         mt9t112_mcu_write(ret, client, VAR(18, 8), 11 + width  + wstart);
383
384         mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
385
386         return ret;
387 }
388
389 static int mt9t112_set_pll_dividers(const struct i2c_client *client,
390                                     u8 m, u8 n,
391                                     u8 p1, u8 p2, u8 p3,
392                                     u8 p4, u8 p5, u8 p6,
393                                     u8 p7)
394 {
395         int ret;
396         u16 val;
397
398         /* N/M */
399         val = (n << 8) |
400               (m << 0);
401         mt9t112_reg_mask_set(ret, client, 0x0010, 0x3fff, val);
402
403         /* P1/P2/P3 */
404         val = ((p3 & 0x0F) << 8) |
405               ((p2 & 0x0F) << 4) |
406               ((p1 & 0x0F) << 0);
407         mt9t112_reg_mask_set(ret, client, 0x0012, 0x0fff, val);
408
409         /* P4/P5/P6 */
410         val = (0x7         << 12) |
411               ((p6 & 0x0F) <<  8) |
412               ((p5 & 0x0F) <<  4) |
413               ((p4 & 0x0F) <<  0);
414         mt9t112_reg_mask_set(ret, client, 0x002A, 0x7fff, val);
415
416         /* P7 */
417         val = (0x1         << 12) |
418               ((p7 & 0x0F) <<  0);
419         mt9t112_reg_mask_set(ret, client, 0x002C, 0x100f, val);
420
421         return ret;
422 }
423
424 static int mt9t112_init_pll(const struct i2c_client *client)
425 {
426         struct mt9t112_priv *priv = to_mt9t112(client);
427         int data, i, ret;
428
429         mt9t112_reg_mask_set(ret, client, 0x0014, 0x003, 0x0001);
430
431         /* PLL control: BYPASS PLL = 8517 */
432         mt9t112_reg_write(ret, client, 0x0014, 0x2145);
433
434         /* Replace these registers when new timing parameters are generated */
435         mt9t112_set_pll_dividers(client,
436                                  priv->info->divider.m,
437                                  priv->info->divider.n,
438                                  priv->info->divider.p1,
439                                  priv->info->divider.p2,
440                                  priv->info->divider.p3,
441                                  priv->info->divider.p4,
442                                  priv->info->divider.p5,
443                                  priv->info->divider.p6,
444                                  priv->info->divider.p7);
445
446         /*
447          * TEST_BYPASS  on
448          * PLL_ENABLE   on
449          * SEL_LOCK_DET on
450          * TEST_BYPASS  off
451          */
452         mt9t112_reg_write(ret, client, 0x0014, 0x2525);
453         mt9t112_reg_write(ret, client, 0x0014, 0x2527);
454         mt9t112_reg_write(ret, client, 0x0014, 0x3427);
455         mt9t112_reg_write(ret, client, 0x0014, 0x3027);
456
457         mdelay(10);
458
459         /*
460          * PLL_BYPASS off
461          * Reference clock count
462          * I2C Master Clock Divider
463          */
464         mt9t112_reg_write(ret, client, 0x0014, 0x3046);
465         mt9t112_reg_write(ret, client, 0x0022, 0x0190);
466         mt9t112_reg_write(ret, client, 0x3B84, 0x0212);
467
468         /* External sensor clock is PLL bypass */
469         mt9t112_reg_write(ret, client, 0x002E, 0x0500);
470
471         mt9t112_reg_mask_set(ret, client, 0x0018, 0x0002, 0x0002);
472         mt9t112_reg_mask_set(ret, client, 0x3B82, 0x0004, 0x0004);
473
474         /* MCU disabled */
475         mt9t112_reg_mask_set(ret, client, 0x0018, 0x0004, 0x0004);
476
477         /* out of standby */
478         mt9t112_reg_mask_set(ret, client, 0x0018, 0x0001, 0);
479
480         mdelay(50);
481
482         /*
483          * Standby Workaround
484          * Disable Secondary I2C Pads
485          */
486         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
487         mdelay(1);
488         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
489         mdelay(1);
490         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
491         mdelay(1);
492         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
493         mdelay(1);
494         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
495         mdelay(1);
496         mt9t112_reg_write(ret, client, 0x0614, 0x0001);
497         mdelay(1);
498
499         /* poll to verify out of standby. Must Poll this bit */
500         for (i = 0; i < 100; i++) {
501                 mt9t112_reg_read(data, client, 0x0018);
502                 if (!(0x4000 & data))
503                         break;
504
505                 mdelay(10);
506         }
507
508         return ret;
509 }
510
511 static int mt9t112_init_setting(const struct i2c_client *client)
512 {
513
514         int ret;
515
516         /* Adaptive Output Clock (A) */
517         mt9t112_mcu_mask_set(ret, client, VAR(26, 160), 0x0040, 0x0000);
518
519         /* Read Mode (A) */
520         mt9t112_mcu_write(ret, client, VAR(18, 12), 0x0024);
521
522         /* Fine Correction (A) */
523         mt9t112_mcu_write(ret, client, VAR(18, 15), 0x00CC);
524
525         /* Fine IT Min (A) */
526         mt9t112_mcu_write(ret, client, VAR(18, 17), 0x01f1);
527
528         /* Fine IT Max Margin (A) */
529         mt9t112_mcu_write(ret, client, VAR(18, 19), 0x00fF);
530
531         /* Base Frame Lines (A) */
532         mt9t112_mcu_write(ret, client, VAR(18, 29), 0x032D);
533
534         /* Min Line Length (A) */
535         mt9t112_mcu_write(ret, client, VAR(18, 31), 0x073a);
536
537         /* Line Length (A) */
538         mt9t112_mcu_write(ret, client, VAR(18, 37), 0x07d0);
539
540         /* Adaptive Output Clock (B) */
541         mt9t112_mcu_mask_set(ret, client, VAR(27, 160), 0x0040, 0x0000);
542
543         /* Row Start (B) */
544         mt9t112_mcu_write(ret, client, VAR(18, 74), 0x004);
545
546         /* Column Start (B) */
547         mt9t112_mcu_write(ret, client, VAR(18, 76), 0x004);
548
549         /* Row End (B) */
550         mt9t112_mcu_write(ret, client, VAR(18, 78), 0x60B);
551
552         /* Column End (B) */
553         mt9t112_mcu_write(ret, client, VAR(18, 80), 0x80B);
554
555         /* Fine Correction (B) */
556         mt9t112_mcu_write(ret, client, VAR(18, 87), 0x008C);
557
558         /* Fine IT Min (B) */
559         mt9t112_mcu_write(ret, client, VAR(18, 89), 0x01F1);
560
561         /* Fine IT Max Margin (B) */
562         mt9t112_mcu_write(ret, client, VAR(18, 91), 0x00FF);
563
564         /* Base Frame Lines (B) */
565         mt9t112_mcu_write(ret, client, VAR(18, 101), 0x0668);
566
567         /* Min Line Length (B) */
568         mt9t112_mcu_write(ret, client, VAR(18, 103), 0x0AF0);
569
570         /* Line Length (B) */
571         mt9t112_mcu_write(ret, client, VAR(18, 109), 0x0AF0);
572
573         /*
574          * Flicker Dectection registers
575          * This section should be replaced whenever new Timing file is generated
576          * All the following registers need to be replaced
577          * Following registers are generated from Register Wizard but user can
578          * modify them. For detail see auto flicker detection tuning
579          */
580
581         /* FD_FDPERIOD_SELECT */
582         mt9t112_mcu_write(ret, client, VAR8(8, 5), 0x01);
583
584         /* PRI_B_CONFIG_FD_ALGO_RUN */
585         mt9t112_mcu_write(ret, client, VAR(27, 17), 0x0003);
586
587         /* PRI_A_CONFIG_FD_ALGO_RUN */
588         mt9t112_mcu_write(ret, client, VAR(26, 17), 0x0003);
589
590         /*
591          * AFD range detection tuning registers
592          */
593
594         /* search_f1_50 */
595         mt9t112_mcu_write(ret, client, VAR8(18, 165), 0x25);
596
597         /* search_f2_50 */
598         mt9t112_mcu_write(ret, client, VAR8(18, 166), 0x28);
599
600         /* search_f1_60 */
601         mt9t112_mcu_write(ret, client, VAR8(18, 167), 0x2C);
602
603         /* search_f2_60 */
604         mt9t112_mcu_write(ret, client, VAR8(18, 168), 0x2F);
605
606         /* period_50Hz (A) */
607         mt9t112_mcu_write(ret, client, VAR8(18, 68), 0xBA);
608
609         /* secret register by aptina */
610         /* period_50Hz (A MSB) */
611         mt9t112_mcu_write(ret, client, VAR8(18, 303), 0x00);
612
613         /* period_60Hz (A) */
614         mt9t112_mcu_write(ret, client, VAR8(18, 69), 0x9B);
615
616         /* secret register by aptina */
617         /* period_60Hz (A MSB) */
618         mt9t112_mcu_write(ret, client, VAR8(18, 301), 0x00);
619
620         /* period_50Hz (B) */
621         mt9t112_mcu_write(ret, client, VAR8(18, 140), 0x82);
622
623         /* secret register by aptina */
624         /* period_50Hz (B) MSB */
625         mt9t112_mcu_write(ret, client, VAR8(18, 304), 0x00);
626
627         /* period_60Hz (B) */
628         mt9t112_mcu_write(ret, client, VAR8(18, 141), 0x6D);
629
630         /* secret register by aptina */
631         /* period_60Hz (B) MSB */
632         mt9t112_mcu_write(ret, client, VAR8(18, 302), 0x00);
633
634         /* FD Mode */
635         mt9t112_mcu_write(ret, client, VAR8(8, 2), 0x10);
636
637         /* Stat_min */
638         mt9t112_mcu_write(ret, client, VAR8(8, 9), 0x02);
639
640         /* Stat_max */
641         mt9t112_mcu_write(ret, client, VAR8(8, 10), 0x03);
642
643         /* Min_amplitude */
644         mt9t112_mcu_write(ret, client, VAR8(8, 12), 0x0A);
645
646         /* RX FIFO Watermark (A) */
647         mt9t112_mcu_write(ret, client, VAR(18, 70), 0x0014);
648
649         /* RX FIFO Watermark (B) */
650         mt9t112_mcu_write(ret, client, VAR(18, 142), 0x0014);
651
652         /* MCLK: 16MHz
653          * PCLK: 73MHz
654          * CorePixCLK: 36.5 MHz
655          */
656         mt9t112_mcu_write(ret, client, VAR8(18, 0x0044), 133);
657         mt9t112_mcu_write(ret, client, VAR8(18, 0x0045), 110);
658         mt9t112_mcu_write(ret, client, VAR8(18, 0x008c), 130);
659         mt9t112_mcu_write(ret, client, VAR8(18, 0x008d), 108);
660
661         mt9t112_mcu_write(ret, client, VAR8(18, 0x00A5), 27);
662         mt9t112_mcu_write(ret, client, VAR8(18, 0x00a6), 30);
663         mt9t112_mcu_write(ret, client, VAR8(18, 0x00a7), 32);
664         mt9t112_mcu_write(ret, client, VAR8(18, 0x00a8), 35);
665
666         return ret;
667 }
668
669 static int mt9t112_auto_focus_setting(const struct i2c_client *client)
670 {
671         int ret;
672
673         mt9t112_mcu_write(ret, client, VAR(12, 13),     0x000F);
674         mt9t112_mcu_write(ret, client, VAR(12, 23),     0x0F0F);
675         mt9t112_mcu_write(ret, client, VAR8(1, 0),      0x06);
676
677         mt9t112_reg_write(ret, client, 0x0614, 0x0000);
678
679         mt9t112_mcu_write(ret, client, VAR8(1, 0),      0x05);
680         mt9t112_mcu_write(ret, client, VAR8(12, 2),     0x02);
681         mt9t112_mcu_write(ret, client, VAR(12, 3),      0x0002);
682         mt9t112_mcu_write(ret, client, VAR(17, 3),      0x8001);
683         mt9t112_mcu_write(ret, client, VAR(17, 11),     0x0025);
684         mt9t112_mcu_write(ret, client, VAR(17, 13),     0x0193);
685         mt9t112_mcu_write(ret, client, VAR8(17, 33),    0x18);
686         mt9t112_mcu_write(ret, client, VAR8(1, 0),      0x05);
687
688         return ret;
689 }
690
691 static int mt9t112_auto_focus_trigger(const struct i2c_client *client)
692 {
693         int ret;
694
695         mt9t112_mcu_write(ret, client, VAR8(12, 25), 0x01);
696
697         return ret;
698 }
699
700 static int mt9t112_init_camera(const struct i2c_client *client)
701 {
702         int ret;
703
704         ECHECKER(ret, mt9t112_reset(client));
705
706         ECHECKER(ret, mt9t112_init_pll(client));
707
708         ECHECKER(ret, mt9t112_init_setting(client));
709
710         ECHECKER(ret, mt9t112_auto_focus_setting(client));
711
712         mt9t112_reg_mask_set(ret, client, 0x0018, 0x0004, 0);
713
714         /* Analog setting B */
715         mt9t112_reg_write(ret, client, 0x3084, 0x2409);
716         mt9t112_reg_write(ret, client, 0x3092, 0x0A49);
717         mt9t112_reg_write(ret, client, 0x3094, 0x4949);
718         mt9t112_reg_write(ret, client, 0x3096, 0x4950);
719
720         /*
721          * Disable adaptive clock
722          * PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR
723          * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
724          */
725         mt9t112_mcu_write(ret, client, VAR(26, 160), 0x0A2E);
726         mt9t112_mcu_write(ret, client, VAR(27, 160), 0x0A2E);
727
728         /* Configure STatus in Status_before_length Format and enable header */
729         /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
730         mt9t112_mcu_write(ret, client, VAR(27, 144), 0x0CB4);
731
732         /* Enable JPEG in context B */
733         /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
734         mt9t112_mcu_write(ret, client, VAR8(27, 142), 0x01);
735
736         /* Disable Dac_TXLO */
737         mt9t112_reg_write(ret, client, 0x316C, 0x350F);
738
739         /* Set max slew rates */
740         mt9t112_reg_write(ret, client, 0x1E, 0x777);
741
742         return ret;
743 }
744
745 /************************************************************************
746                         soc_camera_ops
747 ************************************************************************/
748 static int mt9t112_set_bus_param(struct soc_camera_device *icd,
749                                  unsigned long  flags)
750 {
751         struct soc_camera_link *icl = to_soc_camera_link(icd);
752         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
753         struct mt9t112_priv *priv = to_mt9t112(client);
754
755         if (soc_camera_apply_sensor_flags(icl, flags) & SOCAM_PCLK_SAMPLE_RISING)
756                 priv->flags |= PCLK_RISING;
757
758         return 0;
759 }
760
761 static unsigned long mt9t112_query_bus_param(struct soc_camera_device *icd)
762 {
763         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
764         struct mt9t112_priv *priv = to_mt9t112(client);
765         struct soc_camera_link *icl = to_soc_camera_link(icd);
766         unsigned long flags = SOCAM_MASTER | SOCAM_VSYNC_ACTIVE_HIGH |
767                 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH;
768
769         flags |= (priv->info->flags & MT9T112_FLAG_PCLK_RISING_EDGE) ?
770                 SOCAM_PCLK_SAMPLE_RISING : SOCAM_PCLK_SAMPLE_FALLING;
771
772         if (priv->info->flags & MT9T112_FLAG_DATAWIDTH_8)
773                 flags |= SOCAM_DATAWIDTH_8;
774         else
775                 flags |= SOCAM_DATAWIDTH_10;
776
777         return soc_camera_apply_sensor_flags(icl, flags);
778 }
779
780 static struct soc_camera_ops mt9t112_ops = {
781         .set_bus_param          = mt9t112_set_bus_param,
782         .query_bus_param        = mt9t112_query_bus_param,
783 };
784
785 /************************************************************************
786                         v4l2_subdev_core_ops
787 ************************************************************************/
788 static int mt9t112_g_chip_ident(struct v4l2_subdev *sd,
789                                 struct v4l2_dbg_chip_ident *id)
790 {
791         struct i2c_client *client = v4l2_get_subdevdata(sd);
792         struct mt9t112_priv *priv = to_mt9t112(client);
793
794         id->ident    = priv->model;
795         id->revision = 0;
796
797         return 0;
798 }
799
800 #ifdef CONFIG_VIDEO_ADV_DEBUG
801 static int mt9t112_g_register(struct v4l2_subdev *sd,
802                               struct v4l2_dbg_register *reg)
803 {
804         struct i2c_client *client = v4l2_get_subdevdata(sd);
805         int                ret;
806
807         reg->size = 2;
808         mt9t112_reg_read(ret, client, reg->reg);
809
810         reg->val = (__u64)ret;
811
812         return 0;
813 }
814
815 static int mt9t112_s_register(struct v4l2_subdev *sd,
816                               struct v4l2_dbg_register *reg)
817 {
818         struct i2c_client *client = v4l2_get_subdevdata(sd);
819         int ret;
820
821         mt9t112_reg_write(ret, client, reg->reg, reg->val);
822
823         return ret;
824 }
825 #endif
826
827 static struct v4l2_subdev_core_ops mt9t112_subdev_core_ops = {
828         .g_chip_ident   = mt9t112_g_chip_ident,
829 #ifdef CONFIG_VIDEO_ADV_DEBUG
830         .g_register     = mt9t112_g_register,
831         .s_register     = mt9t112_s_register,
832 #endif
833 };
834
835
836 /************************************************************************
837                         v4l2_subdev_video_ops
838 ************************************************************************/
839 static int mt9t112_s_stream(struct v4l2_subdev *sd, int enable)
840 {
841         struct i2c_client *client = v4l2_get_subdevdata(sd);
842         struct mt9t112_priv *priv = to_mt9t112(client);
843         int ret = 0;
844
845         if (!enable) {
846                 /* FIXME
847                  *
848                  * If user selected large output size,
849                  * and used it long time,
850                  * mt9t112 camera will be very warm.
851                  *
852                  * But current driver can not stop mt9t112 camera.
853                  * So, set small size here to solve this problem.
854                  */
855                 mt9t112_set_a_frame_size(client, VGA_WIDTH, VGA_HEIGHT);
856                 return ret;
857         }
858
859         if (!(priv->flags & INIT_DONE)) {
860                 u16 param = PCLK_RISING & priv->flags ? 0x0001 : 0x0000;
861
862                 ECHECKER(ret, mt9t112_init_camera(client));
863
864                 /* Invert PCLK (Data sampled on falling edge of pixclk) */
865                 mt9t112_reg_write(ret, client, 0x3C20, param);
866
867                 mdelay(5);
868
869                 priv->flags |= INIT_DONE;
870         }
871
872         mt9t112_mcu_write(ret, client, VAR(26, 7), priv->format->fmt);
873         mt9t112_mcu_write(ret, client, VAR(26, 9), priv->format->order);
874         mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
875
876         mt9t112_set_a_frame_size(client,
877                                  priv->frame.width,
878                                  priv->frame.height);
879
880         ECHECKER(ret, mt9t112_auto_focus_trigger(client));
881
882         dev_dbg(&client->dev, "format : %d\n", priv->format->code);
883         dev_dbg(&client->dev, "size   : %d x %d\n",
884                 priv->frame.width,
885                 priv->frame.height);
886
887         CLOCK_INFO(client, EXT_CLOCK);
888
889         return ret;
890 }
891
892 static int mt9t112_set_params(struct i2c_client *client, u32 width, u32 height,
893                               enum v4l2_mbus_pixelcode code)
894 {
895         struct mt9t112_priv *priv = to_mt9t112(client);
896         int i;
897
898         priv->format = NULL;
899
900         /*
901          * frame size check
902          */
903         mt9t112_frame_check(&width, &height);
904
905         /*
906          * get color format
907          */
908         for (i = 0; i < ARRAY_SIZE(mt9t112_cfmts); i++)
909                 if (mt9t112_cfmts[i].code == code)
910                         break;
911
912         if (i == ARRAY_SIZE(mt9t112_cfmts))
913                 return -EINVAL;
914
915         priv->frame.width  = (u16)width;
916         priv->frame.height = (u16)height;
917
918         priv->format = mt9t112_cfmts + i;
919
920         return 0;
921 }
922
923 static int mt9t112_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
924 {
925         a->bounds.left                  = 0;
926         a->bounds.top                   = 0;
927         a->bounds.width                 = VGA_WIDTH;
928         a->bounds.height                = VGA_HEIGHT;
929         a->defrect                      = a->bounds;
930         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
931         a->pixelaspect.numerator        = 1;
932         a->pixelaspect.denominator      = 1;
933
934         return 0;
935 }
936
937 static int mt9t112_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
938 {
939         a->c.left       = 0;
940         a->c.top        = 0;
941         a->c.width      = VGA_WIDTH;
942         a->c.height     = VGA_HEIGHT;
943         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
944
945         return 0;
946 }
947
948 static int mt9t112_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
949 {
950         struct i2c_client *client = v4l2_get_subdevdata(sd);
951         struct v4l2_rect *rect = &a->c;
952
953         return mt9t112_set_params(client, rect->width, rect->height,
954                                  V4L2_MBUS_FMT_UYVY8_2X8);
955 }
956
957 static int mt9t112_g_fmt(struct v4l2_subdev *sd,
958                          struct v4l2_mbus_framefmt *mf)
959 {
960         struct i2c_client *client = v4l2_get_subdevdata(sd);
961         struct mt9t112_priv *priv = to_mt9t112(client);
962
963         if (!priv->format) {
964                 int ret = mt9t112_set_params(client, VGA_WIDTH, VGA_HEIGHT,
965                                              V4L2_MBUS_FMT_UYVY8_2X8);
966                 if (ret < 0)
967                         return ret;
968         }
969
970         mf->width       = priv->frame.width;
971         mf->height      = priv->frame.height;
972         /* TODO: set colorspace */
973         mf->code        = priv->format->code;
974         mf->field       = V4L2_FIELD_NONE;
975
976         return 0;
977 }
978
979 static int mt9t112_s_fmt(struct v4l2_subdev *sd,
980                          struct v4l2_mbus_framefmt *mf)
981 {
982         struct i2c_client *client = v4l2_get_subdevdata(sd);
983
984         /* TODO: set colorspace */
985         return mt9t112_set_params(client, mf->width, mf->height, mf->code);
986 }
987
988 static int mt9t112_try_fmt(struct v4l2_subdev *sd,
989                            struct v4l2_mbus_framefmt *mf)
990 {
991         mt9t112_frame_check(&mf->width, &mf->height);
992
993         /* TODO: set colorspace */
994         mf->field = V4L2_FIELD_NONE;
995
996         return 0;
997 }
998
999 static int mt9t112_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
1000                            enum v4l2_mbus_pixelcode *code)
1001 {
1002         if (index >= ARRAY_SIZE(mt9t112_cfmts))
1003                 return -EINVAL;
1004
1005         *code = mt9t112_cfmts[index].code;
1006         return 0;
1007 }
1008
1009 static int mt9t112_g_mbus_config(struct v4l2_subdev *sd,
1010                                  struct v4l2_mbus_config *cfg)
1011 {
1012         struct i2c_client *client = v4l2_get_subdevdata(sd);
1013         struct soc_camera_device *icd = client->dev.platform_data;
1014         struct soc_camera_link *icl = to_soc_camera_link(icd);
1015
1016         cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
1017                 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH |
1018                 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
1019         cfg->type = V4L2_MBUS_PARALLEL;
1020         cfg->flags = soc_camera_apply_board_flags(icl, cfg);
1021
1022         return 0;
1023 }
1024
1025 static int mt9t112_s_mbus_config(struct v4l2_subdev *sd,
1026                                  const struct v4l2_mbus_config *cfg)
1027 {
1028         struct i2c_client *client = v4l2_get_subdevdata(sd);
1029         struct soc_camera_device *icd = client->dev.platform_data;
1030         struct soc_camera_link *icl = to_soc_camera_link(icd);
1031         struct mt9t112_priv *priv = to_mt9t112(client);
1032
1033         if (soc_camera_apply_board_flags(icl, cfg) & V4L2_MBUS_PCLK_SAMPLE_RISING)
1034                 priv->flags |= PCLK_RISING;
1035
1036         return 0;
1037 }
1038
1039 static struct v4l2_subdev_video_ops mt9t112_subdev_video_ops = {
1040         .s_stream       = mt9t112_s_stream,
1041         .g_mbus_fmt     = mt9t112_g_fmt,
1042         .s_mbus_fmt     = mt9t112_s_fmt,
1043         .try_mbus_fmt   = mt9t112_try_fmt,
1044         .cropcap        = mt9t112_cropcap,
1045         .g_crop         = mt9t112_g_crop,
1046         .s_crop         = mt9t112_s_crop,
1047         .enum_mbus_fmt  = mt9t112_enum_fmt,
1048         .g_mbus_config  = mt9t112_g_mbus_config,
1049         .s_mbus_config  = mt9t112_s_mbus_config,
1050 };
1051
1052 /************************************************************************
1053                         i2c driver
1054 ************************************************************************/
1055 static struct v4l2_subdev_ops mt9t112_subdev_ops = {
1056         .core   = &mt9t112_subdev_core_ops,
1057         .video  = &mt9t112_subdev_video_ops,
1058 };
1059
1060 static int mt9t112_camera_probe(struct soc_camera_device *icd,
1061                                 struct i2c_client *client)
1062 {
1063         struct mt9t112_priv *priv = to_mt9t112(client);
1064         const char          *devname;
1065         int                  chipid;
1066
1067         /* We must have a parent by now. And it cannot be a wrong one. */
1068         BUG_ON(!icd->parent ||
1069                to_soc_camera_host(icd->parent)->nr != icd->iface);
1070
1071         /*
1072          * check and show chip ID
1073          */
1074         mt9t112_reg_read(chipid, client, 0x0000);
1075
1076         switch (chipid) {
1077         case 0x2680:
1078                 devname = "mt9t111";
1079                 priv->model = V4L2_IDENT_MT9T111;
1080                 break;
1081         case 0x2682:
1082                 devname = "mt9t112";
1083                 priv->model = V4L2_IDENT_MT9T112;
1084                 break;
1085         default:
1086                 dev_err(&client->dev, "Product ID error %04x\n", chipid);
1087                 return -ENODEV;
1088         }
1089
1090         dev_info(&client->dev, "%s chip ID %04x\n", devname, chipid);
1091
1092         return 0;
1093 }
1094
1095 static int mt9t112_probe(struct i2c_client *client,
1096                          const struct i2c_device_id *did)
1097 {
1098         struct mt9t112_priv        *priv;
1099         struct soc_camera_device   *icd = client->dev.platform_data;
1100         struct soc_camera_link     *icl;
1101         int                         ret;
1102
1103         if (!icd) {
1104                 dev_err(&client->dev, "mt9t112: missing soc-camera data!\n");
1105                 return -EINVAL;
1106         }
1107
1108         icl = to_soc_camera_link(icd);
1109         if (!icl || !icl->priv)
1110                 return -EINVAL;
1111
1112         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1113         if (!priv)
1114                 return -ENOMEM;
1115
1116         priv->info = icl->priv;
1117
1118         v4l2_i2c_subdev_init(&priv->subdev, client, &mt9t112_subdev_ops);
1119
1120         icd->ops = &mt9t112_ops;
1121
1122         ret = mt9t112_camera_probe(icd, client);
1123         if (ret) {
1124                 icd->ops = NULL;
1125                 kfree(priv);
1126         }
1127
1128         return ret;
1129 }
1130
1131 static int mt9t112_remove(struct i2c_client *client)
1132 {
1133         struct mt9t112_priv *priv = to_mt9t112(client);
1134         struct soc_camera_device *icd = client->dev.platform_data;
1135
1136         icd->ops = NULL;
1137         kfree(priv);
1138         return 0;
1139 }
1140
1141 static const struct i2c_device_id mt9t112_id[] = {
1142         { "mt9t112", 0 },
1143         { }
1144 };
1145 MODULE_DEVICE_TABLE(i2c, mt9t112_id);
1146
1147 static struct i2c_driver mt9t112_i2c_driver = {
1148         .driver = {
1149                 .name = "mt9t112",
1150         },
1151         .probe    = mt9t112_probe,
1152         .remove   = mt9t112_remove,
1153         .id_table = mt9t112_id,
1154 };
1155
1156 /************************************************************************
1157                         module function
1158 ************************************************************************/
1159 static int __init mt9t112_module_init(void)
1160 {
1161         return i2c_add_driver(&mt9t112_i2c_driver);
1162 }
1163
1164 static void __exit mt9t112_module_exit(void)
1165 {
1166         i2c_del_driver(&mt9t112_i2c_driver);
1167 }
1168
1169 module_init(mt9t112_module_init);
1170 module_exit(mt9t112_module_exit);
1171
1172 MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
1173 MODULE_AUTHOR("Kuninori Morimoto");
1174 MODULE_LICENSE("GPL v2");