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>
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>
33 #include "camif-core.h"
35 static char *camif_clocks[CLK_MAX_NUM] = {
36 /* HCLK CAMIF clock */
38 /* CAMIF / external camera sensor master clock */
42 static const struct camif_fmt camif_formats[] = {
44 .fourcc = V4L2_PIX_FMT_YUV422P,
47 .color = IMG_FMT_YCBCR422P,
49 .flags = FMT_FL_S3C24XX_CODEC |
52 .fourcc = V4L2_PIX_FMT_YUV420,
55 .color = IMG_FMT_YCBCR420,
57 .flags = FMT_FL_S3C24XX_CODEC |
60 .fourcc = V4L2_PIX_FMT_YVU420,
63 .color = IMG_FMT_YCRCB420,
65 .flags = FMT_FL_S3C24XX_CODEC |
68 .fourcc = V4L2_PIX_FMT_RGB565X,
71 .color = IMG_FMT_RGB565,
73 .flags = FMT_FL_S3C24XX_PREVIEW |
76 .fourcc = V4L2_PIX_FMT_RGB32,
79 .color = IMG_FMT_XRGB8888,
81 .flags = FMT_FL_S3C24XX_PREVIEW |
84 .fourcc = V4L2_PIX_FMT_BGR666,
87 .color = IMG_FMT_RGB666,
89 .flags = FMT_FL_S3C64XX,
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
99 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
100 const u32 *pixelformat,
103 const struct camif_fmt *fmt, *def_fmt = NULL;
107 if (index >= (int)ARRAY_SIZE(camif_formats))
110 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
111 fmt = &camif_formats[i];
112 if (vp && !(vp->fmt_flags & fmt->flags))
114 if (pixelformat && fmt->fourcc == *pixelformat)
123 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
131 unsigned int tmp = 1 << sh;
132 if (src >= tar * tmp) {
143 int s3c_camif_get_scaler_config(struct camif_vp *vp,
144 struct camif_scaler *scaler)
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;
153 if (vp->rotation == 90 || vp->rotation == 270)
154 swap(target_x, target_y);
156 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
161 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
166 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
167 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
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);
172 scaler->scaleup_h = (target_x >= source_x);
173 scaler->scaleup_v = (target_y >= source_y);
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);
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);
188 static int camif_register_sensor(struct camif_dev *camif)
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 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
196 struct v4l2_subdev *sd;
199 camif->sensor.sd = NULL;
201 if (sensor->i2c_board_info.addr == 0)
204 adapter = i2c_get_adapter(sensor->i2c_bus_num);
205 if (adapter == NULL) {
206 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
207 sensor->i2c_bus_num);
208 return -EPROBE_DEFER;
211 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
212 &sensor->i2c_board_info, NULL);
214 i2c_put_adapter(adapter);
215 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
216 sensor->i2c_board_info.type);
217 return -EPROBE_DEFER;
219 camif->sensor.sd = sd;
221 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
223 /* Get initial pixel format and set it at the camif sink pad */
225 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
230 format.pad = CAMIF_SD_PAD_SINK;
231 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
233 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
234 format.format.width, format.format.height,
239 static void camif_unregister_sensor(struct camif_dev *camif)
241 struct v4l2_subdev *sd = camif->sensor.sd;
242 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
243 struct i2c_adapter *adapter;
248 adapter = client->adapter;
249 v4l2_device_unregister_subdev(sd);
250 camif->sensor.sd = NULL;
251 i2c_unregister_device(client);
252 i2c_put_adapter(adapter);
255 static int camif_create_media_links(struct camif_dev *camif)
259 ret = media_create_pad_link(&camif->sensor.sd->entity, 0,
260 &camif->subdev.entity, CAMIF_SD_PAD_SINK,
261 MEDIA_LNK_FL_IMMUTABLE |
262 MEDIA_LNK_FL_ENABLED);
266 for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
267 ret = media_create_pad_link(&camif->subdev.entity, i,
268 &camif->vp[i - 1].vdev.entity, 0,
269 MEDIA_LNK_FL_IMMUTABLE |
270 MEDIA_LNK_FL_ENABLED);
276 static int camif_register_video_nodes(struct camif_dev *camif)
278 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
282 return s3c_camif_register_video_node(camif, VP_PREVIEW);
285 static void camif_unregister_video_nodes(struct camif_dev *camif)
287 s3c_camif_unregister_video_node(camif, VP_CODEC);
288 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
291 static void camif_unregister_media_entities(struct camif_dev *camif)
293 camif_unregister_video_nodes(camif);
294 camif_unregister_sensor(camif);
300 static int camif_media_dev_init(struct camif_dev *camif)
302 struct media_device *md = &camif->media_dev;
303 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
304 unsigned int ip_rev = camif->variant->ip_revision;
307 memset(md, 0, sizeof(*md));
308 snprintf(md->model, sizeof(md->model), "Samsung S3C%s CAMIF",
309 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
310 strscpy(md->bus_info, "platform", sizeof(md->bus_info));
311 md->hw_revision = ip_rev;
313 md->dev = camif->dev;
315 strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
318 media_device_init(md);
320 ret = v4l2_device_register(camif->dev, v4l2_dev);
327 static void camif_clk_put(struct camif_dev *camif)
331 for (i = 0; i < CLK_MAX_NUM; i++) {
332 if (IS_ERR(camif->clock[i]))
334 clk_unprepare(camif->clock[i]);
335 clk_put(camif->clock[i]);
336 camif->clock[i] = ERR_PTR(-EINVAL);
340 static int camif_clk_get(struct camif_dev *camif)
344 for (i = 1; i < CLK_MAX_NUM; i++)
345 camif->clock[i] = ERR_PTR(-EINVAL);
347 for (i = 0; i < CLK_MAX_NUM; i++) {
348 camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
349 if (IS_ERR(camif->clock[i])) {
350 ret = PTR_ERR(camif->clock[i]);
353 ret = clk_prepare(camif->clock[i]);
355 clk_put(camif->clock[i]);
356 camif->clock[i] = NULL;
362 camif_clk_put(camif);
363 dev_err(camif->dev, "failed to get clock: %s\n",
369 * The CAMIF device has two relatively independent data processing paths
370 * that can source data from memory or the common camera input frontend.
371 * Register interrupts for each data processing path (camif_vp).
373 static int camif_request_irqs(struct platform_device *pdev,
374 struct camif_dev *camif)
378 for (i = 0; i < CAMIF_VP_NUM; i++) {
379 struct camif_vp *vp = &camif->vp[i];
381 init_waitqueue_head(&vp->irq_queue);
383 irq = platform_get_irq(pdev, i);
387 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
388 0, dev_name(&pdev->dev), vp);
390 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
398 static int s3c_camif_probe(struct platform_device *pdev)
400 struct device *dev = &pdev->dev;
401 struct s3c_camif_plat_data *pdata = dev->platform_data;
402 struct s3c_camif_drvdata *drvdata;
403 struct camif_dev *camif;
406 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
410 spin_lock_init(&camif->slock);
411 mutex_init(&camif->lock);
415 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
416 dev_err(dev, "wrong platform data\n");
420 camif->pdata = *pdata;
421 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
422 camif->variant = drvdata->variant;
424 camif->io_base = devm_platform_ioremap_resource(pdev, 0);
425 if (IS_ERR(camif->io_base))
426 return PTR_ERR(camif->io_base);
428 ret = camif_request_irqs(pdev, camif);
432 ret = pdata->gpio_get();
436 ret = s3c_camif_create_subdev(camif);
440 ret = camif_clk_get(camif);
444 platform_set_drvdata(pdev, camif);
445 clk_set_rate(camif->clock[CLK_CAM],
446 camif->pdata.sensor.clock_frequency);
448 dev_info(dev, "sensor clock frequency: %lu\n",
449 clk_get_rate(camif->clock[CLK_CAM]));
451 * Set initial pixel format, resolution and crop rectangle.
452 * Must be done before a sensor subdev is registered as some
453 * settings are overrode with values from sensor subdev.
455 s3c_camif_set_defaults(camif);
457 pm_runtime_enable(dev);
459 ret = pm_runtime_resume_and_get(dev);
463 ret = camif_media_dev_init(camif);
467 ret = camif_register_sensor(camif);
471 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
475 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
479 ret = camif_register_video_nodes(camif);
483 ret = camif_create_media_links(camif);
487 ret = media_device_register(&camif->media_dev);
495 v4l2_device_unregister(&camif->v4l2_dev);
496 media_device_unregister(&camif->media_dev);
497 media_device_cleanup(&camif->media_dev);
498 camif_unregister_media_entities(camif);
502 pm_runtime_disable(dev);
503 camif_clk_put(camif);
505 s3c_camif_unregister_subdev(camif);
511 static void s3c_camif_remove(struct platform_device *pdev)
513 struct camif_dev *camif = platform_get_drvdata(pdev);
514 struct s3c_camif_plat_data *pdata = &camif->pdata;
516 media_device_unregister(&camif->media_dev);
517 media_device_cleanup(&camif->media_dev);
518 camif_unregister_media_entities(camif);
519 v4l2_device_unregister(&camif->v4l2_dev);
521 pm_runtime_disable(&pdev->dev);
522 camif_clk_put(camif);
523 s3c_camif_unregister_subdev(camif);
527 static int s3c_camif_runtime_resume(struct device *dev)
529 struct camif_dev *camif = dev_get_drvdata(dev);
531 clk_enable(camif->clock[CLK_GATE]);
532 /* null op on s3c244x */
533 clk_enable(camif->clock[CLK_CAM]);
537 static int s3c_camif_runtime_suspend(struct device *dev)
539 struct camif_dev *camif = dev_get_drvdata(dev);
541 /* null op on s3c244x */
542 clk_disable(camif->clock[CLK_CAM]);
544 clk_disable(camif->clock[CLK_GATE]);
548 static const struct s3c_camif_variant s3c244x_camif_variant = {
551 .max_out_width = 4096,
552 .max_sc_out_width = 2048,
553 .out_width_align = 16,
558 .max_out_width = 640,
559 .max_sc_out_width = 640,
560 .out_width_align = 16,
566 .win_hor_offset_align = 8,
568 .ip_revision = S3C244X_CAMIF_IP_REV,
571 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
572 .variant = &s3c244x_camif_variant,
573 .bus_clk_freq = 24000000UL,
576 static const struct s3c_camif_variant s3c6410_camif_variant = {
579 .max_out_width = 4096,
580 .max_sc_out_width = 2048,
581 .out_width_align = 16,
586 .max_out_width = 4096,
587 .max_sc_out_width = 720,
588 .out_width_align = 16,
594 .win_hor_offset_align = 8,
596 .ip_revision = S3C6410_CAMIF_IP_REV,
601 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
602 .variant = &s3c6410_camif_variant,
603 .bus_clk_freq = 133000000UL,
606 static const struct platform_device_id s3c_camif_driver_ids[] = {
608 .name = "s3c2440-camif",
609 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
611 .name = "s3c6410-camif",
612 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
616 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
618 static const struct dev_pm_ops s3c_camif_pm_ops = {
619 .runtime_suspend = s3c_camif_runtime_suspend,
620 .runtime_resume = s3c_camif_runtime_resume,
623 static struct platform_driver s3c_camif_driver = {
624 .probe = s3c_camif_probe,
625 .remove_new = s3c_camif_remove,
626 .id_table = s3c_camif_driver_ids,
628 .name = S3C_CAMIF_DRIVER_NAME,
629 .pm = &s3c_camif_pm_ops,
633 module_platform_driver(s3c_camif_driver);
635 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
636 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
637 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
638 MODULE_LICENSE("GPL");