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