[media] videobuf-dma-contig: use gfp_t for GFP flags
[deliverable/linux.git] / drivers / media / video / s5p-fimc / fimc-mdevice.c
CommitLineData
d3953223
SN
1/*
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>
131b6c61 24#include <media/v4l2-ctrls.h>
d3953223
SN
25#include <media/media-device.h>
26
27#include "fimc-core.h"
0f735f52 28#include "fimc-lite.h"
d3953223
SN
29#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 */
0f735f52 41void fimc_pipeline_prepare(struct fimc_pipeline *p, struct media_entity *me)
d3953223 42{
0f735f52 43 struct media_pad *pad = &me->pads[0];
d3953223 44 struct v4l2_subdev *sd;
0f735f52 45 int i;
d3953223 46
0f735f52
SN
47 for (i = 0; i < IDX_MAX; i++)
48 p->subdevs[i] = NULL;
d3953223 49
0f735f52
SN
50 while (1) {
51 if (!(pad->flags & MEDIA_PAD_FL_SINK))
52 break;
53
54 /* 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;
d3953223 59
0f735f52
SN
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;
4af81310
SN
69 case FLITE_GROUP_ID:
70 p->subdevs[IDX_FLITE] = sd;
71 break;
0f735f52
SN
72 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];
d3953223
SN
81 }
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
0f735f52 113 * @state: true to power on, false to power off
d3953223 114 *
0f735f52 115 * Needs to be called with the graph mutex held.
d3953223 116 */
0f735f52 117int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
d3953223 118{
0f735f52
SN
119 unsigned int i;
120 int ret;
d3953223 121
0f735f52 122 if (p->subdevs[IDX_SENSOR] == NULL)
d3953223
SN
123 return -ENXIO;
124
0f735f52
SN
125 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)
d3953223 130 return ret;
d3953223
SN
131 }
132
0f735f52 133 return 0;
d3953223
SN
134}
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 */
0f735f52 144static int __fimc_pipeline_initialize(struct fimc_pipeline *p,
d3953223
SN
145 struct media_entity *me, bool prep)
146{
147 int ret;
148
149 if (prep)
0f735f52
SN
150 fimc_pipeline_prepare(p, me);
151
152 if (p->subdevs[IDX_SENSOR] == NULL)
d3953223 153 return -EINVAL;
0f735f52
SN
154
155 ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
d3953223
SN
156 if (ret)
157 return ret;
0f735f52
SN
158
159 return fimc_pipeline_s_power(p, 1);
d3953223
SN
160}
161
0f735f52 162int fimc_pipeline_initialize(struct fimc_pipeline *p, struct media_entity *me,
d3953223
SN
163 bool prep)
164{
165 int ret;
166
167 mutex_lock(&me->parent->graph_mutex);
0f735f52 168 ret = __fimc_pipeline_initialize(p, me, prep);
d3953223
SN
169 mutex_unlock(&me->parent->graph_mutex);
170
171 return ret;
172}
0f735f52 173EXPORT_SYMBOL_GPL(fimc_pipeline_initialize);
d3953223
SN
174
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 */
0f735f52 183int __fimc_pipeline_shutdown(struct fimc_pipeline *p)
d3953223
SN
184{
185 int ret = 0;
186
0f735f52
SN
187 if (p->subdevs[IDX_SENSOR]) {
188 ret = fimc_pipeline_s_power(p, 0);
189 fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
d3953223
SN
190 }
191 return ret == -ENXIO ? 0 : ret;
192}
193
0f735f52 194int fimc_pipeline_shutdown(struct fimc_pipeline *p)
d3953223 195{
0f735f52 196 struct media_entity *me = &p->subdevs[IDX_SENSOR]->entity;
d3953223
SN
197 int ret;
198
199 mutex_lock(&me->parent->graph_mutex);
0f735f52 200 ret = __fimc_pipeline_shutdown(p);
d3953223
SN
201 mutex_unlock(&me->parent->graph_mutex);
202
203 return ret;
204}
0f735f52 205EXPORT_SYMBOL_GPL(fimc_pipeline_shutdown);
d3953223
SN
206
207/**
208 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
0f735f52 209 * @pipeline: video pipeline structure
d3953223
SN
210 * @on: passed as the s_stream call argument
211 */
0f735f52 212int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
d3953223 213{
0f735f52 214 int i, ret;
d3953223 215
0f735f52 216 if (p->subdevs[IDX_SENSOR] == NULL)
d3953223
SN
217 return -ENODEV;
218
0f735f52
SN
219 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
d3953223 230}
0f735f52 231EXPORT_SYMBOL_GPL(fimc_pipeline_s_stream);
d3953223
SN
232
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);
ecd9acbf
SN
246 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 }
d3953223
SN
252 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
253 s_info->pdata->board_info, NULL);
254 if (IS_ERR_OR_NULL(sd)) {
7acde02a 255 i2c_put_adapter(adapter);
ecd9acbf
SN
256 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);
d3953223
SN
260 }
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);
7acde02a 272 struct i2c_adapter *adapter;
d3953223
SN
273
274 if (!client)
275 return;
276 v4l2_device_unregister_subdev(sd);
7acde02a 277 adapter = client->adapter;
d3953223 278 i2c_unregister_device(client);
7acde02a
SN
279 if (adapter)
280 i2c_put_adapter(adapter);
d3953223
SN
281}
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++) {
ecd9acbf
SN
307 struct v4l2_subdev *sd;
308
d3953223
SN
309 fmd->sensor[i].pdata = &pdata->isp_info[i];
310 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
311 if (ret)
312 break;
ecd9acbf 313 sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
d3953223 314 ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
ecd9acbf
SN
315
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 }
d3953223
SN
323 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);
693f5c40 336 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
d3953223 337 struct fimc_md *fmd = p;
693f5c40 338 int ret = 0;
d3953223
SN
339
340 if (!fimc || !fimc->pdev)
341 return 0;
4af81310 342
d3953223
SN
343 if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
344 return 0;
345
346 fmd->fimc[fimc->pdev->id] = fimc;
693f5c40
SN
347 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
d3953223
SN
355 return ret;
356}
357
4af81310
SN
358static 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
d3953223
SN
383static 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{
ecd9acbf 412 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
d3953223 413 struct device_driver *driver;
ecd9acbf 414 int ret, i;
d3953223
SN
415
416 driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
ecd9acbf
SN
417 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
d3953223
SN
424 ret = driver_for_each_device(driver, NULL, fmd,
425 fimc_register_callback);
d3953223
SN
426 if (ret)
427 return ret;
4af81310
SN
428
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 }
ecd9acbf
SN
437 /*
438 * Check if there is any sensor on the MIPI-CSI2 bus and
439 * if not skip the s5p-csis module loading.
440 */
41df5bf0
SN
441 if (pdata == NULL)
442 return 0;
ecd9acbf
SN
443 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;
d3953223
SN
451
452 driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
ecd9acbf
SN
453 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);
d3953223
SN
462}
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;
693f5c40 471 v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
d3953223
SN
472 fmd->fimc[i] = NULL;
473 }
4af81310
SN
474 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 }
d3953223
SN
480 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);
ecd9acbf 484 module_put(fmd->csis[i].sd->owner);
d3953223
SN
485 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
d3953223
SN
495/**
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 */
4af81310
SN
503static 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)
d3953223
SN
507{
508 struct fimc_sensor_info *s_info;
509 struct media_entity *sink;
4af81310 510 unsigned int flags = 0;
237e0265 511 int ret, i;
d3953223
SN
512
513 for (i = 0; i < FIMC_MAX_DEVS; i++) {
514 if (!fmd->fimc[i])
4af81310 515 continue;
d3953223
SN
516 /*
517 * Some FIMC variants are not fitted with camera capture
518 * interface. Skip creating a link from sensor for those.
519 */
4af81310 520 if (!fmd->fimc[i]->variant->has_cam_if)
d3953223
SN
521 continue;
522
523 flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0;
4af81310 524
693f5c40 525 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
237e0265
SN
526 ret = media_entity_create_link(source, pad, sink,
527 FIMC_SD_PAD_SINK, flags);
d3953223
SN
528 if (ret)
529 return ret;
530
237e0265
SN
531 /* 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
d3953223
SN
537 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
538 source->name, flags ? '=' : '-', sink->name);
539
4af81310 540 if (flags == 0 || sensor == NULL)
d3953223
SN
541 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 }
4af81310
SN
550
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 }
d3953223
SN
572 return 0;
573}
574
4af81310
SN
575/* 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
d3953223
SN
599/**
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;
237e0265
SN
616 struct media_entity *source, *sink;
617 int i, pad, fimc_id = 0;
d3953223 618 int ret = 0;
237e0265 619 u32 flags;
d3953223
SN
620
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"))
d12392ec 643 return -EINVAL;
d3953223
SN
644
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
4af81310 655 source = NULL;
d3953223
SN
656 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
4af81310
SN
671 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
672 pad, fimc_id++);
673 }
674
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++);
d3953223 684 }
4af81310 685
237e0265
SN
686 /* 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;
693f5c40 691 source = &fmd->fimc[i]->vid_cap.subdev.entity;
237e0265
SN
692 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
4af81310 699 return __fimc_md_create_flite_source_links(fmd);
d3953223
SN
700}
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{
4af81310
SN
807 struct fimc_lite *fimc_lite = NULL;
808 struct fimc_dev *fimc = NULL;
0f735f52 809 struct fimc_pipeline *pipeline;
237e0265 810 struct v4l2_subdev *sd;
d3953223
SN
811 int ret = 0;
812
237e0265 813 if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
d3953223
SN
814 return 0;
815
237e0265 816 sd = media_entity_to_v4l2_subdev(sink->entity);
d3953223 817
0f735f52 818 switch (sd->grp_id) {
4af81310
SN
819 case FLITE_GROUP_ID:
820 fimc_lite = v4l2_get_subdevdata(sd);
821 pipeline = &fimc_lite->pipeline;
822 break;
0f735f52
SN
823 case FIMC_GROUP_ID:
824 fimc = v4l2_get_subdevdata(sd);
825 pipeline = &fimc->pipeline;
826 break;
827 default:
828 return 0;
829 }
131b6c61 830
0f735f52
SN
831 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
832 ret = __fimc_pipeline_shutdown(pipeline);
833 pipeline->subdevs[IDX_SENSOR] = NULL;
834 pipeline->subdevs[IDX_CSIS] = NULL;
835
836 if (fimc) {
837 mutex_lock(&fimc->lock);
838 fimc_ctrls_delete(fimc->vid_cap.ctx);
839 mutex_unlock(&fimc->lock);
840 }
d3953223
SN
841 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.
131b6c61 846 * Recreate the controls destroyed during the link deactivation.
d3953223 847 */
4af81310
SN
848 if (fimc) {
849 mutex_lock(&fimc->lock);
850 if (fimc->vid_cap.refcnt > 0) {
0f735f52
SN
851 ret = __fimc_pipeline_initialize(pipeline,
852 source->entity, true);
131b6c61
SN
853 if (!ret)
854 ret = fimc_capture_ctrls_create(fimc);
4af81310
SN
855 }
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);
131b6c61 864 }
d3953223
SN
865 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
ecd9acbf 913static int fimc_md_probe(struct platform_device *pdev)
d3953223
SN
914{
915 struct v4l2_device *v4l2_dev;
916 struct fimc_md *fmd;
917 int ret;
918
6d91a51a 919 fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
d3953223
SN
920 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;
e1d72f4d 933 v4l2_dev->notify = fimc_sensor_notify;
d3953223
SN
934 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);
6d91a51a 940 return ret;
d3953223
SN
941 }
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);
693f5c40 945 goto err_md;
d3953223
SN
946 }
947 ret = fimc_md_get_clocks(fmd);
948 if (ret)
693f5c40 949 goto err_clk;
d3953223
SN
950
951 fmd->user_subdev_api = false;
693f5c40
SN
952
953 /* Protect the media graph while we're registering entities */
954 mutex_lock(&fmd->media_dev.graph_mutex);
955
d3953223
SN
956 ret = fimc_md_register_platform_entities(fmd);
957 if (ret)
693f5c40 958 goto err_unlock;
d3953223 959
5cbf6f16
SN
960 if (pdev->dev.platform_data) {
961 ret = fimc_md_register_sensor_entities(fmd);
962 if (ret)
693f5c40 963 goto err_unlock;
5cbf6f16 964 }
d3953223
SN
965 ret = fimc_md_create_links(fmd);
966 if (ret)
693f5c40 967 goto err_unlock;
d3953223
SN
968 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
969 if (ret)
693f5c40 970 goto err_unlock;
d3953223
SN
971
972 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
693f5c40
SN
973 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:
d3953223
SN
983 media_device_unregister(&fmd->media_dev);
984 fimc_md_put_clocks(fmd);
985 fimc_md_unregister_entities(fmd);
693f5c40 986err_md:
d3953223 987 v4l2_device_unregister(&fmd->v4l2_dev);
d3953223
SN
988 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);
d3953223
SN
1001 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;
ecd9acbf 1016
d3953223
SN
1017 request_module("s5p-csis");
1018 ret = fimc_register_driver();
1019 if (ret)
1020 return ret;
ecd9acbf 1021
d3953223
SN
1022 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");
This page took 0.115274 seconds and 5 git commands to generate.