2 * mt9t112 Camera Driver
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
7 * Based on ov772x driver, mt9m111 driver,
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>
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.
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/v4l2-mediabus.h>
26 #include <linux/videodev2.h>
28 #include <media/i2c/mt9t112.h>
29 #include <media/soc_camera.h>
30 #include <media/v4l2-clk.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-image-sizes.h>
34 /* you can check PLL/clock info */
35 /* #define EXT_CLOCK 24000000 */
37 /************************************************************************
39 ************************************************************************/
43 #define MAX_WIDTH 2048
44 #define MAX_HEIGHT 1536
49 #define ECHECKER(ret, x) \
56 #define mt9t112_reg_write(ret, client, a, b) \
57 ECHECKER(ret, __mt9t112_reg_write(client, a, b))
58 #define mt9t112_mcu_write(ret, client, a, b) \
59 ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
61 #define mt9t112_reg_mask_set(ret, client, a, b, c) \
62 ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
63 #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
64 ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
66 #define mt9t112_reg_read(ret, client, a) \
67 ECHECKER(ret, __mt9t112_reg_read(client, a))
72 #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
73 #define VAR(id, offset) _VAR(id, offset, 0x0000)
74 #define VAR8(id, offset) _VAR(id, offset, 0x8000)
76 /************************************************************************
78 ************************************************************************/
79 struct mt9t112_format
{
81 enum v4l2_colorspace colorspace
;
87 struct v4l2_subdev subdev
;
88 struct mt9t112_platform_data
*info
;
89 struct i2c_client
*client
;
90 struct v4l2_rect frame
;
92 const struct mt9t112_format
*format
;
96 #define INIT_DONE (1 << 0)
97 #define PCLK_RISING (1 << 1)
100 /************************************************************************
102 ************************************************************************/
104 static const struct mt9t112_format mt9t112_cfmts
[] = {
106 .code
= MEDIA_BUS_FMT_UYVY8_2X8
,
107 .colorspace
= V4L2_COLORSPACE_SRGB
,
111 .code
= MEDIA_BUS_FMT_VYUY8_2X8
,
112 .colorspace
= V4L2_COLORSPACE_SRGB
,
116 .code
= MEDIA_BUS_FMT_YUYV8_2X8
,
117 .colorspace
= V4L2_COLORSPACE_SRGB
,
121 .code
= MEDIA_BUS_FMT_YVYU8_2X8
,
122 .colorspace
= V4L2_COLORSPACE_SRGB
,
126 .code
= MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE
,
127 .colorspace
= V4L2_COLORSPACE_SRGB
,
131 .code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
132 .colorspace
= V4L2_COLORSPACE_SRGB
,
138 /************************************************************************
140 ************************************************************************/
141 static struct mt9t112_priv
*to_mt9t112(const struct i2c_client
*client
)
143 return container_of(i2c_get_clientdata(client
),
148 static int __mt9t112_reg_read(const struct i2c_client
*client
, u16 command
)
150 struct i2c_msg msg
[2];
154 command
= swab16(command
);
156 msg
[0].addr
= client
->addr
;
159 msg
[0].buf
= (u8
*)&command
;
161 msg
[1].addr
= client
->addr
;
162 msg
[1].flags
= I2C_M_RD
;
167 * if return value of this function is < 0,
169 * else, under 16bit is valid data.
171 ret
= i2c_transfer(client
->adapter
, msg
, 2);
175 memcpy(&ret
, buf
, 2);
179 static int __mt9t112_reg_write(const struct i2c_client
*client
,
180 u16 command
, u16 data
)
186 command
= swab16(command
);
189 memcpy(buf
+ 0, &command
, 2);
190 memcpy(buf
+ 2, &data
, 2);
192 msg
.addr
= client
->addr
;
198 * i2c_transfer return message length,
199 * but this function should return 0 if correct case
201 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
208 static int __mt9t112_reg_mask_set(const struct i2c_client
*client
,
213 int val
= __mt9t112_reg_read(client
, command
);
220 return __mt9t112_reg_write(client
, command
, val
);
224 static int __mt9t112_mcu_read(const struct i2c_client
*client
, u16 command
)
228 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
232 return __mt9t112_reg_read(client
, 0x0990);
235 static int __mt9t112_mcu_write(const struct i2c_client
*client
,
236 u16 command
, u16 data
)
240 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
244 return __mt9t112_reg_write(client
, 0x0990, data
);
247 static int __mt9t112_mcu_mask_set(const struct i2c_client
*client
,
252 int val
= __mt9t112_mcu_read(client
, command
);
259 return __mt9t112_mcu_write(client
, command
, val
);
262 static int mt9t112_reset(const struct i2c_client
*client
)
266 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0001);
268 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0000);
274 #define CLOCK_INFO(a, b)
276 #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
277 static int mt9t112_clock_info(const struct i2c_client
*client
, u32 ext
)
279 int m
, n
, p1
, p2
, p3
, p4
, p5
, p6
, p7
;
283 ext
/= 1000; /* kbyte order */
285 mt9t112_reg_read(n
, client
, 0x0012);
292 mt9t112_reg_read(n
, client
, 0x002a);
299 mt9t112_reg_read(n
, client
, 0x002c);
302 mt9t112_reg_read(n
, client
, 0x0010);
304 n
= (n
>> 8) & 0x003f;
306 enable
= ((6000 > ext
) || (54000 < ext
)) ? "X" : "";
307 dev_dbg(&client
->dev
, "EXTCLK : %10u K %s\n", ext
, enable
);
309 vco
= 2 * m
* ext
/ (n
+1);
310 enable
= ((384000 > vco
) || (768000 < vco
)) ? "X" : "";
311 dev_dbg(&client
->dev
, "VCO : %10u K %s\n", vco
, enable
);
313 clk
= vco
/ (p1
+1) / (p2
+1);
314 enable
= (96000 < clk
) ? "X" : "";
315 dev_dbg(&client
->dev
, "PIXCLK : %10u K %s\n", clk
, enable
);
318 enable
= (768000 < clk
) ? "X" : "";
319 dev_dbg(&client
->dev
, "MIPICLK : %10u K %s\n", clk
, enable
);
322 enable
= (96000 < clk
) ? "X" : "";
323 dev_dbg(&client
->dev
, "MCU CLK : %10u K %s\n", clk
, enable
);
326 enable
= (54000 < clk
) ? "X" : "";
327 dev_dbg(&client
->dev
, "SOC CLK : %10u K %s\n", clk
, enable
);
330 enable
= (70000 < clk
) ? "X" : "";
331 dev_dbg(&client
->dev
, "Sensor CLK : %10u K %s\n", clk
, enable
);
334 dev_dbg(&client
->dev
, "External sensor : %10u K\n", clk
);
337 enable
= ((2000 > clk
) || (24000 < clk
)) ? "X" : "";
338 dev_dbg(&client
->dev
, "PFD : %10u K %s\n", clk
, enable
);
344 static void mt9t112_frame_check(u32
*width
, u32
*height
, u32
*left
, u32
*top
)
346 soc_camera_limit_side(left
, width
, 0, 0, MAX_WIDTH
);
347 soc_camera_limit_side(top
, height
, 0, 0, MAX_HEIGHT
);
350 static int mt9t112_set_a_frame_size(const struct i2c_client
*client
,
355 u16 wstart
= (MAX_WIDTH
- width
) / 2;
356 u16 hstart
= (MAX_HEIGHT
- height
) / 2;
358 /* (Context A) Image Width/Height */
359 mt9t112_mcu_write(ret
, client
, VAR(26, 0), width
);
360 mt9t112_mcu_write(ret
, client
, VAR(26, 2), height
);
362 /* (Context A) Output Width/Height */
363 mt9t112_mcu_write(ret
, client
, VAR(18, 43), 8 + width
);
364 mt9t112_mcu_write(ret
, client
, VAR(18, 45), 8 + height
);
366 /* (Context A) Start Row/Column */
367 mt9t112_mcu_write(ret
, client
, VAR(18, 2), 4 + hstart
);
368 mt9t112_mcu_write(ret
, client
, VAR(18, 4), 4 + wstart
);
370 /* (Context A) End Row/Column */
371 mt9t112_mcu_write(ret
, client
, VAR(18, 6), 11 + height
+ hstart
);
372 mt9t112_mcu_write(ret
, client
, VAR(18, 8), 11 + width
+ wstart
);
374 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
379 static int mt9t112_set_pll_dividers(const struct i2c_client
*client
,
391 mt9t112_reg_mask_set(ret
, client
, 0x0010, 0x3fff, val
);
394 val
= ((p3
& 0x0F) << 8) |
397 mt9t112_reg_mask_set(ret
, client
, 0x0012, 0x0fff, val
);
404 mt9t112_reg_mask_set(ret
, client
, 0x002A, 0x7fff, val
);
409 mt9t112_reg_mask_set(ret
, client
, 0x002C, 0x100f, val
);
414 static int mt9t112_init_pll(const struct i2c_client
*client
)
416 struct mt9t112_priv
*priv
= to_mt9t112(client
);
419 mt9t112_reg_mask_set(ret
, client
, 0x0014, 0x003, 0x0001);
421 /* PLL control: BYPASS PLL = 8517 */
422 mt9t112_reg_write(ret
, client
, 0x0014, 0x2145);
424 /* Replace these registers when new timing parameters are generated */
425 mt9t112_set_pll_dividers(client
,
426 priv
->info
->divider
.m
,
427 priv
->info
->divider
.n
,
428 priv
->info
->divider
.p1
,
429 priv
->info
->divider
.p2
,
430 priv
->info
->divider
.p3
,
431 priv
->info
->divider
.p4
,
432 priv
->info
->divider
.p5
,
433 priv
->info
->divider
.p6
,
434 priv
->info
->divider
.p7
);
442 mt9t112_reg_write(ret
, client
, 0x0014, 0x2525);
443 mt9t112_reg_write(ret
, client
, 0x0014, 0x2527);
444 mt9t112_reg_write(ret
, client
, 0x0014, 0x3427);
445 mt9t112_reg_write(ret
, client
, 0x0014, 0x3027);
451 * Reference clock count
452 * I2C Master Clock Divider
454 mt9t112_reg_write(ret
, client
, 0x0014, 0x3046);
455 mt9t112_reg_write(ret
, client
, 0x0016, 0x0400); /* JPEG initialization workaround */
456 mt9t112_reg_write(ret
, client
, 0x0022, 0x0190);
457 mt9t112_reg_write(ret
, client
, 0x3B84, 0x0212);
459 /* External sensor clock is PLL bypass */
460 mt9t112_reg_write(ret
, client
, 0x002E, 0x0500);
462 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0002, 0x0002);
463 mt9t112_reg_mask_set(ret
, client
, 0x3B82, 0x0004, 0x0004);
466 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0x0004);
469 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0001, 0);
475 * Disable Secondary I2C Pads
477 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
479 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
481 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
483 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
485 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
487 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
490 /* poll to verify out of standby. Must Poll this bit */
491 for (i
= 0; i
< 100; i
++) {
492 mt9t112_reg_read(data
, client
, 0x0018);
493 if (!(0x4000 & data
))
502 static int mt9t112_init_setting(const struct i2c_client
*client
)
507 /* Adaptive Output Clock (A) */
508 mt9t112_mcu_mask_set(ret
, client
, VAR(26, 160), 0x0040, 0x0000);
511 mt9t112_mcu_write(ret
, client
, VAR(18, 12), 0x0024);
513 /* Fine Correction (A) */
514 mt9t112_mcu_write(ret
, client
, VAR(18, 15), 0x00CC);
516 /* Fine IT Min (A) */
517 mt9t112_mcu_write(ret
, client
, VAR(18, 17), 0x01f1);
519 /* Fine IT Max Margin (A) */
520 mt9t112_mcu_write(ret
, client
, VAR(18, 19), 0x00fF);
522 /* Base Frame Lines (A) */
523 mt9t112_mcu_write(ret
, client
, VAR(18, 29), 0x032D);
525 /* Min Line Length (A) */
526 mt9t112_mcu_write(ret
, client
, VAR(18, 31), 0x073a);
528 /* Line Length (A) */
529 mt9t112_mcu_write(ret
, client
, VAR(18, 37), 0x07d0);
531 /* Adaptive Output Clock (B) */
532 mt9t112_mcu_mask_set(ret
, client
, VAR(27, 160), 0x0040, 0x0000);
535 mt9t112_mcu_write(ret
, client
, VAR(18, 74), 0x004);
537 /* Column Start (B) */
538 mt9t112_mcu_write(ret
, client
, VAR(18, 76), 0x004);
541 mt9t112_mcu_write(ret
, client
, VAR(18, 78), 0x60B);
544 mt9t112_mcu_write(ret
, client
, VAR(18, 80), 0x80B);
546 /* Fine Correction (B) */
547 mt9t112_mcu_write(ret
, client
, VAR(18, 87), 0x008C);
549 /* Fine IT Min (B) */
550 mt9t112_mcu_write(ret
, client
, VAR(18, 89), 0x01F1);
552 /* Fine IT Max Margin (B) */
553 mt9t112_mcu_write(ret
, client
, VAR(18, 91), 0x00FF);
555 /* Base Frame Lines (B) */
556 mt9t112_mcu_write(ret
, client
, VAR(18, 101), 0x0668);
558 /* Min Line Length (B) */
559 mt9t112_mcu_write(ret
, client
, VAR(18, 103), 0x0AF0);
561 /* Line Length (B) */
562 mt9t112_mcu_write(ret
, client
, VAR(18, 109), 0x0AF0);
565 * Flicker Dectection registers
566 * This section should be replaced whenever new Timing file is generated
567 * All the following registers need to be replaced
568 * Following registers are generated from Register Wizard but user can
569 * modify them. For detail see auto flicker detection tuning
572 /* FD_FDPERIOD_SELECT */
573 mt9t112_mcu_write(ret
, client
, VAR8(8, 5), 0x01);
575 /* PRI_B_CONFIG_FD_ALGO_RUN */
576 mt9t112_mcu_write(ret
, client
, VAR(27, 17), 0x0003);
578 /* PRI_A_CONFIG_FD_ALGO_RUN */
579 mt9t112_mcu_write(ret
, client
, VAR(26, 17), 0x0003);
582 * AFD range detection tuning registers
586 mt9t112_mcu_write(ret
, client
, VAR8(18, 165), 0x25);
589 mt9t112_mcu_write(ret
, client
, VAR8(18, 166), 0x28);
592 mt9t112_mcu_write(ret
, client
, VAR8(18, 167), 0x2C);
595 mt9t112_mcu_write(ret
, client
, VAR8(18, 168), 0x2F);
597 /* period_50Hz (A) */
598 mt9t112_mcu_write(ret
, client
, VAR8(18, 68), 0xBA);
600 /* secret register by aptina */
601 /* period_50Hz (A MSB) */
602 mt9t112_mcu_write(ret
, client
, VAR8(18, 303), 0x00);
604 /* period_60Hz (A) */
605 mt9t112_mcu_write(ret
, client
, VAR8(18, 69), 0x9B);
607 /* secret register by aptina */
608 /* period_60Hz (A MSB) */
609 mt9t112_mcu_write(ret
, client
, VAR8(18, 301), 0x00);
611 /* period_50Hz (B) */
612 mt9t112_mcu_write(ret
, client
, VAR8(18, 140), 0x82);
614 /* secret register by aptina */
615 /* period_50Hz (B) MSB */
616 mt9t112_mcu_write(ret
, client
, VAR8(18, 304), 0x00);
618 /* period_60Hz (B) */
619 mt9t112_mcu_write(ret
, client
, VAR8(18, 141), 0x6D);
621 /* secret register by aptina */
622 /* period_60Hz (B) MSB */
623 mt9t112_mcu_write(ret
, client
, VAR8(18, 302), 0x00);
626 mt9t112_mcu_write(ret
, client
, VAR8(8, 2), 0x10);
629 mt9t112_mcu_write(ret
, client
, VAR8(8, 9), 0x02);
632 mt9t112_mcu_write(ret
, client
, VAR8(8, 10), 0x03);
635 mt9t112_mcu_write(ret
, client
, VAR8(8, 12), 0x0A);
637 /* RX FIFO Watermark (A) */
638 mt9t112_mcu_write(ret
, client
, VAR(18, 70), 0x0014);
640 /* RX FIFO Watermark (B) */
641 mt9t112_mcu_write(ret
, client
, VAR(18, 142), 0x0014);
645 * CorePixCLK: 36.5 MHz
647 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0044), 133);
648 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0045), 110);
649 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008c), 130);
650 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008d), 108);
652 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00A5), 27);
653 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a6), 30);
654 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a7), 32);
655 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a8), 35);
660 static int mt9t112_auto_focus_setting(const struct i2c_client
*client
)
664 mt9t112_mcu_write(ret
, client
, VAR(12, 13), 0x000F);
665 mt9t112_mcu_write(ret
, client
, VAR(12, 23), 0x0F0F);
666 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
668 mt9t112_reg_write(ret
, client
, 0x0614, 0x0000);
670 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
671 mt9t112_mcu_write(ret
, client
, VAR8(12, 2), 0x02);
672 mt9t112_mcu_write(ret
, client
, VAR(12, 3), 0x0002);
673 mt9t112_mcu_write(ret
, client
, VAR(17, 3), 0x8001);
674 mt9t112_mcu_write(ret
, client
, VAR(17, 11), 0x0025);
675 mt9t112_mcu_write(ret
, client
, VAR(17, 13), 0x0193);
676 mt9t112_mcu_write(ret
, client
, VAR8(17, 33), 0x18);
677 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
682 static int mt9t112_auto_focus_trigger(const struct i2c_client
*client
)
686 mt9t112_mcu_write(ret
, client
, VAR8(12, 25), 0x01);
691 static int mt9t112_init_camera(const struct i2c_client
*client
)
695 ECHECKER(ret
, mt9t112_reset(client
));
697 ECHECKER(ret
, mt9t112_init_pll(client
));
699 ECHECKER(ret
, mt9t112_init_setting(client
));
701 ECHECKER(ret
, mt9t112_auto_focus_setting(client
));
703 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0);
705 /* Analog setting B */
706 mt9t112_reg_write(ret
, client
, 0x3084, 0x2409);
707 mt9t112_reg_write(ret
, client
, 0x3092, 0x0A49);
708 mt9t112_reg_write(ret
, client
, 0x3094, 0x4949);
709 mt9t112_reg_write(ret
, client
, 0x3096, 0x4950);
712 * Disable adaptive clock
713 * PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR
714 * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
716 mt9t112_mcu_write(ret
, client
, VAR(26, 160), 0x0A2E);
717 mt9t112_mcu_write(ret
, client
, VAR(27, 160), 0x0A2E);
719 /* Configure STatus in Status_before_length Format and enable header */
720 /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
721 mt9t112_mcu_write(ret
, client
, VAR(27, 144), 0x0CB4);
723 /* Enable JPEG in context B */
724 /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
725 mt9t112_mcu_write(ret
, client
, VAR8(27, 142), 0x01);
727 /* Disable Dac_TXLO */
728 mt9t112_reg_write(ret
, client
, 0x316C, 0x350F);
730 /* Set max slew rates */
731 mt9t112_reg_write(ret
, client
, 0x1E, 0x777);
736 /************************************************************************
738 ************************************************************************/
740 #ifdef CONFIG_VIDEO_ADV_DEBUG
741 static int mt9t112_g_register(struct v4l2_subdev
*sd
,
742 struct v4l2_dbg_register
*reg
)
744 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
748 mt9t112_reg_read(ret
, client
, reg
->reg
);
750 reg
->val
= (__u64
)ret
;
755 static int mt9t112_s_register(struct v4l2_subdev
*sd
,
756 const struct v4l2_dbg_register
*reg
)
758 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
761 mt9t112_reg_write(ret
, client
, reg
->reg
, reg
->val
);
767 static int mt9t112_s_power(struct v4l2_subdev
*sd
, int on
)
769 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
770 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
771 struct mt9t112_priv
*priv
= to_mt9t112(client
);
773 return soc_camera_set_power(&client
->dev
, ssdd
, priv
->clk
, on
);
776 static const struct v4l2_subdev_core_ops mt9t112_subdev_core_ops
= {
777 #ifdef CONFIG_VIDEO_ADV_DEBUG
778 .g_register
= mt9t112_g_register
,
779 .s_register
= mt9t112_s_register
,
781 .s_power
= mt9t112_s_power
,
785 /************************************************************************
786 v4l2_subdev_video_ops
787 ************************************************************************/
788 static int mt9t112_s_stream(struct v4l2_subdev
*sd
, int enable
)
790 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
791 struct mt9t112_priv
*priv
= to_mt9t112(client
);
797 * If user selected large output size,
798 * and used it long time,
799 * mt9t112 camera will be very warm.
801 * But current driver can not stop mt9t112 camera.
802 * So, set small size here to solve this problem.
804 mt9t112_set_a_frame_size(client
, VGA_WIDTH
, VGA_HEIGHT
);
808 if (!(priv
->flags
& INIT_DONE
)) {
809 u16 param
= PCLK_RISING
& priv
->flags
? 0x0001 : 0x0000;
811 ECHECKER(ret
, mt9t112_init_camera(client
));
813 /* Invert PCLK (Data sampled on falling edge of pixclk) */
814 mt9t112_reg_write(ret
, client
, 0x3C20, param
);
818 priv
->flags
|= INIT_DONE
;
821 mt9t112_mcu_write(ret
, client
, VAR(26, 7), priv
->format
->fmt
);
822 mt9t112_mcu_write(ret
, client
, VAR(26, 9), priv
->format
->order
);
823 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
825 mt9t112_set_a_frame_size(client
,
829 ECHECKER(ret
, mt9t112_auto_focus_trigger(client
));
831 dev_dbg(&client
->dev
, "format : %d\n", priv
->format
->code
);
832 dev_dbg(&client
->dev
, "size : %d x %d\n",
836 CLOCK_INFO(client
, EXT_CLOCK
);
841 static int mt9t112_set_params(struct mt9t112_priv
*priv
,
842 const struct v4l2_rect
*rect
,
850 for (i
= 0; i
< priv
->num_formats
; i
++)
851 if (mt9t112_cfmts
[i
].code
== code
)
854 if (i
== priv
->num_formats
)
862 mt9t112_frame_check(&priv
->frame
.width
, &priv
->frame
.height
,
863 &priv
->frame
.left
, &priv
->frame
.top
);
865 priv
->format
= mt9t112_cfmts
+ i
;
870 static int mt9t112_get_selection(struct v4l2_subdev
*sd
,
871 struct v4l2_subdev_pad_config
*cfg
,
872 struct v4l2_subdev_selection
*sel
)
874 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
875 struct mt9t112_priv
*priv
= to_mt9t112(client
);
877 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
880 switch (sel
->target
) {
881 case V4L2_SEL_TGT_CROP_BOUNDS
:
884 sel
->r
.width
= MAX_WIDTH
;
885 sel
->r
.height
= MAX_HEIGHT
;
887 case V4L2_SEL_TGT_CROP_DEFAULT
:
890 sel
->r
.width
= VGA_WIDTH
;
891 sel
->r
.height
= VGA_HEIGHT
;
893 case V4L2_SEL_TGT_CROP
:
894 sel
->r
= priv
->frame
;
901 static int mt9t112_set_selection(struct v4l2_subdev
*sd
,
902 struct v4l2_subdev_pad_config
*cfg
,
903 struct v4l2_subdev_selection
*sel
)
905 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
906 struct mt9t112_priv
*priv
= to_mt9t112(client
);
907 const struct v4l2_rect
*rect
= &sel
->r
;
909 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
||
910 sel
->target
!= V4L2_SEL_TGT_CROP
)
913 return mt9t112_set_params(priv
, rect
, priv
->format
->code
);
916 static int mt9t112_get_fmt(struct v4l2_subdev
*sd
,
917 struct v4l2_subdev_pad_config
*cfg
,
918 struct v4l2_subdev_format
*format
)
920 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
921 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
922 struct mt9t112_priv
*priv
= to_mt9t112(client
);
927 mf
->width
= priv
->frame
.width
;
928 mf
->height
= priv
->frame
.height
;
929 mf
->colorspace
= priv
->format
->colorspace
;
930 mf
->code
= priv
->format
->code
;
931 mf
->field
= V4L2_FIELD_NONE
;
936 static int mt9t112_s_fmt(struct v4l2_subdev
*sd
,
937 struct v4l2_mbus_framefmt
*mf
)
939 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
940 struct mt9t112_priv
*priv
= to_mt9t112(client
);
941 struct v4l2_rect rect
= {
943 .height
= mf
->height
,
944 .left
= priv
->frame
.left
,
945 .top
= priv
->frame
.top
,
949 ret
= mt9t112_set_params(priv
, &rect
, mf
->code
);
952 mf
->colorspace
= priv
->format
->colorspace
;
957 static int mt9t112_set_fmt(struct v4l2_subdev
*sd
,
958 struct v4l2_subdev_pad_config
*cfg
,
959 struct v4l2_subdev_format
*format
)
961 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
962 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
963 struct mt9t112_priv
*priv
= to_mt9t112(client
);
964 unsigned int top
, left
;
970 for (i
= 0; i
< priv
->num_formats
; i
++)
971 if (mt9t112_cfmts
[i
].code
== mf
->code
)
974 if (i
== priv
->num_formats
) {
975 mf
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
976 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
978 mf
->colorspace
= mt9t112_cfmts
[i
].colorspace
;
981 mt9t112_frame_check(&mf
->width
, &mf
->height
, &left
, &top
);
983 mf
->field
= V4L2_FIELD_NONE
;
985 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
986 return mt9t112_s_fmt(sd
, mf
);
991 static int mt9t112_enum_mbus_code(struct v4l2_subdev
*sd
,
992 struct v4l2_subdev_pad_config
*cfg
,
993 struct v4l2_subdev_mbus_code_enum
*code
)
995 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
996 struct mt9t112_priv
*priv
= to_mt9t112(client
);
998 if (code
->pad
|| code
->index
>= priv
->num_formats
)
1001 code
->code
= mt9t112_cfmts
[code
->index
].code
;
1006 static int mt9t112_g_mbus_config(struct v4l2_subdev
*sd
,
1007 struct v4l2_mbus_config
*cfg
)
1009 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1010 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1012 cfg
->flags
= V4L2_MBUS_MASTER
| V4L2_MBUS_VSYNC_ACTIVE_HIGH
|
1013 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_DATA_ACTIVE_HIGH
|
1014 V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
;
1015 cfg
->type
= V4L2_MBUS_PARALLEL
;
1016 cfg
->flags
= soc_camera_apply_board_flags(ssdd
, cfg
);
1021 static int mt9t112_s_mbus_config(struct v4l2_subdev
*sd
,
1022 const struct v4l2_mbus_config
*cfg
)
1024 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1025 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1026 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1028 if (soc_camera_apply_board_flags(ssdd
, cfg
) & V4L2_MBUS_PCLK_SAMPLE_RISING
)
1029 priv
->flags
|= PCLK_RISING
;
1034 static const struct v4l2_subdev_video_ops mt9t112_subdev_video_ops
= {
1035 .s_stream
= mt9t112_s_stream
,
1036 .g_mbus_config
= mt9t112_g_mbus_config
,
1037 .s_mbus_config
= mt9t112_s_mbus_config
,
1040 static const struct v4l2_subdev_pad_ops mt9t112_subdev_pad_ops
= {
1041 .enum_mbus_code
= mt9t112_enum_mbus_code
,
1042 .get_selection
= mt9t112_get_selection
,
1043 .set_selection
= mt9t112_set_selection
,
1044 .get_fmt
= mt9t112_get_fmt
,
1045 .set_fmt
= mt9t112_set_fmt
,
1048 /************************************************************************
1050 ************************************************************************/
1051 static const struct v4l2_subdev_ops mt9t112_subdev_ops
= {
1052 .core
= &mt9t112_subdev_core_ops
,
1053 .video
= &mt9t112_subdev_video_ops
,
1054 .pad
= &mt9t112_subdev_pad_ops
,
1057 static int mt9t112_camera_probe(struct i2c_client
*client
)
1059 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1060 const char *devname
;
1064 ret
= mt9t112_s_power(&priv
->subdev
, 1);
1069 * check and show chip ID
1071 mt9t112_reg_read(chipid
, client
, 0x0000);
1075 devname
= "mt9t111";
1076 priv
->num_formats
= 1;
1079 devname
= "mt9t112";
1080 priv
->num_formats
= ARRAY_SIZE(mt9t112_cfmts
);
1083 dev_err(&client
->dev
, "Product ID error %04x\n", chipid
);
1088 dev_info(&client
->dev
, "%s chip ID %04x\n", devname
, chipid
);
1091 mt9t112_s_power(&priv
->subdev
, 0);
1095 static int mt9t112_probe(struct i2c_client
*client
,
1096 const struct i2c_device_id
*did
)
1098 struct mt9t112_priv
*priv
;
1099 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1100 struct v4l2_rect rect
= {
1102 .height
= VGA_HEIGHT
,
1103 .left
= (MAX_WIDTH
- VGA_WIDTH
) / 2,
1104 .top
= (MAX_HEIGHT
- VGA_HEIGHT
) / 2,
1108 if (!ssdd
|| !ssdd
->drv_priv
) {
1109 dev_err(&client
->dev
, "mt9t112: missing platform data!\n");
1113 priv
= devm_kzalloc(&client
->dev
, sizeof(*priv
), GFP_KERNEL
);
1117 priv
->info
= ssdd
->drv_priv
;
1119 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &mt9t112_subdev_ops
);
1121 priv
->clk
= v4l2_clk_get(&client
->dev
, "mclk");
1122 if (IS_ERR(priv
->clk
))
1123 return PTR_ERR(priv
->clk
);
1125 ret
= mt9t112_camera_probe(client
);
1127 /* Cannot fail: using the default supported pixel code */
1129 mt9t112_set_params(priv
, &rect
, MEDIA_BUS_FMT_UYVY8_2X8
);
1131 v4l2_clk_put(priv
->clk
);
1136 static int mt9t112_remove(struct i2c_client
*client
)
1138 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1140 v4l2_clk_put(priv
->clk
);
1144 static const struct i2c_device_id mt9t112_id
[] = {
1148 MODULE_DEVICE_TABLE(i2c
, mt9t112_id
);
1150 static struct i2c_driver mt9t112_i2c_driver
= {
1154 .probe
= mt9t112_probe
,
1155 .remove
= mt9t112_remove
,
1156 .id_table
= mt9t112_id
,
1159 module_i2c_driver(mt9t112_i2c_driver
);
1161 MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
1162 MODULE_AUTHOR("Kuninori Morimoto");
1163 MODULE_LICENSE("GPL v2");