fimc-mdevice.c 36 KB

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