media-dev.c 37 KB

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