Commit | Line | Data |
---|---|---|
d3953223 SN |
1 | /* |
2 | * S5P/EXYNOS4 SoC series camera host interface media device driver | |
3 | * | |
7b43a6f3 SN |
4 | * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. |
5 | * Sylwester Nawrocki <s.nawrocki@samsung.com> | |
d3953223 SN |
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> | |
e2985a26 SN |
20 | #include <linux/of.h> |
21 | #include <linux/of_platform.h> | |
22 | #include <linux/of_device.h> | |
23 | #include <linux/of_i2c.h> | |
d3953223 SN |
24 | #include <linux/platform_device.h> |
25 | #include <linux/pm_runtime.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/slab.h> | |
131b6c61 | 28 | #include <media/v4l2-ctrls.h> |
e2985a26 | 29 | #include <media/v4l2-of.h> |
d3953223 | 30 | #include <media/media-device.h> |
b9ee31e6 | 31 | #include <media/s5p_fimc.h> |
d3953223 SN |
32 | |
33 | #include "fimc-core.h" | |
0f735f52 | 34 | #include "fimc-lite.h" |
d3953223 SN |
35 | #include "fimc-mdevice.h" |
36 | #include "mipi-csis.h" | |
37 | ||
38 | static int __fimc_md_set_camclk(struct fimc_md *fmd, | |
39 | struct fimc_sensor_info *s_info, | |
40 | bool on); | |
41 | /** | |
42 | * fimc_pipeline_prepare - update pipeline information with subdevice pointers | |
39bb6df6 | 43 | * @me: media entity terminating the pipeline |
d3953223 SN |
44 | * |
45 | * Caller holds the graph mutex. | |
46 | */ | |
b9ee31e6 SN |
47 | static void fimc_pipeline_prepare(struct fimc_pipeline *p, |
48 | struct media_entity *me) | |
d3953223 | 49 | { |
d3953223 | 50 | struct v4l2_subdev *sd; |
0f735f52 | 51 | int i; |
d3953223 | 52 | |
0f735f52 SN |
53 | for (i = 0; i < IDX_MAX; i++) |
54 | p->subdevs[i] = NULL; | |
d3953223 | 55 | |
0f735f52 | 56 | while (1) { |
39bb6df6 SN |
57 | struct media_pad *pad = NULL; |
58 | ||
59 | /* Find remote source pad */ | |
60 | for (i = 0; i < me->num_pads; i++) { | |
61 | struct media_pad *spad = &me->pads[i]; | |
62 | if (!(spad->flags & MEDIA_PAD_FL_SINK)) | |
63 | continue; | |
64 | pad = media_entity_remote_source(spad); | |
65 | if (pad) | |
66 | break; | |
67 | } | |
0f735f52 | 68 | |
0f735f52 SN |
69 | if (pad == NULL || |
70 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
71 | break; | |
0f735f52 SN |
72 | sd = media_entity_to_v4l2_subdev(pad->entity); |
73 | ||
74 | switch (sd->grp_id) { | |
588c87be SN |
75 | case GRP_ID_FIMC_IS_SENSOR: |
76 | case GRP_ID_SENSOR: | |
0f735f52 SN |
77 | p->subdevs[IDX_SENSOR] = sd; |
78 | break; | |
588c87be | 79 | case GRP_ID_CSIS: |
0f735f52 SN |
80 | p->subdevs[IDX_CSIS] = sd; |
81 | break; | |
588c87be | 82 | case GRP_ID_FLITE: |
4af81310 SN |
83 | p->subdevs[IDX_FLITE] = sd; |
84 | break; | |
588c87be | 85 | case GRP_ID_FIMC: |
0f735f52 SN |
86 | /* No need to control FIMC subdev through subdev ops */ |
87 | break; | |
88 | default: | |
89 | pr_warn("%s: Unknown subdev grp_id: %#x\n", | |
90 | __func__, sd->grp_id); | |
91 | } | |
39bb6df6 SN |
92 | me = &sd->entity; |
93 | if (me->num_pads == 1) | |
94 | break; | |
d3953223 SN |
95 | } |
96 | } | |
97 | ||
98 | /** | |
99 | * __subdev_set_power - change power state of a single subdev | |
100 | * @sd: subdevice to change power state for | |
101 | * @on: 1 to enable power or 0 to disable | |
102 | * | |
103 | * Return result of s_power subdev operation or -ENXIO if sd argument | |
104 | * is NULL. Return 0 if the subdevice does not implement s_power. | |
105 | */ | |
106 | static int __subdev_set_power(struct v4l2_subdev *sd, int on) | |
107 | { | |
108 | int *use_count; | |
109 | int ret; | |
110 | ||
111 | if (sd == NULL) | |
112 | return -ENXIO; | |
113 | ||
114 | use_count = &sd->entity.use_count; | |
115 | if (on && (*use_count)++ > 0) | |
116 | return 0; | |
117 | else if (!on && (*use_count == 0 || --(*use_count) > 0)) | |
118 | return 0; | |
119 | ret = v4l2_subdev_call(sd, core, s_power, on); | |
120 | ||
121 | return ret != -ENOIOCTLCMD ? ret : 0; | |
122 | } | |
123 | ||
124 | /** | |
125 | * fimc_pipeline_s_power - change power state of all pipeline subdevs | |
126 | * @fimc: fimc device terminating the pipeline | |
0f735f52 | 127 | * @state: true to power on, false to power off |
d3953223 | 128 | * |
0f735f52 | 129 | * Needs to be called with the graph mutex held. |
d3953223 | 130 | */ |
b9ee31e6 | 131 | static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state) |
d3953223 | 132 | { |
0f735f52 SN |
133 | unsigned int i; |
134 | int ret; | |
d3953223 | 135 | |
0f735f52 | 136 | if (p->subdevs[IDX_SENSOR] == NULL) |
d3953223 SN |
137 | return -ENXIO; |
138 | ||
0f735f52 SN |
139 | for (i = 0; i < IDX_MAX; i++) { |
140 | unsigned int idx = state ? (IDX_MAX - 1) - i : i; | |
141 | ||
142 | ret = __subdev_set_power(p->subdevs[idx], state); | |
143 | if (ret < 0 && ret != -ENXIO) | |
d3953223 | 144 | return ret; |
d3953223 SN |
145 | } |
146 | ||
0f735f52 | 147 | return 0; |
d3953223 SN |
148 | } |
149 | ||
150 | /** | |
b9ee31e6 SN |
151 | * __fimc_pipeline_open - update the pipeline information, enable power |
152 | * of all pipeline subdevs and the sensor clock | |
d3953223 SN |
153 | * @me: media entity to start graph walk with |
154 | * @prep: true to acquire sensor (and csis) subdevs | |
155 | * | |
740ad921 | 156 | * Called with the graph mutex held. |
d3953223 | 157 | */ |
b9ee31e6 SN |
158 | static int __fimc_pipeline_open(struct fimc_pipeline *p, |
159 | struct media_entity *me, bool prep) | |
d3953223 SN |
160 | { |
161 | int ret; | |
162 | ||
163 | if (prep) | |
0f735f52 SN |
164 | fimc_pipeline_prepare(p, me); |
165 | ||
166 | if (p->subdevs[IDX_SENSOR] == NULL) | |
d3953223 | 167 | return -EINVAL; |
0f735f52 SN |
168 | |
169 | ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true); | |
d3953223 SN |
170 | if (ret) |
171 | return ret; | |
0f735f52 SN |
172 | |
173 | return fimc_pipeline_s_power(p, 1); | |
d3953223 SN |
174 | } |
175 | ||
d3953223 | 176 | /** |
b9ee31e6 | 177 | * __fimc_pipeline_close - disable the sensor clock and pipeline power |
d3953223 SN |
178 | * @fimc: fimc device terminating the pipeline |
179 | * | |
740ad921 | 180 | * Disable power of all subdevs and turn the external sensor clock off. |
d3953223 | 181 | */ |
b9ee31e6 | 182 | static int __fimc_pipeline_close(struct fimc_pipeline *p) |
d3953223 SN |
183 | { |
184 | int ret = 0; | |
185 | ||
740ad921 SN |
186 | if (!p || !p->subdevs[IDX_SENSOR]) |
187 | return -EINVAL; | |
188 | ||
0f735f52 SN |
189 | if (p->subdevs[IDX_SENSOR]) { |
190 | ret = fimc_pipeline_s_power(p, 0); | |
191 | fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false); | |
d3953223 SN |
192 | } |
193 | return ret == -ENXIO ? 0 : ret; | |
194 | } | |
195 | ||
d3953223 | 196 | /** |
740ad921 | 197 | * __fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs |
0f735f52 | 198 | * @pipeline: video pipeline structure |
d3953223 SN |
199 | * @on: passed as the s_stream call argument |
200 | */ | |
740ad921 | 201 | static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on) |
d3953223 | 202 | { |
0f735f52 | 203 | int i, ret; |
d3953223 | 204 | |
0f735f52 | 205 | if (p->subdevs[IDX_SENSOR] == NULL) |
d3953223 SN |
206 | return -ENODEV; |
207 | ||
0f735f52 SN |
208 | for (i = 0; i < IDX_MAX; i++) { |
209 | unsigned int idx = on ? (IDX_MAX - 1) - i : i; | |
210 | ||
211 | ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on); | |
212 | ||
213 | if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) | |
214 | return ret; | |
215 | } | |
216 | ||
217 | return 0; | |
218 | ||
d3953223 | 219 | } |
b9ee31e6 SN |
220 | |
221 | /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */ | |
222 | static const struct fimc_pipeline_ops fimc_pipeline_ops = { | |
740ad921 SN |
223 | .open = __fimc_pipeline_open, |
224 | .close = __fimc_pipeline_close, | |
225 | .set_stream = __fimc_pipeline_s_stream, | |
b9ee31e6 | 226 | }; |
d3953223 SN |
227 | |
228 | /* | |
229 | * Sensor subdevice helper functions | |
230 | */ | |
231 | static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd, | |
232 | struct fimc_sensor_info *s_info) | |
233 | { | |
234 | struct i2c_adapter *adapter; | |
235 | struct v4l2_subdev *sd = NULL; | |
236 | ||
237 | if (!s_info || !fmd) | |
238 | return NULL; | |
239 | ||
6612a082 | 240 | adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num); |
ecd9acbf SN |
241 | if (!adapter) { |
242 | v4l2_warn(&fmd->v4l2_dev, | |
243 | "Failed to get I2C adapter %d, deferring probe\n", | |
6612a082 | 244 | s_info->pdata.i2c_bus_num); |
ecd9acbf SN |
245 | return ERR_PTR(-EPROBE_DEFER); |
246 | } | |
d3953223 | 247 | sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter, |
6612a082 | 248 | s_info->pdata.board_info, NULL); |
d3953223 | 249 | if (IS_ERR_OR_NULL(sd)) { |
7acde02a | 250 | i2c_put_adapter(adapter); |
ecd9acbf SN |
251 | v4l2_warn(&fmd->v4l2_dev, |
252 | "Failed to acquire subdev %s, deferring probe\n", | |
6612a082 | 253 | s_info->pdata.board_info->type); |
ecd9acbf | 254 | return ERR_PTR(-EPROBE_DEFER); |
d3953223 SN |
255 | } |
256 | v4l2_set_subdev_hostdata(sd, s_info); | |
588c87be | 257 | sd->grp_id = GRP_ID_SENSOR; |
d3953223 SN |
258 | |
259 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n", | |
2b13f7d4 | 260 | sd->name); |
d3953223 SN |
261 | return sd; |
262 | } | |
263 | ||
264 | static void fimc_md_unregister_sensor(struct v4l2_subdev *sd) | |
265 | { | |
266 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
7acde02a | 267 | struct i2c_adapter *adapter; |
d3953223 SN |
268 | |
269 | if (!client) | |
270 | return; | |
271 | v4l2_device_unregister_subdev(sd); | |
2b13f7d4 SN |
272 | |
273 | if (!client->dev.of_node) { | |
274 | adapter = client->adapter; | |
275 | i2c_unregister_device(client); | |
276 | if (adapter) | |
277 | i2c_put_adapter(adapter); | |
278 | } | |
d3953223 SN |
279 | } |
280 | ||
e2985a26 | 281 | #ifdef CONFIG_OF |
2b13f7d4 SN |
282 | /* Register I2C client subdev associated with @node. */ |
283 | static int fimc_md_of_add_sensor(struct fimc_md *fmd, | |
284 | struct device_node *node, int index) | |
285 | { | |
286 | struct fimc_sensor_info *si; | |
287 | struct i2c_client *client; | |
288 | struct v4l2_subdev *sd; | |
289 | int ret; | |
290 | ||
291 | if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) | |
292 | return -EINVAL; | |
293 | si = &fmd->sensor[index]; | |
294 | ||
295 | client = of_find_i2c_device_by_node(node); | |
296 | if (!client) | |
297 | return -EPROBE_DEFER; | |
298 | ||
299 | device_lock(&client->dev); | |
300 | ||
301 | if (!client->driver || | |
302 | !try_module_get(client->driver->driver.owner)) { | |
303 | ret = -EPROBE_DEFER; | |
304 | v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n", | |
305 | node->full_name); | |
306 | goto dev_put; | |
307 | } | |
308 | ||
309 | /* Enable sensor's master clock */ | |
310 | ret = __fimc_md_set_camclk(fmd, si, true); | |
311 | if (ret < 0) | |
312 | goto mod_put; | |
313 | sd = i2c_get_clientdata(client); | |
314 | ||
315 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); | |
316 | __fimc_md_set_camclk(fmd, si, false); | |
317 | if (ret < 0) | |
318 | goto mod_put; | |
319 | ||
320 | v4l2_set_subdev_hostdata(sd, si); | |
321 | sd->grp_id = GRP_ID_SENSOR; | |
322 | si->subdev = sd; | |
323 | v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n", | |
324 | sd->name, fmd->num_sensors); | |
325 | fmd->num_sensors++; | |
326 | ||
327 | mod_put: | |
328 | module_put(client->driver->driver.owner); | |
329 | dev_put: | |
330 | device_unlock(&client->dev); | |
331 | put_device(&client->dev); | |
332 | return ret; | |
333 | } | |
334 | ||
335 | /* Parse port node and register as a sub-device any sensor specified there. */ | |
336 | static int fimc_md_parse_port_node(struct fimc_md *fmd, | |
337 | struct device_node *port, | |
338 | unsigned int index) | |
339 | { | |
340 | struct device_node *rem, *ep, *np; | |
341 | struct fimc_source_info *pd; | |
342 | struct v4l2_of_endpoint endpoint; | |
343 | int ret; | |
344 | u32 val; | |
345 | ||
346 | pd = &fmd->sensor[index].pdata; | |
347 | ||
348 | /* Assume here a port node can have only one endpoint node. */ | |
349 | ep = of_get_next_child(port, NULL); | |
350 | if (!ep) | |
351 | return 0; | |
352 | ||
353 | v4l2_of_parse_endpoint(ep, &endpoint); | |
354 | if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS) | |
355 | return -EINVAL; | |
356 | ||
357 | pd->mux_id = (endpoint.port - 1) & 0x1; | |
358 | ||
359 | rem = v4l2_of_get_remote_port_parent(ep); | |
360 | of_node_put(ep); | |
361 | if (rem == NULL) { | |
362 | v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n", | |
363 | ep->full_name); | |
364 | return 0; | |
365 | } | |
366 | if (!of_property_read_u32(rem, "samsung,camclk-out", &val)) | |
367 | pd->clk_id = val; | |
368 | ||
369 | if (!of_property_read_u32(rem, "clock-frequency", &val)) | |
370 | pd->clk_frequency = val; | |
371 | ||
372 | if (pd->clk_frequency == 0) { | |
373 | v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n", | |
374 | rem->full_name); | |
375 | of_node_put(rem); | |
376 | return -EINVAL; | |
377 | } | |
378 | ||
379 | if (fimc_input_is_parallel(endpoint.port)) { | |
380 | if (endpoint.bus_type == V4L2_MBUS_PARALLEL) | |
381 | pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601; | |
382 | else | |
383 | pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656; | |
384 | pd->flags = endpoint.bus.parallel.flags; | |
385 | } else if (fimc_input_is_mipi_csi(endpoint.port)) { | |
386 | /* | |
387 | * MIPI CSI-2: only input mux selection and | |
388 | * the sensor's clock frequency is needed. | |
389 | */ | |
390 | pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2; | |
391 | } else { | |
392 | v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n", | |
393 | endpoint.port, rem->full_name); | |
394 | } | |
395 | /* | |
396 | * For FIMC-IS handled sensors, that are placed under i2c-isp device | |
397 | * node, FIMC is connected to the FIMC-IS through its ISP Writeback | |
398 | * input. Sensors are attached to the FIMC-LITE hostdata interface | |
399 | * directly or through MIPI-CSIS, depending on the external media bus | |
400 | * used. This needs to be handled in a more reliable way, not by just | |
401 | * checking parent's node name. | |
402 | */ | |
403 | np = of_get_parent(rem); | |
404 | ||
405 | if (np && !of_node_cmp(np->name, "i2c-isp")) | |
406 | pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK; | |
407 | else | |
408 | pd->fimc_bus_type = pd->sensor_bus_type; | |
409 | ||
410 | ret = fimc_md_of_add_sensor(fmd, rem, index); | |
411 | of_node_put(rem); | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
416 | /* Register all SoC external sub-devices */ | |
417 | static int fimc_md_of_sensors_register(struct fimc_md *fmd, | |
418 | struct device_node *np) | |
419 | { | |
420 | struct device_node *parent = fmd->pdev->dev.of_node; | |
421 | struct device_node *node, *ports; | |
422 | int index = 0; | |
423 | int ret; | |
424 | ||
425 | /* Attach sensors linked to MIPI CSI-2 receivers */ | |
426 | for_each_available_child_of_node(parent, node) { | |
427 | struct device_node *port; | |
428 | ||
429 | if (of_node_cmp(node->name, "csis")) | |
430 | continue; | |
431 | /* The csis node can have only port subnode. */ | |
432 | port = of_get_next_child(node, NULL); | |
433 | if (!port) | |
434 | continue; | |
435 | ||
436 | ret = fimc_md_parse_port_node(fmd, port, index); | |
437 | if (ret < 0) | |
438 | return ret; | |
439 | index++; | |
440 | } | |
441 | ||
442 | /* Attach sensors listed in the parallel-ports node */ | |
443 | ports = of_get_child_by_name(parent, "parallel-ports"); | |
444 | if (!ports) | |
445 | return 0; | |
446 | ||
447 | for_each_child_of_node(ports, node) { | |
448 | ret = fimc_md_parse_port_node(fmd, node, index); | |
449 | if (ret < 0) | |
450 | break; | |
451 | index++; | |
452 | } | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
e2985a26 SN |
457 | static int __of_get_csis_id(struct device_node *np) |
458 | { | |
459 | u32 reg = 0; | |
460 | ||
461 | np = of_get_child_by_name(np, "port"); | |
462 | if (!np) | |
463 | return -EINVAL; | |
464 | of_property_read_u32(np, "reg", ®); | |
465 | return reg - FIMC_INPUT_MIPI_CSI2_0; | |
466 | } | |
467 | #else | |
2b13f7d4 | 468 | #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS) |
e2985a26 SN |
469 | #define __of_get_csis_id(np) (-ENOSYS) |
470 | #endif | |
471 | ||
d3953223 SN |
472 | static int fimc_md_register_sensor_entities(struct fimc_md *fmd) |
473 | { | |
474 | struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data; | |
2b13f7d4 | 475 | struct device_node *of_node = fmd->pdev->dev.of_node; |
d3953223 | 476 | struct fimc_dev *fd = NULL; |
2b13f7d4 SN |
477 | int num_clients = 0; |
478 | int ret, i; | |
d3953223 SN |
479 | |
480 | /* | |
481 | * Runtime resume one of the FIMC entities to make sure | |
482 | * the sclk_cam clocks are not globally disabled. | |
483 | */ | |
484 | for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++) | |
485 | if (fmd->fimc[i]) | |
486 | fd = fmd->fimc[i]; | |
487 | if (!fd) | |
488 | return -ENXIO; | |
2b13f7d4 | 489 | |
d3953223 SN |
490 | ret = pm_runtime_get_sync(&fd->pdev->dev); |
491 | if (ret < 0) | |
492 | return ret; | |
493 | ||
2b13f7d4 SN |
494 | if (of_node) { |
495 | fmd->num_sensors = 0; | |
496 | ret = fimc_md_of_sensors_register(fmd, of_node); | |
497 | } else if (pdata) { | |
498 | WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor)); | |
499 | num_clients = min_t(u32, pdata->num_clients, | |
500 | ARRAY_SIZE(fmd->sensor)); | |
501 | fmd->num_sensors = num_clients; | |
d3953223 | 502 | |
2b13f7d4 SN |
503 | for (i = 0; i < num_clients; i++) { |
504 | struct v4l2_subdev *sd; | |
ecd9acbf | 505 | |
2b13f7d4 SN |
506 | fmd->sensor[i].pdata = pdata->source_info[i]; |
507 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true); | |
508 | if (ret) | |
509 | break; | |
510 | sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]); | |
511 | ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false); | |
512 | ||
513 | if (IS_ERR(sd)) { | |
514 | fmd->sensor[i].subdev = NULL; | |
515 | ret = PTR_ERR(sd); | |
516 | break; | |
517 | } | |
ecd9acbf | 518 | fmd->sensor[i].subdev = sd; |
2b13f7d4 SN |
519 | if (ret) |
520 | break; | |
ecd9acbf | 521 | } |
d3953223 | 522 | } |
2b13f7d4 | 523 | |
d3953223 SN |
524 | pm_runtime_put(&fd->pdev->dev); |
525 | return ret; | |
526 | } | |
527 | ||
528 | /* | |
7b43a6f3 | 529 | * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration. |
d3953223 | 530 | */ |
7b43a6f3 SN |
531 | |
532 | static int register_fimc_lite_entity(struct fimc_md *fmd, | |
533 | struct fimc_lite *fimc_lite) | |
d3953223 | 534 | { |
8163ec0b | 535 | struct v4l2_subdev *sd; |
afd7348c | 536 | int ret; |
4af81310 | 537 | |
7b43a6f3 SN |
538 | if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS || |
539 | fmd->fimc_lite[fimc_lite->index])) | |
540 | return -EBUSY; | |
d3953223 | 541 | |
7b43a6f3 SN |
542 | sd = &fimc_lite->subdev; |
543 | sd->grp_id = GRP_ID_FLITE; | |
97d66c47 | 544 | v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops); |
693f5c40 SN |
545 | |
546 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); | |
7b43a6f3 SN |
547 | if (!ret) |
548 | fmd->fimc_lite[fimc_lite->index] = fimc_lite; | |
549 | else | |
550 | v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n", | |
551 | fimc_lite->index); | |
552 | return ret; | |
d3953223 SN |
553 | } |
554 | ||
7b43a6f3 | 555 | static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc) |
4af81310 | 556 | { |
7b43a6f3 | 557 | struct v4l2_subdev *sd; |
4af81310 SN |
558 | int ret; |
559 | ||
7b43a6f3 SN |
560 | if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id])) |
561 | return -EBUSY; | |
4af81310 | 562 | |
7b43a6f3 SN |
563 | sd = &fimc->vid_cap.subdev; |
564 | sd->grp_id = GRP_ID_FIMC; | |
565 | v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops); | |
4af81310 | 566 | |
7b43a6f3 SN |
567 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
568 | if (!ret) { | |
569 | fmd->fimc[fimc->id] = fimc; | |
570 | fimc->vid_cap.user_subdev_api = fmd->user_subdev_api; | |
571 | } else { | |
572 | v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n", | |
573 | fimc->id, ret); | |
4af81310 | 574 | } |
7b43a6f3 | 575 | return ret; |
4af81310 SN |
576 | } |
577 | ||
7b43a6f3 SN |
578 | static int register_csis_entity(struct fimc_md *fmd, |
579 | struct platform_device *pdev, | |
580 | struct v4l2_subdev *sd) | |
d3953223 | 581 | { |
7b43a6f3 | 582 | struct device_node *node = pdev->dev.of_node; |
d3953223 SN |
583 | int id, ret; |
584 | ||
e2985a26 | 585 | id = node ? __of_get_csis_id(node) : max(0, pdev->id); |
7b43a6f3 | 586 | |
e2985a26 SN |
587 | if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES)) |
588 | return -ENOENT; | |
7b43a6f3 | 589 | |
e2985a26 SN |
590 | if (WARN_ON(fmd->csis[id].sd)) |
591 | return -EBUSY; | |
d3953223 | 592 | |
588c87be | 593 | sd->grp_id = GRP_ID_CSIS; |
d3953223 | 594 | ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd); |
afd7348c SN |
595 | if (!ret) |
596 | fmd->csis[id].sd = sd; | |
597 | else | |
d3953223 | 598 | v4l2_err(&fmd->v4l2_dev, |
7b43a6f3 | 599 | "Failed to register MIPI-CSIS.%d (%d)\n", id, ret); |
d3953223 SN |
600 | return ret; |
601 | } | |
602 | ||
7b43a6f3 SN |
603 | static int fimc_md_register_platform_entity(struct fimc_md *fmd, |
604 | struct platform_device *pdev, | |
605 | int plat_entity) | |
d3953223 | 606 | { |
7b43a6f3 SN |
607 | struct device *dev = &pdev->dev; |
608 | int ret = -EPROBE_DEFER; | |
609 | void *drvdata; | |
610 | ||
611 | /* Lock to ensure dev->driver won't change. */ | |
612 | device_lock(dev); | |
613 | ||
614 | if (!dev->driver || !try_module_get(dev->driver->owner)) | |
615 | goto dev_unlock; | |
616 | ||
617 | drvdata = dev_get_drvdata(dev); | |
618 | /* Some subdev didn't probe succesfully id drvdata is NULL */ | |
619 | if (drvdata) { | |
620 | switch (plat_entity) { | |
621 | case IDX_FIMC: | |
622 | ret = register_fimc_entity(fmd, drvdata); | |
623 | break; | |
624 | case IDX_FLITE: | |
625 | ret = register_fimc_lite_entity(fmd, drvdata); | |
ecd9acbf | 626 | break; |
7b43a6f3 SN |
627 | case IDX_CSIS: |
628 | ret = register_csis_entity(fmd, pdev, drvdata); | |
629 | break; | |
630 | default: | |
631 | ret = -ENODEV; | |
ecd9acbf SN |
632 | } |
633 | } | |
d3953223 | 634 | |
7b43a6f3 SN |
635 | module_put(dev->driver->owner); |
636 | dev_unlock: | |
637 | device_unlock(dev); | |
638 | if (ret == -EPROBE_DEFER) | |
639 | dev_info(&fmd->pdev->dev, "deferring %s device registration\n", | |
640 | dev_name(dev)); | |
641 | else if (ret < 0) | |
642 | dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n", | |
643 | dev_name(dev), ret); | |
644 | return ret; | |
645 | } | |
646 | ||
647 | static int fimc_md_pdev_match(struct device *dev, void *data) | |
648 | { | |
649 | struct platform_device *pdev = to_platform_device(dev); | |
650 | int plat_entity = -1; | |
651 | int ret; | |
652 | char *p; | |
653 | ||
654 | if (!get_device(dev)) | |
655 | return -ENODEV; | |
656 | ||
657 | if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) { | |
658 | plat_entity = IDX_CSIS; | |
659 | } else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) { | |
660 | plat_entity = IDX_FLITE; | |
661 | } else { | |
662 | p = strstr(pdev->name, "fimc"); | |
663 | if (p && *(p + 4) == 0) | |
664 | plat_entity = IDX_FIMC; | |
ecd9acbf SN |
665 | } |
666 | ||
7b43a6f3 SN |
667 | if (plat_entity >= 0) |
668 | ret = fimc_md_register_platform_entity(data, pdev, | |
669 | plat_entity); | |
670 | put_device(dev); | |
671 | return 0; | |
d3953223 SN |
672 | } |
673 | ||
e2985a26 SN |
674 | /* Register FIMC, FIMC-LITE and CSIS media entities */ |
675 | #ifdef CONFIG_OF | |
676 | static int fimc_md_register_of_platform_entities(struct fimc_md *fmd, | |
677 | struct device_node *parent) | |
678 | { | |
679 | struct device_node *node; | |
680 | int ret = 0; | |
681 | ||
682 | for_each_available_child_of_node(parent, node) { | |
683 | struct platform_device *pdev; | |
684 | int plat_entity = -1; | |
685 | ||
686 | pdev = of_find_device_by_node(node); | |
687 | if (!pdev) | |
688 | continue; | |
689 | ||
690 | /* If driver of any entity isn't ready try all again later. */ | |
691 | if (!strcmp(node->name, CSIS_OF_NODE_NAME)) | |
692 | plat_entity = IDX_CSIS; | |
693 | else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME)) | |
694 | plat_entity = IDX_FLITE; | |
695 | else if (!strcmp(node->name, FIMC_OF_NODE_NAME) && | |
696 | !of_property_read_bool(node, "samsung,lcd-wb")) | |
697 | plat_entity = IDX_FIMC; | |
698 | ||
699 | if (plat_entity >= 0) | |
700 | ret = fimc_md_register_platform_entity(fmd, pdev, | |
701 | plat_entity); | |
702 | put_device(&pdev->dev); | |
703 | if (ret < 0) | |
704 | break; | |
705 | } | |
706 | ||
707 | return ret; | |
708 | } | |
709 | #else | |
710 | #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS) | |
711 | #endif | |
712 | ||
d3953223 SN |
713 | static void fimc_md_unregister_entities(struct fimc_md *fmd) |
714 | { | |
715 | int i; | |
716 | ||
717 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
718 | if (fmd->fimc[i] == NULL) | |
719 | continue; | |
693f5c40 | 720 | v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev); |
b9ee31e6 | 721 | fmd->fimc[i]->pipeline_ops = NULL; |
d3953223 SN |
722 | fmd->fimc[i] = NULL; |
723 | } | |
4af81310 SN |
724 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { |
725 | if (fmd->fimc_lite[i] == NULL) | |
726 | continue; | |
727 | v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev); | |
248ac368 | 728 | fmd->fimc_lite[i]->pipeline_ops = NULL; |
4af81310 SN |
729 | fmd->fimc_lite[i] = NULL; |
730 | } | |
d3953223 SN |
731 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
732 | if (fmd->csis[i].sd == NULL) | |
733 | continue; | |
734 | v4l2_device_unregister_subdev(fmd->csis[i].sd); | |
ecd9acbf | 735 | module_put(fmd->csis[i].sd->owner); |
d3953223 SN |
736 | fmd->csis[i].sd = NULL; |
737 | } | |
738 | for (i = 0; i < fmd->num_sensors; i++) { | |
739 | if (fmd->sensor[i].subdev == NULL) | |
740 | continue; | |
741 | fimc_md_unregister_sensor(fmd->sensor[i].subdev); | |
742 | fmd->sensor[i].subdev = NULL; | |
743 | } | |
7b43a6f3 | 744 | v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n"); |
d3953223 SN |
745 | } |
746 | ||
d3953223 SN |
747 | /** |
748 | * __fimc_md_create_fimc_links - create links to all FIMC entities | |
749 | * @fmd: fimc media device | |
750 | * @source: the source entity to create links to all fimc entities from | |
751 | * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null | |
752 | * @pad: the source entity pad index | |
d0da3c35 | 753 | * @link_mask: bitmask of the fimc devices for which link should be enabled |
d3953223 | 754 | */ |
4af81310 SN |
755 | static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd, |
756 | struct media_entity *source, | |
757 | struct v4l2_subdev *sensor, | |
d0da3c35 | 758 | int pad, int link_mask) |
d3953223 | 759 | { |
56bc911a | 760 | struct fimc_sensor_info *s_info = NULL; |
d3953223 | 761 | struct media_entity *sink; |
4af81310 | 762 | unsigned int flags = 0; |
237e0265 | 763 | int ret, i; |
d3953223 SN |
764 | |
765 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
766 | if (!fmd->fimc[i]) | |
4af81310 | 767 | continue; |
d3953223 SN |
768 | /* |
769 | * Some FIMC variants are not fitted with camera capture | |
770 | * interface. Skip creating a link from sensor for those. | |
771 | */ | |
4af81310 | 772 | if (!fmd->fimc[i]->variant->has_cam_if) |
d3953223 SN |
773 | continue; |
774 | ||
d0da3c35 | 775 | flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0; |
4af81310 | 776 | |
693f5c40 | 777 | sink = &fmd->fimc[i]->vid_cap.subdev.entity; |
237e0265 SN |
778 | ret = media_entity_create_link(source, pad, sink, |
779 | FIMC_SD_PAD_SINK, flags); | |
d3953223 SN |
780 | if (ret) |
781 | return ret; | |
782 | ||
237e0265 SN |
783 | /* Notify FIMC capture subdev entity */ |
784 | ret = media_entity_call(sink, link_setup, &sink->pads[0], | |
785 | &source->pads[pad], flags); | |
786 | if (ret) | |
787 | break; | |
788 | ||
542fb082 | 789 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n", |
d3953223 SN |
790 | source->name, flags ? '=' : '-', sink->name); |
791 | ||
4af81310 | 792 | if (flags == 0 || sensor == NULL) |
d3953223 SN |
793 | continue; |
794 | s_info = v4l2_get_subdev_hostdata(sensor); | |
795 | if (!WARN_ON(s_info == NULL)) { | |
796 | unsigned long irq_flags; | |
797 | spin_lock_irqsave(&fmd->slock, irq_flags); | |
798 | s_info->host = fmd->fimc[i]; | |
799 | spin_unlock_irqrestore(&fmd->slock, irq_flags); | |
800 | } | |
801 | } | |
4af81310 SN |
802 | |
803 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { | |
804 | if (!fmd->fimc_lite[i]) | |
805 | continue; | |
806 | ||
d0da3c35 SN |
807 | if (link_mask & (1 << (i + FIMC_MAX_DEVS))) |
808 | flags = MEDIA_LNK_FL_ENABLED; | |
809 | else | |
810 | flags = 0; | |
4af81310 SN |
811 | |
812 | sink = &fmd->fimc_lite[i]->subdev.entity; | |
813 | ret = media_entity_create_link(source, pad, sink, | |
814 | FLITE_SD_PAD_SINK, flags); | |
815 | if (ret) | |
816 | return ret; | |
817 | ||
818 | /* Notify FIMC-LITE subdev entity */ | |
819 | ret = media_entity_call(sink, link_setup, &sink->pads[0], | |
820 | &source->pads[pad], flags); | |
821 | if (ret) | |
822 | break; | |
823 | ||
969e877c | 824 | v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n", |
4af81310 SN |
825 | source->name, flags ? '=' : '-', sink->name); |
826 | } | |
d3953223 SN |
827 | return 0; |
828 | } | |
829 | ||
4af81310 SN |
830 | /* Create links from FIMC-LITE source pads to other entities */ |
831 | static int __fimc_md_create_flite_source_links(struct fimc_md *fmd) | |
832 | { | |
833 | struct media_entity *source, *sink; | |
834 | unsigned int flags = MEDIA_LNK_FL_ENABLED; | |
a26860bd | 835 | int i, ret = 0; |
4af81310 SN |
836 | |
837 | for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) { | |
838 | struct fimc_lite *fimc = fmd->fimc_lite[i]; | |
839 | if (fimc == NULL) | |
840 | continue; | |
841 | source = &fimc->subdev.entity; | |
1bcd7041 | 842 | sink = &fimc->vfd.entity; |
4af81310 | 843 | /* FIMC-LITE's subdev and video node */ |
6319d6a0 | 844 | ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA, |
4af81310 SN |
845 | sink, 0, flags); |
846 | if (ret) | |
847 | break; | |
848 | /* TODO: create links to other entities */ | |
849 | } | |
850 | ||
851 | return ret; | |
852 | } | |
853 | ||
d3953223 SN |
854 | /** |
855 | * fimc_md_create_links - create default links between registered entities | |
856 | * | |
857 | * Parallel interface sensor entities are connected directly to FIMC capture | |
858 | * entities. The sensors using MIPI CSIS bus are connected through immutable | |
859 | * link with CSI receiver entity specified by mux_id. Any registered CSIS | |
860 | * entity has a link to each registered FIMC capture entity. Enabled links | |
861 | * are created by default between each subsequent registered sensor and | |
862 | * subsequent FIMC capture entity. The number of default active links is | |
863 | * determined by the number of available sensors or FIMC entities, | |
864 | * whichever is less. | |
865 | */ | |
866 | static int fimc_md_create_links(struct fimc_md *fmd) | |
867 | { | |
a8697ec8 | 868 | struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL }; |
d3953223 | 869 | struct v4l2_subdev *sensor, *csis; |
56bc911a | 870 | struct fimc_source_info *pdata; |
d3953223 | 871 | struct fimc_sensor_info *s_info; |
237e0265 | 872 | struct media_entity *source, *sink; |
d0da3c35 SN |
873 | int i, pad, fimc_id = 0, ret = 0; |
874 | u32 flags, link_mask = 0; | |
d3953223 SN |
875 | |
876 | for (i = 0; i < fmd->num_sensors; i++) { | |
877 | if (fmd->sensor[i].subdev == NULL) | |
878 | continue; | |
879 | ||
880 | sensor = fmd->sensor[i].subdev; | |
881 | s_info = v4l2_get_subdev_hostdata(sensor); | |
6612a082 | 882 | if (!s_info) |
d3953223 SN |
883 | continue; |
884 | ||
885 | source = NULL; | |
6612a082 | 886 | pdata = &s_info->pdata; |
d3953223 | 887 | |
56bc911a SN |
888 | switch (pdata->sensor_bus_type) { |
889 | case FIMC_BUS_TYPE_MIPI_CSI2: | |
d3953223 SN |
890 | if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES, |
891 | "Wrong CSI channel id: %d\n", pdata->mux_id)) | |
892 | return -EINVAL; | |
893 | ||
894 | csis = fmd->csis[pdata->mux_id].sd; | |
895 | if (WARN(csis == NULL, | |
896 | "MIPI-CSI interface specified " | |
897 | "but s5p-csis module is not loaded!\n")) | |
d12392ec | 898 | return -EINVAL; |
d3953223 | 899 | |
1c9f5bd7 AH |
900 | pad = sensor->entity.num_pads - 1; |
901 | ret = media_entity_create_link(&sensor->entity, pad, | |
d3953223 SN |
902 | &csis->entity, CSIS_PAD_SINK, |
903 | MEDIA_LNK_FL_IMMUTABLE | | |
904 | MEDIA_LNK_FL_ENABLED); | |
905 | if (ret) | |
906 | return ret; | |
907 | ||
969e877c | 908 | v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n", |
d3953223 SN |
909 | sensor->entity.name, csis->entity.name); |
910 | ||
4af81310 | 911 | source = NULL; |
5d33ee92 | 912 | csi_sensors[pdata->mux_id] = sensor; |
d3953223 SN |
913 | break; |
914 | ||
56bc911a | 915 | case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656: |
d3953223 SN |
916 | source = &sensor->entity; |
917 | pad = 0; | |
918 | break; | |
919 | ||
920 | default: | |
921 | v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n", | |
56bc911a | 922 | pdata->sensor_bus_type); |
d3953223 SN |
923 | return -EINVAL; |
924 | } | |
925 | if (source == NULL) | |
926 | continue; | |
927 | ||
d0da3c35 | 928 | link_mask = 1 << fimc_id++; |
4af81310 | 929 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
d0da3c35 | 930 | pad, link_mask); |
4af81310 SN |
931 | } |
932 | ||
a8697ec8 | 933 | for (i = 0; i < CSIS_MAX_ENTITIES; i++) { |
4af81310 SN |
934 | if (fmd->csis[i].sd == NULL) |
935 | continue; | |
936 | source = &fmd->csis[i].sd->entity; | |
937 | pad = CSIS_PAD_SOURCE; | |
5d33ee92 | 938 | sensor = csi_sensors[i]; |
4af81310 | 939 | |
d0da3c35 | 940 | link_mask = 1 << fimc_id++; |
5d33ee92 | 941 | ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor, |
d0da3c35 | 942 | pad, link_mask); |
d3953223 | 943 | } |
4af81310 | 944 | |
237e0265 SN |
945 | /* Create immutable links between each FIMC's subdev and video node */ |
946 | flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED; | |
947 | for (i = 0; i < FIMC_MAX_DEVS; i++) { | |
948 | if (!fmd->fimc[i]) | |
949 | continue; | |
693f5c40 | 950 | source = &fmd->fimc[i]->vid_cap.subdev.entity; |
31d34d9b | 951 | sink = &fmd->fimc[i]->vid_cap.vfd.entity; |
237e0265 SN |
952 | ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE, |
953 | sink, 0, flags); | |
954 | if (ret) | |
955 | break; | |
956 | } | |
957 | ||
4af81310 | 958 | return __fimc_md_create_flite_source_links(fmd); |
d3953223 SN |
959 | } |
960 | ||
961 | /* | |
962 | * The peripheral sensor clock management. | |
963 | */ | |
0e23cbbe SN |
964 | static void fimc_md_put_clocks(struct fimc_md *fmd) |
965 | { | |
966 | int i = FIMC_MAX_CAMCLKS; | |
967 | ||
968 | while (--i >= 0) { | |
969 | if (IS_ERR(fmd->camclk[i].clock)) | |
970 | continue; | |
971 | clk_unprepare(fmd->camclk[i].clock); | |
972 | clk_put(fmd->camclk[i].clock); | |
973 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); | |
974 | } | |
975 | } | |
976 | ||
d3953223 SN |
977 | static int fimc_md_get_clocks(struct fimc_md *fmd) |
978 | { | |
0e23cbbe | 979 | struct device *dev = NULL; |
d3953223 SN |
980 | char clk_name[32]; |
981 | struct clk *clock; | |
0e23cbbe SN |
982 | int ret, i; |
983 | ||
984 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) | |
985 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); | |
986 | ||
987 | if (fmd->pdev->dev.of_node) | |
988 | dev = &fmd->pdev->dev; | |
d3953223 SN |
989 | |
990 | for (i = 0; i < FIMC_MAX_CAMCLKS; i++) { | |
991 | snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i); | |
0e23cbbe SN |
992 | clock = clk_get(dev, clk_name); |
993 | ||
dc3ae328 | 994 | if (IS_ERR(clock)) { |
0e23cbbe SN |
995 | dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n", |
996 | clk_name); | |
997 | ret = PTR_ERR(clock); | |
998 | break; | |
999 | } | |
1000 | ret = clk_prepare(clock); | |
1001 | if (ret < 0) { | |
1002 | clk_put(clock); | |
1003 | fmd->camclk[i].clock = ERR_PTR(-EINVAL); | |
1004 | break; | |
d3953223 SN |
1005 | } |
1006 | fmd->camclk[i].clock = clock; | |
1007 | } | |
0e23cbbe SN |
1008 | if (ret) |
1009 | fimc_md_put_clocks(fmd); | |
d3953223 | 1010 | |
0e23cbbe | 1011 | return ret; |
d3953223 SN |
1012 | } |
1013 | ||
1014 | static int __fimc_md_set_camclk(struct fimc_md *fmd, | |
e3fc82e8 SN |
1015 | struct fimc_sensor_info *s_info, |
1016 | bool on) | |
d3953223 | 1017 | { |
56bc911a | 1018 | struct fimc_source_info *pdata = &s_info->pdata; |
d3953223 SN |
1019 | struct fimc_camclk_info *camclk; |
1020 | int ret = 0; | |
1021 | ||
1022 | if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL) | |
1023 | return -EINVAL; | |
1024 | ||
d3953223 SN |
1025 | camclk = &fmd->camclk[pdata->clk_id]; |
1026 | ||
e3fc82e8 SN |
1027 | dbg("camclk %d, f: %lu, use_count: %d, on: %d", |
1028 | pdata->clk_id, pdata->clk_frequency, camclk->use_count, on); | |
d3953223 SN |
1029 | |
1030 | if (on) { | |
1031 | if (camclk->use_count > 0 && | |
1032 | camclk->frequency != pdata->clk_frequency) | |
1033 | return -EINVAL; | |
1034 | ||
1035 | if (camclk->use_count++ == 0) { | |
1036 | clk_set_rate(camclk->clock, pdata->clk_frequency); | |
1037 | camclk->frequency = pdata->clk_frequency; | |
1038 | ret = clk_enable(camclk->clock); | |
e3fc82e8 SN |
1039 | dbg("Enabled camclk %d: f: %lu", pdata->clk_id, |
1040 | clk_get_rate(camclk->clock)); | |
d3953223 | 1041 | } |
d3953223 SN |
1042 | return ret; |
1043 | } | |
1044 | ||
1045 | if (WARN_ON(camclk->use_count == 0)) | |
1046 | return 0; | |
1047 | ||
1048 | if (--camclk->use_count == 0) { | |
1049 | clk_disable(camclk->clock); | |
d3953223 SN |
1050 | dbg("Disabled camclk %d", pdata->clk_id); |
1051 | } | |
1052 | return ret; | |
1053 | } | |
1054 | ||
1055 | /** | |
1056 | * fimc_md_set_camclk - peripheral sensor clock setup | |
1057 | * @sd: sensor subdev to configure sclk_cam clock for | |
1058 | * @on: 1 to enable or 0 to disable the clock | |
1059 | * | |
1060 | * There are 2 separate clock outputs available in the SoC for external | |
1061 | * image processors. These clocks are shared between all registered FIMC | |
1062 | * devices to which sensors can be attached, either directly or through | |
1063 | * the MIPI CSI receiver. The clock is allowed here to be used by | |
1064 | * multiple sensors concurrently if they use same frequency. | |
d3953223 SN |
1065 | * This function should only be called when the graph mutex is held. |
1066 | */ | |
1067 | int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on) | |
1068 | { | |
1069 | struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd); | |
1070 | struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity); | |
1071 | ||
1072 | return __fimc_md_set_camclk(fmd, s_info, on); | |
1073 | } | |
1074 | ||
1075 | static int fimc_md_link_notify(struct media_pad *source, | |
1076 | struct media_pad *sink, u32 flags) | |
1077 | { | |
4af81310 SN |
1078 | struct fimc_lite *fimc_lite = NULL; |
1079 | struct fimc_dev *fimc = NULL; | |
0f735f52 | 1080 | struct fimc_pipeline *pipeline; |
237e0265 | 1081 | struct v4l2_subdev *sd; |
740ad921 | 1082 | struct mutex *lock; |
d3953223 | 1083 | int ret = 0; |
740ad921 | 1084 | int ref_count; |
d3953223 | 1085 | |
237e0265 | 1086 | if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
d3953223 SN |
1087 | return 0; |
1088 | ||
237e0265 | 1089 | sd = media_entity_to_v4l2_subdev(sink->entity); |
d3953223 | 1090 | |
0f735f52 | 1091 | switch (sd->grp_id) { |
588c87be | 1092 | case GRP_ID_FLITE: |
4af81310 | 1093 | fimc_lite = v4l2_get_subdevdata(sd); |
740ad921 SN |
1094 | if (WARN_ON(fimc_lite == NULL)) |
1095 | return 0; | |
4af81310 | 1096 | pipeline = &fimc_lite->pipeline; |
740ad921 | 1097 | lock = &fimc_lite->lock; |
4af81310 | 1098 | break; |
588c87be | 1099 | case GRP_ID_FIMC: |
0f735f52 | 1100 | fimc = v4l2_get_subdevdata(sd); |
740ad921 SN |
1101 | if (WARN_ON(fimc == NULL)) |
1102 | return 0; | |
0f735f52 | 1103 | pipeline = &fimc->pipeline; |
740ad921 | 1104 | lock = &fimc->lock; |
0f735f52 SN |
1105 | break; |
1106 | default: | |
1107 | return 0; | |
1108 | } | |
131b6c61 | 1109 | |
0f735f52 | 1110 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
740ad921 SN |
1111 | int i; |
1112 | mutex_lock(lock); | |
b9ee31e6 | 1113 | ret = __fimc_pipeline_close(pipeline); |
740ad921 SN |
1114 | for (i = 0; i < IDX_MAX; i++) |
1115 | pipeline->subdevs[i] = NULL; | |
1116 | if (fimc) | |
0f735f52 | 1117 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
740ad921 | 1118 | mutex_unlock(lock); |
d3953223 SN |
1119 | return ret; |
1120 | } | |
1121 | /* | |
1122 | * Link activation. Enable power of pipeline elements only if the | |
1123 | * pipeline is already in use, i.e. its video node is opened. | |
131b6c61 | 1124 | * Recreate the controls destroyed during the link deactivation. |
d3953223 | 1125 | */ |
740ad921 SN |
1126 | mutex_lock(lock); |
1127 | ||
1128 | ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count; | |
1129 | if (ref_count > 0) | |
1130 | ret = __fimc_pipeline_open(pipeline, source->entity, true); | |
1131 | if (!ret && fimc) | |
1132 | ret = fimc_capture_ctrls_create(fimc); | |
1133 | ||
1134 | mutex_unlock(lock); | |
d3953223 SN |
1135 | return ret ? -EPIPE : ret; |
1136 | } | |
1137 | ||
1138 | static ssize_t fimc_md_sysfs_show(struct device *dev, | |
1139 | struct device_attribute *attr, char *buf) | |
1140 | { | |
1141 | struct platform_device *pdev = to_platform_device(dev); | |
1142 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
1143 | ||
1144 | if (fmd->user_subdev_api) | |
1145 | return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE); | |
1146 | ||
1147 | return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE); | |
1148 | } | |
1149 | ||
1150 | static ssize_t fimc_md_sysfs_store(struct device *dev, | |
1151 | struct device_attribute *attr, | |
1152 | const char *buf, size_t count) | |
1153 | { | |
1154 | struct platform_device *pdev = to_platform_device(dev); | |
1155 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
1156 | bool subdev_api; | |
1157 | int i; | |
1158 | ||
1159 | if (!strcmp(buf, "vid-dev\n")) | |
1160 | subdev_api = false; | |
1161 | else if (!strcmp(buf, "sub-dev\n")) | |
1162 | subdev_api = true; | |
1163 | else | |
1164 | return count; | |
1165 | ||
1166 | fmd->user_subdev_api = subdev_api; | |
1167 | for (i = 0; i < FIMC_MAX_DEVS; i++) | |
1168 | if (fmd->fimc[i]) | |
1169 | fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api; | |
1170 | return count; | |
1171 | } | |
1172 | /* | |
1173 | * This device attribute is to select video pipeline configuration method. | |
1174 | * There are following valid values: | |
1175 | * vid-dev - for V4L2 video node API only, subdevice will be configured | |
1176 | * by the host driver. | |
1177 | * sub-dev - for media controller API, subdevs must be configured in user | |
1178 | * space before starting streaming. | |
1179 | */ | |
1180 | static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO, | |
1181 | fimc_md_sysfs_show, fimc_md_sysfs_store); | |
1182 | ||
4163851f SN |
1183 | static int fimc_md_get_pinctrl(struct fimc_md *fmd) |
1184 | { | |
1185 | struct device *dev = &fmd->pdev->dev; | |
1186 | struct fimc_pinctrl *pctl = &fmd->pinctl; | |
1187 | ||
1188 | pctl->pinctrl = devm_pinctrl_get(dev); | |
1189 | if (IS_ERR(pctl->pinctrl)) | |
1190 | return PTR_ERR(pctl->pinctrl); | |
1191 | ||
1192 | pctl->state_default = pinctrl_lookup_state(pctl->pinctrl, | |
1193 | PINCTRL_STATE_DEFAULT); | |
1194 | if (IS_ERR(pctl->state_default)) | |
1195 | return PTR_ERR(pctl->state_default); | |
1196 | ||
1197 | pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl, | |
1198 | PINCTRL_STATE_IDLE); | |
1199 | return 0; | |
1200 | } | |
1201 | ||
ecd9acbf | 1202 | static int fimc_md_probe(struct platform_device *pdev) |
d3953223 | 1203 | { |
e2985a26 | 1204 | struct device *dev = &pdev->dev; |
d3953223 SN |
1205 | struct v4l2_device *v4l2_dev; |
1206 | struct fimc_md *fmd; | |
1207 | int ret; | |
1208 | ||
e2985a26 | 1209 | fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL); |
d3953223 SN |
1210 | if (!fmd) |
1211 | return -ENOMEM; | |
1212 | ||
1213 | spin_lock_init(&fmd->slock); | |
1214 | fmd->pdev = pdev; | |
1215 | ||
1216 | strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC", | |
1217 | sizeof(fmd->media_dev.model)); | |
1218 | fmd->media_dev.link_notify = fimc_md_link_notify; | |
e2985a26 | 1219 | fmd->media_dev.dev = dev; |
d3953223 SN |
1220 | |
1221 | v4l2_dev = &fmd->v4l2_dev; | |
1222 | v4l2_dev->mdev = &fmd->media_dev; | |
e1d72f4d | 1223 | v4l2_dev->notify = fimc_sensor_notify; |
e2985a26 | 1224 | strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name)); |
d3953223 | 1225 | |
e2985a26 | 1226 | ret = v4l2_device_register(dev, &fmd->v4l2_dev); |
d3953223 SN |
1227 | if (ret < 0) { |
1228 | v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret); | |
6d91a51a | 1229 | return ret; |
d3953223 SN |
1230 | } |
1231 | ret = media_device_register(&fmd->media_dev); | |
1232 | if (ret < 0) { | |
1233 | v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret); | |
693f5c40 | 1234 | goto err_md; |
d3953223 SN |
1235 | } |
1236 | ret = fimc_md_get_clocks(fmd); | |
1237 | if (ret) | |
693f5c40 | 1238 | goto err_clk; |
d3953223 | 1239 | |
e2985a26 | 1240 | fmd->user_subdev_api = (dev->of_node != NULL); |
693f5c40 SN |
1241 | |
1242 | /* Protect the media graph while we're registering entities */ | |
1243 | mutex_lock(&fmd->media_dev.graph_mutex); | |
1244 | ||
4163851f SN |
1245 | ret = fimc_md_get_pinctrl(fmd); |
1246 | if (ret < 0) { | |
1247 | if (ret != EPROBE_DEFER) | |
1248 | dev_err(dev, "Failed to get pinctrl: %d\n", ret); | |
1249 | goto err_unlock; | |
1250 | } | |
1251 | ||
e2985a26 SN |
1252 | if (dev->of_node) |
1253 | ret = fimc_md_register_of_platform_entities(fmd, dev->of_node); | |
1254 | else | |
1255 | ret = bus_for_each_dev(&platform_bus_type, NULL, fmd, | |
1256 | fimc_md_pdev_match); | |
d3953223 | 1257 | if (ret) |
693f5c40 | 1258 | goto err_unlock; |
d3953223 | 1259 | |
2b13f7d4 | 1260 | if (dev->platform_data || dev->of_node) { |
5cbf6f16 SN |
1261 | ret = fimc_md_register_sensor_entities(fmd); |
1262 | if (ret) | |
693f5c40 | 1263 | goto err_unlock; |
5cbf6f16 | 1264 | } |
e2985a26 | 1265 | |
d3953223 SN |
1266 | ret = fimc_md_create_links(fmd); |
1267 | if (ret) | |
693f5c40 | 1268 | goto err_unlock; |
d3953223 SN |
1269 | ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev); |
1270 | if (ret) | |
693f5c40 | 1271 | goto err_unlock; |
d3953223 SN |
1272 | |
1273 | ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode); | |
693f5c40 SN |
1274 | if (ret) |
1275 | goto err_unlock; | |
1276 | ||
1277 | platform_set_drvdata(pdev, fmd); | |
1278 | mutex_unlock(&fmd->media_dev.graph_mutex); | |
1279 | return 0; | |
1280 | ||
1281 | err_unlock: | |
1282 | mutex_unlock(&fmd->media_dev.graph_mutex); | |
1283 | err_clk: | |
d3953223 SN |
1284 | media_device_unregister(&fmd->media_dev); |
1285 | fimc_md_put_clocks(fmd); | |
1286 | fimc_md_unregister_entities(fmd); | |
693f5c40 | 1287 | err_md: |
d3953223 | 1288 | v4l2_device_unregister(&fmd->v4l2_dev); |
d3953223 SN |
1289 | return ret; |
1290 | } | |
1291 | ||
4c62e976 | 1292 | static int fimc_md_remove(struct platform_device *pdev) |
d3953223 SN |
1293 | { |
1294 | struct fimc_md *fmd = platform_get_drvdata(pdev); | |
1295 | ||
1296 | if (!fmd) | |
1297 | return 0; | |
1298 | device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode); | |
1299 | fimc_md_unregister_entities(fmd); | |
1300 | media_device_unregister(&fmd->media_dev); | |
1301 | fimc_md_put_clocks(fmd); | |
d3953223 SN |
1302 | return 0; |
1303 | } | |
1304 | ||
e2985a26 SN |
1305 | static struct platform_device_id fimc_driver_ids[] __always_unused = { |
1306 | { .name = "s5p-fimc-md" }, | |
1307 | { }, | |
1308 | }; | |
1309 | MODULE_DEVICE_TABLE(platform, fimc_driver_ids); | |
1310 | ||
1311 | static const struct of_device_id fimc_md_of_match[] = { | |
1312 | { .compatible = "samsung,fimc" }, | |
1313 | { }, | |
1314 | }; | |
1315 | MODULE_DEVICE_TABLE(of, fimc_md_of_match); | |
1316 | ||
d3953223 SN |
1317 | static struct platform_driver fimc_md_driver = { |
1318 | .probe = fimc_md_probe, | |
4c62e976 | 1319 | .remove = fimc_md_remove, |
d3953223 | 1320 | .driver = { |
e2985a26 SN |
1321 | .of_match_table = of_match_ptr(fimc_md_of_match), |
1322 | .name = "s5p-fimc-md", | |
1323 | .owner = THIS_MODULE, | |
d3953223 SN |
1324 | } |
1325 | }; | |
1326 | ||
7e566be2 | 1327 | static int __init fimc_md_init(void) |
d3953223 SN |
1328 | { |
1329 | int ret; | |
ecd9acbf | 1330 | |
d3953223 SN |
1331 | request_module("s5p-csis"); |
1332 | ret = fimc_register_driver(); | |
1333 | if (ret) | |
1334 | return ret; | |
ecd9acbf | 1335 | |
d3953223 SN |
1336 | return platform_driver_register(&fimc_md_driver); |
1337 | } | |
7e566be2 SK |
1338 | |
1339 | static void __exit fimc_md_exit(void) | |
d3953223 SN |
1340 | { |
1341 | platform_driver_unregister(&fimc_md_driver); | |
1342 | fimc_unregister_driver(); | |
1343 | } | |
1344 | ||
1345 | module_init(fimc_md_init); | |
1346 | module_exit(fimc_md_exit); | |
1347 | ||
1348 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); | |
1349 | MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver"); | |
1350 | MODULE_LICENSE("GPL"); | |
1351 | MODULE_VERSION("2.0.1"); |