PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / platform / exynos4-is / media-dev.c
blobc1bce170df6fbce44d519a498bf6d19694389306
1 /*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
4 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
5 * Author: 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/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-of.h>
29 #include <media/media-device.h>
30 #include <media/s5p_fimc.h>
32 #include "media-dev.h"
33 #include "fimc-core.h"
34 #include "fimc-is.h"
35 #include "fimc-lite.h"
36 #include "mipi-csis.h"
38 static int __fimc_md_set_camclk(struct fimc_md *fmd,
39 struct fimc_source_info *si,
40 bool on);
42 /* Set up image sensor subdev -> FIMC capture node notifications. */
43 static void __setup_sensor_notification(struct fimc_md *fmd,
44 struct v4l2_subdev *sensor,
45 struct v4l2_subdev *fimc_sd)
47 struct fimc_source_info *src_inf;
48 struct fimc_sensor_info *md_si;
49 unsigned long flags;
51 src_inf = v4l2_get_subdev_hostdata(sensor);
52 if (!src_inf || WARN_ON(fmd == NULL))
53 return;
55 md_si = source_to_sensor_info(src_inf);
56 spin_lock_irqsave(&fmd->slock, flags);
57 md_si->host = v4l2_get_subdevdata(fimc_sd);
58 spin_unlock_irqrestore(&fmd->slock, flags);
61 /**
62 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
63 * @me: media entity terminating the pipeline
65 * Caller holds the graph mutex.
67 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
68 struct media_entity *me)
70 struct fimc_md *fmd = entity_to_fimc_mdev(me);
71 struct v4l2_subdev *sd;
72 struct v4l2_subdev *sensor = NULL;
73 int i;
75 for (i = 0; i < IDX_MAX; i++)
76 p->subdevs[i] = NULL;
78 while (1) {
79 struct media_pad *pad = NULL;
81 /* Find remote source pad */
82 for (i = 0; i < me->num_pads; i++) {
83 struct media_pad *spad = &me->pads[i];
84 if (!(spad->flags & MEDIA_PAD_FL_SINK))
85 continue;
86 pad = media_entity_remote_pad(spad);
87 if (pad)
88 break;
91 if (pad == NULL ||
92 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
93 break;
94 sd = media_entity_to_v4l2_subdev(pad->entity);
96 switch (sd->grp_id) {
97 case GRP_ID_SENSOR:
98 sensor = sd;
99 /* fall through */
100 case GRP_ID_FIMC_IS_SENSOR:
101 p->subdevs[IDX_SENSOR] = sd;
102 break;
103 case GRP_ID_CSIS:
104 p->subdevs[IDX_CSIS] = sd;
105 break;
106 case GRP_ID_FLITE:
107 p->subdevs[IDX_FLITE] = sd;
108 break;
109 case GRP_ID_FIMC:
110 p->subdevs[IDX_FIMC] = sd;
111 break;
112 case GRP_ID_FIMC_IS:
113 p->subdevs[IDX_IS_ISP] = sd;
114 break;
115 default:
116 break;
118 me = &sd->entity;
119 if (me->num_pads == 1)
120 break;
123 if (sensor && p->subdevs[IDX_FIMC])
124 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
128 * __subdev_set_power - change power state of a single subdev
129 * @sd: subdevice to change power state for
130 * @on: 1 to enable power or 0 to disable
132 * Return result of s_power subdev operation or -ENXIO if sd argument
133 * is NULL. Return 0 if the subdevice does not implement s_power.
135 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
137 int *use_count;
138 int ret;
140 if (sd == NULL)
141 return -ENXIO;
143 use_count = &sd->entity.use_count;
144 if (on && (*use_count)++ > 0)
145 return 0;
146 else if (!on && (*use_count == 0 || --(*use_count) > 0))
147 return 0;
148 ret = v4l2_subdev_call(sd, core, s_power, on);
150 return ret != -ENOIOCTLCMD ? ret : 0;
154 * fimc_pipeline_s_power - change power state of all pipeline subdevs
155 * @fimc: fimc device terminating the pipeline
156 * @state: true to power on, false to power off
158 * Needs to be called with the graph mutex held.
160 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
162 static const u8 seq[2][IDX_MAX - 1] = {
163 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
164 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
166 int i, ret = 0;
168 if (p->subdevs[IDX_SENSOR] == NULL)
169 return -ENXIO;
171 for (i = 0; i < IDX_MAX - 1; i++) {
172 unsigned int idx = seq[on][i];
174 ret = __subdev_set_power(p->subdevs[idx], on);
177 if (ret < 0 && ret != -ENXIO)
178 goto error;
180 return 0;
181 error:
182 for (; i >= 0; i--) {
183 unsigned int idx = seq[on][i];
184 __subdev_set_power(p->subdevs[idx], !on);
186 return ret;
190 * __fimc_pipeline_open - update the pipeline information, enable power
191 * of all pipeline subdevs and the sensor clock
192 * @me: media entity to start graph walk with
193 * @prepare: true to walk the current pipeline and acquire all subdevs
195 * Called with the graph mutex held.
197 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
198 struct media_entity *me, bool prepare)
200 struct fimc_md *fmd = entity_to_fimc_mdev(me);
201 struct fimc_pipeline *p = to_fimc_pipeline(ep);
202 struct v4l2_subdev *sd;
203 int ret;
205 if (WARN_ON(p == NULL || me == NULL))
206 return -EINVAL;
208 if (prepare)
209 fimc_pipeline_prepare(p, me);
211 sd = p->subdevs[IDX_SENSOR];
212 if (sd == NULL)
213 return -EINVAL;
215 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
216 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
217 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
218 if (ret < 0)
219 return ret;
221 ret = fimc_md_set_camclk(sd, true);
222 if (ret < 0)
223 goto err_wbclk;
225 ret = fimc_pipeline_s_power(p, 1);
226 if (!ret)
227 return 0;
229 fimc_md_set_camclk(sd, false);
231 err_wbclk:
232 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
233 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
235 return ret;
239 * __fimc_pipeline_close - disable the sensor clock and pipeline power
240 * @fimc: fimc device terminating the pipeline
242 * Disable power of all subdevs and turn the external sensor clock off.
244 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
246 struct fimc_pipeline *p = to_fimc_pipeline(ep);
247 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
248 struct fimc_md *fmd;
249 int ret;
251 if (sd == NULL) {
252 pr_warn("%s(): No sensor subdev\n", __func__);
253 return 0;
256 ret = fimc_pipeline_s_power(p, 0);
257 fimc_md_set_camclk(sd, false);
259 fmd = entity_to_fimc_mdev(&sd->entity);
261 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
262 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
263 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
265 return ret == -ENXIO ? 0 : ret;
269 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
270 * @pipeline: video pipeline structure
271 * @on: passed as the s_stream() callback argument
273 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
275 static const u8 seq[2][IDX_MAX] = {
276 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
277 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
279 struct fimc_pipeline *p = to_fimc_pipeline(ep);
280 int i, ret = 0;
282 if (p->subdevs[IDX_SENSOR] == NULL)
283 return -ENODEV;
285 for (i = 0; i < IDX_MAX; i++) {
286 unsigned int idx = seq[on][i];
288 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
290 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
291 goto error;
293 return 0;
294 error:
295 for (; i >= 0; i--) {
296 unsigned int idx = seq[on][i];
297 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
299 return ret;
302 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
303 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
304 .open = __fimc_pipeline_open,
305 .close = __fimc_pipeline_close,
306 .set_stream = __fimc_pipeline_s_stream,
309 static struct exynos_media_pipeline *fimc_md_pipeline_create(
310 struct fimc_md *fmd)
312 struct fimc_pipeline *p;
314 p = kzalloc(sizeof(*p), GFP_KERNEL);
315 if (!p)
316 return NULL;
318 list_add_tail(&p->list, &fmd->pipelines);
320 p->ep.ops = &fimc_pipeline_ops;
321 return &p->ep;
324 static void fimc_md_pipelines_free(struct fimc_md *fmd)
326 while (!list_empty(&fmd->pipelines)) {
327 struct fimc_pipeline *p;
329 p = list_entry(fmd->pipelines.next, typeof(*p), list);
330 list_del(&p->list);
331 kfree(p);
336 * Sensor subdevice helper functions
338 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
339 struct fimc_source_info *si)
341 struct i2c_adapter *adapter;
342 struct v4l2_subdev *sd = NULL;
344 if (!si || !fmd)
345 return NULL;
347 * If FIMC bus type is not Writeback FIFO assume it is same
348 * as sensor_bus_type.
350 si->fimc_bus_type = si->sensor_bus_type;
352 adapter = i2c_get_adapter(si->i2c_bus_num);
353 if (!adapter) {
354 v4l2_warn(&fmd->v4l2_dev,
355 "Failed to get I2C adapter %d, deferring probe\n",
356 si->i2c_bus_num);
357 return ERR_PTR(-EPROBE_DEFER);
359 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
360 si->board_info, NULL);
361 if (IS_ERR_OR_NULL(sd)) {
362 i2c_put_adapter(adapter);
363 v4l2_warn(&fmd->v4l2_dev,
364 "Failed to acquire subdev %s, deferring probe\n",
365 si->board_info->type);
366 return ERR_PTR(-EPROBE_DEFER);
368 v4l2_set_subdev_hostdata(sd, si);
369 sd->grp_id = GRP_ID_SENSOR;
371 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
372 sd->name);
373 return sd;
376 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
378 struct i2c_client *client = v4l2_get_subdevdata(sd);
379 struct i2c_adapter *adapter;
381 if (!client)
382 return;
384 v4l2_device_unregister_subdev(sd);
386 if (!client->dev.of_node) {
387 adapter = client->adapter;
388 i2c_unregister_device(client);
389 if (adapter)
390 i2c_put_adapter(adapter);
394 #ifdef CONFIG_OF
395 /* Register I2C client subdev associated with @node. */
396 static int fimc_md_of_add_sensor(struct fimc_md *fmd,
397 struct device_node *node, int index)
399 struct fimc_sensor_info *si;
400 struct i2c_client *client;
401 struct v4l2_subdev *sd;
402 int ret;
404 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
405 return -EINVAL;
406 si = &fmd->sensor[index];
408 client = of_find_i2c_device_by_node(node);
409 if (!client)
410 return -EPROBE_DEFER;
412 device_lock(&client->dev);
414 if (!client->dev.driver ||
415 !try_module_get(client->dev.driver->owner)) {
416 ret = -EPROBE_DEFER;
417 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
418 node->full_name);
419 goto dev_put;
422 /* Enable sensor's master clock */
423 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
424 if (ret < 0)
425 goto mod_put;
426 sd = i2c_get_clientdata(client);
428 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
429 __fimc_md_set_camclk(fmd, &si->pdata, false);
430 if (ret < 0)
431 goto mod_put;
433 v4l2_set_subdev_hostdata(sd, &si->pdata);
434 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
435 sd->grp_id = GRP_ID_FIMC_IS_SENSOR;
436 else
437 sd->grp_id = GRP_ID_SENSOR;
439 si->subdev = sd;
440 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
441 sd->name, fmd->num_sensors);
442 fmd->num_sensors++;
444 mod_put:
445 module_put(client->dev.driver->owner);
446 dev_put:
447 device_unlock(&client->dev);
448 put_device(&client->dev);
449 return ret;
452 /* Parse port node and register as a sub-device any sensor specified there. */
453 static int fimc_md_parse_port_node(struct fimc_md *fmd,
454 struct device_node *port,
455 unsigned int index)
457 struct device_node *rem, *ep, *np;
458 struct fimc_source_info *pd;
459 struct v4l2_of_endpoint endpoint;
460 int ret;
461 u32 val;
463 pd = &fmd->sensor[index].pdata;
465 /* Assume here a port node can have only one endpoint node. */
466 ep = of_get_next_child(port, NULL);
467 if (!ep)
468 return 0;
470 v4l2_of_parse_endpoint(ep, &endpoint);
471 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
472 return -EINVAL;
474 pd->mux_id = (endpoint.port - 1) & 0x1;
476 rem = v4l2_of_get_remote_port_parent(ep);
477 of_node_put(ep);
478 if (rem == NULL) {
479 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
480 ep->full_name);
481 return 0;
483 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
484 pd->clk_id = val;
486 if (!of_property_read_u32(rem, "clock-frequency", &val))
487 pd->clk_frequency = val;
489 if (pd->clk_frequency == 0) {
490 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
491 rem->full_name);
492 of_node_put(rem);
493 return -EINVAL;
496 if (fimc_input_is_parallel(endpoint.port)) {
497 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
498 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
499 else
500 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
501 pd->flags = endpoint.bus.parallel.flags;
502 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
504 * MIPI CSI-2: only input mux selection and
505 * the sensor's clock frequency is needed.
507 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
508 } else {
509 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
510 endpoint.port, rem->full_name);
513 * For FIMC-IS handled sensors, that are placed under i2c-isp device
514 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
515 * input. Sensors are attached to the FIMC-LITE hostdata interface
516 * directly or through MIPI-CSIS, depending on the external media bus
517 * used. This needs to be handled in a more reliable way, not by just
518 * checking parent's node name.
520 np = of_get_parent(rem);
522 if (np && !of_node_cmp(np->name, "i2c-isp"))
523 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
524 else
525 pd->fimc_bus_type = pd->sensor_bus_type;
527 ret = fimc_md_of_add_sensor(fmd, rem, index);
528 of_node_put(rem);
530 return ret;
533 /* Register all SoC external sub-devices */
534 static int fimc_md_of_sensors_register(struct fimc_md *fmd,
535 struct device_node *np)
537 struct device_node *parent = fmd->pdev->dev.of_node;
538 struct device_node *node, *ports;
539 int index = 0;
540 int ret;
542 /* Attach sensors linked to MIPI CSI-2 receivers */
543 for_each_available_child_of_node(parent, node) {
544 struct device_node *port;
546 if (of_node_cmp(node->name, "csis"))
547 continue;
548 /* The csis node can have only port subnode. */
549 port = of_get_next_child(node, NULL);
550 if (!port)
551 continue;
553 ret = fimc_md_parse_port_node(fmd, port, index);
554 if (ret < 0)
555 return ret;
556 index++;
559 /* Attach sensors listed in the parallel-ports node */
560 ports = of_get_child_by_name(parent, "parallel-ports");
561 if (!ports)
562 return 0;
564 for_each_child_of_node(ports, node) {
565 ret = fimc_md_parse_port_node(fmd, node, index);
566 if (ret < 0)
567 break;
568 index++;
571 return 0;
574 static int __of_get_csis_id(struct device_node *np)
576 u32 reg = 0;
578 np = of_get_child_by_name(np, "port");
579 if (!np)
580 return -EINVAL;
581 of_property_read_u32(np, "reg", &reg);
582 return reg - FIMC_INPUT_MIPI_CSI2_0;
584 #else
585 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
586 #define __of_get_csis_id(np) (-ENOSYS)
587 #endif
589 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
591 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
592 struct device_node *of_node = fmd->pdev->dev.of_node;
593 int num_clients = 0;
594 int ret, i;
597 * Runtime resume one of the FIMC entities to make sure
598 * the sclk_cam clocks are not globally disabled.
600 if (!fmd->pmf)
601 return -ENXIO;
603 ret = pm_runtime_get_sync(fmd->pmf);
604 if (ret < 0)
605 return ret;
607 if (of_node) {
608 fmd->num_sensors = 0;
609 ret = fimc_md_of_sensors_register(fmd, of_node);
610 } else if (pdata) {
611 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
612 num_clients = min_t(u32, pdata->num_clients,
613 ARRAY_SIZE(fmd->sensor));
614 fmd->num_sensors = num_clients;
616 for (i = 0; i < num_clients; i++) {
617 struct fimc_sensor_info *si = &fmd->sensor[i];
618 struct v4l2_subdev *sd;
620 si->pdata = pdata->source_info[i];
621 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
622 if (ret)
623 break;
624 sd = fimc_md_register_sensor(fmd, &si->pdata);
625 ret = __fimc_md_set_camclk(fmd, &si->pdata, false);
627 if (IS_ERR(sd)) {
628 si->subdev = NULL;
629 ret = PTR_ERR(sd);
630 break;
632 si->subdev = sd;
633 if (ret)
634 break;
638 pm_runtime_put(fmd->pmf);
639 return ret;
643 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
646 static int register_fimc_lite_entity(struct fimc_md *fmd,
647 struct fimc_lite *fimc_lite)
649 struct v4l2_subdev *sd;
650 struct exynos_media_pipeline *ep;
651 int ret;
653 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
654 fmd->fimc_lite[fimc_lite->index]))
655 return -EBUSY;
657 sd = &fimc_lite->subdev;
658 sd->grp_id = GRP_ID_FLITE;
660 ep = fimc_md_pipeline_create(fmd);
661 if (!ep)
662 return -ENOMEM;
664 v4l2_set_subdev_hostdata(sd, ep);
666 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
667 if (!ret)
668 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
669 else
670 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
671 fimc_lite->index);
672 return ret;
675 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
677 struct v4l2_subdev *sd;
678 struct exynos_media_pipeline *ep;
679 int ret;
681 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
682 return -EBUSY;
684 sd = &fimc->vid_cap.subdev;
685 sd->grp_id = GRP_ID_FIMC;
687 ep = fimc_md_pipeline_create(fmd);
688 if (!ep)
689 return -ENOMEM;
691 v4l2_set_subdev_hostdata(sd, ep);
693 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
694 if (!ret) {
695 if (!fmd->pmf && fimc->pdev)
696 fmd->pmf = &fimc->pdev->dev;
697 fmd->fimc[fimc->id] = fimc;
698 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
699 } else {
700 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
701 fimc->id, ret);
703 return ret;
706 static int register_csis_entity(struct fimc_md *fmd,
707 struct platform_device *pdev,
708 struct v4l2_subdev *sd)
710 struct device_node *node = pdev->dev.of_node;
711 int id, ret;
713 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
715 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
716 return -ENOENT;
718 if (WARN_ON(fmd->csis[id].sd))
719 return -EBUSY;
721 sd->grp_id = GRP_ID_CSIS;
722 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
723 if (!ret)
724 fmd->csis[id].sd = sd;
725 else
726 v4l2_err(&fmd->v4l2_dev,
727 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
728 return ret;
731 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
733 struct v4l2_subdev *sd = &is->isp.subdev;
734 int ret;
736 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
737 if (ret) {
738 v4l2_err(&fmd->v4l2_dev,
739 "Failed to register FIMC-ISP (%d)\n", ret);
740 return ret;
743 fmd->fimc_is = is;
744 return 0;
747 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
748 struct platform_device *pdev,
749 int plat_entity)
751 struct device *dev = &pdev->dev;
752 int ret = -EPROBE_DEFER;
753 void *drvdata;
755 /* Lock to ensure dev->driver won't change. */
756 device_lock(dev);
758 if (!dev->driver || !try_module_get(dev->driver->owner))
759 goto dev_unlock;
761 drvdata = dev_get_drvdata(dev);
762 /* Some subdev didn't probe successfully id drvdata is NULL */
763 if (drvdata) {
764 switch (plat_entity) {
765 case IDX_FIMC:
766 ret = register_fimc_entity(fmd, drvdata);
767 break;
768 case IDX_FLITE:
769 ret = register_fimc_lite_entity(fmd, drvdata);
770 break;
771 case IDX_CSIS:
772 ret = register_csis_entity(fmd, pdev, drvdata);
773 break;
774 case IDX_IS_ISP:
775 ret = register_fimc_is_entity(fmd, drvdata);
776 break;
777 default:
778 ret = -ENODEV;
782 module_put(dev->driver->owner);
783 dev_unlock:
784 device_unlock(dev);
785 if (ret == -EPROBE_DEFER)
786 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
787 dev_name(dev));
788 else if (ret < 0)
789 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
790 dev_name(dev), ret);
791 return ret;
794 static int fimc_md_pdev_match(struct device *dev, void *data)
796 struct platform_device *pdev = to_platform_device(dev);
797 int plat_entity = -1;
798 int ret;
799 char *p;
801 if (!get_device(dev))
802 return -ENODEV;
804 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
805 plat_entity = IDX_CSIS;
806 } else {
807 p = strstr(pdev->name, "fimc");
808 if (p && *(p + 4) == 0)
809 plat_entity = IDX_FIMC;
812 if (plat_entity >= 0)
813 ret = fimc_md_register_platform_entity(data, pdev,
814 plat_entity);
815 put_device(dev);
816 return 0;
819 /* Register FIMC, FIMC-LITE and CSIS media entities */
820 #ifdef CONFIG_OF
821 static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
822 struct device_node *parent)
824 struct device_node *node;
825 int ret = 0;
827 for_each_available_child_of_node(parent, node) {
828 struct platform_device *pdev;
829 int plat_entity = -1;
831 pdev = of_find_device_by_node(node);
832 if (!pdev)
833 continue;
835 /* If driver of any entity isn't ready try all again later. */
836 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
837 plat_entity = IDX_CSIS;
838 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
839 plat_entity = IDX_IS_ISP;
840 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
841 plat_entity = IDX_FLITE;
842 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
843 !of_property_read_bool(node, "samsung,lcd-wb"))
844 plat_entity = IDX_FIMC;
846 if (plat_entity >= 0)
847 ret = fimc_md_register_platform_entity(fmd, pdev,
848 plat_entity);
849 put_device(&pdev->dev);
850 if (ret < 0)
851 break;
854 return ret;
856 #else
857 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
858 #endif
860 static void fimc_md_unregister_entities(struct fimc_md *fmd)
862 int i;
864 for (i = 0; i < FIMC_MAX_DEVS; i++) {
865 struct fimc_dev *dev = fmd->fimc[i];
866 if (dev == NULL)
867 continue;
868 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
869 dev->vid_cap.ve.pipe = NULL;
870 fmd->fimc[i] = NULL;
872 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
873 struct fimc_lite *dev = fmd->fimc_lite[i];
874 if (dev == NULL)
875 continue;
876 v4l2_device_unregister_subdev(&dev->subdev);
877 dev->ve.pipe = NULL;
878 fmd->fimc_lite[i] = NULL;
880 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
881 if (fmd->csis[i].sd == NULL)
882 continue;
883 v4l2_device_unregister_subdev(fmd->csis[i].sd);
884 fmd->csis[i].sd = NULL;
886 for (i = 0; i < fmd->num_sensors; i++) {
887 if (fmd->sensor[i].subdev == NULL)
888 continue;
889 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
890 fmd->sensor[i].subdev = NULL;
893 if (fmd->fimc_is)
894 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
896 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
900 * __fimc_md_create_fimc_links - create links to all FIMC entities
901 * @fmd: fimc media device
902 * @source: the source entity to create links to all fimc entities from
903 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
904 * @pad: the source entity pad index
905 * @link_mask: bitmask of the fimc devices for which link should be enabled
907 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
908 struct media_entity *source,
909 struct v4l2_subdev *sensor,
910 int pad, int link_mask)
912 struct fimc_source_info *si = NULL;
913 struct media_entity *sink;
914 unsigned int flags = 0;
915 int i, ret = 0;
917 if (sensor) {
918 si = v4l2_get_subdev_hostdata(sensor);
919 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
920 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
921 ret = 1;
924 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
925 if (!fmd->fimc[i])
926 continue;
928 * Some FIMC variants are not fitted with camera capture
929 * interface. Skip creating a link from sensor for those.
931 if (!fmd->fimc[i]->variant->has_cam_if)
932 continue;
934 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
936 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
937 ret = media_entity_create_link(source, pad, sink,
938 FIMC_SD_PAD_SINK_CAM, flags);
939 if (ret)
940 return ret;
942 /* Notify FIMC capture subdev entity */
943 ret = media_entity_call(sink, link_setup, &sink->pads[0],
944 &source->pads[pad], flags);
945 if (ret)
946 break;
948 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
949 source->name, flags ? '=' : '-', sink->name);
952 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
953 if (!fmd->fimc_lite[i])
954 continue;
956 sink = &fmd->fimc_lite[i]->subdev.entity;
957 ret = media_entity_create_link(source, pad, sink,
958 FLITE_SD_PAD_SINK, 0);
959 if (ret)
960 return ret;
962 /* Notify FIMC-LITE subdev entity */
963 ret = media_entity_call(sink, link_setup, &sink->pads[0],
964 &source->pads[pad], 0);
965 if (ret)
966 break;
968 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
969 source->name, sink->name);
971 return 0;
974 /* Create links from FIMC-LITE source pads to other entities */
975 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
977 struct media_entity *source, *sink;
978 int i, ret = 0;
980 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
981 struct fimc_lite *fimc = fmd->fimc_lite[i];
983 if (fimc == NULL)
984 continue;
986 source = &fimc->subdev.entity;
987 sink = &fimc->ve.vdev.entity;
988 /* FIMC-LITE's subdev and video node */
989 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
990 sink, 0, 0);
991 if (ret)
992 break;
993 /* Link from FIMC-LITE to IS-ISP subdev */
994 sink = &fmd->fimc_is->isp.subdev.entity;
995 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
996 sink, 0, 0);
997 if (ret)
998 break;
1001 return ret;
1004 /* Create FIMC-IS links */
1005 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
1007 struct media_entity *source, *sink;
1008 int i, ret;
1010 source = &fmd->fimc_is->isp.subdev.entity;
1012 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1013 if (fmd->fimc[i] == NULL)
1014 continue;
1016 /* Link from IS-ISP subdev to FIMC */
1017 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
1018 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
1019 sink, FIMC_SD_PAD_SINK_FIFO, 0);
1020 if (ret)
1021 return ret;
1024 return ret;
1028 * fimc_md_create_links - create default links between registered entities
1030 * Parallel interface sensor entities are connected directly to FIMC capture
1031 * entities. The sensors using MIPI CSIS bus are connected through immutable
1032 * link with CSI receiver entity specified by mux_id. Any registered CSIS
1033 * entity has a link to each registered FIMC capture entity. Enabled links
1034 * are created by default between each subsequent registered sensor and
1035 * subsequent FIMC capture entity. The number of default active links is
1036 * determined by the number of available sensors or FIMC entities,
1037 * whichever is less.
1039 static int fimc_md_create_links(struct fimc_md *fmd)
1041 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
1042 struct v4l2_subdev *sensor, *csis;
1043 struct fimc_source_info *pdata;
1044 struct media_entity *source, *sink;
1045 int i, pad, fimc_id = 0, ret = 0;
1046 u32 flags, link_mask = 0;
1048 for (i = 0; i < fmd->num_sensors; i++) {
1049 if (fmd->sensor[i].subdev == NULL)
1050 continue;
1052 sensor = fmd->sensor[i].subdev;
1053 pdata = v4l2_get_subdev_hostdata(sensor);
1054 if (!pdata)
1055 continue;
1057 source = NULL;
1059 switch (pdata->sensor_bus_type) {
1060 case FIMC_BUS_TYPE_MIPI_CSI2:
1061 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
1062 "Wrong CSI channel id: %d\n", pdata->mux_id))
1063 return -EINVAL;
1065 csis = fmd->csis[pdata->mux_id].sd;
1066 if (WARN(csis == NULL,
1067 "MIPI-CSI interface specified "
1068 "but s5p-csis module is not loaded!\n"))
1069 return -EINVAL;
1071 pad = sensor->entity.num_pads - 1;
1072 ret = media_entity_create_link(&sensor->entity, pad,
1073 &csis->entity, CSIS_PAD_SINK,
1074 MEDIA_LNK_FL_IMMUTABLE |
1075 MEDIA_LNK_FL_ENABLED);
1076 if (ret)
1077 return ret;
1079 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
1080 sensor->entity.name, csis->entity.name);
1082 source = NULL;
1083 csi_sensors[pdata->mux_id] = sensor;
1084 break;
1086 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1087 source = &sensor->entity;
1088 pad = 0;
1089 break;
1091 default:
1092 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1093 pdata->sensor_bus_type);
1094 return -EINVAL;
1096 if (source == NULL)
1097 continue;
1099 link_mask = 1 << fimc_id++;
1100 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1101 pad, link_mask);
1104 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1105 if (fmd->csis[i].sd == NULL)
1106 continue;
1108 source = &fmd->csis[i].sd->entity;
1109 pad = CSIS_PAD_SOURCE;
1110 sensor = csi_sensors[i];
1112 link_mask = 1 << fimc_id++;
1113 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1114 pad, link_mask);
1117 /* Create immutable links between each FIMC's subdev and video node */
1118 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1119 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1120 if (!fmd->fimc[i])
1121 continue;
1123 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1124 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1126 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1127 sink, 0, flags);
1128 if (ret)
1129 break;
1132 ret = __fimc_md_create_flite_source_links(fmd);
1133 if (ret < 0)
1134 return ret;
1136 if (fmd->use_isp)
1137 ret = __fimc_md_create_fimc_is_links(fmd);
1139 return ret;
1143 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1145 static void fimc_md_put_clocks(struct fimc_md *fmd)
1147 int i = FIMC_MAX_CAMCLKS;
1149 while (--i >= 0) {
1150 if (IS_ERR(fmd->camclk[i].clock))
1151 continue;
1152 clk_put(fmd->camclk[i].clock);
1153 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1156 /* Writeback (PIXELASYNCMx) clocks */
1157 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1158 if (IS_ERR(fmd->wbclk[i]))
1159 continue;
1160 clk_put(fmd->wbclk[i]);
1161 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1165 static int fimc_md_get_clocks(struct fimc_md *fmd)
1167 struct device *dev = NULL;
1168 char clk_name[32];
1169 struct clk *clock;
1170 int i, ret = 0;
1172 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1173 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1175 if (fmd->pdev->dev.of_node)
1176 dev = &fmd->pdev->dev;
1178 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1179 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1180 clock = clk_get(dev, clk_name);
1182 if (IS_ERR(clock)) {
1183 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1184 clk_name);
1185 ret = PTR_ERR(clock);
1186 break;
1188 fmd->camclk[i].clock = clock;
1190 if (ret)
1191 fimc_md_put_clocks(fmd);
1193 if (!fmd->use_isp)
1194 return 0;
1196 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1197 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1199 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1201 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1202 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1203 clock = clk_get(dev, clk_name);
1204 if (IS_ERR(clock)) {
1205 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1206 clk_name);
1207 ret = PTR_ERR(clock);
1208 break;
1210 fmd->wbclk[i] = clock;
1212 if (ret)
1213 fimc_md_put_clocks(fmd);
1215 return ret;
1218 static int __fimc_md_set_camclk(struct fimc_md *fmd,
1219 struct fimc_source_info *si,
1220 bool on)
1222 struct fimc_camclk_info *camclk;
1223 int ret = 0;
1225 if (WARN_ON(si->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
1226 return -EINVAL;
1228 camclk = &fmd->camclk[si->clk_id];
1230 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1231 si->clk_id, si->clk_frequency, camclk->use_count, on);
1233 if (on) {
1234 if (camclk->use_count > 0 &&
1235 camclk->frequency != si->clk_frequency)
1236 return -EINVAL;
1238 if (camclk->use_count++ == 0) {
1239 clk_set_rate(camclk->clock, si->clk_frequency);
1240 camclk->frequency = si->clk_frequency;
1241 ret = pm_runtime_get_sync(fmd->pmf);
1242 if (ret < 0)
1243 return ret;
1244 ret = clk_prepare_enable(camclk->clock);
1245 dbg("Enabled camclk %d: f: %lu", si->clk_id,
1246 clk_get_rate(camclk->clock));
1248 return ret;
1251 if (WARN_ON(camclk->use_count == 0))
1252 return 0;
1254 if (--camclk->use_count == 0) {
1255 clk_disable_unprepare(camclk->clock);
1256 pm_runtime_put(fmd->pmf);
1257 dbg("Disabled camclk %d", si->clk_id);
1259 return ret;
1263 * fimc_md_set_camclk - peripheral sensor clock setup
1264 * @sd: sensor subdev to configure sclk_cam clock for
1265 * @on: 1 to enable or 0 to disable the clock
1267 * There are 2 separate clock outputs available in the SoC for external
1268 * image processors. These clocks are shared between all registered FIMC
1269 * devices to which sensors can be attached, either directly or through
1270 * the MIPI CSI receiver. The clock is allowed here to be used by
1271 * multiple sensors concurrently if they use same frequency.
1272 * This function should only be called when the graph mutex is held.
1274 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1276 struct fimc_source_info *si = v4l2_get_subdev_hostdata(sd);
1277 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1279 return __fimc_md_set_camclk(fmd, si, on);
1282 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1284 struct exynos_video_entity *ve;
1285 struct fimc_pipeline *p;
1286 struct video_device *vdev;
1287 int ret;
1289 vdev = media_entity_to_video_device(entity);
1290 if (vdev->entity.use_count == 0)
1291 return 0;
1293 ve = vdev_to_exynos_video_entity(vdev);
1294 p = to_fimc_pipeline(ve->pipe);
1296 * Nothing to do if we are disabling the pipeline, some link
1297 * has been disconnected and p->subdevs array is cleared now.
1299 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1300 return 0;
1302 if (enable)
1303 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1304 else
1305 ret = __fimc_pipeline_close(ve->pipe);
1307 if (ret == 0 && !enable)
1308 memset(p->subdevs, 0, sizeof(p->subdevs));
1310 return ret;
1313 /* Locking: called with entity->parent->graph_mutex mutex held. */
1314 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1316 struct media_entity *entity_err = entity;
1317 struct media_entity_graph graph;
1318 int ret;
1321 * Walk current graph and call the pipeline open/close routine for each
1322 * opened video node that belongs to the graph of entities connected
1323 * through active links. This is needed as we cannot power on/off the
1324 * subdevs in random order.
1326 media_entity_graph_walk_start(&graph, entity);
1328 while ((entity = media_entity_graph_walk_next(&graph))) {
1329 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1330 continue;
1332 ret = __fimc_md_modify_pipeline(entity, enable);
1334 if (ret < 0)
1335 goto err;
1338 return 0;
1339 err:
1340 media_entity_graph_walk_start(&graph, entity_err);
1342 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1343 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1344 continue;
1346 __fimc_md_modify_pipeline(entity_err, !enable);
1348 if (entity_err == entity)
1349 break;
1352 return ret;
1355 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1356 unsigned int notification)
1358 struct media_entity *sink = link->sink->entity;
1359 int ret = 0;
1361 /* Before link disconnection */
1362 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1363 if (!(flags & MEDIA_LNK_FL_ENABLED))
1364 ret = __fimc_md_modify_pipelines(sink, false);
1365 else
1366 ; /* TODO: Link state change validation */
1367 /* After link activation */
1368 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1369 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1370 ret = __fimc_md_modify_pipelines(sink, true);
1373 return ret ? -EPIPE : 0;
1376 static ssize_t fimc_md_sysfs_show(struct device *dev,
1377 struct device_attribute *attr, char *buf)
1379 struct platform_device *pdev = to_platform_device(dev);
1380 struct fimc_md *fmd = platform_get_drvdata(pdev);
1382 if (fmd->user_subdev_api)
1383 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1385 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1388 static ssize_t fimc_md_sysfs_store(struct device *dev,
1389 struct device_attribute *attr,
1390 const char *buf, size_t count)
1392 struct platform_device *pdev = to_platform_device(dev);
1393 struct fimc_md *fmd = platform_get_drvdata(pdev);
1394 bool subdev_api;
1395 int i;
1397 if (!strcmp(buf, "vid-dev\n"))
1398 subdev_api = false;
1399 else if (!strcmp(buf, "sub-dev\n"))
1400 subdev_api = true;
1401 else
1402 return count;
1404 fmd->user_subdev_api = subdev_api;
1405 for (i = 0; i < FIMC_MAX_DEVS; i++)
1406 if (fmd->fimc[i])
1407 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1408 return count;
1411 * This device attribute is to select video pipeline configuration method.
1412 * There are following valid values:
1413 * vid-dev - for V4L2 video node API only, subdevice will be configured
1414 * by the host driver.
1415 * sub-dev - for media controller API, subdevs must be configured in user
1416 * space before starting streaming.
1418 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1419 fimc_md_sysfs_show, fimc_md_sysfs_store);
1421 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1423 struct device *dev = &fmd->pdev->dev;
1424 struct fimc_pinctrl *pctl = &fmd->pinctl;
1426 pctl->pinctrl = devm_pinctrl_get(dev);
1427 if (IS_ERR(pctl->pinctrl))
1428 return PTR_ERR(pctl->pinctrl);
1430 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1431 PINCTRL_STATE_DEFAULT);
1432 if (IS_ERR(pctl->state_default))
1433 return PTR_ERR(pctl->state_default);
1435 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1436 PINCTRL_STATE_IDLE);
1437 return 0;
1440 static int fimc_md_probe(struct platform_device *pdev)
1442 struct device *dev = &pdev->dev;
1443 struct v4l2_device *v4l2_dev;
1444 struct fimc_md *fmd;
1445 int ret;
1447 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1448 if (!fmd)
1449 return -ENOMEM;
1451 spin_lock_init(&fmd->slock);
1452 fmd->pdev = pdev;
1453 INIT_LIST_HEAD(&fmd->pipelines);
1455 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1456 sizeof(fmd->media_dev.model));
1457 fmd->media_dev.link_notify = fimc_md_link_notify;
1458 fmd->media_dev.dev = dev;
1460 v4l2_dev = &fmd->v4l2_dev;
1461 v4l2_dev->mdev = &fmd->media_dev;
1462 v4l2_dev->notify = fimc_sensor_notify;
1463 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1465 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1467 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1468 if (ret < 0) {
1469 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1470 return ret;
1472 ret = media_device_register(&fmd->media_dev);
1473 if (ret < 0) {
1474 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1475 goto err_md;
1477 ret = fimc_md_get_clocks(fmd);
1478 if (ret)
1479 goto err_clk;
1481 fmd->user_subdev_api = (dev->of_node != NULL);
1483 /* Protect the media graph while we're registering entities */
1484 mutex_lock(&fmd->media_dev.graph_mutex);
1486 ret = fimc_md_get_pinctrl(fmd);
1487 if (ret < 0) {
1488 if (ret != EPROBE_DEFER)
1489 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1490 goto err_unlock;
1493 if (dev->of_node)
1494 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1495 else
1496 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1497 fimc_md_pdev_match);
1498 if (ret)
1499 goto err_unlock;
1501 if (dev->platform_data || dev->of_node) {
1502 ret = fimc_md_register_sensor_entities(fmd);
1503 if (ret)
1504 goto err_unlock;
1507 ret = fimc_md_create_links(fmd);
1508 if (ret)
1509 goto err_unlock;
1510 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1511 if (ret)
1512 goto err_unlock;
1514 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1515 if (ret)
1516 goto err_unlock;
1518 platform_set_drvdata(pdev, fmd);
1519 mutex_unlock(&fmd->media_dev.graph_mutex);
1520 return 0;
1522 err_unlock:
1523 mutex_unlock(&fmd->media_dev.graph_mutex);
1524 err_clk:
1525 fimc_md_put_clocks(fmd);
1526 fimc_md_unregister_entities(fmd);
1527 media_device_unregister(&fmd->media_dev);
1528 err_md:
1529 v4l2_device_unregister(&fmd->v4l2_dev);
1530 return ret;
1533 static int fimc_md_remove(struct platform_device *pdev)
1535 struct fimc_md *fmd = platform_get_drvdata(pdev);
1537 if (!fmd)
1538 return 0;
1540 v4l2_device_unregister(&fmd->v4l2_dev);
1541 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1542 fimc_md_unregister_entities(fmd);
1543 fimc_md_pipelines_free(fmd);
1544 media_device_unregister(&fmd->media_dev);
1545 fimc_md_put_clocks(fmd);
1546 return 0;
1549 static struct platform_device_id fimc_driver_ids[] __always_unused = {
1550 { .name = "s5p-fimc-md" },
1551 { },
1553 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1555 static const struct of_device_id fimc_md_of_match[] = {
1556 { .compatible = "samsung,fimc" },
1557 { },
1559 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1561 static struct platform_driver fimc_md_driver = {
1562 .probe = fimc_md_probe,
1563 .remove = fimc_md_remove,
1564 .driver = {
1565 .of_match_table = of_match_ptr(fimc_md_of_match),
1566 .name = "s5p-fimc-md",
1567 .owner = THIS_MODULE,
1571 static int __init fimc_md_init(void)
1573 int ret;
1575 request_module("s5p-csis");
1576 ret = fimc_register_driver();
1577 if (ret)
1578 return ret;
1580 return platform_driver_register(&fimc_md_driver);
1583 static void __exit fimc_md_exit(void)
1585 platform_driver_unregister(&fimc_md_driver);
1586 fimc_unregister_driver();
1589 module_init(fimc_md_init);
1590 module_exit(fimc_md_exit);
1592 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1593 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1594 MODULE_LICENSE("GPL");
1595 MODULE_VERSION("2.0.1");