xref: /OK3568_Linux_fs/kernel/drivers/media/platform/rockchip/cif/mipi-csi2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip MIPI CSI2 Driver
4  *
5  * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/iopoll.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/reset.h>
19 #include <linux/rk-camera-module.h>
20 #include <media/v4l2-ioctl.h>
21 #include "mipi-csi2.h"
22 #include <linux/regulator/consumer.h>
23 
24 static int csi2_debug;
25 module_param_named(debug_csi2, csi2_debug, int, 0644);
26 MODULE_PARM_DESC(debug_csi2, "Debug level (0-1)");
27 
28 #define write_csihost_reg(base, addr, val)  writel(val, (addr) + (base))
29 #define read_csihost_reg(base, addr) readl((addr) + (base))
30 
31 static ATOMIC_NOTIFIER_HEAD(g_csi_host_chain);
32 
rkcif_csi2_register_notifier(struct notifier_block * nb)33 int rkcif_csi2_register_notifier(struct notifier_block *nb)
34 {
35 	return atomic_notifier_chain_register(&g_csi_host_chain, nb);
36 }
37 
rkcif_csi2_unregister_notifier(struct notifier_block * nb)38 int rkcif_csi2_unregister_notifier(struct notifier_block *nb)
39 {
40 	return atomic_notifier_chain_unregister(&g_csi_host_chain, nb);
41 }
42 
sd_to_dev(struct v4l2_subdev * sdev)43 static inline struct csi2_dev *sd_to_dev(struct v4l2_subdev *sdev)
44 {
45 	return container_of(sdev, struct csi2_dev, sd);
46 }
47 
sd_to_sensor(struct csi2_dev * csi2,struct v4l2_subdev * sd)48 static struct csi2_sensor_info *sd_to_sensor(struct csi2_dev *csi2,
49 					struct v4l2_subdev *sd)
50 {
51 	int i;
52 
53 	for (i = 0; i < csi2->num_sensors; ++i)
54 		if (csi2->sensors[i].sd == sd)
55 			return &csi2->sensors[i];
56 
57 	return NULL;
58 }
59 
get_remote_sensor(struct v4l2_subdev * sd)60 static struct v4l2_subdev *get_remote_sensor(struct v4l2_subdev *sd)
61 {
62 	struct media_pad *local, *remote;
63 	struct media_entity *sensor_me;
64 
65 	local = &sd->entity.pads[RK_CSI2_PAD_SINK];
66 	remote = media_entity_remote_pad(local);
67 	if (!remote) {
68 		v4l2_warn(sd, "No link between dphy and sensor\n");
69 		return NULL;
70 	}
71 
72 	sensor_me = media_entity_remote_pad(local)->entity;
73 	return media_entity_to_v4l2_subdev(sensor_me);
74 }
75 
get_remote_terminal_sensor(struct v4l2_subdev * sd,struct v4l2_subdev ** sensor_sd)76 static void get_remote_terminal_sensor(struct v4l2_subdev *sd,
77 				       struct v4l2_subdev **sensor_sd)
78 {
79 	struct media_graph graph;
80 	struct media_entity *entity = &sd->entity;
81 	struct media_device *mdev = entity->graph_obj.mdev;
82 	int ret;
83 
84 	/* Walk the graph to locate sensor nodes. */
85 	mutex_lock(&mdev->graph_mutex);
86 	ret = media_graph_walk_init(&graph, mdev);
87 	if (ret) {
88 		mutex_unlock(&mdev->graph_mutex);
89 		*sensor_sd = NULL;
90 		return;
91 	}
92 
93 	media_graph_walk_start(&graph, entity);
94 	while ((entity = media_graph_walk_next(&graph))) {
95 		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
96 			break;
97 	}
98 	mutex_unlock(&mdev->graph_mutex);
99 	media_graph_walk_cleanup(&graph);
100 
101 	if (entity)
102 		*sensor_sd = media_entity_to_v4l2_subdev(entity);
103 	else
104 		*sensor_sd = NULL;
105 }
106 
csi2_update_sensor_info(struct csi2_dev * csi2)107 static void csi2_update_sensor_info(struct csi2_dev *csi2)
108 {
109 	struct v4l2_subdev *terminal_sensor_sd = NULL;
110 	struct csi2_sensor_info *sensor = &csi2->sensors[0];
111 	struct v4l2_mbus_config mbus;
112 	int ret = 0;
113 
114 	ret = v4l2_subdev_call(sensor->sd, pad, get_mbus_config, 0, &mbus);
115 	if (ret) {
116 		v4l2_err(&csi2->sd, "update sensor info failed!\n");
117 		return;
118 	}
119 
120 	get_remote_terminal_sensor(&csi2->sd, &terminal_sensor_sd);
121 	ret = v4l2_subdev_call(terminal_sensor_sd, core, ioctl,
122 				RKMODULE_GET_CSI_DSI_INFO, &csi2->dsi_input_en);
123 	if (ret) {
124 		v4l2_dbg(1, csi2_debug, &csi2->sd, "get CSI/DSI sel failed, default csi!\n");
125 		csi2->dsi_input_en = 0;
126 	}
127 
128 	csi2->bus.flags = mbus.flags;
129 	switch (csi2->bus.flags & V4L2_MBUS_CSI2_LANES) {
130 	case V4L2_MBUS_CSI2_1_LANE:
131 		csi2->bus.num_data_lanes = 1;
132 		break;
133 	case V4L2_MBUS_CSI2_2_LANE:
134 		csi2->bus.num_data_lanes = 2;
135 		break;
136 	case V4L2_MBUS_CSI2_3_LANE:
137 		csi2->bus.num_data_lanes = 3;
138 		break;
139 	case V4L2_MBUS_CSI2_4_LANE:
140 		csi2->bus.num_data_lanes = 4;
141 		break;
142 	default:
143 		v4l2_warn(&csi2->sd, "lane num is invalid\n");
144 		csi2->bus.num_data_lanes = 0;
145 		break;
146 	}
147 
148 }
149 
csi2_hw_do_reset(struct csi2_hw * csi2_hw)150 static void csi2_hw_do_reset(struct csi2_hw *csi2_hw)
151 {
152 
153 	if (!csi2_hw->rsts_bulk)
154 		return;
155 
156 	reset_control_assert(csi2_hw->rsts_bulk);
157 
158 	udelay(5);
159 
160 	reset_control_deassert(csi2_hw->rsts_bulk);
161 }
162 
csi2_enable_clks(struct csi2_hw * csi2_hw)163 static int csi2_enable_clks(struct csi2_hw *csi2_hw)
164 {
165 	int ret = 0;
166 
167 	if (!csi2_hw->clks_bulk)
168 		return -EINVAL;
169 
170 	ret = clk_bulk_prepare_enable(csi2_hw->clks_num, csi2_hw->clks_bulk);
171 	if (ret)
172 		dev_err(csi2_hw->dev, "failed to enable clks\n");
173 
174 	return ret;
175 }
176 
csi2_disable_clks(struct csi2_hw * csi2_hw)177 static void csi2_disable_clks(struct csi2_hw *csi2_hw)
178 {
179 	if (!csi2_hw->clks_bulk)
180 		return;
181 	clk_bulk_disable_unprepare(csi2_hw->clks_num,  csi2_hw->clks_bulk);
182 }
183 
csi2_disable(struct csi2_hw * csi2_hw)184 static void csi2_disable(struct csi2_hw *csi2_hw)
185 {
186 	write_csihost_reg(csi2_hw->base, CSIHOST_RESETN, 0);
187 	write_csihost_reg(csi2_hw->base, CSIHOST_MSK1, 0xffffffff);
188 	write_csihost_reg(csi2_hw->base, CSIHOST_MSK2, 0xffffffff);
189 }
190 
191 static int csi2_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
192 			      struct v4l2_mbus_config *mbus);
193 
csi2_enable(struct csi2_hw * csi2_hw,enum host_type_t host_type)194 static void csi2_enable(struct csi2_hw *csi2_hw,
195 			enum host_type_t host_type)
196 {
197 	void __iomem *base = csi2_hw->base;
198 	struct csi2_dev *csi2 = csi2_hw->csi2;
199 	int lanes = csi2->bus.num_data_lanes;
200 	struct v4l2_mbus_config mbus;
201 	u32 val = 0;
202 
203 	csi2_g_mbus_config(&csi2->sd, 0, &mbus);
204 	if (mbus.type == V4L2_MBUS_CSI2_DPHY)
205 		val = SW_CPHY_EN(0);
206 	else if (mbus.type == V4L2_MBUS_CSI2_CPHY)
207 		val = SW_CPHY_EN(1);
208 
209 	write_csihost_reg(base, CSIHOST_N_LANES, lanes - 1);
210 
211 	if (host_type == RK_DSI_RXHOST) {
212 		val |= SW_DSI_EN(1) | SW_DATATYPE_FS(0x01) |
213 		       SW_DATATYPE_FE(0x11) | SW_DATATYPE_LS(0x21) |
214 		       SW_DATATYPE_LE(0x31);
215 		write_csihost_reg(base, CSIHOST_CONTROL, val);
216 		/* Disable some error interrupt when HOST work on DSI RX mode */
217 		write_csihost_reg(base, CSIHOST_MSK1, 0xe00000f0);
218 		write_csihost_reg(base, CSIHOST_MSK2, 0xff00);
219 	} else {
220 		val |= SW_DSI_EN(0) | SW_DATATYPE_FS(0x0) |
221 		       SW_DATATYPE_FE(0x01) | SW_DATATYPE_LS(0x02) |
222 		       SW_DATATYPE_LE(0x03);
223 		write_csihost_reg(base, CSIHOST_CONTROL, val);
224 		write_csihost_reg(base, CSIHOST_MSK1, 0x0);
225 		write_csihost_reg(base, CSIHOST_MSK2, 0xf000);
226 		csi2->is_check_sot_sync = true;
227 	}
228 
229 	write_csihost_reg(base, CSIHOST_RESETN, 1);
230 }
231 
csi2_start(struct csi2_dev * csi2)232 static int csi2_start(struct csi2_dev *csi2)
233 {
234 	enum host_type_t host_type;
235 	int ret, i;
236 	int csi_idx = 0;
237 
238 	atomic_set(&csi2->frm_sync_seq, 0);
239 
240 	csi2_update_sensor_info(csi2);
241 
242 	if (csi2->dsi_input_en == RKMODULE_DSI_INPUT)
243 		host_type = RK_DSI_RXHOST;
244 	else
245 		host_type = RK_CSI_RXHOST;
246 
247 	for (i = 0; i < csi2->csi_info.csi_num; i++) {
248 		csi_idx = csi2->csi_info.csi_idx[i];
249 		csi2_hw_do_reset(csi2->csi2_hw[csi_idx]);
250 		ret = csi2_enable_clks(csi2->csi2_hw[csi_idx]);
251 		if (ret) {
252 			v4l2_err(&csi2->sd, "%s: enable clks failed\n", __func__);
253 			return ret;
254 		}
255 		csi2_enable(csi2->csi2_hw[csi_idx], host_type);
256 	}
257 
258 	pr_debug("stream sd: %s\n", csi2->src_sd->name);
259 	ret = v4l2_subdev_call(csi2->src_sd, video, s_stream, 1);
260 	ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
261 	if (ret)
262 		goto err_assert_reset;
263 
264 	for (i = 0; i < RK_CSI2_ERR_MAX; i++)
265 		csi2->err_list[i].cnt = 0;
266 
267 	return 0;
268 
269 err_assert_reset:
270 	for (i = 0; i < csi2->csi_info.csi_num; i++) {
271 		csi_idx = csi2->csi_info.csi_idx[i];
272 		csi2_disable(csi2->csi2_hw[csi_idx]);
273 		csi2_disable_clks(csi2->csi2_hw[csi_idx]);
274 	}
275 
276 	return ret;
277 }
278 
csi2_stop(struct csi2_dev * csi2)279 static void csi2_stop(struct csi2_dev *csi2)
280 {
281 	int i = 0;
282 	int csi_idx = 0;
283 
284 	/* stop upstream */
285 	v4l2_subdev_call(csi2->src_sd, video, s_stream, 0);
286 
287 	for (i = 0; i < csi2->csi_info.csi_num; i++) {
288 		csi_idx = csi2->csi_info.csi_idx[i];
289 		csi2_disable(csi2->csi2_hw[csi_idx]);
290 		csi2_hw_do_reset(csi2->csi2_hw[csi_idx]);
291 		csi2_disable_clks(csi2->csi2_hw[csi_idx]);
292 	}
293 }
294 
295 /*
296  * V4L2 subdev operations.
297  */
298 
csi2_s_stream(struct v4l2_subdev * sd,int enable)299 static int csi2_s_stream(struct v4l2_subdev *sd, int enable)
300 {
301 	struct csi2_dev *csi2 = sd_to_dev(sd);
302 	int ret = 0;
303 
304 	mutex_lock(&csi2->lock);
305 
306 	dev_err(csi2->dev, "stream %s, src_sd: %p, sd_name:%s\n",
307 		enable ? "on" : "off",
308 		csi2->src_sd, csi2->src_sd->name);
309 
310 	/*
311 	 * enable/disable streaming only if stream_count is
312 	 * going from 0 to 1 / 1 to 0.
313 	 */
314 	if (csi2->stream_count != !enable)
315 		goto update_count;
316 
317 	dev_err(csi2->dev, "stream %s\n", enable ? "ON" : "OFF");
318 
319 	if (enable)
320 		ret = csi2_start(csi2);
321 	else
322 		csi2_stop(csi2);
323 	if (ret)
324 		goto out;
325 
326 update_count:
327 	csi2->stream_count += enable ? 1 : -1;
328 	if (csi2->stream_count < 0)
329 		csi2->stream_count = 0;
330 out:
331 	mutex_unlock(&csi2->lock);
332 
333 	return ret;
334 }
335 
csi2_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)336 static int csi2_link_setup(struct media_entity *entity,
337 			   const struct media_pad *local,
338 			   const struct media_pad *remote, u32 flags)
339 {
340 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
341 	struct csi2_dev *csi2 = sd_to_dev(sd);
342 	struct v4l2_subdev *remote_sd;
343 	int ret = 0;
344 
345 	remote_sd = media_entity_to_v4l2_subdev(remote->entity);
346 
347 	mutex_lock(&csi2->lock);
348 
349 	if (local->flags & MEDIA_PAD_FL_SOURCE) {
350 		if (flags & MEDIA_LNK_FL_ENABLED) {
351 			if (csi2->sink_linked[local->index - 1]) {
352 				ret = -EBUSY;
353 				goto out;
354 			}
355 			csi2->sink_linked[local->index - 1] = true;
356 		} else {
357 			csi2->sink_linked[local->index - 1] = false;
358 		}
359 	} else {
360 		if (flags & MEDIA_LNK_FL_ENABLED) {
361 			if (csi2->src_sd) {
362 				ret = -EBUSY;
363 				goto out;
364 			}
365 			csi2->src_sd = remote_sd;
366 		} else {
367 			csi2->src_sd = NULL;
368 		}
369 	}
370 
371 out:
372 	mutex_unlock(&csi2->lock);
373 	return ret;
374 }
375 
csi2_media_init(struct v4l2_subdev * sd)376 static int csi2_media_init(struct v4l2_subdev *sd)
377 {
378 	struct csi2_dev *csi2 = sd_to_dev(sd);
379 	int i = 0, num_pads = 0;
380 
381 	num_pads = csi2->match_data->num_pads;
382 
383 	for (i = 0; i < num_pads; i++) {
384 		csi2->pad[i].flags = (i == CSI2_SINK_PAD) ?
385 		MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
386 	}
387 
388 	csi2->pad[RK_CSI2X_PAD_SOURCE0].flags =
389 		MEDIA_PAD_FL_SOURCE | MEDIA_PAD_FL_MUST_CONNECT;
390 	csi2->pad[RK_CSI2_PAD_SINK].flags =
391 		MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
392 
393 	/* set a default mbus format  */
394 	csi2->format_mbus.code =  MEDIA_BUS_FMT_UYVY8_2X8;
395 	csi2->format_mbus.field = V4L2_FIELD_NONE;
396 	csi2->format_mbus.width = RKCIF_DEFAULT_WIDTH;
397 	csi2->format_mbus.height = RKCIF_DEFAULT_HEIGHT;
398 	csi2->crop.top = 0;
399 	csi2->crop.left = 0;
400 	csi2->crop.width = RKCIF_DEFAULT_WIDTH;
401 	csi2->crop.height = RKCIF_DEFAULT_HEIGHT;
402 
403 	return media_entity_pads_init(&sd->entity, num_pads, csi2->pad);
404 }
405 
406 /* csi2 accepts all fmt/size from sensor */
csi2_get_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)407 static int csi2_get_set_fmt(struct v4l2_subdev *sd,
408 			    struct v4l2_subdev_pad_config *cfg,
409 			    struct v4l2_subdev_format *fmt)
410 {
411 	int ret;
412 	struct csi2_dev *csi2 = sd_to_dev(sd);
413 	struct v4l2_subdev *sensor = get_remote_sensor(sd);
414 
415 	/*
416 	 * Do not allow format changes and just relay whatever
417 	 * set currently in the sensor.
418 	 */
419 	ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, fmt);
420 	if (!ret)
421 		csi2->format_mbus = fmt->format;
422 
423 	return ret;
424 }
425 
mipi_csi2_get_crop(struct csi2_dev * csi2,struct v4l2_subdev_pad_config * cfg,enum v4l2_subdev_format_whence which)426 static struct v4l2_rect *mipi_csi2_get_crop(struct csi2_dev *csi2,
427 						 struct v4l2_subdev_pad_config *cfg,
428 						 enum v4l2_subdev_format_whence which)
429 {
430 	if (which == V4L2_SUBDEV_FORMAT_TRY)
431 		return v4l2_subdev_get_try_crop(&csi2->sd, cfg, RK_CSI2_PAD_SINK);
432 	else
433 		return &csi2->crop;
434 }
435 
csi2_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)436 static int csi2_get_selection(struct v4l2_subdev *sd,
437 				   struct v4l2_subdev_pad_config *cfg,
438 				   struct v4l2_subdev_selection *sel)
439 {
440 	struct csi2_dev *csi2 = sd_to_dev(sd);
441 	struct v4l2_subdev *sensor = get_remote_sensor(sd);
442 	struct v4l2_subdev_format fmt;
443 	int ret = 0;
444 
445 	if (!sel) {
446 		v4l2_dbg(1, csi2_debug, &csi2->sd, "sel is null\n");
447 		goto err;
448 	}
449 
450 	if (sel->pad > RK_CSI2X_PAD_SOURCE3) {
451 		v4l2_dbg(1, csi2_debug, &csi2->sd, "pad[%d] isn't matched\n", sel->pad);
452 		goto err;
453 	}
454 
455 	switch (sel->target) {
456 	case V4L2_SEL_TGT_CROP_BOUNDS:
457 		if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
458 			sel->pad = 0;
459 			ret = v4l2_subdev_call(sensor, pad, get_selection,
460 					       cfg, sel);
461 			if (ret) {
462 				fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
463 				fmt.pad = 0;
464 				ret = v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt);
465 				if (!ret) {
466 					csi2->format_mbus = fmt.format;
467 					sel->r.top = 0;
468 					sel->r.left = 0;
469 					sel->r.width = csi2->format_mbus.width;
470 					sel->r.height = csi2->format_mbus.height;
471 					csi2->crop = sel->r;
472 				} else {
473 					sel->r = csi2->crop;
474 				}
475 			} else {
476 				csi2->crop = sel->r;
477 			}
478 		} else {
479 			sel->r = *v4l2_subdev_get_try_crop(&csi2->sd, cfg, sel->pad);
480 		}
481 		break;
482 
483 	case V4L2_SEL_TGT_CROP:
484 		sel->r = *mipi_csi2_get_crop(csi2, cfg, sel->which);
485 		break;
486 
487 	default:
488 		return -EINVAL;
489 	}
490 
491 	return 0;
492 err:
493 	return -EINVAL;
494 }
495 
csi2_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)496 static int csi2_set_selection(struct v4l2_subdev *sd,
497 				   struct v4l2_subdev_pad_config *cfg,
498 				   struct v4l2_subdev_selection *sel)
499 {
500 	struct csi2_dev *csi2 = sd_to_dev(sd);
501 	struct v4l2_subdev *sensor = get_remote_sensor(sd);
502 	int ret = 0;
503 
504 	ret = v4l2_subdev_call(sensor, pad, set_selection,
505 			       cfg, sel);
506 	if (!ret)
507 		csi2->crop = sel->r;
508 
509 	return ret;
510 }
511 
csi2_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * mbus)512 static int csi2_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
513 			      struct v4l2_mbus_config *mbus)
514 {
515 	struct csi2_dev *csi2 = sd_to_dev(sd);
516 	struct v4l2_subdev *sensor_sd = get_remote_sensor(sd);
517 	int ret;
518 
519 	ret = v4l2_subdev_call(sensor_sd, pad, get_mbus_config, 0, mbus);
520 	if (ret) {
521 		mbus->type = V4L2_MBUS_CSI2_DPHY;
522 		mbus->flags = csi2->bus.flags;
523 		mbus->flags |= BIT(csi2->bus.num_data_lanes - 1);
524 	}
525 
526 	return 0;
527 }
528 
529 static const struct media_entity_operations csi2_entity_ops = {
530 	.link_setup = csi2_link_setup,
531 	.link_validate = v4l2_subdev_link_validate,
532 };
533 
rkcif_csi2_event_reset_pipe(struct csi2_dev * csi2_dev,int reset_src)534 void rkcif_csi2_event_reset_pipe(struct csi2_dev *csi2_dev, int reset_src)
535 {
536 	if (csi2_dev) {
537 		struct v4l2_event event = {
538 			.type = V4L2_EVENT_RESET_DEV,
539 			.reserved[0] = reset_src,
540 		};
541 		v4l2_event_queue(csi2_dev->sd.devnode, &event);
542 	}
543 }
544 
rkcif_csi2_event_inc_sof(struct csi2_dev * csi2_dev)545 void rkcif_csi2_event_inc_sof(struct csi2_dev *csi2_dev)
546 {
547 	if (csi2_dev) {
548 		struct v4l2_event event = {
549 			.type = V4L2_EVENT_FRAME_SYNC,
550 			.u.frame_sync.frame_sequence =
551 				atomic_inc_return(&csi2_dev->frm_sync_seq) - 1,
552 		};
553 		v4l2_event_queue(csi2_dev->sd.devnode, &event);
554 	}
555 }
556 
rkcif_csi2_get_sof(struct csi2_dev * csi2_dev)557 u32 rkcif_csi2_get_sof(struct csi2_dev *csi2_dev)
558 {
559 	if (csi2_dev)
560 		return atomic_read(&csi2_dev->frm_sync_seq) - 1;
561 
562 	return 0;
563 }
564 
rkcif_csi2_set_sof(struct csi2_dev * csi2_dev,u32 seq)565 void rkcif_csi2_set_sof(struct csi2_dev *csi2_dev, u32 seq)
566 {
567 	if (csi2_dev)
568 		atomic_set(&csi2_dev->frm_sync_seq, seq);
569 }
570 
rkcif_csi2_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)571 static int rkcif_csi2_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
572 					     struct v4l2_event_subscription *sub)
573 {
574 	if (sub->type == V4L2_EVENT_FRAME_SYNC ||
575 	    sub->type == V4L2_EVENT_RESET_DEV)
576 		return v4l2_event_subscribe(fh, sub, RKCIF_V4L2_EVENT_ELEMS, NULL);
577 	else
578 		return -EINVAL;
579 }
580 
rkcif_csi2_s_power(struct v4l2_subdev * sd,int on)581 static int rkcif_csi2_s_power(struct v4l2_subdev *sd, int on)
582 {
583 	return 0;
584 }
585 
rkcif_csi2_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)586 static long rkcif_csi2_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
587 {
588 	struct csi2_dev *csi2 = sd_to_dev(sd);
589 	struct v4l2_subdev *sensor = get_remote_sensor(sd);
590 	long ret = 0;
591 	int i = 0;
592 
593 	switch (cmd) {
594 	case RKCIF_CMD_SET_CSI_IDX:
595 		csi2->csi_info = *((struct rkcif_csi_info *)arg);
596 		for (i = 0; i < csi2->csi_info.csi_num; i++)
597 			csi2->csi2_hw[csi2->csi_info.csi_idx[i]]->csi2 = csi2;
598 		if (csi2->match_data->chip_id > CHIP_RV1126_CSI2)
599 			ret = v4l2_subdev_call(sensor, core, ioctl,
600 					       RKCIF_CMD_SET_CSI_IDX,
601 					       arg);
602 		break;
603 	default:
604 		ret = -ENOIOCTLCMD;
605 		break;
606 	}
607 
608 	return ret;
609 }
610 
611 #ifdef CONFIG_COMPAT
rkcif_csi2_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)612 static long rkcif_csi2_compat_ioctl32(struct v4l2_subdev *sd,
613 				      unsigned int cmd, unsigned long arg)
614 {
615 	void __user *up = compat_ptr(arg);
616 	struct rkcif_csi_info csi_info;
617 	long ret;
618 
619 	switch (cmd) {
620 	case RKCIF_CMD_SET_CSI_IDX:
621 		if (copy_from_user(&csi_info, up, sizeof(struct rkcif_csi_info)))
622 			return -EFAULT;
623 
624 		ret = rkcif_csi2_ioctl(sd, cmd, &csi_info);
625 		break;
626 	default:
627 		ret = -ENOIOCTLCMD;
628 		break;
629 	}
630 
631 	return ret;
632 }
633 #endif
634 
635 static const struct v4l2_subdev_core_ops csi2_core_ops = {
636 	.subscribe_event = rkcif_csi2_subscribe_event,
637 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
638 	.s_power = rkcif_csi2_s_power,
639 	.ioctl = rkcif_csi2_ioctl,
640 #ifdef CONFIG_COMPAT
641 	.compat_ioctl32 = rkcif_csi2_compat_ioctl32,
642 #endif
643 };
644 
645 static const struct v4l2_subdev_video_ops csi2_video_ops = {
646 	.s_stream = csi2_s_stream,
647 };
648 
649 static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
650 	.get_fmt = csi2_get_set_fmt,
651 	.set_fmt = csi2_get_set_fmt,
652 	.get_selection = csi2_get_selection,
653 	.set_selection = csi2_set_selection,
654 	.get_mbus_config = csi2_g_mbus_config,
655 };
656 
657 static const struct v4l2_subdev_ops csi2_subdev_ops = {
658 	.core = &csi2_core_ops,
659 	.video = &csi2_video_ops,
660 	.pad = &csi2_pad_ops,
661 };
662 
csi2_parse_endpoint(struct device * dev,struct v4l2_fwnode_endpoint * vep,struct v4l2_async_subdev * asd)663 static int csi2_parse_endpoint(struct device *dev,
664 			       struct v4l2_fwnode_endpoint *vep,
665 			       struct v4l2_async_subdev *asd)
666 {
667 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
668 	struct csi2_dev *csi2 = sd_to_dev(sd);
669 
670 	if (vep->base.port != 0) {
671 		dev_err(dev, "The csi host node needs to parse port 0\n");
672 		return -EINVAL;
673 	}
674 
675 	csi2->bus = vep->bus.mipi_csi2;
676 
677 	return 0;
678 }
679 
680 /* The .bound() notifier callback when a match is found */
681 static int
csi2_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)682 csi2_notifier_bound(struct v4l2_async_notifier *notifier,
683 		    struct v4l2_subdev *sd,
684 		    struct v4l2_async_subdev *asd)
685 {
686 	struct csi2_dev *csi2 = container_of(notifier,
687 			struct csi2_dev,
688 			notifier);
689 	struct csi2_sensor_info *sensor;
690 	struct media_link *link;
691 	unsigned int pad, ret;
692 
693 	if (csi2->num_sensors == ARRAY_SIZE(csi2->sensors)) {
694 		v4l2_err(&csi2->sd,
695 			 "%s: the num of sd is beyond:%d\n",
696 			 __func__, csi2->num_sensors);
697 		return -EBUSY;
698 	}
699 	sensor = &csi2->sensors[csi2->num_sensors++];
700 	sensor->sd = sd;
701 
702 	for (pad = 0; pad < sd->entity.num_pads; pad++)
703 		if (sensor->sd->entity.pads[pad].flags
704 					& MEDIA_PAD_FL_SOURCE)
705 			break;
706 
707 	if (pad == sensor->sd->entity.num_pads) {
708 		dev_err(csi2->dev,
709 			"failed to find src pad for %s\n",
710 			sd->name);
711 
712 		return -ENXIO;
713 	}
714 
715 	ret = media_create_pad_link(&sensor->sd->entity, pad,
716 				    &csi2->sd.entity, RK_CSI2_PAD_SINK,
717 				    0/* csi2->num_sensors != 1 ? 0 : MEDIA_LNK_FL_ENABLED */);
718 	if (ret) {
719 		dev_err(csi2->dev,
720 			"failed to create link for %s\n",
721 			sd->name);
722 		return ret;
723 	}
724 
725 	link = list_first_entry(&csi2->sd.entity.links, struct media_link, list);
726 	ret = media_entity_setup_link(link, MEDIA_LNK_FL_ENABLED);
727 	if (ret) {
728 		dev_err(csi2->dev,
729 			"failed to create link for %s\n",
730 			sensor->sd->name);
731 		return ret;
732 	}
733 
734 	return 0;
735 }
736 
737 /* The .unbind callback */
csi2_notifier_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * sd,struct v4l2_async_subdev * asd)738 static void csi2_notifier_unbind(struct v4l2_async_notifier *notifier,
739 				 struct v4l2_subdev *sd,
740 				 struct v4l2_async_subdev *asd)
741 {
742 	struct csi2_dev *csi2 = container_of(notifier,
743 						  struct csi2_dev,
744 						  notifier);
745 	struct csi2_sensor_info *sensor = sd_to_sensor(csi2, sd);
746 
747 	if (sensor)
748 		sensor->sd = NULL;
749 }
750 
751 static const struct
752 v4l2_async_notifier_operations csi2_async_ops = {
753 	.bound = csi2_notifier_bound,
754 	.unbind = csi2_notifier_unbind,
755 };
756 
csi2_find_err_vc(int val,char * vc_info)757 static void csi2_find_err_vc(int val, char *vc_info)
758 {
759 	int i;
760 	char cur_str[CSI_VCINFO_LEN] = {0};
761 
762 	memset(vc_info, 0, sizeof(*vc_info));
763 	for (i = 0; i < 4; i++) {
764 		if ((val >> i) & 0x1) {
765 			snprintf(cur_str, CSI_VCINFO_LEN, " %d", i);
766 			if (strlen(vc_info) + strlen(cur_str) < CSI_VCINFO_LEN)
767 				strncat(vc_info, cur_str, strlen(cur_str));
768 		}
769 	}
770 }
771 
772 #define csi2_err_strncat(dst_str, src_str) {\
773 	if (strlen(dst_str) + strlen(src_str) < CSI_ERRSTR_LEN)\
774 		strncat(dst_str, src_str, strlen(src_str)); }
775 
rk_csirx_irq1_handler(int irq,void * ctx)776 static irqreturn_t rk_csirx_irq1_handler(int irq, void *ctx)
777 {
778 	struct device *dev = ctx;
779 	struct csi2_hw *csi2_hw = dev_get_drvdata(dev);
780 	struct csi2_dev *csi2 = csi2_hw->csi2;
781 	struct csi2_err_stats *err_list = NULL;
782 	unsigned long err_stat = 0;
783 	u32 val;
784 	char err_str[CSI_ERRSTR_LEN] = {0};
785 	char cur_str[CSI_ERRSTR_LEN] = {0};
786 	char vc_info[CSI_VCINFO_LEN] = {0};
787 	bool is_add_cnt = false;
788 
789 	val = read_csihost_reg(csi2_hw->base, CSIHOST_ERR1);
790 	if (val) {
791 		if (val & CSIHOST_ERR1_PHYERR_SPTSYNCHS) {
792 			err_list = &csi2->err_list[RK_CSI2_ERR_SOTSYN];
793 			err_list->cnt++;
794 			if (csi2->match_data->chip_id == CHIP_RK3588_CSI2) {
795 				if (err_list->cnt > 3 &&
796 				    csi2->err_list[RK_CSI2_ERR_ALL].cnt <= err_list->cnt) {
797 					csi2->is_check_sot_sync = false;
798 					write_csihost_reg(csi2_hw->base, CSIHOST_MSK1, 0xf);
799 				}
800 				if (csi2->is_check_sot_sync) {
801 					csi2_find_err_vc(val & 0xf, vc_info);
802 					snprintf(cur_str, CSI_ERRSTR_LEN, "(sot sync,lane:%s) ", vc_info);
803 					csi2_err_strncat(err_str, cur_str);
804 				}
805 			} else {
806 				csi2_find_err_vc(val & 0xf, vc_info);
807 				snprintf(cur_str, CSI_ERRSTR_LEN, "(sot sync,lane:%s) ", vc_info);
808 				csi2_err_strncat(err_str, cur_str);
809 				is_add_cnt = true;
810 			}
811 		}
812 
813 		if (val & CSIHOST_ERR1_ERR_BNDRY_MATCH) {
814 			err_list = &csi2->err_list[RK_CSI2_ERR_FS_FE_MIS];
815 			err_list->cnt++;
816 			csi2_find_err_vc((val >> 4) & 0xf, vc_info);
817 			snprintf(cur_str, CSI_ERRSTR_LEN, "(fs/fe mis,vc:%s) ", vc_info);
818 			csi2_err_strncat(err_str, cur_str);
819 			if (csi2->match_data->chip_id < CHIP_RK3588_CSI2)
820 				is_add_cnt = true;
821 		}
822 
823 		if (val & CSIHOST_ERR1_ERR_SEQ) {
824 			err_list = &csi2->err_list[RK_CSI2_ERR_FRM_SEQ_ERR];
825 			err_list->cnt++;
826 			csi2_find_err_vc((val >> 8) & 0xf, vc_info);
827 			snprintf(cur_str, CSI_ERRSTR_LEN, "(f_seq,vc:%s) ", vc_info);
828 			csi2_err_strncat(err_str, cur_str);
829 		}
830 
831 		if (val & CSIHOST_ERR1_ERR_FRM_DATA) {
832 			err_list = &csi2->err_list[RK_CSI2_ERR_CRC_ONCE];
833 			is_add_cnt = true;
834 			err_list->cnt++;
835 			csi2_find_err_vc((val >> 12) & 0xf, vc_info);
836 			snprintf(cur_str, CSI_ERRSTR_LEN, "(err_data,vc:%s) ", vc_info);
837 			csi2_err_strncat(err_str, cur_str);
838 		}
839 
840 		if (val & CSIHOST_ERR1_ERR_CRC) {
841 			err_list = &csi2->err_list[RK_CSI2_ERR_CRC];
842 			err_list->cnt++;
843 			is_add_cnt = true;
844 			csi2_find_err_vc((val >> 24) & 0xf, vc_info);
845 			snprintf(cur_str, CSI_ERRSTR_LEN, "(crc,vc:%s) ", vc_info);
846 			csi2_err_strncat(err_str, cur_str);
847 		}
848 
849 		if (val & CSIHOST_ERR1_ERR_ECC2) {
850 			err_list = &csi2->err_list[RK_CSI2_ERR_CRC];
851 			err_list->cnt++;
852 			is_add_cnt = true;
853 			snprintf(cur_str, CSI_ERRSTR_LEN, "(ecc2) ");
854 			csi2_err_strncat(err_str, cur_str);
855 		}
856 
857 		if (val & CSIHOST_ERR1_ERR_CTRL) {
858 			csi2_find_err_vc((val >> 16) & 0xf, vc_info);
859 			snprintf(cur_str, CSI_ERRSTR_LEN, "(ctrl,vc:%s) ", vc_info);
860 			csi2_err_strncat(err_str, cur_str);
861 		}
862 
863 		pr_err("%s ERR1:0x%x %s\n", csi2_hw->dev_name, val, err_str);
864 
865 		if (is_add_cnt) {
866 			csi2->err_list[RK_CSI2_ERR_ALL].cnt++;
867 			err_stat = ((csi2->err_list[RK_CSI2_ERR_FS_FE_MIS].cnt & 0xff) << 8) |
868 				    ((csi2->err_list[RK_CSI2_ERR_ALL].cnt) & 0xff);
869 
870 			atomic_notifier_call_chain(&g_csi_host_chain,
871 						   err_stat,
872 						   &csi2->csi_info.csi_idx[csi2->csi_info.csi_num - 1]);
873 		}
874 
875 	}
876 
877 	return IRQ_HANDLED;
878 }
879 
rk_csirx_irq2_handler(int irq,void * ctx)880 static irqreturn_t rk_csirx_irq2_handler(int irq, void *ctx)
881 {
882 	struct device *dev = ctx;
883 	struct csi2_hw *csi2_hw = dev_get_drvdata(dev);
884 	u32 val;
885 	char cur_str[CSI_ERRSTR_LEN] = {0};
886 	char err_str[CSI_ERRSTR_LEN] = {0};
887 	char vc_info[CSI_VCINFO_LEN] = {0};
888 
889 	val = read_csihost_reg(csi2_hw->base, CSIHOST_ERR2);
890 	if (val) {
891 		if (val & CSIHOST_ERR2_PHYERR_ESC) {
892 			csi2_find_err_vc(val & 0xf, vc_info);
893 			snprintf(cur_str, CSI_ERRSTR_LEN, "(ULPM,lane:%s) ", vc_info);
894 			csi2_err_strncat(err_str, cur_str);
895 		}
896 		if (val & CSIHOST_ERR2_PHYERR_SOTHS) {
897 			csi2_find_err_vc((val >> 4) & 0xf, vc_info);
898 			snprintf(cur_str, CSI_ERRSTR_LEN, "(sot,lane:%s) ", vc_info);
899 			csi2_err_strncat(err_str, cur_str);
900 		}
901 		if (val & CSIHOST_ERR2_ECC_CORRECTED) {
902 			csi2_find_err_vc((val >> 8) & 0xf, vc_info);
903 			snprintf(cur_str, CSI_ERRSTR_LEN, "(ecc,vc:%s) ", vc_info);
904 			csi2_err_strncat(err_str, cur_str);
905 		}
906 		if (val & CSIHOST_ERR2_ERR_ID) {
907 			csi2_find_err_vc((val >> 12) & 0xf, vc_info);
908 			snprintf(cur_str, CSI_ERRSTR_LEN, "(err id,vc:%s) ", vc_info);
909 			csi2_err_strncat(err_str, cur_str);
910 		}
911 		if (val & CSIHOST_ERR2_PHYERR_CODEHS) {
912 			snprintf(cur_str, CSI_ERRSTR_LEN, "(err code) ");
913 			csi2_err_strncat(err_str, cur_str);
914 		}
915 
916 		pr_err("%s ERR2:0x%x %s\n", csi2_hw->dev_name, val, err_str);
917 	}
918 
919 	return IRQ_HANDLED;
920 }
921 
csi2_notifier(struct csi2_dev * csi2)922 static int csi2_notifier(struct csi2_dev *csi2)
923 {
924 	struct v4l2_async_notifier *ntf = &csi2->notifier;
925 	int ret;
926 
927 	v4l2_async_notifier_init(ntf);
928 
929 	ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(csi2->dev,
930 								 &csi2->notifier,
931 								 sizeof(struct v4l2_async_subdev), 0,
932 								 csi2_parse_endpoint);
933 	if (ret < 0)
934 		return ret;
935 
936 	csi2->sd.subdev_notifier = &csi2->notifier;
937 	csi2->notifier.ops = &csi2_async_ops;
938 	ret = v4l2_async_subdev_notifier_register(&csi2->sd, &csi2->notifier);
939 	if (ret) {
940 		v4l2_err(&csi2->sd,
941 			 "failed to register async notifier : %d\n",
942 			 ret);
943 		v4l2_async_notifier_cleanup(&csi2->notifier);
944 		return ret;
945 	}
946 
947 	ret = v4l2_async_register_subdev(&csi2->sd);
948 
949 	return ret;
950 }
951 
952 static const struct csi2_match_data rk1808_csi2_match_data = {
953 	.chip_id = CHIP_RK1808_CSI2,
954 	.num_pads = CSI2_NUM_PADS,
955 	.num_hw = 1,
956 };
957 
958 static const struct csi2_match_data rk3288_csi2_match_data = {
959 	.chip_id = CHIP_RK3288_CSI2,
960 	.num_pads = CSI2_NUM_PADS_SINGLE_LINK,
961 	.num_hw = 1,
962 };
963 
964 static const struct csi2_match_data rv1126_csi2_match_data = {
965 	.chip_id = CHIP_RV1126_CSI2,
966 	.num_pads = CSI2_NUM_PADS,
967 	.num_hw = 1,
968 };
969 
970 static const struct csi2_match_data rk3568_csi2_match_data = {
971 	.chip_id = CHIP_RK3568_CSI2,
972 	.num_pads = CSI2_NUM_PADS,
973 	.num_hw = 1,
974 };
975 
976 static const struct csi2_match_data rk3588_csi2_match_data = {
977 	.chip_id = CHIP_RK3588_CSI2,
978 	.num_pads = CSI2_NUM_PADS_MAX,
979 	.num_hw = 6,
980 };
981 
982 static const struct csi2_match_data rv1106_csi2_match_data = {
983 	.chip_id = CHIP_RV1106_CSI2,
984 	.num_pads = CSI2_NUM_PADS_MAX,
985 	.num_hw = 2,
986 };
987 
988 static const struct csi2_match_data rk3562_csi2_match_data = {
989 	.chip_id = CHIP_RK3562_CSI2,
990 	.num_pads = CSI2_NUM_PADS_MAX,
991 	.num_hw = 4,
992 };
993 
994 static const struct of_device_id csi2_dt_ids[] = {
995 	{
996 		.compatible = "rockchip,rk1808-mipi-csi2",
997 		.data = &rk1808_csi2_match_data,
998 	},
999 	{
1000 		.compatible = "rockchip,rk3288-mipi-csi2",
1001 		.data = &rk3288_csi2_match_data,
1002 	},
1003 	{
1004 		.compatible = "rockchip,rk3568-mipi-csi2",
1005 		.data = &rk3568_csi2_match_data,
1006 	},
1007 	{
1008 		.compatible = "rockchip,rv1126-mipi-csi2",
1009 		.data = &rv1126_csi2_match_data,
1010 	},
1011 	{
1012 		.compatible = "rockchip,rk3588-mipi-csi2",
1013 		.data = &rk3588_csi2_match_data,
1014 	},
1015 	{
1016 		.compatible = "rockchip,rv1106-mipi-csi2",
1017 		.data = &rv1106_csi2_match_data,
1018 	},
1019 	{
1020 		.compatible = "rockchip,rk3562-mipi-csi2",
1021 		.data = &rk3562_csi2_match_data,
1022 	},
1023 	{ /* sentinel */ }
1024 };
1025 MODULE_DEVICE_TABLE(of, csi2_dt_ids);
1026 
csi2_attach_hw(struct csi2_dev * csi2)1027 static int csi2_attach_hw(struct csi2_dev *csi2)
1028 {
1029 	struct device_node *np;
1030 	struct platform_device *pdev;
1031 	struct csi2_hw *hw;
1032 	int i = 0;
1033 
1034 	for (i = 0; i < csi2->match_data->num_hw; i++) {
1035 		np = of_parse_phandle(csi2->dev->of_node, "rockchip,hw", i);
1036 		if (!np || !of_device_is_available(np)) {
1037 			dev_err(csi2->dev, "failed to get csi2 hw node\n");
1038 			return -ENODEV;
1039 		}
1040 
1041 		pdev = of_find_device_by_node(np);
1042 		of_node_put(np);
1043 		if (!pdev) {
1044 			dev_err(csi2->dev, "failed to get csi2 hw from node\n");
1045 			return -ENODEV;
1046 		}
1047 
1048 		hw = platform_get_drvdata(pdev);
1049 		if (!hw) {
1050 			dev_err(csi2->dev, "failed attach csi2 hw\n");
1051 			return -EINVAL;
1052 		}
1053 
1054 		hw->csi2 = csi2;
1055 		csi2->csi2_hw[i] = hw;
1056 	}
1057 	dev_info(csi2->dev, "attach to csi2 hw node\n");
1058 
1059 	return 0;
1060 }
1061 
csi2_probe(struct platform_device * pdev)1062 static int csi2_probe(struct platform_device *pdev)
1063 {
1064 	const struct of_device_id *match;
1065 	struct device_node *node = pdev->dev.of_node;
1066 	struct csi2_dev *csi2 = NULL;
1067 	const struct csi2_match_data *data;
1068 	int ret;
1069 
1070 	match = of_match_node(csi2_dt_ids, node);
1071 	if (IS_ERR(match))
1072 		return PTR_ERR(match);
1073 	data = match->data;
1074 
1075 	csi2 = devm_kzalloc(&pdev->dev, sizeof(*csi2), GFP_KERNEL);
1076 	if (!csi2)
1077 		return -ENOMEM;
1078 
1079 	csi2->dev = &pdev->dev;
1080 	csi2->match_data = data;
1081 
1082 	csi2->dev_name = node->name;
1083 	v4l2_subdev_init(&csi2->sd, &csi2_subdev_ops);
1084 	v4l2_set_subdevdata(&csi2->sd, &pdev->dev);
1085 	csi2->sd.entity.ops = &csi2_entity_ops;
1086 	csi2->sd.dev = &pdev->dev;
1087 	csi2->sd.owner = THIS_MODULE;
1088 	csi2->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1089 	ret = strscpy(csi2->sd.name, DEVICE_NAME, sizeof(csi2->sd.name));
1090 	if (ret < 0)
1091 		v4l2_err(&csi2->sd, "failed to copy name\n");
1092 	platform_set_drvdata(pdev, &csi2->sd);
1093 
1094 	ret = csi2_attach_hw(csi2);
1095 	if (ret) {
1096 		v4l2_err(&csi2->sd, "must enable all mipi csi2 hw node\n");
1097 		return -EINVAL;
1098 	}
1099 	mutex_init(&csi2->lock);
1100 
1101 	ret = csi2_media_init(&csi2->sd);
1102 	if (ret < 0)
1103 		goto rmmutex;
1104 	ret = csi2_notifier(csi2);
1105 	if (ret)
1106 		goto rmmutex;
1107 
1108 	v4l2_info(&csi2->sd, "probe success, v4l2_dev:%s!\n", csi2->sd.v4l2_dev->name);
1109 
1110 	return 0;
1111 
1112 rmmutex:
1113 	mutex_destroy(&csi2->lock);
1114 	return ret;
1115 }
1116 
csi2_remove(struct platform_device * pdev)1117 static int csi2_remove(struct platform_device *pdev)
1118 {
1119 	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1120 	struct csi2_dev *csi2 = sd_to_dev(sd);
1121 
1122 	v4l2_async_unregister_subdev(sd);
1123 	mutex_destroy(&csi2->lock);
1124 	media_entity_cleanup(&sd->entity);
1125 
1126 	return 0;
1127 }
1128 
1129 static struct platform_driver csi2_driver = {
1130 	.driver = {
1131 		.name = DEVICE_NAME,
1132 		.of_match_table = csi2_dt_ids,
1133 	},
1134 	.probe = csi2_probe,
1135 	.remove = csi2_remove,
1136 };
1137 
rkcif_csi2_plat_drv_init(void)1138 int rkcif_csi2_plat_drv_init(void)
1139 {
1140 	return platform_driver_register(&csi2_driver);
1141 }
1142 
rkcif_csi2_plat_drv_exit(void)1143 void rkcif_csi2_plat_drv_exit(void)
1144 {
1145 	platform_driver_unregister(&csi2_driver);
1146 }
1147 
1148 static const struct csi2_hw_match_data rk1808_csi2_hw_match_data = {
1149 	.chip_id = CHIP_RK1808_CSI2,
1150 };
1151 
1152 static const struct csi2_hw_match_data rk3288_csi2_hw_match_data = {
1153 	.chip_id = CHIP_RK3288_CSI2,
1154 };
1155 
1156 static const struct csi2_hw_match_data rv1126_csi2_hw_match_data = {
1157 	.chip_id = CHIP_RV1126_CSI2,
1158 };
1159 
1160 static const struct csi2_hw_match_data rk3568_csi2_hw_match_data = {
1161 	.chip_id = CHIP_RK3568_CSI2,
1162 };
1163 
1164 static const struct csi2_hw_match_data rk3588_csi2_hw_match_data = {
1165 	.chip_id = CHIP_RK3588_CSI2,
1166 };
1167 
1168 static const struct csi2_hw_match_data rv1106_csi2_hw_match_data = {
1169 	.chip_id = CHIP_RV1106_CSI2,
1170 };
1171 
1172 static const struct csi2_hw_match_data rk3562_csi2_hw_match_data = {
1173 	.chip_id = CHIP_RK3562_CSI2,
1174 };
1175 
1176 static const struct of_device_id csi2_hw_ids[] = {
1177 	{
1178 		.compatible = "rockchip,rk1808-mipi-csi2-hw",
1179 		.data = &rk1808_csi2_hw_match_data,
1180 	},
1181 	{
1182 		.compatible = "rockchip,rk3288-mipi-csi2-hw",
1183 		.data = &rk3288_csi2_hw_match_data,
1184 	},
1185 	{
1186 		.compatible = "rockchip,rk3568-mipi-csi2-hw",
1187 		.data = &rk3568_csi2_hw_match_data,
1188 	},
1189 	{
1190 		.compatible = "rockchip,rv1126-mipi-csi2-hw",
1191 		.data = &rv1126_csi2_hw_match_data,
1192 	},
1193 	{
1194 		.compatible = "rockchip,rk3588-mipi-csi2-hw",
1195 		.data = &rk3588_csi2_hw_match_data,
1196 	},
1197 	{
1198 		.compatible = "rockchip,rv1106-mipi-csi2-hw",
1199 		.data = &rv1106_csi2_hw_match_data,
1200 	},
1201 	{
1202 		.compatible = "rockchip,rk3562-mipi-csi2-hw",
1203 		.data = &rk3588_csi2_hw_match_data,
1204 	},
1205 	{ /* sentinel */ }
1206 };
1207 MODULE_DEVICE_TABLE(of, csi2_hw_ids);
1208 
csi2_hw_probe(struct platform_device * pdev)1209 static int csi2_hw_probe(struct platform_device *pdev)
1210 {
1211 	const struct of_device_id *match;
1212 	struct device *dev = &pdev->dev;
1213 	struct device_node *node = pdev->dev.of_node;
1214 	struct csi2_hw *csi2_hw = NULL;
1215 	struct resource *res;
1216 	const struct csi2_hw_match_data *data;
1217 	int ret, irq;
1218 
1219 	dev_info(&pdev->dev, "enter mipi csi2 hw probe!\n");
1220 	match = of_match_node(csi2_hw_ids, node);
1221 	if (IS_ERR(match))
1222 		return PTR_ERR(match);
1223 	data = match->data;
1224 
1225 	csi2_hw = devm_kzalloc(&pdev->dev, sizeof(*csi2_hw), GFP_KERNEL);
1226 	if (!csi2_hw)
1227 		return -ENOMEM;
1228 
1229 	csi2_hw->dev = &pdev->dev;
1230 	csi2_hw->match_data = data;
1231 
1232 	csi2_hw->dev_name = node->name;
1233 
1234 	csi2_hw->clks_num = devm_clk_bulk_get_all(dev, &csi2_hw->clks_bulk);
1235 	if (csi2_hw->clks_num < 0) {
1236 		csi2_hw->clks_num = 0;
1237 		dev_err(dev, "failed to get csi2 clks\n");
1238 	}
1239 
1240 	csi2_hw->rsts_bulk = devm_reset_control_array_get_optional_exclusive(dev);
1241 	if (IS_ERR(csi2_hw->rsts_bulk)) {
1242 		if (PTR_ERR(csi2_hw->rsts_bulk) != -EPROBE_DEFER)
1243 			dev_err(dev, "failed to get csi2 reset\n");
1244 		csi2_hw->rsts_bulk = NULL;
1245 	}
1246 
1247 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1248 	csi2_hw->base = devm_ioremap_resource(&pdev->dev, res);
1249 	if (IS_ERR(csi2_hw->base)) {
1250 		resource_size_t offset = res->start;
1251 		resource_size_t size = resource_size(res);
1252 
1253 		dev_warn(&pdev->dev, "avoid secondary mipi resource check!\n");
1254 
1255 		csi2_hw->base = devm_ioremap(&pdev->dev, offset, size);
1256 		if (IS_ERR(csi2_hw->base)) {
1257 			dev_err(&pdev->dev, "Failed to ioremap resource\n");
1258 
1259 			return PTR_ERR(csi2_hw->base);
1260 		}
1261 	}
1262 
1263 	irq = platform_get_irq_byname(pdev, "csi-intr1");
1264 	if (irq > 0) {
1265 		ret = devm_request_irq(&pdev->dev, irq,
1266 				       rk_csirx_irq1_handler, 0,
1267 				       dev_driver_string(&pdev->dev),
1268 				       &pdev->dev);
1269 		if (ret < 0)
1270 			dev_err(&pdev->dev, "request csi-intr1 irq failed: %d\n",
1271 				 ret);
1272 		csi2_hw->irq1 = irq;
1273 	} else {
1274 		dev_err(&pdev->dev, "No found irq csi-intr1\n");
1275 	}
1276 
1277 	irq = platform_get_irq_byname(pdev, "csi-intr2");
1278 	if (irq > 0) {
1279 		ret = devm_request_irq(&pdev->dev, irq,
1280 				       rk_csirx_irq2_handler, 0,
1281 				       dev_driver_string(&pdev->dev),
1282 				       &pdev->dev);
1283 		if (ret < 0)
1284 			dev_err(&pdev->dev, "request csi-intr2 failed: %d\n",
1285 				 ret);
1286 		csi2_hw->irq2 = irq;
1287 	} else {
1288 		dev_err(&pdev->dev, "No found irq csi-intr2\n");
1289 	}
1290 	platform_set_drvdata(pdev, csi2_hw);
1291 	dev_info(&pdev->dev, "probe success, v4l2_dev:%s!\n", csi2_hw->dev_name);
1292 
1293 	return 0;
1294 }
1295 
csi2_hw_remove(struct platform_device * pdev)1296 static int csi2_hw_remove(struct platform_device *pdev)
1297 {
1298 	return 0;
1299 }
1300 
1301 static struct platform_driver csi2_hw_driver = {
1302 	.driver = {
1303 		.name = DEVICE_NAME_HW,
1304 		.of_match_table = csi2_hw_ids,
1305 	},
1306 	.probe = csi2_hw_probe,
1307 	.remove = csi2_hw_remove,
1308 };
1309 
rkcif_csi2_hw_plat_drv_init(void)1310 int rkcif_csi2_hw_plat_drv_init(void)
1311 {
1312 	return platform_driver_register(&csi2_hw_driver);
1313 }
1314 
rkcif_csi2_hw_plat_drv_exit(void)1315 void rkcif_csi2_hw_plat_drv_exit(void)
1316 {
1317 	platform_driver_unregister(&csi2_hw_driver);
1318 }
1319 
1320 MODULE_DESCRIPTION("Rockchip MIPI CSI2 driver");
1321 MODULE_AUTHOR("Macrofly.xu <xuhf@rock-chips.com>");
1322 MODULE_LICENSE("GPL");
1323