blob: 6753c45631b856e1a06d19492e56e97edf01f9f6 [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 Nawrocki0f735f52012-04-27 09:33:10 -0300196 struct media_entity *me = &p->subdevs[IDX_SENSOR]->entity;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300197 int ret;
198
199 mutex_lock(&me->parent->graph_mutex);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300200 ret = __fimc_pipeline_shutdown(p);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300201 mutex_unlock(&me->parent->graph_mutex);
202
203 return ret;
204}
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300205EXPORT_SYMBOL_GPL(fimc_pipeline_shutdown);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300206
207/**
208 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300209 * @pipeline: video pipeline structure
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300210 * @on: passed as the s_stream call argument
211 */
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300212int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300213{
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300214 int i, ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300215
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300216 if (p->subdevs[IDX_SENSOR] == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300217 return -ENODEV;
218
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300219 for (i = 0; i < IDX_MAX; i++) {
220 unsigned int idx = on ? (IDX_MAX - 1) - i : i;
221
222 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
223
224 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
225 return ret;
226 }
227
228 return 0;
229
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300230}
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300231EXPORT_SYMBOL_GPL(fimc_pipeline_s_stream);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300232
233/*
234 * Sensor subdevice helper functions
235 */
236static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
237 struct fimc_sensor_info *s_info)
238{
239 struct i2c_adapter *adapter;
240 struct v4l2_subdev *sd = NULL;
241
242 if (!s_info || !fmd)
243 return NULL;
244
245 adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300246 if (!adapter) {
247 v4l2_warn(&fmd->v4l2_dev,
248 "Failed to get I2C adapter %d, deferring probe\n",
249 s_info->pdata->i2c_bus_num);
250 return ERR_PTR(-EPROBE_DEFER);
251 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300252 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
253 s_info->pdata->board_info, NULL);
254 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300255 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300256 v4l2_warn(&fmd->v4l2_dev,
257 "Failed to acquire subdev %s, deferring probe\n",
258 s_info->pdata->board_info->type);
259 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300260 }
261 v4l2_set_subdev_hostdata(sd, s_info);
262 sd->grp_id = SENSOR_GROUP_ID;
263
264 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
265 s_info->pdata->board_info->type);
266 return sd;
267}
268
269static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
270{
271 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300272 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300273
274 if (!client)
275 return;
276 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300277 adapter = client->adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300278 i2c_unregister_device(client);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300279 if (adapter)
280 i2c_put_adapter(adapter);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300281}
282
283static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
284{
285 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
286 struct fimc_dev *fd = NULL;
287 int num_clients, ret, i;
288
289 /*
290 * Runtime resume one of the FIMC entities to make sure
291 * the sclk_cam clocks are not globally disabled.
292 */
293 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
294 if (fmd->fimc[i])
295 fd = fmd->fimc[i];
296 if (!fd)
297 return -ENXIO;
298 ret = pm_runtime_get_sync(&fd->pdev->dev);
299 if (ret < 0)
300 return ret;
301
302 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
303 num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
304
305 fmd->num_sensors = num_clients;
306 for (i = 0; i < num_clients; i++) {
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300307 struct v4l2_subdev *sd;
308
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300309 fmd->sensor[i].pdata = &pdata->isp_info[i];
310 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
311 if (ret)
312 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300313 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300314 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300315
316 if (!IS_ERR(sd)) {
317 fmd->sensor[i].subdev = sd;
318 } else {
319 fmd->sensor[i].subdev = NULL;
320 ret = PTR_ERR(sd);
321 break;
322 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300323 if (ret)
324 break;
325 }
326 pm_runtime_put(&fd->pdev->dev);
327 return ret;
328}
329
330/*
331 * MIPI CSIS and FIMC platform devices registration.
332 */
333static int fimc_register_callback(struct device *dev, void *p)
334{
335 struct fimc_dev *fimc = dev_get_drvdata(dev);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300336 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300337 struct fimc_md *fmd = p;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300338 int ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300339
340 if (!fimc || !fimc->pdev)
341 return 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300342
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300343 if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
344 return 0;
345
346 fmd->fimc[fimc->pdev->id] = fimc;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300347 sd->grp_id = FIMC_GROUP_ID;
348
349 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
350 if (ret) {
351 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
352 fimc->id, ret);
353 }
354
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300355 return ret;
356}
357
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300358static int fimc_lite_register_callback(struct device *dev, void *p)
359{
360 struct fimc_lite *fimc = dev_get_drvdata(dev);
361 struct v4l2_subdev *sd = &fimc->subdev;
362 struct fimc_md *fmd = p;
363 int ret;
364
365 if (fimc == NULL)
366 return 0;
367
368 if (fimc->index >= FIMC_LITE_MAX_DEVS)
369 return 0;
370
371 fmd->fimc_lite[fimc->index] = fimc;
372 sd->grp_id = FLITE_GROUP_ID;
373
374 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
375 if (ret) {
376 v4l2_err(&fmd->v4l2_dev,
377 "Failed to register FIMC-LITE.%d (%d)\n",
378 fimc->index, ret);
379 }
380 return ret;
381}
382
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300383static int csis_register_callback(struct device *dev, void *p)
384{
385 struct v4l2_subdev *sd = dev_get_drvdata(dev);
386 struct platform_device *pdev;
387 struct fimc_md *fmd = p;
388 int id, ret;
389
390 if (!sd)
391 return 0;
392 pdev = v4l2_get_subdevdata(sd);
393 if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
394 return 0;
395 v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);
396
397 id = pdev->id < 0 ? 0 : pdev->id;
398 fmd->csis[id].sd = sd;
399 sd->grp_id = CSIS_GROUP_ID;
400 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
401 if (ret)
402 v4l2_err(&fmd->v4l2_dev,
403 "Failed to register CSIS subdevice: %d\n", ret);
404 return ret;
405}
406
407/**
408 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
409 */
410static int fimc_md_register_platform_entities(struct fimc_md *fmd)
411{
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300412 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300413 struct device_driver *driver;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300414 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300415
416 driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300417 if (!driver) {
418 v4l2_warn(&fmd->v4l2_dev,
419 "%s driver not found, deffering probe\n",
420 FIMC_MODULE_NAME);
421 return -EPROBE_DEFER;
422 }
423
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300424 ret = driver_for_each_device(driver, NULL, fmd,
425 fimc_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300426 if (ret)
427 return ret;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300428
429 driver = driver_find(FIMC_LITE_DRV_NAME, &platform_bus_type);
430 if (driver && try_module_get(driver->owner)) {
431 ret = driver_for_each_device(driver, NULL, fmd,
432 fimc_lite_register_callback);
433 if (ret)
434 return ret;
435 module_put(driver->owner);
436 }
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300437 /*
438 * Check if there is any sensor on the MIPI-CSI2 bus and
439 * if not skip the s5p-csis module loading.
440 */
Sylwester Nawrocki41df5bf2012-04-30 12:10:58 -0300441 if (pdata == NULL)
442 return 0;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300443 for (i = 0; i < pdata->num_clients; i++) {
444 if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) {
445 ret = 1;
446 break;
447 }
448 }
449 if (!ret)
450 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300451
452 driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300453 if (!driver || !try_module_get(driver->owner)) {
454 v4l2_warn(&fmd->v4l2_dev,
455 "%s driver not found, deffering probe\n",
456 CSIS_DRIVER_NAME);
457 return -EPROBE_DEFER;
458 }
459
460 return driver_for_each_device(driver, NULL, fmd,
461 csis_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300462}
463
464static void fimc_md_unregister_entities(struct fimc_md *fmd)
465{
466 int i;
467
468 for (i = 0; i < FIMC_MAX_DEVS; i++) {
469 if (fmd->fimc[i] == NULL)
470 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300471 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300472 fmd->fimc[i] = NULL;
473 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300474 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
475 if (fmd->fimc_lite[i] == NULL)
476 continue;
477 v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
478 fmd->fimc_lite[i] = NULL;
479 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300480 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
481 if (fmd->csis[i].sd == NULL)
482 continue;
483 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300484 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300485 fmd->csis[i].sd = NULL;
486 }
487 for (i = 0; i < fmd->num_sensors; i++) {
488 if (fmd->sensor[i].subdev == NULL)
489 continue;
490 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
491 fmd->sensor[i].subdev = NULL;
492 }
493}
494
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300495/**
496 * __fimc_md_create_fimc_links - create links to all FIMC entities
497 * @fmd: fimc media device
498 * @source: the source entity to create links to all fimc entities from
499 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
500 * @pad: the source entity pad index
501 * @fimc_id: index of the fimc device for which link should be enabled
502 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300503static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
504 struct media_entity *source,
505 struct v4l2_subdev *sensor,
506 int pad, int fimc_id)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300507{
508 struct fimc_sensor_info *s_info;
509 struct media_entity *sink;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300510 unsigned int flags = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300511 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300512
513 for (i = 0; i < FIMC_MAX_DEVS; i++) {
514 if (!fmd->fimc[i])
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300515 continue;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300516 /*
517 * Some FIMC variants are not fitted with camera capture
518 * interface. Skip creating a link from sensor for those.
519 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300520 if (!fmd->fimc[i]->variant->has_cam_if)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300521 continue;
522
523 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300524
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300525 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300526 ret = media_entity_create_link(source, pad, sink,
527 FIMC_SD_PAD_SINK, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300528 if (ret)
529 return ret;
530
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300531 /* Notify FIMC capture subdev entity */
532 ret = media_entity_call(sink, link_setup, &sink->pads[0],
533 &source->pads[pad], flags);
534 if (ret)
535 break;
536
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300537 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
538 source->name, flags ? '=' : '-', sink->name);
539
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300540 if (flags == 0 || sensor == NULL)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300541 continue;
542 s_info = v4l2_get_subdev_hostdata(sensor);
543 if (!WARN_ON(s_info == NULL)) {
544 unsigned long irq_flags;
545 spin_lock_irqsave(&fmd->slock, irq_flags);
546 s_info->host = fmd->fimc[i];
547 spin_unlock_irqrestore(&fmd->slock, irq_flags);
548 }
549 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300550
551 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
552 if (!fmd->fimc_lite[i])
553 continue;
554
555 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
556
557 sink = &fmd->fimc_lite[i]->subdev.entity;
558 ret = media_entity_create_link(source, pad, sink,
559 FLITE_SD_PAD_SINK, flags);
560 if (ret)
561 return ret;
562
563 /* Notify FIMC-LITE subdev entity */
564 ret = media_entity_call(sink, link_setup, &sink->pads[0],
565 &source->pads[pad], flags);
566 if (ret)
567 break;
568
569 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
570 source->name, flags ? '=' : '-', sink->name);
571 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300572 return 0;
573}
574
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300575/* Create links from FIMC-LITE source pads to other entities */
576static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
577{
578 struct media_entity *source, *sink;
579 unsigned int flags = MEDIA_LNK_FL_ENABLED;
580 int i, ret;
581
582 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
583 struct fimc_lite *fimc = fmd->fimc_lite[i];
584 if (fimc == NULL)
585 continue;
586 source = &fimc->subdev.entity;
587 sink = &fimc->vfd->entity;
588 /* FIMC-LITE's subdev and video node */
589 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
590 sink, 0, flags);
591 if (ret)
592 break;
593 /* TODO: create links to other entities */
594 }
595
596 return ret;
597}
598
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300599/**
600 * fimc_md_create_links - create default links between registered entities
601 *
602 * Parallel interface sensor entities are connected directly to FIMC capture
603 * entities. The sensors using MIPI CSIS bus are connected through immutable
604 * link with CSI receiver entity specified by mux_id. Any registered CSIS
605 * entity has a link to each registered FIMC capture entity. Enabled links
606 * are created by default between each subsequent registered sensor and
607 * subsequent FIMC capture entity. The number of default active links is
608 * determined by the number of available sensors or FIMC entities,
609 * whichever is less.
610 */
611static int fimc_md_create_links(struct fimc_md *fmd)
612{
613 struct v4l2_subdev *sensor, *csis;
614 struct s5p_fimc_isp_info *pdata;
615 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300616 struct media_entity *source, *sink;
617 int i, pad, fimc_id = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300618 int ret = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300619 u32 flags;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300620
621 for (i = 0; i < fmd->num_sensors; i++) {
622 if (fmd->sensor[i].subdev == NULL)
623 continue;
624
625 sensor = fmd->sensor[i].subdev;
626 s_info = v4l2_get_subdev_hostdata(sensor);
627 if (!s_info || !s_info->pdata)
628 continue;
629
630 source = NULL;
631 pdata = s_info->pdata;
632
633 switch (pdata->bus_type) {
634 case FIMC_MIPI_CSI2:
635 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
636 "Wrong CSI channel id: %d\n", pdata->mux_id))
637 return -EINVAL;
638
639 csis = fmd->csis[pdata->mux_id].sd;
640 if (WARN(csis == NULL,
641 "MIPI-CSI interface specified "
642 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300643 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300644
645 ret = media_entity_create_link(&sensor->entity, 0,
646 &csis->entity, CSIS_PAD_SINK,
647 MEDIA_LNK_FL_IMMUTABLE |
648 MEDIA_LNK_FL_ENABLED);
649 if (ret)
650 return ret;
651
652 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
653 sensor->entity.name, csis->entity.name);
654
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300655 source = NULL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300656 break;
657
658 case FIMC_ITU_601...FIMC_ITU_656:
659 source = &sensor->entity;
660 pad = 0;
661 break;
662
663 default:
664 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
665 pdata->bus_type);
666 return -EINVAL;
667 }
668 if (source == NULL)
669 continue;
670
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300671 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
672 pad, fimc_id++);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300673 }
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300674
675 fimc_id = 0;
676 for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) {
677 if (fmd->csis[i].sd == NULL)
678 continue;
679 source = &fmd->csis[i].sd->entity;
680 pad = CSIS_PAD_SOURCE;
681
682 ret = __fimc_md_create_fimc_sink_links(fmd, source, NULL,
683 pad, fimc_id++);
684 }
685
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300686 /* Create immutable links between each FIMC's subdev and video node */
687 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
688 for (i = 0; i < FIMC_MAX_DEVS; i++) {
689 if (!fmd->fimc[i])
690 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300691 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300692 sink = &fmd->fimc[i]->vid_cap.vfd->entity;
693 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
694 sink, 0, flags);
695 if (ret)
696 break;
697 }
698
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300699 return __fimc_md_create_flite_source_links(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300700}
701
702/*
703 * The peripheral sensor clock management.
704 */
705static int fimc_md_get_clocks(struct fimc_md *fmd)
706{
707 char clk_name[32];
708 struct clk *clock;
709 int i;
710
711 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
712 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
713 clock = clk_get(NULL, clk_name);
714 if (IS_ERR_OR_NULL(clock)) {
715 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
716 clk_name);
717 return -ENXIO;
718 }
719 fmd->camclk[i].clock = clock;
720 }
721 return 0;
722}
723
724static void fimc_md_put_clocks(struct fimc_md *fmd)
725{
726 int i = FIMC_MAX_CAMCLKS;
727
728 while (--i >= 0) {
729 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
730 continue;
731 clk_put(fmd->camclk[i].clock);
732 fmd->camclk[i].clock = NULL;
733 }
734}
735
736static int __fimc_md_set_camclk(struct fimc_md *fmd,
737 struct fimc_sensor_info *s_info,
738 bool on)
739{
740 struct s5p_fimc_isp_info *pdata = s_info->pdata;
741 struct fimc_camclk_info *camclk;
742 int ret = 0;
743
744 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
745 return -EINVAL;
746
747 if (s_info->clk_on == on)
748 return 0;
749 camclk = &fmd->camclk[pdata->clk_id];
750
751 dbg("camclk %d, f: %lu, clk: %p, on: %d",
752 pdata->clk_id, pdata->clk_frequency, camclk, on);
753
754 if (on) {
755 if (camclk->use_count > 0 &&
756 camclk->frequency != pdata->clk_frequency)
757 return -EINVAL;
758
759 if (camclk->use_count++ == 0) {
760 clk_set_rate(camclk->clock, pdata->clk_frequency);
761 camclk->frequency = pdata->clk_frequency;
762 ret = clk_enable(camclk->clock);
763 }
764 s_info->clk_on = 1;
765 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
766 clk_get_rate(camclk->clock));
767
768 return ret;
769 }
770
771 if (WARN_ON(camclk->use_count == 0))
772 return 0;
773
774 if (--camclk->use_count == 0) {
775 clk_disable(camclk->clock);
776 s_info->clk_on = 0;
777 dbg("Disabled camclk %d", pdata->clk_id);
778 }
779 return ret;
780}
781
782/**
783 * fimc_md_set_camclk - peripheral sensor clock setup
784 * @sd: sensor subdev to configure sclk_cam clock for
785 * @on: 1 to enable or 0 to disable the clock
786 *
787 * There are 2 separate clock outputs available in the SoC for external
788 * image processors. These clocks are shared between all registered FIMC
789 * devices to which sensors can be attached, either directly or through
790 * the MIPI CSI receiver. The clock is allowed here to be used by
791 * multiple sensors concurrently if they use same frequency.
792 * The per sensor subdev clk_on attribute helps to synchronize accesses
793 * to the sclk_cam clocks from the video and media device nodes.
794 * This function should only be called when the graph mutex is held.
795 */
796int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
797{
798 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
799 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
800
801 return __fimc_md_set_camclk(fmd, s_info, on);
802}
803
804static int fimc_md_link_notify(struct media_pad *source,
805 struct media_pad *sink, u32 flags)
806{
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300807 struct fimc_lite *fimc_lite = NULL;
808 struct fimc_dev *fimc = NULL;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300809 struct fimc_pipeline *pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300810 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300811 int ret = 0;
812
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300813 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300814 return 0;
815
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300816 sd = media_entity_to_v4l2_subdev(sink->entity);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300817
818 switch (sd->grp_id) {
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300819 case FLITE_GROUP_ID:
820 fimc_lite = v4l2_get_subdevdata(sd);
821 pipeline = &fimc_lite->pipeline;
822 break;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300823 case FIMC_GROUP_ID:
824 fimc = v4l2_get_subdevdata(sd);
825 pipeline = &fimc->pipeline;
826 break;
827 default:
828 return 0;
829 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300830
831 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300832 ret = __fimc_pipeline_shutdown(pipeline);
833 pipeline->subdevs[IDX_SENSOR] = NULL;
834 pipeline->subdevs[IDX_CSIS] = NULL;
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300835
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300836 if (fimc) {
837 mutex_lock(&fimc->lock);
838 fimc_ctrls_delete(fimc->vid_cap.ctx);
839 mutex_unlock(&fimc->lock);
840 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300841 return ret;
842 }
843 /*
844 * Link activation. Enable power of pipeline elements only if the
845 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300846 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300847 */
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300848 if (fimc) {
849 mutex_lock(&fimc->lock);
850 if (fimc->vid_cap.refcnt > 0) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300851 ret = __fimc_pipeline_initialize(pipeline,
852 source->entity, true);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300853 if (!ret)
854 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki4af81312012-04-27 05:29:05 -0300855 }
856 mutex_unlock(&fimc->lock);
857 } else {
858 mutex_lock(&fimc_lite->lock);
859 if (fimc_lite->ref_count > 0) {
860 ret = __fimc_pipeline_initialize(pipeline,
861 source->entity, true);
862 }
863 mutex_unlock(&fimc_lite->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300864 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300865 return ret ? -EPIPE : ret;
866}
867
868static ssize_t fimc_md_sysfs_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
870{
871 struct platform_device *pdev = to_platform_device(dev);
872 struct fimc_md *fmd = platform_get_drvdata(pdev);
873
874 if (fmd->user_subdev_api)
875 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
876
877 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
878}
879
880static ssize_t fimc_md_sysfs_store(struct device *dev,
881 struct device_attribute *attr,
882 const char *buf, size_t count)
883{
884 struct platform_device *pdev = to_platform_device(dev);
885 struct fimc_md *fmd = platform_get_drvdata(pdev);
886 bool subdev_api;
887 int i;
888
889 if (!strcmp(buf, "vid-dev\n"))
890 subdev_api = false;
891 else if (!strcmp(buf, "sub-dev\n"))
892 subdev_api = true;
893 else
894 return count;
895
896 fmd->user_subdev_api = subdev_api;
897 for (i = 0; i < FIMC_MAX_DEVS; i++)
898 if (fmd->fimc[i])
899 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
900 return count;
901}
902/*
903 * This device attribute is to select video pipeline configuration method.
904 * There are following valid values:
905 * vid-dev - for V4L2 video node API only, subdevice will be configured
906 * by the host driver.
907 * sub-dev - for media controller API, subdevs must be configured in user
908 * space before starting streaming.
909 */
910static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
911 fimc_md_sysfs_show, fimc_md_sysfs_store);
912
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300913static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300914{
915 struct v4l2_device *v4l2_dev;
916 struct fimc_md *fmd;
917 int ret;
918
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300919 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300920 if (!fmd)
921 return -ENOMEM;
922
923 spin_lock_init(&fmd->slock);
924 fmd->pdev = pdev;
925
926 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
927 sizeof(fmd->media_dev.model));
928 fmd->media_dev.link_notify = fimc_md_link_notify;
929 fmd->media_dev.dev = &pdev->dev;
930
931 v4l2_dev = &fmd->v4l2_dev;
932 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -0300933 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300934 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
935 dev_name(&pdev->dev));
936
937 ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
938 if (ret < 0) {
939 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300940 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300941 }
942 ret = media_device_register(&fmd->media_dev);
943 if (ret < 0) {
944 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300945 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300946 }
947 ret = fimc_md_get_clocks(fmd);
948 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300949 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300950
951 fmd->user_subdev_api = false;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300952
953 /* Protect the media graph while we're registering entities */
954 mutex_lock(&fmd->media_dev.graph_mutex);
955
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300956 ret = fimc_md_register_platform_entities(fmd);
957 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300958 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300959
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300960 if (pdev->dev.platform_data) {
961 ret = fimc_md_register_sensor_entities(fmd);
962 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300963 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300964 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300965 ret = fimc_md_create_links(fmd);
966 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300967 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300968 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
969 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300970 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300971
972 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300973 if (ret)
974 goto err_unlock;
975
976 platform_set_drvdata(pdev, fmd);
977 mutex_unlock(&fmd->media_dev.graph_mutex);
978 return 0;
979
980err_unlock:
981 mutex_unlock(&fmd->media_dev.graph_mutex);
982err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300983 media_device_unregister(&fmd->media_dev);
984 fimc_md_put_clocks(fmd);
985 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300986err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300987 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300988 return ret;
989}
990
991static int __devexit fimc_md_remove(struct platform_device *pdev)
992{
993 struct fimc_md *fmd = platform_get_drvdata(pdev);
994
995 if (!fmd)
996 return 0;
997 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
998 fimc_md_unregister_entities(fmd);
999 media_device_unregister(&fmd->media_dev);
1000 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001001 return 0;
1002}
1003
1004static struct platform_driver fimc_md_driver = {
1005 .probe = fimc_md_probe,
1006 .remove = __devexit_p(fimc_md_remove),
1007 .driver = {
1008 .name = "s5p-fimc-md",
1009 .owner = THIS_MODULE,
1010 }
1011};
1012
1013int __init fimc_md_init(void)
1014{
1015 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001016
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001017 request_module("s5p-csis");
1018 ret = fimc_register_driver();
1019 if (ret)
1020 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -03001021
Sylwester Nawrockid3953222011-09-01 06:01:08 -03001022 return platform_driver_register(&fimc_md_driver);
1023}
1024void __exit fimc_md_exit(void)
1025{
1026 platform_driver_unregister(&fimc_md_driver);
1027 fimc_unregister_driver();
1028}
1029
1030module_init(fimc_md_init);
1031module_exit(fimc_md_exit);
1032
1033MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1034MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1035MODULE_LICENSE("GPL");
1036MODULE_VERSION("2.0.1");