fimc-mdevice.c 26 KB

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