media-dev.c 39 KB

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