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>
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-core.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 */
43 /* CAMIF / external camera sensor master clock */
47 static const struct camif_fmt camif_formats
[] = {
49 .name
= "YUV 4:2:2 planar, Y/Cb/Cr",
50 .fourcc
= V4L2_PIX_FMT_YUV422P
,
53 .color
= IMG_FMT_YCBCR422P
,
55 .flags
= FMT_FL_S3C24XX_CODEC
|
58 .name
= "YUV 4:2:0 planar, Y/Cb/Cr",
59 .fourcc
= V4L2_PIX_FMT_YUV420
,
62 .color
= IMG_FMT_YCBCR420
,
64 .flags
= FMT_FL_S3C24XX_CODEC
|
67 .name
= "YVU 4:2:0 planar, Y/Cr/Cb",
68 .fourcc
= V4L2_PIX_FMT_YVU420
,
71 .color
= IMG_FMT_YCRCB420
,
73 .flags
= FMT_FL_S3C24XX_CODEC
|
76 .name
= "RGB565, 16 bpp",
77 .fourcc
= V4L2_PIX_FMT_RGB565X
,
80 .color
= IMG_FMT_RGB565
,
82 .flags
= FMT_FL_S3C24XX_PREVIEW
|
85 .name
= "XRGB8888, 32 bpp",
86 .fourcc
= V4L2_PIX_FMT_RGB32
,
89 .color
= IMG_FMT_XRGB8888
,
91 .flags
= FMT_FL_S3C24XX_PREVIEW
|
95 .fourcc
= V4L2_PIX_FMT_BGR666
,
98 .color
= IMG_FMT_RGB666
,
100 .flags
= FMT_FL_S3C64XX
,
105 * s3c_camif_find_format() - lookup camif color format by fourcc or an index
106 * @pixelformat: fourcc to match, ignored if null
107 * @index: index to the camif_formats array, ignored if negative
109 const struct camif_fmt
*s3c_camif_find_format(struct camif_vp
*vp
,
110 const u32
*pixelformat
,
113 const struct camif_fmt
*fmt
, *def_fmt
= NULL
;
117 if (index
>= (int)ARRAY_SIZE(camif_formats
))
120 for (i
= 0; i
< ARRAY_SIZE(camif_formats
); ++i
) {
121 fmt
= &camif_formats
[i
];
122 if (vp
&& !(vp
->fmt_flags
& fmt
->flags
))
124 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
133 static int camif_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
141 unsigned int tmp
= 1 << sh
;
142 if (src
>= tar
* tmp
) {
143 *shift
= sh
, *ratio
= tmp
;
147 *shift
= 0, *ratio
= 1;
151 int s3c_camif_get_scaler_config(struct camif_vp
*vp
,
152 struct camif_scaler
*scaler
)
154 struct v4l2_rect
*camif_crop
= &vp
->camif
->camif_crop
;
155 int source_x
= camif_crop
->width
;
156 int source_y
= camif_crop
->height
;
157 int target_x
= vp
->out_frame
.rect
.width
;
158 int target_y
= vp
->out_frame
.rect
.height
;
161 if (vp
->rotation
== 90 || vp
->rotation
== 270)
162 swap(target_x
, target_y
);
164 ret
= camif_get_scaler_factor(source_x
, target_x
, &scaler
->pre_h_ratio
,
169 ret
= camif_get_scaler_factor(source_y
, target_y
, &scaler
->pre_v_ratio
,
174 scaler
->pre_dst_width
= source_x
/ scaler
->pre_h_ratio
;
175 scaler
->pre_dst_height
= source_y
/ scaler
->pre_v_ratio
;
177 scaler
->main_h_ratio
= (source_x
<< 8) / (target_x
<< scaler
->h_shift
);
178 scaler
->main_v_ratio
= (source_y
<< 8) / (target_y
<< scaler
->v_shift
);
180 scaler
->scaleup_h
= (target_x
>= source_x
);
181 scaler
->scaleup_v
= (target_y
>= source_y
);
185 pr_debug("H: ratio: %u, shift: %u. V: ratio: %u, shift: %u.\n",
186 scaler
->pre_h_ratio
, scaler
->h_shift
,
187 scaler
->pre_v_ratio
, scaler
->v_shift
);
189 pr_debug("Source: %dx%d, Target: %dx%d, scaleup_h/v: %d/%d\n",
190 source_x
, source_y
, target_x
, target_y
,
191 scaler
->scaleup_h
, scaler
->scaleup_v
);
196 static int camif_register_sensor(struct camif_dev
*camif
)
198 struct s3c_camif_sensor_info
*sensor
= &camif
->pdata
.sensor
;
199 struct v4l2_device
*v4l2_dev
= &camif
->v4l2_dev
;
200 struct i2c_adapter
*adapter
;
201 struct v4l2_subdev_format format
;
202 struct v4l2_subdev
*sd
;
205 camif
->sensor
.sd
= NULL
;
207 if (sensor
->i2c_board_info
.addr
== 0)
210 adapter
= i2c_get_adapter(sensor
->i2c_bus_num
);
211 if (adapter
== NULL
) {
212 v4l2_warn(v4l2_dev
, "failed to get I2C adapter %d\n",
213 sensor
->i2c_bus_num
);
214 return -EPROBE_DEFER
;
217 sd
= v4l2_i2c_new_subdev_board(v4l2_dev
, adapter
,
218 &sensor
->i2c_board_info
, NULL
);
220 i2c_put_adapter(adapter
);
221 v4l2_warn(v4l2_dev
, "failed to acquire subdev %s\n",
222 sensor
->i2c_board_info
.type
);
223 return -EPROBE_DEFER
;
225 camif
->sensor
.sd
= sd
;
227 v4l2_info(v4l2_dev
, "registered sensor subdevice %s\n", sd
->name
);
229 /* Get initial pixel format and set it at the camif sink pad */
231 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
232 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &format
);
237 format
.pad
= CAMIF_SD_PAD_SINK
;
238 v4l2_subdev_call(&camif
->subdev
, pad
, set_fmt
, NULL
, &format
);
240 v4l2_info(sd
, "Initial format from sensor: %dx%d, %#x\n",
241 format
.format
.width
, format
.format
.height
,
246 static void camif_unregister_sensor(struct camif_dev
*camif
)
248 struct v4l2_subdev
*sd
= camif
->sensor
.sd
;
249 struct i2c_client
*client
= sd
? v4l2_get_subdevdata(sd
) : NULL
;
250 struct i2c_adapter
*adapter
;
255 adapter
= client
->adapter
;
256 v4l2_device_unregister_subdev(sd
);
257 camif
->sensor
.sd
= NULL
;
258 i2c_unregister_device(client
);
260 i2c_put_adapter(adapter
);
263 static int camif_create_media_links(struct camif_dev
*camif
)
267 ret
= media_entity_create_link(&camif
->sensor
.sd
->entity
, 0,
268 &camif
->subdev
.entity
, CAMIF_SD_PAD_SINK
,
269 MEDIA_LNK_FL_IMMUTABLE
|
270 MEDIA_LNK_FL_ENABLED
);
274 for (i
= 1; i
< CAMIF_SD_PADS_NUM
&& !ret
; i
++) {
275 ret
= media_entity_create_link(&camif
->subdev
.entity
, i
,
276 &camif
->vp
[i
- 1].vdev
.entity
, 0,
277 MEDIA_LNK_FL_IMMUTABLE
|
278 MEDIA_LNK_FL_ENABLED
);
284 static int camif_register_video_nodes(struct camif_dev
*camif
)
286 int ret
= s3c_camif_register_video_node(camif
, VP_CODEC
);
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
);
309 static int camif_media_dev_register(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
;
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
;
321 md
->driver_version
= KERNEL_VERSION(1, 0, 0);
323 md
->dev
= camif
->dev
;
325 strlcpy(v4l2_dev
->name
, "s3c-camif", sizeof(v4l2_dev
->name
));
328 ret
= v4l2_device_register(camif
->dev
, v4l2_dev
);
332 ret
= media_device_register(md
);
334 v4l2_device_unregister(v4l2_dev
);
339 static void camif_clk_put(struct camif_dev
*camif
)
343 for (i
= 0; i
< CLK_MAX_NUM
; i
++) {
344 if (IS_ERR_OR_NULL(camif
->clock
[i
]))
346 clk_unprepare(camif
->clock
[i
]);
347 clk_put(camif
->clock
[i
]);
351 static int camif_clk_get(struct camif_dev
*camif
)
355 for (i
= 0; i
< CLK_MAX_NUM
; i
++) {
356 camif
->clock
[i
] = clk_get(camif
->dev
, camif_clocks
[i
]);
357 if (IS_ERR(camif
->clock
[i
])) {
358 ret
= PTR_ERR(camif
->clock
[i
]);
361 ret
= clk_prepare(camif
->clock
[i
]);
363 clk_put(camif
->clock
[i
]);
364 camif
->clock
[i
] = NULL
;
370 camif_clk_put(camif
);
371 dev_err(camif
->dev
, "failed to get clock: %s\n",
377 * The CAMIF device has two relatively independent data processing paths
378 * that can source data from memory or the common camera input frontend.
379 * Register interrupts for each data processing path (camif_vp).
381 static int camif_request_irqs(struct platform_device
*pdev
,
382 struct camif_dev
*camif
)
386 for (i
= 0; i
< CAMIF_VP_NUM
; i
++) {
387 struct camif_vp
*vp
= &camif
->vp
[i
];
389 init_waitqueue_head(&vp
->irq_queue
);
391 irq
= platform_get_irq(pdev
, i
);
393 dev_err(&pdev
->dev
, "failed to get IRQ %d\n", i
);
397 ret
= devm_request_irq(&pdev
->dev
, irq
, s3c_camif_irq_handler
,
398 0, dev_name(&pdev
->dev
), vp
);
400 dev_err(&pdev
->dev
, "failed to install IRQ: %d\n", ret
);
408 static int s3c_camif_probe(struct platform_device
*pdev
)
410 struct device
*dev
= &pdev
->dev
;
411 struct s3c_camif_plat_data
*pdata
= dev
->platform_data
;
412 struct s3c_camif_drvdata
*drvdata
;
413 struct camif_dev
*camif
;
414 struct resource
*mres
;
417 camif
= devm_kzalloc(dev
, sizeof(*camif
), GFP_KERNEL
);
421 spin_lock_init(&camif
->slock
);
422 mutex_init(&camif
->lock
);
426 if (!pdata
|| !pdata
->gpio_get
|| !pdata
->gpio_put
) {
427 dev_err(dev
, "wrong platform data\n");
431 camif
->pdata
= *pdata
;
432 drvdata
= (void *)platform_get_device_id(pdev
)->driver_data
;
433 camif
->variant
= drvdata
->variant
;
435 mres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
437 camif
->io_base
= devm_ioremap_resource(dev
, mres
);
438 if (IS_ERR(camif
->io_base
))
439 return PTR_ERR(camif
->io_base
);
441 ret
= camif_request_irqs(pdev
, camif
);
445 ret
= pdata
->gpio_get();
449 ret
= s3c_camif_create_subdev(camif
);
453 ret
= camif_clk_get(camif
);
457 platform_set_drvdata(pdev
, camif
);
458 clk_set_rate(camif
->clock
[CLK_CAM
],
459 camif
->pdata
.sensor
.clock_frequency
);
461 dev_info(dev
, "sensor clock frequency: %lu\n",
462 clk_get_rate(camif
->clock
[CLK_CAM
]));
464 * Set initial pixel format, resolution and crop rectangle.
465 * Must be done before a sensor subdev is registered as some
466 * settings are overrode with values from sensor subdev.
468 s3c_camif_set_defaults(camif
);
470 pm_runtime_enable(dev
);
472 ret
= pm_runtime_get_sync(dev
);
476 /* Initialize contiguous memory allocator */
477 camif
->alloc_ctx
= vb2_dma_contig_init_ctx(dev
);
478 if (IS_ERR(camif
->alloc_ctx
)) {
479 ret
= PTR_ERR(camif
->alloc_ctx
);
483 ret
= camif_media_dev_register(camif
);
487 ret
= camif_register_sensor(camif
);
491 ret
= v4l2_device_register_subdev(&camif
->v4l2_dev
, &camif
->subdev
);
495 mutex_lock(&camif
->media_dev
.graph_mutex
);
497 ret
= v4l2_device_register_subdev_nodes(&camif
->v4l2_dev
);
501 ret
= camif_register_video_nodes(camif
);
505 ret
= camif_create_media_links(camif
);
509 mutex_unlock(&camif
->media_dev
.graph_mutex
);
514 mutex_unlock(&camif
->media_dev
.graph_mutex
);
516 v4l2_device_unregister(&camif
->v4l2_dev
);
517 media_device_unregister(&camif
->media_dev
);
518 camif_unregister_media_entities(camif
);
520 vb2_dma_contig_cleanup_ctx(camif
->alloc_ctx
);
523 pm_runtime_disable(dev
);
525 camif_clk_put(camif
);
527 s3c_camif_unregister_subdev(camif
);
533 static int s3c_camif_remove(struct platform_device
*pdev
)
535 struct camif_dev
*camif
= platform_get_drvdata(pdev
);
536 struct s3c_camif_plat_data
*pdata
= &camif
->pdata
;
538 media_device_unregister(&camif
->media_dev
);
539 camif_unregister_media_entities(camif
);
540 v4l2_device_unregister(&camif
->v4l2_dev
);
542 pm_runtime_disable(&pdev
->dev
);
543 camif_clk_put(camif
);
549 static int s3c_camif_runtime_resume(struct device
*dev
)
551 struct camif_dev
*camif
= dev_get_drvdata(dev
);
553 clk_enable(camif
->clock
[CLK_GATE
]);
554 /* null op on s3c244x */
555 clk_enable(camif
->clock
[CLK_CAM
]);
559 static int s3c_camif_runtime_suspend(struct device
*dev
)
561 struct camif_dev
*camif
= dev_get_drvdata(dev
);
563 /* null op on s3c244x */
564 clk_disable(camif
->clock
[CLK_CAM
]);
566 clk_disable(camif
->clock
[CLK_GATE
]);
570 static const struct s3c_camif_variant s3c244x_camif_variant
= {
573 .max_out_width
= 4096,
574 .max_sc_out_width
= 2048,
575 .out_width_align
= 16,
580 .max_out_width
= 640,
581 .max_sc_out_width
= 640,
582 .out_width_align
= 16,
588 .win_hor_offset_align
= 8,
590 .ip_revision
= S3C244X_CAMIF_IP_REV
,
593 static struct s3c_camif_drvdata s3c244x_camif_drvdata
= {
594 .variant
= &s3c244x_camif_variant
,
595 .bus_clk_freq
= 24000000UL,
598 static const struct s3c_camif_variant s3c6410_camif_variant
= {
601 .max_out_width
= 4096,
602 .max_sc_out_width
= 2048,
603 .out_width_align
= 16,
608 .max_out_width
= 4096,
609 .max_sc_out_width
= 720,
610 .out_width_align
= 16,
616 .win_hor_offset_align
= 8,
618 .ip_revision
= S3C6410_CAMIF_IP_REV
,
623 static struct s3c_camif_drvdata s3c6410_camif_drvdata
= {
624 .variant
= &s3c6410_camif_variant
,
625 .bus_clk_freq
= 133000000UL,
628 static struct platform_device_id s3c_camif_driver_ids
[] = {
630 .name
= "s3c2440-camif",
631 .driver_data
= (unsigned long)&s3c244x_camif_drvdata
,
633 .name
= "s3c6410-camif",
634 .driver_data
= (unsigned long)&s3c6410_camif_drvdata
,
638 MODULE_DEVICE_TABLE(platform
, s3c_camif_driver_ids
);
640 static const struct dev_pm_ops s3c_camif_pm_ops
= {
641 .runtime_suspend
= s3c_camif_runtime_suspend
,
642 .runtime_resume
= s3c_camif_runtime_resume
,
645 static struct platform_driver s3c_camif_driver
= {
646 .probe
= s3c_camif_probe
,
647 .remove
= s3c_camif_remove
,
648 .id_table
= s3c_camif_driver_ids
,
650 .name
= S3C_CAMIF_DRIVER_NAME
,
651 .owner
= THIS_MODULE
,
652 .pm
= &s3c_camif_pm_ops
,
656 module_platform_driver(s3c_camif_driver
);
658 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
659 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
660 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
661 MODULE_LICENSE("GPL");