fimc-mdevice.c 23 KB

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