soc_camera.c 40 KB

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