blob: 26a4e5c7b47ad08f261fd1ca055301653bf6721f [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/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300189 * __fimc_pipeline_open - update the pipeline information, enable power
190 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300191 * @me: media entity to start graph walk with
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300192 * @prepare: true to walk the current pipeline and acquire all subdevs
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 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300196static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300197 struct media_entity *me, bool prepare)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300198{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300199 struct fimc_md *fmd = entity_to_fimc_mdev(me);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300200 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300201 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300202 int ret;
203
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300204 if (WARN_ON(p == NULL || me == NULL))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300205 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300206
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300207 if (prepare)
208 fimc_pipeline_prepare(p, me);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300209
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300210 sd = p->subdevs[IDX_SENSOR];
211 if (sd == NULL)
212 return -EINVAL;
213
214 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
215 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
216 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
217 if (ret < 0)
218 return ret;
219 }
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300220
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300221 ret = fimc_pipeline_s_power(p, 1);
222 if (!ret)
223 return 0;
224
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300225 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
226 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
227
228 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300229}
230
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300231/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300232 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300233 * @fimc: fimc device terminating the pipeline
234 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300235 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300236 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300237static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300238{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300239 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300240 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
241 struct fimc_md *fmd;
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300242 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300243
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300244 if (sd == NULL) {
245 pr_warn("%s(): No sensor subdev\n", __func__);
246 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300247 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300248
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300249 ret = fimc_pipeline_s_power(p, 0);
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300250
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300251 fmd = entity_to_fimc_mdev(&sd->entity);
252
253 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
254 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
255 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
256
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300257 return ret == -ENXIO ? 0 : ret;
258}
259
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300260/**
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300261 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300262 * @pipeline: video pipeline structure
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300263 * @on: passed as the s_stream() callback argument
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300264 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300265static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300266{
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300267 static const u8 seq[2][IDX_MAX] = {
268 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
269 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
270 };
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300271 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300272 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300273
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300274 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300275 return -ENODEV;
276
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300277 for (i = 0; i < IDX_MAX; i++) {
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300278 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300279
280 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
281
282 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300283 goto error;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300284 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300285 return 0;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300286error:
287 for (; i >= 0; i--) {
288 unsigned int idx = seq[on][i];
289 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
290 }
291 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300292}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300293
294/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300295static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300296 .open = __fimc_pipeline_open,
297 .close = __fimc_pipeline_close,
298 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300299};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300300
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300301static struct exynos_media_pipeline *fimc_md_pipeline_create(
302 struct fimc_md *fmd)
303{
304 struct fimc_pipeline *p;
305
306 p = kzalloc(sizeof(*p), GFP_KERNEL);
307 if (!p)
308 return NULL;
309
310 list_add_tail(&p->list, &fmd->pipelines);
311
312 p->ep.ops = &fimc_pipeline_ops;
313 return &p->ep;
314}
315
316static void fimc_md_pipelines_free(struct fimc_md *fmd)
317{
318 while (!list_empty(&fmd->pipelines)) {
319 struct fimc_pipeline *p;
320
321 p = list_entry(fmd->pipelines.next, typeof(*p), list);
322 list_del(&p->list);
323 kfree(p);
324 }
325}
326
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300327/* Parse port node and register as a sub-device any sensor specified there. */
328static int fimc_md_parse_port_node(struct fimc_md *fmd,
329 struct device_node *port,
330 unsigned int index)
331{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300332 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300333 struct device_node *rem, *ep, *np;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300334 struct v4l2_of_endpoint endpoint;
Javier Martinez Canillas234eab82016-01-11 14:47:15 -0200335 int ret;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300336
337 /* Assume here a port node can have only one endpoint node. */
338 ep = of_get_next_child(port, NULL);
339 if (!ep)
340 return 0;
341
Javier Martinez Canillas234eab82016-01-11 14:47:15 -0200342 ret = v4l2_of_parse_endpoint(ep, &endpoint);
343 if (ret) {
344 of_node_put(ep);
345 return ret;
346 }
347
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100348 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300349 return -EINVAL;
350
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100351 pd->mux_id = (endpoint.base.port - 1) & 0x1;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300352
Philipp Zabelfd9fdb72014-02-10 22:01:48 +0100353 rem = of_graph_get_remote_port_parent(ep);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300354 of_node_put(ep);
355 if (rem == NULL) {
356 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
357 ep->full_name);
358 return 0;
359 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300360
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100361 if (fimc_input_is_parallel(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300362 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
363 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
364 else
365 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
366 pd->flags = endpoint.bus.parallel.flags;
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100367 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300368 /*
369 * MIPI CSI-2: only input mux selection and
370 * the sensor's clock frequency is needed.
371 */
372 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
373 } else {
374 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100375 endpoint.base.port, rem->full_name);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300376 }
377 /*
378 * For FIMC-IS handled sensors, that are placed under i2c-isp device
379 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
380 * input. Sensors are attached to the FIMC-LITE hostdata interface
381 * directly or through MIPI-CSIS, depending on the external media bus
382 * used. This needs to be handled in a more reliable way, not by just
383 * checking parent's node name.
384 */
385 np = of_get_parent(rem);
386
387 if (np && !of_node_cmp(np->name, "i2c-isp"))
388 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
389 else
390 pd->fimc_bus_type = pd->sensor_bus_type;
391
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300392 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
393 return -EINVAL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300394
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300395 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
396 fmd->sensor[index].asd.match.of.node = rem;
397 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
398
399 fmd->num_sensors++;
400
401 of_node_put(rem);
402 return 0;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300403}
404
405/* Register all SoC external sub-devices */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300406static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300407{
408 struct device_node *parent = fmd->pdev->dev.of_node;
409 struct device_node *node, *ports;
410 int index = 0;
411 int ret;
412
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300413 /*
414 * Runtime resume one of the FIMC entities to make sure
415 * the sclk_cam clocks are not globally disabled.
416 */
417 if (!fmd->pmf)
418 return -ENXIO;
419
420 ret = pm_runtime_get_sync(fmd->pmf);
421 if (ret < 0)
422 return ret;
423
424 fmd->num_sensors = 0;
425
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300426 /* Attach sensors linked to MIPI CSI-2 receivers */
427 for_each_available_child_of_node(parent, node) {
428 struct device_node *port;
429
430 if (of_node_cmp(node->name, "csis"))
431 continue;
432 /* The csis node can have only port subnode. */
433 port = of_get_next_child(node, NULL);
434 if (!port)
435 continue;
436
437 ret = fimc_md_parse_port_node(fmd, port, index);
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200438 if (ret < 0) {
439 of_node_put(node);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300440 goto rpm_put;
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200441 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300442 index++;
443 }
444
445 /* Attach sensors listed in the parallel-ports node */
446 ports = of_get_child_by_name(parent, "parallel-ports");
447 if (!ports)
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300448 goto rpm_put;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300449
450 for_each_child_of_node(ports, node) {
451 ret = fimc_md_parse_port_node(fmd, node, index);
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200452 if (ret < 0) {
453 of_node_put(node);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300454 break;
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200455 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300456 index++;
457 }
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300458rpm_put:
459 pm_runtime_put(fmd->pmf);
460 return ret;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300461}
462
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300463static int __of_get_csis_id(struct device_node *np)
464{
465 u32 reg = 0;
466
467 np = of_get_child_by_name(np, "port");
468 if (!np)
469 return -EINVAL;
470 of_property_read_u32(np, "reg", &reg);
471 return reg - FIMC_INPUT_MIPI_CSI2_0;
472}
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300473
474/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300475 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300476 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300477static int register_fimc_lite_entity(struct fimc_md *fmd,
478 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300479{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300480 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300481 struct exynos_media_pipeline *ep;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300482 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300483
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300484 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
485 fmd->fimc_lite[fimc_lite->index]))
486 return -EBUSY;
487
488 sd = &fimc_lite->subdev;
489 sd->grp_id = GRP_ID_FLITE;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300490
491 ep = fimc_md_pipeline_create(fmd);
492 if (!ep)
493 return -ENOMEM;
494
495 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300496
497 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
498 if (!ret)
499 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
500 else
501 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
502 fimc_lite->index);
503 return ret;
504}
505
506static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
507{
508 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300509 struct exynos_media_pipeline *ep;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300510 int ret;
511
512 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
513 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300514
Sachin Kamat8163ec02012-10-25 10:56:00 -0300515 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300516 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300517
518 ep = fimc_md_pipeline_create(fmd);
519 if (!ep)
520 return -ENOMEM;
521
522 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300523
524 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300525 if (!ret) {
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300526 if (!fmd->pmf && fimc->pdev)
527 fmd->pmf = &fimc->pdev->dev;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300528 fmd->fimc[fimc->id] = fimc;
529 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
530 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300531 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
532 fimc->id, ret);
533 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300534 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300535}
536
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300537static int register_csis_entity(struct fimc_md *fmd,
538 struct platform_device *pdev,
539 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300540{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300541 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300542 int id, ret;
543
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300544 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300545
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300546 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
547 return -ENOENT;
548
549 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300550 return -EBUSY;
551
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300552 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300553 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300554 if (!ret)
555 fmd->csis[id].sd = sd;
556 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300557 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300558 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300559 return ret;
560}
561
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300562static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
563{
564 struct v4l2_subdev *sd = &is->isp.subdev;
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300565 struct exynos_media_pipeline *ep;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300566 int ret;
567
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300568 /* Allocate pipeline object for the ISP capture video node. */
569 ep = fimc_md_pipeline_create(fmd);
570 if (!ep)
571 return -ENOMEM;
572
573 v4l2_set_subdev_hostdata(sd, ep);
574
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300575 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
576 if (ret) {
577 v4l2_err(&fmd->v4l2_dev,
578 "Failed to register FIMC-ISP (%d)\n", ret);
579 return ret;
580 }
581
582 fmd->fimc_is = is;
583 return 0;
584}
585
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300586static int fimc_md_register_platform_entity(struct fimc_md *fmd,
587 struct platform_device *pdev,
588 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300589{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300590 struct device *dev = &pdev->dev;
591 int ret = -EPROBE_DEFER;
592 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300593
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300594 /* Lock to ensure dev->driver won't change. */
595 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300596
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300597 if (!dev->driver || !try_module_get(dev->driver->owner))
598 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300599
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300600 drvdata = dev_get_drvdata(dev);
Jonathan McCrohanf58c91c2013-10-20 21:34:01 -0300601 /* Some subdev didn't probe successfully id drvdata is NULL */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300602 if (drvdata) {
603 switch (plat_entity) {
604 case IDX_FIMC:
605 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300606 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300607 case IDX_FLITE:
608 ret = register_fimc_lite_entity(fmd, drvdata);
609 break;
610 case IDX_CSIS:
611 ret = register_csis_entity(fmd, pdev, drvdata);
612 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300613 case IDX_IS_ISP:
614 ret = register_fimc_is_entity(fmd, drvdata);
615 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300616 default:
617 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300618 }
619 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300620
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300621 module_put(dev->driver->owner);
622dev_unlock:
623 device_unlock(dev);
624 if (ret == -EPROBE_DEFER)
625 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
626 dev_name(dev));
627 else if (ret < 0)
628 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
629 dev_name(dev), ret);
630 return ret;
631}
632
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300633/* Register FIMC, FIMC-LITE and CSIS media entities */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300634static int fimc_md_register_platform_entities(struct fimc_md *fmd,
635 struct device_node *parent)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300636{
637 struct device_node *node;
638 int ret = 0;
639
640 for_each_available_child_of_node(parent, node) {
641 struct platform_device *pdev;
642 int plat_entity = -1;
643
644 pdev = of_find_device_by_node(node);
645 if (!pdev)
646 continue;
647
648 /* If driver of any entity isn't ready try all again later. */
649 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
650 plat_entity = IDX_CSIS;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300651 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
652 plat_entity = IDX_IS_ISP;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300653 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
654 plat_entity = IDX_FLITE;
655 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
656 !of_property_read_bool(node, "samsung,lcd-wb"))
657 plat_entity = IDX_FIMC;
658
659 if (plat_entity >= 0)
660 ret = fimc_md_register_platform_entity(fmd, pdev,
661 plat_entity);
662 put_device(&pdev->dev);
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200663 if (ret < 0) {
664 of_node_put(node);
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300665 break;
Amitoj Kaur Chawla458a3952016-01-25 13:21:37 -0200666 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300667 }
668
669 return ret;
670}
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300671
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300672static void fimc_md_unregister_entities(struct fimc_md *fmd)
673{
674 int i;
675
676 for (i = 0; i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300677 struct fimc_dev *dev = fmd->fimc[i];
678 if (dev == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300679 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300680 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
681 dev->vid_cap.ve.pipe = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300682 fmd->fimc[i] = NULL;
683 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300684 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300685 struct fimc_lite *dev = fmd->fimc_lite[i];
686 if (dev == NULL)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300687 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300688 v4l2_device_unregister_subdev(&dev->subdev);
689 dev->ve.pipe = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300690 fmd->fimc_lite[i] = NULL;
691 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300692 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
693 if (fmd->csis[i].sd == NULL)
694 continue;
695 v4l2_device_unregister_subdev(fmd->csis[i].sd);
696 fmd->csis[i].sd = NULL;
697 }
Sylwester Nawrockie41a35c2013-04-18 09:05:54 -0300698
699 if (fmd->fimc_is)
700 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
701
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300702 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300703}
704
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300705/**
706 * __fimc_md_create_fimc_links - create links to all FIMC entities
707 * @fmd: fimc media device
708 * @source: the source entity to create links to all fimc entities from
709 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
710 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300711 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300712 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300713static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
714 struct media_entity *source,
715 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300716 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300717{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300718 struct fimc_source_info *si = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300719 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300720 unsigned int flags = 0;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300721 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300722
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300723 if (sensor) {
724 si = v4l2_get_subdev_hostdata(sensor);
725 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300726 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300727 ret = 1;
728 }
729
730 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300731 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300732 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300733 /*
734 * Some FIMC variants are not fitted with camera capture
735 * interface. Skip creating a link from sensor for those.
736 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300737 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300738 continue;
739
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300740 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300741
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300742 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300743 ret = media_create_pad_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300744 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300745 if (ret)
746 return ret;
747
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300748 /* Notify FIMC capture subdev entity */
749 ret = media_entity_call(sink, link_setup, &sink->pads[0],
750 &source->pads[pad], flags);
751 if (ret)
752 break;
753
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300754 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300755 source->name, flags ? '=' : '-', sink->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300756 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300757
758 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
759 if (!fmd->fimc_lite[i])
760 continue;
761
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300762 sink = &fmd->fimc_lite[i]->subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300763 ret = media_create_pad_link(source, pad, sink,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300764 FLITE_SD_PAD_SINK, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300765 if (ret)
766 return ret;
767
768 /* Notify FIMC-LITE subdev entity */
769 ret = media_entity_call(sink, link_setup, &sink->pads[0],
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300770 &source->pads[pad], 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300771 if (ret)
772 break;
773
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300774 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
775 source->name, sink->name);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300776 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300777 return 0;
778}
779
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300780/* Create links from FIMC-LITE source pads to other entities */
781static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
782{
783 struct media_entity *source, *sink;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300784 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300785
786 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
787 struct fimc_lite *fimc = fmd->fimc_lite[i];
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300788
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300789 if (fimc == NULL)
790 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300791
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300792 source = &fimc->subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300793 sink = &fimc->ve.vdev.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300794 /* FIMC-LITE's subdev and video node */
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300795 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300796 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300797 if (ret)
798 break;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300799 /* Link from FIMC-LITE to IS-ISP subdev */
800 sink = &fmd->fimc_is->isp.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300801 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300802 sink, 0, 0);
803 if (ret)
804 break;
805 }
806
807 return ret;
808}
809
810/* Create FIMC-IS links */
811static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
812{
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300813 struct fimc_isp *isp = &fmd->fimc_is->isp;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300814 struct media_entity *source, *sink;
815 int i, ret;
816
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300817 source = &isp->subdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300818
819 for (i = 0; i < FIMC_MAX_DEVS; i++) {
820 if (fmd->fimc[i] == NULL)
821 continue;
822
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300823 /* Link from FIMC-IS-ISP subdev to FIMC */
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300824 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300825 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300826 sink, FIMC_SD_PAD_SINK_FIFO, 0);
827 if (ret)
828 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300829 }
830
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300831 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
832 sink = &isp->video_capture.ve.vdev.entity;
833
834 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
835 if (sink->num_pads == 0)
836 return 0;
837
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300838 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300839 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300840}
841
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300842/**
843 * fimc_md_create_links - create default links between registered entities
844 *
845 * Parallel interface sensor entities are connected directly to FIMC capture
846 * entities. The sensors using MIPI CSIS bus are connected through immutable
847 * link with CSI receiver entity specified by mux_id. Any registered CSIS
848 * entity has a link to each registered FIMC capture entity. Enabled links
849 * are created by default between each subsequent registered sensor and
850 * subsequent FIMC capture entity. The number of default active links is
851 * determined by the number of available sensors or FIMC entities,
852 * whichever is less.
853 */
854static int fimc_md_create_links(struct fimc_md *fmd)
855{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300856 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300857 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300858 struct fimc_source_info *pdata;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300859 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300860 int i, pad, fimc_id = 0, ret = 0;
861 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300862
863 for (i = 0; i < fmd->num_sensors; i++) {
864 if (fmd->sensor[i].subdev == NULL)
865 continue;
866
867 sensor = fmd->sensor[i].subdev;
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300868 pdata = v4l2_get_subdev_hostdata(sensor);
869 if (!pdata)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300870 continue;
871
872 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300873
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300874 switch (pdata->sensor_bus_type) {
875 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300876 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
877 "Wrong CSI channel id: %d\n", pdata->mux_id))
878 return -EINVAL;
879
880 csis = fmd->csis[pdata->mux_id].sd;
881 if (WARN(csis == NULL,
882 "MIPI-CSI interface specified "
883 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300884 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300885
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300886 pad = sensor->entity.num_pads - 1;
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300887 ret = media_create_pad_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300888 &csis->entity, CSIS_PAD_SINK,
889 MEDIA_LNK_FL_IMMUTABLE |
890 MEDIA_LNK_FL_ENABLED);
891 if (ret)
892 return ret;
893
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300894 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300895 sensor->entity.name, csis->entity.name);
896
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300897 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300898 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300899 break;
900
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300901 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300902 source = &sensor->entity;
903 pad = 0;
904 break;
905
906 default:
907 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300908 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300909 return -EINVAL;
910 }
911 if (source == NULL)
912 continue;
913
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300914 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300915 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300916 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300917 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300918
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300919 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300920 if (fmd->csis[i].sd == NULL)
921 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300922
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300923 source = &fmd->csis[i].sd->entity;
924 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300925 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300926
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300927 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300928 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300929 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300930 }
931
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300932 /* Create immutable links between each FIMC's subdev and video node */
933 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
934 for (i = 0; i < FIMC_MAX_DEVS; i++) {
935 if (!fmd->fimc[i])
936 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300937
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300938 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300939 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300940
Mauro Carvalho Chehab8df00a12015-08-07 08:14:38 -0300941 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300942 sink, 0, flags);
943 if (ret)
944 break;
945 }
946
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300947 ret = __fimc_md_create_flite_source_links(fmd);
948 if (ret < 0)
949 return ret;
950
951 if (fmd->use_isp)
952 ret = __fimc_md_create_fimc_is_links(fmd);
953
954 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300955}
956
957/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300958 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300959 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300960static void fimc_md_put_clocks(struct fimc_md *fmd)
961{
962 int i = FIMC_MAX_CAMCLKS;
963
964 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300965 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300966 continue;
967 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300968 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300969 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300970
971 /* Writeback (PIXELASYNCMx) clocks */
972 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
973 if (IS_ERR(fmd->wbclk[i]))
974 continue;
975 clk_put(fmd->wbclk[i]);
976 fmd->wbclk[i] = ERR_PTR(-EINVAL);
977 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300978}
979
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300980static int fimc_md_get_clocks(struct fimc_md *fmd)
981{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300982 struct device *dev = &fmd->pdev->dev;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300983 char clk_name[32];
984 struct clk *clock;
Sylwester Nawrocki044c3722013-07-19 10:05:17 -0300985 int i, ret = 0;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300986
987 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
988 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
989
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300990 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
991 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
992 clock = clk_get(dev, clk_name);
993
994 if (IS_ERR(clock)) {
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300995 dev_err(dev, "Failed to get clock: %s\n", clk_name);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300996 ret = PTR_ERR(clock);
997 break;
998 }
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300999 fmd->camclk[i].clock = clock;
1000 }
1001 if (ret)
1002 fimc_md_put_clocks(fmd);
1003
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -03001004 if (!fmd->use_isp)
1005 return 0;
1006 /*
1007 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1008 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1009 */
1010 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1011
1012 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1013 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1014 clock = clk_get(dev, clk_name);
1015 if (IS_ERR(clock)) {
1016 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1017 clk_name);
1018 ret = PTR_ERR(clock);
1019 break;
1020 }
1021 fmd->wbclk[i] = clock;
1022 }
1023 if (ret)
1024 fimc_md_put_clocks(fmd);
1025
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001026 return ret;
1027}
1028
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001029static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001030{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001031 struct exynos_video_entity *ve;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001032 struct fimc_pipeline *p;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001033 struct video_device *vdev;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001034 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001035
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001036 vdev = media_entity_to_video_device(entity);
1037 if (vdev->entity.use_count == 0)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001038 return 0;
1039
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001040 ve = vdev_to_exynos_video_entity(vdev);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001041 p = to_fimc_pipeline(ve->pipe);
1042 /*
1043 * Nothing to do if we are disabling the pipeline, some link
1044 * has been disconnected and p->subdevs array is cleared now.
1045 */
1046 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1047 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001048
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001049 if (enable)
1050 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1051 else
1052 ret = __fimc_pipeline_close(ve->pipe);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001053
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001054 if (ret == 0 && !enable)
1055 memset(p->subdevs, 0, sizeof(p->subdevs));
1056
1057 return ret;
1058}
1059
Javier Martinez Canillasd10c9892015-08-19 12:35:21 -03001060/* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001061static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1062 struct media_entity_graph *graph)
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001063{
1064 struct media_entity *entity_err = entity;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001065 int ret;
1066
1067 /*
1068 * Walk current graph and call the pipeline open/close routine for each
1069 * opened video node that belongs to the graph of entities connected
1070 * through active links. This is needed as we cannot power on/off the
1071 * subdevs in random order.
1072 */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001073 media_entity_graph_walk_start(graph, entity);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001074
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001075 while ((entity = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001076 if (!is_media_entity_v4l2_io(entity))
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001077 continue;
1078
1079 ret = __fimc_md_modify_pipeline(entity, enable);
1080
1081 if (ret < 0)
1082 goto err;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001083 }
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001084
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001085 return 0;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001086
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001087err:
1088 media_entity_graph_walk_start(graph, entity_err);
1089
1090 while ((entity_err = media_entity_graph_walk_next(graph))) {
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001091 if (!is_media_entity_v4l2_io(entity_err))
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001092 continue;
1093
1094 __fimc_md_modify_pipeline(entity_err, !enable);
1095
1096 if (entity_err == entity)
1097 break;
1098 }
1099
1100 return ret;
1101}
1102
1103static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1104 unsigned int notification)
1105{
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001106 struct media_entity_graph *graph =
1107 &container_of(link->graph_obj.mdev, struct fimc_md,
1108 media_dev)->link_setup_graph;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001109 struct media_entity *sink = link->sink->entity;
1110 int ret = 0;
1111
1112 /* Before link disconnection */
1113 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001114 ret = media_entity_graph_walk_init(graph,
1115 link->graph_obj.mdev);
1116 if (ret)
1117 return ret;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001118 if (!(flags & MEDIA_LNK_FL_ENABLED))
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001119 ret = __fimc_md_modify_pipelines(sink, false, graph);
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001120#if 0
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001121 else
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001122 /* TODO: Link state change validation */
1123#endif
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001124 /* After link activation */
Sakari Ailusfd7e5302015-12-16 15:32:25 +02001125 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1126 if (link->flags & MEDIA_LNK_FL_ENABLED)
1127 ret = __fimc_md_modify_pipelines(sink, true, graph);
1128 media_entity_graph_walk_cleanup(graph);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001129 }
1130
1131 return ret ? -EPIPE : 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001132}
1133
1134static ssize_t fimc_md_sysfs_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136{
1137 struct platform_device *pdev = to_platform_device(dev);
1138 struct fimc_md *fmd = platform_get_drvdata(pdev);
1139
1140 if (fmd->user_subdev_api)
1141 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1142
1143 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1144}
1145
1146static ssize_t fimc_md_sysfs_store(struct device *dev,
1147 struct device_attribute *attr,
1148 const char *buf, size_t count)
1149{
1150 struct platform_device *pdev = to_platform_device(dev);
1151 struct fimc_md *fmd = platform_get_drvdata(pdev);
1152 bool subdev_api;
1153 int i;
1154
1155 if (!strcmp(buf, "vid-dev\n"))
1156 subdev_api = false;
1157 else if (!strcmp(buf, "sub-dev\n"))
1158 subdev_api = true;
1159 else
1160 return count;
1161
1162 fmd->user_subdev_api = subdev_api;
1163 for (i = 0; i < FIMC_MAX_DEVS; i++)
1164 if (fmd->fimc[i])
1165 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1166 return count;
1167}
1168/*
1169 * This device attribute is to select video pipeline configuration method.
1170 * There are following valid values:
1171 * vid-dev - for V4L2 video node API only, subdevice will be configured
1172 * by the host driver.
1173 * sub-dev - for media controller API, subdevs must be configured in user
1174 * space before starting streaming.
1175 */
1176static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1177 fimc_md_sysfs_show, fimc_md_sysfs_store);
1178
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001179static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1180{
1181 struct device *dev = &fmd->pdev->dev;
1182 struct fimc_pinctrl *pctl = &fmd->pinctl;
1183
1184 pctl->pinctrl = devm_pinctrl_get(dev);
1185 if (IS_ERR(pctl->pinctrl))
1186 return PTR_ERR(pctl->pinctrl);
1187
1188 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1189 PINCTRL_STATE_DEFAULT);
1190 if (IS_ERR(pctl->state_default))
1191 return PTR_ERR(pctl->state_default);
1192
1193 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1194 PINCTRL_STATE_IDLE);
1195 return 0;
1196}
1197
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001198static int cam_clk_prepare(struct clk_hw *hw)
1199{
1200 struct cam_clk *camclk = to_cam_clk(hw);
1201 int ret;
1202
1203 if (camclk->fmd->pmf == NULL)
1204 return -ENODEV;
1205
1206 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1207 return ret < 0 ? ret : 0;
1208}
1209
1210static void cam_clk_unprepare(struct clk_hw *hw)
1211{
1212 struct cam_clk *camclk = to_cam_clk(hw);
1213
1214 if (camclk->fmd->pmf == NULL)
1215 return;
1216
1217 pm_runtime_put_sync(camclk->fmd->pmf);
1218}
1219
1220static const struct clk_ops cam_clk_ops = {
1221 .prepare = cam_clk_prepare,
1222 .unprepare = cam_clk_unprepare,
1223};
1224
1225static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1226{
1227 struct cam_clk_provider *cp = &fmd->clk_provider;
1228 unsigned int i;
1229
1230 if (cp->of_node)
1231 of_clk_del_provider(cp->of_node);
1232
1233 for (i = 0; i < cp->num_clocks; i++)
1234 clk_unregister(cp->clks[i]);
1235}
1236
1237static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1238{
1239 struct cam_clk_provider *cp = &fmd->clk_provider;
1240 struct device *dev = &fmd->pdev->dev;
1241 int i, ret;
1242
1243 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1244 struct cam_clk *camclk = &cp->camclk[i];
1245 struct clk_init_data init;
1246 const char *p_name;
1247
1248 ret = of_property_read_string_index(dev->of_node,
1249 "clock-output-names", i, &init.name);
1250 if (ret < 0)
1251 break;
1252
1253 p_name = __clk_get_name(fmd->camclk[i].clock);
1254
1255 /* It's safe since clk_register() will duplicate the string. */
1256 init.parent_names = &p_name;
1257 init.num_parents = 1;
1258 init.ops = &cam_clk_ops;
1259 init.flags = CLK_SET_RATE_PARENT;
1260 camclk->hw.init = &init;
1261 camclk->fmd = fmd;
1262
1263 cp->clks[i] = clk_register(NULL, &camclk->hw);
1264 if (IS_ERR(cp->clks[i])) {
1265 dev_err(dev, "failed to register clock: %s (%ld)\n",
1266 init.name, PTR_ERR(cp->clks[i]));
1267 ret = PTR_ERR(cp->clks[i]);
1268 goto err;
1269 }
1270 cp->num_clocks++;
1271 }
1272
1273 if (cp->num_clocks == 0) {
1274 dev_warn(dev, "clk provider not registered\n");
1275 return 0;
1276 }
1277
1278 cp->clk_data.clks = cp->clks;
1279 cp->clk_data.clk_num = cp->num_clocks;
1280 cp->of_node = dev->of_node;
1281 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1282 &cp->clk_data);
1283 if (ret == 0)
1284 return 0;
1285err:
1286 fimc_md_unregister_clk_provider(fmd);
1287 return ret;
1288}
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001289
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001290static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1291 struct v4l2_subdev *subdev,
1292 struct v4l2_async_subdev *asd)
1293{
1294 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1295 struct fimc_sensor_info *si = NULL;
1296 int i;
1297
1298 /* Find platform data for this sensor subdev */
1299 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1300 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1301 si = &fmd->sensor[i];
1302
1303 if (si == NULL)
1304 return -EINVAL;
1305
1306 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1307
1308 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1309 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1310 else
1311 subdev->grp_id = GRP_ID_SENSOR;
1312
1313 si->subdev = subdev;
1314
1315 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1316 subdev->name, fmd->num_sensors);
1317
1318 fmd->num_sensors++;
1319
1320 return 0;
1321}
1322
1323static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1324{
1325 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1326 int ret;
1327
1328 mutex_lock(&fmd->media_dev.graph_mutex);
1329
1330 ret = fimc_md_create_links(fmd);
1331 if (ret < 0)
1332 goto unlock;
1333
1334 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1335unlock:
1336 mutex_unlock(&fmd->media_dev.graph_mutex);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001337 if (ret < 0)
1338 return ret;
1339
1340 return media_device_register(&fmd->media_dev);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001341}
1342
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001343static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001344{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001345 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001346 struct v4l2_device *v4l2_dev;
1347 struct fimc_md *fmd;
1348 int ret;
1349
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001350 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001351 if (!fmd)
1352 return -ENOMEM;
1353
1354 spin_lock_init(&fmd->slock);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001355 INIT_LIST_HEAD(&fmd->pipelines);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001356 fmd->pdev = pdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001357
1358 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1359 sizeof(fmd->media_dev.model));
1360 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001361 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001362
1363 v4l2_dev = &fmd->v4l2_dev;
1364 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001365 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001366 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001367
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001368 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001369 fmd->user_subdev_api = true;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001370
Mauro Carvalho Chehab2e7508e2015-12-28 09:24:23 -02001371 media_device_init(&fmd->media_dev);
1372
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001373 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001374 if (ret < 0) {
1375 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001376 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001377 }
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001378
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001379 ret = fimc_md_get_clocks(fmd);
1380 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001381 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001382
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001383 ret = fimc_md_get_pinctrl(fmd);
1384 if (ret < 0) {
1385 if (ret != EPROBE_DEFER)
1386 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001387 goto err_clk;
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001388 }
1389
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001390 platform_set_drvdata(pdev, fmd);
1391
1392 /* Protect the media graph while we're registering entities */
1393 mutex_lock(&fmd->media_dev.graph_mutex);
1394
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001395 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001396 if (ret) {
1397 mutex_unlock(&fmd->media_dev.graph_mutex);
1398 goto err_clk;
1399 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001400
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001401 ret = fimc_md_register_sensor_entities(fmd);
1402 if (ret) {
1403 mutex_unlock(&fmd->media_dev.graph_mutex);
1404 goto err_m_ent;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001405 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001406
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001407 mutex_unlock(&fmd->media_dev.graph_mutex);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001408
1409 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001410 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001411 goto err_m_ent;
1412 /*
1413 * FIMC platform devices need to be registered before the sclk_cam
1414 * clocks provider, as one of these devices needs to be activated
1415 * to enable the clock.
1416 */
1417 ret = fimc_md_register_clk_provider(fmd);
1418 if (ret < 0) {
1419 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1420 goto err_attr;
1421 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001422
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001423 if (fmd->num_sensors > 0) {
1424 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1425 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1426 fmd->subdev_notifier.bound = subdev_notifier_bound;
1427 fmd->subdev_notifier.complete = subdev_notifier_complete;
1428 fmd->num_sensors = 0;
1429
1430 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1431 &fmd->subdev_notifier);
1432 if (ret)
1433 goto err_clk_p;
1434 }
1435
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001436 return 0;
1437
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001438err_clk_p:
1439 fimc_md_unregister_clk_provider(fmd);
1440err_attr:
1441 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001442err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001443 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001444err_m_ent:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001445 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001446err_md:
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001447 media_device_cleanup(&fmd->media_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001448 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001449 return ret;
1450}
1451
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001452static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001453{
1454 struct fimc_md *fmd = platform_get_drvdata(pdev);
1455
1456 if (!fmd)
1457 return 0;
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001458
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001459 fimc_md_unregister_clk_provider(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001460 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1461
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001462 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001463 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1464 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001465 fimc_md_pipelines_free(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001466 media_device_unregister(&fmd->media_dev);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -02001467 media_device_cleanup(&fmd->media_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001468 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001469
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001470 return 0;
1471}
1472
Krzysztof Kozlowskic42639d2015-05-01 12:51:01 -03001473static const struct platform_device_id fimc_driver_ids[] __always_unused = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001474 { .name = "s5p-fimc-md" },
1475 { },
1476};
1477MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1478
1479static const struct of_device_id fimc_md_of_match[] = {
1480 { .compatible = "samsung,fimc" },
1481 { },
1482};
1483MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1484
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001485static struct platform_driver fimc_md_driver = {
1486 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001487 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001488 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001489 .of_match_table = of_match_ptr(fimc_md_of_match),
1490 .name = "s5p-fimc-md",
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001491 }
1492};
1493
Sachin Kamat7e566be2012-05-26 11:11:54 -03001494static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001495{
1496 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001497
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001498 request_module("s5p-csis");
1499 ret = fimc_register_driver();
1500 if (ret)
1501 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001502
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001503 return platform_driver_register(&fimc_md_driver);
1504}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001505
1506static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001507{
1508 platform_driver_unregister(&fimc_md_driver);
1509 fimc_unregister_driver();
1510}
1511
1512module_init(fimc_md_init);
1513module_exit(fimc_md_exit);
1514
1515MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1516MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1517MODULE_LICENSE("GPL");
1518MODULE_VERSION("2.0.1");