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