blob: 608a3b6a8e99206088ebd79df1ee50f3cbb8392a [file] [log] [blame]
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001/*
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/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -030037 macro
Kuninori Morimoto858424b2009-12-11 11:53:55 -030038************************************************************************/
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/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -030079 struct
Kuninori Morimoto858424b2009-12-11 11:53:55 -030080************************************************************************/
81struct mt9t112_frame_size {
82 u16 width;
83 u16 height;
84};
85
86struct mt9t112_format {
87 enum v4l2_mbus_pixelcode code;
88 enum v4l2_colorspace colorspace;
89 u16 fmt;
90 u16 order;
91};
92
93struct 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 */
Guennadi Liakhovetskid46ebd42011-07-26 11:52:42 -0300103#define INIT_DONE (1 << 0)
104#define PCLK_RISING (1 << 1)
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300105};
106
107/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300108 supported format
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300109************************************************************************/
110
111static const struct mt9t112_format mt9t112_cfmts[] = {
112 {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300113 .code = V4L2_MBUS_FMT_UYVY8_2X8,
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300114 .colorspace = V4L2_COLORSPACE_JPEG,
115 .fmt = 1,
116 .order = 0,
117 }, {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300118 .code = V4L2_MBUS_FMT_VYUY8_2X8,
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300119 .colorspace = V4L2_COLORSPACE_JPEG,
120 .fmt = 1,
121 .order = 1,
122 }, {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300123 .code = V4L2_MBUS_FMT_YUYV8_2X8,
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300124 .colorspace = V4L2_COLORSPACE_JPEG,
125 .fmt = 1,
126 .order = 2,
127 }, {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300128 .code = V4L2_MBUS_FMT_YVYU8_2X8,
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300129 .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/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300146 general function
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300147************************************************************************/
148static 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
155static 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
186static 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
215static 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 */
231static 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
242static 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
254static 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
269static 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)
284static 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
351static 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
360static 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
389static 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
424static 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);
Kuninori Morimoto2b591252010-02-02 13:17:54 +0900502 if (!(0x4000 & data))
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300503 break;
504
505 mdelay(10);
506 }
507
508 return ret;
509}
510
511static 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
669static 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
691static 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
700static 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/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300746 v4l2_subdev_core_ops
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300747************************************************************************/
748static int mt9t112_g_chip_ident(struct v4l2_subdev *sd,
749 struct v4l2_dbg_chip_ident *id)
750{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300751 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300752 struct mt9t112_priv *priv = to_mt9t112(client);
753
754 id->ident = priv->model;
755 id->revision = 0;
756
757 return 0;
758}
759
760#ifdef CONFIG_VIDEO_ADV_DEBUG
761static int mt9t112_g_register(struct v4l2_subdev *sd,
762 struct v4l2_dbg_register *reg)
763{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300764 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300765 int ret;
766
767 reg->size = 2;
768 mt9t112_reg_read(ret, client, reg->reg);
769
770 reg->val = (__u64)ret;
771
772 return 0;
773}
774
775static int mt9t112_s_register(struct v4l2_subdev *sd,
776 struct v4l2_dbg_register *reg)
777{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300778 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300779 int ret;
780
781 mt9t112_reg_write(ret, client, reg->reg, reg->val);
782
783 return ret;
784}
785#endif
786
787static struct v4l2_subdev_core_ops mt9t112_subdev_core_ops = {
788 .g_chip_ident = mt9t112_g_chip_ident,
789#ifdef CONFIG_VIDEO_ADV_DEBUG
790 .g_register = mt9t112_g_register,
791 .s_register = mt9t112_s_register,
792#endif
793};
794
795
796/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300797 v4l2_subdev_video_ops
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300798************************************************************************/
799static int mt9t112_s_stream(struct v4l2_subdev *sd, int enable)
800{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300801 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300802 struct mt9t112_priv *priv = to_mt9t112(client);
803 int ret = 0;
804
805 if (!enable) {
806 /* FIXME
807 *
808 * If user selected large output size,
809 * and used it long time,
810 * mt9t112 camera will be very warm.
811 *
812 * But current driver can not stop mt9t112 camera.
813 * So, set small size here to solve this problem.
814 */
815 mt9t112_set_a_frame_size(client, VGA_WIDTH, VGA_HEIGHT);
816 return ret;
817 }
818
819 if (!(priv->flags & INIT_DONE)) {
Guennadi Liakhovetskid46ebd42011-07-26 11:52:42 -0300820 u16 param = PCLK_RISING & priv->flags ? 0x0001 : 0x0000;
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300821
822 ECHECKER(ret, mt9t112_init_camera(client));
823
824 /* Invert PCLK (Data sampled on falling edge of pixclk) */
825 mt9t112_reg_write(ret, client, 0x3C20, param);
826
827 mdelay(5);
828
829 priv->flags |= INIT_DONE;
830 }
831
832 mt9t112_mcu_write(ret, client, VAR(26, 7), priv->format->fmt);
833 mt9t112_mcu_write(ret, client, VAR(26, 9), priv->format->order);
834 mt9t112_mcu_write(ret, client, VAR8(1, 0), 0x06);
835
836 mt9t112_set_a_frame_size(client,
837 priv->frame.width,
838 priv->frame.height);
839
840 ECHECKER(ret, mt9t112_auto_focus_trigger(client));
841
842 dev_dbg(&client->dev, "format : %d\n", priv->format->code);
843 dev_dbg(&client->dev, "size : %d x %d\n",
844 priv->frame.width,
845 priv->frame.height);
846
847 CLOCK_INFO(client, EXT_CLOCK);
848
849 return ret;
850}
851
852static int mt9t112_set_params(struct i2c_client *client, u32 width, u32 height,
853 enum v4l2_mbus_pixelcode code)
854{
855 struct mt9t112_priv *priv = to_mt9t112(client);
856 int i;
857
858 priv->format = NULL;
859
860 /*
861 * frame size check
862 */
863 mt9t112_frame_check(&width, &height);
864
865 /*
866 * get color format
867 */
868 for (i = 0; i < ARRAY_SIZE(mt9t112_cfmts); i++)
869 if (mt9t112_cfmts[i].code == code)
870 break;
871
872 if (i == ARRAY_SIZE(mt9t112_cfmts))
873 return -EINVAL;
874
875 priv->frame.width = (u16)width;
876 priv->frame.height = (u16)height;
877
878 priv->format = mt9t112_cfmts + i;
879
880 return 0;
881}
882
883static int mt9t112_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
884{
885 a->bounds.left = 0;
886 a->bounds.top = 0;
887 a->bounds.width = VGA_WIDTH;
888 a->bounds.height = VGA_HEIGHT;
889 a->defrect = a->bounds;
890 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
891 a->pixelaspect.numerator = 1;
892 a->pixelaspect.denominator = 1;
893
894 return 0;
895}
896
897static int mt9t112_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
898{
899 a->c.left = 0;
900 a->c.top = 0;
901 a->c.width = VGA_WIDTH;
902 a->c.height = VGA_HEIGHT;
903 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
904
905 return 0;
906}
907
908static int mt9t112_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
909{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300910 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300911 struct v4l2_rect *rect = &a->c;
912
913 return mt9t112_set_params(client, rect->width, rect->height,
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300914 V4L2_MBUS_FMT_UYVY8_2X8);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300915}
916
917static int mt9t112_g_fmt(struct v4l2_subdev *sd,
918 struct v4l2_mbus_framefmt *mf)
919{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300920 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300921 struct mt9t112_priv *priv = to_mt9t112(client);
922
923 if (!priv->format) {
924 int ret = mt9t112_set_params(client, VGA_WIDTH, VGA_HEIGHT,
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300925 V4L2_MBUS_FMT_UYVY8_2X8);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300926 if (ret < 0)
927 return ret;
928 }
929
930 mf->width = priv->frame.width;
931 mf->height = priv->frame.height;
932 /* TODO: set colorspace */
933 mf->code = priv->format->code;
934 mf->field = V4L2_FIELD_NONE;
935
936 return 0;
937}
938
939static int mt9t112_s_fmt(struct v4l2_subdev *sd,
940 struct v4l2_mbus_framefmt *mf)
941{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300942 struct i2c_client *client = v4l2_get_subdevdata(sd);
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300943
944 /* TODO: set colorspace */
945 return mt9t112_set_params(client, mf->width, mf->height, mf->code);
946}
947
948static int mt9t112_try_fmt(struct v4l2_subdev *sd,
949 struct v4l2_mbus_framefmt *mf)
950{
951 mt9t112_frame_check(&mf->width, &mf->height);
952
953 /* TODO: set colorspace */
954 mf->field = V4L2_FIELD_NONE;
955
956 return 0;
957}
958
Hans Verkuil3805f202010-05-08 17:55:00 -0300959static int mt9t112_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300960 enum v4l2_mbus_pixelcode *code)
961{
Hans Verkuil3805f202010-05-08 17:55:00 -0300962 if (index >= ARRAY_SIZE(mt9t112_cfmts))
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300963 return -EINVAL;
964
965 *code = mt9t112_cfmts[index].code;
966 return 0;
967}
968
Guennadi Liakhovetskid46ebd42011-07-26 11:52:42 -0300969static int mt9t112_g_mbus_config(struct v4l2_subdev *sd,
970 struct v4l2_mbus_config *cfg)
971{
972 struct i2c_client *client = v4l2_get_subdevdata(sd);
973 struct soc_camera_device *icd = client->dev.platform_data;
974 struct soc_camera_link *icl = to_soc_camera_link(icd);
975
976 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
977 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH |
978 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
979 cfg->type = V4L2_MBUS_PARALLEL;
980 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
981
982 return 0;
983}
984
985static int mt9t112_s_mbus_config(struct v4l2_subdev *sd,
986 const struct v4l2_mbus_config *cfg)
987{
988 struct i2c_client *client = v4l2_get_subdevdata(sd);
989 struct soc_camera_device *icd = client->dev.platform_data;
990 struct soc_camera_link *icl = to_soc_camera_link(icd);
991 struct mt9t112_priv *priv = to_mt9t112(client);
992
993 if (soc_camera_apply_board_flags(icl, cfg) & V4L2_MBUS_PCLK_SAMPLE_RISING)
994 priv->flags |= PCLK_RISING;
995
996 return 0;
997}
998
Kuninori Morimoto858424b2009-12-11 11:53:55 -0300999static struct v4l2_subdev_video_ops mt9t112_subdev_video_ops = {
1000 .s_stream = mt9t112_s_stream,
1001 .g_mbus_fmt = mt9t112_g_fmt,
1002 .s_mbus_fmt = mt9t112_s_fmt,
1003 .try_mbus_fmt = mt9t112_try_fmt,
1004 .cropcap = mt9t112_cropcap,
1005 .g_crop = mt9t112_g_crop,
1006 .s_crop = mt9t112_s_crop,
1007 .enum_mbus_fmt = mt9t112_enum_fmt,
Guennadi Liakhovetskid46ebd42011-07-26 11:52:42 -03001008 .g_mbus_config = mt9t112_g_mbus_config,
1009 .s_mbus_config = mt9t112_s_mbus_config,
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001010};
1011
1012/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001013 i2c driver
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001014************************************************************************/
1015static struct v4l2_subdev_ops mt9t112_subdev_ops = {
1016 .core = &mt9t112_subdev_core_ops,
1017 .video = &mt9t112_subdev_video_ops,
1018};
1019
1020static int mt9t112_camera_probe(struct soc_camera_device *icd,
1021 struct i2c_client *client)
1022{
1023 struct mt9t112_priv *priv = to_mt9t112(client);
1024 const char *devname;
1025 int chipid;
1026
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -03001027 /* We must have a parent by now. And it cannot be a wrong one. */
1028 BUG_ON(!icd->parent ||
1029 to_soc_camera_host(icd->parent)->nr != icd->iface);
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001030
1031 /*
1032 * check and show chip ID
1033 */
1034 mt9t112_reg_read(chipid, client, 0x0000);
1035
1036 switch (chipid) {
1037 case 0x2680:
1038 devname = "mt9t111";
1039 priv->model = V4L2_IDENT_MT9T111;
1040 break;
1041 case 0x2682:
1042 devname = "mt9t112";
1043 priv->model = V4L2_IDENT_MT9T112;
1044 break;
1045 default:
1046 dev_err(&client->dev, "Product ID error %04x\n", chipid);
1047 return -ENODEV;
1048 }
1049
1050 dev_info(&client->dev, "%s chip ID %04x\n", devname, chipid);
1051
1052 return 0;
1053}
1054
1055static int mt9t112_probe(struct i2c_client *client,
1056 const struct i2c_device_id *did)
1057{
1058 struct mt9t112_priv *priv;
1059 struct soc_camera_device *icd = client->dev.platform_data;
1060 struct soc_camera_link *icl;
1061 int ret;
1062
1063 if (!icd) {
1064 dev_err(&client->dev, "mt9t112: missing soc-camera data!\n");
1065 return -EINVAL;
1066 }
1067
1068 icl = to_soc_camera_link(icd);
1069 if (!icl || !icl->priv)
1070 return -EINVAL;
1071
1072 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1073 if (!priv)
1074 return -ENOMEM;
1075
1076 priv->info = icl->priv;
1077
1078 v4l2_i2c_subdev_init(&priv->subdev, client, &mt9t112_subdev_ops);
1079
Guennadi Liakhovetskiff513452011-07-28 14:42:22 -03001080 icd->ops = NULL;
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001081
1082 ret = mt9t112_camera_probe(icd, client);
Guennadi Liakhovetskiff513452011-07-28 14:42:22 -03001083 if (ret)
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001084 kfree(priv);
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001085
1086 return ret;
1087}
1088
1089static int mt9t112_remove(struct i2c_client *client)
1090{
1091 struct mt9t112_priv *priv = to_mt9t112(client);
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001092
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001093 kfree(priv);
1094 return 0;
1095}
1096
1097static const struct i2c_device_id mt9t112_id[] = {
1098 { "mt9t112", 0 },
1099 { }
1100};
1101MODULE_DEVICE_TABLE(i2c, mt9t112_id);
1102
1103static struct i2c_driver mt9t112_i2c_driver = {
1104 .driver = {
1105 .name = "mt9t112",
1106 },
1107 .probe = mt9t112_probe,
1108 .remove = mt9t112_remove,
1109 .id_table = mt9t112_id,
1110};
1111
1112/************************************************************************
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001113 module function
Kuninori Morimoto858424b2009-12-11 11:53:55 -03001114************************************************************************/
1115static int __init mt9t112_module_init(void)
1116{
1117 return i2c_add_driver(&mt9t112_i2c_driver);
1118}
1119
1120static void __exit mt9t112_module_exit(void)
1121{
1122 i2c_del_driver(&mt9t112_i2c_driver);
1123}
1124
1125module_init(mt9t112_module_init);
1126module_exit(mt9t112_module_exit);
1127
1128MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
1129MODULE_AUTHOR("Kuninori Morimoto");
1130MODULE_LICENSE("GPL v2");