blob: c1bce170df6fbce44d519a498bf6d19694389306 [file] [log] [blame]
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001/*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
3 *
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -03004 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
5 * Author: 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>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030023#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/types.h>
26#include <linux/slab.h>
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -030027#include <media/v4l2-ctrls.h>
Sylwester Nawrockie2985a22013-03-08 12:59:33 -030028#include <media/v4l2-of.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030029#include <media/media-device.h>
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030030#include <media/s5p_fimc.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030031
Sylwester Nawrocki56fa1a62013-03-24 16:54:25 +010032#include "media-dev.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030033#include "fimc-core.h"
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -030034#include "fimc-is.h"
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030035#include "fimc-lite.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030036#include "mipi-csis.h"
37
38static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -030039 struct fimc_source_info *si,
Sylwester Nawrockid3953222011-09-01 06:01:08 -030040 bool on);
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030041
42/* Set up image sensor subdev -> FIMC capture node notifications. */
43static void __setup_sensor_notification(struct fimc_md *fmd,
44 struct v4l2_subdev *sensor,
45 struct v4l2_subdev *fimc_sd)
46{
47 struct fimc_source_info *src_inf;
48 struct fimc_sensor_info *md_si;
49 unsigned long flags;
50
51 src_inf = v4l2_get_subdev_hostdata(sensor);
52 if (!src_inf || WARN_ON(fmd == NULL))
53 return;
54
55 md_si = source_to_sensor_info(src_inf);
56 spin_lock_irqsave(&fmd->slock, flags);
57 md_si->host = v4l2_get_subdevdata(fimc_sd);
58 spin_unlock_irqrestore(&fmd->slock, flags);
59}
60
Sylwester Nawrockid3953222011-09-01 06:01:08 -030061/**
62 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030063 * @me: media entity terminating the pipeline
Sylwester Nawrockid3953222011-09-01 06:01:08 -030064 *
65 * Caller holds the graph mutex.
66 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030067static void fimc_pipeline_prepare(struct fimc_pipeline *p,
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -030068 struct media_entity *me)
Sylwester Nawrockid3953222011-09-01 06:01:08 -030069{
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030070 struct fimc_md *fmd = entity_to_fimc_mdev(me);
Sylwester Nawrockid3953222011-09-01 06:01:08 -030071 struct v4l2_subdev *sd;
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030072 struct v4l2_subdev *sensor = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030073 int i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030074
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030075 for (i = 0; i < IDX_MAX; i++)
76 p->subdevs[i] = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030077
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030078 while (1) {
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030079 struct media_pad *pad = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030080
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030081 /* Find remote source pad */
82 for (i = 0; i < me->num_pads; i++) {
83 struct media_pad *spad = &me->pads[i];
84 if (!(spad->flags & MEDIA_PAD_FL_SINK))
85 continue;
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -030086 pad = media_entity_remote_pad(spad);
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -030087 if (pad)
88 break;
89 }
90
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030091 if (pad == NULL ||
92 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
93 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030094 sd = media_entity_to_v4l2_subdev(pad->entity);
95
96 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030097 case GRP_ID_SENSOR:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030098 sensor = sd;
99 /* fall through */
100 case GRP_ID_FIMC_IS_SENSOR:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300101 p->subdevs[IDX_SENSOR] = sd;
102 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300103 case GRP_ID_CSIS:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300104 p->subdevs[IDX_CSIS] = sd;
105 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300106 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300107 p->subdevs[IDX_FLITE] = sd;
108 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300109 case GRP_ID_FIMC:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300110 p->subdevs[IDX_FIMC] = sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300111 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300112 case GRP_ID_FIMC_IS:
113 p->subdevs[IDX_IS_ISP] = sd;
114 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300115 default:
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300116 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300117 }
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -0300118 me = &sd->entity;
119 if (me->num_pads == 1)
120 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300121 }
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300122
123 if (sensor && p->subdevs[IDX_FIMC])
124 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300125}
126
127/**
128 * __subdev_set_power - change power state of a single subdev
129 * @sd: subdevice to change power state for
130 * @on: 1 to enable power or 0 to disable
131 *
132 * Return result of s_power subdev operation or -ENXIO if sd argument
133 * is NULL. Return 0 if the subdevice does not implement s_power.
134 */
135static int __subdev_set_power(struct v4l2_subdev *sd, int on)
136{
137 int *use_count;
138 int ret;
139
140 if (sd == NULL)
141 return -ENXIO;
142
143 use_count = &sd->entity.use_count;
144 if (on && (*use_count)++ > 0)
145 return 0;
146 else if (!on && (*use_count == 0 || --(*use_count) > 0))
147 return 0;
148 ret = v4l2_subdev_call(sd, core, s_power, on);
149
150 return ret != -ENOIOCTLCMD ? ret : 0;
151}
152
153/**
154 * fimc_pipeline_s_power - change power state of all pipeline subdevs
155 * @fimc: fimc device terminating the pipeline
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300156 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300157 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300158 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300159 */
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300160static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300161{
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300162 static const u8 seq[2][IDX_MAX - 1] = {
163 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
164 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
165 };
166 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300167
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300168 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300169 return -ENXIO;
170
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300171 for (i = 0; i < IDX_MAX - 1; i++) {
172 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300173
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300174 ret = __subdev_set_power(p->subdevs[idx], on);
175
176
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300177 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300178 goto error;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300179 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300180 return 0;
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300181error:
182 for (; i >= 0; i--) {
183 unsigned int idx = seq[on][i];
184 __subdev_set_power(p->subdevs[idx], !on);
185 }
186 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300187}
188
189/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300190 * __fimc_pipeline_open - update the pipeline information, enable power
191 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300192 * @me: media entity to start graph walk with
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300193 * @prepare: true to walk the current pipeline and acquire all subdevs
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300194 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300195 * Called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300196 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300197static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300198 struct media_entity *me, bool prepare)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300199{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300200 struct fimc_md *fmd = entity_to_fimc_mdev(me);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300201 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300202 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300203 int ret;
204
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300205 if (WARN_ON(p == NULL || me == NULL))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300206 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300207
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300208 if (prepare)
209 fimc_pipeline_prepare(p, me);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300210
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300211 sd = p->subdevs[IDX_SENSOR];
212 if (sd == NULL)
213 return -EINVAL;
214
215 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
216 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
217 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
218 if (ret < 0)
219 return ret;
220 }
221 ret = fimc_md_set_camclk(sd, true);
222 if (ret < 0)
223 goto err_wbclk;
224
225 ret = fimc_pipeline_s_power(p, 1);
226 if (!ret)
227 return 0;
228
229 fimc_md_set_camclk(sd, false);
230
231err_wbclk:
232 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
233 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
234
235 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300236}
237
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300238/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300239 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300240 * @fimc: fimc device terminating the pipeline
241 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300242 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300243 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300244static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300245{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300246 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300247 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
248 struct fimc_md *fmd;
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300249 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300250
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300251 if (sd == NULL) {
252 pr_warn("%s(): No sensor subdev\n", __func__);
253 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300254 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300255
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300256 ret = fimc_pipeline_s_power(p, 0);
257 fimc_md_set_camclk(sd, false);
258
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300259 fmd = entity_to_fimc_mdev(&sd->entity);
260
261 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
262 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
263 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
264
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300265 return ret == -ENXIO ? 0 : ret;
266}
267
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300268/**
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300269 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300270 * @pipeline: video pipeline structure
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300271 * @on: passed as the s_stream() callback argument
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300272 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300273static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300274{
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300275 static const u8 seq[2][IDX_MAX] = {
276 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
277 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
278 };
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300279 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300280 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300281
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300282 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300283 return -ENODEV;
284
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300285 for (i = 0; i < IDX_MAX; i++) {
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300286 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300287
288 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
289
290 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300291 goto error;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300292 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300293 return 0;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300294error:
295 for (; i >= 0; i--) {
296 unsigned int idx = seq[on][i];
297 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
298 }
299 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300300}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300301
302/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300303static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300304 .open = __fimc_pipeline_open,
305 .close = __fimc_pipeline_close,
306 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300307};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300308
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300309static struct exynos_media_pipeline *fimc_md_pipeline_create(
310 struct fimc_md *fmd)
311{
312 struct fimc_pipeline *p;
313
314 p = kzalloc(sizeof(*p), GFP_KERNEL);
315 if (!p)
316 return NULL;
317
318 list_add_tail(&p->list, &fmd->pipelines);
319
320 p->ep.ops = &fimc_pipeline_ops;
321 return &p->ep;
322}
323
324static void fimc_md_pipelines_free(struct fimc_md *fmd)
325{
326 while (!list_empty(&fmd->pipelines)) {
327 struct fimc_pipeline *p;
328
329 p = list_entry(fmd->pipelines.next, typeof(*p), list);
330 list_del(&p->list);
331 kfree(p);
332 }
333}
334
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300335/*
336 * Sensor subdevice helper functions
337 */
338static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300339 struct fimc_source_info *si)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300340{
341 struct i2c_adapter *adapter;
342 struct v4l2_subdev *sd = NULL;
343
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300344 if (!si || !fmd)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300345 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300346 /*
347 * If FIMC bus type is not Writeback FIFO assume it is same
348 * as sensor_bus_type.
349 */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300350 si->fimc_bus_type = si->sensor_bus_type;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300351
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300352 adapter = i2c_get_adapter(si->i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300353 if (!adapter) {
354 v4l2_warn(&fmd->v4l2_dev,
355 "Failed to get I2C adapter %d, deferring probe\n",
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300356 si->i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300357 return ERR_PTR(-EPROBE_DEFER);
358 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300359 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300360 si->board_info, NULL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300361 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300362 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300363 v4l2_warn(&fmd->v4l2_dev,
364 "Failed to acquire subdev %s, deferring probe\n",
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300365 si->board_info->type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300366 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300367 }
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300368 v4l2_set_subdev_hostdata(sd, si);
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300369 sd->grp_id = GRP_ID_SENSOR;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300370
371 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300372 sd->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300373 return sd;
374}
375
376static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
377{
378 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300379 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300380
381 if (!client)
382 return;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300383
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300384 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300385
386 if (!client->dev.of_node) {
387 adapter = client->adapter;
388 i2c_unregister_device(client);
389 if (adapter)
390 i2c_put_adapter(adapter);
391 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300392}
393
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300394#ifdef CONFIG_OF
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300395/* Register I2C client subdev associated with @node. */
396static int fimc_md_of_add_sensor(struct fimc_md *fmd,
397 struct device_node *node, int index)
398{
399 struct fimc_sensor_info *si;
400 struct i2c_client *client;
401 struct v4l2_subdev *sd;
402 int ret;
403
404 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
405 return -EINVAL;
406 si = &fmd->sensor[index];
407
408 client = of_find_i2c_device_by_node(node);
409 if (!client)
410 return -EPROBE_DEFER;
411
412 device_lock(&client->dev);
413
Lars-Peter Clausenc7110f32013-09-29 10:51:00 +0200414 if (!client->dev.driver ||
415 !try_module_get(client->dev.driver->owner)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300416 ret = -EPROBE_DEFER;
417 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
418 node->full_name);
419 goto dev_put;
420 }
421
422 /* Enable sensor's master clock */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300423 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300424 if (ret < 0)
425 goto mod_put;
426 sd = i2c_get_clientdata(client);
427
428 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300429 __fimc_md_set_camclk(fmd, &si->pdata, false);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300430 if (ret < 0)
431 goto mod_put;
432
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300433 v4l2_set_subdev_hostdata(sd, &si->pdata);
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300434 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
435 sd->grp_id = GRP_ID_FIMC_IS_SENSOR;
436 else
437 sd->grp_id = GRP_ID_SENSOR;
438
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300439 si->subdev = sd;
440 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
441 sd->name, fmd->num_sensors);
442 fmd->num_sensors++;
443
444mod_put:
Lars-Peter Clausenc7110f32013-09-29 10:51:00 +0200445 module_put(client->dev.driver->owner);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300446dev_put:
447 device_unlock(&client->dev);
448 put_device(&client->dev);
449 return ret;
450}
451
452/* Parse port node and register as a sub-device any sensor specified there. */
453static int fimc_md_parse_port_node(struct fimc_md *fmd,
454 struct device_node *port,
455 unsigned int index)
456{
457 struct device_node *rem, *ep, *np;
458 struct fimc_source_info *pd;
459 struct v4l2_of_endpoint endpoint;
460 int ret;
461 u32 val;
462
463 pd = &fmd->sensor[index].pdata;
464
465 /* Assume here a port node can have only one endpoint node. */
466 ep = of_get_next_child(port, NULL);
467 if (!ep)
468 return 0;
469
470 v4l2_of_parse_endpoint(ep, &endpoint);
471 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
472 return -EINVAL;
473
474 pd->mux_id = (endpoint.port - 1) & 0x1;
475
476 rem = v4l2_of_get_remote_port_parent(ep);
477 of_node_put(ep);
478 if (rem == NULL) {
479 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
480 ep->full_name);
481 return 0;
482 }
483 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
484 pd->clk_id = val;
485
486 if (!of_property_read_u32(rem, "clock-frequency", &val))
487 pd->clk_frequency = val;
488
489 if (pd->clk_frequency == 0) {
490 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
491 rem->full_name);
492 of_node_put(rem);
493 return -EINVAL;
494 }
495
496 if (fimc_input_is_parallel(endpoint.port)) {
497 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
498 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
499 else
500 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
501 pd->flags = endpoint.bus.parallel.flags;
502 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
503 /*
504 * MIPI CSI-2: only input mux selection and
505 * the sensor's clock frequency is needed.
506 */
507 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
508 } else {
509 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
510 endpoint.port, rem->full_name);
511 }
512 /*
513 * For FIMC-IS handled sensors, that are placed under i2c-isp device
514 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
515 * input. Sensors are attached to the FIMC-LITE hostdata interface
516 * directly or through MIPI-CSIS, depending on the external media bus
517 * used. This needs to be handled in a more reliable way, not by just
518 * checking parent's node name.
519 */
520 np = of_get_parent(rem);
521
522 if (np && !of_node_cmp(np->name, "i2c-isp"))
523 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
524 else
525 pd->fimc_bus_type = pd->sensor_bus_type;
526
527 ret = fimc_md_of_add_sensor(fmd, rem, index);
528 of_node_put(rem);
529
530 return ret;
531}
532
533/* Register all SoC external sub-devices */
534static int fimc_md_of_sensors_register(struct fimc_md *fmd,
535 struct device_node *np)
536{
537 struct device_node *parent = fmd->pdev->dev.of_node;
538 struct device_node *node, *ports;
539 int index = 0;
540 int ret;
541
542 /* Attach sensors linked to MIPI CSI-2 receivers */
543 for_each_available_child_of_node(parent, node) {
544 struct device_node *port;
545
546 if (of_node_cmp(node->name, "csis"))
547 continue;
548 /* The csis node can have only port subnode. */
549 port = of_get_next_child(node, NULL);
550 if (!port)
551 continue;
552
553 ret = fimc_md_parse_port_node(fmd, port, index);
554 if (ret < 0)
555 return ret;
556 index++;
557 }
558
559 /* Attach sensors listed in the parallel-ports node */
560 ports = of_get_child_by_name(parent, "parallel-ports");
561 if (!ports)
562 return 0;
563
564 for_each_child_of_node(ports, node) {
565 ret = fimc_md_parse_port_node(fmd, node, index);
566 if (ret < 0)
567 break;
568 index++;
569 }
570
571 return 0;
572}
573
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300574static int __of_get_csis_id(struct device_node *np)
575{
576 u32 reg = 0;
577
578 np = of_get_child_by_name(np, "port");
579 if (!np)
580 return -EINVAL;
581 of_property_read_u32(np, "reg", &reg);
582 return reg - FIMC_INPUT_MIPI_CSI2_0;
583}
584#else
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300585#define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300586#define __of_get_csis_id(np) (-ENOSYS)
587#endif
588
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300589static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
590{
591 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300592 struct device_node *of_node = fmd->pdev->dev.of_node;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300593 int num_clients = 0;
594 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300595
596 /*
597 * Runtime resume one of the FIMC entities to make sure
598 * the sclk_cam clocks are not globally disabled.
599 */
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300600 if (!fmd->pmf)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300601 return -ENXIO;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300602
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300603 ret = pm_runtime_get_sync(fmd->pmf);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300604 if (ret < 0)
605 return ret;
606
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300607 if (of_node) {
608 fmd->num_sensors = 0;
609 ret = fimc_md_of_sensors_register(fmd, of_node);
610 } else if (pdata) {
611 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
612 num_clients = min_t(u32, pdata->num_clients,
613 ARRAY_SIZE(fmd->sensor));
614 fmd->num_sensors = num_clients;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300615
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300616 for (i = 0; i < num_clients; i++) {
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300617 struct fimc_sensor_info *si = &fmd->sensor[i];
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300618 struct v4l2_subdev *sd;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300619
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300620 si->pdata = pdata->source_info[i];
621 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300622 if (ret)
623 break;
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300624 sd = fimc_md_register_sensor(fmd, &si->pdata);
625 ret = __fimc_md_set_camclk(fmd, &si->pdata, false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300626
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300627 if (IS_ERR(sd)) {
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300628 si->subdev = NULL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300629 ret = PTR_ERR(sd);
630 break;
631 }
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300632 si->subdev = sd;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300633 if (ret)
634 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300635 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300636 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300637
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300638 pm_runtime_put(fmd->pmf);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300639 return ret;
640}
641
642/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300643 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300644 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300645
646static int register_fimc_lite_entity(struct fimc_md *fmd,
647 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300648{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300649 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300650 struct exynos_media_pipeline *ep;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300651 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300652
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300653 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
654 fmd->fimc_lite[fimc_lite->index]))
655 return -EBUSY;
656
657 sd = &fimc_lite->subdev;
658 sd->grp_id = GRP_ID_FLITE;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300659
660 ep = fimc_md_pipeline_create(fmd);
661 if (!ep)
662 return -ENOMEM;
663
664 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300665
666 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
667 if (!ret)
668 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
669 else
670 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
671 fimc_lite->index);
672 return ret;
673}
674
675static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
676{
677 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300678 struct exynos_media_pipeline *ep;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300679 int ret;
680
681 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
682 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300683
Sachin Kamat8163ec02012-10-25 10:56:00 -0300684 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300685 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300686
687 ep = fimc_md_pipeline_create(fmd);
688 if (!ep)
689 return -ENOMEM;
690
691 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300692
693 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300694 if (!ret) {
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300695 if (!fmd->pmf && fimc->pdev)
696 fmd->pmf = &fimc->pdev->dev;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300697 fmd->fimc[fimc->id] = fimc;
698 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
699 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300700 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
701 fimc->id, ret);
702 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300703 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300704}
705
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300706static int register_csis_entity(struct fimc_md *fmd,
707 struct platform_device *pdev,
708 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300709{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300710 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300711 int id, ret;
712
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300713 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300714
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300715 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
716 return -ENOENT;
717
718 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300719 return -EBUSY;
720
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300721 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300722 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300723 if (!ret)
724 fmd->csis[id].sd = sd;
725 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300726 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300727 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300728 return ret;
729}
730
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300731static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
732{
733 struct v4l2_subdev *sd = &is->isp.subdev;
734 int ret;
735
736 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
737 if (ret) {
738 v4l2_err(&fmd->v4l2_dev,
739 "Failed to register FIMC-ISP (%d)\n", ret);
740 return ret;
741 }
742
743 fmd->fimc_is = is;
744 return 0;
745}
746
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300747static int fimc_md_register_platform_entity(struct fimc_md *fmd,
748 struct platform_device *pdev,
749 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300750{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300751 struct device *dev = &pdev->dev;
752 int ret = -EPROBE_DEFER;
753 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300754
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300755 /* Lock to ensure dev->driver won't change. */
756 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300757
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300758 if (!dev->driver || !try_module_get(dev->driver->owner))
759 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300760
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300761 drvdata = dev_get_drvdata(dev);
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300762 /* Some subdev didn't probe successfully id drvdata is NULL */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300763 if (drvdata) {
764 switch (plat_entity) {
765 case IDX_FIMC:
766 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300767 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300768 case IDX_FLITE:
769 ret = register_fimc_lite_entity(fmd, drvdata);
770 break;
771 case IDX_CSIS:
772 ret = register_csis_entity(fmd, pdev, drvdata);
773 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300774 case IDX_IS_ISP:
775 ret = register_fimc_is_entity(fmd, drvdata);
776 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300777 default:
778 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300779 }
780 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300781
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300782 module_put(dev->driver->owner);
783dev_unlock:
784 device_unlock(dev);
785 if (ret == -EPROBE_DEFER)
786 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
787 dev_name(dev));
788 else if (ret < 0)
789 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
790 dev_name(dev), ret);
791 return ret;
792}
793
794static int fimc_md_pdev_match(struct device *dev, void *data)
795{
796 struct platform_device *pdev = to_platform_device(dev);
797 int plat_entity = -1;
798 int ret;
799 char *p;
800
801 if (!get_device(dev))
802 return -ENODEV;
803
804 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
805 plat_entity = IDX_CSIS;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300806 } else {
807 p = strstr(pdev->name, "fimc");
808 if (p && *(p + 4) == 0)
809 plat_entity = IDX_FIMC;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300810 }
811
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300812 if (plat_entity >= 0)
813 ret = fimc_md_register_platform_entity(data, pdev,
814 plat_entity);
815 put_device(dev);
816 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300817}
818
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300819/* Register FIMC, FIMC-LITE and CSIS media entities */
820#ifdef CONFIG_OF
821static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
822 struct device_node *parent)
823{
824 struct device_node *node;
825 int ret = 0;
826
827 for_each_available_child_of_node(parent, node) {
828 struct platform_device *pdev;
829 int plat_entity = -1;
830
831 pdev = of_find_device_by_node(node);
832 if (!pdev)
833 continue;
834
835 /* If driver of any entity isn't ready try all again later. */
836 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
837 plat_entity = IDX_CSIS;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300838 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
839 plat_entity = IDX_IS_ISP;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300840 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
841 plat_entity = IDX_FLITE;
842 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
843 !of_property_read_bool(node, "samsung,lcd-wb"))
844 plat_entity = IDX_FIMC;
845
846 if (plat_entity >= 0)
847 ret = fimc_md_register_platform_entity(fmd, pdev,
848 plat_entity);
849 put_device(&pdev->dev);
850 if (ret < 0)
851 break;
852 }
853
854 return ret;
855}
856#else
857#define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
858#endif
859
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300860static void fimc_md_unregister_entities(struct fimc_md *fmd)
861{
862 int i;
863
864 for (i = 0; i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300865 struct fimc_dev *dev = fmd->fimc[i];
866 if (dev == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300867 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300868 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
869 dev->vid_cap.ve.pipe = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300870 fmd->fimc[i] = NULL;
871 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300872 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300873 struct fimc_lite *dev = fmd->fimc_lite[i];
874 if (dev == NULL)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300875 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300876 v4l2_device_unregister_subdev(&dev->subdev);
877 dev->ve.pipe = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300878 fmd->fimc_lite[i] = NULL;
879 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300880 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
881 if (fmd->csis[i].sd == NULL)
882 continue;
883 v4l2_device_unregister_subdev(fmd->csis[i].sd);
884 fmd->csis[i].sd = NULL;
885 }
886 for (i = 0; i < fmd->num_sensors; i++) {
887 if (fmd->sensor[i].subdev == NULL)
888 continue;
889 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
890 fmd->sensor[i].subdev = NULL;
891 }
Sylwester Nawrockie41a35c2013-04-18 09:05:54 -0300892
893 if (fmd->fimc_is)
894 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
895
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300896 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300897}
898
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300899/**
900 * __fimc_md_create_fimc_links - create links to all FIMC entities
901 * @fmd: fimc media device
902 * @source: the source entity to create links to all fimc entities from
903 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
904 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300905 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300906 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300907static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
908 struct media_entity *source,
909 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300910 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300911{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300912 struct fimc_source_info *si = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300913 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300914 unsigned int flags = 0;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300915 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300916
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300917 if (sensor) {
918 si = v4l2_get_subdev_hostdata(sensor);
919 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300920 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300921 ret = 1;
922 }
923
924 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300925 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300926 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300927 /*
928 * Some FIMC variants are not fitted with camera capture
929 * interface. Skip creating a link from sensor for those.
930 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300931 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300932 continue;
933
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300934 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300935
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300936 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300937 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300938 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300939 if (ret)
940 return ret;
941
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300942 /* Notify FIMC capture subdev entity */
943 ret = media_entity_call(sink, link_setup, &sink->pads[0],
944 &source->pads[pad], flags);
945 if (ret)
946 break;
947
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300948 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300949 source->name, flags ? '=' : '-', sink->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300950 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300951
952 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
953 if (!fmd->fimc_lite[i])
954 continue;
955
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300956 sink = &fmd->fimc_lite[i]->subdev.entity;
957 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300958 FLITE_SD_PAD_SINK, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300959 if (ret)
960 return ret;
961
962 /* Notify FIMC-LITE subdev entity */
963 ret = media_entity_call(sink, link_setup, &sink->pads[0],
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300964 &source->pads[pad], 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300965 if (ret)
966 break;
967
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300968 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
969 source->name, sink->name);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300970 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300971 return 0;
972}
973
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300974/* Create links from FIMC-LITE source pads to other entities */
975static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
976{
977 struct media_entity *source, *sink;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300978 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300979
980 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
981 struct fimc_lite *fimc = fmd->fimc_lite[i];
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300982
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300983 if (fimc == NULL)
984 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300985
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300986 source = &fimc->subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300987 sink = &fimc->ve.vdev.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300988 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300989 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300990 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300991 if (ret)
992 break;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300993 /* Link from FIMC-LITE to IS-ISP subdev */
994 sink = &fmd->fimc_is->isp.subdev.entity;
995 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
996 sink, 0, 0);
997 if (ret)
998 break;
999 }
1000
1001 return ret;
1002}
1003
1004/* Create FIMC-IS links */
1005static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
1006{
1007 struct media_entity *source, *sink;
1008 int i, ret;
1009
1010 source = &fmd->fimc_is->isp.subdev.entity;
1011
1012 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1013 if (fmd->fimc[i] == NULL)
1014 continue;
1015
1016 /* Link from IS-ISP subdev to FIMC */
1017 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
1018 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
1019 sink, FIMC_SD_PAD_SINK_FIFO, 0);
1020 if (ret)
1021 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001022 }
1023
1024 return ret;
1025}
1026
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001027/**
1028 * fimc_md_create_links - create default links between registered entities
1029 *
1030 * Parallel interface sensor entities are connected directly to FIMC capture
1031 * entities. The sensors using MIPI CSIS bus are connected through immutable
1032 * link with CSI receiver entity specified by mux_id. Any registered CSIS
1033 * entity has a link to each registered FIMC capture entity. Enabled links
1034 * are created by default between each subsequent registered sensor and
1035 * subsequent FIMC capture entity. The number of default active links is
1036 * determined by the number of available sensors or FIMC entities,
1037 * whichever is less.
1038 */
1039static int fimc_md_create_links(struct fimc_md *fmd)
1040{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -03001041 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001042 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001043 struct fimc_source_info *pdata;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001044 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -03001045 int i, pad, fimc_id = 0, ret = 0;
1046 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001047
1048 for (i = 0; i < fmd->num_sensors; i++) {
1049 if (fmd->sensor[i].subdev == NULL)
1050 continue;
1051
1052 sensor = fmd->sensor[i].subdev;
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001053 pdata = v4l2_get_subdev_hostdata(sensor);
1054 if (!pdata)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001055 continue;
1056
1057 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001058
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001059 switch (pdata->sensor_bus_type) {
1060 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001061 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
1062 "Wrong CSI channel id: %d\n", pdata->mux_id))
1063 return -EINVAL;
1064
1065 csis = fmd->csis[pdata->mux_id].sd;
1066 if (WARN(csis == NULL,
1067 "MIPI-CSI interface specified "
1068 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -03001069 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001070
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -03001071 pad = sensor->entity.num_pads - 1;
1072 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001073 &csis->entity, CSIS_PAD_SINK,
1074 MEDIA_LNK_FL_IMMUTABLE |
1075 MEDIA_LNK_FL_ENABLED);
1076 if (ret)
1077 return ret;
1078
Sylwester Nawrocki969e8772012-12-07 16:40:08 -03001079 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001080 sensor->entity.name, csis->entity.name);
1081
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001082 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -03001083 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001084 break;
1085
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001086 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001087 source = &sensor->entity;
1088 pad = 0;
1089 break;
1090
1091 default:
1092 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -03001093 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001094 return -EINVAL;
1095 }
1096 if (source == NULL)
1097 continue;
1098
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -03001099 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001100 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -03001101 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001102 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001103
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -03001104 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001105 if (fmd->csis[i].sd == NULL)
1106 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -03001107
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001108 source = &fmd->csis[i].sd->entity;
1109 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -03001110 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001111
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -03001112 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -03001113 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -03001114 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -03001115 }
1116
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001117 /* Create immutable links between each FIMC's subdev and video node */
1118 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1119 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1120 if (!fmd->fimc[i])
1121 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -03001122
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001123 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001124 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -03001125
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001126 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1127 sink, 0, flags);
1128 if (ret)
1129 break;
1130 }
1131
Sylwester Nawrockif998bb72012-11-27 13:29:48 -03001132 ret = __fimc_md_create_flite_source_links(fmd);
1133 if (ret < 0)
1134 return ret;
1135
1136 if (fmd->use_isp)
1137 ret = __fimc_md_create_fimc_is_links(fmd);
1138
1139 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001140}
1141
1142/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001143 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001144 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001145static void fimc_md_put_clocks(struct fimc_md *fmd)
1146{
1147 int i = FIMC_MAX_CAMCLKS;
1148
1149 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001150 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001151 continue;
1152 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001153 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001154 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001155
1156 /* Writeback (PIXELASYNCMx) clocks */
1157 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1158 if (IS_ERR(fmd->wbclk[i]))
1159 continue;
1160 clk_put(fmd->wbclk[i]);
1161 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1162 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001163}
1164
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001165static int fimc_md_get_clocks(struct fimc_md *fmd)
1166{
1167 struct device *dev = NULL;
1168 char clk_name[32];
1169 struct clk *clock;
Sylwester Nawrocki044c3722013-07-19 10:05:17 -03001170 int i, ret = 0;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001171
1172 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1173 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1174
1175 if (fmd->pdev->dev.of_node)
1176 dev = &fmd->pdev->dev;
1177
1178 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1179 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1180 clock = clk_get(dev, clk_name);
1181
1182 if (IS_ERR(clock)) {
1183 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1184 clk_name);
1185 ret = PTR_ERR(clock);
1186 break;
1187 }
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001188 fmd->camclk[i].clock = clock;
1189 }
1190 if (ret)
1191 fimc_md_put_clocks(fmd);
1192
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001193 if (!fmd->use_isp)
1194 return 0;
1195 /*
1196 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1197 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1198 */
1199 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1200
1201 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1202 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1203 clock = clk_get(dev, clk_name);
1204 if (IS_ERR(clock)) {
1205 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1206 clk_name);
1207 ret = PTR_ERR(clock);
1208 break;
1209 }
1210 fmd->wbclk[i] = clock;
1211 }
1212 if (ret)
1213 fimc_md_put_clocks(fmd);
1214
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001215 return ret;
1216}
1217
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001218static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001219 struct fimc_source_info *si,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001220 bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001221{
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001222 struct fimc_camclk_info *camclk;
1223 int ret = 0;
1224
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001225 if (WARN_ON(si->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001226 return -EINVAL;
1227
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001228 camclk = &fmd->camclk[si->clk_id];
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001229
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001230 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001231 si->clk_id, si->clk_frequency, camclk->use_count, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001232
1233 if (on) {
1234 if (camclk->use_count > 0 &&
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001235 camclk->frequency != si->clk_frequency)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001236 return -EINVAL;
1237
1238 if (camclk->use_count++ == 0) {
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001239 clk_set_rate(camclk->clock, si->clk_frequency);
1240 camclk->frequency = si->clk_frequency;
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -03001241 ret = pm_runtime_get_sync(fmd->pmf);
1242 if (ret < 0)
1243 return ret;
Sylwester Nawrocki044c3722013-07-19 10:05:17 -03001244 ret = clk_prepare_enable(camclk->clock);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001245 dbg("Enabled camclk %d: f: %lu", si->clk_id,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -03001246 clk_get_rate(camclk->clock));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001247 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001248 return ret;
1249 }
1250
1251 if (WARN_ON(camclk->use_count == 0))
1252 return 0;
1253
1254 if (--camclk->use_count == 0) {
Sylwester Nawrocki044c3722013-07-19 10:05:17 -03001255 clk_disable_unprepare(camclk->clock);
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -03001256 pm_runtime_put(fmd->pmf);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001257 dbg("Disabled camclk %d", si->clk_id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001258 }
1259 return ret;
1260}
1261
1262/**
1263 * fimc_md_set_camclk - peripheral sensor clock setup
1264 * @sd: sensor subdev to configure sclk_cam clock for
1265 * @on: 1 to enable or 0 to disable the clock
1266 *
1267 * There are 2 separate clock outputs available in the SoC for external
1268 * image processors. These clocks are shared between all registered FIMC
1269 * devices to which sensors can be attached, either directly or through
1270 * the MIPI CSI receiver. The clock is allowed here to be used by
1271 * multiple sensors concurrently if they use same frequency.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001272 * This function should only be called when the graph mutex is held.
1273 */
1274int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1275{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001276 struct fimc_source_info *si = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001277 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1278
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001279 return __fimc_md_set_camclk(fmd, si, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001280}
1281
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001282static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001283{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001284 struct exynos_video_entity *ve;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001285 struct fimc_pipeline *p;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001286 struct video_device *vdev;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001287 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001288
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001289 vdev = media_entity_to_video_device(entity);
1290 if (vdev->entity.use_count == 0)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001291 return 0;
1292
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001293 ve = vdev_to_exynos_video_entity(vdev);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001294 p = to_fimc_pipeline(ve->pipe);
1295 /*
1296 * Nothing to do if we are disabling the pipeline, some link
1297 * has been disconnected and p->subdevs array is cleared now.
1298 */
1299 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1300 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001301
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001302 if (enable)
1303 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1304 else
1305 ret = __fimc_pipeline_close(ve->pipe);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001306
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001307 if (ret == 0 && !enable)
1308 memset(p->subdevs, 0, sizeof(p->subdevs));
1309
1310 return ret;
1311}
1312
1313/* Locking: called with entity->parent->graph_mutex mutex held. */
1314static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1315{
1316 struct media_entity *entity_err = entity;
1317 struct media_entity_graph graph;
1318 int ret;
1319
1320 /*
1321 * Walk current graph and call the pipeline open/close routine for each
1322 * opened video node that belongs to the graph of entities connected
1323 * through active links. This is needed as we cannot power on/off the
1324 * subdevs in random order.
1325 */
1326 media_entity_graph_walk_start(&graph, entity);
1327
1328 while ((entity = media_entity_graph_walk_next(&graph))) {
1329 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1330 continue;
1331
1332 ret = __fimc_md_modify_pipeline(entity, enable);
1333
1334 if (ret < 0)
1335 goto err;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001336 }
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001337
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001338 return 0;
1339 err:
1340 media_entity_graph_walk_start(&graph, entity_err);
1341
1342 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1343 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1344 continue;
1345
1346 __fimc_md_modify_pipeline(entity_err, !enable);
1347
1348 if (entity_err == entity)
1349 break;
1350 }
1351
1352 return ret;
1353}
1354
1355static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1356 unsigned int notification)
1357{
1358 struct media_entity *sink = link->sink->entity;
1359 int ret = 0;
1360
1361 /* Before link disconnection */
1362 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1363 if (!(flags & MEDIA_LNK_FL_ENABLED))
1364 ret = __fimc_md_modify_pipelines(sink, false);
1365 else
1366 ; /* TODO: Link state change validation */
1367 /* After link activation */
1368 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1369 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1370 ret = __fimc_md_modify_pipelines(sink, true);
1371 }
1372
1373 return ret ? -EPIPE : 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001374}
1375
1376static ssize_t fimc_md_sysfs_show(struct device *dev,
1377 struct device_attribute *attr, char *buf)
1378{
1379 struct platform_device *pdev = to_platform_device(dev);
1380 struct fimc_md *fmd = platform_get_drvdata(pdev);
1381
1382 if (fmd->user_subdev_api)
1383 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1384
1385 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1386}
1387
1388static ssize_t fimc_md_sysfs_store(struct device *dev,
1389 struct device_attribute *attr,
1390 const char *buf, size_t count)
1391{
1392 struct platform_device *pdev = to_platform_device(dev);
1393 struct fimc_md *fmd = platform_get_drvdata(pdev);
1394 bool subdev_api;
1395 int i;
1396
1397 if (!strcmp(buf, "vid-dev\n"))
1398 subdev_api = false;
1399 else if (!strcmp(buf, "sub-dev\n"))
1400 subdev_api = true;
1401 else
1402 return count;
1403
1404 fmd->user_subdev_api = subdev_api;
1405 for (i = 0; i < FIMC_MAX_DEVS; i++)
1406 if (fmd->fimc[i])
1407 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1408 return count;
1409}
1410/*
1411 * This device attribute is to select video pipeline configuration method.
1412 * There are following valid values:
1413 * vid-dev - for V4L2 video node API only, subdevice will be configured
1414 * by the host driver.
1415 * sub-dev - for media controller API, subdevs must be configured in user
1416 * space before starting streaming.
1417 */
1418static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1419 fimc_md_sysfs_show, fimc_md_sysfs_store);
1420
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001421static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1422{
1423 struct device *dev = &fmd->pdev->dev;
1424 struct fimc_pinctrl *pctl = &fmd->pinctl;
1425
1426 pctl->pinctrl = devm_pinctrl_get(dev);
1427 if (IS_ERR(pctl->pinctrl))
1428 return PTR_ERR(pctl->pinctrl);
1429
1430 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1431 PINCTRL_STATE_DEFAULT);
1432 if (IS_ERR(pctl->state_default))
1433 return PTR_ERR(pctl->state_default);
1434
1435 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1436 PINCTRL_STATE_IDLE);
1437 return 0;
1438}
1439
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001440static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001441{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001442 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001443 struct v4l2_device *v4l2_dev;
1444 struct fimc_md *fmd;
1445 int ret;
1446
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001447 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001448 if (!fmd)
1449 return -ENOMEM;
1450
1451 spin_lock_init(&fmd->slock);
1452 fmd->pdev = pdev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001453 INIT_LIST_HEAD(&fmd->pipelines);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001454
1455 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1456 sizeof(fmd->media_dev.model));
1457 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001458 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001459
1460 v4l2_dev = &fmd->v4l2_dev;
1461 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001462 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001463 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001464
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001465 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1466
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001467 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001468 if (ret < 0) {
1469 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001470 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001471 }
1472 ret = media_device_register(&fmd->media_dev);
1473 if (ret < 0) {
1474 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001475 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001476 }
1477 ret = fimc_md_get_clocks(fmd);
1478 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001479 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001480
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001481 fmd->user_subdev_api = (dev->of_node != NULL);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001482
1483 /* Protect the media graph while we're registering entities */
1484 mutex_lock(&fmd->media_dev.graph_mutex);
1485
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001486 ret = fimc_md_get_pinctrl(fmd);
1487 if (ret < 0) {
1488 if (ret != EPROBE_DEFER)
1489 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1490 goto err_unlock;
1491 }
1492
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001493 if (dev->of_node)
1494 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1495 else
1496 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1497 fimc_md_pdev_match);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001498 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001499 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001500
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -03001501 if (dev->platform_data || dev->of_node) {
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001502 ret = fimc_md_register_sensor_entities(fmd);
1503 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001504 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001505 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001506
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001507 ret = fimc_md_create_links(fmd);
1508 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001509 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001510 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1511 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001512 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001513
1514 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001515 if (ret)
1516 goto err_unlock;
1517
1518 platform_set_drvdata(pdev, fmd);
1519 mutex_unlock(&fmd->media_dev.graph_mutex);
1520 return 0;
1521
1522err_unlock:
1523 mutex_unlock(&fmd->media_dev.graph_mutex);
1524err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001525 fimc_md_put_clocks(fmd);
1526 fimc_md_unregister_entities(fmd);
Sylwester Nawrockid2b903b2013-07-29 06:53:59 -03001527 media_device_unregister(&fmd->media_dev);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001528err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001529 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001530 return ret;
1531}
1532
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001533static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001534{
1535 struct fimc_md *fmd = platform_get_drvdata(pdev);
1536
1537 if (!fmd)
1538 return 0;
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001539
1540 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001541 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1542 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001543 fimc_md_pipelines_free(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001544 media_device_unregister(&fmd->media_dev);
1545 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001546 return 0;
1547}
1548
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001549static struct platform_device_id fimc_driver_ids[] __always_unused = {
1550 { .name = "s5p-fimc-md" },
1551 { },
1552};
1553MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1554
1555static const struct of_device_id fimc_md_of_match[] = {
1556 { .compatible = "samsung,fimc" },
1557 { },
1558};
1559MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1560
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001561static struct platform_driver fimc_md_driver = {
1562 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001563 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001564 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001565 .of_match_table = of_match_ptr(fimc_md_of_match),
1566 .name = "s5p-fimc-md",
1567 .owner = THIS_MODULE,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001568 }
1569};
1570
Sachin Kamat7e566be2012-05-26 11:11:54 -03001571static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001572{
1573 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001574
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001575 request_module("s5p-csis");
1576 ret = fimc_register_driver();
1577 if (ret)
1578 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001579
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001580 return platform_driver_register(&fimc_md_driver);
1581}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001582
1583static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001584{
1585 platform_driver_unregister(&fimc_md_driver);
1586 fimc_unregister_driver();
1587}
1588
1589module_init(fimc_md_init);
1590module_exit(fimc_md_exit);
1591
1592MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1593MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1594MODULE_LICENSE("GPL");
1595MODULE_VERSION("2.0.1");