soc_camera.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. /*
  2. * camera image capture (abstract) bus driver
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  5. *
  6. * This driver provides an interface between platform-specific camera
  7. * busses and camera devices. It should be used if the camera is
  8. * connected not over a "proper" bus like PCI or USB, but over a
  9. * special bus, like, for example, the Quick Capture interface on PXA270
  10. * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
  11. * It can handle multiple cameras and / or multiple busses, which can
  12. * be used, e.g., in stereo-vision applications.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include <media/soc_camera.h>
  31. #include <media/drv-intf/soc_mediabus.h>
  32. #include <media/v4l2-async.h>
  33. #include <media/v4l2-clk.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-dev.h>
  37. #include <media/v4l2-of.h>
  38. #include <media/videobuf2-v4l2.h>
  39. /* Default to VGA resolution */
  40. #define DEFAULT_WIDTH 640
  41. #define DEFAULT_HEIGHT 480
  42. #define MAP_MAX_NUM 32
  43. static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
  44. static LIST_HEAD(hosts);
  45. static LIST_HEAD(devices);
  46. /*
  47. * Protects lists and bitmaps of hosts and devices.
  48. * Lock nesting: Ok to take ->host_lock under list_lock.
  49. */
  50. static DEFINE_MUTEX(list_lock);
  51. struct soc_camera_async_client {
  52. struct v4l2_async_subdev *sensor;
  53. struct v4l2_async_notifier notifier;
  54. struct platform_device *pdev;
  55. struct list_head list; /* needed for clean up */
  56. };
  57. static int soc_camera_video_start(struct soc_camera_device *icd);
  58. static int video_dev_create(struct soc_camera_device *icd);
  59. int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
  60. struct v4l2_clk *clk)
  61. {
  62. int ret;
  63. bool clock_toggle;
  64. if (clk && (!ssdd->unbalanced_power ||
  65. !test_and_set_bit(0, &ssdd->clock_state))) {
  66. ret = v4l2_clk_enable(clk);
  67. if (ret < 0) {
  68. dev_err(dev, "Cannot enable clock: %d\n", ret);
  69. return ret;
  70. }
  71. clock_toggle = true;
  72. } else {
  73. clock_toggle = false;
  74. }
  75. ret = regulator_bulk_enable(ssdd->sd_pdata.num_regulators,
  76. ssdd->sd_pdata.regulators);
  77. if (ret < 0) {
  78. dev_err(dev, "Cannot enable regulators\n");
  79. goto eregenable;
  80. }
  81. if (ssdd->power) {
  82. ret = ssdd->power(dev, 1);
  83. if (ret < 0) {
  84. dev_err(dev,
  85. "Platform failed to power-on the camera.\n");
  86. goto epwron;
  87. }
  88. }
  89. return 0;
  90. epwron:
  91. regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
  92. ssdd->sd_pdata.regulators);
  93. eregenable:
  94. if (clock_toggle)
  95. v4l2_clk_disable(clk);
  96. return ret;
  97. }
  98. EXPORT_SYMBOL(soc_camera_power_on);
  99. int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
  100. struct v4l2_clk *clk)
  101. {
  102. int ret = 0;
  103. int err;
  104. if (ssdd->power) {
  105. err = ssdd->power(dev, 0);
  106. if (err < 0) {
  107. dev_err(dev,
  108. "Platform failed to power-off the camera.\n");
  109. ret = err;
  110. }
  111. }
  112. err = regulator_bulk_disable(ssdd->sd_pdata.num_regulators,
  113. ssdd->sd_pdata.regulators);
  114. if (err < 0) {
  115. dev_err(dev, "Cannot disable regulators\n");
  116. ret = ret ? : err;
  117. }
  118. if (clk && (!ssdd->unbalanced_power || test_and_clear_bit(0, &ssdd->clock_state)))
  119. v4l2_clk_disable(clk);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(soc_camera_power_off);
  123. int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
  124. {
  125. /* Should not have any effect in synchronous case */
  126. return devm_regulator_bulk_get(dev, ssdd->sd_pdata.num_regulators,
  127. ssdd->sd_pdata.regulators);
  128. }
  129. EXPORT_SYMBOL(soc_camera_power_init);
  130. static int __soc_camera_power_on(struct soc_camera_device *icd)
  131. {
  132. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  133. int ret;
  134. ret = v4l2_subdev_call(sd, core, s_power, 1);
  135. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  136. return ret;
  137. return 0;
  138. }
  139. static int __soc_camera_power_off(struct soc_camera_device *icd)
  140. {
  141. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  142. int ret;
  143. ret = v4l2_subdev_call(sd, core, s_power, 0);
  144. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  145. return ret;
  146. return 0;
  147. }
  148. static int soc_camera_clock_start(struct soc_camera_host *ici)
  149. {
  150. int ret;
  151. if (!ici->ops->clock_start)
  152. return 0;
  153. mutex_lock(&ici->clk_lock);
  154. ret = ici->ops->clock_start(ici);
  155. mutex_unlock(&ici->clk_lock);
  156. return ret;
  157. }
  158. static void soc_camera_clock_stop(struct soc_camera_host *ici)
  159. {
  160. if (!ici->ops->clock_stop)
  161. return;
  162. mutex_lock(&ici->clk_lock);
  163. ici->ops->clock_stop(ici);
  164. mutex_unlock(&ici->clk_lock);
  165. }
  166. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  167. struct soc_camera_device *icd, unsigned int fourcc)
  168. {
  169. unsigned int i;
  170. for (i = 0; i < icd->num_user_formats; i++)
  171. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  172. return icd->user_formats + i;
  173. return NULL;
  174. }
  175. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  176. /**
  177. * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  178. * @ssdd: camera platform parameters
  179. * @cfg: media bus configuration
  180. * @return: resulting flags
  181. */
  182. unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
  183. const struct v4l2_mbus_config *cfg)
  184. {
  185. unsigned long f, flags = cfg->flags;
  186. /* If only one of the two polarities is supported, switch to the opposite */
  187. if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  188. f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
  189. if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
  190. flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
  191. }
  192. if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  193. f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
  194. if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
  195. flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
  196. }
  197. if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
  198. f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
  199. if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
  200. flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  201. }
  202. return flags;
  203. }
  204. EXPORT_SYMBOL(soc_camera_apply_board_flags);
  205. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  206. ((x) >> 24) & 0xff
  207. static int soc_camera_try_fmt(struct soc_camera_device *icd,
  208. struct v4l2_format *f)
  209. {
  210. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  211. const struct soc_camera_format_xlate *xlate;
  212. struct v4l2_pix_format *pix = &f->fmt.pix;
  213. int ret;
  214. dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
  215. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  216. if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
  217. !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
  218. pix->bytesperline = 0;
  219. pix->sizeimage = 0;
  220. }
  221. ret = ici->ops->try_fmt(icd, f);
  222. if (ret < 0)
  223. return ret;
  224. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  225. if (!xlate)
  226. return -EINVAL;
  227. ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
  228. if (ret < 0)
  229. return ret;
  230. pix->bytesperline = max_t(u32, pix->bytesperline, ret);
  231. ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
  232. pix->height);
  233. if (ret < 0)
  234. return ret;
  235. pix->sizeimage = max_t(u32, pix->sizeimage, ret);
  236. return 0;
  237. }
  238. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  239. struct v4l2_format *f)
  240. {
  241. struct soc_camera_device *icd = file->private_data;
  242. WARN_ON(priv != file->private_data);
  243. /* Only single-plane capture is supported so far */
  244. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  245. return -EINVAL;
  246. /* limit format to hardware capabilities */
  247. return soc_camera_try_fmt(icd, f);
  248. }
  249. static int soc_camera_enum_input(struct file *file, void *priv,
  250. struct v4l2_input *inp)
  251. {
  252. struct soc_camera_device *icd = file->private_data;
  253. if (inp->index != 0)
  254. return -EINVAL;
  255. /* default is camera */
  256. inp->type = V4L2_INPUT_TYPE_CAMERA;
  257. inp->std = icd->vdev->tvnorms;
  258. strcpy(inp->name, "Camera");
  259. return 0;
  260. }
  261. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  262. {
  263. *i = 0;
  264. return 0;
  265. }
  266. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  267. {
  268. if (i > 0)
  269. return -EINVAL;
  270. return 0;
  271. }
  272. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
  273. {
  274. struct soc_camera_device *icd = file->private_data;
  275. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  276. return v4l2_subdev_call(sd, video, s_std, a);
  277. }
  278. static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
  279. {
  280. struct soc_camera_device *icd = file->private_data;
  281. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  282. return v4l2_subdev_call(sd, video, g_std, a);
  283. }
  284. static int soc_camera_enum_framesizes(struct file *file, void *fh,
  285. struct v4l2_frmsizeenum *fsize)
  286. {
  287. struct soc_camera_device *icd = file->private_data;
  288. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  289. return ici->ops->enum_framesizes(icd, fsize);
  290. }
  291. static int soc_camera_reqbufs(struct file *file, void *priv,
  292. struct v4l2_requestbuffers *p)
  293. {
  294. int ret;
  295. struct soc_camera_device *icd = file->private_data;
  296. WARN_ON(priv != file->private_data);
  297. if (icd->streamer && icd->streamer != file)
  298. return -EBUSY;
  299. ret = vb2_reqbufs(&icd->vb2_vidq, p);
  300. if (!ret)
  301. icd->streamer = p->count ? file : NULL;
  302. return ret;
  303. }
  304. static int soc_camera_querybuf(struct file *file, void *priv,
  305. struct v4l2_buffer *p)
  306. {
  307. struct soc_camera_device *icd = file->private_data;
  308. WARN_ON(priv != file->private_data);
  309. return vb2_querybuf(&icd->vb2_vidq, p);
  310. }
  311. static int soc_camera_qbuf(struct file *file, void *priv,
  312. struct v4l2_buffer *p)
  313. {
  314. struct soc_camera_device *icd = file->private_data;
  315. WARN_ON(priv != file->private_data);
  316. if (icd->streamer != file)
  317. return -EBUSY;
  318. return vb2_qbuf(&icd->vb2_vidq, p);
  319. }
  320. static int soc_camera_dqbuf(struct file *file, void *priv,
  321. struct v4l2_buffer *p)
  322. {
  323. struct soc_camera_device *icd = file->private_data;
  324. WARN_ON(priv != file->private_data);
  325. if (icd->streamer != file)
  326. return -EBUSY;
  327. return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
  328. }
  329. static int soc_camera_create_bufs(struct file *file, void *priv,
  330. struct v4l2_create_buffers *create)
  331. {
  332. struct soc_camera_device *icd = file->private_data;
  333. int ret;
  334. if (icd->streamer && icd->streamer != file)
  335. return -EBUSY;
  336. ret = vb2_create_bufs(&icd->vb2_vidq, create);
  337. if (!ret)
  338. icd->streamer = file;
  339. return ret;
  340. }
  341. static int soc_camera_prepare_buf(struct file *file, void *priv,
  342. struct v4l2_buffer *b)
  343. {
  344. struct soc_camera_device *icd = file->private_data;
  345. return vb2_prepare_buf(&icd->vb2_vidq, b);
  346. }
  347. static int soc_camera_expbuf(struct file *file, void *priv,
  348. struct v4l2_exportbuffer *p)
  349. {
  350. struct soc_camera_device *icd = file->private_data;
  351. if (icd->streamer && icd->streamer != file)
  352. return -EBUSY;
  353. return vb2_expbuf(&icd->vb2_vidq, p);
  354. }
  355. /* Always entered with .host_lock held */
  356. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  357. {
  358. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  359. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  360. unsigned int i, fmts = 0, raw_fmts = 0;
  361. int ret;
  362. struct v4l2_subdev_mbus_code_enum code = {
  363. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  364. };
  365. while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
  366. raw_fmts++;
  367. code.index++;
  368. }
  369. if (!ici->ops->get_formats)
  370. /*
  371. * Fallback mode - the host will have to serve all
  372. * sensor-provided formats one-to-one to the user
  373. */
  374. fmts = raw_fmts;
  375. else
  376. /*
  377. * First pass - only count formats this host-sensor
  378. * configuration can provide
  379. */
  380. for (i = 0; i < raw_fmts; i++) {
  381. ret = ici->ops->get_formats(icd, i, NULL);
  382. if (ret < 0)
  383. return ret;
  384. fmts += ret;
  385. }
  386. if (!fmts)
  387. return -ENXIO;
  388. icd->user_formats =
  389. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  390. if (!icd->user_formats)
  391. return -ENOMEM;
  392. dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
  393. /* Second pass - actually fill data formats */
  394. fmts = 0;
  395. for (i = 0; i < raw_fmts; i++)
  396. if (!ici->ops->get_formats) {
  397. code.index = i;
  398. v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
  399. icd->user_formats[fmts].host_fmt =
  400. soc_mbus_get_fmtdesc(code.code);
  401. if (icd->user_formats[fmts].host_fmt)
  402. icd->user_formats[fmts++].code = code.code;
  403. } else {
  404. ret = ici->ops->get_formats(icd, i,
  405. &icd->user_formats[fmts]);
  406. if (ret < 0)
  407. goto egfmt;
  408. fmts += ret;
  409. }
  410. icd->num_user_formats = fmts;
  411. icd->current_fmt = &icd->user_formats[0];
  412. return 0;
  413. egfmt:
  414. vfree(icd->user_formats);
  415. return ret;
  416. }
  417. /* Always entered with .host_lock held */
  418. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  419. {
  420. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  421. if (ici->ops->put_formats)
  422. ici->ops->put_formats(icd);
  423. icd->current_fmt = NULL;
  424. icd->num_user_formats = 0;
  425. vfree(icd->user_formats);
  426. icd->user_formats = NULL;
  427. }
  428. /* Called with .vb_lock held, or from the first open(2), see comment there */
  429. static int soc_camera_set_fmt(struct soc_camera_device *icd,
  430. struct v4l2_format *f)
  431. {
  432. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  433. struct v4l2_pix_format *pix = &f->fmt.pix;
  434. int ret;
  435. dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
  436. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  437. /* We always call try_fmt() before set_fmt() or set_selection() */
  438. ret = soc_camera_try_fmt(icd, f);
  439. if (ret < 0)
  440. return ret;
  441. ret = ici->ops->set_fmt(icd, f);
  442. if (ret < 0) {
  443. return ret;
  444. } else if (!icd->current_fmt ||
  445. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  446. dev_err(icd->pdev,
  447. "Host driver hasn't set up current format correctly!\n");
  448. return -EINVAL;
  449. }
  450. icd->user_width = pix->width;
  451. icd->user_height = pix->height;
  452. icd->bytesperline = pix->bytesperline;
  453. icd->sizeimage = pix->sizeimage;
  454. icd->colorspace = pix->colorspace;
  455. icd->field = pix->field;
  456. dev_dbg(icd->pdev, "set width: %d height: %d\n",
  457. icd->user_width, icd->user_height);
  458. /* set physical bus parameters */
  459. return ici->ops->set_bus_param(icd);
  460. }
  461. static int soc_camera_add_device(struct soc_camera_device *icd)
  462. {
  463. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  464. int ret;
  465. if (ici->icd)
  466. return -EBUSY;
  467. if (!icd->clk) {
  468. ret = soc_camera_clock_start(ici);
  469. if (ret < 0)
  470. return ret;
  471. }
  472. if (ici->ops->add) {
  473. ret = ici->ops->add(icd);
  474. if (ret < 0)
  475. goto eadd;
  476. }
  477. ici->icd = icd;
  478. return 0;
  479. eadd:
  480. if (!icd->clk)
  481. soc_camera_clock_stop(ici);
  482. return ret;
  483. }
  484. static void soc_camera_remove_device(struct soc_camera_device *icd)
  485. {
  486. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  487. if (WARN_ON(icd != ici->icd))
  488. return;
  489. if (ici->ops->remove)
  490. ici->ops->remove(icd);
  491. if (!icd->clk)
  492. soc_camera_clock_stop(ici);
  493. ici->icd = NULL;
  494. }
  495. static int soc_camera_open(struct file *file)
  496. {
  497. struct video_device *vdev = video_devdata(file);
  498. struct soc_camera_device *icd;
  499. struct soc_camera_host *ici;
  500. int ret;
  501. /*
  502. * Don't mess with the host during probe: wait until the loop in
  503. * scan_add_host() completes. Also protect against a race with
  504. * soc_camera_host_unregister().
  505. */
  506. if (mutex_lock_interruptible(&list_lock))
  507. return -ERESTARTSYS;
  508. if (!vdev || !video_is_registered(vdev)) {
  509. mutex_unlock(&list_lock);
  510. return -ENODEV;
  511. }
  512. icd = video_get_drvdata(vdev);
  513. ici = to_soc_camera_host(icd->parent);
  514. ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
  515. mutex_unlock(&list_lock);
  516. if (ret < 0) {
  517. dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
  518. return ret;
  519. }
  520. if (!to_soc_camera_control(icd)) {
  521. /* No device driver attached */
  522. ret = -ENODEV;
  523. goto econtrol;
  524. }
  525. if (mutex_lock_interruptible(&ici->host_lock)) {
  526. ret = -ERESTARTSYS;
  527. goto elockhost;
  528. }
  529. icd->use_count++;
  530. /* Now we really have to activate the camera */
  531. if (icd->use_count == 1) {
  532. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  533. /* Restore parameters before the last close() per V4L2 API */
  534. struct v4l2_format f = {
  535. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  536. .fmt.pix = {
  537. .width = icd->user_width,
  538. .height = icd->user_height,
  539. .field = icd->field,
  540. .colorspace = icd->colorspace,
  541. .pixelformat =
  542. icd->current_fmt->host_fmt->fourcc,
  543. },
  544. };
  545. /* The camera could have been already on, try to reset */
  546. if (sdesc->subdev_desc.reset)
  547. if (icd->control)
  548. sdesc->subdev_desc.reset(icd->control);
  549. ret = soc_camera_add_device(icd);
  550. if (ret < 0) {
  551. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  552. goto eiciadd;
  553. }
  554. ret = __soc_camera_power_on(icd);
  555. if (ret < 0)
  556. goto epower;
  557. pm_runtime_enable(&icd->vdev->dev);
  558. ret = pm_runtime_resume(&icd->vdev->dev);
  559. if (ret < 0 && ret != -ENOSYS)
  560. goto eresume;
  561. /*
  562. * Try to configure with default parameters. Notice: this is the
  563. * very first open, so, we cannot race against other calls,
  564. * apart from someone else calling open() simultaneously, but
  565. * .host_lock is protecting us against it.
  566. */
  567. ret = soc_camera_set_fmt(icd, &f);
  568. if (ret < 0)
  569. goto esfmt;
  570. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  571. if (ret < 0)
  572. goto einitvb;
  573. v4l2_ctrl_handler_setup(&icd->ctrl_handler);
  574. }
  575. mutex_unlock(&ici->host_lock);
  576. file->private_data = icd;
  577. dev_dbg(icd->pdev, "camera device open\n");
  578. return 0;
  579. /*
  580. * All errors are entered with the .host_lock held, first four also
  581. * with use_count == 1
  582. */
  583. einitvb:
  584. esfmt:
  585. pm_runtime_disable(&icd->vdev->dev);
  586. eresume:
  587. __soc_camera_power_off(icd);
  588. epower:
  589. soc_camera_remove_device(icd);
  590. eiciadd:
  591. icd->use_count--;
  592. mutex_unlock(&ici->host_lock);
  593. elockhost:
  594. econtrol:
  595. module_put(ici->ops->owner);
  596. return ret;
  597. }
  598. static int soc_camera_close(struct file *file)
  599. {
  600. struct soc_camera_device *icd = file->private_data;
  601. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  602. mutex_lock(&ici->host_lock);
  603. if (icd->streamer == file) {
  604. if (ici->ops->init_videobuf2)
  605. vb2_queue_release(&icd->vb2_vidq);
  606. icd->streamer = NULL;
  607. }
  608. icd->use_count--;
  609. if (!icd->use_count) {
  610. pm_runtime_suspend(&icd->vdev->dev);
  611. pm_runtime_disable(&icd->vdev->dev);
  612. __soc_camera_power_off(icd);
  613. soc_camera_remove_device(icd);
  614. }
  615. mutex_unlock(&ici->host_lock);
  616. module_put(ici->ops->owner);
  617. dev_dbg(icd->pdev, "camera device close\n");
  618. return 0;
  619. }
  620. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  621. size_t count, loff_t *ppos)
  622. {
  623. struct soc_camera_device *icd = file->private_data;
  624. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  625. dev_dbg(icd->pdev, "read called, buf %p\n", buf);
  626. if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
  627. return vb2_read(&icd->vb2_vidq, buf, count, ppos,
  628. file->f_flags & O_NONBLOCK);
  629. dev_err(icd->pdev, "camera device read not implemented\n");
  630. return -EINVAL;
  631. }
  632. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  633. {
  634. struct soc_camera_device *icd = file->private_data;
  635. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  636. int err;
  637. dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  638. if (icd->streamer != file)
  639. return -EBUSY;
  640. if (mutex_lock_interruptible(&ici->host_lock))
  641. return -ERESTARTSYS;
  642. err = vb2_mmap(&icd->vb2_vidq, vma);
  643. mutex_unlock(&ici->host_lock);
  644. dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  645. (unsigned long)vma->vm_start,
  646. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  647. err);
  648. return err;
  649. }
  650. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  651. {
  652. struct soc_camera_device *icd = file->private_data;
  653. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  654. unsigned res = POLLERR;
  655. if (icd->streamer != file)
  656. return POLLERR;
  657. mutex_lock(&ici->host_lock);
  658. res = ici->ops->poll(file, pt);
  659. mutex_unlock(&ici->host_lock);
  660. return res;
  661. }
  662. static struct v4l2_file_operations soc_camera_fops = {
  663. .owner = THIS_MODULE,
  664. .open = soc_camera_open,
  665. .release = soc_camera_close,
  666. .unlocked_ioctl = video_ioctl2,
  667. .read = soc_camera_read,
  668. .mmap = soc_camera_mmap,
  669. .poll = soc_camera_poll,
  670. };
  671. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  672. struct v4l2_format *f)
  673. {
  674. struct soc_camera_device *icd = file->private_data;
  675. int ret;
  676. WARN_ON(priv != file->private_data);
  677. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  678. dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
  679. return -EINVAL;
  680. }
  681. if (icd->streamer && icd->streamer != file)
  682. return -EBUSY;
  683. if (vb2_is_streaming(&icd->vb2_vidq)) {
  684. dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
  685. return -EBUSY;
  686. }
  687. ret = soc_camera_set_fmt(icd, f);
  688. if (!ret && !icd->streamer)
  689. icd->streamer = file;
  690. return ret;
  691. }
  692. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  693. struct v4l2_fmtdesc *f)
  694. {
  695. struct soc_camera_device *icd = file->private_data;
  696. const struct soc_mbus_pixelfmt *format;
  697. WARN_ON(priv != file->private_data);
  698. if (f->index >= icd->num_user_formats)
  699. return -EINVAL;
  700. format = icd->user_formats[f->index].host_fmt;
  701. if (format->name)
  702. strlcpy(f->description, format->name, sizeof(f->description));
  703. f->pixelformat = format->fourcc;
  704. return 0;
  705. }
  706. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  707. struct v4l2_format *f)
  708. {
  709. struct soc_camera_device *icd = file->private_data;
  710. struct v4l2_pix_format *pix = &f->fmt.pix;
  711. WARN_ON(priv != file->private_data);
  712. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  713. return -EINVAL;
  714. pix->width = icd->user_width;
  715. pix->height = icd->user_height;
  716. pix->bytesperline = icd->bytesperline;
  717. pix->sizeimage = icd->sizeimage;
  718. pix->field = icd->field;
  719. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  720. pix->colorspace = icd->colorspace;
  721. dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
  722. icd->current_fmt->host_fmt->fourcc);
  723. return 0;
  724. }
  725. static int soc_camera_querycap(struct file *file, void *priv,
  726. struct v4l2_capability *cap)
  727. {
  728. struct soc_camera_device *icd = file->private_data;
  729. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  730. WARN_ON(priv != file->private_data);
  731. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  732. return ici->ops->querycap(ici, cap);
  733. }
  734. static int soc_camera_streamon(struct file *file, void *priv,
  735. enum v4l2_buf_type i)
  736. {
  737. struct soc_camera_device *icd = file->private_data;
  738. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  739. int ret;
  740. WARN_ON(priv != file->private_data);
  741. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  742. return -EINVAL;
  743. if (icd->streamer != file)
  744. return -EBUSY;
  745. /* This calls buf_queue from host driver's videobuf2_queue_ops */
  746. ret = vb2_streamon(&icd->vb2_vidq, i);
  747. if (!ret)
  748. v4l2_subdev_call(sd, video, s_stream, 1);
  749. return ret;
  750. }
  751. static int soc_camera_streamoff(struct file *file, void *priv,
  752. enum v4l2_buf_type i)
  753. {
  754. struct soc_camera_device *icd = file->private_data;
  755. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  756. int ret;
  757. WARN_ON(priv != file->private_data);
  758. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  759. return -EINVAL;
  760. if (icd->streamer != file)
  761. return -EBUSY;
  762. /*
  763. * This calls buf_release from host driver's videobuf2_queue_ops for all
  764. * remaining buffers. When the last buffer is freed, stop capture
  765. */
  766. ret = vb2_streamoff(&icd->vb2_vidq, i);
  767. v4l2_subdev_call(sd, video, s_stream, 0);
  768. return ret;
  769. }
  770. static int soc_camera_g_selection(struct file *file, void *fh,
  771. struct v4l2_selection *s)
  772. {
  773. struct soc_camera_device *icd = file->private_data;
  774. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  775. /* With a wrong type no need to try to fall back to cropping */
  776. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  777. return -EINVAL;
  778. return ici->ops->get_selection(icd, s);
  779. }
  780. static int soc_camera_s_selection(struct file *file, void *fh,
  781. struct v4l2_selection *s)
  782. {
  783. struct soc_camera_device *icd = file->private_data;
  784. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  785. int ret;
  786. /* In all these cases cropping emulation will not help */
  787. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  788. (s->target != V4L2_SEL_TGT_COMPOSE &&
  789. s->target != V4L2_SEL_TGT_CROP))
  790. return -EINVAL;
  791. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  792. /* No output size change during a running capture! */
  793. if (vb2_is_streaming(&icd->vb2_vidq) &&
  794. (icd->user_width != s->r.width ||
  795. icd->user_height != s->r.height))
  796. return -EBUSY;
  797. /*
  798. * Only one user is allowed to change the output format, touch
  799. * buffers, start / stop streaming, poll for data
  800. */
  801. if (icd->streamer && icd->streamer != file)
  802. return -EBUSY;
  803. }
  804. if (s->target == V4L2_SEL_TGT_CROP &&
  805. vb2_is_streaming(&icd->vb2_vidq) &&
  806. ici->ops->set_liveselection)
  807. ret = ici->ops->set_liveselection(icd, s);
  808. else
  809. ret = ici->ops->set_selection(icd, s);
  810. if (!ret &&
  811. s->target == V4L2_SEL_TGT_COMPOSE) {
  812. icd->user_width = s->r.width;
  813. icd->user_height = s->r.height;
  814. if (!icd->streamer)
  815. icd->streamer = file;
  816. }
  817. return ret;
  818. }
  819. static int soc_camera_g_parm(struct file *file, void *fh,
  820. struct v4l2_streamparm *a)
  821. {
  822. struct soc_camera_device *icd = file->private_data;
  823. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  824. if (ici->ops->get_parm)
  825. return ici->ops->get_parm(icd, a);
  826. return -ENOIOCTLCMD;
  827. }
  828. static int soc_camera_s_parm(struct file *file, void *fh,
  829. struct v4l2_streamparm *a)
  830. {
  831. struct soc_camera_device *icd = file->private_data;
  832. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  833. if (ici->ops->set_parm)
  834. return ici->ops->set_parm(icd, a);
  835. return -ENOIOCTLCMD;
  836. }
  837. static int soc_camera_probe(struct soc_camera_host *ici,
  838. struct soc_camera_device *icd);
  839. /* So far this function cannot fail */
  840. static void scan_add_host(struct soc_camera_host *ici)
  841. {
  842. struct soc_camera_device *icd;
  843. mutex_lock(&list_lock);
  844. list_for_each_entry(icd, &devices, list)
  845. if (icd->iface == ici->nr) {
  846. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  847. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  848. /* The camera could have been already on, try to reset */
  849. if (ssdd->reset)
  850. if (icd->control)
  851. ssdd->reset(icd->control);
  852. icd->parent = ici->v4l2_dev.dev;
  853. /* Ignore errors */
  854. soc_camera_probe(ici, icd);
  855. }
  856. mutex_unlock(&list_lock);
  857. }
  858. /*
  859. * It is invalid to call v4l2_clk_enable() after a successful probing
  860. * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
  861. */
  862. static int soc_camera_clk_enable(struct v4l2_clk *clk)
  863. {
  864. struct soc_camera_device *icd = clk->priv;
  865. struct soc_camera_host *ici;
  866. if (!icd || !icd->parent)
  867. return -ENODEV;
  868. ici = to_soc_camera_host(icd->parent);
  869. if (!try_module_get(ici->ops->owner))
  870. return -ENODEV;
  871. /*
  872. * If a different client is currently being probed, the host will tell
  873. * you to go
  874. */
  875. return soc_camera_clock_start(ici);
  876. }
  877. static void soc_camera_clk_disable(struct v4l2_clk *clk)
  878. {
  879. struct soc_camera_device *icd = clk->priv;
  880. struct soc_camera_host *ici;
  881. if (!icd || !icd->parent)
  882. return;
  883. ici = to_soc_camera_host(icd->parent);
  884. soc_camera_clock_stop(ici);
  885. module_put(ici->ops->owner);
  886. }
  887. /*
  888. * Eventually, it would be more logical to make the respective host the clock
  889. * owner, but then we would have to copy this struct for each ici. Besides, it
  890. * would introduce the circular dependency problem, unless we port all client
  891. * drivers to release the clock, when not in use.
  892. */
  893. static const struct v4l2_clk_ops soc_camera_clk_ops = {
  894. .owner = THIS_MODULE,
  895. .enable = soc_camera_clk_enable,
  896. .disable = soc_camera_clk_disable,
  897. };
  898. static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
  899. struct soc_camera_async_client *sasc)
  900. {
  901. struct platform_device *pdev;
  902. int ret, i;
  903. mutex_lock(&list_lock);
  904. i = find_first_zero_bit(device_map, MAP_MAX_NUM);
  905. if (i < MAP_MAX_NUM)
  906. set_bit(i, device_map);
  907. mutex_unlock(&list_lock);
  908. if (i >= MAP_MAX_NUM)
  909. return -ENOMEM;
  910. pdev = platform_device_alloc("soc-camera-pdrv", i);
  911. if (!pdev)
  912. return -ENOMEM;
  913. ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
  914. if (ret < 0) {
  915. platform_device_put(pdev);
  916. return ret;
  917. }
  918. sasc->pdev = pdev;
  919. return 0;
  920. }
  921. static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
  922. {
  923. struct platform_device *pdev = sasc->pdev;
  924. int ret;
  925. ret = platform_device_add(pdev);
  926. if (ret < 0 || !pdev->dev.driver)
  927. return NULL;
  928. return platform_get_drvdata(pdev);
  929. }
  930. /* Locking: called with .host_lock held */
  931. static int soc_camera_probe_finish(struct soc_camera_device *icd)
  932. {
  933. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  934. struct v4l2_subdev_format fmt = {
  935. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  936. };
  937. struct v4l2_mbus_framefmt *mf = &fmt.format;
  938. int ret;
  939. sd->grp_id = soc_camera_grp_id(icd);
  940. v4l2_set_subdev_hostdata(sd, icd);
  941. v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
  942. ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
  943. if (ret < 0)
  944. return ret;
  945. ret = soc_camera_add_device(icd);
  946. if (ret < 0) {
  947. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  948. return ret;
  949. }
  950. /* At this point client .probe() should have run already */
  951. ret = soc_camera_init_user_formats(icd);
  952. if (ret < 0)
  953. goto eusrfmt;
  954. icd->field = V4L2_FIELD_ANY;
  955. ret = soc_camera_video_start(icd);
  956. if (ret < 0)
  957. goto evidstart;
  958. /* Try to improve our guess of a reasonable window format */
  959. if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
  960. icd->user_width = mf->width;
  961. icd->user_height = mf->height;
  962. icd->colorspace = mf->colorspace;
  963. icd->field = mf->field;
  964. }
  965. soc_camera_remove_device(icd);
  966. return 0;
  967. evidstart:
  968. soc_camera_free_user_formats(icd);
  969. eusrfmt:
  970. soc_camera_remove_device(icd);
  971. return ret;
  972. }
  973. #ifdef CONFIG_I2C_BOARDINFO
  974. static int soc_camera_i2c_init(struct soc_camera_device *icd,
  975. struct soc_camera_desc *sdesc)
  976. {
  977. struct soc_camera_subdev_desc *ssdd;
  978. struct i2c_client *client;
  979. struct soc_camera_host *ici;
  980. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  981. struct i2c_adapter *adap;
  982. struct v4l2_subdev *subdev;
  983. char clk_name[V4L2_CLK_NAME_SIZE];
  984. int ret;
  985. /* First find out how we link the main client */
  986. if (icd->sasc) {
  987. /* Async non-OF probing handled by the subdevice list */
  988. return -EPROBE_DEFER;
  989. }
  990. ici = to_soc_camera_host(icd->parent);
  991. adap = i2c_get_adapter(shd->i2c_adapter_id);
  992. if (!adap) {
  993. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  994. shd->i2c_adapter_id);
  995. return -ENODEV;
  996. }
  997. ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
  998. if (!ssdd) {
  999. ret = -ENOMEM;
  1000. goto ealloc;
  1001. }
  1002. /*
  1003. * In synchronous case we request regulators ourselves in
  1004. * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
  1005. * to allocate them again.
  1006. */
  1007. ssdd->sd_pdata.num_regulators = 0;
  1008. ssdd->sd_pdata.regulators = NULL;
  1009. shd->board_info->platform_data = ssdd;
  1010. v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
  1011. shd->i2c_adapter_id, shd->board_info->addr);
  1012. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1013. if (IS_ERR(icd->clk)) {
  1014. ret = PTR_ERR(icd->clk);
  1015. goto eclkreg;
  1016. }
  1017. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  1018. shd->board_info, NULL);
  1019. if (!subdev) {
  1020. ret = -ENODEV;
  1021. goto ei2cnd;
  1022. }
  1023. client = v4l2_get_subdevdata(subdev);
  1024. /* Use to_i2c_client(dev) to recover the i2c client */
  1025. icd->control = &client->dev;
  1026. return 0;
  1027. ei2cnd:
  1028. v4l2_clk_unregister(icd->clk);
  1029. icd->clk = NULL;
  1030. eclkreg:
  1031. kfree(ssdd);
  1032. ealloc:
  1033. i2c_put_adapter(adap);
  1034. return ret;
  1035. }
  1036. static void soc_camera_i2c_free(struct soc_camera_device *icd)
  1037. {
  1038. struct i2c_client *client =
  1039. to_i2c_client(to_soc_camera_control(icd));
  1040. struct i2c_adapter *adap;
  1041. struct soc_camera_subdev_desc *ssdd;
  1042. icd->control = NULL;
  1043. if (icd->sasc)
  1044. return;
  1045. adap = client->adapter;
  1046. ssdd = client->dev.platform_data;
  1047. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  1048. i2c_unregister_device(client);
  1049. i2c_put_adapter(adap);
  1050. kfree(ssdd);
  1051. v4l2_clk_unregister(icd->clk);
  1052. icd->clk = NULL;
  1053. }
  1054. /*
  1055. * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
  1056. * internal global mutex, therefore cannot race against other asynchronous
  1057. * events. Until notifier->complete() (soc_camera_async_complete()) is called,
  1058. * the video device node is not registered and no V4L fops can occur. Unloading
  1059. * of the host driver also calls a v4l2-async function, so also there we're
  1060. * protected.
  1061. */
  1062. static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
  1063. struct v4l2_subdev *sd,
  1064. struct v4l2_async_subdev *asd)
  1065. {
  1066. struct soc_camera_async_client *sasc = container_of(notifier,
  1067. struct soc_camera_async_client, notifier);
  1068. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1069. if (asd == sasc->sensor && !WARN_ON(icd->control)) {
  1070. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1071. /*
  1072. * Only now we get subdevice-specific information like
  1073. * regulators, flags, callbacks, etc.
  1074. */
  1075. if (client) {
  1076. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1077. struct soc_camera_subdev_desc *ssdd =
  1078. soc_camera_i2c_to_desc(client);
  1079. if (ssdd) {
  1080. memcpy(&sdesc->subdev_desc, ssdd,
  1081. sizeof(sdesc->subdev_desc));
  1082. if (ssdd->reset)
  1083. ssdd->reset(&client->dev);
  1084. }
  1085. icd->control = &client->dev;
  1086. }
  1087. }
  1088. return 0;
  1089. }
  1090. static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
  1091. struct v4l2_subdev *sd,
  1092. struct v4l2_async_subdev *asd)
  1093. {
  1094. struct soc_camera_async_client *sasc = container_of(notifier,
  1095. struct soc_camera_async_client, notifier);
  1096. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1097. icd->control = NULL;
  1098. if (icd->clk) {
  1099. v4l2_clk_unregister(icd->clk);
  1100. icd->clk = NULL;
  1101. }
  1102. }
  1103. static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
  1104. {
  1105. struct soc_camera_async_client *sasc = container_of(notifier,
  1106. struct soc_camera_async_client, notifier);
  1107. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1108. if (to_soc_camera_control(icd)) {
  1109. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1110. int ret;
  1111. mutex_lock(&list_lock);
  1112. ret = soc_camera_probe(ici, icd);
  1113. mutex_unlock(&list_lock);
  1114. if (ret < 0)
  1115. return ret;
  1116. }
  1117. return 0;
  1118. }
  1119. static int scan_async_group(struct soc_camera_host *ici,
  1120. struct v4l2_async_subdev **asd, unsigned int size)
  1121. {
  1122. struct soc_camera_async_subdev *sasd;
  1123. struct soc_camera_async_client *sasc;
  1124. struct soc_camera_device *icd;
  1125. struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
  1126. char clk_name[V4L2_CLK_NAME_SIZE];
  1127. unsigned int i;
  1128. int ret;
  1129. /* First look for a sensor */
  1130. for (i = 0; i < size; i++) {
  1131. sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
  1132. if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
  1133. break;
  1134. }
  1135. if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
  1136. /* All useless */
  1137. dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
  1138. return -ENODEV;
  1139. }
  1140. /* Or shall this be managed by the soc-camera device? */
  1141. sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
  1142. if (!sasc)
  1143. return -ENOMEM;
  1144. /* HACK: just need a != NULL */
  1145. sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
  1146. ret = soc_camera_dyn_pdev(&sdesc, sasc);
  1147. if (ret < 0)
  1148. goto eallocpdev;
  1149. sasc->sensor = &sasd->asd;
  1150. icd = soc_camera_add_pdev(sasc);
  1151. if (!icd) {
  1152. ret = -ENOMEM;
  1153. goto eaddpdev;
  1154. }
  1155. sasc->notifier.subdevs = asd;
  1156. sasc->notifier.num_subdevs = size;
  1157. sasc->notifier.bound = soc_camera_async_bound;
  1158. sasc->notifier.unbind = soc_camera_async_unbind;
  1159. sasc->notifier.complete = soc_camera_async_complete;
  1160. icd->sasc = sasc;
  1161. icd->parent = ici->v4l2_dev.dev;
  1162. v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
  1163. sasd->asd.match.i2c.adapter_id,
  1164. sasd->asd.match.i2c.address);
  1165. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1166. if (IS_ERR(icd->clk)) {
  1167. ret = PTR_ERR(icd->clk);
  1168. goto eclkreg;
  1169. }
  1170. ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
  1171. if (!ret)
  1172. return 0;
  1173. v4l2_clk_unregister(icd->clk);
  1174. eclkreg:
  1175. icd->clk = NULL;
  1176. platform_device_del(sasc->pdev);
  1177. eaddpdev:
  1178. platform_device_put(sasc->pdev);
  1179. eallocpdev:
  1180. devm_kfree(ici->v4l2_dev.dev, sasc);
  1181. dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
  1182. return ret;
  1183. }
  1184. static void scan_async_host(struct soc_camera_host *ici)
  1185. {
  1186. struct v4l2_async_subdev **asd;
  1187. int j;
  1188. for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
  1189. scan_async_group(ici, asd, ici->asd_sizes[j]);
  1190. asd += ici->asd_sizes[j];
  1191. }
  1192. }
  1193. #else
  1194. #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
  1195. #define soc_camera_i2c_free(icd) do {} while (0)
  1196. #define scan_async_host(ici) do {} while (0)
  1197. #endif
  1198. #ifdef CONFIG_OF
  1199. struct soc_of_info {
  1200. struct soc_camera_async_subdev sasd;
  1201. struct soc_camera_async_client sasc;
  1202. struct v4l2_async_subdev *subdev;
  1203. };
  1204. static int soc_of_bind(struct soc_camera_host *ici,
  1205. struct device_node *ep,
  1206. struct device_node *remote)
  1207. {
  1208. struct soc_camera_device *icd;
  1209. struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
  1210. struct soc_camera_async_client *sasc;
  1211. struct soc_of_info *info;
  1212. struct i2c_client *client;
  1213. char clk_name[V4L2_CLK_NAME_SIZE];
  1214. int ret;
  1215. /* allocate a new subdev and add match info to it */
  1216. info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
  1217. GFP_KERNEL);
  1218. if (!info)
  1219. return -ENOMEM;
  1220. info->sasd.asd.match.of.node = remote;
  1221. info->sasd.asd.match_type = V4L2_ASYNC_MATCH_OF;
  1222. info->subdev = &info->sasd.asd;
  1223. /* Or shall this be managed by the soc-camera device? */
  1224. sasc = &info->sasc;
  1225. /* HACK: just need a != NULL */
  1226. sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
  1227. ret = soc_camera_dyn_pdev(&sdesc, sasc);
  1228. if (ret < 0)
  1229. goto eallocpdev;
  1230. sasc->sensor = &info->sasd.asd;
  1231. icd = soc_camera_add_pdev(sasc);
  1232. if (!icd) {
  1233. ret = -ENOMEM;
  1234. goto eaddpdev;
  1235. }
  1236. sasc->notifier.subdevs = &info->subdev;
  1237. sasc->notifier.num_subdevs = 1;
  1238. sasc->notifier.bound = soc_camera_async_bound;
  1239. sasc->notifier.unbind = soc_camera_async_unbind;
  1240. sasc->notifier.complete = soc_camera_async_complete;
  1241. icd->sasc = sasc;
  1242. icd->parent = ici->v4l2_dev.dev;
  1243. client = of_find_i2c_device_by_node(remote);
  1244. if (client)
  1245. v4l2_clk_name_i2c(clk_name, sizeof(clk_name),
  1246. client->adapter->nr, client->addr);
  1247. else
  1248. v4l2_clk_name_of(clk_name, sizeof(clk_name),
  1249. of_node_full_name(remote));
  1250. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1251. if (IS_ERR(icd->clk)) {
  1252. ret = PTR_ERR(icd->clk);
  1253. goto eclkreg;
  1254. }
  1255. ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
  1256. if (!ret)
  1257. return 0;
  1258. v4l2_clk_unregister(icd->clk);
  1259. eclkreg:
  1260. icd->clk = NULL;
  1261. platform_device_del(sasc->pdev);
  1262. eaddpdev:
  1263. platform_device_put(sasc->pdev);
  1264. eallocpdev:
  1265. devm_kfree(ici->v4l2_dev.dev, info);
  1266. dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
  1267. return ret;
  1268. }
  1269. static void scan_of_host(struct soc_camera_host *ici)
  1270. {
  1271. struct device *dev = ici->v4l2_dev.dev;
  1272. struct device_node *np = dev->of_node;
  1273. struct device_node *epn = NULL, *ren;
  1274. unsigned int i;
  1275. for (i = 0; ; i++) {
  1276. epn = of_graph_get_next_endpoint(np, epn);
  1277. if (!epn)
  1278. break;
  1279. ren = of_graph_get_remote_port(epn);
  1280. if (!ren) {
  1281. dev_notice(dev, "no remote for %s\n",
  1282. of_node_full_name(epn));
  1283. continue;
  1284. }
  1285. /* so we now have a remote node to connect */
  1286. if (!i)
  1287. soc_of_bind(ici, epn, ren->parent);
  1288. of_node_put(ren);
  1289. if (i) {
  1290. dev_err(dev, "multiple subdevices aren't supported yet!\n");
  1291. break;
  1292. }
  1293. }
  1294. of_node_put(epn);
  1295. }
  1296. #else
  1297. static inline void scan_of_host(struct soc_camera_host *ici) { }
  1298. #endif
  1299. /* Called during host-driver probe */
  1300. static int soc_camera_probe(struct soc_camera_host *ici,
  1301. struct soc_camera_device *icd)
  1302. {
  1303. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1304. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  1305. struct device *control = NULL;
  1306. int ret;
  1307. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  1308. /*
  1309. * Currently the subdev with the largest number of controls (13) is
  1310. * ov6550. So let's pick 16 as a hint for the control handler. Note
  1311. * that this is a hint only: too large and you waste some memory, too
  1312. * small and there is a (very) small performance hit when looking up
  1313. * controls in the internal hash.
  1314. */
  1315. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  1316. if (ret < 0)
  1317. return ret;
  1318. /* Must have icd->vdev before registering the device */
  1319. ret = video_dev_create(icd);
  1320. if (ret < 0)
  1321. goto evdc;
  1322. /*
  1323. * ..._video_start() will create a device node, video_register_device()
  1324. * itself is protected against concurrent open() calls, but we also have
  1325. * to protect our data also during client probing.
  1326. */
  1327. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  1328. if (shd->board_info) {
  1329. ret = soc_camera_i2c_init(icd, sdesc);
  1330. if (ret < 0 && ret != -EPROBE_DEFER)
  1331. goto eadd;
  1332. } else if (!shd->add_device || !shd->del_device) {
  1333. ret = -EINVAL;
  1334. goto eadd;
  1335. } else {
  1336. ret = soc_camera_clock_start(ici);
  1337. if (ret < 0)
  1338. goto eadd;
  1339. if (shd->module_name)
  1340. ret = request_module(shd->module_name);
  1341. ret = shd->add_device(icd);
  1342. if (ret < 0)
  1343. goto eadddev;
  1344. /*
  1345. * FIXME: this is racy, have to use driver-binding notification,
  1346. * when it is available
  1347. */
  1348. control = to_soc_camera_control(icd);
  1349. if (!control || !control->driver || !dev_get_drvdata(control) ||
  1350. !try_module_get(control->driver->owner)) {
  1351. shd->del_device(icd);
  1352. ret = -ENODEV;
  1353. goto enodrv;
  1354. }
  1355. }
  1356. mutex_lock(&ici->host_lock);
  1357. ret = soc_camera_probe_finish(icd);
  1358. mutex_unlock(&ici->host_lock);
  1359. if (ret < 0)
  1360. goto efinish;
  1361. return 0;
  1362. efinish:
  1363. if (shd->board_info) {
  1364. soc_camera_i2c_free(icd);
  1365. } else {
  1366. shd->del_device(icd);
  1367. module_put(control->driver->owner);
  1368. enodrv:
  1369. eadddev:
  1370. soc_camera_clock_stop(ici);
  1371. }
  1372. eadd:
  1373. if (icd->vdev) {
  1374. video_device_release(icd->vdev);
  1375. icd->vdev = NULL;
  1376. }
  1377. evdc:
  1378. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1379. return ret;
  1380. }
  1381. /*
  1382. * This is called on device_unregister, which only means we have to disconnect
  1383. * from the host, but not remove ourselves from the device list. With
  1384. * asynchronous client probing this can also be called without
  1385. * soc_camera_probe_finish() having run. Careful with clean up.
  1386. */
  1387. static int soc_camera_remove(struct soc_camera_device *icd)
  1388. {
  1389. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1390. struct video_device *vdev = icd->vdev;
  1391. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1392. if (vdev) {
  1393. video_unregister_device(vdev);
  1394. icd->vdev = NULL;
  1395. }
  1396. if (sdesc->host_desc.board_info) {
  1397. soc_camera_i2c_free(icd);
  1398. } else {
  1399. struct device *dev = to_soc_camera_control(icd);
  1400. struct device_driver *drv = dev ? dev->driver : NULL;
  1401. if (drv) {
  1402. sdesc->host_desc.del_device(icd);
  1403. module_put(drv->owner);
  1404. }
  1405. }
  1406. if (icd->num_user_formats)
  1407. soc_camera_free_user_formats(icd);
  1408. if (icd->clk) {
  1409. /* For the synchronous case */
  1410. v4l2_clk_unregister(icd->clk);
  1411. icd->clk = NULL;
  1412. }
  1413. if (icd->sasc)
  1414. platform_device_unregister(icd->sasc->pdev);
  1415. return 0;
  1416. }
  1417. static int default_g_selection(struct soc_camera_device *icd,
  1418. struct v4l2_selection *sel)
  1419. {
  1420. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1421. struct v4l2_subdev_selection sdsel = {
  1422. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1423. .target = sel->target,
  1424. };
  1425. int ret;
  1426. ret = v4l2_subdev_call(sd, pad, get_selection, NULL, &sdsel);
  1427. if (ret)
  1428. return ret;
  1429. sel->r = sdsel.r;
  1430. return 0;
  1431. }
  1432. static int default_s_selection(struct soc_camera_device *icd,
  1433. struct v4l2_selection *sel)
  1434. {
  1435. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1436. struct v4l2_subdev_selection sdsel = {
  1437. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1438. .target = sel->target,
  1439. .flags = sel->flags,
  1440. .r = sel->r,
  1441. };
  1442. int ret;
  1443. ret = v4l2_subdev_call(sd, pad, set_selection, NULL, &sdsel);
  1444. if (ret)
  1445. return ret;
  1446. sel->r = sdsel.r;
  1447. return 0;
  1448. }
  1449. static int default_g_parm(struct soc_camera_device *icd,
  1450. struct v4l2_streamparm *parm)
  1451. {
  1452. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1453. return v4l2_subdev_call(sd, video, g_parm, parm);
  1454. }
  1455. static int default_s_parm(struct soc_camera_device *icd,
  1456. struct v4l2_streamparm *parm)
  1457. {
  1458. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1459. return v4l2_subdev_call(sd, video, s_parm, parm);
  1460. }
  1461. static int default_enum_framesizes(struct soc_camera_device *icd,
  1462. struct v4l2_frmsizeenum *fsize)
  1463. {
  1464. int ret;
  1465. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1466. const struct soc_camera_format_xlate *xlate;
  1467. struct v4l2_subdev_frame_size_enum fse = {
  1468. .index = fsize->index,
  1469. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1470. };
  1471. xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
  1472. if (!xlate)
  1473. return -EINVAL;
  1474. fse.code = xlate->code;
  1475. ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
  1476. if (ret < 0)
  1477. return ret;
  1478. if (fse.min_width == fse.max_width &&
  1479. fse.min_height == fse.max_height) {
  1480. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1481. fsize->discrete.width = fse.min_width;
  1482. fsize->discrete.height = fse.min_height;
  1483. return 0;
  1484. }
  1485. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  1486. fsize->stepwise.min_width = fse.min_width;
  1487. fsize->stepwise.max_width = fse.max_width;
  1488. fsize->stepwise.min_height = fse.min_height;
  1489. fsize->stepwise.max_height = fse.max_height;
  1490. fsize->stepwise.step_width = 1;
  1491. fsize->stepwise.step_height = 1;
  1492. return 0;
  1493. }
  1494. int soc_camera_host_register(struct soc_camera_host *ici)
  1495. {
  1496. struct soc_camera_host *ix;
  1497. int ret;
  1498. if (!ici || !ici->ops ||
  1499. !ici->ops->try_fmt ||
  1500. !ici->ops->set_fmt ||
  1501. !ici->ops->set_bus_param ||
  1502. !ici->ops->querycap ||
  1503. !ici->ops->init_videobuf2 ||
  1504. !ici->ops->poll ||
  1505. !ici->v4l2_dev.dev)
  1506. return -EINVAL;
  1507. if (!ici->ops->set_selection)
  1508. ici->ops->set_selection = default_s_selection;
  1509. if (!ici->ops->get_selection)
  1510. ici->ops->get_selection = default_g_selection;
  1511. if (!ici->ops->set_parm)
  1512. ici->ops->set_parm = default_s_parm;
  1513. if (!ici->ops->get_parm)
  1514. ici->ops->get_parm = default_g_parm;
  1515. if (!ici->ops->enum_framesizes)
  1516. ici->ops->enum_framesizes = default_enum_framesizes;
  1517. mutex_lock(&list_lock);
  1518. list_for_each_entry(ix, &hosts, list) {
  1519. if (ix->nr == ici->nr) {
  1520. ret = -EBUSY;
  1521. goto edevreg;
  1522. }
  1523. }
  1524. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1525. if (ret < 0)
  1526. goto edevreg;
  1527. list_add_tail(&ici->list, &hosts);
  1528. mutex_unlock(&list_lock);
  1529. mutex_init(&ici->host_lock);
  1530. mutex_init(&ici->clk_lock);
  1531. if (ici->v4l2_dev.dev->of_node)
  1532. scan_of_host(ici);
  1533. else if (ici->asd_sizes)
  1534. /*
  1535. * No OF, host with a list of subdevices. Don't try to mix
  1536. * modes by initialising some groups statically and some
  1537. * dynamically!
  1538. */
  1539. scan_async_host(ici);
  1540. else
  1541. /* Legacy: static platform devices from board data */
  1542. scan_add_host(ici);
  1543. return 0;
  1544. edevreg:
  1545. mutex_unlock(&list_lock);
  1546. return ret;
  1547. }
  1548. EXPORT_SYMBOL(soc_camera_host_register);
  1549. /* Unregister all clients! */
  1550. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1551. {
  1552. struct soc_camera_device *icd, *tmp;
  1553. struct soc_camera_async_client *sasc;
  1554. LIST_HEAD(notifiers);
  1555. mutex_lock(&list_lock);
  1556. list_del(&ici->list);
  1557. list_for_each_entry(icd, &devices, list)
  1558. if (icd->iface == ici->nr && icd->sasc) {
  1559. /* as long as we hold the device, sasc won't be freed */
  1560. get_device(icd->pdev);
  1561. list_add(&icd->sasc->list, &notifiers);
  1562. }
  1563. mutex_unlock(&list_lock);
  1564. list_for_each_entry(sasc, &notifiers, list) {
  1565. /* Must call unlocked to avoid AB-BA dead-lock */
  1566. v4l2_async_notifier_unregister(&sasc->notifier);
  1567. put_device(&sasc->pdev->dev);
  1568. }
  1569. mutex_lock(&list_lock);
  1570. list_for_each_entry_safe(icd, tmp, &devices, list)
  1571. if (icd->iface == ici->nr)
  1572. soc_camera_remove(icd);
  1573. mutex_unlock(&list_lock);
  1574. v4l2_device_unregister(&ici->v4l2_dev);
  1575. }
  1576. EXPORT_SYMBOL(soc_camera_host_unregister);
  1577. /* Image capture device */
  1578. static int soc_camera_device_register(struct soc_camera_device *icd)
  1579. {
  1580. struct soc_camera_device *ix;
  1581. int num = -1, i;
  1582. mutex_lock(&list_lock);
  1583. for (i = 0; i < 256 && num < 0; i++) {
  1584. num = i;
  1585. /* Check if this index is available on this interface */
  1586. list_for_each_entry(ix, &devices, list) {
  1587. if (ix->iface == icd->iface && ix->devnum == i) {
  1588. num = -1;
  1589. break;
  1590. }
  1591. }
  1592. }
  1593. if (num < 0) {
  1594. /*
  1595. * ok, we have 256 cameras on this host...
  1596. * man, stay reasonable...
  1597. */
  1598. mutex_unlock(&list_lock);
  1599. return -ENOMEM;
  1600. }
  1601. icd->devnum = num;
  1602. icd->use_count = 0;
  1603. icd->host_priv = NULL;
  1604. /*
  1605. * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
  1606. * it again
  1607. */
  1608. i = to_platform_device(icd->pdev)->id;
  1609. if (i < 0)
  1610. /* One static (legacy) soc-camera platform device */
  1611. i = 0;
  1612. if (i >= MAP_MAX_NUM) {
  1613. mutex_unlock(&list_lock);
  1614. return -EBUSY;
  1615. }
  1616. set_bit(i, device_map);
  1617. list_add_tail(&icd->list, &devices);
  1618. mutex_unlock(&list_lock);
  1619. return 0;
  1620. }
  1621. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1622. .vidioc_querycap = soc_camera_querycap,
  1623. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1624. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1625. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1626. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1627. .vidioc_enum_input = soc_camera_enum_input,
  1628. .vidioc_g_input = soc_camera_g_input,
  1629. .vidioc_s_input = soc_camera_s_input,
  1630. .vidioc_s_std = soc_camera_s_std,
  1631. .vidioc_g_std = soc_camera_g_std,
  1632. .vidioc_enum_framesizes = soc_camera_enum_framesizes,
  1633. .vidioc_reqbufs = soc_camera_reqbufs,
  1634. .vidioc_querybuf = soc_camera_querybuf,
  1635. .vidioc_qbuf = soc_camera_qbuf,
  1636. .vidioc_dqbuf = soc_camera_dqbuf,
  1637. .vidioc_create_bufs = soc_camera_create_bufs,
  1638. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1639. .vidioc_expbuf = soc_camera_expbuf,
  1640. .vidioc_streamon = soc_camera_streamon,
  1641. .vidioc_streamoff = soc_camera_streamoff,
  1642. .vidioc_g_selection = soc_camera_g_selection,
  1643. .vidioc_s_selection = soc_camera_s_selection,
  1644. .vidioc_g_parm = soc_camera_g_parm,
  1645. .vidioc_s_parm = soc_camera_s_parm,
  1646. };
  1647. static int video_dev_create(struct soc_camera_device *icd)
  1648. {
  1649. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1650. struct video_device *vdev = video_device_alloc();
  1651. if (!vdev)
  1652. return -ENOMEM;
  1653. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1654. vdev->v4l2_dev = &ici->v4l2_dev;
  1655. vdev->fops = &soc_camera_fops;
  1656. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1657. vdev->release = video_device_release;
  1658. vdev->ctrl_handler = &icd->ctrl_handler;
  1659. vdev->lock = &ici->host_lock;
  1660. icd->vdev = vdev;
  1661. return 0;
  1662. }
  1663. /*
  1664. * Called from soc_camera_probe() above with .host_lock held
  1665. */
  1666. static int soc_camera_video_start(struct soc_camera_device *icd)
  1667. {
  1668. const struct device_type *type = icd->vdev->dev.type;
  1669. int ret;
  1670. if (!icd->parent)
  1671. return -ENODEV;
  1672. video_set_drvdata(icd->vdev, icd);
  1673. if (icd->vdev->tvnorms == 0) {
  1674. /* disable the STD API if there are no tvnorms defined */
  1675. v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
  1676. v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
  1677. v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
  1678. }
  1679. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1680. if (ret < 0) {
  1681. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1682. return ret;
  1683. }
  1684. /* Restore device type, possibly set by the subdevice driver */
  1685. icd->vdev->dev.type = type;
  1686. return 0;
  1687. }
  1688. static int soc_camera_pdrv_probe(struct platform_device *pdev)
  1689. {
  1690. struct soc_camera_desc *sdesc = pdev->dev.platform_data;
  1691. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  1692. struct soc_camera_device *icd;
  1693. int ret;
  1694. if (!sdesc)
  1695. return -EINVAL;
  1696. icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
  1697. if (!icd)
  1698. return -ENOMEM;
  1699. /*
  1700. * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
  1701. * regulator allocation is a dummy. They are actually requested by the
  1702. * subdevice driver, using soc_camera_power_init(). Also note, that in
  1703. * that case regulators are attached to the I2C device and not to the
  1704. * camera platform device.
  1705. */
  1706. ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
  1707. ssdd->sd_pdata.regulators);
  1708. if (ret < 0)
  1709. return ret;
  1710. icd->iface = sdesc->host_desc.bus_id;
  1711. icd->sdesc = sdesc;
  1712. icd->pdev = &pdev->dev;
  1713. platform_set_drvdata(pdev, icd);
  1714. icd->user_width = DEFAULT_WIDTH;
  1715. icd->user_height = DEFAULT_HEIGHT;
  1716. return soc_camera_device_register(icd);
  1717. }
  1718. /*
  1719. * Only called on rmmod for each platform device, since they are not
  1720. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1721. * been unloaded already
  1722. */
  1723. static int soc_camera_pdrv_remove(struct platform_device *pdev)
  1724. {
  1725. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1726. int i;
  1727. if (!icd)
  1728. return -EINVAL;
  1729. i = pdev->id;
  1730. if (i < 0)
  1731. i = 0;
  1732. /*
  1733. * In synchronous mode with static platform devices this is called in a
  1734. * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
  1735. * no need to lock. In asynchronous case the caller -
  1736. * soc_camera_host_unregister() - already holds the lock
  1737. */
  1738. if (test_bit(i, device_map)) {
  1739. clear_bit(i, device_map);
  1740. list_del(&icd->list);
  1741. }
  1742. return 0;
  1743. }
  1744. static struct platform_driver __refdata soc_camera_pdrv = {
  1745. .probe = soc_camera_pdrv_probe,
  1746. .remove = soc_camera_pdrv_remove,
  1747. .driver = {
  1748. .name = "soc-camera-pdrv",
  1749. },
  1750. };
  1751. module_platform_driver(soc_camera_pdrv);
  1752. MODULE_DESCRIPTION("Image capture bus driver");
  1753. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1754. MODULE_LICENSE("GPL");
  1755. MODULE_ALIAS("platform:soc-camera-pdrv");