1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung LSI S5C73M3 8M pixel camera driver
5 * Copyright (C) 2012, Samsung Electronics, Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Andrzej Hajda <a.hajda@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/gpio.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/media.h>
17 #include <linux/module.h>
18 #include <linux/of_gpio.h>
19 #include <linux/of_graph.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/spi/spi.h>
24 #include <linux/videodev2.h>
25 #include <media/media-entity.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/v4l2-mediabus.h>
30 #include <media/i2c/s5c73m3.h>
31 #include <media/v4l2-fwnode.h>
36 module_param_named(debug
, s5c73m3_dbg
, int, 0644);
38 static int boot_from_rom
= 1;
39 module_param(boot_from_rom
, int, 0644);
42 module_param(update_fw
, int, 0644);
44 #define S5C73M3_EMBEDDED_DATA_MAXLEN SZ_4K
45 #define S5C73M3_MIPI_DATA_LANES 4
46 #define S5C73M3_CLK_NAME "cis_extclk"
48 static const char * const s5c73m3_supply_names
[S5C73M3_MAX_SUPPLIES
] = {
49 "vdd-int", /* Digital Core supply (1.2V), CAM_ISP_CORE_1.2V */
50 "vdda", /* Analog Core supply (1.2V), CAM_SENSOR_CORE_1.2V */
51 "vdd-reg", /* Regulator input supply (2.8V), CAM_SENSOR_A2.8V */
52 "vddio-host", /* Digital Host I/O power supply (1.8V...2.8V),
53 CAM_ISP_SENSOR_1.8V */
54 "vddio-cis", /* Digital CIS I/O power (1.2V...1.8V),
56 "vdd-af", /* Lens, CAM_AF_2.8V */
59 static const struct s5c73m3_frame_size s5c73m3_isp_resolutions
[] = {
60 { 320, 240, COMM_CHG_MODE_YUV_320_240
},
61 { 352, 288, COMM_CHG_MODE_YUV_352_288
},
62 { 640, 480, COMM_CHG_MODE_YUV_640_480
},
63 { 880, 720, COMM_CHG_MODE_YUV_880_720
},
64 { 960, 720, COMM_CHG_MODE_YUV_960_720
},
65 { 1008, 672, COMM_CHG_MODE_YUV_1008_672
},
66 { 1184, 666, COMM_CHG_MODE_YUV_1184_666
},
67 { 1280, 720, COMM_CHG_MODE_YUV_1280_720
},
68 { 1536, 864, COMM_CHG_MODE_YUV_1536_864
},
69 { 1600, 1200, COMM_CHG_MODE_YUV_1600_1200
},
70 { 1632, 1224, COMM_CHG_MODE_YUV_1632_1224
},
71 { 1920, 1080, COMM_CHG_MODE_YUV_1920_1080
},
72 { 1920, 1440, COMM_CHG_MODE_YUV_1920_1440
},
73 { 2304, 1296, COMM_CHG_MODE_YUV_2304_1296
},
74 { 3264, 2448, COMM_CHG_MODE_YUV_3264_2448
},
77 static const struct s5c73m3_frame_size s5c73m3_jpeg_resolutions
[] = {
78 { 640, 480, COMM_CHG_MODE_JPEG_640_480
},
79 { 800, 450, COMM_CHG_MODE_JPEG_800_450
},
80 { 800, 600, COMM_CHG_MODE_JPEG_800_600
},
81 { 1024, 768, COMM_CHG_MODE_JPEG_1024_768
},
82 { 1280, 720, COMM_CHG_MODE_JPEG_1280_720
},
83 { 1280, 960, COMM_CHG_MODE_JPEG_1280_960
},
84 { 1600, 900, COMM_CHG_MODE_JPEG_1600_900
},
85 { 1600, 1200, COMM_CHG_MODE_JPEG_1600_1200
},
86 { 2048, 1152, COMM_CHG_MODE_JPEG_2048_1152
},
87 { 2048, 1536, COMM_CHG_MODE_JPEG_2048_1536
},
88 { 2560, 1440, COMM_CHG_MODE_JPEG_2560_1440
},
89 { 2560, 1920, COMM_CHG_MODE_JPEG_2560_1920
},
90 { 3264, 1836, COMM_CHG_MODE_JPEG_3264_1836
},
91 { 3264, 2176, COMM_CHG_MODE_JPEG_3264_2176
},
92 { 3264, 2448, COMM_CHG_MODE_JPEG_3264_2448
},
95 static const struct s5c73m3_frame_size
* const s5c73m3_resolutions
[] = {
96 [RES_ISP
] = s5c73m3_isp_resolutions
,
97 [RES_JPEG
] = s5c73m3_jpeg_resolutions
100 static const int s5c73m3_resolutions_len
[] = {
101 [RES_ISP
] = ARRAY_SIZE(s5c73m3_isp_resolutions
),
102 [RES_JPEG
] = ARRAY_SIZE(s5c73m3_jpeg_resolutions
)
105 static const struct s5c73m3_interval s5c73m3_intervals
[] = {
106 { COMM_FRAME_RATE_FIXED_7FPS
, {142857, 1000000}, {3264, 2448} },
107 { COMM_FRAME_RATE_FIXED_15FPS
, {66667, 1000000}, {3264, 2448} },
108 { COMM_FRAME_RATE_FIXED_20FPS
, {50000, 1000000}, {2304, 1296} },
109 { COMM_FRAME_RATE_FIXED_30FPS
, {33333, 1000000}, {2304, 1296} },
112 #define S5C73M3_DEFAULT_FRAME_INTERVAL 3 /* 30 fps */
114 static void s5c73m3_fill_mbus_fmt(struct v4l2_mbus_framefmt
*mf
,
115 const struct s5c73m3_frame_size
*fs
,
118 mf
->width
= fs
->width
;
119 mf
->height
= fs
->height
;
121 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
122 mf
->field
= V4L2_FIELD_NONE
;
125 static int s5c73m3_i2c_write(struct i2c_client
*client
, u16 addr
, u16 data
)
127 u8 buf
[4] = { addr
>> 8, addr
& 0xff, data
>> 8, data
& 0xff };
129 int ret
= i2c_master_send(client
, buf
, sizeof(buf
));
131 v4l_dbg(4, s5c73m3_dbg
, client
, "%s: addr 0x%04x, data 0x%04x\n",
132 __func__
, addr
, data
);
137 return ret
< 0 ? ret
: -EREMOTEIO
;
140 static int s5c73m3_i2c_read(struct i2c_client
*client
, u16 addr
, u16
*data
)
143 u8 rbuf
[2], wbuf
[2] = { addr
>> 8, addr
& 0xff };
144 struct i2c_msg msg
[2] = {
146 .addr
= client
->addr
,
151 .addr
= client
->addr
,
158 * Issue repeated START after writing 2 address bytes and
159 * just one STOP only after reading the data bytes.
161 ret
= i2c_transfer(client
->adapter
, msg
, 2);
163 *data
= be16_to_cpup((__be16
*)rbuf
);
164 v4l2_dbg(4, s5c73m3_dbg
, client
,
165 "%s: addr: 0x%04x, data: 0x%04x\n",
166 __func__
, addr
, *data
);
170 v4l2_err(client
, "I2C read failed: addr: %04x, (%d)\n", addr
, ret
);
172 return ret
>= 0 ? -EREMOTEIO
: ret
;
175 int s5c73m3_write(struct s5c73m3
*state
, u32 addr
, u16 data
)
177 struct i2c_client
*client
= state
->i2c_client
;
180 if ((addr
^ state
->i2c_write_address
) & 0xffff0000) {
181 ret
= s5c73m3_i2c_write(client
, REG_CMDWR_ADDRH
, addr
>> 16);
183 state
->i2c_write_address
= 0;
188 if ((addr
^ state
->i2c_write_address
) & 0xffff) {
189 ret
= s5c73m3_i2c_write(client
, REG_CMDWR_ADDRL
, addr
& 0xffff);
191 state
->i2c_write_address
= 0;
196 state
->i2c_write_address
= addr
;
198 ret
= s5c73m3_i2c_write(client
, REG_CMDBUF_ADDR
, data
);
202 state
->i2c_write_address
+= 2;
207 int s5c73m3_read(struct s5c73m3
*state
, u32 addr
, u16
*data
)
209 struct i2c_client
*client
= state
->i2c_client
;
212 if ((addr
^ state
->i2c_read_address
) & 0xffff0000) {
213 ret
= s5c73m3_i2c_write(client
, REG_CMDRD_ADDRH
, addr
>> 16);
215 state
->i2c_read_address
= 0;
220 if ((addr
^ state
->i2c_read_address
) & 0xffff) {
221 ret
= s5c73m3_i2c_write(client
, REG_CMDRD_ADDRL
, addr
& 0xffff);
223 state
->i2c_read_address
= 0;
228 state
->i2c_read_address
= addr
;
230 ret
= s5c73m3_i2c_read(client
, REG_CMDBUF_ADDR
, data
);
234 state
->i2c_read_address
+= 2;
239 static int s5c73m3_check_status(struct s5c73m3
*state
, unsigned int value
)
241 unsigned long start
= jiffies
;
242 unsigned long end
= start
+ msecs_to_jiffies(2000);
248 ret
= s5c73m3_read(state
, REG_STATUS
, &status
);
249 if (ret
< 0 || status
== value
)
251 usleep_range(500, 1000);
253 } while (time_is_after_jiffies(end
));
256 v4l2_dbg(1, s5c73m3_dbg
, &state
->sensor_sd
,
257 "status check took %dms\n",
258 jiffies_to_msecs(jiffies
- start
));
260 if (ret
== 0 && status
!= value
) {
262 u16 i2c_seq_status
= 0;
264 s5c73m3_read(state
, REG_I2C_STATUS
, &i2c_status
);
265 s5c73m3_read(state
, REG_I2C_SEQ_STATUS
, &i2c_seq_status
);
267 v4l2_err(&state
->sensor_sd
,
268 "wrong status %#x, expected: %#x, i2c_status: %#x/%#x\n",
269 status
, value
, i2c_status
, i2c_seq_status
);
277 int s5c73m3_isp_command(struct s5c73m3
*state
, u16 command
, u16 data
)
281 ret
= s5c73m3_check_status(state
, REG_STATUS_ISP_COMMAND_COMPLETED
);
285 ret
= s5c73m3_write(state
, 0x00095000, command
);
289 ret
= s5c73m3_write(state
, 0x00095002, data
);
293 return s5c73m3_write(state
, REG_STATUS
, 0x0001);
296 static int s5c73m3_isp_comm_result(struct s5c73m3
*state
, u16 command
,
299 return s5c73m3_read(state
, COMM_RESULT_OFFSET
+ command
, data
);
302 static int s5c73m3_set_af_softlanding(struct s5c73m3
*state
)
304 unsigned long start
= jiffies
;
310 ret
= s5c73m3_isp_command(state
, COMM_AF_SOFTLANDING
,
311 COMM_AF_SOFTLANDING_ON
);
313 v4l2_info(&state
->sensor_sd
, "AF soft-landing failed\n");
318 ret
= s5c73m3_isp_comm_result(state
, COMM_AF_SOFTLANDING
,
324 if (af_softlanding
== COMM_AF_SOFTLANDING_RES_COMPLETE
) {
336 v4l2_info(&state
->sensor_sd
, "AF soft-landing %s after %dms\n",
337 msg
, jiffies_to_msecs(jiffies
- start
));
342 static int s5c73m3_load_fw(struct v4l2_subdev
*sd
)
344 struct s5c73m3
*state
= sensor_sd_to_s5c73m3(sd
);
345 struct i2c_client
*client
= state
->i2c_client
;
346 const struct firmware
*fw
;
350 snprintf(fw_name
, sizeof(fw_name
), "SlimISP_%.2s.bin",
351 state
->fw_file_version
);
352 ret
= request_firmware(&fw
, fw_name
, &client
->dev
);
354 v4l2_err(sd
, "Firmware request failed (%s)\n", fw_name
);
358 v4l2_info(sd
, "Loading firmware (%s, %zu B)\n", fw_name
, fw
->size
);
360 ret
= s5c73m3_spi_write(state
, fw
->data
, fw
->size
, 64);
363 state
->isp_ready
= 1;
365 v4l2_err(sd
, "SPI write failed\n");
367 release_firmware(fw
);
372 static int s5c73m3_set_frame_size(struct s5c73m3
*state
)
374 const struct s5c73m3_frame_size
*prev_size
=
375 state
->sensor_pix_size
[RES_ISP
];
376 const struct s5c73m3_frame_size
*cap_size
=
377 state
->sensor_pix_size
[RES_JPEG
];
378 unsigned int chg_mode
;
380 v4l2_dbg(1, s5c73m3_dbg
, &state
->sensor_sd
,
381 "Preview size: %dx%d, reg_val: 0x%x\n",
382 prev_size
->width
, prev_size
->height
, prev_size
->reg_val
);
384 chg_mode
= prev_size
->reg_val
| COMM_CHG_MODE_NEW
;
386 if (state
->mbus_code
== S5C73M3_JPEG_FMT
) {
387 v4l2_dbg(1, s5c73m3_dbg
, &state
->sensor_sd
,
388 "Capture size: %dx%d, reg_val: 0x%x\n",
389 cap_size
->width
, cap_size
->height
, cap_size
->reg_val
);
390 chg_mode
|= cap_size
->reg_val
;
393 return s5c73m3_isp_command(state
, COMM_CHG_MODE
, chg_mode
);
396 static int s5c73m3_set_frame_rate(struct s5c73m3
*state
)
400 if (state
->ctrls
.stabilization
->val
)
403 if (WARN_ON(state
->fiv
== NULL
))
406 ret
= s5c73m3_isp_command(state
, COMM_FRAME_RATE
, state
->fiv
->fps_reg
);
408 state
->apply_fiv
= 0;
413 static int __s5c73m3_s_stream(struct s5c73m3
*state
, struct v4l2_subdev
*sd
,
419 if (on
&& state
->apply_fmt
) {
420 if (state
->mbus_code
== S5C73M3_JPEG_FMT
)
421 mode
= COMM_IMG_OUTPUT_INTERLEAVED
;
423 mode
= COMM_IMG_OUTPUT_YUV
;
425 ret
= s5c73m3_isp_command(state
, COMM_IMG_OUTPUT
, mode
);
427 ret
= s5c73m3_set_frame_size(state
);
430 state
->apply_fmt
= 0;
433 ret
= s5c73m3_isp_command(state
, COMM_SENSOR_STREAMING
, !!on
);
437 state
->streaming
= !!on
;
442 if (state
->apply_fiv
) {
443 ret
= s5c73m3_set_frame_rate(state
);
445 v4l2_err(sd
, "Error setting frame rate(%d)\n", ret
);
448 return s5c73m3_check_status(state
, REG_STATUS_ISP_COMMAND_COMPLETED
);
451 static int s5c73m3_oif_s_stream(struct v4l2_subdev
*sd
, int on
)
453 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
456 mutex_lock(&state
->lock
);
457 ret
= __s5c73m3_s_stream(state
, sd
, on
);
458 mutex_unlock(&state
->lock
);
463 static int s5c73m3_system_status_wait(struct s5c73m3
*state
, u32 value
,
464 unsigned int delay
, unsigned int steps
)
468 while (steps
-- > 0) {
469 int ret
= s5c73m3_read(state
, 0x30100010, ®
);
474 usleep_range(delay
, delay
+ 25);
479 static int s5c73m3_read_fw_version(struct s5c73m3
*state
)
481 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
486 offset
= state
->isp_ready
? 0x60 : 0;
488 for (i
= 0; i
< S5C73M3_SENSOR_FW_LEN
/ 2; i
++) {
489 ret
= s5c73m3_read(state
, offset
+ i
* 2, data
);
492 state
->sensor_fw
[i
* 2] = (char)(*data
& 0xff);
493 state
->sensor_fw
[i
* 2 + 1] = (char)(*data
>> 8);
495 state
->sensor_fw
[S5C73M3_SENSOR_FW_LEN
] = '\0';
498 for (i
= 0; i
< S5C73M3_SENSOR_TYPE_LEN
/ 2; i
++) {
499 ret
= s5c73m3_read(state
, offset
+ 6 + i
* 2, data
);
502 state
->sensor_type
[i
* 2] = (char)(*data
& 0xff);
503 state
->sensor_type
[i
* 2 + 1] = (char)(*data
>> 8);
505 state
->sensor_type
[S5C73M3_SENSOR_TYPE_LEN
] = '\0';
507 ret
= s5c73m3_read(state
, offset
+ 0x14, data
);
509 ret
= s5c73m3_read(state
, offset
+ 0x16, data
+ 1);
511 state
->fw_size
= data
[0] + (data
[1] << 16);
514 v4l2_info(sd
, "Sensor type: %s, FW version: %s\n",
515 state
->sensor_type
, state
->sensor_fw
);
519 static int s5c73m3_fw_update_from(struct s5c73m3
*state
)
521 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
522 u16 status
= COMM_FW_UPDATE_NOT_READY
;
526 v4l2_warn(sd
, "Updating F-ROM firmware.\n");
528 if (status
== COMM_FW_UPDATE_NOT_READY
) {
529 ret
= s5c73m3_isp_command(state
, COMM_FW_UPDATE
, 0);
534 ret
= s5c73m3_read(state
, 0x00095906, &status
);
538 case COMM_FW_UPDATE_FAIL
:
539 v4l2_warn(sd
, "Updating F-ROM firmware failed.\n");
541 case COMM_FW_UPDATE_SUCCESS
:
542 v4l2_warn(sd
, "Updating F-ROM firmware finished.\n");
547 } while (count
< 500);
549 v4l2_warn(sd
, "Updating F-ROM firmware timed-out.\n");
553 static int s5c73m3_spi_boot(struct s5c73m3
*state
, bool load_fw
)
555 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
559 ret
= s5c73m3_write(state
, 0x30000004, 0xffff);
563 usleep_range(400, 500);
565 /* Check booting status */
566 ret
= s5c73m3_system_status_wait(state
, 0x0c, 100, 3);
568 v4l2_err(sd
, "booting failed: %d\n", ret
);
572 /* P,M,S and Boot Mode */
573 ret
= s5c73m3_write(state
, 0x30100014, 0x2146);
577 ret
= s5c73m3_write(state
, 0x30100010, 0x210c);
581 usleep_range(200, 250);
583 /* Check SPI status */
584 ret
= s5c73m3_system_status_wait(state
, 0x210d, 100, 300);
586 v4l2_err(sd
, "SPI not ready: %d\n", ret
);
588 /* Firmware download over SPI */
593 ret
= s5c73m3_write(state
, 0x30000004, 0xfffd);
598 ret
= s5c73m3_write(state
, 0x301000a4, 0x0183);
603 ret
= s5c73m3_write(state
, 0x30000004, 0xffff);
604 if (ret
< 0 || !load_fw
)
607 ret
= s5c73m3_read_fw_version(state
);
611 if (load_fw
&& update_fw
) {
612 ret
= s5c73m3_fw_update_from(state
);
619 static int s5c73m3_set_timing_register_for_vdd(struct s5c73m3
*state
)
621 static const u32 regs
[][2] = {
622 { 0x30100018, 0x0618 },
623 { 0x3010001c, 0x10c1 },
624 { 0x30100020, 0x249e }
629 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++) {
630 ret
= s5c73m3_write(state
, regs
[i
][0], regs
[i
][1]);
638 static void s5c73m3_set_fw_file_version(struct s5c73m3
*state
)
640 switch (state
->sensor_fw
[0]) {
643 state
->fw_file_version
[0] = 'G';
647 state
->fw_file_version
[0] = 'Z';
651 switch (state
->sensor_fw
[1]) {
653 state
->fw_file_version
[1] = state
->sensor_fw
[1];
658 static int s5c73m3_get_fw_version(struct s5c73m3
*state
)
660 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
664 ret
= s5c73m3_write(state
, 0x30000004, 0xffff);
667 usleep_range(400, 500);
669 /* Check booting status */
670 ret
= s5c73m3_system_status_wait(state
, 0x0c, 100, 3);
673 v4l2_err(sd
, "%s: booting failed: %d\n", __func__
, ret
);
677 /* Change I/O Driver Current in order to read from F-ROM */
678 ret
= s5c73m3_write(state
, 0x30100120, 0x0820);
679 ret
= s5c73m3_write(state
, 0x30100124, 0x0820);
682 ret
= s5c73m3_write(state
, 0x00010418, 0x0008);
684 /* P,M,S and Boot Mode */
685 ret
= s5c73m3_write(state
, 0x30100014, 0x2146);
688 ret
= s5c73m3_write(state
, 0x30100010, 0x230c);
692 usleep_range(200, 250);
694 /* Check SPI status */
695 ret
= s5c73m3_system_status_wait(state
, 0x230e, 100, 300);
697 v4l2_err(sd
, "SPI not ready: %d\n", ret
);
700 ret
= s5c73m3_write(state
, 0x30000004, 0xfffd);
705 ret
= s5c73m3_write(state
, 0x301000a4, 0x0183);
709 s5c73m3_set_timing_register_for_vdd(state
);
711 ret
= s5c73m3_read_fw_version(state
);
713 s5c73m3_set_fw_file_version(state
);
718 static int s5c73m3_rom_boot(struct s5c73m3
*state
, bool load_fw
)
720 static const u32 boot_regs
[][2] = {
721 { 0x3100010c, 0x0044 },
722 { 0x31000108, 0x000d },
723 { 0x31000304, 0x0001 },
724 { 0x00010000, 0x5800 },
725 { 0x00010002, 0x0002 },
726 { 0x31000000, 0x0001 },
727 { 0x30100014, 0x1b85 },
728 { 0x30100010, 0x230c }
730 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
734 ret
= s5c73m3_write(state
, 0x30000004, 0xffff);
737 usleep_range(400, 450);
739 /* Check booting status */
740 ret
= s5c73m3_system_status_wait(state
, 0x0c, 100, 4);
742 v4l2_err(sd
, "Booting failed: %d\n", ret
);
746 for (i
= 0; i
< ARRAY_SIZE(boot_regs
); i
++) {
747 ret
= s5c73m3_write(state
, boot_regs
[i
][0], boot_regs
[i
][1]);
753 /* Check the binary read status */
754 ret
= s5c73m3_system_status_wait(state
, 0x230e, 1000, 150);
756 v4l2_err(sd
, "Binary read failed: %d\n", ret
);
761 ret
= s5c73m3_write(state
, 0x30000004, 0xfffd);
765 ret
= s5c73m3_write(state
, 0x301000a4, 0x0183);
769 ret
= s5c73m3_write(state
, 0x30000004, 0xffff);
773 state
->isp_ready
= 1;
775 return s5c73m3_read_fw_version(state
);
778 static int s5c73m3_isp_init(struct s5c73m3
*state
)
782 state
->i2c_read_address
= 0;
783 state
->i2c_write_address
= 0;
785 ret
= s5c73m3_i2c_write(state
->i2c_client
, AHB_MSB_ADDR_PTR
, 0x3310);
790 return s5c73m3_rom_boot(state
, true);
792 return s5c73m3_spi_boot(state
, true);
795 static const struct s5c73m3_frame_size
*s5c73m3_find_frame_size(
796 struct v4l2_mbus_framefmt
*fmt
,
797 enum s5c73m3_resolution_types idx
)
799 const struct s5c73m3_frame_size
*fs
;
800 const struct s5c73m3_frame_size
*best_fs
;
801 int best_dist
= INT_MAX
;
804 fs
= s5c73m3_resolutions
[idx
];
806 for (i
= 0; i
< s5c73m3_resolutions_len
[idx
]; ++i
) {
807 int dist
= abs(fs
->width
- fmt
->width
) +
808 abs(fs
->height
- fmt
->height
);
809 if (dist
< best_dist
) {
819 static void s5c73m3_oif_try_format(struct s5c73m3
*state
,
820 struct v4l2_subdev_pad_config
*cfg
,
821 struct v4l2_subdev_format
*fmt
,
822 const struct s5c73m3_frame_size
**fs
)
824 struct v4l2_subdev
*sd
= &state
->sensor_sd
;
829 *fs
= s5c73m3_find_frame_size(&fmt
->format
, RES_ISP
);
830 code
= S5C73M3_ISP_FMT
;
833 *fs
= s5c73m3_find_frame_size(&fmt
->format
, RES_JPEG
);
834 code
= S5C73M3_JPEG_FMT
;
838 if (fmt
->format
.code
== S5C73M3_JPEG_FMT
)
839 code
= S5C73M3_JPEG_FMT
;
841 code
= S5C73M3_ISP_FMT
;
843 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
844 *fs
= state
->oif_pix_size
[RES_ISP
];
846 *fs
= s5c73m3_find_frame_size(
847 v4l2_subdev_get_try_format(sd
, cfg
,
853 s5c73m3_fill_mbus_fmt(&fmt
->format
, *fs
, code
);
856 static void s5c73m3_try_format(struct s5c73m3
*state
,
857 struct v4l2_subdev_pad_config
*cfg
,
858 struct v4l2_subdev_format
*fmt
,
859 const struct s5c73m3_frame_size
**fs
)
863 if (fmt
->pad
== S5C73M3_ISP_PAD
) {
864 *fs
= s5c73m3_find_frame_size(&fmt
->format
, RES_ISP
);
865 code
= S5C73M3_ISP_FMT
;
867 *fs
= s5c73m3_find_frame_size(&fmt
->format
, RES_JPEG
);
868 code
= S5C73M3_JPEG_FMT
;
871 s5c73m3_fill_mbus_fmt(&fmt
->format
, *fs
, code
);
874 static int s5c73m3_oif_g_frame_interval(struct v4l2_subdev
*sd
,
875 struct v4l2_subdev_frame_interval
*fi
)
877 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
879 if (fi
->pad
!= OIF_SOURCE_PAD
)
882 mutex_lock(&state
->lock
);
883 fi
->interval
= state
->fiv
->interval
;
884 mutex_unlock(&state
->lock
);
889 static int __s5c73m3_set_frame_interval(struct s5c73m3
*state
,
890 struct v4l2_subdev_frame_interval
*fi
)
892 const struct s5c73m3_frame_size
*prev_size
=
893 state
->sensor_pix_size
[RES_ISP
];
894 const struct s5c73m3_interval
*fiv
= &s5c73m3_intervals
[0];
895 unsigned int ret
, min_err
= UINT_MAX
;
896 unsigned int i
, fr_time
;
898 if (fi
->interval
.denominator
== 0)
901 fr_time
= fi
->interval
.numerator
* 1000 / fi
->interval
.denominator
;
903 for (i
= 0; i
< ARRAY_SIZE(s5c73m3_intervals
); i
++) {
904 const struct s5c73m3_interval
*iv
= &s5c73m3_intervals
[i
];
906 if (prev_size
->width
> iv
->size
.width
||
907 prev_size
->height
> iv
->size
.height
)
910 ret
= abs(iv
->interval
.numerator
/ 1000 - fr_time
);
918 v4l2_dbg(1, s5c73m3_dbg
, &state
->sensor_sd
,
919 "Changed frame interval to %u us\n", fiv
->interval
.numerator
);
923 static int s5c73m3_oif_s_frame_interval(struct v4l2_subdev
*sd
,
924 struct v4l2_subdev_frame_interval
*fi
)
926 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
929 if (fi
->pad
!= OIF_SOURCE_PAD
)
932 v4l2_dbg(1, s5c73m3_dbg
, sd
, "Setting %d/%d frame interval\n",
933 fi
->interval
.numerator
, fi
->interval
.denominator
);
935 mutex_lock(&state
->lock
);
937 ret
= __s5c73m3_set_frame_interval(state
, fi
);
939 if (state
->streaming
)
940 ret
= s5c73m3_set_frame_rate(state
);
942 state
->apply_fiv
= 1;
944 mutex_unlock(&state
->lock
);
948 static int s5c73m3_oif_enum_frame_interval(struct v4l2_subdev
*sd
,
949 struct v4l2_subdev_pad_config
*cfg
,
950 struct v4l2_subdev_frame_interval_enum
*fie
)
952 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
953 const struct s5c73m3_interval
*fi
;
956 if (fie
->pad
!= OIF_SOURCE_PAD
)
958 if (fie
->index
>= ARRAY_SIZE(s5c73m3_intervals
))
961 mutex_lock(&state
->lock
);
962 fi
= &s5c73m3_intervals
[fie
->index
];
963 if (fie
->width
> fi
->size
.width
|| fie
->height
> fi
->size
.height
)
966 fie
->interval
= fi
->interval
;
967 mutex_unlock(&state
->lock
);
972 static int s5c73m3_oif_get_pad_code(int pad
, int index
)
974 if (pad
== OIF_SOURCE_PAD
) {
977 return (index
== 0) ? S5C73M3_ISP_FMT
: S5C73M3_JPEG_FMT
;
983 return (pad
== OIF_ISP_PAD
) ? S5C73M3_ISP_FMT
: S5C73M3_JPEG_FMT
;
986 static int s5c73m3_get_fmt(struct v4l2_subdev
*sd
,
987 struct v4l2_subdev_pad_config
*cfg
,
988 struct v4l2_subdev_format
*fmt
)
990 struct s5c73m3
*state
= sensor_sd_to_s5c73m3(sd
);
991 const struct s5c73m3_frame_size
*fs
;
994 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
995 fmt
->format
= *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
999 mutex_lock(&state
->lock
);
1002 case S5C73M3_ISP_PAD
:
1003 code
= S5C73M3_ISP_FMT
;
1004 fs
= state
->sensor_pix_size
[RES_ISP
];
1006 case S5C73M3_JPEG_PAD
:
1007 code
= S5C73M3_JPEG_FMT
;
1008 fs
= state
->sensor_pix_size
[RES_JPEG
];
1011 mutex_unlock(&state
->lock
);
1014 s5c73m3_fill_mbus_fmt(&fmt
->format
, fs
, code
);
1016 mutex_unlock(&state
->lock
);
1020 static int s5c73m3_oif_get_fmt(struct v4l2_subdev
*sd
,
1021 struct v4l2_subdev_pad_config
*cfg
,
1022 struct v4l2_subdev_format
*fmt
)
1024 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1025 const struct s5c73m3_frame_size
*fs
;
1028 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1029 fmt
->format
= *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1033 mutex_lock(&state
->lock
);
1037 code
= S5C73M3_ISP_FMT
;
1038 fs
= state
->oif_pix_size
[RES_ISP
];
1041 code
= S5C73M3_JPEG_FMT
;
1042 fs
= state
->oif_pix_size
[RES_JPEG
];
1044 case OIF_SOURCE_PAD
:
1045 code
= state
->mbus_code
;
1046 fs
= state
->oif_pix_size
[RES_ISP
];
1049 mutex_unlock(&state
->lock
);
1052 s5c73m3_fill_mbus_fmt(&fmt
->format
, fs
, code
);
1054 mutex_unlock(&state
->lock
);
1058 static int s5c73m3_set_fmt(struct v4l2_subdev
*sd
,
1059 struct v4l2_subdev_pad_config
*cfg
,
1060 struct v4l2_subdev_format
*fmt
)
1062 const struct s5c73m3_frame_size
*frame_size
= NULL
;
1063 struct s5c73m3
*state
= sensor_sd_to_s5c73m3(sd
);
1064 struct v4l2_mbus_framefmt
*mf
;
1067 mutex_lock(&state
->lock
);
1069 s5c73m3_try_format(state
, cfg
, fmt
, &frame_size
);
1071 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1072 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1076 case S5C73M3_ISP_PAD
:
1077 state
->sensor_pix_size
[RES_ISP
] = frame_size
;
1079 case S5C73M3_JPEG_PAD
:
1080 state
->sensor_pix_size
[RES_JPEG
] = frame_size
;
1086 if (state
->streaming
)
1089 state
->apply_fmt
= 1;
1092 mutex_unlock(&state
->lock
);
1097 static int s5c73m3_oif_set_fmt(struct v4l2_subdev
*sd
,
1098 struct v4l2_subdev_pad_config
*cfg
,
1099 struct v4l2_subdev_format
*fmt
)
1101 const struct s5c73m3_frame_size
*frame_size
= NULL
;
1102 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1103 struct v4l2_mbus_framefmt
*mf
;
1106 mutex_lock(&state
->lock
);
1108 s5c73m3_oif_try_format(state
, cfg
, fmt
, &frame_size
);
1110 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1111 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1113 if (fmt
->pad
== OIF_ISP_PAD
) {
1114 mf
= v4l2_subdev_get_try_format(sd
, cfg
, OIF_SOURCE_PAD
);
1115 mf
->width
= fmt
->format
.width
;
1116 mf
->height
= fmt
->format
.height
;
1121 state
->oif_pix_size
[RES_ISP
] = frame_size
;
1124 state
->oif_pix_size
[RES_JPEG
] = frame_size
;
1126 case OIF_SOURCE_PAD
:
1127 state
->mbus_code
= fmt
->format
.code
;
1133 if (state
->streaming
)
1136 state
->apply_fmt
= 1;
1139 mutex_unlock(&state
->lock
);
1144 static int s5c73m3_oif_get_frame_desc(struct v4l2_subdev
*sd
, unsigned int pad
,
1145 struct v4l2_mbus_frame_desc
*fd
)
1147 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1150 if (pad
!= OIF_SOURCE_PAD
|| fd
== NULL
)
1153 mutex_lock(&state
->lock
);
1154 fd
->num_entries
= 2;
1155 for (i
= 0; i
< fd
->num_entries
; i
++)
1156 fd
->entry
[i
] = state
->frame_desc
.entry
[i
];
1157 mutex_unlock(&state
->lock
);
1162 static int s5c73m3_oif_set_frame_desc(struct v4l2_subdev
*sd
, unsigned int pad
,
1163 struct v4l2_mbus_frame_desc
*fd
)
1165 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1166 struct v4l2_mbus_frame_desc
*frame_desc
= &state
->frame_desc
;
1169 if (pad
!= OIF_SOURCE_PAD
|| fd
== NULL
)
1172 fd
->entry
[0].length
= 10 * SZ_1M
;
1173 fd
->entry
[1].length
= max_t(u32
, fd
->entry
[1].length
,
1174 S5C73M3_EMBEDDED_DATA_MAXLEN
);
1175 fd
->num_entries
= 2;
1177 mutex_lock(&state
->lock
);
1178 for (i
= 0; i
< fd
->num_entries
; i
++)
1179 frame_desc
->entry
[i
] = fd
->entry
[i
];
1180 mutex_unlock(&state
->lock
);
1185 static int s5c73m3_enum_mbus_code(struct v4l2_subdev
*sd
,
1186 struct v4l2_subdev_pad_config
*cfg
,
1187 struct v4l2_subdev_mbus_code_enum
*code
)
1189 static const int codes
[] = {
1190 [S5C73M3_ISP_PAD
] = S5C73M3_ISP_FMT
,
1191 [S5C73M3_JPEG_PAD
] = S5C73M3_JPEG_FMT
};
1193 if (code
->index
> 0 || code
->pad
>= S5C73M3_NUM_PADS
)
1196 code
->code
= codes
[code
->pad
];
1201 static int s5c73m3_oif_enum_mbus_code(struct v4l2_subdev
*sd
,
1202 struct v4l2_subdev_pad_config
*cfg
,
1203 struct v4l2_subdev_mbus_code_enum
*code
)
1207 ret
= s5c73m3_oif_get_pad_code(code
->pad
, code
->index
);
1216 static int s5c73m3_enum_frame_size(struct v4l2_subdev
*sd
,
1217 struct v4l2_subdev_pad_config
*cfg
,
1218 struct v4l2_subdev_frame_size_enum
*fse
)
1222 if (fse
->pad
== S5C73M3_ISP_PAD
) {
1223 if (fse
->code
!= S5C73M3_ISP_FMT
)
1227 if (fse
->code
!= S5C73M3_JPEG_FMT
)
1232 if (fse
->index
>= s5c73m3_resolutions_len
[idx
])
1235 fse
->min_width
= s5c73m3_resolutions
[idx
][fse
->index
].width
;
1236 fse
->max_width
= fse
->min_width
;
1237 fse
->max_height
= s5c73m3_resolutions
[idx
][fse
->index
].height
;
1238 fse
->min_height
= fse
->max_height
;
1243 static int s5c73m3_oif_enum_frame_size(struct v4l2_subdev
*sd
,
1244 struct v4l2_subdev_pad_config
*cfg
,
1245 struct v4l2_subdev_frame_size_enum
*fse
)
1247 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1250 if (fse
->pad
== OIF_SOURCE_PAD
) {
1254 switch (fse
->code
) {
1255 case S5C73M3_JPEG_FMT
:
1256 case S5C73M3_ISP_FMT
: {
1259 if (fse
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1260 struct v4l2_mbus_framefmt
*mf
;
1262 mf
= v4l2_subdev_get_try_format(sd
, cfg
,
1268 const struct s5c73m3_frame_size
*fs
;
1270 fs
= state
->oif_pix_size
[RES_ISP
];
1274 fse
->max_width
= fse
->min_width
= w
;
1275 fse
->max_height
= fse
->min_height
= h
;
1283 if (fse
->code
!= s5c73m3_oif_get_pad_code(fse
->pad
, 0))
1286 if (fse
->pad
== OIF_JPEG_PAD
)
1291 if (fse
->index
>= s5c73m3_resolutions_len
[idx
])
1294 fse
->min_width
= s5c73m3_resolutions
[idx
][fse
->index
].width
;
1295 fse
->max_width
= fse
->min_width
;
1296 fse
->max_height
= s5c73m3_resolutions
[idx
][fse
->index
].height
;
1297 fse
->min_height
= fse
->max_height
;
1302 static int s5c73m3_oif_log_status(struct v4l2_subdev
*sd
)
1304 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1306 v4l2_ctrl_handler_log_status(sd
->ctrl_handler
, sd
->name
);
1308 v4l2_info(sd
, "power: %d, apply_fmt: %d\n", state
->power
,
1314 static int s5c73m3_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1316 struct v4l2_mbus_framefmt
*mf
;
1318 mf
= v4l2_subdev_get_try_format(sd
, fh
->pad
, S5C73M3_ISP_PAD
);
1319 s5c73m3_fill_mbus_fmt(mf
, &s5c73m3_isp_resolutions
[1],
1322 mf
= v4l2_subdev_get_try_format(sd
, fh
->pad
, S5C73M3_JPEG_PAD
);
1323 s5c73m3_fill_mbus_fmt(mf
, &s5c73m3_jpeg_resolutions
[1],
1329 static int s5c73m3_oif_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1331 struct v4l2_mbus_framefmt
*mf
;
1333 mf
= v4l2_subdev_get_try_format(sd
, fh
->pad
, OIF_ISP_PAD
);
1334 s5c73m3_fill_mbus_fmt(mf
, &s5c73m3_isp_resolutions
[1],
1337 mf
= v4l2_subdev_get_try_format(sd
, fh
->pad
, OIF_JPEG_PAD
);
1338 s5c73m3_fill_mbus_fmt(mf
, &s5c73m3_jpeg_resolutions
[1],
1341 mf
= v4l2_subdev_get_try_format(sd
, fh
->pad
, OIF_SOURCE_PAD
);
1342 s5c73m3_fill_mbus_fmt(mf
, &s5c73m3_isp_resolutions
[1],
1347 static int s5c73m3_gpio_set_value(struct s5c73m3
*priv
, int id
, u32 val
)
1349 if (!gpio_is_valid(priv
->gpio
[id
].gpio
))
1351 gpio_set_value(priv
->gpio
[id
].gpio
, !!val
);
1355 static int s5c73m3_gpio_assert(struct s5c73m3
*priv
, int id
)
1357 return s5c73m3_gpio_set_value(priv
, id
, priv
->gpio
[id
].level
);
1360 static int s5c73m3_gpio_deassert(struct s5c73m3
*priv
, int id
)
1362 return s5c73m3_gpio_set_value(priv
, id
, !priv
->gpio
[id
].level
);
1365 static int __s5c73m3_power_on(struct s5c73m3
*state
)
1369 for (i
= 0; i
< S5C73M3_MAX_SUPPLIES
; i
++) {
1370 ret
= regulator_enable(state
->supplies
[i
].consumer
);
1375 ret
= clk_set_rate(state
->clock
, state
->mclk_frequency
);
1379 ret
= clk_prepare_enable(state
->clock
);
1383 v4l2_dbg(1, s5c73m3_dbg
, &state
->oif_sd
, "clock frequency: %ld\n",
1384 clk_get_rate(state
->clock
));
1386 s5c73m3_gpio_deassert(state
, STBY
);
1387 usleep_range(100, 200);
1389 s5c73m3_gpio_deassert(state
, RST
);
1390 usleep_range(50, 100);
1395 for (--i
; i
>= 0; i
--)
1396 regulator_disable(state
->supplies
[i
].consumer
);
1400 static int __s5c73m3_power_off(struct s5c73m3
*state
)
1404 if (s5c73m3_gpio_assert(state
, RST
))
1405 usleep_range(10, 50);
1407 if (s5c73m3_gpio_assert(state
, STBY
))
1408 usleep_range(100, 200);
1410 clk_disable_unprepare(state
->clock
);
1412 state
->streaming
= 0;
1413 state
->isp_ready
= 0;
1415 for (i
= S5C73M3_MAX_SUPPLIES
- 1; i
>= 0; i
--) {
1416 ret
= regulator_disable(state
->supplies
[i
].consumer
);
1423 for (++i
; i
< S5C73M3_MAX_SUPPLIES
; i
++) {
1424 int r
= regulator_enable(state
->supplies
[i
].consumer
);
1426 v4l2_err(&state
->oif_sd
, "Failed to re-enable %s: %d\n",
1427 state
->supplies
[i
].supply
, r
);
1430 clk_prepare_enable(state
->clock
);
1434 static int s5c73m3_oif_set_power(struct v4l2_subdev
*sd
, int on
)
1436 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1439 mutex_lock(&state
->lock
);
1441 if (on
&& !state
->power
) {
1442 ret
= __s5c73m3_power_on(state
);
1444 ret
= s5c73m3_isp_init(state
);
1446 state
->apply_fiv
= 1;
1447 state
->apply_fmt
= 1;
1449 } else if (state
->power
== !on
) {
1450 ret
= s5c73m3_set_af_softlanding(state
);
1452 ret
= __s5c73m3_power_off(state
);
1454 v4l2_err(sd
, "Soft landing lens failed\n");
1457 state
->power
+= on
? 1 : -1;
1459 v4l2_dbg(1, s5c73m3_dbg
, sd
, "%s: power: %d\n",
1460 __func__
, state
->power
);
1462 mutex_unlock(&state
->lock
);
1466 static int s5c73m3_oif_registered(struct v4l2_subdev
*sd
)
1468 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1471 ret
= v4l2_device_register_subdev(sd
->v4l2_dev
, &state
->sensor_sd
);
1473 v4l2_err(sd
->v4l2_dev
, "Failed to register %s\n",
1474 state
->oif_sd
.name
);
1478 ret
= media_create_pad_link(&state
->sensor_sd
.entity
,
1479 S5C73M3_ISP_PAD
, &state
->oif_sd
.entity
, OIF_ISP_PAD
,
1480 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
1482 ret
= media_create_pad_link(&state
->sensor_sd
.entity
,
1483 S5C73M3_JPEG_PAD
, &state
->oif_sd
.entity
, OIF_JPEG_PAD
,
1484 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
1489 static void s5c73m3_oif_unregistered(struct v4l2_subdev
*sd
)
1491 struct s5c73m3
*state
= oif_sd_to_s5c73m3(sd
);
1492 v4l2_device_unregister_subdev(&state
->sensor_sd
);
1495 static const struct v4l2_subdev_internal_ops s5c73m3_internal_ops
= {
1496 .open
= s5c73m3_open
,
1499 static const struct v4l2_subdev_pad_ops s5c73m3_pad_ops
= {
1500 .enum_mbus_code
= s5c73m3_enum_mbus_code
,
1501 .enum_frame_size
= s5c73m3_enum_frame_size
,
1502 .get_fmt
= s5c73m3_get_fmt
,
1503 .set_fmt
= s5c73m3_set_fmt
,
1506 static const struct v4l2_subdev_ops s5c73m3_subdev_ops
= {
1507 .pad
= &s5c73m3_pad_ops
,
1510 static const struct v4l2_subdev_internal_ops oif_internal_ops
= {
1511 .registered
= s5c73m3_oif_registered
,
1512 .unregistered
= s5c73m3_oif_unregistered
,
1513 .open
= s5c73m3_oif_open
,
1516 static const struct v4l2_subdev_pad_ops s5c73m3_oif_pad_ops
= {
1517 .enum_mbus_code
= s5c73m3_oif_enum_mbus_code
,
1518 .enum_frame_size
= s5c73m3_oif_enum_frame_size
,
1519 .enum_frame_interval
= s5c73m3_oif_enum_frame_interval
,
1520 .get_fmt
= s5c73m3_oif_get_fmt
,
1521 .set_fmt
= s5c73m3_oif_set_fmt
,
1522 .get_frame_desc
= s5c73m3_oif_get_frame_desc
,
1523 .set_frame_desc
= s5c73m3_oif_set_frame_desc
,
1526 static const struct v4l2_subdev_core_ops s5c73m3_oif_core_ops
= {
1527 .s_power
= s5c73m3_oif_set_power
,
1528 .log_status
= s5c73m3_oif_log_status
,
1531 static const struct v4l2_subdev_video_ops s5c73m3_oif_video_ops
= {
1532 .s_stream
= s5c73m3_oif_s_stream
,
1533 .g_frame_interval
= s5c73m3_oif_g_frame_interval
,
1534 .s_frame_interval
= s5c73m3_oif_s_frame_interval
,
1537 static const struct v4l2_subdev_ops oif_subdev_ops
= {
1538 .core
= &s5c73m3_oif_core_ops
,
1539 .pad
= &s5c73m3_oif_pad_ops
,
1540 .video
= &s5c73m3_oif_video_ops
,
1543 static int s5c73m3_configure_gpios(struct s5c73m3
*state
)
1545 static const char * const gpio_names
[] = {
1546 "S5C73M3_STBY", "S5C73M3_RST"
1548 struct i2c_client
*c
= state
->i2c_client
;
1549 struct s5c73m3_gpio
*g
= state
->gpio
;
1552 for (i
= 0; i
< GPIO_NUM
; ++i
) {
1553 unsigned int flags
= GPIOF_DIR_OUT
;
1555 flags
|= GPIOF_INIT_HIGH
;
1556 ret
= devm_gpio_request_one(&c
->dev
, g
[i
].gpio
, flags
,
1559 v4l2_err(c
, "failed to request gpio %s\n",
1567 static int s5c73m3_parse_gpios(struct s5c73m3
*state
)
1569 static const char * const prop_names
[] = {
1570 "standby-gpios", "xshutdown-gpios",
1572 struct device
*dev
= &state
->i2c_client
->dev
;
1573 struct device_node
*node
= dev
->of_node
;
1576 for (i
= 0; i
< GPIO_NUM
; ++i
) {
1577 enum of_gpio_flags of_flags
;
1579 ret
= of_get_named_gpio_flags(node
, prop_names
[i
],
1582 dev_err(dev
, "failed to parse %s DT property\n",
1586 state
->gpio
[i
].gpio
= ret
;
1587 state
->gpio
[i
].level
= !(of_flags
& OF_GPIO_ACTIVE_LOW
);
1592 static int s5c73m3_get_platform_data(struct s5c73m3
*state
)
1594 struct device
*dev
= &state
->i2c_client
->dev
;
1595 const struct s5c73m3_platform_data
*pdata
= dev
->platform_data
;
1596 struct device_node
*node
= dev
->of_node
;
1597 struct device_node
*node_ep
;
1598 struct v4l2_fwnode_endpoint ep
= { .bus_type
= 0 };
1603 dev_err(dev
, "Platform data not specified\n");
1607 state
->mclk_frequency
= pdata
->mclk_frequency
;
1608 state
->gpio
[STBY
] = pdata
->gpio_stby
;
1609 state
->gpio
[RST
] = pdata
->gpio_reset
;
1613 state
->clock
= devm_clk_get(dev
, S5C73M3_CLK_NAME
);
1614 if (IS_ERR(state
->clock
))
1615 return PTR_ERR(state
->clock
);
1617 if (of_property_read_u32(node
, "clock-frequency",
1618 &state
->mclk_frequency
)) {
1619 state
->mclk_frequency
= S5C73M3_DEFAULT_MCLK_FREQ
;
1620 dev_info(dev
, "using default %u Hz clock frequency\n",
1621 state
->mclk_frequency
);
1624 ret
= s5c73m3_parse_gpios(state
);
1628 node_ep
= of_graph_get_next_endpoint(node
, NULL
);
1630 dev_warn(dev
, "no endpoint defined for node: %pOF\n", node
);
1634 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(node_ep
), &ep
);
1635 of_node_put(node_ep
);
1639 if (ep
.bus_type
!= V4L2_MBUS_CSI2_DPHY
) {
1640 dev_err(dev
, "unsupported bus type\n");
1644 * Number of MIPI CSI-2 data lanes is currently not configurable,
1645 * always a default value of 4 lanes is used.
1647 if (ep
.bus
.mipi_csi2
.num_data_lanes
!= S5C73M3_MIPI_DATA_LANES
)
1648 dev_info(dev
, "falling back to 4 MIPI CSI-2 data lanes\n");
1653 static int s5c73m3_probe(struct i2c_client
*client
)
1655 struct device
*dev
= &client
->dev
;
1656 struct v4l2_subdev
*sd
;
1657 struct v4l2_subdev
*oif_sd
;
1658 struct s5c73m3
*state
;
1661 state
= devm_kzalloc(dev
, sizeof(*state
), GFP_KERNEL
);
1665 state
->i2c_client
= client
;
1666 ret
= s5c73m3_get_platform_data(state
);
1670 mutex_init(&state
->lock
);
1671 sd
= &state
->sensor_sd
;
1672 oif_sd
= &state
->oif_sd
;
1674 v4l2_subdev_init(sd
, &s5c73m3_subdev_ops
);
1675 sd
->owner
= client
->dev
.driver
->owner
;
1676 v4l2_set_subdevdata(sd
, state
);
1677 strscpy(sd
->name
, "S5C73M3", sizeof(sd
->name
));
1679 sd
->internal_ops
= &s5c73m3_internal_ops
;
1680 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1682 state
->sensor_pads
[S5C73M3_JPEG_PAD
].flags
= MEDIA_PAD_FL_SOURCE
;
1683 state
->sensor_pads
[S5C73M3_ISP_PAD
].flags
= MEDIA_PAD_FL_SOURCE
;
1684 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1686 ret
= media_entity_pads_init(&sd
->entity
, S5C73M3_NUM_PADS
,
1687 state
->sensor_pads
);
1691 v4l2_i2c_subdev_init(oif_sd
, client
, &oif_subdev_ops
);
1692 /* Static name; NEVER use in new drivers! */
1693 strscpy(oif_sd
->name
, "S5C73M3-OIF", sizeof(oif_sd
->name
));
1695 oif_sd
->internal_ops
= &oif_internal_ops
;
1696 oif_sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1698 state
->oif_pads
[OIF_ISP_PAD
].flags
= MEDIA_PAD_FL_SINK
;
1699 state
->oif_pads
[OIF_JPEG_PAD
].flags
= MEDIA_PAD_FL_SINK
;
1700 state
->oif_pads
[OIF_SOURCE_PAD
].flags
= MEDIA_PAD_FL_SOURCE
;
1701 oif_sd
->entity
.function
= MEDIA_ENT_F_PROC_VIDEO_SCALER
;
1703 ret
= media_entity_pads_init(&oif_sd
->entity
, OIF_NUM_PADS
,
1708 ret
= s5c73m3_configure_gpios(state
);
1712 for (i
= 0; i
< S5C73M3_MAX_SUPPLIES
; i
++)
1713 state
->supplies
[i
].supply
= s5c73m3_supply_names
[i
];
1715 ret
= devm_regulator_bulk_get(dev
, S5C73M3_MAX_SUPPLIES
,
1718 dev_err(dev
, "failed to get regulators\n");
1722 ret
= s5c73m3_init_controls(state
);
1726 state
->sensor_pix_size
[RES_ISP
] = &s5c73m3_isp_resolutions
[1];
1727 state
->sensor_pix_size
[RES_JPEG
] = &s5c73m3_jpeg_resolutions
[1];
1728 state
->oif_pix_size
[RES_ISP
] = state
->sensor_pix_size
[RES_ISP
];
1729 state
->oif_pix_size
[RES_JPEG
] = state
->sensor_pix_size
[RES_JPEG
];
1731 state
->mbus_code
= S5C73M3_ISP_FMT
;
1733 state
->fiv
= &s5c73m3_intervals
[S5C73M3_DEFAULT_FRAME_INTERVAL
];
1735 state
->fw_file_version
[0] = 'G';
1736 state
->fw_file_version
[1] = 'C';
1738 ret
= s5c73m3_register_spi_driver(state
);
1744 ret
= __s5c73m3_power_on(state
);
1748 ret
= s5c73m3_get_fw_version(state
);
1749 __s5c73m3_power_off(state
);
1752 dev_err(dev
, "Device detection failed: %d\n", ret
);
1756 ret
= v4l2_async_register_subdev(oif_sd
);
1760 v4l2_info(sd
, "%s: completed successfully\n", __func__
);
1764 s5c73m3_unregister_spi_driver(state
);
1766 media_entity_cleanup(&sd
->entity
);
1770 static int s5c73m3_remove(struct i2c_client
*client
)
1772 struct v4l2_subdev
*oif_sd
= i2c_get_clientdata(client
);
1773 struct s5c73m3
*state
= oif_sd_to_s5c73m3(oif_sd
);
1774 struct v4l2_subdev
*sensor_sd
= &state
->sensor_sd
;
1776 v4l2_async_unregister_subdev(oif_sd
);
1778 v4l2_ctrl_handler_free(oif_sd
->ctrl_handler
);
1779 media_entity_cleanup(&oif_sd
->entity
);
1781 v4l2_device_unregister_subdev(sensor_sd
);
1782 media_entity_cleanup(&sensor_sd
->entity
);
1784 s5c73m3_unregister_spi_driver(state
);
1789 static const struct i2c_device_id s5c73m3_id
[] = {
1793 MODULE_DEVICE_TABLE(i2c
, s5c73m3_id
);
1796 static const struct of_device_id s5c73m3_of_match
[] = {
1797 { .compatible
= "samsung,s5c73m3" },
1800 MODULE_DEVICE_TABLE(of
, s5c73m3_of_match
);
1803 static struct i2c_driver s5c73m3_i2c_driver
= {
1805 .of_match_table
= of_match_ptr(s5c73m3_of_match
),
1806 .name
= DRIVER_NAME
,
1808 .probe_new
= s5c73m3_probe
,
1809 .remove
= s5c73m3_remove
,
1810 .id_table
= s5c73m3_id
,
1813 module_i2c_driver(s5c73m3_i2c_driver
);
1815 MODULE_DESCRIPTION("Samsung S5C73M3 camera driver");
1816 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1817 MODULE_LICENSE("GPL");