Merge branch 'samsung/exynos5' into next/soc2
[linux-2.6.git] / drivers / media / video / s5p-fimc / fimc-mdevice.c
1 /*
2  * S5P/EXYNOS4 SoC series camera host interface media device driver
3  *
4  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5  * Contact: 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/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/types.h>
23 #include <linux/slab.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/media-device.h>
26
27 #include "fimc-core.h"
28 #include "fimc-mdevice.h"
29 #include "mipi-csis.h"
30
31 static int __fimc_md_set_camclk(struct fimc_md *fmd,
32                                 struct fimc_sensor_info *s_info,
33                                 bool on);
34 /**
35  * fimc_pipeline_prepare - update pipeline information with subdevice pointers
36  * @fimc: fimc device terminating the pipeline
37  *
38  * Caller holds the graph mutex.
39  */
40 void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me)
41 {
42         struct media_entity_graph graph;
43         struct v4l2_subdev *sd;
44
45         media_entity_graph_walk_start(&graph, me);
46
47         while ((me = media_entity_graph_walk_next(&graph))) {
48                 if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV)
49                         continue;
50                 sd = media_entity_to_v4l2_subdev(me);
51
52                 if (sd->grp_id == SENSOR_GROUP_ID)
53                         fimc->pipeline.sensor = sd;
54                 else if (sd->grp_id == CSIS_GROUP_ID)
55                         fimc->pipeline.csis = sd;
56         }
57 }
58
59 /**
60  * __subdev_set_power - change power state of a single subdev
61  * @sd: subdevice to change power state for
62  * @on: 1 to enable power or 0 to disable
63  *
64  * Return result of s_power subdev operation or -ENXIO if sd argument
65  * is NULL. Return 0 if the subdevice does not implement s_power.
66  */
67 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
68 {
69         int *use_count;
70         int ret;
71
72         if (sd == NULL)
73                 return -ENXIO;
74
75         use_count = &sd->entity.use_count;
76         if (on && (*use_count)++ > 0)
77                 return 0;
78         else if (!on && (*use_count == 0 || --(*use_count) > 0))
79                 return 0;
80         ret = v4l2_subdev_call(sd, core, s_power, on);
81
82         return ret != -ENOIOCTLCMD ? ret : 0;
83 }
84
85 /**
86  * fimc_pipeline_s_power - change power state of all pipeline subdevs
87  * @fimc: fimc device terminating the pipeline
88  * @state: 1 to enable power or 0 for power down
89  *
90  * Need to be called with the graph mutex held.
91  */
92 int fimc_pipeline_s_power(struct fimc_dev *fimc, int state)
93 {
94         int ret = 0;
95
96         if (fimc->pipeline.sensor == NULL)
97                 return -ENXIO;
98
99         if (state) {
100                 ret = __subdev_set_power(fimc->pipeline.csis, 1);
101                 if (ret && ret != -ENXIO)
102                         return ret;
103                 return __subdev_set_power(fimc->pipeline.sensor, 1);
104         }
105
106         ret = __subdev_set_power(fimc->pipeline.sensor, 0);
107         if (ret)
108                 return ret;
109         ret = __subdev_set_power(fimc->pipeline.csis, 0);
110
111         return ret == -ENXIO ? 0 : ret;
112 }
113
114 /**
115  * __fimc_pipeline_initialize - update the pipeline information, enable power
116  *                              of all pipeline subdevs and the sensor clock
117  * @me: media entity to start graph walk with
118  * @prep: true to acquire sensor (and csis) subdevs
119  *
120  * This function must be called with the graph mutex held.
121  */
122 static int __fimc_pipeline_initialize(struct fimc_dev *fimc,
123                                       struct media_entity *me, bool prep)
124 {
125         int ret;
126
127         if (prep)
128                 fimc_pipeline_prepare(fimc, me);
129         if (fimc->pipeline.sensor == NULL)
130                 return -EINVAL;
131         ret = fimc_md_set_camclk(fimc->pipeline.sensor, true);
132         if (ret)
133                 return ret;
134         return fimc_pipeline_s_power(fimc, 1);
135 }
136
137 int fimc_pipeline_initialize(struct fimc_dev *fimc, struct media_entity *me,
138                              bool prep)
139 {
140         int ret;
141
142         mutex_lock(&me->parent->graph_mutex);
143         ret =  __fimc_pipeline_initialize(fimc, me, prep);
144         mutex_unlock(&me->parent->graph_mutex);
145
146         return ret;
147 }
148
149 /**
150  * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power
151  * @fimc: fimc device terminating the pipeline
152  *
153  * Disable power of all subdevs in the pipeline and turn off the external
154  * sensor clock.
155  * Called with the graph mutex held.
156  */
157 int __fimc_pipeline_shutdown(struct fimc_dev *fimc)
158 {
159         int ret = 0;
160
161         if (fimc->pipeline.sensor) {
162                 ret = fimc_pipeline_s_power(fimc, 0);
163                 fimc_md_set_camclk(fimc->pipeline.sensor, false);
164         }
165         return ret == -ENXIO ? 0 : ret;
166 }
167
168 int fimc_pipeline_shutdown(struct fimc_dev *fimc)
169 {
170         struct media_entity *me = &fimc->vid_cap.vfd->entity;
171         int ret;
172
173         mutex_lock(&me->parent->graph_mutex);
174         ret = __fimc_pipeline_shutdown(fimc);
175         mutex_unlock(&me->parent->graph_mutex);
176
177         return ret;
178 }
179
180 /**
181  * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
182  * @fimc: fimc device terminating the pipeline
183  * @on: passed as the s_stream call argument
184  */
185 int fimc_pipeline_s_stream(struct fimc_dev *fimc, int on)
186 {
187         struct fimc_pipeline *p = &fimc->pipeline;
188         int ret = 0;
189
190         if (p->sensor == NULL)
191                 return -ENODEV;
192
193         if ((on && p->csis) || !on)
194                 ret = v4l2_subdev_call(on ? p->csis : p->sensor,
195                                        video, s_stream, on);
196         if (ret < 0 && ret != -ENOIOCTLCMD)
197                 return ret;
198         if ((!on && p->csis) || on)
199                 ret = v4l2_subdev_call(on ? p->sensor : p->csis,
200                                        video, s_stream, on);
201         return ret == -ENOIOCTLCMD ? 0 : ret;
202 }
203
204 /*
205  * Sensor subdevice helper functions
206  */
207 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
208                                    struct fimc_sensor_info *s_info)
209 {
210         struct i2c_adapter *adapter;
211         struct v4l2_subdev *sd = NULL;
212
213         if (!s_info || !fmd)
214                 return NULL;
215
216         adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num);
217         if (!adapter)
218                 return NULL;
219         sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
220                                        s_info->pdata->board_info, NULL);
221         if (IS_ERR_OR_NULL(sd)) {
222                 i2c_put_adapter(adapter);
223                 v4l2_err(&fmd->v4l2_dev, "Failed to acquire subdev\n");
224                 return NULL;
225         }
226         v4l2_set_subdev_hostdata(sd, s_info);
227         sd->grp_id = SENSOR_GROUP_ID;
228
229         v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
230                   s_info->pdata->board_info->type);
231         return sd;
232 }
233
234 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
235 {
236         struct i2c_client *client = v4l2_get_subdevdata(sd);
237         struct i2c_adapter *adapter;
238
239         if (!client)
240                 return;
241         v4l2_device_unregister_subdev(sd);
242         adapter = client->adapter;
243         i2c_unregister_device(client);
244         if (adapter)
245                 i2c_put_adapter(adapter);
246 }
247
248 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
249 {
250         struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
251         struct fimc_dev *fd = NULL;
252         int num_clients, ret, i;
253
254         /*
255          * Runtime resume one of the FIMC entities to make sure
256          * the sclk_cam clocks are not globally disabled.
257          */
258         for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
259                 if (fmd->fimc[i])
260                         fd = fmd->fimc[i];
261         if (!fd)
262                 return -ENXIO;
263         ret = pm_runtime_get_sync(&fd->pdev->dev);
264         if (ret < 0)
265                 return ret;
266
267         WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
268         num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
269
270         fmd->num_sensors = num_clients;
271         for (i = 0; i < num_clients; i++) {
272                 fmd->sensor[i].pdata = &pdata->isp_info[i];
273                 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
274                 if (ret)
275                         break;
276                 fmd->sensor[i].subdev =
277                         fimc_md_register_sensor(fmd, &fmd->sensor[i]);
278                 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
279                 if (ret)
280                         break;
281         }
282         pm_runtime_put(&fd->pdev->dev);
283         return ret;
284 }
285
286 /*
287  * MIPI CSIS and FIMC platform devices registration.
288  */
289 static int fimc_register_callback(struct device *dev, void *p)
290 {
291         struct fimc_dev *fimc = dev_get_drvdata(dev);
292         struct fimc_md *fmd = p;
293         int ret;
294
295         if (!fimc || !fimc->pdev)
296                 return 0;
297         if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
298                 return 0;
299
300         fmd->fimc[fimc->pdev->id] = fimc;
301         ret = fimc_register_m2m_device(fimc, &fmd->v4l2_dev);
302         if (ret)
303                 return ret;
304         ret = fimc_register_capture_device(fimc, &fmd->v4l2_dev);
305         if (!ret)
306                 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
307         return ret;
308 }
309
310 static int csis_register_callback(struct device *dev, void *p)
311 {
312         struct v4l2_subdev *sd = dev_get_drvdata(dev);
313         struct platform_device *pdev;
314         struct fimc_md *fmd = p;
315         int id, ret;
316
317         if (!sd)
318                 return 0;
319         pdev = v4l2_get_subdevdata(sd);
320         if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
321                 return 0;
322         v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);
323
324         id = pdev->id < 0 ? 0 : pdev->id;
325         fmd->csis[id].sd = sd;
326         sd->grp_id = CSIS_GROUP_ID;
327         ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
328         if (ret)
329                 v4l2_err(&fmd->v4l2_dev,
330                          "Failed to register CSIS subdevice: %d\n", ret);
331         return ret;
332 }
333
334 /**
335  * fimc_md_register_platform_entities - register FIMC and CSIS media entities
336  */
337 static int fimc_md_register_platform_entities(struct fimc_md *fmd)
338 {
339         struct device_driver *driver;
340         int ret;
341
342         driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
343         if (!driver)
344                 return -ENODEV;
345         ret = driver_for_each_device(driver, NULL, fmd,
346                                      fimc_register_callback);
347         if (ret)
348                 return ret;
349
350         driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
351         if (driver)
352                 ret = driver_for_each_device(driver, NULL, fmd,
353                                              csis_register_callback);
354         return ret;
355 }
356
357 static void fimc_md_unregister_entities(struct fimc_md *fmd)
358 {
359         int i;
360
361         for (i = 0; i < FIMC_MAX_DEVS; i++) {
362                 if (fmd->fimc[i] == NULL)
363                         continue;
364                 fimc_unregister_m2m_device(fmd->fimc[i]);
365                 fimc_unregister_capture_device(fmd->fimc[i]);
366                 fmd->fimc[i] = NULL;
367         }
368         for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
369                 if (fmd->csis[i].sd == NULL)
370                         continue;
371                 v4l2_device_unregister_subdev(fmd->csis[i].sd);
372                 fmd->csis[i].sd = NULL;
373         }
374         for (i = 0; i < fmd->num_sensors; i++) {
375                 if (fmd->sensor[i].subdev == NULL)
376                         continue;
377                 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
378                 fmd->sensor[i].subdev = NULL;
379         }
380 }
381
382 static int fimc_md_register_video_nodes(struct fimc_md *fmd)
383 {
384         struct video_device *vdev;
385         int i, ret = 0;
386
387         for (i = 0; i < FIMC_MAX_DEVS && !ret; i++) {
388                 if (!fmd->fimc[i])
389                         continue;
390
391                 vdev = fmd->fimc[i]->m2m.vfd;
392                 if (vdev) {
393                         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
394                         if (ret)
395                                 break;
396                         v4l2_info(&fmd->v4l2_dev, "Registered %s as /dev/%s\n",
397                                   vdev->name, video_device_node_name(vdev));
398                 }
399
400                 vdev = fmd->fimc[i]->vid_cap.vfd;
401                 if (vdev == NULL)
402                         continue;
403                 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
404                 v4l2_info(&fmd->v4l2_dev, "Registered %s as /dev/%s\n",
405                           vdev->name, video_device_node_name(vdev));
406         }
407
408         return ret;
409 }
410
411 /**
412  * __fimc_md_create_fimc_links - create links to all FIMC entities
413  * @fmd: fimc media device
414  * @source: the source entity to create links to all fimc entities from
415  * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
416  * @pad: the source entity pad index
417  * @fimc_id: index of the fimc device for which link should be enabled
418  */
419 static int __fimc_md_create_fimc_links(struct fimc_md *fmd,
420                                        struct media_entity *source,
421                                        struct v4l2_subdev *sensor,
422                                        int pad, int fimc_id)
423 {
424         struct fimc_sensor_info *s_info;
425         struct media_entity *sink;
426         unsigned int flags;
427         int ret, i;
428
429         for (i = 0; i < FIMC_MAX_DEVS; i++) {
430                 if (!fmd->fimc[i])
431                         break;
432                 /*
433                  * Some FIMC variants are not fitted with camera capture
434                  * interface. Skip creating a link from sensor for those.
435                  */
436                 if (sensor->grp_id == SENSOR_GROUP_ID &&
437                     !fmd->fimc[i]->variant->has_cam_if)
438                         continue;
439
440                 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
441                 sink = &fmd->fimc[i]->vid_cap.subdev->entity;
442                 ret = media_entity_create_link(source, pad, sink,
443                                               FIMC_SD_PAD_SINK, flags);
444                 if (ret)
445                         return ret;
446
447                 /* Notify FIMC capture subdev entity */
448                 ret = media_entity_call(sink, link_setup, &sink->pads[0],
449                                         &source->pads[pad], flags);
450                 if (ret)
451                         break;
452
453                 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
454                           source->name, flags ? '=' : '-', sink->name);
455
456                 if (flags == 0)
457                         continue;
458                 s_info = v4l2_get_subdev_hostdata(sensor);
459                 if (!WARN_ON(s_info == NULL)) {
460                         unsigned long irq_flags;
461                         spin_lock_irqsave(&fmd->slock, irq_flags);
462                         s_info->host = fmd->fimc[i];
463                         spin_unlock_irqrestore(&fmd->slock, irq_flags);
464                 }
465         }
466         return 0;
467 }
468
469 /**
470  * fimc_md_create_links - create default links between registered entities
471  *
472  * Parallel interface sensor entities are connected directly to FIMC capture
473  * entities. The sensors using MIPI CSIS bus are connected through immutable
474  * link with CSI receiver entity specified by mux_id. Any registered CSIS
475  * entity has a link to each registered FIMC capture entity. Enabled links
476  * are created by default between each subsequent registered sensor and
477  * subsequent FIMC capture entity. The number of default active links is
478  * determined by the number of available sensors or FIMC entities,
479  * whichever is less.
480  */
481 static int fimc_md_create_links(struct fimc_md *fmd)
482 {
483         struct v4l2_subdev *sensor, *csis;
484         struct s5p_fimc_isp_info *pdata;
485         struct fimc_sensor_info *s_info;
486         struct media_entity *source, *sink;
487         int i, pad, fimc_id = 0;
488         int ret = 0;
489         u32 flags;
490
491         for (i = 0; i < fmd->num_sensors; i++) {
492                 if (fmd->sensor[i].subdev == NULL)
493                         continue;
494
495                 sensor = fmd->sensor[i].subdev;
496                 s_info = v4l2_get_subdev_hostdata(sensor);
497                 if (!s_info || !s_info->pdata)
498                         continue;
499
500                 source = NULL;
501                 pdata = s_info->pdata;
502
503                 switch (pdata->bus_type) {
504                 case FIMC_MIPI_CSI2:
505                         if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
506                                 "Wrong CSI channel id: %d\n", pdata->mux_id))
507                                 return -EINVAL;
508
509                         csis = fmd->csis[pdata->mux_id].sd;
510                         if (WARN(csis == NULL,
511                                  "MIPI-CSI interface specified "
512                                  "but s5p-csis module is not loaded!\n"))
513                                 return -EINVAL;
514
515                         ret = media_entity_create_link(&sensor->entity, 0,
516                                               &csis->entity, CSIS_PAD_SINK,
517                                               MEDIA_LNK_FL_IMMUTABLE |
518                                               MEDIA_LNK_FL_ENABLED);
519                         if (ret)
520                                 return ret;
521
522                         v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
523                                   sensor->entity.name, csis->entity.name);
524
525                         source = &csis->entity;
526                         pad = CSIS_PAD_SOURCE;
527                         break;
528
529                 case FIMC_ITU_601...FIMC_ITU_656:
530                         source = &sensor->entity;
531                         pad = 0;
532                         break;
533
534                 default:
535                         v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
536                                  pdata->bus_type);
537                         return -EINVAL;
538                 }
539                 if (source == NULL)
540                         continue;
541
542                 ret = __fimc_md_create_fimc_links(fmd, source, sensor, pad,
543                                                   fimc_id++);
544         }
545         /* Create immutable links between each FIMC's subdev and video node */
546         flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
547         for (i = 0; i < FIMC_MAX_DEVS; i++) {
548                 if (!fmd->fimc[i])
549                         continue;
550                 source = &fmd->fimc[i]->vid_cap.subdev->entity;
551                 sink = &fmd->fimc[i]->vid_cap.vfd->entity;
552                 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
553                                               sink, 0, flags);
554                 if (ret)
555                         break;
556         }
557
558         return ret;
559 }
560
561 /*
562  * The peripheral sensor clock management.
563  */
564 static int fimc_md_get_clocks(struct fimc_md *fmd)
565 {
566         char clk_name[32];
567         struct clk *clock;
568         int i;
569
570         for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
571                 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
572                 clock = clk_get(NULL, clk_name);
573                 if (IS_ERR_OR_NULL(clock)) {
574                         v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
575                                   clk_name);
576                         return -ENXIO;
577                 }
578                 fmd->camclk[i].clock = clock;
579         }
580         return 0;
581 }
582
583 static void fimc_md_put_clocks(struct fimc_md *fmd)
584 {
585         int i = FIMC_MAX_CAMCLKS;
586
587         while (--i >= 0) {
588                 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
589                         continue;
590                 clk_put(fmd->camclk[i].clock);
591                 fmd->camclk[i].clock = NULL;
592         }
593 }
594
595 static int __fimc_md_set_camclk(struct fimc_md *fmd,
596                                          struct fimc_sensor_info *s_info,
597                                          bool on)
598 {
599         struct s5p_fimc_isp_info *pdata = s_info->pdata;
600         struct fimc_camclk_info *camclk;
601         int ret = 0;
602
603         if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
604                 return -EINVAL;
605
606         if (s_info->clk_on == on)
607                 return 0;
608         camclk = &fmd->camclk[pdata->clk_id];
609
610         dbg("camclk %d, f: %lu, clk: %p, on: %d",
611             pdata->clk_id, pdata->clk_frequency, camclk, on);
612
613         if (on) {
614                 if (camclk->use_count > 0 &&
615                     camclk->frequency != pdata->clk_frequency)
616                         return -EINVAL;
617
618                 if (camclk->use_count++ == 0) {
619                         clk_set_rate(camclk->clock, pdata->clk_frequency);
620                         camclk->frequency = pdata->clk_frequency;
621                         ret = clk_enable(camclk->clock);
622                 }
623                 s_info->clk_on = 1;
624                 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
625                     clk_get_rate(camclk->clock));
626
627                 return ret;
628         }
629
630         if (WARN_ON(camclk->use_count == 0))
631                 return 0;
632
633         if (--camclk->use_count == 0) {
634                 clk_disable(camclk->clock);
635                 s_info->clk_on = 0;
636                 dbg("Disabled camclk %d", pdata->clk_id);
637         }
638         return ret;
639 }
640
641 /**
642  * fimc_md_set_camclk - peripheral sensor clock setup
643  * @sd: sensor subdev to configure sclk_cam clock for
644  * @on: 1 to enable or 0 to disable the clock
645  *
646  * There are 2 separate clock outputs available in the SoC for external
647  * image processors. These clocks are shared between all registered FIMC
648  * devices to which sensors can be attached, either directly or through
649  * the MIPI CSI receiver. The clock is allowed here to be used by
650  * multiple sensors concurrently if they use same frequency.
651  * The per sensor subdev clk_on attribute helps to synchronize accesses
652  * to the sclk_cam clocks from the video and media device nodes.
653  * This function should only be called when the graph mutex is held.
654  */
655 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
656 {
657         struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
658         struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
659
660         return __fimc_md_set_camclk(fmd, s_info, on);
661 }
662
663 static int fimc_md_link_notify(struct media_pad *source,
664                                struct media_pad *sink, u32 flags)
665 {
666         struct v4l2_subdev *sd;
667         struct fimc_dev *fimc;
668         int ret = 0;
669
670         if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
671                 return 0;
672
673         sd = media_entity_to_v4l2_subdev(sink->entity);
674         fimc = v4l2_get_subdevdata(sd);
675
676         if (!(flags & MEDIA_LNK_FL_ENABLED)) {
677                 ret = __fimc_pipeline_shutdown(fimc);
678                 fimc->pipeline.sensor = NULL;
679                 fimc->pipeline.csis = NULL;
680
681                 mutex_lock(&fimc->lock);
682                 fimc_ctrls_delete(fimc->vid_cap.ctx);
683                 mutex_unlock(&fimc->lock);
684                 return ret;
685         }
686         /*
687          * Link activation. Enable power of pipeline elements only if the
688          * pipeline is already in use, i.e. its video node is opened.
689          * Recreate the controls destroyed during the link deactivation.
690          */
691         mutex_lock(&fimc->lock);
692         if (fimc->vid_cap.refcnt > 0) {
693                 ret = __fimc_pipeline_initialize(fimc, source->entity, true);
694                 if (!ret)
695                         ret = fimc_capture_ctrls_create(fimc);
696         }
697         mutex_unlock(&fimc->lock);
698
699         return ret ? -EPIPE : ret;
700 }
701
702 static ssize_t fimc_md_sysfs_show(struct device *dev,
703                                   struct device_attribute *attr, char *buf)
704 {
705         struct platform_device *pdev = to_platform_device(dev);
706         struct fimc_md *fmd = platform_get_drvdata(pdev);
707
708         if (fmd->user_subdev_api)
709                 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
710
711         return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
712 }
713
714 static ssize_t fimc_md_sysfs_store(struct device *dev,
715                                    struct device_attribute *attr,
716                                    const char *buf, size_t count)
717 {
718         struct platform_device *pdev = to_platform_device(dev);
719         struct fimc_md *fmd = platform_get_drvdata(pdev);
720         bool subdev_api;
721         int i;
722
723         if (!strcmp(buf, "vid-dev\n"))
724                 subdev_api = false;
725         else if (!strcmp(buf, "sub-dev\n"))
726                 subdev_api = true;
727         else
728                 return count;
729
730         fmd->user_subdev_api = subdev_api;
731         for (i = 0; i < FIMC_MAX_DEVS; i++)
732                 if (fmd->fimc[i])
733                         fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
734         return count;
735 }
736 /*
737  * This device attribute is to select video pipeline configuration method.
738  * There are following valid values:
739  *  vid-dev - for V4L2 video node API only, subdevice will be configured
740  *  by the host driver.
741  *  sub-dev - for media controller API, subdevs must be configured in user
742  *  space before starting streaming.
743  */
744 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
745                    fimc_md_sysfs_show, fimc_md_sysfs_store);
746
747 static int __devinit fimc_md_probe(struct platform_device *pdev)
748 {
749         struct v4l2_device *v4l2_dev;
750         struct fimc_md *fmd;
751         int ret;
752
753         fmd = kzalloc(sizeof(struct fimc_md), GFP_KERNEL);
754         if (!fmd)
755                 return -ENOMEM;
756
757         spin_lock_init(&fmd->slock);
758         fmd->pdev = pdev;
759
760         strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
761                 sizeof(fmd->media_dev.model));
762         fmd->media_dev.link_notify = fimc_md_link_notify;
763         fmd->media_dev.dev = &pdev->dev;
764
765         v4l2_dev = &fmd->v4l2_dev;
766         v4l2_dev->mdev = &fmd->media_dev;
767         v4l2_dev->notify = fimc_sensor_notify;
768         snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
769                  dev_name(&pdev->dev));
770
771         ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
772         if (ret < 0) {
773                 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
774                 goto err1;
775         }
776         ret = media_device_register(&fmd->media_dev);
777         if (ret < 0) {
778                 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
779                 goto err2;
780         }
781         ret = fimc_md_get_clocks(fmd);
782         if (ret)
783                 goto err3;
784
785         fmd->user_subdev_api = false;
786         ret = fimc_md_register_platform_entities(fmd);
787         if (ret)
788                 goto err3;
789
790         if (pdev->dev.platform_data) {
791                 ret = fimc_md_register_sensor_entities(fmd);
792                 if (ret)
793                         goto err3;
794         }
795         ret = fimc_md_create_links(fmd);
796         if (ret)
797                 goto err3;
798         ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
799         if (ret)
800                 goto err3;
801         ret = fimc_md_register_video_nodes(fmd);
802         if (ret)
803                 goto err3;
804
805         ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
806         if (!ret) {
807                 platform_set_drvdata(pdev, fmd);
808                 return 0;
809         }
810 err3:
811         media_device_unregister(&fmd->media_dev);
812         fimc_md_put_clocks(fmd);
813         fimc_md_unregister_entities(fmd);
814 err2:
815         v4l2_device_unregister(&fmd->v4l2_dev);
816 err1:
817         kfree(fmd);
818         return ret;
819 }
820
821 static int __devexit fimc_md_remove(struct platform_device *pdev)
822 {
823         struct fimc_md *fmd = platform_get_drvdata(pdev);
824
825         if (!fmd)
826                 return 0;
827         device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
828         fimc_md_unregister_entities(fmd);
829         media_device_unregister(&fmd->media_dev);
830         fimc_md_put_clocks(fmd);
831         kfree(fmd);
832         return 0;
833 }
834
835 static struct platform_driver fimc_md_driver = {
836         .probe          = fimc_md_probe,
837         .remove         = __devexit_p(fimc_md_remove),
838         .driver = {
839                 .name   = "s5p-fimc-md",
840                 .owner  = THIS_MODULE,
841         }
842 };
843
844 int __init fimc_md_init(void)
845 {
846         int ret;
847         request_module("s5p-csis");
848         ret = fimc_register_driver();
849         if (ret)
850                 return ret;
851         return platform_driver_register(&fimc_md_driver);
852 }
853 void __exit fimc_md_exit(void)
854 {
855         platform_driver_unregister(&fimc_md_driver);
856         fimc_unregister_driver();
857 }
858
859 module_init(fimc_md_init);
860 module_exit(fimc_md_exit);
861
862 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
863 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
864 MODULE_LICENSE("GPL");
865 MODULE_VERSION("2.0.1");