blob: 4f5586a4cbffa05d8f2079696806622337fead70 [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>
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -030034#include <media/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
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030091 if (pad == NULL ||
92 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
93 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030094 sd = media_entity_to_v4l2_subdev(pad->entity);
95
96 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030097 case GRP_ID_SENSOR:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -030098 sensor = sd;
99 /* fall through */
100 case GRP_ID_FIMC_IS_SENSOR:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300101 p->subdevs[IDX_SENSOR] = sd;
102 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300103 case GRP_ID_CSIS:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300104 p->subdevs[IDX_CSIS] = sd;
105 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300106 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300107 p->subdevs[IDX_FLITE] = sd;
108 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300109 case GRP_ID_FIMC:
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300110 p->subdevs[IDX_FIMC] = sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300111 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300112 case GRP_ID_FIMC_IS:
113 p->subdevs[IDX_IS_ISP] = sd;
114 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300115 default:
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300116 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300117 }
Sylwester Nawrocki39bb6df62013-03-08 08:58:34 -0300118 me = &sd->entity;
119 if (me->num_pads == 1)
120 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300121 }
Sylwester Nawrocki52917bc2013-05-31 11:37:24 -0300122
123 if (sensor && p->subdevs[IDX_FIMC])
124 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300125}
126
127/**
128 * __subdev_set_power - change power state of a single subdev
129 * @sd: subdevice to change power state for
130 * @on: 1 to enable power or 0 to disable
131 *
132 * Return result of s_power subdev operation or -ENXIO if sd argument
133 * is NULL. Return 0 if the subdevice does not implement s_power.
134 */
135static int __subdev_set_power(struct v4l2_subdev *sd, int on)
136{
137 int *use_count;
138 int ret;
139
140 if (sd == NULL)
141 return -ENXIO;
142
143 use_count = &sd->entity.use_count;
144 if (on && (*use_count)++ > 0)
145 return 0;
146 else if (!on && (*use_count == 0 || --(*use_count) > 0))
147 return 0;
148 ret = v4l2_subdev_call(sd, core, s_power, on);
149
150 return ret != -ENOIOCTLCMD ? ret : 0;
151}
152
153/**
154 * fimc_pipeline_s_power - change power state of all pipeline subdevs
155 * @fimc: fimc device terminating the pipeline
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300156 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300157 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300158 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300159 */
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300160static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300161{
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300162 static const u8 seq[2][IDX_MAX - 1] = {
163 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
164 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
165 };
166 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300167
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300168 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300169 return -ENXIO;
170
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300171 for (i = 0; i < IDX_MAX - 1; i++) {
172 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300173
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300174 ret = __subdev_set_power(p->subdevs[idx], on);
175
176
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300177 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300178 goto error;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300179 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300180 return 0;
Sylwester Nawrockif8bca4f2012-11-20 08:54:22 -0300181error:
182 for (; i >= 0; i--) {
183 unsigned int idx = seq[on][i];
184 __subdev_set_power(p->subdevs[idx], !on);
185 }
186 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300187}
188
189/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300190 * __fimc_pipeline_open - update the pipeline information, enable power
191 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300192 * @me: media entity to start graph walk with
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300193 * @prepare: true to walk the current pipeline and acquire all subdevs
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300194 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300195 * Called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300196 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300197static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300198 struct media_entity *me, bool prepare)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300199{
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300200 struct fimc_md *fmd = entity_to_fimc_mdev(me);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300201 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300202 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300203 int ret;
204
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300205 if (WARN_ON(p == NULL || me == NULL))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300206 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300207
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300208 if (prepare)
209 fimc_pipeline_prepare(p, me);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300210
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300211 sd = p->subdevs[IDX_SENSOR];
212 if (sd == NULL)
213 return -EINVAL;
214
215 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
216 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
217 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
218 if (ret < 0)
219 return ret;
220 }
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300221
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300222 ret = fimc_pipeline_s_power(p, 1);
223 if (!ret)
224 return 0;
225
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300226 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
227 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
228
229 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300230}
231
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300232/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300233 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300234 * @fimc: fimc device terminating the pipeline
235 *
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300236 * Disable power of all subdevs and turn the external sensor clock off.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300237 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300238static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300239{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300240 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300241 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
242 struct fimc_md *fmd;
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300243 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300244
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300245 if (sd == NULL) {
246 pr_warn("%s(): No sensor subdev\n", __func__);
247 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300248 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300249
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300250 ret = fimc_pipeline_s_power(p, 0);
Sylwester Nawrocki36da6fc2013-05-31 11:37:23 -0300251
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300252 fmd = entity_to_fimc_mdev(&sd->entity);
253
254 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
255 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
256 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
257
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300258 return ret == -ENXIO ? 0 : ret;
259}
260
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300261/**
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300262 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300263 * @pipeline: video pipeline structure
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300264 * @on: passed as the s_stream() callback argument
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300265 */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300266static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300267{
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300268 static const u8 seq[2][IDX_MAX] = {
269 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
270 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
271 };
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300272 struct fimc_pipeline *p = to_fimc_pipeline(ep);
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300273 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300274
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300275 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300276 return -ENODEV;
277
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300278 for (i = 0; i < IDX_MAX; i++) {
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300279 unsigned int idx = seq[on][i];
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300280
281 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
282
283 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300284 goto error;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300285 }
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300286 return 0;
Sylwester Nawrocki8d274e72012-11-13 14:35:22 -0300287error:
288 for (; i >= 0; i--) {
289 unsigned int idx = seq[on][i];
290 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
291 }
292 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300293}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300294
295/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300296static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300297 .open = __fimc_pipeline_open,
298 .close = __fimc_pipeline_close,
299 .set_stream = __fimc_pipeline_s_stream,
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300300};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300301
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300302static struct exynos_media_pipeline *fimc_md_pipeline_create(
303 struct fimc_md *fmd)
304{
305 struct fimc_pipeline *p;
306
307 p = kzalloc(sizeof(*p), GFP_KERNEL);
308 if (!p)
309 return NULL;
310
311 list_add_tail(&p->list, &fmd->pipelines);
312
313 p->ep.ops = &fimc_pipeline_ops;
314 return &p->ep;
315}
316
317static void fimc_md_pipelines_free(struct fimc_md *fmd)
318{
319 while (!list_empty(&fmd->pipelines)) {
320 struct fimc_pipeline *p;
321
322 p = list_entry(fmd->pipelines.next, typeof(*p), list);
323 list_del(&p->list);
324 kfree(p);
325 }
326}
327
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300328/* Parse port node and register as a sub-device any sensor specified there. */
329static int fimc_md_parse_port_node(struct fimc_md *fmd,
330 struct device_node *port,
331 unsigned int index)
332{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300333 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300334 struct device_node *rem, *ep, *np;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300335 struct v4l2_of_endpoint endpoint;
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
342 v4l2_of_parse_endpoint(ep, &endpoint);
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100343 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300344 return -EINVAL;
345
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100346 pd->mux_id = (endpoint.base.port - 1) & 0x1;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300347
Philipp Zabelfd9fdb72014-02-10 22:01:48 +0100348 rem = of_graph_get_remote_port_parent(ep);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300349 of_node_put(ep);
350 if (rem == NULL) {
351 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
352 ep->full_name);
353 return 0;
354 }
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300355
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100356 if (fimc_input_is_parallel(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300357 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
358 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
359 else
360 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
361 pd->flags = endpoint.bus.parallel.flags;
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100362 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300363 /*
364 * MIPI CSI-2: only input mux selection and
365 * the sensor's clock frequency is needed.
366 */
367 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
368 } else {
369 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
Philipp Zabelf2a575f2014-02-14 11:53:56 +0100370 endpoint.base.port, rem->full_name);
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300371 }
372 /*
373 * For FIMC-IS handled sensors, that are placed under i2c-isp device
374 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
375 * input. Sensors are attached to the FIMC-LITE hostdata interface
376 * directly or through MIPI-CSIS, depending on the external media bus
377 * used. This needs to be handled in a more reliable way, not by just
378 * checking parent's node name.
379 */
380 np = of_get_parent(rem);
381
382 if (np && !of_node_cmp(np->name, "i2c-isp"))
383 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
384 else
385 pd->fimc_bus_type = pd->sensor_bus_type;
386
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300387 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
388 return -EINVAL;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300389
Sylwester Nawrockifa91f102014-02-24 11:44:50 -0300390 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
391 fmd->sensor[index].asd.match.of.node = rem;
392 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
393
394 fmd->num_sensors++;
395
396 of_node_put(rem);
397 return 0;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300398}
399
400/* Register all SoC external sub-devices */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300401static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300402{
403 struct device_node *parent = fmd->pdev->dev.of_node;
404 struct device_node *node, *ports;
405 int index = 0;
406 int ret;
407
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300408 /*
409 * Runtime resume one of the FIMC entities to make sure
410 * the sclk_cam clocks are not globally disabled.
411 */
412 if (!fmd->pmf)
413 return -ENXIO;
414
415 ret = pm_runtime_get_sync(fmd->pmf);
416 if (ret < 0)
417 return ret;
418
419 fmd->num_sensors = 0;
420
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300421 /* Attach sensors linked to MIPI CSI-2 receivers */
422 for_each_available_child_of_node(parent, node) {
423 struct device_node *port;
424
425 if (of_node_cmp(node->name, "csis"))
426 continue;
427 /* The csis node can have only port subnode. */
428 port = of_get_next_child(node, NULL);
429 if (!port)
430 continue;
431
432 ret = fimc_md_parse_port_node(fmd, port, index);
433 if (ret < 0)
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300434 goto rpm_put;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300435 index++;
436 }
437
438 /* Attach sensors listed in the parallel-ports node */
439 ports = of_get_child_by_name(parent, "parallel-ports");
440 if (!ports)
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300441 goto rpm_put;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300442
443 for_each_child_of_node(ports, node) {
444 ret = fimc_md_parse_port_node(fmd, node, index);
445 if (ret < 0)
446 break;
447 index++;
448 }
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300449rpm_put:
450 pm_runtime_put(fmd->pmf);
451 return ret;
Sylwester Nawrocki2b13f7d2013-03-29 14:12:39 -0300452}
453
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300454static int __of_get_csis_id(struct device_node *np)
455{
456 u32 reg = 0;
457
458 np = of_get_child_by_name(np, "port");
459 if (!np)
460 return -EINVAL;
461 of_property_read_u32(np, "reg", &reg);
462 return reg - FIMC_INPUT_MIPI_CSI2_0;
463}
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300464
465/*
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300466 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300467 */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300468static int register_fimc_lite_entity(struct fimc_md *fmd,
469 struct fimc_lite *fimc_lite)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300470{
Sachin Kamat8163ec02012-10-25 10:56:00 -0300471 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300472 struct exynos_media_pipeline *ep;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300473 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300474
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300475 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
476 fmd->fimc_lite[fimc_lite->index]))
477 return -EBUSY;
478
479 sd = &fimc_lite->subdev;
480 sd->grp_id = GRP_ID_FLITE;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300481
482 ep = fimc_md_pipeline_create(fmd);
483 if (!ep)
484 return -ENOMEM;
485
486 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300487
488 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
489 if (!ret)
490 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
491 else
492 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
493 fimc_lite->index);
494 return ret;
495}
496
497static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
498{
499 struct v4l2_subdev *sd;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300500 struct exynos_media_pipeline *ep;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300501 int ret;
502
503 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
504 return -EBUSY;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300505
Sachin Kamat8163ec02012-10-25 10:56:00 -0300506 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300507 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300508
509 ep = fimc_md_pipeline_create(fmd);
510 if (!ep)
511 return -ENOMEM;
512
513 v4l2_set_subdev_hostdata(sd, ep);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300514
515 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300516 if (!ret) {
Sylwester Nawrocki3e20c342013-03-21 14:47:17 -0300517 if (!fmd->pmf && fimc->pdev)
518 fmd->pmf = &fimc->pdev->dev;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300519 fmd->fimc[fimc->id] = fimc;
520 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
521 } else {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300522 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
523 fimc->id, ret);
524 }
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300525 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300526}
527
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300528static int register_csis_entity(struct fimc_md *fmd,
529 struct platform_device *pdev,
530 struct v4l2_subdev *sd)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300531{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300532 struct device_node *node = pdev->dev.of_node;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300533 int id, ret;
534
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300535 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300536
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300537 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
538 return -ENOENT;
539
540 if (WARN_ON(fmd->csis[id].sd))
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300541 return -EBUSY;
542
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300543 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300544 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300545 if (!ret)
546 fmd->csis[id].sd = sd;
547 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300548 v4l2_err(&fmd->v4l2_dev,
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300549 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300550 return ret;
551}
552
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300553static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
554{
555 struct v4l2_subdev *sd = &is->isp.subdev;
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300556 struct exynos_media_pipeline *ep;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300557 int ret;
558
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300559 /* Allocate pipeline object for the ISP capture video node. */
560 ep = fimc_md_pipeline_create(fmd);
561 if (!ep)
562 return -ENOMEM;
563
564 v4l2_set_subdev_hostdata(sd, ep);
565
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300566 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
567 if (ret) {
568 v4l2_err(&fmd->v4l2_dev,
569 "Failed to register FIMC-ISP (%d)\n", ret);
570 return ret;
571 }
572
573 fmd->fimc_is = is;
574 return 0;
575}
576
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300577static int fimc_md_register_platform_entity(struct fimc_md *fmd,
578 struct platform_device *pdev,
579 int plat_entity)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300580{
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300581 struct device *dev = &pdev->dev;
582 int ret = -EPROBE_DEFER;
583 void *drvdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300584
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300585 /* Lock to ensure dev->driver won't change. */
586 device_lock(dev);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300587
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300588 if (!dev->driver || !try_module_get(dev->driver->owner))
589 goto dev_unlock;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300590
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300591 drvdata = dev_get_drvdata(dev);
Jonathan McCrohanf58c91c2013-10-20 21:34:01 -0300592 /* Some subdev didn't probe successfully id drvdata is NULL */
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300593 if (drvdata) {
594 switch (plat_entity) {
595 case IDX_FIMC:
596 ret = register_fimc_entity(fmd, drvdata);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300597 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300598 case IDX_FLITE:
599 ret = register_fimc_lite_entity(fmd, drvdata);
600 break;
601 case IDX_CSIS:
602 ret = register_csis_entity(fmd, pdev, drvdata);
603 break;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300604 case IDX_IS_ISP:
605 ret = register_fimc_is_entity(fmd, drvdata);
606 break;
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300607 default:
608 ret = -ENODEV;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300609 }
610 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300611
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300612 module_put(dev->driver->owner);
613dev_unlock:
614 device_unlock(dev);
615 if (ret == -EPROBE_DEFER)
616 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
617 dev_name(dev));
618 else if (ret < 0)
619 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
620 dev_name(dev), ret);
621 return ret;
622}
623
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300624/* Register FIMC, FIMC-LITE and CSIS media entities */
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300625static int fimc_md_register_platform_entities(struct fimc_md *fmd,
626 struct device_node *parent)
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300627{
628 struct device_node *node;
629 int ret = 0;
630
631 for_each_available_child_of_node(parent, node) {
632 struct platform_device *pdev;
633 int plat_entity = -1;
634
635 pdev = of_find_device_by_node(node);
636 if (!pdev)
637 continue;
638
639 /* If driver of any entity isn't ready try all again later. */
640 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
641 plat_entity = IDX_CSIS;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -0300642 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
643 plat_entity = IDX_IS_ISP;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300644 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
645 plat_entity = IDX_FLITE;
646 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
647 !of_property_read_bool(node, "samsung,lcd-wb"))
648 plat_entity = IDX_FIMC;
649
650 if (plat_entity >= 0)
651 ret = fimc_md_register_platform_entity(fmd, pdev,
652 plat_entity);
653 put_device(&pdev->dev);
654 if (ret < 0)
655 break;
656 }
657
658 return ret;
659}
Sylwester Nawrockie2985a22013-03-08 12:59:33 -0300660
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300661static void fimc_md_unregister_entities(struct fimc_md *fmd)
662{
663 int i;
664
665 for (i = 0; i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300666 struct fimc_dev *dev = fmd->fimc[i];
667 if (dev == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300668 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300669 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
670 dev->vid_cap.ve.pipe = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300671 fmd->fimc[i] = NULL;
672 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300673 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300674 struct fimc_lite *dev = fmd->fimc_lite[i];
675 if (dev == NULL)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300676 continue;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300677 v4l2_device_unregister_subdev(&dev->subdev);
678 dev->ve.pipe = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300679 fmd->fimc_lite[i] = NULL;
680 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300681 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
682 if (fmd->csis[i].sd == NULL)
683 continue;
684 v4l2_device_unregister_subdev(fmd->csis[i].sd);
685 fmd->csis[i].sd = NULL;
686 }
Sylwester Nawrockie41a35c2013-04-18 09:05:54 -0300687
688 if (fmd->fimc_is)
689 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
690
Sylwester Nawrocki7b43a6f2012-12-12 08:16:05 -0300691 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300692}
693
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300694/**
695 * __fimc_md_create_fimc_links - create links to all FIMC entities
696 * @fmd: fimc media device
697 * @source: the source entity to create links to all fimc entities from
698 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
699 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300700 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300701 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300702static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
703 struct media_entity *source,
704 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300705 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300706{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300707 struct fimc_source_info *si = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300708 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300709 unsigned int flags = 0;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300710 int i, ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300711
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300712 if (sensor) {
713 si = v4l2_get_subdev_hostdata(sensor);
714 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300715 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300716 ret = 1;
717 }
718
719 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300720 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300721 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300722 /*
723 * Some FIMC variants are not fitted with camera capture
724 * interface. Skip creating a link from sensor for those.
725 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300726 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300727 continue;
728
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300729 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300730
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300731 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300732 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300733 FIMC_SD_PAD_SINK_CAM, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300734 if (ret)
735 return ret;
736
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300737 /* Notify FIMC capture subdev entity */
738 ret = media_entity_call(sink, link_setup, &sink->pads[0],
739 &source->pads[pad], flags);
740 if (ret)
741 break;
742
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300743 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300744 source->name, flags ? '=' : '-', sink->name);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300745 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300746
747 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
748 if (!fmd->fimc_lite[i])
749 continue;
750
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300751 sink = &fmd->fimc_lite[i]->subdev.entity;
752 ret = media_entity_create_link(source, pad, sink,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300753 FLITE_SD_PAD_SINK, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300754 if (ret)
755 return ret;
756
757 /* Notify FIMC-LITE subdev entity */
758 ret = media_entity_call(sink, link_setup, &sink->pads[0],
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300759 &source->pads[pad], 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300760 if (ret)
761 break;
762
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300763 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
764 source->name, sink->name);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300765 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300766 return 0;
767}
768
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300769/* Create links from FIMC-LITE source pads to other entities */
770static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
771{
772 struct media_entity *source, *sink;
Sylwester Nawrockia26860b2012-12-05 13:43:05 -0300773 int i, ret = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300774
775 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
776 struct fimc_lite *fimc = fmd->fimc_lite[i];
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300777
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300778 if (fimc == NULL)
779 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300780
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300781 source = &fimc->subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300782 sink = &fimc->ve.vdev.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300783 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300784 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300785 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300786 if (ret)
787 break;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300788 /* Link from FIMC-LITE to IS-ISP subdev */
789 sink = &fmd->fimc_is->isp.subdev.entity;
790 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
791 sink, 0, 0);
792 if (ret)
793 break;
794 }
795
796 return ret;
797}
798
799/* Create FIMC-IS links */
800static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
801{
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300802 struct fimc_isp *isp = &fmd->fimc_is->isp;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300803 struct media_entity *source, *sink;
804 int i, ret;
805
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300806 source = &isp->subdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300807
808 for (i = 0; i < FIMC_MAX_DEVS; i++) {
809 if (fmd->fimc[i] == NULL)
810 continue;
811
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300812 /* Link from FIMC-IS-ISP subdev to FIMC */
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300813 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
814 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
815 sink, FIMC_SD_PAD_SINK_FIFO, 0);
816 if (ret)
817 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300818 }
819
Sylwester Nawrocki34947b82013-12-20 19:35:06 -0300820 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
821 sink = &isp->video_capture.ve.vdev.entity;
822
823 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
824 if (sink->num_pads == 0)
825 return 0;
826
827 return media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
828 sink, 0, 0);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300829}
830
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300831/**
832 * fimc_md_create_links - create default links between registered entities
833 *
834 * Parallel interface sensor entities are connected directly to FIMC capture
835 * entities. The sensors using MIPI CSIS bus are connected through immutable
836 * link with CSI receiver entity specified by mux_id. Any registered CSIS
837 * entity has a link to each registered FIMC capture entity. Enabled links
838 * are created by default between each subsequent registered sensor and
839 * subsequent FIMC capture entity. The number of default active links is
840 * determined by the number of available sensors or FIMC entities,
841 * whichever is less.
842 */
843static int fimc_md_create_links(struct fimc_md *fmd)
844{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300845 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300846 struct v4l2_subdev *sensor, *csis;
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300847 struct fimc_source_info *pdata;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300848 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300849 int i, pad, fimc_id = 0, ret = 0;
850 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300851
852 for (i = 0; i < fmd->num_sensors; i++) {
853 if (fmd->sensor[i].subdev == NULL)
854 continue;
855
856 sensor = fmd->sensor[i].subdev;
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -0300857 pdata = v4l2_get_subdev_hostdata(sensor);
858 if (!pdata)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300859 continue;
860
861 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300862
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300863 switch (pdata->sensor_bus_type) {
864 case FIMC_BUS_TYPE_MIPI_CSI2:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300865 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
866 "Wrong CSI channel id: %d\n", pdata->mux_id))
867 return -EINVAL;
868
869 csis = fmd->csis[pdata->mux_id].sd;
870 if (WARN(csis == NULL,
871 "MIPI-CSI interface specified "
872 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300873 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300874
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300875 pad = sensor->entity.num_pads - 1;
876 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300877 &csis->entity, CSIS_PAD_SINK,
878 MEDIA_LNK_FL_IMMUTABLE |
879 MEDIA_LNK_FL_ENABLED);
880 if (ret)
881 return ret;
882
Sylwester Nawrocki969e8772012-12-07 16:40:08 -0300883 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300884 sensor->entity.name, csis->entity.name);
885
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300886 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300887 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300888 break;
889
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300890 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300891 source = &sensor->entity;
892 pad = 0;
893 break;
894
895 default:
896 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
Sylwester Nawrocki56bc9112013-02-01 15:00:40 -0300897 pdata->sensor_bus_type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300898 return -EINVAL;
899 }
900 if (source == NULL)
901 continue;
902
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300903 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300904 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300905 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300906 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300907
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300908 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300909 if (fmd->csis[i].sd == NULL)
910 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300911
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300912 source = &fmd->csis[i].sd->entity;
913 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300914 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300915
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300916 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300917 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300918 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300919 }
920
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300921 /* Create immutable links between each FIMC's subdev and video node */
922 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
923 for (i = 0; i < FIMC_MAX_DEVS; i++) {
924 if (!fmd->fimc[i])
925 continue;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300926
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300927 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300928 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300929
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300930 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
931 sink, 0, flags);
932 if (ret)
933 break;
934 }
935
Sylwester Nawrockif998bb72012-11-27 13:29:48 -0300936 ret = __fimc_md_create_flite_source_links(fmd);
937 if (ret < 0)
938 return ret;
939
940 if (fmd->use_isp)
941 ret = __fimc_md_create_fimc_is_links(fmd);
942
943 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300944}
945
946/*
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300947 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300948 */
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300949static void fimc_md_put_clocks(struct fimc_md *fmd)
950{
951 int i = FIMC_MAX_CAMCLKS;
952
953 while (--i >= 0) {
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300954 if (IS_ERR(fmd->camclk[i].clock))
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300955 continue;
956 clk_put(fmd->camclk[i].clock);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300957 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300958 }
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300959
960 /* Writeback (PIXELASYNCMx) clocks */
961 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
962 if (IS_ERR(fmd->wbclk[i]))
963 continue;
964 clk_put(fmd->wbclk[i]);
965 fmd->wbclk[i] = ERR_PTR(-EINVAL);
966 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300967}
968
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300969static int fimc_md_get_clocks(struct fimc_md *fmd)
970{
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300971 struct device *dev = &fmd->pdev->dev;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300972 char clk_name[32];
973 struct clk *clock;
Sylwester Nawrocki044c3722013-07-19 10:05:17 -0300974 int i, ret = 0;
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300975
976 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
977 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
978
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300979 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
980 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
981 clock = clk_get(dev, clk_name);
982
983 if (IS_ERR(clock)) {
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -0300984 dev_err(dev, "Failed to get clock: %s\n", clk_name);
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300985 ret = PTR_ERR(clock);
986 break;
987 }
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -0300988 fmd->camclk[i].clock = clock;
989 }
990 if (ret)
991 fimc_md_put_clocks(fmd);
992
Sylwester Nawrocki056f4f32013-03-21 14:43:17 -0300993 if (!fmd->use_isp)
994 return 0;
995 /*
996 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
997 * leave PIXELASYNCM0 out for the LCD Writeback driver.
998 */
999 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1000
1001 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1002 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1003 clock = clk_get(dev, clk_name);
1004 if (IS_ERR(clock)) {
1005 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1006 clk_name);
1007 ret = PTR_ERR(clock);
1008 break;
1009 }
1010 fmd->wbclk[i] = clock;
1011 }
1012 if (ret)
1013 fimc_md_put_clocks(fmd);
1014
Sylwester Nawrocki0e23cbb2013-01-18 15:34:37 -03001015 return ret;
1016}
1017
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001018static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001019{
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001020 struct exynos_video_entity *ve;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001021 struct fimc_pipeline *p;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001022 struct video_device *vdev;
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001023 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001024
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001025 vdev = media_entity_to_video_device(entity);
1026 if (vdev->entity.use_count == 0)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001027 return 0;
1028
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001029 ve = vdev_to_exynos_video_entity(vdev);
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001030 p = to_fimc_pipeline(ve->pipe);
1031 /*
1032 * Nothing to do if we are disabling the pipeline, some link
1033 * has been disconnected and p->subdevs array is cleared now.
1034 */
1035 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1036 return 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001037
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001038 if (enable)
1039 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1040 else
1041 ret = __fimc_pipeline_close(ve->pipe);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001042
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001043 if (ret == 0 && !enable)
1044 memset(p->subdevs, 0, sizeof(p->subdevs));
1045
1046 return ret;
1047}
1048
1049/* Locking: called with entity->parent->graph_mutex mutex held. */
1050static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1051{
1052 struct media_entity *entity_err = entity;
1053 struct media_entity_graph graph;
1054 int ret;
1055
1056 /*
1057 * Walk current graph and call the pipeline open/close routine for each
1058 * opened video node that belongs to the graph of entities connected
1059 * through active links. This is needed as we cannot power on/off the
1060 * subdevs in random order.
1061 */
1062 media_entity_graph_walk_start(&graph, entity);
1063
1064 while ((entity = media_entity_graph_walk_next(&graph))) {
1065 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1066 continue;
1067
1068 ret = __fimc_md_modify_pipeline(entity, enable);
1069
1070 if (ret < 0)
1071 goto err;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001072 }
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001073
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001074 return 0;
1075 err:
1076 media_entity_graph_walk_start(&graph, entity_err);
1077
1078 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1079 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1080 continue;
1081
1082 __fimc_md_modify_pipeline(entity_err, !enable);
1083
1084 if (entity_err == entity)
1085 break;
1086 }
1087
1088 return ret;
1089}
1090
1091static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1092 unsigned int notification)
1093{
1094 struct media_entity *sink = link->sink->entity;
1095 int ret = 0;
1096
1097 /* Before link disconnection */
1098 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1099 if (!(flags & MEDIA_LNK_FL_ENABLED))
1100 ret = __fimc_md_modify_pipelines(sink, false);
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001101#if 0
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001102 else
Mauro Carvalho Chehabcdf58a62014-08-26 11:19:18 -03001103 /* TODO: Link state change validation */
1104#endif
Sylwester Nawrockid3775fa2013-05-31 10:37:27 -03001105 /* After link activation */
1106 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1107 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1108 ret = __fimc_md_modify_pipelines(sink, true);
1109 }
1110
1111 return ret ? -EPIPE : 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001112}
1113
1114static ssize_t fimc_md_sysfs_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct platform_device *pdev = to_platform_device(dev);
1118 struct fimc_md *fmd = platform_get_drvdata(pdev);
1119
1120 if (fmd->user_subdev_api)
1121 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1122
1123 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1124}
1125
1126static ssize_t fimc_md_sysfs_store(struct device *dev,
1127 struct device_attribute *attr,
1128 const char *buf, size_t count)
1129{
1130 struct platform_device *pdev = to_platform_device(dev);
1131 struct fimc_md *fmd = platform_get_drvdata(pdev);
1132 bool subdev_api;
1133 int i;
1134
1135 if (!strcmp(buf, "vid-dev\n"))
1136 subdev_api = false;
1137 else if (!strcmp(buf, "sub-dev\n"))
1138 subdev_api = true;
1139 else
1140 return count;
1141
1142 fmd->user_subdev_api = subdev_api;
1143 for (i = 0; i < FIMC_MAX_DEVS; i++)
1144 if (fmd->fimc[i])
1145 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1146 return count;
1147}
1148/*
1149 * This device attribute is to select video pipeline configuration method.
1150 * There are following valid values:
1151 * vid-dev - for V4L2 video node API only, subdevice will be configured
1152 * by the host driver.
1153 * sub-dev - for media controller API, subdevs must be configured in user
1154 * space before starting streaming.
1155 */
1156static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1157 fimc_md_sysfs_show, fimc_md_sysfs_store);
1158
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001159static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1160{
1161 struct device *dev = &fmd->pdev->dev;
1162 struct fimc_pinctrl *pctl = &fmd->pinctl;
1163
1164 pctl->pinctrl = devm_pinctrl_get(dev);
1165 if (IS_ERR(pctl->pinctrl))
1166 return PTR_ERR(pctl->pinctrl);
1167
1168 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1169 PINCTRL_STATE_DEFAULT);
1170 if (IS_ERR(pctl->state_default))
1171 return PTR_ERR(pctl->state_default);
1172
1173 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1174 PINCTRL_STATE_IDLE);
1175 return 0;
1176}
1177
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001178static int cam_clk_prepare(struct clk_hw *hw)
1179{
1180 struct cam_clk *camclk = to_cam_clk(hw);
1181 int ret;
1182
1183 if (camclk->fmd->pmf == NULL)
1184 return -ENODEV;
1185
1186 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1187 return ret < 0 ? ret : 0;
1188}
1189
1190static void cam_clk_unprepare(struct clk_hw *hw)
1191{
1192 struct cam_clk *camclk = to_cam_clk(hw);
1193
1194 if (camclk->fmd->pmf == NULL)
1195 return;
1196
1197 pm_runtime_put_sync(camclk->fmd->pmf);
1198}
1199
1200static const struct clk_ops cam_clk_ops = {
1201 .prepare = cam_clk_prepare,
1202 .unprepare = cam_clk_unprepare,
1203};
1204
1205static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1206{
1207 struct cam_clk_provider *cp = &fmd->clk_provider;
1208 unsigned int i;
1209
1210 if (cp->of_node)
1211 of_clk_del_provider(cp->of_node);
1212
1213 for (i = 0; i < cp->num_clocks; i++)
1214 clk_unregister(cp->clks[i]);
1215}
1216
1217static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1218{
1219 struct cam_clk_provider *cp = &fmd->clk_provider;
1220 struct device *dev = &fmd->pdev->dev;
1221 int i, ret;
1222
1223 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1224 struct cam_clk *camclk = &cp->camclk[i];
1225 struct clk_init_data init;
1226 const char *p_name;
1227
1228 ret = of_property_read_string_index(dev->of_node,
1229 "clock-output-names", i, &init.name);
1230 if (ret < 0)
1231 break;
1232
1233 p_name = __clk_get_name(fmd->camclk[i].clock);
1234
1235 /* It's safe since clk_register() will duplicate the string. */
1236 init.parent_names = &p_name;
1237 init.num_parents = 1;
1238 init.ops = &cam_clk_ops;
1239 init.flags = CLK_SET_RATE_PARENT;
1240 camclk->hw.init = &init;
1241 camclk->fmd = fmd;
1242
1243 cp->clks[i] = clk_register(NULL, &camclk->hw);
1244 if (IS_ERR(cp->clks[i])) {
1245 dev_err(dev, "failed to register clock: %s (%ld)\n",
1246 init.name, PTR_ERR(cp->clks[i]));
1247 ret = PTR_ERR(cp->clks[i]);
1248 goto err;
1249 }
1250 cp->num_clocks++;
1251 }
1252
1253 if (cp->num_clocks == 0) {
1254 dev_warn(dev, "clk provider not registered\n");
1255 return 0;
1256 }
1257
1258 cp->clk_data.clks = cp->clks;
1259 cp->clk_data.clk_num = cp->num_clocks;
1260 cp->of_node = dev->of_node;
1261 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1262 &cp->clk_data);
1263 if (ret == 0)
1264 return 0;
1265err:
1266 fimc_md_unregister_clk_provider(fmd);
1267 return ret;
1268}
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001269
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001270static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1271 struct v4l2_subdev *subdev,
1272 struct v4l2_async_subdev *asd)
1273{
1274 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1275 struct fimc_sensor_info *si = NULL;
1276 int i;
1277
1278 /* Find platform data for this sensor subdev */
1279 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1280 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1281 si = &fmd->sensor[i];
1282
1283 if (si == NULL)
1284 return -EINVAL;
1285
1286 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1287
1288 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1289 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1290 else
1291 subdev->grp_id = GRP_ID_SENSOR;
1292
1293 si->subdev = subdev;
1294
1295 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1296 subdev->name, fmd->num_sensors);
1297
1298 fmd->num_sensors++;
1299
1300 return 0;
1301}
1302
1303static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1304{
1305 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1306 int ret;
1307
1308 mutex_lock(&fmd->media_dev.graph_mutex);
1309
1310 ret = fimc_md_create_links(fmd);
1311 if (ret < 0)
1312 goto unlock;
1313
1314 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1315unlock:
1316 mutex_unlock(&fmd->media_dev.graph_mutex);
1317 return ret;
1318}
1319
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001320static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001321{
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001322 struct device *dev = &pdev->dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001323 struct v4l2_device *v4l2_dev;
1324 struct fimc_md *fmd;
1325 int ret;
1326
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001327 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001328 if (!fmd)
1329 return -ENOMEM;
1330
1331 spin_lock_init(&fmd->slock);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001332 INIT_LIST_HEAD(&fmd->pipelines);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001333 fmd->pdev = pdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001334
1335 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1336 sizeof(fmd->media_dev.model));
1337 fmd->media_dev.link_notify = fimc_md_link_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001338 fmd->media_dev.dev = dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001339
1340 v4l2_dev = &fmd->v4l2_dev;
1341 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001342 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001343 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001344
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001345 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001346 fmd->user_subdev_api = true;
Sylwester Nawrockie781bbe32013-03-21 14:49:16 -03001347
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001348 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001349 if (ret < 0) {
1350 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -03001351 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001352 }
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001353
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001354 ret = media_device_register(&fmd->media_dev);
1355 if (ret < 0) {
1356 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001357 goto err_v4l2_dev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001358 }
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001359
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001360 ret = fimc_md_get_clocks(fmd);
1361 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001362 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001363
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001364 ret = fimc_md_get_pinctrl(fmd);
1365 if (ret < 0) {
1366 if (ret != EPROBE_DEFER)
1367 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001368 goto err_clk;
Sylwester Nawrocki41638512013-03-08 10:41:47 -03001369 }
1370
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001371 platform_set_drvdata(pdev, fmd);
1372
1373 /* Protect the media graph while we're registering entities */
1374 mutex_lock(&fmd->media_dev.graph_mutex);
1375
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001376 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001377 if (ret) {
1378 mutex_unlock(&fmd->media_dev.graph_mutex);
1379 goto err_clk;
1380 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001381
Sylwester Nawrocki49b2f4c52014-04-15 08:35:25 -03001382 ret = fimc_md_register_sensor_entities(fmd);
1383 if (ret) {
1384 mutex_unlock(&fmd->media_dev.graph_mutex);
1385 goto err_m_ent;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -03001386 }
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001387
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001388 mutex_unlock(&fmd->media_dev.graph_mutex);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001389
1390 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001391 if (ret)
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001392 goto err_m_ent;
1393 /*
1394 * FIMC platform devices need to be registered before the sclk_cam
1395 * clocks provider, as one of these devices needs to be activated
1396 * to enable the clock.
1397 */
1398 ret = fimc_md_register_clk_provider(fmd);
1399 if (ret < 0) {
1400 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1401 goto err_attr;
1402 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001403
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001404 if (fmd->num_sensors > 0) {
1405 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1406 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1407 fmd->subdev_notifier.bound = subdev_notifier_bound;
1408 fmd->subdev_notifier.complete = subdev_notifier_complete;
1409 fmd->num_sensors = 0;
1410
1411 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1412 &fmd->subdev_notifier);
1413 if (ret)
1414 goto err_clk_p;
1415 }
1416
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001417 return 0;
1418
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001419err_clk_p:
1420 fimc_md_unregister_clk_provider(fmd);
1421err_attr:
1422 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001423err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001424 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001425err_m_ent:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001426 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001427err_md:
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001428 media_device_unregister(&fmd->media_dev);
1429err_v4l2_dev:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001430 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001431 return ret;
1432}
1433
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001434static int fimc_md_remove(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001435{
1436 struct fimc_md *fmd = platform_get_drvdata(pdev);
1437
1438 if (!fmd)
1439 return 0;
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001440
Sylwester Nawrockid3f5e0c2013-12-20 18:53:53 -03001441 fimc_md_unregister_clk_provider(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001442 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1443
Sylwester Nawrockib74bee12013-08-09 14:23:53 -03001444 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001445 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1446 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001447 fimc_md_pipelines_free(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001448 media_device_unregister(&fmd->media_dev);
1449 fimc_md_put_clocks(fmd);
Sylwester Nawrockifa91f102014-02-24 11:44:50 -03001450
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001451 return 0;
1452}
1453
Krzysztof Kozlowskic42639d2015-05-01 12:51:01 -03001454static const struct platform_device_id fimc_driver_ids[] __always_unused = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001455 { .name = "s5p-fimc-md" },
1456 { },
1457};
1458MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1459
1460static const struct of_device_id fimc_md_of_match[] = {
1461 { .compatible = "samsung,fimc" },
1462 { },
1463};
1464MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1465
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001466static struct platform_driver fimc_md_driver = {
1467 .probe = fimc_md_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001468 .remove = fimc_md_remove,
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001469 .driver = {
Sylwester Nawrockie2985a22013-03-08 12:59:33 -03001470 .of_match_table = of_match_ptr(fimc_md_of_match),
1471 .name = "s5p-fimc-md",
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001472 }
1473};
1474
Sachin Kamat7e566be2012-05-26 11:11:54 -03001475static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001476{
1477 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001478
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001479 request_module("s5p-csis");
1480 ret = fimc_register_driver();
1481 if (ret)
1482 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001483
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001484 return platform_driver_register(&fimc_md_driver);
1485}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001486
1487static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001488{
1489 platform_driver_unregister(&fimc_md_driver);
1490 fimc_unregister_driver();
1491}
1492
1493module_init(fimc_md_init);
1494module_exit(fimc_md_exit);
1495
1496MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1497MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1498MODULE_LICENSE("GPL");
1499MODULE_VERSION("2.0.1");