1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
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 */
39 /* CAMIF / external camera sensor master clock */
43 static const struct camif_fmt camif_formats
[] = {
45 .fourcc
= V4L2_PIX_FMT_YUV422P
,
48 .color
= IMG_FMT_YCBCR422P
,
50 .flags
= FMT_FL_S3C24XX_CODEC
|
53 .fourcc
= V4L2_PIX_FMT_YUV420
,
56 .color
= IMG_FMT_YCBCR420
,
58 .flags
= FMT_FL_S3C24XX_CODEC
|
61 .fourcc
= V4L2_PIX_FMT_YVU420
,
64 .color
= IMG_FMT_YCRCB420
,
66 .flags
= FMT_FL_S3C24XX_CODEC
|
69 .fourcc
= V4L2_PIX_FMT_RGB565X
,
72 .color
= IMG_FMT_RGB565
,
74 .flags
= FMT_FL_S3C24XX_PREVIEW
|
77 .fourcc
= V4L2_PIX_FMT_RGB32
,
80 .color
= IMG_FMT_XRGB8888
,
82 .flags
= FMT_FL_S3C24XX_PREVIEW
|
85 .fourcc
= V4L2_PIX_FMT_BGR666
,
88 .color
= IMG_FMT_RGB666
,
90 .flags
= FMT_FL_S3C64XX
,
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
,
104 const struct camif_fmt
*fmt
, *def_fmt
= NULL
;
108 if (index
>= (int)ARRAY_SIZE(camif_formats
))
111 for (i
= 0; i
< ARRAY_SIZE(camif_formats
); ++i
) {
112 fmt
= &camif_formats
[i
];
113 if (vp
&& !(vp
->fmt_flags
& fmt
->flags
))
115 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
124 static int camif_get_scaler_factor(u32 src
, u32 tar
, u32
*ratio
, u32
*shift
)
132 unsigned int tmp
= 1 << sh
;
133 if (src
>= tar
* tmp
) {
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
;
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
,
162 ret
= camif_get_scaler_factor(source_y
, target_y
, &scaler
->pre_v_ratio
,
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
);
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
);
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
;
198 camif
->sensor
.sd
= NULL
;
200 if (sensor
->i2c_board_info
.addr
== 0)
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
);
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 */
224 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
225 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &format
);
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
,
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
;
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
)
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
);
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
);
276 static int camif_register_video_nodes(struct camif_dev
*camif
)
278 int ret
= s3c_camif_register_video_node(camif
, VP_CODEC
);
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
);
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
;
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
));
319 media_device_init(md
);
321 ret
= v4l2_device_register(camif
->dev
, v4l2_dev
);
328 static void camif_clk_put(struct camif_dev
*camif
)
332 for (i
= 0; i
< CLK_MAX_NUM
; i
++) {
333 if (IS_ERR(camif
->clock
[i
]))
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
)
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
]);
354 ret
= clk_prepare(camif
->clock
[i
]);
356 clk_put(camif
->clock
[i
]);
357 camif
->clock
[i
] = NULL
;
363 camif_clk_put(camif
);
364 dev_err(camif
->dev
, "failed to get clock: %s\n",
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
)
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
);
388 ret
= devm_request_irq(&pdev
->dev
, irq
, s3c_camif_irq_handler
,
389 0, dev_name(&pdev
->dev
), vp
);
391 dev_err(&pdev
->dev
, "failed to install IRQ: %d\n", 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
;
408 camif
= devm_kzalloc(dev
, sizeof(*camif
), GFP_KERNEL
);
412 spin_lock_init(&camif
->slock
);
413 mutex_init(&camif
->lock
);
417 if (!pdata
|| !pdata
->gpio_get
|| !pdata
->gpio_put
) {
418 dev_err(dev
, "wrong platform data\n");
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
);
436 ret
= pdata
->gpio_get();
440 ret
= s3c_camif_create_subdev(camif
);
444 ret
= camif_clk_get(camif
);
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
);
467 ret
= camif_media_dev_init(camif
);
471 ret
= camif_register_sensor(camif
);
475 ret
= v4l2_device_register_subdev(&camif
->v4l2_dev
, &camif
->subdev
);
479 ret
= v4l2_device_register_subdev_nodes(&camif
->v4l2_dev
);
483 ret
= camif_register_video_nodes(camif
);
487 ret
= camif_create_media_links(camif
);
491 ret
= media_device_register(&camif
->media_dev
);
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
);
505 pm_runtime_disable(dev
);
506 camif_clk_put(camif
);
508 s3c_camif_unregister_subdev(camif
);
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
);
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
]);
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
]);
552 static const struct s3c_camif_variant s3c244x_camif_variant
= {
555 .max_out_width
= 4096,
556 .max_sc_out_width
= 2048,
557 .out_width_align
= 16,
562 .max_out_width
= 640,
563 .max_sc_out_width
= 640,
564 .out_width_align
= 16,
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
= {
583 .max_out_width
= 4096,
584 .max_sc_out_width
= 2048,
585 .out_width_align
= 16,
590 .max_out_width
= 4096,
591 .max_sc_out_width
= 720,
592 .out_width_align
= 16,
598 .win_hor_offset_align
= 8,
600 .ip_revision
= S3C6410_CAMIF_IP_REV
,
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
,
615 .name
= "s3c6410-camif",
616 .driver_data
= (unsigned long)&s3c6410_camif_drvdata
,
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
,
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");