soc_camera.c 52 KB

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