1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
4 *
5 * Author: Dingxian Wen <shawn.wen@rock-chips.com>
6 * V0.0X01.0X00 first version.
7 * V0.0X01.0X01 fix if plugin_gpio was not used.
8 * V0.0X01.0X02 modify driver init level to late_initcall.
9 */
10
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/hdmi.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of_graph.h>
20 #include <linux/rk-camera-module.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
23 #include <linux/v4l2-dv-timings.h>
24 #include <linux/version.h>
25 #include <linux/videodev2.h>
26 #include <linux/workqueue.h>
27 #include <linux/compat.h>
28 #include <media/v4l2-controls_rockchip.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-dv-timings.h>
32 #include <media/v4l2-event.h>
33 #include <media/v4l2-fwnode.h>
34 #include "lt6911uxc.h"
35
36 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x2)
37 #define LT6911UXC_NAME "LT6911UXC"
38
39 #define LT6911UXC_LINK_FREQ_HIGH 400000000
40 #define LT6911UXC_LINK_FREQ_LOW 200000000
41 #define LT6911UXC_PIXEL_RATE 400000000
42
43 #define I2C_MAX_XFER_SIZE 128
44
45 #ifdef LT6911UXC_OUT_RGB
46 #define LT6911UXC_MEDIA_BUS_FMT MEDIA_BUS_FMT_BGR888_1X24
47 #else
48 #define LT6911UXC_MEDIA_BUS_FMT MEDIA_BUS_FMT_UYVY8_2X8
49 #endif
50
51 static int debug;
52 module_param(debug, int, 0644);
53 MODULE_PARM_DESC(debug, "debug level (0-2)");
54
55 static const s64 link_freq_menu_items[] = {
56 LT6911UXC_LINK_FREQ_HIGH,
57 LT6911UXC_LINK_FREQ_LOW,
58 };
59
60 struct lt6911uxc {
61 struct clk *xvclk;
62 struct delayed_work delayed_work_enable_hotplug;
63 struct delayed_work delayed_work_res_change;
64 struct gpio_desc *hpd_ctl_gpio;
65 struct gpio_desc *plugin_det_gpio;
66 struct gpio_desc *power_gpio;
67 struct gpio_desc *reset_gpio;
68 struct i2c_client *i2c_client;
69 struct media_pad pad;
70 struct mutex confctl_mutex;
71 struct v4l2_ctrl *audio_present_ctrl;
72 struct v4l2_ctrl *audio_sampling_rate_ctrl;
73 struct v4l2_ctrl *detect_tx_5v_ctrl;
74 struct v4l2_ctrl *link_freq;
75 struct v4l2_ctrl *pixel_rate;
76 struct v4l2_ctrl_handler hdl;
77 struct v4l2_dv_timings timings;
78 struct v4l2_fwnode_bus_mipi_csi2 bus;
79 struct v4l2_subdev sd;
80 const char *len_name;
81 const char *module_facing;
82 const char *module_name;
83 const struct lt6911uxc_mode *cur_mode;
84 bool enable_hdcp;
85 bool nosignal;
86 bool is_audio_present;
87 int plugin_irq;
88 u32 mbus_fmt_code;
89 u32 module_index;
90 u32 csi_lanes_in_use;
91 u32 audio_sampling_rate;
92 };
93
94 struct lt6911uxc_mode {
95 u32 width;
96 u32 height;
97 struct v4l2_fract max_fps;
98 u32 hts_def;
99 u32 vts_def;
100 u32 exp_def;
101 };
102
103 static const struct v4l2_dv_timings_cap lt6911uxc_timings_cap = {
104 .type = V4L2_DV_BT_656_1120,
105 /* keep this initialization for compatibility with GCC < 4.4.6 */
106 .reserved = { 0 },
107 V4L2_INIT_BT_TIMINGS(1, 10000, 1, 10000, 0, 400000000,
108 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
109 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
110 V4L2_DV_BT_CAP_PROGRESSIVE |
111 V4L2_DV_BT_CAP_INTERLACED |
112 V4L2_DV_BT_CAP_REDUCED_BLANKING |
113 V4L2_DV_BT_CAP_CUSTOM)
114 };
115
116 static const struct lt6911uxc_mode supported_modes[] = {
117 {
118 .width = 3840,
119 .height = 2160,
120 .max_fps = {
121 .numerator = 10000,
122 .denominator = 300000,
123 },
124 .hts_def = 4400,
125 .vts_def = 2250,
126 }, {
127 .width = 1920,
128 .height = 1080,
129 .max_fps = {
130 .numerator = 10000,
131 .denominator = 600000,
132 },
133 .hts_def = 2200,
134 .vts_def = 1125,
135 }, {
136 .width = 1920,
137 .height = 540,
138 .max_fps = {
139 .numerator = 10000,
140 .denominator = 600000,
141 },
142 }, {
143 .width = 1440,
144 .height = 240,
145 .max_fps = {
146 .numerator = 10000,
147 .denominator = 600000,
148 },
149 }, {
150 .width = 1440,
151 .height = 288,
152 .max_fps = {
153 .numerator = 10000,
154 .denominator = 500000,
155 },
156 }, {
157 .width = 1280,
158 .height = 720,
159 .max_fps = {
160 .numerator = 10000,
161 .denominator = 600000,
162 },
163 .hts_def = 1650,
164 .vts_def = 750,
165 }, {
166 .width = 720,
167 .height = 576,
168 .max_fps = {
169 .numerator = 10000,
170 .denominator = 500000,
171 },
172 .hts_def = 864,
173 .vts_def = 625,
174 }, {
175 .width = 720,
176 .height = 480,
177 .max_fps = {
178 .numerator = 10000,
179 .denominator = 600000,
180 },
181 .hts_def = 858,
182 .vts_def = 525,
183 },
184 };
185
186 static void lt6911uxc_format_change(struct v4l2_subdev *sd);
187 static int lt6911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
188 static int lt6911uxc_s_dv_timings(struct v4l2_subdev *sd,
189 struct v4l2_dv_timings *timings);
190
to_state(struct v4l2_subdev * sd)191 static inline struct lt6911uxc *to_state(struct v4l2_subdev *sd)
192 {
193 return container_of(sd, struct lt6911uxc, sd);
194 }
195
i2c_rd(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)196 static int i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
197 {
198 struct lt6911uxc *lt6911uxc = to_state(sd);
199 struct i2c_client *client = lt6911uxc->i2c_client;
200 struct i2c_msg msgs[3];
201 int err;
202 u8 bank = reg >> 8;
203 u8 reg_addr = reg & 0xFF;
204 u8 buf[2] = {0xFF, bank};
205
206 /* write bank */
207 msgs[0].addr = client->addr;
208 msgs[0].flags = 0;
209 msgs[0].len = 2;
210 msgs[0].buf = buf;
211
212 /* write reg addr */
213 msgs[1].addr = client->addr;
214 msgs[1].flags = 0;
215 msgs[1].len = 1;
216 msgs[1].buf = ®_addr;
217
218 /* read data */
219 msgs[2].addr = client->addr;
220 msgs[2].flags = I2C_M_RD;
221 msgs[2].len = n;
222 msgs[2].buf = values;
223
224 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
225 if (err != ARRAY_SIZE(msgs)) {
226 v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
227 __func__, reg, client->addr);
228 return -EIO;
229 }
230
231 return 0;
232 }
233
i2c_wr(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)234 static int i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
235 {
236 struct lt6911uxc *lt6911uxc = to_state(sd);
237 struct i2c_client *client = lt6911uxc->i2c_client;
238 struct i2c_msg msgs[2];
239 int err, i;
240 u8 data[I2C_MAX_XFER_SIZE];
241 u8 bank = reg >> 8;
242 u8 reg_addr = reg & 0xFF;
243 u8 buf[2] = {0xFF, bank};
244
245 if ((1 + n) > I2C_MAX_XFER_SIZE) {
246 n = I2C_MAX_XFER_SIZE - 1;
247 v4l2_warn(sd, "i2c wr reg=%04x: len=%d is too big!\n", reg,
248 1 + n);
249 }
250
251 data[0] = reg_addr;
252 for (i = 0; i < n; i++)
253 data[i + 1] = values[i];
254
255 /* write bank */
256 msgs[0].addr = client->addr;
257 msgs[0].flags = 0;
258 msgs[0].len = 2;
259 msgs[0].buf = buf;
260
261 /* write reg data */
262 msgs[1].addr = client->addr;
263 msgs[1].flags = 0;
264 msgs[1].len = 1 + n;
265 msgs[1].buf = data;
266
267 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
268 if (err < 0) {
269 v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed\n",
270 __func__, reg, client->addr);
271 return -EIO;
272 }
273
274 return 0;
275 }
276
i2c_rd8(struct v4l2_subdev * sd,u16 reg,u8 * val_p)277 static int i2c_rd8(struct v4l2_subdev *sd, u16 reg, u8 *val_p)
278 {
279 return i2c_rd(sd, reg, val_p, 1);
280 }
281
i2c_wr8(struct v4l2_subdev * sd,u16 reg,u8 val)282 static int i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
283 {
284 return i2c_wr(sd, reg, &val, 1);
285 }
286
lt6911uxc_i2c_enable(struct v4l2_subdev * sd)287 static void lt6911uxc_i2c_enable(struct v4l2_subdev *sd)
288 {
289 i2c_wr8(sd, I2C_EN_REG, I2C_ENABLE);
290 }
291
lt6911uxc_i2c_disable(struct v4l2_subdev * sd)292 static void lt6911uxc_i2c_disable(struct v4l2_subdev *sd)
293 {
294 i2c_wr8(sd, I2C_EN_REG, I2C_DISABLE);
295 }
296
tx_5v_power_present(struct v4l2_subdev * sd)297 static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
298 {
299 bool ret;
300 int val, i, cnt;
301 struct lt6911uxc *lt6911uxc = to_state(sd);
302
303 /* if not use plugin det gpio */
304 if (!lt6911uxc->plugin_det_gpio)
305 return true;
306
307 cnt = 0;
308 for (i = 0; i < 5; i++) {
309 val = gpiod_get_value(lt6911uxc->plugin_det_gpio);
310
311 if (val > 0)
312 cnt++;
313 usleep_range(500, 600);
314 }
315
316 ret = (cnt >= 3) ? true : false;
317 v4l2_dbg(1, debug, sd, "%s: %d\n", __func__, ret);
318
319 return ret;
320 }
321
no_signal(struct v4l2_subdev * sd)322 static inline bool no_signal(struct v4l2_subdev *sd)
323 {
324 struct lt6911uxc *lt6911uxc = to_state(sd);
325
326 v4l2_dbg(1, debug, sd, "%s no signal:%d\n", __func__,
327 lt6911uxc->nosignal);
328
329 return lt6911uxc->nosignal;
330 }
331
audio_present(struct v4l2_subdev * sd)332 static inline bool audio_present(struct v4l2_subdev *sd)
333 {
334 struct lt6911uxc *lt6911uxc = to_state(sd);
335
336 return lt6911uxc->is_audio_present;
337 }
338
get_audio_sampling_rate(struct v4l2_subdev * sd)339 static int get_audio_sampling_rate(struct v4l2_subdev *sd)
340 {
341 struct lt6911uxc *lt6911uxc = to_state(sd);
342
343 if (no_signal(sd))
344 return 0;
345
346 return lt6911uxc->audio_sampling_rate;
347 }
348
fps_calc(const struct v4l2_bt_timings * t)349 static inline unsigned int fps_calc(const struct v4l2_bt_timings *t)
350 {
351 if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
352 return 0;
353
354 return DIV_ROUND_CLOSEST((unsigned int)t->pixelclock,
355 V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
356 }
357
lt6911uxc_rcv_supported_res(struct v4l2_subdev * sd,u32 width,u32 height)358 static bool lt6911uxc_rcv_supported_res(struct v4l2_subdev *sd, u32 width,
359 u32 height)
360 {
361 u32 i;
362
363 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
364 if ((supported_modes[i].width == width) &&
365 (supported_modes[i].height == height)) {
366 break;
367 }
368 }
369
370 if (i == ARRAY_SIZE(supported_modes)) {
371 v4l2_err(sd, "%s do not support res wxh: %dx%d\n", __func__,
372 width, height);
373 return false;
374 } else {
375 return true;
376 }
377 }
378
lt6911uxc_get_detected_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)379 static int lt6911uxc_get_detected_timings(struct v4l2_subdev *sd,
380 struct v4l2_dv_timings *timings)
381 {
382 struct lt6911uxc *lt6911uxc = to_state(sd);
383 struct v4l2_bt_timings *bt = &timings->bt;
384 u32 hact, vact, htotal, vtotal;
385 u32 hbp, hs, hfp, vbp, vs, vfp;
386 u32 pixel_clock, fps;
387 u8 clk_h, clk_m, clk_l;
388 u8 value, val_h, val_l;
389 u32 fw_ver, mipi_byte_clk, mipi_bitrate;
390 u8 fw_a, fw_b, fw_c, fw_d, lanes;
391 int ret;
392
393 memset(timings, 0, sizeof(struct v4l2_dv_timings));
394 lt6911uxc_i2c_enable(sd);
395
396 ret = i2c_rd8(sd, FW_VER_A, &fw_a);
397 ret |= i2c_rd8(sd, FW_VER_B, &fw_b);
398 ret |= i2c_rd8(sd, FW_VER_C, &fw_c);
399 ret |= i2c_rd8(sd, FW_VER_D, &fw_d);
400 if (ret) {
401 v4l2_err(sd, "%s: I2C transform err!\n", __func__);
402 return -ENOLINK;
403 }
404 fw_ver = (fw_a << 24) | (fw_b << 16) | (fw_c << 8) | fw_d;
405 v4l2_info(sd, "read fw_version:%#x", fw_ver);
406 i2c_wr8(sd, INT_COMPARE_REG, RECEIVED_INT);
407
408 i2c_rd8(sd, INT_STATUS_86A3, &val_h);
409 i2c_rd8(sd, INT_STATUS_86A5, &val_l);
410 v4l2_info(sd, "int status REG_86A3:%#x, REG_86A5:%#x\n", val_h, val_l);
411
412 i2c_rd8(sd, HDMI_VERSION, &value);
413 i2c_rd8(sd, TMDS_CLK_H, &clk_h);
414 i2c_rd8(sd, TMDS_CLK_M, &clk_m);
415 i2c_rd8(sd, TMDS_CLK_L, &clk_l);
416 pixel_clock = (((clk_h & 0xf) << 16) | (clk_m << 8) | clk_l) * 1000;
417 if (value & BIT(0)) /* HDMI 2.0 */
418 pixel_clock *= 4;
419
420 i2c_rd8(sd, MIPI_LANES, &lanes);
421 lt6911uxc->csi_lanes_in_use = lanes;
422 i2c_wr8(sd, FM1_DET_CLK_SRC_SEL, AD_LMTX_WRITE_CLK);
423 i2c_rd8(sd, FREQ_METER_H, &clk_h);
424 i2c_rd8(sd, FREQ_METER_M, &clk_m);
425 i2c_rd8(sd, FREQ_METER_L, &clk_l);
426 mipi_byte_clk = (((clk_h & 0xf) << 16) | (clk_m << 8) | clk_l);
427 mipi_bitrate = mipi_byte_clk * 8 / 1000;
428 v4l2_info(sd, "MIPI Byte clk: %dKHz, MIPI bitrate: %dMbps, lanes:%d\n",
429 mipi_byte_clk, mipi_bitrate, lanes);
430
431 i2c_rd8(sd, HTOTAL_H, &val_h);
432 i2c_rd8(sd, HTOTAL_L, &val_l);
433 htotal = ((val_h << 8) | val_l) * 2;
434 i2c_rd8(sd, VTOTAL_H, &val_h);
435 i2c_rd8(sd, VTOTAL_L, &val_l);
436 vtotal = (val_h << 8) | val_l;
437 i2c_rd8(sd, HACT_H, &val_h);
438 i2c_rd8(sd, HACT_L, &val_l);
439 hact = ((val_h << 8) | val_l) * 2;
440 i2c_rd8(sd, VACT_H, &val_h);
441 i2c_rd8(sd, VACT_L, &val_l);
442 vact = (val_h << 8) | val_l;
443 i2c_rd8(sd, HS_H, &val_h);
444 i2c_rd8(sd, HS_L, &val_l);
445 hs = ((val_h << 8) | val_l) * 2;
446 i2c_rd8(sd, VS, &value);
447 vs = value;
448 i2c_rd8(sd, HFP_H, &val_h);
449 i2c_rd8(sd, HFP_L, &val_l);
450 hfp = ((val_h << 8) | val_l) * 2;
451 i2c_rd8(sd, VFP, &value);
452 vfp = value;
453 i2c_rd8(sd, HBP_H, &val_h);
454 i2c_rd8(sd, HBP_L, &val_l);
455 hbp = ((val_h << 8) | val_l) * 2;
456 i2c_rd8(sd, VBP, &value);
457 vbp = value;
458 lt6911uxc_i2c_disable(sd);
459
460 if (!lt6911uxc_rcv_supported_res(sd, hact, vact)) {
461 lt6911uxc->nosignal = true;
462 v4l2_err(sd, "%s: rcv err res, return no signal!\n", __func__);
463 return -EINVAL;
464 }
465
466 lt6911uxc->nosignal = false;
467 i2c_rd8(sd, AUDIO_IN_STATUS, &value);
468 lt6911uxc->is_audio_present = (value & BIT(5)) ? true : false;
469 i2c_rd8(sd, AUDIO_SAMPLE_RATAE_H, &val_h);
470 i2c_rd8(sd, AUDIO_SAMPLE_RATAE_L, &val_l);
471 lt6911uxc->audio_sampling_rate = ((val_h << 8) | val_l) + 2;
472 v4l2_info(sd, "is_audio_present: %d, audio_sampling_rate: %dKhz\n",
473 lt6911uxc->is_audio_present,
474 lt6911uxc->audio_sampling_rate);
475
476 timings->type = V4L2_DV_BT_656_1120;
477 bt->width = hact;
478 bt->height = vact;
479 bt->vsync = vs;
480 bt->hsync = hs;
481 bt->pixelclock = pixel_clock;
482 bt->hfrontporch = hfp;
483 bt->vfrontporch = vfp;
484 bt->hbackporch = hbp;
485 bt->vbackporch = vbp;
486 fps = fps_calc(bt);
487
488 /* for interlaced res 1080i 576i 480i*/
489 if ((hact == 1920 && vact == 540) || (hact == 1440 && vact == 288)
490 || (hact == 1440 && vact == 240)) {
491 bt->interlaced = V4L2_DV_INTERLACED;
492 bt->height *= 2;
493 bt->il_vsync = bt->vsync + 1;
494 } else {
495 bt->interlaced = V4L2_DV_PROGRESSIVE;
496 }
497
498 v4l2_info(sd, "act:%dx%d, total:%dx%d, pixclk:%d, fps:%d\n",
499 hact, vact, htotal, vtotal, pixel_clock, fps);
500 v4l2_info(sd, "hfp:%d, hs:%d, hbp:%d, vfp:%d, vs:%d, vbp:%d, inerlaced:%d\n",
501 bt->hfrontporch, bt->hsync, bt->hbackporch, bt->vfrontporch,
502 bt->vsync, bt->vbackporch, bt->interlaced);
503
504 return 0;
505 }
506
lt6911uxc_config_hpd(struct v4l2_subdev * sd)507 static void lt6911uxc_config_hpd(struct v4l2_subdev *sd)
508 {
509 struct lt6911uxc *lt6911uxc = to_state(sd);
510 bool plugin;
511
512 plugin = tx_5v_power_present(sd);
513 v4l2_dbg(2, debug, sd, "%s: plugin: %d\n", __func__, plugin);
514
515 if (plugin) {
516 gpiod_set_value(lt6911uxc->hpd_ctl_gpio, 1);
517 } else {
518 lt6911uxc->nosignal = true;
519 gpiod_set_value(lt6911uxc->hpd_ctl_gpio, 0);
520 }
521 }
522
lt6911uxc_delayed_work_enable_hotplug(struct work_struct * work)523 static void lt6911uxc_delayed_work_enable_hotplug(struct work_struct *work)
524 {
525 struct delayed_work *dwork = to_delayed_work(work);
526 struct lt6911uxc *lt6911uxc = container_of(dwork,
527 struct lt6911uxc, delayed_work_enable_hotplug);
528 struct v4l2_subdev *sd = <6911uxc->sd;
529
530 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
531
532 v4l2_ctrl_s_ctrl(lt6911uxc->detect_tx_5v_ctrl, tx_5v_power_present(sd));
533 lt6911uxc_config_hpd(sd);
534 }
535
lt6911uxc_delayed_work_res_change(struct work_struct * work)536 static void lt6911uxc_delayed_work_res_change(struct work_struct *work)
537 {
538 struct delayed_work *dwork = to_delayed_work(work);
539 struct lt6911uxc *lt6911uxc = container_of(dwork,
540 struct lt6911uxc, delayed_work_res_change);
541 struct v4l2_subdev *sd = <6911uxc->sd;
542
543 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
544 lt6911uxc_format_change(sd);
545 }
546
lt6911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev * sd)547 static int lt6911uxc_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
548 {
549 struct lt6911uxc *lt6911uxc = to_state(sd);
550
551 return v4l2_ctrl_s_ctrl(lt6911uxc->detect_tx_5v_ctrl,
552 tx_5v_power_present(sd));
553 }
554
lt6911uxc_s_ctrl_audio_sampling_rate(struct v4l2_subdev * sd)555 static int lt6911uxc_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
556 {
557 struct lt6911uxc *lt6911uxc = to_state(sd);
558
559 return v4l2_ctrl_s_ctrl(lt6911uxc->audio_sampling_rate_ctrl,
560 get_audio_sampling_rate(sd));
561 }
562
lt6911uxc_s_ctrl_audio_present(struct v4l2_subdev * sd)563 static int lt6911uxc_s_ctrl_audio_present(struct v4l2_subdev *sd)
564 {
565 struct lt6911uxc *lt6911uxc = to_state(sd);
566
567 return v4l2_ctrl_s_ctrl(lt6911uxc->audio_present_ctrl,
568 audio_present(sd));
569 }
570
lt6911uxc_update_controls(struct v4l2_subdev * sd)571 static int lt6911uxc_update_controls(struct v4l2_subdev *sd)
572 {
573 int ret = 0;
574
575 ret |= lt6911uxc_s_ctrl_detect_tx_5v(sd);
576 ret |= lt6911uxc_s_ctrl_audio_sampling_rate(sd);
577 ret |= lt6911uxc_s_ctrl_audio_present(sd);
578
579 return ret;
580 }
581
enable_stream(struct v4l2_subdev * sd,bool enable)582 static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
583 {
584 v4l2_dbg(2, debug, sd, "%s: %sable\n",
585 __func__, enable ? "en" : "dis");
586 }
587
lt6911uxc_format_change(struct v4l2_subdev * sd)588 static void lt6911uxc_format_change(struct v4l2_subdev *sd)
589 {
590 struct lt6911uxc *lt6911uxc = to_state(sd);
591 struct v4l2_dv_timings timings;
592 const struct v4l2_event lt6911uxc_ev_fmt = {
593 .type = V4L2_EVENT_SOURCE_CHANGE,
594 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
595 };
596
597 if (lt6911uxc_get_detected_timings(sd, &timings)) {
598 enable_stream(sd, false);
599
600 v4l2_dbg(1, debug, sd, "%s: No signal\n", __func__);
601 } else {
602 if (!v4l2_match_dv_timings(<6911uxc->timings, &timings, 0,
603 false)) {
604 enable_stream(sd, false);
605 /* automatically set timing rather than set by user */
606 lt6911uxc_s_dv_timings(sd, &timings);
607 v4l2_print_dv_timings(sd->name,
608 "Format_change: New format: ",
609 &timings, false);
610 }
611 }
612
613 if (sd->devnode)
614 v4l2_subdev_notify_event(sd, <6911uxc_ev_fmt);
615 }
616
lt6911uxc_isr(struct v4l2_subdev * sd,u32 status,bool * handled)617 static int lt6911uxc_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
618 {
619 struct lt6911uxc *lt6911uxc = to_state(sd);
620
621 schedule_delayed_work(<6911uxc->delayed_work_res_change, HZ / 20);
622 *handled = true;
623
624 return 0;
625 }
626
lt6911uxc_res_change_irq_handler(int irq,void * dev_id)627 static irqreturn_t lt6911uxc_res_change_irq_handler(int irq, void *dev_id)
628 {
629 struct lt6911uxc *lt6911uxc = dev_id;
630 bool handled;
631
632 lt6911uxc_isr(<6911uxc->sd, 0, &handled);
633
634 return handled ? IRQ_HANDLED : IRQ_NONE;
635 }
636
plugin_detect_irq_handler(int irq,void * dev_id)637 static irqreturn_t plugin_detect_irq_handler(int irq, void *dev_id)
638 {
639 struct lt6911uxc *lt6911uxc = dev_id;
640 struct v4l2_subdev *sd = <6911uxc->sd;
641
642 /* control hpd output level after 25ms */
643 schedule_delayed_work(<6911uxc->delayed_work_enable_hotplug,
644 HZ / 40);
645 tx_5v_power_present(sd);
646
647 return IRQ_HANDLED;
648 }
649
lt6911uxc_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)650 static int lt6911uxc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
651 struct v4l2_event_subscription *sub)
652 {
653 switch (sub->type) {
654 case V4L2_EVENT_SOURCE_CHANGE:
655 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
656 case V4L2_EVENT_CTRL:
657 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
658 default:
659 return -EINVAL;
660 }
661 }
662
lt6911uxc_g_input_status(struct v4l2_subdev * sd,u32 * status)663 static int lt6911uxc_g_input_status(struct v4l2_subdev *sd, u32 *status)
664 {
665 *status = 0;
666 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
667 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
668
669 return 0;
670 }
671
lt6911uxc_s_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)672 static int lt6911uxc_s_dv_timings(struct v4l2_subdev *sd,
673 struct v4l2_dv_timings *timings)
674 {
675 struct lt6911uxc *lt6911uxc = to_state(sd);
676
677 if (!timings)
678 return -EINVAL;
679
680 if (debug)
681 v4l2_print_dv_timings(sd->name, "s_dv_timings: ", timings, false);
682
683 if (v4l2_match_dv_timings(<6911uxc->timings, timings, 0, false)) {
684 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
685 return 0;
686 }
687
688 if (!v4l2_valid_dv_timings(timings,
689 <6911uxc_timings_cap, NULL, NULL)) {
690 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
691 return -ERANGE;
692 }
693
694 lt6911uxc->timings = *timings;
695
696 enable_stream(sd, false);
697
698 return 0;
699 }
700
lt6911uxc_g_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)701 static int lt6911uxc_g_dv_timings(struct v4l2_subdev *sd,
702 struct v4l2_dv_timings *timings)
703 {
704 struct lt6911uxc *lt6911uxc = to_state(sd);
705
706 *timings = lt6911uxc->timings;
707
708 return 0;
709 }
710
lt6911uxc_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)711 static int lt6911uxc_enum_dv_timings(struct v4l2_subdev *sd,
712 struct v4l2_enum_dv_timings *timings)
713 {
714 if (timings->pad != 0)
715 return -EINVAL;
716
717 return v4l2_enum_dv_timings_cap(timings,
718 <6911uxc_timings_cap, NULL, NULL);
719 }
720
lt6911uxc_query_dv_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)721 static int lt6911uxc_query_dv_timings(struct v4l2_subdev *sd,
722 struct v4l2_dv_timings *timings)
723 {
724 struct lt6911uxc *lt6911uxc = to_state(sd);
725
726 *timings = lt6911uxc->timings;
727 if (debug)
728 v4l2_print_dv_timings(sd->name, "query_dv_timings: ", timings, false);
729
730 if (!v4l2_valid_dv_timings(timings, <6911uxc_timings_cap, NULL,
731 NULL)) {
732 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
733
734 return -ERANGE;
735 }
736
737 return 0;
738 }
739
lt6911uxc_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)740 static int lt6911uxc_dv_timings_cap(struct v4l2_subdev *sd,
741 struct v4l2_dv_timings_cap *cap)
742 {
743 if (cap->pad != 0)
744 return -EINVAL;
745
746 *cap = lt6911uxc_timings_cap;
747
748 return 0;
749 }
750
lt6911uxc_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)751 static int lt6911uxc_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
752 struct v4l2_mbus_config *cfg)
753 {
754 struct lt6911uxc *lt6911uxc = to_state(sd);
755
756 cfg->type = V4L2_MBUS_CSI2_DPHY;
757 cfg->flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK | V4L2_MBUS_CSI2_CHANNEL_0;
758
759 switch (lt6911uxc->csi_lanes_in_use) {
760 case 1:
761 cfg->flags |= V4L2_MBUS_CSI2_1_LANE;
762 break;
763 case 2:
764 cfg->flags |= V4L2_MBUS_CSI2_2_LANE;
765 break;
766 case 3:
767 cfg->flags |= V4L2_MBUS_CSI2_3_LANE;
768 break;
769 case 4:
770 cfg->flags |= V4L2_MBUS_CSI2_4_LANE;
771 break;
772
773 default:
774 return -EINVAL;
775 }
776
777 return 0;
778 }
779
lt6911uxc_s_stream(struct v4l2_subdev * sd,int enable)780 static int lt6911uxc_s_stream(struct v4l2_subdev *sd, int enable)
781 {
782 enable_stream(sd, enable);
783
784 return 0;
785 }
786
lt6911uxc_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)787 static int lt6911uxc_enum_mbus_code(struct v4l2_subdev *sd,
788 struct v4l2_subdev_pad_config *cfg,
789 struct v4l2_subdev_mbus_code_enum *code)
790 {
791 switch (code->index) {
792 case 0:
793 code->code = LT6911UXC_MEDIA_BUS_FMT;
794 break;
795
796 default:
797 return -EINVAL;
798 }
799
800 return 0;
801 }
802
lt6911uxc_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)803 static int lt6911uxc_enum_frame_sizes(struct v4l2_subdev *sd,
804 struct v4l2_subdev_pad_config *cfg,
805 struct v4l2_subdev_frame_size_enum *fse)
806 {
807 if (fse->index >= ARRAY_SIZE(supported_modes))
808 return -EINVAL;
809
810 if (fse->code != LT6911UXC_MEDIA_BUS_FMT)
811 return -EINVAL;
812
813 fse->min_width = supported_modes[fse->index].width;
814 fse->max_width = supported_modes[fse->index].width;
815 fse->max_height = supported_modes[fse->index].height;
816 fse->min_height = supported_modes[fse->index].height;
817
818 return 0;
819 }
820
lt6911uxc_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)821 static int lt6911uxc_enum_frame_interval(struct v4l2_subdev *sd,
822 struct v4l2_subdev_pad_config *cfg,
823 struct v4l2_subdev_frame_interval_enum *fie)
824 {
825 if (fie->index >= ARRAY_SIZE(supported_modes))
826 return -EINVAL;
827
828 fie->code = LT6911UXC_MEDIA_BUS_FMT;
829
830 fie->width = supported_modes[fie->index].width;
831 fie->height = supported_modes[fie->index].height;
832 fie->interval = supported_modes[fie->index].max_fps;
833
834 return 0;
835 }
836
lt6911uxc_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)837 static int lt6911uxc_get_fmt(struct v4l2_subdev *sd,
838 struct v4l2_subdev_pad_config *cfg,
839 struct v4l2_subdev_format *format)
840 {
841 struct lt6911uxc *lt6911uxc = to_state(sd);
842
843 mutex_lock(<6911uxc->confctl_mutex);
844 format->format.code = lt6911uxc->mbus_fmt_code;
845 format->format.width = lt6911uxc->timings.bt.width;
846 format->format.height = lt6911uxc->timings.bt.height;
847 format->format.field =
848 lt6911uxc->timings.bt.interlaced ?
849 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE;
850 format->format.colorspace = V4L2_COLORSPACE_SRGB;
851 mutex_unlock(<6911uxc->confctl_mutex);
852
853 v4l2_dbg(1, debug, sd, "%s: fmt code:%d, w:%d, h:%d, field mode:%s\n",
854 __func__, format->format.code, format->format.width, format->format.height,
855 (format->format.field == V4L2_FIELD_INTERLACED) ? "I" : "P");
856
857 return 0;
858 }
859
lt6911uxc_get_reso_dist(const struct lt6911uxc_mode * mode,struct v4l2_mbus_framefmt * framefmt)860 static int lt6911uxc_get_reso_dist(const struct lt6911uxc_mode *mode,
861 struct v4l2_mbus_framefmt *framefmt)
862 {
863 return abs(mode->width - framefmt->width) +
864 abs(mode->height - framefmt->height);
865 }
866
867 static const struct lt6911uxc_mode *
lt6911uxc_find_best_fit(struct v4l2_subdev_format * fmt)868 lt6911uxc_find_best_fit(struct v4l2_subdev_format *fmt)
869 {
870 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
871 int dist;
872 int cur_best_fit = 0;
873 int cur_best_fit_dist = -1;
874 unsigned int i;
875
876 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
877 dist = lt6911uxc_get_reso_dist(&supported_modes[i], framefmt);
878 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
879 cur_best_fit_dist = dist;
880 cur_best_fit = i;
881 }
882 }
883
884 return &supported_modes[cur_best_fit];
885 }
886
lt6911uxc_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)887 static int lt6911uxc_set_fmt(struct v4l2_subdev *sd,
888 struct v4l2_subdev_pad_config *cfg,
889 struct v4l2_subdev_format *format)
890 {
891 struct lt6911uxc *lt6911uxc = to_state(sd);
892 const struct lt6911uxc_mode *mode;
893 int index;
894
895 /* is overwritten by get_fmt */
896 u32 code = format->format.code;
897 int ret = lt6911uxc_get_fmt(sd, cfg, format);
898
899 format->format.code = code;
900
901 if (ret)
902 return ret;
903
904 switch (code) {
905 case LT6911UXC_MEDIA_BUS_FMT:
906 break;
907
908 default:
909 return -EINVAL;
910 }
911
912 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
913 return 0;
914
915 lt6911uxc->mbus_fmt_code = format->format.code;
916 mode = lt6911uxc_find_best_fit(format);
917 lt6911uxc->cur_mode = mode;
918 enable_stream(sd, false);
919
920 if (((mode->width == 720) && (mode->height == 576)) ||
921 ((mode->width == 720) && (mode->height == 480)))
922 index = 1;
923 else
924 index = 0;
925
926 __v4l2_ctrl_s_ctrl(lt6911uxc->link_freq, index);
927 v4l2_dbg(1, debug, sd, "%s res wxh:%dx%d, link freq:%llu", __func__,
928 mode->width, mode->height, link_freq_menu_items[index]);
929
930 return 0;
931 }
932
lt6911uxc_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)933 static int lt6911uxc_g_frame_interval(struct v4l2_subdev *sd,
934 struct v4l2_subdev_frame_interval *fi)
935 {
936 struct lt6911uxc *lt6911uxc = to_state(sd);
937 const struct lt6911uxc_mode *mode = lt6911uxc->cur_mode;
938
939 mutex_lock(<6911uxc->confctl_mutex);
940 fi->interval = mode->max_fps;
941 mutex_unlock(<6911uxc->confctl_mutex);
942
943 return 0;
944 }
945
lt6911uxc_get_module_inf(struct lt6911uxc * lt6911uxc,struct rkmodule_inf * inf)946 static void lt6911uxc_get_module_inf(struct lt6911uxc *lt6911uxc,
947 struct rkmodule_inf *inf)
948 {
949 memset(inf, 0, sizeof(*inf));
950 strscpy(inf->base.sensor, LT6911UXC_NAME, sizeof(inf->base.sensor));
951 strscpy(inf->base.module, lt6911uxc->module_name, sizeof(inf->base.module));
952 strscpy(inf->base.lens, lt6911uxc->len_name, sizeof(inf->base.lens));
953 }
954
lt6911uxc_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)955 static long lt6911uxc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
956 {
957 struct lt6911uxc *lt6911uxc = to_state(sd);
958 long ret = 0;
959
960 switch (cmd) {
961 case RKMODULE_GET_MODULE_INFO:
962 lt6911uxc_get_module_inf(lt6911uxc, (struct rkmodule_inf *)arg);
963 break;
964 case RKMODULE_GET_HDMI_MODE:
965 *(int *)arg = RKMODULE_HDMIIN_MODE;
966 break;
967 default:
968 ret = -ENOIOCTLCMD;
969 break;
970 }
971
972 return ret;
973 }
974
975 #ifdef CONFIG_COMPAT
lt6911uxc_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)976 static long lt6911uxc_compat_ioctl32(struct v4l2_subdev *sd,
977 unsigned int cmd, unsigned long arg)
978 {
979 void __user *up = compat_ptr(arg);
980 struct rkmodule_inf *inf;
981 long ret;
982 int *seq;
983
984 switch (cmd) {
985 case RKMODULE_GET_MODULE_INFO:
986 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
987 if (!inf) {
988 ret = -ENOMEM;
989 return ret;
990 }
991
992 ret = lt6911uxc_ioctl(sd, cmd, inf);
993 if (!ret) {
994 ret = copy_to_user(up, inf, sizeof(*inf));
995 if (ret)
996 ret = -EFAULT;
997 }
998 kfree(inf);
999 break;
1000 case RKMODULE_GET_HDMI_MODE:
1001 seq = kzalloc(sizeof(*seq), GFP_KERNEL);
1002 if (!seq) {
1003 ret = -ENOMEM;
1004 return ret;
1005 }
1006
1007 ret = lt6911uxc_ioctl(sd, cmd, seq);
1008 if (!ret) {
1009 ret = copy_to_user(up, seq, sizeof(*seq));
1010 if (ret)
1011 ret = -EFAULT;
1012 }
1013 kfree(seq);
1014 break;
1015 default:
1016 ret = -ENOIOCTLCMD;
1017 break;
1018 }
1019
1020 return ret;
1021 }
1022 #endif
1023
1024 static const struct v4l2_subdev_core_ops lt6911uxc_core_ops = {
1025 .interrupt_service_routine = lt6911uxc_isr,
1026 .subscribe_event = lt6911uxc_subscribe_event,
1027 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1028 .ioctl = lt6911uxc_ioctl,
1029 #ifdef CONFIG_COMPAT
1030 .compat_ioctl32 = lt6911uxc_compat_ioctl32,
1031 #endif
1032 };
1033
1034 static const struct v4l2_subdev_video_ops lt6911uxc_video_ops = {
1035 .g_input_status = lt6911uxc_g_input_status,
1036 .s_dv_timings = lt6911uxc_s_dv_timings,
1037 .g_dv_timings = lt6911uxc_g_dv_timings,
1038 .query_dv_timings = lt6911uxc_query_dv_timings,
1039 .s_stream = lt6911uxc_s_stream,
1040 .g_frame_interval = lt6911uxc_g_frame_interval,
1041 };
1042
1043 static const struct v4l2_subdev_pad_ops lt6911uxc_pad_ops = {
1044 .enum_mbus_code = lt6911uxc_enum_mbus_code,
1045 .enum_frame_size = lt6911uxc_enum_frame_sizes,
1046 .enum_frame_interval = lt6911uxc_enum_frame_interval,
1047 .set_fmt = lt6911uxc_set_fmt,
1048 .get_fmt = lt6911uxc_get_fmt,
1049 .enum_dv_timings = lt6911uxc_enum_dv_timings,
1050 .dv_timings_cap = lt6911uxc_dv_timings_cap,
1051 .get_mbus_config = lt6911uxc_g_mbus_config,
1052 };
1053
1054 static const struct v4l2_subdev_ops lt6911uxc_ops = {
1055 .core = <6911uxc_core_ops,
1056 .video = <6911uxc_video_ops,
1057 .pad = <6911uxc_pad_ops,
1058 };
1059
1060 static const struct v4l2_ctrl_config lt6911uxc_ctrl_audio_sampling_rate = {
1061 .id = RK_V4L2_CID_AUDIO_SAMPLING_RATE,
1062 .name = "Audio sampling rate",
1063 .type = V4L2_CTRL_TYPE_INTEGER,
1064 .min = 0,
1065 .max = 768000,
1066 .step = 1,
1067 .def = 0,
1068 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1069 };
1070
1071 static const struct v4l2_ctrl_config lt6911uxc_ctrl_audio_present = {
1072 .id = RK_V4L2_CID_AUDIO_PRESENT,
1073 .name = "Audio present",
1074 .type = V4L2_CTRL_TYPE_BOOLEAN,
1075 .min = 0,
1076 .max = 1,
1077 .step = 1,
1078 .def = 0,
1079 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1080 };
1081
lt6911uxc_reset(struct lt6911uxc * lt6911uxc)1082 static void lt6911uxc_reset(struct lt6911uxc *lt6911uxc)
1083 {
1084 gpiod_set_value(lt6911uxc->reset_gpio, 0);
1085 usleep_range(2000, 2100);
1086 gpiod_set_value(lt6911uxc->reset_gpio, 1);
1087 usleep_range(120*1000, 121*1000);
1088 gpiod_set_value(lt6911uxc->reset_gpio, 0);
1089 usleep_range(300*1000, 310*1000);
1090 }
1091
lt6911uxc_init_v4l2_ctrls(struct lt6911uxc * lt6911uxc)1092 static int lt6911uxc_init_v4l2_ctrls(struct lt6911uxc *lt6911uxc)
1093 {
1094 struct v4l2_subdev *sd;
1095 int ret;
1096
1097 sd = <6911uxc->sd;
1098 ret = v4l2_ctrl_handler_init(<6911uxc->hdl, 5);
1099 if (ret)
1100 return ret;
1101
1102 lt6911uxc->link_freq = v4l2_ctrl_new_int_menu(<6911uxc->hdl, NULL,
1103 V4L2_CID_LINK_FREQ,
1104 ARRAY_SIZE(link_freq_menu_items) - 1, 0,
1105 link_freq_menu_items);
1106 v4l2_ctrl_new_std(<6911uxc->hdl, NULL, V4L2_CID_PIXEL_RATE,
1107 0, LT6911UXC_PIXEL_RATE, 1, LT6911UXC_PIXEL_RATE);
1108
1109 lt6911uxc->detect_tx_5v_ctrl = v4l2_ctrl_new_std(<6911uxc->hdl,
1110 NULL, V4L2_CID_DV_RX_POWER_PRESENT,
1111 0, 1, 0, 0);
1112
1113 lt6911uxc->audio_sampling_rate_ctrl =
1114 v4l2_ctrl_new_custom(<6911uxc->hdl,
1115 <6911uxc_ctrl_audio_sampling_rate, NULL);
1116 lt6911uxc->audio_present_ctrl = v4l2_ctrl_new_custom(<6911uxc->hdl,
1117 <6911uxc_ctrl_audio_present, NULL);
1118
1119 sd->ctrl_handler = <6911uxc->hdl;
1120 if (lt6911uxc->hdl.error) {
1121 ret = lt6911uxc->hdl.error;
1122 v4l2_err(sd, "cfg v4l2 ctrls failed! ret:%d\n", ret);
1123 return ret;
1124 }
1125
1126 if (lt6911uxc_update_controls(sd)) {
1127 ret = -ENODEV;
1128 v4l2_err(sd, "update v4l2 ctrls failed! ret:%d\n", ret);
1129 return ret;
1130 }
1131
1132 return 0;
1133 }
1134
lt6911uxc_check_chip_id(struct lt6911uxc * lt6911uxc)1135 static int lt6911uxc_check_chip_id(struct lt6911uxc *lt6911uxc)
1136 {
1137 struct device *dev = <6911uxc->i2c_client->dev;
1138 struct v4l2_subdev *sd = <6911uxc->sd;
1139 u8 fw_a, fw_b, fw_c, fw_d;
1140 u8 id_h, id_l;
1141 u32 chipid, fw_ver;
1142 int ret;
1143
1144 lt6911uxc_i2c_enable(sd);
1145 ret = i2c_rd8(sd, CHIPID_L, &id_l);
1146 ret |= i2c_rd8(sd, CHIPID_H, &id_h);
1147
1148 ret |= i2c_rd8(sd, FW_VER_A, &fw_a);
1149 ret |= i2c_rd8(sd, FW_VER_B, &fw_b);
1150 ret |= i2c_rd8(sd, FW_VER_C, &fw_c);
1151 ret |= i2c_rd8(sd, FW_VER_D, &fw_d);
1152 lt6911uxc_i2c_disable(sd);
1153
1154 if (!ret) {
1155 chipid = (id_h << 8) | id_l;
1156 if (chipid != LT6911UXC_CHIPID) {
1157 dev_err(dev, "chipid err, read:%#x, expect:%#x\n",
1158 chipid, LT6911UXC_CHIPID);
1159 return -EINVAL;
1160 }
1161
1162 fw_ver = (fw_a << 24) | (fw_b << 16) | (fw_c << 8) | fw_d;
1163 dev_info(dev, "chipid ok, id:%#x, fw_ver:%#x", chipid, fw_ver);
1164 ret = 0;
1165 } else {
1166 dev_err(dev, "%s i2c trans failed!\n", __func__);
1167 }
1168
1169 return ret;
1170 }
1171
1172 #ifdef CONFIG_OF
lt6911uxc_parse_of(struct lt6911uxc * lt6911uxc)1173 static int lt6911uxc_parse_of(struct lt6911uxc *lt6911uxc)
1174 {
1175 struct device *dev = <6911uxc->i2c_client->dev;
1176 struct device_node *node = dev->of_node;
1177 struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
1178 struct device_node *ep;
1179 int ret;
1180
1181 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1182 <6911uxc->module_index);
1183 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1184 <6911uxc->module_facing);
1185 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1186 <6911uxc->module_name);
1187 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1188 <6911uxc->len_name);
1189 if (ret) {
1190 dev_err(dev, "could not get module information!\n");
1191 return -EINVAL;
1192 }
1193
1194 lt6911uxc->power_gpio = devm_gpiod_get_optional(dev, "power",
1195 GPIOD_OUT_LOW);
1196 if (IS_ERR(lt6911uxc->power_gpio)) {
1197 dev_err(dev, "failed to get power gpio\n");
1198 ret = PTR_ERR(lt6911uxc->power_gpio);
1199 return ret;
1200 }
1201
1202 lt6911uxc->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1203 GPIOD_OUT_LOW);
1204 if (IS_ERR(lt6911uxc->reset_gpio)) {
1205 dev_err(dev, "failed to get reset gpio\n");
1206 ret = PTR_ERR(lt6911uxc->reset_gpio);
1207 return ret;
1208 }
1209
1210 lt6911uxc->plugin_det_gpio = devm_gpiod_get_optional(dev, "plugin-det",
1211 GPIOD_IN);
1212 if (IS_ERR(lt6911uxc->plugin_det_gpio)) {
1213 dev_err(dev, "failed to get plugin det gpio\n");
1214 ret = PTR_ERR(lt6911uxc->plugin_det_gpio);
1215 return ret;
1216 }
1217
1218 lt6911uxc->hpd_ctl_gpio = devm_gpiod_get_optional(dev, "hpd-ctl",
1219 GPIOD_OUT_HIGH);
1220 if (IS_ERR(lt6911uxc->hpd_ctl_gpio)) {
1221 dev_err(dev, "failed to get hpd ctl gpio\n");
1222 ret = PTR_ERR(lt6911uxc->hpd_ctl_gpio);
1223 return ret;
1224 }
1225
1226 ep = of_graph_get_next_endpoint(dev->of_node, NULL);
1227 if (!ep) {
1228 dev_err(dev, "missing endpoint node\n");
1229 ret = -EINVAL;
1230 return ret;
1231 }
1232
1233 ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep), &endpoint);
1234 if (ret) {
1235 dev_err(dev, "failed to parse endpoint\n");
1236 goto put_node;
1237 }
1238
1239 if (endpoint.bus_type != V4L2_MBUS_CSI2_DPHY ||
1240 endpoint.bus.mipi_csi2.num_data_lanes == 0) {
1241 dev_err(dev, "missing CSI-2 properties in endpoint\n");
1242 ret = -EINVAL;
1243 goto free_endpoint;
1244 }
1245
1246 lt6911uxc->xvclk = devm_clk_get(dev, "xvclk");
1247 if (IS_ERR(lt6911uxc->xvclk)) {
1248 dev_err(dev, "failed to get xvclk\n");
1249 ret = -EINVAL;
1250 goto free_endpoint;
1251 }
1252
1253 ret = clk_prepare_enable(lt6911uxc->xvclk);
1254 if (ret) {
1255 dev_err(dev, "Failed! to enable xvclk\n");
1256 goto free_endpoint;
1257 }
1258
1259 lt6911uxc->csi_lanes_in_use = endpoint.bus.mipi_csi2.num_data_lanes;
1260 lt6911uxc->bus = endpoint.bus.mipi_csi2;
1261 lt6911uxc->enable_hdcp = false;
1262
1263 gpiod_set_value(lt6911uxc->hpd_ctl_gpio, 0);
1264 gpiod_set_value(lt6911uxc->power_gpio, 1);
1265 lt6911uxc_reset(lt6911uxc);
1266
1267 ret = 0;
1268
1269 free_endpoint:
1270 v4l2_fwnode_endpoint_free(&endpoint);
1271 put_node:
1272 of_node_put(ep);
1273 return ret;
1274 }
1275 #else
lt6911uxc_parse_of(struct lt6911uxc * lt6911uxc)1276 static inline int lt6911uxc_parse_of(struct lt6911uxc *lt6911uxc)
1277 {
1278 return -ENODEV;
1279 }
1280 #endif
1281
lt6911uxc_probe(struct i2c_client * client,const struct i2c_device_id * id)1282 static int lt6911uxc_probe(struct i2c_client *client,
1283 const struct i2c_device_id *id)
1284 {
1285 struct v4l2_dv_timings default_timing =
1286 V4L2_DV_BT_CEA_640X480P59_94;
1287 struct lt6911uxc *lt6911uxc;
1288 struct v4l2_subdev *sd;
1289 struct device *dev = &client->dev;
1290 char facing[2];
1291 int err;
1292
1293 dev_info(dev, "driver version: %02x.%02x.%02x",
1294 DRIVER_VERSION >> 16,
1295 (DRIVER_VERSION & 0xff00) >> 8,
1296 DRIVER_VERSION & 0x00ff);
1297
1298 lt6911uxc = devm_kzalloc(dev, sizeof(struct lt6911uxc), GFP_KERNEL);
1299 if (!lt6911uxc)
1300 return -ENOMEM;
1301
1302 sd = <6911uxc->sd;
1303 lt6911uxc->i2c_client = client;
1304 lt6911uxc->timings = default_timing;
1305 lt6911uxc->cur_mode = &supported_modes[0];
1306 lt6911uxc->mbus_fmt_code = LT6911UXC_MEDIA_BUS_FMT;
1307
1308 err = lt6911uxc_parse_of(lt6911uxc);
1309 if (err) {
1310 v4l2_err(sd, "lt6911uxc_parse_of failed! err:%d\n", err);
1311 return err;
1312 }
1313
1314 err = lt6911uxc_check_chip_id(lt6911uxc);
1315 if (err < 0)
1316 return err;
1317
1318 /* after the CPU actively accesses the lt6911uxc through I2C,
1319 * a reset operation is required.
1320 */
1321 lt6911uxc_reset(lt6911uxc);
1322
1323 mutex_init(<6911uxc->confctl_mutex);
1324 err = lt6911uxc_init_v4l2_ctrls(lt6911uxc);
1325 if (err)
1326 goto err_free_hdl;
1327
1328 client->flags |= I2C_CLIENT_SCCB;
1329 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1330 v4l2_i2c_subdev_init(sd, client, <6911uxc_ops);
1331 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1332 #endif
1333
1334 #if defined(CONFIG_MEDIA_CONTROLLER)
1335 lt6911uxc->pad.flags = MEDIA_PAD_FL_SOURCE;
1336 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1337 err = media_entity_pads_init(&sd->entity, 1, <6911uxc->pad);
1338 if (err < 0) {
1339 v4l2_err(sd, "media entity init failed! err: %d\n", err);
1340 goto err_free_hdl;
1341 }
1342 #endif
1343 memset(facing, 0, sizeof(facing));
1344 if (strcmp(lt6911uxc->module_facing, "back") == 0)
1345 facing[0] = 'b';
1346 else
1347 facing[0] = 'f';
1348
1349 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1350 lt6911uxc->module_index, facing,
1351 LT6911UXC_NAME, dev_name(sd->dev));
1352 err = v4l2_async_register_subdev_sensor_common(sd);
1353 if (err < 0) {
1354 v4l2_err(sd, "v4l2 register subdev failed! err:%d\n", err);
1355 goto err_clean_entity;
1356 }
1357
1358 INIT_DELAYED_WORK(<6911uxc->delayed_work_enable_hotplug,
1359 lt6911uxc_delayed_work_enable_hotplug);
1360 INIT_DELAYED_WORK(<6911uxc->delayed_work_res_change,
1361 lt6911uxc_delayed_work_res_change);
1362
1363 if (lt6911uxc->i2c_client->irq) {
1364 v4l2_dbg(1, debug, sd, "cfg lt6911uxc irq!\n");
1365 err = devm_request_threaded_irq(dev,
1366 lt6911uxc->i2c_client->irq,
1367 NULL, lt6911uxc_res_change_irq_handler,
1368 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1369 "lt6911uxc", lt6911uxc);
1370 if (err) {
1371 v4l2_err(sd, "request irq failed! err:%d\n", err);
1372 goto err_work_queues;
1373 }
1374 } else {
1375 err = -EINVAL;
1376 v4l2_err(sd, "no irq cfg failed!\n");
1377 goto err_work_queues;
1378 }
1379
1380 lt6911uxc->plugin_irq = gpiod_to_irq(lt6911uxc->plugin_det_gpio);
1381 if (lt6911uxc->plugin_irq < 0)
1382 dev_err(dev, "failed to get plugin det irq, maybe no use\n");
1383
1384 err = devm_request_threaded_irq(dev, lt6911uxc->plugin_irq, NULL,
1385 plugin_detect_irq_handler, IRQF_TRIGGER_FALLING |
1386 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "lt6911uxc",
1387 lt6911uxc);
1388 if (err)
1389 dev_err(dev, "failed to register plugin det irq (%d), maybe no use\n", err);
1390
1391 err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
1392 if (err) {
1393 v4l2_err(sd, "v4l2 ctrl handler setup failed! err:%d\n", err);
1394 goto err_work_queues;
1395 }
1396
1397 lt6911uxc_config_hpd(sd);
1398 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1399 client->addr << 1, client->adapter->name);
1400
1401 return 0;
1402
1403 err_work_queues:
1404 cancel_delayed_work(<6911uxc->delayed_work_enable_hotplug);
1405 cancel_delayed_work(<6911uxc->delayed_work_res_change);
1406 err_clean_entity:
1407 #if defined(CONFIG_MEDIA_CONTROLLER)
1408 media_entity_cleanup(&sd->entity);
1409 #endif
1410 err_free_hdl:
1411 v4l2_ctrl_handler_free(<6911uxc->hdl);
1412 mutex_destroy(<6911uxc->confctl_mutex);
1413 return err;
1414 }
1415
lt6911uxc_remove(struct i2c_client * client)1416 static int lt6911uxc_remove(struct i2c_client *client)
1417 {
1418 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1419 struct lt6911uxc *lt6911uxc = to_state(sd);
1420
1421 cancel_delayed_work_sync(<6911uxc->delayed_work_enable_hotplug);
1422 cancel_delayed_work_sync(<6911uxc->delayed_work_res_change);
1423 v4l2_async_unregister_subdev(sd);
1424 v4l2_device_unregister_subdev(sd);
1425 #if defined(CONFIG_MEDIA_CONTROLLER)
1426 media_entity_cleanup(&sd->entity);
1427 #endif
1428 v4l2_ctrl_handler_free(<6911uxc->hdl);
1429 mutex_destroy(<6911uxc->confctl_mutex);
1430 clk_disable_unprepare(lt6911uxc->xvclk);
1431
1432 return 0;
1433 }
1434
1435 #if IS_ENABLED(CONFIG_OF)
1436 static const struct of_device_id lt6911uxc_of_match[] = {
1437 { .compatible = "lontium,lt6911uxc" },
1438 {},
1439 };
1440 MODULE_DEVICE_TABLE(of, lt6911uxc_of_match);
1441 #endif
1442
1443 static struct i2c_driver lt6911uxc_driver = {
1444 .driver = {
1445 .name = LT6911UXC_NAME,
1446 .of_match_table = of_match_ptr(lt6911uxc_of_match),
1447 },
1448 .probe = lt6911uxc_probe,
1449 .remove = lt6911uxc_remove,
1450 };
1451
lt6911uxc_driver_init(void)1452 static int __init lt6911uxc_driver_init(void)
1453 {
1454 return i2c_add_driver(<6911uxc_driver);
1455 }
1456
lt6911uxc_driver_exit(void)1457 static void __exit lt6911uxc_driver_exit(void)
1458 {
1459 i2c_del_driver(<6911uxc_driver);
1460 }
1461
1462 late_initcall(lt6911uxc_driver_init);
1463 module_exit(lt6911uxc_driver_exit);
1464
1465 MODULE_DESCRIPTION("Lontium LT6911UXC HDMI to MIPI CSI-2 bridge driver");
1466 MODULE_AUTHOR("Dingxian Wen <shawn.wen@rock-chips.com>");
1467 MODULE_LICENSE("GPL v2");
1468