Drivers: media: remove __dev* attributes.
[deliverable/linux.git] / drivers / media / platform / s3c-camif / camif-core.c
CommitLineData
babde1c2
SN
1/*
2 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
3 *
4 * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
5 * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
6 *
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.
11 */
12#define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
13
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
31#include <media/media-device.h>
32#include <media/v4l2-ctrls.h>
33#include <media/v4l2-ioctl.h>
34#include <media/videobuf2-core.h>
35#include <media/videobuf2-dma-contig.h>
36
37#include "camif-core.h"
38
39static char *camif_clocks[CLK_MAX_NUM] = {
40 /* HCLK CAMIF clock */
41 [CLK_GATE] = "camif",
42 /* CAMIF / external camera sensor master clock */
43 [CLK_CAM] = "camera",
44};
45
46static const struct camif_fmt camif_formats[] = {
47 {
48 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
49 .fourcc = V4L2_PIX_FMT_YUV422P,
50 .depth = 16,
51 .ybpp = 1,
52 .color = IMG_FMT_YCBCR422P,
53 .colplanes = 3,
54 .flags = FMT_FL_S3C24XX_CODEC |
55 FMT_FL_S3C64XX,
56 }, {
57 .name = "YUV 4:2:0 planar, Y/Cb/Cr",
58 .fourcc = V4L2_PIX_FMT_YUV420,
59 .depth = 12,
60 .ybpp = 1,
61 .color = IMG_FMT_YCBCR420,
62 .colplanes = 3,
63 .flags = FMT_FL_S3C24XX_CODEC |
64 FMT_FL_S3C64XX,
65 }, {
66 .name = "YVU 4:2:0 planar, Y/Cr/Cb",
67 .fourcc = V4L2_PIX_FMT_YVU420,
68 .depth = 12,
69 .ybpp = 1,
70 .color = IMG_FMT_YCRCB420,
71 .colplanes = 3,
72 .flags = FMT_FL_S3C24XX_CODEC |
73 FMT_FL_S3C64XX,
74 }, {
75 .name = "RGB565, 16 bpp",
76 .fourcc = V4L2_PIX_FMT_RGB565X,
77 .depth = 16,
78 .ybpp = 2,
79 .color = IMG_FMT_RGB565,
80 .colplanes = 1,
81 .flags = FMT_FL_S3C24XX_PREVIEW |
82 FMT_FL_S3C64XX,
83 }, {
84 .name = "XRGB8888, 32 bpp",
85 .fourcc = V4L2_PIX_FMT_RGB32,
86 .depth = 32,
87 .ybpp = 4,
88 .color = IMG_FMT_XRGB8888,
89 .colplanes = 1,
90 .flags = FMT_FL_S3C24XX_PREVIEW |
91 FMT_FL_S3C64XX,
92 }, {
93 .name = "BGR666",
94 .fourcc = V4L2_PIX_FMT_BGR666,
95 .depth = 32,
96 .ybpp = 4,
97 .color = IMG_FMT_RGB666,
98 .colplanes = 1,
99 .flags = FMT_FL_S3C64XX,
100 }
101};
102
103/**
104 * s3c_camif_find_format() - lookup camif color format by fourcc or an index
105 * @pixelformat: fourcc to match, ignored if null
106 * @index: index to the camif_formats array, ignored if negative
107 */
108const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
109 const u32 *pixelformat,
110 int index)
111{
112 const struct camif_fmt *fmt, *def_fmt = NULL;
113 unsigned int i;
114 int id = 0;
115
116 if (index >= (int)ARRAY_SIZE(camif_formats))
117 return NULL;
118
119 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
120 fmt = &camif_formats[i];
121 if (vp && !(vp->fmt_flags & fmt->flags))
122 continue;
123 if (pixelformat && fmt->fourcc == *pixelformat)
124 return fmt;
125 if (index == id)
126 def_fmt = fmt;
127 id++;
128 }
129 return def_fmt;
130}
131
132static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
133{
134 unsigned int sh = 6;
135
136 if (src >= 64 * tar)
137 return -EINVAL;
138
139 while (sh--) {
140 unsigned int tmp = 1 << sh;
141 if (src >= tar * tmp) {
142 *shift = sh, *ratio = tmp;
143 return 0;
144 }
145 }
146 *shift = 0, *ratio = 1;
147 return 0;
148}
149
150int s3c_camif_get_scaler_config(struct camif_vp *vp,
151 struct camif_scaler *scaler)
152{
153 struct v4l2_rect *camif_crop = &vp->camif->camif_crop;
154 int source_x = camif_crop->width;
155 int source_y = camif_crop->height;
156 int target_x = vp->out_frame.rect.width;
157 int target_y = vp->out_frame.rect.height;
158 int ret;
159
160 if (vp->rotation == 90 || vp->rotation == 270)
161 swap(target_x, target_y);
162
163 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
164 &scaler->h_shift);
165 if (ret < 0)
166 return ret;
167
168 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
169 &scaler->v_shift);
170 if (ret < 0)
171 return ret;
172
173 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
174 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
175
176 scaler->main_h_ratio = (source_x << 8) / (target_x << scaler->h_shift);
177 scaler->main_v_ratio = (source_y << 8) / (target_y << scaler->v_shift);
178
179 scaler->scaleup_h = (target_x >= source_x);
180 scaler->scaleup_v = (target_y >= source_y);
181
182 scaler->copy = 0;
183
184 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
185 scaler->pre_h_ratio, scaler->h_shift,
186 scaler->pre_v_ratio, scaler->v_shift);
187
188 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
189 source_x, source_y, target_x, target_y,
190 scaler->scaleup_h, scaler->scaleup_v);
191
192 return 0;
193}
194
195static int camif_register_sensor(struct camif_dev *camif)
196{
197 struct s3c_camif_sensor_info *sensor = &camif->pdata.sensor;
198 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
199 struct i2c_adapter *adapter;
200 struct v4l2_subdev_format format;
201 struct v4l2_subdev *sd;
202 int ret;
203
204 camif->sensor.sd = NULL;
205
206 if (sensor->i2c_board_info.addr == 0)
207 return -EINVAL;
208
209 adapter = i2c_get_adapter(sensor->i2c_bus_num);
210 if (adapter == NULL) {
211 v4l2_warn(v4l2_dev, "failed to get I2C adapter %d\n",
212 sensor->i2c_bus_num);
213 return -EPROBE_DEFER;
214 }
215
216 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
217 &sensor->i2c_board_info, NULL);
218 if (sd == NULL) {
219 i2c_put_adapter(adapter);
220 v4l2_warn(v4l2_dev, "failed to acquire subdev %s\n",
221 sensor->i2c_board_info.type);
222 return -EPROBE_DEFER;
223 }
224 camif->sensor.sd = sd;
225
226 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
227
228 /* Get initial pixel format and set it at the camif sink pad */
229 format.pad = 0;
230 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
231 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
232
233 if (ret < 0)
234 return 0;
235
236 format.pad = CAMIF_SD_PAD_SINK;
237 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
238
239 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
240 format.format.width, format.format.height,
241 format.format.code);
242 return 0;
243}
244
245static void camif_unregister_sensor(struct camif_dev *camif)
246{
247 struct v4l2_subdev *sd = camif->sensor.sd;
248 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
249 struct i2c_adapter *adapter;
250
251 if (client == NULL)
252 return;
253
254 adapter = client->adapter;
255 v4l2_device_unregister_subdev(sd);
256 camif->sensor.sd = NULL;
257 i2c_unregister_device(client);
258 if (adapter)
259 i2c_put_adapter(adapter);
260}
261
262static int camif_create_media_links(struct camif_dev *camif)
263{
264 int i, ret;
265
266 ret = media_entity_create_link(&camif->sensor.sd->entity, 0,
267 &camif->subdev.entity, CAMIF_SD_PAD_SINK,
268 MEDIA_LNK_FL_IMMUTABLE |
269 MEDIA_LNK_FL_ENABLED);
270 if (ret)
271 return ret;
272
273 for (i = 1; i < CAMIF_SD_PADS_NUM && !ret; i++) {
274 ret = media_entity_create_link(&camif->subdev.entity, i,
275 &camif->vp[i - 1].vdev.entity, 0,
276 MEDIA_LNK_FL_IMMUTABLE |
277 MEDIA_LNK_FL_ENABLED);
278 }
279
280 return ret;
281}
282
283static int camif_register_video_nodes(struct camif_dev *camif)
284{
285 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
286 if (ret < 0)
287 return ret;
288
289 return s3c_camif_register_video_node(camif, VP_PREVIEW);
290}
291
292static void camif_unregister_video_nodes(struct camif_dev *camif)
293{
294 s3c_camif_unregister_video_node(camif, VP_CODEC);
295 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
296}
297
298static void camif_unregister_media_entities(struct camif_dev *camif)
299{
300 camif_unregister_video_nodes(camif);
301 camif_unregister_sensor(camif);
302 s3c_camif_unregister_subdev(camif);
303}
304
305/*
306 * Media device
307 */
308static int camif_media_dev_register(struct camif_dev *camif)
309{
310 struct media_device *md = &camif->media_dev;
311 struct v4l2_device *v4l2_dev = &camif->v4l2_dev;
312 unsigned int ip_rev = camif->variant->ip_revision;
313 int ret;
314
315 memset(md, 0, sizeof(*md));
316 snprintf(md->model, sizeof(md->model), "SAMSUNG S3C%s CAMIF",
317 ip_rev == S3C6410_CAMIF_IP_REV ? "6410" : "244X");
318 strlcpy(md->bus_info, "platform", sizeof(md->bus_info));
319 md->hw_revision = ip_rev;
320 md->driver_version = KERNEL_VERSION(1, 0, 0);
321
322 md->dev = camif->dev;
323
324 strlcpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
325 v4l2_dev->mdev = md;
326
327 ret = v4l2_device_register(camif->dev, v4l2_dev);
328 if (ret < 0)
329 return ret;
330
331 ret = media_device_register(md);
332 if (ret < 0)
333 v4l2_device_unregister(v4l2_dev);
334
335 return ret;
336}
337
338static void camif_clk_put(struct camif_dev *camif)
339{
340 int i;
341
342 for (i = 0; i < CLK_MAX_NUM; i++) {
343 if (IS_ERR_OR_NULL(camif->clock[i]))
344 continue;
345 clk_unprepare(camif->clock[i]);
346 clk_put(camif->clock[i]);
347 }
348}
349
350static int camif_clk_get(struct camif_dev *camif)
351{
352 int ret, i;
353
354 for (i = 0; i < CLK_MAX_NUM; i++) {
355 camif->clock[i] = clk_get(camif->dev, camif_clocks[i]);
356 if (IS_ERR(camif->clock[i])) {
357 ret = PTR_ERR(camif->clock[i]);
358 goto err;
359 }
360 ret = clk_prepare(camif->clock[i]);
361 if (ret < 0) {
362 clk_put(camif->clock[i]);
363 camif->clock[i] = NULL;
364 goto err;
365 }
366 }
367 return 0;
368err:
369 camif_clk_put(camif);
370 dev_err(camif->dev, "failed to get clock: %s\n",
371 camif_clocks[i]);
372 return ret;
373}
374
375/*
376 * The CAMIF device has two relatively independent data processing paths
377 * that can source data from memory or the common camera input frontend.
378 * Register interrupts for each data processing path (camif_vp).
379 */
380static int camif_request_irqs(struct platform_device *pdev,
381 struct camif_dev *camif)
382{
383 int irq, ret, i;
384
385 for (i = 0; i < CAMIF_VP_NUM; i++) {
386 struct camif_vp *vp = &camif->vp[i];
387
388 init_waitqueue_head(&vp->irq_queue);
389
390 irq = platform_get_irq(pdev, i);
391 if (irq <= 0) {
392 dev_err(&pdev->dev, "failed to get IRQ %d\n", i);
393 return -ENXIO;
394 }
395
396 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
397 0, dev_name(&pdev->dev), vp);
398 if (ret < 0) {
399 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
400 break;
401 }
402 }
403
404 return ret;
405}
406
407static int s3c_camif_probe(struct platform_device *pdev)
408{
409 struct device *dev = &pdev->dev;
410 struct s3c_camif_plat_data *pdata = dev->platform_data;
411 struct s3c_camif_drvdata *drvdata;
412 struct camif_dev *camif;
413 struct resource *mres;
414 int ret = 0;
415
416 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
417 if (!camif)
418 return -ENOMEM;
419
420 spin_lock_init(&camif->slock);
421 mutex_init(&camif->lock);
422
423 camif->dev = dev;
424
425 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
426 dev_err(dev, "wrong platform data\n");
427 return -EINVAL;
428 }
429
430 camif->pdata = *pdata;
431 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
432 camif->variant = drvdata->variant;
433
434 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
435
436 camif->io_base = devm_request_and_ioremap(dev, mres);
437 if (!camif->io_base) {
438 dev_err(dev, "failed to obtain I/O memory\n");
439 return -ENOENT;
440 }
441
442 ret = camif_request_irqs(pdev, camif);
443 if (ret < 0)
444 return ret;
445
446 ret = pdata->gpio_get();
447 if (ret < 0)
448 return ret;
449
450 ret = s3c_camif_create_subdev(camif);
451 if (ret < 0)
452 goto err_sd;
453
454 ret = camif_clk_get(camif);
455 if (ret < 0)
456 goto err_clk;
457
458 platform_set_drvdata(pdev, camif);
459 clk_set_rate(camif->clock[CLK_CAM],
460 camif->pdata.sensor.clock_frequency);
461
462 dev_info(dev, "sensor clock frequency: %lu\n",
463 clk_get_rate(camif->clock[CLK_CAM]));
464 /*
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.
468 */
469 s3c_camif_set_defaults(camif);
470
471 pm_runtime_enable(dev);
472
473 ret = pm_runtime_get_sync(dev);
474 if (ret < 0)
475 goto err_pm;
476
477 /* Initialize contiguous memory allocator */
478 camif->alloc_ctx = vb2_dma_contig_init_ctx(dev);
479 if (IS_ERR(camif->alloc_ctx)) {
480 ret = PTR_ERR(camif->alloc_ctx);
481 goto err_alloc;
482 }
483
484 ret = camif_media_dev_register(camif);
485 if (ret < 0)
486 goto err_mdev;
487
488 ret = camif_register_sensor(camif);
489 if (ret < 0)
490 goto err_sens;
491
492 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
493 if (ret < 0)
494 goto err_sens;
495
496 mutex_lock(&camif->media_dev.graph_mutex);
497
498 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
499 if (ret < 0)
500 goto err_unlock;
501
502 ret = camif_register_video_nodes(camif);
503 if (ret < 0)
504 goto err_unlock;
505
506 ret = camif_create_media_links(camif);
507 if (ret < 0)
508 goto err_unlock;
509
510 mutex_unlock(&camif->media_dev.graph_mutex);
511 pm_runtime_put(dev);
512 return 0;
513
514err_unlock:
515 mutex_unlock(&camif->media_dev.graph_mutex);
516err_sens:
517 v4l2_device_unregister(&camif->v4l2_dev);
518 media_device_unregister(&camif->media_dev);
519 camif_unregister_media_entities(camif);
520err_mdev:
521 vb2_dma_contig_cleanup_ctx(camif->alloc_ctx);
522err_alloc:
523 pm_runtime_put(dev);
524 pm_runtime_disable(dev);
525err_pm:
526 camif_clk_put(camif);
527err_clk:
528 s3c_camif_unregister_subdev(camif);
529err_sd:
530 pdata->gpio_put();
531 return ret;
532}
533
4c62e976 534static int s3c_camif_remove(struct platform_device *pdev)
babde1c2
SN
535{
536 struct camif_dev *camif = platform_get_drvdata(pdev);
537 struct s3c_camif_plat_data *pdata = &camif->pdata;
538
539 media_device_unregister(&camif->media_dev);
540 camif_unregister_media_entities(camif);
541 v4l2_device_unregister(&camif->v4l2_dev);
542
543 pm_runtime_disable(&pdev->dev);
544 camif_clk_put(camif);
545 pdata->gpio_put();
546
547 return 0;
548}
549
550static int s3c_camif_runtime_resume(struct device *dev)
551{
552 struct camif_dev *camif = dev_get_drvdata(dev);
553
554 clk_enable(camif->clock[CLK_GATE]);
555 /* null op on s3c244x */
556 clk_enable(camif->clock[CLK_CAM]);
557 return 0;
558}
559
560static int s3c_camif_runtime_suspend(struct device *dev)
561{
562 struct camif_dev *camif = dev_get_drvdata(dev);
563
564 /* null op on s3c244x */
565 clk_disable(camif->clock[CLK_CAM]);
566
567 clk_disable(camif->clock[CLK_GATE]);
568 return 0;
569}
570
571static const struct s3c_camif_variant s3c244x_camif_variant = {
572 .vp_pix_limits = {
573 [VP_CODEC] = {
574 .max_out_width = 4096,
575 .max_sc_out_width = 2048,
576 .out_width_align = 16,
577 .min_out_width = 16,
578 .max_height = 4096,
579 },
580 [VP_PREVIEW] = {
581 .max_out_width = 640,
582 .max_sc_out_width = 640,
583 .out_width_align = 16,
584 .min_out_width = 16,
585 .max_height = 480,
586 }
587 },
588 .pix_limits = {
589 .win_hor_offset_align = 8,
590 },
591 .ip_revision = S3C244X_CAMIF_IP_REV,
592};
593
594static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
595 .variant = &s3c244x_camif_variant,
596 .bus_clk_freq = 24000000UL,
597};
598
599static const struct s3c_camif_variant s3c6410_camif_variant = {
600 .vp_pix_limits = {
601 [VP_CODEC] = {
602 .max_out_width = 4096,
603 .max_sc_out_width = 2048,
604 .out_width_align = 16,
605 .min_out_width = 16,
606 .max_height = 4096,
607 },
608 [VP_PREVIEW] = {
609 .max_out_width = 4096,
610 .max_sc_out_width = 720,
611 .out_width_align = 16,
612 .min_out_width = 16,
613 .max_height = 4096,
614 }
615 },
616 .pix_limits = {
617 .win_hor_offset_align = 8,
618 },
619 .ip_revision = S3C6410_CAMIF_IP_REV,
620 .has_img_effect = 1,
621 .vp_offset = 0x20,
622};
623
624static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
625 .variant = &s3c6410_camif_variant,
626 .bus_clk_freq = 133000000UL,
627};
628
629static struct platform_device_id s3c_camif_driver_ids[] = {
630 {
631 .name = "s3c2440-camif",
632 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
633 }, {
634 .name = "s3c6410-camif",
635 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
636 },
637 { /* sentinel */ },
638};
639MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
640
641static const struct dev_pm_ops s3c_camif_pm_ops = {
642 .runtime_suspend = s3c_camif_runtime_suspend,
643 .runtime_resume = s3c_camif_runtime_resume,
644};
645
646static struct platform_driver s3c_camif_driver = {
647 .probe = s3c_camif_probe,
4c62e976 648 .remove = s3c_camif_remove,
babde1c2
SN
649 .id_table = s3c_camif_driver_ids,
650 .driver = {
651 .name = S3C_CAMIF_DRIVER_NAME,
652 .owner = THIS_MODULE,
653 .pm = &s3c_camif_pm_ops,
654 }
655};
656
657module_platform_driver(s3c_camif_driver);
658
659MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
660MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
661MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
662MODULE_LICENSE("GPL");
This page took 0.053406 seconds and 5 git commands to generate.