fimc-mdevice.c 26 KB

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