soc_camera.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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/mutex.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/vmalloc.h>
  30. #include <media/soc_camera.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-dev.h>
  34. #include <media/videobuf-core.h>
  35. #include <media/videobuf2-core.h>
  36. #include <media/soc_mediabus.h>
  37. /* Default to VGA resolution */
  38. #define DEFAULT_WIDTH 640
  39. #define DEFAULT_HEIGHT 480
  40. #define is_streaming(ici, icd) \
  41. (((ici)->ops->init_videobuf) ? \
  42. (icd)->vb_vidq.streaming : \
  43. vb2_is_streaming(&(icd)->vb2_vidq))
  44. static LIST_HEAD(hosts);
  45. static LIST_HEAD(devices);
  46. static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
  47. static int soc_camera_power_on(struct soc_camera_device *icd,
  48. struct soc_camera_link *icl)
  49. {
  50. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  51. int ret = regulator_bulk_enable(icl->num_regulators,
  52. icl->regulators);
  53. if (ret < 0) {
  54. dev_err(icd->pdev, "Cannot enable regulators\n");
  55. return ret;
  56. }
  57. if (icl->power) {
  58. ret = icl->power(icd->pdev, 1);
  59. if (ret < 0) {
  60. dev_err(icd->pdev,
  61. "Platform failed to power-on the camera.\n");
  62. goto elinkpwr;
  63. }
  64. }
  65. ret = v4l2_subdev_call(sd, core, s_power, 1);
  66. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  67. goto esdpwr;
  68. return 0;
  69. esdpwr:
  70. if (icl->power)
  71. icl->power(icd->pdev, 0);
  72. elinkpwr:
  73. regulator_bulk_disable(icl->num_regulators,
  74. icl->regulators);
  75. return ret;
  76. }
  77. static int soc_camera_power_off(struct soc_camera_device *icd,
  78. struct soc_camera_link *icl)
  79. {
  80. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  81. int ret = v4l2_subdev_call(sd, core, s_power, 0);
  82. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  83. return ret;
  84. if (icl->power) {
  85. ret = icl->power(icd->pdev, 0);
  86. if (ret < 0) {
  87. dev_err(icd->pdev,
  88. "Platform failed to power-off the camera.\n");
  89. return ret;
  90. }
  91. }
  92. ret = regulator_bulk_disable(icl->num_regulators,
  93. icl->regulators);
  94. if (ret < 0)
  95. dev_err(icd->pdev, "Cannot disable regulators\n");
  96. return ret;
  97. }
  98. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  99. struct soc_camera_device *icd, unsigned int fourcc)
  100. {
  101. unsigned int i;
  102. for (i = 0; i < icd->num_user_formats; i++)
  103. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  104. return icd->user_formats + i;
  105. return NULL;
  106. }
  107. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  108. /**
  109. * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  110. * @icl: camera platform parameters
  111. * @cfg: media bus configuration
  112. * @return: resulting flags
  113. */
  114. unsigned long soc_camera_apply_board_flags(struct soc_camera_link *icl,
  115. const struct v4l2_mbus_config *cfg)
  116. {
  117. unsigned long f, flags = cfg->flags;
  118. /* If only one of the two polarities is supported, switch to the opposite */
  119. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  120. f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
  121. if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
  122. flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
  123. }
  124. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  125. f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
  126. if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
  127. flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
  128. }
  129. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  130. f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
  131. if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
  132. flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  133. }
  134. return flags;
  135. }
  136. EXPORT_SYMBOL(soc_camera_apply_board_flags);
  137. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  138. ((x) >> 24) & 0xff
  139. static int soc_camera_try_fmt(struct soc_camera_device *icd,
  140. struct v4l2_format *f)
  141. {
  142. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  143. struct v4l2_pix_format *pix = &f->fmt.pix;
  144. int ret;
  145. dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
  146. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  147. pix->bytesperline = 0;
  148. pix->sizeimage = 0;
  149. ret = ici->ops->try_fmt(icd, f);
  150. if (ret < 0)
  151. return ret;
  152. if (!pix->sizeimage) {
  153. if (!pix->bytesperline) {
  154. const struct soc_camera_format_xlate *xlate;
  155. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  156. if (!xlate)
  157. return -EINVAL;
  158. ret = soc_mbus_bytes_per_line(pix->width,
  159. xlate->host_fmt);
  160. if (ret > 0)
  161. pix->bytesperline = ret;
  162. }
  163. if (pix->bytesperline)
  164. pix->sizeimage = pix->bytesperline * pix->height;
  165. }
  166. return 0;
  167. }
  168. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  169. struct v4l2_format *f)
  170. {
  171. struct soc_camera_device *icd = file->private_data;
  172. WARN_ON(priv != file->private_data);
  173. /* Only single-plane capture is supported so far */
  174. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  175. return -EINVAL;
  176. /* limit format to hardware capabilities */
  177. return soc_camera_try_fmt(icd, f);
  178. }
  179. static int soc_camera_enum_input(struct file *file, void *priv,
  180. struct v4l2_input *inp)
  181. {
  182. if (inp->index != 0)
  183. return -EINVAL;
  184. /* default is camera */
  185. inp->type = V4L2_INPUT_TYPE_CAMERA;
  186. inp->std = V4L2_STD_UNKNOWN;
  187. strcpy(inp->name, "Camera");
  188. return 0;
  189. }
  190. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  191. {
  192. *i = 0;
  193. return 0;
  194. }
  195. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  196. {
  197. if (i > 0)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  202. {
  203. struct soc_camera_device *icd = file->private_data;
  204. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  205. return v4l2_subdev_call(sd, core, s_std, *a);
  206. }
  207. static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
  208. {
  209. struct soc_camera_device *icd = file->private_data;
  210. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  211. return v4l2_subdev_call(sd, core, g_std, a);
  212. }
  213. static int soc_camera_enum_fsizes(struct file *file, void *fh,
  214. struct v4l2_frmsizeenum *fsize)
  215. {
  216. struct soc_camera_device *icd = file->private_data;
  217. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  218. return ici->ops->enum_fsizes(icd, fsize);
  219. }
  220. static int soc_camera_reqbufs(struct file *file, void *priv,
  221. struct v4l2_requestbuffers *p)
  222. {
  223. int ret;
  224. struct soc_camera_device *icd = file->private_data;
  225. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  226. WARN_ON(priv != file->private_data);
  227. if (icd->streamer && icd->streamer != file)
  228. return -EBUSY;
  229. if (ici->ops->init_videobuf) {
  230. ret = videobuf_reqbufs(&icd->vb_vidq, p);
  231. if (ret < 0)
  232. return ret;
  233. ret = ici->ops->reqbufs(icd, p);
  234. } else {
  235. ret = vb2_reqbufs(&icd->vb2_vidq, p);
  236. }
  237. if (!ret && !icd->streamer)
  238. icd->streamer = file;
  239. return ret;
  240. }
  241. static int soc_camera_querybuf(struct file *file, void *priv,
  242. struct v4l2_buffer *p)
  243. {
  244. struct soc_camera_device *icd = file->private_data;
  245. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  246. WARN_ON(priv != file->private_data);
  247. if (ici->ops->init_videobuf)
  248. return videobuf_querybuf(&icd->vb_vidq, p);
  249. else
  250. return vb2_querybuf(&icd->vb2_vidq, p);
  251. }
  252. static int soc_camera_qbuf(struct file *file, void *priv,
  253. struct v4l2_buffer *p)
  254. {
  255. struct soc_camera_device *icd = file->private_data;
  256. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  257. WARN_ON(priv != file->private_data);
  258. if (icd->streamer != file)
  259. return -EBUSY;
  260. if (ici->ops->init_videobuf)
  261. return videobuf_qbuf(&icd->vb_vidq, p);
  262. else
  263. return vb2_qbuf(&icd->vb2_vidq, p);
  264. }
  265. static int soc_camera_dqbuf(struct file *file, void *priv,
  266. struct v4l2_buffer *p)
  267. {
  268. struct soc_camera_device *icd = file->private_data;
  269. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  270. WARN_ON(priv != file->private_data);
  271. if (icd->streamer != file)
  272. return -EBUSY;
  273. if (ici->ops->init_videobuf)
  274. return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
  275. else
  276. return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
  277. }
  278. static int soc_camera_create_bufs(struct file *file, void *priv,
  279. struct v4l2_create_buffers *create)
  280. {
  281. struct soc_camera_device *icd = file->private_data;
  282. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  283. /* videobuf2 only */
  284. if (ici->ops->init_videobuf)
  285. return -EINVAL;
  286. else
  287. return vb2_create_bufs(&icd->vb2_vidq, create);
  288. }
  289. static int soc_camera_prepare_buf(struct file *file, void *priv,
  290. struct v4l2_buffer *b)
  291. {
  292. struct soc_camera_device *icd = file->private_data;
  293. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  294. /* videobuf2 only */
  295. if (ici->ops->init_videobuf)
  296. return -EINVAL;
  297. else
  298. return vb2_prepare_buf(&icd->vb2_vidq, b);
  299. }
  300. /* Always entered with .video_lock held */
  301. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  302. {
  303. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  304. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  305. unsigned int i, fmts = 0, raw_fmts = 0;
  306. int ret;
  307. enum v4l2_mbus_pixelcode code;
  308. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  309. raw_fmts++;
  310. if (!ici->ops->get_formats)
  311. /*
  312. * Fallback mode - the host will have to serve all
  313. * sensor-provided formats one-to-one to the user
  314. */
  315. fmts = raw_fmts;
  316. else
  317. /*
  318. * First pass - only count formats this host-sensor
  319. * configuration can provide
  320. */
  321. for (i = 0; i < raw_fmts; i++) {
  322. ret = ici->ops->get_formats(icd, i, NULL);
  323. if (ret < 0)
  324. return ret;
  325. fmts += ret;
  326. }
  327. if (!fmts)
  328. return -ENXIO;
  329. icd->user_formats =
  330. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  331. if (!icd->user_formats)
  332. return -ENOMEM;
  333. dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
  334. /* Second pass - actually fill data formats */
  335. fmts = 0;
  336. for (i = 0; i < raw_fmts; i++)
  337. if (!ici->ops->get_formats) {
  338. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  339. icd->user_formats[fmts].host_fmt =
  340. soc_mbus_get_fmtdesc(code);
  341. if (icd->user_formats[fmts].host_fmt)
  342. icd->user_formats[fmts++].code = code;
  343. } else {
  344. ret = ici->ops->get_formats(icd, i,
  345. &icd->user_formats[fmts]);
  346. if (ret < 0)
  347. goto egfmt;
  348. fmts += ret;
  349. }
  350. icd->num_user_formats = fmts;
  351. icd->current_fmt = &icd->user_formats[0];
  352. return 0;
  353. egfmt:
  354. vfree(icd->user_formats);
  355. return ret;
  356. }
  357. /* Always entered with .video_lock held */
  358. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  359. {
  360. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  361. if (ici->ops->put_formats)
  362. ici->ops->put_formats(icd);
  363. icd->current_fmt = NULL;
  364. icd->num_user_formats = 0;
  365. vfree(icd->user_formats);
  366. icd->user_formats = NULL;
  367. }
  368. /* Called with .vb_lock held, or from the first open(2), see comment there */
  369. static int soc_camera_set_fmt(struct soc_camera_device *icd,
  370. struct v4l2_format *f)
  371. {
  372. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  373. struct v4l2_pix_format *pix = &f->fmt.pix;
  374. int ret;
  375. dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
  376. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  377. /* We always call try_fmt() before set_fmt() or set_crop() */
  378. ret = soc_camera_try_fmt(icd, f);
  379. if (ret < 0)
  380. return ret;
  381. ret = ici->ops->set_fmt(icd, f);
  382. if (ret < 0) {
  383. return ret;
  384. } else if (!icd->current_fmt ||
  385. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  386. dev_err(icd->pdev,
  387. "Host driver hasn't set up current format correctly!\n");
  388. return -EINVAL;
  389. }
  390. icd->user_width = pix->width;
  391. icd->user_height = pix->height;
  392. icd->bytesperline = pix->bytesperline;
  393. icd->sizeimage = pix->sizeimage;
  394. icd->colorspace = pix->colorspace;
  395. icd->field = pix->field;
  396. if (ici->ops->init_videobuf)
  397. icd->vb_vidq.field = pix->field;
  398. dev_dbg(icd->pdev, "set width: %d height: %d\n",
  399. icd->user_width, icd->user_height);
  400. /* set physical bus parameters */
  401. return ici->ops->set_bus_param(icd);
  402. }
  403. static int soc_camera_open(struct file *file)
  404. {
  405. struct video_device *vdev = video_devdata(file);
  406. struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
  407. struct soc_camera_link *icl = to_soc_camera_link(icd);
  408. struct soc_camera_host *ici;
  409. int ret;
  410. if (!to_soc_camera_control(icd))
  411. /* No device driver attached */
  412. return -ENODEV;
  413. ici = to_soc_camera_host(icd->parent);
  414. if (!try_module_get(ici->ops->owner)) {
  415. dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
  416. return -EINVAL;
  417. }
  418. icd->use_count++;
  419. /* Now we really have to activate the camera */
  420. if (icd->use_count == 1) {
  421. /* Restore parameters before the last close() per V4L2 API */
  422. struct v4l2_format f = {
  423. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  424. .fmt.pix = {
  425. .width = icd->user_width,
  426. .height = icd->user_height,
  427. .field = icd->field,
  428. .colorspace = icd->colorspace,
  429. .pixelformat =
  430. icd->current_fmt->host_fmt->fourcc,
  431. },
  432. };
  433. /* The camera could have been already on, try to reset */
  434. if (icl->reset)
  435. icl->reset(icd->pdev);
  436. /* Don't mess with the host during probe */
  437. mutex_lock(&ici->host_lock);
  438. ret = ici->ops->add(icd);
  439. mutex_unlock(&ici->host_lock);
  440. if (ret < 0) {
  441. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  442. goto eiciadd;
  443. }
  444. ret = soc_camera_power_on(icd, icl);
  445. if (ret < 0)
  446. goto epower;
  447. pm_runtime_enable(&icd->vdev->dev);
  448. ret = pm_runtime_resume(&icd->vdev->dev);
  449. if (ret < 0 && ret != -ENOSYS)
  450. goto eresume;
  451. /*
  452. * Try to configure with default parameters. Notice: this is the
  453. * very first open, so, we cannot race against other calls,
  454. * apart from someone else calling open() simultaneously, but
  455. * .video_lock is protecting us against it.
  456. */
  457. ret = soc_camera_set_fmt(icd, &f);
  458. if (ret < 0)
  459. goto esfmt;
  460. if (ici->ops->init_videobuf) {
  461. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  462. } else {
  463. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  464. if (ret < 0)
  465. goto einitvb;
  466. }
  467. v4l2_ctrl_handler_setup(&icd->ctrl_handler);
  468. }
  469. file->private_data = icd;
  470. dev_dbg(icd->pdev, "camera device open\n");
  471. return 0;
  472. /*
  473. * First four errors are entered with the .video_lock held
  474. * and use_count == 1
  475. */
  476. einitvb:
  477. esfmt:
  478. pm_runtime_disable(&icd->vdev->dev);
  479. eresume:
  480. soc_camera_power_off(icd, icl);
  481. epower:
  482. ici->ops->remove(icd);
  483. eiciadd:
  484. icd->use_count--;
  485. module_put(ici->ops->owner);
  486. return ret;
  487. }
  488. static int soc_camera_close(struct file *file)
  489. {
  490. struct soc_camera_device *icd = file->private_data;
  491. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  492. icd->use_count--;
  493. if (!icd->use_count) {
  494. struct soc_camera_link *icl = to_soc_camera_link(icd);
  495. pm_runtime_suspend(&icd->vdev->dev);
  496. pm_runtime_disable(&icd->vdev->dev);
  497. if (ici->ops->init_videobuf2)
  498. vb2_queue_release(&icd->vb2_vidq);
  499. ici->ops->remove(icd);
  500. soc_camera_power_off(icd, icl);
  501. }
  502. if (icd->streamer == file)
  503. icd->streamer = NULL;
  504. module_put(ici->ops->owner);
  505. dev_dbg(icd->pdev, "camera device close\n");
  506. return 0;
  507. }
  508. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  509. size_t count, loff_t *ppos)
  510. {
  511. struct soc_camera_device *icd = file->private_data;
  512. int err = -EINVAL;
  513. dev_err(icd->pdev, "camera device read not implemented\n");
  514. return err;
  515. }
  516. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  517. {
  518. struct soc_camera_device *icd = file->private_data;
  519. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  520. int err;
  521. dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  522. if (icd->streamer != file)
  523. return -EBUSY;
  524. if (ici->ops->init_videobuf)
  525. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  526. else
  527. err = vb2_mmap(&icd->vb2_vidq, vma);
  528. dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  529. (unsigned long)vma->vm_start,
  530. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  531. err);
  532. return err;
  533. }
  534. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  535. {
  536. struct soc_camera_device *icd = file->private_data;
  537. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  538. if (icd->streamer != file)
  539. return -EBUSY;
  540. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
  541. dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
  542. return POLLERR;
  543. }
  544. return ici->ops->poll(file, pt);
  545. }
  546. void soc_camera_lock(struct vb2_queue *vq)
  547. {
  548. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  549. mutex_lock(&icd->video_lock);
  550. }
  551. EXPORT_SYMBOL(soc_camera_lock);
  552. void soc_camera_unlock(struct vb2_queue *vq)
  553. {
  554. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  555. mutex_unlock(&icd->video_lock);
  556. }
  557. EXPORT_SYMBOL(soc_camera_unlock);
  558. static struct v4l2_file_operations soc_camera_fops = {
  559. .owner = THIS_MODULE,
  560. .open = soc_camera_open,
  561. .release = soc_camera_close,
  562. .unlocked_ioctl = video_ioctl2,
  563. .read = soc_camera_read,
  564. .mmap = soc_camera_mmap,
  565. .poll = soc_camera_poll,
  566. };
  567. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  568. struct v4l2_format *f)
  569. {
  570. struct soc_camera_device *icd = file->private_data;
  571. int ret;
  572. WARN_ON(priv != file->private_data);
  573. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  574. dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
  575. return -EINVAL;
  576. }
  577. if (icd->streamer && icd->streamer != file)
  578. return -EBUSY;
  579. if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
  580. dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
  581. return -EBUSY;
  582. }
  583. ret = soc_camera_set_fmt(icd, f);
  584. if (!ret && !icd->streamer)
  585. icd->streamer = file;
  586. return ret;
  587. }
  588. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  589. struct v4l2_fmtdesc *f)
  590. {
  591. struct soc_camera_device *icd = file->private_data;
  592. const struct soc_mbus_pixelfmt *format;
  593. WARN_ON(priv != file->private_data);
  594. if (f->index >= icd->num_user_formats)
  595. return -EINVAL;
  596. format = icd->user_formats[f->index].host_fmt;
  597. if (format->name)
  598. strlcpy(f->description, format->name, sizeof(f->description));
  599. f->pixelformat = format->fourcc;
  600. return 0;
  601. }
  602. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  603. struct v4l2_format *f)
  604. {
  605. struct soc_camera_device *icd = file->private_data;
  606. struct v4l2_pix_format *pix = &f->fmt.pix;
  607. WARN_ON(priv != file->private_data);
  608. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  609. return -EINVAL;
  610. pix->width = icd->user_width;
  611. pix->height = icd->user_height;
  612. pix->bytesperline = icd->bytesperline;
  613. pix->sizeimage = icd->sizeimage;
  614. pix->field = icd->field;
  615. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  616. pix->colorspace = icd->colorspace;
  617. dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
  618. icd->current_fmt->host_fmt->fourcc);
  619. return 0;
  620. }
  621. static int soc_camera_querycap(struct file *file, void *priv,
  622. struct v4l2_capability *cap)
  623. {
  624. struct soc_camera_device *icd = file->private_data;
  625. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  626. WARN_ON(priv != file->private_data);
  627. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  628. return ici->ops->querycap(ici, cap);
  629. }
  630. static int soc_camera_streamon(struct file *file, void *priv,
  631. enum v4l2_buf_type i)
  632. {
  633. struct soc_camera_device *icd = file->private_data;
  634. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  635. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  636. int ret;
  637. WARN_ON(priv != file->private_data);
  638. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  639. return -EINVAL;
  640. if (icd->streamer != file)
  641. return -EBUSY;
  642. /* This calls buf_queue from host driver's videobuf_queue_ops */
  643. if (ici->ops->init_videobuf)
  644. ret = videobuf_streamon(&icd->vb_vidq);
  645. else
  646. ret = vb2_streamon(&icd->vb2_vidq, i);
  647. if (!ret)
  648. v4l2_subdev_call(sd, video, s_stream, 1);
  649. return ret;
  650. }
  651. static int soc_camera_streamoff(struct file *file, void *priv,
  652. enum v4l2_buf_type i)
  653. {
  654. struct soc_camera_device *icd = file->private_data;
  655. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  656. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  657. WARN_ON(priv != file->private_data);
  658. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  659. return -EINVAL;
  660. if (icd->streamer != file)
  661. return -EBUSY;
  662. /*
  663. * This calls buf_release from host driver's videobuf_queue_ops for all
  664. * remaining buffers. When the last buffer is freed, stop capture
  665. */
  666. if (ici->ops->init_videobuf)
  667. videobuf_streamoff(&icd->vb_vidq);
  668. else
  669. vb2_streamoff(&icd->vb2_vidq, i);
  670. v4l2_subdev_call(sd, video, s_stream, 0);
  671. return 0;
  672. }
  673. static int soc_camera_cropcap(struct file *file, void *fh,
  674. struct v4l2_cropcap *a)
  675. {
  676. struct soc_camera_device *icd = file->private_data;
  677. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  678. return ici->ops->cropcap(icd, a);
  679. }
  680. static int soc_camera_g_crop(struct file *file, void *fh,
  681. struct v4l2_crop *a)
  682. {
  683. struct soc_camera_device *icd = file->private_data;
  684. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  685. int ret;
  686. ret = ici->ops->get_crop(icd, a);
  687. return ret;
  688. }
  689. /*
  690. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  691. * argument with the actual geometry, instead, the user shall use G_CROP to
  692. * retrieve it.
  693. */
  694. static int soc_camera_s_crop(struct file *file, void *fh,
  695. struct v4l2_crop *a)
  696. {
  697. struct soc_camera_device *icd = file->private_data;
  698. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  699. struct v4l2_rect *rect = &a->c;
  700. struct v4l2_crop current_crop;
  701. int ret;
  702. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  703. return -EINVAL;
  704. dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
  705. rect->width, rect->height, rect->left, rect->top);
  706. /* If get_crop fails, we'll let host and / or client drivers decide */
  707. ret = ici->ops->get_crop(icd, &current_crop);
  708. /* Prohibit window size change with initialised buffers */
  709. if (ret < 0) {
  710. dev_err(icd->pdev,
  711. "S_CROP denied: getting current crop failed\n");
  712. } else if ((a->c.width == current_crop.c.width &&
  713. a->c.height == current_crop.c.height) ||
  714. !is_streaming(ici, icd)) {
  715. /* same size or not streaming - use .set_crop() */
  716. ret = ici->ops->set_crop(icd, a);
  717. } else if (ici->ops->set_livecrop) {
  718. ret = ici->ops->set_livecrop(icd, a);
  719. } else {
  720. dev_err(icd->pdev,
  721. "S_CROP denied: queue initialised and sizes differ\n");
  722. ret = -EBUSY;
  723. }
  724. return ret;
  725. }
  726. static int soc_camera_g_parm(struct file *file, void *fh,
  727. struct v4l2_streamparm *a)
  728. {
  729. struct soc_camera_device *icd = file->private_data;
  730. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  731. if (ici->ops->get_parm)
  732. return ici->ops->get_parm(icd, a);
  733. return -ENOIOCTLCMD;
  734. }
  735. static int soc_camera_s_parm(struct file *file, void *fh,
  736. struct v4l2_streamparm *a)
  737. {
  738. struct soc_camera_device *icd = file->private_data;
  739. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  740. if (ici->ops->set_parm)
  741. return ici->ops->set_parm(icd, a);
  742. return -ENOIOCTLCMD;
  743. }
  744. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  745. struct v4l2_dbg_chip_ident *id)
  746. {
  747. struct soc_camera_device *icd = file->private_data;
  748. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  749. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  750. }
  751. #ifdef CONFIG_VIDEO_ADV_DEBUG
  752. static int soc_camera_g_register(struct file *file, void *fh,
  753. struct v4l2_dbg_register *reg)
  754. {
  755. struct soc_camera_device *icd = file->private_data;
  756. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  757. return v4l2_subdev_call(sd, core, g_register, reg);
  758. }
  759. static int soc_camera_s_register(struct file *file, void *fh,
  760. struct v4l2_dbg_register *reg)
  761. {
  762. struct soc_camera_device *icd = file->private_data;
  763. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  764. return v4l2_subdev_call(sd, core, s_register, reg);
  765. }
  766. #endif
  767. static int soc_camera_probe(struct soc_camera_device *icd);
  768. /* So far this function cannot fail */
  769. static void scan_add_host(struct soc_camera_host *ici)
  770. {
  771. struct soc_camera_device *icd;
  772. mutex_lock(&ici->host_lock);
  773. list_for_each_entry(icd, &devices, list) {
  774. if (icd->iface == ici->nr) {
  775. int ret;
  776. icd->parent = ici->v4l2_dev.dev;
  777. ret = soc_camera_probe(icd);
  778. }
  779. }
  780. mutex_unlock(&ici->host_lock);
  781. }
  782. #ifdef CONFIG_I2C_BOARDINFO
  783. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  784. struct soc_camera_link *icl)
  785. {
  786. struct i2c_client *client;
  787. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  788. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  789. struct v4l2_subdev *subdev;
  790. if (!adap) {
  791. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  792. icl->i2c_adapter_id);
  793. goto ei2cga;
  794. }
  795. icl->board_info->platform_data = icl;
  796. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  797. icl->board_info, NULL);
  798. if (!subdev)
  799. goto ei2cnd;
  800. client = v4l2_get_subdevdata(subdev);
  801. /* Use to_i2c_client(dev) to recover the i2c client */
  802. icd->control = &client->dev;
  803. return 0;
  804. ei2cnd:
  805. i2c_put_adapter(adap);
  806. ei2cga:
  807. return -ENODEV;
  808. }
  809. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  810. {
  811. struct i2c_client *client =
  812. to_i2c_client(to_soc_camera_control(icd));
  813. struct i2c_adapter *adap = client->adapter;
  814. icd->control = NULL;
  815. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  816. i2c_unregister_device(client);
  817. i2c_put_adapter(adap);
  818. }
  819. #else
  820. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  821. #define soc_camera_free_i2c(icd) do {} while (0)
  822. #endif
  823. static int soc_camera_video_start(struct soc_camera_device *icd);
  824. static int video_dev_create(struct soc_camera_device *icd);
  825. /* Called during host-driver probe */
  826. static int soc_camera_probe(struct soc_camera_device *icd)
  827. {
  828. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  829. struct soc_camera_link *icl = to_soc_camera_link(icd);
  830. struct device *control = NULL;
  831. struct v4l2_subdev *sd;
  832. struct v4l2_mbus_framefmt mf;
  833. int ret;
  834. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  835. /*
  836. * Currently the subdev with the largest number of controls (13) is
  837. * ov6550. So let's pick 16 as a hint for the control handler. Note
  838. * that this is a hint only: too large and you waste some memory, too
  839. * small and there is a (very) small performance hit when looking up
  840. * controls in the internal hash.
  841. */
  842. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  843. if (ret < 0)
  844. return ret;
  845. ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
  846. icl->regulators);
  847. if (ret < 0)
  848. goto ereg;
  849. /* The camera could have been already on, try to reset */
  850. if (icl->reset)
  851. icl->reset(icd->pdev);
  852. ret = ici->ops->add(icd);
  853. if (ret < 0)
  854. goto eadd;
  855. /*
  856. * This will not yet call v4l2_subdev_core_ops::s_power(1), because the
  857. * subdevice has not been initialised yet. We'll have to call it once
  858. * again after initialisation, even though it shouldn't be needed, we
  859. * don't do any IO here.
  860. */
  861. ret = soc_camera_power_on(icd, icl);
  862. if (ret < 0)
  863. goto epower;
  864. /* Must have icd->vdev before registering the device */
  865. ret = video_dev_create(icd);
  866. if (ret < 0)
  867. goto evdc;
  868. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  869. if (icl->board_info) {
  870. ret = soc_camera_init_i2c(icd, icl);
  871. if (ret < 0)
  872. goto eadddev;
  873. } else if (!icl->add_device || !icl->del_device) {
  874. ret = -EINVAL;
  875. goto eadddev;
  876. } else {
  877. if (icl->module_name)
  878. ret = request_module(icl->module_name);
  879. ret = icl->add_device(icd);
  880. if (ret < 0)
  881. goto eadddev;
  882. /*
  883. * FIXME: this is racy, have to use driver-binding notification,
  884. * when it is available
  885. */
  886. control = to_soc_camera_control(icd);
  887. if (!control || !control->driver || !dev_get_drvdata(control) ||
  888. !try_module_get(control->driver->owner)) {
  889. icl->del_device(icd);
  890. ret = -ENODEV;
  891. goto enodrv;
  892. }
  893. }
  894. sd = soc_camera_to_subdev(icd);
  895. sd->grp_id = soc_camera_grp_id(icd);
  896. v4l2_set_subdev_hostdata(sd, icd);
  897. if (v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler))
  898. goto ectrl;
  899. /* At this point client .probe() should have run already */
  900. ret = soc_camera_init_user_formats(icd);
  901. if (ret < 0)
  902. goto eiufmt;
  903. icd->field = V4L2_FIELD_ANY;
  904. /*
  905. * ..._video_start() will create a device node, video_register_device()
  906. * itself is protected against concurrent open() calls, but we also have
  907. * to protect our data.
  908. */
  909. mutex_lock(&icd->video_lock);
  910. ret = soc_camera_video_start(icd);
  911. if (ret < 0)
  912. goto evidstart;
  913. ret = v4l2_subdev_call(sd, core, s_power, 1);
  914. if (ret < 0 && ret != -ENOIOCTLCMD)
  915. goto esdpwr;
  916. /* Try to improve our guess of a reasonable window format */
  917. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  918. icd->user_width = mf.width;
  919. icd->user_height = mf.height;
  920. icd->colorspace = mf.colorspace;
  921. icd->field = mf.field;
  922. }
  923. ici->ops->remove(icd);
  924. soc_camera_power_off(icd, icl);
  925. mutex_unlock(&icd->video_lock);
  926. return 0;
  927. esdpwr:
  928. video_unregister_device(icd->vdev);
  929. evidstart:
  930. mutex_unlock(&icd->video_lock);
  931. soc_camera_free_user_formats(icd);
  932. eiufmt:
  933. ectrl:
  934. if (icl->board_info) {
  935. soc_camera_free_i2c(icd);
  936. } else {
  937. icl->del_device(icd);
  938. module_put(control->driver->owner);
  939. }
  940. enodrv:
  941. eadddev:
  942. video_device_release(icd->vdev);
  943. icd->vdev = NULL;
  944. evdc:
  945. soc_camera_power_off(icd, icl);
  946. epower:
  947. ici->ops->remove(icd);
  948. eadd:
  949. regulator_bulk_free(icl->num_regulators, icl->regulators);
  950. ereg:
  951. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  952. return ret;
  953. }
  954. /*
  955. * This is called on device_unregister, which only means we have to disconnect
  956. * from the host, but not remove ourselves from the device list
  957. */
  958. static int soc_camera_remove(struct soc_camera_device *icd)
  959. {
  960. struct soc_camera_link *icl = to_soc_camera_link(icd);
  961. struct video_device *vdev = icd->vdev;
  962. BUG_ON(!icd->parent);
  963. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  964. if (vdev) {
  965. video_unregister_device(vdev);
  966. icd->vdev = NULL;
  967. }
  968. if (icl->board_info) {
  969. soc_camera_free_i2c(icd);
  970. } else {
  971. struct device_driver *drv = to_soc_camera_control(icd)->driver;
  972. if (drv) {
  973. icl->del_device(icd);
  974. module_put(drv->owner);
  975. }
  976. }
  977. soc_camera_free_user_formats(icd);
  978. regulator_bulk_free(icl->num_regulators, icl->regulators);
  979. return 0;
  980. }
  981. static int default_cropcap(struct soc_camera_device *icd,
  982. struct v4l2_cropcap *a)
  983. {
  984. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  985. return v4l2_subdev_call(sd, video, cropcap, a);
  986. }
  987. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  988. {
  989. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  990. return v4l2_subdev_call(sd, video, g_crop, a);
  991. }
  992. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  993. {
  994. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  995. return v4l2_subdev_call(sd, video, s_crop, a);
  996. }
  997. static int default_g_parm(struct soc_camera_device *icd,
  998. struct v4l2_streamparm *parm)
  999. {
  1000. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1001. return v4l2_subdev_call(sd, video, g_parm, parm);
  1002. }
  1003. static int default_s_parm(struct soc_camera_device *icd,
  1004. struct v4l2_streamparm *parm)
  1005. {
  1006. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1007. return v4l2_subdev_call(sd, video, s_parm, parm);
  1008. }
  1009. static int default_enum_fsizes(struct soc_camera_device *icd,
  1010. struct v4l2_frmsizeenum *fsize)
  1011. {
  1012. int ret;
  1013. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1014. const struct soc_camera_format_xlate *xlate;
  1015. __u32 pixfmt = fsize->pixel_format;
  1016. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1017. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1018. if (!xlate)
  1019. return -EINVAL;
  1020. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1021. fsize_mbus.pixel_format = xlate->code;
  1022. ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
  1023. if (ret < 0)
  1024. return ret;
  1025. *fsize = fsize_mbus;
  1026. fsize->pixel_format = pixfmt;
  1027. return 0;
  1028. }
  1029. int soc_camera_host_register(struct soc_camera_host *ici)
  1030. {
  1031. struct soc_camera_host *ix;
  1032. int ret;
  1033. if (!ici || !ici->ops ||
  1034. !ici->ops->try_fmt ||
  1035. !ici->ops->set_fmt ||
  1036. !ici->ops->set_bus_param ||
  1037. !ici->ops->querycap ||
  1038. ((!ici->ops->init_videobuf ||
  1039. !ici->ops->reqbufs) &&
  1040. !ici->ops->init_videobuf2) ||
  1041. !ici->ops->add ||
  1042. !ici->ops->remove ||
  1043. !ici->ops->poll ||
  1044. !ici->v4l2_dev.dev)
  1045. return -EINVAL;
  1046. if (!ici->ops->set_crop)
  1047. ici->ops->set_crop = default_s_crop;
  1048. if (!ici->ops->get_crop)
  1049. ici->ops->get_crop = default_g_crop;
  1050. if (!ici->ops->cropcap)
  1051. ici->ops->cropcap = default_cropcap;
  1052. if (!ici->ops->set_parm)
  1053. ici->ops->set_parm = default_s_parm;
  1054. if (!ici->ops->get_parm)
  1055. ici->ops->get_parm = default_g_parm;
  1056. if (!ici->ops->enum_fsizes)
  1057. ici->ops->enum_fsizes = default_enum_fsizes;
  1058. mutex_lock(&list_lock);
  1059. list_for_each_entry(ix, &hosts, list) {
  1060. if (ix->nr == ici->nr) {
  1061. ret = -EBUSY;
  1062. goto edevreg;
  1063. }
  1064. }
  1065. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1066. if (ret < 0)
  1067. goto edevreg;
  1068. list_add_tail(&ici->list, &hosts);
  1069. mutex_unlock(&list_lock);
  1070. mutex_init(&ici->host_lock);
  1071. scan_add_host(ici);
  1072. return 0;
  1073. edevreg:
  1074. mutex_unlock(&list_lock);
  1075. return ret;
  1076. }
  1077. EXPORT_SYMBOL(soc_camera_host_register);
  1078. /* Unregister all clients! */
  1079. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1080. {
  1081. struct soc_camera_device *icd;
  1082. mutex_lock(&list_lock);
  1083. list_del(&ici->list);
  1084. list_for_each_entry(icd, &devices, list)
  1085. if (icd->iface == ici->nr && to_soc_camera_control(icd))
  1086. soc_camera_remove(icd);
  1087. mutex_unlock(&list_lock);
  1088. v4l2_device_unregister(&ici->v4l2_dev);
  1089. }
  1090. EXPORT_SYMBOL(soc_camera_host_unregister);
  1091. /* Image capture device */
  1092. static int soc_camera_device_register(struct soc_camera_device *icd)
  1093. {
  1094. struct soc_camera_device *ix;
  1095. int num = -1, i;
  1096. for (i = 0; i < 256 && num < 0; i++) {
  1097. num = i;
  1098. /* Check if this index is available on this interface */
  1099. list_for_each_entry(ix, &devices, list) {
  1100. if (ix->iface == icd->iface && ix->devnum == i) {
  1101. num = -1;
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. if (num < 0)
  1107. /*
  1108. * ok, we have 256 cameras on this host...
  1109. * man, stay reasonable...
  1110. */
  1111. return -ENOMEM;
  1112. icd->devnum = num;
  1113. icd->use_count = 0;
  1114. icd->host_priv = NULL;
  1115. mutex_init(&icd->video_lock);
  1116. list_add_tail(&icd->list, &devices);
  1117. return 0;
  1118. }
  1119. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1120. .vidioc_querycap = soc_camera_querycap,
  1121. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1122. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1123. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1124. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1125. .vidioc_enum_input = soc_camera_enum_input,
  1126. .vidioc_g_input = soc_camera_g_input,
  1127. .vidioc_s_input = soc_camera_s_input,
  1128. .vidioc_s_std = soc_camera_s_std,
  1129. .vidioc_g_std = soc_camera_g_std,
  1130. .vidioc_enum_framesizes = soc_camera_enum_fsizes,
  1131. .vidioc_reqbufs = soc_camera_reqbufs,
  1132. .vidioc_querybuf = soc_camera_querybuf,
  1133. .vidioc_qbuf = soc_camera_qbuf,
  1134. .vidioc_dqbuf = soc_camera_dqbuf,
  1135. .vidioc_create_bufs = soc_camera_create_bufs,
  1136. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1137. .vidioc_streamon = soc_camera_streamon,
  1138. .vidioc_streamoff = soc_camera_streamoff,
  1139. .vidioc_cropcap = soc_camera_cropcap,
  1140. .vidioc_g_crop = soc_camera_g_crop,
  1141. .vidioc_s_crop = soc_camera_s_crop,
  1142. .vidioc_g_parm = soc_camera_g_parm,
  1143. .vidioc_s_parm = soc_camera_s_parm,
  1144. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1145. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1146. .vidioc_g_register = soc_camera_g_register,
  1147. .vidioc_s_register = soc_camera_s_register,
  1148. #endif
  1149. };
  1150. static int video_dev_create(struct soc_camera_device *icd)
  1151. {
  1152. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1153. struct video_device *vdev = video_device_alloc();
  1154. if (!vdev)
  1155. return -ENOMEM;
  1156. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1157. vdev->parent = icd->pdev;
  1158. vdev->current_norm = V4L2_STD_UNKNOWN;
  1159. vdev->fops = &soc_camera_fops;
  1160. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1161. vdev->release = video_device_release;
  1162. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1163. vdev->ctrl_handler = &icd->ctrl_handler;
  1164. vdev->lock = &icd->video_lock;
  1165. icd->vdev = vdev;
  1166. return 0;
  1167. }
  1168. /*
  1169. * Called from soc_camera_probe() above (with .video_lock held???)
  1170. */
  1171. static int soc_camera_video_start(struct soc_camera_device *icd)
  1172. {
  1173. const struct device_type *type = icd->vdev->dev.type;
  1174. int ret;
  1175. if (!icd->parent)
  1176. return -ENODEV;
  1177. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1178. if (ret < 0) {
  1179. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1180. return ret;
  1181. }
  1182. /* Restore device type, possibly set by the subdevice driver */
  1183. icd->vdev->dev.type = type;
  1184. return 0;
  1185. }
  1186. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1187. {
  1188. struct soc_camera_link *icl = pdev->dev.platform_data;
  1189. struct soc_camera_device *icd;
  1190. int ret;
  1191. if (!icl)
  1192. return -EINVAL;
  1193. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1194. if (!icd)
  1195. return -ENOMEM;
  1196. icd->iface = icl->bus_id;
  1197. icd->link = icl;
  1198. icd->pdev = &pdev->dev;
  1199. platform_set_drvdata(pdev, icd);
  1200. ret = soc_camera_device_register(icd);
  1201. if (ret < 0)
  1202. goto escdevreg;
  1203. icd->user_width = DEFAULT_WIDTH;
  1204. icd->user_height = DEFAULT_HEIGHT;
  1205. return 0;
  1206. escdevreg:
  1207. kfree(icd);
  1208. return ret;
  1209. }
  1210. /*
  1211. * Only called on rmmod for each platform device, since they are not
  1212. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1213. * been unloaded already
  1214. */
  1215. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1216. {
  1217. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1218. if (!icd)
  1219. return -EINVAL;
  1220. list_del(&icd->list);
  1221. kfree(icd);
  1222. return 0;
  1223. }
  1224. static struct platform_driver __refdata soc_camera_pdrv = {
  1225. .remove = __devexit_p(soc_camera_pdrv_remove),
  1226. .driver = {
  1227. .name = "soc-camera-pdrv",
  1228. .owner = THIS_MODULE,
  1229. },
  1230. };
  1231. static int __init soc_camera_init(void)
  1232. {
  1233. return platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1234. }
  1235. static void __exit soc_camera_exit(void)
  1236. {
  1237. platform_driver_unregister(&soc_camera_pdrv);
  1238. }
  1239. module_init(soc_camera_init);
  1240. module_exit(soc_camera_exit);
  1241. MODULE_DESCRIPTION("Image capture bus driver");
  1242. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1243. MODULE_LICENSE("GPL");
  1244. MODULE_ALIAS("platform:soc-camera-pdrv");