1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
4 *
5 * lt7911uxc type-c/DP to MIPI CSI-2 bridge driver.
6 *
7 * Author: Jianwei Fan <jianwei.fan@rock-chips.com>
8 *
9 * V0.0X01.0X00 first version.
10 * V0.0X01.0X01 support DPHY 4K60.
11 * V0.0X01.0X02 add CPHY support.
12 * V0.0X01.0X03 add rk3588 dcphy param.
13 * V0.0X01.0X04 add 5K60 support for CPHY.
14 * V0.0X01.0X05 add CSI BGR888 fmt.
15 * V0.0X01.0X06 fix dcphy params and add more fmt.
16 * V0.0X01.0X07
17 * 1.fix driver probe sequence.
18 * 2.set default timing
19 * 3.fix dcphy params
20 * 4.fix hotplug event report
21 *
22 */
23
24 #include <linux/clk.h>
25 #include <linux/delay.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/hdmi.h>
28 #include <linux/i2c.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/of_graph.h>
33 #include <linux/rk-camera-module.h>
34 #include <linux/slab.h>
35 #include <linux/timer.h>
36 #include <linux/v4l2-dv-timings.h>
37 #include <linux/version.h>
38 #include <linux/videodev2.h>
39 #include <linux/workqueue.h>
40 #include <linux/compat.h>
41 #include <media/v4l2-controls_rockchip.h>
42 #include <media/v4l2-ctrls.h>
43 #include <media/v4l2-device.h>
44 #include <media/v4l2-dv-timings.h>
45 #include <media/v4l2-event.h>
46 #include <media/v4l2-fwnode.h>
47
48 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x07)
49
50 static int debug;
51 module_param(debug, int, 0644);
52 MODULE_PARM_DESC(debug, "debug level (0-3)");
53
54 #define I2C_MAX_XFER_SIZE 128
55 #define POLL_INTERVAL_MS 1000
56
57 #define LT7911UXC_LINK_FREQ_1250M 1250000000
58 #define LT7911UXC_LINK_FREQ_860M 860000000
59 #define LT7911UXC_LINK_FREQ_700M 700000000
60 #define LT7911UXC_LINK_FREQ_400M 400000000
61 #define LT7911UXC_LINK_FREQ_300M 300000000
62 #define LT7911UXC_LINK_FREQ_200M 200000000
63 #define LT7911UXC_LINK_FREQ_100M 100000000
64
65 #define LT7911UXC_PIXEL_RATE 800000000
66
67 #define LT7911UXC_CHIPID 0x0119
68 #define CHIPID_REGH 0xe101
69 #define CHIPID_REGL 0xe100
70 #define I2C_EN_REG 0xe0ee
71 #define I2C_ENABLE 0x1
72 #define I2C_DISABLE 0x0
73
74 #define HTOTAL_H 0xe088
75 #define HTOTAL_L 0xe089
76 #define HACT_H 0xe08c
77 #define HACT_L 0xe08d
78
79 #define VTOTAL_H 0xe08a
80 #define VTOTAL_L 0xe08b
81 #define VACT_H 0xe08e
82 #define VACT_L 0xe08f
83
84 #define PCLK_H 0xe085
85 #define PCLK_M 0xe086
86 #define PCLK_L 0xe087
87
88 #define BYTE_PCLK_H 0xe092
89 #define BYTE_PCLK_M 0xe093
90 #define BYTE_PCLK_L 0xe094
91
92 #define AUDIO_FS_VALUE_H 0xe090
93 #define AUDIO_FS_VALUE_L 0xe091
94
95 //CPHY timing
96 #define CLK_ZERO_REG 0xf9a7
97 #define CLK_PRE_REG 0xf9a8
98 #define CLK_POST_REG 0xf9a9
99 #define HS_LPX_REG 0xf9a4
100 #define HS_PREP_REG 0xf9a5
101 #define HS_TRAIL 0xf9a6
102 #define HS_RQST_PRE_REG 0xf98a
103
104 #define STREAM_CTL 0xe0b0
105 #define ENABLE_STREAM 0x01
106 #define DISABLE_STREAM 0x00
107
108 #ifdef LT7911UXC_OUT_RGB
109 #define LT7911UXC_MEDIA_BUS_FMT MEDIA_BUS_FMT_BGR888_1X24
110 #else
111 #define LT7911UXC_MEDIA_BUS_FMT MEDIA_BUS_FMT_UYVY8_2X8
112 #endif
113
114 #define LT7911UXC_NAME "LT7911UXC"
115
116 static const s64 link_freq_menu_items[] = {
117 LT7911UXC_LINK_FREQ_1250M,
118 LT7911UXC_LINK_FREQ_860M,
119 LT7911UXC_LINK_FREQ_700M,
120 LT7911UXC_LINK_FREQ_400M,
121 LT7911UXC_LINK_FREQ_300M,
122 LT7911UXC_LINK_FREQ_200M,
123 LT7911UXC_LINK_FREQ_100M,
124 };
125
126 struct lt7911uxc {
127 struct v4l2_fwnode_bus_mipi_csi2 bus;
128 struct v4l2_subdev sd;
129 struct media_pad pad;
130 struct v4l2_ctrl_handler hdl;
131 struct i2c_client *i2c_client;
132 struct mutex confctl_mutex;
133 struct v4l2_ctrl *detect_tx_5v_ctrl;
134 struct v4l2_ctrl *audio_sampling_rate_ctrl;
135 struct v4l2_ctrl *audio_present_ctrl;
136 struct v4l2_ctrl *link_freq;
137 struct v4l2_ctrl *pixel_rate;
138 struct delayed_work delayed_work_hotplug;
139 struct delayed_work delayed_work_res_change;
140 struct v4l2_dv_timings timings;
141 struct clk *xvclk;
142 struct gpio_desc *reset_gpio;
143 struct gpio_desc *plugin_det_gpio;
144 struct gpio_desc *power_gpio;
145 struct work_struct work_i2c_poll;
146 struct timer_list timer;
147 const char *module_facing;
148 const char *module_name;
149 const char *len_name;
150 const struct lt7911uxc_mode *cur_mode;
151 const struct lt7911uxc_mode *support_modes;
152 u32 cfg_num;
153 struct v4l2_fwnode_endpoint bus_cfg;
154 bool nosignal;
155 bool enable_hdcp;
156 bool is_audio_present;
157 bool power_on;
158 int plugin_irq;
159 u32 mbus_fmt_code;
160 u32 module_index;
161 u32 audio_sampling_rate;
162 };
163
164 static const struct v4l2_dv_timings_cap lt7911uxc_timings_cap = {
165 .type = V4L2_DV_BT_656_1120,
166 .reserved = { 0 },
167 V4L2_INIT_BT_TIMINGS(1, 10000, 1, 10000, 0, 800000000,
168 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
169 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
170 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED |
171 V4L2_DV_BT_CAP_REDUCED_BLANKING |
172 V4L2_DV_BT_CAP_CUSTOM)
173 };
174
175 struct lt7911uxc_mode {
176 u32 width;
177 u32 height;
178 struct v4l2_fract max_fps;
179 u32 hts_def;
180 u32 vts_def;
181 u32 exp_def;
182 u32 mipi_freq_idx;
183 };
184
185 static struct rkmodule_csi_dphy_param rk3588_dcphy_param = {
186 .vendor = PHY_VENDOR_SAMSUNG,
187 .lp_vol_ref = 3,
188 .lp_hys_sw = {3, 0, 3, 0},
189 .lp_escclk_pol_sel = {1, 1, 0, 0},
190 .skew_data_cal_clk = {0, 0, 0, 0},
191 .clk_hs_term_sel = 0,
192 .data_hs_term_sel = {0, 0, 0, 0},
193 .reserved = {0},
194 };
195
196 static const struct lt7911uxc_mode supported_modes_dphy[] = {
197 {
198 .width = 3840,
199 .height = 2160,
200 .max_fps = {
201 .numerator = 10000,
202 .denominator = 600000,
203 },
204 .hts_def = 4400,
205 .vts_def = 2250,
206 .mipi_freq_idx = 0,
207 }, {
208 .width = 1920,
209 .height = 1080,
210 .max_fps = {
211 .numerator = 10000,
212 .denominator = 600000,
213 },
214 .hts_def = 2200,
215 .vts_def = 1125,
216 .mipi_freq_idx = 4,
217 }, {
218 .width = 1600,
219 .height = 1200,
220 .max_fps = {
221 .numerator = 10000,
222 .denominator = 600000,
223 },
224 .hts_def = 2160,
225 .vts_def = 1250,
226 .mipi_freq_idx = 4,
227 }, {
228 .width = 1280,
229 .height = 960,
230 .max_fps = {
231 .numerator = 10000,
232 .denominator = 600000,
233 },
234 .hts_def = 1712,
235 .vts_def = 994,
236 .mipi_freq_idx = 5,
237 }, {
238 .width = 1280,
239 .height = 720,
240 .max_fps = {
241 .numerator = 10000,
242 .denominator = 600000,
243 },
244 .hts_def = 1650,
245 .vts_def = 750,
246 .mipi_freq_idx = 5,
247 }, {
248 .width = 800,
249 .height = 600,
250 .max_fps = {
251 .numerator = 10000,
252 .denominator = 600000,
253 },
254 .hts_def = 1056,
255 .vts_def = 628,
256 .mipi_freq_idx = 6,
257 }, {
258 .width = 720,
259 .height = 576,
260 .max_fps = {
261 .numerator = 10000,
262 .denominator = 500000,
263 },
264 .hts_def = 864,
265 .vts_def = 625,
266 .mipi_freq_idx = 6,
267 }, {
268 .width = 720,
269 .height = 480,
270 .max_fps = {
271 .numerator = 10000,
272 .denominator = 600000,
273 },
274 .hts_def = 858,
275 .vts_def = 525,
276 .mipi_freq_idx = 6,
277 },
278 };
279
280 static const struct lt7911uxc_mode supported_modes_cphy[] = {
281 {
282 .width = 5120,
283 .height = 2160,
284 .max_fps = {
285 .numerator = 10000,
286 .denominator = 600000,
287 },
288 .hts_def = 5500,
289 .vts_def = 2250,
290 .mipi_freq_idx = 1,
291 }, {
292 .width = 3840,
293 .height = 2160,
294 .max_fps = {
295 .numerator = 10000,
296 .denominator = 600000,
297 },
298 .hts_def = 4400,
299 .vts_def = 2250,
300 .mipi_freq_idx = 2,
301 }, {
302 .width = 1920,
303 .height = 1080,
304 .max_fps = {
305 .numerator = 10000,
306 .denominator = 600000,
307 },
308 .hts_def = 2200,
309 .vts_def = 1125,
310 .mipi_freq_idx = 5,
311 }, {
312 .width = 1280,
313 .height = 720,
314 .max_fps = {
315 .numerator = 10000,
316 .denominator = 600000,
317 },
318 .hts_def = 1650,
319 .vts_def = 750,
320 .mipi_freq_idx = 6,
321 }, {
322 .width = 720,
323 .height = 576,
324 .max_fps = {
325 .numerator = 10000,
326 .denominator = 500000,
327 },
328 .hts_def = 864,
329 .vts_def = 625,
330 .mipi_freq_idx = 6,
331 }, {
332 .width = 720,
333 .height = 480,
334 .max_fps = {
335 .numerator = 10000,
336 .denominator = 600000,
337 },
338 .hts_def = 858,
339 .vts_def = 525,
340 .mipi_freq_idx = 6,
341 },
342 };
343
344 static void lt7911uxc_format_change(struct v4l2_subdev *sd);
345 static int lt7911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
346 static int lt7911uxc_s_dv_timings(struct v4l2_subdev *sd,
347 struct v4l2_dv_timings *timings);
348
to_lt7911uxc(struct v4l2_subdev * sd)349 static inline struct lt7911uxc *to_lt7911uxc(struct v4l2_subdev *sd)
350 {
351 return container_of(sd, struct lt7911uxc, sd);
352 }
353
i2c_rd(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)354 static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
355 {
356 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
357 struct i2c_client *client = lt7911uxc->i2c_client;
358 int err;
359 u8 buf[2] = { 0xFF, reg >> 8};
360 u8 reg_addr = reg & 0xFF;
361 struct i2c_msg msgs[3];
362
363 msgs[0].addr = client->addr;
364 msgs[0].flags = 0;
365 msgs[0].len = 2;
366 msgs[0].buf = buf;
367
368 msgs[1].addr = client->addr;
369 msgs[1].flags = 0;
370 msgs[1].len = 1;
371 msgs[1].buf = ®_addr;
372
373 msgs[2].addr = client->addr;
374 msgs[2].flags = I2C_M_RD;
375 msgs[2].len = n;
376 msgs[2].buf = values;
377
378 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
379 if (err != ARRAY_SIZE(msgs)) {
380 v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
381 __func__, reg, client->addr);
382 }
383
384 if (!debug)
385 return;
386
387 switch (n) {
388 case 1:
389 v4l2_info(sd, "I2C read 0x%04x = 0x%02x\n",
390 reg, values[0]);
391 break;
392 case 2:
393 v4l2_info(sd, "I2C read 0x%04x = 0x%02x%02x\n",
394 reg, values[1], values[0]);
395 break;
396 case 4:
397 v4l2_info(sd, "I2C read 0x%04x = 0x%02x%02x%02x%02x\n",
398 reg, values[3], values[2], values[1], values[0]);
399 break;
400 default:
401 v4l2_info(sd, "I2C read %d bytes from address 0x%04x\n",
402 n, reg);
403 }
404 }
405
i2c_wr(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)406 static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
407 {
408 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
409 struct i2c_client *client = lt7911uxc->i2c_client;
410 int err, i;
411 struct i2c_msg msgs[2];
412 u8 data[I2C_MAX_XFER_SIZE];
413 u8 buf[2] = { 0xFF, reg >> 8};
414
415 if ((1 + n) > I2C_MAX_XFER_SIZE) {
416 n = I2C_MAX_XFER_SIZE - 1;
417 v4l2_warn(sd, "i2c wr reg=%04x: len=%d is too big!\n",
418 reg, 1 + n);
419 }
420
421 msgs[0].addr = client->addr;
422 msgs[0].flags = 0;
423 msgs[0].len = 2;
424 msgs[0].buf = buf;
425
426 msgs[1].addr = client->addr;
427 msgs[1].flags = 0;
428 msgs[1].len = 1 + n;
429 msgs[1].buf = data;
430
431 data[0] = reg & 0xff;
432 for (i = 0; i < n; i++)
433 data[1 + i] = values[i];
434
435 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
436 if (err < 0) {
437 v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed\n",
438 __func__, reg, client->addr);
439 return;
440 }
441
442 if (!debug)
443 return;
444
445 switch (n) {
446 case 1:
447 v4l2_info(sd, "I2C write 0x%04x = 0x%02x\n",
448 reg, data[1]);
449 break;
450 case 2:
451 v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x\n",
452 reg, data[2], data[1]);
453 break;
454 case 4:
455 v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x%02x%02x\n",
456 reg, data[4], data[3], data[2], data[1]);
457 break;
458 default:
459 v4l2_info(sd, "I2C write %d bytes from address 0x%04x\n",
460 n, reg);
461 }
462 }
463
i2c_rd8(struct v4l2_subdev * sd,u16 reg)464 static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
465 {
466 u32 val;
467
468 i2c_rd(sd, reg, (u8 __force *)&val, 1);
469 return val;
470 }
471
i2c_wr8(struct v4l2_subdev * sd,u16 reg,u8 val)472 static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
473 {
474 i2c_wr(sd, reg, &val, 1);
475 }
476
lt7911uxc_i2c_enable(struct v4l2_subdev * sd)477 static void lt7911uxc_i2c_enable(struct v4l2_subdev *sd)
478 {
479 i2c_wr8(sd, I2C_EN_REG, I2C_ENABLE);
480 }
481
lt7911uxc_i2c_disable(struct v4l2_subdev * sd)482 static void lt7911uxc_i2c_disable(struct v4l2_subdev *sd)
483 {
484 i2c_wr8(sd, I2C_EN_REG, I2C_DISABLE);
485 }
486
tx_5v_power_present(struct v4l2_subdev * sd)487 static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
488 {
489 bool ret;
490 int val, i, cnt;
491 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
492
493 /* if not use plugin det gpio */
494 if (!lt7911uxc->plugin_det_gpio)
495 return true;
496
497 cnt = 0;
498 for (i = 0; i < 5; i++) {
499 val = gpiod_get_value(lt7911uxc->plugin_det_gpio);
500 if (val > 0)
501 cnt++;
502 usleep_range(500, 600);
503 }
504
505 ret = (cnt >= 3) ? true : false;
506 v4l2_dbg(1, debug, sd, "%s: %d\n", __func__, ret);
507
508 return ret;
509 }
510
no_signal(struct v4l2_subdev * sd)511 static inline bool no_signal(struct v4l2_subdev *sd)
512 {
513 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
514
515 v4l2_dbg(1, debug, sd, "%s no signal:%d\n", __func__,
516 lt7911uxc->nosignal);
517
518 return lt7911uxc->nosignal;
519 }
520
audio_present(struct v4l2_subdev * sd)521 static inline bool audio_present(struct v4l2_subdev *sd)
522 {
523 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
524
525 return lt7911uxc->is_audio_present;
526 }
527
get_audio_sampling_rate(struct v4l2_subdev * sd)528 static int get_audio_sampling_rate(struct v4l2_subdev *sd)
529 {
530 static const int code_to_rate[] = {
531 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
532 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
533 };
534
535 if (no_signal(sd))
536 return 0;
537
538 return code_to_rate[2];
539 }
540
fps_calc(const struct v4l2_bt_timings * t)541 static inline unsigned int fps_calc(const struct v4l2_bt_timings *t)
542 {
543 if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
544 return 0;
545
546 return DIV_ROUND_CLOSEST((unsigned int)t->pixelclock,
547 V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
548 }
549
lt7911uxc_rcv_supported_res(struct v4l2_subdev * sd,u32 width,u32 height)550 static bool lt7911uxc_rcv_supported_res(struct v4l2_subdev *sd, u32 width,
551 u32 height)
552 {
553 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
554 u32 i;
555
556 for (i = 0; i < lt7911uxc->cfg_num; i++) {
557 if ((lt7911uxc->support_modes[i].width == width) &&
558 (lt7911uxc->support_modes[i].height == height)) {
559 break;
560 }
561 }
562
563 if (i == lt7911uxc->cfg_num) {
564 v4l2_err(sd, "%s do not support res wxh: %dx%d\n", __func__,
565 width, height);
566 return false;
567 } else {
568 return true;
569 }
570 }
571
lt7911uxc_get_detected_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)572 static int lt7911uxc_get_detected_timings(struct v4l2_subdev *sd,
573 struct v4l2_dv_timings *timings)
574 {
575 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
576 struct v4l2_bt_timings *bt = &timings->bt;
577 u32 hact, vact, htotal, vtotal;
578 u32 pixel_clock, fps, halt_pix_clk;
579 u8 clk_h, clk_m, clk_l;
580 u8 val_h, val_l;
581 u64 byte_clk, mipi_clk, mipi_data_rate;
582
583 memset(timings, 0, sizeof(struct v4l2_dv_timings));
584
585 clk_h = i2c_rd8(sd, PCLK_H);
586 clk_m = i2c_rd8(sd, PCLK_M);
587 clk_l = i2c_rd8(sd, PCLK_L);
588 halt_pix_clk = ((clk_h << 16) | (clk_m << 8) | clk_l);
589 pixel_clock = halt_pix_clk * 1000;
590
591 clk_h = i2c_rd8(sd, BYTE_PCLK_H);
592 clk_m = i2c_rd8(sd, BYTE_PCLK_M);
593 clk_l = i2c_rd8(sd, BYTE_PCLK_L);
594 byte_clk = ((clk_h << 16) | (clk_m << 8) | clk_l) * 1000;
595 mipi_clk = byte_clk * 4;
596 mipi_data_rate = byte_clk * 8;
597
598 val_h = i2c_rd8(sd, HTOTAL_H);
599 val_l = i2c_rd8(sd, HTOTAL_L);
600 htotal = ((val_h << 8) | val_l);
601
602 val_h = i2c_rd8(sd, VTOTAL_H);
603 val_l = i2c_rd8(sd, VTOTAL_L);
604 vtotal = (val_h << 8) | val_l;
605
606 val_h = i2c_rd8(sd, HACT_H);
607 val_l = i2c_rd8(sd, HACT_L);
608 hact = ((val_h << 8) | val_l);
609
610 val_h = i2c_rd8(sd, VACT_H);
611 val_l = i2c_rd8(sd, VACT_L);
612 vact = (val_h << 8) | val_l;
613
614 lt7911uxc->nosignal = false;
615 lt7911uxc->is_audio_present = true;
616 timings->type = V4L2_DV_BT_656_1120;
617 bt->interlaced = V4L2_DV_PROGRESSIVE;
618 bt->width = hact;
619 bt->height = vact;
620 bt->pixelclock = pixel_clock;
621 fps = pixel_clock / (htotal * vtotal);
622
623 if (!lt7911uxc_rcv_supported_res(sd, hact, vact)) {
624 lt7911uxc->nosignal = true;
625 v4l2_err(sd, "%s: rcv err res, return no signal!\n", __func__);
626 return -EINVAL;
627 }
628
629 v4l2_info(sd, "act:%dx%d, total:%dx%d, pixclk:%d, fps:%d\n",
630 hact, vact, htotal, vtotal, pixel_clock, fps);
631 v4l2_info(sd, "byte_clk:%llu, mipi_clk:%llu, mipi_data_rate:%llu\n",
632 byte_clk, mipi_clk, mipi_data_rate);
633 v4l2_info(sd, "inerlaced:%d\n", bt->interlaced);
634
635 return 0;
636 }
637
lt7911uxc_delayed_work_hotplug(struct work_struct * work)638 static void lt7911uxc_delayed_work_hotplug(struct work_struct *work)
639 {
640 struct delayed_work *dwork = to_delayed_work(work);
641 struct lt7911uxc *lt7911uxc = container_of(dwork,
642 struct lt7911uxc, delayed_work_hotplug);
643 struct v4l2_subdev *sd = <7911uxc->sd;
644
645 lt7911uxc_s_ctrl_detect_tx_5v(sd);
646 }
647
lt7911uxc_s_ctrl_detect_event(struct v4l2_subdev * sd)648 static void lt7911uxc_s_ctrl_detect_event(struct v4l2_subdev *sd)
649 {
650 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
651 u8 val;
652
653 val = i2c_rd8(sd, 0xe084);
654 if (val == 0x01)
655 v4l2_ctrl_s_ctrl(lt7911uxc->detect_tx_5v_ctrl, 1);
656 else if (val == 0x00)
657 v4l2_ctrl_s_ctrl(lt7911uxc->detect_tx_5v_ctrl, 0);
658 }
659
lt7911uxc_delayed_work_res_change(struct work_struct * work)660 static void lt7911uxc_delayed_work_res_change(struct work_struct *work)
661 {
662 struct delayed_work *dwork = to_delayed_work(work);
663 struct lt7911uxc *lt7911uxc = container_of(dwork,
664 struct lt7911uxc, delayed_work_res_change);
665 struct v4l2_subdev *sd = <7911uxc->sd;
666
667 lt7911uxc_s_ctrl_detect_event(sd);
668 lt7911uxc_format_change(sd);
669 }
670
lt7911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev * sd)671 static int lt7911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
672 {
673 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
674
675 return v4l2_ctrl_s_ctrl(lt7911uxc->detect_tx_5v_ctrl,
676 tx_5v_power_present(sd));
677 }
678
lt7911uxc_s_ctrl_audio_sampling_rate(struct v4l2_subdev * sd)679 static int lt7911uxc_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
680 {
681 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
682
683 return v4l2_ctrl_s_ctrl(lt7911uxc->audio_sampling_rate_ctrl,
684 get_audio_sampling_rate(sd));
685 }
686
lt7911uxc_s_ctrl_audio_present(struct v4l2_subdev * sd)687 static int lt7911uxc_s_ctrl_audio_present(struct v4l2_subdev *sd)
688 {
689 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
690
691 return v4l2_ctrl_s_ctrl(lt7911uxc->audio_present_ctrl,
692 audio_present(sd));
693 }
694
lt7911uxc_update_controls(struct v4l2_subdev * sd)695 static int lt7911uxc_update_controls(struct v4l2_subdev *sd)
696 {
697 int ret = 0;
698
699 ret |= lt7911uxc_s_ctrl_detect_tx_5v(sd);
700 ret |= lt7911uxc_s_ctrl_audio_sampling_rate(sd);
701 ret |= lt7911uxc_s_ctrl_audio_present(sd);
702
703 return ret;
704 }
705
lt7911uxc_cphy_timing_config(struct v4l2_subdev * sd)706 static void lt7911uxc_cphy_timing_config(struct v4l2_subdev *sd)
707 {
708 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
709
710 if (lt7911uxc->bus_cfg.bus_type == V4L2_MBUS_CSI2_CPHY) {
711 lt7911uxc_i2c_enable(sd);
712 while (i2c_rd8(sd, HS_RQST_PRE_REG) != 0x3c) {
713 i2c_wr8(sd, HS_RQST_PRE_REG, 0x3c);
714 usleep_range(500, 600);
715 }
716 // i2c_wr8(sd, HS_TRAIL, 0x0b);
717 lt7911uxc_i2c_disable(sd);
718 }
719
720 v4l2_dbg(1, debug, sd, "%s config timing succeed\n", __func__);
721 }
722
lt7911uxc_match_timings(const struct v4l2_dv_timings * t1,const struct v4l2_dv_timings * t2)723 static bool lt7911uxc_match_timings(const struct v4l2_dv_timings *t1,
724 const struct v4l2_dv_timings *t2)
725 {
726 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
727 return false;
728 if (t1->bt.width == t2->bt.width &&
729 t1->bt.height == t2->bt.height &&
730 t1->bt.interlaced == t2->bt.interlaced)
731 return true;
732
733 return false;
734 }
735
enable_stream(struct v4l2_subdev * sd,bool enable)736 static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
737 {
738 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
739
740 lt7911uxc_cphy_timing_config(<7911uxc->sd);
741
742 if (enable)
743 i2c_wr8(<7911uxc->sd, STREAM_CTL, ENABLE_STREAM);
744 else
745 i2c_wr8(<7911uxc->sd, STREAM_CTL, DISABLE_STREAM);
746 msleep(50);
747
748 v4l2_dbg(2, debug, sd, "%s: %sable\n",
749 __func__, enable ? "en" : "dis");
750 }
751
lt7911uxc_get_reso_dist(const struct lt7911uxc_mode * mode,struct v4l2_dv_timings * timings)752 static int lt7911uxc_get_reso_dist(const struct lt7911uxc_mode *mode,
753 struct v4l2_dv_timings *timings)
754 {
755 struct v4l2_bt_timings *bt = &timings->bt;
756 u32 cur_fps, dist_fps;
757
758 cur_fps = fps_calc(bt);
759 dist_fps = DIV_ROUND_CLOSEST(mode->max_fps.denominator, mode->max_fps.numerator);
760
761 return abs(mode->width - bt->width) +
762 abs(mode->height - bt->height) + abs(dist_fps - cur_fps);
763 }
764
765 static const struct lt7911uxc_mode *
lt7911uxc_find_best_fit(struct lt7911uxc * lt7911uxc)766 lt7911uxc_find_best_fit(struct lt7911uxc *lt7911uxc)
767 {
768 int dist;
769 int cur_best_fit = 0;
770 int cur_best_fit_dist = -1;
771 unsigned int i;
772
773 for (i = 0; i < lt7911uxc->cfg_num; i++) {
774 dist = lt7911uxc_get_reso_dist(<7911uxc->support_modes[i], <7911uxc->timings);
775 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
776 cur_best_fit_dist = dist;
777 cur_best_fit = i;
778 }
779 }
780 dev_dbg(<7911uxc->i2c_client->dev,
781 "find current mode: support_mode[%d], %dx%d@%dfps\n",
782 cur_best_fit, lt7911uxc->support_modes[cur_best_fit].width,
783 lt7911uxc->support_modes[cur_best_fit].height,
784 DIV_ROUND_CLOSEST(lt7911uxc->support_modes[cur_best_fit].max_fps.denominator,
785 lt7911uxc->support_modes[cur_best_fit].max_fps.numerator));
786
787 return <7911uxc->support_modes[cur_best_fit];
788 }
789
lt7911uxc_print_dv_timings(struct v4l2_subdev * sd,const char * prefix)790 static void lt7911uxc_print_dv_timings(struct v4l2_subdev *sd, const char *prefix)
791 {
792 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
793 struct device *dev = <7911uxc->i2c_client->dev;
794 const struct v4l2_bt_timings *bt = <7911uxc->timings.bt;
795 const struct lt7911uxc_mode *mode;
796 u32 htot, vtot;
797 u32 fps;
798
799 mode = lt7911uxc_find_best_fit(lt7911uxc);
800 lt7911uxc->cur_mode = mode;
801 htot = lt7911uxc->cur_mode->hts_def;
802 vtot = lt7911uxc->cur_mode->vts_def;
803 if (bt->interlaced)
804 vtot /= 2;
805
806 fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
807 (htot * vtot)) : 0;
808
809 if (prefix == NULL)
810 prefix = "";
811
812 dev_info(dev, "%s: %s%ux%u%s%u.%02u (%ux%u)\n", sd->name, prefix,
813 bt->width, bt->height, bt->interlaced ? "i" : "p",
814 fps / 100, fps % 100, htot, vtot);
815 }
816
lt7911uxc_format_change(struct v4l2_subdev * sd)817 static void lt7911uxc_format_change(struct v4l2_subdev *sd)
818 {
819 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
820 struct v4l2_dv_timings timings;
821 const struct v4l2_event lt7911uxc_ev_fmt = {
822 .type = V4L2_EVENT_SOURCE_CHANGE,
823 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
824 };
825
826 if (lt7911uxc_get_detected_timings(sd, &timings)) {
827 enable_stream(sd, false);
828 v4l2_dbg(1, debug, sd, "%s: No signal\n", __func__);
829 }
830
831 if (!lt7911uxc_match_timings(<7911uxc->timings, &timings)) {
832 enable_stream(sd, false);
833 /* automatically set timing rather than set by user */
834 lt7911uxc_s_dv_timings(sd, &timings);
835 lt7911uxc_print_dv_timings(sd,
836 "Format_change: New format: ");
837 }
838 if (sd->devnode)
839 v4l2_subdev_notify_event(sd, <7911uxc_ev_fmt);
840 }
841
lt7911uxc_isr(struct v4l2_subdev * sd,u32 status,bool * handled)842 static int lt7911uxc_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
843 {
844 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
845
846 schedule_delayed_work(<7911uxc->delayed_work_res_change, HZ / 20);
847 *handled = true;
848
849 return 0;
850 }
851
lt7911uxc_res_change_irq_handler(int irq,void * dev_id)852 static irqreturn_t lt7911uxc_res_change_irq_handler(int irq, void *dev_id)
853 {
854 struct lt7911uxc *lt7911uxc = dev_id;
855 bool handled;
856
857 lt7911uxc_isr(<7911uxc->sd, 0, &handled);
858
859 return handled ? IRQ_HANDLED : IRQ_NONE;
860 }
861
plugin_detect_irq_handler(int irq,void * dev_id)862 static irqreturn_t plugin_detect_irq_handler(int irq, void *dev_id)
863 {
864 struct lt7911uxc *lt7911uxc = dev_id;
865
866 /* control hpd output level after 25ms */
867 schedule_delayed_work(<7911uxc->delayed_work_hotplug,
868 HZ / 40);
869
870 return IRQ_HANDLED;
871 }
872
lt7911uxc_irq_poll_timer(struct timer_list * t)873 static void lt7911uxc_irq_poll_timer(struct timer_list *t)
874 {
875 struct lt7911uxc *lt7911uxc = from_timer(lt7911uxc, t, timer);
876
877 schedule_work(<7911uxc->work_i2c_poll);
878 mod_timer(<7911uxc->timer, jiffies + msecs_to_jiffies(POLL_INTERVAL_MS));
879 }
880
lt7911uxc_work_i2c_poll(struct work_struct * work)881 static void lt7911uxc_work_i2c_poll(struct work_struct *work)
882 {
883 struct lt7911uxc *lt7911uxc = container_of(work,
884 struct lt7911uxc, work_i2c_poll);
885 struct v4l2_subdev *sd = <7911uxc->sd;
886
887 lt7911uxc_format_change(sd);
888 }
889
lt7911uxc_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)890 static int lt7911uxc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
891 struct v4l2_event_subscription *sub)
892 {
893 switch (sub->type) {
894 case V4L2_EVENT_SOURCE_CHANGE:
895 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
896 case V4L2_EVENT_CTRL:
897 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
898 default:
899 return -EINVAL;
900 }
901 }
902
lt7911uxc_g_input_status(struct v4l2_subdev * sd,u32 * status)903 static int lt7911uxc_g_input_status(struct v4l2_subdev *sd, u32 *status)
904 {
905 *status = 0;
906 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
907
908 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
909
910 return 0;
911 }
912
lt7911uxc_s_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)913 static int lt7911uxc_s_dv_timings(struct v4l2_subdev *sd,
914 struct v4l2_dv_timings *timings)
915 {
916 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
917
918 if (!timings)
919 return -EINVAL;
920
921 if (debug)
922 v4l2_print_dv_timings(sd->name, "s_dv_timings: ",
923 timings, false);
924
925 if (lt7911uxc_match_timings(<7911uxc->timings, timings)) {
926 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
927 return 0;
928 }
929
930 if (!v4l2_valid_dv_timings(timings,
931 <7911uxc_timings_cap, NULL, NULL)) {
932 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
933 return -ERANGE;
934 }
935
936 lt7911uxc->timings = *timings;
937
938 enable_stream(sd, false);
939
940 return 0;
941 }
942
lt7911uxc_g_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)943 static int lt7911uxc_g_dv_timings(struct v4l2_subdev *sd,
944 struct v4l2_dv_timings *timings)
945 {
946 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
947
948 *timings = lt7911uxc->timings;
949
950 return 0;
951 }
952
lt7911uxc_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)953 static int lt7911uxc_enum_dv_timings(struct v4l2_subdev *sd,
954 struct v4l2_enum_dv_timings *timings)
955 {
956 if (timings->pad != 0)
957 return -EINVAL;
958
959 return v4l2_enum_dv_timings_cap(timings,
960 <7911uxc_timings_cap, NULL, NULL);
961 }
962
lt7911uxc_query_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)963 static int lt7911uxc_query_dv_timings(struct v4l2_subdev *sd,
964 struct v4l2_dv_timings *timings)
965 {
966 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
967
968 *timings = lt7911uxc->timings;
969 if (debug)
970 v4l2_print_dv_timings(sd->name,
971 "query_dv_timings: ", timings, false);
972
973 if (!v4l2_valid_dv_timings(timings, <7911uxc_timings_cap, NULL,
974 NULL)) {
975 v4l2_dbg(1, debug, sd, "%s: timings out of range\n",
976 __func__);
977
978 return -ERANGE;
979 }
980
981 return 0;
982 }
983
lt7911uxc_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)984 static int lt7911uxc_dv_timings_cap(struct v4l2_subdev *sd,
985 struct v4l2_dv_timings_cap *cap)
986 {
987 if (cap->pad != 0)
988 return -EINVAL;
989
990 *cap = lt7911uxc_timings_cap;
991
992 return 0;
993 }
994
lt7911uxc_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)995 static int lt7911uxc_g_mbus_config(struct v4l2_subdev *sd,
996 unsigned int pad, struct v4l2_mbus_config *cfg)
997 {
998 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
999 u32 lane_num = lt7911uxc->bus_cfg.bus.mipi_csi2.num_data_lanes;
1000 u32 val = 0;
1001
1002 val = 1 << (lane_num - 1) |
1003 V4L2_MBUS_CSI2_CHANNEL_0 |
1004 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
1005
1006 cfg->type = lt7911uxc->bus_cfg.bus_type;
1007 cfg->flags = val;
1008
1009 return 0;
1010 }
1011
lt7911uxc_s_stream(struct v4l2_subdev * sd,int on)1012 static int lt7911uxc_s_stream(struct v4l2_subdev *sd, int on)
1013 {
1014 enable_stream(sd, on);
1015
1016 return 0;
1017 }
1018
lt7911uxc_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1019 static int lt7911uxc_enum_mbus_code(struct v4l2_subdev *sd,
1020 struct v4l2_subdev_pad_config *cfg,
1021 struct v4l2_subdev_mbus_code_enum *code)
1022 {
1023 switch (code->index) {
1024 case 0:
1025 code->code = LT7911UXC_MEDIA_BUS_FMT;
1026 break;
1027
1028 default:
1029 return -EINVAL;
1030 }
1031
1032 return 0;
1033 }
1034
lt7911uxc_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1035 static int lt7911uxc_enum_frame_sizes(struct v4l2_subdev *sd,
1036 struct v4l2_subdev_pad_config *cfg,
1037 struct v4l2_subdev_frame_size_enum *fse)
1038 {
1039 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1040
1041 if (fse->index >= lt7911uxc->cfg_num)
1042 return -EINVAL;
1043
1044 if (fse->code != LT7911UXC_MEDIA_BUS_FMT)
1045 return -EINVAL;
1046
1047 fse->min_width = lt7911uxc->support_modes[fse->index].width;
1048 fse->max_width = lt7911uxc->support_modes[fse->index].width;
1049 fse->max_height = lt7911uxc->support_modes[fse->index].height;
1050 fse->min_height = lt7911uxc->support_modes[fse->index].height;
1051
1052 return 0;
1053 }
1054
lt7911uxc_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)1055 static int lt7911uxc_get_fmt(struct v4l2_subdev *sd,
1056 struct v4l2_subdev_pad_config *cfg,
1057 struct v4l2_subdev_format *format)
1058 {
1059 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1060 const struct lt7911uxc_mode *mode;
1061
1062 mutex_lock(<7911uxc->confctl_mutex);
1063 format->format.code = lt7911uxc->mbus_fmt_code;
1064 format->format.width = lt7911uxc->timings.bt.width;
1065 format->format.height = lt7911uxc->timings.bt.height;
1066 format->format.field =
1067 lt7911uxc->timings.bt.interlaced ?
1068 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE;
1069 format->format.colorspace = V4L2_COLORSPACE_SRGB;
1070 mutex_unlock(<7911uxc->confctl_mutex);
1071
1072 mode = lt7911uxc_find_best_fit(lt7911uxc);
1073 lt7911uxc->cur_mode = mode;
1074
1075 __v4l2_ctrl_s_ctrl_int64(lt7911uxc->pixel_rate,
1076 LT7911UXC_PIXEL_RATE);
1077 __v4l2_ctrl_s_ctrl(lt7911uxc->link_freq,
1078 mode->mipi_freq_idx);
1079
1080 v4l2_dbg(1, debug, sd, "%s: mode->mipi_freq_idx(%d)",
1081 __func__, mode->mipi_freq_idx);
1082
1083 v4l2_dbg(1, debug, sd, "%s: fmt code:%d, w:%d, h:%d, field code:%d\n",
1084 __func__, format->format.code, format->format.width,
1085 format->format.height, format->format.field);
1086
1087 return 0;
1088 }
1089
lt7911uxc_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)1090 static int lt7911uxc_enum_frame_interval(struct v4l2_subdev *sd,
1091 struct v4l2_subdev_pad_config *cfg,
1092 struct v4l2_subdev_frame_interval_enum *fie)
1093 {
1094 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1095
1096 if (fie->index >= lt7911uxc->cfg_num)
1097 return -EINVAL;
1098
1099 fie->code = LT7911UXC_MEDIA_BUS_FMT;
1100
1101 fie->width = lt7911uxc->support_modes[fie->index].width;
1102 fie->height = lt7911uxc->support_modes[fie->index].height;
1103 fie->interval = lt7911uxc->support_modes[fie->index].max_fps;
1104
1105 return 0;
1106 }
1107
lt7911uxc_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)1108 static int lt7911uxc_set_fmt(struct v4l2_subdev *sd,
1109 struct v4l2_subdev_pad_config *cfg,
1110 struct v4l2_subdev_format *format)
1111 {
1112 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1113 const struct lt7911uxc_mode *mode;
1114
1115 /* is overwritten by get_fmt */
1116 u32 code = format->format.code;
1117 int ret = lt7911uxc_get_fmt(sd, cfg, format);
1118
1119 format->format.code = code;
1120
1121 if (ret)
1122 return ret;
1123
1124 switch (code) {
1125 case LT7911UXC_MEDIA_BUS_FMT:
1126 break;
1127
1128 default:
1129 return -EINVAL;
1130 }
1131
1132 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1133 return 0;
1134
1135 lt7911uxc->mbus_fmt_code = format->format.code;
1136 mode = lt7911uxc_find_best_fit(lt7911uxc);
1137 lt7911uxc->cur_mode = mode;
1138
1139 enable_stream(sd, false);
1140
1141 return 0;
1142 }
1143
lt7911uxc_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1144 static int lt7911uxc_g_frame_interval(struct v4l2_subdev *sd,
1145 struct v4l2_subdev_frame_interval *fi)
1146 {
1147 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1148 const struct lt7911uxc_mode *mode = lt7911uxc->cur_mode;
1149
1150 mutex_lock(<7911uxc->confctl_mutex);
1151 fi->interval = mode->max_fps;
1152 mutex_unlock(<7911uxc->confctl_mutex);
1153
1154 return 0;
1155 }
1156
lt7911uxc_get_module_inf(struct lt7911uxc * lt7911uxc,struct rkmodule_inf * inf)1157 static void lt7911uxc_get_module_inf(struct lt7911uxc *lt7911uxc,
1158 struct rkmodule_inf *inf)
1159 {
1160 memset(inf, 0, sizeof(*inf));
1161 strscpy(inf->base.sensor, LT7911UXC_NAME, sizeof(inf->base.sensor));
1162 strscpy(inf->base.module, lt7911uxc->module_name, sizeof(inf->base.module));
1163 strscpy(inf->base.lens, lt7911uxc->len_name, sizeof(inf->base.lens));
1164 }
1165
lt7911uxc_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)1166 static long lt7911uxc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1167 {
1168 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1169 long ret = 0;
1170 struct rkmodule_csi_dphy_param *dphy_param;
1171
1172 switch (cmd) {
1173 case RKMODULE_GET_MODULE_INFO:
1174 lt7911uxc_get_module_inf(lt7911uxc, (struct rkmodule_inf *)arg);
1175 break;
1176 case RKMODULE_GET_HDMI_MODE:
1177 *(int *)arg = RKMODULE_HDMIIN_MODE;
1178 break;
1179 case RKMODULE_SET_CSI_DPHY_PARAM:
1180 dphy_param = (struct rkmodule_csi_dphy_param *)arg;
1181 if (dphy_param->vendor == rk3588_dcphy_param.vendor)
1182 rk3588_dcphy_param = *dphy_param;
1183 dev_dbg(<7911uxc->i2c_client->dev,
1184 "sensor set dphy param\n");
1185 break;
1186 case RKMODULE_GET_CSI_DPHY_PARAM:
1187 dphy_param = (struct rkmodule_csi_dphy_param *)arg;
1188 if (dphy_param->vendor == rk3588_dcphy_param.vendor)
1189 *dphy_param = rk3588_dcphy_param;
1190 dev_dbg(<7911uxc->i2c_client->dev,
1191 "sensor get dphy param\n");
1192 break;
1193 default:
1194 ret = -ENOIOCTLCMD;
1195 break;
1196 }
1197
1198 return ret;
1199 }
1200
lt7911uxc_s_power(struct v4l2_subdev * sd,int on)1201 static int lt7911uxc_s_power(struct v4l2_subdev *sd, int on)
1202 {
1203 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1204 int ret = 0;
1205
1206 mutex_lock(<7911uxc->confctl_mutex);
1207
1208 if (lt7911uxc->power_on == !!on)
1209 goto unlock_and_return;
1210
1211 if (on)
1212 lt7911uxc->power_on = true;
1213 else
1214 lt7911uxc->power_on = false;
1215
1216 unlock_and_return:
1217 mutex_unlock(<7911uxc->confctl_mutex);
1218
1219 return ret;
1220 }
1221
1222 #ifdef CONFIG_COMPAT
lt7911uxc_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)1223 static long lt7911uxc_compat_ioctl32(struct v4l2_subdev *sd,
1224 unsigned int cmd, unsigned long arg)
1225 {
1226 void __user *up = compat_ptr(arg);
1227 struct rkmodule_inf *inf;
1228 long ret;
1229 int *seq;
1230 struct rkmodule_csi_dphy_param *dphy_param;
1231
1232 switch (cmd) {
1233 case RKMODULE_GET_MODULE_INFO:
1234 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
1235 if (!inf) {
1236 ret = -ENOMEM;
1237 return ret;
1238 }
1239
1240 ret = lt7911uxc_ioctl(sd, cmd, inf);
1241 if (!ret) {
1242 ret = copy_to_user(up, inf, sizeof(*inf));
1243 if (ret)
1244 ret = -EFAULT;
1245 }
1246 kfree(inf);
1247 break;
1248 case RKMODULE_GET_HDMI_MODE:
1249 seq = kzalloc(sizeof(*seq), GFP_KERNEL);
1250 if (!seq) {
1251 ret = -ENOMEM;
1252 return ret;
1253 }
1254
1255 ret = lt7911uxc_ioctl(sd, cmd, seq);
1256 if (!ret) {
1257 ret = copy_to_user(up, seq, sizeof(*seq));
1258 if (ret)
1259 ret = -EFAULT;
1260 }
1261 kfree(seq);
1262 break;
1263 case RKMODULE_SET_CSI_DPHY_PARAM:
1264 dphy_param = kzalloc(sizeof(*dphy_param), GFP_KERNEL);
1265 if (!dphy_param) {
1266 ret = -ENOMEM;
1267 return ret;
1268 }
1269
1270 ret = copy_from_user(dphy_param, up, sizeof(*dphy_param));
1271 if (!ret)
1272 ret = lt7911uxc_ioctl(sd, cmd, dphy_param);
1273 else
1274 ret = -EFAULT;
1275 kfree(dphy_param);
1276 break;
1277 case RKMODULE_GET_CSI_DPHY_PARAM:
1278 dphy_param = kzalloc(sizeof(*dphy_param), GFP_KERNEL);
1279 if (!dphy_param) {
1280 ret = -ENOMEM;
1281 return ret;
1282 }
1283
1284 ret = lt7911uxc_ioctl(sd, cmd, dphy_param);
1285 if (!ret) {
1286 ret = copy_to_user(up, dphy_param, sizeof(*dphy_param));
1287 if (ret)
1288 ret = -EFAULT;
1289 }
1290 kfree(dphy_param);
1291 break;
1292 default:
1293 ret = -ENOIOCTLCMD;
1294 break;
1295 }
1296
1297 return ret;
1298 }
1299 #endif
1300
1301 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
lt7911uxc_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1302 static int lt7911uxc_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1303 {
1304 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1305 struct v4l2_mbus_framefmt *try_fmt =
1306 v4l2_subdev_get_try_format(sd, fh->pad, 0);
1307 const struct lt7911uxc_mode *def_mode = <7911uxc->support_modes[0];
1308
1309 mutex_lock(<7911uxc->confctl_mutex);
1310 /* Initialize try_fmt */
1311 try_fmt->width = def_mode->width;
1312 try_fmt->height = def_mode->height;
1313 try_fmt->code = LT7911UXC_MEDIA_BUS_FMT;
1314 try_fmt->field = V4L2_FIELD_NONE;
1315 mutex_unlock(<7911uxc->confctl_mutex);
1316
1317 return 0;
1318 }
1319 #endif
1320
1321 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1322 static const struct v4l2_subdev_internal_ops lt7911uxc_internal_ops = {
1323 .open = lt7911uxc_open,
1324 };
1325 #endif
1326
1327 static const struct v4l2_subdev_core_ops lt7911uxc_core_ops = {
1328 .s_power = lt7911uxc_s_power,
1329 .interrupt_service_routine = lt7911uxc_isr,
1330 .subscribe_event = lt7911uxc_subscribe_event,
1331 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1332 .ioctl = lt7911uxc_ioctl,
1333 #ifdef CONFIG_COMPAT
1334 .compat_ioctl32 = lt7911uxc_compat_ioctl32,
1335 #endif
1336 };
1337
1338 static const struct v4l2_subdev_video_ops lt7911uxc_video_ops = {
1339 .g_input_status = lt7911uxc_g_input_status,
1340 .s_dv_timings = lt7911uxc_s_dv_timings,
1341 .g_dv_timings = lt7911uxc_g_dv_timings,
1342 .query_dv_timings = lt7911uxc_query_dv_timings,
1343 .s_stream = lt7911uxc_s_stream,
1344 .g_frame_interval = lt7911uxc_g_frame_interval,
1345 };
1346
1347 static const struct v4l2_subdev_pad_ops lt7911uxc_pad_ops = {
1348 .enum_mbus_code = lt7911uxc_enum_mbus_code,
1349 .enum_frame_size = lt7911uxc_enum_frame_sizes,
1350 .enum_frame_interval = lt7911uxc_enum_frame_interval,
1351 .set_fmt = lt7911uxc_set_fmt,
1352 .get_fmt = lt7911uxc_get_fmt,
1353 .enum_dv_timings = lt7911uxc_enum_dv_timings,
1354 .dv_timings_cap = lt7911uxc_dv_timings_cap,
1355 .get_mbus_config = lt7911uxc_g_mbus_config,
1356 };
1357
1358 static const struct v4l2_subdev_ops lt7911uxc_ops = {
1359 .core = <7911uxc_core_ops,
1360 .video = <7911uxc_video_ops,
1361 .pad = <7911uxc_pad_ops,
1362 };
1363
1364 static const struct v4l2_ctrl_config lt7911uxc_ctrl_audio_sampling_rate = {
1365 .id = RK_V4L2_CID_AUDIO_SAMPLING_RATE,
1366 .name = "Audio sampling rate",
1367 .type = V4L2_CTRL_TYPE_INTEGER,
1368 .min = 0,
1369 .max = 768000,
1370 .step = 1,
1371 .def = 0,
1372 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1373 };
1374
1375 static const struct v4l2_ctrl_config lt7911uxc_ctrl_audio_present = {
1376 .id = RK_V4L2_CID_AUDIO_PRESENT,
1377 .name = "Audio present",
1378 .type = V4L2_CTRL_TYPE_BOOLEAN,
1379 .min = 0,
1380 .max = 1,
1381 .step = 1,
1382 .def = 0,
1383 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1384 };
1385
lt7911uxc_init_v4l2_ctrls(struct lt7911uxc * lt7911uxc)1386 static int lt7911uxc_init_v4l2_ctrls(struct lt7911uxc *lt7911uxc)
1387 {
1388 const struct lt7911uxc_mode *mode;
1389 struct v4l2_subdev *sd;
1390 int ret;
1391
1392 mode = lt7911uxc->cur_mode;
1393 sd = <7911uxc->sd;
1394 ret = v4l2_ctrl_handler_init(<7911uxc->hdl, 5);
1395 if (ret)
1396 return ret;
1397
1398 lt7911uxc->link_freq = v4l2_ctrl_new_int_menu(<7911uxc->hdl, NULL,
1399 V4L2_CID_LINK_FREQ,
1400 ARRAY_SIZE(link_freq_menu_items) - 1, 0,
1401 link_freq_menu_items);
1402 lt7911uxc->pixel_rate = v4l2_ctrl_new_std(<7911uxc->hdl, NULL,
1403 V4L2_CID_PIXEL_RATE,
1404 0, LT7911UXC_PIXEL_RATE, 1, LT7911UXC_PIXEL_RATE);
1405
1406 lt7911uxc->detect_tx_5v_ctrl = v4l2_ctrl_new_std(<7911uxc->hdl,
1407 NULL, V4L2_CID_DV_RX_POWER_PRESENT,
1408 0, 1, 0, 0);
1409
1410 lt7911uxc->audio_sampling_rate_ctrl =
1411 v4l2_ctrl_new_custom(<7911uxc->hdl,
1412 <7911uxc_ctrl_audio_sampling_rate, NULL);
1413 lt7911uxc->audio_present_ctrl = v4l2_ctrl_new_custom(<7911uxc->hdl,
1414 <7911uxc_ctrl_audio_present, NULL);
1415
1416 sd->ctrl_handler = <7911uxc->hdl;
1417 if (lt7911uxc->hdl.error) {
1418 ret = lt7911uxc->hdl.error;
1419 v4l2_err(sd, "cfg v4l2 ctrls failed! ret:%d\n", ret);
1420 return ret;
1421 }
1422
1423 __v4l2_ctrl_s_ctrl(lt7911uxc->link_freq, mode->mipi_freq_idx);
1424 __v4l2_ctrl_s_ctrl_int64(lt7911uxc->pixel_rate, LT7911UXC_PIXEL_RATE);
1425
1426 if (lt7911uxc_update_controls(sd)) {
1427 ret = -ENODEV;
1428 v4l2_err(sd, "update v4l2 ctrls failed! ret:%d\n", ret);
1429 return ret;
1430 }
1431
1432 return 0;
1433 }
1434
1435 #ifdef CONFIG_OF
lt7911uxc_probe_of(struct lt7911uxc * lt7911uxc)1436 static int lt7911uxc_probe_of(struct lt7911uxc *lt7911uxc)
1437 {
1438 struct device *dev = <7911uxc->i2c_client->dev;
1439 struct device_node *node = dev->of_node;
1440 struct device_node *ep;
1441 int ret;
1442
1443 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1444 <7911uxc->module_index);
1445 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1446 <7911uxc->module_facing);
1447 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1448 <7911uxc->module_name);
1449 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1450 <7911uxc->len_name);
1451 if (ret) {
1452 dev_err(dev, "could not get module information!\n");
1453 return -EINVAL;
1454 }
1455
1456 lt7911uxc->power_gpio = devm_gpiod_get_optional(dev, "power",
1457 GPIOD_OUT_LOW);
1458 if (IS_ERR(lt7911uxc->power_gpio)) {
1459 dev_err(dev, "failed to get power gpio\n");
1460 ret = PTR_ERR(lt7911uxc->power_gpio);
1461 return ret;
1462 }
1463
1464 lt7911uxc->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1465 GPIOD_OUT_HIGH);
1466 if (IS_ERR(lt7911uxc->reset_gpio)) {
1467 dev_err(dev, "failed to get reset gpio\n");
1468 ret = PTR_ERR(lt7911uxc->reset_gpio);
1469 return ret;
1470 }
1471
1472 lt7911uxc->plugin_det_gpio = devm_gpiod_get_optional(dev, "plugin-det",
1473 GPIOD_IN);
1474 if (IS_ERR(lt7911uxc->plugin_det_gpio)) {
1475 dev_err(dev, "failed to get plugin det gpio\n");
1476 ret = PTR_ERR(lt7911uxc->plugin_det_gpio);
1477 return ret;
1478 }
1479
1480 ep = of_graph_get_next_endpoint(dev->of_node, NULL);
1481 if (!ep) {
1482 dev_err(dev, "missing endpoint node\n");
1483 return -EINVAL;
1484 }
1485
1486 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep),
1487 <7911uxc->bus_cfg);
1488 if (ret) {
1489 dev_err(dev, "failed to parse endpoint\n");
1490 goto put_node;
1491 }
1492
1493 if (lt7911uxc->bus_cfg.bus_type == V4L2_MBUS_CSI2_DPHY) {
1494 lt7911uxc->support_modes = supported_modes_dphy;
1495 lt7911uxc->cfg_num = ARRAY_SIZE(supported_modes_dphy);
1496 } else {
1497 lt7911uxc->support_modes = supported_modes_cphy;
1498 lt7911uxc->cfg_num = ARRAY_SIZE(supported_modes_cphy);
1499 }
1500
1501 lt7911uxc->xvclk = devm_clk_get(dev, "xvclk");
1502 if (IS_ERR(lt7911uxc->xvclk)) {
1503 dev_err(dev, "failed to get xvclk\n");
1504 ret = -EINVAL;
1505 goto put_node;
1506 }
1507
1508 ret = clk_prepare_enable(lt7911uxc->xvclk);
1509 if (ret) {
1510 dev_err(dev, "Failed! to enable xvclk\n");
1511 goto put_node;
1512 }
1513
1514 lt7911uxc->enable_hdcp = false;
1515
1516 ret = 0;
1517
1518 put_node:
1519 of_node_put(ep);
1520 return ret;
1521 }
1522 #else
lt7911uxc_probe_of(struct lt7911uxc * state)1523 static inline int lt7911uxc_probe_of(struct lt7911uxc *state)
1524 {
1525 return -ENODEV;
1526 }
1527 #endif
1528
__lt7911uxc_power_on(struct lt7911uxc * lt7911uxc)1529 static int __lt7911uxc_power_on(struct lt7911uxc *lt7911uxc)
1530 {
1531 struct device *dev = <7911uxc->i2c_client->dev;
1532
1533 dev_info(dev, "lt7911uxc power on\n");
1534 gpiod_set_value(lt7911uxc->reset_gpio, 1);
1535 usleep_range(20000, 25000);
1536 gpiod_set_value(lt7911uxc->power_gpio, 1);
1537 //delay 20ms before reset
1538 usleep_range(25000, 30000);
1539 gpiod_set_value(lt7911uxc->reset_gpio, 0);
1540 usleep_range(25000, 30000);
1541
1542 return 0;
1543 }
1544
__lt7911uxc_power_off(struct lt7911uxc * lt7911uxc)1545 static void __lt7911uxc_power_off(struct lt7911uxc *lt7911uxc)
1546 {
1547 struct device *dev = <7911uxc->i2c_client->dev;
1548
1549 dev_info(dev, "lt7911uxc power off\n");
1550
1551 if (!IS_ERR(lt7911uxc->reset_gpio))
1552 gpiod_set_value(lt7911uxc->reset_gpio, 1);
1553
1554 if (!IS_ERR(lt7911uxc->power_gpio))
1555 gpiod_set_value(lt7911uxc->power_gpio, 0);
1556 }
1557
lt7911uxc_resume(struct device * dev)1558 static int lt7911uxc_resume(struct device *dev)
1559 {
1560 struct i2c_client *client = to_i2c_client(dev);
1561 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1562 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1563
1564 return __lt7911uxc_power_on(lt7911uxc);
1565 }
1566
lt7911uxc_suspend(struct device * dev)1567 static int lt7911uxc_suspend(struct device *dev)
1568 {
1569 struct i2c_client *client = to_i2c_client(dev);
1570 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1571 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1572
1573 __lt7911uxc_power_off(lt7911uxc);
1574
1575 return 0;
1576 }
1577
1578 static const struct dev_pm_ops lt7911uxc_pm_ops = {
1579 .suspend = lt7911uxc_suspend,
1580 .resume = lt7911uxc_resume,
1581 };
1582
lt7911uxc_check_chip_id(struct lt7911uxc * lt7911uxc)1583 static int lt7911uxc_check_chip_id(struct lt7911uxc *lt7911uxc)
1584 {
1585 struct device *dev = <7911uxc->i2c_client->dev;
1586 struct v4l2_subdev *sd = <7911uxc->sd;
1587 u8 id_h, id_l;
1588 u32 chipid;
1589 int ret = 0;
1590
1591 lt7911uxc_i2c_enable(sd);
1592 id_l = i2c_rd8(sd, CHIPID_REGL);
1593 id_h = i2c_rd8(sd, CHIPID_REGH);
1594 lt7911uxc_i2c_disable(sd);
1595
1596 chipid = (id_h << 8) | id_l;
1597 if (chipid != LT7911UXC_CHIPID) {
1598 dev_err(dev, "chipid err, read:%#x, expect:%#x\n",
1599 chipid, LT7911UXC_CHIPID);
1600 return -EINVAL;
1601 }
1602 dev_info(dev, "check chipid ok, id:%#x", chipid);
1603
1604 return ret;
1605 }
1606
lt7911uxc_probe(struct i2c_client * client,const struct i2c_device_id * id)1607 static int lt7911uxc_probe(struct i2c_client *client,
1608 const struct i2c_device_id *id)
1609 {
1610 struct v4l2_dv_timings default_timing =
1611 V4L2_DV_BT_CEA_640X480P59_94;
1612 struct lt7911uxc *lt7911uxc;
1613 struct v4l2_subdev *sd;
1614 struct device *dev = &client->dev;
1615 char facing[2];
1616 int err;
1617
1618 dev_info(dev, "driver version: %02x.%02x.%02x",
1619 DRIVER_VERSION >> 16,
1620 (DRIVER_VERSION & 0xff00) >> 8,
1621 DRIVER_VERSION & 0x00ff);
1622
1623 lt7911uxc = devm_kzalloc(dev, sizeof(struct lt7911uxc), GFP_KERNEL);
1624 if (!lt7911uxc)
1625 return -ENOMEM;
1626
1627 sd = <7911uxc->sd;
1628 lt7911uxc->i2c_client = client;
1629 lt7911uxc->mbus_fmt_code = LT7911UXC_MEDIA_BUS_FMT;
1630
1631 err = lt7911uxc_probe_of(lt7911uxc);
1632 if (err) {
1633 v4l2_err(sd, "lt7911uxc_parse_of failed! err:%d\n", err);
1634 return err;
1635 }
1636
1637 lt7911uxc->timings = default_timing;
1638 lt7911uxc->cur_mode = <7911uxc->support_modes[0];
1639
1640 __lt7911uxc_power_on(lt7911uxc);
1641 err = lt7911uxc_check_chip_id(lt7911uxc);
1642 if (err < 0)
1643 return err;
1644
1645 INIT_DELAYED_WORK(<7911uxc->delayed_work_hotplug,
1646 lt7911uxc_delayed_work_hotplug);
1647 INIT_DELAYED_WORK(<7911uxc->delayed_work_res_change,
1648 lt7911uxc_delayed_work_res_change);
1649
1650 if (lt7911uxc->i2c_client->irq) {
1651 v4l2_dbg(1, debug, sd, "cfg lt7911uxc irq!\n");
1652 err = devm_request_threaded_irq(dev,
1653 lt7911uxc->i2c_client->irq,
1654 NULL, lt7911uxc_res_change_irq_handler,
1655 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1656 "lt7911uxc", lt7911uxc);
1657 if (err) {
1658 v4l2_err(sd, "request irq failed! err:%d\n", err);
1659 goto err_work_queues;
1660 }
1661 } else {
1662 v4l2_dbg(1, debug, sd, "no irq, cfg poll!\n");
1663 INIT_WORK(<7911uxc->work_i2c_poll, lt7911uxc_work_i2c_poll);
1664 timer_setup(<7911uxc->timer, lt7911uxc_irq_poll_timer, 0);
1665 lt7911uxc->timer.expires = jiffies +
1666 msecs_to_jiffies(POLL_INTERVAL_MS);
1667 add_timer(<7911uxc->timer);
1668 }
1669
1670 lt7911uxc->plugin_irq = gpiod_to_irq(lt7911uxc->plugin_det_gpio);
1671 if (lt7911uxc->plugin_irq < 0)
1672 dev_err(dev, "failed to get plugin det irq, maybe no use\n");
1673
1674 err = devm_request_threaded_irq(dev, lt7911uxc->plugin_irq, NULL,
1675 plugin_detect_irq_handler, IRQF_TRIGGER_FALLING |
1676 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "lt7911uxc",
1677 lt7911uxc);
1678 if (err)
1679 dev_err(dev, "failed to register plugin det irq (%d), maybe no use\n", err);
1680
1681 mutex_init(<7911uxc->confctl_mutex);
1682 err = lt7911uxc_init_v4l2_ctrls(lt7911uxc);
1683 if (err)
1684 goto err_free_hdl;
1685
1686 client->flags |= I2C_CLIENT_SCCB;
1687 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1688 v4l2_i2c_subdev_init(sd, client, <7911uxc_ops);
1689 sd->internal_ops = <7911uxc_internal_ops;
1690 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1691 #endif
1692
1693 #if defined(CONFIG_MEDIA_CONTROLLER)
1694 lt7911uxc->pad.flags = MEDIA_PAD_FL_SOURCE;
1695 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1696 err = media_entity_pads_init(&sd->entity, 1, <7911uxc->pad);
1697 if (err < 0) {
1698 v4l2_err(sd, "media entity init failed! err:%d\n", err);
1699 goto err_free_hdl;
1700 }
1701 #endif
1702 memset(facing, 0, sizeof(facing));
1703 if (strcmp(lt7911uxc->module_facing, "back") == 0)
1704 facing[0] = 'b';
1705 else
1706 facing[0] = 'f';
1707
1708 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1709 lt7911uxc->module_index, facing,
1710 LT7911UXC_NAME, dev_name(sd->dev));
1711 err = v4l2_async_register_subdev_sensor_common(sd);
1712 if (err < 0) {
1713 v4l2_err(sd, "v4l2 register subdev failed! err:%d\n", err);
1714 goto err_clean_entity;
1715 }
1716
1717 err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
1718 if (err) {
1719 v4l2_err(sd, "v4l2 ctrl handler setup failed! err:%d\n", err);
1720 goto err_clean_entity;
1721 }
1722
1723 schedule_delayed_work(<7911uxc->delayed_work_res_change, 100);
1724
1725 enable_stream(sd, false);
1726 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1727 client->addr << 1, client->adapter->name);
1728
1729 return 0;
1730
1731 err_clean_entity:
1732 #if defined(CONFIG_MEDIA_CONTROLLER)
1733 media_entity_cleanup(&sd->entity);
1734 #endif
1735 err_free_hdl:
1736 v4l2_ctrl_handler_free(<7911uxc->hdl);
1737 mutex_destroy(<7911uxc->confctl_mutex);
1738 err_work_queues:
1739 if (!lt7911uxc->i2c_client->irq)
1740 flush_work(<7911uxc->work_i2c_poll);
1741 cancel_delayed_work(<7911uxc->delayed_work_hotplug);
1742 cancel_delayed_work(<7911uxc->delayed_work_res_change);
1743
1744 return err;
1745 }
1746
lt7911uxc_remove(struct i2c_client * client)1747 static int lt7911uxc_remove(struct i2c_client *client)
1748 {
1749 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1750 struct lt7911uxc *lt7911uxc = to_lt7911uxc(sd);
1751
1752 if (!lt7911uxc->i2c_client->irq) {
1753 del_timer_sync(<7911uxc->timer);
1754 flush_work(<7911uxc->work_i2c_poll);
1755 }
1756 cancel_delayed_work_sync(<7911uxc->delayed_work_hotplug);
1757 cancel_delayed_work_sync(<7911uxc->delayed_work_res_change);
1758 v4l2_async_unregister_subdev(sd);
1759 v4l2_device_unregister_subdev(sd);
1760 #if defined(CONFIG_MEDIA_CONTROLLER)
1761 media_entity_cleanup(&sd->entity);
1762 #endif
1763 v4l2_ctrl_handler_free(<7911uxc->hdl);
1764 mutex_destroy(<7911uxc->confctl_mutex);
1765 clk_disable_unprepare(lt7911uxc->xvclk);
1766
1767 return 0;
1768 }
1769
1770 #if IS_ENABLED(CONFIG_OF)
1771 static const struct of_device_id lt7911uxc_of_match[] = {
1772 { .compatible = "lontium,lt7911uxc" },
1773 {},
1774 };
1775 MODULE_DEVICE_TABLE(of, lt7911uxc_of_match);
1776 #endif
1777
1778 static struct i2c_driver lt7911uxc_driver = {
1779 .driver = {
1780 .name = LT7911UXC_NAME,
1781 .pm = <7911uxc_pm_ops,
1782 .of_match_table = of_match_ptr(lt7911uxc_of_match),
1783 },
1784 .probe = lt7911uxc_probe,
1785 .remove = lt7911uxc_remove,
1786 };
1787
lt7911uxc_driver_init(void)1788 static int __init lt7911uxc_driver_init(void)
1789 {
1790 return i2c_add_driver(<7911uxc_driver);
1791 }
1792
lt7911uxc_driver_exit(void)1793 static void __exit lt7911uxc_driver_exit(void)
1794 {
1795 i2c_del_driver(<7911uxc_driver);
1796 }
1797
1798 device_initcall_sync(lt7911uxc_driver_init);
1799 module_exit(lt7911uxc_driver_exit);
1800
1801 MODULE_DESCRIPTION("Lontium lt7911uxc DP/type-c to CSI-2 bridge driver");
1802 MODULE_AUTHOR("Jianwei Fan <jianwei.fan@rock-chips.com>");
1803 MODULE_LICENSE("GPL");
1804