blob: dffe4da5eaa06db1b25cdf3f4c8b47aa0e9bd7a3 [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>
26
27#include "fimc-core.h"
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030028#include "fimc-lite.h"
Sylwester Nawrockid3953222011-09-01 06:01:08 -030029#include "fimc-mdevice.h"
30#include "mipi-csis.h"
31
32static int __fimc_md_set_camclk(struct fimc_md *fmd,
33 struct fimc_sensor_info *s_info,
34 bool on);
35/**
36 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
37 * @fimc: fimc device terminating the pipeline
38 *
39 * Caller holds the graph mutex.
40 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030041void fimc_pipeline_prepare(struct fimc_pipeline *p, struct media_entity *me)
Sylwester Nawrockid3953222011-09-01 06:01:08 -030042{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030043 struct media_pad *pad = &me->pads[0];
Sylwester Nawrockid3953222011-09-01 06:01:08 -030044 struct v4l2_subdev *sd;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030045 int i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030046
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030047 for (i = 0; i < IDX_MAX; i++)
48 p->subdevs[i] = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030049
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030050 while (1) {
51 if (!(pad->flags & MEDIA_PAD_FL_SINK))
52 break;
Sylwester Nawrockid3953222011-09-01 06:01:08 -030053
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030054 /* source pad */
55 pad = media_entity_remote_source(pad);
56 if (pad == NULL ||
57 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
58 break;
59
60 sd = media_entity_to_v4l2_subdev(pad->entity);
61
62 switch (sd->grp_id) {
63 case SENSOR_GROUP_ID:
64 p->subdevs[IDX_SENSOR] = sd;
65 break;
66 case CSIS_GROUP_ID:
67 p->subdevs[IDX_CSIS] = sd;
68 break;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -030069 case FLITE_GROUP_ID:
70 p->subdevs[IDX_FLITE] = sd;
71 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030072 case FIMC_GROUP_ID:
73 /* No need to control FIMC subdev through subdev ops */
74 break;
75 default:
76 pr_warn("%s: Unknown subdev grp_id: %#x\n",
77 __func__, sd->grp_id);
78 }
79 /* sink pad */
80 pad = &sd->entity.pads[0];
Sylwester Nawrockid3953222011-09-01 06:01:08 -030081 }
82}
83
84/**
85 * __subdev_set_power - change power state of a single subdev
86 * @sd: subdevice to change power state for
87 * @on: 1 to enable power or 0 to disable
88 *
89 * Return result of s_power subdev operation or -ENXIO if sd argument
90 * is NULL. Return 0 if the subdevice does not implement s_power.
91 */
92static int __subdev_set_power(struct v4l2_subdev *sd, int on)
93{
94 int *use_count;
95 int ret;
96
97 if (sd == NULL)
98 return -ENXIO;
99
100 use_count = &sd->entity.use_count;
101 if (on && (*use_count)++ > 0)
102 return 0;
103 else if (!on && (*use_count == 0 || --(*use_count) > 0))
104 return 0;
105 ret = v4l2_subdev_call(sd, core, s_power, on);
106
107 return ret != -ENOIOCTLCMD ? ret : 0;
108}
109
110/**
111 * fimc_pipeline_s_power - change power state of all pipeline subdevs
112 * @fimc: fimc device terminating the pipeline
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300113 * @state: true to power on, false to power off
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300114 *
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300115 * Needs to be called with the graph mutex held.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300116 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300117int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300118{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300119 unsigned int i;
120 int ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300121
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300122 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300123 return -ENXIO;
124
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300125 for (i = 0; i < IDX_MAX; i++) {
126 unsigned int idx = state ? (IDX_MAX - 1) - i : i;
127
128 ret = __subdev_set_power(p->subdevs[idx], state);
129 if (ret < 0 && ret != -ENXIO)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300130 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300131 }
132
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300133 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300134}
135
136/**
137 * __fimc_pipeline_initialize - update the pipeline information, enable power
138 * of all pipeline subdevs and the sensor clock
139 * @me: media entity to start graph walk with
140 * @prep: true to acquire sensor (and csis) subdevs
141 *
142 * This function must be called with the graph mutex held.
143 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300144static int __fimc_pipeline_initialize(struct fimc_pipeline *p,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300145 struct media_entity *me, bool prep)
146{
147 int ret;
148
149 if (prep)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300150 fimc_pipeline_prepare(p, me);
151
152 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300153 return -EINVAL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300154
155 ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300156 if (ret)
157 return ret;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300158
159 return fimc_pipeline_s_power(p, 1);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300160}
161
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300162int fimc_pipeline_initialize(struct fimc_pipeline *p, struct media_entity *me,
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300163 bool prep)
164{
165 int ret;
166
167 mutex_lock(&me->parent->graph_mutex);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300168 ret = __fimc_pipeline_initialize(p, me, prep);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300169 mutex_unlock(&me->parent->graph_mutex);
170
171 return ret;
172}
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300173EXPORT_SYMBOL_GPL(fimc_pipeline_initialize);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300174
175/**
176 * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power
177 * @fimc: fimc device terminating the pipeline
178 *
179 * Disable power of all subdevs in the pipeline and turn off the external
180 * sensor clock.
181 * Called with the graph mutex held.
182 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300183int __fimc_pipeline_shutdown(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300184{
185 int ret = 0;
186
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300187 if (p->subdevs[IDX_SENSOR]) {
188 ret = fimc_pipeline_s_power(p, 0);
189 fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300190 }
191 return ret == -ENXIO ? 0 : ret;
192}
193
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300194int fimc_pipeline_shutdown(struct fimc_pipeline *p)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300195{
Sylwester Nawrocki90e614bb2012-05-16 11:35:04 -0300196 struct media_entity *me;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300197 int ret;
198
Sylwester Nawrocki90e614bb2012-05-16 11:35:04 -0300199 if (!p || !p->subdevs[IDX_SENSOR])
200 return -EINVAL;
201
202 me = &p->subdevs[IDX_SENSOR]->entity;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300203 mutex_lock(&me->parent->graph_mutex);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300204 ret = __fimc_pipeline_shutdown(p);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300205 mutex_unlock(&me->parent->graph_mutex);
206
207 return ret;
208}
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300209EXPORT_SYMBOL_GPL(fimc_pipeline_shutdown);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300210
211/**
212 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300213 * @pipeline: video pipeline structure
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300214 * @on: passed as the s_stream call argument
215 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300216int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300217{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300218 int i, ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300219
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300220 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300221 return -ENODEV;
222
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300223 for (i = 0; i < IDX_MAX; i++) {
224 unsigned int idx = on ? (IDX_MAX - 1) - i : i;
225
226 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
227
228 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
229 return ret;
230 }
231
232 return 0;
233
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300234}
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300235EXPORT_SYMBOL_GPL(fimc_pipeline_s_stream);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300236
237/*
238 * Sensor subdevice helper functions
239 */
240static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
241 struct fimc_sensor_info *s_info)
242{
243 struct i2c_adapter *adapter;
244 struct v4l2_subdev *sd = NULL;
245
246 if (!s_info || !fmd)
247 return NULL;
248
249 adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300250 if (!adapter) {
251 v4l2_warn(&fmd->v4l2_dev,
252 "Failed to get I2C adapter %d, deferring probe\n",
253 s_info->pdata->i2c_bus_num);
254 return ERR_PTR(-EPROBE_DEFER);
255 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300256 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
257 s_info->pdata->board_info, NULL);
258 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300259 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300260 v4l2_warn(&fmd->v4l2_dev,
261 "Failed to acquire subdev %s, deferring probe\n",
262 s_info->pdata->board_info->type);
263 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300264 }
265 v4l2_set_subdev_hostdata(sd, s_info);
266 sd->grp_id = SENSOR_GROUP_ID;
267
268 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
269 s_info->pdata->board_info->type);
270 return sd;
271}
272
273static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
274{
275 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300276 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300277
278 if (!client)
279 return;
280 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300281 adapter = client->adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300282 i2c_unregister_device(client);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300283 if (adapter)
284 i2c_put_adapter(adapter);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300285}
286
287static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
288{
289 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
290 struct fimc_dev *fd = NULL;
291 int num_clients, ret, i;
292
293 /*
294 * Runtime resume one of the FIMC entities to make sure
295 * the sclk_cam clocks are not globally disabled.
296 */
297 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
298 if (fmd->fimc[i])
299 fd = fmd->fimc[i];
300 if (!fd)
301 return -ENXIO;
302 ret = pm_runtime_get_sync(&fd->pdev->dev);
303 if (ret < 0)
304 return ret;
305
306 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
307 num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
308
309 fmd->num_sensors = num_clients;
310 for (i = 0; i < num_clients; i++) {
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300311 struct v4l2_subdev *sd;
312
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300313 fmd->sensor[i].pdata = &pdata->isp_info[i];
314 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
315 if (ret)
316 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300317 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300318 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300319
320 if (!IS_ERR(sd)) {
321 fmd->sensor[i].subdev = sd;
322 } else {
323 fmd->sensor[i].subdev = NULL;
324 ret = PTR_ERR(sd);
325 break;
326 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300327 if (ret)
328 break;
329 }
330 pm_runtime_put(&fd->pdev->dev);
331 return ret;
332}
333
334/*
335 * MIPI CSIS and FIMC platform devices registration.
336 */
337static int fimc_register_callback(struct device *dev, void *p)
338{
339 struct fimc_dev *fimc = dev_get_drvdata(dev);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300340 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300341 struct fimc_md *fmd = p;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300342 int ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300343
344 if (!fimc || !fimc->pdev)
345 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300346
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300347 if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
348 return 0;
349
350 fmd->fimc[fimc->pdev->id] = fimc;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300351 sd->grp_id = FIMC_GROUP_ID;
352
353 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
354 if (ret) {
355 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
356 fimc->id, ret);
357 }
358
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300359 return ret;
360}
361
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300362static int fimc_lite_register_callback(struct device *dev, void *p)
363{
364 struct fimc_lite *fimc = dev_get_drvdata(dev);
365 struct v4l2_subdev *sd = &fimc->subdev;
366 struct fimc_md *fmd = p;
367 int ret;
368
369 if (fimc == NULL)
370 return 0;
371
372 if (fimc->index >= FIMC_LITE_MAX_DEVS)
373 return 0;
374
375 fmd->fimc_lite[fimc->index] = fimc;
376 sd->grp_id = FLITE_GROUP_ID;
377
378 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
379 if (ret) {
380 v4l2_err(&fmd->v4l2_dev,
381 "Failed to register FIMC-LITE.%d (%d)\n",
382 fimc->index, ret);
383 }
384 return ret;
385}
386
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300387static int csis_register_callback(struct device *dev, void *p)
388{
389 struct v4l2_subdev *sd = dev_get_drvdata(dev);
390 struct platform_device *pdev;
391 struct fimc_md *fmd = p;
392 int id, ret;
393
394 if (!sd)
395 return 0;
396 pdev = v4l2_get_subdevdata(sd);
397 if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
398 return 0;
399 v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);
400
401 id = pdev->id < 0 ? 0 : pdev->id;
402 fmd->csis[id].sd = sd;
403 sd->grp_id = CSIS_GROUP_ID;
404 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
405 if (ret)
406 v4l2_err(&fmd->v4l2_dev,
407 "Failed to register CSIS subdevice: %d\n", ret);
408 return ret;
409}
410
411/**
412 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
413 */
414static int fimc_md_register_platform_entities(struct fimc_md *fmd)
415{
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300416 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300417 struct device_driver *driver;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300418 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300419
420 driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300421 if (!driver) {
422 v4l2_warn(&fmd->v4l2_dev,
423 "%s driver not found, deffering probe\n",
424 FIMC_MODULE_NAME);
425 return -EPROBE_DEFER;
426 }
427
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300428 ret = driver_for_each_device(driver, NULL, fmd,
429 fimc_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300430 if (ret)
431 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300432
433 driver = driver_find(FIMC_LITE_DRV_NAME, &platform_bus_type);
434 if (driver && try_module_get(driver->owner)) {
435 ret = driver_for_each_device(driver, NULL, fmd,
436 fimc_lite_register_callback);
437 if (ret)
438 return ret;
439 module_put(driver->owner);
440 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300441 /*
442 * Check if there is any sensor on the MIPI-CSI2 bus and
443 * if not skip the s5p-csis module loading.
444 */
Sylwester Nawrocki41df5bf2012-04-30 12:10:58 -0300445 if (pdata == NULL)
446 return 0;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300447 for (i = 0; i < pdata->num_clients; i++) {
448 if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) {
449 ret = 1;
450 break;
451 }
452 }
453 if (!ret)
454 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300455
456 driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300457 if (!driver || !try_module_get(driver->owner)) {
458 v4l2_warn(&fmd->v4l2_dev,
459 "%s driver not found, deffering probe\n",
460 CSIS_DRIVER_NAME);
461 return -EPROBE_DEFER;
462 }
463
464 return driver_for_each_device(driver, NULL, fmd,
465 csis_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300466}
467
468static void fimc_md_unregister_entities(struct fimc_md *fmd)
469{
470 int i;
471
472 for (i = 0; i < FIMC_MAX_DEVS; i++) {
473 if (fmd->fimc[i] == NULL)
474 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300475 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300476 fmd->fimc[i] = NULL;
477 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300478 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
479 if (fmd->fimc_lite[i] == NULL)
480 continue;
481 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
482 fmd->fimc_lite[i] = NULL;
483 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300484 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
485 if (fmd->csis[i].sd == NULL)
486 continue;
487 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300488 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300489 fmd->csis[i].sd = NULL;
490 }
491 for (i = 0; i < fmd->num_sensors; i++) {
492 if (fmd->sensor[i].subdev == NULL)
493 continue;
494 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
495 fmd->sensor[i].subdev = NULL;
496 }
497}
498
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300499/**
500 * __fimc_md_create_fimc_links - create links to all FIMC entities
501 * @fmd: fimc media device
502 * @source: the source entity to create links to all fimc entities from
503 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
504 * @pad: the source entity pad index
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300505 * @link_mask: bitmask of the fimc devices for which link should be enabled
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300506 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300507static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
508 struct media_entity *source,
509 struct v4l2_subdev *sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300510 int pad, int link_mask)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300511{
512 struct fimc_sensor_info *s_info;
513 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300514 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300515 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300516
517 for (i = 0; i < FIMC_MAX_DEVS; i++) {
518 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300519 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300520 /*
521 * Some FIMC variants are not fitted with camera capture
522 * interface. Skip creating a link from sensor for those.
523 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300524 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300525 continue;
526
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300527 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300528
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300529 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300530 ret = media_entity_create_link(source, pad, sink,
531 FIMC_SD_PAD_SINK, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300532 if (ret)
533 return ret;
534
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300535 /* Notify FIMC capture subdev entity */
536 ret = media_entity_call(sink, link_setup, &sink->pads[0],
537 &source->pads[pad], flags);
538 if (ret)
539 break;
540
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300541 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
542 source->name, flags ? '=' : '-', sink->name);
543
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300544 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300545 continue;
546 s_info = v4l2_get_subdev_hostdata(sensor);
547 if (!WARN_ON(s_info == NULL)) {
548 unsigned long irq_flags;
549 spin_lock_irqsave(&fmd->slock, irq_flags);
550 s_info->host = fmd->fimc[i];
551 spin_unlock_irqrestore(&fmd->slock, irq_flags);
552 }
553 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300554
555 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
556 if (!fmd->fimc_lite[i])
557 continue;
558
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300559 if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
560 flags = MEDIA_LNK_FL_ENABLED;
561 else
562 flags = 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300563
564 sink = &fmd->fimc_lite[i]->subdev.entity;
565 ret = media_entity_create_link(source, pad, sink,
566 FLITE_SD_PAD_SINK, flags);
567 if (ret)
568 return ret;
569
570 /* Notify FIMC-LITE subdev entity */
571 ret = media_entity_call(sink, link_setup, &sink->pads[0],
572 &source->pads[pad], flags);
573 if (ret)
574 break;
575
576 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
577 source->name, flags ? '=' : '-', sink->name);
578 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300579 return 0;
580}
581
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300582/* Create links from FIMC-LITE source pads to other entities */
583static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
584{
585 struct media_entity *source, *sink;
586 unsigned int flags = MEDIA_LNK_FL_ENABLED;
587 int i, ret;
588
589 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
590 struct fimc_lite *fimc = fmd->fimc_lite[i];
591 if (fimc == NULL)
592 continue;
593 source = &fimc->subdev.entity;
594 sink = &fimc->vfd->entity;
595 /* FIMC-LITE's subdev and video node */
596 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
597 sink, 0, flags);
598 if (ret)
599 break;
600 /* TODO: create links to other entities */
601 }
602
603 return ret;
604}
605
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300606/**
607 * fimc_md_create_links - create default links between registered entities
608 *
609 * Parallel interface sensor entities are connected directly to FIMC capture
610 * entities. The sensors using MIPI CSIS bus are connected through immutable
611 * link with CSI receiver entity specified by mux_id. Any registered CSIS
612 * entity has a link to each registered FIMC capture entity. Enabled links
613 * are created by default between each subsequent registered sensor and
614 * subsequent FIMC capture entity. The number of default active links is
615 * determined by the number of available sensors or FIMC entities,
616 * whichever is less.
617 */
618static int fimc_md_create_links(struct fimc_md *fmd)
619{
620 struct v4l2_subdev *sensor, *csis;
621 struct s5p_fimc_isp_info *pdata;
622 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300623 struct media_entity *source, *sink;
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300624 int i, pad, fimc_id = 0, ret = 0;
625 u32 flags, link_mask = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300626
627 for (i = 0; i < fmd->num_sensors; i++) {
628 if (fmd->sensor[i].subdev == NULL)
629 continue;
630
631 sensor = fmd->sensor[i].subdev;
632 s_info = v4l2_get_subdev_hostdata(sensor);
633 if (!s_info || !s_info->pdata)
634 continue;
635
636 source = NULL;
637 pdata = s_info->pdata;
638
639 switch (pdata->bus_type) {
640 case FIMC_MIPI_CSI2:
641 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
642 "Wrong CSI channel id: %d\n", pdata->mux_id))
643 return -EINVAL;
644
645 csis = fmd->csis[pdata->mux_id].sd;
646 if (WARN(csis == NULL,
647 "MIPI-CSI interface specified "
648 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300649 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300650
651 ret = media_entity_create_link(&sensor->entity, 0,
652 &csis->entity, CSIS_PAD_SINK,
653 MEDIA_LNK_FL_IMMUTABLE |
654 MEDIA_LNK_FL_ENABLED);
655 if (ret)
656 return ret;
657
658 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
659 sensor->entity.name, csis->entity.name);
660
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300661 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300662 break;
663
664 case FIMC_ITU_601...FIMC_ITU_656:
665 source = &sensor->entity;
666 pad = 0;
667 break;
668
669 default:
670 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
671 pdata->bus_type);
672 return -EINVAL;
673 }
674 if (source == NULL)
675 continue;
676
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300677 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300678 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300679 pad, link_mask);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300680 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300681
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300682 for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) {
683 if (fmd->csis[i].sd == NULL)
684 continue;
685 source = &fmd->csis[i].sd->entity;
686 pad = CSIS_PAD_SOURCE;
687
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300688 link_mask = 1 << fimc_id++;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300689 ret = __fimc_md_create_fimc_sink_links(fmd, source, NULL,
Sylwester Nawrockid0da3c32012-05-16 13:08:30 -0300690 pad, link_mask);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300691 }
692
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300693 /* Create immutable links between each FIMC's subdev and video node */
694 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
695 for (i = 0; i < FIMC_MAX_DEVS; i++) {
696 if (!fmd->fimc[i])
697 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300698 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300699 sink = &fmd->fimc[i]->vid_cap.vfd->entity;
700 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
701 sink, 0, flags);
702 if (ret)
703 break;
704 }
705
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300706 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300707}
708
709/*
710 * The peripheral sensor clock management.
711 */
712static int fimc_md_get_clocks(struct fimc_md *fmd)
713{
714 char clk_name[32];
715 struct clk *clock;
716 int i;
717
718 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
719 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
720 clock = clk_get(NULL, clk_name);
721 if (IS_ERR_OR_NULL(clock)) {
722 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
723 clk_name);
724 return -ENXIO;
725 }
726 fmd->camclk[i].clock = clock;
727 }
728 return 0;
729}
730
731static void fimc_md_put_clocks(struct fimc_md *fmd)
732{
733 int i = FIMC_MAX_CAMCLKS;
734
735 while (--i >= 0) {
736 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
737 continue;
738 clk_put(fmd->camclk[i].clock);
739 fmd->camclk[i].clock = NULL;
740 }
741}
742
743static int __fimc_md_set_camclk(struct fimc_md *fmd,
744 struct fimc_sensor_info *s_info,
745 bool on)
746{
747 struct s5p_fimc_isp_info *pdata = s_info->pdata;
748 struct fimc_camclk_info *camclk;
749 int ret = 0;
750
751 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
752 return -EINVAL;
753
754 if (s_info->clk_on == on)
755 return 0;
756 camclk = &fmd->camclk[pdata->clk_id];
757
758 dbg("camclk %d, f: %lu, clk: %p, on: %d",
759 pdata->clk_id, pdata->clk_frequency, camclk, on);
760
761 if (on) {
762 if (camclk->use_count > 0 &&
763 camclk->frequency != pdata->clk_frequency)
764 return -EINVAL;
765
766 if (camclk->use_count++ == 0) {
767 clk_set_rate(camclk->clock, pdata->clk_frequency);
768 camclk->frequency = pdata->clk_frequency;
769 ret = clk_enable(camclk->clock);
770 }
771 s_info->clk_on = 1;
772 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
773 clk_get_rate(camclk->clock));
774
775 return ret;
776 }
777
778 if (WARN_ON(camclk->use_count == 0))
779 return 0;
780
781 if (--camclk->use_count == 0) {
782 clk_disable(camclk->clock);
783 s_info->clk_on = 0;
784 dbg("Disabled camclk %d", pdata->clk_id);
785 }
786 return ret;
787}
788
789/**
790 * fimc_md_set_camclk - peripheral sensor clock setup
791 * @sd: sensor subdev to configure sclk_cam clock for
792 * @on: 1 to enable or 0 to disable the clock
793 *
794 * There are 2 separate clock outputs available in the SoC for external
795 * image processors. These clocks are shared between all registered FIMC
796 * devices to which sensors can be attached, either directly or through
797 * the MIPI CSI receiver. The clock is allowed here to be used by
798 * multiple sensors concurrently if they use same frequency.
799 * The per sensor subdev clk_on attribute helps to synchronize accesses
800 * to the sclk_cam clocks from the video and media device nodes.
801 * This function should only be called when the graph mutex is held.
802 */
803int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
804{
805 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
806 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
807
808 return __fimc_md_set_camclk(fmd, s_info, on);
809}
810
811static int fimc_md_link_notify(struct media_pad *source,
812 struct media_pad *sink, u32 flags)
813{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300814 struct fimc_lite *fimc_lite = NULL;
815 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300816 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300817 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300818 int ret = 0;
819
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300820 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300821 return 0;
822
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300823 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300824
825 switch (sd->grp_id) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300826 case FLITE_GROUP_ID:
827 fimc_lite = v4l2_get_subdevdata(sd);
828 pipeline = &fimc_lite->pipeline;
829 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300830 case FIMC_GROUP_ID:
831 fimc = v4l2_get_subdevdata(sd);
832 pipeline = &fimc->pipeline;
833 break;
834 default:
835 return 0;
836 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300837
838 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300839 ret = __fimc_pipeline_shutdown(pipeline);
840 pipeline->subdevs[IDX_SENSOR] = NULL;
841 pipeline->subdevs[IDX_CSIS] = NULL;
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300842
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300843 if (fimc) {
844 mutex_lock(&fimc->lock);
845 fimc_ctrls_delete(fimc->vid_cap.ctx);
846 mutex_unlock(&fimc->lock);
847 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300848 return ret;
849 }
850 /*
851 * Link activation. Enable power of pipeline elements only if the
852 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300853 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300854 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300855 if (fimc) {
856 mutex_lock(&fimc->lock);
857 if (fimc->vid_cap.refcnt > 0) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300858 ret = __fimc_pipeline_initialize(pipeline,
859 source->entity, true);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300860 if (!ret)
861 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300862 }
863 mutex_unlock(&fimc->lock);
864 } else {
865 mutex_lock(&fimc_lite->lock);
866 if (fimc_lite->ref_count > 0) {
867 ret = __fimc_pipeline_initialize(pipeline,
868 source->entity, true);
869 }
870 mutex_unlock(&fimc_lite->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300871 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300872 return ret ? -EPIPE : ret;
873}
874
875static ssize_t fimc_md_sysfs_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
877{
878 struct platform_device *pdev = to_platform_device(dev);
879 struct fimc_md *fmd = platform_get_drvdata(pdev);
880
881 if (fmd->user_subdev_api)
882 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
883
884 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
885}
886
887static ssize_t fimc_md_sysfs_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t count)
890{
891 struct platform_device *pdev = to_platform_device(dev);
892 struct fimc_md *fmd = platform_get_drvdata(pdev);
893 bool subdev_api;
894 int i;
895
896 if (!strcmp(buf, "vid-dev\n"))
897 subdev_api = false;
898 else if (!strcmp(buf, "sub-dev\n"))
899 subdev_api = true;
900 else
901 return count;
902
903 fmd->user_subdev_api = subdev_api;
904 for (i = 0; i < FIMC_MAX_DEVS; i++)
905 if (fmd->fimc[i])
906 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
907 return count;
908}
909/*
910 * This device attribute is to select video pipeline configuration method.
911 * There are following valid values:
912 * vid-dev - for V4L2 video node API only, subdevice will be configured
913 * by the host driver.
914 * sub-dev - for media controller API, subdevs must be configured in user
915 * space before starting streaming.
916 */
917static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
918 fimc_md_sysfs_show, fimc_md_sysfs_store);
919
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300920static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300921{
922 struct v4l2_device *v4l2_dev;
923 struct fimc_md *fmd;
924 int ret;
925
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300926 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300927 if (!fmd)
928 return -ENOMEM;
929
930 spin_lock_init(&fmd->slock);
931 fmd->pdev = pdev;
932
933 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
934 sizeof(fmd->media_dev.model));
935 fmd->media_dev.link_notify = fimc_md_link_notify;
936 fmd->media_dev.dev = &pdev->dev;
937
938 v4l2_dev = &fmd->v4l2_dev;
939 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -0300940 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300941 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
942 dev_name(&pdev->dev));
943
944 ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
945 if (ret < 0) {
946 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300947 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300948 }
949 ret = media_device_register(&fmd->media_dev);
950 if (ret < 0) {
951 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300952 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300953 }
954 ret = fimc_md_get_clocks(fmd);
955 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300956 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300957
958 fmd->user_subdev_api = false;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300959
960 /* Protect the media graph while we're registering entities */
961 mutex_lock(&fmd->media_dev.graph_mutex);
962
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300963 ret = fimc_md_register_platform_entities(fmd);
964 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300965 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300966
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300967 if (pdev->dev.platform_data) {
968 ret = fimc_md_register_sensor_entities(fmd);
969 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300970 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300971 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300972 ret = fimc_md_create_links(fmd);
973 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300974 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300975 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
976 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300977 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300978
979 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300980 if (ret)
981 goto err_unlock;
982
983 platform_set_drvdata(pdev, fmd);
984 mutex_unlock(&fmd->media_dev.graph_mutex);
985 return 0;
986
987err_unlock:
988 mutex_unlock(&fmd->media_dev.graph_mutex);
989err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300990 media_device_unregister(&fmd->media_dev);
991 fimc_md_put_clocks(fmd);
992 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300993err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300994 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300995 return ret;
996}
997
998static int __devexit fimc_md_remove(struct platform_device *pdev)
999{
1000 struct fimc_md *fmd = platform_get_drvdata(pdev);
1001
1002 if (!fmd)
1003 return 0;
1004 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1005 fimc_md_unregister_entities(fmd);
1006 media_device_unregister(&fmd->media_dev);
1007 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001008 return 0;
1009}
1010
1011static struct platform_driver fimc_md_driver = {
1012 .probe = fimc_md_probe,
1013 .remove = __devexit_p(fimc_md_remove),
1014 .driver = {
1015 .name = "s5p-fimc-md",
1016 .owner = THIS_MODULE,
1017 }
1018};
1019
1020int __init fimc_md_init(void)
1021{
1022 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001023
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001024 request_module("s5p-csis");
1025 ret = fimc_register_driver();
1026 if (ret)
1027 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001028
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001029 return platform_driver_register(&fimc_md_driver);
1030}
1031void __exit fimc_md_exit(void)
1032{
1033 platform_driver_unregister(&fimc_md_driver);
1034 fimc_unregister_driver();
1035}
1036
1037module_init(fimc_md_init);
1038module_exit(fimc_md_exit);
1039
1040MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1041MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1042MODULE_LICENSE("GPL");
1043MODULE_VERSION("2.0.1");