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