5e96775fca8dc2d6ceea888f582edd8c66fd8e53
[deliverable/linux.git] / drivers / media / platform / s5p-fimc / fimc-mdevice.c
1 /*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
3 *
4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
11 */
12
13 #include <linux/bug.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_device.h>
23 #include <linux/of_i2c.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-of.h>
30 #include <media/media-device.h>
31 #include <media/s5p_fimc.h>
32
33 #include "fimc-core.h"
34 #include "fimc-lite.h"
35 #include "fimc-mdevice.h"
36 #include "mipi-csis.h"
37
38 static int __fimc_md_set_camclk(struct fimc_md *fmd,
39 struct fimc_sensor_info *s_info,
40 bool on);
41 /**
42 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
43 * @fimc: fimc device terminating the pipeline
44 *
45 * Caller holds the graph mutex.
46 */
47 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
48 struct media_entity *me)
49 {
50 struct media_pad *pad = &me->pads[0];
51 struct v4l2_subdev *sd;
52 int i;
53
54 for (i = 0; i < IDX_MAX; i++)
55 p->subdevs[i] = NULL;
56
57 while (1) {
58 if (!(pad->flags & MEDIA_PAD_FL_SINK))
59 break;
60
61 /* source pad */
62 pad = media_entity_remote_source(pad);
63 if (pad == NULL ||
64 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
65 break;
66
67 sd = media_entity_to_v4l2_subdev(pad->entity);
68
69 switch (sd->grp_id) {
70 case GRP_ID_FIMC_IS_SENSOR:
71 case GRP_ID_SENSOR:
72 p->subdevs[IDX_SENSOR] = sd;
73 break;
74 case GRP_ID_CSIS:
75 p->subdevs[IDX_CSIS] = sd;
76 break;
77 case GRP_ID_FLITE:
78 p->subdevs[IDX_FLITE] = sd;
79 break;
80 case GRP_ID_FIMC:
81 /* No need to control FIMC subdev through subdev ops */
82 break;
83 default:
84 pr_warn("%s: Unknown subdev grp_id: %#x\n",
85 __func__, sd->grp_id);
86 }
87 /* sink pad */
88 pad = &sd->entity.pads[0];
89 }
90 }
91
92 /**
93 * __subdev_set_power - change power state of a single subdev
94 * @sd: subdevice to change power state for
95 * @on: 1 to enable power or 0 to disable
96 *
97 * Return result of s_power subdev operation or -ENXIO if sd argument
98 * is NULL. Return 0 if the subdevice does not implement s_power.
99 */
100 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
101 {
102 int *use_count;
103 int ret;
104
105 if (sd == NULL)
106 return -ENXIO;
107
108 use_count = &sd->entity.use_count;
109 if (on && (*use_count)++ > 0)
110 return 0;
111 else if (!on && (*use_count == 0 || --(*use_count) > 0))
112 return 0;
113 ret = v4l2_subdev_call(sd, core, s_power, on);
114
115 return ret != -ENOIOCTLCMD ? ret : 0;
116 }
117
118 /**
119 * fimc_pipeline_s_power - change power state of all pipeline subdevs
120 * @fimc: fimc device terminating the pipeline
121 * @state: true to power on, false to power off
122 *
123 * Needs to be called with the graph mutex held.
124 */
125 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
126 {
127 unsigned int i;
128 int ret;
129
130 if (p->subdevs[IDX_SENSOR] == NULL)
131 return -ENXIO;
132
133 for (i = 0; i < IDX_MAX; i++) {
134 unsigned int idx = state ? (IDX_MAX - 1) - i : i;
135
136 ret = __subdev_set_power(p->subdevs[idx], state);
137 if (ret < 0 && ret != -ENXIO)
138 return ret;
139 }
140
141 return 0;
142 }
143
144 /**
145 * __fimc_pipeline_open - update the pipeline information, enable power
146 * of all pipeline subdevs and the sensor clock
147 * @me: media entity to start graph walk with
148 * @prep: true to acquire sensor (and csis) subdevs
149 *
150 * Called with the graph mutex held.
151 */
152 static int __fimc_pipeline_open(struct fimc_pipeline *p,
153 struct media_entity *me, bool prep)
154 {
155 int ret;
156
157 if (prep)
158 fimc_pipeline_prepare(p, me);
159
160 if (p->subdevs[IDX_SENSOR] == NULL)
161 return -EINVAL;
162
163 ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
164 if (ret)
165 return ret;
166
167 return fimc_pipeline_s_power(p, 1);
168 }
169
170 /**
171 * __fimc_pipeline_close - disable the sensor clock and pipeline power
172 * @fimc: fimc device terminating the pipeline
173 *
174 * Disable power of all subdevs and turn the external sensor clock off.
175 */
176 static int __fimc_pipeline_close(struct fimc_pipeline *p)
177 {
178 int ret = 0;
179
180 if (!p || !p->subdevs[IDX_SENSOR])
181 return -EINVAL;
182
183 if (p->subdevs[IDX_SENSOR]) {
184 ret = fimc_pipeline_s_power(p, 0);
185 fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
186 }
187 return ret == -ENXIO ? 0 : ret;
188 }
189
190 /**
191 * __fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
192 * @pipeline: video pipeline structure
193 * @on: passed as the s_stream call argument
194 */
195 static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
196 {
197 int i, ret;
198
199 if (p->subdevs[IDX_SENSOR] == NULL)
200 return -ENODEV;
201
202 for (i = 0; i < IDX_MAX; i++) {
203 unsigned int idx = on ? (IDX_MAX - 1) - i : i;
204
205 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
206
207 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
208 return ret;
209 }
210
211 return 0;
212
213 }
214
215 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
216 static const struct fimc_pipeline_ops fimc_pipeline_ops = {
217 .open = __fimc_pipeline_open,
218 .close = __fimc_pipeline_close,
219 .set_stream = __fimc_pipeline_s_stream,
220 };
221
222 /*
223 * Sensor subdevice helper functions
224 */
225 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
226 struct fimc_sensor_info *s_info)
227 {
228 struct i2c_adapter *adapter;
229 struct v4l2_subdev *sd = NULL;
230
231 if (!s_info || !fmd)
232 return NULL;
233
234 adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
235 if (!adapter) {
236 v4l2_warn(&fmd->v4l2_dev,
237 "Failed to get I2C adapter %d, deferring probe\n",
238 s_info->pdata.i2c_bus_num);
239 return ERR_PTR(-EPROBE_DEFER);
240 }
241 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
242 s_info->pdata.board_info, NULL);
243 if (IS_ERR_OR_NULL(sd)) {
244 i2c_put_adapter(adapter);
245 v4l2_warn(&fmd->v4l2_dev,
246 "Failed to acquire subdev %s, deferring probe\n",
247 s_info->pdata.board_info->type);
248 return ERR_PTR(-EPROBE_DEFER);
249 }
250 v4l2_set_subdev_hostdata(sd, s_info);
251 sd->grp_id = GRP_ID_SENSOR;
252
253 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
254 sd->name);
255 return sd;
256 }
257
258 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
259 {
260 struct i2c_client *client = v4l2_get_subdevdata(sd);
261 struct i2c_adapter *adapter;
262
263 if (!client)
264 return;
265 v4l2_device_unregister_subdev(sd);
266
267 if (!client->dev.of_node) {
268 adapter = client->adapter;
269 i2c_unregister_device(client);
270 if (adapter)
271 i2c_put_adapter(adapter);
272 }
273 }
274
275 #ifdef CONFIG_OF
276 /* Register I2C client subdev associated with @node. */
277 static int fimc_md_of_add_sensor(struct fimc_md *fmd,
278 struct device_node *node, int index)
279 {
280 struct fimc_sensor_info *si;
281 struct i2c_client *client;
282 struct v4l2_subdev *sd;
283 int ret;
284
285 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
286 return -EINVAL;
287 si = &fmd->sensor[index];
288
289 client = of_find_i2c_device_by_node(node);
290 if (!client)
291 return -EPROBE_DEFER;
292
293 device_lock(&client->dev);
294
295 if (!client->driver ||
296 !try_module_get(client->driver->driver.owner)) {
297 ret = -EPROBE_DEFER;
298 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
299 node->full_name);
300 goto dev_put;
301 }
302
303 /* Enable sensor's master clock */
304 ret = __fimc_md_set_camclk(fmd, si, true);
305 if (ret < 0)
306 goto mod_put;
307 sd = i2c_get_clientdata(client);
308
309 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
310 __fimc_md_set_camclk(fmd, si, false);
311 if (ret < 0)
312 goto mod_put;
313
314 v4l2_set_subdev_hostdata(sd, si);
315 sd->grp_id = GRP_ID_SENSOR;
316 si->subdev = sd;
317 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
318 sd->name, fmd->num_sensors);
319 fmd->num_sensors++;
320
321 mod_put:
322 module_put(client->driver->driver.owner);
323 dev_put:
324 device_unlock(&client->dev);
325 put_device(&client->dev);
326 return ret;
327 }
328
329 /* Parse port node and register as a sub-device any sensor specified there. */
330 static int fimc_md_parse_port_node(struct fimc_md *fmd,
331 struct device_node *port,
332 unsigned int index)
333 {
334 struct device_node *rem, *ep, *np;
335 struct fimc_source_info *pd;
336 struct v4l2_of_endpoint endpoint;
337 int ret;
338 u32 val;
339
340 pd = &fmd->sensor[index].pdata;
341
342 /* Assume here a port node can have only one endpoint node. */
343 ep = of_get_next_child(port, NULL);
344 if (!ep)
345 return 0;
346
347 v4l2_of_parse_endpoint(ep, &endpoint);
348 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
349 return -EINVAL;
350
351 pd->mux_id = (endpoint.port - 1) & 0x1;
352
353 rem = v4l2_of_get_remote_port_parent(ep);
354 of_node_put(ep);
355 if (rem == NULL) {
356 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
357 ep->full_name);
358 return 0;
359 }
360 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
361 pd->clk_id = val;
362
363 if (!of_property_read_u32(rem, "clock-frequency", &val))
364 pd->clk_frequency = val;
365
366 if (pd->clk_frequency == 0) {
367 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
368 rem->full_name);
369 of_node_put(rem);
370 return -EINVAL;
371 }
372
373 if (fimc_input_is_parallel(endpoint.port)) {
374 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
375 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
376 else
377 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
378 pd->flags = endpoint.bus.parallel.flags;
379 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
380 /*
381 * MIPI CSI-2: only input mux selection and
382 * the sensor's clock frequency is needed.
383 */
384 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
385 } else {
386 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
387 endpoint.port, rem->full_name);
388 }
389 /*
390 * For FIMC-IS handled sensors, that are placed under i2c-isp device
391 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
392 * input. Sensors are attached to the FIMC-LITE hostdata interface
393 * directly or through MIPI-CSIS, depending on the external media bus
394 * used. This needs to be handled in a more reliable way, not by just
395 * checking parent's node name.
396 */
397 np = of_get_parent(rem);
398
399 if (np && !of_node_cmp(np->name, "i2c-isp"))
400 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
401 else
402 pd->fimc_bus_type = pd->sensor_bus_type;
403
404 ret = fimc_md_of_add_sensor(fmd, rem, index);
405 of_node_put(rem);
406
407 return ret;
408 }
409
410 /* Register all SoC external sub-devices */
411 static int fimc_md_of_sensors_register(struct fimc_md *fmd,
412 struct device_node *np)
413 {
414 struct device_node *parent = fmd->pdev->dev.of_node;
415 struct device_node *node, *ports;
416 int index = 0;
417 int ret;
418
419 /* Attach sensors linked to MIPI CSI-2 receivers */
420 for_each_available_child_of_node(parent, node) {
421 struct device_node *port;
422
423 if (of_node_cmp(node->name, "csis"))
424 continue;
425 /* The csis node can have only port subnode. */
426 port = of_get_next_child(node, NULL);
427 if (!port)
428 continue;
429
430 ret = fimc_md_parse_port_node(fmd, port, index);
431 if (ret < 0)
432 return ret;
433 index++;
434 }
435
436 /* Attach sensors listed in the parallel-ports node */
437 ports = of_get_child_by_name(parent, "parallel-ports");
438 if (!ports)
439 return 0;
440
441 for_each_child_of_node(ports, node) {
442 ret = fimc_md_parse_port_node(fmd, node, index);
443 if (ret < 0)
444 break;
445 index++;
446 }
447
448 return 0;
449 }
450
451 static int __of_get_csis_id(struct device_node *np)
452 {
453 u32 reg = 0;
454
455 np = of_get_child_by_name(np, "port");
456 if (!np)
457 return -EINVAL;
458 of_property_read_u32(np, "reg", &reg);
459 return reg - FIMC_INPUT_MIPI_CSI2_0;
460 }
461 #else
462 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
463 #define __of_get_csis_id(np) (-ENOSYS)
464 #endif
465
466 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
467 {
468 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
469 struct device_node *of_node = fmd->pdev->dev.of_node;
470 struct fimc_dev *fd = NULL;
471 int num_clients = 0;
472 int ret, i;
473
474 /*
475 * Runtime resume one of the FIMC entities to make sure
476 * the sclk_cam clocks are not globally disabled.
477 */
478 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
479 if (fmd->fimc[i])
480 fd = fmd->fimc[i];
481 if (!fd)
482 return -ENXIO;
483
484 ret = pm_runtime_get_sync(&fd->pdev->dev);
485 if (ret < 0)
486 return ret;
487
488 if (of_node) {
489 fmd->num_sensors = 0;
490 ret = fimc_md_of_sensors_register(fmd, of_node);
491 } else if (pdata) {
492 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
493 num_clients = min_t(u32, pdata->num_clients,
494 ARRAY_SIZE(fmd->sensor));
495 fmd->num_sensors = num_clients;
496
497 for (i = 0; i < num_clients; i++) {
498 struct v4l2_subdev *sd;
499
500 fmd->sensor[i].pdata = pdata->source_info[i];
501 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
502 if (ret)
503 break;
504 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
505 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
506
507 if (IS_ERR(sd)) {
508 fmd->sensor[i].subdev = NULL;
509 ret = PTR_ERR(sd);
510 break;
511 }
512 fmd->sensor[i].subdev = sd;
513 if (ret)
514 break;
515 }
516 }
517
518 pm_runtime_put(&fd->pdev->dev);
519 return ret;
520 }
521
522 /*
523 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
524 */
525
526 static int register_fimc_lite_entity(struct fimc_md *fmd,
527 struct fimc_lite *fimc_lite)
528 {
529 struct v4l2_subdev *sd;
530 int ret;
531
532 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
533 fmd->fimc_lite[fimc_lite->index]))
534 return -EBUSY;
535
536 sd = &fimc_lite->subdev;
537 sd->grp_id = GRP_ID_FLITE;
538 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
539
540 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
541 if (!ret)
542 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
543 else
544 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
545 fimc_lite->index);
546 return ret;
547 }
548
549 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
550 {
551 struct v4l2_subdev *sd;
552 int ret;
553
554 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
555 return -EBUSY;
556
557 sd = &fimc->vid_cap.subdev;
558 sd->grp_id = GRP_ID_FIMC;
559 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
560
561 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
562 if (!ret) {
563 fmd->fimc[fimc->id] = fimc;
564 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
565 } else {
566 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
567 fimc->id, ret);
568 }
569 return ret;
570 }
571
572 static int register_csis_entity(struct fimc_md *fmd,
573 struct platform_device *pdev,
574 struct v4l2_subdev *sd)
575 {
576 struct device_node *node = pdev->dev.of_node;
577 int id, ret;
578
579 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
580
581 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
582 return -ENOENT;
583
584 if (WARN_ON(fmd->csis[id].sd))
585 return -EBUSY;
586
587 sd->grp_id = GRP_ID_CSIS;
588 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
589 if (!ret)
590 fmd->csis[id].sd = sd;
591 else
592 v4l2_err(&fmd->v4l2_dev,
593 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
594 return ret;
595 }
596
597 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
598 struct platform_device *pdev,
599 int plat_entity)
600 {
601 struct device *dev = &pdev->dev;
602 int ret = -EPROBE_DEFER;
603 void *drvdata;
604
605 /* Lock to ensure dev->driver won't change. */
606 device_lock(dev);
607
608 if (!dev->driver || !try_module_get(dev->driver->owner))
609 goto dev_unlock;
610
611 drvdata = dev_get_drvdata(dev);
612 /* Some subdev didn't probe succesfully id drvdata is NULL */
613 if (drvdata) {
614 switch (plat_entity) {
615 case IDX_FIMC:
616 ret = register_fimc_entity(fmd, drvdata);
617 break;
618 case IDX_FLITE:
619 ret = register_fimc_lite_entity(fmd, drvdata);
620 break;
621 case IDX_CSIS:
622 ret = register_csis_entity(fmd, pdev, drvdata);
623 break;
624 default:
625 ret = -ENODEV;
626 }
627 }
628
629 module_put(dev->driver->owner);
630 dev_unlock:
631 device_unlock(dev);
632 if (ret == -EPROBE_DEFER)
633 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
634 dev_name(dev));
635 else if (ret < 0)
636 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
637 dev_name(dev), ret);
638 return ret;
639 }
640
641 static int fimc_md_pdev_match(struct device *dev, void *data)
642 {
643 struct platform_device *pdev = to_platform_device(dev);
644 int plat_entity = -1;
645 int ret;
646 char *p;
647
648 if (!get_device(dev))
649 return -ENODEV;
650
651 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
652 plat_entity = IDX_CSIS;
653 } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) {
654 plat_entity = IDX_FLITE;
655 } else {
656 p = strstr(pdev->name, "fimc");
657 if (p && *(p + 4) == 0)
658 plat_entity = IDX_FIMC;
659 }
660
661 if (plat_entity >= 0)
662 ret = fimc_md_register_platform_entity(data, pdev,
663 plat_entity);
664 put_device(dev);
665 return 0;
666 }
667
668 /* Register FIMC, FIMC-LITE and CSIS media entities */
669 #ifdef CONFIG_OF
670 static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
671 struct device_node *parent)
672 {
673 struct device_node *node;
674 int ret = 0;
675
676 for_each_available_child_of_node(parent, node) {
677 struct platform_device *pdev;
678 int plat_entity = -1;
679
680 pdev = of_find_device_by_node(node);
681 if (!pdev)
682 continue;
683
684 /* If driver of any entity isn't ready try all again later. */
685 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
686 plat_entity = IDX_CSIS;
687 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
688 plat_entity = IDX_FLITE;
689 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
690 !of_property_read_bool(node, "samsung,lcd-wb"))
691 plat_entity = IDX_FIMC;
692
693 if (plat_entity >= 0)
694 ret = fimc_md_register_platform_entity(fmd, pdev,
695 plat_entity);
696 put_device(&pdev->dev);
697 if (ret < 0)
698 break;
699 }
700
701 return ret;
702 }
703 #else
704 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
705 #endif
706
707 static void fimc_md_unregister_entities(struct fimc_md *fmd)
708 {
709 int i;
710
711 for (i = 0; i < FIMC_MAX_DEVS; i++) {
712 if (fmd->fimc[i] == NULL)
713 continue;
714 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
715 fmd->fimc[i]->pipeline_ops = NULL;
716 fmd->fimc[i] = NULL;
717 }
718 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
719 if (fmd->fimc_lite[i] == NULL)
720 continue;
721 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
722 fmd->fimc_lite[i]->pipeline_ops = NULL;
723 fmd->fimc_lite[i] = NULL;
724 }
725 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
726 if (fmd->csis[i].sd == NULL)
727 continue;
728 v4l2_device_unregister_subdev(fmd->csis[i].sd);
729 module_put(fmd->csis[i].sd->owner);
730 fmd->csis[i].sd = NULL;
731 }
732 for (i = 0; i < fmd->num_sensors; i++) {
733 if (fmd->sensor[i].subdev == NULL)
734 continue;
735 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
736 fmd->sensor[i].subdev = NULL;
737 }
738 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
739 }
740
741 /**
742 * __fimc_md_create_fimc_links - create links to all FIMC entities
743 * @fmd: fimc media device
744 * @source: the source entity to create links to all fimc entities from
745 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
746 * @pad: the source entity pad index
747 * @link_mask: bitmask of the fimc devices for which link should be enabled
748 */
749 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
750 struct media_entity *source,
751 struct v4l2_subdev *sensor,
752 int pad, int link_mask)
753 {
754 struct fimc_sensor_info *s_info = NULL;
755 struct media_entity *sink;
756 unsigned int flags = 0;
757 int ret, i;
758
759 for (i = 0; i < FIMC_MAX_DEVS; i++) {
760 if (!fmd->fimc[i])
761 continue;
762 /*
763 * Some FIMC variants are not fitted with camera capture
764 * interface. Skip creating a link from sensor for those.
765 */
766 if (!fmd->fimc[i]->variant->has_cam_if)
767 continue;
768
769 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
770
771 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
772 ret = media_entity_create_link(source, pad, sink,
773 FIMC_SD_PAD_SINK, flags);
774 if (ret)
775 return ret;
776
777 /* Notify FIMC capture subdev entity */
778 ret = media_entity_call(sink, link_setup, &sink->pads[0],
779 &source->pads[pad], flags);
780 if (ret)
781 break;
782
783 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
784 source->name, flags ? '=' : '-', sink->name);
785
786 if (flags == 0 || sensor == NULL)
787 continue;
788 s_info = v4l2_get_subdev_hostdata(sensor);
789 if (!WARN_ON(s_info == NULL)) {
790 unsigned long irq_flags;
791 spin_lock_irqsave(&fmd->slock, irq_flags);
792 s_info->host = fmd->fimc[i];
793 spin_unlock_irqrestore(&fmd->slock, irq_flags);
794 }
795 }
796
797 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
798 if (!fmd->fimc_lite[i])
799 continue;
800
801 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
802 flags = MEDIA_LNK_FL_ENABLED;
803 else
804 flags = 0;
805
806 sink = &fmd->fimc_lite[i]->subdev.entity;
807 ret = media_entity_create_link(source, pad, sink,
808 FLITE_SD_PAD_SINK, flags);
809 if (ret)
810 return ret;
811
812 /* Notify FIMC-LITE subdev entity */
813 ret = media_entity_call(sink, link_setup, &sink->pads[0],
814 &source->pads[pad], flags);
815 if (ret)
816 break;
817
818 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
819 source->name, flags ? '=' : '-', sink->name);
820 }
821 return 0;
822 }
823
824 /* Create links from FIMC-LITE source pads to other entities */
825 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
826 {
827 struct media_entity *source, *sink;
828 unsigned int flags = MEDIA_LNK_FL_ENABLED;
829 int i, ret = 0;
830
831 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
832 struct fimc_lite *fimc = fmd->fimc_lite[i];
833 if (fimc == NULL)
834 continue;
835 source = &fimc->subdev.entity;
836 sink = &fimc->vfd.entity;
837 /* FIMC-LITE's subdev and video node */
838 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
839 sink, 0, flags);
840 if (ret)
841 break;
842 /* TODO: create links to other entities */
843 }
844
845 return ret;
846 }
847
848 /**
849 * fimc_md_create_links - create default links between registered entities
850 *
851 * Parallel interface sensor entities are connected directly to FIMC capture
852 * entities. The sensors using MIPI CSIS bus are connected through immutable
853 * link with CSI receiver entity specified by mux_id. Any registered CSIS
854 * entity has a link to each registered FIMC capture entity. Enabled links
855 * are created by default between each subsequent registered sensor and
856 * subsequent FIMC capture entity. The number of default active links is
857 * determined by the number of available sensors or FIMC entities,
858 * whichever is less.
859 */
860 static int fimc_md_create_links(struct fimc_md *fmd)
861 {
862 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
863 struct v4l2_subdev *sensor, *csis;
864 struct fimc_source_info *pdata;
865 struct fimc_sensor_info *s_info;
866 struct media_entity *source, *sink;
867 int i, pad, fimc_id = 0, ret = 0;
868 u32 flags, link_mask = 0;
869
870 for (i = 0; i < fmd->num_sensors; i++) {
871 if (fmd->sensor[i].subdev == NULL)
872 continue;
873
874 sensor = fmd->sensor[i].subdev;
875 s_info = v4l2_get_subdev_hostdata(sensor);
876 if (!s_info)
877 continue;
878
879 source = NULL;
880 pdata = &s_info->pdata;
881
882 switch (pdata->sensor_bus_type) {
883 case FIMC_BUS_TYPE_MIPI_CSI2:
884 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
885 "Wrong CSI channel id: %d\n", pdata->mux_id))
886 return -EINVAL;
887
888 csis = fmd->csis[pdata->mux_id].sd;
889 if (WARN(csis == NULL,
890 "MIPI-CSI interface specified "
891 "but s5p-csis module is not loaded!\n"))
892 return -EINVAL;
893
894 pad = sensor->entity.num_pads - 1;
895 ret = media_entity_create_link(&sensor->entity, pad,
896 &csis->entity, CSIS_PAD_SINK,
897 MEDIA_LNK_FL_IMMUTABLE |
898 MEDIA_LNK_FL_ENABLED);
899 if (ret)
900 return ret;
901
902 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
903 sensor->entity.name, csis->entity.name);
904
905 source = NULL;
906 csi_sensors[pdata->mux_id] = sensor;
907 break;
908
909 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
910 source = &sensor->entity;
911 pad = 0;
912 break;
913
914 default:
915 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
916 pdata->sensor_bus_type);
917 return -EINVAL;
918 }
919 if (source == NULL)
920 continue;
921
922 link_mask = 1 << fimc_id++;
923 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
924 pad, link_mask);
925 }
926
927 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
928 if (fmd->csis[i].sd == NULL)
929 continue;
930 source = &fmd->csis[i].sd->entity;
931 pad = CSIS_PAD_SOURCE;
932 sensor = csi_sensors[i];
933
934 link_mask = 1 << fimc_id++;
935 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
936 pad, link_mask);
937 }
938
939 /* Create immutable links between each FIMC's subdev and video node */
940 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
941 for (i = 0; i < FIMC_MAX_DEVS; i++) {
942 if (!fmd->fimc[i])
943 continue;
944 source = &fmd->fimc[i]->vid_cap.subdev.entity;
945 sink = &fmd->fimc[i]->vid_cap.vfd.entity;
946 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
947 sink, 0, flags);
948 if (ret)
949 break;
950 }
951
952 return __fimc_md_create_flite_source_links(fmd);
953 }
954
955 /*
956 * The peripheral sensor clock management.
957 */
958 static void fimc_md_put_clocks(struct fimc_md *fmd)
959 {
960 int i = FIMC_MAX_CAMCLKS;
961
962 while (--i >= 0) {
963 if (IS_ERR(fmd->camclk[i].clock))
964 continue;
965 clk_unprepare(fmd->camclk[i].clock);
966 clk_put(fmd->camclk[i].clock);
967 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
968 }
969 }
970
971 static int fimc_md_get_clocks(struct fimc_md *fmd)
972 {
973 struct device *dev = NULL;
974 char clk_name[32];
975 struct clk *clock;
976 int ret, i;
977
978 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
979 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
980
981 if (fmd->pdev->dev.of_node)
982 dev = &fmd->pdev->dev;
983
984 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
985 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
986 clock = clk_get(dev, clk_name);
987
988 if (IS_ERR(clock)) {
989 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
990 clk_name);
991 ret = PTR_ERR(clock);
992 break;
993 }
994 ret = clk_prepare(clock);
995 if (ret < 0) {
996 clk_put(clock);
997 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
998 break;
999 }
1000 fmd->camclk[i].clock = clock;
1001 }
1002 if (ret)
1003 fimc_md_put_clocks(fmd);
1004
1005 return ret;
1006 }
1007
1008 static int __fimc_md_set_camclk(struct fimc_md *fmd,
1009 struct fimc_sensor_info *s_info,
1010 bool on)
1011 {
1012 struct fimc_source_info *pdata = &s_info->pdata;
1013 struct fimc_camclk_info *camclk;
1014 int ret = 0;
1015
1016 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
1017 return -EINVAL;
1018
1019 camclk = &fmd->camclk[pdata->clk_id];
1020
1021 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1022 pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
1023
1024 if (on) {
1025 if (camclk->use_count > 0 &&
1026 camclk->frequency != pdata->clk_frequency)
1027 return -EINVAL;
1028
1029 if (camclk->use_count++ == 0) {
1030 clk_set_rate(camclk->clock, pdata->clk_frequency);
1031 camclk->frequency = pdata->clk_frequency;
1032 ret = clk_enable(camclk->clock);
1033 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
1034 clk_get_rate(camclk->clock));
1035 }
1036 return ret;
1037 }
1038
1039 if (WARN_ON(camclk->use_count == 0))
1040 return 0;
1041
1042 if (--camclk->use_count == 0) {
1043 clk_disable(camclk->clock);
1044 dbg("Disabled camclk %d", pdata->clk_id);
1045 }
1046 return ret;
1047 }
1048
1049 /**
1050 * fimc_md_set_camclk - peripheral sensor clock setup
1051 * @sd: sensor subdev to configure sclk_cam clock for
1052 * @on: 1 to enable or 0 to disable the clock
1053 *
1054 * There are 2 separate clock outputs available in the SoC for external
1055 * image processors. These clocks are shared between all registered FIMC
1056 * devices to which sensors can be attached, either directly or through
1057 * the MIPI CSI receiver. The clock is allowed here to be used by
1058 * multiple sensors concurrently if they use same frequency.
1059 * This function should only be called when the graph mutex is held.
1060 */
1061 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1062 {
1063 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
1064 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1065
1066 return __fimc_md_set_camclk(fmd, s_info, on);
1067 }
1068
1069 static int fimc_md_link_notify(struct media_pad *source,
1070 struct media_pad *sink, u32 flags)
1071 {
1072 struct fimc_lite *fimc_lite = NULL;
1073 struct fimc_dev *fimc = NULL;
1074 struct fimc_pipeline *pipeline;
1075 struct v4l2_subdev *sd;
1076 struct mutex *lock;
1077 int ret = 0;
1078 int ref_count;
1079
1080 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1081 return 0;
1082
1083 sd = media_entity_to_v4l2_subdev(sink->entity);
1084
1085 switch (sd->grp_id) {
1086 case GRP_ID_FLITE:
1087 fimc_lite = v4l2_get_subdevdata(sd);
1088 if (WARN_ON(fimc_lite == NULL))
1089 return 0;
1090 pipeline = &fimc_lite->pipeline;
1091 lock = &fimc_lite->lock;
1092 break;
1093 case GRP_ID_FIMC:
1094 fimc = v4l2_get_subdevdata(sd);
1095 if (WARN_ON(fimc == NULL))
1096 return 0;
1097 pipeline = &fimc->pipeline;
1098 lock = &fimc->lock;
1099 break;
1100 default:
1101 return 0;
1102 }
1103
1104 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1105 int i;
1106 mutex_lock(lock);
1107 ret = __fimc_pipeline_close(pipeline);
1108 for (i = 0; i < IDX_MAX; i++)
1109 pipeline->subdevs[i] = NULL;
1110 if (fimc)
1111 fimc_ctrls_delete(fimc->vid_cap.ctx);
1112 mutex_unlock(lock);
1113 return ret;
1114 }
1115 /*
1116 * Link activation. Enable power of pipeline elements only if the
1117 * pipeline is already in use, i.e. its video node is opened.
1118 * Recreate the controls destroyed during the link deactivation.
1119 */
1120 mutex_lock(lock);
1121
1122 ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;
1123 if (ref_count > 0)
1124 ret = __fimc_pipeline_open(pipeline, source->entity, true);
1125 if (!ret && fimc)
1126 ret = fimc_capture_ctrls_create(fimc);
1127
1128 mutex_unlock(lock);
1129 return ret ? -EPIPE : ret;
1130 }
1131
1132 static ssize_t fimc_md_sysfs_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1134 {
1135 struct platform_device *pdev = to_platform_device(dev);
1136 struct fimc_md *fmd = platform_get_drvdata(pdev);
1137
1138 if (fmd->user_subdev_api)
1139 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1140
1141 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1142 }
1143
1144 static ssize_t fimc_md_sysfs_store(struct device *dev,
1145 struct device_attribute *attr,
1146 const char *buf, size_t count)
1147 {
1148 struct platform_device *pdev = to_platform_device(dev);
1149 struct fimc_md *fmd = platform_get_drvdata(pdev);
1150 bool subdev_api;
1151 int i;
1152
1153 if (!strcmp(buf, "vid-dev\n"))
1154 subdev_api = false;
1155 else if (!strcmp(buf, "sub-dev\n"))
1156 subdev_api = true;
1157 else
1158 return count;
1159
1160 fmd->user_subdev_api = subdev_api;
1161 for (i = 0; i < FIMC_MAX_DEVS; i++)
1162 if (fmd->fimc[i])
1163 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1164 return count;
1165 }
1166 /*
1167 * This device attribute is to select video pipeline configuration method.
1168 * There are following valid values:
1169 * vid-dev - for V4L2 video node API only, subdevice will be configured
1170 * by the host driver.
1171 * sub-dev - for media controller API, subdevs must be configured in user
1172 * space before starting streaming.
1173 */
1174 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1175 fimc_md_sysfs_show, fimc_md_sysfs_store);
1176
1177 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1178 {
1179 struct device *dev = &fmd->pdev->dev;
1180 struct fimc_pinctrl *pctl = &fmd->pinctl;
1181
1182 pctl->pinctrl = devm_pinctrl_get(dev);
1183 if (IS_ERR(pctl->pinctrl))
1184 return PTR_ERR(pctl->pinctrl);
1185
1186 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1187 PINCTRL_STATE_DEFAULT);
1188 if (IS_ERR(pctl->state_default))
1189 return PTR_ERR(pctl->state_default);
1190
1191 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1192 PINCTRL_STATE_IDLE);
1193 return 0;
1194 }
1195
1196 static int fimc_md_probe(struct platform_device *pdev)
1197 {
1198 struct device *dev = &pdev->dev;
1199 struct v4l2_device *v4l2_dev;
1200 struct fimc_md *fmd;
1201 int ret;
1202
1203 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1204 if (!fmd)
1205 return -ENOMEM;
1206
1207 spin_lock_init(&fmd->slock);
1208 fmd->pdev = pdev;
1209
1210 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1211 sizeof(fmd->media_dev.model));
1212 fmd->media_dev.link_notify = fimc_md_link_notify;
1213 fmd->media_dev.dev = dev;
1214
1215 v4l2_dev = &fmd->v4l2_dev;
1216 v4l2_dev->mdev = &fmd->media_dev;
1217 v4l2_dev->notify = fimc_sensor_notify;
1218 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1219
1220 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1221 if (ret < 0) {
1222 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1223 return ret;
1224 }
1225 ret = media_device_register(&fmd->media_dev);
1226 if (ret < 0) {
1227 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1228 goto err_md;
1229 }
1230 ret = fimc_md_get_clocks(fmd);
1231 if (ret)
1232 goto err_clk;
1233
1234 fmd->user_subdev_api = (dev->of_node != NULL);
1235
1236 /* Protect the media graph while we're registering entities */
1237 mutex_lock(&fmd->media_dev.graph_mutex);
1238
1239 ret = fimc_md_get_pinctrl(fmd);
1240 if (ret < 0) {
1241 if (ret != EPROBE_DEFER)
1242 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1243 goto err_unlock;
1244 }
1245
1246 if (dev->of_node)
1247 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1248 else
1249 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1250 fimc_md_pdev_match);
1251 if (ret)
1252 goto err_unlock;
1253
1254 if (dev->platform_data || dev->of_node) {
1255 ret = fimc_md_register_sensor_entities(fmd);
1256 if (ret)
1257 goto err_unlock;
1258 }
1259
1260 ret = fimc_md_create_links(fmd);
1261 if (ret)
1262 goto err_unlock;
1263 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1264 if (ret)
1265 goto err_unlock;
1266
1267 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1268 if (ret)
1269 goto err_unlock;
1270
1271 platform_set_drvdata(pdev, fmd);
1272 mutex_unlock(&fmd->media_dev.graph_mutex);
1273 return 0;
1274
1275 err_unlock:
1276 mutex_unlock(&fmd->media_dev.graph_mutex);
1277 err_clk:
1278 media_device_unregister(&fmd->media_dev);
1279 fimc_md_put_clocks(fmd);
1280 fimc_md_unregister_entities(fmd);
1281 err_md:
1282 v4l2_device_unregister(&fmd->v4l2_dev);
1283 return ret;
1284 }
1285
1286 static int fimc_md_remove(struct platform_device *pdev)
1287 {
1288 struct fimc_md *fmd = platform_get_drvdata(pdev);
1289
1290 if (!fmd)
1291 return 0;
1292 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1293 fimc_md_unregister_entities(fmd);
1294 media_device_unregister(&fmd->media_dev);
1295 fimc_md_put_clocks(fmd);
1296 return 0;
1297 }
1298
1299 static struct platform_device_id fimc_driver_ids[] __always_unused = {
1300 { .name = "s5p-fimc-md" },
1301 { },
1302 };
1303 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1304
1305 static const struct of_device_id fimc_md_of_match[] = {
1306 { .compatible = "samsung,fimc" },
1307 { },
1308 };
1309 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1310
1311 static struct platform_driver fimc_md_driver = {
1312 .probe = fimc_md_probe,
1313 .remove = fimc_md_remove,
1314 .driver = {
1315 .of_match_table = of_match_ptr(fimc_md_of_match),
1316 .name = "s5p-fimc-md",
1317 .owner = THIS_MODULE,
1318 }
1319 };
1320
1321 static int __init fimc_md_init(void)
1322 {
1323 int ret;
1324
1325 request_module("s5p-csis");
1326 ret = fimc_register_driver();
1327 if (ret)
1328 return ret;
1329
1330 return platform_driver_register(&fimc_md_driver);
1331 }
1332
1333 static void __exit fimc_md_exit(void)
1334 {
1335 platform_driver_unregister(&fimc_md_driver);
1336 fimc_unregister_driver();
1337 }
1338
1339 module_init(fimc_md_init);
1340 module_exit(fimc_md_exit);
1341
1342 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1343 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1344 MODULE_LICENSE("GPL");
1345 MODULE_VERSION("2.0.1");
This page took 0.209812 seconds and 4 git commands to generate.