1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
5 * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
6 * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
8 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
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>
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 #include <linux/version.h>
28 #include <media/media-device.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/videobuf2-v4l2.h>
32 #include <media/videobuf2-dma-contig.h>
34 #include "camif-core.h"
36 static char *camif_clocks[CLK_MAX_NUM] = {
37 /* HCLK CAMIF clock */
39 /* CAMIF / external camera sensor master clock */
43 static const struct camif_fmt camif_formats[] = {
45 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
46 .fourcc = V4L2_PIX_FMT_YUV422P,
49 .color = IMG_FMT_YCBCR422P,
51 .flags = FMT_FL_S3C24XX_CODEC |
54 .name = "YUV 4:2:0 planar, Y/Cb/Cr",
55 .fourcc = V4L2_PIX_FMT_YUV420,
58 .color = IMG_FMT_YCBCR420,
60 .flags = FMT_FL_S3C24XX_CODEC |
63 .name = "YVU 4:2:0 planar, Y/Cr/Cb",
64 .fourcc = V4L2_PIX_FMT_YVU420,
67 .color = IMG_FMT_YCRCB420,
69 .flags = FMT_FL_S3C24XX_CODEC |
72 .name = "RGB565, 16 bpp",
73 .fourcc = V4L2_PIX_FMT_RGB565X,
76 .color = IMG_FMT_RGB565,
78 .flags = FMT_FL_S3C24XX_PREVIEW |
81 .name = "XRGB8888, 32 bpp",
82 .fourcc = V4L2_PIX_FMT_RGB32,
85 .color = IMG_FMT_XRGB8888,
87 .flags = FMT_FL_S3C24XX_PREVIEW |
91 .fourcc = V4L2_PIX_FMT_BGR666,
94 .color = IMG_FMT_RGB666,
96 .flags = FMT_FL_S3C64XX,
101 * s3c_camif_find_format() - lookup camif color format by fourcc or an index
102 * @vp: video path (DMA) description (codec/preview)
103 * @pixelformat: fourcc to match, ignored if null
104 * @index: index to the camif_formats array, ignored if negative
106 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
107 const u32 *pixelformat,
110 const struct camif_fmt *fmt, *def_fmt = NULL;
114 if (index >= (int)ARRAY_SIZE(camif_formats))
117 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
118 fmt = &camif_formats[i];
119 if (vp && !(vp->fmt_flags & fmt->flags))
121 if (pixelformat && fmt->fourcc == *pixelformat)
130 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
138 unsigned int tmp = 1 << sh;
139 if (src >= tar * tmp) {
140 *shift = sh, *ratio = tmp;
144 *shift = 0, *ratio = 1;
148 int s3c_camif_get_scaler_config(struct camif_vp *vp,
149 struct camif_scaler *scaler)
151 struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
152 int source_x = camif_crop->width;
153 int source_y = camif_crop->height;
154 int target_x = vp->out_frame.rect.width;
155 int target_y = vp->out_frame.rect.height;
158 if (vp->rotation == 90 || vp->rotation == 270)
159 swap(target_x, target_y);
161 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
166 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
171 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
172 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
174 scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
175 scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
177 scaler->scaleup_h = (target_x >= source_x);
178 scaler->scaleup_v = (target_y >= source_y);
182 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
183 scaler->pre_h_ratio, scaler->h_shift,
184 scaler->pre_v_ratio, scaler->v_shift);
186 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
187 source_x, source_y, target_x, target_y,
188 scaler->scaleup_h, scaler->scaleup_v);
193 static int camif_register_sensor(struct camif_dev *camif)
195 struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
196 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
197 struct i2c_adapter *adapter;
198 struct v4l2_subdev_format format;
199 struct v4l2_subdev *sd;
202 camif->sensor.sd = NULL;
204 if (sensor->i2c_board_info.addr == 0)
207 adapter = i2c_get_adapter(sensor->i2c_bus_num);
208 if (adapter == NULL) {
209 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
210 sensor->i2c_bus_num);
211 return -EPROBE_DEFER;
214 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
215 &sensor->i2c_board_info, NULL);
217 i2c_put_adapter(adapter);
218 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
219 sensor->i2c_board_info.type);
220 return -EPROBE_DEFER;
222 camif->sensor.sd = sd;
224 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
226 /* Get initial pixel format and set it at the camif sink pad */
228 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
229 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
234 format.pad = CAMIF_SD_PAD_SINK;
235 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
237 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
238 format.format.width, format.format.height,
243 static void camif_unregister_sensor(struct camif_dev *camif)
245 struct v4l2_subdev *sd = camif->sensor.sd;
246 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
247 struct i2c_adapter *adapter;
252 adapter = client->adapter;
253 v4l2_device_unregister_subdev(sd);
254 camif->sensor.sd = NULL;
255 i2c_unregister_device(client);
256 i2c_put_adapter(adapter);
259 static int camif_create_media_links(struct camif_dev *camif)
263 ret = media_create_pad_link(&camif->sensor.sd->entity, 0,
264 &camif->subdev.entity, CAMIF_SD_PAD_SINK,
265 MEDIA_LNK_FL_IMMUTABLE |
266 MEDIA_LNK_FL_ENABLED);
270 for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
271 ret = media_create_pad_link(&camif->subdev.entity, i,
272 &camif->vp[i - 1].vdev.entity, 0,
273 MEDIA_LNK_FL_IMMUTABLE |
274 MEDIA_LNK_FL_ENABLED);
280 static int camif_register_video_nodes(struct camif_dev *camif)
282 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
286 return s3c_camif_register_video_node(camif, VP_PREVIEW);
289 static void camif_unregister_video_nodes(struct camif_dev *camif)
291 s3c_camif_unregister_video_node(camif, VP_CODEC);
292 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
295 static void camif_unregister_media_entities(struct camif_dev *camif)
297 camif_unregister_video_nodes(camif);
298 camif_unregister_sensor(camif);
299 s3c_camif_unregister_subdev(camif);
305 static int camif_media_dev_init(struct camif_dev *camif)
307 struct media_device *md = &camif->media_dev;
308 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
309 unsigned int ip_rev = camif->variant->ip_revision;
312 memset(md, 0, sizeof(*md));
313 snprintf(md->model, sizeof(md->model), "SAMSUNG S3C%s CAMIF",
314 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
315 strscpy(md->bus_info, "platform", sizeof(md->bus_info));
316 md->hw_revision = ip_rev;
318 md->dev = camif->dev;
320 strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
323 media_device_init(md);
325 ret = v4l2_device_register(camif->dev, v4l2_dev);
332 static void camif_clk_put(struct camif_dev *camif)
336 for (i = 0; i < CLK_MAX_NUM; i++) {
337 if (IS_ERR(camif->clock[i]))
339 clk_unprepare(camif->clock[i]);
340 clk_put(camif->clock[i]);
341 camif->clock[i] = ERR_PTR(-EINVAL);
345 static int camif_clk_get(struct camif_dev *camif)
349 for (i = 1; i < CLK_MAX_NUM; i++)
350 camif->clock[i] = ERR_PTR(-EINVAL);
352 for (i = 0; i < CLK_MAX_NUM; i++) {
353 camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
354 if (IS_ERR(camif->clock[i])) {
355 ret = PTR_ERR(camif->clock[i]);
358 ret = clk_prepare(camif->clock[i]);
360 clk_put(camif->clock[i]);
361 camif->clock[i] = NULL;
367 camif_clk_put(camif);
368 dev_err(camif->dev, "failed to get clock: %s\n",
374 * The CAMIF device has two relatively independent data processing paths
375 * that can source data from memory or the common camera input frontend.
376 * Register interrupts for each data processing path (camif_vp).
378 static int camif_request_irqs(struct platform_device *pdev,
379 struct camif_dev *camif)
383 for (i = 0; i < CAMIF_VP_NUM; i++) {
384 struct camif_vp *vp = &camif->vp[i];
386 init_waitqueue_head(&vp->irq_queue);
388 irq = platform_get_irq(pdev, i);
390 dev_err(&pdev->dev, "failed to get IRQ %d\n", i);
394 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
395 0, dev_name(&pdev->dev), vp);
397 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
405 static int s3c_camif_probe(struct platform_device *pdev)
407 struct device *dev = &pdev->dev;
408 struct s3c_camif_plat_data *pdata = dev->platform_data;
409 struct s3c_camif_drvdata *drvdata;
410 struct camif_dev *camif;
411 struct resource *mres;
414 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
418 spin_lock_init(&camif->slock);
419 mutex_init(&camif->lock);
423 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
424 dev_err(dev, "wrong platform data\n");
428 camif->pdata = *pdata;
429 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
430 camif->variant = drvdata->variant;
432 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434 camif->io_base = devm_ioremap_resource(dev, mres);
435 if (IS_ERR(camif->io_base))
436 return PTR_ERR(camif->io_base);
438 ret = camif_request_irqs(pdev, camif);
442 ret = pdata->gpio_get();
446 ret = s3c_camif_create_subdev(camif);
450 ret = camif_clk_get(camif);
454 platform_set_drvdata(pdev, camif);
455 clk_set_rate(camif->clock[CLK_CAM],
456 camif->pdata.sensor.clock_frequency);
458 dev_info(dev, "sensor clock frequency: %lu\n",
459 clk_get_rate(camif->clock[CLK_CAM]));
461 * Set initial pixel format, resolution and crop rectangle.
462 * Must be done before a sensor subdev is registered as some
463 * settings are overrode with values from sensor subdev.
465 s3c_camif_set_defaults(camif);
467 pm_runtime_enable(dev);
469 ret = pm_runtime_get_sync(dev);
473 ret = camif_media_dev_init(camif);
477 ret = camif_register_sensor(camif);
481 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
485 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
489 ret = camif_register_video_nodes(camif);
493 ret = camif_create_media_links(camif);
497 ret = media_device_register(&camif->media_dev);
505 v4l2_device_unregister(&camif->v4l2_dev);
506 media_device_unregister(&camif->media_dev);
507 media_device_cleanup(&camif->media_dev);
508 camif_unregister_media_entities(camif);
511 pm_runtime_disable(dev);
513 camif_clk_put(camif);
515 s3c_camif_unregister_subdev(camif);
521 static int s3c_camif_remove(struct platform_device *pdev)
523 struct camif_dev *camif = platform_get_drvdata(pdev);
524 struct s3c_camif_plat_data *pdata = &camif->pdata;
526 media_device_unregister(&camif->media_dev);
527 media_device_cleanup(&camif->media_dev);
528 camif_unregister_media_entities(camif);
529 v4l2_device_unregister(&camif->v4l2_dev);
531 pm_runtime_disable(&pdev->dev);
532 camif_clk_put(camif);
538 static int s3c_camif_runtime_resume(struct device *dev)
540 struct camif_dev *camif = dev_get_drvdata(dev);
542 clk_enable(camif->clock[CLK_GATE]);
543 /* null op on s3c244x */
544 clk_enable(camif->clock[CLK_CAM]);
548 static int s3c_camif_runtime_suspend(struct device *dev)
550 struct camif_dev *camif = dev_get_drvdata(dev);
552 /* null op on s3c244x */
553 clk_disable(camif->clock[CLK_CAM]);
555 clk_disable(camif->clock[CLK_GATE]);
559 static const struct s3c_camif_variant s3c244x_camif_variant = {
562 .max_out_width = 4096,
563 .max_sc_out_width = 2048,
564 .out_width_align = 16,
569 .max_out_width = 640,
570 .max_sc_out_width = 640,
571 .out_width_align = 16,
577 .win_hor_offset_align = 8,
579 .ip_revision = S3C244X_CAMIF_IP_REV,
582 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
583 .variant = &s3c244x_camif_variant,
584 .bus_clk_freq = 24000000UL,
587 static const struct s3c_camif_variant s3c6410_camif_variant = {
590 .max_out_width = 4096,
591 .max_sc_out_width = 2048,
592 .out_width_align = 16,
597 .max_out_width = 4096,
598 .max_sc_out_width = 720,
599 .out_width_align = 16,
605 .win_hor_offset_align = 8,
607 .ip_revision = S3C6410_CAMIF_IP_REV,
612 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
613 .variant = &s3c6410_camif_variant,
614 .bus_clk_freq = 133000000UL,
617 static const struct platform_device_id s3c_camif_driver_ids[] = {
619 .name = "s3c2440-camif",
620 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
622 .name = "s3c6410-camif",
623 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
627 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
629 static const struct dev_pm_ops s3c_camif_pm_ops = {
630 .runtime_suspend = s3c_camif_runtime_suspend,
631 .runtime_resume = s3c_camif_runtime_resume,
634 static struct platform_driver s3c_camif_driver = {
635 .probe = s3c_camif_probe,
636 .remove = s3c_camif_remove,
637 .id_table = s3c_camif_driver_ids,
639 .name = S3C_CAMIF_DRIVER_NAME,
640 .pm = &s3c_camif_pm_ops,
644 module_platform_driver(s3c_camif_driver);
646 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
647 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
648 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
649 MODULE_LICENSE("GPL");