soc_camera.c 39 KB

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