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