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/videodev2.h>
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>
33 /* you can check PLL/clock info */
34 /* #define EXT_CLOCK 24000000 */
36 /************************************************************************
42 ************************************************************************/
46 #define MAX_WIDTH 2048
47 #define MAX_HEIGHT 1536
50 #define VGA_HEIGHT 480
55 #define ECHECKER(ret, x) \
62 #define mt9t112_reg_write(ret, client, a, b) \
63 ECHECKER(ret, __mt9t112_reg_write(client, a, b))
64 #define mt9t112_mcu_write(ret, client, a, b) \
65 ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
67 #define mt9t112_reg_mask_set(ret, client, a, b, c) \
68 ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
69 #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
70 ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
72 #define mt9t112_reg_read(ret, client, a) \
73 ECHECKER(ret, __mt9t112_reg_read(client, a))
78 #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
79 #define VAR(id, offset) _VAR(id, offset, 0x0000)
80 #define VAR8(id, offset) _VAR(id, offset, 0x8000)
82 /************************************************************************
88 ************************************************************************/
89 struct mt9t112_frame_size
{
94 struct mt9t112_format
{
95 enum v4l2_mbus_pixelcode code
;
96 enum v4l2_colorspace colorspace
;
101 struct mt9t112_priv
{
102 struct v4l2_subdev subdev
;
103 struct mt9t112_camera_info
*info
;
104 struct i2c_client
*client
;
105 struct soc_camera_device icd
;
106 struct mt9t112_frame_size frame
;
107 const struct mt9t112_format
*format
;
111 #define INIT_DONE (1<<0)
114 /************************************************************************
120 ************************************************************************/
122 static const struct mt9t112_format mt9t112_cfmts
[] = {
124 .code
= V4L2_MBUS_FMT_YUYV8_2X8_BE
,
125 .colorspace
= V4L2_COLORSPACE_JPEG
,
129 .code
= V4L2_MBUS_FMT_YVYU8_2X8_BE
,
130 .colorspace
= V4L2_COLORSPACE_JPEG
,
134 .code
= V4L2_MBUS_FMT_YUYV8_2X8_LE
,
135 .colorspace
= V4L2_COLORSPACE_JPEG
,
139 .code
= V4L2_MBUS_FMT_YVYU8_2X8_LE
,
140 .colorspace
= V4L2_COLORSPACE_JPEG
,
144 .code
= V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE
,
145 .colorspace
= V4L2_COLORSPACE_SRGB
,
149 .code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
150 .colorspace
= V4L2_COLORSPACE_SRGB
,
156 /************************************************************************
162 ************************************************************************/
163 static struct mt9t112_priv
*to_mt9t112(const struct i2c_client
*client
)
165 return container_of(i2c_get_clientdata(client
),
170 static int __mt9t112_reg_read(const struct i2c_client
*client
, u16 command
)
172 struct i2c_msg msg
[2];
176 command
= swab16(command
);
178 msg
[0].addr
= client
->addr
;
181 msg
[0].buf
= (u8
*)&command
;
183 msg
[1].addr
= client
->addr
;
184 msg
[1].flags
= I2C_M_RD
;
189 * if return value of this function is < 0,
191 * else, under 16bit is valid data.
193 ret
= i2c_transfer(client
->adapter
, msg
, 2);
197 memcpy(&ret
, buf
, 2);
201 static int __mt9t112_reg_write(const struct i2c_client
*client
,
202 u16 command
, u16 data
)
208 command
= swab16(command
);
211 memcpy(buf
+ 0, &command
, 2);
212 memcpy(buf
+ 2, &data
, 2);
214 msg
.addr
= client
->addr
;
220 * i2c_transfer return message length,
221 * but this function should return 0 if correct case
223 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
230 static int __mt9t112_reg_mask_set(const struct i2c_client
*client
,
235 int val
= __mt9t112_reg_read(client
, command
);
242 return __mt9t112_reg_write(client
, command
, val
);
246 static int __mt9t112_mcu_read(const struct i2c_client
*client
, u16 command
)
250 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
254 return __mt9t112_reg_read(client
, 0x0990);
257 static int __mt9t112_mcu_write(const struct i2c_client
*client
,
258 u16 command
, u16 data
)
262 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
266 return __mt9t112_reg_write(client
, 0x0990, data
);
269 static int __mt9t112_mcu_mask_set(const struct i2c_client
*client
,
274 int val
= __mt9t112_mcu_read(client
, command
);
281 return __mt9t112_mcu_write(client
, command
, val
);
284 static int mt9t112_reset(const struct i2c_client
*client
)
288 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0001);
290 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0000);
296 #define CLOCK_INFO(a, b)
298 #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
299 static int mt9t112_clock_info(const struct i2c_client
*client
, u32 ext
)
301 int m
, n
, p1
, p2
, p3
, p4
, p5
, p6
, p7
;
305 ext
/= 1000; /* kbyte order */
307 mt9t112_reg_read(n
, client
, 0x0012);
314 mt9t112_reg_read(n
, client
, 0x002a);
321 mt9t112_reg_read(n
, client
, 0x002c);
324 mt9t112_reg_read(n
, client
, 0x0010);
326 n
= (n
>> 8) & 0x003f;
328 enable
= ((6000 > ext
) || (54000 < ext
)) ? "X" : "";
329 dev_info(&client
->dev
, "EXTCLK : %10u K %s\n", ext
, enable
);
331 vco
= 2 * m
* ext
/ (n
+1);
332 enable
= ((384000 > vco
) || (768000 < vco
)) ? "X" : "";
333 dev_info(&client
->dev
, "VCO : %10u K %s\n", vco
, enable
);
335 clk
= vco
/ (p1
+1) / (p2
+1);
336 enable
= (96000 < clk
) ? "X" : "";
337 dev_info(&client
->dev
, "PIXCLK : %10u K %s\n", clk
, enable
);
340 enable
= (768000 < clk
) ? "X" : "";
341 dev_info(&client
->dev
, "MIPICLK : %10u K %s\n", clk
, enable
);
344 enable
= (96000 < clk
) ? "X" : "";
345 dev_info(&client
->dev
, "MCU CLK : %10u K %s\n", clk
, enable
);
348 enable
= (54000 < clk
) ? "X" : "";
349 dev_info(&client
->dev
, "SOC CLK : %10u K %s\n", clk
, enable
);
352 enable
= (70000 < clk
) ? "X" : "";
353 dev_info(&client
->dev
, "Sensor CLK : %10u K %s\n", clk
, enable
);
356 dev_info(&client
->dev
, "External sensor : %10u K\n", clk
);
359 enable
= ((2000 > clk
) || (24000 < clk
)) ? "X" : "";
360 dev_info(&client
->dev
, "PFD : %10u K %s\n", clk
, enable
);
366 static void mt9t112_frame_check(u32
*width
, u32
*height
)
368 if (*width
> MAX_WIDTH
)
371 if (*height
> MAX_HEIGHT
)
372 *height
= MAX_HEIGHT
;
375 static int mt9t112_set_a_frame_size(const struct i2c_client
*client
,
380 u16 wstart
= (MAX_WIDTH
- width
) / 2;
381 u16 hstart
= (MAX_HEIGHT
- height
) / 2;
383 /* (Context A) Image Width/Height */
384 mt9t112_mcu_write(ret
, client
, VAR(26, 0), width
);
385 mt9t112_mcu_write(ret
, client
, VAR(26, 2), height
);
387 /* (Context A) Output Width/Height */
388 mt9t112_mcu_write(ret
, client
, VAR(18, 43), 8 + width
);
389 mt9t112_mcu_write(ret
, client
, VAR(18, 45), 8 + height
);
391 /* (Context A) Start Row/Column */
392 mt9t112_mcu_write(ret
, client
, VAR(18, 2), 4 + hstart
);
393 mt9t112_mcu_write(ret
, client
, VAR(18, 4), 4 + wstart
);
395 /* (Context A) End Row/Column */
396 mt9t112_mcu_write(ret
, client
, VAR(18, 6), 11 + height
+ hstart
);
397 mt9t112_mcu_write(ret
, client
, VAR(18, 8), 11 + width
+ wstart
);
399 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
404 static int mt9t112_set_pll_dividers(const struct i2c_client
*client
,
416 mt9t112_reg_mask_set(ret
, client
, 0x0010, 0x3fff, val
);
419 val
= ((p3
& 0x0F) << 8) |
422 mt9t112_reg_mask_set(ret
, client
, 0x0012, 0x0fff, val
);
429 mt9t112_reg_mask_set(ret
, client
, 0x002A, 0x7fff, val
);
434 mt9t112_reg_mask_set(ret
, client
, 0x002C, 0x100f, val
);
439 static int mt9t112_init_pll(const struct i2c_client
*client
)
441 struct mt9t112_priv
*priv
= to_mt9t112(client
);
444 mt9t112_reg_mask_set(ret
, client
, 0x0014, 0x003, 0x0001);
446 /* PLL control: BYPASS PLL = 8517 */
447 mt9t112_reg_write(ret
, client
, 0x0014, 0x2145);
449 /* Replace these registers when new timing parameters are generated */
450 mt9t112_set_pll_dividers(client
,
451 priv
->info
->divider
.m
,
452 priv
->info
->divider
.n
,
453 priv
->info
->divider
.p1
,
454 priv
->info
->divider
.p2
,
455 priv
->info
->divider
.p3
,
456 priv
->info
->divider
.p4
,
457 priv
->info
->divider
.p5
,
458 priv
->info
->divider
.p6
,
459 priv
->info
->divider
.p7
);
467 mt9t112_reg_write(ret
, client
, 0x0014, 0x2525);
468 mt9t112_reg_write(ret
, client
, 0x0014, 0x2527);
469 mt9t112_reg_write(ret
, client
, 0x0014, 0x3427);
470 mt9t112_reg_write(ret
, client
, 0x0014, 0x3027);
476 * Reference clock count
477 * I2C Master Clock Divider
479 mt9t112_reg_write(ret
, client
, 0x0014, 0x3046);
480 mt9t112_reg_write(ret
, client
, 0x0022, 0x0190);
481 mt9t112_reg_write(ret
, client
, 0x3B84, 0x0212);
483 /* External sensor clock is PLL bypass */
484 mt9t112_reg_write(ret
, client
, 0x002E, 0x0500);
486 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0002, 0x0002);
487 mt9t112_reg_mask_set(ret
, client
, 0x3B82, 0x0004, 0x0004);
490 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0x0004);
493 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0001, 0);
499 * Disable Secondary I2C Pads
501 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
503 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
505 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
507 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
509 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
511 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
514 /* poll to verify out of standby. Must Poll this bit */
515 for (i
= 0; i
< 100; i
++) {
516 mt9t112_reg_read(data
, client
, 0x0018);
517 if (!(0x4000 & data
))
526 static int mt9t112_init_setting(const struct i2c_client
*client
)
531 /* Adaptive Output Clock (A) */
532 mt9t112_mcu_mask_set(ret
, client
, VAR(26, 160), 0x0040, 0x0000);
535 mt9t112_mcu_write(ret
, client
, VAR(18, 12), 0x0024);
537 /* Fine Correction (A) */
538 mt9t112_mcu_write(ret
, client
, VAR(18, 15), 0x00CC);
540 /* Fine IT Min (A) */
541 mt9t112_mcu_write(ret
, client
, VAR(18, 17), 0x01f1);
543 /* Fine IT Max Margin (A) */
544 mt9t112_mcu_write(ret
, client
, VAR(18, 19), 0x00fF);
546 /* Base Frame Lines (A) */
547 mt9t112_mcu_write(ret
, client
, VAR(18, 29), 0x032D);
549 /* Min Line Length (A) */
550 mt9t112_mcu_write(ret
, client
, VAR(18, 31), 0x073a);
552 /* Line Length (A) */
553 mt9t112_mcu_write(ret
, client
, VAR(18, 37), 0x07d0);
555 /* Adaptive Output Clock (B) */
556 mt9t112_mcu_mask_set(ret
, client
, VAR(27, 160), 0x0040, 0x0000);
559 mt9t112_mcu_write(ret
, client
, VAR(18, 74), 0x004);
561 /* Column Start (B) */
562 mt9t112_mcu_write(ret
, client
, VAR(18, 76), 0x004);
565 mt9t112_mcu_write(ret
, client
, VAR(18, 78), 0x60B);
568 mt9t112_mcu_write(ret
, client
, VAR(18, 80), 0x80B);
570 /* Fine Correction (B) */
571 mt9t112_mcu_write(ret
, client
, VAR(18, 87), 0x008C);
573 /* Fine IT Min (B) */
574 mt9t112_mcu_write(ret
, client
, VAR(18, 89), 0x01F1);
576 /* Fine IT Max Margin (B) */
577 mt9t112_mcu_write(ret
, client
, VAR(18, 91), 0x00FF);
579 /* Base Frame Lines (B) */
580 mt9t112_mcu_write(ret
, client
, VAR(18, 101), 0x0668);
582 /* Min Line Length (B) */
583 mt9t112_mcu_write(ret
, client
, VAR(18, 103), 0x0AF0);
585 /* Line Length (B) */
586 mt9t112_mcu_write(ret
, client
, VAR(18, 109), 0x0AF0);
589 * Flicker Dectection registers
590 * This section should be replaced whenever new Timing file is generated
591 * All the following registers need to be replaced
592 * Following registers are generated from Register Wizard but user can
593 * modify them. For detail see auto flicker detection tuning
596 /* FD_FDPERIOD_SELECT */
597 mt9t112_mcu_write(ret
, client
, VAR8(8, 5), 0x01);
599 /* PRI_B_CONFIG_FD_ALGO_RUN */
600 mt9t112_mcu_write(ret
, client
, VAR(27, 17), 0x0003);
602 /* PRI_A_CONFIG_FD_ALGO_RUN */
603 mt9t112_mcu_write(ret
, client
, VAR(26, 17), 0x0003);
606 * AFD range detection tuning registers
610 mt9t112_mcu_write(ret
, client
, VAR8(18, 165), 0x25);
613 mt9t112_mcu_write(ret
, client
, VAR8(18, 166), 0x28);
616 mt9t112_mcu_write(ret
, client
, VAR8(18, 167), 0x2C);
619 mt9t112_mcu_write(ret
, client
, VAR8(18, 168), 0x2F);
621 /* period_50Hz (A) */
622 mt9t112_mcu_write(ret
, client
, VAR8(18, 68), 0xBA);
624 /* secret register by aptina */
625 /* period_50Hz (A MSB) */
626 mt9t112_mcu_write(ret
, client
, VAR8(18, 303), 0x00);
628 /* period_60Hz (A) */
629 mt9t112_mcu_write(ret
, client
, VAR8(18, 69), 0x9B);
631 /* secret register by aptina */
632 /* period_60Hz (A MSB) */
633 mt9t112_mcu_write(ret
, client
, VAR8(18, 301), 0x00);
635 /* period_50Hz (B) */
636 mt9t112_mcu_write(ret
, client
, VAR8(18, 140), 0x82);
638 /* secret register by aptina */
639 /* period_50Hz (B) MSB */
640 mt9t112_mcu_write(ret
, client
, VAR8(18, 304), 0x00);
642 /* period_60Hz (B) */
643 mt9t112_mcu_write(ret
, client
, VAR8(18, 141), 0x6D);
645 /* secret register by aptina */
646 /* period_60Hz (B) MSB */
647 mt9t112_mcu_write(ret
, client
, VAR8(18, 302), 0x00);
650 mt9t112_mcu_write(ret
, client
, VAR8(8, 2), 0x10);
653 mt9t112_mcu_write(ret
, client
, VAR8(8, 9), 0x02);
656 mt9t112_mcu_write(ret
, client
, VAR8(8, 10), 0x03);
659 mt9t112_mcu_write(ret
, client
, VAR8(8, 12), 0x0A);
661 /* RX FIFO Watermark (A) */
662 mt9t112_mcu_write(ret
, client
, VAR(18, 70), 0x0014);
664 /* RX FIFO Watermark (B) */
665 mt9t112_mcu_write(ret
, client
, VAR(18, 142), 0x0014);
669 * CorePixCLK: 36.5 MHz
671 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0044), 133);
672 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0045), 110);
673 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008c), 130);
674 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008d), 108);
676 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00A5), 27);
677 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a6), 30);
678 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a7), 32);
679 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a8), 35);
684 static int mt9t112_auto_focus_setting(const struct i2c_client
*client
)
688 mt9t112_mcu_write(ret
, client
, VAR(12, 13), 0x000F);
689 mt9t112_mcu_write(ret
, client
, VAR(12, 23), 0x0F0F);
690 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
692 mt9t112_reg_write(ret
, client
, 0x0614, 0x0000);
694 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
695 mt9t112_mcu_write(ret
, client
, VAR8(12, 2), 0x02);
696 mt9t112_mcu_write(ret
, client
, VAR(12, 3), 0x0002);
697 mt9t112_mcu_write(ret
, client
, VAR(17, 3), 0x8001);
698 mt9t112_mcu_write(ret
, client
, VAR(17, 11), 0x0025);
699 mt9t112_mcu_write(ret
, client
, VAR(17, 13), 0x0193);
700 mt9t112_mcu_write(ret
, client
, VAR8(17, 33), 0x18);
701 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
706 static int mt9t112_auto_focus_trigger(const struct i2c_client
*client
)
710 mt9t112_mcu_write(ret
, client
, VAR8(12, 25), 0x01);
715 static int mt9t112_init_camera(const struct i2c_client
*client
)
719 ECHECKER(ret
, mt9t112_reset(client
));
721 ECHECKER(ret
, mt9t112_init_pll(client
));
723 ECHECKER(ret
, mt9t112_init_setting(client
));
725 ECHECKER(ret
, mt9t112_auto_focus_setting(client
));
727 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0);
729 /* Analog setting B */
730 mt9t112_reg_write(ret
, client
, 0x3084, 0x2409);
731 mt9t112_reg_write(ret
, client
, 0x3092, 0x0A49);
732 mt9t112_reg_write(ret
, client
, 0x3094, 0x4949);
733 mt9t112_reg_write(ret
, client
, 0x3096, 0x4950);
736 * Disable adaptive clock
737 * PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR
738 * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
740 mt9t112_mcu_write(ret
, client
, VAR(26, 160), 0x0A2E);
741 mt9t112_mcu_write(ret
, client
, VAR(27, 160), 0x0A2E);
743 /* Configure STatus in Status_before_length Format and enable header */
744 /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
745 mt9t112_mcu_write(ret
, client
, VAR(27, 144), 0x0CB4);
747 /* Enable JPEG in context B */
748 /* PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR */
749 mt9t112_mcu_write(ret
, client
, VAR8(27, 142), 0x01);
751 /* Disable Dac_TXLO */
752 mt9t112_reg_write(ret
, client
, 0x316C, 0x350F);
754 /* Set max slew rates */
755 mt9t112_reg_write(ret
, client
, 0x1E, 0x777);
760 /************************************************************************
766 ************************************************************************/
767 static int mt9t112_set_bus_param(struct soc_camera_device
*icd
,
773 static unsigned long mt9t112_query_bus_param(struct soc_camera_device
*icd
)
775 struct i2c_client
*client
= to_i2c_client(to_soc_camera_control(icd
));
776 struct mt9t112_priv
*priv
= to_mt9t112(client
);
777 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
778 unsigned long flags
= SOCAM_MASTER
| SOCAM_VSYNC_ACTIVE_HIGH
|
779 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_DATA_ACTIVE_HIGH
;
781 flags
|= (priv
->info
->flags
& MT9T112_FLAG_PCLK_RISING_EDGE
) ?
782 SOCAM_PCLK_SAMPLE_RISING
: SOCAM_PCLK_SAMPLE_FALLING
;
784 if (priv
->info
->flags
& MT9T112_FLAG_DATAWIDTH_8
)
785 flags
|= SOCAM_DATAWIDTH_8
;
787 flags
|= SOCAM_DATAWIDTH_10
;
789 return soc_camera_apply_sensor_flags(icl
, flags
);
792 static struct soc_camera_ops mt9t112_ops
= {
793 .set_bus_param
= mt9t112_set_bus_param
,
794 .query_bus_param
= mt9t112_query_bus_param
,
797 /************************************************************************
803 ************************************************************************/
804 static int mt9t112_g_chip_ident(struct v4l2_subdev
*sd
,
805 struct v4l2_dbg_chip_ident
*id
)
807 struct i2c_client
*client
= sd
->priv
;
808 struct mt9t112_priv
*priv
= to_mt9t112(client
);
810 id
->ident
= priv
->model
;
816 #ifdef CONFIG_VIDEO_ADV_DEBUG
817 static int mt9t112_g_register(struct v4l2_subdev
*sd
,
818 struct v4l2_dbg_register
*reg
)
820 struct i2c_client
*client
= sd
->priv
;
824 mt9t112_reg_read(ret
, client
, reg
->reg
);
826 reg
->val
= (__u64
)ret
;
831 static int mt9t112_s_register(struct v4l2_subdev
*sd
,
832 struct v4l2_dbg_register
*reg
)
834 struct i2c_client
*client
= sd
->priv
;
837 mt9t112_reg_write(ret
, client
, reg
->reg
, reg
->val
);
843 static struct v4l2_subdev_core_ops mt9t112_subdev_core_ops
= {
844 .g_chip_ident
= mt9t112_g_chip_ident
,
845 #ifdef CONFIG_VIDEO_ADV_DEBUG
846 .g_register
= mt9t112_g_register
,
847 .s_register
= mt9t112_s_register
,
852 /************************************************************************
855 v4l2_subdev_video_ops
858 ************************************************************************/
859 static int mt9t112_s_stream(struct v4l2_subdev
*sd
, int enable
)
861 struct i2c_client
*client
= sd
->priv
;
862 struct mt9t112_priv
*priv
= to_mt9t112(client
);
868 * If user selected large output size,
869 * and used it long time,
870 * mt9t112 camera will be very warm.
872 * But current driver can not stop mt9t112 camera.
873 * So, set small size here to solve this problem.
875 mt9t112_set_a_frame_size(client
, VGA_WIDTH
, VGA_HEIGHT
);
879 if (!(priv
->flags
& INIT_DONE
)) {
880 u16 param
= (MT9T112_FLAG_PCLK_RISING_EDGE
&
881 priv
->info
->flags
) ? 0x0001 : 0x0000;
883 ECHECKER(ret
, mt9t112_init_camera(client
));
885 /* Invert PCLK (Data sampled on falling edge of pixclk) */
886 mt9t112_reg_write(ret
, client
, 0x3C20, param
);
890 priv
->flags
|= INIT_DONE
;
893 mt9t112_mcu_write(ret
, client
, VAR(26, 7), priv
->format
->fmt
);
894 mt9t112_mcu_write(ret
, client
, VAR(26, 9), priv
->format
->order
);
895 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
897 mt9t112_set_a_frame_size(client
,
901 ECHECKER(ret
, mt9t112_auto_focus_trigger(client
));
903 dev_dbg(&client
->dev
, "format : %d\n", priv
->format
->code
);
904 dev_dbg(&client
->dev
, "size : %d x %d\n",
908 CLOCK_INFO(client
, EXT_CLOCK
);
913 static int mt9t112_set_params(struct i2c_client
*client
, u32 width
, u32 height
,
914 enum v4l2_mbus_pixelcode code
)
916 struct mt9t112_priv
*priv
= to_mt9t112(client
);
924 mt9t112_frame_check(&width
, &height
);
929 for (i
= 0; i
< ARRAY_SIZE(mt9t112_cfmts
); i
++)
930 if (mt9t112_cfmts
[i
].code
== code
)
933 if (i
== ARRAY_SIZE(mt9t112_cfmts
))
936 priv
->frame
.width
= (u16
)width
;
937 priv
->frame
.height
= (u16
)height
;
939 priv
->format
= mt9t112_cfmts
+ i
;
944 static int mt9t112_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
948 a
->bounds
.width
= VGA_WIDTH
;
949 a
->bounds
.height
= VGA_HEIGHT
;
950 a
->defrect
= a
->bounds
;
951 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
952 a
->pixelaspect
.numerator
= 1;
953 a
->pixelaspect
.denominator
= 1;
958 static int mt9t112_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
962 a
->c
.width
= VGA_WIDTH
;
963 a
->c
.height
= VGA_HEIGHT
;
964 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
969 static int mt9t112_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
971 struct i2c_client
*client
= sd
->priv
;
972 struct v4l2_rect
*rect
= &a
->c
;
974 return mt9t112_set_params(client
, rect
->width
, rect
->height
,
975 V4L2_MBUS_FMT_YUYV8_2X8_BE
);
978 static int mt9t112_g_fmt(struct v4l2_subdev
*sd
,
979 struct v4l2_mbus_framefmt
*mf
)
981 struct i2c_client
*client
= sd
->priv
;
982 struct mt9t112_priv
*priv
= to_mt9t112(client
);
985 int ret
= mt9t112_set_params(client
, VGA_WIDTH
, VGA_HEIGHT
,
986 V4L2_MBUS_FMT_YUYV8_2X8_BE
);
991 mf
->width
= priv
->frame
.width
;
992 mf
->height
= priv
->frame
.height
;
993 /* TODO: set colorspace */
994 mf
->code
= priv
->format
->code
;
995 mf
->field
= V4L2_FIELD_NONE
;
1000 static int mt9t112_s_fmt(struct v4l2_subdev
*sd
,
1001 struct v4l2_mbus_framefmt
*mf
)
1003 struct i2c_client
*client
= sd
->priv
;
1005 /* TODO: set colorspace */
1006 return mt9t112_set_params(client
, mf
->width
, mf
->height
, mf
->code
);
1009 static int mt9t112_try_fmt(struct v4l2_subdev
*sd
,
1010 struct v4l2_mbus_framefmt
*mf
)
1012 mt9t112_frame_check(&mf
->width
, &mf
->height
);
1014 /* TODO: set colorspace */
1015 mf
->field
= V4L2_FIELD_NONE
;
1020 static int mt9t112_enum_fmt(struct v4l2_subdev
*sd
, int index
,
1021 enum v4l2_mbus_pixelcode
*code
)
1023 if ((unsigned int)index
>= ARRAY_SIZE(mt9t112_cfmts
))
1026 *code
= mt9t112_cfmts
[index
].code
;
1030 static struct v4l2_subdev_video_ops mt9t112_subdev_video_ops
= {
1031 .s_stream
= mt9t112_s_stream
,
1032 .g_mbus_fmt
= mt9t112_g_fmt
,
1033 .s_mbus_fmt
= mt9t112_s_fmt
,
1034 .try_mbus_fmt
= mt9t112_try_fmt
,
1035 .cropcap
= mt9t112_cropcap
,
1036 .g_crop
= mt9t112_g_crop
,
1037 .s_crop
= mt9t112_s_crop
,
1038 .enum_mbus_fmt
= mt9t112_enum_fmt
,
1041 /************************************************************************
1047 ************************************************************************/
1048 static struct v4l2_subdev_ops mt9t112_subdev_ops
= {
1049 .core
= &mt9t112_subdev_core_ops
,
1050 .video
= &mt9t112_subdev_video_ops
,
1053 static int mt9t112_camera_probe(struct soc_camera_device
*icd
,
1054 struct i2c_client
*client
)
1056 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1057 const char *devname
;
1061 * We must have a parent by now. And it cannot be a wrong one.
1062 * So this entire test is completely redundant.
1064 if (!icd
->dev
.parent
||
1065 to_soc_camera_host(icd
->dev
.parent
)->nr
!= icd
->iface
)
1069 * check and show chip ID
1071 mt9t112_reg_read(chipid
, client
, 0x0000);
1075 devname
= "mt9t111";
1076 priv
->model
= V4L2_IDENT_MT9T111
;
1079 devname
= "mt9t112";
1080 priv
->model
= V4L2_IDENT_MT9T112
;
1083 dev_err(&client
->dev
, "Product ID error %04x\n", chipid
);
1087 dev_info(&client
->dev
, "%s chip ID %04x\n", devname
, chipid
);
1092 static int mt9t112_probe(struct i2c_client
*client
,
1093 const struct i2c_device_id
*did
)
1095 struct mt9t112_priv
*priv
;
1096 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
1097 struct soc_camera_link
*icl
;
1101 dev_err(&client
->dev
, "mt9t112: missing soc-camera data!\n");
1105 icl
= to_soc_camera_link(icd
);
1106 if (!icl
|| !icl
->priv
)
1109 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1113 priv
->info
= icl
->priv
;
1115 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &mt9t112_subdev_ops
);
1117 icd
->ops
= &mt9t112_ops
;
1119 ret
= mt9t112_camera_probe(icd
, client
);
1122 i2c_set_clientdata(client
, NULL
);
1129 static int mt9t112_remove(struct i2c_client
*client
)
1131 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1132 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
1135 i2c_set_clientdata(client
, NULL
);
1140 static const struct i2c_device_id mt9t112_id
[] = {
1144 MODULE_DEVICE_TABLE(i2c
, mt9t112_id
);
1146 static struct i2c_driver mt9t112_i2c_driver
= {
1150 .probe
= mt9t112_probe
,
1151 .remove
= mt9t112_remove
,
1152 .id_table
= mt9t112_id
,
1155 /************************************************************************
1161 ************************************************************************/
1162 static int __init
mt9t112_module_init(void)
1164 return i2c_add_driver(&mt9t112_i2c_driver
);
1167 static void __exit
mt9t112_module_exit(void)
1169 i2c_del_driver(&mt9t112_i2c_driver
);
1172 module_init(mt9t112_module_init
);
1173 module_exit(mt9t112_module_exit
);
1175 MODULE_DESCRIPTION("SoC Camera driver for mt9t112");
1176 MODULE_AUTHOR("Kuninori Morimoto");
1177 MODULE_LICENSE("GPL v2");