blob: c319842c762d48754aff8bfc32c549fb07d8f076 [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"
28#include "fimc-mdevice.h"
29#include "mipi-csis.h"
30
31static int __fimc_md_set_camclk(struct fimc_md *fmd,
32 struct fimc_sensor_info *s_info,
33 bool on);
34/**
35 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
36 * @fimc: fimc device terminating the pipeline
37 *
38 * Caller holds the graph mutex.
39 */
40void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me)
41{
42 struct media_entity_graph graph;
43 struct v4l2_subdev *sd;
44
45 media_entity_graph_walk_start(&graph, me);
46
47 while ((me = media_entity_graph_walk_next(&graph))) {
48 if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV)
49 continue;
50 sd = media_entity_to_v4l2_subdev(me);
51
52 if (sd->grp_id == SENSOR_GROUP_ID)
53 fimc->pipeline.sensor = sd;
54 else if (sd->grp_id == CSIS_GROUP_ID)
55 fimc->pipeline.csis = sd;
56 }
57}
58
59/**
60 * __subdev_set_power - change power state of a single subdev
61 * @sd: subdevice to change power state for
62 * @on: 1 to enable power or 0 to disable
63 *
64 * Return result of s_power subdev operation or -ENXIO if sd argument
65 * is NULL. Return 0 if the subdevice does not implement s_power.
66 */
67static int __subdev_set_power(struct v4l2_subdev *sd, int on)
68{
69 int *use_count;
70 int ret;
71
72 if (sd == NULL)
73 return -ENXIO;
74
75 use_count = &sd->entity.use_count;
76 if (on && (*use_count)++ > 0)
77 return 0;
78 else if (!on && (*use_count == 0 || --(*use_count) > 0))
79 return 0;
80 ret = v4l2_subdev_call(sd, core, s_power, on);
81
82 return ret != -ENOIOCTLCMD ? ret : 0;
83}
84
85/**
86 * fimc_pipeline_s_power - change power state of all pipeline subdevs
87 * @fimc: fimc device terminating the pipeline
88 * @state: 1 to enable power or 0 for power down
89 *
90 * Need to be called with the graph mutex held.
91 */
92int fimc_pipeline_s_power(struct fimc_dev *fimc, int state)
93{
94 int ret = 0;
95
96 if (fimc->pipeline.sensor == NULL)
97 return -ENXIO;
98
99 if (state) {
100 ret = __subdev_set_power(fimc->pipeline.csis, 1);
101 if (ret && ret != -ENXIO)
102 return ret;
103 return __subdev_set_power(fimc->pipeline.sensor, 1);
104 }
105
106 ret = __subdev_set_power(fimc->pipeline.sensor, 0);
107 if (ret)
108 return ret;
109 ret = __subdev_set_power(fimc->pipeline.csis, 0);
110
111 return ret == -ENXIO ? 0 : ret;
112}
113
114/**
115 * __fimc_pipeline_initialize - update the pipeline information, enable power
116 * of all pipeline subdevs and the sensor clock
117 * @me: media entity to start graph walk with
118 * @prep: true to acquire sensor (and csis) subdevs
119 *
120 * This function must be called with the graph mutex held.
121 */
122static int __fimc_pipeline_initialize(struct fimc_dev *fimc,
123 struct media_entity *me, bool prep)
124{
125 int ret;
126
127 if (prep)
128 fimc_pipeline_prepare(fimc, me);
129 if (fimc->pipeline.sensor == NULL)
130 return -EINVAL;
131 ret = fimc_md_set_camclk(fimc->pipeline.sensor, true);
132 if (ret)
133 return ret;
134 return fimc_pipeline_s_power(fimc, 1);
135}
136
137int fimc_pipeline_initialize(struct fimc_dev *fimc, struct media_entity *me,
138 bool prep)
139{
140 int ret;
141
142 mutex_lock(&me->parent->graph_mutex);
143 ret = __fimc_pipeline_initialize(fimc, me, prep);
144 mutex_unlock(&me->parent->graph_mutex);
145
146 return ret;
147}
148
149/**
150 * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power
151 * @fimc: fimc device terminating the pipeline
152 *
153 * Disable power of all subdevs in the pipeline and turn off the external
154 * sensor clock.
155 * Called with the graph mutex held.
156 */
157int __fimc_pipeline_shutdown(struct fimc_dev *fimc)
158{
159 int ret = 0;
160
161 if (fimc->pipeline.sensor) {
162 ret = fimc_pipeline_s_power(fimc, 0);
163 fimc_md_set_camclk(fimc->pipeline.sensor, false);
164 }
165 return ret == -ENXIO ? 0 : ret;
166}
167
168int fimc_pipeline_shutdown(struct fimc_dev *fimc)
169{
170 struct media_entity *me = &fimc->vid_cap.vfd->entity;
171 int ret;
172
173 mutex_lock(&me->parent->graph_mutex);
174 ret = __fimc_pipeline_shutdown(fimc);
175 mutex_unlock(&me->parent->graph_mutex);
176
177 return ret;
178}
179
180/**
181 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
182 * @fimc: fimc device terminating the pipeline
183 * @on: passed as the s_stream call argument
184 */
185int fimc_pipeline_s_stream(struct fimc_dev *fimc, int on)
186{
187 struct fimc_pipeline *p = &fimc->pipeline;
188 int ret = 0;
189
190 if (p->sensor == NULL)
191 return -ENODEV;
192
193 if ((on && p->csis) || !on)
194 ret = v4l2_subdev_call(on ? p->csis : p->sensor,
195 video, s_stream, on);
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300196 if (ret < 0 && ret != -ENOIOCTLCMD)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300197 return ret;
198 if ((!on && p->csis) || on)
199 ret = v4l2_subdev_call(on ? p->sensor : p->csis,
200 video, s_stream, on);
201 return ret == -ENOIOCTLCMD ? 0 : ret;
202}
203
204/*
205 * Sensor subdevice helper functions
206 */
207static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
208 struct fimc_sensor_info *s_info)
209{
210 struct i2c_adapter *adapter;
211 struct v4l2_subdev *sd = NULL;
212
213 if (!s_info || !fmd)
214 return NULL;
215
216 adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300217 if (!adapter) {
218 v4l2_warn(&fmd->v4l2_dev,
219 "Failed to get I2C adapter %d, deferring probe\n",
220 s_info->pdata->i2c_bus_num);
221 return ERR_PTR(-EPROBE_DEFER);
222 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300223 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
224 s_info->pdata->board_info, NULL);
225 if (IS_ERR_OR_NULL(sd)) {
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300226 i2c_put_adapter(adapter);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300227 v4l2_warn(&fmd->v4l2_dev,
228 "Failed to acquire subdev %s, deferring probe\n",
229 s_info->pdata->board_info->type);
230 return ERR_PTR(-EPROBE_DEFER);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300231 }
232 v4l2_set_subdev_hostdata(sd, s_info);
233 sd->grp_id = SENSOR_GROUP_ID;
234
235 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
236 s_info->pdata->board_info->type);
237 return sd;
238}
239
240static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
241{
242 struct i2c_client *client = v4l2_get_subdevdata(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300243 struct i2c_adapter *adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300244
245 if (!client)
246 return;
247 v4l2_device_unregister_subdev(sd);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300248 adapter = client->adapter;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300249 i2c_unregister_device(client);
Sylwester Nawrocki7acde022011-09-13 14:07:09 -0300250 if (adapter)
251 i2c_put_adapter(adapter);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300252}
253
254static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
255{
256 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
257 struct fimc_dev *fd = NULL;
258 int num_clients, ret, i;
259
260 /*
261 * Runtime resume one of the FIMC entities to make sure
262 * the sclk_cam clocks are not globally disabled.
263 */
264 for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
265 if (fmd->fimc[i])
266 fd = fmd->fimc[i];
267 if (!fd)
268 return -ENXIO;
269 ret = pm_runtime_get_sync(&fd->pdev->dev);
270 if (ret < 0)
271 return ret;
272
273 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
274 num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor));
275
276 fmd->num_sensors = num_clients;
277 for (i = 0; i < num_clients; i++) {
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300278 struct v4l2_subdev *sd;
279
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300280 fmd->sensor[i].pdata = &pdata->isp_info[i];
281 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
282 if (ret)
283 break;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300284 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300285 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300286
287 if (!IS_ERR(sd)) {
288 fmd->sensor[i].subdev = sd;
289 } else {
290 fmd->sensor[i].subdev = NULL;
291 ret = PTR_ERR(sd);
292 break;
293 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300294 if (ret)
295 break;
296 }
297 pm_runtime_put(&fd->pdev->dev);
298 return ret;
299}
300
301/*
302 * MIPI CSIS and FIMC platform devices registration.
303 */
304static int fimc_register_callback(struct device *dev, void *p)
305{
306 struct fimc_dev *fimc = dev_get_drvdata(dev);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300307 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300308 struct fimc_md *fmd = p;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300309 int ret = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300310
311 if (!fimc || !fimc->pdev)
312 return 0;
313 if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
314 return 0;
315
316 fmd->fimc[fimc->pdev->id] = fimc;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300317 sd->grp_id = FIMC_GROUP_ID;
318
319 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
320 if (ret) {
321 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
322 fimc->id, ret);
323 }
324
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300325 return ret;
326}
327
328static int csis_register_callback(struct device *dev, void *p)
329{
330 struct v4l2_subdev *sd = dev_get_drvdata(dev);
331 struct platform_device *pdev;
332 struct fimc_md *fmd = p;
333 int id, ret;
334
335 if (!sd)
336 return 0;
337 pdev = v4l2_get_subdevdata(sd);
338 if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
339 return 0;
340 v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);
341
342 id = pdev->id < 0 ? 0 : pdev->id;
343 fmd->csis[id].sd = sd;
344 sd->grp_id = CSIS_GROUP_ID;
345 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
346 if (ret)
347 v4l2_err(&fmd->v4l2_dev,
348 "Failed to register CSIS subdevice: %d\n", ret);
349 return ret;
350}
351
352/**
353 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
354 */
355static int fimc_md_register_platform_entities(struct fimc_md *fmd)
356{
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300357 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300358 struct device_driver *driver;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300359 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300360
361 driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300362 if (!driver) {
363 v4l2_warn(&fmd->v4l2_dev,
364 "%s driver not found, deffering probe\n",
365 FIMC_MODULE_NAME);
366 return -EPROBE_DEFER;
367 }
368
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300369 ret = driver_for_each_device(driver, NULL, fmd,
370 fimc_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300371 if (ret)
372 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300373 /*
374 * Check if there is any sensor on the MIPI-CSI2 bus and
375 * if not skip the s5p-csis module loading.
376 */
Sylwester Nawrocki41df5bf2012-04-30 12:10:58 -0300377 if (pdata == NULL)
378 return 0;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300379 for (i = 0; i < pdata->num_clients; i++) {
380 if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) {
381 ret = 1;
382 break;
383 }
384 }
385 if (!ret)
386 return 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300387
388 driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300389 if (!driver || !try_module_get(driver->owner)) {
390 v4l2_warn(&fmd->v4l2_dev,
391 "%s driver not found, deffering probe\n",
392 CSIS_DRIVER_NAME);
393 return -EPROBE_DEFER;
394 }
395
396 return driver_for_each_device(driver, NULL, fmd,
397 csis_register_callback);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300398}
399
400static void fimc_md_unregister_entities(struct fimc_md *fmd)
401{
402 int i;
403
404 for (i = 0; i < FIMC_MAX_DEVS; i++) {
405 if (fmd->fimc[i] == NULL)
406 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300407 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300408 fmd->fimc[i] = NULL;
409 }
410 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
411 if (fmd->csis[i].sd == NULL)
412 continue;
413 v4l2_device_unregister_subdev(fmd->csis[i].sd);
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300414 module_put(fmd->csis[i].sd->owner);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300415 fmd->csis[i].sd = NULL;
416 }
417 for (i = 0; i < fmd->num_sensors; i++) {
418 if (fmd->sensor[i].subdev == NULL)
419 continue;
420 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
421 fmd->sensor[i].subdev = NULL;
422 }
423}
424
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300425/**
426 * __fimc_md_create_fimc_links - create links to all FIMC entities
427 * @fmd: fimc media device
428 * @source: the source entity to create links to all fimc entities from
429 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
430 * @pad: the source entity pad index
431 * @fimc_id: index of the fimc device for which link should be enabled
432 */
433static int __fimc_md_create_fimc_links(struct fimc_md *fmd,
434 struct media_entity *source,
435 struct v4l2_subdev *sensor,
436 int pad, int fimc_id)
437{
438 struct fimc_sensor_info *s_info;
439 struct media_entity *sink;
440 unsigned int flags;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300441 int ret, i;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300442
443 for (i = 0; i < FIMC_MAX_DEVS; i++) {
444 if (!fmd->fimc[i])
445 break;
446 /*
447 * Some FIMC variants are not fitted with camera capture
448 * interface. Skip creating a link from sensor for those.
449 */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300450 if (sensor->grp_id == SENSOR_GROUP_ID &&
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300451 !fmd->fimc[i]->variant->has_cam_if)
452 continue;
453
454 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300455 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300456 ret = media_entity_create_link(source, pad, sink,
457 FIMC_SD_PAD_SINK, flags);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300458 if (ret)
459 return ret;
460
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300461 /* Notify FIMC capture subdev entity */
462 ret = media_entity_call(sink, link_setup, &sink->pads[0],
463 &source->pads[pad], flags);
464 if (ret)
465 break;
466
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300467 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
468 source->name, flags ? '=' : '-', sink->name);
469
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300470 if (flags == 0)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300471 continue;
472 s_info = v4l2_get_subdev_hostdata(sensor);
473 if (!WARN_ON(s_info == NULL)) {
474 unsigned long irq_flags;
475 spin_lock_irqsave(&fmd->slock, irq_flags);
476 s_info->host = fmd->fimc[i];
477 spin_unlock_irqrestore(&fmd->slock, irq_flags);
478 }
479 }
480 return 0;
481}
482
483/**
484 * fimc_md_create_links - create default links between registered entities
485 *
486 * Parallel interface sensor entities are connected directly to FIMC capture
487 * entities. The sensors using MIPI CSIS bus are connected through immutable
488 * link with CSI receiver entity specified by mux_id. Any registered CSIS
489 * entity has a link to each registered FIMC capture entity. Enabled links
490 * are created by default between each subsequent registered sensor and
491 * subsequent FIMC capture entity. The number of default active links is
492 * determined by the number of available sensors or FIMC entities,
493 * whichever is less.
494 */
495static int fimc_md_create_links(struct fimc_md *fmd)
496{
497 struct v4l2_subdev *sensor, *csis;
498 struct s5p_fimc_isp_info *pdata;
499 struct fimc_sensor_info *s_info;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300500 struct media_entity *source, *sink;
501 int i, pad, fimc_id = 0;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300502 int ret = 0;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300503 u32 flags;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300504
505 for (i = 0; i < fmd->num_sensors; i++) {
506 if (fmd->sensor[i].subdev == NULL)
507 continue;
508
509 sensor = fmd->sensor[i].subdev;
510 s_info = v4l2_get_subdev_hostdata(sensor);
511 if (!s_info || !s_info->pdata)
512 continue;
513
514 source = NULL;
515 pdata = s_info->pdata;
516
517 switch (pdata->bus_type) {
518 case FIMC_MIPI_CSI2:
519 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
520 "Wrong CSI channel id: %d\n", pdata->mux_id))
521 return -EINVAL;
522
523 csis = fmd->csis[pdata->mux_id].sd;
524 if (WARN(csis == NULL,
525 "MIPI-CSI interface specified "
526 "but s5p-csis module is not loaded!\n"))
Sylwester Nawrockid12392e2011-10-20 05:15:59 -0300527 return -EINVAL;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300528
529 ret = media_entity_create_link(&sensor->entity, 0,
530 &csis->entity, CSIS_PAD_SINK,
531 MEDIA_LNK_FL_IMMUTABLE |
532 MEDIA_LNK_FL_ENABLED);
533 if (ret)
534 return ret;
535
536 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]",
537 sensor->entity.name, csis->entity.name);
538
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300539 source = &csis->entity;
540 pad = CSIS_PAD_SOURCE;
541 break;
542
543 case FIMC_ITU_601...FIMC_ITU_656:
544 source = &sensor->entity;
545 pad = 0;
546 break;
547
548 default:
549 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
550 pdata->bus_type);
551 return -EINVAL;
552 }
553 if (source == NULL)
554 continue;
555
556 ret = __fimc_md_create_fimc_links(fmd, source, sensor, pad,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300557 fimc_id++);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300558 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300559 /* Create immutable links between each FIMC's subdev and video node */
560 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
561 for (i = 0; i < FIMC_MAX_DEVS; i++) {
562 if (!fmd->fimc[i])
563 continue;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300564 source = &fmd->fimc[i]->vid_cap.subdev.entity;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300565 sink = &fmd->fimc[i]->vid_cap.vfd->entity;
566 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
567 sink, 0, flags);
568 if (ret)
569 break;
570 }
571
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300572 return ret;
573}
574
575/*
576 * The peripheral sensor clock management.
577 */
578static int fimc_md_get_clocks(struct fimc_md *fmd)
579{
580 char clk_name[32];
581 struct clk *clock;
582 int i;
583
584 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
585 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
586 clock = clk_get(NULL, clk_name);
587 if (IS_ERR_OR_NULL(clock)) {
588 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
589 clk_name);
590 return -ENXIO;
591 }
592 fmd->camclk[i].clock = clock;
593 }
594 return 0;
595}
596
597static void fimc_md_put_clocks(struct fimc_md *fmd)
598{
599 int i = FIMC_MAX_CAMCLKS;
600
601 while (--i >= 0) {
602 if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
603 continue;
604 clk_put(fmd->camclk[i].clock);
605 fmd->camclk[i].clock = NULL;
606 }
607}
608
609static int __fimc_md_set_camclk(struct fimc_md *fmd,
610 struct fimc_sensor_info *s_info,
611 bool on)
612{
613 struct s5p_fimc_isp_info *pdata = s_info->pdata;
614 struct fimc_camclk_info *camclk;
615 int ret = 0;
616
617 if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
618 return -EINVAL;
619
620 if (s_info->clk_on == on)
621 return 0;
622 camclk = &fmd->camclk[pdata->clk_id];
623
624 dbg("camclk %d, f: %lu, clk: %p, on: %d",
625 pdata->clk_id, pdata->clk_frequency, camclk, on);
626
627 if (on) {
628 if (camclk->use_count > 0 &&
629 camclk->frequency != pdata->clk_frequency)
630 return -EINVAL;
631
632 if (camclk->use_count++ == 0) {
633 clk_set_rate(camclk->clock, pdata->clk_frequency);
634 camclk->frequency = pdata->clk_frequency;
635 ret = clk_enable(camclk->clock);
636 }
637 s_info->clk_on = 1;
638 dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
639 clk_get_rate(camclk->clock));
640
641 return ret;
642 }
643
644 if (WARN_ON(camclk->use_count == 0))
645 return 0;
646
647 if (--camclk->use_count == 0) {
648 clk_disable(camclk->clock);
649 s_info->clk_on = 0;
650 dbg("Disabled camclk %d", pdata->clk_id);
651 }
652 return ret;
653}
654
655/**
656 * fimc_md_set_camclk - peripheral sensor clock setup
657 * @sd: sensor subdev to configure sclk_cam clock for
658 * @on: 1 to enable or 0 to disable the clock
659 *
660 * There are 2 separate clock outputs available in the SoC for external
661 * image processors. These clocks are shared between all registered FIMC
662 * devices to which sensors can be attached, either directly or through
663 * the MIPI CSI receiver. The clock is allowed here to be used by
664 * multiple sensors concurrently if they use same frequency.
665 * The per sensor subdev clk_on attribute helps to synchronize accesses
666 * to the sclk_cam clocks from the video and media device nodes.
667 * This function should only be called when the graph mutex is held.
668 */
669int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
670{
671 struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
672 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
673
674 return __fimc_md_set_camclk(fmd, s_info, on);
675}
676
677static int fimc_md_link_notify(struct media_pad *source,
678 struct media_pad *sink, u32 flags)
679{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300680 struct v4l2_subdev *sd;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300681 struct fimc_dev *fimc;
682 int ret = 0;
683
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300684 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300685 return 0;
686
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300687 sd = media_entity_to_v4l2_subdev(sink->entity);
688 fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300689
690 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
691 ret = __fimc_pipeline_shutdown(fimc);
692 fimc->pipeline.sensor = NULL;
693 fimc->pipeline.csis = NULL;
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300694
695 mutex_lock(&fimc->lock);
696 fimc_ctrls_delete(fimc->vid_cap.ctx);
697 mutex_unlock(&fimc->lock);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300698 return ret;
699 }
700 /*
701 * Link activation. Enable power of pipeline elements only if the
702 * pipeline is already in use, i.e. its video node is opened.
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300703 * Recreate the controls destroyed during the link deactivation.
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300704 */
705 mutex_lock(&fimc->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300706 if (fimc->vid_cap.refcnt > 0) {
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300707 ret = __fimc_pipeline_initialize(fimc, source->entity, true);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300708 if (!ret)
709 ret = fimc_capture_ctrls_create(fimc);
710 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300711 mutex_unlock(&fimc->lock);
712
713 return ret ? -EPIPE : ret;
714}
715
716static ssize_t fimc_md_sysfs_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
718{
719 struct platform_device *pdev = to_platform_device(dev);
720 struct fimc_md *fmd = platform_get_drvdata(pdev);
721
722 if (fmd->user_subdev_api)
723 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
724
725 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
726}
727
728static ssize_t fimc_md_sysfs_store(struct device *dev,
729 struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 struct platform_device *pdev = to_platform_device(dev);
733 struct fimc_md *fmd = platform_get_drvdata(pdev);
734 bool subdev_api;
735 int i;
736
737 if (!strcmp(buf, "vid-dev\n"))
738 subdev_api = false;
739 else if (!strcmp(buf, "sub-dev\n"))
740 subdev_api = true;
741 else
742 return count;
743
744 fmd->user_subdev_api = subdev_api;
745 for (i = 0; i < FIMC_MAX_DEVS; i++)
746 if (fmd->fimc[i])
747 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
748 return count;
749}
750/*
751 * This device attribute is to select video pipeline configuration method.
752 * There are following valid values:
753 * vid-dev - for V4L2 video node API only, subdevice will be configured
754 * by the host driver.
755 * sub-dev - for media controller API, subdevs must be configured in user
756 * space before starting streaming.
757 */
758static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
759 fimc_md_sysfs_show, fimc_md_sysfs_store);
760
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300761static int fimc_md_probe(struct platform_device *pdev)
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300762{
763 struct v4l2_device *v4l2_dev;
764 struct fimc_md *fmd;
765 int ret;
766
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300767 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300768 if (!fmd)
769 return -ENOMEM;
770
771 spin_lock_init(&fmd->slock);
772 fmd->pdev = pdev;
773
774 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
775 sizeof(fmd->media_dev.model));
776 fmd->media_dev.link_notify = fimc_md_link_notify;
777 fmd->media_dev.dev = &pdev->dev;
778
779 v4l2_dev = &fmd->v4l2_dev;
780 v4l2_dev->mdev = &fmd->media_dev;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -0300781 v4l2_dev->notify = fimc_sensor_notify;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300782 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
783 dev_name(&pdev->dev));
784
785 ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
786 if (ret < 0) {
787 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
Sylwester Nawrocki6d91a512012-01-30 11:37:59 -0300788 return ret;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300789 }
790 ret = media_device_register(&fmd->media_dev);
791 if (ret < 0) {
792 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300793 goto err_md;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300794 }
795 ret = fimc_md_get_clocks(fmd);
796 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300797 goto err_clk;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300798
799 fmd->user_subdev_api = false;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300800
801 /* Protect the media graph while we're registering entities */
802 mutex_lock(&fmd->media_dev.graph_mutex);
803
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300804 ret = fimc_md_register_platform_entities(fmd);
805 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300806 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300807
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300808 if (pdev->dev.platform_data) {
809 ret = fimc_md_register_sensor_entities(fmd);
810 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300811 goto err_unlock;
Sylwester Nawrocki5cbf6f162011-10-07 07:40:00 -0300812 }
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300813 ret = fimc_md_create_links(fmd);
814 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300815 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300816 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
817 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300818 goto err_unlock;
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300819
820 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300821 if (ret)
822 goto err_unlock;
823
824 platform_set_drvdata(pdev, fmd);
825 mutex_unlock(&fmd->media_dev.graph_mutex);
826 return 0;
827
828err_unlock:
829 mutex_unlock(&fmd->media_dev.graph_mutex);
830err_clk:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300831 media_device_unregister(&fmd->media_dev);
832 fimc_md_put_clocks(fmd);
833 fimc_md_unregister_entities(fmd);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300834err_md:
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300835 v4l2_device_unregister(&fmd->v4l2_dev);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300836 return ret;
837}
838
839static int __devexit fimc_md_remove(struct platform_device *pdev)
840{
841 struct fimc_md *fmd = platform_get_drvdata(pdev);
842
843 if (!fmd)
844 return 0;
845 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
846 fimc_md_unregister_entities(fmd);
847 media_device_unregister(&fmd->media_dev);
848 fimc_md_put_clocks(fmd);
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300849 return 0;
850}
851
852static struct platform_driver fimc_md_driver = {
853 .probe = fimc_md_probe,
854 .remove = __devexit_p(fimc_md_remove),
855 .driver = {
856 .name = "s5p-fimc-md",
857 .owner = THIS_MODULE,
858 }
859};
860
861int __init fimc_md_init(void)
862{
863 int ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300864
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300865 request_module("s5p-csis");
866 ret = fimc_register_driver();
867 if (ret)
868 return ret;
Sylwester Nawrockiecd9acb2012-03-21 09:58:09 -0300869
Sylwester Nawrockid3953222011-09-01 06:01:08 -0300870 return platform_driver_register(&fimc_md_driver);
871}
872void __exit fimc_md_exit(void)
873{
874 platform_driver_unregister(&fimc_md_driver);
875 fimc_unregister_driver();
876}
877
878module_init(fimc_md_init);
879module_exit(fimc_md_exit);
880
881MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
882MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
883MODULE_LICENSE("GPL");
884MODULE_VERSION("2.0.1");