blob: 77075a4d1145e3e1f0b1229d705c99ab7b44ba0c [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 Nawrockif8bca4f2012-11-20 08:54:22 -0300131static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300132{
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300133 static const u8 seq[2][IDX_MAX - 1] = {
134 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
135 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
136 };
137 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300138
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300139 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300140 return -ENXIO;
141
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300142 for (i = 0; i < IDX_MAX - 1; i++) {
143 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300144
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300145 ret = __subdev_set_power(p->subdevs[idx], on);
146
147
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300148 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300149 goto error;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300150 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300151 return 0;
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300152error:
153 for (; i >= 0; i--) {
154 unsigned int idx = seq[on][i];
155 __subdev_set_power(p->subdevs[idx], !on);
156 }
157 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300158}
159
160/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300161 * __fimc_pipeline_open - update the pipeline information, enable power
162 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300163 * @me: media entity to start graph walk with
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300164 * @prepare: true to walk the current pipeline and acquire all subdevs
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300165 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300166 * Called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300167 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300168static int __fimc_pipeline_open(struct fimc_pipeline *p,
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300169 struct media_entity *me, bool prepare)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300170{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300171 struct fimc_md *fmd = entity_to_fimc_mdev(me);
172 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300173 int ret;
174
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300175 if (WARN_ON(p == NULL || me == NULL))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300176 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300177
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300178 if (prepare)
179 fimc_pipeline_prepare(p, me);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300180
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300181 sd = p->subdevs[IDX_SENSOR];
182 if (sd == NULL)
183 return -EINVAL;
184
185 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
186 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
187 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
188 if (ret < 0)
189 return ret;
190 }
191 ret = fimc_md_set_camclk(sd, true);
192 if (ret < 0)
193 goto err_wbclk;
194
195 ret = fimc_pipeline_s_power(p, 1);
196 if (!ret)
197 return 0;
198
199 fimc_md_set_camclk(sd, false);
200
201err_wbclk:
202 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
203 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
204
205 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300206}
207
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300208/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300209 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300210 * @fimc: fimc device terminating the pipeline
211 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300212 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300213 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300214static int __fimc_pipeline_close(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300215{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300216 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
217 struct fimc_md *fmd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300218 int ret = 0;
219
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300220 if (WARN_ON(sd == NULL))
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300221 return -EINVAL;
222
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300223 if (p->subdevs[IDX_SENSOR]) {
224 ret = fimc_pipeline_s_power(p, 0);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300225 fimc_md_set_camclk(sd, false);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300226 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300227
228 fmd = entity_to_fimc_mdev(&sd->entity);
229
230 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
231 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
232 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
233
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300234 return ret == -ENXIO ? 0 : ret;
235}
236
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300237/**
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300238 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300239 * @pipeline: video pipeline structure
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300240 * @on: passed as the s_stream() callback argument
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300241 */
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300242static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300243{
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300244 static const u8 seq[2][IDX_MAX] = {
245 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
246 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
247 };
248 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300249
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300250 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300251 return -ENODEV;
252
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300253 for (i = 0; i < IDX_MAX; i++) {
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300254 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300255
256 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
257
258 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300259 goto error;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300260 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300261 return 0;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300262error:
263 for (; i >= 0; i--) {
264 unsigned int idx = seq[on][i];
265 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
266 }
267 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300268}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300269
270/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
271static const struct fimc_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300272 .open = __fimc_pipeline_open,
273 .close = __fimc_pipeline_close,
274 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300275};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300276
277/*
278 * Sensor subdevice helper functions
279 */
280static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
281 struct fimc_sensor_info *s_info)
282{
283 struct i2c_adapter *adapter;
284 struct v4l2_subdev *sd = NULL;
285
286 if (!s_info || !fmd)
287 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300288 /*
289 * If FIMC bus type is not Writeback FIFO assume it is same
290 * as sensor_bus_type.
291 */
292 s_info->pdata.fimc_bus_type = s_info->pdata.sensor_bus_type;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300293
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300294 adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300295 if (!adapter) {
296 v4l2_warn(&fmd->v4l2_dev,
297 "Failed to get I2C adapter %d, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300298 s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300299 return ERR_PTR(-EPROBE_DEFER);
300 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300301 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300302 s_info->pdata.board_info, NULL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300303 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300304 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300305 v4l2_warn(&fmd->v4l2_dev,
306 "Failed to acquire subdev %s, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300307 s_info->pdata.board_info->type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300308 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300309 }
310 v4l2_set_subdev_hostdata(sd, s_info);
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300311 sd->grp_id = GRP_ID_SENSOR;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300312
313 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300314 sd->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300315 return sd;
316}
317
318static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
319{
320 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300321 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300322
323 if (!client)
324 return;
325 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300326
327 if (!client->dev.of_node) {
328 adapter = client->adapter;
329 i2c_unregister_device(client);
330 if (adapter)
331 i2c_put_adapter(adapter);
332 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300333}
334
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300335#ifdef CONFIG_OF
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300336/* Register I2C client subdev associated with @node. */
337static int fimc_md_of_add_sensor(struct fimc_md *fmd,
338 struct device_node *node, int index)
339{
340 struct fimc_sensor_info *si;
341 struct i2c_client *client;
342 struct v4l2_subdev *sd;
343 int ret;
344
345 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
346 return -EINVAL;
347 si = &fmd->sensor[index];
348
349 client = of_find_i2c_device_by_node(node);
350 if (!client)
351 return -EPROBE_DEFER;
352
353 device_lock(&client->dev);
354
355 if (!client->driver ||
356 !try_module_get(client->driver->driver.owner)) {
357 ret = -EPROBE_DEFER;
358 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
359 node->full_name);
360 goto dev_put;
361 }
362
363 /* Enable sensor's master clock */
364 ret = __fimc_md_set_camclk(fmd, si, true);
365 if (ret < 0)
366 goto mod_put;
367 sd = i2c_get_clientdata(client);
368
369 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
370 __fimc_md_set_camclk(fmd, si, false);
371 if (ret < 0)
372 goto mod_put;
373
374 v4l2_set_subdev_hostdata(sd, si);
375 sd->grp_id = GRP_ID_SENSOR;
376 si->subdev = sd;
377 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
378 sd->name, fmd->num_sensors);
379 fmd->num_sensors++;
380
381mod_put:
382 module_put(client->driver->driver.owner);
383dev_put:
384 device_unlock(&client->dev);
385 put_device(&client->dev);
386 return ret;
387}
388
389/* Parse port node and register as a sub-device any sensor specified there. */
390static int fimc_md_parse_port_node(struct fimc_md *fmd,
391 struct device_node *port,
392 unsigned int index)
393{
394 struct device_node *rem, *ep, *np;
395 struct fimc_source_info *pd;
396 struct v4l2_of_endpoint endpoint;
397 int ret;
398 u32 val;
399
400 pd = &fmd->sensor[index].pdata;
401
402 /* Assume here a port node can have only one endpoint node. */
403 ep = of_get_next_child(port, NULL);
404 if (!ep)
405 return 0;
406
407 v4l2_of_parse_endpoint(ep, &endpoint);
408 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
409 return -EINVAL;
410
411 pd->mux_id = (endpoint.port - 1) & 0x1;
412
413 rem = v4l2_of_get_remote_port_parent(ep);
414 of_node_put(ep);
415 if (rem == NULL) {
416 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
417 ep->full_name);
418 return 0;
419 }
420 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
421 pd->clk_id = val;
422
423 if (!of_property_read_u32(rem, "clock-frequency", &val))
424 pd->clk_frequency = val;
425
426 if (pd->clk_frequency == 0) {
427 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
428 rem->full_name);
429 of_node_put(rem);
430 return -EINVAL;
431 }
432
433 if (fimc_input_is_parallel(endpoint.port)) {
434 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
435 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
436 else
437 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
438 pd->flags = endpoint.bus.parallel.flags;
439 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
440 /*
441 * MIPI CSI-2: only input mux selection and
442 * the sensor's clock frequency is needed.
443 */
444 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
445 } else {
446 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
447 endpoint.port, rem->full_name);
448 }
449 /*
450 * For FIMC-IS handled sensors, that are placed under i2c-isp device
451 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
452 * input. Sensors are attached to the FIMC-LITE hostdata interface
453 * directly or through MIPI-CSIS, depending on the external media bus
454 * used. This needs to be handled in a more reliable way, not by just
455 * checking parent's node name.
456 */
457 np = of_get_parent(rem);
458
459 if (np && !of_node_cmp(np->name, "i2c-isp"))
460 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
461 else
462 pd->fimc_bus_type = pd->sensor_bus_type;
463
464 ret = fimc_md_of_add_sensor(fmd, rem, index);
465 of_node_put(rem);
466
467 return ret;
468}
469
470/* Register all SoC external sub-devices */
471static int fimc_md_of_sensors_register(struct fimc_md *fmd,
472 struct device_node *np)
473{
474 struct device_node *parent = fmd->pdev->dev.of_node;
475 struct device_node *node, *ports;
476 int index = 0;
477 int ret;
478
479 /* Attach sensors linked to MIPI CSI-2 receivers */
480 for_each_available_child_of_node(parent, node) {
481 struct device_node *port;
482
483 if (of_node_cmp(node->name, "csis"))
484 continue;
485 /* The csis node can have only port subnode. */
486 port = of_get_next_child(node, NULL);
487 if (!port)
488 continue;
489
490 ret = fimc_md_parse_port_node(fmd, port, index);
491 if (ret < 0)
492 return ret;
493 index++;
494 }
495
496 /* Attach sensors listed in the parallel-ports node */
497 ports = of_get_child_by_name(parent, "parallel-ports");
498 if (!ports)
499 return 0;
500
501 for_each_child_of_node(ports, node) {
502 ret = fimc_md_parse_port_node(fmd, node, index);
503 if (ret < 0)
504 break;
505 index++;
506 }
507
508 return 0;
509}
510
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300511static int __of_get_csis_id(struct device_node *np)
512{
513 u32 reg = 0;
514
515 np = of_get_child_by_name(np, "port");
516 if (!np)
517 return -EINVAL;
518 of_property_read_u32(np, "reg", &reg);
519 return reg - FIMC_INPUT_MIPI_CSI2_0;
520}
521#else
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300522#define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300523#define __of_get_csis_id(np) (-ENOSYS)
524#endif
525
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300526static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
527{
528 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300529 struct device_node *of_node = fmd->pdev->dev.of_node;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300530 int num_clients = 0;
531 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300532
533 /*
534 * Runtime resume one of the FIMC entities to make sure
535 * the sclk_cam clocks are not globally disabled.
536 */
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300537 if (!fmd->pmf)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300538 return -ENXIO;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300539
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300540 ret = pm_runtime_get_sync(fmd->pmf);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300541 if (ret < 0)
542 return ret;
543
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300544 if (of_node) {
545 fmd->num_sensors = 0;
546 ret = fimc_md_of_sensors_register(fmd, of_node);
547 } else if (pdata) {
548 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
549 num_clients = min_t(u32, pdata->num_clients,
550 ARRAY_SIZE(fmd->sensor));
551 fmd->num_sensors = num_clients;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300552
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300553 for (i = 0; i < num_clients; i++) {
554 struct v4l2_subdev *sd;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300555
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300556 fmd->sensor[i].pdata = pdata->source_info[i];
557 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
558 if (ret)
559 break;
560 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
561 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300562
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300563 if (IS_ERR(sd)) {
564 fmd->sensor[i].subdev = NULL;
565 ret = PTR_ERR(sd);
566 break;
567 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300568 fmd->sensor[i].subdev = sd;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300569 if (ret)
570 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300571 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300572 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300573
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300574 pm_runtime_put(fmd->pmf);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300575 return ret;
576}
577
578/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300579 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300580 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300581
582static int register_fimc_lite_entity(struct fimc_md *fmd,
583 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300584{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300585 struct v4l2_subdev *sd;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300586 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300587
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300588 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
589 fmd->fimc_lite[fimc_lite->index]))
590 return -EBUSY;
591
592 sd = &fimc_lite->subdev;
593 sd->grp_id = GRP_ID_FLITE;
594 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
595
596 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
597 if (!ret)
598 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
599 else
600 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
601 fimc_lite->index);
602 return ret;
603}
604
605static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
606{
607 struct v4l2_subdev *sd;
608 int ret;
609
610 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
611 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300612
Sachin Kamat8163ec02012-10-25 10:56:00 -0300613 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300614 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -0300615 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300616
617 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300618 if (!ret) {
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300619 if (!fmd->pmf && fimc->pdev)
620 fmd->pmf = &fimc->pdev->dev;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300621 fmd->fimc[fimc->id] = fimc;
622 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
623 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300624 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
625 fimc->id, ret);
626 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300627 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300628}
629
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300630static int register_csis_entity(struct fimc_md *fmd,
631 struct platform_device *pdev,
632 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300633{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300634 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300635 int id, ret;
636
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300637 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300638
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300639 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
640 return -ENOENT;
641
642 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300643 return -EBUSY;
644
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300645 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300646 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300647 if (!ret)
648 fmd->csis[id].sd = sd;
649 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300650 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300651 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300652 return ret;
653}
654
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300655static int fimc_md_register_platform_entity(struct fimc_md *fmd,
656 struct platform_device *pdev,
657 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300658{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300659 struct device *dev = &pdev->dev;
660 int ret = -EPROBE_DEFER;
661 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300662
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300663 /* Lock to ensure dev->driver won't change. */
664 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300665
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300666 if (!dev->driver || !try_module_get(dev->driver->owner))
667 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300668
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300669 drvdata = dev_get_drvdata(dev);
670 /* Some subdev didn't probe succesfully id drvdata is NULL */
671 if (drvdata) {
672 switch (plat_entity) {
673 case IDX_FIMC:
674 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300675 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300676 case IDX_FLITE:
677 ret = register_fimc_lite_entity(fmd, drvdata);
678 break;
679 case IDX_CSIS:
680 ret = register_csis_entity(fmd, pdev, drvdata);
681 break;
682 default:
683 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300684 }
685 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300686
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300687 module_put(dev->driver->owner);
688dev_unlock:
689 device_unlock(dev);
690 if (ret == -EPROBE_DEFER)
691 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
692 dev_name(dev));
693 else if (ret < 0)
694 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
695 dev_name(dev), ret);
696 return ret;
697}
698
699static int fimc_md_pdev_match(struct device *dev, void *data)
700{
701 struct platform_device *pdev = to_platform_device(dev);
702 int plat_entity = -1;
703 int ret;
704 char *p;
705
706 if (!get_device(dev))
707 return -ENODEV;
708
709 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
710 plat_entity = IDX_CSIS;
711 } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) {
712 plat_entity = IDX_FLITE;
713 } else {
714 p = strstr(pdev->name, "fimc");
715 if (p && *(p + 4) == 0)
716 plat_entity = IDX_FIMC;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300717 }
718
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300719 if (plat_entity >= 0)
720 ret = fimc_md_register_platform_entity(data, pdev,
721 plat_entity);
722 put_device(dev);
723 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300724}
725
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300726/* Register FIMC, FIMC-LITE and CSIS media entities */
727#ifdef CONFIG_OF
728static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
729 struct device_node *parent)
730{
731 struct device_node *node;
732 int ret = 0;
733
734 for_each_available_child_of_node(parent, node) {
735 struct platform_device *pdev;
736 int plat_entity = -1;
737
738 pdev = of_find_device_by_node(node);
739 if (!pdev)
740 continue;
741
742 /* If driver of any entity isn't ready try all again later. */
743 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
744 plat_entity = IDX_CSIS;
745 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
746 plat_entity = IDX_FLITE;
747 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
748 !of_property_read_bool(node, "samsung,lcd-wb"))
749 plat_entity = IDX_FIMC;
750
751 if (plat_entity >= 0)
752 ret = fimc_md_register_platform_entity(fmd, pdev,
753 plat_entity);
754 put_device(&pdev->dev);
755 if (ret < 0)
756 break;
757 }
758
759 return ret;
760}
761#else
762#define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
763#endif
764
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300765static void fimc_md_unregister_entities(struct fimc_md *fmd)
766{
767 int i;
768
769 for (i = 0; i < FIMC_MAX_DEVS; i++) {
770 if (fmd->fimc[i] == NULL)
771 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300772 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300773 fmd->fimc[i]->pipeline_ops = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300774 fmd->fimc[i] = NULL;
775 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300776 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
777 if (fmd->fimc_lite[i] == NULL)
778 continue;
779 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
Sylwester Nawrocki248ac362013-01-11 07:06:28 -0300780 fmd->fimc_lite[i]->pipeline_ops = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300781 fmd->fimc_lite[i] = NULL;
782 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300783 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
784 if (fmd->csis[i].sd == NULL)
785 continue;
786 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300787 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300788 fmd->csis[i].sd = NULL;
789 }
790 for (i = 0; i < fmd->num_sensors; i++) {
791 if (fmd->sensor[i].subdev == NULL)
792 continue;
793 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
794 fmd->sensor[i].subdev = NULL;
795 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300796 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300797}
798
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300799/**
800 * __fimc_md_create_fimc_links - create links to all FIMC entities
801 * @fmd: fimc media device
802 * @source: the source entity to create links to all fimc entities from
803 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
804 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300805 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300806 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300807static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
808 struct media_entity *source,
809 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300810 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300811{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300812 struct fimc_sensor_info *s_info = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300813 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300814 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300815 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300816
817 for (i = 0; i < FIMC_MAX_DEVS; i++) {
818 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300819 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300820 /*
821 * Some FIMC variants are not fitted with camera capture
822 * interface. Skip creating a link from sensor for those.
823 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300824 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300825 continue;
826
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300827 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300828
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300829 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300830 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300831 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300832 if (ret)
833 return ret;
834
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300835 /* Notify FIMC capture subdev entity */
836 ret = media_entity_call(sink, link_setup, &sink->pads[0],
837 &source->pads[pad], flags);
838 if (ret)
839 break;
840
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300841 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300842 source->name, flags ? '=' : '-', sink->name);
843
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300844 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300845 continue;
846 s_info = v4l2_get_subdev_hostdata(sensor);
847 if (!WARN_ON(s_info == NULL)) {
848 unsigned long irq_flags;
849 spin_lock_irqsave(&fmd->slock, irq_flags);
850 s_info->host = fmd->fimc[i];
851 spin_unlock_irqrestore(&fmd->slock, irq_flags);
852 }
853 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300854
855 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
856 if (!fmd->fimc_lite[i])
857 continue;
858
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300859 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
860 flags = MEDIA_LNK_FL_ENABLED;
861 else
862 flags = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300863
864 sink = &fmd->fimc_lite[i]->subdev.entity;
865 ret = media_entity_create_link(source, pad, sink,
866 FLITE_SD_PAD_SINK, flags);
867 if (ret)
868 return ret;
869
870 /* Notify FIMC-LITE subdev entity */
871 ret = media_entity_call(sink, link_setup, &sink->pads[0],
872 &source->pads[pad], flags);
873 if (ret)
874 break;
875
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300876 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300877 source->name, flags ? '=' : '-', sink->name);
878 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300879 return 0;
880}
881
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300882/* Create links from FIMC-LITE source pads to other entities */
883static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
884{
885 struct media_entity *source, *sink;
886 unsigned int flags = MEDIA_LNK_FL_ENABLED;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300887 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300888
889 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
890 struct fimc_lite *fimc = fmd->fimc_lite[i];
891 if (fimc == NULL)
892 continue;
893 source = &fimc->subdev.entity;
Sylwester Nawrocki1bcd7042012-07-26 07:13:08 -0300894 sink = &fimc->vfd.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300895 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300896 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300897 sink, 0, flags);
898 if (ret)
899 break;
900 /* TODO: create links to other entities */
901 }
902
903 return ret;
904}
905
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300906/**
907 * fimc_md_create_links - create default links between registered entities
908 *
909 * Parallel interface sensor entities are connected directly to FIMC capture
910 * entities. The sensors using MIPI CSIS bus are connected through immutable
911 * link with CSI receiver entity specified by mux_id. Any registered CSIS
912 * entity has a link to each registered FIMC capture entity. Enabled links
913 * are created by default between each subsequent registered sensor and
914 * subsequent FIMC capture entity. The number of default active links is
915 * determined by the number of available sensors or FIMC entities,
916 * whichever is less.
917 */
918static int fimc_md_create_links(struct fimc_md *fmd)
919{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300920 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300921 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300922 struct fimc_source_info *pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300923 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300924 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300925 int i, pad, fimc_id = 0, ret = 0;
926 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300927
928 for (i = 0; i < fmd->num_sensors; i++) {
929 if (fmd->sensor[i].subdev == NULL)
930 continue;
931
932 sensor = fmd->sensor[i].subdev;
933 s_info = v4l2_get_subdev_hostdata(sensor);
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300934 if (!s_info)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300935 continue;
936
937 source = NULL;
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300938 pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300939
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300940 switch (pdata->sensor_bus_type) {
941 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300942 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
943 "Wrong CSI channel id: %d\n", pdata->mux_id))
944 return -EINVAL;
945
946 csis = fmd->csis[pdata->mux_id].sd;
947 if (WARN(csis == NULL,
948 "MIPI-CSI interface specified "
949 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300950 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300951
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300952 pad = sensor->entity.num_pads - 1;
953 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300954 &csis->entity, CSIS_PAD_SINK,
955 MEDIA_LNK_FL_IMMUTABLE |
956 MEDIA_LNK_FL_ENABLED);
957 if (ret)
958 return ret;
959
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300960 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300961 sensor->entity.name, csis->entity.name);
962
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300963 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300964 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300965 break;
966
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300967 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300968 source = &sensor->entity;
969 pad = 0;
970 break;
971
972 default:
973 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300974 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300975 return -EINVAL;
976 }
977 if (source == NULL)
978 continue;
979
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300980 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300981 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300982 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300983 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300984
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300985 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300986 if (fmd->csis[i].sd == NULL)
987 continue;
988 source = &fmd->csis[i].sd->entity;
989 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300990 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300991
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300992 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300993 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300994 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300995 }
996
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300997 /* Create immutable links between each FIMC's subdev and video node */
998 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
999 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1000 if (!fmd->fimc[i])
1001 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001002 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001003 sink = &fmd->fimc[i]->vid_cap.vfd.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001004 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1005 sink, 0, flags);
1006 if (ret)
1007 break;
1008 }
1009
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001010 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001011}
1012
1013/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001014 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001015 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001016static void fimc_md_put_clocks(struct fimc_md *fmd)
1017{
1018 int i = FIMC_MAX_CAMCLKS;
1019
1020 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001021 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001022 continue;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001023 clk_unprepare(fmd->camclk[i].clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001024 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001025 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001026 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001027
1028 /* Writeback (PIXELASYNCMx) clocks */
1029 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1030 if (IS_ERR(fmd->wbclk[i]))
1031 continue;
1032 clk_put(fmd->wbclk[i]);
1033 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1034 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001035}
1036
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001037static int fimc_md_get_clocks(struct fimc_md *fmd)
1038{
1039 struct device *dev = NULL;
1040 char clk_name[32];
1041 struct clk *clock;
1042 int ret, i;
1043
1044 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1045 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1046
1047 if (fmd->pdev->dev.of_node)
1048 dev = &fmd->pdev->dev;
1049
1050 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1051 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1052 clock = clk_get(dev, clk_name);
1053
1054 if (IS_ERR(clock)) {
1055 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1056 clk_name);
1057 ret = PTR_ERR(clock);
1058 break;
1059 }
1060 ret = clk_prepare(clock);
1061 if (ret < 0) {
1062 clk_put(clock);
1063 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1064 break;
1065 }
1066 fmd->camclk[i].clock = clock;
1067 }
1068 if (ret)
1069 fimc_md_put_clocks(fmd);
1070
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001071 if (!fmd->use_isp)
1072 return 0;
1073 /*
1074 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1075 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1076 */
1077 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1078
1079 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1080 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1081 clock = clk_get(dev, clk_name);
1082 if (IS_ERR(clock)) {
1083 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1084 clk_name);
1085 ret = PTR_ERR(clock);
1086 break;
1087 }
1088 fmd->wbclk[i] = clock;
1089 }
1090 if (ret)
1091 fimc_md_put_clocks(fmd);
1092
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001093 return ret;
1094}
1095
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001096static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001097 struct fimc_sensor_info *s_info,
1098 bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001099{
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001100 struct fimc_source_info *pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001101 struct fimc_camclk_info *camclk;
1102 int ret = 0;
1103
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -03001104 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001105 return -EINVAL;
1106
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001107 camclk = &fmd->camclk[pdata->clk_id];
1108
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001109 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1110 pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001111
1112 if (on) {
1113 if (camclk->use_count > 0 &&
1114 camclk->frequency != pdata->clk_frequency)
1115 return -EINVAL;
1116
1117 if (camclk->use_count++ == 0) {
1118 clk_set_rate(camclk->clock, pdata->clk_frequency);
1119 camclk->frequency = pdata->clk_frequency;
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -03001120 ret = pm_runtime_get_sync(fmd->pmf);
1121 if (ret < 0)
1122 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001123 ret = clk_enable(camclk->clock);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001124 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
1125 clk_get_rate(camclk->clock));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001126 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001127 return ret;
1128 }
1129
1130 if (WARN_ON(camclk->use_count == 0))
1131 return 0;
1132
1133 if (--camclk->use_count == 0) {
1134 clk_disable(camclk->clock);
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -03001135 pm_runtime_put(fmd->pmf);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001136 dbg("Disabled camclk %d", pdata->clk_id);
1137 }
1138 return ret;
1139}
1140
1141/**
1142 * fimc_md_set_camclk - peripheral sensor clock setup
1143 * @sd: sensor subdev to configure sclk_cam clock for
1144 * @on: 1 to enable or 0 to disable the clock
1145 *
1146 * There are 2 separate clock outputs available in the SoC for external
1147 * image processors. These clocks are shared between all registered FIMC
1148 * devices to which sensors can be attached, either directly or through
1149 * the MIPI CSI receiver. The clock is allowed here to be used by
1150 * multiple sensors concurrently if they use same frequency.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001151 * This function should only be called when the graph mutex is held.
1152 */
1153int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1154{
1155 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
1156 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1157
1158 return __fimc_md_set_camclk(fmd, s_info, on);
1159}
1160
1161static int fimc_md_link_notify(struct media_pad *source,
1162 struct media_pad *sink, u32 flags)
1163{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001164 struct fimc_lite *fimc_lite = NULL;
1165 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001166 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001167 struct v4l2_subdev *sd;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001168 struct mutex *lock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001169 int ret = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001170 int ref_count;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001171
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001172 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001173 return 0;
1174
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001175 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001176
1177 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001178 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001179 fimc_lite = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001180 if (WARN_ON(fimc_lite == NULL))
1181 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001182 pipeline = &fimc_lite->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001183 lock = &fimc_lite->lock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001184 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -03001185 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001186 fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001187 if (WARN_ON(fimc == NULL))
1188 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001189 pipeline = &fimc->pipeline;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001190 lock = &fimc->lock;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001191 break;
1192 default:
1193 return 0;
1194 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001195
1196 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001197 int i;
1198 mutex_lock(lock);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -03001199 ret = __fimc_pipeline_close(pipeline);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001200 for (i = 0; i < IDX_MAX; i++)
1201 pipeline->subdevs[i] = NULL;
1202 if (fimc)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001203 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001204 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001205 return ret;
1206 }
1207 /*
1208 * Link activation. Enable power of pipeline elements only if the
1209 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001210 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001211 */
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001212 mutex_lock(lock);
1213
1214 ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;
1215 if (ref_count > 0)
1216 ret = __fimc_pipeline_open(pipeline, source->entity, true);
1217 if (!ret && fimc)
1218 ret = fimc_capture_ctrls_create(fimc);
1219
1220 mutex_unlock(lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001221 return ret ? -EPIPE : ret;
1222}
1223
1224static ssize_t fimc_md_sysfs_show(struct device *dev,
1225 struct device_attribute *attr, char *buf)
1226{
1227 struct platform_device *pdev = to_platform_device(dev);
1228 struct fimc_md *fmd = platform_get_drvdata(pdev);
1229
1230 if (fmd->user_subdev_api)
1231 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1232
1233 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1234}
1235
1236static ssize_t fimc_md_sysfs_store(struct device *dev,
1237 struct device_attribute *attr,
1238 const char *buf, size_t count)
1239{
1240 struct platform_device *pdev = to_platform_device(dev);
1241 struct fimc_md *fmd = platform_get_drvdata(pdev);
1242 bool subdev_api;
1243 int i;
1244
1245 if (!strcmp(buf, "vid-dev\n"))
1246 subdev_api = false;
1247 else if (!strcmp(buf, "sub-dev\n"))
1248 subdev_api = true;
1249 else
1250 return count;
1251
1252 fmd->user_subdev_api = subdev_api;
1253 for (i = 0; i < FIMC_MAX_DEVS; i++)
1254 if (fmd->fimc[i])
1255 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1256 return count;
1257}
1258/*
1259 * This device attribute is to select video pipeline configuration method.
1260 * There are following valid values:
1261 * vid-dev - for V4L2 video node API only, subdevice will be configured
1262 * by the host driver.
1263 * sub-dev - for media controller API, subdevs must be configured in user
1264 * space before starting streaming.
1265 */
1266static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1267 fimc_md_sysfs_show, fimc_md_sysfs_store);
1268
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001269static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1270{
1271 struct device *dev = &fmd->pdev->dev;
1272 struct fimc_pinctrl *pctl = &fmd->pinctl;
1273
1274 pctl->pinctrl = devm_pinctrl_get(dev);
1275 if (IS_ERR(pctl->pinctrl))
1276 return PTR_ERR(pctl->pinctrl);
1277
1278 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1279 PINCTRL_STATE_DEFAULT);
1280 if (IS_ERR(pctl->state_default))
1281 return PTR_ERR(pctl->state_default);
1282
1283 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1284 PINCTRL_STATE_IDLE);
1285 return 0;
1286}
1287
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001288static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001289{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001290 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001291 struct v4l2_device *v4l2_dev;
1292 struct fimc_md *fmd;
1293 int ret;
1294
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001295 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001296 if (!fmd)
1297 return -ENOMEM;
1298
1299 spin_lock_init(&fmd->slock);
1300 fmd->pdev = pdev;
1301
1302 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1303 sizeof(fmd->media_dev.model));
1304 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001305 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001306
1307 v4l2_dev = &fmd->v4l2_dev;
1308 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001309 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001310 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001311
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001312 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001313 if (ret < 0) {
1314 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001315 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001316 }
1317 ret = media_device_register(&fmd->media_dev);
1318 if (ret < 0) {
1319 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001320 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001321 }
1322 ret = fimc_md_get_clocks(fmd);
1323 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001324 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001325
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001326 fmd->user_subdev_api = (dev->of_node != NULL);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001327
1328 /* Protect the media graph while we're registering entities */
1329 mutex_lock(&fmd->media_dev.graph_mutex);
1330
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001331 ret = fimc_md_get_pinctrl(fmd);
1332 if (ret < 0) {
1333 if (ret != EPROBE_DEFER)
1334 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1335 goto err_unlock;
1336 }
1337
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001338 if (dev->of_node)
1339 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1340 else
1341 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1342 fimc_md_pdev_match);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001343 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001344 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001345
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -03001346 if (dev->platform_data || dev->of_node) {
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001347 ret = fimc_md_register_sensor_entities(fmd);
1348 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001349 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001350 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001351
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001352 ret = fimc_md_create_links(fmd);
1353 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001354 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001355 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1356 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001357 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001358
1359 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001360 if (ret)
1361 goto err_unlock;
1362
1363 platform_set_drvdata(pdev, fmd);
1364 mutex_unlock(&fmd->media_dev.graph_mutex);
1365 return 0;
1366
1367err_unlock:
1368 mutex_unlock(&fmd->media_dev.graph_mutex);
1369err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001370 media_device_unregister(&fmd->media_dev);
1371 fimc_md_put_clocks(fmd);
1372 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001373err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001374 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001375 return ret;
1376}
1377
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001378static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001379{
1380 struct fimc_md *fmd = platform_get_drvdata(pdev);
1381
1382 if (!fmd)
1383 return 0;
1384 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1385 fimc_md_unregister_entities(fmd);
1386 media_device_unregister(&fmd->media_dev);
1387 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001388 return 0;
1389}
1390
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001391static struct platform_device_id fimc_driver_ids[] __always_unused = {
1392 { .name = "s5p-fimc-md" },
1393 { },
1394};
1395MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1396
1397static const struct of_device_id fimc_md_of_match[] = {
1398 { .compatible = "samsung,fimc" },
1399 { },
1400};
1401MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1402
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001403static struct platform_driver fimc_md_driver = {
1404 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001405 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001406 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001407 .of_match_table = of_match_ptr(fimc_md_of_match),
1408 .name = "s5p-fimc-md",
1409 .owner = THIS_MODULE,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001410 }
1411};
1412
Sachin Kamat7e566be2012-05-26 11:11:54 -03001413static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001414{
1415 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001416
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001417 request_module("s5p-csis");
1418 ret = fimc_register_driver();
1419 if (ret)
1420 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001421
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001422 return platform_driver_register(&fimc_md_driver);
1423}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001424
1425static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001426{
1427 platform_driver_unregister(&fimc_md_driver);
1428 fimc_unregister_driver();
1429}
1430
1431module_init(fimc_md_init);
1432module_exit(fimc_md_exit);
1433
1434MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1435MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1436MODULE_LICENSE("GPL");
1437MODULE_VERSION("2.0.1");