soc_camera.c 53 KB

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