fimc-mdevice.c 34 KB

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