[media] exynos4-is: Add the FIMC-IS ISP capture DMA driver
[deliverable/linux.git] / drivers / media / platform / exynos4-is / media-dev.c
1 /*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
3 *
4 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
5 * Author: 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/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/device.h>
17 #include <linux/errno.h>
18 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <media/v4l2-async.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-of.h>
32 #include <media/media-device.h>
33 #include <media/s5p_fimc.h>
34
35 #include "media-dev.h"
36 #include "fimc-core.h"
37 #include "fimc-is.h"
38 #include "fimc-lite.h"
39 #include "mipi-csis.h"
40
41 static int __fimc_md_set_camclk(struct fimc_md *fmd,
42 struct fimc_source_info *si,
43 bool on);
44
45 /* Set up image sensor subdev -> FIMC capture node notifications. */
46 static void __setup_sensor_notification(struct fimc_md *fmd,
47 struct v4l2_subdev *sensor,
48 struct v4l2_subdev *fimc_sd)
49 {
50 struct fimc_source_info *src_inf;
51 struct fimc_sensor_info *md_si;
52 unsigned long flags;
53
54 src_inf = v4l2_get_subdev_hostdata(sensor);
55 if (!src_inf || WARN_ON(fmd == NULL))
56 return;
57
58 md_si = source_to_sensor_info(src_inf);
59 spin_lock_irqsave(&fmd->slock, flags);
60 md_si->host = v4l2_get_subdevdata(fimc_sd);
61 spin_unlock_irqrestore(&fmd->slock, flags);
62 }
63
64 /**
65 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
66 * @me: media entity terminating the pipeline
67 *
68 * Caller holds the graph mutex.
69 */
70 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
71 struct media_entity *me)
72 {
73 struct fimc_md *fmd = entity_to_fimc_mdev(me);
74 struct v4l2_subdev *sd;
75 struct v4l2_subdev *sensor = NULL;
76 int i;
77
78 for (i = 0; i < IDX_MAX; i++)
79 p->subdevs[i] = NULL;
80
81 while (1) {
82 struct media_pad *pad = NULL;
83
84 /* Find remote source pad */
85 for (i = 0; i < me->num_pads; i++) {
86 struct media_pad *spad = &me->pads[i];
87 if (!(spad->flags & MEDIA_PAD_FL_SINK))
88 continue;
89 pad = media_entity_remote_pad(spad);
90 if (pad)
91 break;
92 }
93
94 if (pad == NULL ||
95 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
96 break;
97 sd = media_entity_to_v4l2_subdev(pad->entity);
98
99 switch (sd->grp_id) {
100 case GRP_ID_SENSOR:
101 sensor = sd;
102 /* fall through */
103 case GRP_ID_FIMC_IS_SENSOR:
104 p->subdevs[IDX_SENSOR] = sd;
105 break;
106 case GRP_ID_CSIS:
107 p->subdevs[IDX_CSIS] = sd;
108 break;
109 case GRP_ID_FLITE:
110 p->subdevs[IDX_FLITE] = sd;
111 break;
112 case GRP_ID_FIMC:
113 p->subdevs[IDX_FIMC] = sd;
114 break;
115 case GRP_ID_FIMC_IS:
116 p->subdevs[IDX_IS_ISP] = sd;
117 break;
118 default:
119 break;
120 }
121 me = &sd->entity;
122 if (me->num_pads == 1)
123 break;
124 }
125
126 if (sensor && p->subdevs[IDX_FIMC])
127 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
128 }
129
130 /**
131 * __subdev_set_power - change power state of a single subdev
132 * @sd: subdevice to change power state for
133 * @on: 1 to enable power or 0 to disable
134 *
135 * Return result of s_power subdev operation or -ENXIO if sd argument
136 * is NULL. Return 0 if the subdevice does not implement s_power.
137 */
138 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
139 {
140 int *use_count;
141 int ret;
142
143 if (sd == NULL)
144 return -ENXIO;
145
146 use_count = &sd->entity.use_count;
147 if (on && (*use_count)++ > 0)
148 return 0;
149 else if (!on && (*use_count == 0 || --(*use_count) > 0))
150 return 0;
151 ret = v4l2_subdev_call(sd, core, s_power, on);
152
153 return ret != -ENOIOCTLCMD ? ret : 0;
154 }
155
156 /**
157 * fimc_pipeline_s_power - change power state of all pipeline subdevs
158 * @fimc: fimc device terminating the pipeline
159 * @state: true to power on, false to power off
160 *
161 * Needs to be called with the graph mutex held.
162 */
163 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
164 {
165 static const u8 seq[2][IDX_MAX - 1] = {
166 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
167 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
168 };
169 int i, ret = 0;
170
171 if (p->subdevs[IDX_SENSOR] == NULL)
172 return -ENXIO;
173
174 for (i = 0; i < IDX_MAX - 1; i++) {
175 unsigned int idx = seq[on][i];
176
177 ret = __subdev_set_power(p->subdevs[idx], on);
178
179
180 if (ret < 0 && ret != -ENXIO)
181 goto error;
182 }
183 return 0;
184 error:
185 for (; i >= 0; i--) {
186 unsigned int idx = seq[on][i];
187 __subdev_set_power(p->subdevs[idx], !on);
188 }
189 return ret;
190 }
191
192 /**
193 * __fimc_pipeline_open - update the pipeline information, enable power
194 * of all pipeline subdevs and the sensor clock
195 * @me: media entity to start graph walk with
196 * @prepare: true to walk the current pipeline and acquire all subdevs
197 *
198 * Called with the graph mutex held.
199 */
200 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
201 struct media_entity *me, bool prepare)
202 {
203 struct fimc_md *fmd = entity_to_fimc_mdev(me);
204 struct fimc_pipeline *p = to_fimc_pipeline(ep);
205 struct v4l2_subdev *sd;
206 int ret;
207
208 if (WARN_ON(p == NULL || me == NULL))
209 return -EINVAL;
210
211 if (prepare)
212 fimc_pipeline_prepare(p, me);
213
214 sd = p->subdevs[IDX_SENSOR];
215 if (sd == NULL)
216 return -EINVAL;
217
218 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
219 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
220 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
221 if (ret < 0)
222 return ret;
223 }
224
225 ret = fimc_md_set_camclk(sd, true);
226 if (ret < 0)
227 goto err_wbclk;
228
229 ret = fimc_pipeline_s_power(p, 1);
230 if (!ret)
231 return 0;
232
233 fimc_md_set_camclk(sd, false);
234
235 err_wbclk:
236 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
237 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
238
239 return ret;
240 }
241
242 /**
243 * __fimc_pipeline_close - disable the sensor clock and pipeline power
244 * @fimc: fimc device terminating the pipeline
245 *
246 * Disable power of all subdevs and turn the external sensor clock off.
247 */
248 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
249 {
250 struct fimc_pipeline *p = to_fimc_pipeline(ep);
251 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
252 struct fimc_md *fmd;
253 int ret;
254
255 if (sd == NULL) {
256 pr_warn("%s(): No sensor subdev\n", __func__);
257 return 0;
258 }
259
260 ret = fimc_pipeline_s_power(p, 0);
261 fimc_md_set_camclk(sd, false);
262
263 fmd = entity_to_fimc_mdev(&sd->entity);
264
265 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
266 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
267 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
268
269 return ret == -ENXIO ? 0 : ret;
270 }
271
272 /**
273 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
274 * @pipeline: video pipeline structure
275 * @on: passed as the s_stream() callback argument
276 */
277 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
278 {
279 static const u8 seq[2][IDX_MAX] = {
280 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
281 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
282 };
283 struct fimc_pipeline *p = to_fimc_pipeline(ep);
284 int i, ret = 0;
285
286 if (p->subdevs[IDX_SENSOR] == NULL)
287 return -ENODEV;
288
289 for (i = 0; i < IDX_MAX; i++) {
290 unsigned int idx = seq[on][i];
291
292 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
293
294 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
295 goto error;
296 }
297 return 0;
298 error:
299 for (; i >= 0; i--) {
300 unsigned int idx = seq[on][i];
301 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
302 }
303 return ret;
304 }
305
306 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
307 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
308 .open = __fimc_pipeline_open,
309 .close = __fimc_pipeline_close,
310 .set_stream = __fimc_pipeline_s_stream,
311 };
312
313 static struct exynos_media_pipeline *fimc_md_pipeline_create(
314 struct fimc_md *fmd)
315 {
316 struct fimc_pipeline *p;
317
318 p = kzalloc(sizeof(*p), GFP_KERNEL);
319 if (!p)
320 return NULL;
321
322 list_add_tail(&p->list, &fmd->pipelines);
323
324 p->ep.ops = &fimc_pipeline_ops;
325 return &p->ep;
326 }
327
328 static void fimc_md_pipelines_free(struct fimc_md *fmd)
329 {
330 while (!list_empty(&fmd->pipelines)) {
331 struct fimc_pipeline *p;
332
333 p = list_entry(fmd->pipelines.next, typeof(*p), list);
334 list_del(&p->list);
335 kfree(p);
336 }
337 }
338
339 /*
340 * Sensor subdevice helper functions
341 */
342 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
343 struct fimc_source_info *si)
344 {
345 struct i2c_adapter *adapter;
346 struct v4l2_subdev *sd = NULL;
347
348 if (!si || !fmd)
349 return NULL;
350 /*
351 * If FIMC bus type is not Writeback FIFO assume it is same
352 * as sensor_bus_type.
353 */
354 si->fimc_bus_type = si->sensor_bus_type;
355
356 adapter = i2c_get_adapter(si->i2c_bus_num);
357 if (!adapter) {
358 v4l2_warn(&fmd->v4l2_dev,
359 "Failed to get I2C adapter %d, deferring probe\n",
360 si->i2c_bus_num);
361 return ERR_PTR(-EPROBE_DEFER);
362 }
363 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
364 si->board_info, NULL);
365 if (IS_ERR_OR_NULL(sd)) {
366 i2c_put_adapter(adapter);
367 v4l2_warn(&fmd->v4l2_dev,
368 "Failed to acquire subdev %s, deferring probe\n",
369 si->board_info->type);
370 return ERR_PTR(-EPROBE_DEFER);
371 }
372 v4l2_set_subdev_hostdata(sd, si);
373 sd->grp_id = GRP_ID_SENSOR;
374
375 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
376 sd->name);
377 return sd;
378 }
379
380 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
381 {
382 struct i2c_client *client = v4l2_get_subdevdata(sd);
383 struct i2c_adapter *adapter;
384
385 if (!client || client->dev.of_node)
386 return;
387
388 v4l2_device_unregister_subdev(sd);
389
390 adapter = client->adapter;
391 i2c_unregister_device(client);
392 if (adapter)
393 i2c_put_adapter(adapter);
394 }
395
396 #ifdef CONFIG_OF
397 /* Parse port node and register as a sub-device any sensor specified there. */
398 static int fimc_md_parse_port_node(struct fimc_md *fmd,
399 struct device_node *port,
400 unsigned int index)
401 {
402 struct device_node *rem, *ep, *np;
403 struct fimc_source_info *pd;
404 struct v4l2_of_endpoint endpoint;
405 u32 val;
406
407 pd = &fmd->sensor[index].pdata;
408
409 /* Assume here a port node can have only one endpoint node. */
410 ep = of_get_next_child(port, NULL);
411 if (!ep)
412 return 0;
413
414 v4l2_of_parse_endpoint(ep, &endpoint);
415 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
416 return -EINVAL;
417
418 pd->mux_id = (endpoint.port - 1) & 0x1;
419
420 rem = v4l2_of_get_remote_port_parent(ep);
421 of_node_put(ep);
422 if (rem == NULL) {
423 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
424 ep->full_name);
425 return 0;
426 }
427 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
428 pd->clk_id = val;
429
430 if (!of_property_read_u32(rem, "clock-frequency", &val))
431 pd->clk_frequency = val;
432 else
433 pd->clk_frequency = DEFAULT_SENSOR_CLK_FREQ;
434
435 if (pd->clk_frequency == 0) {
436 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
437 rem->full_name);
438 of_node_put(rem);
439 return -EINVAL;
440 }
441
442 if (fimc_input_is_parallel(endpoint.port)) {
443 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
444 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
445 else
446 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
447 pd->flags = endpoint.bus.parallel.flags;
448 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
449 /*
450 * MIPI CSI-2: only input mux selection and
451 * the sensor's clock frequency is needed.
452 */
453 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
454 } else {
455 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
456 endpoint.port, rem->full_name);
457 }
458 /*
459 * For FIMC-IS handled sensors, that are placed under i2c-isp device
460 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
461 * input. Sensors are attached to the FIMC-LITE hostdata interface
462 * directly or through MIPI-CSIS, depending on the external media bus
463 * used. This needs to be handled in a more reliable way, not by just
464 * checking parent's node name.
465 */
466 np = of_get_parent(rem);
467
468 if (np && !of_node_cmp(np->name, "i2c-isp"))
469 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
470 else
471 pd->fimc_bus_type = pd->sensor_bus_type;
472
473 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
474 return -EINVAL;
475
476 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
477 fmd->sensor[index].asd.match.of.node = rem;
478 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
479
480 fmd->num_sensors++;
481
482 of_node_put(rem);
483 return 0;
484 }
485
486 /* Register all SoC external sub-devices */
487 static int fimc_md_of_sensors_register(struct fimc_md *fmd,
488 struct device_node *np)
489 {
490 struct device_node *parent = fmd->pdev->dev.of_node;
491 struct device_node *node, *ports;
492 int index = 0;
493 int ret;
494
495 /* Attach sensors linked to MIPI CSI-2 receivers */
496 for_each_available_child_of_node(parent, node) {
497 struct device_node *port;
498
499 if (of_node_cmp(node->name, "csis"))
500 continue;
501 /* The csis node can have only port subnode. */
502 port = of_get_next_child(node, NULL);
503 if (!port)
504 continue;
505
506 ret = fimc_md_parse_port_node(fmd, port, index);
507 if (ret < 0)
508 return ret;
509 index++;
510 }
511
512 /* Attach sensors listed in the parallel-ports node */
513 ports = of_get_child_by_name(parent, "parallel-ports");
514 if (!ports)
515 return 0;
516
517 for_each_child_of_node(ports, node) {
518 ret = fimc_md_parse_port_node(fmd, node, index);
519 if (ret < 0)
520 break;
521 index++;
522 }
523
524 return 0;
525 }
526
527 static int __of_get_csis_id(struct device_node *np)
528 {
529 u32 reg = 0;
530
531 np = of_get_child_by_name(np, "port");
532 if (!np)
533 return -EINVAL;
534 of_property_read_u32(np, "reg", &reg);
535 return reg - FIMC_INPUT_MIPI_CSI2_0;
536 }
537 #else
538 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
539 #define __of_get_csis_id(np) (-ENOSYS)
540 #endif
541
542 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
543 {
544 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
545 struct device_node *of_node = fmd->pdev->dev.of_node;
546 int num_clients = 0;
547 int ret, i;
548
549 /*
550 * Runtime resume one of the FIMC entities to make sure
551 * the sclk_cam clocks are not globally disabled.
552 */
553 if (!fmd->pmf)
554 return -ENXIO;
555
556 ret = pm_runtime_get_sync(fmd->pmf);
557 if (ret < 0)
558 return ret;
559
560 if (of_node) {
561 fmd->num_sensors = 0;
562 ret = fimc_md_of_sensors_register(fmd, of_node);
563 } else if (pdata) {
564 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
565 num_clients = min_t(u32, pdata->num_clients,
566 ARRAY_SIZE(fmd->sensor));
567 fmd->num_sensors = num_clients;
568
569 for (i = 0; i < num_clients; i++) {
570 struct fimc_sensor_info *si = &fmd->sensor[i];
571 struct v4l2_subdev *sd;
572
573 si->pdata = pdata->source_info[i];
574 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
575 if (ret)
576 break;
577 sd = fimc_md_register_sensor(fmd, &si->pdata);
578 ret = __fimc_md_set_camclk(fmd, &si->pdata, false);
579
580 if (IS_ERR(sd)) {
581 si->subdev = NULL;
582 ret = PTR_ERR(sd);
583 break;
584 }
585 si->subdev = sd;
586 if (ret)
587 break;
588 }
589 }
590
591 pm_runtime_put(fmd->pmf);
592 return ret;
593 }
594
595 /*
596 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
597 */
598
599 static int register_fimc_lite_entity(struct fimc_md *fmd,
600 struct fimc_lite *fimc_lite)
601 {
602 struct v4l2_subdev *sd;
603 struct exynos_media_pipeline *ep;
604 int ret;
605
606 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
607 fmd->fimc_lite[fimc_lite->index]))
608 return -EBUSY;
609
610 sd = &fimc_lite->subdev;
611 sd->grp_id = GRP_ID_FLITE;
612
613 ep = fimc_md_pipeline_create(fmd);
614 if (!ep)
615 return -ENOMEM;
616
617 v4l2_set_subdev_hostdata(sd, ep);
618
619 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
620 if (!ret)
621 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
622 else
623 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
624 fimc_lite->index);
625 return ret;
626 }
627
628 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
629 {
630 struct v4l2_subdev *sd;
631 struct exynos_media_pipeline *ep;
632 int ret;
633
634 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
635 return -EBUSY;
636
637 sd = &fimc->vid_cap.subdev;
638 sd->grp_id = GRP_ID_FIMC;
639
640 ep = fimc_md_pipeline_create(fmd);
641 if (!ep)
642 return -ENOMEM;
643
644 v4l2_set_subdev_hostdata(sd, ep);
645
646 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
647 if (!ret) {
648 if (!fmd->pmf && fimc->pdev)
649 fmd->pmf = &fimc->pdev->dev;
650 fmd->fimc[fimc->id] = fimc;
651 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
652 } else {
653 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
654 fimc->id, ret);
655 }
656 return ret;
657 }
658
659 static int register_csis_entity(struct fimc_md *fmd,
660 struct platform_device *pdev,
661 struct v4l2_subdev *sd)
662 {
663 struct device_node *node = pdev->dev.of_node;
664 int id, ret;
665
666 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
667
668 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
669 return -ENOENT;
670
671 if (WARN_ON(fmd->csis[id].sd))
672 return -EBUSY;
673
674 sd->grp_id = GRP_ID_CSIS;
675 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
676 if (!ret)
677 fmd->csis[id].sd = sd;
678 else
679 v4l2_err(&fmd->v4l2_dev,
680 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
681 return ret;
682 }
683
684 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
685 {
686 struct v4l2_subdev *sd = &is->isp.subdev;
687 struct exynos_media_pipeline *ep;
688 int ret;
689
690 /* Allocate pipeline object for the ISP capture video node. */
691 ep = fimc_md_pipeline_create(fmd);
692 if (!ep)
693 return -ENOMEM;
694
695 v4l2_set_subdev_hostdata(sd, ep);
696
697 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
698 if (ret) {
699 v4l2_err(&fmd->v4l2_dev,
700 "Failed to register FIMC-ISP (%d)\n", ret);
701 return ret;
702 }
703
704 fmd->fimc_is = is;
705 return 0;
706 }
707
708 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
709 struct platform_device *pdev,
710 int plat_entity)
711 {
712 struct device *dev = &pdev->dev;
713 int ret = -EPROBE_DEFER;
714 void *drvdata;
715
716 /* Lock to ensure dev->driver won't change. */
717 device_lock(dev);
718
719 if (!dev->driver || !try_module_get(dev->driver->owner))
720 goto dev_unlock;
721
722 drvdata = dev_get_drvdata(dev);
723 /* Some subdev didn't probe successfully id drvdata is NULL */
724 if (drvdata) {
725 switch (plat_entity) {
726 case IDX_FIMC:
727 ret = register_fimc_entity(fmd, drvdata);
728 break;
729 case IDX_FLITE:
730 ret = register_fimc_lite_entity(fmd, drvdata);
731 break;
732 case IDX_CSIS:
733 ret = register_csis_entity(fmd, pdev, drvdata);
734 break;
735 case IDX_IS_ISP:
736 ret = register_fimc_is_entity(fmd, drvdata);
737 break;
738 default:
739 ret = -ENODEV;
740 }
741 }
742
743 module_put(dev->driver->owner);
744 dev_unlock:
745 device_unlock(dev);
746 if (ret == -EPROBE_DEFER)
747 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
748 dev_name(dev));
749 else if (ret < 0)
750 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
751 dev_name(dev), ret);
752 return ret;
753 }
754
755 static int fimc_md_pdev_match(struct device *dev, void *data)
756 {
757 struct platform_device *pdev = to_platform_device(dev);
758 int plat_entity = -1;
759 int ret;
760 char *p;
761
762 if (!get_device(dev))
763 return -ENODEV;
764
765 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
766 plat_entity = IDX_CSIS;
767 } else {
768 p = strstr(pdev->name, "fimc");
769 if (p && *(p + 4) == 0)
770 plat_entity = IDX_FIMC;
771 }
772
773 if (plat_entity >= 0)
774 ret = fimc_md_register_platform_entity(data, pdev,
775 plat_entity);
776 put_device(dev);
777 return 0;
778 }
779
780 /* Register FIMC, FIMC-LITE and CSIS media entities */
781 #ifdef CONFIG_OF
782 static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
783 struct device_node *parent)
784 {
785 struct device_node *node;
786 int ret = 0;
787
788 for_each_available_child_of_node(parent, node) {
789 struct platform_device *pdev;
790 int plat_entity = -1;
791
792 pdev = of_find_device_by_node(node);
793 if (!pdev)
794 continue;
795
796 /* If driver of any entity isn't ready try all again later. */
797 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
798 plat_entity = IDX_CSIS;
799 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
800 plat_entity = IDX_IS_ISP;
801 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
802 plat_entity = IDX_FLITE;
803 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
804 !of_property_read_bool(node, "samsung,lcd-wb"))
805 plat_entity = IDX_FIMC;
806
807 if (plat_entity >= 0)
808 ret = fimc_md_register_platform_entity(fmd, pdev,
809 plat_entity);
810 put_device(&pdev->dev);
811 if (ret < 0)
812 break;
813 }
814
815 return ret;
816 }
817 #else
818 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
819 #endif
820
821 static void fimc_md_unregister_entities(struct fimc_md *fmd)
822 {
823 int i;
824
825 for (i = 0; i < FIMC_MAX_DEVS; i++) {
826 struct fimc_dev *dev = fmd->fimc[i];
827 if (dev == NULL)
828 continue;
829 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
830 dev->vid_cap.ve.pipe = NULL;
831 fmd->fimc[i] = NULL;
832 }
833 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
834 struct fimc_lite *dev = fmd->fimc_lite[i];
835 if (dev == NULL)
836 continue;
837 v4l2_device_unregister_subdev(&dev->subdev);
838 dev->ve.pipe = NULL;
839 fmd->fimc_lite[i] = NULL;
840 }
841 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
842 if (fmd->csis[i].sd == NULL)
843 continue;
844 v4l2_device_unregister_subdev(fmd->csis[i].sd);
845 fmd->csis[i].sd = NULL;
846 }
847 if (fmd->pdev->dev.of_node == NULL) {
848 for (i = 0; i < fmd->num_sensors; i++) {
849 if (fmd->sensor[i].subdev == NULL)
850 continue;
851 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
852 fmd->sensor[i].subdev = NULL;
853 }
854 }
855
856 if (fmd->fimc_is)
857 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
858
859 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
860 }
861
862 /**
863 * __fimc_md_create_fimc_links - create links to all FIMC entities
864 * @fmd: fimc media device
865 * @source: the source entity to create links to all fimc entities from
866 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
867 * @pad: the source entity pad index
868 * @link_mask: bitmask of the fimc devices for which link should be enabled
869 */
870 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
871 struct media_entity *source,
872 struct v4l2_subdev *sensor,
873 int pad, int link_mask)
874 {
875 struct fimc_source_info *si = NULL;
876 struct media_entity *sink;
877 unsigned int flags = 0;
878 int i, ret = 0;
879
880 if (sensor) {
881 si = v4l2_get_subdev_hostdata(sensor);
882 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
883 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
884 ret = 1;
885 }
886
887 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
888 if (!fmd->fimc[i])
889 continue;
890 /*
891 * Some FIMC variants are not fitted with camera capture
892 * interface. Skip creating a link from sensor for those.
893 */
894 if (!fmd->fimc[i]->variant->has_cam_if)
895 continue;
896
897 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
898
899 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
900 ret = media_entity_create_link(source, pad, sink,
901 FIMC_SD_PAD_SINK_CAM, flags);
902 if (ret)
903 return ret;
904
905 /* Notify FIMC capture subdev entity */
906 ret = media_entity_call(sink, link_setup, &sink->pads[0],
907 &source->pads[pad], flags);
908 if (ret)
909 break;
910
911 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
912 source->name, flags ? '=' : '-', sink->name);
913 }
914
915 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
916 if (!fmd->fimc_lite[i])
917 continue;
918
919 sink = &fmd->fimc_lite[i]->subdev.entity;
920 ret = media_entity_create_link(source, pad, sink,
921 FLITE_SD_PAD_SINK, 0);
922 if (ret)
923 return ret;
924
925 /* Notify FIMC-LITE subdev entity */
926 ret = media_entity_call(sink, link_setup, &sink->pads[0],
927 &source->pads[pad], 0);
928 if (ret)
929 break;
930
931 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
932 source->name, sink->name);
933 }
934 return 0;
935 }
936
937 /* Create links from FIMC-LITE source pads to other entities */
938 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
939 {
940 struct media_entity *source, *sink;
941 int i, ret = 0;
942
943 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
944 struct fimc_lite *fimc = fmd->fimc_lite[i];
945
946 if (fimc == NULL)
947 continue;
948
949 source = &fimc->subdev.entity;
950 sink = &fimc->ve.vdev.entity;
951 /* FIMC-LITE's subdev and video node */
952 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
953 sink, 0, 0);
954 if (ret)
955 break;
956 /* Link from FIMC-LITE to IS-ISP subdev */
957 sink = &fmd->fimc_is->isp.subdev.entity;
958 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
959 sink, 0, 0);
960 if (ret)
961 break;
962 }
963
964 return ret;
965 }
966
967 /* Create FIMC-IS links */
968 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
969 {
970 struct fimc_isp *isp = &fmd->fimc_is->isp;
971 struct media_entity *source, *sink;
972 int i, ret;
973
974 source = &isp->subdev.entity;
975
976 for (i = 0; i < FIMC_MAX_DEVS; i++) {
977 if (fmd->fimc[i] == NULL)
978 continue;
979
980 /* Link from FIMC-IS-ISP subdev to FIMC */
981 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
982 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
983 sink, FIMC_SD_PAD_SINK_FIFO, 0);
984 if (ret)
985 return ret;
986 }
987
988 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
989 sink = &isp->video_capture.ve.vdev.entity;
990
991 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
992 if (sink->num_pads == 0)
993 return 0;
994
995 return media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
996 sink, 0, 0);
997 }
998
999 /**
1000 * fimc_md_create_links - create default links between registered entities
1001 *
1002 * Parallel interface sensor entities are connected directly to FIMC capture
1003 * entities. The sensors using MIPI CSIS bus are connected through immutable
1004 * link with CSI receiver entity specified by mux_id. Any registered CSIS
1005 * entity has a link to each registered FIMC capture entity. Enabled links
1006 * are created by default between each subsequent registered sensor and
1007 * subsequent FIMC capture entity. The number of default active links is
1008 * determined by the number of available sensors or FIMC entities,
1009 * whichever is less.
1010 */
1011 static int fimc_md_create_links(struct fimc_md *fmd)
1012 {
1013 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
1014 struct v4l2_subdev *sensor, *csis;
1015 struct fimc_source_info *pdata;
1016 struct media_entity *source, *sink;
1017 int i, pad, fimc_id = 0, ret = 0;
1018 u32 flags, link_mask = 0;
1019
1020 for (i = 0; i < fmd->num_sensors; i++) {
1021 if (fmd->sensor[i].subdev == NULL)
1022 continue;
1023
1024 sensor = fmd->sensor[i].subdev;
1025 pdata = v4l2_get_subdev_hostdata(sensor);
1026 if (!pdata)
1027 continue;
1028
1029 source = NULL;
1030
1031 switch (pdata->sensor_bus_type) {
1032 case FIMC_BUS_TYPE_MIPI_CSI2:
1033 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
1034 "Wrong CSI channel id: %d\n", pdata->mux_id))
1035 return -EINVAL;
1036
1037 csis = fmd->csis[pdata->mux_id].sd;
1038 if (WARN(csis == NULL,
1039 "MIPI-CSI interface specified "
1040 "but s5p-csis module is not loaded!\n"))
1041 return -EINVAL;
1042
1043 pad = sensor->entity.num_pads - 1;
1044 ret = media_entity_create_link(&sensor->entity, pad,
1045 &csis->entity, CSIS_PAD_SINK,
1046 MEDIA_LNK_FL_IMMUTABLE |
1047 MEDIA_LNK_FL_ENABLED);
1048 if (ret)
1049 return ret;
1050
1051 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
1052 sensor->entity.name, csis->entity.name);
1053
1054 source = NULL;
1055 csi_sensors[pdata->mux_id] = sensor;
1056 break;
1057
1058 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1059 source = &sensor->entity;
1060 pad = 0;
1061 break;
1062
1063 default:
1064 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1065 pdata->sensor_bus_type);
1066 return -EINVAL;
1067 }
1068 if (source == NULL)
1069 continue;
1070
1071 link_mask = 1 << fimc_id++;
1072 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1073 pad, link_mask);
1074 }
1075
1076 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1077 if (fmd->csis[i].sd == NULL)
1078 continue;
1079
1080 source = &fmd->csis[i].sd->entity;
1081 pad = CSIS_PAD_SOURCE;
1082 sensor = csi_sensors[i];
1083
1084 link_mask = 1 << fimc_id++;
1085 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1086 pad, link_mask);
1087 }
1088
1089 /* Create immutable links between each FIMC's subdev and video node */
1090 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1091 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1092 if (!fmd->fimc[i])
1093 continue;
1094
1095 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1096 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1097
1098 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1099 sink, 0, flags);
1100 if (ret)
1101 break;
1102 }
1103
1104 ret = __fimc_md_create_flite_source_links(fmd);
1105 if (ret < 0)
1106 return ret;
1107
1108 if (fmd->use_isp)
1109 ret = __fimc_md_create_fimc_is_links(fmd);
1110
1111 return ret;
1112 }
1113
1114 /*
1115 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1116 */
1117 static void fimc_md_put_clocks(struct fimc_md *fmd)
1118 {
1119 int i = FIMC_MAX_CAMCLKS;
1120
1121 while (--i >= 0) {
1122 if (IS_ERR(fmd->camclk[i].clock))
1123 continue;
1124 clk_put(fmd->camclk[i].clock);
1125 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1126 }
1127
1128 /* Writeback (PIXELASYNCMx) clocks */
1129 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1130 if (IS_ERR(fmd->wbclk[i]))
1131 continue;
1132 clk_put(fmd->wbclk[i]);
1133 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1134 }
1135 }
1136
1137 static int fimc_md_get_clocks(struct fimc_md *fmd)
1138 {
1139 struct device *dev = NULL;
1140 char clk_name[32];
1141 struct clk *clock;
1142 int i, ret = 0;
1143
1144 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1145 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1146
1147 if (fmd->pdev->dev.of_node)
1148 dev = &fmd->pdev->dev;
1149
1150 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1151 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1152 clock = clk_get(dev, clk_name);
1153
1154 if (IS_ERR(clock)) {
1155 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1156 clk_name);
1157 ret = PTR_ERR(clock);
1158 break;
1159 }
1160 fmd->camclk[i].clock = clock;
1161 }
1162 if (ret)
1163 fimc_md_put_clocks(fmd);
1164
1165 if (!fmd->use_isp)
1166 return 0;
1167 /*
1168 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1169 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1170 */
1171 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1172
1173 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1174 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1175 clock = clk_get(dev, clk_name);
1176 if (IS_ERR(clock)) {
1177 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1178 clk_name);
1179 ret = PTR_ERR(clock);
1180 break;
1181 }
1182 fmd->wbclk[i] = clock;
1183 }
1184 if (ret)
1185 fimc_md_put_clocks(fmd);
1186
1187 return ret;
1188 }
1189
1190 static int __fimc_md_set_camclk(struct fimc_md *fmd,
1191 struct fimc_source_info *si,
1192 bool on)
1193 {
1194 struct fimc_camclk_info *camclk;
1195 int ret = 0;
1196
1197 /*
1198 * When device tree is used the sensor drivers are supposed to
1199 * control the clock themselves. This whole function will be
1200 * removed once S5PV210 platform is converted to the device tree.
1201 */
1202 if (fmd->pdev->dev.of_node)
1203 return 0;
1204
1205 if (WARN_ON(si->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
1206 return -EINVAL;
1207
1208 camclk = &fmd->camclk[si->clk_id];
1209
1210 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1211 si->clk_id, si->clk_frequency, camclk->use_count, on);
1212
1213 if (on) {
1214 if (camclk->use_count > 0 &&
1215 camclk->frequency != si->clk_frequency)
1216 return -EINVAL;
1217
1218 if (camclk->use_count++ == 0) {
1219 clk_set_rate(camclk->clock, si->clk_frequency);
1220 camclk->frequency = si->clk_frequency;
1221 ret = pm_runtime_get_sync(fmd->pmf);
1222 if (ret < 0)
1223 return ret;
1224 ret = clk_prepare_enable(camclk->clock);
1225 dbg("Enabled camclk %d: f: %lu", si->clk_id,
1226 clk_get_rate(camclk->clock));
1227 }
1228 return ret;
1229 }
1230
1231 if (WARN_ON(camclk->use_count == 0))
1232 return 0;
1233
1234 if (--camclk->use_count == 0) {
1235 clk_disable_unprepare(camclk->clock);
1236 pm_runtime_put(fmd->pmf);
1237 dbg("Disabled camclk %d", si->clk_id);
1238 }
1239 return ret;
1240 }
1241
1242 /**
1243 * fimc_md_set_camclk - peripheral sensor clock setup
1244 * @sd: sensor subdev to configure sclk_cam clock for
1245 * @on: 1 to enable or 0 to disable the clock
1246 *
1247 * There are 2 separate clock outputs available in the SoC for external
1248 * image processors. These clocks are shared between all registered FIMC
1249 * devices to which sensors can be attached, either directly or through
1250 * the MIPI CSI receiver. The clock is allowed here to be used by
1251 * multiple sensors concurrently if they use same frequency.
1252 * This function should only be called when the graph mutex is held.
1253 */
1254 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1255 {
1256 struct fimc_source_info *si = v4l2_get_subdev_hostdata(sd);
1257 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1258
1259 /*
1260 * If there is a clock provider registered the sensors will
1261 * handle their clock themselves, no need to control it on
1262 * the host interface side.
1263 */
1264 if (fmd->clk_provider.num_clocks > 0)
1265 return 0;
1266
1267 return __fimc_md_set_camclk(fmd, si, on);
1268 }
1269
1270 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1271 {
1272 struct exynos_video_entity *ve;
1273 struct fimc_pipeline *p;
1274 struct video_device *vdev;
1275 int ret;
1276
1277 vdev = media_entity_to_video_device(entity);
1278 if (vdev->entity.use_count == 0)
1279 return 0;
1280
1281 ve = vdev_to_exynos_video_entity(vdev);
1282 p = to_fimc_pipeline(ve->pipe);
1283 /*
1284 * Nothing to do if we are disabling the pipeline, some link
1285 * has been disconnected and p->subdevs array is cleared now.
1286 */
1287 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1288 return 0;
1289
1290 if (enable)
1291 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1292 else
1293 ret = __fimc_pipeline_close(ve->pipe);
1294
1295 if (ret == 0 && !enable)
1296 memset(p->subdevs, 0, sizeof(p->subdevs));
1297
1298 return ret;
1299 }
1300
1301 /* Locking: called with entity->parent->graph_mutex mutex held. */
1302 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1303 {
1304 struct media_entity *entity_err = entity;
1305 struct media_entity_graph graph;
1306 int ret;
1307
1308 /*
1309 * Walk current graph and call the pipeline open/close routine for each
1310 * opened video node that belongs to the graph of entities connected
1311 * through active links. This is needed as we cannot power on/off the
1312 * subdevs in random order.
1313 */
1314 media_entity_graph_walk_start(&graph, entity);
1315
1316 while ((entity = media_entity_graph_walk_next(&graph))) {
1317 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1318 continue;
1319
1320 ret = __fimc_md_modify_pipeline(entity, enable);
1321
1322 if (ret < 0)
1323 goto err;
1324 }
1325
1326 return 0;
1327 err:
1328 media_entity_graph_walk_start(&graph, entity_err);
1329
1330 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1331 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1332 continue;
1333
1334 __fimc_md_modify_pipeline(entity_err, !enable);
1335
1336 if (entity_err == entity)
1337 break;
1338 }
1339
1340 return ret;
1341 }
1342
1343 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1344 unsigned int notification)
1345 {
1346 struct media_entity *sink = link->sink->entity;
1347 int ret = 0;
1348
1349 /* Before link disconnection */
1350 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1351 if (!(flags & MEDIA_LNK_FL_ENABLED))
1352 ret = __fimc_md_modify_pipelines(sink, false);
1353 else
1354 ; /* TODO: Link state change validation */
1355 /* After link activation */
1356 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1357 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1358 ret = __fimc_md_modify_pipelines(sink, true);
1359 }
1360
1361 return ret ? -EPIPE : 0;
1362 }
1363
1364 static ssize_t fimc_md_sysfs_show(struct device *dev,
1365 struct device_attribute *attr, char *buf)
1366 {
1367 struct platform_device *pdev = to_platform_device(dev);
1368 struct fimc_md *fmd = platform_get_drvdata(pdev);
1369
1370 if (fmd->user_subdev_api)
1371 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1372
1373 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1374 }
1375
1376 static ssize_t fimc_md_sysfs_store(struct device *dev,
1377 struct device_attribute *attr,
1378 const char *buf, size_t count)
1379 {
1380 struct platform_device *pdev = to_platform_device(dev);
1381 struct fimc_md *fmd = platform_get_drvdata(pdev);
1382 bool subdev_api;
1383 int i;
1384
1385 if (!strcmp(buf, "vid-dev\n"))
1386 subdev_api = false;
1387 else if (!strcmp(buf, "sub-dev\n"))
1388 subdev_api = true;
1389 else
1390 return count;
1391
1392 fmd->user_subdev_api = subdev_api;
1393 for (i = 0; i < FIMC_MAX_DEVS; i++)
1394 if (fmd->fimc[i])
1395 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1396 return count;
1397 }
1398 /*
1399 * This device attribute is to select video pipeline configuration method.
1400 * There are following valid values:
1401 * vid-dev - for V4L2 video node API only, subdevice will be configured
1402 * by the host driver.
1403 * sub-dev - for media controller API, subdevs must be configured in user
1404 * space before starting streaming.
1405 */
1406 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1407 fimc_md_sysfs_show, fimc_md_sysfs_store);
1408
1409 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1410 {
1411 struct device *dev = &fmd->pdev->dev;
1412 struct fimc_pinctrl *pctl = &fmd->pinctl;
1413
1414 pctl->pinctrl = devm_pinctrl_get(dev);
1415 if (IS_ERR(pctl->pinctrl))
1416 return PTR_ERR(pctl->pinctrl);
1417
1418 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1419 PINCTRL_STATE_DEFAULT);
1420 if (IS_ERR(pctl->state_default))
1421 return PTR_ERR(pctl->state_default);
1422
1423 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1424 PINCTRL_STATE_IDLE);
1425 return 0;
1426 }
1427
1428 #ifdef CONFIG_OF
1429 static int cam_clk_prepare(struct clk_hw *hw)
1430 {
1431 struct cam_clk *camclk = to_cam_clk(hw);
1432 int ret;
1433
1434 if (camclk->fmd->pmf == NULL)
1435 return -ENODEV;
1436
1437 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1438 return ret < 0 ? ret : 0;
1439 }
1440
1441 static void cam_clk_unprepare(struct clk_hw *hw)
1442 {
1443 struct cam_clk *camclk = to_cam_clk(hw);
1444
1445 if (camclk->fmd->pmf == NULL)
1446 return;
1447
1448 pm_runtime_put_sync(camclk->fmd->pmf);
1449 }
1450
1451 static const struct clk_ops cam_clk_ops = {
1452 .prepare = cam_clk_prepare,
1453 .unprepare = cam_clk_unprepare,
1454 };
1455
1456 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1457 {
1458 struct cam_clk_provider *cp = &fmd->clk_provider;
1459 unsigned int i;
1460
1461 if (cp->of_node)
1462 of_clk_del_provider(cp->of_node);
1463
1464 for (i = 0; i < cp->num_clocks; i++)
1465 clk_unregister(cp->clks[i]);
1466 }
1467
1468 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1469 {
1470 struct cam_clk_provider *cp = &fmd->clk_provider;
1471 struct device *dev = &fmd->pdev->dev;
1472 int i, ret;
1473
1474 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1475 struct cam_clk *camclk = &cp->camclk[i];
1476 struct clk_init_data init;
1477 const char *p_name;
1478
1479 ret = of_property_read_string_index(dev->of_node,
1480 "clock-output-names", i, &init.name);
1481 if (ret < 0)
1482 break;
1483
1484 p_name = __clk_get_name(fmd->camclk[i].clock);
1485
1486 /* It's safe since clk_register() will duplicate the string. */
1487 init.parent_names = &p_name;
1488 init.num_parents = 1;
1489 init.ops = &cam_clk_ops;
1490 init.flags = CLK_SET_RATE_PARENT;
1491 camclk->hw.init = &init;
1492 camclk->fmd = fmd;
1493
1494 cp->clks[i] = clk_register(NULL, &camclk->hw);
1495 if (IS_ERR(cp->clks[i])) {
1496 dev_err(dev, "failed to register clock: %s (%ld)\n",
1497 init.name, PTR_ERR(cp->clks[i]));
1498 ret = PTR_ERR(cp->clks[i]);
1499 goto err;
1500 }
1501 cp->num_clocks++;
1502 }
1503
1504 if (cp->num_clocks == 0) {
1505 dev_warn(dev, "clk provider not registered\n");
1506 return 0;
1507 }
1508
1509 cp->clk_data.clks = cp->clks;
1510 cp->clk_data.clk_num = cp->num_clocks;
1511 cp->of_node = dev->of_node;
1512 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1513 &cp->clk_data);
1514 if (ret == 0)
1515 return 0;
1516 err:
1517 fimc_md_unregister_clk_provider(fmd);
1518 return ret;
1519 }
1520 #else
1521 #define fimc_md_register_clk_provider(fmd) (0)
1522 #define fimc_md_unregister_clk_provider(fmd) (0)
1523 #endif
1524
1525 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1526 struct v4l2_subdev *subdev,
1527 struct v4l2_async_subdev *asd)
1528 {
1529 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1530 struct fimc_sensor_info *si = NULL;
1531 int i;
1532
1533 /* Find platform data for this sensor subdev */
1534 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1535 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1536 si = &fmd->sensor[i];
1537
1538 if (si == NULL)
1539 return -EINVAL;
1540
1541 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1542
1543 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1544 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1545 else
1546 subdev->grp_id = GRP_ID_SENSOR;
1547
1548 si->subdev = subdev;
1549
1550 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1551 subdev->name, fmd->num_sensors);
1552
1553 fmd->num_sensors++;
1554
1555 return 0;
1556 }
1557
1558 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1559 {
1560 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1561 int ret;
1562
1563 mutex_lock(&fmd->media_dev.graph_mutex);
1564
1565 ret = fimc_md_create_links(fmd);
1566 if (ret < 0)
1567 goto unlock;
1568
1569 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1570 unlock:
1571 mutex_unlock(&fmd->media_dev.graph_mutex);
1572 return ret;
1573 }
1574
1575 static int fimc_md_probe(struct platform_device *pdev)
1576 {
1577 struct device *dev = &pdev->dev;
1578 struct v4l2_device *v4l2_dev;
1579 struct fimc_md *fmd;
1580 int ret;
1581
1582 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1583 if (!fmd)
1584 return -ENOMEM;
1585
1586 spin_lock_init(&fmd->slock);
1587 fmd->pdev = pdev;
1588 INIT_LIST_HEAD(&fmd->pipelines);
1589
1590 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1591 sizeof(fmd->media_dev.model));
1592 fmd->media_dev.link_notify = fimc_md_link_notify;
1593 fmd->media_dev.dev = dev;
1594
1595 v4l2_dev = &fmd->v4l2_dev;
1596 v4l2_dev->mdev = &fmd->media_dev;
1597 v4l2_dev->notify = fimc_sensor_notify;
1598 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1599
1600 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1601
1602 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1603 if (ret < 0) {
1604 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1605 return ret;
1606 }
1607
1608 ret = media_device_register(&fmd->media_dev);
1609 if (ret < 0) {
1610 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1611 goto err_v4l2_dev;
1612 }
1613
1614 ret = fimc_md_get_clocks(fmd);
1615 if (ret)
1616 goto err_md;
1617
1618 fmd->user_subdev_api = (dev->of_node != NULL);
1619
1620 ret = fimc_md_get_pinctrl(fmd);
1621 if (ret < 0) {
1622 if (ret != EPROBE_DEFER)
1623 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1624 goto err_clk;
1625 }
1626
1627 platform_set_drvdata(pdev, fmd);
1628
1629 /* Protect the media graph while we're registering entities */
1630 mutex_lock(&fmd->media_dev.graph_mutex);
1631
1632 if (dev->of_node)
1633 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1634 else
1635 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1636 fimc_md_pdev_match);
1637 if (ret) {
1638 mutex_unlock(&fmd->media_dev.graph_mutex);
1639 goto err_clk;
1640 }
1641
1642 if (dev->platform_data || dev->of_node) {
1643 ret = fimc_md_register_sensor_entities(fmd);
1644 if (ret) {
1645 mutex_unlock(&fmd->media_dev.graph_mutex);
1646 goto err_m_ent;
1647 }
1648 }
1649
1650 mutex_unlock(&fmd->media_dev.graph_mutex);
1651
1652 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1653 if (ret)
1654 goto err_m_ent;
1655 /*
1656 * FIMC platform devices need to be registered before the sclk_cam
1657 * clocks provider, as one of these devices needs to be activated
1658 * to enable the clock.
1659 */
1660 ret = fimc_md_register_clk_provider(fmd);
1661 if (ret < 0) {
1662 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1663 goto err_attr;
1664 }
1665
1666 if (fmd->num_sensors > 0) {
1667 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1668 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1669 fmd->subdev_notifier.bound = subdev_notifier_bound;
1670 fmd->subdev_notifier.complete = subdev_notifier_complete;
1671 fmd->num_sensors = 0;
1672
1673 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1674 &fmd->subdev_notifier);
1675 if (ret)
1676 goto err_clk_p;
1677 }
1678
1679 return 0;
1680
1681 err_clk_p:
1682 fimc_md_unregister_clk_provider(fmd);
1683 err_attr:
1684 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1685 err_clk:
1686 fimc_md_put_clocks(fmd);
1687 err_m_ent:
1688 fimc_md_unregister_entities(fmd);
1689 err_md:
1690 media_device_unregister(&fmd->media_dev);
1691 err_v4l2_dev:
1692 v4l2_device_unregister(&fmd->v4l2_dev);
1693 return ret;
1694 }
1695
1696 static int fimc_md_remove(struct platform_device *pdev)
1697 {
1698 struct fimc_md *fmd = platform_get_drvdata(pdev);
1699
1700 if (!fmd)
1701 return 0;
1702
1703 fimc_md_unregister_clk_provider(fmd);
1704 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1705
1706 v4l2_device_unregister(&fmd->v4l2_dev);
1707 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1708 fimc_md_unregister_entities(fmd);
1709 fimc_md_pipelines_free(fmd);
1710 media_device_unregister(&fmd->media_dev);
1711 fimc_md_put_clocks(fmd);
1712
1713 return 0;
1714 }
1715
1716 static struct platform_device_id fimc_driver_ids[] __always_unused = {
1717 { .name = "s5p-fimc-md" },
1718 { },
1719 };
1720 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1721
1722 static const struct of_device_id fimc_md_of_match[] = {
1723 { .compatible = "samsung,fimc" },
1724 { },
1725 };
1726 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1727
1728 static struct platform_driver fimc_md_driver = {
1729 .probe = fimc_md_probe,
1730 .remove = fimc_md_remove,
1731 .driver = {
1732 .of_match_table = of_match_ptr(fimc_md_of_match),
1733 .name = "s5p-fimc-md",
1734 .owner = THIS_MODULE,
1735 }
1736 };
1737
1738 static int __init fimc_md_init(void)
1739 {
1740 int ret;
1741
1742 request_module("s5p-csis");
1743 ret = fimc_register_driver();
1744 if (ret)
1745 return ret;
1746
1747 return platform_driver_register(&fimc_md_driver);
1748 }
1749
1750 static void __exit fimc_md_exit(void)
1751 {
1752 platform_driver_unregister(&fimc_md_driver);
1753 fimc_unregister_driver();
1754 }
1755
1756 module_init(fimc_md_init);
1757 module_exit(fimc_md_exit);
1758
1759 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1760 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1761 MODULE_LICENSE("GPL");
1762 MODULE_VERSION("2.0.1");
This page took 0.073828 seconds and 5 git commands to generate.