1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
4  *
5  * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
6  * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
7  */
8 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
9 
10 #include <linux/bug.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/gpio.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 
27 #include <media/media-device.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf2-v4l2.h>
31 #include <media/videobuf2-dma-contig.h>
32 
33 #include "camif-core.h"
34 
35 static char *camif_clocks[CLK_MAX_NUM] = {
36 	/* HCLK CAMIF clock */
37 	[CLK_GATE]	= "camif",
38 	/* CAMIF / external camera sensor master clock */
39 	[CLK_CAM]	= "camera",
40 };
41 
42 static const struct camif_fmt camif_formats[] = {
43 	{
44 		.fourcc		= V4L2_PIX_FMT_YUV422P,
45 		.depth		= 16,
46 		.ybpp		= 1,
47 		.color		= IMG_FMT_YCBCR422P,
48 		.colplanes	= 3,
49 		.flags		= FMT_FL_S3C24XX_CODEC |
50 				  FMT_FL_S3C64XX,
51 	}, {
52 		.fourcc		= V4L2_PIX_FMT_YUV420,
53 		.depth		= 12,
54 		.ybpp		= 1,
55 		.color		= IMG_FMT_YCBCR420,
56 		.colplanes	= 3,
57 		.flags		= FMT_FL_S3C24XX_CODEC |
58 				  FMT_FL_S3C64XX,
59 	}, {
60 		.fourcc		= V4L2_PIX_FMT_YVU420,
61 		.depth		= 12,
62 		.ybpp		= 1,
63 		.color		= IMG_FMT_YCRCB420,
64 		.colplanes	= 3,
65 		.flags		= FMT_FL_S3C24XX_CODEC |
66 				  FMT_FL_S3C64XX,
67 	}, {
68 		.fourcc		= V4L2_PIX_FMT_RGB565X,
69 		.depth		= 16,
70 		.ybpp		= 2,
71 		.color		= IMG_FMT_RGB565,
72 		.colplanes	= 1,
73 		.flags		= FMT_FL_S3C24XX_PREVIEW |
74 				  FMT_FL_S3C64XX,
75 	}, {
76 		.fourcc		= V4L2_PIX_FMT_RGB32,
77 		.depth		= 32,
78 		.ybpp		= 4,
79 		.color		= IMG_FMT_XRGB8888,
80 		.colplanes	= 1,
81 		.flags		= FMT_FL_S3C24XX_PREVIEW |
82 				  FMT_FL_S3C64XX,
83 	}, {
84 		.fourcc		= V4L2_PIX_FMT_BGR666,
85 		.depth		= 32,
86 		.ybpp		= 4,
87 		.color		= IMG_FMT_RGB666,
88 		.colplanes	= 1,
89 		.flags		= FMT_FL_S3C64XX,
90 	}
91 };
92 
93 /**
94  * s3c_camif_find_format() - lookup camif color format by fourcc or an index
95  * @vp: video path (DMA) description (codec/preview)
96  * @pixelformat: fourcc to match, ignored if null
97  * @index: index to the camif_formats array, ignored if negative
98  */
99 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
100 					      const u32 *pixelformat,
101 					      int index)
102 {
103 	const struct camif_fmt *fmt, *def_fmt = NULL;
104 	unsigned int i;
105 	int id = 0;
106 
107 	if (index >= (int)ARRAY_SIZE(camif_formats))
108 		return NULL;
109 
110 	for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
111 		fmt = &camif_formats[i];
112 		if (vp && !(vp->fmt_flags & fmt->flags))
113 			continue;
114 		if (pixelformat && fmt->fourcc == *pixelformat)
115 			return fmt;
116 		if (index == id)
117 			def_fmt = fmt;
118 		id++;
119 	}
120 	return def_fmt;
121 }
122 
123 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
124 {
125 	unsigned int sh = 6;
126 
127 	if (src >= 64 * tar)
128 		return -EINVAL;
129 
130 	while (sh--) {
131 		unsigned int tmp = 1 << sh;
132 		if (src >= tar * tmp) {
133 			*shift = sh;
134 			*ratio = tmp;
135 			return 0;
136 		}
137 	}
138 	*shift = 0;
139 	*ratio = 1;
140 	return 0;
141 }
142 
143 int s3c_camif_get_scaler_config(struct camif_vp *vp,
144 				struct camif_scaler *scaler)
145 {
146 	struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
147 	int source_x = camif_crop->width;
148 	int source_y = camif_crop->height;
149 	int target_x = vp->out_frame.rect.width;
150 	int target_y = vp->out_frame.rect.height;
151 	int ret;
152 
153 	if (vp->rotation == 90 || vp->rotation == 270)
154 		swap(target_x, target_y);
155 
156 	ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
157 				      &scaler->h_shift);
158 	if (ret < 0)
159 		return ret;
160 
161 	ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
162 				      &scaler->v_shift);
163 	if (ret < 0)
164 		return ret;
165 
166 	scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
167 	scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
168 
169 	scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
170 	scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
171 
172 	scaler->scaleup_h = (target_x >= source_x);
173 	scaler->scaleup_v = (target_y >= source_y);
174 
175 	scaler->copy = 0;
176 
177 	pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
178 		 scaler->pre_h_ratio, scaler->h_shift,
179 		 scaler->pre_v_ratio, scaler->v_shift);
180 
181 	pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
182 		 source_x, source_y, target_x, target_y,
183 		 scaler->scaleup_h, scaler->scaleup_v);
184 
185 	return 0;
186 }
187 
188 static int camif_register_sensor(struct camif_dev *camif)
189 {
190 	struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
191 	struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
192 	struct i2c_adapter *adapter;
193 	struct v4l2_subdev_format format;
194 	struct v4l2_subdev *sd;
195 	int ret;
196 
197 	camif->sensor.sd = NULL;
198 
199 	if (sensor->i2c_board_info.addr == 0)
200 		return -EINVAL;
201 
202 	adapter = i2c_get_adapter(sensor->i2c_bus_num);
203 	if (adapter == NULL) {
204 		v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
205 			  sensor->i2c_bus_num);
206 		return -EPROBE_DEFER;
207 	}
208 
209 	sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
210 				       &sensor->i2c_board_info, NULL);
211 	if (sd == NULL) {
212 		i2c_put_adapter(adapter);
213 		v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
214 			  sensor->i2c_board_info.type);
215 		return -EPROBE_DEFER;
216 	}
217 	camif->sensor.sd = sd;
218 
219 	v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
220 
221 	/* Get initial pixel format and set it at the camif sink pad */
222 	format.pad = 0;
223 	format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
224 	ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
225 
226 	if (ret < 0)
227 		return 0;
228 
229 	format.pad = CAMIF_SD_PAD_SINK;
230 	v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
231 
232 	v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
233 		  format.format.width, format.format.height,
234 		  format.format.code);
235 	return 0;
236 }
237 
238 static void camif_unregister_sensor(struct camif_dev *camif)
239 {
240 	struct v4l2_subdev *sd = camif->sensor.sd;
241 	struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
242 	struct i2c_adapter *adapter;
243 
244 	if (client == NULL)
245 		return;
246 
247 	adapter = client->adapter;
248 	v4l2_device_unregister_subdev(sd);
249 	camif->sensor.sd = NULL;
250 	i2c_unregister_device(client);
251 	i2c_put_adapter(adapter);
252 }
253 
254 static int camif_create_media_links(struct camif_dev *camif)
255 {
256 	int i, ret;
257 
258 	ret = media_create_pad_link(&camif->sensor.sd->entity, 0,
259 				&camif->subdev.entity, CAMIF_SD_PAD_SINK,
260 				MEDIA_LNK_FL_IMMUTABLE |
261 				MEDIA_LNK_FL_ENABLED);
262 	if (ret)
263 		return ret;
264 
265 	for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
266 		ret = media_create_pad_link(&camif->subdev.entity, i,
267 				&camif->vp[i - 1].vdev.entity, 0,
268 				MEDIA_LNK_FL_IMMUTABLE |
269 				MEDIA_LNK_FL_ENABLED);
270 	}
271 
272 	return ret;
273 }
274 
275 static int camif_register_video_nodes(struct camif_dev *camif)
276 {
277 	int ret = s3c_camif_register_video_node(camif, VP_CODEC);
278 	if (ret < 0)
279 		return ret;
280 
281 	return s3c_camif_register_video_node(camif, VP_PREVIEW);
282 }
283 
284 static void camif_unregister_video_nodes(struct camif_dev *camif)
285 {
286 	s3c_camif_unregister_video_node(camif, VP_CODEC);
287 	s3c_camif_unregister_video_node(camif, VP_PREVIEW);
288 }
289 
290 static void camif_unregister_media_entities(struct camif_dev *camif)
291 {
292 	camif_unregister_video_nodes(camif);
293 	camif_unregister_sensor(camif);
294 }
295 
296 /*
297  * Media device
298  */
299 static int camif_media_dev_init(struct camif_dev *camif)
300 {
301 	struct media_device *md = &camif->media_dev;
302 	struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
303 	unsigned int ip_rev = camif->variant->ip_revision;
304 	int ret;
305 
306 	memset(md, 0, sizeof(*md));
307 	snprintf(md->model, sizeof(md->model), "Samsung S3C%s CAMIF",
308 		 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
309 	strscpy(md->bus_info, "platform", sizeof(md->bus_info));
310 	md->hw_revision = ip_rev;
311 
312 	md->dev = camif->dev;
313 
314 	strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
315 	v4l2_dev->mdev = md;
316 
317 	media_device_init(md);
318 
319 	ret = v4l2_device_register(camif->dev, v4l2_dev);
320 	if (ret < 0)
321 		return ret;
322 
323 	return ret;
324 }
325 
326 static void camif_clk_put(struct camif_dev *camif)
327 {
328 	int i;
329 
330 	for (i = 0; i < CLK_MAX_NUM; i++) {
331 		if (IS_ERR(camif->clock[i]))
332 			continue;
333 		clk_unprepare(camif->clock[i]);
334 		clk_put(camif->clock[i]);
335 		camif->clock[i] = ERR_PTR(-EINVAL);
336 	}
337 }
338 
339 static int camif_clk_get(struct camif_dev *camif)
340 {
341 	int ret, i;
342 
343 	for (i = 1; i < CLK_MAX_NUM; i++)
344 		camif->clock[i] = ERR_PTR(-EINVAL);
345 
346 	for (i = 0; i < CLK_MAX_NUM; i++) {
347 		camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
348 		if (IS_ERR(camif->clock[i])) {
349 			ret = PTR_ERR(camif->clock[i]);
350 			goto err;
351 		}
352 		ret = clk_prepare(camif->clock[i]);
353 		if (ret < 0) {
354 			clk_put(camif->clock[i]);
355 			camif->clock[i] = NULL;
356 			goto err;
357 		}
358 	}
359 	return 0;
360 err:
361 	camif_clk_put(camif);
362 	dev_err(camif->dev, "failed to get clock: %s\n",
363 		camif_clocks[i]);
364 	return ret;
365 }
366 
367 /*
368  * The CAMIF device has two relatively independent data processing paths
369  * that can source data from memory or the common camera input frontend.
370  * Register interrupts for each data processing path (camif_vp).
371  */
372 static int camif_request_irqs(struct platform_device *pdev,
373 			      struct camif_dev *camif)
374 {
375 	int irq, ret, i;
376 
377 	for (i = 0; i < CAMIF_VP_NUM; i++) {
378 		struct camif_vp *vp = &camif->vp[i];
379 
380 		init_waitqueue_head(&vp->irq_queue);
381 
382 		irq = platform_get_irq(pdev, i);
383 		if (irq <= 0)
384 			return -ENXIO;
385 
386 		ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
387 				       0, dev_name(&pdev->dev), vp);
388 		if (ret < 0) {
389 			dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
390 			break;
391 		}
392 	}
393 
394 	return ret;
395 }
396 
397 static int s3c_camif_probe(struct platform_device *pdev)
398 {
399 	struct device *dev = &pdev->dev;
400 	struct s3c_camif_plat_data *pdata = dev->platform_data;
401 	struct s3c_camif_drvdata *drvdata;
402 	struct camif_dev *camif;
403 	int ret = 0;
404 
405 	camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
406 	if (!camif)
407 		return -ENOMEM;
408 
409 	spin_lock_init(&camif->slock);
410 	mutex_init(&camif->lock);
411 
412 	camif->dev = dev;
413 
414 	if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
415 		dev_err(dev, "wrong platform data\n");
416 		return -EINVAL;
417 	}
418 
419 	camif->pdata = *pdata;
420 	drvdata = (void *)platform_get_device_id(pdev)->driver_data;
421 	camif->variant = drvdata->variant;
422 
423 	camif->io_base = devm_platform_ioremap_resource(pdev, 0);
424 	if (IS_ERR(camif->io_base))
425 		return PTR_ERR(camif->io_base);
426 
427 	ret = camif_request_irqs(pdev, camif);
428 	if (ret < 0)
429 		return ret;
430 
431 	ret = pdata->gpio_get();
432 	if (ret < 0)
433 		return ret;
434 
435 	ret = s3c_camif_create_subdev(camif);
436 	if (ret < 0)
437 		goto err_sd;
438 
439 	ret = camif_clk_get(camif);
440 	if (ret < 0)
441 		goto err_clk;
442 
443 	platform_set_drvdata(pdev, camif);
444 	clk_set_rate(camif->clock[CLK_CAM],
445 			camif->pdata.sensor.clock_frequency);
446 
447 	dev_info(dev, "sensor clock frequency: %lu\n",
448 		 clk_get_rate(camif->clock[CLK_CAM]));
449 	/*
450 	 * Set initial pixel format, resolution and crop rectangle.
451 	 * Must be done before a sensor subdev is registered as some
452 	 * settings are overrode with values from sensor subdev.
453 	 */
454 	s3c_camif_set_defaults(camif);
455 
456 	pm_runtime_enable(dev);
457 
458 	ret = pm_runtime_resume_and_get(dev);
459 	if (ret < 0)
460 		goto err_disable;
461 
462 	ret = camif_media_dev_init(camif);
463 	if (ret < 0)
464 		goto err_pm;
465 
466 	ret = camif_register_sensor(camif);
467 	if (ret < 0)
468 		goto err_sens;
469 
470 	ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
471 	if (ret < 0)
472 		goto err_sens;
473 
474 	ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
475 	if (ret < 0)
476 		goto err_sens;
477 
478 	ret = camif_register_video_nodes(camif);
479 	if (ret < 0)
480 		goto err_sens;
481 
482 	ret = camif_create_media_links(camif);
483 	if (ret < 0)
484 		goto err_sens;
485 
486 	ret = media_device_register(&camif->media_dev);
487 	if (ret < 0)
488 		goto err_sens;
489 
490 	pm_runtime_put(dev);
491 	return 0;
492 
493 err_sens:
494 	v4l2_device_unregister(&camif->v4l2_dev);
495 	media_device_unregister(&camif->media_dev);
496 	media_device_cleanup(&camif->media_dev);
497 	camif_unregister_media_entities(camif);
498 err_pm:
499 	pm_runtime_put(dev);
500 err_disable:
501 	pm_runtime_disable(dev);
502 	camif_clk_put(camif);
503 err_clk:
504 	s3c_camif_unregister_subdev(camif);
505 err_sd:
506 	pdata->gpio_put();
507 	return ret;
508 }
509 
510 static int s3c_camif_remove(struct platform_device *pdev)
511 {
512 	struct camif_dev *camif = platform_get_drvdata(pdev);
513 	struct s3c_camif_plat_data *pdata = &camif->pdata;
514 
515 	media_device_unregister(&camif->media_dev);
516 	media_device_cleanup(&camif->media_dev);
517 	camif_unregister_media_entities(camif);
518 	v4l2_device_unregister(&camif->v4l2_dev);
519 
520 	pm_runtime_disable(&pdev->dev);
521 	camif_clk_put(camif);
522 	s3c_camif_unregister_subdev(camif);
523 	pdata->gpio_put();
524 
525 	return 0;
526 }
527 
528 static int s3c_camif_runtime_resume(struct device *dev)
529 {
530 	struct camif_dev *camif = dev_get_drvdata(dev);
531 
532 	clk_enable(camif->clock[CLK_GATE]);
533 	/* null op on s3c244x */
534 	clk_enable(camif->clock[CLK_CAM]);
535 	return 0;
536 }
537 
538 static int s3c_camif_runtime_suspend(struct device *dev)
539 {
540 	struct camif_dev *camif = dev_get_drvdata(dev);
541 
542 	/* null op on s3c244x */
543 	clk_disable(camif->clock[CLK_CAM]);
544 
545 	clk_disable(camif->clock[CLK_GATE]);
546 	return 0;
547 }
548 
549 static const struct s3c_camif_variant s3c244x_camif_variant = {
550 	.vp_pix_limits = {
551 		[VP_CODEC] = {
552 			.max_out_width		= 4096,
553 			.max_sc_out_width	= 2048,
554 			.out_width_align	= 16,
555 			.min_out_width		= 16,
556 			.max_height		= 4096,
557 		},
558 		[VP_PREVIEW] = {
559 			.max_out_width		= 640,
560 			.max_sc_out_width	= 640,
561 			.out_width_align	= 16,
562 			.min_out_width		= 16,
563 			.max_height		= 480,
564 		}
565 	},
566 	.pix_limits = {
567 		.win_hor_offset_align	= 8,
568 	},
569 	.ip_revision = S3C244X_CAMIF_IP_REV,
570 };
571 
572 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
573 	.variant	= &s3c244x_camif_variant,
574 	.bus_clk_freq	= 24000000UL,
575 };
576 
577 static const struct s3c_camif_variant s3c6410_camif_variant = {
578 	.vp_pix_limits = {
579 		[VP_CODEC] = {
580 			.max_out_width		= 4096,
581 			.max_sc_out_width	= 2048,
582 			.out_width_align	= 16,
583 			.min_out_width		= 16,
584 			.max_height		= 4096,
585 		},
586 		[VP_PREVIEW] = {
587 			.max_out_width		= 4096,
588 			.max_sc_out_width	= 720,
589 			.out_width_align	= 16,
590 			.min_out_width		= 16,
591 			.max_height		= 4096,
592 		}
593 	},
594 	.pix_limits = {
595 		.win_hor_offset_align	= 8,
596 	},
597 	.ip_revision = S3C6410_CAMIF_IP_REV,
598 	.has_img_effect = 1,
599 	.vp_offset = 0x20,
600 };
601 
602 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
603 	.variant	= &s3c6410_camif_variant,
604 	.bus_clk_freq	= 133000000UL,
605 };
606 
607 static const struct platform_device_id s3c_camif_driver_ids[] = {
608 	{
609 		.name		= "s3c2440-camif",
610 		.driver_data	= (unsigned long)&s3c244x_camif_drvdata,
611 	}, {
612 		.name		= "s3c6410-camif",
613 		.driver_data	= (unsigned long)&s3c6410_camif_drvdata,
614 	},
615 	{ /* sentinel */ },
616 };
617 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
618 
619 static const struct dev_pm_ops s3c_camif_pm_ops = {
620 	.runtime_suspend	= s3c_camif_runtime_suspend,
621 	.runtime_resume		= s3c_camif_runtime_resume,
622 };
623 
624 static struct platform_driver s3c_camif_driver = {
625 	.probe		= s3c_camif_probe,
626 	.remove		= s3c_camif_remove,
627 	.id_table	= s3c_camif_driver_ids,
628 	.driver = {
629 		.name	= S3C_CAMIF_DRIVER_NAME,
630 		.pm	= &s3c_camif_pm_ops,
631 	}
632 };
633 
634 module_platform_driver(s3c_camif_driver);
635 
636 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
637 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
638 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
639 MODULE_LICENSE("GPL");
640