]>
Commit | Line | Data |
---|---|---|
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" | |
28 | #include "fimc-mdevice.h" | |
29 | #include "mipi-csis.h" | |
30 | ||
31 | static 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 | */ | |
40 | void 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 | */ | |
67 | static 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 | */ | |
92 | int 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 | */ | |
122 | static 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 | ||
137 | int 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 | */ | |
157 | int __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 | ||
168 | int 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 | */ | |
185 | int 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); | |
cf52df8a | 196 | if (ret < 0 && ret != -ENOIOCTLCMD) |
d3953223 SN |
197 | 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 | */ | |
207 | static 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); | |
ecd9acbf SN |
217 | 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 | } | |
d3953223 SN |
223 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, |
224 | s_info->pdata->board_info, NULL); | |
225 | if (IS_ERR_OR_NULL(sd)) { | |
7acde02a | 226 | i2c_put_adapter(adapter); |
ecd9acbf SN |
227 | 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); | |
d3953223 SN |
231 | } |
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 | ||
240 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) | |
241 | { | |
242 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
7acde02a | 243 | struct i2c_adapter *adapter; |
d3953223 SN |
244 | |
245 | if (!client) | |
246 | return; | |
247 | v4l2_device_unregister_subdev(sd); | |
7acde02a | 248 | adapter = client->adapter; |
d3953223 | 249 | i2c_unregister_device(client); |
7acde02a SN |
250 | if (adapter) |
251 | i2c_put_adapter(adapter); | |
d3953223 SN |
252 | } |
253 | ||
254 | static 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++) { | |
ecd9acbf SN |
278 | struct v4l2_subdev *sd; |
279 | ||
d3953223 SN |
280 | fmd->sensor[i].pdata = &pdata->isp_info[i]; |
281 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); | |
282 | if (ret) | |
283 | break; | |
ecd9acbf | 284 | sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]); |
d3953223 | 285 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); |
ecd9acbf SN |
286 | |
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 | } | |
d3953223 SN |
294 | 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 | */ | |
304 | static int fimc_register_callback(struct device *dev, void *p) | |
305 | { | |
306 | struct fimc_dev *fimc = dev_get_drvdata(dev); | |
693f5c40 | 307 | struct v4l2_subdev *sd = &fimc->vid_cap.subdev; |
d3953223 | 308 | struct fimc_md *fmd = p; |
693f5c40 | 309 | int ret = 0; |
d3953223 SN |
310 | |
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; | |
693f5c40 SN |
317 | 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 | ||
d3953223 SN |
325 | return ret; |
326 | } | |
327 | ||
328 | static 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 | */ | |
355 | static int fimc_md_register_platform_entities(struct fimc_md *fmd) | |
356 | { | |
ecd9acbf | 357 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; |
d3953223 | 358 | struct device_driver *driver; |
ecd9acbf | 359 | int ret, i; |
d3953223 SN |
360 | |
361 | driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type); | |
ecd9acbf SN |
362 | 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 | ||
d3953223 SN |
369 | ret = driver_for_each_device(driver, NULL, fmd, |
370 | fimc_register_callback); | |
d3953223 SN |
371 | if (ret) |
372 | return ret; | |
ecd9acbf SN |
373 | /* |
374 | * Check if there is any sensor on the MIPI-CSI2 bus and | |
375 | * if not skip the s5p-csis module loading. | |
376 | */ | |
41df5bf0 SN |
377 | if (pdata == NULL) |
378 | return 0; | |
ecd9acbf SN |
379 | 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; | |
d3953223 SN |
387 | |
388 | driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type); | |
ecd9acbf SN |
389 | 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); | |
d3953223 SN |
398 | } |
399 | ||
400 | static 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; | |
693f5c40 | 407 | v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev); |
d3953223 SN |
408 | 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); | |
ecd9acbf | 414 | module_put(fmd->csis[i].sd->owner); |
d3953223 SN |
415 | 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 | ||
d3953223 SN |
425 | /** |
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 | */ | |
433 | static 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; | |
237e0265 | 441 | int ret, i; |
d3953223 SN |
442 | |
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 | */ | |
237e0265 | 450 | if (sensor->grp_id == SENSOR_GROUP_ID && |
d3953223 SN |
451 | !fmd->fimc[i]->variant->has_cam_if) |
452 | continue; | |
453 | ||
454 | flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0; | |
693f5c40 | 455 | sink = &fmd->fimc[i]->vid_cap.subdev.entity; |
237e0265 SN |
456 | ret = media_entity_create_link(source, pad, sink, |
457 | FIMC_SD_PAD_SINK, flags); | |
d3953223 SN |
458 | if (ret) |
459 | return ret; | |
460 | ||
237e0265 SN |
461 | /* 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 | ||
d3953223 SN |
467 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
468 | source->name, flags ? '=' : '-', sink->name); | |
469 | ||
237e0265 | 470 | if (flags == 0) |
d3953223 SN |
471 | 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 | */ | |
495 | static 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; | |
237e0265 SN |
500 | struct media_entity *source, *sink; |
501 | int i, pad, fimc_id = 0; | |
d3953223 | 502 | int ret = 0; |
237e0265 | 503 | u32 flags; |
d3953223 SN |
504 | |
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")) | |
d12392ec | 527 | return -EINVAL; |
d3953223 SN |
528 | |
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 | ||
d3953223 SN |
539 | 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, | |
237e0265 | 557 | fimc_id++); |
d3953223 | 558 | } |
237e0265 SN |
559 | /* 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; | |
693f5c40 | 564 | source = &fmd->fimc[i]->vid_cap.subdev.entity; |
237e0265 SN |
565 | 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 | ||
d3953223 SN |
572 | return ret; |
573 | } | |
574 | ||
575 | /* | |
576 | * The peripheral sensor clock management. | |
577 | */ | |
578 | static 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 | ||
597 | static 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 | ||
609 | static 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 | */ | |
669 | int 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 | ||
677 | static int fimc_md_link_notify(struct media_pad *source, | |
678 | struct media_pad *sink, u32 flags) | |
679 | { | |
237e0265 | 680 | struct v4l2_subdev *sd; |
d3953223 SN |
681 | struct fimc_dev *fimc; |
682 | int ret = 0; | |
683 | ||
237e0265 | 684 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
d3953223 SN |
685 | return 0; |
686 | ||
237e0265 SN |
687 | sd = media_entity_to_v4l2_subdev(sink->entity); |
688 | fimc = v4l2_get_subdevdata(sd); | |
d3953223 SN |
689 | |
690 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { | |
691 | ret = __fimc_pipeline_shutdown(fimc); | |
692 | fimc->pipeline.sensor = NULL; | |
693 | fimc->pipeline.csis = NULL; | |
131b6c61 SN |
694 | |
695 | mutex_lock(&fimc->lock); | |
696 | fimc_ctrls_delete(fimc->vid_cap.ctx); | |
697 | mutex_unlock(&fimc->lock); | |
d3953223 SN |
698 | 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. | |
131b6c61 | 703 | * Recreate the controls destroyed during the link deactivation. |
d3953223 SN |
704 | */ |
705 | mutex_lock(&fimc->lock); | |
131b6c61 | 706 | if (fimc->vid_cap.refcnt > 0) { |
d3953223 | 707 | ret = __fimc_pipeline_initialize(fimc, source->entity, true); |
131b6c61 SN |
708 | if (!ret) |
709 | ret = fimc_capture_ctrls_create(fimc); | |
710 | } | |
d3953223 SN |
711 | mutex_unlock(&fimc->lock); |
712 | ||
713 | return ret ? -EPIPE : ret; | |
714 | } | |
715 | ||
716 | static 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 | ||
728 | static 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 | */ | |
758 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, | |
759 | fimc_md_sysfs_show, fimc_md_sysfs_store); | |
760 | ||
ecd9acbf | 761 | static int fimc_md_probe(struct platform_device *pdev) |
d3953223 SN |
762 | { |
763 | struct v4l2_device *v4l2_dev; | |
764 | struct fimc_md *fmd; | |
765 | int ret; | |
766 | ||
6d91a51a | 767 | fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL); |
d3953223 SN |
768 | 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; | |
e1d72f4d | 781 | v4l2_dev->notify = fimc_sensor_notify; |
d3953223 SN |
782 | 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); | |
6d91a51a | 788 | return ret; |
d3953223 SN |
789 | } |
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); | |
693f5c40 | 793 | goto err_md; |
d3953223 SN |
794 | } |
795 | ret = fimc_md_get_clocks(fmd); | |
796 | if (ret) | |
693f5c40 | 797 | goto err_clk; |
d3953223 SN |
798 | |
799 | fmd->user_subdev_api = false; | |
693f5c40 SN |
800 | |
801 | /* Protect the media graph while we're registering entities */ | |
802 | mutex_lock(&fmd->media_dev.graph_mutex); | |
803 | ||
d3953223 SN |
804 | ret = fimc_md_register_platform_entities(fmd); |
805 | if (ret) | |
693f5c40 | 806 | goto err_unlock; |
d3953223 | 807 | |
5cbf6f16 SN |
808 | if (pdev->dev.platform_data) { |
809 | ret = fimc_md_register_sensor_entities(fmd); | |
810 | if (ret) | |
693f5c40 | 811 | goto err_unlock; |
5cbf6f16 | 812 | } |
d3953223 SN |
813 | ret = fimc_md_create_links(fmd); |
814 | if (ret) | |
693f5c40 | 815 | goto err_unlock; |
d3953223 SN |
816 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
817 | if (ret) | |
693f5c40 | 818 | goto err_unlock; |
d3953223 SN |
819 | |
820 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); | |
693f5c40 SN |
821 | 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 | ||
828 | err_unlock: | |
829 | mutex_unlock(&fmd->media_dev.graph_mutex); | |
830 | err_clk: | |
d3953223 SN |
831 | media_device_unregister(&fmd->media_dev); |
832 | fimc_md_put_clocks(fmd); | |
833 | fimc_md_unregister_entities(fmd); | |
693f5c40 | 834 | err_md: |
d3953223 | 835 | v4l2_device_unregister(&fmd->v4l2_dev); |
d3953223 SN |
836 | return ret; |
837 | } | |
838 | ||
839 | static 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); | |
d3953223 SN |
849 | return 0; |
850 | } | |
851 | ||
852 | static 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 | ||
861 | int __init fimc_md_init(void) | |
862 | { | |
863 | int ret; | |
ecd9acbf | 864 | |
d3953223 SN |
865 | request_module("s5p-csis"); |
866 | ret = fimc_register_driver(); | |
867 | if (ret) | |
868 | return ret; | |
ecd9acbf | 869 | |
d3953223 SN |
870 | return platform_driver_register(&fimc_md_driver); |
871 | } | |
872 | void __exit fimc_md_exit(void) | |
873 | { | |
874 | platform_driver_unregister(&fimc_md_driver); | |
875 | fimc_unregister_driver(); | |
876 | } | |
877 | ||
878 | module_init(fimc_md_init); | |
879 | module_exit(fimc_md_exit); | |
880 | ||
881 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); | |
882 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); | |
883 | MODULE_LICENSE("GPL"); | |
884 | MODULE_VERSION("2.0.1"); |