fimc-mdevice.c 35 KB

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