linux/drivers/media/platform/exynos4-is/media-dev.c
<<
>>
Prefs
   1/*
   2 * S5P/EXYNOS4 SoC series camera host interface media device driver
   3 *
   4 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
   5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published
   9 * by the Free Software Foundation, either version 2 of the License,
  10 * or (at your option) any later version.
  11 */
  12
  13#include <linux/bug.h>
  14#include <linux/clk.h>
  15#include <linux/clk-provider.h>
  16#include <linux/device.h>
  17#include <linux/errno.h>
  18#include <linux/i2c.h>
  19#include <linux/kernel.h>
  20#include <linux/list.h>
  21#include <linux/module.h>
  22#include <linux/of.h>
  23#include <linux/of_platform.h>
  24#include <linux/of_device.h>
  25#include <linux/of_graph.h>
  26#include <linux/platform_device.h>
  27#include <linux/pm_runtime.h>
  28#include <linux/types.h>
  29#include <linux/slab.h>
  30#include <media/v4l2-async.h>
  31#include <media/v4l2-ctrls.h>
  32#include <media/v4l2-of.h>
  33#include <media/media-device.h>
  34#include <media/drv-intf/exynos-fimc.h>
  35
  36#include "media-dev.h"
  37#include "fimc-core.h"
  38#include "fimc-is.h"
  39#include "fimc-lite.h"
  40#include "mipi-csis.h"
  41
  42/* Set up image sensor subdev -> FIMC capture node notifications. */
  43static void __setup_sensor_notification(struct fimc_md *fmd,
  44                                        struct v4l2_subdev *sensor,
  45                                        struct v4l2_subdev *fimc_sd)
  46{
  47        struct fimc_source_info *src_inf;
  48        struct fimc_sensor_info *md_si;
  49        unsigned long flags;
  50
  51        src_inf = v4l2_get_subdev_hostdata(sensor);
  52        if (!src_inf || WARN_ON(fmd == NULL))
  53                return;
  54
  55        md_si = source_to_sensor_info(src_inf);
  56        spin_lock_irqsave(&fmd->slock, flags);
  57        md_si->host = v4l2_get_subdevdata(fimc_sd);
  58        spin_unlock_irqrestore(&fmd->slock, flags);
  59}
  60
  61/**
  62 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
  63 * @me: media entity terminating the pipeline
  64 *
  65 * Caller holds the graph mutex.
  66 */
  67static void fimc_pipeline_prepare(struct fimc_pipeline *p,
  68                                        struct media_entity *me)
  69{
  70        struct fimc_md *fmd = entity_to_fimc_mdev(me);
  71        struct v4l2_subdev *sd;
  72        struct v4l2_subdev *sensor = NULL;
  73        int i;
  74
  75        for (i = 0; i < IDX_MAX; i++)
  76                p->subdevs[i] = NULL;
  77
  78        while (1) {
  79                struct media_pad *pad = NULL;
  80
  81                /* Find remote source pad */
  82                for (i = 0; i < me->num_pads; i++) {
  83                        struct media_pad *spad = &me->pads[i];
  84                        if (!(spad->flags & MEDIA_PAD_FL_SINK))
  85                                continue;
  86                        pad = media_entity_remote_pad(spad);
  87                        if (pad)
  88                                break;
  89                }
  90
  91                if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
  92                        break;
  93                sd = media_entity_to_v4l2_subdev(pad->entity);
  94
  95                switch (sd->grp_id) {
  96                case GRP_ID_SENSOR:
  97                        sensor = sd;
  98                        /* fall through */
  99                case GRP_ID_FIMC_IS_SENSOR:
 100                        p->subdevs[IDX_SENSOR] = sd;
 101                        break;
 102                case GRP_ID_CSIS:
 103                        p->subdevs[IDX_CSIS] = sd;
 104                        break;
 105                case GRP_ID_FLITE:
 106                        p->subdevs[IDX_FLITE] = sd;
 107                        break;
 108                case GRP_ID_FIMC:
 109                        p->subdevs[IDX_FIMC] = sd;
 110                        break;
 111                case GRP_ID_FIMC_IS:
 112                        p->subdevs[IDX_IS_ISP] = sd;
 113                        break;
 114                default:
 115                        break;
 116                }
 117                me = &sd->entity;
 118                if (me->num_pads == 1)
 119                        break;
 120        }
 121
 122        if (sensor && p->subdevs[IDX_FIMC])
 123                __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
 124}
 125
 126/**
 127 * __subdev_set_power - change power state of a single subdev
 128 * @sd: subdevice to change power state for
 129 * @on: 1 to enable power or 0 to disable
 130 *
 131 * Return result of s_power subdev operation or -ENXIO if sd argument
 132 * is NULL. Return 0 if the subdevice does not implement s_power.
 133 */
 134static int __subdev_set_power(struct v4l2_subdev *sd, int on)
 135{
 136        int *use_count;
 137        int ret;
 138
 139        if (sd == NULL)
 140                return -ENXIO;
 141
 142        use_count = &sd->entity.use_count;
 143        if (on && (*use_count)++ > 0)
 144                return 0;
 145        else if (!on && (*use_count == 0 || --(*use_count) > 0))
 146                return 0;
 147        ret = v4l2_subdev_call(sd, core, s_power, on);
 148
 149        return ret != -ENOIOCTLCMD ? ret : 0;
 150}
 151
 152/**
 153 * fimc_pipeline_s_power - change power state of all pipeline subdevs
 154 * @fimc: fimc device terminating the pipeline
 155 * @state: true to power on, false to power off
 156 *
 157 * Needs to be called with the graph mutex held.
 158 */
 159static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
 160{
 161        static const u8 seq[2][IDX_MAX - 1] = {
 162                { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
 163                { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
 164        };
 165        int i, ret = 0;
 166
 167        if (p->subdevs[IDX_SENSOR] == NULL)
 168                return -ENXIO;
 169
 170        for (i = 0; i < IDX_MAX - 1; i++) {
 171                unsigned int idx = seq[on][i];
 172
 173                ret = __subdev_set_power(p->subdevs[idx], on);
 174
 175
 176                if (ret < 0 && ret != -ENXIO)
 177                        goto error;
 178        }
 179        return 0;
 180error:
 181        for (; i >= 0; i--) {
 182                unsigned int idx = seq[on][i];
 183                __subdev_set_power(p->subdevs[idx], !on);
 184        }
 185        return ret;
 186}
 187
 188/**
 189 * __fimc_pipeline_enable - enable power of all pipeline subdevs
 190 *                          and the sensor clock
 191 * @ep: video pipeline structure
 192 * @fmd: fimc media device
 193 *
 194 * Called with the graph mutex held.
 195 */
 196static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
 197                                  struct fimc_md *fmd)
 198{
 199        struct fimc_pipeline *p = to_fimc_pipeline(ep);
 200        int ret;
 201
 202        /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
 203        if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
 204                ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
 205                if (ret < 0)
 206                        return ret;
 207        }
 208
 209        ret = fimc_pipeline_s_power(p, 1);
 210        if (!ret)
 211                return 0;
 212
 213        if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
 214                clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
 215
 216        return ret;
 217}
 218
 219/**
 220 * __fimc_pipeline_open - update the pipeline information, enable power
 221 *                        of all pipeline subdevs and the sensor clock
 222 * @me: media entity to start graph walk with
 223 * @prepare: true to walk the current pipeline and acquire all subdevs
 224 *
 225 * Called with the graph mutex held.
 226 */
 227static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
 228                                struct media_entity *me, bool prepare)
 229{
 230        struct fimc_md *fmd = entity_to_fimc_mdev(me);
 231        struct fimc_pipeline *p = to_fimc_pipeline(ep);
 232        struct v4l2_subdev *sd;
 233
 234        if (WARN_ON(p == NULL || me == NULL))
 235                return -EINVAL;
 236
 237        if (prepare)
 238                fimc_pipeline_prepare(p, me);
 239
 240        sd = p->subdevs[IDX_SENSOR];
 241        if (sd == NULL) {
 242                pr_warn("%s(): No sensor subdev\n", __func__);
 243                /*
 244                 * Pipeline open cannot fail so as to make it possible
 245                 * for the user space to configure the pipeline.
 246                 */
 247                return 0;
 248        }
 249
 250        return __fimc_pipeline_enable(ep, fmd);
 251}
 252
 253/**
 254 * __fimc_pipeline_close - disable the sensor clock and pipeline power
 255 * @fimc: fimc device terminating the pipeline
 256 *
 257 * Disable power of all subdevs and turn the external sensor clock off.
 258 */
 259static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
 260{
 261        struct fimc_pipeline *p = to_fimc_pipeline(ep);
 262        struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
 263        struct fimc_md *fmd;
 264        int ret;
 265
 266        if (sd == NULL) {
 267                pr_warn("%s(): No sensor subdev\n", __func__);
 268                return 0;
 269        }
 270
 271        ret = fimc_pipeline_s_power(p, 0);
 272
 273        fmd = entity_to_fimc_mdev(&sd->entity);
 274
 275        /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
 276        if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
 277                clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
 278
 279        return ret == -ENXIO ? 0 : ret;
 280}
 281
 282/**
 283 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
 284 * @pipeline: video pipeline structure
 285 * @on: passed as the s_stream() callback argument
 286 */
 287static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
 288{
 289        static const u8 seq[2][IDX_MAX] = {
 290                { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
 291                { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
 292        };
 293        struct fimc_pipeline *p = to_fimc_pipeline(ep);
 294        struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
 295        enum fimc_subdev_index sd_id;
 296        int i, ret = 0;
 297
 298        if (p->subdevs[IDX_SENSOR] == NULL) {
 299                if (!fmd->user_subdev_api) {
 300                        /*
 301                         * Sensor must be already discovered if we
 302                         * aren't in the user_subdev_api mode
 303                         */
 304                        return -ENODEV;
 305                }
 306
 307                /* Get pipeline sink entity */
 308                if (p->subdevs[IDX_FIMC])
 309                        sd_id = IDX_FIMC;
 310                else if (p->subdevs[IDX_IS_ISP])
 311                        sd_id = IDX_IS_ISP;
 312                else if (p->subdevs[IDX_FLITE])
 313                        sd_id = IDX_FLITE;
 314                else
 315                        return -ENODEV;
 316
 317                /*
 318                 * Sensor could have been linked between open and STREAMON -
 319                 * check if this is the case.
 320                 */
 321                fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
 322
 323                if (p->subdevs[IDX_SENSOR] == NULL)
 324                        return -ENODEV;
 325
 326                ret = __fimc_pipeline_enable(ep, fmd);
 327                if (ret < 0)
 328                        return ret;
 329
 330        }
 331
 332        for (i = 0; i < IDX_MAX; i++) {
 333                unsigned int idx = seq[on][i];
 334
 335                ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
 336
 337                if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
 338                        goto error;
 339        }
 340
 341        return 0;
 342error:
 343        fimc_pipeline_s_power(p, !on);
 344        for (; i >= 0; i--) {
 345                unsigned int idx = seq[on][i];
 346                v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
 347        }
 348        return ret;
 349}
 350
 351/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
 352static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
 353        .open           = __fimc_pipeline_open,
 354        .close          = __fimc_pipeline_close,
 355        .set_stream     = __fimc_pipeline_s_stream,
 356};
 357
 358static struct exynos_media_pipeline *fimc_md_pipeline_create(
 359                                                struct fimc_md *fmd)
 360{
 361        struct fimc_pipeline *p;
 362
 363        p = kzalloc(sizeof(*p), GFP_KERNEL);
 364        if (!p)
 365                return NULL;
 366
 367        list_add_tail(&p->list, &fmd->pipelines);
 368
 369        p->ep.ops = &fimc_pipeline_ops;
 370        return &p->ep;
 371}
 372
 373static void fimc_md_pipelines_free(struct fimc_md *fmd)
 374{
 375        while (!list_empty(&fmd->pipelines)) {
 376                struct fimc_pipeline *p;
 377
 378                p = list_entry(fmd->pipelines.next, typeof(*p), list);
 379                list_del(&p->list);
 380                kfree(p);
 381        }
 382}
 383
 384/* Parse port node and register as a sub-device any sensor specified there. */
 385static int fimc_md_parse_port_node(struct fimc_md *fmd,
 386                                   struct device_node *port,
 387                                   unsigned int index)
 388{
 389        struct fimc_source_info *pd = &fmd->sensor[index].pdata;
 390        struct device_node *rem, *ep, *np;
 391        struct v4l2_of_endpoint endpoint;
 392        int ret;
 393
 394        /* Assume here a port node can have only one endpoint node. */
 395        ep = of_get_next_child(port, NULL);
 396        if (!ep)
 397                return 0;
 398
 399        ret = v4l2_of_parse_endpoint(ep, &endpoint);
 400        if (ret) {
 401                of_node_put(ep);
 402                return ret;
 403        }
 404
 405        if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS)
 406                return -EINVAL;
 407
 408        pd->mux_id = (endpoint.base.port - 1) & 0x1;
 409
 410        rem = of_graph_get_remote_port_parent(ep);
 411        of_node_put(ep);
 412        if (rem == NULL) {
 413                v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
 414                                                        ep->full_name);
 415                return 0;
 416        }
 417
 418        if (fimc_input_is_parallel(endpoint.base.port)) {
 419                if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
 420                        pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
 421                else
 422                        pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
 423                pd->flags = endpoint.bus.parallel.flags;
 424        } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
 425                /*
 426                 * MIPI CSI-2: only input mux selection and
 427                 * the sensor's clock frequency is needed.
 428                 */
 429                pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
 430        } else {
 431                v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
 432                         endpoint.base.port, rem->full_name);
 433        }
 434        /*
 435         * For FIMC-IS handled sensors, that are placed under i2c-isp device
 436         * node, FIMC is connected to the FIMC-IS through its ISP Writeback
 437         * input. Sensors are attached to the FIMC-LITE hostdata interface
 438         * directly or through MIPI-CSIS, depending on the external media bus
 439         * used. This needs to be handled in a more reliable way, not by just
 440         * checking parent's node name.
 441         */
 442        np = of_get_parent(rem);
 443
 444        if (np && !of_node_cmp(np->name, "i2c-isp"))
 445                pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
 446        else
 447                pd->fimc_bus_type = pd->sensor_bus_type;
 448
 449        if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
 450                return -EINVAL;
 451
 452        fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
 453        fmd->sensor[index].asd.match.of.node = rem;
 454        fmd->async_subdevs[index] = &fmd->sensor[index].asd;
 455
 456        fmd->num_sensors++;
 457
 458        of_node_put(rem);
 459        return 0;
 460}
 461
 462/* Register all SoC external sub-devices */
 463static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
 464{
 465        struct device_node *parent = fmd->pdev->dev.of_node;
 466        struct device_node *node, *ports;
 467        int index = 0;
 468        int ret;
 469
 470        /*
 471         * Runtime resume one of the FIMC entities to make sure
 472         * the sclk_cam clocks are not globally disabled.
 473         */
 474        if (!fmd->pmf)
 475                return -ENXIO;
 476
 477        ret = pm_runtime_get_sync(fmd->pmf);
 478        if (ret < 0)
 479                return ret;
 480
 481        fmd->num_sensors = 0;
 482
 483        /* Attach sensors linked to MIPI CSI-2 receivers */
 484        for_each_available_child_of_node(parent, node) {
 485                struct device_node *port;
 486
 487                if (of_node_cmp(node->name, "csis"))
 488                        continue;
 489                /* The csis node can have only port subnode. */
 490                port = of_get_next_child(node, NULL);
 491                if (!port)
 492                        continue;
 493
 494                ret = fimc_md_parse_port_node(fmd, port, index);
 495                if (ret < 0) {
 496                        of_node_put(node);
 497                        goto rpm_put;
 498                }
 499                index++;
 500        }
 501
 502        /* Attach sensors listed in the parallel-ports node */
 503        ports = of_get_child_by_name(parent, "parallel-ports");
 504        if (!ports)
 505                goto rpm_put;
 506
 507        for_each_child_of_node(ports, node) {
 508                ret = fimc_md_parse_port_node(fmd, node, index);
 509                if (ret < 0) {
 510                        of_node_put(node);
 511                        break;
 512                }
 513                index++;
 514        }
 515rpm_put:
 516        pm_runtime_put(fmd->pmf);
 517        return ret;
 518}
 519
 520static int __of_get_csis_id(struct device_node *np)
 521{
 522        u32 reg = 0;
 523
 524        np = of_get_child_by_name(np, "port");
 525        if (!np)
 526                return -EINVAL;
 527        of_property_read_u32(np, "reg", &reg);
 528        return reg - FIMC_INPUT_MIPI_CSI2_0;
 529}
 530
 531/*
 532 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
 533 */
 534static int register_fimc_lite_entity(struct fimc_md *fmd,
 535                                     struct fimc_lite *fimc_lite)
 536{
 537        struct v4l2_subdev *sd;
 538        struct exynos_media_pipeline *ep;
 539        int ret;
 540
 541        if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
 542                    fmd->fimc_lite[fimc_lite->index]))
 543                return -EBUSY;
 544
 545        sd = &fimc_lite->subdev;
 546        sd->grp_id = GRP_ID_FLITE;
 547
 548        ep = fimc_md_pipeline_create(fmd);
 549        if (!ep)
 550                return -ENOMEM;
 551
 552        v4l2_set_subdev_hostdata(sd, ep);
 553
 554        ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
 555        if (!ret)
 556                fmd->fimc_lite[fimc_lite->index] = fimc_lite;
 557        else
 558                v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
 559                         fimc_lite->index);
 560        return ret;
 561}
 562
 563static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
 564{
 565        struct v4l2_subdev *sd;
 566        struct exynos_media_pipeline *ep;
 567        int ret;
 568
 569        if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
 570                return -EBUSY;
 571
 572        sd = &fimc->vid_cap.subdev;
 573        sd->grp_id = GRP_ID_FIMC;
 574
 575        ep = fimc_md_pipeline_create(fmd);
 576        if (!ep)
 577                return -ENOMEM;
 578
 579        v4l2_set_subdev_hostdata(sd, ep);
 580
 581        ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
 582        if (!ret) {
 583                if (!fmd->pmf && fimc->pdev)
 584                        fmd->pmf = &fimc->pdev->dev;
 585                fmd->fimc[fimc->id] = fimc;
 586                fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
 587        } else {
 588                v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
 589                         fimc->id, ret);
 590        }
 591        return ret;
 592}
 593
 594static int register_csis_entity(struct fimc_md *fmd,
 595                                struct platform_device *pdev,
 596                                struct v4l2_subdev *sd)
 597{
 598        struct device_node *node = pdev->dev.of_node;
 599        int id, ret;
 600
 601        id = node ? __of_get_csis_id(node) : max(0, pdev->id);
 602
 603        if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
 604                return -ENOENT;
 605
 606        if (WARN_ON(fmd->csis[id].sd))
 607                return -EBUSY;
 608
 609        sd->grp_id = GRP_ID_CSIS;
 610        ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
 611        if (!ret)
 612                fmd->csis[id].sd = sd;
 613        else
 614                v4l2_err(&fmd->v4l2_dev,
 615                         "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
 616        return ret;
 617}
 618
 619static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
 620{
 621        struct v4l2_subdev *sd = &is->isp.subdev;
 622        struct exynos_media_pipeline *ep;
 623        int ret;
 624
 625        /* Allocate pipeline object for the ISP capture video node. */
 626        ep = fimc_md_pipeline_create(fmd);
 627        if (!ep)
 628                return -ENOMEM;
 629
 630        v4l2_set_subdev_hostdata(sd, ep);
 631
 632        ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
 633        if (ret) {
 634                v4l2_err(&fmd->v4l2_dev,
 635                         "Failed to register FIMC-ISP (%d)\n", ret);
 636                return ret;
 637        }
 638
 639        fmd->fimc_is = is;
 640        return 0;
 641}
 642
 643static int fimc_md_register_platform_entity(struct fimc_md *fmd,
 644                                            struct platform_device *pdev,
 645                                            int plat_entity)
 646{
 647        struct device *dev = &pdev->dev;
 648        int ret = -EPROBE_DEFER;
 649        void *drvdata;
 650
 651        /* Lock to ensure dev->driver won't change. */
 652        device_lock(dev);
 653
 654        if (!dev->driver || !try_module_get(dev->driver->owner))
 655                goto dev_unlock;
 656
 657        drvdata = dev_get_drvdata(dev);
 658        /* Some subdev didn't probe successfully id drvdata is NULL */
 659        if (drvdata) {
 660                switch (plat_entity) {
 661                case IDX_FIMC:
 662                        ret = register_fimc_entity(fmd, drvdata);
 663                        break;
 664                case IDX_FLITE:
 665                        ret = register_fimc_lite_entity(fmd, drvdata);
 666                        break;
 667                case IDX_CSIS:
 668                        ret = register_csis_entity(fmd, pdev, drvdata);
 669                        break;
 670                case IDX_IS_ISP:
 671                        ret = register_fimc_is_entity(fmd, drvdata);
 672                        break;
 673                default:
 674                        ret = -ENODEV;
 675                }
 676        }
 677
 678        module_put(dev->driver->owner);
 679dev_unlock:
 680        device_unlock(dev);
 681        if (ret == -EPROBE_DEFER)
 682                dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
 683                        dev_name(dev));
 684        else if (ret < 0)
 685                dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
 686                        dev_name(dev), ret);
 687        return ret;
 688}
 689
 690/* Register FIMC, FIMC-LITE and CSIS media entities */
 691static int fimc_md_register_platform_entities(struct fimc_md *fmd,
 692                                              struct device_node *parent)
 693{
 694        struct device_node *node;
 695        int ret = 0;
 696
 697        for_each_available_child_of_node(parent, node) {
 698                struct platform_device *pdev;
 699                int plat_entity = -1;
 700
 701                pdev = of_find_device_by_node(node);
 702                if (!pdev)
 703                        continue;
 704
 705                /* If driver of any entity isn't ready try all again later. */
 706                if (!strcmp(node->name, CSIS_OF_NODE_NAME))
 707                        plat_entity = IDX_CSIS;
 708                else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
 709                        plat_entity = IDX_IS_ISP;
 710                else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
 711                        plat_entity = IDX_FLITE;
 712                else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
 713                         !of_property_read_bool(node, "samsung,lcd-wb"))
 714                        plat_entity = IDX_FIMC;
 715
 716                if (plat_entity >= 0)
 717                        ret = fimc_md_register_platform_entity(fmd, pdev,
 718                                                        plat_entity);
 719                put_device(&pdev->dev);
 720                if (ret < 0) {
 721                        of_node_put(node);
 722                        break;
 723                }
 724        }
 725
 726        return ret;
 727}
 728
 729static void fimc_md_unregister_entities(struct fimc_md *fmd)
 730{
 731        int i;
 732
 733        for (i = 0; i < FIMC_MAX_DEVS; i++) {
 734                struct fimc_dev *dev = fmd->fimc[i];
 735                if (dev == NULL)
 736                        continue;
 737                v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
 738                dev->vid_cap.ve.pipe = NULL;
 739                fmd->fimc[i] = NULL;
 740        }
 741        for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
 742                struct fimc_lite *dev = fmd->fimc_lite[i];
 743                if (dev == NULL)
 744                        continue;
 745                v4l2_device_unregister_subdev(&dev->subdev);
 746                dev->ve.pipe = NULL;
 747                fmd->fimc_lite[i] = NULL;
 748        }
 749        for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
 750                if (fmd->csis[i].sd == NULL)
 751                        continue;
 752                v4l2_device_unregister_subdev(fmd->csis[i].sd);
 753                fmd->csis[i].sd = NULL;
 754        }
 755
 756        if (fmd->fimc_is)
 757                v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
 758
 759        v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
 760}
 761
 762/**
 763 * __fimc_md_create_fimc_links - create links to all FIMC entities
 764 * @fmd: fimc media device
 765 * @source: the source entity to create links to all fimc entities from
 766 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
 767 * @pad: the source entity pad index
 768 * @link_mask: bitmask of the fimc devices for which link should be enabled
 769 */
 770static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
 771                                            struct media_entity *source,
 772                                            struct v4l2_subdev *sensor,
 773                                            int pad, int link_mask)
 774{
 775        struct fimc_source_info *si = NULL;
 776        struct media_entity *sink;
 777        unsigned int flags = 0;
 778        int i, ret = 0;
 779
 780        if (sensor) {
 781                si = v4l2_get_subdev_hostdata(sensor);
 782                /* Skip direct FIMC links in the logical FIMC-IS sensor path */
 783                if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
 784                        ret = 1;
 785        }
 786
 787        for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
 788                if (!fmd->fimc[i])
 789                        continue;
 790                /*
 791                 * Some FIMC variants are not fitted with camera capture
 792                 * interface. Skip creating a link from sensor for those.
 793                 */
 794                if (!fmd->fimc[i]->variant->has_cam_if)
 795                        continue;
 796
 797                flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
 798
 799                sink = &fmd->fimc[i]->vid_cap.subdev.entity;
 800                ret = media_create_pad_link(source, pad, sink,
 801                                              FIMC_SD_PAD_SINK_CAM, flags);
 802                if (ret)
 803                        return ret;
 804
 805                /* Notify FIMC capture subdev entity */
 806                ret = media_entity_call(sink, link_setup, &sink->pads[0],
 807                                        &source->pads[pad], flags);
 808                if (ret)
 809                        break;
 810
 811                v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
 812                          source->name, flags ? '=' : '-', sink->name);
 813        }
 814
 815        for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
 816                if (!fmd->fimc_lite[i])
 817                        continue;
 818
 819                sink = &fmd->fimc_lite[i]->subdev.entity;
 820                ret = media_create_pad_link(source, pad, sink,
 821                                               FLITE_SD_PAD_SINK, 0);
 822                if (ret)
 823                        return ret;
 824
 825                /* Notify FIMC-LITE subdev entity */
 826                ret = media_entity_call(sink, link_setup, &sink->pads[0],
 827                                        &source->pads[pad], 0);
 828                if (ret)
 829                        break;
 830
 831                v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
 832                          source->name, sink->name);
 833        }
 834        return 0;
 835}
 836
 837/* Create links from FIMC-LITE source pads to other entities */
 838static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
 839{
 840        struct media_entity *source, *sink;
 841        int i, ret = 0;
 842
 843        for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
 844                struct fimc_lite *fimc = fmd->fimc_lite[i];
 845
 846                if (fimc == NULL)
 847                        continue;
 848
 849                source = &fimc->subdev.entity;
 850                sink = &fimc->ve.vdev.entity;
 851                /* FIMC-LITE's subdev and video node */
 852                ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
 853                                               sink, 0, 0);
 854                if (ret)
 855                        break;
 856                /* Link from FIMC-LITE to IS-ISP subdev */
 857                sink = &fmd->fimc_is->isp.subdev.entity;
 858                ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
 859                                               sink, 0, 0);
 860                if (ret)
 861                        break;
 862        }
 863
 864        return ret;
 865}
 866
 867/* Create FIMC-IS links */
 868static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
 869{
 870        struct fimc_isp *isp = &fmd->fimc_is->isp;
 871        struct media_entity *source, *sink;
 872        int i, ret;
 873
 874        source = &isp->subdev.entity;
 875
 876        for (i = 0; i < FIMC_MAX_DEVS; i++) {
 877                if (fmd->fimc[i] == NULL)
 878                        continue;
 879
 880                /* Link from FIMC-IS-ISP subdev to FIMC */
 881                sink = &fmd->fimc[i]->vid_cap.subdev.entity;
 882                ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
 883                                               sink, FIMC_SD_PAD_SINK_FIFO, 0);
 884                if (ret)
 885                        return ret;
 886        }
 887
 888        /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
 889        sink = &isp->video_capture.ve.vdev.entity;
 890
 891        /* Skip this link if the fimc-is-isp video node driver isn't built-in */
 892        if (sink->num_pads == 0)
 893                return 0;
 894
 895        return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
 896                                        sink, 0, 0);
 897}
 898
 899/**
 900 * fimc_md_create_links - create default links between registered entities
 901 *
 902 * Parallel interface sensor entities are connected directly to FIMC capture
 903 * entities. The sensors using MIPI CSIS bus are connected through immutable
 904 * link with CSI receiver entity specified by mux_id. Any registered CSIS
 905 * entity has a link to each registered FIMC capture entity. Enabled links
 906 * are created by default between each subsequent registered sensor and
 907 * subsequent FIMC capture entity. The number of default active links is
 908 * determined by the number of available sensors or FIMC entities,
 909 * whichever is less.
 910 */
 911static int fimc_md_create_links(struct fimc_md *fmd)
 912{
 913        struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
 914        struct v4l2_subdev *sensor, *csis;
 915        struct fimc_source_info *pdata;
 916        struct media_entity *source, *sink;
 917        int i, pad, fimc_id = 0, ret = 0;
 918        u32 flags, link_mask = 0;
 919
 920        for (i = 0; i < fmd->num_sensors; i++) {
 921                if (fmd->sensor[i].subdev == NULL)
 922                        continue;
 923
 924                sensor = fmd->sensor[i].subdev;
 925                pdata = v4l2_get_subdev_hostdata(sensor);
 926                if (!pdata)
 927                        continue;
 928
 929                source = NULL;
 930
 931                switch (pdata->sensor_bus_type) {
 932                case FIMC_BUS_TYPE_MIPI_CSI2:
 933                        if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
 934                                "Wrong CSI channel id: %d\n", pdata->mux_id))
 935                                return -EINVAL;
 936
 937                        csis = fmd->csis[pdata->mux_id].sd;
 938                        if (WARN(csis == NULL,
 939                                 "MIPI-CSI interface specified "
 940                                 "but s5p-csis module is not loaded!\n"))
 941                                return -EINVAL;
 942
 943                        pad = sensor->entity.num_pads - 1;
 944                        ret = media_create_pad_link(&sensor->entity, pad,
 945                                              &csis->entity, CSIS_PAD_SINK,
 946                                              MEDIA_LNK_FL_IMMUTABLE |
 947                                              MEDIA_LNK_FL_ENABLED);
 948                        if (ret)
 949                                return ret;
 950
 951                        v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
 952                                  sensor->entity.name, csis->entity.name);
 953
 954                        source = NULL;
 955                        csi_sensors[pdata->mux_id] = sensor;
 956                        break;
 957
 958                case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
 959                        source = &sensor->entity;
 960                        pad = 0;
 961                        break;
 962
 963                default:
 964                        v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
 965                                 pdata->sensor_bus_type);
 966                        return -EINVAL;
 967                }
 968                if (source == NULL)
 969                        continue;
 970
 971                link_mask = 1 << fimc_id++;
 972                ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
 973                                                       pad, link_mask);
 974        }
 975
 976        for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
 977                if (fmd->csis[i].sd == NULL)
 978                        continue;
 979
 980                source = &fmd->csis[i].sd->entity;
 981                pad = CSIS_PAD_SOURCE;
 982                sensor = csi_sensors[i];
 983
 984                link_mask = 1 << fimc_id++;
 985                ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
 986                                                       pad, link_mask);
 987        }
 988
 989        /* Create immutable links between each FIMC's subdev and video node */
 990        flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
 991        for (i = 0; i < FIMC_MAX_DEVS; i++) {
 992                if (!fmd->fimc[i])
 993                        continue;
 994
 995                source = &fmd->fimc[i]->vid_cap.subdev.entity;
 996                sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
 997
 998                ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
 999                                              sink, 0, flags);
1000                if (ret)
1001                        break;
1002        }
1003
1004        ret = __fimc_md_create_flite_source_links(fmd);
1005        if (ret < 0)
1006                return ret;
1007
1008        if (fmd->use_isp)
1009                ret = __fimc_md_create_fimc_is_links(fmd);
1010
1011        return ret;
1012}
1013
1014/*
1015 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1016 */
1017static void fimc_md_put_clocks(struct fimc_md *fmd)
1018{
1019        int i = FIMC_MAX_CAMCLKS;
1020
1021        while (--i >= 0) {
1022                if (IS_ERR(fmd->camclk[i].clock))
1023                        continue;
1024                clk_put(fmd->camclk[i].clock);
1025                fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1026        }
1027
1028        /* Writeback (PIXELASYNCMx) clocks */
1029        for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1030                if (IS_ERR(fmd->wbclk[i]))
1031                        continue;
1032                clk_put(fmd->wbclk[i]);
1033                fmd->wbclk[i] = ERR_PTR(-EINVAL);
1034        }
1035}
1036
1037static int fimc_md_get_clocks(struct fimc_md *fmd)
1038{
1039        struct device *dev = &fmd->pdev->dev;
1040        char clk_name[32];
1041        struct clk *clock;
1042        int i, ret = 0;
1043
1044        for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1045                fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1046
1047        for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1048                snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1049                clock = clk_get(dev, clk_name);
1050
1051                if (IS_ERR(clock)) {
1052                        dev_err(dev, "Failed to get clock: %s\n", clk_name);
1053                        ret = PTR_ERR(clock);
1054                        break;
1055                }
1056                fmd->camclk[i].clock = clock;
1057        }
1058        if (ret)
1059                fimc_md_put_clocks(fmd);
1060
1061        if (!fmd->use_isp)
1062                return 0;
1063        /*
1064         * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1065         * leave PIXELASYNCM0 out for the LCD Writeback driver.
1066         */
1067        fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1068
1069        for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1070                snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1071                clock = clk_get(dev, clk_name);
1072                if (IS_ERR(clock)) {
1073                        v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1074                                  clk_name);
1075                        ret = PTR_ERR(clock);
1076                        break;
1077                }
1078                fmd->wbclk[i] = clock;
1079        }
1080        if (ret)
1081                fimc_md_put_clocks(fmd);
1082
1083        return ret;
1084}
1085
1086static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1087{
1088        struct exynos_video_entity *ve;
1089        struct fimc_pipeline *p;
1090        struct video_device *vdev;
1091        int ret;
1092
1093        vdev = media_entity_to_video_device(entity);
1094        if (vdev->entity.use_count == 0)
1095                return 0;
1096
1097        ve = vdev_to_exynos_video_entity(vdev);
1098        p = to_fimc_pipeline(ve->pipe);
1099        /*
1100         * Nothing to do if we are disabling the pipeline, some link
1101         * has been disconnected and p->subdevs array is cleared now.
1102         */
1103        if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1104                return 0;
1105
1106        if (enable)
1107                ret = __fimc_pipeline_open(ve->pipe, entity, true);
1108        else
1109                ret = __fimc_pipeline_close(ve->pipe);
1110
1111        if (ret == 0 && !enable)
1112                memset(p->subdevs, 0, sizeof(p->subdevs));
1113
1114        return ret;
1115}
1116
1117/* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1118static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1119                                      struct media_entity_graph *graph)
1120{
1121        struct media_entity *entity_err = entity;
1122        int ret;
1123
1124        /*
1125         * Walk current graph and call the pipeline open/close routine for each
1126         * opened video node that belongs to the graph of entities connected
1127         * through active links. This is needed as we cannot power on/off the
1128         * subdevs in random order.
1129         */
1130        media_entity_graph_walk_start(graph, entity);
1131
1132        while ((entity = media_entity_graph_walk_next(graph))) {
1133                if (!is_media_entity_v4l2_io(entity))
1134                        continue;
1135
1136                ret  = __fimc_md_modify_pipeline(entity, enable);
1137
1138                if (ret < 0)
1139                        goto err;
1140        }
1141
1142        return 0;
1143
1144err:
1145        media_entity_graph_walk_start(graph, entity_err);
1146
1147        while ((entity_err = media_entity_graph_walk_next(graph))) {
1148                if (!is_media_entity_v4l2_io(entity_err))
1149                        continue;
1150
1151                __fimc_md_modify_pipeline(entity_err, !enable);
1152
1153                if (entity_err == entity)
1154                        break;
1155        }
1156
1157        return ret;
1158}
1159
1160static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1161                                unsigned int notification)
1162{
1163        struct media_entity_graph *graph =
1164                &container_of(link->graph_obj.mdev, struct fimc_md,
1165                              media_dev)->link_setup_graph;
1166        struct media_entity *sink = link->sink->entity;
1167        int ret = 0;
1168
1169        /* Before link disconnection */
1170        if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1171                ret = media_entity_graph_walk_init(graph,
1172                                                   link->graph_obj.mdev);
1173                if (ret)
1174                        return ret;
1175                if (!(flags & MEDIA_LNK_FL_ENABLED))
1176                        ret = __fimc_md_modify_pipelines(sink, false, graph);
1177#if 0
1178                else
1179                        /* TODO: Link state change validation */
1180#endif
1181        /* After link activation */
1182        } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1183                if (link->flags & MEDIA_LNK_FL_ENABLED)
1184                        ret = __fimc_md_modify_pipelines(sink, true, graph);
1185                media_entity_graph_walk_cleanup(graph);
1186        }
1187
1188        return ret ? -EPIPE : 0;
1189}
1190
1191static ssize_t fimc_md_sysfs_show(struct device *dev,
1192                                  struct device_attribute *attr, char *buf)
1193{
1194        struct platform_device *pdev = to_platform_device(dev);
1195        struct fimc_md *fmd = platform_get_drvdata(pdev);
1196
1197        if (fmd->user_subdev_api)
1198                return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1199
1200        return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1201}
1202
1203static ssize_t fimc_md_sysfs_store(struct device *dev,
1204                                   struct device_attribute *attr,
1205                                   const char *buf, size_t count)
1206{
1207        struct platform_device *pdev = to_platform_device(dev);
1208        struct fimc_md *fmd = platform_get_drvdata(pdev);
1209        bool subdev_api;
1210        int i;
1211
1212        if (!strcmp(buf, "vid-dev\n"))
1213                subdev_api = false;
1214        else if (!strcmp(buf, "sub-dev\n"))
1215                subdev_api = true;
1216        else
1217                return count;
1218
1219        fmd->user_subdev_api = subdev_api;
1220        for (i = 0; i < FIMC_MAX_DEVS; i++)
1221                if (fmd->fimc[i])
1222                        fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1223        return count;
1224}
1225/*
1226 * This device attribute is to select video pipeline configuration method.
1227 * There are following valid values:
1228 *  vid-dev - for V4L2 video node API only, subdevice will be configured
1229 *  by the host driver.
1230 *  sub-dev - for media controller API, subdevs must be configured in user
1231 *  space before starting streaming.
1232 */
1233static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1234                   fimc_md_sysfs_show, fimc_md_sysfs_store);
1235
1236static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1237{
1238        struct device *dev = &fmd->pdev->dev;
1239        struct fimc_pinctrl *pctl = &fmd->pinctl;
1240
1241        pctl->pinctrl = devm_pinctrl_get(dev);
1242        if (IS_ERR(pctl->pinctrl))
1243                return PTR_ERR(pctl->pinctrl);
1244
1245        pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1246                                        PINCTRL_STATE_DEFAULT);
1247        if (IS_ERR(pctl->state_default))
1248                return PTR_ERR(pctl->state_default);
1249
1250        pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1251                                        PINCTRL_STATE_IDLE);
1252        return 0;
1253}
1254
1255static int cam_clk_prepare(struct clk_hw *hw)
1256{
1257        struct cam_clk *camclk = to_cam_clk(hw);
1258        int ret;
1259
1260        if (camclk->fmd->pmf == NULL)
1261                return -ENODEV;
1262
1263        ret = pm_runtime_get_sync(camclk->fmd->pmf);
1264        return ret < 0 ? ret : 0;
1265}
1266
1267static void cam_clk_unprepare(struct clk_hw *hw)
1268{
1269        struct cam_clk *camclk = to_cam_clk(hw);
1270
1271        if (camclk->fmd->pmf == NULL)
1272                return;
1273
1274        pm_runtime_put_sync(camclk->fmd->pmf);
1275}
1276
1277static const struct clk_ops cam_clk_ops = {
1278        .prepare = cam_clk_prepare,
1279        .unprepare = cam_clk_unprepare,
1280};
1281
1282static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1283{
1284        struct cam_clk_provider *cp = &fmd->clk_provider;
1285        unsigned int i;
1286
1287        if (cp->of_node)
1288                of_clk_del_provider(cp->of_node);
1289
1290        for (i = 0; i < cp->num_clocks; i++)
1291                clk_unregister(cp->clks[i]);
1292}
1293
1294static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1295{
1296        struct cam_clk_provider *cp = &fmd->clk_provider;
1297        struct device *dev = &fmd->pdev->dev;
1298        int i, ret;
1299
1300        for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1301                struct cam_clk *camclk = &cp->camclk[i];
1302                struct clk_init_data init;
1303                const char *p_name;
1304
1305                ret = of_property_read_string_index(dev->of_node,
1306                                        "clock-output-names", i, &init.name);
1307                if (ret < 0)
1308                        break;
1309
1310                p_name = __clk_get_name(fmd->camclk[i].clock);
1311
1312                /* It's safe since clk_register() will duplicate the string. */
1313                init.parent_names = &p_name;
1314                init.num_parents = 1;
1315                init.ops = &cam_clk_ops;
1316                init.flags = CLK_SET_RATE_PARENT;
1317                camclk->hw.init = &init;
1318                camclk->fmd = fmd;
1319
1320                cp->clks[i] = clk_register(NULL, &camclk->hw);
1321                if (IS_ERR(cp->clks[i])) {
1322                        dev_err(dev, "failed to register clock: %s (%ld)\n",
1323                                        init.name, PTR_ERR(cp->clks[i]));
1324                        ret = PTR_ERR(cp->clks[i]);
1325                        goto err;
1326                }
1327                cp->num_clocks++;
1328        }
1329
1330        if (cp->num_clocks == 0) {
1331                dev_warn(dev, "clk provider not registered\n");
1332                return 0;
1333        }
1334
1335        cp->clk_data.clks = cp->clks;
1336        cp->clk_data.clk_num = cp->num_clocks;
1337        cp->of_node = dev->of_node;
1338        ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1339                                  &cp->clk_data);
1340        if (ret == 0)
1341                return 0;
1342err:
1343        fimc_md_unregister_clk_provider(fmd);
1344        return ret;
1345}
1346
1347static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1348                                 struct v4l2_subdev *subdev,
1349                                 struct v4l2_async_subdev *asd)
1350{
1351        struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1352        struct fimc_sensor_info *si = NULL;
1353        int i;
1354
1355        /* Find platform data for this sensor subdev */
1356        for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1357                if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1358                        si = &fmd->sensor[i];
1359
1360        if (si == NULL)
1361                return -EINVAL;
1362
1363        v4l2_set_subdev_hostdata(subdev, &si->pdata);
1364
1365        if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1366                subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1367        else
1368                subdev->grp_id = GRP_ID_SENSOR;
1369
1370        si->subdev = subdev;
1371
1372        v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1373                  subdev->name, fmd->num_sensors);
1374
1375        fmd->num_sensors++;
1376
1377        return 0;
1378}
1379
1380static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1381{
1382        struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1383        int ret;
1384
1385        mutex_lock(&fmd->media_dev.graph_mutex);
1386
1387        ret = fimc_md_create_links(fmd);
1388        if (ret < 0)
1389                goto unlock;
1390
1391        ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1392unlock:
1393        mutex_unlock(&fmd->media_dev.graph_mutex);
1394        if (ret < 0)
1395                return ret;
1396
1397        return media_device_register(&fmd->media_dev);
1398}
1399
1400static int fimc_md_probe(struct platform_device *pdev)
1401{
1402        struct device *dev = &pdev->dev;
1403        struct v4l2_device *v4l2_dev;
1404        struct fimc_md *fmd;
1405        int ret;
1406
1407        fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1408        if (!fmd)
1409                return -ENOMEM;
1410
1411        spin_lock_init(&fmd->slock);
1412        INIT_LIST_HEAD(&fmd->pipelines);
1413        fmd->pdev = pdev;
1414
1415        strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1416                sizeof(fmd->media_dev.model));
1417        fmd->media_dev.link_notify = fimc_md_link_notify;
1418        fmd->media_dev.dev = dev;
1419
1420        v4l2_dev = &fmd->v4l2_dev;
1421        v4l2_dev->mdev = &fmd->media_dev;
1422        v4l2_dev->notify = fimc_sensor_notify;
1423        strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1424
1425        fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1426        fmd->user_subdev_api = true;
1427
1428        media_device_init(&fmd->media_dev);
1429
1430        ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1431        if (ret < 0) {
1432                v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1433                return ret;
1434        }
1435
1436        ret = fimc_md_get_clocks(fmd);
1437        if (ret)
1438                goto err_md;
1439
1440        ret = fimc_md_get_pinctrl(fmd);
1441        if (ret < 0) {
1442                if (ret != EPROBE_DEFER)
1443                        dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1444                goto err_clk;
1445        }
1446
1447        platform_set_drvdata(pdev, fmd);
1448
1449        ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1450        if (ret)
1451                goto err_clk;
1452
1453        ret = fimc_md_register_sensor_entities(fmd);
1454        if (ret)
1455                goto err_m_ent;
1456
1457        ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1458        if (ret)
1459                goto err_m_ent;
1460        /*
1461         * FIMC platform devices need to be registered before the sclk_cam
1462         * clocks provider, as one of these devices needs to be activated
1463         * to enable the clock.
1464         */
1465        ret = fimc_md_register_clk_provider(fmd);
1466        if (ret < 0) {
1467                v4l2_err(v4l2_dev, "clock provider registration failed\n");
1468                goto err_attr;
1469        }
1470
1471        if (fmd->num_sensors > 0) {
1472                fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1473                fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1474                fmd->subdev_notifier.bound = subdev_notifier_bound;
1475                fmd->subdev_notifier.complete = subdev_notifier_complete;
1476                fmd->num_sensors = 0;
1477
1478                ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1479                                                &fmd->subdev_notifier);
1480                if (ret)
1481                        goto err_clk_p;
1482        }
1483
1484        return 0;
1485
1486err_clk_p:
1487        fimc_md_unregister_clk_provider(fmd);
1488err_attr:
1489        device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1490err_clk:
1491        fimc_md_put_clocks(fmd);
1492err_m_ent:
1493        fimc_md_unregister_entities(fmd);
1494err_md:
1495        media_device_cleanup(&fmd->media_dev);
1496        v4l2_device_unregister(&fmd->v4l2_dev);
1497        return ret;
1498}
1499
1500static int fimc_md_remove(struct platform_device *pdev)
1501{
1502        struct fimc_md *fmd = platform_get_drvdata(pdev);
1503
1504        if (!fmd)
1505                return 0;
1506
1507        fimc_md_unregister_clk_provider(fmd);
1508        v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1509
1510        v4l2_device_unregister(&fmd->v4l2_dev);
1511        device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1512        fimc_md_unregister_entities(fmd);
1513        fimc_md_pipelines_free(fmd);
1514        media_device_unregister(&fmd->media_dev);
1515        media_device_cleanup(&fmd->media_dev);
1516        fimc_md_put_clocks(fmd);
1517
1518        return 0;
1519}
1520
1521static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1522        { .name = "s5p-fimc-md" },
1523        { },
1524};
1525MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1526
1527static const struct of_device_id fimc_md_of_match[] = {
1528        { .compatible = "samsung,fimc" },
1529        { },
1530};
1531MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1532
1533static struct platform_driver fimc_md_driver = {
1534        .probe          = fimc_md_probe,
1535        .remove         = fimc_md_remove,
1536        .driver = {
1537                .of_match_table = of_match_ptr(fimc_md_of_match),
1538                .name           = "s5p-fimc-md",
1539        }
1540};
1541
1542static int __init fimc_md_init(void)
1543{
1544        int ret;
1545
1546        request_module("s5p-csis");
1547        ret = fimc_register_driver();
1548        if (ret)
1549                return ret;
1550
1551        return platform_driver_register(&fimc_md_driver);
1552}
1553
1554static void __exit fimc_md_exit(void)
1555{
1556        platform_driver_unregister(&fimc_md_driver);
1557        fimc_unregister_driver();
1558}
1559
1560module_init(fimc_md_init);
1561module_exit(fimc_md_exit);
1562
1563MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1564MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1565MODULE_LICENSE("GPL");
1566MODULE_VERSION("2.0.1");
1567