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