fimc-mdevice.c 22 KB

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