1
2
3
4
5
6
7
8#define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
9
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>
18#include <linux/io.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>
27
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>
33
34#include "camif-core.h"
35
36static char *camif_clocks[CLK_MAX_NUM] = {
37
38 [CLK_GATE] = "camif",
39
40 [CLK_CAM] = "camera",
41};
42
43static const struct camif_fmt camif_formats[] = {
44 {
45 .fourcc = V4L2_PIX_FMT_YUV422P,
46 .depth = 16,
47 .ybpp = 1,
48 .color = IMG_FMT_YCBCR422P,
49 .colplanes = 3,
50 .flags = FMT_FL_S3C24XX_CODEC |
51 FMT_FL_S3C64XX,
52 }, {
53 .fourcc = V4L2_PIX_FMT_YUV420,
54 .depth = 12,
55 .ybpp = 1,
56 .color = IMG_FMT_YCBCR420,
57 .colplanes = 3,
58 .flags = FMT_FL_S3C24XX_CODEC |
59 FMT_FL_S3C64XX,
60 }, {
61 .fourcc = V4L2_PIX_FMT_YVU420,
62 .depth = 12,
63 .ybpp = 1,
64 .color = IMG_FMT_YCRCB420,
65 .colplanes = 3,
66 .flags = FMT_FL_S3C24XX_CODEC |
67 FMT_FL_S3C64XX,
68 }, {
69 .fourcc = V4L2_PIX_FMT_RGB565X,
70 .depth = 16,
71 .ybpp = 2,
72 .color = IMG_FMT_RGB565,
73 .colplanes = 1,
74 .flags = FMT_FL_S3C24XX_PREVIEW |
75 FMT_FL_S3C64XX,
76 }, {
77 .fourcc = V4L2_PIX_FMT_RGB32,
78 .depth = 32,
79 .ybpp = 4,
80 .color = IMG_FMT_XRGB8888,
81 .colplanes = 1,
82 .flags = FMT_FL_S3C24XX_PREVIEW |
83 FMT_FL_S3C64XX,
84 }, {
85 .fourcc = V4L2_PIX_FMT_BGR666,
86 .depth = 32,
87 .ybpp = 4,
88 .color = IMG_FMT_RGB666,
89 .colplanes = 1,
90 .flags = FMT_FL_S3C64XX,
91 }
92};
93
94
95
96
97
98
99
100const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
101 const u32 *pixelformat,
102 int index)
103{
104 const struct camif_fmt *fmt, *def_fmt = NULL;
105 unsigned int i;
106 int id = 0;
107
108 if (index >= (int)ARRAY_SIZE(camif_formats))
109 return NULL;
110
111 for (i = 0; i < ARRAY_SIZE(camif_formats); ++i) {
112 fmt = &camif_formats[i];
113 if (vp && !(vp->fmt_flags & fmt->flags))
114 continue;
115 if (pixelformat && fmt->fourcc == *pixelformat)
116 return fmt;
117 if (index == id)
118 def_fmt = fmt;
119 id++;
120 }
121 return def_fmt;
122}
123
124static int camif_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
125{
126 unsigned int sh = 6;
127
128 if (src >= 64 * tar)
129 return -EINVAL;
130
131 while (sh--) {
132 unsigned int tmp = 1 << sh;
133 if (src >= tar * tmp) {
134 *shift = sh, *ratio = tmp;
135 return 0;
136 }
137 }
138 *shift = 0, *ratio = 1;
139 return 0;
140}
141
142int s3c_camif_get_scaler_config(struct camif_vp *vp,
143 struct camif_scaler *scaler)
144{
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;
150 int ret;
151
152 if (vp->rotation == 90 || vp->rotation == 270)
153 swap(target_x, target_y);
154
155 ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
156 &scaler->h_shift);
157 if (ret < 0)
158 return ret;
159
160 ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
161 &scaler->v_shift);
162 if (ret < 0)
163 return ret;
164
165 scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
166 scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
167
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);
170
171 scaler->scaleup_h = (target_x >= source_x);
172 scaler->scaleup_v = (target_y >= source_y);
173
174 scaler->copy = 0;
175
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);
179
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);
183
184 return 0;
185}
186
187static int camif_register_sensor(struct camif_dev *camif)
188{
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;
194 int ret;
195
196 camif->sensor.sd = NULL;
197
198 if (sensor->i2c_board_info.addr == 0)
199 return -EINVAL;
200
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;
206 }
207
208 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
209 &sensor->i2c_board_info, NULL);
210 if (sd == 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;
215 }
216 camif->sensor.sd = sd;
217
218 v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
219
220
221 format.pad = 0;
222 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
223 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &format);
224
225 if (ret < 0)
226 return 0;
227
228 format.pad = CAMIF_SD_PAD_SINK;
229 v4l2_subdev_call(&camif->subdev, pad, set_fmt, NULL, &format);
230
231 v4l2_info(sd, "Initial format from sensor: %dx%d, %#x\n",
232 format.format.width, format.format.height,
233 format.format.code);
234 return 0;
235}
236
237static void camif_unregister_sensor(struct camif_dev *camif)
238{
239 struct v4l2_subdev *sd = camif->sensor.sd;
240 struct i2c_client *client = sd ? v4l2_get_subdevdata(sd) : NULL;
241 struct i2c_adapter *adapter;
242
243 if (client == NULL)
244 return;
245
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);
251}
252
253static int camif_create_media_links(struct camif_dev *camif)
254{
255 int i, ret;
256
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);
261 if (ret)
262 return ret;
263
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);
269 }
270
271 return ret;
272}
273
274static int camif_register_video_nodes(struct camif_dev *camif)
275{
276 int ret = s3c_camif_register_video_node(camif, VP_CODEC);
277 if (ret < 0)
278 return ret;
279
280 return s3c_camif_register_video_node(camif, VP_PREVIEW);
281}
282
283static void camif_unregister_video_nodes(struct camif_dev *camif)
284{
285 s3c_camif_unregister_video_node(camif, VP_CODEC);
286 s3c_camif_unregister_video_node(camif, VP_PREVIEW);
287}
288
289static void camif_unregister_media_entities(struct camif_dev *camif)
290{
291 camif_unregister_video_nodes(camif);
292 camif_unregister_sensor(camif);
293 s3c_camif_unregister_subdev(camif);
294}
295
296
297
298
299static int camif_media_dev_init(struct camif_dev *camif)
300{
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;
304 int ret;
305
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;
311
312 md->dev = camif->dev;
313
314 strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
315 v4l2_dev->mdev = md;
316
317 media_device_init(md);
318
319 ret = v4l2_device_register(camif->dev, v4l2_dev);
320 if (ret < 0)
321 return ret;
322
323 return ret;
324}
325
326static void camif_clk_put(struct camif_dev *camif)
327{
328 int i;
329
330 for (i = 0; i < CLK_MAX_NUM; i++) {
331 if (IS_ERR(camif->clock[i]))
332 continue;
333 clk_unprepare(camif->clock[i]);
334 clk_put(camif->clock[i]);
335 camif->clock[i] = ERR_PTR(-EINVAL);
336 }
337}
338
339static int camif_clk_get(struct camif_dev *camif)
340{
341 int ret, i;
342
343 for (i = 1; i < CLK_MAX_NUM; i++)
344 camif->clock[i] = ERR_PTR(-EINVAL);
345
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]);
350 goto err;
351 }
352 ret = clk_prepare(camif->clock[i]);
353 if (ret < 0) {
354 clk_put(camif->clock[i]);
355 camif->clock[i] = NULL;
356 goto err;
357 }
358 }
359 return 0;
360err:
361 camif_clk_put(camif);
362 dev_err(camif->dev, "failed to get clock: %s\n",
363 camif_clocks[i]);
364 return ret;
365}
366
367
368
369
370
371
372static int camif_request_irqs(struct platform_device *pdev,
373 struct camif_dev *camif)
374{
375 int irq, ret, i;
376
377 for (i = 0; i < CAMIF_VP_NUM; i++) {
378 struct camif_vp *vp = &camif->vp[i];
379
380 init_waitqueue_head(&vp->irq_queue);
381
382 irq = platform_get_irq(pdev, i);
383 if (irq <= 0)
384 return -ENXIO;
385
386 ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
387 0, dev_name(&pdev->dev), vp);
388 if (ret < 0) {
389 dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
390 break;
391 }
392 }
393
394 return ret;
395}
396
397static int s3c_camif_probe(struct platform_device *pdev)
398{
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;
404 int ret = 0;
405
406 camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
407 if (!camif)
408 return -ENOMEM;
409
410 spin_lock_init(&camif->slock);
411 mutex_init(&camif->lock);
412
413 camif->dev = dev;
414
415 if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
416 dev_err(dev, "wrong platform data\n");
417 return -EINVAL;
418 }
419
420 camif->pdata = *pdata;
421 drvdata = (void *)platform_get_device_id(pdev)->driver_data;
422 camif->variant = drvdata->variant;
423
424 mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
425
426 camif->io_base = devm_ioremap_resource(dev, mres);
427 if (IS_ERR(camif->io_base))
428 return PTR_ERR(camif->io_base);
429
430 ret = camif_request_irqs(pdev, camif);
431 if (ret < 0)
432 return ret;
433
434 ret = pdata->gpio_get();
435 if (ret < 0)
436 return ret;
437
438 ret = s3c_camif_create_subdev(camif);
439 if (ret < 0)
440 goto err_sd;
441
442 ret = camif_clk_get(camif);
443 if (ret < 0)
444 goto err_clk;
445
446 platform_set_drvdata(pdev, camif);
447 clk_set_rate(camif->clock[CLK_CAM],
448 camif->pdata.sensor.clock_frequency);
449
450 dev_info(dev, "sensor clock frequency: %lu\n",
451 clk_get_rate(camif->clock[CLK_CAM]));
452
453
454
455
456
457 s3c_camif_set_defaults(camif);
458
459 pm_runtime_enable(dev);
460
461 ret = pm_runtime_get_sync(dev);
462 if (ret < 0)
463 goto err_pm;
464
465 ret = camif_media_dev_init(camif);
466 if (ret < 0)
467 goto err_alloc;
468
469 ret = camif_register_sensor(camif);
470 if (ret < 0)
471 goto err_sens;
472
473 ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
474 if (ret < 0)
475 goto err_sens;
476
477 ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
478 if (ret < 0)
479 goto err_sens;
480
481 ret = camif_register_video_nodes(camif);
482 if (ret < 0)
483 goto err_sens;
484
485 ret = camif_create_media_links(camif);
486 if (ret < 0)
487 goto err_sens;
488
489 ret = media_device_register(&camif->media_dev);
490 if (ret < 0)
491 goto err_sens;
492
493 pm_runtime_put(dev);
494 return 0;
495
496err_sens:
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);
501err_alloc:
502 pm_runtime_put(dev);
503 pm_runtime_disable(dev);
504err_pm:
505 camif_clk_put(camif);
506err_clk:
507 s3c_camif_unregister_subdev(camif);
508err_sd:
509 pdata->gpio_put();
510 return ret;
511}
512
513static int s3c_camif_remove(struct platform_device *pdev)
514{
515 struct camif_dev *camif = platform_get_drvdata(pdev);
516 struct s3c_camif_plat_data *pdata = &camif->pdata;
517
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);
522
523 pm_runtime_disable(&pdev->dev);
524 camif_clk_put(camif);
525 pdata->gpio_put();
526
527 return 0;
528}
529
530static int s3c_camif_runtime_resume(struct device *dev)
531{
532 struct camif_dev *camif = dev_get_drvdata(dev);
533
534 clk_enable(camif->clock[CLK_GATE]);
535
536 clk_enable(camif->clock[CLK_CAM]);
537 return 0;
538}
539
540static int s3c_camif_runtime_suspend(struct device *dev)
541{
542 struct camif_dev *camif = dev_get_drvdata(dev);
543
544
545 clk_disable(camif->clock[CLK_CAM]);
546
547 clk_disable(camif->clock[CLK_GATE]);
548 return 0;
549}
550
551static const struct s3c_camif_variant s3c244x_camif_variant = {
552 .vp_pix_limits = {
553 [VP_CODEC] = {
554 .max_out_width = 4096,
555 .max_sc_out_width = 2048,
556 .out_width_align = 16,
557 .min_out_width = 16,
558 .max_height = 4096,
559 },
560 [VP_PREVIEW] = {
561 .max_out_width = 640,
562 .max_sc_out_width = 640,
563 .out_width_align = 16,
564 .min_out_width = 16,
565 .max_height = 480,
566 }
567 },
568 .pix_limits = {
569 .win_hor_offset_align = 8,
570 },
571 .ip_revision = S3C244X_CAMIF_IP_REV,
572};
573
574static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
575 .variant = &s3c244x_camif_variant,
576 .bus_clk_freq = 24000000UL,
577};
578
579static const struct s3c_camif_variant s3c6410_camif_variant = {
580 .vp_pix_limits = {
581 [VP_CODEC] = {
582 .max_out_width = 4096,
583 .max_sc_out_width = 2048,
584 .out_width_align = 16,
585 .min_out_width = 16,
586 .max_height = 4096,
587 },
588 [VP_PREVIEW] = {
589 .max_out_width = 4096,
590 .max_sc_out_width = 720,
591 .out_width_align = 16,
592 .min_out_width = 16,
593 .max_height = 4096,
594 }
595 },
596 .pix_limits = {
597 .win_hor_offset_align = 8,
598 },
599 .ip_revision = S3C6410_CAMIF_IP_REV,
600 .has_img_effect = 1,
601 .vp_offset = 0x20,
602};
603
604static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
605 .variant = &s3c6410_camif_variant,
606 .bus_clk_freq = 133000000UL,
607};
608
609static const struct platform_device_id s3c_camif_driver_ids[] = {
610 {
611 .name = "s3c2440-camif",
612 .driver_data = (unsigned long)&s3c244x_camif_drvdata,
613 }, {
614 .name = "s3c6410-camif",
615 .driver_data = (unsigned long)&s3c6410_camif_drvdata,
616 },
617 { },
618};
619MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
620
621static const struct dev_pm_ops s3c_camif_pm_ops = {
622 .runtime_suspend = s3c_camif_runtime_suspend,
623 .runtime_resume = s3c_camif_runtime_resume,
624};
625
626static struct platform_driver s3c_camif_driver = {
627 .probe = s3c_camif_probe,
628 .remove = s3c_camif_remove,
629 .id_table = s3c_camif_driver_ids,
630 .driver = {
631 .name = S3C_CAMIF_DRIVER_NAME,
632 .pm = &s3c_camif_pm_ops,
633 }
634};
635
636module_platform_driver(s3c_camif_driver);
637
638MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
639MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
640MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
641MODULE_LICENSE("GPL");
642