media-dev.c 38 KB

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