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