linux/drivers/staging/media/imx/imx6-mipi-csi2.c
<<
>>
Prefs
   1/*
   2 * MIPI CSI-2 Receiver Subdev for Freescale i.MX6 SOC.
   3 *
   4 * Copyright (c) 2012-2017 Mentor Graphics Inc.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 */
  11#include <linux/clk.h>
  12#include <linux/interrupt.h>
  13#include <linux/io.h>
  14#include <linux/iopoll.h>
  15#include <linux/irq.h>
  16#include <linux/module.h>
  17#include <linux/of_graph.h>
  18#include <linux/platform_device.h>
  19#include <media/v4l2-device.h>
  20#include <media/v4l2-fwnode.h>
  21#include <media/v4l2-subdev.h>
  22#include "imx-media.h"
  23
  24/*
  25 * there must be 5 pads: 1 input pad from sensor, and
  26 * the 4 virtual channel output pads
  27 */
  28#define CSI2_SINK_PAD       0
  29#define CSI2_NUM_SINK_PADS  1
  30#define CSI2_NUM_SRC_PADS   4
  31#define CSI2_NUM_PADS       5
  32
  33/*
  34 * The default maximum bit-rate per lane in Mbps, if the
  35 * source subdev does not provide V4L2_CID_LINK_FREQ.
  36 */
  37#define CSI2_DEFAULT_MAX_MBPS 849
  38
  39struct csi2_dev {
  40        struct device          *dev;
  41        struct v4l2_subdev      sd;
  42        struct media_pad       pad[CSI2_NUM_PADS];
  43        struct clk             *dphy_clk;
  44        struct clk             *pllref_clk;
  45        struct clk             *pix_clk; /* what is this? */
  46        void __iomem           *base;
  47        struct v4l2_fwnode_bus_mipi_csi2 bus;
  48
  49        /* lock to protect all members below */
  50        struct mutex lock;
  51
  52        struct v4l2_mbus_framefmt format_mbus;
  53
  54        int                     stream_count;
  55        struct v4l2_subdev      *src_sd;
  56        bool                    sink_linked[CSI2_NUM_SRC_PADS];
  57};
  58
  59#define DEVICE_NAME "imx6-mipi-csi2"
  60
  61/* Register offsets */
  62#define CSI2_VERSION            0x000
  63#define CSI2_N_LANES            0x004
  64#define CSI2_PHY_SHUTDOWNZ      0x008
  65#define CSI2_DPHY_RSTZ          0x00c
  66#define CSI2_RESETN             0x010
  67#define CSI2_PHY_STATE          0x014
  68#define PHY_STOPSTATEDATA_BIT   4
  69#define PHY_STOPSTATEDATA(n)    BIT(PHY_STOPSTATEDATA_BIT + (n))
  70#define PHY_RXCLKACTIVEHS       BIT(8)
  71#define PHY_RXULPSCLKNOT        BIT(9)
  72#define PHY_STOPSTATECLK        BIT(10)
  73#define CSI2_DATA_IDS_1         0x018
  74#define CSI2_DATA_IDS_2         0x01c
  75#define CSI2_ERR1               0x020
  76#define CSI2_ERR2               0x024
  77#define CSI2_MSK1               0x028
  78#define CSI2_MSK2               0x02c
  79#define CSI2_PHY_TST_CTRL0      0x030
  80#define PHY_TESTCLR             BIT(0)
  81#define PHY_TESTCLK             BIT(1)
  82#define CSI2_PHY_TST_CTRL1      0x034
  83#define PHY_TESTEN              BIT(16)
  84/*
  85 * i.MX CSI2IPU Gasket registers follow. The CSI2IPU gasket is
  86 * not part of the MIPI CSI-2 core, but its registers fall in the
  87 * same register map range.
  88 */
  89#define CSI2IPU_GASKET          0xf00
  90#define CSI2IPU_YUV422_YUYV     BIT(2)
  91
  92static inline struct csi2_dev *sd_to_dev(struct v4l2_subdev *sdev)
  93{
  94        return container_of(sdev, struct csi2_dev, sd);
  95}
  96
  97/*
  98 * The required sequence of MIPI CSI-2 startup as specified in the i.MX6
  99 * reference manual is as follows:
 100 *
 101 * 1. Deassert presetn signal (global reset).
 102 *        It's not clear what this "global reset" signal is (maybe APB
 103 *        global reset), but in any case this step would be probably
 104 *        be carried out during driver load in csi2_probe().
 105 *
 106 * 2. Configure MIPI Camera Sensor to put all Tx lanes in LP-11 state.
 107 *        This must be carried out by the MIPI sensor's s_power(ON) subdev
 108 *        op.
 109 *
 110 * 3. D-PHY initialization.
 111 * 4. CSI2 Controller programming (Set N_LANES, deassert PHY_SHUTDOWNZ,
 112 *    deassert PHY_RSTZ, deassert CSI2_RESETN).
 113 * 5. Read the PHY status register (PHY_STATE) to confirm that all data and
 114 *    clock lanes of the D-PHY are in LP-11 state.
 115 * 6. Configure the MIPI Camera Sensor to start transmitting a clock on the
 116 *    D-PHY clock lane.
 117 * 7. CSI2 Controller programming - Read the PHY status register (PHY_STATE)
 118 *    to confirm that the D-PHY is receiving a clock on the D-PHY clock lane.
 119 *
 120 * All steps 3 through 7 are carried out by csi2_s_stream(ON) here. Step
 121 * 6 is accomplished by calling the source subdev's s_stream(ON) between
 122 * steps 5 and 7.
 123 */
 124
 125static void csi2_enable(struct csi2_dev *csi2, bool enable)
 126{
 127        if (enable) {
 128                writel(0x1, csi2->base + CSI2_PHY_SHUTDOWNZ);
 129                writel(0x1, csi2->base + CSI2_DPHY_RSTZ);
 130                writel(0x1, csi2->base + CSI2_RESETN);
 131        } else {
 132                writel(0x0, csi2->base + CSI2_PHY_SHUTDOWNZ);
 133                writel(0x0, csi2->base + CSI2_DPHY_RSTZ);
 134                writel(0x0, csi2->base + CSI2_RESETN);
 135        }
 136}
 137
 138static void csi2_set_lanes(struct csi2_dev *csi2)
 139{
 140        int lanes = csi2->bus.num_data_lanes;
 141
 142        writel(lanes - 1, csi2->base + CSI2_N_LANES);
 143}
 144
 145static void dw_mipi_csi2_phy_write(struct csi2_dev *csi2,
 146                                   u32 test_code, u32 test_data)
 147{
 148        /* Clear PHY test interface */
 149        writel(PHY_TESTCLR, csi2->base + CSI2_PHY_TST_CTRL0);
 150        writel(0x0, csi2->base + CSI2_PHY_TST_CTRL1);
 151        writel(0x0, csi2->base + CSI2_PHY_TST_CTRL0);
 152
 153        /* Raise test interface strobe signal */
 154        writel(PHY_TESTCLK, csi2->base + CSI2_PHY_TST_CTRL0);
 155
 156        /* Configure address write on falling edge and lower strobe signal */
 157        writel(PHY_TESTEN | test_code, csi2->base + CSI2_PHY_TST_CTRL1);
 158        writel(0x0, csi2->base + CSI2_PHY_TST_CTRL0);
 159
 160        /* Configure data write on rising edge and raise strobe signal */
 161        writel(test_data, csi2->base + CSI2_PHY_TST_CTRL1);
 162        writel(PHY_TESTCLK, csi2->base + CSI2_PHY_TST_CTRL0);
 163
 164        /* Clear strobe signal */
 165        writel(0x0, csi2->base + CSI2_PHY_TST_CTRL0);
 166}
 167
 168/*
 169 * This table is based on the table documented at
 170 * https://community.nxp.com/docs/DOC-94312. It assumes
 171 * a 27MHz D-PHY pll reference clock.
 172 */
 173static const struct {
 174        u32 max_mbps;
 175        u32 hsfreqrange_sel;
 176} hsfreq_map[] = {
 177        { 90, 0x00}, {100, 0x20}, {110, 0x40}, {125, 0x02},
 178        {140, 0x22}, {150, 0x42}, {160, 0x04}, {180, 0x24},
 179        {200, 0x44}, {210, 0x06}, {240, 0x26}, {250, 0x46},
 180        {270, 0x08}, {300, 0x28}, {330, 0x48}, {360, 0x2a},
 181        {400, 0x4a}, {450, 0x0c}, {500, 0x2c}, {550, 0x0e},
 182        {600, 0x2e}, {650, 0x10}, {700, 0x30}, {750, 0x12},
 183        {800, 0x32}, {850, 0x14}, {900, 0x34}, {950, 0x54},
 184        {1000, 0x74},
 185};
 186
 187static int max_mbps_to_hsfreqrange_sel(u32 max_mbps)
 188{
 189        int i;
 190
 191        for (i = 0; i < ARRAY_SIZE(hsfreq_map); i++)
 192                if (hsfreq_map[i].max_mbps > max_mbps)
 193                        return hsfreq_map[i].hsfreqrange_sel;
 194
 195        return -EINVAL;
 196}
 197
 198static int csi2_dphy_init(struct csi2_dev *csi2)
 199{
 200        struct v4l2_ctrl *ctrl;
 201        u32 mbps_per_lane;
 202        int sel;
 203
 204        ctrl = v4l2_ctrl_find(csi2->src_sd->ctrl_handler,
 205                              V4L2_CID_LINK_FREQ);
 206        if (!ctrl)
 207                mbps_per_lane = CSI2_DEFAULT_MAX_MBPS;
 208        else
 209                mbps_per_lane = DIV_ROUND_UP_ULL(2 * ctrl->qmenu_int[ctrl->val],
 210                                                 USEC_PER_SEC);
 211
 212        sel = max_mbps_to_hsfreqrange_sel(mbps_per_lane);
 213        if (sel < 0)
 214                return sel;
 215
 216        dw_mipi_csi2_phy_write(csi2, 0x44, sel);
 217
 218        return 0;
 219}
 220
 221/*
 222 * Waits for ultra-low-power state on D-PHY clock lane. This is currently
 223 * unused and may not be needed at all, but keep around just in case.
 224 */
 225static int __maybe_unused csi2_dphy_wait_ulp(struct csi2_dev *csi2)
 226{
 227        u32 reg;
 228        int ret;
 229
 230        /* wait for ULP on clock lane */
 231        ret = readl_poll_timeout(csi2->base + CSI2_PHY_STATE, reg,
 232                                 !(reg & PHY_RXULPSCLKNOT), 0, 500000);
 233        if (ret) {
 234                v4l2_err(&csi2->sd, "ULP timeout, phy_state = 0x%08x\n", reg);
 235                return ret;
 236        }
 237
 238        /* wait until no errors on bus */
 239        ret = readl_poll_timeout(csi2->base + CSI2_ERR1, reg,
 240                                 reg == 0x0, 0, 500000);
 241        if (ret) {
 242                v4l2_err(&csi2->sd, "stable bus timeout, err1 = 0x%08x\n", reg);
 243                return ret;
 244        }
 245
 246        return 0;
 247}
 248
 249/* Waits for low-power LP-11 state on data and clock lanes. */
 250static int csi2_dphy_wait_stopstate(struct csi2_dev *csi2)
 251{
 252        u32 mask, reg;
 253        int ret;
 254
 255        mask = PHY_STOPSTATECLK |
 256                ((csi2->bus.num_data_lanes - 1) << PHY_STOPSTATEDATA_BIT);
 257
 258        ret = readl_poll_timeout(csi2->base + CSI2_PHY_STATE, reg,
 259                                 (reg & mask) == mask, 0, 500000);
 260        if (ret) {
 261                v4l2_err(&csi2->sd, "LP-11 timeout, phy_state = 0x%08x\n", reg);
 262                return ret;
 263        }
 264
 265        return 0;
 266}
 267
 268/* Wait for active clock on the clock lane. */
 269static int csi2_dphy_wait_clock_lane(struct csi2_dev *csi2)
 270{
 271        u32 reg;
 272        int ret;
 273
 274        ret = readl_poll_timeout(csi2->base + CSI2_PHY_STATE, reg,
 275                                 (reg & PHY_RXCLKACTIVEHS), 0, 500000);
 276        if (ret) {
 277                v4l2_err(&csi2->sd, "clock lane timeout, phy_state = 0x%08x\n",
 278                         reg);
 279                return ret;
 280        }
 281
 282        return 0;
 283}
 284
 285/* Setup the i.MX CSI2IPU Gasket */
 286static void csi2ipu_gasket_init(struct csi2_dev *csi2)
 287{
 288        u32 reg = 0;
 289
 290        switch (csi2->format_mbus.code) {
 291        case MEDIA_BUS_FMT_YUYV8_2X8:
 292        case MEDIA_BUS_FMT_YUYV8_1X16:
 293                reg = CSI2IPU_YUV422_YUYV;
 294                break;
 295        default:
 296                break;
 297        }
 298
 299        writel(reg, csi2->base + CSI2IPU_GASKET);
 300}
 301
 302static int csi2_start(struct csi2_dev *csi2)
 303{
 304        int ret;
 305
 306        ret = clk_prepare_enable(csi2->pix_clk);
 307        if (ret)
 308                return ret;
 309
 310        /* setup the gasket */
 311        csi2ipu_gasket_init(csi2);
 312
 313        /* Step 3 */
 314        ret = csi2_dphy_init(csi2);
 315        if (ret)
 316                goto err_disable_clk;
 317
 318        /* Step 4 */
 319        csi2_set_lanes(csi2);
 320        csi2_enable(csi2, true);
 321
 322        /* Step 5 */
 323        ret = csi2_dphy_wait_stopstate(csi2);
 324        if (ret)
 325                goto err_assert_reset;
 326
 327        /* Step 6 */
 328        ret = v4l2_subdev_call(csi2->src_sd, video, s_stream, 1);
 329        ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
 330        if (ret)
 331                goto err_assert_reset;
 332
 333        /* Step 7 */
 334        ret = csi2_dphy_wait_clock_lane(csi2);
 335        if (ret)
 336                goto err_stop_upstream;
 337
 338        return 0;
 339
 340err_stop_upstream:
 341        v4l2_subdev_call(csi2->src_sd, video, s_stream, 0);
 342err_assert_reset:
 343        csi2_enable(csi2, false);
 344err_disable_clk:
 345        clk_disable_unprepare(csi2->pix_clk);
 346        return ret;
 347}
 348
 349static void csi2_stop(struct csi2_dev *csi2)
 350{
 351        /* stop upstream */
 352        v4l2_subdev_call(csi2->src_sd, video, s_stream, 0);
 353
 354        csi2_enable(csi2, false);
 355        clk_disable_unprepare(csi2->pix_clk);
 356}
 357
 358/*
 359 * V4L2 subdev operations.
 360 */
 361
 362static int csi2_s_stream(struct v4l2_subdev *sd, int enable)
 363{
 364        struct csi2_dev *csi2 = sd_to_dev(sd);
 365        int i, ret = 0;
 366
 367        mutex_lock(&csi2->lock);
 368
 369        if (!csi2->src_sd) {
 370                ret = -EPIPE;
 371                goto out;
 372        }
 373
 374        for (i = 0; i < CSI2_NUM_SRC_PADS; i++) {
 375                if (csi2->sink_linked[i])
 376                        break;
 377        }
 378        if (i >= CSI2_NUM_SRC_PADS) {
 379                ret = -EPIPE;
 380                goto out;
 381        }
 382
 383        /*
 384         * enable/disable streaming only if stream_count is
 385         * going from 0 to 1 / 1 to 0.
 386         */
 387        if (csi2->stream_count != !enable)
 388                goto update_count;
 389
 390        dev_dbg(csi2->dev, "stream %s\n", enable ? "ON" : "OFF");
 391        if (enable)
 392                ret = csi2_start(csi2);
 393        else
 394                csi2_stop(csi2);
 395        if (ret)
 396                goto out;
 397
 398update_count:
 399        csi2->stream_count += enable ? 1 : -1;
 400        if (csi2->stream_count < 0)
 401                csi2->stream_count = 0;
 402out:
 403        mutex_unlock(&csi2->lock);
 404        return ret;
 405}
 406
 407static int csi2_link_setup(struct media_entity *entity,
 408                           const struct media_pad *local,
 409                           const struct media_pad *remote, u32 flags)
 410{
 411        struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
 412        struct csi2_dev *csi2 = sd_to_dev(sd);
 413        struct v4l2_subdev *remote_sd;
 414        int ret = 0;
 415
 416        dev_dbg(csi2->dev, "link setup %s -> %s", remote->entity->name,
 417                local->entity->name);
 418
 419        remote_sd = media_entity_to_v4l2_subdev(remote->entity);
 420
 421        mutex_lock(&csi2->lock);
 422
 423        if (local->flags & MEDIA_PAD_FL_SOURCE) {
 424                if (flags & MEDIA_LNK_FL_ENABLED) {
 425                        if (csi2->sink_linked[local->index - 1]) {
 426                                ret = -EBUSY;
 427                                goto out;
 428                        }
 429                        csi2->sink_linked[local->index - 1] = true;
 430                } else {
 431                        csi2->sink_linked[local->index - 1] = false;
 432                }
 433        } else {
 434                if (flags & MEDIA_LNK_FL_ENABLED) {
 435                        if (csi2->src_sd) {
 436                                ret = -EBUSY;
 437                                goto out;
 438                        }
 439                        csi2->src_sd = remote_sd;
 440                } else {
 441                        csi2->src_sd = NULL;
 442                }
 443        }
 444
 445out:
 446        mutex_unlock(&csi2->lock);
 447        return ret;
 448}
 449
 450static int csi2_get_fmt(struct v4l2_subdev *sd,
 451                        struct v4l2_subdev_pad_config *cfg,
 452                        struct v4l2_subdev_format *sdformat)
 453{
 454        struct csi2_dev *csi2 = sd_to_dev(sd);
 455        struct v4l2_mbus_framefmt *fmt;
 456
 457        mutex_lock(&csi2->lock);
 458
 459        if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
 460                fmt = v4l2_subdev_get_try_format(&csi2->sd, cfg,
 461                                                 sdformat->pad);
 462        else
 463                fmt = &csi2->format_mbus;
 464
 465        sdformat->format = *fmt;
 466
 467        mutex_unlock(&csi2->lock);
 468
 469        return 0;
 470}
 471
 472static int csi2_set_fmt(struct v4l2_subdev *sd,
 473                        struct v4l2_subdev_pad_config *cfg,
 474                        struct v4l2_subdev_format *sdformat)
 475{
 476        struct csi2_dev *csi2 = sd_to_dev(sd);
 477        int ret = 0;
 478
 479        if (sdformat->pad >= CSI2_NUM_PADS)
 480                return -EINVAL;
 481
 482        mutex_lock(&csi2->lock);
 483
 484        if (csi2->stream_count > 0) {
 485                ret = -EBUSY;
 486                goto out;
 487        }
 488
 489        /* Output pads mirror active input pad, no limits on input pads */
 490        if (sdformat->pad != CSI2_SINK_PAD)
 491                sdformat->format = csi2->format_mbus;
 492
 493        if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
 494                cfg->try_fmt = sdformat->format;
 495        else
 496                csi2->format_mbus = sdformat->format;
 497out:
 498        mutex_unlock(&csi2->lock);
 499        return ret;
 500}
 501
 502/*
 503 * retrieve our pads parsed from the OF graph by the media device
 504 */
 505static int csi2_registered(struct v4l2_subdev *sd)
 506{
 507        struct csi2_dev *csi2 = sd_to_dev(sd);
 508        int i, ret;
 509
 510        for (i = 0; i < CSI2_NUM_PADS; i++) {
 511                csi2->pad[i].flags = (i == CSI2_SINK_PAD) ?
 512                MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
 513        }
 514
 515        /* set a default mbus format  */
 516        ret = imx_media_init_mbus_fmt(&csi2->format_mbus,
 517                                      640, 480, 0, V4L2_FIELD_NONE, NULL);
 518        if (ret)
 519                return ret;
 520
 521        return media_entity_pads_init(&sd->entity, CSI2_NUM_PADS, csi2->pad);
 522}
 523
 524static const struct media_entity_operations csi2_entity_ops = {
 525        .link_setup = csi2_link_setup,
 526        .link_validate = v4l2_subdev_link_validate,
 527};
 528
 529static const struct v4l2_subdev_video_ops csi2_video_ops = {
 530        .s_stream = csi2_s_stream,
 531};
 532
 533static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
 534        .get_fmt = csi2_get_fmt,
 535        .set_fmt = csi2_set_fmt,
 536};
 537
 538static const struct v4l2_subdev_ops csi2_subdev_ops = {
 539        .video = &csi2_video_ops,
 540        .pad = &csi2_pad_ops,
 541};
 542
 543static const struct v4l2_subdev_internal_ops csi2_internal_ops = {
 544        .registered = csi2_registered,
 545};
 546
 547static int csi2_parse_endpoints(struct csi2_dev *csi2)
 548{
 549        struct device_node *node = csi2->dev->of_node;
 550        struct device_node *epnode;
 551        struct v4l2_fwnode_endpoint ep;
 552
 553        epnode = of_graph_get_endpoint_by_regs(node, 0, -1);
 554        if (!epnode) {
 555                v4l2_err(&csi2->sd, "failed to get sink endpoint node\n");
 556                return -EINVAL;
 557        }
 558
 559        v4l2_fwnode_endpoint_parse(of_fwnode_handle(epnode), &ep);
 560        of_node_put(epnode);
 561
 562        if (ep.bus_type != V4L2_MBUS_CSI2) {
 563                v4l2_err(&csi2->sd, "invalid bus type, must be MIPI CSI2\n");
 564                return -EINVAL;
 565        }
 566
 567        csi2->bus = ep.bus.mipi_csi2;
 568
 569        dev_dbg(csi2->dev, "data lanes: %d\n", csi2->bus.num_data_lanes);
 570        dev_dbg(csi2->dev, "flags: 0x%08x\n", csi2->bus.flags);
 571        return 0;
 572}
 573
 574static int csi2_probe(struct platform_device *pdev)
 575{
 576        struct csi2_dev *csi2;
 577        struct resource *res;
 578        int ret;
 579
 580        csi2 = devm_kzalloc(&pdev->dev, sizeof(*csi2), GFP_KERNEL);
 581        if (!csi2)
 582                return -ENOMEM;
 583
 584        csi2->dev = &pdev->dev;
 585
 586        v4l2_subdev_init(&csi2->sd, &csi2_subdev_ops);
 587        v4l2_set_subdevdata(&csi2->sd, &pdev->dev);
 588        csi2->sd.internal_ops = &csi2_internal_ops;
 589        csi2->sd.entity.ops = &csi2_entity_ops;
 590        csi2->sd.dev = &pdev->dev;
 591        csi2->sd.owner = THIS_MODULE;
 592        csi2->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
 593        strcpy(csi2->sd.name, DEVICE_NAME);
 594        csi2->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
 595        csi2->sd.grp_id = IMX_MEDIA_GRP_ID_CSI2;
 596
 597        ret = csi2_parse_endpoints(csi2);
 598        if (ret)
 599                return ret;
 600
 601        csi2->pllref_clk = devm_clk_get(&pdev->dev, "ref");
 602        if (IS_ERR(csi2->pllref_clk)) {
 603                v4l2_err(&csi2->sd, "failed to get pll reference clock\n");
 604                ret = PTR_ERR(csi2->pllref_clk);
 605                return ret;
 606        }
 607
 608        csi2->dphy_clk = devm_clk_get(&pdev->dev, "dphy");
 609        if (IS_ERR(csi2->dphy_clk)) {
 610                v4l2_err(&csi2->sd, "failed to get dphy clock\n");
 611                ret = PTR_ERR(csi2->dphy_clk);
 612                return ret;
 613        }
 614
 615        csi2->pix_clk = devm_clk_get(&pdev->dev, "pix");
 616        if (IS_ERR(csi2->pix_clk)) {
 617                v4l2_err(&csi2->sd, "failed to get pixel clock\n");
 618                ret = PTR_ERR(csi2->pix_clk);
 619                return ret;
 620        }
 621
 622        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 623        if (!res) {
 624                v4l2_err(&csi2->sd, "failed to get platform resources\n");
 625                return -ENODEV;
 626        }
 627
 628        csi2->base = devm_ioremap(&pdev->dev, res->start, PAGE_SIZE);
 629        if (!csi2->base) {
 630                v4l2_err(&csi2->sd, "failed to map CSI-2 registers\n");
 631                return -ENOMEM;
 632        }
 633
 634        mutex_init(&csi2->lock);
 635
 636        ret = clk_prepare_enable(csi2->pllref_clk);
 637        if (ret) {
 638                v4l2_err(&csi2->sd, "failed to enable pllref_clk\n");
 639                goto rmmutex;
 640        }
 641
 642        ret = clk_prepare_enable(csi2->dphy_clk);
 643        if (ret) {
 644                v4l2_err(&csi2->sd, "failed to enable dphy_clk\n");
 645                goto pllref_off;
 646        }
 647
 648        platform_set_drvdata(pdev, &csi2->sd);
 649
 650        ret = v4l2_async_register_subdev(&csi2->sd);
 651        if (ret)
 652                goto dphy_off;
 653
 654        return 0;
 655
 656dphy_off:
 657        clk_disable_unprepare(csi2->dphy_clk);
 658pllref_off:
 659        clk_disable_unprepare(csi2->pllref_clk);
 660rmmutex:
 661        mutex_destroy(&csi2->lock);
 662        return ret;
 663}
 664
 665static int csi2_remove(struct platform_device *pdev)
 666{
 667        struct v4l2_subdev *sd = platform_get_drvdata(pdev);
 668        struct csi2_dev *csi2 = sd_to_dev(sd);
 669
 670        v4l2_async_unregister_subdev(sd);
 671        clk_disable_unprepare(csi2->dphy_clk);
 672        clk_disable_unprepare(csi2->pllref_clk);
 673        mutex_destroy(&csi2->lock);
 674        media_entity_cleanup(&sd->entity);
 675
 676        return 0;
 677}
 678
 679static const struct of_device_id csi2_dt_ids[] = {
 680        { .compatible = "fsl,imx6-mipi-csi2", },
 681        { /* sentinel */ }
 682};
 683MODULE_DEVICE_TABLE(of, csi2_dt_ids);
 684
 685static struct platform_driver csi2_driver = {
 686        .driver = {
 687                .name = DEVICE_NAME,
 688                .of_match_table = csi2_dt_ids,
 689        },
 690        .probe = csi2_probe,
 691        .remove = csi2_remove,
 692};
 693
 694module_platform_driver(csi2_driver);
 695
 696MODULE_DESCRIPTION("i.MX5/6 MIPI CSI-2 Receiver driver");
 697MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
 698MODULE_LICENSE("GPL");
 699