media-dev.c 39 KB

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