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
) {
134 *shift
= sh
, *ratio
= tmp
;
138 *shift
= 0, *ratio
= 1;
142 int s3c_camif_get_scaler_config(struct camif_vp
*vp
,
143 struct camif_scaler
*scaler
)
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
;
152 if (vp
->rotation
== 90 || vp
->rotation
== 270)
153 swap(target_x
, target_y
);
155 ret
= camif_get_scaler_factor(source_x
, target_x
, &scaler
->pre_h_ratio
,
160 ret
= camif_get_scaler_factor(source_y
, target_y
, &scaler
->pre_v_ratio
,
165 scaler
->pre_dst_width
= source_x
/ scaler
->pre_h_ratio
;
166 scaler
->pre_dst_height
= source_y
/ scaler
->pre_v_ratio
;
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
);
171 scaler
->scaleup_h
= (target_x
>= source_x
);
172 scaler
->scaleup_v
= (target_y
>= source_y
);
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
);
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
);
187 static int camif_register_sensor(struct camif_dev
*camif
)
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
;
196 camif
->sensor
.sd
= NULL
;
198 if (sensor
->i2c_board_info
.addr
== 0)
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
;
208 sd
= v4l2_i2c_new_subdev_board(v4l2_dev
, adapter
,
209 &sensor
->i2c_board_info
, 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
;
216 camif
->sensor
.sd
= sd
;
218 v4l2_info(v4l2_dev
, "registered sensor subdevice %s\n", sd
->name
);
220 /* Get initial pixel format and set it at the camif sink pad */
222 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
223 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &format
);
228 format
.pad
= CAMIF_SD_PAD_SINK
;
229 v4l2_subdev_call(&camif
->subdev
, pad
, set_fmt
, NULL
, &format
);
231 v4l2_info(sd
, "Initial format from sensor: %dx%d, %#x\n",
232 format
.format
.width
, format
.format
.height
,
237 static void camif_unregister_sensor(struct camif_dev
*camif
)
239 struct v4l2_subdev
*sd
= camif
->sensor
.sd
;
240 struct i2c_client
*client
= sd
? v4l2_get_subdevdata(sd
) : NULL
;
241 struct i2c_adapter
*adapter
;
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
);
253 static int camif_create_media_links(struct camif_dev
*camif
)
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
);
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
);
274 static int camif_register_video_nodes(struct camif_dev
*camif
)
276 int ret
= s3c_camif_register_video_node(camif
, VP_CODEC
);
280 return s3c_camif_register_video_node(camif
, VP_PREVIEW
);
283 static void camif_unregister_video_nodes(struct camif_dev
*camif
)
285 s3c_camif_unregister_video_node(camif
, VP_CODEC
);
286 s3c_camif_unregister_video_node(camif
, VP_PREVIEW
);
289 static void camif_unregister_media_entities(struct camif_dev
*camif
)
291 camif_unregister_video_nodes(camif
);
292 camif_unregister_sensor(camif
);
293 s3c_camif_unregister_subdev(camif
);
299 static int camif_media_dev_init(struct camif_dev
*camif
)
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
;
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
;
312 md
->dev
= camif
->dev
;
314 strscpy(v4l2_dev
->name
, "s3c-camif", sizeof(v4l2_dev
->name
));
317 media_device_init(md
);
319 ret
= v4l2_device_register(camif
->dev
, v4l2_dev
);
326 static void camif_clk_put(struct camif_dev
*camif
)
330 for (i
= 0; i
< CLK_MAX_NUM
; i
++) {
331 if (IS_ERR(camif
->clock
[i
]))
333 clk_unprepare(camif
->clock
[i
]);
334 clk_put(camif
->clock
[i
]);
335 camif
->clock
[i
] = ERR_PTR(-EINVAL
);
339 static int camif_clk_get(struct camif_dev
*camif
)
343 for (i
= 1; i
< CLK_MAX_NUM
; i
++)
344 camif
->clock
[i
] = ERR_PTR(-EINVAL
);
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
]);
352 ret
= clk_prepare(camif
->clock
[i
]);
354 clk_put(camif
->clock
[i
]);
355 camif
->clock
[i
] = NULL
;
361 camif_clk_put(camif
);
362 dev_err(camif
->dev
, "failed to get clock: %s\n",
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).
372 static int camif_request_irqs(struct platform_device
*pdev
,
373 struct camif_dev
*camif
)
377 for (i
= 0; i
< CAMIF_VP_NUM
; i
++) {
378 struct camif_vp
*vp
= &camif
->vp
[i
];
380 init_waitqueue_head(&vp
->irq_queue
);
382 irq
= platform_get_irq(pdev
, i
);
386 ret
= devm_request_irq(&pdev
->dev
, irq
, s3c_camif_irq_handler
,
387 0, dev_name(&pdev
->dev
), vp
);
389 dev_err(&pdev
->dev
, "failed to install IRQ: %d\n", ret
);
397 static int s3c_camif_probe(struct platform_device
*pdev
)
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
;
406 camif
= devm_kzalloc(dev
, sizeof(*camif
), GFP_KERNEL
);
410 spin_lock_init(&camif
->slock
);
411 mutex_init(&camif
->lock
);
415 if (!pdata
|| !pdata
->gpio_get
|| !pdata
->gpio_put
) {
416 dev_err(dev
, "wrong platform data\n");
420 camif
->pdata
= *pdata
;
421 drvdata
= (void *)platform_get_device_id(pdev
)->driver_data
;
422 camif
->variant
= drvdata
->variant
;
424 mres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
426 camif
->io_base
= devm_ioremap_resource(dev
, mres
);
427 if (IS_ERR(camif
->io_base
))
428 return PTR_ERR(camif
->io_base
);
430 ret
= camif_request_irqs(pdev
, camif
);
434 ret
= pdata
->gpio_get();
438 ret
= s3c_camif_create_subdev(camif
);
442 ret
= camif_clk_get(camif
);
446 platform_set_drvdata(pdev
, camif
);
447 clk_set_rate(camif
->clock
[CLK_CAM
],
448 camif
->pdata
.sensor
.clock_frequency
);
450 dev_info(dev
, "sensor clock frequency: %lu\n",
451 clk_get_rate(camif
->clock
[CLK_CAM
]));
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.
457 s3c_camif_set_defaults(camif
);
459 pm_runtime_enable(dev
);
461 ret
= pm_runtime_get_sync(dev
);
465 ret
= camif_media_dev_init(camif
);
469 ret
= camif_register_sensor(camif
);
473 ret
= v4l2_device_register_subdev(&camif
->v4l2_dev
, &camif
->subdev
);
477 ret
= v4l2_device_register_subdev_nodes(&camif
->v4l2_dev
);
481 ret
= camif_register_video_nodes(camif
);
485 ret
= camif_create_media_links(camif
);
489 ret
= media_device_register(&camif
->media_dev
);
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
);
503 pm_runtime_disable(dev
);
505 camif_clk_put(camif
);
507 s3c_camif_unregister_subdev(camif
);
513 static int s3c_camif_remove(struct platform_device
*pdev
)
515 struct camif_dev
*camif
= platform_get_drvdata(pdev
);
516 struct s3c_camif_plat_data
*pdata
= &camif
->pdata
;
518 media_device_unregister(&camif
->media_dev
);
519 media_device_cleanup(&camif
->media_dev
);
520 camif_unregister_media_entities(camif
);
521 v4l2_device_unregister(&camif
->v4l2_dev
);
523 pm_runtime_disable(&pdev
->dev
);
524 camif_clk_put(camif
);
530 static int s3c_camif_runtime_resume(struct device
*dev
)
532 struct camif_dev
*camif
= dev_get_drvdata(dev
);
534 clk_enable(camif
->clock
[CLK_GATE
]);
535 /* null op on s3c244x */
536 clk_enable(camif
->clock
[CLK_CAM
]);
540 static int s3c_camif_runtime_suspend(struct device
*dev
)
542 struct camif_dev
*camif
= dev_get_drvdata(dev
);
544 /* null op on s3c244x */
545 clk_disable(camif
->clock
[CLK_CAM
]);
547 clk_disable(camif
->clock
[CLK_GATE
]);
551 static const struct s3c_camif_variant s3c244x_camif_variant
= {
554 .max_out_width
= 4096,
555 .max_sc_out_width
= 2048,
556 .out_width_align
= 16,
561 .max_out_width
= 640,
562 .max_sc_out_width
= 640,
563 .out_width_align
= 16,
569 .win_hor_offset_align
= 8,
571 .ip_revision
= S3C244X_CAMIF_IP_REV
,
574 static struct s3c_camif_drvdata s3c244x_camif_drvdata
= {
575 .variant
= &s3c244x_camif_variant
,
576 .bus_clk_freq
= 24000000UL,
579 static const struct s3c_camif_variant s3c6410_camif_variant
= {
582 .max_out_width
= 4096,
583 .max_sc_out_width
= 2048,
584 .out_width_align
= 16,
589 .max_out_width
= 4096,
590 .max_sc_out_width
= 720,
591 .out_width_align
= 16,
597 .win_hor_offset_align
= 8,
599 .ip_revision
= S3C6410_CAMIF_IP_REV
,
604 static struct s3c_camif_drvdata s3c6410_camif_drvdata
= {
605 .variant
= &s3c6410_camif_variant
,
606 .bus_clk_freq
= 133000000UL,
609 static const struct platform_device_id s3c_camif_driver_ids
[] = {
611 .name
= "s3c2440-camif",
612 .driver_data
= (unsigned long)&s3c244x_camif_drvdata
,
614 .name
= "s3c6410-camif",
615 .driver_data
= (unsigned long)&s3c6410_camif_drvdata
,
619 MODULE_DEVICE_TABLE(platform
, s3c_camif_driver_ids
);
621 static const struct dev_pm_ops s3c_camif_pm_ops
= {
622 .runtime_suspend
= s3c_camif_runtime_suspend
,
623 .runtime_resume
= s3c_camif_runtime_resume
,
626 static struct platform_driver s3c_camif_driver
= {
627 .probe
= s3c_camif_probe
,
628 .remove
= s3c_camif_remove
,
629 .id_table
= s3c_camif_driver_ids
,
631 .name
= S3C_CAMIF_DRIVER_NAME
,
632 .pm
= &s3c_camif_pm_ops
,
636 module_platform_driver(s3c_camif_driver
);
638 MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
639 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
640 MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
641 MODULE_LICENSE("GPL");