media-dev.c 39 KB

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