soc_camera.c 52 KB

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