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 bool rkisp_irq_dbg;
67 module_param_named(irq_dbg, rkisp_irq_dbg, bool, 0644);
68 MODULE_PARM_DESC(irq_dbg, "rkisp interrupt runtime");
69
70 static bool rkisp_rdbk_auto;
71 module_param_named(rdbk_auto, rkisp_rdbk_auto, bool, 0644);
72 MODULE_PARM_DESC(irq_dbg, "rkisp and vicap auto readback mode");
73
74 static bool rkisp_clk_dbg;
75 module_param_named(clk_dbg, rkisp_clk_dbg, bool, 0644);
76 MODULE_PARM_DESC(clk_dbg, "rkisp clk set by user");
77
78 static char rkisp_version[RKISP_VERNO_LEN];
79 module_param_string(version, rkisp_version, RKISP_VERNO_LEN, 0444);
80 MODULE_PARM_DESC(version, "version number");
81
82 u64 rkisp_debug_reg = 0xFFFFFFFFFLL;
83 module_param_named(debug_reg, rkisp_debug_reg, ullong, 0644);
84 MODULE_PARM_DESC(debug_reg, "rkisp debug register");
85
86 static unsigned int rkisp_wait_line;
87 module_param_named(wait_line, rkisp_wait_line, uint, 0644);
88 MODULE_PARM_DESC(wait_line, "rkisp wait line to buf done early");
89
90 static unsigned int rkisp_wrap_line;
91 module_param_named(wrap_line, rkisp_wrap_line, uint, 0644);
92 MODULE_PARM_DESC(wrap_line, "rkisp wrap line for mpp");
93
94 static DEFINE_MUTEX(rkisp_dev_mutex);
95 static LIST_HEAD(rkisp_device_list);
96
rkisp_set_clk_rate(struct clk * clk,unsigned long rate)97 void rkisp_set_clk_rate(struct clk *clk, unsigned long rate)
98 {
99 if (rkisp_clk_dbg)
100 return;
101
102 clk_set_rate(clk, rate);
103 }
104
__rkisp_clr_unready_dev(void)105 static int __maybe_unused __rkisp_clr_unready_dev(void)
106 {
107 struct rkisp_device *isp_dev;
108
109 mutex_lock(&rkisp_dev_mutex);
110 list_for_each_entry(isp_dev, &rkisp_device_list, list)
111 v4l2_async_notifier_clr_unready_dev(&isp_dev->notifier);
112 mutex_unlock(&rkisp_dev_mutex);
113
114 return 0;
115 }
116
rkisp_clr_unready_dev_param_set(const char * val,const struct kernel_param * kp)117 static int rkisp_clr_unready_dev_param_set(const char *val, const struct kernel_param *kp)
118 {
119 #ifdef MODULE
120 __rkisp_clr_unready_dev();
121 #endif
122
123 return 0;
124 }
125
126 module_param_call(clr_unready_dev, rkisp_clr_unready_dev_param_set, NULL, NULL, 0200);
127 MODULE_PARM_DESC(clr_unready_dev, "clear unready devices");
128
129 /**************************** pipeline operations *****************************/
130
__isp_pipeline_prepare(struct rkisp_pipeline * p,struct media_entity * me)131 static int __isp_pipeline_prepare(struct rkisp_pipeline *p,
132 struct media_entity *me)
133 {
134 struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
135 struct v4l2_subdev *sd;
136 int i;
137
138 p->num_subdevs = 0;
139 memset(p->subdevs, 0, sizeof(p->subdevs));
140
141 if (!(dev->isp_inp & (INP_CSI | INP_DVP | INP_LVDS | INP_CIF)))
142 return 0;
143
144 while (1) {
145 struct media_pad *pad = NULL;
146
147 /* Find remote source pad */
148 for (i = 0; i < me->num_pads; i++) {
149 struct media_pad *spad = &me->pads[i];
150
151 if (!(spad->flags & MEDIA_PAD_FL_SINK))
152 continue;
153 pad = rkisp_media_entity_remote_pad(spad);
154 if (pad)
155 break;
156 }
157
158 if (!pad)
159 break;
160
161 sd = media_entity_to_v4l2_subdev(pad->entity);
162 if (sd != &dev->isp_sdev.sd)
163 p->subdevs[p->num_subdevs++] = sd;
164
165 me = &sd->entity;
166 if (me->num_pads == 1)
167 break;
168 }
169 if (!p->num_subdevs)
170 return -EINVAL;
171
172 return 0;
173 }
174
__isp_pipeline_s_isp_clk(struct rkisp_pipeline * p)175 static int __isp_pipeline_s_isp_clk(struct rkisp_pipeline *p)
176 {
177 struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
178 struct rkisp_hw_dev *hw_dev = dev->hw_dev;
179 struct v4l2_subdev *sd;
180 struct v4l2_ctrl *ctrl;
181 u64 data_rate = 0;
182 int i, fps;
183
184 hw_dev->isp_size[dev->dev_id].is_on = true;
185 if (hw_dev->is_runing) {
186 if (dev->isp_ver >= ISP_V30 && !rkisp_clk_dbg)
187 hw_dev->is_dvfs = true;
188 return 0;
189 }
190
191 if (dev->isp_inp & (INP_RAWRD0 | INP_RAWRD1 | INP_RAWRD2) ||
192 (dev->is_pre_on && hw_dev->dev_num > 1)) {
193 if (dev->isp_ver < ISP_V30 || dev->is_pre_on) {
194 /* isp with mipi no support dvfs, calculate max data rate */
195 for (i = 0; i < hw_dev->dev_num; i++) {
196 fps = hw_dev->isp_size[i].fps;
197 if (!fps)
198 fps = 30;
199 data_rate += (fps * hw_dev->isp_size[i].size);
200 }
201 } else {
202 i = dev->dev_id;
203 fps = hw_dev->isp_size[i].fps;
204 if (!fps)
205 fps = 30;
206 data_rate = fps * hw_dev->isp_size[i].size;
207 }
208 goto end;
209 }
210
211 if (dev->isp_inp == INP_DMARX_ISP && dev->hw_dev->clks[0]) {
212 rkisp_set_clk_rate(hw_dev->clks[0], 400 * 1000000UL);
213 return 0;
214 }
215
216 /* find the subdev of active sensor or vicap itf */
217 sd = p->subdevs[0];
218 for (i = 0; i < p->num_subdevs; i++) {
219 sd = p->subdevs[i];
220 if (sd->entity.function == MEDIA_ENT_F_CAM_SENSOR ||
221 sd->entity.function == MEDIA_ENT_F_PROC_VIDEO_COMPOSER)
222 break;
223 }
224
225 if (i == p->num_subdevs) {
226 v4l2_warn(&dev->v4l2_dev, "No active sensor\n");
227 return -EPIPE;
228 }
229
230 ctrl = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_PIXEL_RATE);
231 if (!ctrl) {
232 v4l2_warn(&dev->v4l2_dev, "No pixel rate control in subdev\n");
233 return -EPIPE;
234 }
235
236 /* calculate data rate */
237 data_rate = v4l2_ctrl_g_ctrl_int64(ctrl) *
238 dev->isp_sdev.in_fmt.bus_width;
239 data_rate >>= 3;
240 end:
241 do_div(data_rate, 1000 * 1000);
242
243 /* increase 25% margin */
244 data_rate += data_rate >> 2;
245
246 /* compare with isp clock adjustment table */
247 for (i = 0; i < hw_dev->num_clk_rate_tbl; i++)
248 if (data_rate <= hw_dev->clk_rate_tbl[i].clk_rate)
249 break;
250 if (i == hw_dev->num_clk_rate_tbl)
251 i--;
252
253 /* set isp clock rate */
254 rkisp_set_clk_rate(hw_dev->clks[0], hw_dev->clk_rate_tbl[i].clk_rate * 1000000UL);
255 if (hw_dev->is_unite)
256 rkisp_set_clk_rate(hw_dev->clks[5], hw_dev->clk_rate_tbl[i].clk_rate * 1000000UL);
257 /* aclk equal to core clk */
258 if (dev->isp_ver == ISP_V32)
259 rkisp_set_clk_rate(hw_dev->clks[1], hw_dev->clk_rate_tbl[i].clk_rate * 1000000UL);
260 dev_info(hw_dev->dev, "set isp clk = %luHz\n", clk_get_rate(hw_dev->clks[0]));
261
262 return 0;
263 }
264
rkisp_pipeline_open(struct rkisp_pipeline * p,struct media_entity * me,bool prepare)265 static int rkisp_pipeline_open(struct rkisp_pipeline *p,
266 struct media_entity *me,
267 bool prepare)
268 {
269 int ret;
270 struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
271
272 if (WARN_ON(!p || !me))
273 return -EINVAL;
274 if (atomic_inc_return(&p->power_cnt) > 1)
275 return 0;
276
277 /* go through media graphic and get subdevs */
278 if (prepare) {
279 ret = __isp_pipeline_prepare(p, me);
280 if (ret < 0)
281 return ret;
282 }
283
284 ret = __isp_pipeline_s_isp_clk(p);
285 if (ret < 0)
286 return ret;
287
288 if (dev->isp_inp & (INP_CSI | INP_RAWRD0 | INP_RAWRD1 | INP_RAWRD2 | INP_CIF))
289 rkisp_csi_config_patch(dev);
290 return 0;
291 }
292
rkisp_pipeline_close(struct rkisp_pipeline * p)293 static int rkisp_pipeline_close(struct rkisp_pipeline *p)
294 {
295 struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
296
297 if (atomic_dec_return(&p->power_cnt))
298 return 0;
299
300 rkisp_rx_buf_pool_free(dev);
301 dev->hw_dev->isp_size[dev->dev_id].is_on = false;
302 if (dev->hw_dev->is_runing && (dev->isp_ver >= ISP_V30) && !rkisp_clk_dbg)
303 dev->hw_dev->is_dvfs = true;
304 return 0;
305 }
306
307 /*
308 * stream-on order: isp_subdev, mipi dphy, sensor
309 * stream-off order: mipi dphy, sensor, isp_subdev
310 */
rkisp_pipeline_set_stream(struct rkisp_pipeline * p,bool on)311 static int rkisp_pipeline_set_stream(struct rkisp_pipeline *p, bool on)
312 {
313 struct rkisp_device *dev = container_of(p, struct rkisp_device, pipe);
314 int i, ret;
315
316 if ((on && atomic_inc_return(&p->stream_cnt) > 1) ||
317 (!on && atomic_dec_return(&p->stream_cnt) > 0))
318 return 0;
319
320 if (on) {
321 if (dev->vs_irq >= 0)
322 enable_irq(dev->vs_irq);
323 rockchip_set_system_status(SYS_STATUS_ISP);
324 ret = v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, true);
325 if (ret < 0)
326 goto err;
327 /* phy -> sensor */
328 for (i = 0; i < p->num_subdevs; ++i) {
329 if ((dev->vicap_in.merge_num > 1) &&
330 (p->subdevs[i]->entity.function == MEDIA_ENT_F_CAM_SENSOR))
331 continue;
332 ret = v4l2_subdev_call(p->subdevs[i], video, s_stream, on);
333 if (on && ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
334 goto err_stream_off;
335 }
336 } else {
337 if (dev->hw_dev->monitor.is_en) {
338 dev->hw_dev->monitor.is_en = 0;
339 dev->hw_dev->monitor.state = ISP_STOP;
340 if (!completion_done(&dev->hw_dev->monitor.cmpl))
341 complete(&dev->hw_dev->monitor.cmpl);
342 }
343 /* sensor -> phy */
344 for (i = p->num_subdevs - 1; i >= 0; --i) {
345 if ((dev->vicap_in.merge_num > 1) &&
346 (p->subdevs[i]->entity.function == MEDIA_ENT_F_CAM_SENSOR))
347 continue;
348 v4l2_subdev_call(p->subdevs[i], video, s_stream, on);
349 }
350 if (dev->vs_irq >= 0)
351 disable_irq(dev->vs_irq);
352 v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, false);
353 rockchip_clear_system_status(SYS_STATUS_ISP);
354 }
355
356 return 0;
357
358 err_stream_off:
359 for (--i; i >= 0; --i)
360 v4l2_subdev_call(p->subdevs[i], video, s_stream, false);
361 v4l2_subdev_call(&dev->isp_sdev.sd, video, s_stream, false);
362 err:
363 rockchip_clear_system_status(SYS_STATUS_ISP);
364 atomic_dec_return(&p->stream_cnt);
365 return ret;
366 }
367
368 /***************************** media controller *******************************/
369 /* See http://opensource.rock-chips.com/wiki_Rockchip-isp1 for Topology */
370
rkisp_create_links(struct rkisp_device * dev)371 static int rkisp_create_links(struct rkisp_device *dev)
372 {
373 unsigned int s, pad;
374 int ret = 0;
375
376 /* sensor links(or mipi-phy) */
377 for (s = 0; s < dev->num_sensors; ++s) {
378 struct rkisp_sensor_info *sensor = &dev->sensors[s];
379 u32 type = sensor->sd->entity.function;
380 bool en = s ? 0 : true;
381
382 for (pad = 0; pad < sensor->sd->entity.num_pads; pad++)
383 if (sensor->sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)
384 break;
385
386 if (pad == sensor->sd->entity.num_pads) {
387 dev_err(dev->dev, "failed to find src pad for %s\n",
388 sensor->sd->name);
389 return -ENXIO;
390 }
391
392 /* sensor link -> isp */
393 if (type == MEDIA_ENT_F_CAM_SENSOR) {
394 dev->isp_inp = INP_DVP;
395 ret = media_create_pad_link(&sensor->sd->entity, pad,
396 &dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
397 } else if (type == MEDIA_ENT_F_PROC_VIDEO_COMPOSER) {
398 dev->isp_inp = INP_CIF;
399 ret = media_create_pad_link(&sensor->sd->entity, pad,
400 &dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
401 } else {
402 v4l2_subdev_call(sensor->sd, pad,
403 get_mbus_config, 0, &sensor->mbus);
404 if (sensor->mbus.type == V4L2_MBUS_CCP2) {
405 /* mipi-phy lvds link -> isp */
406 dev->isp_inp = INP_LVDS;
407 ret = media_create_pad_link(&sensor->sd->entity, pad,
408 &dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
409 } else {
410 /* mipi-phy link -> csi -> isp */
411 dev->isp_inp = INP_CSI;
412 ret = media_create_pad_link(&sensor->sd->entity,
413 pad, &dev->csi_dev.sd.entity, CSI_SINK, en);
414 ret |= media_create_pad_link(&dev->csi_dev.sd.entity, CSI_SRC_CH0,
415 &dev->isp_sdev.sd.entity, RKISP_ISP_PAD_SINK, en);
416 dev->csi_dev.sink[0].linked = en;
417 dev->csi_dev.sink[0].index = BIT(0);
418 }
419 }
420 if (ret)
421 dev_err(dev->dev, "failed to create link for %s\n", sensor->sd->name);
422 }
423 return ret;
424 }
425
_set_pipeline_default_fmt(struct rkisp_device * dev,bool is_init)426 static int _set_pipeline_default_fmt(struct rkisp_device *dev, bool is_init)
427 {
428 struct v4l2_subdev *isp;
429 struct v4l2_subdev_format fmt;
430 struct v4l2_subdev_selection sel;
431 u32 i, width, height, code;
432
433 memset(&sel, 0, sizeof(sel));
434 memset(&fmt, 0, sizeof(fmt));
435 isp = &dev->isp_sdev.sd;
436
437 if (dev->active_sensor) {
438 fmt = dev->active_sensor->fmt[0];
439 if (!is_init &&
440 fmt.format.code == dev->isp_sdev.in_frm.code &&
441 fmt.format.width == dev->isp_sdev.in_frm.width &&
442 fmt.format.height == dev->isp_sdev.in_frm.height)
443 return 0;
444 } else {
445 fmt.format = dev->isp_sdev.in_frm;
446 }
447 code = fmt.format.code;
448 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
449 fmt.pad = RKISP_ISP_PAD_SINK;
450 /* isp input format information from sensor */
451 v4l2_subdev_call(isp, pad, set_fmt, NULL, &fmt);
452
453 rkisp_align_sensor_resolution(dev, &sel.r, false);
454 width = sel.r.width;
455 height = sel.r.height;
456 sel.target = V4L2_SEL_TGT_CROP;
457 sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
458 sel.pad = RKISP_ISP_PAD_SINK;
459 /* image resolution processed by isp */
460 v4l2_subdev_call(isp, pad, set_selection, NULL, &sel);
461
462 /* change fmt&size for RKISP_ISP_PAD_SOURCE_PATH */
463 if ((code & RKISP_MEDIA_BUS_FMT_MASK) == RKISP_MEDIA_BUS_FMT_BAYER)
464 fmt.format.code = MEDIA_BUS_FMT_YUYV8_2X8;
465
466 sel.r.left = 0;
467 sel.r.top = 0;
468 fmt.format.width = width;
469 fmt.format.height = height;
470 fmt.pad = RKISP_ISP_PAD_SOURCE_PATH;
471 sel.pad = RKISP_ISP_PAD_SOURCE_PATH;
472 v4l2_subdev_call(isp, pad, set_fmt, NULL, &fmt);
473 v4l2_subdev_call(isp, pad, set_selection, NULL, &sel);
474
475 /* change fmt&size of MP/SP */
476 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_MP,
477 width, height, V4L2_PIX_FMT_NV12);
478 if (dev->isp_ver != ISP_V10_1)
479 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_SP,
480 width, height, V4L2_PIX_FMT_NV12);
481 if ((dev->isp_ver == ISP_V20 || dev->isp_ver == ISP_V21) &&
482 dev->isp_inp == INP_CSI && dev->active_sensor) {
483 width = dev->active_sensor->fmt[1].format.width;
484 height = dev->active_sensor->fmt[1].format.height;
485 code = dev->active_sensor->fmt[1].format.code;
486 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX0,
487 width, height, rkisp_mbus_pixelcode_to_v4l2(code));
488
489 width = dev->active_sensor->fmt[3].format.width;
490 height = dev->active_sensor->fmt[3].format.height;
491 code = dev->active_sensor->fmt[3].format.code;
492 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX2,
493 width, height, rkisp_mbus_pixelcode_to_v4l2(code));
494
495 width = dev->active_sensor->fmt[4].format.width;
496 height = dev->active_sensor->fmt[4].format.height;
497 code = dev->active_sensor->fmt[4].format.code;
498 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX3,
499 width, height, rkisp_mbus_pixelcode_to_v4l2(code));
500 }
501
502 if (dev->isp_ver == ISP_V20 &&
503 dev->isp_inp == INP_CSI && dev->active_sensor) {
504 width = dev->active_sensor->fmt[2].format.width;
505 height = dev->active_sensor->fmt[2].format.height;
506 code = dev->active_sensor->fmt[2].format.code;
507 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_DMATX1,
508 width, height, rkisp_mbus_pixelcode_to_v4l2(code));
509 }
510
511 if (dev->isp_ver == ISP_V30) {
512 struct v4l2_pix_format_mplane pixm = {
513 .width = width,
514 .height = height,
515 .pixelformat = rkisp_mbus_pixelcode_to_v4l2(code),
516 };
517
518 for (i = RKISP_STREAM_RAWRD0; i <= RKISP_STREAM_RAWRD2; i++)
519 rkisp_dmarx_set_fmt(&dev->dmarx_dev.stream[i], pixm);
520 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_FBC,
521 width, height, V4L2_PIX_FMT_FBC0);
522 #ifdef RKISP_STREAM_BP_EN
523 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_BP,
524 width, height, V4L2_PIX_FMT_NV12);
525 #endif
526 }
527
528 if (dev->isp_ver == ISP_V32 || dev->isp_ver == ISP_V32_L) {
529 struct v4l2_pix_format_mplane pixm = {
530 .width = width,
531 .height = height,
532 .pixelformat = rkisp_mbus_pixelcode_to_v4l2(code),
533 };
534
535 rkisp_dmarx_set_fmt(&dev->dmarx_dev.stream[RKISP_STREAM_RAWRD0], pixm);
536 rkisp_dmarx_set_fmt(&dev->dmarx_dev.stream[RKISP_STREAM_RAWRD2], pixm);
537 if (dev->isp_ver == ISP_V32) {
538 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_BP,
539 width, height, V4L2_PIX_FMT_NV12);
540 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_MPDS,
541 width / 4, height / 4, V4L2_PIX_FMT_NV12);
542 rkisp_set_stream_def_fmt(dev, RKISP_STREAM_BPDS,
543 width / 4, height / 4, V4L2_PIX_FMT_NV12);
544 }
545 }
546 return 0;
547 }
548
subdev_notifier_complete(struct v4l2_async_notifier * notifier)549 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
550 {
551 struct rkisp_device *dev;
552 int ret;
553
554 dev = container_of(notifier, struct rkisp_device, notifier);
555
556 mutex_lock(&dev->media_dev.graph_mutex);
557 ret = rkisp_create_links(dev);
558 if (ret < 0)
559 goto unlock;
560 ret = v4l2_device_register_subdev_nodes(&dev->v4l2_dev);
561 if (ret < 0)
562 goto unlock;
563
564 if (dev->isp_inp) {
565 ret = rkisp_update_sensor_info(dev);
566 if (ret < 0) {
567 v4l2_err(&dev->v4l2_dev, "update sensor failed\n");
568 goto unlock;
569 }
570 dev->is_hw_link = true;
571 }
572
573 ret = _set_pipeline_default_fmt(dev, true);
574 if (ret < 0)
575 goto unlock;
576
577 v4l2_info(&dev->v4l2_dev, "Async subdev notifier completed\n");
578
579 unlock:
580 mutex_unlock(&dev->media_dev.graph_mutex);
581 if (!ret && dev->is_thunderboot)
582 schedule_work(&dev->cap_dev.fast_work);
583 return ret;
584 }
585
586 struct rkisp_async_subdev {
587 struct v4l2_async_subdev asd;
588 struct v4l2_mbus_config mbus;
589 };
590
subdev_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)591 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
592 struct v4l2_subdev *subdev,
593 struct v4l2_async_subdev *asd)
594 {
595 struct rkisp_device *isp_dev = container_of(notifier,
596 struct rkisp_device, notifier);
597 struct rkisp_async_subdev *s_asd = container_of(asd,
598 struct rkisp_async_subdev, asd);
599
600 if (isp_dev->num_sensors == ARRAY_SIZE(isp_dev->sensors))
601 return -EBUSY;
602
603 isp_dev->sensors[isp_dev->num_sensors].mbus = s_asd->mbus;
604 isp_dev->sensors[isp_dev->num_sensors].sd = subdev;
605 ++isp_dev->num_sensors;
606
607 v4l2_dbg(1, rkisp_debug, subdev, "Async registered subdev\n");
608
609 return 0;
610 }
611
rkisp_fwnode_parse(struct device * dev,struct v4l2_fwnode_endpoint * vep,struct v4l2_async_subdev * asd)612 static int rkisp_fwnode_parse(struct device *dev,
613 struct v4l2_fwnode_endpoint *vep,
614 struct v4l2_async_subdev *asd)
615 {
616 struct rkisp_async_subdev *rk_asd =
617 container_of(asd, struct rkisp_async_subdev, asd);
618 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
619
620 /*
621 * MIPI sensor is linked with a mipi dphy and its media bus config can
622 * not be get in here
623 */
624 if (vep->bus_type != V4L2_MBUS_BT656 &&
625 vep->bus_type != V4L2_MBUS_PARALLEL)
626 return 0;
627
628 rk_asd->mbus.flags = bus->flags;
629 rk_asd->mbus.type = vep->bus_type;
630
631 return 0;
632 }
633
subdev_notifier_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)634 static void subdev_notifier_unbind(struct v4l2_async_notifier *notifier,
635 struct v4l2_subdev *subdev,
636 struct v4l2_async_subdev *asd)
637 {
638 struct rkisp_device *isp_dev = container_of(notifier, struct rkisp_device, notifier);
639 struct rkisp_isp_subdev *isp_sdev = &isp_dev->isp_sdev;
640 struct v4l2_subdev *isp_sd = &isp_sdev->sd;
641 int i;
642
643 for (i = 0; i < isp_dev->num_sensors; i++) {
644 if (isp_dev->sensors[i].sd == subdev) {
645 media_entity_call(&isp_sd->entity, link_setup,
646 isp_sd->entity.pads, subdev->entity.pads, 0);
647 isp_dev->sensors[i].sd = NULL;
648 }
649 }
650 }
651
652 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
653 .bound = subdev_notifier_bound,
654 .complete = subdev_notifier_complete,
655 .unbind = subdev_notifier_unbind,
656 };
657
isp_subdev_notifier(struct rkisp_device * isp_dev)658 static int isp_subdev_notifier(struct rkisp_device *isp_dev)
659 {
660 struct v4l2_async_notifier *ntf = &isp_dev->notifier;
661 struct device *dev = isp_dev->dev;
662 int ret;
663
664 v4l2_async_notifier_init(ntf);
665
666 ret = v4l2_async_notifier_parse_fwnode_endpoints(
667 dev, ntf, sizeof(struct rkisp_async_subdev),
668 rkisp_fwnode_parse);
669 if (ret < 0)
670 return ret;
671
672 ntf->ops = &subdev_notifier_ops;
673
674 return v4l2_async_notifier_register(&isp_dev->v4l2_dev, ntf);
675 }
676
677 /***************************** platform deive *******************************/
678
rkisp_register_platform_subdevs(struct rkisp_device * dev)679 static int rkisp_register_platform_subdevs(struct rkisp_device *dev)
680 {
681 int ret;
682
683 ret = rkisp_register_isp_subdev(dev, &dev->v4l2_dev);
684 if (ret < 0)
685 return ret;
686
687 ret = rkisp_register_csi_subdev(dev, &dev->v4l2_dev);
688 if (ret < 0)
689 goto err_unreg_isp_subdev;
690
691 ret = rkisp_register_bridge_subdev(dev, &dev->v4l2_dev);
692 if (ret < 0)
693 goto err_unreg_csi_subdev;
694
695 ret = rkisp_register_stream_vdevs(dev);
696 if (ret < 0)
697 goto err_unreg_bridge_subdev;
698
699 ret = rkisp_register_dmarx_vdev(dev);
700 if (ret < 0)
701 goto err_unreg_stream_vdev;
702
703 ret = rkisp_register_stats_vdev(&dev->stats_vdev, &dev->v4l2_dev, dev);
704 if (ret < 0)
705 goto err_unreg_dmarx_vdev;
706
707 ret = rkisp_register_params_vdev(&dev->params_vdev, &dev->v4l2_dev, dev);
708 if (ret < 0)
709 goto err_unreg_stats_vdev;
710
711 ret = rkisp_register_luma_vdev(&dev->luma_vdev, &dev->v4l2_dev, dev);
712 if (ret < 0)
713 goto err_unreg_params_vdev;
714
715 ret = isp_subdev_notifier(dev);
716 if (ret < 0) {
717 v4l2_err(&dev->v4l2_dev,
718 "Failed to register subdev notifier(%d)\n", ret);
719 goto err_unreg_luma_vdev;
720 }
721
722 return 0;
723 err_unreg_luma_vdev:
724 rkisp_unregister_luma_vdev(&dev->luma_vdev);
725 err_unreg_params_vdev:
726 rkisp_unregister_params_vdev(&dev->params_vdev);
727 err_unreg_stats_vdev:
728 rkisp_unregister_stats_vdev(&dev->stats_vdev);
729 err_unreg_dmarx_vdev:
730 rkisp_unregister_dmarx_vdev(dev);
731 err_unreg_stream_vdev:
732 rkisp_unregister_stream_vdevs(dev);
733 err_unreg_bridge_subdev:
734 rkisp_unregister_bridge_subdev(dev);
735 err_unreg_csi_subdev:
736 rkisp_unregister_csi_subdev(dev);
737 err_unreg_isp_subdev:
738 rkisp_unregister_isp_subdev(dev);
739
740 return ret;
741 }
742
rkisp_vs_irq_parse(struct device * dev)743 static int rkisp_vs_irq_parse(struct device *dev)
744 {
745 int ret;
746 int vs_irq;
747 unsigned long vs_irq_flags;
748 struct gpio_desc *vs_irq_gpio;
749 struct rkisp_device *isp_dev = dev_get_drvdata(dev);
750
751 /* this irq recevice the message of sensor vs from preisp */
752 isp_dev->vs_irq = -1;
753 vs_irq_gpio = devm_gpiod_get(dev, "vsirq", GPIOD_IN);
754 if (!IS_ERR(vs_irq_gpio)) {
755 vs_irq_flags = IRQF_TRIGGER_RISING |
756 IRQF_ONESHOT | IRQF_SHARED;
757
758 vs_irq = gpiod_to_irq(vs_irq_gpio);
759 if (vs_irq < 0) {
760 dev_err(dev, "GPIO to interrupt failed\n");
761 return vs_irq;
762 }
763
764 dev_info(dev, "register_irq: %d\n", vs_irq);
765 ret = devm_request_irq(dev,
766 vs_irq,
767 rkisp_vs_isr_handler,
768 vs_irq_flags,
769 "vs_irq_gpio_int",
770 dev);
771 if (ret) {
772 dev_err(dev, "devm_request_irq failed: %d\n", ret);
773 return ret;
774 } else {
775 disable_irq(vs_irq);
776 isp_dev->vs_irq = vs_irq;
777 isp_dev->vs_irq_gpio = vs_irq_gpio;
778 dev_info(dev, "vs_gpio_int interrupt is hooked\n");
779 }
780 }
781
782 return 0;
783 }
784
785 static const struct media_device_ops rkisp_media_ops = {
786 .link_notify = v4l2_pipeline_link_notify,
787 };
788
rkisp_get_reserved_mem(struct rkisp_device * isp_dev)789 static int rkisp_get_reserved_mem(struct rkisp_device *isp_dev)
790 {
791 struct device *dev = isp_dev->dev;
792 struct device_node *np;
793 struct resource r;
794 int ret;
795
796 /* Get reserved memory region from Device-tree */
797 np = of_parse_phandle(dev->of_node, "memory-region-thunderboot", 0);
798 if (!np) {
799 dev_info(dev, "No memory-region-thunderboot specified\n");
800 return 0;
801 }
802
803 ret = of_address_to_resource(np, 0, &r);
804 if (ret) {
805 dev_err(dev, "No memory address assigned to the region\n");
806 return ret;
807 }
808
809 isp_dev->resmem_pa = r.start;
810 isp_dev->resmem_size = resource_size(&r);
811 isp_dev->resmem_addr = dma_map_single(dev, phys_to_virt(r.start),
812 sizeof(struct rkisp_thunderboot_resmem_head),
813 DMA_BIDIRECTIONAL);
814 ret = dma_mapping_error(dev, isp_dev->resmem_addr);
815 isp_dev->is_thunderboot = true;
816 dev_info(dev, "Allocated reserved memory, paddr: 0x%x\n", (u32)isp_dev->resmem_pa);
817 return ret;
818 }
819
rkisp_plat_probe(struct platform_device * pdev)820 static int rkisp_plat_probe(struct platform_device *pdev)
821 {
822 struct device *dev = &pdev->dev;
823 struct v4l2_device *v4l2_dev;
824 struct rkisp_device *isp_dev;
825 int i, ret, mult = 1;
826
827 snprintf(rkisp_version, sizeof(rkisp_version),
828 "v%02x.%02x.%02x",
829 RKISP_DRIVER_VERSION >> 16,
830 (RKISP_DRIVER_VERSION & 0xff00) >> 8,
831 RKISP_DRIVER_VERSION & 0x00ff);
832
833 dev_info(dev, "rkisp driver version: %s\n", rkisp_version);
834
835 isp_dev = devm_kzalloc(dev, sizeof(*isp_dev), GFP_KERNEL);
836 if (!isp_dev)
837 return -ENOMEM;
838
839 dev_set_drvdata(dev, isp_dev);
840 isp_dev->dev = dev;
841 ret = rkisp_attach_hw(isp_dev);
842 if (ret)
843 return ret;
844
845 if (isp_dev->hw_dev->is_unite)
846 mult = 2;
847 isp_dev->sw_base_addr = devm_kzalloc(dev, RKISP_ISP_SW_MAX_SIZE * mult, GFP_KERNEL);
848 if (!isp_dev->sw_base_addr)
849 return -ENOMEM;
850
851 ret = rkisp_vs_irq_parse(dev);
852 if (ret)
853 return ret;
854
855 snprintf(isp_dev->media_dev.model, sizeof(isp_dev->media_dev.model),
856 "%s%d", DRIVER_NAME, isp_dev->dev_id);
857 if (!isp_dev->hw_dev->is_unite)
858 strscpy(isp_dev->name, dev_name(dev), sizeof(isp_dev->name));
859 else
860 snprintf(isp_dev->name, sizeof(isp_dev->name),
861 "%s%d", "rkisp-unite", isp_dev->dev_id);
862 strscpy(isp_dev->media_dev.driver_name, isp_dev->name,
863 sizeof(isp_dev->media_dev.driver_name));
864
865 ret = rkisp_get_reserved_mem(isp_dev);
866 if (ret)
867 return ret;
868
869 mutex_init(&isp_dev->apilock);
870 mutex_init(&isp_dev->iqlock);
871 atomic_set(&isp_dev->pipe.power_cnt, 0);
872 atomic_set(&isp_dev->pipe.stream_cnt, 0);
873 init_waitqueue_head(&isp_dev->sync_onoff);
874 isp_dev->pipe.open = rkisp_pipeline_open;
875 isp_dev->pipe.close = rkisp_pipeline_close;
876 isp_dev->pipe.set_stream = rkisp_pipeline_set_stream;
877
878 if (isp_dev->isp_ver == ISP_V20 || isp_dev->isp_ver == ISP_V21) {
879 atomic_set(&isp_dev->hdr.refcnt, 0);
880 for (i = 0; i < HDR_DMA_MAX; i++) {
881 INIT_LIST_HEAD(&isp_dev->hdr.q_tx[i]);
882 INIT_LIST_HEAD(&isp_dev->hdr.q_rx[i]);
883 }
884 }
885
886 isp_dev->media_dev.dev = dev;
887 isp_dev->media_dev.ops = &rkisp_media_ops;
888
889 v4l2_dev = &isp_dev->v4l2_dev;
890 v4l2_dev->mdev = &isp_dev->media_dev;
891 strlcpy(v4l2_dev->name, isp_dev->name, sizeof(v4l2_dev->name));
892 v4l2_ctrl_handler_init(&isp_dev->ctrl_handler, 5);
893 v4l2_dev->ctrl_handler = &isp_dev->ctrl_handler;
894
895 ret = v4l2_device_register(isp_dev->dev, &isp_dev->v4l2_dev);
896 if (ret < 0) {
897 v4l2_err(v4l2_dev, "Failed to register v4l2 device:%d\n", ret);
898 return ret;
899 }
900
901 media_device_init(&isp_dev->media_dev);
902 ret = media_device_register(&isp_dev->media_dev);
903 if (ret < 0) {
904 v4l2_err(v4l2_dev, "Failed to register media device:%d\n", ret);
905 goto err_unreg_v4l2_dev;
906 }
907
908 pm_runtime_enable(dev);
909 /* create & register platefom subdev (from of_node) */
910 ret = rkisp_register_platform_subdevs(isp_dev);
911 if (ret < 0) {
912 v4l2_err(v4l2_dev, "Failed to register platform subdevs:%d\n", ret);
913 goto err_unreg_media_dev;
914 }
915 rkisp_wait_line = 0;
916 of_property_read_u32(dev->of_node, "wait-line", &rkisp_wait_line);
917
918 rkisp_proc_init(isp_dev);
919
920 mutex_lock(&rkisp_dev_mutex);
921 list_add_tail(&isp_dev->list, &rkisp_device_list);
922 mutex_unlock(&rkisp_dev_mutex);
923 isp_dev->is_probe_end = true;
924 return 0;
925
926 err_unreg_media_dev:
927 media_device_unregister(&isp_dev->media_dev);
928 err_unreg_v4l2_dev:
929 v4l2_device_unregister(&isp_dev->v4l2_dev);
930 return ret;
931 }
932
rkisp_plat_remove(struct platform_device * pdev)933 static int rkisp_plat_remove(struct platform_device *pdev)
934 {
935 struct rkisp_device *isp_dev = platform_get_drvdata(pdev);
936
937 isp_dev->is_hw_link = false;
938 isp_dev->hw_dev->isp[isp_dev->dev_id] = NULL;
939
940 pm_runtime_disable(&pdev->dev);
941
942 rkisp_proc_cleanup(isp_dev);
943 media_device_unregister(&isp_dev->media_dev);
944 v4l2_async_notifier_unregister(&isp_dev->notifier);
945 v4l2_async_notifier_cleanup(&isp_dev->notifier);
946 v4l2_device_unregister(&isp_dev->v4l2_dev);
947 v4l2_ctrl_handler_free(&isp_dev->ctrl_handler);
948 rkisp_unregister_luma_vdev(&isp_dev->luma_vdev);
949 rkisp_unregister_params_vdev(&isp_dev->params_vdev);
950 rkisp_unregister_stats_vdev(&isp_dev->stats_vdev);
951 rkisp_unregister_dmarx_vdev(isp_dev);
952 rkisp_unregister_stream_vdevs(isp_dev);
953 rkisp_unregister_bridge_subdev(isp_dev);
954 rkisp_unregister_csi_subdev(isp_dev);
955 rkisp_unregister_isp_subdev(isp_dev);
956 media_device_cleanup(&isp_dev->media_dev);
957 return 0;
958 }
959
rkisp_runtime_suspend(struct device * dev)960 static int __maybe_unused rkisp_runtime_suspend(struct device *dev)
961 {
962 struct rkisp_device *isp_dev = dev_get_drvdata(dev);
963 int ret;
964
965 mutex_lock(&isp_dev->hw_dev->dev_lock);
966 ret = pm_runtime_put_sync(isp_dev->hw_dev->dev);
967 mutex_unlock(&isp_dev->hw_dev->dev_lock);
968 return (ret > 0) ? 0 : ret;
969 }
970
rkisp_runtime_resume(struct device * dev)971 static int __maybe_unused rkisp_runtime_resume(struct device *dev)
972 {
973 struct rkisp_device *isp_dev = dev_get_drvdata(dev);
974 int ret;
975
976 /* power on to config default format from sensor */
977 if (isp_dev->isp_inp & (INP_CSI | INP_DVP | INP_LVDS | INP_CIF) &&
978 rkisp_update_sensor_info(isp_dev) >= 0)
979 _set_pipeline_default_fmt(isp_dev, false);
980
981 if (isp_dev->hw_dev->is_assigned_clk)
982 rkisp_clk_dbg = true;
983 isp_dev->cap_dev.wait_line = rkisp_wait_line;
984 isp_dev->cap_dev.wrap_line = rkisp_wrap_line;
985 isp_dev->is_rdbk_auto = rkisp_rdbk_auto;
986 mutex_lock(&isp_dev->hw_dev->dev_lock);
987 ret = pm_runtime_get_sync(isp_dev->hw_dev->dev);
988 mutex_unlock(&isp_dev->hw_dev->dev_lock);
989 return (ret > 0) ? 0 : ret;
990 }
991
992 #ifndef MODULE
rkisp_clr_unready_dev(void)993 static int __init rkisp_clr_unready_dev(void)
994 {
995 __rkisp_clr_unready_dev();
996
997 return 0;
998 }
999 late_initcall_sync(rkisp_clr_unready_dev);
1000 #endif
1001
1002 static const struct dev_pm_ops rkisp_plat_pm_ops = {
1003 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1004 pm_runtime_force_resume)
1005 SET_RUNTIME_PM_OPS(rkisp_runtime_suspend, rkisp_runtime_resume, NULL)
1006 };
1007
1008 static const struct of_device_id rkisp_plat_of_match[] = {
1009 {
1010 .compatible = "rockchip,rkisp-vir",
1011 }, {
1012 .compatible = "rockchip,rv1126-rkisp-vir",
1013 },
1014 {},
1015 };
1016
1017 struct platform_driver rkisp_plat_drv = {
1018 .driver = {
1019 .name = DRIVER_NAME,
1020 .of_match_table = of_match_ptr(rkisp_plat_of_match),
1021 .pm = &rkisp_plat_pm_ops,
1022 },
1023 .probe = rkisp_plat_probe,
1024 .remove = rkisp_plat_remove,
1025 };
1026
1027 MODULE_AUTHOR("Rockchip Camera/ISP team");
1028 MODULE_DESCRIPTION("Rockchip ISP platform driver");
1029 MODULE_LICENSE("Dual BSD/GPL");
1030 MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
1031