media-dev.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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 = { .bus_type = 0 };
  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. ret = v4l2_async_notifier_add_subdev(&fmd->subdev_notifier,
  395. &fmd->sensor[index].asd);
  396. if (ret) {
  397. of_node_put(rem);
  398. return ret;
  399. }
  400. fmd->num_sensors++;
  401. return 0;
  402. }
  403. /* Register all SoC external sub-devices */
  404. static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
  405. {
  406. struct device_node *parent = fmd->pdev->dev.of_node;
  407. struct device_node *node, *ports;
  408. int index = 0;
  409. int ret;
  410. /*
  411. * Runtime resume one of the FIMC entities to make sure
  412. * the sclk_cam clocks are not globally disabled.
  413. */
  414. if (!fmd->pmf)
  415. return -ENXIO;
  416. ret = pm_runtime_get_sync(fmd->pmf);
  417. if (ret < 0)
  418. return ret;
  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_cmp(node->name, "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. if (ret < 0) {
  431. of_node_put(node);
  432. goto cleanup;
  433. }
  434. index++;
  435. }
  436. /* Attach sensors listed in the parallel-ports node */
  437. ports = of_get_child_by_name(parent, "parallel-ports");
  438. if (!ports)
  439. goto rpm_put;
  440. for_each_child_of_node(ports, node) {
  441. ret = fimc_md_parse_port_node(fmd, node, index);
  442. if (ret < 0) {
  443. of_node_put(node);
  444. goto cleanup;
  445. }
  446. index++;
  447. }
  448. rpm_put:
  449. pm_runtime_put(fmd->pmf);
  450. return 0;
  451. cleanup:
  452. v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
  453. pm_runtime_put(fmd->pmf);
  454. return ret;
  455. }
  456. static int __of_get_csis_id(struct device_node *np)
  457. {
  458. u32 reg = 0;
  459. np = of_get_child_by_name(np, "port");
  460. if (!np)
  461. return -EINVAL;
  462. of_property_read_u32(np, "reg", &reg);
  463. return reg - FIMC_INPUT_MIPI_CSI2_0;
  464. }
  465. /*
  466. * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
  467. */
  468. static int register_fimc_lite_entity(struct fimc_md *fmd,
  469. struct fimc_lite *fimc_lite)
  470. {
  471. struct v4l2_subdev *sd;
  472. struct exynos_media_pipeline *ep;
  473. int ret;
  474. if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
  475. fmd->fimc_lite[fimc_lite->index]))
  476. return -EBUSY;
  477. sd = &fimc_lite->subdev;
  478. sd->grp_id = GRP_ID_FLITE;
  479. ep = fimc_md_pipeline_create(fmd);
  480. if (!ep)
  481. return -ENOMEM;
  482. v4l2_set_subdev_hostdata(sd, ep);
  483. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  484. if (!ret)
  485. fmd->fimc_lite[fimc_lite->index] = fimc_lite;
  486. else
  487. v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
  488. fimc_lite->index);
  489. return ret;
  490. }
  491. static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
  492. {
  493. struct v4l2_subdev *sd;
  494. struct exynos_media_pipeline *ep;
  495. int ret;
  496. if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
  497. return -EBUSY;
  498. sd = &fimc->vid_cap.subdev;
  499. sd->grp_id = GRP_ID_FIMC;
  500. ep = fimc_md_pipeline_create(fmd);
  501. if (!ep)
  502. return -ENOMEM;
  503. v4l2_set_subdev_hostdata(sd, ep);
  504. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  505. if (!ret) {
  506. if (!fmd->pmf && fimc->pdev)
  507. fmd->pmf = &fimc->pdev->dev;
  508. fmd->fimc[fimc->id] = fimc;
  509. fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
  510. } else {
  511. v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
  512. fimc->id, ret);
  513. }
  514. return ret;
  515. }
  516. static int register_csis_entity(struct fimc_md *fmd,
  517. struct platform_device *pdev,
  518. struct v4l2_subdev *sd)
  519. {
  520. struct device_node *node = pdev->dev.of_node;
  521. int id, ret;
  522. id = node ? __of_get_csis_id(node) : max(0, pdev->id);
  523. if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
  524. return -ENOENT;
  525. if (WARN_ON(fmd->csis[id].sd))
  526. return -EBUSY;
  527. sd->grp_id = GRP_ID_CSIS;
  528. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  529. if (!ret)
  530. fmd->csis[id].sd = sd;
  531. else
  532. v4l2_err(&fmd->v4l2_dev,
  533. "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
  534. return ret;
  535. }
  536. static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
  537. {
  538. struct v4l2_subdev *sd = &is->isp.subdev;
  539. struct exynos_media_pipeline *ep;
  540. int ret;
  541. /* Allocate pipeline object for the ISP capture video node. */
  542. ep = fimc_md_pipeline_create(fmd);
  543. if (!ep)
  544. return -ENOMEM;
  545. v4l2_set_subdev_hostdata(sd, ep);
  546. ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
  547. if (ret) {
  548. v4l2_err(&fmd->v4l2_dev,
  549. "Failed to register FIMC-ISP (%d)\n", ret);
  550. return ret;
  551. }
  552. fmd->fimc_is = is;
  553. return 0;
  554. }
  555. static int fimc_md_register_platform_entity(struct fimc_md *fmd,
  556. struct platform_device *pdev,
  557. int plat_entity)
  558. {
  559. struct device *dev = &pdev->dev;
  560. int ret = -EPROBE_DEFER;
  561. void *drvdata;
  562. /* Lock to ensure dev->driver won't change. */
  563. device_lock(dev);
  564. if (!dev->driver || !try_module_get(dev->driver->owner))
  565. goto dev_unlock;
  566. drvdata = dev_get_drvdata(dev);
  567. /* Some subdev didn't probe successfully id drvdata is NULL */
  568. if (drvdata) {
  569. switch (plat_entity) {
  570. case IDX_FIMC:
  571. ret = register_fimc_entity(fmd, drvdata);
  572. break;
  573. case IDX_FLITE:
  574. ret = register_fimc_lite_entity(fmd, drvdata);
  575. break;
  576. case IDX_CSIS:
  577. ret = register_csis_entity(fmd, pdev, drvdata);
  578. break;
  579. case IDX_IS_ISP:
  580. ret = register_fimc_is_entity(fmd, drvdata);
  581. break;
  582. default:
  583. ret = -ENODEV;
  584. }
  585. }
  586. module_put(dev->driver->owner);
  587. dev_unlock:
  588. device_unlock(dev);
  589. if (ret == -EPROBE_DEFER)
  590. dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
  591. dev_name(dev));
  592. else if (ret < 0)
  593. dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
  594. dev_name(dev), ret);
  595. return ret;
  596. }
  597. /* Register FIMC, FIMC-LITE and CSIS media entities */
  598. static int fimc_md_register_platform_entities(struct fimc_md *fmd,
  599. struct device_node *parent)
  600. {
  601. struct device_node *node;
  602. int ret = 0;
  603. for_each_available_child_of_node(parent, node) {
  604. struct platform_device *pdev;
  605. int plat_entity = -1;
  606. pdev = of_find_device_by_node(node);
  607. if (!pdev)
  608. continue;
  609. /* If driver of any entity isn't ready try all again later. */
  610. if (!strcmp(node->name, CSIS_OF_NODE_NAME))
  611. plat_entity = IDX_CSIS;
  612. else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
  613. plat_entity = IDX_IS_ISP;
  614. else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
  615. plat_entity = IDX_FLITE;
  616. else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
  617. !of_property_read_bool(node, "samsung,lcd-wb"))
  618. plat_entity = IDX_FIMC;
  619. if (plat_entity >= 0)
  620. ret = fimc_md_register_platform_entity(fmd, pdev,
  621. plat_entity);
  622. put_device(&pdev->dev);
  623. if (ret < 0) {
  624. of_node_put(node);
  625. break;
  626. }
  627. }
  628. return ret;
  629. }
  630. static void fimc_md_unregister_entities(struct fimc_md *fmd)
  631. {
  632. int i;
  633. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  634. struct fimc_dev *dev = fmd->fimc[i];
  635. if (dev == NULL)
  636. continue;
  637. v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
  638. dev->vid_cap.ve.pipe = NULL;
  639. fmd->fimc[i] = NULL;
  640. }
  641. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  642. struct fimc_lite *dev = fmd->fimc_lite[i];
  643. if (dev == NULL)
  644. continue;
  645. v4l2_device_unregister_subdev(&dev->subdev);
  646. dev->ve.pipe = NULL;
  647. fmd->fimc_lite[i] = NULL;
  648. }
  649. for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
  650. if (fmd->csis[i].sd == NULL)
  651. continue;
  652. v4l2_device_unregister_subdev(fmd->csis[i].sd);
  653. fmd->csis[i].sd = NULL;
  654. }
  655. if (fmd->fimc_is)
  656. v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
  657. v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
  658. }
  659. /**
  660. * __fimc_md_create_fimc_links - create links to all FIMC entities
  661. * @fmd: fimc media device
  662. * @source: the source entity to create links to all fimc entities from
  663. * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
  664. * @pad: the source entity pad index
  665. * @link_mask: bitmask of the fimc devices for which link should be enabled
  666. */
  667. static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
  668. struct media_entity *source,
  669. struct v4l2_subdev *sensor,
  670. int pad, int link_mask)
  671. {
  672. struct fimc_source_info *si = NULL;
  673. struct media_entity *sink;
  674. unsigned int flags = 0;
  675. int i, ret = 0;
  676. if (sensor) {
  677. si = v4l2_get_subdev_hostdata(sensor);
  678. /* Skip direct FIMC links in the logical FIMC-IS sensor path */
  679. if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
  680. ret = 1;
  681. }
  682. for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
  683. if (!fmd->fimc[i])
  684. continue;
  685. /*
  686. * Some FIMC variants are not fitted with camera capture
  687. * interface. Skip creating a link from sensor for those.
  688. */
  689. if (!fmd->fimc[i]->variant->has_cam_if)
  690. continue;
  691. flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
  692. sink = &fmd->fimc[i]->vid_cap.subdev.entity;
  693. ret = media_create_pad_link(source, pad, sink,
  694. FIMC_SD_PAD_SINK_CAM, flags);
  695. if (ret)
  696. return ret;
  697. /* Notify FIMC capture subdev entity */
  698. ret = media_entity_call(sink, link_setup, &sink->pads[0],
  699. &source->pads[pad], flags);
  700. if (ret)
  701. break;
  702. v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
  703. source->name, flags ? '=' : '-', sink->name);
  704. }
  705. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  706. if (!fmd->fimc_lite[i])
  707. continue;
  708. sink = &fmd->fimc_lite[i]->subdev.entity;
  709. ret = media_create_pad_link(source, pad, sink,
  710. FLITE_SD_PAD_SINK, 0);
  711. if (ret)
  712. return ret;
  713. /* Notify FIMC-LITE subdev entity */
  714. ret = media_entity_call(sink, link_setup, &sink->pads[0],
  715. &source->pads[pad], 0);
  716. if (ret)
  717. break;
  718. v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
  719. source->name, sink->name);
  720. }
  721. return 0;
  722. }
  723. /* Create links from FIMC-LITE source pads to other entities */
  724. static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
  725. {
  726. struct media_entity *source, *sink;
  727. int i, ret = 0;
  728. for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
  729. struct fimc_lite *fimc = fmd->fimc_lite[i];
  730. if (fimc == NULL)
  731. continue;
  732. source = &fimc->subdev.entity;
  733. sink = &fimc->ve.vdev.entity;
  734. /* FIMC-LITE's subdev and video node */
  735. ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
  736. sink, 0, 0);
  737. if (ret)
  738. break;
  739. /* Link from FIMC-LITE to IS-ISP subdev */
  740. sink = &fmd->fimc_is->isp.subdev.entity;
  741. ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
  742. sink, 0, 0);
  743. if (ret)
  744. break;
  745. }
  746. return ret;
  747. }
  748. /* Create FIMC-IS links */
  749. static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
  750. {
  751. struct fimc_isp *isp = &fmd->fimc_is->isp;
  752. struct media_entity *source, *sink;
  753. int i, ret;
  754. source = &isp->subdev.entity;
  755. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  756. if (fmd->fimc[i] == NULL)
  757. continue;
  758. /* Link from FIMC-IS-ISP subdev to FIMC */
  759. sink = &fmd->fimc[i]->vid_cap.subdev.entity;
  760. ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
  761. sink, FIMC_SD_PAD_SINK_FIFO, 0);
  762. if (ret)
  763. return ret;
  764. }
  765. /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
  766. sink = &isp->video_capture.ve.vdev.entity;
  767. /* Skip this link if the fimc-is-isp video node driver isn't built-in */
  768. if (sink->num_pads == 0)
  769. return 0;
  770. return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
  771. sink, 0, 0);
  772. }
  773. /**
  774. * fimc_md_create_links - create default links between registered entities
  775. * @fmd: fimc media device
  776. *
  777. * Parallel interface sensor entities are connected directly to FIMC capture
  778. * entities. The sensors using MIPI CSIS bus are connected through immutable
  779. * link with CSI receiver entity specified by mux_id. Any registered CSIS
  780. * entity has a link to each registered FIMC capture entity. Enabled links
  781. * are created by default between each subsequent registered sensor and
  782. * subsequent FIMC capture entity. The number of default active links is
  783. * determined by the number of available sensors or FIMC entities,
  784. * whichever is less.
  785. */
  786. static int fimc_md_create_links(struct fimc_md *fmd)
  787. {
  788. struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
  789. struct v4l2_subdev *sensor, *csis;
  790. struct fimc_source_info *pdata;
  791. struct media_entity *source, *sink;
  792. int i, pad, fimc_id = 0, ret = 0;
  793. u32 flags, link_mask = 0;
  794. for (i = 0; i < fmd->num_sensors; i++) {
  795. if (fmd->sensor[i].subdev == NULL)
  796. continue;
  797. sensor = fmd->sensor[i].subdev;
  798. pdata = v4l2_get_subdev_hostdata(sensor);
  799. if (!pdata)
  800. continue;
  801. source = NULL;
  802. switch (pdata->sensor_bus_type) {
  803. case FIMC_BUS_TYPE_MIPI_CSI2:
  804. if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
  805. "Wrong CSI channel id: %d\n", pdata->mux_id))
  806. return -EINVAL;
  807. csis = fmd->csis[pdata->mux_id].sd;
  808. if (WARN(csis == NULL,
  809. "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
  810. return -EINVAL;
  811. pad = sensor->entity.num_pads - 1;
  812. ret = media_create_pad_link(&sensor->entity, pad,
  813. &csis->entity, CSIS_PAD_SINK,
  814. MEDIA_LNK_FL_IMMUTABLE |
  815. MEDIA_LNK_FL_ENABLED);
  816. if (ret)
  817. return ret;
  818. v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
  819. sensor->entity.name, csis->entity.name);
  820. source = NULL;
  821. csi_sensors[pdata->mux_id] = sensor;
  822. break;
  823. case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
  824. source = &sensor->entity;
  825. pad = 0;
  826. break;
  827. default:
  828. v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
  829. pdata->sensor_bus_type);
  830. return -EINVAL;
  831. }
  832. if (source == NULL)
  833. continue;
  834. link_mask = 1 << fimc_id++;
  835. ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
  836. pad, link_mask);
  837. }
  838. for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
  839. if (fmd->csis[i].sd == NULL)
  840. continue;
  841. source = &fmd->csis[i].sd->entity;
  842. pad = CSIS_PAD_SOURCE;
  843. sensor = csi_sensors[i];
  844. link_mask = 1 << fimc_id++;
  845. ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
  846. pad, link_mask);
  847. }
  848. /* Create immutable links between each FIMC's subdev and video node */
  849. flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
  850. for (i = 0; i < FIMC_MAX_DEVS; i++) {
  851. if (!fmd->fimc[i])
  852. continue;
  853. source = &fmd->fimc[i]->vid_cap.subdev.entity;
  854. sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
  855. ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
  856. sink, 0, flags);
  857. if (ret)
  858. break;
  859. }
  860. ret = __fimc_md_create_flite_source_links(fmd);
  861. if (ret < 0)
  862. return ret;
  863. if (fmd->use_isp)
  864. ret = __fimc_md_create_fimc_is_links(fmd);
  865. return ret;
  866. }
  867. /*
  868. * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
  869. */
  870. static void fimc_md_put_clocks(struct fimc_md *fmd)
  871. {
  872. int i = FIMC_MAX_CAMCLKS;
  873. while (--i >= 0) {
  874. if (IS_ERR(fmd->camclk[i].clock))
  875. continue;
  876. clk_put(fmd->camclk[i].clock);
  877. fmd->camclk[i].clock = ERR_PTR(-EINVAL);
  878. }
  879. /* Writeback (PIXELASYNCMx) clocks */
  880. for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
  881. if (IS_ERR(fmd->wbclk[i]))
  882. continue;
  883. clk_put(fmd->wbclk[i]);
  884. fmd->wbclk[i] = ERR_PTR(-EINVAL);
  885. }
  886. }
  887. static int fimc_md_get_clocks(struct fimc_md *fmd)
  888. {
  889. struct device *dev = &fmd->pdev->dev;
  890. char clk_name[32];
  891. struct clk *clock;
  892. int i, ret = 0;
  893. for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
  894. fmd->camclk[i].clock = ERR_PTR(-EINVAL);
  895. for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
  896. snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
  897. clock = clk_get(dev, clk_name);
  898. if (IS_ERR(clock)) {
  899. dev_err(dev, "Failed to get clock: %s\n", clk_name);
  900. ret = PTR_ERR(clock);
  901. break;
  902. }
  903. fmd->camclk[i].clock = clock;
  904. }
  905. if (ret)
  906. fimc_md_put_clocks(fmd);
  907. if (!fmd->use_isp)
  908. return 0;
  909. /*
  910. * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
  911. * leave PIXELASYNCM0 out for the LCD Writeback driver.
  912. */
  913. fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
  914. for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
  915. snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
  916. clock = clk_get(dev, clk_name);
  917. if (IS_ERR(clock)) {
  918. v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
  919. clk_name);
  920. ret = PTR_ERR(clock);
  921. break;
  922. }
  923. fmd->wbclk[i] = clock;
  924. }
  925. if (ret)
  926. fimc_md_put_clocks(fmd);
  927. return ret;
  928. }
  929. static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
  930. {
  931. struct exynos_video_entity *ve;
  932. struct fimc_pipeline *p;
  933. struct video_device *vdev;
  934. int ret;
  935. vdev = media_entity_to_video_device(entity);
  936. if (vdev->entity.use_count == 0)
  937. return 0;
  938. ve = vdev_to_exynos_video_entity(vdev);
  939. p = to_fimc_pipeline(ve->pipe);
  940. /*
  941. * Nothing to do if we are disabling the pipeline, some link
  942. * has been disconnected and p->subdevs array is cleared now.
  943. */
  944. if (!enable && p->subdevs[IDX_SENSOR] == NULL)
  945. return 0;
  946. if (enable)
  947. ret = __fimc_pipeline_open(ve->pipe, entity, true);
  948. else
  949. ret = __fimc_pipeline_close(ve->pipe);
  950. if (ret == 0 && !enable)
  951. memset(p->subdevs, 0, sizeof(p->subdevs));
  952. return ret;
  953. }
  954. /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
  955. static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
  956. struct media_graph *graph)
  957. {
  958. struct media_entity *entity_err = entity;
  959. int ret;
  960. /*
  961. * Walk current graph and call the pipeline open/close routine for each
  962. * opened video node that belongs to the graph of entities connected
  963. * through active links. This is needed as we cannot power on/off the
  964. * subdevs in random order.
  965. */
  966. media_graph_walk_start(graph, entity);
  967. while ((entity = media_graph_walk_next(graph))) {
  968. if (!is_media_entity_v4l2_video_device(entity))
  969. continue;
  970. ret = __fimc_md_modify_pipeline(entity, enable);
  971. if (ret < 0)
  972. goto err;
  973. }
  974. return 0;
  975. err:
  976. media_graph_walk_start(graph, entity_err);
  977. while ((entity_err = media_graph_walk_next(graph))) {
  978. if (!is_media_entity_v4l2_video_device(entity_err))
  979. continue;
  980. __fimc_md_modify_pipeline(entity_err, !enable);
  981. if (entity_err == entity)
  982. break;
  983. }
  984. return ret;
  985. }
  986. static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
  987. unsigned int notification)
  988. {
  989. struct media_graph *graph =
  990. &container_of(link->graph_obj.mdev, struct fimc_md,
  991. media_dev)->link_setup_graph;
  992. struct media_entity *sink = link->sink->entity;
  993. int ret = 0;
  994. /* Before link disconnection */
  995. if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
  996. ret = media_graph_walk_init(graph,
  997. link->graph_obj.mdev);
  998. if (ret)
  999. return ret;
  1000. if (!(flags & MEDIA_LNK_FL_ENABLED))
  1001. ret = __fimc_md_modify_pipelines(sink, false, graph);
  1002. #if 0
  1003. else
  1004. /* TODO: Link state change validation */
  1005. #endif
  1006. /* After link activation */
  1007. } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
  1008. if (link->flags & MEDIA_LNK_FL_ENABLED)
  1009. ret = __fimc_md_modify_pipelines(sink, true, graph);
  1010. media_graph_walk_cleanup(graph);
  1011. }
  1012. return ret ? -EPIPE : 0;
  1013. }
  1014. static const struct media_device_ops fimc_md_ops = {
  1015. .link_notify = fimc_md_link_notify,
  1016. };
  1017. static ssize_t fimc_md_sysfs_show(struct device *dev,
  1018. struct device_attribute *attr, char *buf)
  1019. {
  1020. struct fimc_md *fmd = dev_get_drvdata(dev);
  1021. if (fmd->user_subdev_api)
  1022. return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
  1023. return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
  1024. }
  1025. static ssize_t fimc_md_sysfs_store(struct device *dev,
  1026. struct device_attribute *attr,
  1027. const char *buf, size_t count)
  1028. {
  1029. struct fimc_md *fmd = dev_get_drvdata(dev);
  1030. bool subdev_api;
  1031. int i;
  1032. if (!strcmp(buf, "vid-dev\n"))
  1033. subdev_api = false;
  1034. else if (!strcmp(buf, "sub-dev\n"))
  1035. subdev_api = true;
  1036. else
  1037. return count;
  1038. fmd->user_subdev_api = subdev_api;
  1039. for (i = 0; i < FIMC_MAX_DEVS; i++)
  1040. if (fmd->fimc[i])
  1041. fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
  1042. return count;
  1043. }
  1044. /*
  1045. * This device attribute is to select video pipeline configuration method.
  1046. * There are following valid values:
  1047. * vid-dev - for V4L2 video node API only, subdevice will be configured
  1048. * by the host driver.
  1049. * sub-dev - for media controller API, subdevs must be configured in user
  1050. * space before starting streaming.
  1051. */
  1052. static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
  1053. fimc_md_sysfs_show, fimc_md_sysfs_store);
  1054. static int fimc_md_get_pinctrl(struct fimc_md *fmd)
  1055. {
  1056. struct device *dev = &fmd->pdev->dev;
  1057. struct fimc_pinctrl *pctl = &fmd->pinctl;
  1058. pctl->pinctrl = devm_pinctrl_get(dev);
  1059. if (IS_ERR(pctl->pinctrl))
  1060. return PTR_ERR(pctl->pinctrl);
  1061. pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
  1062. PINCTRL_STATE_DEFAULT);
  1063. if (IS_ERR(pctl->state_default))
  1064. return PTR_ERR(pctl->state_default);
  1065. pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
  1066. PINCTRL_STATE_IDLE);
  1067. return 0;
  1068. }
  1069. static int cam_clk_prepare(struct clk_hw *hw)
  1070. {
  1071. struct cam_clk *camclk = to_cam_clk(hw);
  1072. int ret;
  1073. if (camclk->fmd->pmf == NULL)
  1074. return -ENODEV;
  1075. ret = pm_runtime_get_sync(camclk->fmd->pmf);
  1076. return ret < 0 ? ret : 0;
  1077. }
  1078. static void cam_clk_unprepare(struct clk_hw *hw)
  1079. {
  1080. struct cam_clk *camclk = to_cam_clk(hw);
  1081. if (camclk->fmd->pmf == NULL)
  1082. return;
  1083. pm_runtime_put_sync(camclk->fmd->pmf);
  1084. }
  1085. static const struct clk_ops cam_clk_ops = {
  1086. .prepare = cam_clk_prepare,
  1087. .unprepare = cam_clk_unprepare,
  1088. };
  1089. static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
  1090. {
  1091. struct cam_clk_provider *cp = &fmd->clk_provider;
  1092. unsigned int i;
  1093. if (cp->of_node)
  1094. of_clk_del_provider(cp->of_node);
  1095. for (i = 0; i < cp->num_clocks; i++)
  1096. clk_unregister(cp->clks[i]);
  1097. }
  1098. static int fimc_md_register_clk_provider(struct fimc_md *fmd)
  1099. {
  1100. struct cam_clk_provider *cp = &fmd->clk_provider;
  1101. struct device *dev = &fmd->pdev->dev;
  1102. int i, ret;
  1103. for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
  1104. struct cam_clk *camclk = &cp->camclk[i];
  1105. struct clk_init_data init;
  1106. const char *p_name;
  1107. ret = of_property_read_string_index(dev->of_node,
  1108. "clock-output-names", i, &init.name);
  1109. if (ret < 0)
  1110. break;
  1111. p_name = __clk_get_name(fmd->camclk[i].clock);
  1112. /* It's safe since clk_register() will duplicate the string. */
  1113. init.parent_names = &p_name;
  1114. init.num_parents = 1;
  1115. init.ops = &cam_clk_ops;
  1116. init.flags = CLK_SET_RATE_PARENT;
  1117. camclk->hw.init = &init;
  1118. camclk->fmd = fmd;
  1119. cp->clks[i] = clk_register(NULL, &camclk->hw);
  1120. if (IS_ERR(cp->clks[i])) {
  1121. dev_err(dev, "failed to register clock: %s (%ld)\n",
  1122. init.name, PTR_ERR(cp->clks[i]));
  1123. ret = PTR_ERR(cp->clks[i]);
  1124. goto err;
  1125. }
  1126. cp->num_clocks++;
  1127. }
  1128. if (cp->num_clocks == 0) {
  1129. dev_warn(dev, "clk provider not registered\n");
  1130. return 0;
  1131. }
  1132. cp->clk_data.clks = cp->clks;
  1133. cp->clk_data.clk_num = cp->num_clocks;
  1134. cp->of_node = dev->of_node;
  1135. ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
  1136. &cp->clk_data);
  1137. if (ret == 0)
  1138. return 0;
  1139. err:
  1140. fimc_md_unregister_clk_provider(fmd);
  1141. return ret;
  1142. }
  1143. static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
  1144. struct v4l2_subdev *subdev,
  1145. struct v4l2_async_subdev *asd)
  1146. {
  1147. struct fimc_md *fmd = notifier_to_fimc_md(notifier);
  1148. struct fimc_sensor_info *si = NULL;
  1149. int i;
  1150. /* Find platform data for this sensor subdev */
  1151. for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
  1152. if (fmd->sensor[i].asd.match.fwnode ==
  1153. of_fwnode_handle(subdev->dev->of_node))
  1154. si = &fmd->sensor[i];
  1155. if (si == NULL)
  1156. return -EINVAL;
  1157. v4l2_set_subdev_hostdata(subdev, &si->pdata);
  1158. if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
  1159. subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
  1160. else
  1161. subdev->grp_id = GRP_ID_SENSOR;
  1162. si->subdev = subdev;
  1163. v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
  1164. subdev->name, fmd->num_sensors);
  1165. fmd->num_sensors++;
  1166. return 0;
  1167. }
  1168. static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
  1169. {
  1170. struct fimc_md *fmd = notifier_to_fimc_md(notifier);
  1171. int ret;
  1172. mutex_lock(&fmd->media_dev.graph_mutex);
  1173. ret = fimc_md_create_links(fmd);
  1174. if (ret < 0)
  1175. goto unlock;
  1176. ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
  1177. unlock:
  1178. mutex_unlock(&fmd->media_dev.graph_mutex);
  1179. if (ret < 0)
  1180. return ret;
  1181. return media_device_register(&fmd->media_dev);
  1182. }
  1183. static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
  1184. .bound = subdev_notifier_bound,
  1185. .complete = subdev_notifier_complete,
  1186. };
  1187. static int fimc_md_probe(struct platform_device *pdev)
  1188. {
  1189. struct device *dev = &pdev->dev;
  1190. struct v4l2_device *v4l2_dev;
  1191. struct fimc_md *fmd;
  1192. int ret;
  1193. fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
  1194. if (!fmd)
  1195. return -ENOMEM;
  1196. spin_lock_init(&fmd->slock);
  1197. INIT_LIST_HEAD(&fmd->pipelines);
  1198. fmd->pdev = pdev;
  1199. strscpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
  1200. sizeof(fmd->media_dev.model));
  1201. fmd->media_dev.ops = &fimc_md_ops;
  1202. fmd->media_dev.dev = dev;
  1203. v4l2_dev = &fmd->v4l2_dev;
  1204. v4l2_dev->mdev = &fmd->media_dev;
  1205. v4l2_dev->notify = fimc_sensor_notify;
  1206. strscpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
  1207. fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
  1208. fmd->user_subdev_api = true;
  1209. media_device_init(&fmd->media_dev);
  1210. ret = v4l2_device_register(dev, &fmd->v4l2_dev);
  1211. if (ret < 0) {
  1212. v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
  1213. return ret;
  1214. }
  1215. ret = fimc_md_get_clocks(fmd);
  1216. if (ret)
  1217. goto err_md;
  1218. ret = fimc_md_get_pinctrl(fmd);
  1219. if (ret < 0) {
  1220. if (ret != EPROBE_DEFER)
  1221. dev_err(dev, "Failed to get pinctrl: %d\n", ret);
  1222. goto err_clk;
  1223. }
  1224. platform_set_drvdata(pdev, fmd);
  1225. v4l2_async_notifier_init(&fmd->subdev_notifier);
  1226. ret = fimc_md_register_platform_entities(fmd, dev->of_node);
  1227. if (ret)
  1228. goto err_clk;
  1229. ret = fimc_md_register_sensor_entities(fmd);
  1230. if (ret)
  1231. goto err_m_ent;
  1232. ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1233. if (ret)
  1234. goto err_cleanup;
  1235. /*
  1236. * FIMC platform devices need to be registered before the sclk_cam
  1237. * clocks provider, as one of these devices needs to be activated
  1238. * to enable the clock.
  1239. */
  1240. ret = fimc_md_register_clk_provider(fmd);
  1241. if (ret < 0) {
  1242. v4l2_err(v4l2_dev, "clock provider registration failed\n");
  1243. goto err_attr;
  1244. }
  1245. if (fmd->num_sensors > 0) {
  1246. fmd->subdev_notifier.ops = &subdev_notifier_ops;
  1247. fmd->num_sensors = 0;
  1248. ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
  1249. &fmd->subdev_notifier);
  1250. if (ret)
  1251. goto err_clk_p;
  1252. }
  1253. return 0;
  1254. err_clk_p:
  1255. fimc_md_unregister_clk_provider(fmd);
  1256. err_attr:
  1257. device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1258. err_cleanup:
  1259. v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
  1260. err_m_ent:
  1261. fimc_md_unregister_entities(fmd);
  1262. err_clk:
  1263. fimc_md_put_clocks(fmd);
  1264. err_md:
  1265. media_device_cleanup(&fmd->media_dev);
  1266. v4l2_device_unregister(&fmd->v4l2_dev);
  1267. return ret;
  1268. }
  1269. static int fimc_md_remove(struct platform_device *pdev)
  1270. {
  1271. struct fimc_md *fmd = platform_get_drvdata(pdev);
  1272. if (!fmd)
  1273. return 0;
  1274. fimc_md_unregister_clk_provider(fmd);
  1275. v4l2_async_notifier_unregister(&fmd->subdev_notifier);
  1276. v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
  1277. v4l2_device_unregister(&fmd->v4l2_dev);
  1278. device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
  1279. fimc_md_unregister_entities(fmd);
  1280. fimc_md_pipelines_free(fmd);
  1281. media_device_unregister(&fmd->media_dev);
  1282. media_device_cleanup(&fmd->media_dev);
  1283. fimc_md_put_clocks(fmd);
  1284. return 0;
  1285. }
  1286. static const struct platform_device_id fimc_driver_ids[] __always_unused = {
  1287. { .name = "s5p-fimc-md" },
  1288. { },
  1289. };
  1290. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1291. static const struct of_device_id fimc_md_of_match[] = {
  1292. { .compatible = "samsung,fimc" },
  1293. { },
  1294. };
  1295. MODULE_DEVICE_TABLE(of, fimc_md_of_match);
  1296. static struct platform_driver fimc_md_driver = {
  1297. .probe = fimc_md_probe,
  1298. .remove = fimc_md_remove,
  1299. .driver = {
  1300. .of_match_table = of_match_ptr(fimc_md_of_match),
  1301. .name = "s5p-fimc-md",
  1302. }
  1303. };
  1304. static int __init fimc_md_init(void)
  1305. {
  1306. int ret;
  1307. request_module("s5p-csis");
  1308. ret = fimc_register_driver();
  1309. if (ret)
  1310. return ret;
  1311. return platform_driver_register(&fimc_md_driver);
  1312. }
  1313. static void __exit fimc_md_exit(void)
  1314. {
  1315. platform_driver_unregister(&fimc_md_driver);
  1316. fimc_unregister_driver();
  1317. }
  1318. module_init(fimc_md_init);
  1319. module_exit(fimc_md_exit);
  1320. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  1321. MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
  1322. MODULE_LICENSE("GPL");
  1323. MODULE_VERSION("2.0.1");