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