blob: 1f29f51b256bcf33660c9c312e852c9b04d8265b [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;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300270 /*
271 * If FIMC bus type is not Writeback FIFO assume it is same
272 * as sensor_bus_type.
273 */
274 s_info->pdata.fimc_bus_type = s_info->pdata.sensor_bus_type;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300275
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300276 adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300277 if (!adapter) {
278 v4l2_warn(&fmd->v4l2_dev,
279 "Failed to get I2C adapter %d, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300280 s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300281 return ERR_PTR(-EPROBE_DEFER);
282 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300283 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300284 s_info->pdata.board_info, NULL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300285 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300286 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300287 v4l2_warn(&fmd->v4l2_dev,
288 "Failed to acquire subdev %s, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300289 s_info->pdata.board_info->type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300290 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300291 }
292 v4l2_set_subdev_hostdata(sd, s_info);
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300293 sd->grp_id = GRP_ID_SENSOR;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300294
295 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300296 sd->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300297 return sd;
298}
299
300static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
301{
302 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300303 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300304
305 if (!client)
306 return;
307 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300308
309 if (!client->dev.of_node) {
310 adapter = client->adapter;
311 i2c_unregister_device(client);
312 if (adapter)
313 i2c_put_adapter(adapter);
314 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300315}
316
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300317#ifdef CONFIG_OF
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300318/* Register I2C client subdev associated with @node. */
319static int fimc_md_of_add_sensor(struct fimc_md *fmd,
320 struct device_node *node, int index)
321{
322 struct fimc_sensor_info *si;
323 struct i2c_client *client;
324 struct v4l2_subdev *sd;
325 int ret;
326
327 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
328 return -EINVAL;
329 si = &fmd->sensor[index];
330
331 client = of_find_i2c_device_by_node(node);
332 if (!client)
333 return -EPROBE_DEFER;
334
335 device_lock(&client->dev);
336
337 if (!client->driver ||
338 !try_module_get(client->driver->driver.owner)) {
339 ret = -EPROBE_DEFER;
340 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
341 node->full_name);
342 goto dev_put;
343 }
344
345 /* Enable sensor's master clock */
346 ret = __fimc_md_set_camclk(fmd, si, true);
347 if (ret < 0)
348 goto mod_put;
349 sd = i2c_get_clientdata(client);
350
351 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
352 __fimc_md_set_camclk(fmd, si, false);
353 if (ret < 0)
354 goto mod_put;
355
356 v4l2_set_subdev_hostdata(sd, si);
357 sd->grp_id = GRP_ID_SENSOR;
358 si->subdev = sd;
359 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
360 sd->name, fmd->num_sensors);
361 fmd->num_sensors++;
362
363mod_put:
364 module_put(client->driver->driver.owner);
365dev_put:
366 device_unlock(&client->dev);
367 put_device(&client->dev);
368 return ret;
369}
370
371/* Parse port node and register as a sub-device any sensor specified there. */
372static int fimc_md_parse_port_node(struct fimc_md *fmd,
373 struct device_node *port,
374 unsigned int index)
375{
376 struct device_node *rem, *ep, *np;
377 struct fimc_source_info *pd;
378 struct v4l2_of_endpoint endpoint;
379 int ret;
380 u32 val;
381
382 pd = &fmd->sensor[index].pdata;
383
384 /* Assume here a port node can have only one endpoint node. */
385 ep = of_get_next_child(port, NULL);
386 if (!ep)
387 return 0;
388
389 v4l2_of_parse_endpoint(ep, &endpoint);
390 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
391 return -EINVAL;
392
393 pd->mux_id = (endpoint.port - 1) & 0x1;
394
395 rem = v4l2_of_get_remote_port_parent(ep);
396 of_node_put(ep);
397 if (rem == NULL) {
398 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
399 ep->full_name);
400 return 0;
401 }
402 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
403 pd->clk_id = val;
404
405 if (!of_property_read_u32(rem, "clock-frequency", &val))
406 pd->clk_frequency = val;
407
408 if (pd->clk_frequency == 0) {
409 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
410 rem->full_name);
411 of_node_put(rem);
412 return -EINVAL;
413 }
414
415 if (fimc_input_is_parallel(endpoint.port)) {
416 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
417 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
418 else
419 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
420 pd->flags = endpoint.bus.parallel.flags;
421 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
422 /*
423 * MIPI CSI-2: only input mux selection and
424 * the sensor's clock frequency is needed.
425 */
426 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
427 } else {
428 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
429 endpoint.port, rem->full_name);
430 }
431 /*
432 * For FIMC-IS handled sensors, that are placed under i2c-isp device
433 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
434 * input. Sensors are attached to the FIMC-LITE hostdata interface
435 * directly or through MIPI-CSIS, depending on the external media bus
436 * used. This needs to be handled in a more reliable way, not by just
437 * checking parent's node name.
438 */
439 np = of_get_parent(rem);
440
441 if (np && !of_node_cmp(np->name, "i2c-isp"))
442 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
443 else
444 pd->fimc_bus_type = pd->sensor_bus_type;
445
446 ret = fimc_md_of_add_sensor(fmd, rem, index);
447 of_node_put(rem);
448
449 return ret;
450}
451
452/* Register all SoC external sub-devices */
453static int fimc_md_of_sensors_register(struct fimc_md *fmd,
454 struct device_node *np)
455{
456 struct device_node *parent = fmd->pdev->dev.of_node;
457 struct device_node *node, *ports;
458 int index = 0;
459 int ret;
460
461 /* Attach sensors linked to MIPI CSI-2 receivers */
462 for_each_available_child_of_node(parent, node) {
463 struct device_node *port;
464
465 if (of_node_cmp(node->name, "csis"))
466 continue;
467 /* The csis node can have only port subnode. */
468 port = of_get_next_child(node, NULL);
469 if (!port)
470 continue;
471
472 ret = fimc_md_parse_port_node(fmd, port, index);
473 if (ret < 0)
474 return ret;
475 index++;
476 }
477
478 /* Attach sensors listed in the parallel-ports node */
479 ports = of_get_child_by_name(parent, "parallel-ports");
480 if (!ports)
481 return 0;
482
483 for_each_child_of_node(ports, node) {
484 ret = fimc_md_parse_port_node(fmd, node, index);
485 if (ret < 0)
486 break;
487 index++;
488 }
489
490 return 0;
491}
492
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300493static int __of_get_csis_id(struct device_node *np)
494{
495 u32 reg = 0;
496
497 np = of_get_child_by_name(np, "port");
498 if (!np)
499 return -EINVAL;
500 of_property_read_u32(np, "reg", &reg);
501 return reg - FIMC_INPUT_MIPI_CSI2_0;
502}
503#else
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300504#define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300505#define __of_get_csis_id(np) (-ENOSYS)
506#endif
507
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300508static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
509{
510 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300511 struct device_node *of_node = fmd->pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300512 struct fimc_dev *fd = NULL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300513 int num_clients = 0;
514 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300515
516 /*
517 * Runtime resume one of the FIMC entities to make sure
518 * the sclk_cam clocks are not globally disabled.
519 */
520 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
521 if (fmd->fimc[i])
522 fd = fmd->fimc[i];
523 if (!fd)
524 return -ENXIO;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300525
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300526 ret = pm_runtime_get_sync(&fd->pdev->dev);
527 if (ret < 0)
528 return ret;
529
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300530 if (of_node) {
531 fmd->num_sensors = 0;
532 ret = fimc_md_of_sensors_register(fmd, of_node);
533 } else if (pdata) {
534 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
535 num_clients = min_t(u32, pdata->num_clients,
536 ARRAY_SIZE(fmd->sensor));
537 fmd->num_sensors = num_clients;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300538
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300539 for (i = 0; i < num_clients; i++) {
540 struct v4l2_subdev *sd;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300541
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300542 fmd->sensor[i].pdata = pdata->source_info[i];
543 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
544 if (ret)
545 break;
546 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
547 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300548
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300549 if (IS_ERR(sd)) {
550 fmd->sensor[i].subdev = NULL;
551 ret = PTR_ERR(sd);
552 break;
553 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300554 fmd->sensor[i].subdev = sd;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300555 if (ret)
556 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300557 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300558 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300559
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300560 pm_runtime_put(&fd->pdev->dev);
561 return ret;
562}
563
564/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300565 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300566 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300567
568static int register_fimc_lite_entity(struct fimc_md *fmd,
569 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300570{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300571 struct v4l2_subdev *sd;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300572 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300573
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300574 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
575 fmd->fimc_lite[fimc_lite->index]))
576 return -EBUSY;
577
578 sd = &fimc_lite->subdev;
579 sd->grp_id = GRP_ID_FLITE;
580 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
581
582 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
583 if (!ret)
584 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
585 else
586 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
587 fimc_lite->index);
588 return ret;
589}
590
591static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
592{
593 struct v4l2_subdev *sd;
594 int ret;
595
596 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
597 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300598
Sachin Kamat8163ec02012-10-25 10:56:00 -0300599 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300600 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -0300601 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300602
603 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300604 if (!ret) {
605 fmd->fimc[fimc->id] = fimc;
606 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
607 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300608 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
609 fimc->id, ret);
610 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300611 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300612}
613
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300614static int register_csis_entity(struct fimc_md *fmd,
615 struct platform_device *pdev,
616 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300617{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300618 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300619 int id, ret;
620
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300621 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300622
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300623 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
624 return -ENOENT;
625
626 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300627 return -EBUSY;
628
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300629 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300630 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300631 if (!ret)
632 fmd->csis[id].sd = sd;
633 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300634 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300635 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300636 return ret;
637}
638
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300639static int fimc_md_register_platform_entity(struct fimc_md *fmd,
640 struct platform_device *pdev,
641 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300642{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300643 struct device *dev = &pdev->dev;
644 int ret = -EPROBE_DEFER;
645 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300646
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300647 /* Lock to ensure dev->driver won't change. */
648 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300649
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300650 if (!dev->driver || !try_module_get(dev->driver->owner))
651 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300652
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300653 drvdata = dev_get_drvdata(dev);
654 /* Some subdev didn't probe succesfully id drvdata is NULL */
655 if (drvdata) {
656 switch (plat_entity) {
657 case IDX_FIMC:
658 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300659 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300660 case IDX_FLITE:
661 ret = register_fimc_lite_entity(fmd, drvdata);
662 break;
663 case IDX_CSIS:
664 ret = register_csis_entity(fmd, pdev, drvdata);
665 break;
666 default:
667 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300668 }
669 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300670
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300671 module_put(dev->driver->owner);
672dev_unlock:
673 device_unlock(dev);
674 if (ret == -EPROBE_DEFER)
675 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
676 dev_name(dev));
677 else if (ret < 0)
678 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
679 dev_name(dev), ret);
680 return ret;
681}
682
683static int fimc_md_pdev_match(struct device *dev, void *data)
684{
685 struct platform_device *pdev = to_platform_device(dev);
686 int plat_entity = -1;
687 int ret;
688 char *p;
689
690 if (!get_device(dev))
691 return -ENODEV;
692
693 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
694 plat_entity = IDX_CSIS;
695 } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) {
696 plat_entity = IDX_FLITE;
697 } else {
698 p = strstr(pdev->name, "fimc");
699 if (p && *(p + 4) == 0)
700 plat_entity = IDX_FIMC;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300701 }
702
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300703 if (plat_entity >= 0)
704 ret = fimc_md_register_platform_entity(data, pdev,
705 plat_entity);
706 put_device(dev);
707 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300708}
709
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300710/* Register FIMC, FIMC-LITE and CSIS media entities */
711#ifdef CONFIG_OF
712static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
713 struct device_node *parent)
714{
715 struct device_node *node;
716 int ret = 0;
717
718 for_each_available_child_of_node(parent, node) {
719 struct platform_device *pdev;
720 int plat_entity = -1;
721
722 pdev = of_find_device_by_node(node);
723 if (!pdev)
724 continue;
725
726 /* If driver of any entity isn't ready try all again later. */
727 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
728 plat_entity = IDX_CSIS;
729 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
730 plat_entity = IDX_FLITE;
731 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
732 !of_property_read_bool(node, "samsung,lcd-wb"))
733 plat_entity = IDX_FIMC;
734
735 if (plat_entity >= 0)
736 ret = fimc_md_register_platform_entity(fmd, pdev,
737 plat_entity);
738 put_device(&pdev->dev);
739 if (ret < 0)
740 break;
741 }
742
743 return ret;
744}
745#else
746#define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
747#endif
748
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300749static void fimc_md_unregister_entities(struct fimc_md *fmd)
750{
751 int i;
752
753 for (i = 0; i < FIMC_MAX_DEVS; i++) {
754 if (fmd->fimc[i] == NULL)
755 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300756 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300757 fmd->fimc[i]->pipeline_ops = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300758 fmd->fimc[i] = NULL;
759 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300760 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
761 if (fmd->fimc_lite[i] == NULL)
762 continue;
763 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
Sylwester Nawrocki248ac362013-01-11 07:06:28 -0300764 fmd->fimc_lite[i]->pipeline_ops = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300765 fmd->fimc_lite[i] = NULL;
766 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300767 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
768 if (fmd->csis[i].sd == NULL)
769 continue;
770 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300771 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300772 fmd->csis[i].sd = NULL;
773 }
774 for (i = 0; i < fmd->num_sensors; i++) {
775 if (fmd->sensor[i].subdev == NULL)
776 continue;
777 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
778 fmd->sensor[i].subdev = NULL;
779 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300780 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300781}
782
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300783/**
784 * __fimc_md_create_fimc_links - create links to all FIMC entities
785 * @fmd: fimc media device
786 * @source: the source entity to create links to all fimc entities from
787 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
788 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300789 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300790 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300791static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
792 struct media_entity *source,
793 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300794 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300795{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300796 struct fimc_sensor_info *s_info = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300797 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300798 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300799 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300800
801 for (i = 0; i < FIMC_MAX_DEVS; i++) {
802 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300803 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300804 /*
805 * Some FIMC variants are not fitted with camera capture
806 * interface. Skip creating a link from sensor for those.
807 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300808 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300809 continue;
810
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300811 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300812
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300813 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300814 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300815 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300816 if (ret)
817 return ret;
818
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300819 /* Notify FIMC capture subdev entity */
820 ret = media_entity_call(sink, link_setup, &sink->pads[0],
821 &source->pads[pad], flags);
822 if (ret)
823 break;
824
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300825 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300826 source->name, flags ? '=' : '-', sink->name);
827
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300828 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300829 continue;
830 s_info = v4l2_get_subdev_hostdata(sensor);
831 if (!WARN_ON(s_info == NULL)) {
832 unsigned long irq_flags;
833 spin_lock_irqsave(&fmd->slock, irq_flags);
834 s_info->host = fmd->fimc[i];
835 spin_unlock_irqrestore(&fmd->slock, irq_flags);
836 }
837 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300838
839 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
840 if (!fmd->fimc_lite[i])
841 continue;
842
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300843 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
844 flags = MEDIA_LNK_FL_ENABLED;
845 else
846 flags = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300847
848 sink = &fmd->fimc_lite[i]->subdev.entity;
849 ret = media_entity_create_link(source, pad, sink,
850 FLITE_SD_PAD_SINK, flags);
851 if (ret)
852 return ret;
853
854 /* Notify FIMC-LITE subdev entity */
855 ret = media_entity_call(sink, link_setup, &sink->pads[0],
856 &source->pads[pad], flags);
857 if (ret)
858 break;
859
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300860 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300861 source->name, flags ? '=' : '-', sink->name);
862 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300863 return 0;
864}
865
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300866/* Create links from FIMC-LITE source pads to other entities */
867static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
868{
869 struct media_entity *source, *sink;
870 unsigned int flags = MEDIA_LNK_FL_ENABLED;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300871 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300872
873 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
874 struct fimc_lite *fimc = fmd->fimc_lite[i];
875 if (fimc == NULL)
876 continue;
877 source = &fimc->subdev.entity;
Sylwester Nawrocki1bcd7042012-07-26 07:13:08 -0300878 sink = &fimc->vfd.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300879 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300880 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300881 sink, 0, flags);
882 if (ret)
883 break;
884 /* TODO: create links to other entities */
885 }
886
887 return ret;
888}
889
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300890/**
891 * fimc_md_create_links - create default links between registered entities
892 *
893 * Parallel interface sensor entities are connected directly to FIMC capture
894 * entities. The sensors using MIPI CSIS bus are connected through immutable
895 * link with CSI receiver entity specified by mux_id. Any registered CSIS
896 * entity has a link to each registered FIMC capture entity. Enabled links
897 * are created by default between each subsequent registered sensor and
898 * subsequent FIMC capture entity. The number of default active links is
899 * determined by the number of available sensors or FIMC entities,
900 * whichever is less.
901 */
902static int fimc_md_create_links(struct fimc_md *fmd)
903{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300904 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300905 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300906 struct fimc_source_info *pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300907 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300908 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300909 int i, pad, fimc_id = 0, ret = 0;
910 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300911
912 for (i = 0; i < fmd->num_sensors; i++) {
913 if (fmd->sensor[i].subdev == NULL)
914 continue;
915
916 sensor = fmd->sensor[i].subdev;
917 s_info = v4l2_get_subdev_hostdata(sensor);
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300918 if (!s_info)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300919 continue;
920
921 source = NULL;
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300922 pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300923
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300924 switch (pdata->sensor_bus_type) {
925 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300926 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
927 "Wrong CSI channel id: %d\n", pdata->mux_id))
928 return -EINVAL;
929
930 csis = fmd->csis[pdata->mux_id].sd;
931 if (WARN(csis == NULL,
932 "MIPI-CSI interface specified "
933 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300934 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300935
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300936 pad = sensor->entity.num_pads - 1;
937 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300938 &csis->entity, CSIS_PAD_SINK,
939 MEDIA_LNK_FL_IMMUTABLE |
940 MEDIA_LNK_FL_ENABLED);
941 if (ret)
942 return ret;
943
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300944 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300945 sensor->entity.name, csis->entity.name);
946
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300947 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300948 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300949 break;
950
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300951 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300952 source = &sensor->entity;
953 pad = 0;
954 break;
955
956 default:
957 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300958 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300959 return -EINVAL;
960 }
961 if (source == NULL)
962 continue;
963
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300964 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300965 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300966 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300967 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300968
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300969 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300970 if (fmd->csis[i].sd == NULL)
971 continue;
972 source = &fmd->csis[i].sd->entity;
973 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300974 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300975
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300976 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300977 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300978 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300979 }
980
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300981 /* Create immutable links between each FIMC's subdev and video node */
982 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
983 for (i = 0; i < FIMC_MAX_DEVS; i++) {
984 if (!fmd->fimc[i])
985 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300986 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -0300987 sink = &fmd->fimc[i]->vid_cap.vfd.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300988 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
989 sink, 0, flags);
990 if (ret)
991 break;
992 }
993
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300994 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300995}
996
997/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300998 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300999 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001000static void fimc_md_put_clocks(struct fimc_md *fmd)
1001{
1002 int i = FIMC_MAX_CAMCLKS;
1003
1004 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001005 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001006 continue;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001007 clk_unprepare(fmd->camclk[i].clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001008 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001009 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001010 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001011
1012 /* Writeback (PIXELASYNCMx) clocks */
1013 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1014 if (IS_ERR(fmd->wbclk[i]))
1015 continue;
1016 clk_put(fmd->wbclk[i]);
1017 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1018 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001019}
1020
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001021static int fimc_md_get_clocks(struct fimc_md *fmd)
1022{
1023 struct device *dev = NULL;
1024 char clk_name[32];
1025 struct clk *clock;
1026 int ret, i;
1027
1028 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1029 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1030
1031 if (fmd->pdev->dev.of_node)
1032 dev = &fmd->pdev->dev;
1033
1034 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1035 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1036 clock = clk_get(dev, clk_name);
1037
1038 if (IS_ERR(clock)) {
1039 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1040 clk_name);
1041 ret = PTR_ERR(clock);
1042 break;
1043 }
1044 ret = clk_prepare(clock);
1045 if (ret < 0) {
1046 clk_put(clock);
1047 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1048 break;
1049 }
1050 fmd->camclk[i].clock = clock;
1051 }
1052 if (ret)
1053 fimc_md_put_clocks(fmd);
1054
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001055 if (!fmd->use_isp)
1056 return 0;
1057 /*
1058 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1059 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1060 */
1061 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1062
1063 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1064 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1065 clock = clk_get(dev, clk_name);
1066 if (IS_ERR(clock)) {
1067 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1068 clk_name);
1069 ret = PTR_ERR(clock);
1070 break;
1071 }
1072 fmd->wbclk[i] = clock;
1073 }
1074 if (ret)
1075 fimc_md_put_clocks(fmd);
1076
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001077 return ret;
1078}
1079
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001080static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001081 struct fimc_sensor_info *s_info,
1082 bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001083{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001084 struct fimc_source_info *pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001085 struct fimc_camclk_info *camclk;
1086 int ret = 0;
1087
1088 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
1089 return -EINVAL;
1090
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001091 camclk = &fmd->camclk[pdata->clk_id];
1092
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001093 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1094 pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001095
1096 if (on) {
1097 if (camclk->use_count > 0 &&
1098 camclk->frequency != pdata->clk_frequency)
1099 return -EINVAL;
1100
1101 if (camclk->use_count++ == 0) {
1102 clk_set_rate(camclk->clock, pdata->clk_frequency);
1103 camclk->frequency = pdata->clk_frequency;
1104 ret = clk_enable(camclk->clock);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001105 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
1106 clk_get_rate(camclk->clock));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001107 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001108 return ret;
1109 }
1110
1111 if (WARN_ON(camclk->use_count == 0))
1112 return 0;
1113
1114 if (--camclk->use_count == 0) {
1115 clk_disable(camclk->clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001116 dbg("Disabled camclk %d", pdata->clk_id);
1117 }
1118 return ret;
1119}
1120
1121/**
1122 * fimc_md_set_camclk - peripheral sensor clock setup
1123 * @sd: sensor subdev to configure sclk_cam clock for
1124 * @on: 1 to enable or 0 to disable the clock
1125 *
1126 * There are 2 separate clock outputs available in the SoC for external
1127 * image processors. These clocks are shared between all registered FIMC
1128 * devices to which sensors can be attached, either directly or through
1129 * the MIPI CSI receiver. The clock is allowed here to be used by
1130 * multiple sensors concurrently if they use same frequency.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001131 * This function should only be called when the graph mutex is held.
1132 */
1133int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1134{
1135 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
1136 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1137
1138 return __fimc_md_set_camclk(fmd, s_info, on);
1139}
1140
1141static int fimc_md_link_notify(struct media_pad *source,
1142 struct media_pad *sink, u32 flags)
1143{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001144 struct fimc_lite *fimc_lite = NULL;
1145 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001146 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001147 struct v4l2_subdev *sd;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001148 struct mutex *lock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001149 int ret = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001150 int ref_count;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001151
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001152 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001153 return 0;
1154
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001155 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001156
1157 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001158 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001159 fimc_lite = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001160 if (WARN_ON(fimc_lite == NULL))
1161 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001162 pipeline = &fimc_lite->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001163 lock = &fimc_lite->lock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001164 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001165 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001166 fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001167 if (WARN_ON(fimc == NULL))
1168 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001169 pipeline = &fimc->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001170 lock = &fimc->lock;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001171 break;
1172 default:
1173 return 0;
1174 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001175
1176 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001177 int i;
1178 mutex_lock(lock);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -03001179 ret = __fimc_pipeline_close(pipeline);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001180 for (i = 0; i < IDX_MAX; i++)
1181 pipeline->subdevs[i] = NULL;
1182 if (fimc)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001183 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001184 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001185 return ret;
1186 }
1187 /*
1188 * Link activation. Enable power of pipeline elements only if the
1189 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001190 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001191 */
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001192 mutex_lock(lock);
1193
1194 ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;
1195 if (ref_count > 0)
1196 ret = __fimc_pipeline_open(pipeline, source->entity, true);
1197 if (!ret && fimc)
1198 ret = fimc_capture_ctrls_create(fimc);
1199
1200 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001201 return ret ? -EPIPE : ret;
1202}
1203
1204static ssize_t fimc_md_sysfs_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1206{
1207 struct platform_device *pdev = to_platform_device(dev);
1208 struct fimc_md *fmd = platform_get_drvdata(pdev);
1209
1210 if (fmd->user_subdev_api)
1211 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1212
1213 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1214}
1215
1216static ssize_t fimc_md_sysfs_store(struct device *dev,
1217 struct device_attribute *attr,
1218 const char *buf, size_t count)
1219{
1220 struct platform_device *pdev = to_platform_device(dev);
1221 struct fimc_md *fmd = platform_get_drvdata(pdev);
1222 bool subdev_api;
1223 int i;
1224
1225 if (!strcmp(buf, "vid-dev\n"))
1226 subdev_api = false;
1227 else if (!strcmp(buf, "sub-dev\n"))
1228 subdev_api = true;
1229 else
1230 return count;
1231
1232 fmd->user_subdev_api = subdev_api;
1233 for (i = 0; i < FIMC_MAX_DEVS; i++)
1234 if (fmd->fimc[i])
1235 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1236 return count;
1237}
1238/*
1239 * This device attribute is to select video pipeline configuration method.
1240 * There are following valid values:
1241 * vid-dev - for V4L2 video node API only, subdevice will be configured
1242 * by the host driver.
1243 * sub-dev - for media controller API, subdevs must be configured in user
1244 * space before starting streaming.
1245 */
1246static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1247 fimc_md_sysfs_show, fimc_md_sysfs_store);
1248
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001249static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1250{
1251 struct device *dev = &fmd->pdev->dev;
1252 struct fimc_pinctrl *pctl = &fmd->pinctl;
1253
1254 pctl->pinctrl = devm_pinctrl_get(dev);
1255 if (IS_ERR(pctl->pinctrl))
1256 return PTR_ERR(pctl->pinctrl);
1257
1258 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1259 PINCTRL_STATE_DEFAULT);
1260 if (IS_ERR(pctl->state_default))
1261 return PTR_ERR(pctl->state_default);
1262
1263 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1264 PINCTRL_STATE_IDLE);
1265 return 0;
1266}
1267
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001268static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001269{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001270 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001271 struct v4l2_device *v4l2_dev;
1272 struct fimc_md *fmd;
1273 int ret;
1274
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001275 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001276 if (!fmd)
1277 return -ENOMEM;
1278
1279 spin_lock_init(&fmd->slock);
1280 fmd->pdev = pdev;
1281
1282 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1283 sizeof(fmd->media_dev.model));
1284 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001285 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001286
1287 v4l2_dev = &fmd->v4l2_dev;
1288 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001289 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001290 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001291
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001292 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001293 if (ret < 0) {
1294 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001295 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001296 }
1297 ret = media_device_register(&fmd->media_dev);
1298 if (ret < 0) {
1299 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001300 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001301 }
1302 ret = fimc_md_get_clocks(fmd);
1303 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001304 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001305
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001306 fmd->user_subdev_api = (dev->of_node != NULL);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001307
1308 /* Protect the media graph while we're registering entities */
1309 mutex_lock(&fmd->media_dev.graph_mutex);
1310
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001311 ret = fimc_md_get_pinctrl(fmd);
1312 if (ret < 0) {
1313 if (ret != EPROBE_DEFER)
1314 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1315 goto err_unlock;
1316 }
1317
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001318 if (dev->of_node)
1319 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1320 else
1321 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1322 fimc_md_pdev_match);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001323 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001324 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001325
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -03001326 if (dev->platform_data || dev->of_node) {
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001327 ret = fimc_md_register_sensor_entities(fmd);
1328 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001329 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001330 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001331
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001332 ret = fimc_md_create_links(fmd);
1333 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001334 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001335 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1336 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001337 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001338
1339 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001340 if (ret)
1341 goto err_unlock;
1342
1343 platform_set_drvdata(pdev, fmd);
1344 mutex_unlock(&fmd->media_dev.graph_mutex);
1345 return 0;
1346
1347err_unlock:
1348 mutex_unlock(&fmd->media_dev.graph_mutex);
1349err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001350 media_device_unregister(&fmd->media_dev);
1351 fimc_md_put_clocks(fmd);
1352 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001353err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001354 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001355 return ret;
1356}
1357
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001358static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001359{
1360 struct fimc_md *fmd = platform_get_drvdata(pdev);
1361
1362 if (!fmd)
1363 return 0;
1364 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1365 fimc_md_unregister_entities(fmd);
1366 media_device_unregister(&fmd->media_dev);
1367 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001368 return 0;
1369}
1370
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001371static struct platform_device_id fimc_driver_ids[] __always_unused = {
1372 { .name = "s5p-fimc-md" },
1373 { },
1374};
1375MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1376
1377static const struct of_device_id fimc_md_of_match[] = {
1378 { .compatible = "samsung,fimc" },
1379 { },
1380};
1381MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1382
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001383static struct platform_driver fimc_md_driver = {
1384 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001385 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001386 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001387 .of_match_table = of_match_ptr(fimc_md_of_match),
1388 .name = "s5p-fimc-md",
1389 .owner = THIS_MODULE,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001390 }
1391};
1392
Sachin Kamat7e566be2012-05-26 11:11:54 -03001393static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001394{
1395 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001396
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001397 request_module("s5p-csis");
1398 ret = fimc_register_driver();
1399 if (ret)
1400 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001401
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001402 return platform_driver_register(&fimc_md_driver);
1403}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001404
1405static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001406{
1407 platform_driver_unregister(&fimc_md_driver);
1408 fimc_unregister_driver();
1409}
1410
1411module_init(fimc_md_init);
1412module_exit(fimc_md_exit);
1413
1414MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1415MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1416MODULE_LICENSE("GPL");
1417MODULE_VERSION("2.0.1");