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> | |
24 | #include <linux/version.h> | |
131b6c61 | 25 | #include <media/v4l2-ctrls.h> |
d3953223 SN |
26 | #include <media/media-device.h> |
27 | ||
28 | #include "fimc-core.h" | |
29 | #include "fimc-mdevice.h" | |
30 | #include "mipi-csis.h" | |
31 | ||
32 | static 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 | */ | |
41 | void fimc_pipeline_prepare(struct fimc_dev *fimc, struct media_entity *me) | |
42 | { | |
43 | struct media_entity_graph graph; | |
44 | struct v4l2_subdev *sd; | |
45 | ||
46 | media_entity_graph_walk_start(&graph, me); | |
47 | ||
48 | while ((me = media_entity_graph_walk_next(&graph))) { | |
49 | if (media_entity_type(me) != MEDIA_ENT_T_V4L2_SUBDEV) | |
50 | continue; | |
51 | sd = media_entity_to_v4l2_subdev(me); | |
52 | ||
53 | if (sd->grp_id == SENSOR_GROUP_ID) | |
54 | fimc->pipeline.sensor = sd; | |
55 | else if (sd->grp_id == CSIS_GROUP_ID) | |
56 | fimc->pipeline.csis = sd; | |
57 | } | |
58 | } | |
59 | ||
60 | /** | |
61 | * __subdev_set_power - change power state of a single subdev | |
62 | * @sd: subdevice to change power state for | |
63 | * @on: 1 to enable power or 0 to disable | |
64 | * | |
65 | * Return result of s_power subdev operation or -ENXIO if sd argument | |
66 | * is NULL. Return 0 if the subdevice does not implement s_power. | |
67 | */ | |
68 | static int __subdev_set_power(struct v4l2_subdev *sd, int on) | |
69 | { | |
70 | int *use_count; | |
71 | int ret; | |
72 | ||
73 | if (sd == NULL) | |
74 | return -ENXIO; | |
75 | ||
76 | use_count = &sd->entity.use_count; | |
77 | if (on && (*use_count)++ > 0) | |
78 | return 0; | |
79 | else if (!on && (*use_count == 0 || --(*use_count) > 0)) | |
80 | return 0; | |
81 | ret = v4l2_subdev_call(sd, core, s_power, on); | |
82 | ||
83 | return ret != -ENOIOCTLCMD ? ret : 0; | |
84 | } | |
85 | ||
86 | /** | |
87 | * fimc_pipeline_s_power - change power state of all pipeline subdevs | |
88 | * @fimc: fimc device terminating the pipeline | |
89 | * @state: 1 to enable power or 0 for power down | |
90 | * | |
91 | * Need to be called with the graph mutex held. | |
92 | */ | |
93 | int fimc_pipeline_s_power(struct fimc_dev *fimc, int state) | |
94 | { | |
95 | int ret = 0; | |
96 | ||
97 | if (fimc->pipeline.sensor == NULL) | |
98 | return -ENXIO; | |
99 | ||
100 | if (state) { | |
101 | ret = __subdev_set_power(fimc->pipeline.csis, 1); | |
102 | if (ret && ret != -ENXIO) | |
103 | return ret; | |
104 | return __subdev_set_power(fimc->pipeline.sensor, 1); | |
105 | } | |
106 | ||
107 | ret = __subdev_set_power(fimc->pipeline.sensor, 0); | |
108 | if (ret) | |
109 | return ret; | |
110 | ret = __subdev_set_power(fimc->pipeline.csis, 0); | |
111 | ||
112 | return ret == -ENXIO ? 0 : ret; | |
113 | } | |
114 | ||
115 | /** | |
116 | * __fimc_pipeline_initialize - update the pipeline information, enable power | |
117 | * of all pipeline subdevs and the sensor clock | |
118 | * @me: media entity to start graph walk with | |
119 | * @prep: true to acquire sensor (and csis) subdevs | |
120 | * | |
121 | * This function must be called with the graph mutex held. | |
122 | */ | |
123 | static int __fimc_pipeline_initialize(struct fimc_dev *fimc, | |
124 | struct media_entity *me, bool prep) | |
125 | { | |
126 | int ret; | |
127 | ||
128 | if (prep) | |
129 | fimc_pipeline_prepare(fimc, me); | |
130 | if (fimc->pipeline.sensor == NULL) | |
131 | return -EINVAL; | |
132 | ret = fimc_md_set_camclk(fimc->pipeline.sensor, true); | |
133 | if (ret) | |
134 | return ret; | |
135 | return fimc_pipeline_s_power(fimc, 1); | |
136 | } | |
137 | ||
138 | int fimc_pipeline_initialize(struct fimc_dev *fimc, struct media_entity *me, | |
139 | bool prep) | |
140 | { | |
141 | int ret; | |
142 | ||
143 | mutex_lock(&me->parent->graph_mutex); | |
144 | ret = __fimc_pipeline_initialize(fimc, me, prep); | |
145 | mutex_unlock(&me->parent->graph_mutex); | |
146 | ||
147 | return ret; | |
148 | } | |
149 | ||
150 | /** | |
151 | * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power | |
152 | * @fimc: fimc device terminating the pipeline | |
153 | * | |
154 | * Disable power of all subdevs in the pipeline and turn off the external | |
155 | * sensor clock. | |
156 | * Called with the graph mutex held. | |
157 | */ | |
158 | int __fimc_pipeline_shutdown(struct fimc_dev *fimc) | |
159 | { | |
160 | int ret = 0; | |
161 | ||
162 | if (fimc->pipeline.sensor) { | |
163 | ret = fimc_pipeline_s_power(fimc, 0); | |
164 | fimc_md_set_camclk(fimc->pipeline.sensor, false); | |
165 | } | |
166 | return ret == -ENXIO ? 0 : ret; | |
167 | } | |
168 | ||
169 | int fimc_pipeline_shutdown(struct fimc_dev *fimc) | |
170 | { | |
171 | struct media_entity *me = &fimc->vid_cap.vfd->entity; | |
172 | int ret; | |
173 | ||
174 | mutex_lock(&me->parent->graph_mutex); | |
175 | ret = __fimc_pipeline_shutdown(fimc); | |
176 | mutex_unlock(&me->parent->graph_mutex); | |
177 | ||
178 | return ret; | |
179 | } | |
180 | ||
181 | /** | |
182 | * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs | |
183 | * @fimc: fimc device terminating the pipeline | |
184 | * @on: passed as the s_stream call argument | |
185 | */ | |
186 | int fimc_pipeline_s_stream(struct fimc_dev *fimc, int on) | |
187 | { | |
188 | struct fimc_pipeline *p = &fimc->pipeline; | |
189 | int ret = 0; | |
190 | ||
191 | if (p->sensor == NULL) | |
192 | return -ENODEV; | |
193 | ||
194 | if ((on && p->csis) || !on) | |
195 | ret = v4l2_subdev_call(on ? p->csis : p->sensor, | |
196 | video, s_stream, on); | |
cf52df8a | 197 | if (ret < 0 && ret != -ENOIOCTLCMD) |
d3953223 SN |
198 | return ret; |
199 | if ((!on && p->csis) || on) | |
200 | ret = v4l2_subdev_call(on ? p->sensor : p->csis, | |
201 | video, s_stream, on); | |
202 | return ret == -ENOIOCTLCMD ? 0 : ret; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Sensor subdevice helper functions | |
207 | */ | |
208 | static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd, | |
209 | struct fimc_sensor_info *s_info) | |
210 | { | |
211 | struct i2c_adapter *adapter; | |
212 | struct v4l2_subdev *sd = NULL; | |
213 | ||
214 | if (!s_info || !fmd) | |
215 | return NULL; | |
216 | ||
217 | adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num); | |
218 | if (!adapter) | |
219 | return NULL; | |
220 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, | |
221 | s_info->pdata->board_info, NULL); | |
222 | if (IS_ERR_OR_NULL(sd)) { | |
223 | v4l2_err(&fmd->v4l2_dev, "Failed to acquire subdev\n"); | |
224 | return NULL; | |
225 | } | |
226 | v4l2_set_subdev_hostdata(sd, s_info); | |
227 | sd->grp_id = SENSOR_GROUP_ID; | |
228 | ||
229 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n", | |
230 | s_info->pdata->board_info->type); | |
231 | return sd; | |
232 | } | |
233 | ||
234 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) | |
235 | { | |
236 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
237 | ||
238 | if (!client) | |
239 | return; | |
240 | v4l2_device_unregister_subdev(sd); | |
241 | i2c_unregister_device(client); | |
242 | i2c_put_adapter(client->adapter); | |
243 | } | |
244 | ||
245 | static int fimc_md_register_sensor_entities(struct fimc_md *fmd) | |
246 | { | |
247 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; | |
248 | struct fimc_dev *fd = NULL; | |
249 | int num_clients, ret, i; | |
250 | ||
251 | /* | |
252 | * Runtime resume one of the FIMC entities to make sure | |
253 | * the sclk_cam clocks are not globally disabled. | |
254 | */ | |
255 | for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++) | |
256 | if (fmd->fimc[i]) | |
257 | fd = fmd->fimc[i]; | |
258 | if (!fd) | |
259 | return -ENXIO; | |
260 | ret = pm_runtime_get_sync(&fd->pdev->dev); | |
261 | if (ret < 0) | |
262 | return ret; | |
263 | ||
264 | WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor)); | |
265 | num_clients = min_t(u32, pdata->num_clients, ARRAY_SIZE(fmd->sensor)); | |
266 | ||
267 | fmd->num_sensors = num_clients; | |
268 | for (i = 0; i < num_clients; i++) { | |
269 | fmd->sensor[i].pdata = &pdata->isp_info[i]; | |
270 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); | |
271 | if (ret) | |
272 | break; | |
273 | fmd->sensor[i].subdev = | |
274 | fimc_md_register_sensor(fmd, &fmd->sensor[i]); | |
275 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); | |
276 | if (ret) | |
277 | break; | |
278 | } | |
279 | pm_runtime_put(&fd->pdev->dev); | |
280 | return ret; | |
281 | } | |
282 | ||
283 | /* | |
284 | * MIPI CSIS and FIMC platform devices registration. | |
285 | */ | |
286 | static int fimc_register_callback(struct device *dev, void *p) | |
287 | { | |
288 | struct fimc_dev *fimc = dev_get_drvdata(dev); | |
289 | struct fimc_md *fmd = p; | |
290 | int ret; | |
291 | ||
292 | if (!fimc || !fimc->pdev) | |
293 | return 0; | |
294 | if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS) | |
295 | return 0; | |
296 | ||
297 | fmd->fimc[fimc->pdev->id] = fimc; | |
298 | ret = fimc_register_m2m_device(fimc, &fmd->v4l2_dev); | |
299 | if (ret) | |
300 | return ret; | |
301 | ret = fimc_register_capture_device(fimc, &fmd->v4l2_dev); | |
302 | if (!ret) | |
303 | fimc->vid_cap.user_subdev_api = fmd->user_subdev_api; | |
304 | return ret; | |
305 | } | |
306 | ||
307 | static int csis_register_callback(struct device *dev, void *p) | |
308 | { | |
309 | struct v4l2_subdev *sd = dev_get_drvdata(dev); | |
310 | struct platform_device *pdev; | |
311 | struct fimc_md *fmd = p; | |
312 | int id, ret; | |
313 | ||
314 | if (!sd) | |
315 | return 0; | |
316 | pdev = v4l2_get_subdevdata(sd); | |
317 | if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES) | |
318 | return 0; | |
319 | v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name); | |
320 | ||
321 | id = pdev->id < 0 ? 0 : pdev->id; | |
322 | fmd->csis[id].sd = sd; | |
323 | sd->grp_id = CSIS_GROUP_ID; | |
324 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); | |
325 | if (ret) | |
326 | v4l2_err(&fmd->v4l2_dev, | |
327 | "Failed to register CSIS subdevice: %d\n", ret); | |
328 | return ret; | |
329 | } | |
330 | ||
331 | /** | |
332 | * fimc_md_register_platform_entities - register FIMC and CSIS media entities | |
333 | */ | |
334 | static int fimc_md_register_platform_entities(struct fimc_md *fmd) | |
335 | { | |
336 | struct device_driver *driver; | |
337 | int ret; | |
338 | ||
339 | driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type); | |
340 | if (!driver) | |
341 | return -ENODEV; | |
342 | ret = driver_for_each_device(driver, NULL, fmd, | |
343 | fimc_register_callback); | |
344 | put_driver(driver); | |
345 | if (ret) | |
346 | return ret; | |
347 | ||
348 | driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type); | |
349 | if (driver) { | |
350 | ret = driver_for_each_device(driver, NULL, fmd, | |
351 | csis_register_callback); | |
352 | put_driver(driver); | |
353 | } | |
354 | return ret; | |
355 | } | |
356 | ||
357 | static void fimc_md_unregister_entities(struct fimc_md *fmd) | |
358 | { | |
359 | int i; | |
360 | ||
361 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
362 | if (fmd->fimc[i] == NULL) | |
363 | continue; | |
364 | fimc_unregister_m2m_device(fmd->fimc[i]); | |
365 | fimc_unregister_capture_device(fmd->fimc[i]); | |
366 | fmd->fimc[i] = NULL; | |
367 | } | |
368 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { | |
369 | if (fmd->csis[i].sd == NULL) | |
370 | continue; | |
371 | v4l2_device_unregister_subdev(fmd->csis[i].sd); | |
372 | fmd->csis[i].sd = NULL; | |
373 | } | |
374 | for (i = 0; i < fmd->num_sensors; i++) { | |
375 | if (fmd->sensor[i].subdev == NULL) | |
376 | continue; | |
377 | fimc_md_unregister_sensor(fmd->sensor[i].subdev); | |
378 | fmd->sensor[i].subdev = NULL; | |
379 | } | |
380 | } | |
381 | ||
382 | static int fimc_md_register_video_nodes(struct fimc_md *fmd) | |
383 | { | |
384 | int i, ret = 0; | |
385 | ||
386 | for (i = 0; i < FIMC_MAX_DEVS && !ret; i++) { | |
387 | if (!fmd->fimc[i]) | |
388 | continue; | |
389 | ||
390 | if (fmd->fimc[i]->m2m.vfd) | |
391 | ret = video_register_device(fmd->fimc[i]->m2m.vfd, | |
392 | VFL_TYPE_GRABBER, -1); | |
393 | if (ret) | |
394 | break; | |
395 | if (fmd->fimc[i]->vid_cap.vfd) | |
396 | ret = video_register_device(fmd->fimc[i]->vid_cap.vfd, | |
397 | VFL_TYPE_GRABBER, -1); | |
398 | } | |
399 | ||
400 | return ret; | |
401 | } | |
402 | ||
403 | /** | |
404 | * __fimc_md_create_fimc_links - create links to all FIMC entities | |
405 | * @fmd: fimc media device | |
406 | * @source: the source entity to create links to all fimc entities from | |
407 | * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null | |
408 | * @pad: the source entity pad index | |
409 | * @fimc_id: index of the fimc device for which link should be enabled | |
410 | */ | |
411 | static int __fimc_md_create_fimc_links(struct fimc_md *fmd, | |
412 | struct media_entity *source, | |
413 | struct v4l2_subdev *sensor, | |
414 | int pad, int fimc_id) | |
415 | { | |
416 | struct fimc_sensor_info *s_info; | |
417 | struct media_entity *sink; | |
418 | unsigned int flags; | |
237e0265 | 419 | int ret, i; |
d3953223 SN |
420 | |
421 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
422 | if (!fmd->fimc[i]) | |
423 | break; | |
424 | /* | |
425 | * Some FIMC variants are not fitted with camera capture | |
426 | * interface. Skip creating a link from sensor for those. | |
427 | */ | |
237e0265 | 428 | if (sensor->grp_id == SENSOR_GROUP_ID && |
d3953223 SN |
429 | !fmd->fimc[i]->variant->has_cam_if) |
430 | continue; | |
431 | ||
432 | flags = (i == fimc_id) ? MEDIA_LNK_FL_ENABLED : 0; | |
237e0265 SN |
433 | sink = &fmd->fimc[i]->vid_cap.subdev->entity; |
434 | ret = media_entity_create_link(source, pad, sink, | |
435 | FIMC_SD_PAD_SINK, flags); | |
d3953223 SN |
436 | if (ret) |
437 | return ret; | |
438 | ||
237e0265 SN |
439 | /* Notify FIMC capture subdev entity */ |
440 | ret = media_entity_call(sink, link_setup, &sink->pads[0], | |
441 | &source->pads[pad], flags); | |
442 | if (ret) | |
443 | break; | |
444 | ||
d3953223 SN |
445 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]", |
446 | source->name, flags ? '=' : '-', sink->name); | |
447 | ||
237e0265 | 448 | if (flags == 0) |
d3953223 SN |
449 | continue; |
450 | s_info = v4l2_get_subdev_hostdata(sensor); | |
451 | if (!WARN_ON(s_info == NULL)) { | |
452 | unsigned long irq_flags; | |
453 | spin_lock_irqsave(&fmd->slock, irq_flags); | |
454 | s_info->host = fmd->fimc[i]; | |
455 | spin_unlock_irqrestore(&fmd->slock, irq_flags); | |
456 | } | |
457 | } | |
458 | return 0; | |
459 | } | |
460 | ||
461 | /** | |
462 | * fimc_md_create_links - create default links between registered entities | |
463 | * | |
464 | * Parallel interface sensor entities are connected directly to FIMC capture | |
465 | * entities. The sensors using MIPI CSIS bus are connected through immutable | |
466 | * link with CSI receiver entity specified by mux_id. Any registered CSIS | |
467 | * entity has a link to each registered FIMC capture entity. Enabled links | |
468 | * are created by default between each subsequent registered sensor and | |
469 | * subsequent FIMC capture entity. The number of default active links is | |
470 | * determined by the number of available sensors or FIMC entities, | |
471 | * whichever is less. | |
472 | */ | |
473 | static int fimc_md_create_links(struct fimc_md *fmd) | |
474 | { | |
475 | struct v4l2_subdev *sensor, *csis; | |
476 | struct s5p_fimc_isp_info *pdata; | |
477 | struct fimc_sensor_info *s_info; | |
237e0265 SN |
478 | struct media_entity *source, *sink; |
479 | int i, pad, fimc_id = 0; | |
d3953223 | 480 | int ret = 0; |
237e0265 | 481 | u32 flags; |
d3953223 SN |
482 | |
483 | for (i = 0; i < fmd->num_sensors; i++) { | |
484 | if (fmd->sensor[i].subdev == NULL) | |
485 | continue; | |
486 | ||
487 | sensor = fmd->sensor[i].subdev; | |
488 | s_info = v4l2_get_subdev_hostdata(sensor); | |
489 | if (!s_info || !s_info->pdata) | |
490 | continue; | |
491 | ||
492 | source = NULL; | |
493 | pdata = s_info->pdata; | |
494 | ||
495 | switch (pdata->bus_type) { | |
496 | case FIMC_MIPI_CSI2: | |
497 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, | |
498 | "Wrong CSI channel id: %d\n", pdata->mux_id)) | |
499 | return -EINVAL; | |
500 | ||
501 | csis = fmd->csis[pdata->mux_id].sd; | |
502 | if (WARN(csis == NULL, | |
503 | "MIPI-CSI interface specified " | |
504 | "but s5p-csis module is not loaded!\n")) | |
505 | continue; | |
506 | ||
507 | ret = media_entity_create_link(&sensor->entity, 0, | |
508 | &csis->entity, CSIS_PAD_SINK, | |
509 | MEDIA_LNK_FL_IMMUTABLE | | |
510 | MEDIA_LNK_FL_ENABLED); | |
511 | if (ret) | |
512 | return ret; | |
513 | ||
514 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]", | |
515 | sensor->entity.name, csis->entity.name); | |
516 | ||
d3953223 SN |
517 | source = &csis->entity; |
518 | pad = CSIS_PAD_SOURCE; | |
519 | break; | |
520 | ||
521 | case FIMC_ITU_601...FIMC_ITU_656: | |
522 | source = &sensor->entity; | |
523 | pad = 0; | |
524 | break; | |
525 | ||
526 | default: | |
527 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", | |
528 | pdata->bus_type); | |
529 | return -EINVAL; | |
530 | } | |
531 | if (source == NULL) | |
532 | continue; | |
533 | ||
534 | ret = __fimc_md_create_fimc_links(fmd, source, sensor, pad, | |
237e0265 | 535 | fimc_id++); |
d3953223 | 536 | } |
237e0265 SN |
537 | /* Create immutable links between each FIMC's subdev and video node */ |
538 | flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED; | |
539 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
540 | if (!fmd->fimc[i]) | |
541 | continue; | |
542 | source = &fmd->fimc[i]->vid_cap.subdev->entity; | |
543 | sink = &fmd->fimc[i]->vid_cap.vfd->entity; | |
544 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, | |
545 | sink, 0, flags); | |
546 | if (ret) | |
547 | break; | |
548 | } | |
549 | ||
d3953223 SN |
550 | return ret; |
551 | } | |
552 | ||
553 | /* | |
554 | * The peripheral sensor clock management. | |
555 | */ | |
556 | static int fimc_md_get_clocks(struct fimc_md *fmd) | |
557 | { | |
558 | char clk_name[32]; | |
559 | struct clk *clock; | |
560 | int i; | |
561 | ||
562 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { | |
563 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); | |
564 | clock = clk_get(NULL, clk_name); | |
565 | if (IS_ERR_OR_NULL(clock)) { | |
566 | v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s", | |
567 | clk_name); | |
568 | return -ENXIO; | |
569 | } | |
570 | fmd->camclk[i].clock = clock; | |
571 | } | |
572 | return 0; | |
573 | } | |
574 | ||
575 | static void fimc_md_put_clocks(struct fimc_md *fmd) | |
576 | { | |
577 | int i = FIMC_MAX_CAMCLKS; | |
578 | ||
579 | while (--i >= 0) { | |
580 | if (IS_ERR_OR_NULL(fmd->camclk[i].clock)) | |
581 | continue; | |
582 | clk_put(fmd->camclk[i].clock); | |
583 | fmd->camclk[i].clock = NULL; | |
584 | } | |
585 | } | |
586 | ||
587 | static int __fimc_md_set_camclk(struct fimc_md *fmd, | |
588 | struct fimc_sensor_info *s_info, | |
589 | bool on) | |
590 | { | |
591 | struct s5p_fimc_isp_info *pdata = s_info->pdata; | |
592 | struct fimc_camclk_info *camclk; | |
593 | int ret = 0; | |
594 | ||
595 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) | |
596 | return -EINVAL; | |
597 | ||
598 | if (s_info->clk_on == on) | |
599 | return 0; | |
600 | camclk = &fmd->camclk[pdata->clk_id]; | |
601 | ||
602 | dbg("camclk %d, f: %lu, clk: %p, on: %d", | |
603 | pdata->clk_id, pdata->clk_frequency, camclk, on); | |
604 | ||
605 | if (on) { | |
606 | if (camclk->use_count > 0 && | |
607 | camclk->frequency != pdata->clk_frequency) | |
608 | return -EINVAL; | |
609 | ||
610 | if (camclk->use_count++ == 0) { | |
611 | clk_set_rate(camclk->clock, pdata->clk_frequency); | |
612 | camclk->frequency = pdata->clk_frequency; | |
613 | ret = clk_enable(camclk->clock); | |
614 | } | |
615 | s_info->clk_on = 1; | |
616 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, | |
617 | clk_get_rate(camclk->clock)); | |
618 | ||
619 | return ret; | |
620 | } | |
621 | ||
622 | if (WARN_ON(camclk->use_count == 0)) | |
623 | return 0; | |
624 | ||
625 | if (--camclk->use_count == 0) { | |
626 | clk_disable(camclk->clock); | |
627 | s_info->clk_on = 0; | |
628 | dbg("Disabled camclk %d", pdata->clk_id); | |
629 | } | |
630 | return ret; | |
631 | } | |
632 | ||
633 | /** | |
634 | * fimc_md_set_camclk - peripheral sensor clock setup | |
635 | * @sd: sensor subdev to configure sclk_cam clock for | |
636 | * @on: 1 to enable or 0 to disable the clock | |
637 | * | |
638 | * There are 2 separate clock outputs available in the SoC for external | |
639 | * image processors. These clocks are shared between all registered FIMC | |
640 | * devices to which sensors can be attached, either directly or through | |
641 | * the MIPI CSI receiver. The clock is allowed here to be used by | |
642 | * multiple sensors concurrently if they use same frequency. | |
643 | * The per sensor subdev clk_on attribute helps to synchronize accesses | |
644 | * to the sclk_cam clocks from the video and media device nodes. | |
645 | * This function should only be called when the graph mutex is held. | |
646 | */ | |
647 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) | |
648 | { | |
649 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); | |
650 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); | |
651 | ||
652 | return __fimc_md_set_camclk(fmd, s_info, on); | |
653 | } | |
654 | ||
655 | static int fimc_md_link_notify(struct media_pad *source, | |
656 | struct media_pad *sink, u32 flags) | |
657 | { | |
237e0265 | 658 | struct v4l2_subdev *sd; |
d3953223 SN |
659 | struct fimc_dev *fimc; |
660 | int ret = 0; | |
661 | ||
237e0265 | 662 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
d3953223 SN |
663 | return 0; |
664 | ||
237e0265 SN |
665 | sd = media_entity_to_v4l2_subdev(sink->entity); |
666 | fimc = v4l2_get_subdevdata(sd); | |
d3953223 SN |
667 | |
668 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { | |
669 | ret = __fimc_pipeline_shutdown(fimc); | |
670 | fimc->pipeline.sensor = NULL; | |
671 | fimc->pipeline.csis = NULL; | |
131b6c61 SN |
672 | |
673 | mutex_lock(&fimc->lock); | |
674 | fimc_ctrls_delete(fimc->vid_cap.ctx); | |
675 | mutex_unlock(&fimc->lock); | |
d3953223 SN |
676 | return ret; |
677 | } | |
678 | /* | |
679 | * Link activation. Enable power of pipeline elements only if the | |
680 | * pipeline is already in use, i.e. its video node is opened. | |
131b6c61 | 681 | * Recreate the controls destroyed during the link deactivation. |
d3953223 SN |
682 | */ |
683 | mutex_lock(&fimc->lock); | |
131b6c61 | 684 | if (fimc->vid_cap.refcnt > 0) { |
d3953223 | 685 | ret = __fimc_pipeline_initialize(fimc, source->entity, true); |
131b6c61 SN |
686 | if (!ret) |
687 | ret = fimc_capture_ctrls_create(fimc); | |
688 | } | |
d3953223 SN |
689 | mutex_unlock(&fimc->lock); |
690 | ||
691 | return ret ? -EPIPE : ret; | |
692 | } | |
693 | ||
694 | static ssize_t fimc_md_sysfs_show(struct device *dev, | |
695 | struct device_attribute *attr, char *buf) | |
696 | { | |
697 | struct platform_device *pdev = to_platform_device(dev); | |
698 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
699 | ||
700 | if (fmd->user_subdev_api) | |
701 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); | |
702 | ||
703 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); | |
704 | } | |
705 | ||
706 | static ssize_t fimc_md_sysfs_store(struct device *dev, | |
707 | struct device_attribute *attr, | |
708 | const char *buf, size_t count) | |
709 | { | |
710 | struct platform_device *pdev = to_platform_device(dev); | |
711 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
712 | bool subdev_api; | |
713 | int i; | |
714 | ||
715 | if (!strcmp(buf, "vid-dev\n")) | |
716 | subdev_api = false; | |
717 | else if (!strcmp(buf, "sub-dev\n")) | |
718 | subdev_api = true; | |
719 | else | |
720 | return count; | |
721 | ||
722 | fmd->user_subdev_api = subdev_api; | |
723 | for (i = 0; i < FIMC_MAX_DEVS; i++) | |
724 | if (fmd->fimc[i]) | |
725 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; | |
726 | return count; | |
727 | } | |
728 | /* | |
729 | * This device attribute is to select video pipeline configuration method. | |
730 | * There are following valid values: | |
731 | * vid-dev - for V4L2 video node API only, subdevice will be configured | |
732 | * by the host driver. | |
733 | * sub-dev - for media controller API, subdevs must be configured in user | |
734 | * space before starting streaming. | |
735 | */ | |
736 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, | |
737 | fimc_md_sysfs_show, fimc_md_sysfs_store); | |
738 | ||
739 | static int __devinit fimc_md_probe(struct platform_device *pdev) | |
740 | { | |
741 | struct v4l2_device *v4l2_dev; | |
742 | struct fimc_md *fmd; | |
743 | int ret; | |
744 | ||
745 | if (WARN(!pdev->dev.platform_data, "Platform data not specified!\n")) | |
746 | return -EINVAL; | |
747 | ||
748 | fmd = kzalloc(sizeof(struct fimc_md), GFP_KERNEL); | |
749 | if (!fmd) | |
750 | return -ENOMEM; | |
751 | ||
752 | spin_lock_init(&fmd->slock); | |
753 | fmd->pdev = pdev; | |
754 | ||
755 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", | |
756 | sizeof(fmd->media_dev.model)); | |
757 | fmd->media_dev.link_notify = fimc_md_link_notify; | |
758 | fmd->media_dev.dev = &pdev->dev; | |
759 | ||
760 | v4l2_dev = &fmd->v4l2_dev; | |
761 | v4l2_dev->mdev = &fmd->media_dev; | |
762 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s", | |
763 | dev_name(&pdev->dev)); | |
764 | ||
765 | ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev); | |
766 | if (ret < 0) { | |
767 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); | |
768 | goto err1; | |
769 | } | |
770 | ret = media_device_register(&fmd->media_dev); | |
771 | if (ret < 0) { | |
772 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); | |
773 | goto err2; | |
774 | } | |
775 | ret = fimc_md_get_clocks(fmd); | |
776 | if (ret) | |
777 | goto err3; | |
778 | ||
779 | fmd->user_subdev_api = false; | |
780 | ret = fimc_md_register_platform_entities(fmd); | |
781 | if (ret) | |
782 | goto err3; | |
783 | ||
784 | ret = fimc_md_register_sensor_entities(fmd); | |
785 | if (ret) | |
786 | goto err3; | |
787 | ret = fimc_md_create_links(fmd); | |
788 | if (ret) | |
789 | goto err3; | |
790 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); | |
791 | if (ret) | |
792 | goto err3; | |
793 | ret = fimc_md_register_video_nodes(fmd); | |
794 | if (ret) | |
795 | goto err3; | |
796 | ||
797 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); | |
798 | if (!ret) { | |
799 | platform_set_drvdata(pdev, fmd); | |
800 | return 0; | |
801 | } | |
802 | err3: | |
803 | media_device_unregister(&fmd->media_dev); | |
804 | fimc_md_put_clocks(fmd); | |
805 | fimc_md_unregister_entities(fmd); | |
806 | err2: | |
807 | v4l2_device_unregister(&fmd->v4l2_dev); | |
808 | err1: | |
809 | kfree(fmd); | |
810 | return ret; | |
811 | } | |
812 | ||
813 | static int __devexit fimc_md_remove(struct platform_device *pdev) | |
814 | { | |
815 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
816 | ||
817 | if (!fmd) | |
818 | return 0; | |
819 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); | |
820 | fimc_md_unregister_entities(fmd); | |
821 | media_device_unregister(&fmd->media_dev); | |
822 | fimc_md_put_clocks(fmd); | |
823 | kfree(fmd); | |
824 | return 0; | |
825 | } | |
826 | ||
827 | static struct platform_driver fimc_md_driver = { | |
828 | .probe = fimc_md_probe, | |
829 | .remove = __devexit_p(fimc_md_remove), | |
830 | .driver = { | |
831 | .name = "s5p-fimc-md", | |
832 | .owner = THIS_MODULE, | |
833 | } | |
834 | }; | |
835 | ||
836 | int __init fimc_md_init(void) | |
837 | { | |
838 | int ret; | |
839 | request_module("s5p-csis"); | |
840 | ret = fimc_register_driver(); | |
841 | if (ret) | |
842 | return ret; | |
843 | return platform_driver_register(&fimc_md_driver); | |
844 | } | |
845 | void __exit fimc_md_exit(void) | |
846 | { | |
847 | platform_driver_unregister(&fimc_md_driver); | |
848 | fimc_unregister_driver(); | |
849 | } | |
850 | ||
851 | module_init(fimc_md_init); | |
852 | module_exit(fimc_md_exit); | |
853 | ||
854 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); | |
855 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); | |
856 | MODULE_LICENSE("GPL"); | |
857 | MODULE_VERSION("2.0.1"); |