Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / platform / s3c-camif / camif-core.c
blob4c3c00d59c92043079e6a80af9603db8dbe1a505
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
7 */
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>
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 #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 */
38 [CLK_GATE] = "camif",
39 /* CAMIF / external camera sensor master clock */
40 [CLK_CAM] = "camera",
43 static const struct camif_fmt camif_formats[] = {
45 .fourcc = V4L2_PIX_FMT_YUV422P,
46 .depth = 16,
47 .ybpp = 1,
48 .color = IMG_FMT_YCBCR422P,
49 .colplanes = 3,
50 .flags = FMT_FL_S3C24XX_CODEC |
51 FMT_FL_S3C64XX,
52 }, {
53 .fourcc = V4L2_PIX_FMT_YUV420,
54 .depth = 12,
55 .ybpp = 1,
56 .color = IMG_FMT_YCBCR420,
57 .colplanes = 3,
58 .flags = FMT_FL_S3C24XX_CODEC |
59 FMT_FL_S3C64XX,
60 }, {
61 .fourcc = V4L2_PIX_FMT_YVU420,
62 .depth = 12,
63 .ybpp = 1,
64 .color = IMG_FMT_YCRCB420,
65 .colplanes = 3,
66 .flags = FMT_FL_S3C24XX_CODEC |
67 FMT_FL_S3C64XX,
68 }, {
69 .fourcc = V4L2_PIX_FMT_RGB565X,
70 .depth = 16,
71 .ybpp = 2,
72 .color = IMG_FMT_RGB565,
73 .colplanes = 1,
74 .flags = FMT_FL_S3C24XX_PREVIEW |
75 FMT_FL_S3C64XX,
76 }, {
77 .fourcc = V4L2_PIX_FMT_RGB32,
78 .depth = 32,
79 .ybpp = 4,
80 .color = IMG_FMT_XRGB8888,
81 .colplanes = 1,
82 .flags = FMT_FL_S3C24XX_PREVIEW |
83 FMT_FL_S3C64XX,
84 }, {
85 .fourcc = V4L2_PIX_FMT_BGR666,
86 .depth = 32,
87 .ybpp = 4,
88 .color = IMG_FMT_RGB666,
89 .colplanes = 1,
90 .flags = FMT_FL_S3C64XX,
94 /**
95 * s3c_camif_find_format() - lookup camif color format by fourcc or an index
96 * @vp: video path (DMA) description (codec/preview)
97 * @pixelformat: fourcc to match, ignored if null
98 * @index: index to the camif_formats array, ignored if negative
100 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
101 const u32 *pixelformat,
102 int index)
104 const struct camif_fmt *fmt, *def_fmt = NULL;
105 unsigned int i;
106 int id = 0;
108 if (index >= (int)ARRAY_SIZE(camif_formats))
109 return NULL;
111 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
112 fmt = &camif_formats[i];
113 if (vp && !(vp->fmt_flags & fmt->flags))
114 continue;
115 if (pixelformat && fmt->fourcc == *pixelformat)
116 return fmt;
117 if (index == id)
118 def_fmt = fmt;
119 id++;
121 return def_fmt;
124 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
126 unsigned int sh = 6;
128 if (src >= 64 * tar)
129 return -EINVAL;
131 while (sh--) {
132 unsigned int tmp = 1 << sh;
133 if (src >= tar * tmp) {
134 *shift = sh;
135 *ratio = tmp;
136 return 0;
139 *shift = 0;
140 *ratio = 1;
141 return 0;
144 int s3c_camif_get_scaler_config(struct camif_vp *vp,
145 struct camif_scaler *scaler)
147 struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
148 int source_x = camif_crop->width;
149 int source_y = camif_crop->height;
150 int target_x = vp->out_frame.rect.width;
151 int target_y = vp->out_frame.rect.height;
152 int ret;
154 if (vp->rotation == 90 || vp->rotation == 270)
155 swap(target_x, target_y);
157 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
158 &scaler->h_shift);
159 if (ret < 0)
160 return ret;
162 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
163 &scaler->v_shift);
164 if (ret < 0)
165 return ret;
167 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
168 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
170 scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
171 scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
173 scaler->scaleup_h = (target_x >= source_x);
174 scaler->scaleup_v = (target_y >= source_y);
176 scaler->copy = 0;
178 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
179 scaler->pre_h_ratio, scaler->h_shift,
180 scaler->pre_v_ratio, scaler->v_shift);
182 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
183 source_x, source_y, target_x, target_y,
184 scaler->scaleup_h, scaler->scaleup_v);
186 return 0;
189 static int camif_register_sensor(struct camif_dev *camif)
191 struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
192 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
193 struct i2c_adapter *adapter;
194 struct v4l2_subdev_format format;
195 struct v4l2_subdev *sd;
196 int ret;
198 camif->sensor.sd = NULL;
200 if (sensor->i2c_board_info.addr == 0)
201 return -EINVAL;
203 adapter = i2c_get_adapter(sensor->i2c_bus_num);
204 if (adapter == NULL) {
205 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
206 sensor->i2c_bus_num);
207 return -EPROBE_DEFER;
210 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
211 &sensor->i2c_board_info, NULL);
212 if (sd == NULL) {
213 i2c_put_adapter(adapter);
214 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
215 sensor->i2c_board_info.type);
216 return -EPROBE_DEFER;
218 camif->sensor.sd = sd;
220 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
222 /* Get initial pixel format and set it at the camif sink pad */
223 format.pad = 0;
224 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
225 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
227 if (ret < 0)
228 return 0;
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,
235 format.format.code);
236 return 0;
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;
245 if (client == NULL)
246 return;
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)
257 int i, ret;
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);
263 if (ret)
264 return ret;
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);
273 return ret;
276 static int camif_register_video_nodes(struct camif_dev *camif)
278 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
279 if (ret < 0)
280 return ret;
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);
295 s3c_camif_unregister_subdev(camif);
299 * Media device
301 static int camif_media_dev_init(struct camif_dev *camif)
303 struct media_device *md = &camif->media_dev;
304 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
305 unsigned int ip_rev = camif->variant->ip_revision;
306 int ret;
308 memset(md, 0, sizeof(*md));
309 snprintf(md->model, sizeof(md->model), "Samsung S3C%s CAMIF",
310 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
311 strscpy(md->bus_info, "platform", sizeof(md->bus_info));
312 md->hw_revision = ip_rev;
314 md->dev = camif->dev;
316 strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
317 v4l2_dev->mdev = md;
319 media_device_init(md);
321 ret = v4l2_device_register(camif->dev, v4l2_dev);
322 if (ret < 0)
323 return ret;
325 return ret;
328 static void camif_clk_put(struct camif_dev *camif)
330 int i;
332 for (i = 0; i < CLK_MAX_NUM; i++) {
333 if (IS_ERR(camif->clock[i]))
334 continue;
335 clk_unprepare(camif->clock[i]);
336 clk_put(camif->clock[i]);
337 camif->clock[i] = ERR_PTR(-EINVAL);
341 static int camif_clk_get(struct camif_dev *camif)
343 int ret, i;
345 for (i = 1; i < CLK_MAX_NUM; i++)
346 camif->clock[i] = ERR_PTR(-EINVAL);
348 for (i = 0; i < CLK_MAX_NUM; i++) {
349 camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
350 if (IS_ERR(camif->clock[i])) {
351 ret = PTR_ERR(camif->clock[i]);
352 goto err;
354 ret = clk_prepare(camif->clock[i]);
355 if (ret < 0) {
356 clk_put(camif->clock[i]);
357 camif->clock[i] = NULL;
358 goto err;
361 return 0;
362 err:
363 camif_clk_put(camif);
364 dev_err(camif->dev, "failed to get clock: %s\n",
365 camif_clocks[i]);
366 return ret;
370 * The CAMIF device has two relatively independent data processing paths
371 * that can source data from memory or the common camera input frontend.
372 * Register interrupts for each data processing path (camif_vp).
374 static int camif_request_irqs(struct platform_device *pdev,
375 struct camif_dev *camif)
377 int irq, ret, i;
379 for (i = 0; i < CAMIF_VP_NUM; i++) {
380 struct camif_vp *vp = &camif->vp[i];
382 init_waitqueue_head(&vp->irq_queue);
384 irq = platform_get_irq(pdev, i);
385 if (irq <= 0)
386 return -ENXIO;
388 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
389 0, dev_name(&pdev->dev), vp);
390 if (ret < 0) {
391 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
392 break;
396 return ret;
399 static int s3c_camif_probe(struct platform_device *pdev)
401 struct device *dev = &pdev->dev;
402 struct s3c_camif_plat_data *pdata = dev->platform_data;
403 struct s3c_camif_drvdata *drvdata;
404 struct camif_dev *camif;
405 struct resource *mres;
406 int ret = 0;
408 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
409 if (!camif)
410 return -ENOMEM;
412 spin_lock_init(&camif->slock);
413 mutex_init(&camif->lock);
415 camif->dev = dev;
417 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
418 dev_err(dev, "wrong platform data\n");
419 return -EINVAL;
422 camif->pdata = *pdata;
423 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
424 camif->variant = drvdata->variant;
426 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 camif->io_base = devm_ioremap_resource(dev, mres);
429 if (IS_ERR(camif->io_base))
430 return PTR_ERR(camif->io_base);
432 ret = camif_request_irqs(pdev, camif);
433 if (ret < 0)
434 return ret;
436 ret = pdata->gpio_get();
437 if (ret < 0)
438 return ret;
440 ret = s3c_camif_create_subdev(camif);
441 if (ret < 0)
442 goto err_sd;
444 ret = camif_clk_get(camif);
445 if (ret < 0)
446 goto err_clk;
448 platform_set_drvdata(pdev, camif);
449 clk_set_rate(camif->clock[CLK_CAM],
450 camif->pdata.sensor.clock_frequency);
452 dev_info(dev, "sensor clock frequency: %lu\n",
453 clk_get_rate(camif->clock[CLK_CAM]));
455 * Set initial pixel format, resolution and crop rectangle.
456 * Must be done before a sensor subdev is registered as some
457 * settings are overrode with values from sensor subdev.
459 s3c_camif_set_defaults(camif);
461 pm_runtime_enable(dev);
463 ret = pm_runtime_get_sync(dev);
464 if (ret < 0)
465 goto err_pm;
467 ret = camif_media_dev_init(camif);
468 if (ret < 0)
469 goto err_pm;
471 ret = camif_register_sensor(camif);
472 if (ret < 0)
473 goto err_sens;
475 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
476 if (ret < 0)
477 goto err_sens;
479 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
480 if (ret < 0)
481 goto err_sens;
483 ret = camif_register_video_nodes(camif);
484 if (ret < 0)
485 goto err_sens;
487 ret = camif_create_media_links(camif);
488 if (ret < 0)
489 goto err_sens;
491 ret = media_device_register(&camif->media_dev);
492 if (ret < 0)
493 goto err_sens;
495 pm_runtime_put(dev);
496 return 0;
498 err_sens:
499 v4l2_device_unregister(&camif->v4l2_dev);
500 media_device_unregister(&camif->media_dev);
501 media_device_cleanup(&camif->media_dev);
502 camif_unregister_media_entities(camif);
503 err_pm:
504 pm_runtime_put(dev);
505 pm_runtime_disable(dev);
506 camif_clk_put(camif);
507 err_clk:
508 s3c_camif_unregister_subdev(camif);
509 err_sd:
510 pdata->gpio_put();
511 return ret;
514 static int s3c_camif_remove(struct platform_device *pdev)
516 struct camif_dev *camif = platform_get_drvdata(pdev);
517 struct s3c_camif_plat_data *pdata = &camif->pdata;
519 media_device_unregister(&camif->media_dev);
520 media_device_cleanup(&camif->media_dev);
521 camif_unregister_media_entities(camif);
522 v4l2_device_unregister(&camif->v4l2_dev);
524 pm_runtime_disable(&pdev->dev);
525 camif_clk_put(camif);
526 pdata->gpio_put();
528 return 0;
531 static int s3c_camif_runtime_resume(struct device *dev)
533 struct camif_dev *camif = dev_get_drvdata(dev);
535 clk_enable(camif->clock[CLK_GATE]);
536 /* null op on s3c244x */
537 clk_enable(camif->clock[CLK_CAM]);
538 return 0;
541 static int s3c_camif_runtime_suspend(struct device *dev)
543 struct camif_dev *camif = dev_get_drvdata(dev);
545 /* null op on s3c244x */
546 clk_disable(camif->clock[CLK_CAM]);
548 clk_disable(camif->clock[CLK_GATE]);
549 return 0;
552 static const struct s3c_camif_variant s3c244x_camif_variant = {
553 .vp_pix_limits = {
554 [VP_CODEC] = {
555 .max_out_width = 4096,
556 .max_sc_out_width = 2048,
557 .out_width_align = 16,
558 .min_out_width = 16,
559 .max_height = 4096,
561 [VP_PREVIEW] = {
562 .max_out_width = 640,
563 .max_sc_out_width = 640,
564 .out_width_align = 16,
565 .min_out_width = 16,
566 .max_height = 480,
569 .pix_limits = {
570 .win_hor_offset_align = 8,
572 .ip_revision = S3C244X_CAMIF_IP_REV,
575 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
576 .variant = &s3c244x_camif_variant,
577 .bus_clk_freq = 24000000UL,
580 static const struct s3c_camif_variant s3c6410_camif_variant = {
581 .vp_pix_limits = {
582 [VP_CODEC] = {
583 .max_out_width = 4096,
584 .max_sc_out_width = 2048,
585 .out_width_align = 16,
586 .min_out_width = 16,
587 .max_height = 4096,
589 [VP_PREVIEW] = {
590 .max_out_width = 4096,
591 .max_sc_out_width = 720,
592 .out_width_align = 16,
593 .min_out_width = 16,
594 .max_height = 4096,
597 .pix_limits = {
598 .win_hor_offset_align = 8,
600 .ip_revision = S3C6410_CAMIF_IP_REV,
601 .has_img_effect = 1,
602 .vp_offset = 0x20,
605 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
606 .variant = &s3c6410_camif_variant,
607 .bus_clk_freq = 133000000UL,
610 static const struct platform_device_id s3c_camif_driver_ids[] = {
612 .name = "s3c2440-camif",
613 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
614 }, {
615 .name = "s3c6410-camif",
616 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
618 { /* sentinel */ },
620 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
622 static const struct dev_pm_ops s3c_camif_pm_ops = {
623 .runtime_suspend = s3c_camif_runtime_suspend,
624 .runtime_resume = s3c_camif_runtime_resume,
627 static struct platform_driver s3c_camif_driver = {
628 .probe = s3c_camif_probe,
629 .remove = s3c_camif_remove,
630 .id_table = s3c_camif_driver_ids,
631 .driver = {
632 .name = S3C_CAMIF_DRIVER_NAME,
633 .pm = &s3c_camif_pm_ops,
637 module_platform_driver(s3c_camif_driver);
639 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
640 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
641 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
642 MODULE_LICENSE("GPL");