blob: 7450dcdafc87953463a8fc1d8bd4a547ef2c2db8 [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
505 * @fimc_id: index of the fimc device for which link should be enabled
506 */
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,
510 int pad, int fimc_id)
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
527 flags = (i == fimc_id) ? 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
559 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
560
561 sink = &fmd->fimc_lite[i]->subdev.entity;
562 ret = media_entity_create_link(source, pad, sink,
563 FLITE_SD_PAD_SINK, flags);
564 if (ret)
565 return ret;
566
567 /* Notify FIMC-LITE subdev entity */
568 ret = media_entity_call(sink, link_setup, &sink->pads[0],
569 &source->pads[pad], flags);
570 if (ret)
571 break;
572
573 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
574 source->name, flags ? '=' : '-', sink->name);
575 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300576 return 0;
577}
578
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300579/* Create links from FIMC-LITE source pads to other entities */
580static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
581{
582 struct media_entity *source, *sink;
583 unsigned int flags = MEDIA_LNK_FL_ENABLED;
584 int i, ret;
585
586 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
587 struct fimc_lite *fimc = fmd->fimc_lite[i];
588 if (fimc == NULL)
589 continue;
590 source = &fimc->subdev.entity;
591 sink = &fimc->vfd->entity;
592 /* FIMC-LITE's subdev and video node */
593 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
594 sink, 0, flags);
595 if (ret)
596 break;
597 /* TODO: create links to other entities */
598 }
599
600 return ret;
601}
602
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300603/**
604 * fimc_md_create_links - create default links between registered entities
605 *
606 * Parallel interface sensor entities are connected directly to FIMC capture
607 * entities. The sensors using MIPI CSIS bus are connected through immutable
608 * link with CSI receiver entity specified by mux_id. Any registered CSIS
609 * entity has a link to each registered FIMC capture entity. Enabled links
610 * are created by default between each subsequent registered sensor and
611 * subsequent FIMC capture entity. The number of default active links is
612 * determined by the number of available sensors or FIMC entities,
613 * whichever is less.
614 */
615static int fimc_md_create_links(struct fimc_md *fmd)
616{
617 struct v4l2_subdev *sensor, *csis;
618 struct s5p_fimc_isp_info *pdata;
619 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300620 struct media_entity *source, *sink;
621 int i, pad, fimc_id = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300622 int ret = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300623 u32 flags;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300624
625 for (i = 0; i < fmd->num_sensors; i++) {
626 if (fmd->sensor[i].subdev == NULL)
627 continue;
628
629 sensor = fmd->sensor[i].subdev;
630 s_info = v4l2_get_subdev_hostdata(sensor);
631 if (!s_info || !s_info->pdata)
632 continue;
633
634 source = NULL;
635 pdata = s_info->pdata;
636
637 switch (pdata->bus_type) {
638 case FIMC_MIPI_CSI2:
639 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
640 "Wrong CSI channel id: %d\n", pdata->mux_id))
641 return -EINVAL;
642
643 csis = fmd->csis[pdata->mux_id].sd;
644 if (WARN(csis == NULL,
645 "MIPI-CSI interface specified "
646 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300647 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300648
649 ret = media_entity_create_link(&sensor->entity, 0,
650 &csis->entity, CSIS_PAD_SINK,
651 MEDIA_LNK_FL_IMMUTABLE |
652 MEDIA_LNK_FL_ENABLED);
653 if (ret)
654 return ret;
655
656 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
657 sensor->entity.name, csis->entity.name);
658
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300659 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300660 break;
661
662 case FIMC_ITU_601...FIMC_ITU_656:
663 source = &sensor->entity;
664 pad = 0;
665 break;
666
667 default:
668 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
669 pdata->bus_type);
670 return -EINVAL;
671 }
672 if (source == NULL)
673 continue;
674
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300675 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
676 pad, fimc_id++);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300677 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300678
679 fimc_id = 0;
680 for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) {
681 if (fmd->csis[i].sd == NULL)
682 continue;
683 source = &fmd->csis[i].sd->entity;
684 pad = CSIS_PAD_SOURCE;
685
686 ret = __fimc_md_create_fimc_sink_links(fmd, source, NULL,
687 pad, fimc_id++);
688 }
689
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300690 /* Create immutable links between each FIMC's subdev and video node */
691 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
692 for (i = 0; i < FIMC_MAX_DEVS; i++) {
693 if (!fmd->fimc[i])
694 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300695 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300696 sink = &fmd->fimc[i]->vid_cap.vfd->entity;
697 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
698 sink, 0, flags);
699 if (ret)
700 break;
701 }
702
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300703 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300704}
705
706/*
707 * The peripheral sensor clock management.
708 */
709static int fimc_md_get_clocks(struct fimc_md *fmd)
710{
711 char clk_name[32];
712 struct clk *clock;
713 int i;
714
715 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
716 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
717 clock = clk_get(NULL, clk_name);
718 if (IS_ERR_OR_NULL(clock)) {
719 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
720 clk_name);
721 return -ENXIO;
722 }
723 fmd->camclk[i].clock = clock;
724 }
725 return 0;
726}
727
728static void fimc_md_put_clocks(struct fimc_md *fmd)
729{
730 int i = FIMC_MAX_CAMCLKS;
731
732 while (--i >= 0) {
733 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
734 continue;
735 clk_put(fmd->camclk[i].clock);
736 fmd->camclk[i].clock = NULL;
737 }
738}
739
740static int __fimc_md_set_camclk(struct fimc_md *fmd,
741 struct fimc_sensor_info *s_info,
742 bool on)
743{
744 struct s5p_fimc_isp_info *pdata = s_info->pdata;
745 struct fimc_camclk_info *camclk;
746 int ret = 0;
747
748 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
749 return -EINVAL;
750
751 if (s_info->clk_on == on)
752 return 0;
753 camclk = &fmd->camclk[pdata->clk_id];
754
755 dbg("camclk %d, f: %lu, clk: %p, on: %d",
756 pdata->clk_id, pdata->clk_frequency, camclk, on);
757
758 if (on) {
759 if (camclk->use_count > 0 &&
760 camclk->frequency != pdata->clk_frequency)
761 return -EINVAL;
762
763 if (camclk->use_count++ == 0) {
764 clk_set_rate(camclk->clock, pdata->clk_frequency);
765 camclk->frequency = pdata->clk_frequency;
766 ret = clk_enable(camclk->clock);
767 }
768 s_info->clk_on = 1;
769 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
770 clk_get_rate(camclk->clock));
771
772 return ret;
773 }
774
775 if (WARN_ON(camclk->use_count == 0))
776 return 0;
777
778 if (--camclk->use_count == 0) {
779 clk_disable(camclk->clock);
780 s_info->clk_on = 0;
781 dbg("Disabled camclk %d", pdata->clk_id);
782 }
783 return ret;
784}
785
786/**
787 * fimc_md_set_camclk - peripheral sensor clock setup
788 * @sd: sensor subdev to configure sclk_cam clock for
789 * @on: 1 to enable or 0 to disable the clock
790 *
791 * There are 2 separate clock outputs available in the SoC for external
792 * image processors. These clocks are shared between all registered FIMC
793 * devices to which sensors can be attached, either directly or through
794 * the MIPI CSI receiver. The clock is allowed here to be used by
795 * multiple sensors concurrently if they use same frequency.
796 * The per sensor subdev clk_on attribute helps to synchronize accesses
797 * to the sclk_cam clocks from the video and media device nodes.
798 * This function should only be called when the graph mutex is held.
799 */
800int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
801{
802 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
803 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
804
805 return __fimc_md_set_camclk(fmd, s_info, on);
806}
807
808static int fimc_md_link_notify(struct media_pad *source,
809 struct media_pad *sink, u32 flags)
810{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300811 struct fimc_lite *fimc_lite = NULL;
812 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300813 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300814 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300815 int ret = 0;
816
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300817 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300818 return 0;
819
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300820 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300821
822 switch (sd->grp_id) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300823 case FLITE_GROUP_ID:
824 fimc_lite = v4l2_get_subdevdata(sd);
825 pipeline = &fimc_lite->pipeline;
826 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300827 case FIMC_GROUP_ID:
828 fimc = v4l2_get_subdevdata(sd);
829 pipeline = &fimc->pipeline;
830 break;
831 default:
832 return 0;
833 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300834
835 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300836 ret = __fimc_pipeline_shutdown(pipeline);
837 pipeline->subdevs[IDX_SENSOR] = NULL;
838 pipeline->subdevs[IDX_CSIS] = NULL;
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300839
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300840 if (fimc) {
841 mutex_lock(&fimc->lock);
842 fimc_ctrls_delete(fimc->vid_cap.ctx);
843 mutex_unlock(&fimc->lock);
844 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300845 return ret;
846 }
847 /*
848 * Link activation. Enable power of pipeline elements only if the
849 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300850 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300851 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300852 if (fimc) {
853 mutex_lock(&fimc->lock);
854 if (fimc->vid_cap.refcnt > 0) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300855 ret = __fimc_pipeline_initialize(pipeline,
856 source->entity, true);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300857 if (!ret)
858 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300859 }
860 mutex_unlock(&fimc->lock);
861 } else {
862 mutex_lock(&fimc_lite->lock);
863 if (fimc_lite->ref_count > 0) {
864 ret = __fimc_pipeline_initialize(pipeline,
865 source->entity, true);
866 }
867 mutex_unlock(&fimc_lite->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300868 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300869 return ret ? -EPIPE : ret;
870}
871
872static ssize_t fimc_md_sysfs_show(struct device *dev,
873 struct device_attribute *attr, char *buf)
874{
875 struct platform_device *pdev = to_platform_device(dev);
876 struct fimc_md *fmd = platform_get_drvdata(pdev);
877
878 if (fmd->user_subdev_api)
879 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
880
881 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
882}
883
884static ssize_t fimc_md_sysfs_store(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
887{
888 struct platform_device *pdev = to_platform_device(dev);
889 struct fimc_md *fmd = platform_get_drvdata(pdev);
890 bool subdev_api;
891 int i;
892
893 if (!strcmp(buf, "vid-dev\n"))
894 subdev_api = false;
895 else if (!strcmp(buf, "sub-dev\n"))
896 subdev_api = true;
897 else
898 return count;
899
900 fmd->user_subdev_api = subdev_api;
901 for (i = 0; i < FIMC_MAX_DEVS; i++)
902 if (fmd->fimc[i])
903 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
904 return count;
905}
906/*
907 * This device attribute is to select video pipeline configuration method.
908 * There are following valid values:
909 * vid-dev - for V4L2 video node API only, subdevice will be configured
910 * by the host driver.
911 * sub-dev - for media controller API, subdevs must be configured in user
912 * space before starting streaming.
913 */
914static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
915 fimc_md_sysfs_show, fimc_md_sysfs_store);
916
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300917static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300918{
919 struct v4l2_device *v4l2_dev;
920 struct fimc_md *fmd;
921 int ret;
922
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300923 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300924 if (!fmd)
925 return -ENOMEM;
926
927 spin_lock_init(&fmd->slock);
928 fmd->pdev = pdev;
929
930 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
931 sizeof(fmd->media_dev.model));
932 fmd->media_dev.link_notify = fimc_md_link_notify;
933 fmd->media_dev.dev = &pdev->dev;
934
935 v4l2_dev = &fmd->v4l2_dev;
936 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -0300937 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300938 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
939 dev_name(&pdev->dev));
940
941 ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
942 if (ret < 0) {
943 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300944 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300945 }
946 ret = media_device_register(&fmd->media_dev);
947 if (ret < 0) {
948 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300949 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300950 }
951 ret = fimc_md_get_clocks(fmd);
952 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300953 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300954
955 fmd->user_subdev_api = false;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300956
957 /* Protect the media graph while we're registering entities */
958 mutex_lock(&fmd->media_dev.graph_mutex);
959
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300960 ret = fimc_md_register_platform_entities(fmd);
961 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300962 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300963
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300964 if (pdev->dev.platform_data) {
965 ret = fimc_md_register_sensor_entities(fmd);
966 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300967 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300968 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300969 ret = fimc_md_create_links(fmd);
970 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300971 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300972 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
973 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300974 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300975
976 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300977 if (ret)
978 goto err_unlock;
979
980 platform_set_drvdata(pdev, fmd);
981 mutex_unlock(&fmd->media_dev.graph_mutex);
982 return 0;
983
984err_unlock:
985 mutex_unlock(&fmd->media_dev.graph_mutex);
986err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300987 media_device_unregister(&fmd->media_dev);
988 fimc_md_put_clocks(fmd);
989 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300990err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300991 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300992 return ret;
993}
994
995static int __devexit fimc_md_remove(struct platform_device *pdev)
996{
997 struct fimc_md *fmd = platform_get_drvdata(pdev);
998
999 if (!fmd)
1000 return 0;
1001 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1002 fimc_md_unregister_entities(fmd);
1003 media_device_unregister(&fmd->media_dev);
1004 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001005 return 0;
1006}
1007
1008static struct platform_driver fimc_md_driver = {
1009 .probe = fimc_md_probe,
1010 .remove = __devexit_p(fimc_md_remove),
1011 .driver = {
1012 .name = "s5p-fimc-md",
1013 .owner = THIS_MODULE,
1014 }
1015};
1016
1017int __init fimc_md_init(void)
1018{
1019 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001020
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001021 request_module("s5p-csis");
1022 ret = fimc_register_driver();
1023 if (ret)
1024 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001025
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001026 return platform_driver_register(&fimc_md_driver);
1027}
1028void __exit fimc_md_exit(void)
1029{
1030 platform_driver_unregister(&fimc_md_driver);
1031 fimc_unregister_driver();
1032}
1033
1034module_init(fimc_md_init);
1035module_exit(fimc_md_exit);
1036
1037MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1038MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1039MODULE_LICENSE("GPL");
1040MODULE_VERSION("2.0.1");