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