blob: e79ddbb1e14fc0bd8c1b56504183a927edcdec83 [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>
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -030014#include <linux/clk.h>
15#include <linux/clk-provider.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030016#include <linux/device.h>
17#include <linux/errno.h>
18#include <linux/i2c.h>
19#include <linux/kernel.h>
20#include <linux/list.h>
21#include <linux/module.h>
Sylwester Nawrockie2985a22013-03-08 12:59:33 -030022#include <linux/of.h>
23#include <linux/of_platform.h>
24#include <linux/of_device.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010025#include <linux/of_graph.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030026#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/types.h>
29#include <linux/slab.h>
Sylwester Nawrockifa91f102014-02-24 11:44:50 -030030#include <media/v4l2-async.h>
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -030031#include <media/v4l2-ctrls.h>
Sylwester Nawrockie2985a22013-03-08 12:59:33 -030032#include <media/v4l2-of.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030033#include <media/media-device.h>
Mauro Carvalho Chehabd647f0b2015-11-13 19:40:07 -020034#include <media/drv-intf/exynos-fimc.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030035
Sylwester Nawrocki56fa1a62013-03-24 16:54:25 +010036#include "media-dev.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030037#include "fimc-core.h"
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -030038#include "fimc-is.h"
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030039#include "fimc-lite.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030040#include "mipi-csis.h"
41
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030042/* 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
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -030091 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030092 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030093 sd = media_entity_to_v4l2_subdev(pad->entity);
94
95 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030096 case GRP_ID_SENSOR:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030097 sensor = sd;
98 /* fall through */
99 case GRP_ID_FIMC_IS_SENSOR:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300100 p->subdevs[IDX_SENSOR] = sd;
101 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300102 case GRP_ID_CSIS:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300103 p->subdevs[IDX_CSIS] = sd;
104 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300105 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300106 p->subdevs[IDX_FLITE] = sd;
107 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300108 case GRP_ID_FIMC:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300109 p->subdevs[IDX_FIMC] = sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300110 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300111 case GRP_ID_FIMC_IS:
112 p->subdevs[IDX_IS_ISP] = sd;
113 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300114 default:
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300115 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300116 }
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -0300117 me = &sd->entity;
118 if (me->num_pads == 1)
119 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300120 }
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300121
122 if (sensor && p->subdevs[IDX_FIMC])
123 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300124}
125
126/**
127 * __subdev_set_power - change power state of a single subdev
128 * @sd: subdevice to change power state for
129 * @on: 1 to enable power or 0 to disable
130 *
131 * Return result of s_power subdev operation or -ENXIO if sd argument
132 * is NULL. Return 0 if the subdevice does not implement s_power.
133 */
134static int __subdev_set_power(struct v4l2_subdev *sd, int on)
135{
136 int *use_count;
137 int ret;
138
139 if (sd == NULL)
140 return -ENXIO;
141
142 use_count = &sd->entity.use_count;
143 if (on && (*use_count)++ > 0)
144 return 0;
145 else if (!on && (*use_count == 0 || --(*use_count) > 0))
146 return 0;
147 ret = v4l2_subdev_call(sd, core, s_power, on);
148
149 return ret != -ENOIOCTLCMD ? ret : 0;
150}
151
152/**
153 * fimc_pipeline_s_power - change power state of all pipeline subdevs
154 * @fimc: fimc device terminating the pipeline
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300155 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300156 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300157 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300158 */
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300159static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300160{
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300161 static const u8 seq[2][IDX_MAX - 1] = {
162 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
163 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
164 };
165 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300166
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300167 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300168 return -ENXIO;
169
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300170 for (i = 0; i < IDX_MAX - 1; i++) {
171 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300172
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300173 ret = __subdev_set_power(p->subdevs[idx], on);
174
175
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300176 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300177 goto error;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300178 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300179 return 0;
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300180error:
181 for (; i >= 0; i--) {
182 unsigned int idx = seq[on][i];
183 __subdev_set_power(p->subdevs[idx], !on);
184 }
185 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300186}
187
188/**
Jacek Anaszewski76775772016-01-18 14:10:26 -0200189 * __fimc_pipeline_enable - enable power of all pipeline subdevs
190 * and the sensor clock
191 * @ep: video pipeline structure
192 * @fmd: fimc media device
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300193 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300194 * Called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300195 */
Jacek Anaszewski76775772016-01-18 14:10:26 -0200196static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
197 struct fimc_md *fmd)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300198{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300199 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300200 int ret;
201
Jacek Anaszewski76775772016-01-18 14:10:26 -0200202 /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300203 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
204 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
205 if (ret < 0)
206 return ret;
207 }
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300208
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300209 ret = fimc_pipeline_s_power(p, 1);
210 if (!ret)
211 return 0;
212
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300213 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
214 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
215
216 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300217}
218
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300219/**
Jacek Anaszewski76775772016-01-18 14:10:26 -0200220 * __fimc_pipeline_open - update the pipeline information, enable power
221 * of all pipeline subdevs and the sensor clock
222 * @me: media entity to start graph walk with
223 * @prepare: true to walk the current pipeline and acquire all subdevs
224 *
225 * Called with the graph mutex held.
226 */
227static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
228 struct media_entity *me, bool prepare)
229{
230 struct fimc_md *fmd = entity_to_fimc_mdev(me);
231 struct fimc_pipeline *p = to_fimc_pipeline(ep);
232 struct v4l2_subdev *sd;
233
234 if (WARN_ON(p == NULL || me == NULL))
235 return -EINVAL;
236
237 if (prepare)
238 fimc_pipeline_prepare(p, me);
239
240 sd = p->subdevs[IDX_SENSOR];
241 if (sd == NULL) {
242 pr_warn("%s(): No sensor subdev\n", __func__);
243 /*
244 * Pipeline open cannot fail so as to make it possible
245 * for the user space to configure the pipeline.
246 */
247 return 0;
248 }
249
250 return __fimc_pipeline_enable(ep, fmd);
251}
252
253/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300254 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300255 * @fimc: fimc device terminating the pipeline
256 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300257 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300258 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300259static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300260{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300261 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300262 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
263 struct fimc_md *fmd;
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300264 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300265
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300266 if (sd == NULL) {
267 pr_warn("%s(): No sensor subdev\n", __func__);
268 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300269 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300270
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300271 ret = fimc_pipeline_s_power(p, 0);
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300272
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300273 fmd = entity_to_fimc_mdev(&sd->entity);
274
275 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
276 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
277 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
278
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300279 return ret == -ENXIO ? 0 : ret;
280}
281
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300282/**
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300283 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300284 * @pipeline: video pipeline structure
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300285 * @on: passed as the s_stream() callback argument
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300286 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300287static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300288{
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300289 static const u8 seq[2][IDX_MAX] = {
290 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
291 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
292 };
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300293 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Jacek Anaszewski76775772016-01-18 14:10:26 -0200294 struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
295 enum fimc_subdev_index sd_id;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300296 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300297
Jacek Anaszewski76775772016-01-18 14:10:26 -0200298 if (p->subdevs[IDX_SENSOR] == NULL) {
299 if (!fmd->user_subdev_api) {
300 /*
301 * Sensor must be already discovered if we
302 * aren't in the user_subdev_api mode
303 */
304 return -ENODEV;
305 }
306
307 /* Get pipeline sink entity */
308 if (p->subdevs[IDX_FIMC])
309 sd_id = IDX_FIMC;
310 else if (p->subdevs[IDX_IS_ISP])
311 sd_id = IDX_IS_ISP;
312 else if (p->subdevs[IDX_FLITE])
313 sd_id = IDX_FLITE;
314 else
315 return -ENODEV;
316
317 /*
318 * Sensor could have been linked between open and STREAMON -
319 * check if this is the case.
320 */
321 fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
322
323 if (p->subdevs[IDX_SENSOR] == NULL)
324 return -ENODEV;
325
326 ret = __fimc_pipeline_enable(ep, fmd);
327 if (ret < 0)
328 return ret;
329
330 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300331
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300332 for (i = 0; i < IDX_MAX; i++) {
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300333 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300334
335 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
336
337 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300338 goto error;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300339 }
Jacek Anaszewski76775772016-01-18 14:10:26 -0200340
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300341 return 0;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300342error:
Jacek Anaszewski76775772016-01-18 14:10:26 -0200343 fimc_pipeline_s_power(p, !on);
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300344 for (; i >= 0; i--) {
345 unsigned int idx = seq[on][i];
346 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
347 }
348 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300349}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300350
351/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300352static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300353 .open = __fimc_pipeline_open,
354 .close = __fimc_pipeline_close,
355 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300356};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300357
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300358static struct exynos_media_pipeline *fimc_md_pipeline_create(
359 struct fimc_md *fmd)
360{
361 struct fimc_pipeline *p;
362
363 p = kzalloc(sizeof(*p), GFP_KERNEL);
364 if (!p)
365 return NULL;
366
367 list_add_tail(&p->list, &fmd->pipelines);
368
369 p->ep.ops = &fimc_pipeline_ops;
370 return &p->ep;
371}
372
373static void fimc_md_pipelines_free(struct fimc_md *fmd)
374{
375 while (!list_empty(&fmd->pipelines)) {
376 struct fimc_pipeline *p;
377
378 p = list_entry(fmd->pipelines.next, typeof(*p), list);
379 list_del(&p->list);
380 kfree(p);
381 }
382}
383
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300384/* Parse port node and register as a sub-device any sensor specified there. */
385static int fimc_md_parse_port_node(struct fimc_md *fmd,
386 struct device_node *port,
387 unsigned int index)
388{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300389 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300390 struct device_node *rem, *ep, *np;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300391 struct v4l2_of_endpoint endpoint;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300392
393 /* Assume here a port node can have only one endpoint node. */
394 ep = of_get_next_child(port, NULL);
395 if (!ep)
396 return 0;
397
398 v4l2_of_parse_endpoint(ep, &endpoint);
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100399 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300400 return -EINVAL;
401
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100402 pd->mux_id = (endpoint.base.port - 1) & 0x1;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300403
Philipp Zabelfd9fdb72014-02-10 22:01:48 +0100404 rem = of_graph_get_remote_port_parent(ep);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300405 of_node_put(ep);
406 if (rem == NULL) {
407 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
408 ep->full_name);
409 return 0;
410 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300411
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100412 if (fimc_input_is_parallel(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300413 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
414 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
415 else
416 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
417 pd->flags = endpoint.bus.parallel.flags;
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100418 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300419 /*
420 * MIPI CSI-2: only input mux selection and
421 * the sensor's clock frequency is needed.
422 */
423 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
424 } else {
425 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100426 endpoint.base.port, rem->full_name);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300427 }
428 /*
429 * For FIMC-IS handled sensors, that are placed under i2c-isp device
430 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
431 * input. Sensors are attached to the FIMC-LITE hostdata interface
432 * directly or through MIPI-CSIS, depending on the external media bus
433 * used. This needs to be handled in a more reliable way, not by just
434 * checking parent's node name.
435 */
436 np = of_get_parent(rem);
437
438 if (np && !of_node_cmp(np->name, "i2c-isp"))
439 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
440 else
441 pd->fimc_bus_type = pd->sensor_bus_type;
442
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300443 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
444 return -EINVAL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300445
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300446 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
447 fmd->sensor[index].asd.match.of.node = rem;
448 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
449
450 fmd->num_sensors++;
451
452 of_node_put(rem);
453 return 0;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300454}
455
456/* Register all SoC external sub-devices */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300457static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300458{
459 struct device_node *parent = fmd->pdev->dev.of_node;
460 struct device_node *node, *ports;
461 int index = 0;
462 int ret;
463
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300464 /*
465 * Runtime resume one of the FIMC entities to make sure
466 * the sclk_cam clocks are not globally disabled.
467 */
468 if (!fmd->pmf)
469 return -ENXIO;
470
471 ret = pm_runtime_get_sync(fmd->pmf);
472 if (ret < 0)
473 return ret;
474
475 fmd->num_sensors = 0;
476
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300477 /* Attach sensors linked to MIPI CSI-2 receivers */
478 for_each_available_child_of_node(parent, node) {
479 struct device_node *port;
480
481 if (of_node_cmp(node->name, "csis"))
482 continue;
483 /* The csis node can have only port subnode. */
484 port = of_get_next_child(node, NULL);
485 if (!port)
486 continue;
487
488 ret = fimc_md_parse_port_node(fmd, port, index);
489 if (ret < 0)
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300490 goto rpm_put;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300491 index++;
492 }
493
494 /* Attach sensors listed in the parallel-ports node */
495 ports = of_get_child_by_name(parent, "parallel-ports");
496 if (!ports)
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300497 goto rpm_put;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300498
499 for_each_child_of_node(ports, node) {
500 ret = fimc_md_parse_port_node(fmd, node, index);
501 if (ret < 0)
502 break;
503 index++;
504 }
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300505rpm_put:
506 pm_runtime_put(fmd->pmf);
507 return ret;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300508}
509
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300510static int __of_get_csis_id(struct device_node *np)
511{
512 u32 reg = 0;
513
514 np = of_get_child_by_name(np, "port");
515 if (!np)
516 return -EINVAL;
517 of_property_read_u32(np, "reg", &reg);
518 return reg - FIMC_INPUT_MIPI_CSI2_0;
519}
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300520
521/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300522 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300523 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300524static int register_fimc_lite_entity(struct fimc_md *fmd,
525 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300526{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300527 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300528 struct exynos_media_pipeline *ep;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300529 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300530
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300531 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
532 fmd->fimc_lite[fimc_lite->index]))
533 return -EBUSY;
534
535 sd = &fimc_lite->subdev;
536 sd->grp_id = GRP_ID_FLITE;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300537
538 ep = fimc_md_pipeline_create(fmd);
539 if (!ep)
540 return -ENOMEM;
541
542 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300543
544 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
545 if (!ret)
546 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
547 else
548 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
549 fimc_lite->index);
550 return ret;
551}
552
553static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
554{
555 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300556 struct exynos_media_pipeline *ep;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300557 int ret;
558
559 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
560 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300561
Sachin Kamat8163ec02012-10-25 10:56:00 -0300562 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300563 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300564
565 ep = fimc_md_pipeline_create(fmd);
566 if (!ep)
567 return -ENOMEM;
568
569 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300570
571 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300572 if (!ret) {
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300573 if (!fmd->pmf && fimc->pdev)
574 fmd->pmf = &fimc->pdev->dev;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300575 fmd->fimc[fimc->id] = fimc;
576 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
577 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300578 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
579 fimc->id, ret);
580 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300581 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300582}
583
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300584static int register_csis_entity(struct fimc_md *fmd,
585 struct platform_device *pdev,
586 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300587{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300588 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300589 int id, ret;
590
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300591 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300592
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300593 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
594 return -ENOENT;
595
596 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300597 return -EBUSY;
598
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300599 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300600 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300601 if (!ret)
602 fmd->csis[id].sd = sd;
603 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300604 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300605 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300606 return ret;
607}
608
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300609static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
610{
611 struct v4l2_subdev *sd = &is->isp.subdev;
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300612 struct exynos_media_pipeline *ep;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300613 int ret;
614
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300615 /* Allocate pipeline object for the ISP capture video node. */
616 ep = fimc_md_pipeline_create(fmd);
617 if (!ep)
618 return -ENOMEM;
619
620 v4l2_set_subdev_hostdata(sd, ep);
621
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300622 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
623 if (ret) {
624 v4l2_err(&fmd->v4l2_dev,
625 "Failed to register FIMC-ISP (%d)\n", ret);
626 return ret;
627 }
628
629 fmd->fimc_is = is;
630 return 0;
631}
632
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300633static int fimc_md_register_platform_entity(struct fimc_md *fmd,
634 struct platform_device *pdev,
635 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300636{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300637 struct device *dev = &pdev->dev;
638 int ret = -EPROBE_DEFER;
639 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300640
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300641 /* Lock to ensure dev->driver won't change. */
642 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300643
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300644 if (!dev->driver || !try_module_get(dev->driver->owner))
645 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300646
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300647 drvdata = dev_get_drvdata(dev);
Jonathan McCrohanf58c91c2013-10-20 21:34:01 -0300648 /* Some subdev didn't probe successfully id drvdata is NULL */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300649 if (drvdata) {
650 switch (plat_entity) {
651 case IDX_FIMC:
652 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300653 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300654 case IDX_FLITE:
655 ret = register_fimc_lite_entity(fmd, drvdata);
656 break;
657 case IDX_CSIS:
658 ret = register_csis_entity(fmd, pdev, drvdata);
659 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300660 case IDX_IS_ISP:
661 ret = register_fimc_is_entity(fmd, drvdata);
662 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300663 default:
664 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300665 }
666 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300667
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300668 module_put(dev->driver->owner);
669dev_unlock:
670 device_unlock(dev);
671 if (ret == -EPROBE_DEFER)
672 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
673 dev_name(dev));
674 else if (ret < 0)
675 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
676 dev_name(dev), ret);
677 return ret;
678}
679
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300680/* Register FIMC, FIMC-LITE and CSIS media entities */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300681static int fimc_md_register_platform_entities(struct fimc_md *fmd,
682 struct device_node *parent)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300683{
684 struct device_node *node;
685 int ret = 0;
686
687 for_each_available_child_of_node(parent, node) {
688 struct platform_device *pdev;
689 int plat_entity = -1;
690
691 pdev = of_find_device_by_node(node);
692 if (!pdev)
693 continue;
694
695 /* If driver of any entity isn't ready try all again later. */
696 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
697 plat_entity = IDX_CSIS;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300698 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
699 plat_entity = IDX_IS_ISP;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300700 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
701 plat_entity = IDX_FLITE;
702 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
703 !of_property_read_bool(node, "samsung,lcd-wb"))
704 plat_entity = IDX_FIMC;
705
706 if (plat_entity >= 0)
707 ret = fimc_md_register_platform_entity(fmd, pdev,
708 plat_entity);
709 put_device(&pdev->dev);
710 if (ret < 0)
711 break;
712 }
713
714 return ret;
715}
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300716
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300717static void fimc_md_unregister_entities(struct fimc_md *fmd)
718{
719 int i;
720
721 for (i = 0; i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300722 struct fimc_dev *dev = fmd->fimc[i];
723 if (dev == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300724 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300725 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
726 dev->vid_cap.ve.pipe = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300727 fmd->fimc[i] = NULL;
728 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300729 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300730 struct fimc_lite *dev = fmd->fimc_lite[i];
731 if (dev == NULL)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300732 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300733 v4l2_device_unregister_subdev(&dev->subdev);
734 dev->ve.pipe = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300735 fmd->fimc_lite[i] = NULL;
736 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300737 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
738 if (fmd->csis[i].sd == NULL)
739 continue;
740 v4l2_device_unregister_subdev(fmd->csis[i].sd);
741 fmd->csis[i].sd = NULL;
742 }
Sylwester Nawrockie41a35c2013-04-18 09:05:54 -0300743
744 if (fmd->fimc_is)
745 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
746
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300747 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300748}
749
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300750/**
751 * __fimc_md_create_fimc_links - create links to all FIMC entities
752 * @fmd: fimc media device
753 * @source: the source entity to create links to all fimc entities from
754 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
755 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300756 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300757 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300758static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
759 struct media_entity *source,
760 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300761 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300762{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300763 struct fimc_source_info *si = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300764 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300765 unsigned int flags = 0;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300766 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300767
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300768 if (sensor) {
769 si = v4l2_get_subdev_hostdata(sensor);
770 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300771 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300772 ret = 1;
773 }
774
775 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300776 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300777 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300778 /*
779 * Some FIMC variants are not fitted with camera capture
780 * interface. Skip creating a link from sensor for those.
781 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300782 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300783 continue;
784
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300785 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300786
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300787 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300788 ret = media_create_pad_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300789 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300790 if (ret)
791 return ret;
792
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300793 /* Notify FIMC capture subdev entity */
794 ret = media_entity_call(sink, link_setup, &sink->pads[0],
795 &source->pads[pad], flags);
796 if (ret)
797 break;
798
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300799 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300800 source->name, flags ? '=' : '-', sink->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300801 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300802
803 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
804 if (!fmd->fimc_lite[i])
805 continue;
806
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300807 sink = &fmd->fimc_lite[i]->subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300808 ret = media_create_pad_link(source, pad, sink,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300809 FLITE_SD_PAD_SINK, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300810 if (ret)
811 return ret;
812
813 /* Notify FIMC-LITE subdev entity */
814 ret = media_entity_call(sink, link_setup, &sink->pads[0],
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300815 &source->pads[pad], 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300816 if (ret)
817 break;
818
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300819 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
820 source->name, sink->name);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300821 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300822 return 0;
823}
824
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300825/* Create links from FIMC-LITE source pads to other entities */
826static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
827{
828 struct media_entity *source, *sink;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300829 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300830
831 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
832 struct fimc_lite *fimc = fmd->fimc_lite[i];
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300833
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300834 if (fimc == NULL)
835 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300836
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300837 source = &fimc->subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300838 sink = &fimc->ve.vdev.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300839 /* FIMC-LITE's subdev and video node */
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300840 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300841 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300842 if (ret)
843 break;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300844 /* Link from FIMC-LITE to IS-ISP subdev */
845 sink = &fmd->fimc_is->isp.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300846 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300847 sink, 0, 0);
848 if (ret)
849 break;
850 }
851
852 return ret;
853}
854
855/* Create FIMC-IS links */
856static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
857{
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300858 struct fimc_isp *isp = &fmd->fimc_is->isp;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300859 struct media_entity *source, *sink;
860 int i, ret;
861
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300862 source = &isp->subdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300863
864 for (i = 0; i < FIMC_MAX_DEVS; i++) {
865 if (fmd->fimc[i] == NULL)
866 continue;
867
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300868 /* Link from FIMC-IS-ISP subdev to FIMC */
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300869 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300870 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300871 sink, FIMC_SD_PAD_SINK_FIFO, 0);
872 if (ret)
873 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300874 }
875
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300876 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
877 sink = &isp->video_capture.ve.vdev.entity;
878
879 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
880 if (sink->num_pads == 0)
881 return 0;
882
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300883 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300884 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300885}
886
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300887/**
888 * fimc_md_create_links - create default links between registered entities
889 *
890 * Parallel interface sensor entities are connected directly to FIMC capture
891 * entities. The sensors using MIPI CSIS bus are connected through immutable
892 * link with CSI receiver entity specified by mux_id. Any registered CSIS
893 * entity has a link to each registered FIMC capture entity. Enabled links
894 * are created by default between each subsequent registered sensor and
895 * subsequent FIMC capture entity. The number of default active links is
896 * determined by the number of available sensors or FIMC entities,
897 * whichever is less.
898 */
899static int fimc_md_create_links(struct fimc_md *fmd)
900{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300901 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300902 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300903 struct fimc_source_info *pdata;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300904 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300905 int i, pad, fimc_id = 0, ret = 0;
906 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300907
908 for (i = 0; i < fmd->num_sensors; i++) {
909 if (fmd->sensor[i].subdev == NULL)
910 continue;
911
912 sensor = fmd->sensor[i].subdev;
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300913 pdata = v4l2_get_subdev_hostdata(sensor);
914 if (!pdata)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300915 continue;
916
917 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300918
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300919 switch (pdata->sensor_bus_type) {
920 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300921 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
922 "Wrong CSI channel id: %d\n", pdata->mux_id))
923 return -EINVAL;
924
925 csis = fmd->csis[pdata->mux_id].sd;
926 if (WARN(csis == NULL,
927 "MIPI-CSI interface specified "
928 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300929 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300930
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300931 pad = sensor->entity.num_pads - 1;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300932 ret = media_create_pad_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300933 &csis->entity, CSIS_PAD_SINK,
934 MEDIA_LNK_FL_IMMUTABLE |
935 MEDIA_LNK_FL_ENABLED);
936 if (ret)
937 return ret;
938
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300939 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300940 sensor->entity.name, csis->entity.name);
941
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300942 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300943 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300944 break;
945
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300946 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300947 source = &sensor->entity;
948 pad = 0;
949 break;
950
951 default:
952 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300953 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300954 return -EINVAL;
955 }
956 if (source == NULL)
957 continue;
958
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300959 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300960 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300961 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300962 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300963
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300964 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300965 if (fmd->csis[i].sd == NULL)
966 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300967
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300968 source = &fmd->csis[i].sd->entity;
969 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300970 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300971
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300972 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300973 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300974 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300975 }
976
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300977 /* Create immutable links between each FIMC's subdev and video node */
978 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
979 for (i = 0; i < FIMC_MAX_DEVS; i++) {
980 if (!fmd->fimc[i])
981 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300982
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300983 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300984 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300985
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300986 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300987 sink, 0, flags);
988 if (ret)
989 break;
990 }
991
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300992 ret = __fimc_md_create_flite_source_links(fmd);
993 if (ret < 0)
994 return ret;
995
996 if (fmd->use_isp)
997 ret = __fimc_md_create_fimc_is_links(fmd);
998
999 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001000}
1001
1002/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001003 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001004 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001005static void fimc_md_put_clocks(struct fimc_md *fmd)
1006{
1007 int i = FIMC_MAX_CAMCLKS;
1008
1009 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001010 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001011 continue;
1012 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001013 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001014 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001015
1016 /* Writeback (PIXELASYNCMx) clocks */
1017 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1018 if (IS_ERR(fmd->wbclk[i]))
1019 continue;
1020 clk_put(fmd->wbclk[i]);
1021 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1022 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001023}
1024
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001025static int fimc_md_get_clocks(struct fimc_md *fmd)
1026{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001027 struct device *dev = &fmd->pdev->dev;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001028 char clk_name[32];
1029 struct clk *clock;
Sylwester Nawrocki044c3722013-07-19 10:05:17 -03001030 int i, ret = 0;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001031
1032 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1033 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1034
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001035 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1036 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1037 clock = clk_get(dev, clk_name);
1038
1039 if (IS_ERR(clock)) {
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001040 dev_err(dev, "Failed to get clock: %s\n", clk_name);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001041 ret = PTR_ERR(clock);
1042 break;
1043 }
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001044 fmd->camclk[i].clock = clock;
1045 }
1046 if (ret)
1047 fimc_md_put_clocks(fmd);
1048
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001049 if (!fmd->use_isp)
1050 return 0;
1051 /*
1052 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1053 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1054 */
1055 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1056
1057 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1058 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1059 clock = clk_get(dev, clk_name);
1060 if (IS_ERR(clock)) {
1061 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1062 clk_name);
1063 ret = PTR_ERR(clock);
1064 break;
1065 }
1066 fmd->wbclk[i] = clock;
1067 }
1068 if (ret)
1069 fimc_md_put_clocks(fmd);
1070
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001071 return ret;
1072}
1073
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001074static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001075{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001076 struct exynos_video_entity *ve;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001077 struct fimc_pipeline *p;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001078 struct video_device *vdev;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001079 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001080
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001081 vdev = media_entity_to_video_device(entity);
1082 if (vdev->entity.use_count == 0)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001083 return 0;
1084
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001085 ve = vdev_to_exynos_video_entity(vdev);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001086 p = to_fimc_pipeline(ve->pipe);
1087 /*
1088 * Nothing to do if we are disabling the pipeline, some link
1089 * has been disconnected and p->subdevs array is cleared now.
1090 */
1091 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1092 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001093
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001094 if (enable)
1095 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1096 else
1097 ret = __fimc_pipeline_close(ve->pipe);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001098
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001099 if (ret == 0 && !enable)
1100 memset(p->subdevs, 0, sizeof(p->subdevs));
1101
1102 return ret;
1103}
1104
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -03001105/* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001106static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1107 struct media_entity_graph *graph)
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001108{
1109 struct media_entity *entity_err = entity;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001110 int ret;
1111
1112 /*
1113 * Walk current graph and call the pipeline open/close routine for each
1114 * opened video node that belongs to the graph of entities connected
1115 * through active links. This is needed as we cannot power on/off the
1116 * subdevs in random order.
1117 */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001118 media_entity_graph_walk_start(graph, entity);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001119
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001120 while ((entity = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001121 if (!is_media_entity_v4l2_io(entity))
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001122 continue;
1123
1124 ret = __fimc_md_modify_pipeline(entity, enable);
1125
1126 if (ret < 0)
1127 goto err;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001128 }
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001129
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001130 return 0;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001131
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001132err:
1133 media_entity_graph_walk_start(graph, entity_err);
1134
1135 while ((entity_err = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001136 if (!is_media_entity_v4l2_io(entity_err))
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001137 continue;
1138
1139 __fimc_md_modify_pipeline(entity_err, !enable);
1140
1141 if (entity_err == entity)
1142 break;
1143 }
1144
1145 return ret;
1146}
1147
1148static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1149 unsigned int notification)
1150{
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001151 struct media_entity_graph *graph =
1152 &container_of(link->graph_obj.mdev, struct fimc_md,
1153 media_dev)->link_setup_graph;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001154 struct media_entity *sink = link->sink->entity;
1155 int ret = 0;
1156
1157 /* Before link disconnection */
1158 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001159 ret = media_entity_graph_walk_init(graph,
1160 link->graph_obj.mdev);
1161 if (ret)
1162 return ret;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001163 if (!(flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001164 ret = __fimc_md_modify_pipelines(sink, false, graph);
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001165#if 0
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001166 else
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001167 /* TODO: Link state change validation */
1168#endif
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001169 /* After link activation */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001170 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1171 if (link->flags & MEDIA_LNK_FL_ENABLED)
1172 ret = __fimc_md_modify_pipelines(sink, true, graph);
1173 media_entity_graph_walk_cleanup(graph);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001174 }
1175
1176 return ret ? -EPIPE : 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001177}
1178
1179static ssize_t fimc_md_sysfs_show(struct device *dev,
1180 struct device_attribute *attr, char *buf)
1181{
1182 struct platform_device *pdev = to_platform_device(dev);
1183 struct fimc_md *fmd = platform_get_drvdata(pdev);
1184
1185 if (fmd->user_subdev_api)
1186 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1187
1188 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1189}
1190
1191static ssize_t fimc_md_sysfs_store(struct device *dev,
1192 struct device_attribute *attr,
1193 const char *buf, size_t count)
1194{
1195 struct platform_device *pdev = to_platform_device(dev);
1196 struct fimc_md *fmd = platform_get_drvdata(pdev);
1197 bool subdev_api;
1198 int i;
1199
1200 if (!strcmp(buf, "vid-dev\n"))
1201 subdev_api = false;
1202 else if (!strcmp(buf, "sub-dev\n"))
1203 subdev_api = true;
1204 else
1205 return count;
1206
1207 fmd->user_subdev_api = subdev_api;
1208 for (i = 0; i < FIMC_MAX_DEVS; i++)
1209 if (fmd->fimc[i])
1210 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1211 return count;
1212}
1213/*
1214 * This device attribute is to select video pipeline configuration method.
1215 * There are following valid values:
1216 * vid-dev - for V4L2 video node API only, subdevice will be configured
1217 * by the host driver.
1218 * sub-dev - for media controller API, subdevs must be configured in user
1219 * space before starting streaming.
1220 */
1221static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1222 fimc_md_sysfs_show, fimc_md_sysfs_store);
1223
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001224static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1225{
1226 struct device *dev = &fmd->pdev->dev;
1227 struct fimc_pinctrl *pctl = &fmd->pinctl;
1228
1229 pctl->pinctrl = devm_pinctrl_get(dev);
1230 if (IS_ERR(pctl->pinctrl))
1231 return PTR_ERR(pctl->pinctrl);
1232
1233 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1234 PINCTRL_STATE_DEFAULT);
1235 if (IS_ERR(pctl->state_default))
1236 return PTR_ERR(pctl->state_default);
1237
1238 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1239 PINCTRL_STATE_IDLE);
1240 return 0;
1241}
1242
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001243static int cam_clk_prepare(struct clk_hw *hw)
1244{
1245 struct cam_clk *camclk = to_cam_clk(hw);
1246 int ret;
1247
1248 if (camclk->fmd->pmf == NULL)
1249 return -ENODEV;
1250
1251 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1252 return ret < 0 ? ret : 0;
1253}
1254
1255static void cam_clk_unprepare(struct clk_hw *hw)
1256{
1257 struct cam_clk *camclk = to_cam_clk(hw);
1258
1259 if (camclk->fmd->pmf == NULL)
1260 return;
1261
1262 pm_runtime_put_sync(camclk->fmd->pmf);
1263}
1264
1265static const struct clk_ops cam_clk_ops = {
1266 .prepare = cam_clk_prepare,
1267 .unprepare = cam_clk_unprepare,
1268};
1269
1270static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1271{
1272 struct cam_clk_provider *cp = &fmd->clk_provider;
1273 unsigned int i;
1274
1275 if (cp->of_node)
1276 of_clk_del_provider(cp->of_node);
1277
1278 for (i = 0; i < cp->num_clocks; i++)
1279 clk_unregister(cp->clks[i]);
1280}
1281
1282static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1283{
1284 struct cam_clk_provider *cp = &fmd->clk_provider;
1285 struct device *dev = &fmd->pdev->dev;
1286 int i, ret;
1287
1288 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1289 struct cam_clk *camclk = &cp->camclk[i];
1290 struct clk_init_data init;
1291 const char *p_name;
1292
1293 ret = of_property_read_string_index(dev->of_node,
1294 "clock-output-names", i, &init.name);
1295 if (ret < 0)
1296 break;
1297
1298 p_name = __clk_get_name(fmd->camclk[i].clock);
1299
1300 /* It's safe since clk_register() will duplicate the string. */
1301 init.parent_names = &p_name;
1302 init.num_parents = 1;
1303 init.ops = &cam_clk_ops;
1304 init.flags = CLK_SET_RATE_PARENT;
1305 camclk->hw.init = &init;
1306 camclk->fmd = fmd;
1307
1308 cp->clks[i] = clk_register(NULL, &camclk->hw);
1309 if (IS_ERR(cp->clks[i])) {
1310 dev_err(dev, "failed to register clock: %s (%ld)\n",
1311 init.name, PTR_ERR(cp->clks[i]));
1312 ret = PTR_ERR(cp->clks[i]);
1313 goto err;
1314 }
1315 cp->num_clocks++;
1316 }
1317
1318 if (cp->num_clocks == 0) {
1319 dev_warn(dev, "clk provider not registered\n");
1320 return 0;
1321 }
1322
1323 cp->clk_data.clks = cp->clks;
1324 cp->clk_data.clk_num = cp->num_clocks;
1325 cp->of_node = dev->of_node;
1326 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1327 &cp->clk_data);
1328 if (ret == 0)
1329 return 0;
1330err:
1331 fimc_md_unregister_clk_provider(fmd);
1332 return ret;
1333}
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001334
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001335static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1336 struct v4l2_subdev *subdev,
1337 struct v4l2_async_subdev *asd)
1338{
1339 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1340 struct fimc_sensor_info *si = NULL;
1341 int i;
1342
1343 /* Find platform data for this sensor subdev */
1344 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1345 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1346 si = &fmd->sensor[i];
1347
1348 if (si == NULL)
1349 return -EINVAL;
1350
1351 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1352
1353 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1354 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1355 else
1356 subdev->grp_id = GRP_ID_SENSOR;
1357
1358 si->subdev = subdev;
1359
1360 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1361 subdev->name, fmd->num_sensors);
1362
1363 fmd->num_sensors++;
1364
1365 return 0;
1366}
1367
1368static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1369{
1370 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1371 int ret;
1372
1373 mutex_lock(&fmd->media_dev.graph_mutex);
1374
1375 ret = fimc_md_create_links(fmd);
1376 if (ret < 0)
1377 goto unlock;
1378
1379 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1380unlock:
1381 mutex_unlock(&fmd->media_dev.graph_mutex);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001382 if (ret < 0)
1383 return ret;
1384
1385 return media_device_register(&fmd->media_dev);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001386}
1387
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001388static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001389{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001390 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001391 struct v4l2_device *v4l2_dev;
1392 struct fimc_md *fmd;
1393 int ret;
1394
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001395 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001396 if (!fmd)
1397 return -ENOMEM;
1398
1399 spin_lock_init(&fmd->slock);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001400 INIT_LIST_HEAD(&fmd->pipelines);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001401 fmd->pdev = pdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001402
1403 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1404 sizeof(fmd->media_dev.model));
1405 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001406 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001407
1408 v4l2_dev = &fmd->v4l2_dev;
1409 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001410 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001411 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001412
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001413 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001414 fmd->user_subdev_api = true;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001415
Mauro Carvalho Chehab2e7508e2015-12-28 09:24:23 -02001416 media_device_init(&fmd->media_dev);
1417
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001418 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001419 if (ret < 0) {
1420 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001421 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001422 }
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001423
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001424 ret = fimc_md_get_clocks(fmd);
1425 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001426 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001427
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001428 ret = fimc_md_get_pinctrl(fmd);
1429 if (ret < 0) {
1430 if (ret != EPROBE_DEFER)
1431 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001432 goto err_clk;
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001433 }
1434
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001435 platform_set_drvdata(pdev, fmd);
1436
1437 /* Protect the media graph while we're registering entities */
1438 mutex_lock(&fmd->media_dev.graph_mutex);
1439
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001440 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001441 if (ret) {
1442 mutex_unlock(&fmd->media_dev.graph_mutex);
1443 goto err_clk;
1444 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001445
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001446 ret = fimc_md_register_sensor_entities(fmd);
1447 if (ret) {
1448 mutex_unlock(&fmd->media_dev.graph_mutex);
1449 goto err_m_ent;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001450 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001451
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001452 mutex_unlock(&fmd->media_dev.graph_mutex);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001453
1454 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001455 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001456 goto err_m_ent;
1457 /*
1458 * FIMC platform devices need to be registered before the sclk_cam
1459 * clocks provider, as one of these devices needs to be activated
1460 * to enable the clock.
1461 */
1462 ret = fimc_md_register_clk_provider(fmd);
1463 if (ret < 0) {
1464 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1465 goto err_attr;
1466 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001467
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001468 if (fmd->num_sensors > 0) {
1469 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1470 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1471 fmd->subdev_notifier.bound = subdev_notifier_bound;
1472 fmd->subdev_notifier.complete = subdev_notifier_complete;
1473 fmd->num_sensors = 0;
1474
1475 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1476 &fmd->subdev_notifier);
1477 if (ret)
1478 goto err_clk_p;
1479 }
1480
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001481 return 0;
1482
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001483err_clk_p:
1484 fimc_md_unregister_clk_provider(fmd);
1485err_attr:
1486 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001487err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001488 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001489err_m_ent:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001490 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001491err_md:
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001492 media_device_cleanup(&fmd->media_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001493 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001494 return ret;
1495}
1496
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001497static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001498{
1499 struct fimc_md *fmd = platform_get_drvdata(pdev);
1500
1501 if (!fmd)
1502 return 0;
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001503
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001504 fimc_md_unregister_clk_provider(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001505 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1506
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001507 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001508 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1509 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001510 fimc_md_pipelines_free(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001511 media_device_unregister(&fmd->media_dev);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001512 media_device_cleanup(&fmd->media_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001513 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001514
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001515 return 0;
1516}
1517
Krzysztof Kozlowskic42639d2015-05-01 12:51:01 -03001518static const struct platform_device_id fimc_driver_ids[] __always_unused = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001519 { .name = "s5p-fimc-md" },
1520 { },
1521};
1522MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1523
1524static const struct of_device_id fimc_md_of_match[] = {
1525 { .compatible = "samsung,fimc" },
1526 { },
1527};
1528MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1529
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001530static struct platform_driver fimc_md_driver = {
1531 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001532 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001533 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001534 .of_match_table = of_match_ptr(fimc_md_of_match),
1535 .name = "s5p-fimc-md",
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001536 }
1537};
1538
Sachin Kamat7e566be2012-05-26 11:11:54 -03001539static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001540{
1541 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001542
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001543 request_module("s5p-csis");
1544 ret = fimc_register_driver();
1545 if (ret)
1546 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001547
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001548 return platform_driver_register(&fimc_md_driver);
1549}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001550
1551static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001552{
1553 platform_driver_unregister(&fimc_md_driver);
1554 fimc_unregister_driver();
1555}
1556
1557module_init(fimc_md_init);
1558module_exit(fimc_md_exit);
1559
1560MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1561MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1562MODULE_LICENSE("GPL");
1563MODULE_VERSION("2.0.1");