blob: e77dba7a97b84e66d15c07ec749fdaa44d7f0e26 [file] [log] [blame]
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001/*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
3 *
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
11 */
12
13#include <linux/bug.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/i2c.h>
17#include <linux/kernel.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/pm_runtime.h>
22#include <linux/types.h>
23#include <linux/slab.h>
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -030024#include <media/v4l2-ctrls.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030025#include <media/media-device.h>
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030026#include <media/s5p_fimc.h>
Sylwester Nawrockid3953222011-09-01 06:01:08 -030027
28#include "fimc-core.h"
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030029#include "fimc-lite.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030030#include "fimc-mdevice.h"
31#include "mipi-csis.h"
32
33static int __fimc_md_set_camclk(struct fimc_md *fmd,
34 struct fimc_sensor_info *s_info,
35 bool on);
36/**
37 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
38 * @fimc: fimc device terminating the pipeline
39 *
40 * Caller holds the graph mutex.
41 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -030042static void fimc_pipeline_prepare(struct fimc_pipeline *p,
43 struct media_entity *me)
Sylwester Nawrockid3953222011-09-01 06:01:08 -030044{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030045 struct media_pad *pad = &me->pads[0];
Sylwester Nawrockid3953222011-09-01 06:01:08 -030046 struct v4l2_subdev *sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030047 int i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030048
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030049 for (i = 0; i < IDX_MAX; i++)
50 p->subdevs[i] = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030051
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030052 while (1) {
53 if (!(pad->flags & MEDIA_PAD_FL_SINK))
54 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030055
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030056 /* source pad */
57 pad = media_entity_remote_source(pad);
58 if (pad == NULL ||
59 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
60 break;
61
62 sd = media_entity_to_v4l2_subdev(pad->entity);
63
64 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030065 case GRP_ID_FIMC_IS_SENSOR:
66 case GRP_ID_SENSOR:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030067 p->subdevs[IDX_SENSOR] = sd;
68 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030069 case GRP_ID_CSIS:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030070 p->subdevs[IDX_CSIS] = sd;
71 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030072 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -030073 p->subdevs[IDX_FLITE] = sd;
74 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -030075 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030076 /* No need to control FIMC subdev through subdev ops */
77 break;
78 default:
79 pr_warn("%s: Unknown subdev grp_id: %#x\n",
80 __func__, sd->grp_id);
81 }
82 /* sink pad */
83 pad = &sd->entity.pads[0];
Sylwester Nawrockid3953222011-09-01 06:01:08 -030084 }
85}
86
87/**
88 * __subdev_set_power - change power state of a single subdev
89 * @sd: subdevice to change power state for
90 * @on: 1 to enable power or 0 to disable
91 *
92 * Return result of s_power subdev operation or -ENXIO if sd argument
93 * is NULL. Return 0 if the subdevice does not implement s_power.
94 */
95static int __subdev_set_power(struct v4l2_subdev *sd, int on)
96{
97 int *use_count;
98 int ret;
99
100 if (sd == NULL)
101 return -ENXIO;
102
103 use_count = &sd->entity.use_count;
104 if (on && (*use_count)++ > 0)
105 return 0;
106 else if (!on && (*use_count == 0 || --(*use_count) > 0))
107 return 0;
108 ret = v4l2_subdev_call(sd, core, s_power, on);
109
110 return ret != -ENOIOCTLCMD ? ret : 0;
111}
112
113/**
114 * fimc_pipeline_s_power - change power state of all pipeline subdevs
115 * @fimc: fimc device terminating the pipeline
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300116 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300117 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300118 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300119 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300120static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300121{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300122 unsigned int i;
123 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300124
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300125 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300126 return -ENXIO;
127
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300128 for (i = 0; i < IDX_MAX; i++) {
129 unsigned int idx = state ? (IDX_MAX - 1) - i : i;
130
131 ret = __subdev_set_power(p->subdevs[idx], state);
132 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300133 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300134 }
135
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300136 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300137}
138
139/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300140 * __fimc_pipeline_open - update the pipeline information, enable power
141 * of all pipeline subdevs and the sensor clock
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300142 * @me: media entity to start graph walk with
143 * @prep: true to acquire sensor (and csis) subdevs
144 *
145 * This function must be called with the graph mutex held.
146 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300147static int __fimc_pipeline_open(struct fimc_pipeline *p,
148 struct media_entity *me, bool prep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300149{
150 int ret;
151
152 if (prep)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300153 fimc_pipeline_prepare(p, me);
154
155 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300156 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300157
158 ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300159 if (ret)
160 return ret;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300161
162 return fimc_pipeline_s_power(p, 1);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300163}
164
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300165static int fimc_pipeline_open(struct fimc_pipeline *p,
166 struct media_entity *me, bool prep)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300167{
168 int ret;
169
170 mutex_lock(&me->parent->graph_mutex);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300171 ret = __fimc_pipeline_open(p, me, prep);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300172 mutex_unlock(&me->parent->graph_mutex);
173
174 return ret;
175}
176
177/**
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300178 * __fimc_pipeline_close - disable the sensor clock and pipeline power
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300179 * @fimc: fimc device terminating the pipeline
180 *
181 * Disable power of all subdevs in the pipeline and turn off the external
182 * sensor clock.
183 * Called with the graph mutex held.
184 */
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300185static int __fimc_pipeline_close(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300186{
187 int ret = 0;
188
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300189 if (p->subdevs[IDX_SENSOR]) {
190 ret = fimc_pipeline_s_power(p, 0);
191 fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300192 }
193 return ret == -ENXIO ? 0 : ret;
194}
195
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300196static int fimc_pipeline_close(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300197{
Sylwester Nawrocki90e614bb2012-05-16 11:35:04 -0300198 struct media_entity *me;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300199 int ret;
200
Sylwester Nawrocki90e614bb2012-05-16 11:35:04 -0300201 if (!p || !p->subdevs[IDX_SENSOR])
202 return -EINVAL;
203
204 me = &p->subdevs[IDX_SENSOR]->entity;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300205 mutex_lock(&me->parent->graph_mutex);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300206 ret = __fimc_pipeline_close(p);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300207 mutex_unlock(&me->parent->graph_mutex);
208
209 return ret;
210}
211
212/**
213 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300214 * @pipeline: video pipeline structure
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300215 * @on: passed as the s_stream call argument
216 */
Sachin Kamat57fcfb62012-10-17 07:11:51 -0300217static int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300218{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300219 int i, ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300220
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300221 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300222 return -ENODEV;
223
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300224 for (i = 0; i < IDX_MAX; i++) {
225 unsigned int idx = on ? (IDX_MAX - 1) - i : i;
226
227 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
228
229 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
230 return ret;
231 }
232
233 return 0;
234
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300235}
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300236
237/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
238static const struct fimc_pipeline_ops fimc_pipeline_ops = {
239 .open = fimc_pipeline_open,
240 .close = fimc_pipeline_close,
241 .set_stream = fimc_pipeline_s_stream,
242};
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300243
244/*
245 * Sensor subdevice helper functions
246 */
247static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
248 struct fimc_sensor_info *s_info)
249{
250 struct i2c_adapter *adapter;
251 struct v4l2_subdev *sd = NULL;
252
253 if (!s_info || !fmd)
254 return NULL;
255
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300256 adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300257 if (!adapter) {
258 v4l2_warn(&fmd->v4l2_dev,
259 "Failed to get I2C adapter %d, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300260 s_info->pdata.i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300261 return ERR_PTR(-EPROBE_DEFER);
262 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300263 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300264 s_info->pdata.board_info, NULL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300265 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300266 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300267 v4l2_warn(&fmd->v4l2_dev,
268 "Failed to acquire subdev %s, deferring probe\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300269 s_info->pdata.board_info->type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300270 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300271 }
272 v4l2_set_subdev_hostdata(sd, s_info);
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300273 sd->grp_id = GRP_ID_SENSOR;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300274
275 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300276 s_info->pdata.board_info->type);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300277 return sd;
278}
279
280static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
281{
282 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300283 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300284
285 if (!client)
286 return;
287 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300288 adapter = client->adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300289 i2c_unregister_device(client);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300290 if (adapter)
291 i2c_put_adapter(adapter);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300292}
293
294static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
295{
296 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
297 struct fimc_dev *fd = NULL;
298 int num_clients, ret, i;
299
300 /*
301 * Runtime resume one of the FIMC entities to make sure
302 * the sclk_cam clocks are not globally disabled.
303 */
304 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
305 if (fmd->fimc[i])
306 fd = fmd->fimc[i];
307 if (!fd)
308 return -ENXIO;
309 ret = pm_runtime_get_sync(&fd->pdev->dev);
310 if (ret < 0)
311 return ret;
312
313 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
314 num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
315
316 fmd->num_sensors = num_clients;
317 for (i = 0; i < num_clients; i++) {
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300318 struct v4l2_subdev *sd;
319
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300320 fmd->sensor[i].pdata = pdata->isp_info[i];
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300321 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
322 if (ret)
323 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300324 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300325 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300326
327 if (!IS_ERR(sd)) {
328 fmd->sensor[i].subdev = sd;
329 } else {
330 fmd->sensor[i].subdev = NULL;
331 ret = PTR_ERR(sd);
332 break;
333 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300334 if (ret)
335 break;
336 }
337 pm_runtime_put(&fd->pdev->dev);
338 return ret;
339}
340
341/*
342 * MIPI CSIS and FIMC platform devices registration.
343 */
344static int fimc_register_callback(struct device *dev, void *p)
345{
346 struct fimc_dev *fimc = dev_get_drvdata(dev);
Sachin Kamat8163ec02012-10-25 10:56:00 -0300347 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300348 struct fimc_md *fmd = p;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300349 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300350
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300351 if (fimc == NULL || fimc->id >= FIMC_MAX_DEVS)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300352 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300353
Sachin Kamat8163ec02012-10-25 10:56:00 -0300354 sd = &fimc->vid_cap.subdev;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300355 sd->grp_id = GRP_ID_FIMC;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -0300356 v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300357
358 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
359 if (ret) {
360 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
361 fimc->id, ret);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300362 return ret;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300363 }
364
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300365 fmd->fimc[fimc->id] = fimc;
366 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300367}
368
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300369static int fimc_lite_register_callback(struct device *dev, void *p)
370{
371 struct fimc_lite *fimc = dev_get_drvdata(dev);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300372 struct fimc_md *fmd = p;
373 int ret;
374
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300375 if (fimc == NULL || fimc->index >= FIMC_LITE_MAX_DEVS)
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300376 return 0;
377
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300378 fimc->subdev.grp_id = GRP_ID_FLITE;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -0300379 v4l2_set_subdev_hostdata(&fimc->subdev, (void *)&fimc_pipeline_ops);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300380
Sachin Kamat8163ec02012-10-25 10:56:00 -0300381 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, &fimc->subdev);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300382 if (ret) {
383 v4l2_err(&fmd->v4l2_dev,
384 "Failed to register FIMC-LITE.%d (%d)\n",
385 fimc->index, ret);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300386 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300387 }
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300388
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300389 fmd->fimc_lite[fimc->index] = fimc;
390 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300391}
392
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300393static int csis_register_callback(struct device *dev, void *p)
394{
395 struct v4l2_subdev *sd = dev_get_drvdata(dev);
396 struct platform_device *pdev;
397 struct fimc_md *fmd = p;
398 int id, ret;
399
400 if (!sd)
401 return 0;
402 pdev = v4l2_get_subdevdata(sd);
403 if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
404 return 0;
405 v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);
406
407 id = pdev->id < 0 ? 0 : pdev->id;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300408 sd->grp_id = GRP_ID_CSIS;
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300409
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300410 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
Sylwester Nawrockiafd73482012-10-25 10:48:44 -0300411 if (!ret)
412 fmd->csis[id].sd = sd;
413 else
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300414 v4l2_err(&fmd->v4l2_dev,
415 "Failed to register CSIS subdevice: %d\n", ret);
416 return ret;
417}
418
419/**
420 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
421 */
422static int fimc_md_register_platform_entities(struct fimc_md *fmd)
423{
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300424 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300425 struct device_driver *driver;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300426 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300427
428 driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300429 if (!driver) {
430 v4l2_warn(&fmd->v4l2_dev,
431 "%s driver not found, deffering probe\n",
432 FIMC_MODULE_NAME);
433 return -EPROBE_DEFER;
434 }
435
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300436 ret = driver_for_each_device(driver, NULL, fmd,
437 fimc_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300438 if (ret)
439 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300440
441 driver = driver_find(FIMC_LITE_DRV_NAME, &platform_bus_type);
442 if (driver && try_module_get(driver->owner)) {
443 ret = driver_for_each_device(driver, NULL, fmd,
444 fimc_lite_register_callback);
445 if (ret)
446 return ret;
447 module_put(driver->owner);
448 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300449 /*
450 * Check if there is any sensor on the MIPI-CSI2 bus and
451 * if not skip the s5p-csis module loading.
452 */
Sylwester Nawrocki41df5bf2012-04-30 12:10:58 -0300453 if (pdata == NULL)
454 return 0;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300455 for (i = 0; i < pdata->num_clients; i++) {
456 if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) {
457 ret = 1;
458 break;
459 }
460 }
461 if (!ret)
462 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300463
464 driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300465 if (!driver || !try_module_get(driver->owner)) {
466 v4l2_warn(&fmd->v4l2_dev,
467 "%s driver not found, deffering probe\n",
468 CSIS_DRIVER_NAME);
469 return -EPROBE_DEFER;
470 }
471
472 return driver_for_each_device(driver, NULL, fmd,
473 csis_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300474}
475
476static void fimc_md_unregister_entities(struct fimc_md *fmd)
477{
478 int i;
479
480 for (i = 0; i < FIMC_MAX_DEVS; i++) {
481 if (fmd->fimc[i] == NULL)
482 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300483 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300484 fmd->fimc[i]->pipeline_ops = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300485 fmd->fimc[i] = NULL;
486 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300487 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
488 if (fmd->fimc_lite[i] == NULL)
489 continue;
490 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300491 fmd->fimc[i]->pipeline_ops = NULL;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300492 fmd->fimc_lite[i] = NULL;
493 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300494 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
495 if (fmd->csis[i].sd == NULL)
496 continue;
497 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300498 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300499 fmd->csis[i].sd = NULL;
500 }
501 for (i = 0; i < fmd->num_sensors; i++) {
502 if (fmd->sensor[i].subdev == NULL)
503 continue;
504 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
505 fmd->sensor[i].subdev = NULL;
506 }
507}
508
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300509/**
510 * __fimc_md_create_fimc_links - create links to all FIMC entities
511 * @fmd: fimc media device
512 * @source: the source entity to create links to all fimc entities from
513 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
514 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300515 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300516 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300517static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
518 struct media_entity *source,
519 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300520 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300521{
522 struct fimc_sensor_info *s_info;
523 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300524 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300525 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300526
527 for (i = 0; i < FIMC_MAX_DEVS; i++) {
528 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300529 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300530 /*
531 * Some FIMC variants are not fitted with camera capture
532 * interface. Skip creating a link from sensor for those.
533 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300534 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300535 continue;
536
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300537 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300538
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300539 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300540 ret = media_entity_create_link(source, pad, sink,
541 FIMC_SD_PAD_SINK, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300542 if (ret)
543 return ret;
544
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300545 /* Notify FIMC capture subdev entity */
546 ret = media_entity_call(sink, link_setup, &sink->pads[0],
547 &source->pads[pad], flags);
548 if (ret)
549 break;
550
Sylwester Nawrocki542fb082012-10-25 05:48:19 -0300551 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300552 source->name, flags ? '=' : '-', sink->name);
553
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300554 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300555 continue;
556 s_info = v4l2_get_subdev_hostdata(sensor);
557 if (!WARN_ON(s_info == NULL)) {
558 unsigned long irq_flags;
559 spin_lock_irqsave(&fmd->slock, irq_flags);
560 s_info->host = fmd->fimc[i];
561 spin_unlock_irqrestore(&fmd->slock, irq_flags);
562 }
563 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300564
565 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
566 if (!fmd->fimc_lite[i])
567 continue;
568
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300569 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
570 flags = MEDIA_LNK_FL_ENABLED;
571 else
572 flags = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300573
574 sink = &fmd->fimc_lite[i]->subdev.entity;
575 ret = media_entity_create_link(source, pad, sink,
576 FLITE_SD_PAD_SINK, flags);
577 if (ret)
578 return ret;
579
580 /* Notify FIMC-LITE subdev entity */
581 ret = media_entity_call(sink, link_setup, &sink->pads[0],
582 &source->pads[pad], flags);
583 if (ret)
584 break;
585
586 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
587 source->name, flags ? '=' : '-', sink->name);
588 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300589 return 0;
590}
591
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300592/* Create links from FIMC-LITE source pads to other entities */
593static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
594{
595 struct media_entity *source, *sink;
596 unsigned int flags = MEDIA_LNK_FL_ENABLED;
597 int i, ret;
598
599 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
600 struct fimc_lite *fimc = fmd->fimc_lite[i];
601 if (fimc == NULL)
602 continue;
603 source = &fimc->subdev.entity;
Sylwester Nawrocki1bcd7042012-07-26 07:13:08 -0300604 sink = &fimc->vfd.entity;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300605 /* FIMC-LITE's subdev and video node */
Sylwester Nawrocki6319d6a2012-11-28 15:41:01 -0300606 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300607 sink, 0, flags);
608 if (ret)
609 break;
610 /* TODO: create links to other entities */
611 }
612
613 return ret;
614}
615
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300616/**
617 * fimc_md_create_links - create default links between registered entities
618 *
619 * Parallel interface sensor entities are connected directly to FIMC capture
620 * entities. The sensors using MIPI CSIS bus are connected through immutable
621 * link with CSI receiver entity specified by mux_id. Any registered CSIS
622 * entity has a link to each registered FIMC capture entity. Enabled links
623 * are created by default between each subsequent registered sensor and
624 * subsequent FIMC capture entity. The number of default active links is
625 * determined by the number of available sensors or FIMC entities,
626 * whichever is less.
627 */
628static int fimc_md_create_links(struct fimc_md *fmd)
629{
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300630 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300631 struct v4l2_subdev *sensor, *csis;
632 struct s5p_fimc_isp_info *pdata;
633 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300634 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300635 int i, pad, fimc_id = 0, ret = 0;
636 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300637
638 for (i = 0; i < fmd->num_sensors; i++) {
639 if (fmd->sensor[i].subdev == NULL)
640 continue;
641
642 sensor = fmd->sensor[i].subdev;
643 s_info = v4l2_get_subdev_hostdata(sensor);
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300644 if (!s_info)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300645 continue;
646
647 source = NULL;
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300648 pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300649
650 switch (pdata->bus_type) {
651 case FIMC_MIPI_CSI2:
652 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
653 "Wrong CSI channel id: %d\n", pdata->mux_id))
654 return -EINVAL;
655
656 csis = fmd->csis[pdata->mux_id].sd;
657 if (WARN(csis == NULL,
658 "MIPI-CSI interface specified "
659 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300660 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300661
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300662 pad = sensor->entity.num_pads - 1;
663 ret = media_entity_create_link(&sensor->entity, pad,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300664 &csis->entity, CSIS_PAD_SINK,
665 MEDIA_LNK_FL_IMMUTABLE |
666 MEDIA_LNK_FL_ENABLED);
667 if (ret)
668 return ret;
669
670 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
671 sensor->entity.name, csis->entity.name);
672
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300673 source = NULL;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300674 csi_sensors[pdata->mux_id] = sensor;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300675 break;
676
677 case FIMC_ITU_601...FIMC_ITU_656:
678 source = &sensor->entity;
679 pad = 0;
680 break;
681
682 default:
683 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
684 pdata->bus_type);
685 return -EINVAL;
686 }
687 if (source == NULL)
688 continue;
689
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300690 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300691 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300692 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300693 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300694
Sylwester Nawrockia8697ec2012-12-05 13:40:04 -0300695 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300696 if (fmd->csis[i].sd == NULL)
697 continue;
698 source = &fmd->csis[i].sd->entity;
699 pad = CSIS_PAD_SOURCE;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300700 sensor = csi_sensors[i];
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300701
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300702 link_mask = 1 << fimc_id++;
Sylwester Nawrocki5d33ee92012-08-16 14:06:41 -0300703 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300704 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300705 }
706
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300707 /* Create immutable links between each FIMC's subdev and video node */
708 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
709 for (i = 0; i < FIMC_MAX_DEVS; i++) {
710 if (!fmd->fimc[i])
711 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300712 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -0300713 sink = &fmd->fimc[i]->vid_cap.vfd.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300714 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
715 sink, 0, flags);
716 if (ret)
717 break;
718 }
719
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300720 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300721}
722
723/*
724 * The peripheral sensor clock management.
725 */
726static int fimc_md_get_clocks(struct fimc_md *fmd)
727{
728 char clk_name[32];
729 struct clk *clock;
730 int i;
731
732 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
733 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
734 clock = clk_get(NULL, clk_name);
735 if (IS_ERR_OR_NULL(clock)) {
736 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
737 clk_name);
738 return -ENXIO;
739 }
740 fmd->camclk[i].clock = clock;
741 }
742 return 0;
743}
744
745static void fimc_md_put_clocks(struct fimc_md *fmd)
746{
747 int i = FIMC_MAX_CAMCLKS;
748
749 while (--i >= 0) {
750 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
751 continue;
752 clk_put(fmd->camclk[i].clock);
753 fmd->camclk[i].clock = NULL;
754 }
755}
756
757static int __fimc_md_set_camclk(struct fimc_md *fmd,
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300758 struct fimc_sensor_info *s_info,
759 bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300760{
Sylwester Nawrocki6612a082012-09-17 06:00:48 -0300761 struct s5p_fimc_isp_info *pdata = &s_info->pdata;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300762 struct fimc_camclk_info *camclk;
763 int ret = 0;
764
765 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
766 return -EINVAL;
767
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300768 camclk = &fmd->camclk[pdata->clk_id];
769
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300770 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
771 pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300772
773 if (on) {
774 if (camclk->use_count > 0 &&
775 camclk->frequency != pdata->clk_frequency)
776 return -EINVAL;
777
778 if (camclk->use_count++ == 0) {
779 clk_set_rate(camclk->clock, pdata->clk_frequency);
780 camclk->frequency = pdata->clk_frequency;
781 ret = clk_enable(camclk->clock);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300782 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
783 clk_get_rate(camclk->clock));
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300784 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300785 return ret;
786 }
787
788 if (WARN_ON(camclk->use_count == 0))
789 return 0;
790
791 if (--camclk->use_count == 0) {
792 clk_disable(camclk->clock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300793 dbg("Disabled camclk %d", pdata->clk_id);
794 }
795 return ret;
796}
797
798/**
799 * fimc_md_set_camclk - peripheral sensor clock setup
800 * @sd: sensor subdev to configure sclk_cam clock for
801 * @on: 1 to enable or 0 to disable the clock
802 *
803 * There are 2 separate clock outputs available in the SoC for external
804 * image processors. These clocks are shared between all registered FIMC
805 * devices to which sensors can be attached, either directly or through
806 * the MIPI CSI receiver. The clock is allowed here to be used by
807 * multiple sensors concurrently if they use same frequency.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300808 * This function should only be called when the graph mutex is held.
809 */
810int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
811{
812 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
813 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
814
815 return __fimc_md_set_camclk(fmd, s_info, on);
816}
817
818static int fimc_md_link_notify(struct media_pad *source,
819 struct media_pad *sink, u32 flags)
820{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300821 struct fimc_lite *fimc_lite = NULL;
822 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300823 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300824 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300825 int ret = 0;
826
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300827 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300828 return 0;
829
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300830 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300831
832 switch (sd->grp_id) {
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300833 case GRP_ID_FLITE:
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300834 fimc_lite = v4l2_get_subdevdata(sd);
835 pipeline = &fimc_lite->pipeline;
836 break;
Sylwester Nawrocki588c87b2012-11-27 11:57:42 -0300837 case GRP_ID_FIMC:
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300838 fimc = v4l2_get_subdevdata(sd);
839 pipeline = &fimc->pipeline;
840 break;
841 default:
842 return 0;
843 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300844
845 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300846 ret = __fimc_pipeline_close(pipeline);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300847 pipeline->subdevs[IDX_SENSOR] = NULL;
848 pipeline->subdevs[IDX_CSIS] = NULL;
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300849
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300850 if (fimc) {
851 mutex_lock(&fimc->lock);
852 fimc_ctrls_delete(fimc->vid_cap.ctx);
853 mutex_unlock(&fimc->lock);
854 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300855 return ret;
856 }
857 /*
858 * Link activation. Enable power of pipeline elements only if the
859 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300860 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300861 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300862 if (fimc) {
863 mutex_lock(&fimc->lock);
864 if (fimc->vid_cap.refcnt > 0) {
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300865 ret = __fimc_pipeline_open(pipeline,
866 source->entity, true);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300867 if (!ret)
868 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300869 }
870 mutex_unlock(&fimc->lock);
871 } else {
872 mutex_lock(&fimc_lite->lock);
873 if (fimc_lite->ref_count > 0) {
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300874 ret = __fimc_pipeline_open(pipeline,
875 source->entity, true);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300876 }
877 mutex_unlock(&fimc_lite->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300878 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300879 return ret ? -EPIPE : ret;
880}
881
882static ssize_t fimc_md_sysfs_show(struct device *dev,
883 struct device_attribute *attr, char *buf)
884{
885 struct platform_device *pdev = to_platform_device(dev);
886 struct fimc_md *fmd = platform_get_drvdata(pdev);
887
888 if (fmd->user_subdev_api)
889 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
890
891 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
892}
893
894static ssize_t fimc_md_sysfs_store(struct device *dev,
895 struct device_attribute *attr,
896 const char *buf, size_t count)
897{
898 struct platform_device *pdev = to_platform_device(dev);
899 struct fimc_md *fmd = platform_get_drvdata(pdev);
900 bool subdev_api;
901 int i;
902
903 if (!strcmp(buf, "vid-dev\n"))
904 subdev_api = false;
905 else if (!strcmp(buf, "sub-dev\n"))
906 subdev_api = true;
907 else
908 return count;
909
910 fmd->user_subdev_api = subdev_api;
911 for (i = 0; i < FIMC_MAX_DEVS; i++)
912 if (fmd->fimc[i])
913 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
914 return count;
915}
916/*
917 * This device attribute is to select video pipeline configuration method.
918 * There are following valid values:
919 * vid-dev - for V4L2 video node API only, subdevice will be configured
920 * by the host driver.
921 * sub-dev - for media controller API, subdevs must be configured in user
922 * space before starting streaming.
923 */
924static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
925 fimc_md_sysfs_show, fimc_md_sysfs_store);
926
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300927static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300928{
929 struct v4l2_device *v4l2_dev;
930 struct fimc_md *fmd;
931 int ret;
932
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300933 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300934 if (!fmd)
935 return -ENOMEM;
936
937 spin_lock_init(&fmd->slock);
938 fmd->pdev = pdev;
939
940 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
941 sizeof(fmd->media_dev.model));
942 fmd->media_dev.link_notify = fimc_md_link_notify;
943 fmd->media_dev.dev = &pdev->dev;
944
945 v4l2_dev = &fmd->v4l2_dev;
946 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -0300947 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300948 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
949 dev_name(&pdev->dev));
950
951 ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
952 if (ret < 0) {
953 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300954 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300955 }
956 ret = media_device_register(&fmd->media_dev);
957 if (ret < 0) {
958 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300959 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300960 }
961 ret = fimc_md_get_clocks(fmd);
962 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300963 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300964
965 fmd->user_subdev_api = false;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300966
967 /* Protect the media graph while we're registering entities */
968 mutex_lock(&fmd->media_dev.graph_mutex);
969
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300970 ret = fimc_md_register_platform_entities(fmd);
971 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300972 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300973
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300974 if (pdev->dev.platform_data) {
975 ret = fimc_md_register_sensor_entities(fmd);
976 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300977 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300978 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300979 ret = fimc_md_create_links(fmd);
980 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300981 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300982 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
983 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300984 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300985
986 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300987 if (ret)
988 goto err_unlock;
989
990 platform_set_drvdata(pdev, fmd);
991 mutex_unlock(&fmd->media_dev.graph_mutex);
992 return 0;
993
994err_unlock:
995 mutex_unlock(&fmd->media_dev.graph_mutex);
996err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300997 media_device_unregister(&fmd->media_dev);
998 fimc_md_put_clocks(fmd);
999 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001000err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001001 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001002 return ret;
1003}
1004
1005static int __devexit fimc_md_remove(struct platform_device *pdev)
1006{
1007 struct fimc_md *fmd = platform_get_drvdata(pdev);
1008
1009 if (!fmd)
1010 return 0;
1011 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1012 fimc_md_unregister_entities(fmd);
1013 media_device_unregister(&fmd->media_dev);
1014 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001015 return 0;
1016}
1017
1018static struct platform_driver fimc_md_driver = {
1019 .probe = fimc_md_probe,
1020 .remove = __devexit_p(fimc_md_remove),
1021 .driver = {
1022 .name = "s5p-fimc-md",
1023 .owner = THIS_MODULE,
1024 }
1025};
1026
Sachin Kamat7e566be2012-05-26 11:11:54 -03001027static int __init fimc_md_init(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001028{
1029 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001030
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001031 request_module("s5p-csis");
1032 ret = fimc_register_driver();
1033 if (ret)
1034 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001035
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001036 return platform_driver_register(&fimc_md_driver);
1037}
Sachin Kamat7e566be2012-05-26 11:11:54 -03001038
1039static void __exit fimc_md_exit(void)
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001040{
1041 platform_driver_unregister(&fimc_md_driver);
1042 fimc_unregister_driver();
1043}
1044
1045module_init(fimc_md_init);
1046module_exit(fimc_md_exit);
1047
1048MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1049MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1050MODULE_LICENSE("GPL");
1051MODULE_VERSION("2.0.1");