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