fimc-mdevice.c 35 KB

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