linux/drivers/media/platform/s3c-camif/camif-core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
   4 *
   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#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        /* HCLK CAMIF clock */
  38        [CLK_GATE]      = "camif",
  39        /* CAMIF / external camera sensor master clock */
  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 * 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
  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;
 135                        *ratio = tmp;
 136                        return 0;
 137                }
 138        }
 139        *shift = 0;
 140        *ratio = 1;
 141        return 0;
 142}
 143
 144int s3c_camif_get_scaler_config(struct camif_vp *vp,
 145                                struct camif_scaler *scaler)
 146{
 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;
 152        int ret;
 153
 154        if (vp->rotation == 90 || vp->rotation == 270)
 155                swap(target_x, target_y);
 156
 157        ret = camif_get_scaler_factor(source_x, target_x, &scaler->pre_h_ratio,
 158                                      &scaler->h_shift);
 159        if (ret < 0)
 160                return ret;
 161
 162        ret = camif_get_scaler_factor(source_y, target_y, &scaler->pre_v_ratio,
 163                                      &scaler->v_shift);
 164        if (ret < 0)
 165                return ret;
 166
 167        scaler->pre_dst_width = source_x / scaler->pre_h_ratio;
 168        scaler->pre_dst_height = source_y / scaler->pre_v_ratio;
 169
 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);
 172
 173        scaler->scaleup_h = (target_x >= source_x);
 174        scaler->scaleup_v = (target_y >= source_y);
 175
 176        scaler->copy = 0;
 177
 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);
 181
 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);
 185
 186        return 0;
 187}
 188
 189static int camif_register_sensor(struct camif_dev *camif)
 190{
 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;
 196        int ret;
 197
 198        camif->sensor.sd = NULL;
 199
 200        if (sensor->i2c_board_info.addr == 0)
 201                return -EINVAL;
 202
 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;
 208        }
 209
 210        sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter,
 211                                       &sensor->i2c_board_info, NULL);
 212        if (sd == 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;
 217        }
 218        camif->sensor.sd = sd;
 219
 220        v4l2_info(v4l2_dev, "registered sensor subdevice %s\n", sd->name);
 221
 222        /* Get initial pixel format and set it at the camif sink pad */
 223        format.pad = 0;
 224        format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 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        s3c_camif_unregister_subdev(camif);
 296}
 297
 298/*
 299 * Media device
 300 */
 301static int camif_media_dev_init(struct camif_dev *camif)
 302{
 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;
 306        int ret;
 307
 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;
 313
 314        md->dev = camif->dev;
 315
 316        strscpy(v4l2_dev->name, "s3c-camif", sizeof(v4l2_dev->name));
 317        v4l2_dev->mdev = md;
 318
 319        media_device_init(md);
 320
 321        ret = v4l2_device_register(camif->dev, v4l2_dev);
 322        if (ret < 0)
 323                return ret;
 324
 325        return ret;
 326}
 327
 328static void camif_clk_put(struct camif_dev *camif)
 329{
 330        int i;
 331
 332        for (i = 0; i < CLK_MAX_NUM; i++) {
 333                if (IS_ERR(camif->clock[i]))
 334                        continue;
 335                clk_unprepare(camif->clock[i]);
 336                clk_put(camif->clock[i]);
 337                camif->clock[i] = ERR_PTR(-EINVAL);
 338        }
 339}
 340
 341static int camif_clk_get(struct camif_dev *camif)
 342{
 343        int ret, i;
 344
 345        for (i = 1; i < CLK_MAX_NUM; i++)
 346                camif->clock[i] = ERR_PTR(-EINVAL);
 347
 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]);
 352                        goto err;
 353                }
 354                ret = clk_prepare(camif->clock[i]);
 355                if (ret < 0) {
 356                        clk_put(camif->clock[i]);
 357                        camif->clock[i] = NULL;
 358                        goto err;
 359                }
 360        }
 361        return 0;
 362err:
 363        camif_clk_put(camif);
 364        dev_err(camif->dev, "failed to get clock: %s\n",
 365                camif_clocks[i]);
 366        return ret;
 367}
 368
 369/*
 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).
 373 */
 374static int camif_request_irqs(struct platform_device *pdev,
 375                              struct camif_dev *camif)
 376{
 377        int irq, ret, i;
 378
 379        for (i = 0; i < CAMIF_VP_NUM; i++) {
 380                struct camif_vp *vp = &camif->vp[i];
 381
 382                init_waitqueue_head(&vp->irq_queue);
 383
 384                irq = platform_get_irq(pdev, i);
 385                if (irq <= 0)
 386                        return -ENXIO;
 387
 388                ret = devm_request_irq(&pdev->dev, irq, s3c_camif_irq_handler,
 389                                       0, dev_name(&pdev->dev), vp);
 390                if (ret < 0) {
 391                        dev_err(&pdev->dev, "failed to install IRQ: %d\n", ret);
 392                        break;
 393                }
 394        }
 395
 396        return ret;
 397}
 398
 399static int s3c_camif_probe(struct platform_device *pdev)
 400{
 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;
 406        int ret = 0;
 407
 408        camif = devm_kzalloc(dev, sizeof(*camif), GFP_KERNEL);
 409        if (!camif)
 410                return -ENOMEM;
 411
 412        spin_lock_init(&camif->slock);
 413        mutex_init(&camif->lock);
 414
 415        camif->dev = dev;
 416
 417        if (!pdata || !pdata->gpio_get || !pdata->gpio_put) {
 418                dev_err(dev, "wrong platform data\n");
 419                return -EINVAL;
 420        }
 421
 422        camif->pdata = *pdata;
 423        drvdata = (void *)platform_get_device_id(pdev)->driver_data;
 424        camif->variant = drvdata->variant;
 425
 426        mres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 427
 428        camif->io_base = devm_ioremap_resource(dev, mres);
 429        if (IS_ERR(camif->io_base))
 430                return PTR_ERR(camif->io_base);
 431
 432        ret = camif_request_irqs(pdev, camif);
 433        if (ret < 0)
 434                return ret;
 435
 436        ret = pdata->gpio_get();
 437        if (ret < 0)
 438                return ret;
 439
 440        ret = s3c_camif_create_subdev(camif);
 441        if (ret < 0)
 442                goto err_sd;
 443
 444        ret = camif_clk_get(camif);
 445        if (ret < 0)
 446                goto err_clk;
 447
 448        platform_set_drvdata(pdev, camif);
 449        clk_set_rate(camif->clock[CLK_CAM],
 450                        camif->pdata.sensor.clock_frequency);
 451
 452        dev_info(dev, "sensor clock frequency: %lu\n",
 453                 clk_get_rate(camif->clock[CLK_CAM]));
 454        /*
 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.
 458         */
 459        s3c_camif_set_defaults(camif);
 460
 461        pm_runtime_enable(dev);
 462
 463        ret = pm_runtime_resume_and_get(dev);
 464        if (ret < 0)
 465                goto err_disable;
 466
 467        ret = camif_media_dev_init(camif);
 468        if (ret < 0)
 469                goto err_pm;
 470
 471        ret = camif_register_sensor(camif);
 472        if (ret < 0)
 473                goto err_sens;
 474
 475        ret = v4l2_device_register_subdev(&camif->v4l2_dev, &camif->subdev);
 476        if (ret < 0)
 477                goto err_sens;
 478
 479        ret = v4l2_device_register_subdev_nodes(&camif->v4l2_dev);
 480        if (ret < 0)
 481                goto err_sens;
 482
 483        ret = camif_register_video_nodes(camif);
 484        if (ret < 0)
 485                goto err_sens;
 486
 487        ret = camif_create_media_links(camif);
 488        if (ret < 0)
 489                goto err_sens;
 490
 491        ret = media_device_register(&camif->media_dev);
 492        if (ret < 0)
 493                goto err_sens;
 494
 495        pm_runtime_put(dev);
 496        return 0;
 497
 498err_sens:
 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);
 503err_pm:
 504        pm_runtime_put(dev);
 505err_disable:
 506        pm_runtime_disable(dev);
 507        camif_clk_put(camif);
 508err_clk:
 509        s3c_camif_unregister_subdev(camif);
 510err_sd:
 511        pdata->gpio_put();
 512        return ret;
 513}
 514
 515static int s3c_camif_remove(struct platform_device *pdev)
 516{
 517        struct camif_dev *camif = platform_get_drvdata(pdev);
 518        struct s3c_camif_plat_data *pdata = &camif->pdata;
 519
 520        media_device_unregister(&camif->media_dev);
 521        media_device_cleanup(&camif->media_dev);
 522        camif_unregister_media_entities(camif);
 523        v4l2_device_unregister(&camif->v4l2_dev);
 524
 525        pm_runtime_disable(&pdev->dev);
 526        camif_clk_put(camif);
 527        pdata->gpio_put();
 528
 529        return 0;
 530}
 531
 532static int s3c_camif_runtime_resume(struct device *dev)
 533{
 534        struct camif_dev *camif = dev_get_drvdata(dev);
 535
 536        clk_enable(camif->clock[CLK_GATE]);
 537        /* null op on s3c244x */
 538        clk_enable(camif->clock[CLK_CAM]);
 539        return 0;
 540}
 541
 542static int s3c_camif_runtime_suspend(struct device *dev)
 543{
 544        struct camif_dev *camif = dev_get_drvdata(dev);
 545
 546        /* null op on s3c244x */
 547        clk_disable(camif->clock[CLK_CAM]);
 548
 549        clk_disable(camif->clock[CLK_GATE]);
 550        return 0;
 551}
 552
 553static const struct s3c_camif_variant s3c244x_camif_variant = {
 554        .vp_pix_limits = {
 555                [VP_CODEC] = {
 556                        .max_out_width          = 4096,
 557                        .max_sc_out_width       = 2048,
 558                        .out_width_align        = 16,
 559                        .min_out_width          = 16,
 560                        .max_height             = 4096,
 561                },
 562                [VP_PREVIEW] = {
 563                        .max_out_width          = 640,
 564                        .max_sc_out_width       = 640,
 565                        .out_width_align        = 16,
 566                        .min_out_width          = 16,
 567                        .max_height             = 480,
 568                }
 569        },
 570        .pix_limits = {
 571                .win_hor_offset_align   = 8,
 572        },
 573        .ip_revision = S3C244X_CAMIF_IP_REV,
 574};
 575
 576static struct s3c_camif_drvdata s3c244x_camif_drvdata = {
 577        .variant        = &s3c244x_camif_variant,
 578        .bus_clk_freq   = 24000000UL,
 579};
 580
 581static const struct s3c_camif_variant s3c6410_camif_variant = {
 582        .vp_pix_limits = {
 583                [VP_CODEC] = {
 584                        .max_out_width          = 4096,
 585                        .max_sc_out_width       = 2048,
 586                        .out_width_align        = 16,
 587                        .min_out_width          = 16,
 588                        .max_height             = 4096,
 589                },
 590                [VP_PREVIEW] = {
 591                        .max_out_width          = 4096,
 592                        .max_sc_out_width       = 720,
 593                        .out_width_align        = 16,
 594                        .min_out_width          = 16,
 595                        .max_height             = 4096,
 596                }
 597        },
 598        .pix_limits = {
 599                .win_hor_offset_align   = 8,
 600        },
 601        .ip_revision = S3C6410_CAMIF_IP_REV,
 602        .has_img_effect = 1,
 603        .vp_offset = 0x20,
 604};
 605
 606static struct s3c_camif_drvdata s3c6410_camif_drvdata = {
 607        .variant        = &s3c6410_camif_variant,
 608        .bus_clk_freq   = 133000000UL,
 609};
 610
 611static const struct platform_device_id s3c_camif_driver_ids[] = {
 612        {
 613                .name           = "s3c2440-camif",
 614                .driver_data    = (unsigned long)&s3c244x_camif_drvdata,
 615        }, {
 616                .name           = "s3c6410-camif",
 617                .driver_data    = (unsigned long)&s3c6410_camif_drvdata,
 618        },
 619        { /* sentinel */ },
 620};
 621MODULE_DEVICE_TABLE(platform, s3c_camif_driver_ids);
 622
 623static const struct dev_pm_ops s3c_camif_pm_ops = {
 624        .runtime_suspend        = s3c_camif_runtime_suspend,
 625        .runtime_resume         = s3c_camif_runtime_resume,
 626};
 627
 628static struct platform_driver s3c_camif_driver = {
 629        .probe          = s3c_camif_probe,
 630        .remove         = s3c_camif_remove,
 631        .id_table       = s3c_camif_driver_ids,
 632        .driver = {
 633                .name   = S3C_CAMIF_DRIVER_NAME,
 634                .pm     = &s3c_camif_pm_ops,
 635        }
 636};
 637
 638module_platform_driver(s3c_camif_driver);
 639
 640MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
 641MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
 642MODULE_DESCRIPTION("S3C24XX/S3C64XX SoC camera interface driver");
 643MODULE_LICENSE("GPL");
 644