Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / s3c-camif / camif-core.c
blob79bc0ef6bb413d8cd15aaaea3662444d2b047635
1 /*
2 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
4 * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
5 * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
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.
12 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
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>
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>
38 #include "camif-core.h"
40 static 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",
47 static const struct camif_fmt camif_formats[] = {
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,
105 * s3c_camif_find_format() - lookup camif color format by fourcc or an index
106 * @vp: video path (DMA) description (codec/preview)
107 * @pixelformat: fourcc to match, ignored if null
108 * @index: index to the camif_formats array, ignored if negative
110 const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
111 const u32 *pixelformat,
112 int index)
114 const struct camif_fmt *fmt, *def_fmt = NULL;
115 unsigned int i;
116 int id = 0;
118 if (index >= (int)ARRAY_SIZE(camif_formats))
119 return NULL;
121 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
122 fmt = &camif_formats[i];
123 if (vp && !(vp->fmt_flags & fmt->flags))
124 continue;
125 if (pixelformat && fmt->fourcc == *pixelformat)
126 return fmt;
127 if (index == id)
128 def_fmt = fmt;
129 id++;
131 return def_fmt;
134 static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
136 unsigned int sh = 6;
138 if (src >= 64 * tar)
139 return -EINVAL;
141 while (sh--) {
142 unsigned int tmp = 1 << sh;
143 if (src >= tar * tmp) {
144 *shift = sh, *ratio = tmp;
145 return 0;
148 *shift = 0, *ratio = 1;
149 return 0;
152 int s3c_camif_get_scaler_config(struct camif_vp *vp,
153 struct camif_scaler *scaler)
155 struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
156 int source_x = camif_crop->width;
157 int source_y = camif_crop->height;
158 int target_x = vp->out_frame.rect.width;
159 int target_y = vp->out_frame.rect.height;
160 int ret;
162 if (vp->rotation == 90 || vp->rotation == 270)
163 swap(target_x, target_y);
165 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
166 &scaler->h_shift);
167 if (ret < 0)
168 return ret;
170 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
171 &scaler->v_shift);
172 if (ret < 0)
173 return ret;
175 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
176 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
178 scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
179 scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
181 scaler->scaleup_h = (target_x >= source_x);
182 scaler->scaleup_v = (target_y >= source_y);
184 scaler->copy = 0;
186 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
187 scaler->pre_h_ratio, scaler->h_shift,
188 scaler->pre_v_ratio, scaler->v_shift);
190 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
191 source_x, source_y, target_x, target_y,
192 scaler->scaleup_h, scaler->scaleup_v);
194 return 0;
197 static int camif_register_sensor(struct camif_dev *camif)
199 struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
200 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
201 struct i2c_adapter *adapter;
202 struct v4l2_subdev_format format;
203 struct v4l2_subdev *sd;
204 int ret;
206 camif->sensor.sd = NULL;
208 if (sensor->i2c_board_info.addr == 0)
209 return -EINVAL;
211 adapter = i2c_get_adapter(sensor->i2c_bus_num);
212 if (adapter == NULL) {
213 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
214 sensor->i2c_bus_num);
215 return -EPROBE_DEFER;
218 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
219 &sensor->i2c_board_info, NULL);
220 if (sd == NULL) {
221 i2c_put_adapter(adapter);
222 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
223 sensor->i2c_board_info.type);
224 return -EPROBE_DEFER;
226 camif->sensor.sd = sd;
228 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
230 /* Get initial pixel format and set it at the camif sink pad */
231 format.pad = 0;
232 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
233 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
235 if (ret < 0)
236 return 0;
238 format.pad = CAMIF_SD_PAD_SINK;
239 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
241 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
242 format.format.width, format.format.height,
243 format.format.code);
244 return 0;
247 static void camif_unregister_sensor(struct camif_dev *camif)
249 struct v4l2_subdev *sd = camif->sensor.sd;
250 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
251 struct i2c_adapter *adapter;
253 if (client == NULL)
254 return;
256 adapter = client->adapter;
257 v4l2_device_unregister_subdev(sd);
258 camif->sensor.sd = NULL;
259 i2c_unregister_device(client);
260 i2c_put_adapter(adapter);
263 static int camif_create_media_links(struct camif_dev *camif)
265 int i, ret;
267 ret = media_create_pad_link(&camif->sensor.sd->entity, 0,
268 &camif->subdev.entity, CAMIF_SD_PAD_SINK,
269 MEDIA_LNK_FL_IMMUTABLE |
270 MEDIA_LNK_FL_ENABLED);
271 if (ret)
272 return ret;
274 for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
275 ret = media_create_pad_link(&camif->subdev.entity, i,
276 &camif->vp[i - 1].vdev.entity, 0,
277 MEDIA_LNK_FL_IMMUTABLE |
278 MEDIA_LNK_FL_ENABLED);
281 return ret;
284 static int camif_register_video_nodes(struct camif_dev *camif)
286 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
287 if (ret < 0)
288 return ret;
290 return s3c_camif_register_video_node(camif, VP_PREVIEW);
293 static void camif_unregister_video_nodes(struct camif_dev *camif)
295 s3c_camif_unregister_video_node(camif, VP_CODEC);
296 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
299 static void camif_unregister_media_entities(struct camif_dev *camif)
301 camif_unregister_video_nodes(camif);
302 camif_unregister_sensor(camif);
303 s3c_camif_unregister_subdev(camif);
307 * Media device
309 static int camif_media_dev_init(struct camif_dev *camif)
311 struct media_device *md = &camif->media_dev;
312 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
313 unsigned int ip_rev = camif->variant->ip_revision;
314 int ret;
316 memset(md, 0, sizeof(*md));
317 snprintf(md->model, sizeof(md->model), "SAMSUNG S3C%s CAMIF",
318 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
319 strlcpy(md->bus_info, "platform", sizeof(md->bus_info));
320 md->hw_revision = ip_rev;
322 md->dev = camif->dev;
324 strlcpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
325 v4l2_dev->mdev = md;
327 media_device_init(md);
329 ret = v4l2_device_register(camif->dev, v4l2_dev);
330 if (ret < 0)
331 return ret;
333 return ret;
336 static void camif_clk_put(struct camif_dev *camif)
338 int i;
340 for (i = 0; i < CLK_MAX_NUM; i++) {
341 if (IS_ERR(camif->clock[i]))
342 continue;
343 clk_unprepare(camif->clock[i]);
344 clk_put(camif->clock[i]);
345 camif->clock[i] = ERR_PTR(-EINVAL);
349 static int camif_clk_get(struct camif_dev *camif)
351 int ret, i;
353 for (i = 1; i < CLK_MAX_NUM; i++)
354 camif->clock[i] = ERR_PTR(-EINVAL);
356 for (i = 0; i < CLK_MAX_NUM; i++) {
357 camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
358 if (IS_ERR(camif->clock[i])) {
359 ret = PTR_ERR(camif->clock[i]);
360 goto err;
362 ret = clk_prepare(camif->clock[i]);
363 if (ret < 0) {
364 clk_put(camif->clock[i]);
365 camif->clock[i] = NULL;
366 goto err;
369 return 0;
370 err:
371 camif_clk_put(camif);
372 dev_err(camif->dev, "failed to get clock: %s\n",
373 camif_clocks[i]);
374 return ret;
378 * The CAMIF device has two relatively independent data processing paths
379 * that can source data from memory or the common camera input frontend.
380 * Register interrupts for each data processing path (camif_vp).
382 static int camif_request_irqs(struct platform_device *pdev,
383 struct camif_dev *camif)
385 int irq, ret, i;
387 for (i = 0; i < CAMIF_VP_NUM; i++) {
388 struct camif_vp *vp = &camif->vp[i];
390 init_waitqueue_head(&vp->irq_queue);
392 irq = platform_get_irq(pdev, i);
393 if (irq <= 0) {
394 dev_err(&pdev->dev, "failed to get IRQ %d\n", i);
395 return -ENXIO;
398 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
399 0, dev_name(&pdev->dev), vp);
400 if (ret < 0) {
401 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
402 break;
406 return ret;
409 static int s3c_camif_probe(struct platform_device *pdev)
411 struct device *dev = &pdev->dev;
412 struct s3c_camif_plat_data *pdata = dev->platform_data;
413 struct s3c_camif_drvdata *drvdata;
414 struct camif_dev *camif;
415 struct resource *mres;
416 int ret = 0;
418 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
419 if (!camif)
420 return -ENOMEM;
422 spin_lock_init(&camif->slock);
423 mutex_init(&camif->lock);
425 camif->dev = dev;
427 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
428 dev_err(dev, "wrong platform data\n");
429 return -EINVAL;
432 camif->pdata = *pdata;
433 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
434 camif->variant = drvdata->variant;
436 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 camif->io_base = devm_ioremap_resource(dev, mres);
439 if (IS_ERR(camif->io_base))
440 return PTR_ERR(camif->io_base);
442 ret = camif_request_irqs(pdev, camif);
443 if (ret < 0)
444 return ret;
446 ret = pdata->gpio_get();
447 if (ret < 0)
448 return ret;
450 ret = s3c_camif_create_subdev(camif);
451 if (ret < 0)
452 goto err_sd;
454 ret = camif_clk_get(camif);
455 if (ret < 0)
456 goto err_clk;
458 platform_set_drvdata(pdev, camif);
459 clk_set_rate(camif->clock[CLK_CAM],
460 camif->pdata.sensor.clock_frequency);
462 dev_info(dev, "sensor clock frequency: %lu\n",
463 clk_get_rate(camif->clock[CLK_CAM]));
465 * Set initial pixel format, resolution and crop rectangle.
466 * Must be done before a sensor subdev is registered as some
467 * settings are overrode with values from sensor subdev.
469 s3c_camif_set_defaults(camif);
471 pm_runtime_enable(dev);
473 ret = pm_runtime_get_sync(dev);
474 if (ret < 0)
475 goto err_pm;
477 ret = camif_media_dev_init(camif);
478 if (ret < 0)
479 goto err_alloc;
481 ret = camif_register_sensor(camif);
482 if (ret < 0)
483 goto err_sens;
485 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
486 if (ret < 0)
487 goto err_sens;
489 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
490 if (ret < 0)
491 goto err_sens;
493 ret = camif_register_video_nodes(camif);
494 if (ret < 0)
495 goto err_sens;
497 ret = camif_create_media_links(camif);
498 if (ret < 0)
499 goto err_sens;
501 ret = media_device_register(&camif->media_dev);
502 if (ret < 0)
503 goto err_sens;
505 pm_runtime_put(dev);
506 return 0;
508 err_sens:
509 v4l2_device_unregister(&camif->v4l2_dev);
510 media_device_unregister(&camif->media_dev);
511 media_device_cleanup(&camif->media_dev);
512 camif_unregister_media_entities(camif);
513 err_alloc:
514 pm_runtime_put(dev);
515 pm_runtime_disable(dev);
516 err_pm:
517 camif_clk_put(camif);
518 err_clk:
519 s3c_camif_unregister_subdev(camif);
520 err_sd:
521 pdata->gpio_put();
522 return ret;
525 static int s3c_camif_remove(struct platform_device *pdev)
527 struct camif_dev *camif = platform_get_drvdata(pdev);
528 struct s3c_camif_plat_data *pdata = &camif->pdata;
530 media_device_unregister(&camif->media_dev);
531 media_device_cleanup(&camif->media_dev);
532 camif_unregister_media_entities(camif);
533 v4l2_device_unregister(&camif->v4l2_dev);
535 pm_runtime_disable(&pdev->dev);
536 camif_clk_put(camif);
537 pdata->gpio_put();
539 return 0;
542 static int s3c_camif_runtime_resume(struct device *dev)
544 struct camif_dev *camif = dev_get_drvdata(dev);
546 clk_enable(camif->clock[CLK_GATE]);
547 /* null op on s3c244x */
548 clk_enable(camif->clock[CLK_CAM]);
549 return 0;
552 static int s3c_camif_runtime_suspend(struct device *dev)
554 struct camif_dev *camif = dev_get_drvdata(dev);
556 /* null op on s3c244x */
557 clk_disable(camif->clock[CLK_CAM]);
559 clk_disable(camif->clock[CLK_GATE]);
560 return 0;
563 static const struct s3c_camif_variant s3c244x_camif_variant = {
564 .vp_pix_limits = {
565 [VP_CODEC] = {
566 .max_out_width = 4096,
567 .max_sc_out_width = 2048,
568 .out_width_align = 16,
569 .min_out_width = 16,
570 .max_height = 4096,
572 [VP_PREVIEW] = {
573 .max_out_width = 640,
574 .max_sc_out_width = 640,
575 .out_width_align = 16,
576 .min_out_width = 16,
577 .max_height = 480,
580 .pix_limits = {
581 .win_hor_offset_align = 8,
583 .ip_revision = S3C244X_CAMIF_IP_REV,
586 static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
587 .variant = &s3c244x_camif_variant,
588 .bus_clk_freq = 24000000UL,
591 static const struct s3c_camif_variant s3c6410_camif_variant = {
592 .vp_pix_limits = {
593 [VP_CODEC] = {
594 .max_out_width = 4096,
595 .max_sc_out_width = 2048,
596 .out_width_align = 16,
597 .min_out_width = 16,
598 .max_height = 4096,
600 [VP_PREVIEW] = {
601 .max_out_width = 4096,
602 .max_sc_out_width = 720,
603 .out_width_align = 16,
604 .min_out_width = 16,
605 .max_height = 4096,
608 .pix_limits = {
609 .win_hor_offset_align = 8,
611 .ip_revision = S3C6410_CAMIF_IP_REV,
612 .has_img_effect = 1,
613 .vp_offset = 0x20,
616 static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
617 .variant = &s3c6410_camif_variant,
618 .bus_clk_freq = 133000000UL,
621 static const struct platform_device_id s3c_camif_driver_ids[] = {
623 .name = "s3c2440-camif",
624 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
625 }, {
626 .name = "s3c6410-camif",
627 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
629 { /* sentinel */ },
631 MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
633 static const struct dev_pm_ops s3c_camif_pm_ops = {
634 .runtime_suspend = s3c_camif_runtime_suspend,
635 .runtime_resume = s3c_camif_runtime_resume,
638 static struct platform_driver s3c_camif_driver = {
639 .probe = s3c_camif_probe,
640 .remove = s3c_camif_remove,
641 .id_table = s3c_camif_driver_ids,
642 .driver = {
643 .name = S3C_CAMIF_DRIVER_NAME,
644 .pm = &s3c_camif_pm_ops,
648 module_platform_driver(s3c_camif_driver);
650 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
651 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
652 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
653 MODULE_LICENSE("GPL");