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 #include <linux/version.h>
27
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>
33
34 #include "camif-core.h"
35
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",
41 };
42
43 static const struct camif_fmt camif_formats[] = {
44 {
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,
91 }
92 };
93
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
99 */
s3c_camif_find_format(struct camif_vp * vp,const u32 * pixelformat,int index)100 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
101 const u32 *pixelformat,
102 int index)
103 {
104 const struct camif_fmt *fmt, *def_fmt = NULL;
105 unsigned int i;
106 int id = 0;
107
108 if (index >= (int)ARRAY_SIZE(camif_formats))
109 return NULL;
110
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++;
120 }
121 return def_fmt;
122 }
123
camif_get_scaler_factor(u32 src,u32 tar,u32 * ratio,u32 * shift)124 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
125 {
126 unsigned int sh = 6;
127
128 if (src >= 64 * tar)
129 return -EINVAL;
130
131 while (sh--) {
132 unsigned int tmp = 1 << sh;
133 if (src >= tar * tmp) {
134 *shift = sh, *ratio = tmp;
135 return 0;
136 }
137 }
138 *shift = 0, *ratio = 1;
139 return 0;
140 }
141
s3c_camif_get_scaler_config(struct camif_vp * vp,struct camif_scaler * scaler)142 int s3c_camif_get_scaler_config(struct camif_vp *vp,
143 struct camif_scaler *scaler)
144 {
145 struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
146 int source_x = camif_crop->width;
147 int source_y = camif_crop->height;
148 int target_x = vp->out_frame.rect.width;
149 int target_y = vp->out_frame.rect.height;
150 int ret;
151
152 if (vp->rotation == 90 || vp->rotation == 270)
153 swap(target_x, target_y);
154
155 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
156 &scaler->h_shift);
157 if (ret < 0)
158 return ret;
159
160 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
161 &scaler->v_shift);
162 if (ret < 0)
163 return ret;
164
165 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
166 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
167
168 scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
169 scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
170
171 scaler->scaleup_h = (target_x >= source_x);
172 scaler->scaleup_v = (target_y >= source_y);
173
174 scaler->copy = 0;
175
176 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
177 scaler->pre_h_ratio, scaler->h_shift,
178 scaler->pre_v_ratio, scaler->v_shift);
179
180 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
181 source_x, source_y, target_x, target_y,
182 scaler->scaleup_h, scaler->scaleup_v);
183
184 return 0;
185 }
186
camif_register_sensor(struct camif_dev * camif)187 static int camif_register_sensor(struct camif_dev *camif)
188 {
189 struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
190 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
191 struct i2c_adapter *adapter;
192 struct v4l2_subdev_format format;
193 struct v4l2_subdev *sd;
194 int ret;
195
196 camif->sensor.sd = NULL;
197
198 if (sensor->i2c_board_info.addr == 0)
199 return -EINVAL;
200
201 adapter = i2c_get_adapter(sensor->i2c_bus_num);
202 if (adapter == NULL) {
203 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
204 sensor->i2c_bus_num);
205 return -EPROBE_DEFER;
206 }
207
208 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
209 &sensor->i2c_board_info, NULL);
210 if (sd == NULL) {
211 i2c_put_adapter(adapter);
212 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
213 sensor->i2c_board_info.type);
214 return -EPROBE_DEFER;
215 }
216 camif->sensor.sd = sd;
217
218 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
219
220 /* Get initial pixel format and set it at the camif sink pad */
221 format.pad = 0;
222 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
223 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
224
225 if (ret < 0)
226 return 0;
227
228 format.pad = CAMIF_SD_PAD_SINK;
229 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
230
231 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
232 format.format.width, format.format.height,
233 format.format.code);
234 return 0;
235 }
236
camif_unregister_sensor(struct camif_dev * camif)237 static void camif_unregister_sensor(struct camif_dev *camif)
238 {
239 struct v4l2_subdev *sd = camif->sensor.sd;
240 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
241 struct i2c_adapter *adapter;
242
243 if (client == NULL)
244 return;
245
246 adapter = client->adapter;
247 v4l2_device_unregister_subdev(sd);
248 camif->sensor.sd = NULL;
249 i2c_unregister_device(client);
250 i2c_put_adapter(adapter);
251 }
252
camif_create_media_links(struct camif_dev * camif)253 static int camif_create_media_links(struct camif_dev *camif)
254 {
255 int i, ret;
256
257 ret = media_create_pad_link(&camif->sensor.sd->entity, 0,
258 &camif->subdev.entity, CAMIF_SD_PAD_SINK,
259 MEDIA_LNK_FL_IMMUTABLE |
260 MEDIA_LNK_FL_ENABLED);
261 if (ret)
262 return ret;
263
264 for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
265 ret = media_create_pad_link(&camif->subdev.entity, i,
266 &camif->vp[i - 1].vdev.entity, 0,
267 MEDIA_LNK_FL_IMMUTABLE |
268 MEDIA_LNK_FL_ENABLED);
269 }
270
271 return ret;
272 }
273
camif_register_video_nodes(struct camif_dev * camif)274 static int camif_register_video_nodes(struct camif_dev *camif)
275 {
276 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
277 if (ret < 0)
278 return ret;
279
280 return s3c_camif_register_video_node(camif, VP_PREVIEW);
281 }
282
camif_unregister_video_nodes(struct camif_dev * camif)283 static void camif_unregister_video_nodes(struct camif_dev *camif)
284 {
285 s3c_camif_unregister_video_node(camif, VP_CODEC);
286 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
287 }
288
camif_unregister_media_entities(struct camif_dev * camif)289 static void camif_unregister_media_entities(struct camif_dev *camif)
290 {
291 camif_unregister_video_nodes(camif);
292 camif_unregister_sensor(camif);
293 s3c_camif_unregister_subdev(camif);
294 }
295
296 /*
297 * Media device
298 */
camif_media_dev_init(struct camif_dev * camif)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
camif_clk_put(struct camif_dev * camif)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
camif_clk_get(struct camif_dev * camif)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 */
camif_request_irqs(struct platform_device * pdev,struct camif_dev * camif)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
s3c_camif_probe(struct platform_device * pdev)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 struct resource *mres;
404 int ret = 0;
405
406 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
407 if (!camif)
408 return -ENOMEM;
409
410 spin_lock_init(&camif->slock);
411 mutex_init(&camif->lock);
412
413 camif->dev = dev;
414
415 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
416 dev_err(dev, "wrong platform data\n");
417 return -EINVAL;
418 }
419
420 camif->pdata = *pdata;
421 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
422 camif->variant = drvdata->variant;
423
424 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
425
426 camif->io_base = devm_ioremap_resource(dev, mres);
427 if (IS_ERR(camif->io_base))
428 return PTR_ERR(camif->io_base);
429
430 ret = camif_request_irqs(pdev, camif);
431 if (ret < 0)
432 return ret;
433
434 ret = pdata->gpio_get();
435 if (ret < 0)
436 return ret;
437
438 ret = s3c_camif_create_subdev(camif);
439 if (ret < 0)
440 goto err_sd;
441
442 ret = camif_clk_get(camif);
443 if (ret < 0)
444 goto err_clk;
445
446 platform_set_drvdata(pdev, camif);
447 clk_set_rate(camif->clock[CLK_CAM],
448 camif->pdata.sensor.clock_frequency);
449
450 dev_info(dev, "sensor clock frequency: %lu\n",
451 clk_get_rate(camif->clock[CLK_CAM]));
452 /*
453 * Set initial pixel format, resolution and crop rectangle.
454 * Must be done before a sensor subdev is registered as some
455 * settings are overrode with values from sensor subdev.
456 */
457 s3c_camif_set_defaults(camif);
458
459 pm_runtime_enable(dev);
460
461 ret = pm_runtime_get_sync(dev);
462 if (ret < 0)
463 goto err_pm;
464
465 ret = camif_media_dev_init(camif);
466 if (ret < 0)
467 goto err_pm;
468
469 ret = camif_register_sensor(camif);
470 if (ret < 0)
471 goto err_sens;
472
473 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
474 if (ret < 0)
475 goto err_sens;
476
477 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
478 if (ret < 0)
479 goto err_sens;
480
481 ret = camif_register_video_nodes(camif);
482 if (ret < 0)
483 goto err_sens;
484
485 ret = camif_create_media_links(camif);
486 if (ret < 0)
487 goto err_sens;
488
489 ret = media_device_register(&camif->media_dev);
490 if (ret < 0)
491 goto err_sens;
492
493 pm_runtime_put(dev);
494 return 0;
495
496 err_sens:
497 v4l2_device_unregister(&camif->v4l2_dev);
498 media_device_unregister(&camif->media_dev);
499 media_device_cleanup(&camif->media_dev);
500 camif_unregister_media_entities(camif);
501 err_pm:
502 pm_runtime_put(dev);
503 pm_runtime_disable(dev);
504 camif_clk_put(camif);
505 err_clk:
506 s3c_camif_unregister_subdev(camif);
507 err_sd:
508 pdata->gpio_put();
509 return ret;
510 }
511
s3c_camif_remove(struct platform_device * pdev)512 static int s3c_camif_remove(struct platform_device *pdev)
513 {
514 struct camif_dev *camif = platform_get_drvdata(pdev);
515 struct s3c_camif_plat_data *pdata = &camif->pdata;
516
517 media_device_unregister(&camif->media_dev);
518 media_device_cleanup(&camif->media_dev);
519 camif_unregister_media_entities(camif);
520 v4l2_device_unregister(&camif->v4l2_dev);
521
522 pm_runtime_disable(&pdev->dev);
523 camif_clk_put(camif);
524 pdata->gpio_put();
525
526 return 0;
527 }
528
s3c_camif_runtime_resume(struct device * dev)529 static int s3c_camif_runtime_resume(struct device *dev)
530 {
531 struct camif_dev *camif = dev_get_drvdata(dev);
532
533 clk_enable(camif->clock[CLK_GATE]);
534 /* null op on s3c244x */
535 clk_enable(camif->clock[CLK_CAM]);
536 return 0;
537 }
538
s3c_camif_runtime_suspend(struct device * dev)539 static int s3c_camif_runtime_suspend(struct device *dev)
540 {
541 struct camif_dev *camif = dev_get_drvdata(dev);
542
543 /* null op on s3c244x */
544 clk_disable(camif->clock[CLK_CAM]);
545
546 clk_disable(camif->clock[CLK_GATE]);
547 return 0;
548 }
549
550 static const struct s3c_camif_variant s3c244x_camif_variant = {
551 .vp_pix_limits = {
552 [VP_CODEC] = {
553 .max_out_width = 4096,
554 .max_sc_out_width = 2048,
555 .out_width_align = 16,
556 .min_out_width = 16,
557 .max_height = 4096,
558 },
559 [VP_PREVIEW] = {
560 .max_out_width = 640,
561 .max_sc_out_width = 640,
562 .out_width_align = 16,
563 .min_out_width = 16,
564 .max_height = 480,
565 }
566 },
567 .pix_limits = {
568 .win_hor_offset_align = 8,
569 },
570 .ip_revision = S3C244X_CAMIF_IP_REV,
571 };
572
573 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
574 .variant = &s3c244x_camif_variant,
575 .bus_clk_freq = 24000000UL,
576 };
577
578 static const struct s3c_camif_variant s3c6410_camif_variant = {
579 .vp_pix_limits = {
580 [VP_CODEC] = {
581 .max_out_width = 4096,
582 .max_sc_out_width = 2048,
583 .out_width_align = 16,
584 .min_out_width = 16,
585 .max_height = 4096,
586 },
587 [VP_PREVIEW] = {
588 .max_out_width = 4096,
589 .max_sc_out_width = 720,
590 .out_width_align = 16,
591 .min_out_width = 16,
592 .max_height = 4096,
593 }
594 },
595 .pix_limits = {
596 .win_hor_offset_align = 8,
597 },
598 .ip_revision = S3C6410_CAMIF_IP_REV,
599 .has_img_effect = 1,
600 .vp_offset = 0x20,
601 };
602
603 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
604 .variant = &s3c6410_camif_variant,
605 .bus_clk_freq = 133000000UL,
606 };
607
608 static const struct platform_device_id s3c_camif_driver_ids[] = {
609 {
610 .name = "s3c2440-camif",
611 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
612 }, {
613 .name = "s3c6410-camif",
614 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
615 },
616 { /* sentinel */ },
617 };
618 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
619
620 static const struct dev_pm_ops s3c_camif_pm_ops = {
621 .runtime_suspend = s3c_camif_runtime_suspend,
622 .runtime_resume = s3c_camif_runtime_resume,
623 };
624
625 static struct platform_driver s3c_camif_driver = {
626 .probe = s3c_camif_probe,
627 .remove = s3c_camif_remove,
628 .id_table = s3c_camif_driver_ids,
629 .driver = {
630 .name = S3C_CAMIF_DRIVER_NAME,
631 .pm = &s3c_camif_pm_ops,
632 }
633 };
634
635 module_platform_driver(s3c_camif_driver);
636
637 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
638 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
639 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
640 MODULE_LICENSE("GPL");
641