• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 Rockchip Electronics Co., Ltd */
3 
4 #include <linux/clk.h>
5 #include <linux/delay.h>
6 #include <linux/interrupt.h>
7 #include <linux/io.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_graph.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_reserved_mem.h>
13 #include <linux/pinctrl/consumer.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/reset.h>
16 #include <linux/regmap.h>
17 #include <media/videobuf2-dma-contig.h>
18 #include <media/v4l2-fwnode.h>
19 
20 #include "dev.h"
21 #include "regs.h"
22 #include "version.h"
23 
24 #define RKISPP_VERNO_LEN 10
25 
26 int rkispp_debug;
27 module_param_named(debug, rkispp_debug, int, 0644);
28 MODULE_PARM_DESC(debug, "Debug level (0-3)");
29 
30 static bool rkispp_clk_dbg;
31 module_param_named(clk_dbg, rkispp_clk_dbg, bool, 0644);
32 MODULE_PARM_DESC(clk_dbg, "rkispp clk set by user");
33 
34 bool rkispp_monitor;
35 module_param_named(monitor, rkispp_monitor, bool, 0644);
36 MODULE_PARM_DESC(monitor, "rkispp abnormal restart monitor");
37 
38 static int rkisp_ispp_mode = ISP_ISPP_FBC;
39 module_param_named(mode, rkisp_ispp_mode, int, 0644);
40 MODULE_PARM_DESC(mode, "isp->ispp mode: bit0 fbc, bit1 yuv422, bit2 quick");
41 
42 static bool rkispp_stream_sync;
43 module_param_named(stream_sync, rkispp_stream_sync, bool, 0644);
44 MODULE_PARM_DESC(stream_sync, "rkispp stream sync output");
45 
46 static char rkispp_version[RKISPP_VERNO_LEN];
47 module_param_string(version, rkispp_version, RKISPP_VERNO_LEN, 0444);
48 MODULE_PARM_DESC(version, "version number");
49 
50 bool rkispp_reg_withstream;
51 module_param_named(sendreg_withstream, rkispp_reg_withstream, bool, 0644);
52 MODULE_PARM_DESC(sendreg_withstream, "rkispp send reg out with stream");
53 
54 char rkispp_reg_withstream_video_name[RKISPP_VIDEO_NAME_LEN];
55 module_param_string(sendreg_withstream_video_name, rkispp_reg_withstream_video_name,
56 		    RKISPP_VIDEO_NAME_LEN, 0644);
57 MODULE_PARM_DESC(sendreg_withstream, "rkispp video send reg out with stream");
58 
59 unsigned int rkispp_debug_reg = 0x1F;
60 module_param_named(debug_reg, rkispp_debug_reg, uint, 0644);
61 MODULE_PARM_DESC(debug_reg, "rkispp debug register");
62 
63 static unsigned int rkispp_wait_line;
64 module_param_named(wait_line, rkispp_wait_line, uint, 0644);
65 MODULE_PARM_DESC(wait_line, "rkispp wait line to buf done early");
66 
67 char rkispp_dump_path[128];
68 module_param_string(dump_path, rkispp_dump_path, sizeof(rkispp_dump_path), 0644);
69 MODULE_PARM_DESC(dump_path, "rkispp dump debug file path");
70 
rkispp_set_clk_rate(struct clk * clk,unsigned long rate)71 void rkispp_set_clk_rate(struct clk *clk, unsigned long rate)
72 {
73 	if (rkispp_clk_dbg)
74 		return;
75 
76 	clk_set_rate(clk, rate);
77 }
78 
get_remote_node_dev(struct rkispp_device * ispp_dev)79 static void get_remote_node_dev(struct rkispp_device *ispp_dev)
80 {
81 	struct device *dev = ispp_dev->dev;
82 	struct device_node *parent = dev->of_node;
83 	struct platform_device *remote_dev = NULL;
84 	struct device_node *remote = NULL;
85 	struct v4l2_subdev *sd = NULL;
86 	int i;
87 
88 	for (i = 0; i < 2; i++) {
89 		remote = of_graph_get_remote_node(parent, 0, i);
90 		if (!remote)
91 			continue;
92 		remote_dev = of_find_device_by_node(remote);
93 		of_node_put(remote);
94 		if (!remote_dev) {
95 			dev_err(dev, "Failed to get remote device(%s)\n",
96 				of_node_full_name(remote));
97 			continue;
98 		} else {
99 			rkisp_get_bridge_sd(remote_dev, &sd);
100 			if (!sd) {
101 				dev_err(dev, "Failed to get isp bridge sd\n");
102 			} else {
103 				ispp_dev->ispp_sdev.remote_sd = sd;
104 				v4l2_set_subdev_hostdata(sd, &ispp_dev->ispp_sdev.sd);
105 				if (ispp_dev->hw_dev->max_in.w && ispp_dev->hw_dev->max_in.h)
106 					v4l2_subdev_call(sd, core, ioctl, RKISP_ISPP_CMD_SET_FMT,
107 							 &ispp_dev->hw_dev->max_in);
108 				break;
109 			}
110 		}
111 	}
112 }
113 
rkispp_create_links(struct rkispp_device * ispp_dev)114 static int rkispp_create_links(struct rkispp_device *ispp_dev)
115 {
116 	struct rkispp_stream_vdev *stream_vdev;
117 	struct media_entity *source, *sink;
118 	struct rkispp_stream *stream;
119 	unsigned int flags = 0;
120 	int ret;
121 
122 	stream_vdev = &ispp_dev->stream_vdev;
123 	stream = &stream_vdev->stream[STREAM_II];
124 	/* input stream links */
125 	sink = &ispp_dev->ispp_sdev.sd.entity;
126 	get_remote_node_dev(ispp_dev);
127 	if (ispp_dev->ispp_sdev.remote_sd) {
128 		ispp_dev->inp = INP_ISP;
129 	} else {
130 		flags = MEDIA_LNK_FL_ENABLED;
131 		ispp_dev->inp = INP_DDR;
132 		stream->linked = true;
133 	}
134 	source = &stream->vnode.vdev.entity;
135 	ret = media_create_pad_link(source, 0, sink, RKISPP_PAD_SINK, flags);
136 	if (ret < 0)
137 		return ret;
138 
139 	/* params links */
140 	flags = MEDIA_LNK_FL_ENABLED;
141 	source = &ispp_dev->params_vdev.vnode.vdev.entity;
142 	ret = media_create_pad_link(source, 0, sink, RKISPP_PAD_SINK_PARAMS, flags);
143 	if (ret < 0)
144 		return ret;
145 	ispp_dev->stream_vdev.module_ens = ISPP_MODULE_FEC;
146 	if (ispp_dev->ispp_ver == ISPP_V10) {
147 		/* stats links */
148 		flags = MEDIA_LNK_FL_ENABLED;
149 		source = &ispp_dev->ispp_sdev.sd.entity;
150 		sink = &ispp_dev->stats_vdev.vnode.vdev.entity;
151 		ret = media_create_pad_link(source, RKISPP_PAD_SOURCE_STATS, sink, 0, flags);
152 		if (ret < 0)
153 			return ret;
154 
155 		/* output stream links */
156 		stream = &stream_vdev->stream[STREAM_S0];
157 		stream->linked = flags;
158 		sink = &stream->vnode.vdev.entity;
159 		ret = media_create_pad_link(source, RKISPP_PAD_SOURCE, sink, 0, flags);
160 		if (ret < 0)
161 			return ret;
162 
163 		stream = &stream_vdev->stream[STREAM_S1];
164 		stream->linked = flags;
165 		sink = &stream->vnode.vdev.entity;
166 		ret = media_create_pad_link(source, RKISPP_PAD_SOURCE, sink, 0, flags);
167 		if (ret < 0)
168 			return ret;
169 
170 		stream = &stream_vdev->stream[STREAM_S2];
171 		stream->linked = flags;
172 		sink = &stream->vnode.vdev.entity;
173 		ret = media_create_pad_link(source, RKISPP_PAD_SOURCE, sink, 0, flags);
174 		if (ret < 0)
175 			return ret;
176 
177 		ispp_dev->stream_vdev.module_ens = ISPP_MODULE_NR | ISPP_MODULE_SHP;
178 	}
179 
180 	flags = rkispp_stream_sync ? 0 : MEDIA_LNK_FL_ENABLED;
181 	stream = &stream_vdev->stream[STREAM_MB];
182 	stream->linked = flags;
183 	source = &ispp_dev->ispp_sdev.sd.entity;
184 	sink = &stream->vnode.vdev.entity;
185 	ret = media_create_pad_link(source, RKISPP_PAD_SOURCE, sink, 0, flags);
186 	if (ret < 0)
187 		return ret;
188 
189 	stream = &stream_vdev->stream[STREAM_VIR];
190 	stream->linked = flags;
191 	sink = &stream->vnode.vdev.entity;
192 	ret = media_create_pad_link(source, RKISPP_PAD_SOURCE, sink, 0, flags);
193 	if (ret < 0)
194 		return ret;
195 
196 	/* default enable */
197 	return 0;
198 }
199 
rkispp_register_platform_subdevs(struct rkispp_device * ispp_dev)200 static int rkispp_register_platform_subdevs(struct rkispp_device *ispp_dev)
201 {
202 	int ret;
203 
204 	ret = rkispp_register_stream_vdevs(ispp_dev);
205 	if (ret < 0)
206 		return ret;
207 
208 	ret = rkispp_register_params_vdev(ispp_dev);
209 	if (ret < 0)
210 		goto err_unreg_stream_vdevs;
211 
212 	ret = rkispp_register_stats_vdev(ispp_dev);
213 	if (ret < 0)
214 		goto err_unreg_params_vdev;
215 
216 	ret = rkispp_register_subdev(ispp_dev, &ispp_dev->v4l2_dev);
217 	if (ret < 0)
218 		goto err_unreg_stats_vdev;
219 
220 	ret = rkispp_create_links(ispp_dev);
221 	if (ret < 0)
222 		goto err_unreg_ispp_subdev;
223 	return ret;
224 err_unreg_ispp_subdev:
225 	rkispp_unregister_subdev(ispp_dev);
226 err_unreg_stats_vdev:
227 	rkispp_unregister_stats_vdev(ispp_dev);
228 err_unreg_params_vdev:
229 	rkispp_unregister_params_vdev(ispp_dev);
230 err_unreg_stream_vdevs:
231 	rkispp_unregister_stream_vdevs(ispp_dev);
232 	return ret;
233 }
234 
235 static const struct of_device_id rkispp_plat_of_match[] = {
236 	{
237 		.compatible = "rockchip,rv1126-rkispp-vir",
238 	}, {
239 		.compatible = "rockchip,rk3588-rkispp-vir",
240 	},
241 	{},
242 };
243 
rkispp_plat_probe(struct platform_device * pdev)244 static int rkispp_plat_probe(struct platform_device *pdev)
245 {
246 	struct device *dev = &pdev->dev;
247 	struct v4l2_device *v4l2_dev;
248 	struct rkispp_device *ispp_dev;
249 	int ret;
250 
251 	sprintf(rkispp_version, "v%02x.%02x.%02x",
252 		RKISPP_DRIVER_VERSION >> 16,
253 		(RKISPP_DRIVER_VERSION & 0xff00) >> 8,
254 		RKISPP_DRIVER_VERSION & 0x00ff);
255 
256 	dev_info(dev, "rkispp driver version: %s\n", rkispp_version);
257 
258 	ispp_dev = devm_kzalloc(dev, sizeof(*ispp_dev), GFP_KERNEL);
259 	if (!ispp_dev)
260 		return -ENOMEM;
261 	ispp_dev->sw_base_addr = devm_kzalloc(dev, RKISP_ISPP_SW_MAX_SIZE, GFP_KERNEL);
262 	if (!ispp_dev->sw_base_addr)
263 		return -ENOMEM;
264 
265 	dev_set_drvdata(dev, ispp_dev);
266 	ispp_dev->dev = dev;
267 
268 	ret = rkispp_attach_hw(ispp_dev);
269 	if (ret)
270 		return ret;
271 
272 	sprintf(ispp_dev->media_dev.model, "%s%d",
273 		DRIVER_NAME, ispp_dev->dev_id);
274 	ispp_dev->irq_hdl = rkispp_isr;
275 	mutex_init(&ispp_dev->apilock);
276 	mutex_init(&ispp_dev->iqlock);
277 	init_waitqueue_head(&ispp_dev->sync_onoff);
278 
279 	strscpy(ispp_dev->name, dev_name(dev), sizeof(ispp_dev->name));
280 	strscpy(ispp_dev->media_dev.driver_name, ispp_dev->name,
281 		sizeof(ispp_dev->media_dev.driver_name));
282 	ispp_dev->media_dev.dev = &pdev->dev;
283 	v4l2_dev = &ispp_dev->v4l2_dev;
284 	v4l2_dev->mdev = &ispp_dev->media_dev;
285 	strlcpy(v4l2_dev->name, ispp_dev->name, sizeof(v4l2_dev->name));
286 	v4l2_ctrl_handler_init(&ispp_dev->ctrl_handler, 5);
287 	v4l2_dev->ctrl_handler = &ispp_dev->ctrl_handler;
288 
289 	ret = v4l2_device_register(ispp_dev->dev, v4l2_dev);
290 	if (ret < 0) {
291 		v4l2_err(v4l2_dev, "register v4l2 device failed:%d\n", ret);
292 		return ret;
293 	}
294 	media_device_init(&ispp_dev->media_dev);
295 	ret = media_device_register(&ispp_dev->media_dev);
296 	if (ret < 0) {
297 		v4l2_err(v4l2_dev, "register media device failed:%d\n", ret);
298 		goto err_unreg_v4l2_dev;
299 	}
300 
301 	ret = rkispp_register_platform_subdevs(ispp_dev);
302 	if (ret < 0)
303 		goto err_unreg_media_dev;
304 
305 	rkispp_wait_line = 0;
306 	of_property_read_u32(pdev->dev.of_node, "wait-line",
307 			     &rkispp_wait_line);
308 	rkispp_proc_init(ispp_dev);
309 	pm_runtime_enable(&pdev->dev);
310 
311 	return 0;
312 
313 err_unreg_media_dev:
314 	media_device_unregister(&ispp_dev->media_dev);
315 err_unreg_v4l2_dev:
316 	v4l2_device_unregister(&ispp_dev->v4l2_dev);
317 	return ret;
318 }
319 
rkispp_plat_remove(struct platform_device * pdev)320 static int rkispp_plat_remove(struct platform_device *pdev)
321 {
322 	struct rkispp_device *ispp_dev = platform_get_drvdata(pdev);
323 
324 	pm_runtime_disable(&pdev->dev);
325 
326 	rkispp_proc_cleanup(ispp_dev);
327 	rkispp_unregister_subdev(ispp_dev);
328 	rkispp_unregister_stats_vdev(ispp_dev);
329 	rkispp_unregister_params_vdev(ispp_dev);
330 	rkispp_unregister_stream_vdevs(ispp_dev);
331 
332 	media_device_unregister(&ispp_dev->media_dev);
333 	v4l2_device_unregister(&ispp_dev->v4l2_dev);
334 	mutex_destroy(&ispp_dev->apilock);
335 	mutex_destroy(&ispp_dev->iqlock);
336 	return 0;
337 }
338 
rkispp_runtime_suspend(struct device * dev)339 static int __maybe_unused rkispp_runtime_suspend(struct device *dev)
340 {
341 	struct rkispp_device *ispp_dev = dev_get_drvdata(dev);
342 	int ret;
343 
344 	mutex_lock(&ispp_dev->hw_dev->dev_lock);
345 	ret = pm_runtime_put_sync(ispp_dev->hw_dev->dev);
346 	mutex_unlock(&ispp_dev->hw_dev->dev_lock);
347 	return (ret > 0) ? 0 : ret;
348 }
349 
rkispp_runtime_resume(struct device * dev)350 static int __maybe_unused rkispp_runtime_resume(struct device *dev)
351 {
352 	struct rkispp_device *ispp_dev = dev_get_drvdata(dev);
353 	int ret;
354 
355 	ispp_dev->isp_mode = rkisp_ispp_mode;
356 	ispp_dev->stream_sync = rkispp_stream_sync;
357 	ispp_dev->stream_vdev.monitor.is_en = rkispp_monitor;
358 	ispp_dev->stream_vdev.wait_line = rkispp_wait_line;
359 
360 	mutex_lock(&ispp_dev->hw_dev->dev_lock);
361 	ret = pm_runtime_get_sync(ispp_dev->hw_dev->dev);
362 	mutex_unlock(&ispp_dev->hw_dev->dev_lock);
363 	return (ret > 0) ? 0 : ret;
364 }
365 
366 static const struct dev_pm_ops rkispp_plat_pm_ops = {
367 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
368 				pm_runtime_force_resume)
369 	SET_RUNTIME_PM_OPS(rkispp_runtime_suspend,
370 			   rkispp_runtime_resume, NULL)
371 };
372 
373 struct platform_driver rkispp_plat_drv = {
374 	.driver = {
375 		.name = DRIVER_NAME,
376 		.of_match_table = of_match_ptr(rkispp_plat_of_match),
377 		.pm = &rkispp_plat_pm_ops,
378 	},
379 	.probe = rkispp_plat_probe,
380 	.remove = rkispp_plat_remove,
381 };
382 
383 MODULE_AUTHOR("Rockchip Camera/ISP team");
384 MODULE_DESCRIPTION("Rockchip ISPP platform driver");
385 MODULE_LICENSE("GPL v2");
386