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