media-dev.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * S5P/EXYNOS4 SoC series camera host interface media device driver
  3. *
  4. * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
  5. * Author: 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/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/device.h>
  16. #include <linux/errno.h>
  17. #include <linux/i2c.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_graph.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <media/v4l2-async.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-fwnode.h>
  32. #include <media/media-device.h>
  33. #include <media/drv-intf/exynos-fimc.h>
  34. #include "media-dev.h"
  35. #include "fimc-core.h"
  36. #include "fimc-is.h"
  37. #include "fimc-lite.h"
  38. #include "mipi-csis.h"
  39. /* Set up image sensor subdev -> FIMC capture node notifications. */
  40. static void __setup_sensor_notification(struct fimc_md *fmd,
  41. struct v4l2_subdev *sensor,
  42. struct v4l2_subdev *fimc_sd)
  43. {
  44. struct fimc_source_info *src_inf;
  45. struct fimc_sensor_info *md_si;
  46. unsigned long flags;
  47. src_inf = v4l2_get_subdev_hostdata(sensor);
  48. if (!src_inf || WARN_ON(fmd == NULL))
  49. return;
  50. md_si = source_to_sensor_info(src_inf);
  51. spin_lock_irqsave(&fmd->slock, flags);
  52. md_si->host = v4l2_get_subdevdata(fimc_sd);
  53. spin_unlock_irqrestore(&fmd->slock, flags);
  54. }
  55. /**
  56. * fimc_pipeline_prepare - update pipeline information with subdevice pointers
  57. * @p: fimc pipeline
  58. * @me: media entity terminating the pipeline
  59. *
  60. * Caller holds the graph mutex.
  61. */
  62. static void fimc_pipeline_prepare(struct fimc_pipeline *p,
  63. struct media_entity *me)
  64. {
  65. struct fimc_md *fmd = entity_to_fimc_mdev(me);
  66. struct v4l2_subdev *sd;
  67. struct v4l2_subdev *sensor = NULL;
  68. int i;
  69. for (i = 0; i < IDX_MAX; i++)
  70. p->subdevs[i] = NULL;
  71. while (1) {
  72. struct media_pad *pad = NULL;
  73. /* Find remote source pad */
  74. for (i = 0; i < me->num_pads; i++) {
  75. struct media_pad *spad = &me->pads[i];
  76. if (!(spad->flags & MEDIA_PAD_FL_SINK))
  77. continue;
  78. pad = media_entity_remote_pad(spad);
  79. if (pad)
  80. break;
  81. }
  82. if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
  83. break;
  84. sd = media_entity_to_v4l2_subdev(pad->entity);
  85. switch (sd->grp_id) {
  86. case GRP_ID_SENSOR:
  87. sensor = sd;
  88. /* fall through */
  89. case GRP_ID_FIMC_IS_SENSOR:
  90. p->subdevs[IDX_SENSOR] = sd;
  91. break;
  92. case GRP_ID_CSIS:
  93. p->subdevs[IDX_CSIS] = sd;
  94. break;
  95. case GRP_ID_FLITE:
  96. p->subdevs[IDX_FLITE] = sd;
  97. break;
  98. case GRP_ID_FIMC:
  99. p->subdevs[IDX_FIMC] = sd;
  100. break;
  101. case GRP_ID_FIMC_IS:
  102. p->subdevs[IDX_IS_ISP] = sd;
  103. break;
  104. default:
  105. break;
  106. }
  107. me = &sd->entity;
  108. if (me->num_pads == 1)
  109. break;
  110. }
  111. if (sensor && p->subdevs[IDX_FIMC])
  112. __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
  113. }
  114. /**
  115. * __subdev_set_power - change power state of a single subdev
  116. * @sd: subdevice to change power state for
  117. * @on: 1 to enable power or 0 to disable
  118. *
  119. * Return result of s_power subdev operation or -ENXIO if sd argument
  120. * is NULL. Return 0 if the subdevice does not implement s_power.
  121. */
  122. static int __subdev_set_power(struct v4l2_subdev *sd, int on)
  123. {
  124. int *use_count;
  125. int ret;
  126. if (sd == NULL)
  127. return -ENXIO;
  128. use_count = &sd->entity.use_count;
  129. if (on && (*use_count)++ > 0)
  130. return 0;
  131. else if (!on && (*use_count == 0 || --(*use_count) > 0))
  132. return 0;
  133. ret = v4l2_subdev_call(sd, core, s_power, on);
  134. return ret != -ENOIOCTLCMD ? ret : 0;
  135. }
  136. /**
  137. * fimc_pipeline_s_power - change power state of all pipeline subdevs
  138. * @p: fimc device terminating the pipeline
  139. * @on: true to power on, false to power off
  140. *
  141. * Needs to be called with the graph mutex held.
  142. */
  143. static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
  144. {
  145. static const u8 seq[2][IDX_MAX - 1] = {
  146. { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
  147. { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
  148. };
  149. int i, ret = 0;
  150. if (p->subdevs[IDX_SENSOR] == NULL)
  151. return -ENXIO;
  152. for (i = 0; i < IDX_MAX - 1; i++) {
  153. unsigned int idx = seq[on][i];
  154. ret = __subdev_set_power(p->subdevs[idx], on);
  155. if (ret < 0 && ret != -ENXIO)
  156. goto error;
  157. }
  158. return 0;
  159. error:
  160. for (; i >= 0; i--) {
  161. unsigned int idx = seq[on][i];
  162. __subdev_set_power(p->subdevs[idx], !on);
  163. }
  164. return ret;
  165. }
  166. /**
  167. * __fimc_pipeline_enable - enable power of all pipeline subdevs
  168. * and the sensor clock
  169. * @ep: video pipeline structure
  170. * @fmd: fimc media device
  171. *
  172. * Called with the graph mutex held.
  173. */
  174. static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
  175. struct fimc_md *fmd)
  176. {
  177. struct fimc_pipeline *p = to_fimc_pipeline(ep);
  178. int ret;
  179. /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
  180. if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
  181. ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
  182. if (ret < 0)
  183. return ret;
  184. }
  185. ret = fimc_pipeline_s_power(p, 1);
  186. if (!ret)
  187. return 0;
  188. if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
  189. clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
  190. return ret;
  191. }
  192. /**
  193. * __fimc_pipeline_open - update the pipeline information, enable power
  194. * of all pipeline subdevs and the sensor clock
  195. * @ep: fimc device terminating the pipeline
  196. * @me: media entity to start graph walk with
  197. * @prepare: true to walk the current pipeline and acquire all subdevs
  198. *
  199. * Called with the graph mutex held.
  200. */
  201. static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
  202. struct media_entity *me, bool prepare)
  203. {
  204. struct fimc_md *fmd = entity_to_fimc_mdev(me);
  205. struct fimc_pipeline *p = to_fimc_pipeline(ep);
  206. struct v4l2_subdev *sd;
  207. if (WARN_ON(p == NULL || me == NULL))
  208. return -EINVAL;
  209. if (prepare)
  210. fimc_pipeline_prepare(p, me);
  211. sd = p->subdevs[IDX_SENSOR];
  212. if (sd == NULL) {
  213. pr_warn("%s(): No sensor subdev\n", __func__);
  214. /*
  215. * Pipeline open cannot fail so as to make it possible
  216. * for the user space to configure the pipeline.
  217. */
  218. return 0;
  219. }
  220. return __fimc_pipeline_enable(ep, fmd);
  221. }
  222. /**
  223. * __fimc_pipeline_close - disable the sensor clock and pipeline power
  224. * @ep: fimc device terminating the pipeline
  225. *
  226. * Disable power of all subdevs and turn the external sensor clock off.
  227. */
  228. static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
  229. {
  230. struct fimc_pipeline *p = to_fimc_pipeline(ep);
  231. struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
  232. struct fimc_md *fmd;
  233. int ret;
  234. if (sd == NULL) {
  235. pr_warn("%s(): No sensor subdev\n", __func__);
  236. return 0;
  237. }
  238. ret = fimc_pipeline_s_power(p, 0);
  239. fmd = entity_to_fimc_mdev(&sd->entity);
  240. /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
  241. if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
  242. clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
  243. return ret == -ENXIO ? 0 : ret;
  244. }
  245. /**
  246. * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
  247. * @ep: video pipeline structure
  248. * @on: passed as the s_stream() callback argument
  249. */
  250. static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
  251. {
  252. static const u8 seq[2][IDX_MAX] = {
  253. { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
  254. { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
  255. };
  256. struct fimc_pipeline *p = to_fimc_pipeline(ep);
  257. struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
  258. enum fimc_subdev_index sd_id;
  259. int i, ret = 0;
  260. if (p->subdevs[IDX_SENSOR] == NULL) {
  261. if (!fmd->user_subdev_api) {
  262. /*
  263. * Sensor must be already discovered if we
  264. * aren't in the user_subdev_api mode
  265. */
  266. return -ENODEV;
  267. }
  268. /* Get pipeline sink entity */
  269. if (p->subdevs[IDX_FIMC])
  270. sd_id = IDX_FIMC;
  271. else if (p->subdevs[IDX_IS_ISP])
  272. sd_id = IDX_IS_ISP;
  273. else if (p->subdevs[IDX_FLITE])
  274. sd_id = IDX_FLITE;
  275. else
  276. return -ENODEV;
  277. /*
  278. * Sensor could have been linked between open and STREAMON -
  279. * check if this is the case.
  280. */
  281. fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
  282. if (p->subdevs[IDX_SENSOR] == NULL)
  283. return -ENODEV;
  284. ret = __fimc_pipeline_enable(ep, fmd);
  285. if (ret < 0)
  286. return ret;
  287. }
  288. for (i = 0; i < IDX_MAX; i++) {
  289. unsigned int idx = seq[on][i];
  290. ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
  291. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  292. goto error;
  293. }
  294. return 0;
  295. error:
  296. fimc_pipeline_s_power(p, !on);
  297. for (; i >= 0; i--) {
  298. unsigned int idx = seq[on][i];
  299. v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
  300. }
  301. return ret;
  302. }
  303. /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
  304. static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
  305. .open = __fimc_pipeline_open,
  306. .close = __fimc_pipeline_close,
  307. .set_stream = __fimc_pipeline_s_stream,
  308. };
  309. static struct exynos_media_pipeline *fimc_md_pipeline_create(
  310. struct fimc_md *fmd)
  311. {
  312. struct fimc_pipeline *p;
  313. p = kzalloc(sizeof(*p), GFP_KERNEL);
  314. if (!p)
  315. return NULL;
  316. list_add_tail(&p->list, &fmd->pipelines);
  317. p->ep.ops = &fimc_pipeline_ops;
  318. return &p->ep;
  319. }
  320. static void fimc_md_pipelines_free(struct fimc_md *fmd)
  321. {
  322. while (!list_empty(&fmd->pipelines)) {
  323. struct fimc_pipeline *p;
  324. p = list_entry(fmd->pipelines.next, typeof(*p), list);
  325. list_del(&p->list);
  326. kfree(p);
  327. }
  328. }
  329. /* Parse port node and register as a sub-device any sensor specified there. */
  330. static int fimc_md_parse_port_node(struct fimc_md *fmd,
  331. struct device_node *port,
  332. unsigned int index)
  333. {
  334. struct fimc_source_info *pd = &fmd->sensor[index].pdata;
  335. struct device_node *rem, *ep, *np;
  336. struct v4l2_fwnode_endpoint endpoint;
  337. int ret;
  338. /* Assume here a port node can have only one endpoint node. */
  339. ep = of_get_next_child(port, NULL);
  340. if (!ep)
  341. return 0;
  342. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
  343. if (ret) {
  344. of_node_put(ep);
  345. return ret;
  346. }
  347. if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
  348. of_node_put(ep);
  349. return -EINVAL;
  350. }
  351. pd->mux_id = (endpoint.base.port - 1) & 0x1;
  352. rem = of_graph_get_remote_port_parent(ep);
  353. of_node_put(ep);
  354. if (rem == NULL) {
  355. v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
  356. ep);
  357. return 0;
  358. }
  359. if (fimc_input_is_parallel(endpoint.base.port)) {
  360. if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
  361. pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
  362. else
  363. pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
  364. pd->flags = endpoint.bus.parallel.flags;
  365. } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
  366. /*
  367. * MIPI CSI-2: only input mux selection and
  368. * the sensor's clock frequency is needed.
  369. */
  370. pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
  371. } else {
  372. v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
  373. endpoint.base.port, rem);
  374. }
  375. /*
  376. * For FIMC-IS handled sensors, that are placed under i2c-isp device
  377. * node, FIMC is connected to the FIMC-IS through its ISP Writeback
  378. * input. Sensors are attached to the FIMC-LITE hostdata interface
  379. * directly or through MIPI-CSIS, depending on the external media bus
  380. * used. This needs to be handled in a more reliable way, not by just
  381. * checking parent's node name.
  382. */
  383. np = of_get_parent(rem);
  384. if (np && !of_node_cmp(np->name, "i2c-isp"))
  385. pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
  386. else
  387. pd->fimc_bus_type = pd->sensor_bus_type;
  388. if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
  389. of_node_put(rem);
  390. return -EINVAL;
  391. }
  392. fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  393. fmd->sensor[index].asd.match.fwnode = of_fwnode_handle(rem);
  394. fmd->async_subdevs[index] = &fmd->sensor[index].asd;
  395. fmd->num_sensors++;
  396. of_node_put(rem);
  397. return 0;
  398. }
  399. /* Register all SoC external sub-devices */
  400. static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
  401. {
  402. struct device_node *parent = fmd->pdev->dev.of_node;
  403. struct device_node *node, *ports;
  404. int index = 0;
  405. int ret;
  406. /*
  407. * Runtime resume one of the FIMC entities to make sure
  408. * the sclk_cam clocks are not globally disabled.
  409. */
  410. if (!fmd->pmf)
  411. return -ENXIO;
  412. ret = pm_runtime_get_sync(fmd->pmf);
  413. if (ret < 0)
  414. return ret;
  415. fmd->num_sensors = 0;
  416. /* Attach sensors linked to MIPI CSI-2 receivers */
  417. for_each_available_child_of_node(parent, node) {
  418. struct device_node *port;
  419. if (of_node_cmp(node->name, "csis"))
  420. continue;
  421. /* The csis node can have only port subnode. */
  422. port = of_get_next_child(node, NULL);
  423. if (!port)
  424. continue;
  425. ret = fimc_md_parse_port_node(fmd, port, index);
  426. if (ret < 0) {
  427. of_node_put(node);
  428. goto rpm_put;
  429. }
  430. index++;
  431. }
  432. /* Attach sensors listed in the parallel-ports node */
  433. ports = of_get_child_by_name(parent, "parallel-ports");
  434. if (!ports)
  435. goto rpm_put;
  436. for_each_child_of_node(ports, node) {
  437. ret = fimc_md_parse_port_node(fmd, node, index);
  438. if (ret < 0) {
  439. of_node_put(node);
  440. break;
  441. }
  442. index++;
  443. }
  444. rpm_put:
  445. pm_runtime_put(fmd->pmf);
  446. return ret;
  447. }
  448. static int __of_get_csis_id(struct device_node *np)
  449. {
  450. u32 reg = 0;
  451. np = of_get_child_by_name(np, "port");
  452. if (!np)
  453. return -EINVAL;
  454. of_property_read_u32(np, "reg", &reg);
  455. return reg - FIMC_INPUT_MIPI_CSI2_0;
  456. }
  457. /*
  458. * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
  459. */
  460. static int register_fimc_lite_entity(struct fimc_md *fmd,
  461. struct fimc_lite *fimc_lite)
  462. {
  463. struct v4l2_subdev *sd;
  464. struct exynos_media_pipeline *ep;
  465. int ret;
  466. if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
  467. fmd->fimc_lite[fimc_lite->index]))
  468. return -EBUSY;
  469. sd = &fimc_lite->subdev;
  470. sd->grp_id = GRP_ID_FLITE;
  471. ep = fimc_md_pipeline_create(fmd);
  472. if (!ep)
  473. return -ENOMEM;
  474. v4l2_set_subdev_hostdata(sd, ep);
  475. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  476. if (!ret)
  477. fmd->fimc_lite[fimc_lite->index] = fimc_lite;
  478. else
  479. v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
  480. fimc_lite->index);
  481. return ret;
  482. }
  483. static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
  484. {
  485. struct v4l2_subdev *sd;
  486. struct exynos_media_pipeline *ep;
  487. int ret;
  488. if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
  489. return -EBUSY;
  490. sd = &fimc->vid_cap.subdev;
  491. sd->grp_id = GRP_ID_FIMC;
  492. ep = fimc_md_pipeline_create(fmd);
  493. if (!ep)
  494. return -ENOMEM;
  495. v4l2_set_subdev_hostdata(sd, ep);
  496. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  497. if (!ret) {
  498. if (!fmd->pmf && fimc->pdev)
  499. fmd->pmf = &fimc->pdev->dev;
  500. fmd->fimc[fimc->id] = fimc;
  501. fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
  502. } else {
  503. v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
  504. fimc->id, ret);
  505. }
  506. return ret;
  507. }
  508. static int register_csis_entity(struct fimc_md *fmd,
  509. struct platform_device *pdev,
  510. struct v4l2_subdev *sd)
  511. {
  512. struct device_node *node = pdev->dev.of_node;
  513. int id, ret;
  514. id = node ? __of_get_csis_id(node) : max(0, pdev->id);
  515. if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
  516. return -ENOENT;
  517. if (WARN_ON(fmd->csis[id].sd))
  518. return -EBUSY;
  519. sd->grp_id = GRP_ID_CSIS;
  520. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  521. if (!ret)
  522. fmd->csis[id].sd = sd;
  523. else
  524. v4l2_err(&fmd->v4l2_dev,
  525. "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
  526. return ret;
  527. }
  528. static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
  529. {
  530. struct v4l2_subdev *sd = &is->isp.subdev;
  531. struct exynos_media_pipeline *ep;
  532. int ret;
  533. /* Allocate pipeline object for the ISP capture video node. */
  534. ep = fimc_md_pipeline_create(fmd);
  535. if (!ep)
  536. return -ENOMEM;
  537. v4l2_set_subdev_hostdata(sd, ep);
  538. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  539. if (ret) {
  540. v4l2_err(&fmd->v4l2_dev,
  541. "Failed to register FIMC-ISP (%d)\n", ret);
  542. return ret;
  543. }
  544. fmd->fimc_is = is;
  545. return 0;
  546. }
  547. static int fimc_md_register_platform_entity(struct fimc_md *fmd,
  548. struct platform_device *pdev,
  549. int plat_entity)
  550. {
  551. struct device *dev = &pdev->dev;
  552. int ret = -EPROBE_DEFER;
  553. void *drvdata;
  554. /* Lock to ensure dev->driver won't change. */
  555. device_lock(dev);
  556. if (!dev->driver || !try_module_get(dev->driver->owner))
  557. goto dev_unlock;
  558. drvdata = dev_get_drvdata(dev);
  559. /* Some subdev didn't probe successfully id drvdata is NULL */
  560. if (drvdata) {
  561. switch (plat_entity) {
  562. case IDX_FIMC:
  563. ret = register_fimc_entity(fmd, drvdata);
  564. break;
  565. case IDX_FLITE:
  566. ret = register_fimc_lite_entity(fmd, drvdata);
  567. break;
  568. case IDX_CSIS:
  569. ret = register_csis_entity(fmd, pdev, drvdata);
  570. break;
  571. case IDX_IS_ISP:
  572. ret = register_fimc_is_entity(fmd, drvdata);
  573. break;
  574. default:
  575. ret = -ENODEV;
  576. }
  577. }
  578. module_put(dev->driver->owner);
  579. dev_unlock:
  580. device_unlock(dev);
  581. if (ret == -EPROBE_DEFER)
  582. dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
  583. dev_name(dev));
  584. else if (ret < 0)
  585. dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
  586. dev_name(dev), ret);
  587. return ret;
  588. }
  589. /* Register FIMC, FIMC-LITE and CSIS media entities */
  590. static int fimc_md_register_platform_entities(struct fimc_md *fmd,
  591. struct device_node *parent)
  592. {
  593. struct device_node *node;
  594. int ret = 0;
  595. for_each_available_child_of_node(parent, node) {
  596. struct platform_device *pdev;
  597. int plat_entity = -1;
  598. pdev = of_find_device_by_node(node);
  599. if (!pdev)
  600. continue;
  601. /* If driver of any entity isn't ready try all again later. */
  602. if (!strcmp(node->name, CSIS_OF_NODE_NAME))
  603. plat_entity = IDX_CSIS;
  604. else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
  605. plat_entity = IDX_IS_ISP;
  606. else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
  607. plat_entity = IDX_FLITE;
  608. else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
  609. !of_property_read_bool(node, "samsung,lcd-wb"))
  610. plat_entity = IDX_FIMC;
  611. if (plat_entity >= 0)
  612. ret = fimc_md_register_platform_entity(fmd, pdev,
  613. plat_entity);
  614. put_device(&pdev->dev);
  615. if (ret < 0) {
  616. of_node_put(node);
  617. break;
  618. }
  619. }
  620. return ret;
  621. }
  622. static void fimc_md_unregister_entities(struct fimc_md *fmd)
  623. {
  624. int i;
  625. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  626. struct fimc_dev *dev = fmd->fimc[i];
  627. if (dev == NULL)
  628. continue;
  629. v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
  630. dev->vid_cap.ve.pipe = NULL;
  631. fmd->fimc[i] = NULL;
  632. }
  633. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  634. struct fimc_lite *dev = fmd->fimc_lite[i];
  635. if (dev == NULL)
  636. continue;
  637. v4l2_device_unregister_subdev(&dev->subdev);
  638. dev->ve.pipe = NULL;
  639. fmd->fimc_lite[i] = NULL;
  640. }
  641. for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
  642. if (fmd->csis[i].sd == NULL)
  643. continue;
  644. v4l2_device_unregister_subdev(fmd->csis[i].sd);
  645. fmd->csis[i].sd = NULL;
  646. }
  647. if (fmd->fimc_is)
  648. v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
  649. v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
  650. }
  651. /**
  652. * __fimc_md_create_fimc_links - create links to all FIMC entities
  653. * @fmd: fimc media device
  654. * @source: the source entity to create links to all fimc entities from
  655. * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
  656. * @pad: the source entity pad index
  657. * @link_mask: bitmask of the fimc devices for which link should be enabled
  658. */
  659. static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
  660. struct media_entity *source,
  661. struct v4l2_subdev *sensor,
  662. int pad, int link_mask)
  663. {
  664. struct fimc_source_info *si = NULL;
  665. struct media_entity *sink;
  666. unsigned int flags = 0;
  667. int i, ret = 0;
  668. if (sensor) {
  669. si = v4l2_get_subdev_hostdata(sensor);
  670. /* Skip direct FIMC links in the logical FIMC-IS sensor path */
  671. if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
  672. ret = 1;
  673. }
  674. for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
  675. if (!fmd->fimc[i])
  676. continue;
  677. /*
  678. * Some FIMC variants are not fitted with camera capture
  679. * interface. Skip creating a link from sensor for those.
  680. */
  681. if (!fmd->fimc[i]->variant->has_cam_if)
  682. continue;
  683. flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
  684. sink = &fmd->fimc[i]->vid_cap.subdev.entity;
  685. ret = media_create_pad_link(source, pad, sink,
  686. FIMC_SD_PAD_SINK_CAM, flags);
  687. if (ret)
  688. return ret;
  689. /* Notify FIMC capture subdev entity */
  690. ret = media_entity_call(sink, link_setup, &sink->pads[0],
  691. &source->pads[pad], flags);
  692. if (ret)
  693. break;
  694. v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
  695. source->name, flags ? '=' : '-', sink->name);
  696. }
  697. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  698. if (!fmd->fimc_lite[i])
  699. continue;
  700. sink = &fmd->fimc_lite[i]->subdev.entity;
  701. ret = media_create_pad_link(source, pad, sink,
  702. FLITE_SD_PAD_SINK, 0);
  703. if (ret)
  704. return ret;
  705. /* Notify FIMC-LITE subdev entity */
  706. ret = media_entity_call(sink, link_setup, &sink->pads[0],
  707. &source->pads[pad], 0);
  708. if (ret)
  709. break;
  710. v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
  711. source->name, sink->name);
  712. }
  713. return 0;
  714. }
  715. /* Create links from FIMC-LITE source pads to other entities */
  716. static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
  717. {
  718. struct media_entity *source, *sink;
  719. int i, ret = 0;
  720. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  721. struct fimc_lite *fimc = fmd->fimc_lite[i];
  722. if (fimc == NULL)
  723. continue;
  724. source = &fimc->subdev.entity;
  725. sink = &fimc->ve.vdev.entity;
  726. /* FIMC-LITE's subdev and video node */
  727. ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
  728. sink, 0, 0);
  729. if (ret)
  730. break;
  731. /* Link from FIMC-LITE to IS-ISP subdev */
  732. sink = &fmd->fimc_is->isp.subdev.entity;
  733. ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
  734. sink, 0, 0);
  735. if (ret)
  736. break;
  737. }
  738. return ret;
  739. }
  740. /* Create FIMC-IS links */
  741. static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
  742. {
  743. struct fimc_isp *isp = &fmd->fimc_is->isp;
  744. struct media_entity *source, *sink;
  745. int i, ret;
  746. source = &isp->subdev.entity;
  747. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  748. if (fmd->fimc[i] == NULL)
  749. continue;
  750. /* Link from FIMC-IS-ISP subdev to FIMC */
  751. sink = &fmd->fimc[i]->vid_cap.subdev.entity;
  752. ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
  753. sink, FIMC_SD_PAD_SINK_FIFO, 0);
  754. if (ret)
  755. return ret;
  756. }
  757. /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
  758. sink = &isp->video_capture.ve.vdev.entity;
  759. /* Skip this link if the fimc-is-isp video node driver isn't built-in */
  760. if (sink->num_pads == 0)
  761. return 0;
  762. return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
  763. sink, 0, 0);
  764. }
  765. /**
  766. * fimc_md_create_links - create default links between registered entities
  767. * @fmd: fimc media device
  768. *
  769. * Parallel interface sensor entities are connected directly to FIMC capture
  770. * entities. The sensors using MIPI CSIS bus are connected through immutable
  771. * link with CSI receiver entity specified by mux_id. Any registered CSIS
  772. * entity has a link to each registered FIMC capture entity. Enabled links
  773. * are created by default between each subsequent registered sensor and
  774. * subsequent FIMC capture entity. The number of default active links is
  775. * determined by the number of available sensors or FIMC entities,
  776. * whichever is less.
  777. */
  778. static int fimc_md_create_links(struct fimc_md *fmd)
  779. {
  780. struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
  781. struct v4l2_subdev *sensor, *csis;
  782. struct fimc_source_info *pdata;
  783. struct media_entity *source, *sink;
  784. int i, pad, fimc_id = 0, ret = 0;
  785. u32 flags, link_mask = 0;
  786. for (i = 0; i < fmd->num_sensors; i++) {
  787. if (fmd->sensor[i].subdev == NULL)
  788. continue;
  789. sensor = fmd->sensor[i].subdev;
  790. pdata = v4l2_get_subdev_hostdata(sensor);
  791. if (!pdata)
  792. continue;
  793. source = NULL;
  794. switch (pdata->sensor_bus_type) {
  795. case FIMC_BUS_TYPE_MIPI_CSI2:
  796. if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
  797. "Wrong CSI channel id: %d\n", pdata->mux_id))
  798. return -EINVAL;
  799. csis = fmd->csis[pdata->mux_id].sd;
  800. if (WARN(csis == NULL,
  801. "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
  802. return -EINVAL;
  803. pad = sensor->entity.num_pads - 1;
  804. ret = media_create_pad_link(&sensor->entity, pad,
  805. &csis->entity, CSIS_PAD_SINK,
  806. MEDIA_LNK_FL_IMMUTABLE |
  807. MEDIA_LNK_FL_ENABLED);
  808. if (ret)
  809. return ret;
  810. v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
  811. sensor->entity.name, csis->entity.name);
  812. source = NULL;
  813. csi_sensors[pdata->mux_id] = sensor;
  814. break;
  815. case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
  816. source = &sensor->entity;
  817. pad = 0;
  818. break;
  819. default:
  820. v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
  821. pdata->sensor_bus_type);
  822. return -EINVAL;
  823. }
  824. if (source == NULL)
  825. continue;
  826. link_mask = 1 << fimc_id++;
  827. ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
  828. pad, link_mask);
  829. }
  830. for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
  831. if (fmd->csis[i].sd == NULL)
  832. continue;
  833. source = &fmd->csis[i].sd->entity;
  834. pad = CSIS_PAD_SOURCE;
  835. sensor = csi_sensors[i];
  836. link_mask = 1 << fimc_id++;
  837. ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
  838. pad, link_mask);
  839. }
  840. /* Create immutable links between each FIMC's subdev and video node */
  841. flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
  842. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  843. if (!fmd->fimc[i])
  844. continue;
  845. source = &fmd->fimc[i]->vid_cap.subdev.entity;
  846. sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
  847. ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
  848. sink, 0, flags);
  849. if (ret)
  850. break;
  851. }
  852. ret = __fimc_md_create_flite_source_links(fmd);
  853. if (ret < 0)
  854. return ret;
  855. if (fmd->use_isp)
  856. ret = __fimc_md_create_fimc_is_links(fmd);
  857. return ret;
  858. }
  859. /*
  860. * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
  861. */
  862. static void fimc_md_put_clocks(struct fimc_md *fmd)
  863. {
  864. int i = FIMC_MAX_CAMCLKS;
  865. while (--i >= 0) {
  866. if (IS_ERR(fmd->camclk[i].clock))
  867. continue;
  868. clk_put(fmd->camclk[i].clock);
  869. fmd->camclk[i].clock = ERR_PTR(-EINVAL);
  870. }
  871. /* Writeback (PIXELASYNCMx) clocks */
  872. for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
  873. if (IS_ERR(fmd->wbclk[i]))
  874. continue;
  875. clk_put(fmd->wbclk[i]);
  876. fmd->wbclk[i] = ERR_PTR(-EINVAL);
  877. }
  878. }
  879. static int fimc_md_get_clocks(struct fimc_md *fmd)
  880. {
  881. struct device *dev = &fmd->pdev->dev;
  882. char clk_name[32];
  883. struct clk *clock;
  884. int i, ret = 0;
  885. for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
  886. fmd->camclk[i].clock = ERR_PTR(-EINVAL);
  887. for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
  888. snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
  889. clock = clk_get(dev, clk_name);
  890. if (IS_ERR(clock)) {
  891. dev_err(dev, "Failed to get clock: %s\n", clk_name);
  892. ret = PTR_ERR(clock);
  893. break;
  894. }
  895. fmd->camclk[i].clock = clock;
  896. }
  897. if (ret)
  898. fimc_md_put_clocks(fmd);
  899. if (!fmd->use_isp)
  900. return 0;
  901. /*
  902. * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
  903. * leave PIXELASYNCM0 out for the LCD Writeback driver.
  904. */
  905. fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
  906. for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
  907. snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
  908. clock = clk_get(dev, clk_name);
  909. if (IS_ERR(clock)) {
  910. v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
  911. clk_name);
  912. ret = PTR_ERR(clock);
  913. break;
  914. }
  915. fmd->wbclk[i] = clock;
  916. }
  917. if (ret)
  918. fimc_md_put_clocks(fmd);
  919. return ret;
  920. }
  921. static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
  922. {
  923. struct exynos_video_entity *ve;
  924. struct fimc_pipeline *p;
  925. struct video_device *vdev;
  926. int ret;
  927. vdev = media_entity_to_video_device(entity);
  928. if (vdev->entity.use_count == 0)
  929. return 0;
  930. ve = vdev_to_exynos_video_entity(vdev);
  931. p = to_fimc_pipeline(ve->pipe);
  932. /*
  933. * Nothing to do if we are disabling the pipeline, some link
  934. * has been disconnected and p->subdevs array is cleared now.
  935. */
  936. if (!enable && p->subdevs[IDX_SENSOR] == NULL)
  937. return 0;
  938. if (enable)
  939. ret = __fimc_pipeline_open(ve->pipe, entity, true);
  940. else
  941. ret = __fimc_pipeline_close(ve->pipe);
  942. if (ret == 0 && !enable)
  943. memset(p->subdevs, 0, sizeof(p->subdevs));
  944. return ret;
  945. }
  946. /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
  947. static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
  948. struct media_graph *graph)
  949. {
  950. struct media_entity *entity_err = entity;
  951. int ret;
  952. /*
  953. * Walk current graph and call the pipeline open/close routine for each
  954. * opened video node that belongs to the graph of entities connected
  955. * through active links. This is needed as we cannot power on/off the
  956. * subdevs in random order.
  957. */
  958. media_graph_walk_start(graph, entity);
  959. while ((entity = media_graph_walk_next(graph))) {
  960. if (!is_media_entity_v4l2_video_device(entity))
  961. continue;
  962. ret = __fimc_md_modify_pipeline(entity, enable);
  963. if (ret < 0)
  964. goto err;
  965. }
  966. return 0;
  967. err:
  968. media_graph_walk_start(graph, entity_err);
  969. while ((entity_err = media_graph_walk_next(graph))) {
  970. if (!is_media_entity_v4l2_video_device(entity_err))
  971. continue;
  972. __fimc_md_modify_pipeline(entity_err, !enable);
  973. if (entity_err == entity)
  974. break;
  975. }
  976. return ret;
  977. }
  978. static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
  979. unsigned int notification)
  980. {
  981. struct media_graph *graph =
  982. &container_of(link->graph_obj.mdev, struct fimc_md,
  983. media_dev)->link_setup_graph;
  984. struct media_entity *sink = link->sink->entity;
  985. int ret = 0;
  986. /* Before link disconnection */
  987. if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
  988. ret = media_graph_walk_init(graph,
  989. link->graph_obj.mdev);
  990. if (ret)
  991. return ret;
  992. if (!(flags & MEDIA_LNK_FL_ENABLED))
  993. ret = __fimc_md_modify_pipelines(sink, false, graph);
  994. #if 0
  995. else
  996. /* TODO: Link state change validation */
  997. #endif
  998. /* After link activation */
  999. } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
  1000. if (link->flags & MEDIA_LNK_FL_ENABLED)
  1001. ret = __fimc_md_modify_pipelines(sink, true, graph);
  1002. media_graph_walk_cleanup(graph);
  1003. }
  1004. return ret ? -EPIPE : 0;
  1005. }
  1006. static const struct media_device_ops fimc_md_ops = {
  1007. .link_notify = fimc_md_link_notify,
  1008. };
  1009. static ssize_t fimc_md_sysfs_show(struct device *dev,
  1010. struct device_attribute *attr, char *buf)
  1011. {
  1012. struct fimc_md *fmd = dev_get_drvdata(dev);
  1013. if (fmd->user_subdev_api)
  1014. return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
  1015. return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
  1016. }
  1017. static ssize_t fimc_md_sysfs_store(struct device *dev,
  1018. struct device_attribute *attr,
  1019. const char *buf, size_t count)
  1020. {
  1021. struct fimc_md *fmd = dev_get_drvdata(dev);
  1022. bool subdev_api;
  1023. int i;
  1024. if (!strcmp(buf, "vid-dev\n"))
  1025. subdev_api = false;
  1026. else if (!strcmp(buf, "sub-dev\n"))
  1027. subdev_api = true;
  1028. else
  1029. return count;
  1030. fmd->user_subdev_api = subdev_api;
  1031. for (i = 0; i < FIMC_MAX_DEVS; i++)
  1032. if (fmd->fimc[i])
  1033. fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
  1034. return count;
  1035. }
  1036. /*
  1037. * This device attribute is to select video pipeline configuration method.
  1038. * There are following valid values:
  1039. * vid-dev - for V4L2 video node API only, subdevice will be configured
  1040. * by the host driver.
  1041. * sub-dev - for media controller API, subdevs must be configured in user
  1042. * space before starting streaming.
  1043. */
  1044. static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
  1045. fimc_md_sysfs_show, fimc_md_sysfs_store);
  1046. static int fimc_md_get_pinctrl(struct fimc_md *fmd)
  1047. {
  1048. struct device *dev = &fmd->pdev->dev;
  1049. struct fimc_pinctrl *pctl = &fmd->pinctl;
  1050. pctl->pinctrl = devm_pinctrl_get(dev);
  1051. if (IS_ERR(pctl->pinctrl))
  1052. return PTR_ERR(pctl->pinctrl);
  1053. pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
  1054. PINCTRL_STATE_DEFAULT);
  1055. if (IS_ERR(pctl->state_default))
  1056. return PTR_ERR(pctl->state_default);
  1057. pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
  1058. PINCTRL_STATE_IDLE);
  1059. return 0;
  1060. }
  1061. static int cam_clk_prepare(struct clk_hw *hw)
  1062. {
  1063. struct cam_clk *camclk = to_cam_clk(hw);
  1064. int ret;
  1065. if (camclk->fmd->pmf == NULL)
  1066. return -ENODEV;
  1067. ret = pm_runtime_get_sync(camclk->fmd->pmf);
  1068. return ret < 0 ? ret : 0;
  1069. }
  1070. static void cam_clk_unprepare(struct clk_hw *hw)
  1071. {
  1072. struct cam_clk *camclk = to_cam_clk(hw);
  1073. if (camclk->fmd->pmf == NULL)
  1074. return;
  1075. pm_runtime_put_sync(camclk->fmd->pmf);
  1076. }
  1077. static const struct clk_ops cam_clk_ops = {
  1078. .prepare = cam_clk_prepare,
  1079. .unprepare = cam_clk_unprepare,
  1080. };
  1081. static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
  1082. {
  1083. struct cam_clk_provider *cp = &fmd->clk_provider;
  1084. unsigned int i;
  1085. if (cp->of_node)
  1086. of_clk_del_provider(cp->of_node);
  1087. for (i = 0; i < cp->num_clocks; i++)
  1088. clk_unregister(cp->clks[i]);
  1089. }
  1090. static int fimc_md_register_clk_provider(struct fimc_md *fmd)
  1091. {
  1092. struct cam_clk_provider *cp = &fmd->clk_provider;
  1093. struct device *dev = &fmd->pdev->dev;
  1094. int i, ret;
  1095. for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
  1096. struct cam_clk *camclk = &cp->camclk[i];
  1097. struct clk_init_data init;
  1098. const char *p_name;
  1099. ret = of_property_read_string_index(dev->of_node,
  1100. "clock-output-names", i, &init.name);
  1101. if (ret < 0)
  1102. break;
  1103. p_name = __clk_get_name(fmd->camclk[i].clock);
  1104. /* It's safe since clk_register() will duplicate the string. */
  1105. init.parent_names = &p_name;
  1106. init.num_parents = 1;
  1107. init.ops = &cam_clk_ops;
  1108. init.flags = CLK_SET_RATE_PARENT;
  1109. camclk->hw.init = &init;
  1110. camclk->fmd = fmd;
  1111. cp->clks[i] = clk_register(NULL, &camclk->hw);
  1112. if (IS_ERR(cp->clks[i])) {
  1113. dev_err(dev, "failed to register clock: %s (%ld)\n",
  1114. init.name, PTR_ERR(cp->clks[i]));
  1115. ret = PTR_ERR(cp->clks[i]);
  1116. goto err;
  1117. }
  1118. cp->num_clocks++;
  1119. }
  1120. if (cp->num_clocks == 0) {
  1121. dev_warn(dev, "clk provider not registered\n");
  1122. return 0;
  1123. }
  1124. cp->clk_data.clks = cp->clks;
  1125. cp->clk_data.clk_num = cp->num_clocks;
  1126. cp->of_node = dev->of_node;
  1127. ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
  1128. &cp->clk_data);
  1129. if (ret == 0)
  1130. return 0;
  1131. err:
  1132. fimc_md_unregister_clk_provider(fmd);
  1133. return ret;
  1134. }
  1135. static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
  1136. struct v4l2_subdev *subdev,
  1137. struct v4l2_async_subdev *asd)
  1138. {
  1139. struct fimc_md *fmd = notifier_to_fimc_md(notifier);
  1140. struct fimc_sensor_info *si = NULL;
  1141. int i;
  1142. /* Find platform data for this sensor subdev */
  1143. for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
  1144. if (fmd->sensor[i].asd.match.fwnode ==
  1145. of_fwnode_handle(subdev->dev->of_node))
  1146. si = &fmd->sensor[i];
  1147. if (si == NULL)
  1148. return -EINVAL;
  1149. v4l2_set_subdev_hostdata(subdev, &si->pdata);
  1150. if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
  1151. subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
  1152. else
  1153. subdev->grp_id = GRP_ID_SENSOR;
  1154. si->subdev = subdev;
  1155. v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
  1156. subdev->name, fmd->num_sensors);
  1157. fmd->num_sensors++;
  1158. return 0;
  1159. }
  1160. static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
  1161. {
  1162. struct fimc_md *fmd = notifier_to_fimc_md(notifier);
  1163. int ret;
  1164. mutex_lock(&fmd->media_dev.graph_mutex);
  1165. ret = fimc_md_create_links(fmd);
  1166. if (ret < 0)
  1167. goto unlock;
  1168. ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
  1169. unlock:
  1170. mutex_unlock(&fmd->media_dev.graph_mutex);
  1171. if (ret < 0)
  1172. return ret;
  1173. return media_device_register(&fmd->media_dev);
  1174. }
  1175. static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
  1176. .bound = subdev_notifier_bound,
  1177. .complete = subdev_notifier_complete,
  1178. };
  1179. static int fimc_md_probe(struct platform_device *pdev)
  1180. {
  1181. struct device *dev = &pdev->dev;
  1182. struct v4l2_device *v4l2_dev;
  1183. struct fimc_md *fmd;
  1184. int ret;
  1185. fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
  1186. if (!fmd)
  1187. return -ENOMEM;
  1188. spin_lock_init(&fmd->slock);
  1189. INIT_LIST_HEAD(&fmd->pipelines);
  1190. fmd->pdev = pdev;
  1191. strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
  1192. sizeof(fmd->media_dev.model));
  1193. fmd->media_dev.ops = &fimc_md_ops;
  1194. fmd->media_dev.dev = dev;
  1195. v4l2_dev = &fmd->v4l2_dev;
  1196. v4l2_dev->mdev = &fmd->media_dev;
  1197. v4l2_dev->notify = fimc_sensor_notify;
  1198. strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
  1199. fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
  1200. fmd->user_subdev_api = true;
  1201. media_device_init(&fmd->media_dev);
  1202. ret = v4l2_device_register(dev, &fmd->v4l2_dev);
  1203. if (ret < 0) {
  1204. v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
  1205. return ret;
  1206. }
  1207. ret = fimc_md_get_clocks(fmd);
  1208. if (ret)
  1209. goto err_md;
  1210. ret = fimc_md_get_pinctrl(fmd);
  1211. if (ret < 0) {
  1212. if (ret != EPROBE_DEFER)
  1213. dev_err(dev, "Failed to get pinctrl: %d\n", ret);
  1214. goto err_clk;
  1215. }
  1216. platform_set_drvdata(pdev, fmd);
  1217. ret = fimc_md_register_platform_entities(fmd, dev->of_node);
  1218. if (ret)
  1219. goto err_clk;
  1220. ret = fimc_md_register_sensor_entities(fmd);
  1221. if (ret)
  1222. goto err_m_ent;
  1223. ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1224. if (ret)
  1225. goto err_m_ent;
  1226. /*
  1227. * FIMC platform devices need to be registered before the sclk_cam
  1228. * clocks provider, as one of these devices needs to be activated
  1229. * to enable the clock.
  1230. */
  1231. ret = fimc_md_register_clk_provider(fmd);
  1232. if (ret < 0) {
  1233. v4l2_err(v4l2_dev, "clock provider registration failed\n");
  1234. goto err_attr;
  1235. }
  1236. if (fmd->num_sensors > 0) {
  1237. fmd->subdev_notifier.subdevs = fmd->async_subdevs;
  1238. fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
  1239. fmd->subdev_notifier.ops = &subdev_notifier_ops;
  1240. fmd->num_sensors = 0;
  1241. ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
  1242. &fmd->subdev_notifier);
  1243. if (ret)
  1244. goto err_clk_p;
  1245. }
  1246. return 0;
  1247. err_clk_p:
  1248. fimc_md_unregister_clk_provider(fmd);
  1249. err_attr:
  1250. device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1251. err_clk:
  1252. fimc_md_put_clocks(fmd);
  1253. err_m_ent:
  1254. fimc_md_unregister_entities(fmd);
  1255. err_md:
  1256. media_device_cleanup(&fmd->media_dev);
  1257. v4l2_device_unregister(&fmd->v4l2_dev);
  1258. return ret;
  1259. }
  1260. static int fimc_md_remove(struct platform_device *pdev)
  1261. {
  1262. struct fimc_md *fmd = platform_get_drvdata(pdev);
  1263. if (!fmd)
  1264. return 0;
  1265. fimc_md_unregister_clk_provider(fmd);
  1266. v4l2_async_notifier_unregister(&fmd->subdev_notifier);
  1267. v4l2_device_unregister(&fmd->v4l2_dev);
  1268. device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1269. fimc_md_unregister_entities(fmd);
  1270. fimc_md_pipelines_free(fmd);
  1271. media_device_unregister(&fmd->media_dev);
  1272. media_device_cleanup(&fmd->media_dev);
  1273. fimc_md_put_clocks(fmd);
  1274. return 0;
  1275. }
  1276. static const struct platform_device_id fimc_driver_ids[] __always_unused = {
  1277. { .name = "s5p-fimc-md" },
  1278. { },
  1279. };
  1280. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1281. static const struct of_device_id fimc_md_of_match[] = {
  1282. { .compatible = "samsung,fimc" },
  1283. { },
  1284. };
  1285. MODULE_DEVICE_TABLE(of, fimc_md_of_match);
  1286. static struct platform_driver fimc_md_driver = {
  1287. .probe = fimc_md_probe,
  1288. .remove = fimc_md_remove,
  1289. .driver = {
  1290. .of_match_table = of_match_ptr(fimc_md_of_match),
  1291. .name = "s5p-fimc-md",
  1292. }
  1293. };
  1294. static int __init fimc_md_init(void)
  1295. {
  1296. int ret;
  1297. request_module("s5p-csis");
  1298. ret = fimc_register_driver();
  1299. if (ret)
  1300. return ret;
  1301. return platform_driver_register(&fimc_md_driver);
  1302. }
  1303. static void __exit fimc_md_exit(void)
  1304. {
  1305. platform_driver_unregister(&fimc_md_driver);
  1306. fimc_unregister_driver();
  1307. }
  1308. module_init(fimc_md_init);
  1309. module_exit(fimc_md_exit);
  1310. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  1311. MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
  1312. MODULE_LICENSE("GPL");
  1313. MODULE_VERSION("2.0.1");