[media] s5p-fimc: Fail driver probing when sensor configuration is wrong
[linux-2.6/libata-dev.git] / drivers / media / video / s5p-fimc / fimc-mdevice.c
blob615c862f0360ef20061e28701d1a99f460d0a7e1
1 /*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
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.
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>
28 #include "fimc-core.h"
29 #include "fimc-mdevice.h"
30 #include "mipi-csis.h"
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
39 * Caller holds the graph mutex.
41 void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me)
43 struct media_entity_graph graph;
44 struct v4l2_subdev *sd;
46 media_entity_graph_walk_start(&graph, me);
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);
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;
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
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.
68 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
70 int *use_count;
71 int ret;
73 if (sd == NULL)
74 return -ENXIO;
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);
83 return ret != -ENOIOCTLCMD ? ret : 0;
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
91 * Need to be called with the graph mutex held.
93 int fimc_pipeline_s_power(struct fimc_dev *fimc, int state)
95 int ret = 0;
97 if (fimc->pipeline.sensor == NULL)
98 return -ENXIO;
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);
107 ret = __subdev_set_power(fimc->pipeline.sensor, 0);
108 if (ret)
109 return ret;
110 ret = __subdev_set_power(fimc->pipeline.csis, 0);
112 return ret == -ENXIO ? 0 : ret;
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
121 * This function must be called with the graph mutex held.
123 static int __fimc_pipeline_initialize(struct fimc_dev *fimc,
124 struct media_entity *me, bool prep)
126 int ret;
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);
138 int fimc_pipeline_initialize(struct fimc_dev *fimc, struct media_entity *me,
139 bool prep)
141 int ret;
143 mutex_lock(&me->parent->graph_mutex);
144 ret = __fimc_pipeline_initialize(fimc, me, prep);
145 mutex_unlock(&me->parent->graph_mutex);
147 return ret;
151 * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power
152 * @fimc: fimc device terminating the pipeline
154 * Disable power of all subdevs in the pipeline and turn off the external
155 * sensor clock.
156 * Called with the graph mutex held.
158 int __fimc_pipeline_shutdown(struct fimc_dev *fimc)
160 int ret = 0;
162 if (fimc->pipeline.sensor) {
163 ret = fimc_pipeline_s_power(fimc, 0);
164 fimc_md_set_camclk(fimc->pipeline.sensor, false);
166 return ret == -ENXIO ? 0 : ret;
169 int fimc_pipeline_shutdown(struct fimc_dev *fimc)
171 struct media_entity *me = &fimc->vid_cap.vfd->entity;
172 int ret;
174 mutex_lock(&me->parent->graph_mutex);
175 ret = __fimc_pipeline_shutdown(fimc);
176 mutex_unlock(&me->parent->graph_mutex);
178 return ret;
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
186 int fimc_pipeline_s_stream(struct fimc_dev *fimc, int on)
188 struct fimc_pipeline *p = &fimc->pipeline;
189 int ret = 0;
191 if (p->sensor == NULL)
192 return -ENODEV;
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;
206 * Sensor subdevice helper functions
208 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
209 struct fimc_sensor_info *s_info)
211 struct i2c_adapter *adapter;
212 struct v4l2_subdev *sd = NULL;
214 if (!s_info || !fmd)
215 return NULL;
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;
227 v4l2_set_subdev_hostdata(sd, s_info);
228 sd->grp_id = SENSOR_GROUP_ID;
230 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
231 s_info->pdata->board_info->type);
232 return sd;
235 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
237 struct i2c_client *client = v4l2_get_subdevdata(sd);
238 struct i2c_adapter *adapter;
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);
249 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
251 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
252 struct fimc_dev *fd = NULL;
253 int num_clients, ret, i;
256 * Runtime resume one of the FIMC entities to make sure
257 * the sclk_cam clocks are not globally disabled.
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;
268 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
269 num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
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;
283 pm_runtime_put(&fd->pdev->dev);
284 return ret;
288 * MIPI CSIS and FIMC platform devices registration.
290 static int fimc_register_callback(struct device *dev, void *p)
292 struct fimc_dev *fimc = dev_get_drvdata(dev);
293 struct fimc_md *fmd = p;
294 int ret;
296 if (!fimc || !fimc->pdev)
297 return 0;
298 if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
299 return 0;
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;
311 static int csis_register_callback(struct device *dev, void *p)
313 struct v4l2_subdev *sd = dev_get_drvdata(dev);
314 struct platform_device *pdev;
315 struct fimc_md *fmd = p;
316 int id, ret;
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);
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;
336 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
338 static int fimc_md_register_platform_entities(struct fimc_md *fmd)
340 struct device_driver *driver;
341 int ret;
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;
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);
358 return ret;
361 static void fimc_md_unregister_entities(struct fimc_md *fmd)
363 int i;
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;
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;
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;
386 static int fimc_md_register_video_nodes(struct fimc_md *fmd)
388 struct video_device *vdev;
389 int i, ret = 0;
391 for (i = 0; i < FIMC_MAX_DEVS && !ret; i++) {
392 if (!fmd->fimc[i])
393 continue;
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));
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));
412 return ret;
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
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)
428 struct fimc_sensor_info *s_info;
429 struct media_entity *sink;
430 unsigned int flags;
431 int ret, i;
433 for (i = 0; i < FIMC_MAX_DEVS; i++) {
434 if (!fmd->fimc[i])
435 break;
437 * Some FIMC variants are not fitted with camera capture
438 * interface. Skip creating a link from sensor for those.
440 if (sensor->grp_id == SENSOR_GROUP_ID &&
441 !fmd->fimc[i]->variant->has_cam_if)
442 continue;
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;
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;
457 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
458 source->name, flags ? '=' : '-', sink->name);
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);
470 return 0;
474 * fimc_md_create_links - create default links between registered entities
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.
485 static int fimc_md_create_links(struct fimc_md *fmd)
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;
495 for (i = 0; i < fmd->num_sensors; i++) {
496 if (fmd->sensor[i].subdev == NULL)
497 continue;
499 sensor = fmd->sensor[i].subdev;
500 s_info = v4l2_get_subdev_hostdata(sensor);
501 if (!s_info || !s_info->pdata)
502 continue;
504 source = NULL;
505 pdata = s_info->pdata;
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;
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;
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;
526 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
527 sensor->entity.name, csis->entity.name);
529 source = &csis->entity;
530 pad = CSIS_PAD_SOURCE;
531 break;
533 case FIMC_ITU_601...FIMC_ITU_656:
534 source = &sensor->entity;
535 pad = 0;
536 break;
538 default:
539 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
540 pdata->bus_type);
541 return -EINVAL;
543 if (source == NULL)
544 continue;
546 ret = __fimc_md_create_fimc_links(fmd, source, sensor, pad,
547 fimc_id++);
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;
562 return ret;
566 * The peripheral sensor clock management.
568 static int fimc_md_get_clocks(struct fimc_md *fmd)
570 char clk_name[32];
571 struct clk *clock;
572 int i;
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;
582 fmd->camclk[i].clock = clock;
584 return 0;
587 static void fimc_md_put_clocks(struct fimc_md *fmd)
589 int i = FIMC_MAX_CAMCLKS;
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;
599 static int __fimc_md_set_camclk(struct fimc_md *fmd,
600 struct fimc_sensor_info *s_info,
601 bool on)
603 struct s5p_fimc_isp_info *pdata = s_info->pdata;
604 struct fimc_camclk_info *camclk;
605 int ret = 0;
607 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
608 return -EINVAL;
610 if (s_info->clk_on == on)
611 return 0;
612 camclk = &fmd->camclk[pdata->clk_id];
614 dbg("camclk %d, f: %lu, clk: %p, on: %d",
615 pdata->clk_id, pdata->clk_frequency, camclk, on);
617 if (on) {
618 if (camclk->use_count > 0 &&
619 camclk->frequency != pdata->clk_frequency)
620 return -EINVAL;
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);
627 s_info->clk_on = 1;
628 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
629 clk_get_rate(camclk->clock));
631 return ret;
634 if (WARN_ON(camclk->use_count == 0))
635 return 0;
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);
642 return ret;
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
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.
659 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
661 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
662 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
664 return __fimc_md_set_camclk(fmd, s_info, on);
667 static int fimc_md_link_notify(struct media_pad *source,
668 struct media_pad *sink, u32 flags)
670 struct v4l2_subdev *sd;
671 struct fimc_dev *fimc;
672 int ret = 0;
674 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
675 return 0;
677 sd = media_entity_to_v4l2_subdev(sink->entity);
678 fimc = v4l2_get_subdevdata(sd);
680 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
681 ret = __fimc_pipeline_shutdown(fimc);
682 fimc->pipeline.sensor = NULL;
683 fimc->pipeline.csis = NULL;
685 mutex_lock(&fimc->lock);
686 fimc_ctrls_delete(fimc->vid_cap.ctx);
687 mutex_unlock(&fimc->lock);
688 return ret;
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.
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);
701 mutex_unlock(&fimc->lock);
703 return ret ? -EPIPE : ret;
706 static ssize_t fimc_md_sysfs_show(struct device *dev,
707 struct device_attribute *attr, char *buf)
709 struct platform_device *pdev = to_platform_device(dev);
710 struct fimc_md *fmd = platform_get_drvdata(pdev);
712 if (fmd->user_subdev_api)
713 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
715 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
718 static ssize_t fimc_md_sysfs_store(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t count)
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;
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;
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;
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.
748 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
749 fimc_md_sysfs_show, fimc_md_sysfs_store);
751 static int __devinit fimc_md_probe(struct platform_device *pdev)
753 struct v4l2_device *v4l2_dev;
754 struct fimc_md *fmd;
755 int ret;
757 fmd = kzalloc(sizeof(struct fimc_md), GFP_KERNEL);
758 if (!fmd)
759 return -ENOMEM;
761 spin_lock_init(&fmd->slock);
762 fmd->pdev = pdev;
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;
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));
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;
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;
785 ret = fimc_md_get_clocks(fmd);
786 if (ret)
787 goto err3;
789 fmd->user_subdev_api = false;
790 ret = fimc_md_register_platform_entities(fmd);
791 if (ret)
792 goto err3;
794 if (pdev->dev.platform_data) {
795 ret = fimc_md_register_sensor_entities(fmd);
796 if (ret)
797 goto err3;
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;
809 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
810 if (!ret) {
811 platform_set_drvdata(pdev, fmd);
812 return 0;
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;
825 static int __devexit fimc_md_remove(struct platform_device *pdev)
827 struct fimc_md *fmd = platform_get_drvdata(pdev);
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;
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,
848 int __init fimc_md_init(void)
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);
857 void __exit fimc_md_exit(void)
859 platform_driver_unregister(&fimc_md_driver);
860 fimc_unregister_driver();
863 module_init(fimc_md_init);
864 module_exit(fimc_md_exit);
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");