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