1 // SPDX-License-Identifier: GPL-2.0
3 * mt9t112 Camera Driver
5 * Copyright (C) 2018 Jacopo Mondi <jacopo+renesas@jmondi.org>
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * Based on ov772x driver, mt9m111 driver,
12 * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
13 * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
14 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
15 * Copyright (C) 2008 Magnus Damm
16 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
18 * TODO: This driver lacks support for frame rate control due to missing
19 * register level documentation and suitable hardware for testing.
20 * v4l-utils compliance tools will report errors.
23 #include <linux/clk.h>
24 #include <linux/delay.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/i2c.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/v4l2-mediabus.h>
31 #include <linux/videodev2.h>
33 #include <media/i2c/mt9t112.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-image-sizes.h>
36 #include <media/v4l2-subdev.h>
38 /* you can check PLL/clock info */
39 /* #define EXT_CLOCK 24000000 */
41 /************************************************************************
43 ***********************************************************************/
47 #define MAX_WIDTH 2048
48 #define MAX_HEIGHT 1536
53 #define ECHECKER(ret, x) \
60 #define mt9t112_reg_write(ret, client, a, b) \
61 ECHECKER(ret, __mt9t112_reg_write(client, a, b))
62 #define mt9t112_mcu_write(ret, client, a, b) \
63 ECHECKER(ret, __mt9t112_mcu_write(client, a, b))
65 #define mt9t112_reg_mask_set(ret, client, a, b, c) \
66 ECHECKER(ret, __mt9t112_reg_mask_set(client, a, b, c))
67 #define mt9t112_mcu_mask_set(ret, client, a, b, c) \
68 ECHECKER(ret, __mt9t112_mcu_mask_set(client, a, b, c))
70 #define mt9t112_reg_read(ret, client, a) \
71 ECHECKER(ret, __mt9t112_reg_read(client, a))
76 #define _VAR(id, offset, base) (base | (id & 0x1f) << 10 | (offset & 0x3ff))
77 #define VAR(id, offset) _VAR(id, offset, 0x0000)
78 #define VAR8(id, offset) _VAR(id, offset, 0x8000)
80 /************************************************************************
82 ***********************************************************************/
83 struct mt9t112_format
{
85 enum v4l2_colorspace colorspace
;
91 struct v4l2_subdev subdev
;
92 struct mt9t112_platform_data
*info
;
93 struct i2c_client
*client
;
94 struct v4l2_rect frame
;
96 struct gpio_desc
*standby_gpio
;
97 const struct mt9t112_format
*format
;
102 /************************************************************************
104 ***********************************************************************/
106 static const struct mt9t112_format mt9t112_cfmts
[] = {
108 .code
= MEDIA_BUS_FMT_UYVY8_2X8
,
109 .colorspace
= V4L2_COLORSPACE_SRGB
,
113 .code
= MEDIA_BUS_FMT_VYUY8_2X8
,
114 .colorspace
= V4L2_COLORSPACE_SRGB
,
118 .code
= MEDIA_BUS_FMT_YUYV8_2X8
,
119 .colorspace
= V4L2_COLORSPACE_SRGB
,
123 .code
= MEDIA_BUS_FMT_YVYU8_2X8
,
124 .colorspace
= V4L2_COLORSPACE_SRGB
,
128 .code
= MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE
,
129 .colorspace
= V4L2_COLORSPACE_SRGB
,
133 .code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
134 .colorspace
= V4L2_COLORSPACE_SRGB
,
140 /************************************************************************
142 ***********************************************************************/
143 static struct mt9t112_priv
*to_mt9t112(const struct i2c_client
*client
)
145 return container_of(i2c_get_clientdata(client
),
150 static int __mt9t112_reg_read(const struct i2c_client
*client
, u16 command
)
152 struct i2c_msg msg
[2];
156 command
= swab16(command
);
158 msg
[0].addr
= client
->addr
;
161 msg
[0].buf
= (u8
*)&command
;
163 msg
[1].addr
= client
->addr
;
164 msg
[1].flags
= I2C_M_RD
;
169 * If return value of this function is < 0, it means error, else,
170 * below 16bit is valid data.
172 ret
= i2c_transfer(client
->adapter
, msg
, 2);
176 memcpy(&ret
, buf
, 2);
181 static int __mt9t112_reg_write(const struct i2c_client
*client
,
182 u16 command
, u16 data
)
188 command
= swab16(command
);
191 memcpy(buf
+ 0, &command
, 2);
192 memcpy(buf
+ 2, &data
, 2);
194 msg
.addr
= client
->addr
;
200 * i2c_transfer return message length, but this function should
201 * return 0 if correct case.
203 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
205 return ret
>= 0 ? 0 : ret
;
208 static int __mt9t112_reg_mask_set(const struct i2c_client
*client
,
209 u16 command
, u16 mask
, u16 set
)
211 int val
= __mt9t112_reg_read(client
, command
);
219 return __mt9t112_reg_write(client
, command
, val
);
223 static int __mt9t112_mcu_read(const struct i2c_client
*client
, u16 command
)
227 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
231 return __mt9t112_reg_read(client
, 0x0990);
234 static int __mt9t112_mcu_write(const struct i2c_client
*client
,
235 u16 command
, u16 data
)
239 ret
= __mt9t112_reg_write(client
, 0x098E, command
);
243 return __mt9t112_reg_write(client
, 0x0990, data
);
246 static int __mt9t112_mcu_mask_set(const struct i2c_client
*client
,
247 u16 command
, u16 mask
, u16 set
)
249 int val
= __mt9t112_mcu_read(client
, command
);
257 return __mt9t112_mcu_write(client
, command
, val
);
260 static int mt9t112_reset(const struct i2c_client
*client
)
264 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0001);
265 usleep_range(1000, 5000);
266 mt9t112_reg_mask_set(ret
, client
, 0x001a, 0x0001, 0x0000);
272 #define CLOCK_INFO(a, b)
274 #define CLOCK_INFO(a, b) mt9t112_clock_info(a, b)
275 static int mt9t112_clock_info(const struct i2c_client
*client
, u32 ext
)
277 int m
, n
, p1
, p2
, p3
, p4
, p5
, p6
, p7
;
281 ext
/= 1000; /* kbyte order */
283 mt9t112_reg_read(n
, client
, 0x0012);
290 mt9t112_reg_read(n
, client
, 0x002a);
297 mt9t112_reg_read(n
, client
, 0x002c);
300 mt9t112_reg_read(n
, client
, 0x0010);
302 n
= (n
>> 8) & 0x003f;
304 enable
= ((ext
< 6000) || (ext
> 54000)) ? "X" : "";
305 dev_dbg(&client
->dev
, "EXTCLK : %10u K %s\n", ext
, enable
);
307 vco
= 2 * m
* ext
/ (n
+ 1);
308 enable
= ((vco
< 384000) || (vco
> 768000)) ? "X" : "";
309 dev_dbg(&client
->dev
, "VCO : %10u K %s\n", vco
, enable
);
311 clk
= vco
/ (p1
+ 1) / (p2
+ 1);
312 enable
= (clk
> 96000) ? "X" : "";
313 dev_dbg(&client
->dev
, "PIXCLK : %10u K %s\n", clk
, enable
);
315 clk
= vco
/ (p3
+ 1);
316 enable
= (clk
> 768000) ? "X" : "";
317 dev_dbg(&client
->dev
, "MIPICLK : %10u K %s\n", clk
, enable
);
319 clk
= vco
/ (p6
+ 1);
320 enable
= (clk
> 96000) ? "X" : "";
321 dev_dbg(&client
->dev
, "MCU CLK : %10u K %s\n", clk
, enable
);
323 clk
= vco
/ (p5
+ 1);
324 enable
= (clk
> 54000) ? "X" : "";
325 dev_dbg(&client
->dev
, "SOC CLK : %10u K %s\n", clk
, enable
);
327 clk
= vco
/ (p4
+ 1);
328 enable
= (clk
> 70000) ? "X" : "";
329 dev_dbg(&client
->dev
, "Sensor CLK : %10u K %s\n", clk
, enable
);
331 clk
= vco
/ (p7
+ 1);
332 dev_dbg(&client
->dev
, "External sensor : %10u K\n", clk
);
335 enable
= ((clk
< 2000) || (clk
> 24000)) ? "X" : "";
336 dev_dbg(&client
->dev
, "PFD : %10u K %s\n", clk
, enable
);
342 static int mt9t112_set_a_frame_size(const struct i2c_client
*client
,
343 u16 width
, u16 height
)
346 u16 wstart
= (MAX_WIDTH
- width
) / 2;
347 u16 hstart
= (MAX_HEIGHT
- height
) / 2;
349 /* (Context A) Image Width/Height. */
350 mt9t112_mcu_write(ret
, client
, VAR(26, 0), width
);
351 mt9t112_mcu_write(ret
, client
, VAR(26, 2), height
);
353 /* (Context A) Output Width/Height. */
354 mt9t112_mcu_write(ret
, client
, VAR(18, 43), 8 + width
);
355 mt9t112_mcu_write(ret
, client
, VAR(18, 45), 8 + height
);
357 /* (Context A) Start Row/Column. */
358 mt9t112_mcu_write(ret
, client
, VAR(18, 2), 4 + hstart
);
359 mt9t112_mcu_write(ret
, client
, VAR(18, 4), 4 + wstart
);
361 /* (Context A) End Row/Column. */
362 mt9t112_mcu_write(ret
, client
, VAR(18, 6), 11 + height
+ hstart
);
363 mt9t112_mcu_write(ret
, client
, VAR(18, 8), 11 + width
+ wstart
);
365 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
370 static int mt9t112_set_pll_dividers(const struct i2c_client
*client
,
371 u8 m
, u8 n
, u8 p1
, u8 p2
, u8 p3
, u8 p4
,
378 val
= (n
<< 8) | (m
<< 0);
379 mt9t112_reg_mask_set(ret
, client
, 0x0010, 0x3fff, val
);
382 val
= ((p3
& 0x0F) << 8) | ((p2
& 0x0F) << 4) | ((p1
& 0x0F) << 0);
383 mt9t112_reg_mask_set(ret
, client
, 0x0012, 0x0fff, val
);
386 val
= (0x7 << 12) | ((p6
& 0x0F) << 8) | ((p5
& 0x0F) << 4) |
388 mt9t112_reg_mask_set(ret
, client
, 0x002A, 0x7fff, val
);
391 val
= (0x1 << 12) | ((p7
& 0x0F) << 0);
392 mt9t112_reg_mask_set(ret
, client
, 0x002C, 0x100f, val
);
397 static int mt9t112_init_pll(const struct i2c_client
*client
)
399 struct mt9t112_priv
*priv
= to_mt9t112(client
);
402 mt9t112_reg_mask_set(ret
, client
, 0x0014, 0x003, 0x0001);
404 /* PLL control: BYPASS PLL = 8517. */
405 mt9t112_reg_write(ret
, client
, 0x0014, 0x2145);
407 /* Replace these registers when new timing parameters are generated. */
408 mt9t112_set_pll_dividers(client
,
409 priv
->info
->divider
.m
, priv
->info
->divider
.n
,
410 priv
->info
->divider
.p1
, priv
->info
->divider
.p2
,
411 priv
->info
->divider
.p3
, priv
->info
->divider
.p4
,
412 priv
->info
->divider
.p5
, priv
->info
->divider
.p6
,
413 priv
->info
->divider
.p7
);
421 mt9t112_reg_write(ret
, client
, 0x0014, 0x2525);
422 mt9t112_reg_write(ret
, client
, 0x0014, 0x2527);
423 mt9t112_reg_write(ret
, client
, 0x0014, 0x3427);
424 mt9t112_reg_write(ret
, client
, 0x0014, 0x3027);
430 * Reference clock count
431 * I2C Master Clock Divider
433 mt9t112_reg_write(ret
, client
, 0x0014, 0x3046);
434 /* JPEG initialization workaround */
435 mt9t112_reg_write(ret
, client
, 0x0016, 0x0400);
436 mt9t112_reg_write(ret
, client
, 0x0022, 0x0190);
437 mt9t112_reg_write(ret
, client
, 0x3B84, 0x0212);
439 /* External sensor clock is PLL bypass. */
440 mt9t112_reg_write(ret
, client
, 0x002E, 0x0500);
442 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0002, 0x0002);
443 mt9t112_reg_mask_set(ret
, client
, 0x3B82, 0x0004, 0x0004);
446 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0x0004);
448 /* Out of standby. */
449 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0001, 0);
455 * Disable Secondary I2C Pads
457 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
459 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
461 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
463 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
465 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
467 mt9t112_reg_write(ret
, client
, 0x0614, 0x0001);
470 /* Poll to verify out of standby. Must Poll this bit. */
471 for (i
= 0; i
< 100; i
++) {
472 mt9t112_reg_read(data
, client
, 0x0018);
473 if (!(data
& 0x4000))
482 static int mt9t112_init_setting(const struct i2c_client
*client
)
486 /* Adaptive Output Clock (A) */
487 mt9t112_mcu_mask_set(ret
, client
, VAR(26, 160), 0x0040, 0x0000);
490 mt9t112_mcu_write(ret
, client
, VAR(18, 12), 0x0024);
492 /* Fine Correction (A) */
493 mt9t112_mcu_write(ret
, client
, VAR(18, 15), 0x00CC);
495 /* Fine IT Min (A) */
496 mt9t112_mcu_write(ret
, client
, VAR(18, 17), 0x01f1);
498 /* Fine IT Max Margin (A) */
499 mt9t112_mcu_write(ret
, client
, VAR(18, 19), 0x00fF);
501 /* Base Frame Lines (A) */
502 mt9t112_mcu_write(ret
, client
, VAR(18, 29), 0x032D);
504 /* Min Line Length (A) */
505 mt9t112_mcu_write(ret
, client
, VAR(18, 31), 0x073a);
507 /* Line Length (A) */
508 mt9t112_mcu_write(ret
, client
, VAR(18, 37), 0x07d0);
510 /* Adaptive Output Clock (B) */
511 mt9t112_mcu_mask_set(ret
, client
, VAR(27, 160), 0x0040, 0x0000);
514 mt9t112_mcu_write(ret
, client
, VAR(18, 74), 0x004);
516 /* Column Start (B) */
517 mt9t112_mcu_write(ret
, client
, VAR(18, 76), 0x004);
520 mt9t112_mcu_write(ret
, client
, VAR(18, 78), 0x60B);
523 mt9t112_mcu_write(ret
, client
, VAR(18, 80), 0x80B);
525 /* Fine Correction (B) */
526 mt9t112_mcu_write(ret
, client
, VAR(18, 87), 0x008C);
528 /* Fine IT Min (B) */
529 mt9t112_mcu_write(ret
, client
, VAR(18, 89), 0x01F1);
531 /* Fine IT Max Margin (B) */
532 mt9t112_mcu_write(ret
, client
, VAR(18, 91), 0x00FF);
534 /* Base Frame Lines (B) */
535 mt9t112_mcu_write(ret
, client
, VAR(18, 101), 0x0668);
537 /* Min Line Length (B) */
538 mt9t112_mcu_write(ret
, client
, VAR(18, 103), 0x0AF0);
540 /* Line Length (B) */
541 mt9t112_mcu_write(ret
, client
, VAR(18, 109), 0x0AF0);
544 * Flicker Detection registers.
545 * This section should be replaced whenever new timing file is
546 * generated. All the following registers need to be replaced.
547 * Following registers are generated from Register Wizard but user can
548 * modify them. For detail see auto flicker detection tuning.
551 /* FD_FDPERIOD_SELECT */
552 mt9t112_mcu_write(ret
, client
, VAR8(8, 5), 0x01);
554 /* PRI_B_CONFIG_FD_ALGO_RUN */
555 mt9t112_mcu_write(ret
, client
, VAR(27, 17), 0x0003);
557 /* PRI_A_CONFIG_FD_ALGO_RUN */
558 mt9t112_mcu_write(ret
, client
, VAR(26, 17), 0x0003);
561 * AFD range detection tuning registers.
565 mt9t112_mcu_write(ret
, client
, VAR8(18, 165), 0x25);
568 mt9t112_mcu_write(ret
, client
, VAR8(18, 166), 0x28);
571 mt9t112_mcu_write(ret
, client
, VAR8(18, 167), 0x2C);
574 mt9t112_mcu_write(ret
, client
, VAR8(18, 168), 0x2F);
576 /* Period_50Hz (A) */
577 mt9t112_mcu_write(ret
, client
, VAR8(18, 68), 0xBA);
579 /* Secret register by Aptina. */
580 /* Period_50Hz (A MSB) */
581 mt9t112_mcu_write(ret
, client
, VAR8(18, 303), 0x00);
583 /* Period_60Hz (A) */
584 mt9t112_mcu_write(ret
, client
, VAR8(18, 69), 0x9B);
586 /* Secret register by Aptina. */
587 /* Period_60Hz (A MSB) */
588 mt9t112_mcu_write(ret
, client
, VAR8(18, 301), 0x00);
590 /* Period_50Hz (B) */
591 mt9t112_mcu_write(ret
, client
, VAR8(18, 140), 0x82);
593 /* Secret register by Aptina. */
594 /* Period_50Hz (B) MSB */
595 mt9t112_mcu_write(ret
, client
, VAR8(18, 304), 0x00);
597 /* Period_60Hz (B) */
598 mt9t112_mcu_write(ret
, client
, VAR8(18, 141), 0x6D);
600 /* Secret register by Aptina. */
601 /* Period_60Hz (B) MSB */
602 mt9t112_mcu_write(ret
, client
, VAR8(18, 302), 0x00);
605 mt9t112_mcu_write(ret
, client
, VAR8(8, 2), 0x10);
608 mt9t112_mcu_write(ret
, client
, VAR8(8, 9), 0x02);
611 mt9t112_mcu_write(ret
, client
, VAR8(8, 10), 0x03);
614 mt9t112_mcu_write(ret
, client
, VAR8(8, 12), 0x0A);
616 /* RX FIFO Watermark (A) */
617 mt9t112_mcu_write(ret
, client
, VAR(18, 70), 0x0014);
619 /* RX FIFO Watermark (B) */
620 mt9t112_mcu_write(ret
, client
, VAR(18, 142), 0x0014);
624 * CorePixCLK: 36.5 MHz
626 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0044), 133);
627 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x0045), 110);
628 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008c), 130);
629 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x008d), 108);
631 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00A5), 27);
632 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a6), 30);
633 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a7), 32);
634 mt9t112_mcu_write(ret
, client
, VAR8(18, 0x00a8), 35);
639 static int mt9t112_auto_focus_setting(const struct i2c_client
*client
)
643 mt9t112_mcu_write(ret
, client
, VAR(12, 13), 0x000F);
644 mt9t112_mcu_write(ret
, client
, VAR(12, 23), 0x0F0F);
645 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
647 mt9t112_reg_write(ret
, client
, 0x0614, 0x0000);
649 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
650 mt9t112_mcu_write(ret
, client
, VAR8(12, 2), 0x02);
651 mt9t112_mcu_write(ret
, client
, VAR(12, 3), 0x0002);
652 mt9t112_mcu_write(ret
, client
, VAR(17, 3), 0x8001);
653 mt9t112_mcu_write(ret
, client
, VAR(17, 11), 0x0025);
654 mt9t112_mcu_write(ret
, client
, VAR(17, 13), 0x0193);
655 mt9t112_mcu_write(ret
, client
, VAR8(17, 33), 0x18);
656 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x05);
661 static int mt9t112_auto_focus_trigger(const struct i2c_client
*client
)
665 mt9t112_mcu_write(ret
, client
, VAR8(12, 25), 0x01);
670 static int mt9t112_init_camera(const struct i2c_client
*client
)
674 ECHECKER(ret
, mt9t112_reset(client
));
675 ECHECKER(ret
, mt9t112_init_pll(client
));
676 ECHECKER(ret
, mt9t112_init_setting(client
));
677 ECHECKER(ret
, mt9t112_auto_focus_setting(client
));
679 mt9t112_reg_mask_set(ret
, client
, 0x0018, 0x0004, 0);
681 /* Analog setting B.*/
682 mt9t112_reg_write(ret
, client
, 0x3084, 0x2409);
683 mt9t112_reg_write(ret
, client
, 0x3092, 0x0A49);
684 mt9t112_reg_write(ret
, client
, 0x3094, 0x4949);
685 mt9t112_reg_write(ret
, client
, 0x3096, 0x4950);
688 * Disable adaptive clock.
689 * PRI_A_CONFIG_JPEG_OB_TX_CONTROL_VAR
690 * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
692 mt9t112_mcu_write(ret
, client
, VAR(26, 160), 0x0A2E);
693 mt9t112_mcu_write(ret
, client
, VAR(27, 160), 0x0A2E);
696 * Configure Status in Status_before_length Format and enable header.
697 * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
699 mt9t112_mcu_write(ret
, client
, VAR(27, 144), 0x0CB4);
702 * Enable JPEG in context B.
703 * PRI_B_CONFIG_JPEG_OB_TX_CONTROL_VAR
705 mt9t112_mcu_write(ret
, client
, VAR8(27, 142), 0x01);
707 /* Disable Dac_TXLO. */
708 mt9t112_reg_write(ret
, client
, 0x316C, 0x350F);
710 /* Set max slew rates. */
711 mt9t112_reg_write(ret
, client
, 0x1E, 0x777);
716 /************************************************************************
717 * v4l2_subdev_core_ops
718 ***********************************************************************/
720 #ifdef CONFIG_VIDEO_ADV_DEBUG
721 static int mt9t112_g_register(struct v4l2_subdev
*sd
,
722 struct v4l2_dbg_register
*reg
)
724 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
728 mt9t112_reg_read(ret
, client
, reg
->reg
);
730 reg
->val
= (__u64
)ret
;
735 static int mt9t112_s_register(struct v4l2_subdev
*sd
,
736 const struct v4l2_dbg_register
*reg
)
738 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
741 mt9t112_reg_write(ret
, client
, reg
->reg
, reg
->val
);
747 static int mt9t112_power_on(struct mt9t112_priv
*priv
)
751 ret
= clk_prepare_enable(priv
->clk
);
755 if (priv
->standby_gpio
) {
756 gpiod_set_value(priv
->standby_gpio
, 0);
763 static int mt9t112_power_off(struct mt9t112_priv
*priv
)
765 clk_disable_unprepare(priv
->clk
);
766 if (priv
->standby_gpio
) {
767 gpiod_set_value(priv
->standby_gpio
, 1);
774 static int mt9t112_s_power(struct v4l2_subdev
*sd
, int on
)
776 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
777 struct mt9t112_priv
*priv
= to_mt9t112(client
);
779 return on
? mt9t112_power_on(priv
) :
780 mt9t112_power_off(priv
);
783 static const struct v4l2_subdev_core_ops mt9t112_subdev_core_ops
= {
784 #ifdef CONFIG_VIDEO_ADV_DEBUG
785 .g_register
= mt9t112_g_register
,
786 .s_register
= mt9t112_s_register
,
788 .s_power
= mt9t112_s_power
,
791 /************************************************************************
792 * v4l2_subdev_video_ops
793 **********************************************************************/
794 static int mt9t112_s_stream(struct v4l2_subdev
*sd
, int enable
)
796 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
797 struct mt9t112_priv
*priv
= to_mt9t112(client
);
803 * If user selected large output size, and used it long time,
804 * mt9t112 camera will be very warm.
806 * But current driver can not stop mt9t112 camera.
807 * So, set small size here to solve this problem.
809 mt9t112_set_a_frame_size(client
, VGA_WIDTH
, VGA_HEIGHT
);
813 if (!priv
->init_done
) {
814 u16 param
= MT9T112_FLAG_PCLK_RISING_EDGE
& priv
->info
->flags
?
817 ECHECKER(ret
, mt9t112_init_camera(client
));
819 /* Invert PCLK (Data sampled on falling edge of pixclk). */
820 mt9t112_reg_write(ret
, client
, 0x3C20, param
);
824 priv
->init_done
= true;
827 mt9t112_mcu_write(ret
, client
, VAR(26, 7), priv
->format
->fmt
);
828 mt9t112_mcu_write(ret
, client
, VAR(26, 9), priv
->format
->order
);
829 mt9t112_mcu_write(ret
, client
, VAR8(1, 0), 0x06);
831 mt9t112_set_a_frame_size(client
, priv
->frame
.width
, priv
->frame
.height
);
833 ECHECKER(ret
, mt9t112_auto_focus_trigger(client
));
835 dev_dbg(&client
->dev
, "format : %d\n", priv
->format
->code
);
836 dev_dbg(&client
->dev
, "size : %d x %d\n",
840 CLOCK_INFO(client
, EXT_CLOCK
);
845 static int mt9t112_set_params(struct mt9t112_priv
*priv
,
846 const struct v4l2_rect
*rect
,
854 for (i
= 0; i
< priv
->num_formats
; i
++)
855 if (mt9t112_cfmts
[i
].code
== code
)
858 if (i
== priv
->num_formats
)
866 v4l_bound_align_image(&priv
->frame
.width
, 0, MAX_WIDTH
, 0,
867 &priv
->frame
.height
, 0, MAX_HEIGHT
, 0, 0);
869 priv
->format
= mt9t112_cfmts
+ i
;
874 static int mt9t112_get_selection(struct v4l2_subdev
*sd
,
875 struct v4l2_subdev_pad_config
*cfg
,
876 struct v4l2_subdev_selection
*sel
)
878 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
879 struct mt9t112_priv
*priv
= to_mt9t112(client
);
881 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
884 switch (sel
->target
) {
885 case V4L2_SEL_TGT_CROP_BOUNDS
:
888 sel
->r
.width
= MAX_WIDTH
;
889 sel
->r
.height
= MAX_HEIGHT
;
891 case V4L2_SEL_TGT_CROP
:
892 sel
->r
= priv
->frame
;
899 static int mt9t112_set_selection(struct v4l2_subdev
*sd
,
900 struct v4l2_subdev_pad_config
*cfg
,
901 struct v4l2_subdev_selection
*sel
)
903 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
904 struct mt9t112_priv
*priv
= to_mt9t112(client
);
905 const struct v4l2_rect
*rect
= &sel
->r
;
907 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
||
908 sel
->target
!= V4L2_SEL_TGT_CROP
)
911 return mt9t112_set_params(priv
, rect
, priv
->format
->code
);
914 static int mt9t112_get_fmt(struct v4l2_subdev
*sd
,
915 struct v4l2_subdev_pad_config
*cfg
,
916 struct v4l2_subdev_format
*format
)
918 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
919 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
920 struct mt9t112_priv
*priv
= to_mt9t112(client
);
925 mf
->width
= priv
->frame
.width
;
926 mf
->height
= priv
->frame
.height
;
927 mf
->colorspace
= priv
->format
->colorspace
;
928 mf
->code
= priv
->format
->code
;
929 mf
->field
= V4L2_FIELD_NONE
;
934 static int mt9t112_s_fmt(struct v4l2_subdev
*sd
,
935 struct v4l2_mbus_framefmt
*mf
)
937 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
938 struct mt9t112_priv
*priv
= to_mt9t112(client
);
939 struct v4l2_rect rect
= {
941 .height
= mf
->height
,
942 .left
= priv
->frame
.left
,
943 .top
= priv
->frame
.top
,
947 ret
= mt9t112_set_params(priv
, &rect
, mf
->code
);
950 mf
->colorspace
= priv
->format
->colorspace
;
955 static int mt9t112_set_fmt(struct v4l2_subdev
*sd
,
956 struct v4l2_subdev_pad_config
*cfg
,
957 struct v4l2_subdev_format
*format
)
959 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
960 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
961 struct mt9t112_priv
*priv
= to_mt9t112(client
);
967 for (i
= 0; i
< priv
->num_formats
; i
++)
968 if (mt9t112_cfmts
[i
].code
== mf
->code
)
971 if (i
== priv
->num_formats
) {
972 mf
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
973 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
975 mf
->colorspace
= mt9t112_cfmts
[i
].colorspace
;
978 v4l_bound_align_image(&mf
->width
, 0, MAX_WIDTH
, 0,
979 &mf
->height
, 0, MAX_HEIGHT
, 0, 0);
981 mf
->field
= V4L2_FIELD_NONE
;
983 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
984 return mt9t112_s_fmt(sd
, mf
);
990 static int mt9t112_enum_mbus_code(struct v4l2_subdev
*sd
,
991 struct v4l2_subdev_pad_config
*cfg
,
992 struct v4l2_subdev_mbus_code_enum
*code
)
994 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
995 struct mt9t112_priv
*priv
= to_mt9t112(client
);
997 if (code
->pad
|| code
->index
>= priv
->num_formats
)
1000 code
->code
= mt9t112_cfmts
[code
->index
].code
;
1005 static const struct v4l2_subdev_video_ops mt9t112_subdev_video_ops
= {
1006 .s_stream
= mt9t112_s_stream
,
1009 static const struct v4l2_subdev_pad_ops mt9t112_subdev_pad_ops
= {
1010 .enum_mbus_code
= mt9t112_enum_mbus_code
,
1011 .get_selection
= mt9t112_get_selection
,
1012 .set_selection
= mt9t112_set_selection
,
1013 .get_fmt
= mt9t112_get_fmt
,
1014 .set_fmt
= mt9t112_set_fmt
,
1017 /************************************************************************
1019 ***********************************************************************/
1020 static const struct v4l2_subdev_ops mt9t112_subdev_ops
= {
1021 .core
= &mt9t112_subdev_core_ops
,
1022 .video
= &mt9t112_subdev_video_ops
,
1023 .pad
= &mt9t112_subdev_pad_ops
,
1026 static int mt9t112_camera_probe(struct i2c_client
*client
)
1028 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1029 const char *devname
;
1033 ret
= mt9t112_s_power(&priv
->subdev
, 1);
1037 /* Check and show chip ID. */
1038 mt9t112_reg_read(chipid
, client
, 0x0000);
1042 devname
= "mt9t111";
1043 priv
->num_formats
= 1;
1046 devname
= "mt9t112";
1047 priv
->num_formats
= ARRAY_SIZE(mt9t112_cfmts
);
1050 dev_err(&client
->dev
, "Product ID error %04x\n", chipid
);
1055 dev_info(&client
->dev
, "%s chip ID %04x\n", devname
, chipid
);
1058 mt9t112_s_power(&priv
->subdev
, 0);
1063 static int mt9t112_probe(struct i2c_client
*client
,
1064 const struct i2c_device_id
*did
)
1066 struct mt9t112_priv
*priv
;
1069 if (!client
->dev
.platform_data
) {
1070 dev_err(&client
->dev
, "mt9t112: missing platform data!\n");
1074 priv
= devm_kzalloc(&client
->dev
, sizeof(*priv
), GFP_KERNEL
);
1078 priv
->info
= client
->dev
.platform_data
;
1079 priv
->init_done
= false;
1081 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &mt9t112_subdev_ops
);
1083 priv
->clk
= devm_clk_get(&client
->dev
, "extclk");
1084 if (PTR_ERR(priv
->clk
) == -ENOENT
) {
1086 } else if (IS_ERR(priv
->clk
)) {
1087 dev_err(&client
->dev
, "Unable to get clock \"extclk\"\n");
1088 return PTR_ERR(priv
->clk
);
1091 priv
->standby_gpio
= devm_gpiod_get_optional(&client
->dev
, "standby",
1093 if (IS_ERR(priv
->standby_gpio
)) {
1094 dev_err(&client
->dev
, "Unable to get gpio \"standby\"\n");
1095 return PTR_ERR(priv
->standby_gpio
);
1098 ret
= mt9t112_camera_probe(client
);
1102 return v4l2_async_register_subdev(&priv
->subdev
);
1105 static int mt9t112_remove(struct i2c_client
*client
)
1107 struct mt9t112_priv
*priv
= to_mt9t112(client
);
1109 clk_disable_unprepare(priv
->clk
);
1110 v4l2_async_unregister_subdev(&priv
->subdev
);
1115 static const struct i2c_device_id mt9t112_id
[] = {
1119 MODULE_DEVICE_TABLE(i2c
, mt9t112_id
);
1121 static struct i2c_driver mt9t112_i2c_driver
= {
1125 .probe
= mt9t112_probe
,
1126 .remove
= mt9t112_remove
,
1127 .id_table
= mt9t112_id
,
1130 module_i2c_driver(mt9t112_i2c_driver
);
1132 MODULE_DESCRIPTION("V4L2 driver for MT9T111/MT9T112 camera sensor");
1133 MODULE_AUTHOR("Kuninori Morimoto");
1134 MODULE_LICENSE("GPL v2");