• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Rockchip isp1 driver
3  *
4  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/clk.h>
36 #include <linux/interrupt.h>
37 #include <linux/io.h>
38 #include <linux/module.h>
39 #include <linux/of.h>
40 #include <linux/of_address.h>
41 #include <linux/of_gpio.h>
42 #include <linux/of_graph.h>
43 #include <linux/of_platform.h>
44 #include <linux/of_reserved_mem.h>
45 #include <linux/pm_runtime.h>
46 #include <linux/pinctrl/consumer.h>
47 #include <linux/regmap.h>
48 #include <dt-bindings/soc/rockchip-system-status.h>
49 #include <soc/rockchip/rockchip-system-status.h>
50 #include "common.h"
51 #include "isp_ispp.h"
52 #include "regs.h"
53 #include "rkisp.h"
54 #include "version.h"
55 
56 #define RKISP_VERNO_LEN		10
57 
58 int rkisp_debug;
59 module_param_named(debug, rkisp_debug, int, 0644);
60 MODULE_PARM_DESC(debug, "Debug level (0-1)");
61 
62 bool rkisp_monitor;
63 module_param_named(monitor, rkisp_monitor, bool, 0644);
64 MODULE_PARM_DESC(monitor, "rkisp abnormal restart monitor");
65 
66 static bool rkisp_clk_dbg;
67 module_param_named(clk_dbg, rkisp_clk_dbg, bool, 0644);
68 MODULE_PARM_DESC(clk_dbg, "rkisp clk set by user");
69 
70 static char rkisp_version[RKISP_VERNO_LEN];
71 module_param_string(version, rkisp_version, RKISP_VERNO_LEN, 0444);
72 MODULE_PARM_DESC(version, "version number");
73 
74 u64 rkisp_debug_reg = 0xFFFFFFFFFLL;
75 module_param_named(debug_reg, rkisp_debug_reg, ullong, 0644);
76 MODULE_PARM_DESC(debug_reg, "rkisp debug register");
77 
78 static unsigned int rkisp_wait_line;
79 module_param_named(wait_line, rkisp_wait_line, uint, 0644);
80 MODULE_PARM_DESC(wait_line, "rkisp wait line to buf done early");
81 
82 static DEFINE_MUTEX(rkisp_dev_mutex);
83 static LIST_HEAD(rkisp_device_list);
84 
rkisp_set_clk_rate(struct clk * clk,unsigned long rate)85 void rkisp_set_clk_rate(struct clk *clk, unsigned long rate)
86 {
87 	if (rkisp_clk_dbg)
88 		return;
89 
90 	clk_set_rate(clk, rate);
91 }
92 
__rkisp_clr_unready_dev(void)93 static int __maybe_unused __rkisp_clr_unready_dev(void)
94 {
95 	struct rkisp_device *isp_dev;
96 
97 	mutex_lock(&rkisp_dev_mutex);
98 	list_for_each_entry(isp_dev, &rkisp_device_list, list)
99 		v4l2_async_notifier_clr_unready_dev(&isp_dev->notifier);
100 	mutex_unlock(&rkisp_dev_mutex);
101 
102 	return 0;
103 }
104 
rkisp_clr_unready_dev_param_set(const char * val,const struct kernel_param * kp)105 static int rkisp_clr_unready_dev_param_set(const char *val, const struct kernel_param *kp)
106 {
107 #ifdef MODULE
108 	__rkisp_clr_unready_dev();
109 #endif
110 
111 	return 0;
112 }
113 
114 module_param_call(clr_unready_dev, rkisp_clr_unready_dev_param_set, NULL, NULL, 0200);
115 MODULE_PARM_DESC(clr_unready_dev, "clear unready devices");
116 
117 /**************************** pipeline operations *****************************/
118 
__isp_pipeline_prepare(struct rkisp_pipeline * p,struct media_entity * me)119 static int __isp_pipeline_prepare(struct rkisp_pipeline *p,
120 				  struct media_entity *me)
121 {
122 	struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
123 	struct v4l2_subdev *sd;
124 	int i;
125 
126 	p->num_subdevs = 0;
127 	memset(p->subdevs, 0, sizeof(p->subdevs));
128 
129 	if (!(dev->isp_inp & (INP_CSI | INP_DVP | INP_LVDS | INP_CIF)))
130 		return 0;
131 
132 	while (1) {
133 		struct media_pad *pad = NULL;
134 
135 		/* Find remote source pad */
136 		for (i = 0; i < me->num_pads; i++) {
137 			struct media_pad *spad = &me->pads[i];
138 
139 			if (!(spad->flags & MEDIA_PAD_FL_SINK))
140 				continue;
141 			pad = rkisp_media_entity_remote_pad(spad);
142 			if (pad)
143 				break;
144 		}
145 
146 		if (!pad)
147 			break;
148 
149 		sd = media_entity_to_v4l2_subdev(pad->entity);
150 		if (sd != &dev->isp_sdev.sd)
151 			p->subdevs[p->num_subdevs++] = sd;
152 
153 		me = &sd->entity;
154 		if (me->num_pads == 1)
155 			break;
156 	}
157 	if (!p->num_subdevs)
158 		return -EINVAL;
159 
160 	return 0;
161 }
162 
__isp_pipeline_s_isp_clk(struct rkisp_pipeline * p)163 static int __isp_pipeline_s_isp_clk(struct rkisp_pipeline *p)
164 {
165 	struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
166 	struct rkisp_hw_dev *hw_dev = dev->hw_dev;
167 	u32 w = hw_dev->max_in.w ? hw_dev->max_in.w : dev->isp_sdev.in_frm.width;
168 	struct v4l2_subdev *sd;
169 	struct v4l2_ctrl *ctrl;
170 	u64 data_rate;
171 	int i;
172 
173 	if (dev->isp_inp & (INP_RAWRD0 | INP_RAWRD1 | INP_RAWRD2 | INP_CIF)) {
174 		for (i = 0; i < hw_dev->num_clk_rate_tbl; i++) {
175 			if (w <= hw_dev->clk_rate_tbl[i].refer_data)
176 				break;
177 		}
178 		if (!hw_dev->is_single)
179 			i++;
180 		if (i > hw_dev->num_clk_rate_tbl - 1)
181 			i = hw_dev->num_clk_rate_tbl - 1;
182 		goto end;
183 	}
184 
185 	if (dev->isp_inp == INP_DMARX_ISP && dev->hw_dev->clks[0]) {
186 		rkisp_set_clk_rate(hw_dev->clks[0], 400 * 1000000UL);
187 		return 0;
188 	}
189 
190 	/* find the subdev of active sensor */
191 	sd = p->subdevs[0];
192 	for (i = 0; i < p->num_subdevs; i++) {
193 		sd = p->subdevs[i];
194 		if (sd->entity.function == MEDIA_ENT_F_CAM_SENSOR)
195 			break;
196 	}
197 
198 	if (i == p->num_subdevs) {
199 		v4l2_warn(&dev->v4l2_dev, "No active sensor\n");
200 		return -EPIPE;
201 	}
202 
203 	ctrl = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_PIXEL_RATE);
204 	if (!ctrl) {
205 		v4l2_warn(&dev->v4l2_dev, "No pixel rate control in subdev\n");
206 		return -EPIPE;
207 	}
208 
209 	/* calculate data rate */
210 	data_rate = v4l2_ctrl_g_ctrl_int64(ctrl) *
211 		    dev->isp_sdev.in_fmt.bus_width;
212 	data_rate >>= 3;
213 	do_div(data_rate, 1000 * 1000);
214 
215 	/* increase 25% margin */
216 	data_rate += data_rate >> 2;
217 
218 	/* compare with isp clock adjustment table */
219 	for (i = 0; i < hw_dev->num_clk_rate_tbl; i++)
220 		if (data_rate <= hw_dev->clk_rate_tbl[i].clk_rate)
221 			break;
222 	if (i == hw_dev->num_clk_rate_tbl)
223 		i--;
224 end:
225 	/* set isp clock rate */
226 	rkisp_set_clk_rate(hw_dev->clks[0], hw_dev->clk_rate_tbl[i].clk_rate * 1000000UL);
227 	if (hw_dev->is_unite)
228 		rkisp_set_clk_rate(hw_dev->clks[5], hw_dev->clk_rate_tbl[i].clk_rate * 1000000UL);
229 	dev_dbg(hw_dev->dev, "set isp clk = %luHz\n", clk_get_rate(hw_dev->clks[0]));
230 
231 	return 0;
232 }
233 
rkisp_pipeline_open(struct rkisp_pipeline * p,struct media_entity * me,bool prepare)234 static int rkisp_pipeline_open(struct rkisp_pipeline *p,
235 				struct media_entity *me,
236 				bool prepare)
237 {
238 	int ret;
239 	struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
240 
241 	if (WARN_ON(!p || !me))
242 		return -EINVAL;
243 	if (atomic_inc_return(&p->power_cnt) > 1)
244 		return 0;
245 
246 	/* go through media graphic and get subdevs */
247 	if (prepare) {
248 		ret = __isp_pipeline_prepare(p, me);
249 		if (ret < 0)
250 			return ret;
251 	}
252 
253 	ret = __isp_pipeline_s_isp_clk(p);
254 	if (ret < 0)
255 		return ret;
256 
257 	if (dev->isp_inp & (INP_CSI | INP_RAWRD0 | INP_RAWRD1 | INP_RAWRD2 | INP_CIF))
258 		rkisp_csi_config_patch(dev);
259 	return 0;
260 }
261 
rkisp_pipeline_close(struct rkisp_pipeline * p)262 static int rkisp_pipeline_close(struct rkisp_pipeline *p)
263 {
264 	struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
265 
266 	atomic_dec(&p->power_cnt);
267 
268 	if (dev->isp_ver == ISP_V30 && !atomic_read(&p->power_cnt))
269 		rkisp_rx_buf_pool_free(dev);
270 
271 	return 0;
272 }
273 
274 /*
275  * stream-on order: isp_subdev, mipi dphy, sensor
276  * stream-off order: mipi dphy, sensor, isp_subdev
277  */
rkisp_pipeline_set_stream(struct rkisp_pipeline * p,bool on)278 static int rkisp_pipeline_set_stream(struct rkisp_pipeline *p, bool on)
279 {
280 	struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
281 	int i, ret;
282 
283 	if ((on && atomic_inc_return(&p->stream_cnt) > 1) ||
284 	    (!on && atomic_dec_return(&p->stream_cnt) > 0))
285 		return 0;
286 
287 	if (on) {
288 		if (dev->vs_irq >= 0)
289 			enable_irq(dev->vs_irq);
290 		rockchip_set_system_status(SYS_STATUS_ISP);
291 		v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, true);
292 		/* phy -> sensor */
293 		for (i = 0; i < p->num_subdevs; ++i) {
294 			ret = v4l2_subdev_call(p->subdevs[i], video, s_stream, on);
295 			if (on && ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
296 				goto err_stream_off;
297 		}
298 	} else {
299 		/* sensor -> phy */
300 		for (i = p->num_subdevs - 1; i >= 0; --i)
301 			v4l2_subdev_call(p->subdevs[i], video, s_stream, on);
302 		if (dev->vs_irq >= 0)
303 			disable_irq(dev->vs_irq);
304 		v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, false);
305 		rockchip_clear_system_status(SYS_STATUS_ISP);
306 	}
307 
308 	return 0;
309 
310 err_stream_off:
311 	for (--i; i >= 0; --i)
312 		v4l2_subdev_call(p->subdevs[i], video, s_stream, false);
313 	v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, false);
314 	rockchip_clear_system_status(SYS_STATUS_ISP);
315 	return ret;
316 }
317 
318 /***************************** media controller *******************************/
319 /* See http://opensource.rock-chips.com/wiki_Rockchip-isp1 for Topology */
320 
rkisp_create_links(struct rkisp_device * dev)321 static int rkisp_create_links(struct rkisp_device *dev)
322 {
323 	unsigned int s, pad;
324 	int ret = 0;
325 
326 	/* sensor links(or mipi-phy) */
327 	for (s = 0; s < dev->num_sensors; ++s) {
328 		struct rkisp_sensor_info *sensor = &dev->sensors[s];
329 		u32 type = sensor->sd->entity.function;
330 		bool en = s ? 0 : true;
331 
332 		for (pad = 0; pad < sensor->sd->entity.num_pads; pad++)
333 			if (sensor->sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)
334 				break;
335 
336 		if (pad == sensor->sd->entity.num_pads) {
337 			dev_err(dev->dev, "failed to find src pad for %s\n",
338 				sensor->sd->name);
339 			return -ENXIO;
340 		}
341 
342 		/* sensor link -> isp */
343 		if (type == MEDIA_ENT_F_CAM_SENSOR) {
344 			dev->isp_inp = INP_DVP;
345 			ret = media_create_pad_link(&sensor->sd->entity, pad,
346 				&dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
347 		} else if (type == MEDIA_ENT_F_PROC_VIDEO_COMPOSER) {
348 			dev->isp_inp = INP_CIF;
349 			ret = media_create_pad_link(&sensor->sd->entity, pad,
350 				&dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
351 		} else {
352 			v4l2_subdev_call(sensor->sd, pad,
353 					 get_mbus_config, 0, &sensor->mbus);
354 			if (sensor->mbus.type == V4L2_MBUS_CCP2) {
355 				/* mipi-phy lvds link -> isp */
356 				dev->isp_inp = INP_LVDS;
357 				ret = media_create_pad_link(&sensor->sd->entity, pad,
358 					&dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
359 			} else {
360 				/* mipi-phy link -> csi -> isp */
361 				dev->isp_inp = INP_CSI;
362 				ret = media_create_pad_link(&sensor->sd->entity,
363 					pad, &dev->csi_dev.sd.entity, CSI_SINK, en);
364 				ret |= media_create_pad_link(&dev->csi_dev.sd.entity, CSI_SRC_CH0,
365 					&dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
366 				dev->csi_dev.sink[0].linked = en;
367 				dev->csi_dev.sink[0].index = BIT(0);
368 			}
369 		}
370 		if (ret)
371 			dev_err(dev->dev, "failed to create link for %s\n", sensor->sd->name);
372 	}
373 	return ret;
374 }
375 
_set_pipeline_default_fmt(struct rkisp_device * dev)376 static int _set_pipeline_default_fmt(struct rkisp_device *dev)
377 {
378 	struct v4l2_subdev *isp;
379 	struct v4l2_subdev_format fmt;
380 	struct v4l2_subdev_selection sel;
381 	u32 i, width, height, code;
382 
383 	isp = &dev->isp_sdev.sd;
384 
385 	if (dev->active_sensor)
386 		fmt = dev->active_sensor->fmt[0];
387 	else
388 		fmt.format = dev->isp_sdev.in_frm;
389 	code = fmt.format.code;
390 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
391 	fmt.pad = RKISP_ISP_PAD_SINK;
392 	/* isp input format information from sensor */
393 	v4l2_subdev_call(isp, pad, set_fmt, NULL, &fmt);
394 
395 	rkisp_align_sensor_resolution(dev, &sel.r, false);
396 	width = sel.r.width;
397 	height = sel.r.height;
398 	sel.target = V4L2_SEL_TGT_CROP;
399 	sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
400 	sel.pad = RKISP_ISP_PAD_SINK;
401 	/* image resolution processed by isp */
402 	v4l2_subdev_call(isp, pad, set_selection, NULL, &sel);
403 
404 	/* change fmt&size for RKISP_ISP_PAD_SOURCE_PATH */
405 	if ((code & RKISP_MEDIA_BUS_FMT_MASK) == RKISP_MEDIA_BUS_FMT_BAYER)
406 		fmt.format.code = MEDIA_BUS_FMT_YUYV8_2X8;
407 
408 	sel.r.left = 0;
409 	sel.r.top = 0;
410 	fmt.format.width = width;
411 	fmt.format.height = height;
412 	fmt.pad = RKISP_ISP_PAD_SOURCE_PATH;
413 	sel.pad = RKISP_ISP_PAD_SOURCE_PATH;
414 	v4l2_subdev_call(isp, pad, set_fmt, NULL, &fmt);
415 	v4l2_subdev_call(isp, pad, set_selection, NULL, &sel);
416 
417 	/* change fmt&size of MP/SP */
418 	rkisp_set_stream_def_fmt(dev, RKISP_STREAM_MP,
419 				 width, height, V4L2_PIX_FMT_NV12);
420 	if (dev->isp_ver != ISP_V10_1)
421 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_SP,
422 					 width, height, V4L2_PIX_FMT_NV12);
423 	if ((dev->isp_ver == ISP_V20 || dev->isp_ver == ISP_V21) &&
424 	    dev->isp_inp == INP_CSI && dev->active_sensor) {
425 		width = dev->active_sensor->fmt[1].format.width;
426 		height = dev->active_sensor->fmt[1].format.height;
427 		code = dev->active_sensor->fmt[1].format.code;
428 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX0,
429 			width, height, rkisp_mbus_pixelcode_to_v4l2(code));
430 
431 		width = dev->active_sensor->fmt[3].format.width;
432 		height = dev->active_sensor->fmt[3].format.height;
433 		code = dev->active_sensor->fmt[3].format.code;
434 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX2,
435 			width, height, rkisp_mbus_pixelcode_to_v4l2(code));
436 
437 		width = dev->active_sensor->fmt[4].format.width;
438 		height = dev->active_sensor->fmt[4].format.height;
439 		code = dev->active_sensor->fmt[4].format.code;
440 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX3,
441 			width, height, rkisp_mbus_pixelcode_to_v4l2(code));
442 	}
443 
444 	if (dev->isp_ver == ISP_V20 &&
445 	    dev->isp_inp == INP_CSI && dev->active_sensor) {
446 		width = dev->active_sensor->fmt[2].format.width;
447 		height = dev->active_sensor->fmt[2].format.height;
448 		code = dev->active_sensor->fmt[2].format.code;
449 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX1,
450 			width, height, rkisp_mbus_pixelcode_to_v4l2(code));
451 	}
452 
453 	if (dev->isp_ver == ISP_V30) {
454 		struct v4l2_pix_format_mplane pixm = {
455 			.width = width,
456 			.height = height,
457 			.pixelformat = rkisp_mbus_pixelcode_to_v4l2(code),
458 		};
459 
460 		for (i = RKISP_STREAM_RAWRD0; i <= RKISP_STREAM_RAWRD2; i++)
461 			rkisp_dmarx_set_fmt(&dev->dmarx_dev.stream[i], pixm);
462 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_FBC,
463 					 width, height, V4L2_PIX_FMT_FBC0);
464 #ifdef RKISP_STREAM_BP_EN
465 		rkisp_set_stream_def_fmt(dev, RKISP_STREAM_BP,
466 					 width, height, V4L2_PIX_FMT_NV12);
467 #endif
468 	}
469 	return 0;
470 }
471 
subdev_notifier_complete(struct v4l2_async_notifier * notifier)472 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
473 {
474 	struct rkisp_device *dev;
475 	int ret;
476 
477 	dev = container_of(notifier, struct rkisp_device, notifier);
478 
479 	mutex_lock(&dev->media_dev.graph_mutex);
480 	ret = rkisp_create_links(dev);
481 	if (ret < 0)
482 		goto unlock;
483 	ret = v4l2_device_register_subdev_nodes(&dev->v4l2_dev);
484 	if (ret < 0)
485 		goto unlock;
486 
487 	if (dev->isp_inp) {
488 		ret = rkisp_update_sensor_info(dev);
489 		if (ret < 0) {
490 			v4l2_err(&dev->v4l2_dev, "update sensor failed\n");
491 			goto unlock;
492 		}
493 	}
494 
495 	ret = _set_pipeline_default_fmt(dev);
496 	if (ret < 0)
497 		goto unlock;
498 
499 	v4l2_info(&dev->v4l2_dev, "Async subdev notifier completed\n");
500 
501 unlock:
502 	mutex_unlock(&dev->media_dev.graph_mutex);
503 	return ret;
504 }
505 
506 struct rkisp_async_subdev {
507 	struct v4l2_async_subdev asd;
508 	struct v4l2_mbus_config mbus;
509 };
510 
subdev_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)511 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
512 				 struct v4l2_subdev *subdev,
513 				 struct v4l2_async_subdev *asd)
514 {
515 	struct rkisp_device *isp_dev = container_of(notifier,
516 					struct rkisp_device, notifier);
517 	struct rkisp_async_subdev *s_asd = container_of(asd,
518 					struct rkisp_async_subdev, asd);
519 
520 	if (isp_dev->num_sensors == ARRAY_SIZE(isp_dev->sensors))
521 		return -EBUSY;
522 
523 	isp_dev->sensors[isp_dev->num_sensors].mbus = s_asd->mbus;
524 	isp_dev->sensors[isp_dev->num_sensors].sd = subdev;
525 	++isp_dev->num_sensors;
526 
527 	v4l2_dbg(1, rkisp_debug, subdev, "Async registered subdev\n");
528 
529 	return 0;
530 }
531 
rkisp_fwnode_parse(struct device * dev,struct v4l2_fwnode_endpoint * vep,struct v4l2_async_subdev * asd)532 static int rkisp_fwnode_parse(struct device *dev,
533 			       struct v4l2_fwnode_endpoint *vep,
534 			       struct v4l2_async_subdev *asd)
535 {
536 	struct rkisp_async_subdev *rk_asd =
537 			container_of(asd, struct rkisp_async_subdev, asd);
538 	struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
539 
540 	/*
541 	 * MIPI sensor is linked with a mipi dphy and its media bus config can
542 	 * not be get in here
543 	 */
544 	if (vep->bus_type != V4L2_MBUS_BT656 &&
545 	    vep->bus_type != V4L2_MBUS_PARALLEL)
546 		return 0;
547 
548 	rk_asd->mbus.flags = bus->flags;
549 	rk_asd->mbus.type = vep->bus_type;
550 
551 	return 0;
552 }
553 
554 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
555 	.bound = subdev_notifier_bound,
556 	.complete = subdev_notifier_complete,
557 };
558 
isp_subdev_notifier(struct rkisp_device * isp_dev)559 static int isp_subdev_notifier(struct rkisp_device *isp_dev)
560 {
561 	struct v4l2_async_notifier *ntf = &isp_dev->notifier;
562 	struct device *dev = isp_dev->dev;
563 	int ret;
564 
565 	v4l2_async_notifier_init(ntf);
566 
567 	ret = v4l2_async_notifier_parse_fwnode_endpoints(
568 		dev, ntf, sizeof(struct rkisp_async_subdev),
569 		rkisp_fwnode_parse);
570 	if (ret < 0)
571 		return ret;
572 
573 	ntf->ops = &subdev_notifier_ops;
574 
575 	return v4l2_async_notifier_register(&isp_dev->v4l2_dev, ntf);
576 }
577 
578 /***************************** platform deive *******************************/
579 
rkisp_register_platform_subdevs(struct rkisp_device * dev)580 static int rkisp_register_platform_subdevs(struct rkisp_device *dev)
581 {
582 	int ret;
583 
584 	ret = rkisp_register_isp_subdev(dev, &dev->v4l2_dev);
585 	if (ret < 0)
586 		return ret;
587 
588 	ret = rkisp_register_csi_subdev(dev, &dev->v4l2_dev);
589 	if (ret < 0)
590 		goto err_unreg_isp_subdev;
591 
592 	ret = rkisp_register_bridge_subdev(dev, &dev->v4l2_dev);
593 	if (ret < 0)
594 		goto err_unreg_csi_subdev;
595 
596 	ret = rkisp_register_stream_vdevs(dev);
597 	if (ret < 0)
598 		goto err_unreg_bridge_subdev;
599 
600 	ret = rkisp_register_dmarx_vdev(dev);
601 	if (ret < 0)
602 		goto err_unreg_stream_vdev;
603 
604 	ret = rkisp_register_stats_vdev(&dev->stats_vdev, &dev->v4l2_dev, dev);
605 	if (ret < 0)
606 		goto err_unreg_dmarx_vdev;
607 
608 	ret = rkisp_register_params_vdev(&dev->params_vdev, &dev->v4l2_dev, dev);
609 	if (ret < 0)
610 		goto err_unreg_stats_vdev;
611 
612 	ret = rkisp_register_luma_vdev(&dev->luma_vdev, &dev->v4l2_dev, dev);
613 	if (ret < 0)
614 		goto err_unreg_params_vdev;
615 
616 	ret = isp_subdev_notifier(dev);
617 	if (ret < 0) {
618 		v4l2_err(&dev->v4l2_dev,
619 			 "Failed to register subdev notifier(%d)\n", ret);
620 		goto err_unreg_luma_vdev;
621 	}
622 
623 	return 0;
624 err_unreg_luma_vdev:
625 	rkisp_unregister_luma_vdev(&dev->luma_vdev);
626 err_unreg_params_vdev:
627 	rkisp_unregister_params_vdev(&dev->params_vdev);
628 err_unreg_stats_vdev:
629 	rkisp_unregister_stats_vdev(&dev->stats_vdev);
630 err_unreg_dmarx_vdev:
631 	rkisp_unregister_dmarx_vdev(dev);
632 err_unreg_stream_vdev:
633 	rkisp_unregister_stream_vdevs(dev);
634 err_unreg_bridge_subdev:
635 	rkisp_unregister_bridge_subdev(dev);
636 err_unreg_csi_subdev:
637 	rkisp_unregister_csi_subdev(dev);
638 err_unreg_isp_subdev:
639 	rkisp_unregister_isp_subdev(dev);
640 
641 	return ret;
642 }
643 
rkisp_vs_irq_parse(struct device * dev)644 static int rkisp_vs_irq_parse(struct device *dev)
645 {
646 	int ret;
647 	int vs_irq;
648 	unsigned long vs_irq_flags;
649 	struct gpio_desc *vs_irq_gpio;
650 	struct rkisp_device *isp_dev = dev_get_drvdata(dev);
651 
652 	/* this irq recevice the message of sensor vs from preisp */
653 	isp_dev->vs_irq = -1;
654 	vs_irq_gpio = devm_gpiod_get(dev, "vsirq", GPIOD_IN);
655 	if (!IS_ERR(vs_irq_gpio)) {
656 		vs_irq_flags = IRQF_TRIGGER_RISING |
657 			       IRQF_ONESHOT | IRQF_SHARED;
658 
659 		vs_irq = gpiod_to_irq(vs_irq_gpio);
660 		if (vs_irq < 0) {
661 			dev_err(dev, "GPIO to interrupt failed\n");
662 			return vs_irq;
663 		}
664 
665 		dev_info(dev, "register_irq: %d\n", vs_irq);
666 		ret = devm_request_irq(dev,
667 				       vs_irq,
668 				       rkisp_vs_isr_handler,
669 				       vs_irq_flags,
670 				       "vs_irq_gpio_int",
671 				       dev);
672 		if (ret) {
673 			dev_err(dev, "devm_request_irq failed: %d\n", ret);
674 			return ret;
675 		} else {
676 			disable_irq(vs_irq);
677 			isp_dev->vs_irq = vs_irq;
678 			isp_dev->vs_irq_gpio = vs_irq_gpio;
679 			dev_info(dev, "vs_gpio_int interrupt is hooked\n");
680 		}
681 	}
682 
683 	return 0;
684 }
685 
686 static const struct media_device_ops rkisp_media_ops = {
687 	.link_notify = v4l2_pipeline_link_notify,
688 };
689 
rkisp_get_reserved_mem(struct rkisp_device * isp_dev)690 static int rkisp_get_reserved_mem(struct rkisp_device *isp_dev)
691 {
692 	struct device *dev = isp_dev->dev;
693 	struct device_node *np;
694 	struct resource r;
695 	int ret;
696 
697 	/* Get reserved memory region from Device-tree */
698 	np = of_parse_phandle(dev->of_node, "memory-region-thunderboot", 0);
699 	if (!np) {
700 		dev_info(dev, "No memory-region-thunderboot specified\n");
701 		return 0;
702 	}
703 
704 	ret = of_address_to_resource(np, 0, &r);
705 	if (ret) {
706 		dev_err(dev, "No memory address assigned to the region\n");
707 		return ret;
708 	}
709 
710 	isp_dev->resmem_pa = r.start;
711 	isp_dev->resmem_size = resource_size(&r);
712 	isp_dev->resmem_addr = dma_map_single(dev, phys_to_virt(r.start),
713 					      sizeof(struct rkisp_thunderboot_resmem_head),
714 					      DMA_BIDIRECTIONAL);
715 	ret = dma_mapping_error(dev, isp_dev->resmem_addr);
716 
717 	dev_info(dev, "Allocated reserved memory, paddr: 0x%x\n",
718 		(u32)isp_dev->resmem_pa);
719 	return ret;
720 }
721 
rkisp_plat_probe(struct platform_device * pdev)722 static int rkisp_plat_probe(struct platform_device *pdev)
723 {
724 	struct device *dev = &pdev->dev;
725 	struct v4l2_device *v4l2_dev;
726 	struct rkisp_device *isp_dev;
727 	int i, ret, mult = 1;
728 
729 	sprintf(rkisp_version, "v%02x.%02x.%02x",
730 		RKISP_DRIVER_VERSION >> 16,
731 		(RKISP_DRIVER_VERSION & 0xff00) >> 8,
732 		RKISP_DRIVER_VERSION & 0x00ff);
733 
734 	dev_info(dev, "rkisp driver version: %s\n", rkisp_version);
735 
736 	isp_dev = devm_kzalloc(dev, sizeof(*isp_dev), GFP_KERNEL);
737 	if (!isp_dev)
738 		return -ENOMEM;
739 
740 	dev_set_drvdata(dev, isp_dev);
741 	isp_dev->dev = dev;
742 	ret = rkisp_attach_hw(isp_dev);
743 	if (ret)
744 		return ret;
745 
746 	if (isp_dev->hw_dev->is_unite)
747 		mult = 2;
748 	isp_dev->sw_base_addr = devm_kzalloc(dev, RKISP_ISP_SW_MAX_SIZE * mult, GFP_KERNEL);
749 	if (!isp_dev->sw_base_addr)
750 		return -ENOMEM;
751 
752 	ret = rkisp_vs_irq_parse(dev);
753 	if (ret)
754 		return ret;
755 
756 	sprintf(isp_dev->media_dev.model, "%s%d",
757 		DRIVER_NAME, isp_dev->dev_id);
758 	if (!isp_dev->hw_dev->is_unite)
759 		strscpy(isp_dev->name, dev_name(dev), sizeof(isp_dev->name));
760 	else
761 		strscpy(isp_dev->name, "rkisp-unite", sizeof(isp_dev->name));
762 	strscpy(isp_dev->media_dev.driver_name, isp_dev->name,
763 		sizeof(isp_dev->media_dev.driver_name));
764 
765 	ret = rkisp_get_reserved_mem(isp_dev);
766 	if (ret)
767 		return ret;
768 
769 	mutex_init(&isp_dev->apilock);
770 	mutex_init(&isp_dev->iqlock);
771 	atomic_set(&isp_dev->pipe.power_cnt, 0);
772 	atomic_set(&isp_dev->pipe.stream_cnt, 0);
773 	init_waitqueue_head(&isp_dev->sync_onoff);
774 	isp_dev->pipe.open = rkisp_pipeline_open;
775 	isp_dev->pipe.close = rkisp_pipeline_close;
776 	isp_dev->pipe.set_stream = rkisp_pipeline_set_stream;
777 
778 	if (isp_dev->isp_ver == ISP_V20 || isp_dev->isp_ver == ISP_V21) {
779 		atomic_set(&isp_dev->hdr.refcnt, 0);
780 		for (i = 0; i < HDR_DMA_MAX; i++) {
781 			INIT_LIST_HEAD(&isp_dev->hdr.q_tx[i]);
782 			INIT_LIST_HEAD(&isp_dev->hdr.q_rx[i]);
783 		}
784 	}
785 
786 	isp_dev->media_dev.dev = dev;
787 	isp_dev->media_dev.ops = &rkisp_media_ops;
788 
789 	v4l2_dev = &isp_dev->v4l2_dev;
790 	v4l2_dev->mdev = &isp_dev->media_dev;
791 	strlcpy(v4l2_dev->name, isp_dev->name, sizeof(v4l2_dev->name));
792 	v4l2_ctrl_handler_init(&isp_dev->ctrl_handler, 5);
793 	v4l2_dev->ctrl_handler = &isp_dev->ctrl_handler;
794 
795 	ret = v4l2_device_register(isp_dev->dev, &isp_dev->v4l2_dev);
796 	if (ret < 0) {
797 		v4l2_err(v4l2_dev, "Failed to register v4l2 device:%d\n", ret);
798 		return ret;
799 	}
800 
801 	media_device_init(&isp_dev->media_dev);
802 	ret = media_device_register(&isp_dev->media_dev);
803 	if (ret < 0) {
804 		v4l2_err(v4l2_dev, "Failed to register media device:%d\n", ret);
805 		goto err_unreg_v4l2_dev;
806 	}
807 
808 	/* create & register platefom subdev (from of_node) */
809 	ret = rkisp_register_platform_subdevs(isp_dev);
810 	if (ret < 0)
811 		goto err_unreg_media_dev;
812 
813 	rkisp_wait_line = 0;
814 	of_property_read_u32(dev->of_node, "wait-line", &rkisp_wait_line);
815 
816 	rkisp_proc_init(isp_dev);
817 
818 	mutex_lock(&rkisp_dev_mutex);
819 	list_add_tail(&isp_dev->list, &rkisp_device_list);
820 	mutex_unlock(&rkisp_dev_mutex);
821 
822 	pm_runtime_enable(dev);
823 	return 0;
824 
825 err_unreg_media_dev:
826 	media_device_unregister(&isp_dev->media_dev);
827 err_unreg_v4l2_dev:
828 	v4l2_device_unregister(&isp_dev->v4l2_dev);
829 	return ret;
830 }
831 
rkisp_plat_remove(struct platform_device * pdev)832 static int rkisp_plat_remove(struct platform_device *pdev)
833 {
834 	struct rkisp_device *isp_dev = platform_get_drvdata(pdev);
835 
836 	pm_runtime_disable(&pdev->dev);
837 
838 	rkisp_proc_cleanup(isp_dev);
839 	media_device_unregister(&isp_dev->media_dev);
840 	v4l2_device_unregister(&isp_dev->v4l2_dev);
841 	rkisp_unregister_luma_vdev(&isp_dev->luma_vdev);
842 	rkisp_unregister_params_vdev(&isp_dev->params_vdev);
843 	rkisp_unregister_stats_vdev(&isp_dev->stats_vdev);
844 	rkisp_unregister_dmarx_vdev(isp_dev);
845 	rkisp_unregister_stream_vdevs(isp_dev);
846 	rkisp_unregister_bridge_subdev(isp_dev);
847 	rkisp_unregister_csi_subdev(isp_dev);
848 	rkisp_unregister_isp_subdev(isp_dev);
849 	media_device_cleanup(&isp_dev->media_dev);
850 	return 0;
851 }
852 
rkisp_runtime_suspend(struct device * dev)853 static int __maybe_unused rkisp_runtime_suspend(struct device *dev)
854 {
855 	struct rkisp_device *isp_dev = dev_get_drvdata(dev);
856 	int ret;
857 
858 	mutex_lock(&isp_dev->hw_dev->dev_lock);
859 	ret = pm_runtime_put_sync(isp_dev->hw_dev->dev);
860 	mutex_unlock(&isp_dev->hw_dev->dev_lock);
861 	return (ret > 0) ? 0 : ret;
862 }
863 
rkisp_runtime_resume(struct device * dev)864 static int __maybe_unused rkisp_runtime_resume(struct device *dev)
865 {
866 	struct rkisp_device *isp_dev = dev_get_drvdata(dev);
867 	int ret;
868 
869 	isp_dev->cap_dev.wait_line = rkisp_wait_line;
870 	mutex_lock(&isp_dev->hw_dev->dev_lock);
871 	ret = pm_runtime_get_sync(isp_dev->hw_dev->dev);
872 	mutex_unlock(&isp_dev->hw_dev->dev_lock);
873 	return (ret > 0) ? 0 : ret;
874 }
875 
876 #ifndef MODULE
rkisp_clr_unready_dev(void)877 static int __init rkisp_clr_unready_dev(void)
878 {
879 	__rkisp_clr_unready_dev();
880 
881 	return 0;
882 }
883 late_initcall_sync(rkisp_clr_unready_dev);
884 #endif
885 
886 static const struct dev_pm_ops rkisp_plat_pm_ops = {
887 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
888 				pm_runtime_force_resume)
889 	SET_RUNTIME_PM_OPS(rkisp_runtime_suspend, rkisp_runtime_resume, NULL)
890 };
891 
892 static const struct of_device_id rkisp_plat_of_match[] = {
893 	{
894 		.compatible = "rockchip,rkisp-vir",
895 	}, {
896 		.compatible = "rockchip,rv1126-rkisp-vir",
897 	},
898 	{},
899 };
900 
901 struct platform_driver rkisp_plat_drv = {
902 	.driver = {
903 		   .name = DRIVER_NAME,
904 		   .of_match_table = of_match_ptr(rkisp_plat_of_match),
905 		   .pm = &rkisp_plat_pm_ops,
906 	},
907 	.probe = rkisp_plat_probe,
908 	.remove = rkisp_plat_remove,
909 };
910 
911 MODULE_AUTHOR("Rockchip Camera/ISP team");
912 MODULE_DESCRIPTION("Rockchip ISP platform driver");
913 MODULE_LICENSE("Dual BSD/GPL");
914