blob: c4a39782d55e4ca4c7a6ce3dfb7c56a362d372af [file] [log] [blame]
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001/*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
3 *
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -03004 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
Sylwester Nawrockid3953222011-09-01 06:01:08 -03006 *
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>
Sylwester Nawrockie2985a22013-03-08 12:59:33 -030020#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/of_device.h>
23#include <linux/of_i2c.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030024#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/types.h>
27#include <linux/slab.h>
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -030028#include <media/v4l2-ctrls.h>
Sylwester Nawrockie2985a22013-03-08 12:59:33 -030029#include <media/v4l2-of.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030030#include <media/media-device.h>
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030031#include <media/s5p_fimc.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030032
33#include "fimc-core.h"
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030034#include "fimc-lite.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030035#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
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030043 * @me: media entity terminating the pipeline
Sylwester Nawrockid3953222011-09-01 06:01:08 -030044 *
45 * Caller holds the graph mutex.
46 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030047static void fimc_pipeline_prepare(struct fimc_pipeline *p,
48 struct media_entity *me)
Sylwester Nawrockid3953222011-09-01 06:01:08 -030049{
Sylwester Nawrockid3953222011-09-01 06:01:08 -030050 struct v4l2_subdev *sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030051 int i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030052
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030053 for (i = 0; i < IDX_MAX; i++)
54 p->subdevs[i] = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030055
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030056 while (1) {
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030057 struct media_pad *pad = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030058
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030059 /* 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 }
68
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030069 if (pad == NULL ||
70 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
71 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030072 sd = media_entity_to_v4l2_subdev(pad->entity);
73
74 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030075 case GRP_ID_FIMC_IS_SENSOR:
76 case GRP_ID_SENSOR:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030077 p->subdevs[IDX_SENSOR] = sd;
78 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030079 case GRP_ID_CSIS:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030080 p->subdevs[IDX_CSIS] = sd;
81 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030082 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -030083 p->subdevs[IDX_FLITE] = sd;
84 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030085 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030086 /* 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 }
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030092 me = &sd->entity;
93 if (me->num_pads == 1)
94 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030095 }
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
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300127 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300128 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300129 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300130 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300131static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300132{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300133 unsigned int i;
134 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300135
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300136 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300137 return -ENXIO;
138
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300139 for (i = 0; i < IDX_MAX; i++) {
140 unsigned int idx = state ? (IDX_MAX - 1) - i : i;
141
142 ret = __subdev_set_power(p->subdevs[idx], state);
143 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300144 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300145 }
146
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300147 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300148}
149
150/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300151 * __fimc_pipeline_open - update the pipeline information, enable power
152 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300153 * @me: media entity to start graph walk with
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300154 * @prepare: true to walk the current pipeline and acquire all subdevs
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300155 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300156 * Called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300157 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300158static int __fimc_pipeline_open(struct fimc_pipeline *p,
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300159 struct media_entity *me, bool prepare)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300160{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300161 struct fimc_md *fmd = entity_to_fimc_mdev(me);
162 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300163 int ret;
164
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300165 if (WARN_ON(p == NULL || me == NULL))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300166 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300167
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300168 if (prepare)
169 fimc_pipeline_prepare(p, me);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300170
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300171 sd = p->subdevs[IDX_SENSOR];
172 if (sd == NULL)
173 return -EINVAL;
174
175 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
176 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
177 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
178 if (ret < 0)
179 return ret;
180 }
181 ret = fimc_md_set_camclk(sd, true);
182 if (ret < 0)
183 goto err_wbclk;
184
185 ret = fimc_pipeline_s_power(p, 1);
186 if (!ret)
187 return 0;
188
189 fimc_md_set_camclk(sd, false);
190
191err_wbclk:
192 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
193 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
194
195 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300196}
197
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300198/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300199 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300200 * @fimc: fimc device terminating the pipeline
201 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300202 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300203 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300204static int __fimc_pipeline_close(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300205{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300206 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
207 struct fimc_md *fmd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300208 int ret = 0;
209
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300210 if (WARN_ON(sd == NULL))
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300211 return -EINVAL;
212
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300213 if (p->subdevs[IDX_SENSOR]) {
214 ret = fimc_pipeline_s_power(p, 0);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300215 fimc_md_set_camclk(sd, false);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300216 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300217
218 fmd = entity_to_fimc_mdev(&sd->entity);
219
220 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
221 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
222 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
223
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300224 return ret == -ENXIO ? 0 : ret;
225}
226
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300227/**
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300228 * __fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300229 * @pipeline: video pipeline structure
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300230 * @on: passed as the s_stream call argument
231 */
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300232static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300233{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300234 int i, ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300235
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300236 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300237 return -ENODEV;
238
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300239 for (i = 0; i < IDX_MAX; i++) {
240 unsigned int idx = on ? (IDX_MAX - 1) - i : i;
241
242 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
243
244 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
245 return ret;
246 }
247
248 return 0;
249
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300250}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300251
252/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
253static const struct fimc_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300254 .open = __fimc_pipeline_open,
255 .close = __fimc_pipeline_close,
256 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300257};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300258
259/*
260 * Sensor subdevice helper functions
261 */
262static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
263 struct fimc_sensor_info *s_info)
264{
265 struct i2c_adapter *adapter;
266 struct v4l2_subdev *sd = NULL;
267
268 if (!s_info || !fmd)
269 return NULL;
270
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300271 adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300272 if (!adapter) {
273 v4l2_warn(&fmd->v4l2_dev,
274 "Failed to get I2C adapter %d, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300275 s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300276 return ERR_PTR(-EPROBE_DEFER);
277 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300278 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300279 s_info->pdata.board_info, NULL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300280 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300281 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300282 v4l2_warn(&fmd->v4l2_dev,
283 "Failed to acquire subdev %s, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300284 s_info->pdata.board_info->type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300285 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300286 }
287 v4l2_set_subdev_hostdata(sd, s_info);
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300288 sd->grp_id = GRP_ID_SENSOR;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300289
290 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300291 sd->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300292 return sd;
293}
294
295static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
296{
297 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300298 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300299
300 if (!client)
301 return;
302 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300303
304 if (!client->dev.of_node) {
305 adapter = client->adapter;
306 i2c_unregister_device(client);
307 if (adapter)
308 i2c_put_adapter(adapter);
309 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300310}
311
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300312#ifdef CONFIG_OF
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300313/* Register I2C client subdev associated with @node. */
314static int fimc_md_of_add_sensor(struct fimc_md *fmd,
315 struct device_node *node, int index)
316{
317 struct fimc_sensor_info *si;
318 struct i2c_client *client;
319 struct v4l2_subdev *sd;
320 int ret;
321
322 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
323 return -EINVAL;
324 si = &fmd->sensor[index];
325
326 client = of_find_i2c_device_by_node(node);
327 if (!client)
328 return -EPROBE_DEFER;
329
330 device_lock(&client->dev);
331
332 if (!client->driver ||
333 !try_module_get(client->driver->driver.owner)) {
334 ret = -EPROBE_DEFER;
335 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
336 node->full_name);
337 goto dev_put;
338 }
339
340 /* Enable sensor's master clock */
341 ret = __fimc_md_set_camclk(fmd, si, true);
342 if (ret < 0)
343 goto mod_put;
344 sd = i2c_get_clientdata(client);
345
346 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
347 __fimc_md_set_camclk(fmd, si, false);
348 if (ret < 0)
349 goto mod_put;
350
351 v4l2_set_subdev_hostdata(sd, si);
352 sd->grp_id = GRP_ID_SENSOR;
353 si->subdev = sd;
354 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
355 sd->name, fmd->num_sensors);
356 fmd->num_sensors++;
357
358mod_put:
359 module_put(client->driver->driver.owner);
360dev_put:
361 device_unlock(&client->dev);
362 put_device(&client->dev);
363 return ret;
364}
365
366/* Parse port node and register as a sub-device any sensor specified there. */
367static int fimc_md_parse_port_node(struct fimc_md *fmd,
368 struct device_node *port,
369 unsigned int index)
370{
371 struct device_node *rem, *ep, *np;
372 struct fimc_source_info *pd;
373 struct v4l2_of_endpoint endpoint;
374 int ret;
375 u32 val;
376
377 pd = &fmd->sensor[index].pdata;
378
379 /* Assume here a port node can have only one endpoint node. */
380 ep = of_get_next_child(port, NULL);
381 if (!ep)
382 return 0;
383
384 v4l2_of_parse_endpoint(ep, &endpoint);
385 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
386 return -EINVAL;
387
388 pd->mux_id = (endpoint.port - 1) & 0x1;
389
390 rem = v4l2_of_get_remote_port_parent(ep);
391 of_node_put(ep);
392 if (rem == NULL) {
393 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
394 ep->full_name);
395 return 0;
396 }
397 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
398 pd->clk_id = val;
399
400 if (!of_property_read_u32(rem, "clock-frequency", &val))
401 pd->clk_frequency = val;
402
403 if (pd->clk_frequency == 0) {
404 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
405 rem->full_name);
406 of_node_put(rem);
407 return -EINVAL;
408 }
409
410 if (fimc_input_is_parallel(endpoint.port)) {
411 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
412 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
413 else
414 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
415 pd->flags = endpoint.bus.parallel.flags;
416 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
417 /*
418 * MIPI CSI-2: only input mux selection and
419 * the sensor's clock frequency is needed.
420 */
421 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
422 } else {
423 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
424 endpoint.port, rem->full_name);
425 }
426 /*
427 * For FIMC-IS handled sensors, that are placed under i2c-isp device
428 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
429 * input. Sensors are attached to the FIMC-LITE hostdata interface
430 * directly or through MIPI-CSIS, depending on the external media bus
431 * used. This needs to be handled in a more reliable way, not by just
432 * checking parent's node name.
433 */
434 np = of_get_parent(rem);
435
436 if (np && !of_node_cmp(np->name, "i2c-isp"))
437 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
438 else
439 pd->fimc_bus_type = pd->sensor_bus_type;
440
441 ret = fimc_md_of_add_sensor(fmd, rem, index);
442 of_node_put(rem);
443
444 return ret;
445}
446
447/* Register all SoC external sub-devices */
448static int fimc_md_of_sensors_register(struct fimc_md *fmd,
449 struct device_node *np)
450{
451 struct device_node *parent = fmd->pdev->dev.of_node;
452 struct device_node *node, *ports;
453 int index = 0;
454 int ret;
455
456 /* Attach sensors linked to MIPI CSI-2 receivers */
457 for_each_available_child_of_node(parent, node) {
458 struct device_node *port;
459
460 if (of_node_cmp(node->name, "csis"))
461 continue;
462 /* The csis node can have only port subnode. */
463 port = of_get_next_child(node, NULL);
464 if (!port)
465 continue;
466
467 ret = fimc_md_parse_port_node(fmd, port, index);
468 if (ret < 0)
469 return ret;
470 index++;
471 }
472
473 /* Attach sensors listed in the parallel-ports node */
474 ports = of_get_child_by_name(parent, "parallel-ports");
475 if (!ports)
476 return 0;
477
478 for_each_child_of_node(ports, node) {
479 ret = fimc_md_parse_port_node(fmd, node, index);
480 if (ret < 0)
481 break;
482 index++;
483 }
484
485 return 0;
486}
487
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300488static int __of_get_csis_id(struct device_node *np)
489{
490 u32 reg = 0;
491
492 np = of_get_child_by_name(np, "port");
493 if (!np)
494 return -EINVAL;
495 of_property_read_u32(np, "reg", &reg);
496 return reg - FIMC_INPUT_MIPI_CSI2_0;
497}
498#else
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300499#define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300500#define __of_get_csis_id(np) (-ENOSYS)
501#endif
502
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300503static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
504{
505 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300506 struct device_node *of_node = fmd->pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300507 struct fimc_dev *fd = NULL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300508 int num_clients = 0;
509 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300510
511 /*
512 * Runtime resume one of the FIMC entities to make sure
513 * the sclk_cam clocks are not globally disabled.
514 */
515 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
516 if (fmd->fimc[i])
517 fd = fmd->fimc[i];
518 if (!fd)
519 return -ENXIO;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300520
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300521 ret = pm_runtime_get_sync(&fd->pdev->dev);
522 if (ret < 0)
523 return ret;
524
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300525 if (of_node) {
526 fmd->num_sensors = 0;
527 ret = fimc_md_of_sensors_register(fmd, of_node);
528 } else if (pdata) {
529 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
530 num_clients = min_t(u32, pdata->num_clients,
531 ARRAY_SIZE(fmd->sensor));
532 fmd->num_sensors = num_clients;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300533
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300534 for (i = 0; i < num_clients; i++) {
535 struct v4l2_subdev *sd;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300536
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300537 fmd->sensor[i].pdata = pdata->source_info[i];
538 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
539 if (ret)
540 break;
541 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
542 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300543
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300544 if (IS_ERR(sd)) {
545 fmd->sensor[i].subdev = NULL;
546 ret = PTR_ERR(sd);
547 break;
548 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300549 fmd->sensor[i].subdev = sd;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300550 if (ret)
551 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300552 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300553 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300554
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300555 pm_runtime_put(&fd->pdev->dev);
556 return ret;
557}
558
559/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300560 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300561 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300562
563static int register_fimc_lite_entity(struct fimc_md *fmd,
564 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300565{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300566 struct v4l2_subdev *sd;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300567 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300568
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300569 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
570 fmd->fimc_lite[fimc_lite->index]))
571 return -EBUSY;
572
573 sd = &fimc_lite->subdev;
574 sd->grp_id = GRP_ID_FLITE;
575 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
576
577 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
578 if (!ret)
579 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
580 else
581 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
582 fimc_lite->index);
583 return ret;
584}
585
586static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
587{
588 struct v4l2_subdev *sd;
589 int ret;
590
591 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
592 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300593
Sachin Kamat8163ec02012-10-25 10:56:00 -0300594 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300595 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -0300596 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300597
598 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300599 if (!ret) {
600 fmd->fimc[fimc->id] = fimc;
601 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
602 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300603 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
604 fimc->id, ret);
605 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300606 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300607}
608
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300609static int register_csis_entity(struct fimc_md *fmd,
610 struct platform_device *pdev,
611 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300612{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300613 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300614 int id, ret;
615
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300616 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300617
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300618 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
619 return -ENOENT;
620
621 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300622 return -EBUSY;
623
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300624 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300625 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300626 if (!ret)
627 fmd->csis[id].sd = sd;
628 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300629 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300630 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300631 return ret;
632}
633
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300634static int fimc_md_register_platform_entity(struct fimc_md *fmd,
635 struct platform_device *pdev,
636 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300637{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300638 struct device *dev = &pdev->dev;
639 int ret = -EPROBE_DEFER;
640 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300641
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300642 /* Lock to ensure dev->driver won't change. */
643 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300644
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300645 if (!dev->driver || !try_module_get(dev->driver->owner))
646 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300647
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300648 drvdata = dev_get_drvdata(dev);
649 /* Some subdev didn't probe succesfully id drvdata is NULL */
650 if (drvdata) {
651 switch (plat_entity) {
652 case IDX_FIMC:
653 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300654 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300655 case IDX_FLITE:
656 ret = register_fimc_lite_entity(fmd, drvdata);
657 break;
658 case IDX_CSIS:
659 ret = register_csis_entity(fmd, pdev, drvdata);
660 break;
661 default:
662 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300663 }
664 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300665
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300666 module_put(dev->driver->owner);
667dev_unlock:
668 device_unlock(dev);
669 if (ret == -EPROBE_DEFER)
670 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
671 dev_name(dev));
672 else if (ret < 0)
673 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
674 dev_name(dev), ret);
675 return ret;
676}
677
678static int fimc_md_pdev_match(struct device *dev, void *data)
679{
680 struct platform_device *pdev = to_platform_device(dev);
681 int plat_entity = -1;
682 int ret;
683 char *p;
684
685 if (!get_device(dev))
686 return -ENODEV;
687
688 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
689 plat_entity = IDX_CSIS;
690 } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) {
691 plat_entity = IDX_FLITE;
692 } else {
693 p = strstr(pdev->name, "fimc");
694 if (p && *(p + 4) == 0)
695 plat_entity = IDX_FIMC;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300696 }
697
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300698 if (plat_entity >= 0)
699 ret = fimc_md_register_platform_entity(data, pdev,
700 plat_entity);
701 put_device(dev);
702 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300703}
704
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300705/* Register FIMC, FIMC-LITE and CSIS media entities */
706#ifdef CONFIG_OF
707static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
708 struct device_node *parent)
709{
710 struct device_node *node;
711 int ret = 0;
712
713 for_each_available_child_of_node(parent, node) {
714 struct platform_device *pdev;
715 int plat_entity = -1;
716
717 pdev = of_find_device_by_node(node);
718 if (!pdev)
719 continue;
720
721 /* If driver of any entity isn't ready try all again later. */
722 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
723 plat_entity = IDX_CSIS;
724 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
725 plat_entity = IDX_FLITE;
726 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
727 !of_property_read_bool(node, "samsung,lcd-wb"))
728 plat_entity = IDX_FIMC;
729
730 if (plat_entity >= 0)
731 ret = fimc_md_register_platform_entity(fmd, pdev,
732 plat_entity);
733 put_device(&pdev->dev);
734 if (ret < 0)
735 break;
736 }
737
738 return ret;
739}
740#else
741#define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
742#endif
743
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300744static void fimc_md_unregister_entities(struct fimc_md *fmd)
745{
746 int i;
747
748 for (i = 0; i < FIMC_MAX_DEVS; i++) {
749 if (fmd->fimc[i] == NULL)
750 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300751 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300752 fmd->fimc[i]->pipeline_ops = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300753 fmd->fimc[i] = NULL;
754 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300755 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
756 if (fmd->fimc_lite[i] == NULL)
757 continue;
758 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
Sylwester Nawrocki248ac362013-01-11 07:06:28 -0300759 fmd->fimc_lite[i]->pipeline_ops = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300760 fmd->fimc_lite[i] = NULL;
761 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300762 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
763 if (fmd->csis[i].sd == NULL)
764 continue;
765 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300766 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300767 fmd->csis[i].sd = NULL;
768 }
769 for (i = 0; i < fmd->num_sensors; i++) {
770 if (fmd->sensor[i].subdev == NULL)
771 continue;
772 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
773 fmd->sensor[i].subdev = NULL;
774 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300775 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300776}
777
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300778/**
779 * __fimc_md_create_fimc_links - create links to all FIMC entities
780 * @fmd: fimc media device
781 * @source: the source entity to create links to all fimc entities from
782 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
783 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300784 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300785 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300786static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
787 struct media_entity *source,
788 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300789 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300790{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300791 struct fimc_sensor_info *s_info = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300792 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300793 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300794 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300795
796 for (i = 0; i < FIMC_MAX_DEVS; i++) {
797 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300798 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300799 /*
800 * Some FIMC variants are not fitted with camera capture
801 * interface. Skip creating a link from sensor for those.
802 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300803 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300804 continue;
805
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300806 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300807
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300808 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300809 ret = media_entity_create_link(source, pad, sink,
810 FIMC_SD_PAD_SINK, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300811 if (ret)
812 return ret;
813
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300814 /* Notify FIMC capture subdev entity */
815 ret = media_entity_call(sink, link_setup, &sink->pads[0],
816 &source->pads[pad], flags);
817 if (ret)
818 break;
819
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300820 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300821 source->name, flags ? '=' : '-', sink->name);
822
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300823 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300824 continue;
825 s_info = v4l2_get_subdev_hostdata(sensor);
826 if (!WARN_ON(s_info == NULL)) {
827 unsigned long irq_flags;
828 spin_lock_irqsave(&fmd->slock, irq_flags);
829 s_info->host = fmd->fimc[i];
830 spin_unlock_irqrestore(&fmd->slock, irq_flags);
831 }
832 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300833
834 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
835 if (!fmd->fimc_lite[i])
836 continue;
837
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300838 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
839 flags = MEDIA_LNK_FL_ENABLED;
840 else
841 flags = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300842
843 sink = &fmd->fimc_lite[i]->subdev.entity;
844 ret = media_entity_create_link(source, pad, sink,
845 FLITE_SD_PAD_SINK, flags);
846 if (ret)
847 return ret;
848
849 /* Notify FIMC-LITE subdev entity */
850 ret = media_entity_call(sink, link_setup, &sink->pads[0],
851 &source->pads[pad], flags);
852 if (ret)
853 break;
854
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300855 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300856 source->name, flags ? '=' : '-', sink->name);
857 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300858 return 0;
859}
860
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300861/* Create links from FIMC-LITE source pads to other entities */
862static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
863{
864 struct media_entity *source, *sink;
865 unsigned int flags = MEDIA_LNK_FL_ENABLED;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300866 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300867
868 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
869 struct fimc_lite *fimc = fmd->fimc_lite[i];
870 if (fimc == NULL)
871 continue;
872 source = &fimc->subdev.entity;
Sylwester Nawrocki1bcd7042012-07-26 07:13:08 -0300873 sink = &fimc->vfd.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300874 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300875 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300876 sink, 0, flags);
877 if (ret)
878 break;
879 /* TODO: create links to other entities */
880 }
881
882 return ret;
883}
884
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300885/**
886 * fimc_md_create_links - create default links between registered entities
887 *
888 * Parallel interface sensor entities are connected directly to FIMC capture
889 * entities. The sensors using MIPI CSIS bus are connected through immutable
890 * link with CSI receiver entity specified by mux_id. Any registered CSIS
891 * entity has a link to each registered FIMC capture entity. Enabled links
892 * are created by default between each subsequent registered sensor and
893 * subsequent FIMC capture entity. The number of default active links is
894 * determined by the number of available sensors or FIMC entities,
895 * whichever is less.
896 */
897static int fimc_md_create_links(struct fimc_md *fmd)
898{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300899 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300900 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300901 struct fimc_source_info *pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300902 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300903 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300904 int i, pad, fimc_id = 0, ret = 0;
905 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300906
907 for (i = 0; i < fmd->num_sensors; i++) {
908 if (fmd->sensor[i].subdev == NULL)
909 continue;
910
911 sensor = fmd->sensor[i].subdev;
912 s_info = v4l2_get_subdev_hostdata(sensor);
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300913 if (!s_info)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300914 continue;
915
916 source = NULL;
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300917 pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300918
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300919 switch (pdata->sensor_bus_type) {
920 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300921 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
922 "Wrong CSI channel id: %d\n", pdata->mux_id))
923 return -EINVAL;
924
925 csis = fmd->csis[pdata->mux_id].sd;
926 if (WARN(csis == NULL,
927 "MIPI-CSI interface specified "
928 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300929 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300930
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300931 pad = sensor->entity.num_pads - 1;
932 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300933 &csis->entity, CSIS_PAD_SINK,
934 MEDIA_LNK_FL_IMMUTABLE |
935 MEDIA_LNK_FL_ENABLED);
936 if (ret)
937 return ret;
938
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300939 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300940 sensor->entity.name, csis->entity.name);
941
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300942 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300943 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300944 break;
945
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300946 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300947 source = &sensor->entity;
948 pad = 0;
949 break;
950
951 default:
952 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300953 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300954 return -EINVAL;
955 }
956 if (source == NULL)
957 continue;
958
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300959 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300960 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300961 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300962 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300963
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300964 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300965 if (fmd->csis[i].sd == NULL)
966 continue;
967 source = &fmd->csis[i].sd->entity;
968 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300969 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300970
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300971 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300972 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300973 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300974 }
975
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300976 /* Create immutable links between each FIMC's subdev and video node */
977 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
978 for (i = 0; i < FIMC_MAX_DEVS; i++) {
979 if (!fmd->fimc[i])
980 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300981 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -0300982 sink = &fmd->fimc[i]->vid_cap.vfd.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300983 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
984 sink, 0, flags);
985 if (ret)
986 break;
987 }
988
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300989 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300990}
991
992/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300993 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300994 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300995static void fimc_md_put_clocks(struct fimc_md *fmd)
996{
997 int i = FIMC_MAX_CAMCLKS;
998
999 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001000 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001001 continue;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001002 clk_unprepare(fmd->camclk[i].clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001003 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001004 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001005 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001006
1007 /* Writeback (PIXELASYNCMx) clocks */
1008 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1009 if (IS_ERR(fmd->wbclk[i]))
1010 continue;
1011 clk_put(fmd->wbclk[i]);
1012 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1013 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001014}
1015
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001016static int fimc_md_get_clocks(struct fimc_md *fmd)
1017{
1018 struct device *dev = NULL;
1019 char clk_name[32];
1020 struct clk *clock;
1021 int ret, i;
1022
1023 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1024 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1025
1026 if (fmd->pdev->dev.of_node)
1027 dev = &fmd->pdev->dev;
1028
1029 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1030 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1031 clock = clk_get(dev, clk_name);
1032
1033 if (IS_ERR(clock)) {
1034 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1035 clk_name);
1036 ret = PTR_ERR(clock);
1037 break;
1038 }
1039 ret = clk_prepare(clock);
1040 if (ret < 0) {
1041 clk_put(clock);
1042 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1043 break;
1044 }
1045 fmd->camclk[i].clock = clock;
1046 }
1047 if (ret)
1048 fimc_md_put_clocks(fmd);
1049
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001050 if (!fmd->use_isp)
1051 return 0;
1052 /*
1053 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1054 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1055 */
1056 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1057
1058 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1059 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1060 clock = clk_get(dev, clk_name);
1061 if (IS_ERR(clock)) {
1062 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1063 clk_name);
1064 ret = PTR_ERR(clock);
1065 break;
1066 }
1067 fmd->wbclk[i] = clock;
1068 }
1069 if (ret)
1070 fimc_md_put_clocks(fmd);
1071
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001072 return ret;
1073}
1074
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001075static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001076 struct fimc_sensor_info *s_info,
1077 bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001078{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001079 struct fimc_source_info *pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001080 struct fimc_camclk_info *camclk;
1081 int ret = 0;
1082
1083 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
1084 return -EINVAL;
1085
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001086 camclk = &fmd->camclk[pdata->clk_id];
1087
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001088 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1089 pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001090
1091 if (on) {
1092 if (camclk->use_count > 0 &&
1093 camclk->frequency != pdata->clk_frequency)
1094 return -EINVAL;
1095
1096 if (camclk->use_count++ == 0) {
1097 clk_set_rate(camclk->clock, pdata->clk_frequency);
1098 camclk->frequency = pdata->clk_frequency;
1099 ret = clk_enable(camclk->clock);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001100 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
1101 clk_get_rate(camclk->clock));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001102 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001103 return ret;
1104 }
1105
1106 if (WARN_ON(camclk->use_count == 0))
1107 return 0;
1108
1109 if (--camclk->use_count == 0) {
1110 clk_disable(camclk->clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001111 dbg("Disabled camclk %d", pdata->clk_id);
1112 }
1113 return ret;
1114}
1115
1116/**
1117 * fimc_md_set_camclk - peripheral sensor clock setup
1118 * @sd: sensor subdev to configure sclk_cam clock for
1119 * @on: 1 to enable or 0 to disable the clock
1120 *
1121 * There are 2 separate clock outputs available in the SoC for external
1122 * image processors. These clocks are shared between all registered FIMC
1123 * devices to which sensors can be attached, either directly or through
1124 * the MIPI CSI receiver. The clock is allowed here to be used by
1125 * multiple sensors concurrently if they use same frequency.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001126 * This function should only be called when the graph mutex is held.
1127 */
1128int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1129{
1130 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
1131 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1132
1133 return __fimc_md_set_camclk(fmd, s_info, on);
1134}
1135
1136static int fimc_md_link_notify(struct media_pad *source,
1137 struct media_pad *sink, u32 flags)
1138{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001139 struct fimc_lite *fimc_lite = NULL;
1140 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001141 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001142 struct v4l2_subdev *sd;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001143 struct mutex *lock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001144 int ret = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001145 int ref_count;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001146
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001147 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001148 return 0;
1149
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001150 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001151
1152 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001153 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001154 fimc_lite = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001155 if (WARN_ON(fimc_lite == NULL))
1156 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001157 pipeline = &fimc_lite->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001158 lock = &fimc_lite->lock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001159 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001160 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001161 fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001162 if (WARN_ON(fimc == NULL))
1163 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001164 pipeline = &fimc->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001165 lock = &fimc->lock;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001166 break;
1167 default:
1168 return 0;
1169 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001170
1171 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001172 int i;
1173 mutex_lock(lock);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -03001174 ret = __fimc_pipeline_close(pipeline);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001175 for (i = 0; i < IDX_MAX; i++)
1176 pipeline->subdevs[i] = NULL;
1177 if (fimc)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001178 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001179 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001180 return ret;
1181 }
1182 /*
1183 * Link activation. Enable power of pipeline elements only if the
1184 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001185 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001186 */
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001187 mutex_lock(lock);
1188
1189 ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;
1190 if (ref_count > 0)
1191 ret = __fimc_pipeline_open(pipeline, source->entity, true);
1192 if (!ret && fimc)
1193 ret = fimc_capture_ctrls_create(fimc);
1194
1195 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001196 return ret ? -EPIPE : ret;
1197}
1198
1199static ssize_t fimc_md_sysfs_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1201{
1202 struct platform_device *pdev = to_platform_device(dev);
1203 struct fimc_md *fmd = platform_get_drvdata(pdev);
1204
1205 if (fmd->user_subdev_api)
1206 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1207
1208 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1209}
1210
1211static ssize_t fimc_md_sysfs_store(struct device *dev,
1212 struct device_attribute *attr,
1213 const char *buf, size_t count)
1214{
1215 struct platform_device *pdev = to_platform_device(dev);
1216 struct fimc_md *fmd = platform_get_drvdata(pdev);
1217 bool subdev_api;
1218 int i;
1219
1220 if (!strcmp(buf, "vid-dev\n"))
1221 subdev_api = false;
1222 else if (!strcmp(buf, "sub-dev\n"))
1223 subdev_api = true;
1224 else
1225 return count;
1226
1227 fmd->user_subdev_api = subdev_api;
1228 for (i = 0; i < FIMC_MAX_DEVS; i++)
1229 if (fmd->fimc[i])
1230 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1231 return count;
1232}
1233/*
1234 * This device attribute is to select video pipeline configuration method.
1235 * There are following valid values:
1236 * vid-dev - for V4L2 video node API only, subdevice will be configured
1237 * by the host driver.
1238 * sub-dev - for media controller API, subdevs must be configured in user
1239 * space before starting streaming.
1240 */
1241static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1242 fimc_md_sysfs_show, fimc_md_sysfs_store);
1243
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001244static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1245{
1246 struct device *dev = &fmd->pdev->dev;
1247 struct fimc_pinctrl *pctl = &fmd->pinctl;
1248
1249 pctl->pinctrl = devm_pinctrl_get(dev);
1250 if (IS_ERR(pctl->pinctrl))
1251 return PTR_ERR(pctl->pinctrl);
1252
1253 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1254 PINCTRL_STATE_DEFAULT);
1255 if (IS_ERR(pctl->state_default))
1256 return PTR_ERR(pctl->state_default);
1257
1258 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1259 PINCTRL_STATE_IDLE);
1260 return 0;
1261}
1262
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001263static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001264{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001265 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001266 struct v4l2_device *v4l2_dev;
1267 struct fimc_md *fmd;
1268 int ret;
1269
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001270 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001271 if (!fmd)
1272 return -ENOMEM;
1273
1274 spin_lock_init(&fmd->slock);
1275 fmd->pdev = pdev;
1276
1277 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1278 sizeof(fmd->media_dev.model));
1279 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001280 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001281
1282 v4l2_dev = &fmd->v4l2_dev;
1283 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001284 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001285 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001286
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001287 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001288 if (ret < 0) {
1289 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001290 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001291 }
1292 ret = media_device_register(&fmd->media_dev);
1293 if (ret < 0) {
1294 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001295 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001296 }
1297 ret = fimc_md_get_clocks(fmd);
1298 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001299 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001300
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001301 fmd->user_subdev_api = (dev->of_node != NULL);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001302
1303 /* Protect the media graph while we're registering entities */
1304 mutex_lock(&fmd->media_dev.graph_mutex);
1305
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001306 ret = fimc_md_get_pinctrl(fmd);
1307 if (ret < 0) {
1308 if (ret != EPROBE_DEFER)
1309 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1310 goto err_unlock;
1311 }
1312
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001313 if (dev->of_node)
1314 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1315 else
1316 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1317 fimc_md_pdev_match);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001318 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001319 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001320
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -03001321 if (dev->platform_data || dev->of_node) {
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001322 ret = fimc_md_register_sensor_entities(fmd);
1323 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001324 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001325 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001326
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001327 ret = fimc_md_create_links(fmd);
1328 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001329 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001330 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1331 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001332 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001333
1334 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001335 if (ret)
1336 goto err_unlock;
1337
1338 platform_set_drvdata(pdev, fmd);
1339 mutex_unlock(&fmd->media_dev.graph_mutex);
1340 return 0;
1341
1342err_unlock:
1343 mutex_unlock(&fmd->media_dev.graph_mutex);
1344err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001345 media_device_unregister(&fmd->media_dev);
1346 fimc_md_put_clocks(fmd);
1347 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001348err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001349 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001350 return ret;
1351}
1352
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001353static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001354{
1355 struct fimc_md *fmd = platform_get_drvdata(pdev);
1356
1357 if (!fmd)
1358 return 0;
1359 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1360 fimc_md_unregister_entities(fmd);
1361 media_device_unregister(&fmd->media_dev);
1362 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001363 return 0;
1364}
1365
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001366static struct platform_device_id fimc_driver_ids[] __always_unused = {
1367 { .name = "s5p-fimc-md" },
1368 { },
1369};
1370MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1371
1372static const struct of_device_id fimc_md_of_match[] = {
1373 { .compatible = "samsung,fimc" },
1374 { },
1375};
1376MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1377
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001378static struct platform_driver fimc_md_driver = {
1379 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001380 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001381 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001382 .of_match_table = of_match_ptr(fimc_md_of_match),
1383 .name = "s5p-fimc-md",
1384 .owner = THIS_MODULE,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001385 }
1386};
1387
Sachin Kamat7e566be2012-05-26 11:11:54 -03001388static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001389{
1390 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001391
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001392 request_module("s5p-csis");
1393 ret = fimc_register_driver();
1394 if (ret)
1395 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001396
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001397 return platform_driver_register(&fimc_md_driver);
1398}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001399
1400static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001401{
1402 platform_driver_unregister(&fimc_md_driver);
1403 fimc_unregister_driver();
1404}
1405
1406module_init(fimc_md_init);
1407module_exit(fimc_md_exit);
1408
1409MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1410MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1411MODULE_LICENSE("GPL");
1412MODULE_VERSION("2.0.1");