soc_camera.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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_subdev_desc *ssdd)
  48. {
  49. int ret = regulator_bulk_enable(ssdd->num_regulators,
  50. ssdd->regulators);
  51. if (ret < 0) {
  52. dev_err(dev, "Cannot enable regulators\n");
  53. return ret;
  54. }
  55. if (ssdd->power) {
  56. ret = ssdd->power(dev, 1);
  57. if (ret < 0) {
  58. dev_err(dev,
  59. "Platform failed to power-on the camera.\n");
  60. regulator_bulk_disable(ssdd->num_regulators,
  61. ssdd->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_subdev_desc *ssdd)
  68. {
  69. int ret = 0;
  70. int err;
  71. if (ssdd->power) {
  72. err = ssdd->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(ssdd->num_regulators,
  80. ssdd->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. * @ssdd: camera platform parameters
  119. * @cfg: media bus configuration
  120. * @return: resulting flags
  121. */
  122. unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
  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 (ssdd->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 (ssdd->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 (ssdd->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 .host_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 .host_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;
  417. struct soc_camera_host *ici;
  418. int ret;
  419. /*
  420. * Don't mess with the host during probe: wait until the loop in
  421. * scan_add_host() completes. Also protect against a race with
  422. * soc_camera_host_unregister().
  423. */
  424. if (mutex_lock_interruptible(&list_lock))
  425. return -ERESTARTSYS;
  426. if (!vdev || !video_is_registered(vdev)) {
  427. mutex_unlock(&list_lock);
  428. return -ENODEV;
  429. }
  430. icd = dev_get_drvdata(vdev->parent);
  431. ici = to_soc_camera_host(icd->parent);
  432. ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
  433. mutex_unlock(&list_lock);
  434. if (ret < 0) {
  435. dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
  436. return ret;
  437. }
  438. if (!to_soc_camera_control(icd)) {
  439. /* No device driver attached */
  440. ret = -ENODEV;
  441. goto econtrol;
  442. }
  443. if (mutex_lock_interruptible(&ici->host_lock)) {
  444. ret = -ERESTARTSYS;
  445. goto elockhost;
  446. }
  447. icd->use_count++;
  448. /* Now we really have to activate the camera */
  449. if (icd->use_count == 1) {
  450. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  451. /* Restore parameters before the last close() per V4L2 API */
  452. struct v4l2_format f = {
  453. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  454. .fmt.pix = {
  455. .width = icd->user_width,
  456. .height = icd->user_height,
  457. .field = icd->field,
  458. .colorspace = icd->colorspace,
  459. .pixelformat =
  460. icd->current_fmt->host_fmt->fourcc,
  461. },
  462. };
  463. /* The camera could have been already on, try to reset */
  464. if (sdesc->subdev_desc.reset)
  465. sdesc->subdev_desc.reset(icd->pdev);
  466. ret = ici->ops->add(icd);
  467. if (ret < 0) {
  468. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  469. goto eiciadd;
  470. }
  471. ret = __soc_camera_power_on(icd);
  472. if (ret < 0)
  473. goto epower;
  474. pm_runtime_enable(&icd->vdev->dev);
  475. ret = pm_runtime_resume(&icd->vdev->dev);
  476. if (ret < 0 && ret != -ENOSYS)
  477. goto eresume;
  478. /*
  479. * Try to configure with default parameters. Notice: this is the
  480. * very first open, so, we cannot race against other calls,
  481. * apart from someone else calling open() simultaneously, but
  482. * .host_lock is protecting us against it.
  483. */
  484. ret = soc_camera_set_fmt(icd, &f);
  485. if (ret < 0)
  486. goto esfmt;
  487. if (ici->ops->init_videobuf) {
  488. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  489. } else {
  490. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  491. if (ret < 0)
  492. goto einitvb;
  493. }
  494. v4l2_ctrl_handler_setup(&icd->ctrl_handler);
  495. }
  496. mutex_unlock(&ici->host_lock);
  497. file->private_data = icd;
  498. dev_dbg(icd->pdev, "camera device open\n");
  499. return 0;
  500. /*
  501. * First four errors are entered with the .host_lock held
  502. * and use_count == 1
  503. */
  504. einitvb:
  505. esfmt:
  506. pm_runtime_disable(&icd->vdev->dev);
  507. eresume:
  508. __soc_camera_power_off(icd);
  509. epower:
  510. ici->ops->remove(icd);
  511. eiciadd:
  512. icd->use_count--;
  513. mutex_unlock(&ici->host_lock);
  514. elockhost:
  515. econtrol:
  516. module_put(ici->ops->owner);
  517. return ret;
  518. }
  519. static int soc_camera_close(struct file *file)
  520. {
  521. struct soc_camera_device *icd = file->private_data;
  522. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  523. mutex_lock(&ici->host_lock);
  524. icd->use_count--;
  525. if (!icd->use_count) {
  526. pm_runtime_suspend(&icd->vdev->dev);
  527. pm_runtime_disable(&icd->vdev->dev);
  528. if (ici->ops->init_videobuf2)
  529. vb2_queue_release(&icd->vb2_vidq);
  530. ici->ops->remove(icd);
  531. __soc_camera_power_off(icd);
  532. }
  533. if (icd->streamer == file)
  534. icd->streamer = NULL;
  535. mutex_unlock(&ici->host_lock);
  536. module_put(ici->ops->owner);
  537. dev_dbg(icd->pdev, "camera device close\n");
  538. return 0;
  539. }
  540. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  541. size_t count, loff_t *ppos)
  542. {
  543. struct soc_camera_device *icd = file->private_data;
  544. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  545. dev_dbg(icd->pdev, "read called, buf %p\n", buf);
  546. if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
  547. return vb2_read(&icd->vb2_vidq, buf, count, ppos,
  548. file->f_flags & O_NONBLOCK);
  549. dev_err(icd->pdev, "camera device read not implemented\n");
  550. return -EINVAL;
  551. }
  552. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  553. {
  554. struct soc_camera_device *icd = file->private_data;
  555. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  556. int err;
  557. dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  558. if (icd->streamer != file)
  559. return -EBUSY;
  560. if (mutex_lock_interruptible(&ici->host_lock))
  561. return -ERESTARTSYS;
  562. if (ici->ops->init_videobuf)
  563. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  564. else
  565. err = vb2_mmap(&icd->vb2_vidq, vma);
  566. mutex_unlock(&ici->host_lock);
  567. dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  568. (unsigned long)vma->vm_start,
  569. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  570. err);
  571. return err;
  572. }
  573. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  574. {
  575. struct soc_camera_device *icd = file->private_data;
  576. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  577. unsigned res = POLLERR;
  578. if (icd->streamer != file)
  579. return POLLERR;
  580. mutex_lock(&ici->host_lock);
  581. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
  582. dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
  583. else
  584. res = ici->ops->poll(file, pt);
  585. mutex_unlock(&ici->host_lock);
  586. return res;
  587. }
  588. void soc_camera_lock(struct vb2_queue *vq)
  589. {
  590. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  591. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  592. mutex_lock(&ici->host_lock);
  593. }
  594. EXPORT_SYMBOL(soc_camera_lock);
  595. void soc_camera_unlock(struct vb2_queue *vq)
  596. {
  597. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  598. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  599. mutex_unlock(&ici->host_lock);
  600. }
  601. EXPORT_SYMBOL(soc_camera_unlock);
  602. static struct v4l2_file_operations soc_camera_fops = {
  603. .owner = THIS_MODULE,
  604. .open = soc_camera_open,
  605. .release = soc_camera_close,
  606. .unlocked_ioctl = video_ioctl2,
  607. .read = soc_camera_read,
  608. .mmap = soc_camera_mmap,
  609. .poll = soc_camera_poll,
  610. };
  611. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  612. struct v4l2_format *f)
  613. {
  614. struct soc_camera_device *icd = file->private_data;
  615. int ret;
  616. WARN_ON(priv != file->private_data);
  617. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  618. dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
  619. return -EINVAL;
  620. }
  621. if (icd->streamer && icd->streamer != file)
  622. return -EBUSY;
  623. if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
  624. dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
  625. return -EBUSY;
  626. }
  627. ret = soc_camera_set_fmt(icd, f);
  628. if (!ret && !icd->streamer)
  629. icd->streamer = file;
  630. return ret;
  631. }
  632. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  633. struct v4l2_fmtdesc *f)
  634. {
  635. struct soc_camera_device *icd = file->private_data;
  636. const struct soc_mbus_pixelfmt *format;
  637. WARN_ON(priv != file->private_data);
  638. if (f->index >= icd->num_user_formats)
  639. return -EINVAL;
  640. format = icd->user_formats[f->index].host_fmt;
  641. if (format->name)
  642. strlcpy(f->description, format->name, sizeof(f->description));
  643. f->pixelformat = format->fourcc;
  644. return 0;
  645. }
  646. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  647. struct v4l2_format *f)
  648. {
  649. struct soc_camera_device *icd = file->private_data;
  650. struct v4l2_pix_format *pix = &f->fmt.pix;
  651. WARN_ON(priv != file->private_data);
  652. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  653. return -EINVAL;
  654. pix->width = icd->user_width;
  655. pix->height = icd->user_height;
  656. pix->bytesperline = icd->bytesperline;
  657. pix->sizeimage = icd->sizeimage;
  658. pix->field = icd->field;
  659. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  660. pix->colorspace = icd->colorspace;
  661. dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
  662. icd->current_fmt->host_fmt->fourcc);
  663. return 0;
  664. }
  665. static int soc_camera_querycap(struct file *file, void *priv,
  666. struct v4l2_capability *cap)
  667. {
  668. struct soc_camera_device *icd = file->private_data;
  669. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  670. WARN_ON(priv != file->private_data);
  671. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  672. return ici->ops->querycap(ici, cap);
  673. }
  674. static int soc_camera_streamon(struct file *file, void *priv,
  675. enum v4l2_buf_type i)
  676. {
  677. struct soc_camera_device *icd = file->private_data;
  678. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  679. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  680. int ret;
  681. WARN_ON(priv != file->private_data);
  682. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  683. return -EINVAL;
  684. if (icd->streamer != file)
  685. return -EBUSY;
  686. /* This calls buf_queue from host driver's videobuf_queue_ops */
  687. if (ici->ops->init_videobuf)
  688. ret = videobuf_streamon(&icd->vb_vidq);
  689. else
  690. ret = vb2_streamon(&icd->vb2_vidq, i);
  691. if (!ret)
  692. v4l2_subdev_call(sd, video, s_stream, 1);
  693. return ret;
  694. }
  695. static int soc_camera_streamoff(struct file *file, void *priv,
  696. enum v4l2_buf_type i)
  697. {
  698. struct soc_camera_device *icd = file->private_data;
  699. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  700. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  701. WARN_ON(priv != file->private_data);
  702. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  703. return -EINVAL;
  704. if (icd->streamer != file)
  705. return -EBUSY;
  706. /*
  707. * This calls buf_release from host driver's videobuf_queue_ops for all
  708. * remaining buffers. When the last buffer is freed, stop capture
  709. */
  710. if (ici->ops->init_videobuf)
  711. videobuf_streamoff(&icd->vb_vidq);
  712. else
  713. vb2_streamoff(&icd->vb2_vidq, i);
  714. v4l2_subdev_call(sd, video, s_stream, 0);
  715. return 0;
  716. }
  717. static int soc_camera_cropcap(struct file *file, void *fh,
  718. struct v4l2_cropcap *a)
  719. {
  720. struct soc_camera_device *icd = file->private_data;
  721. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  722. return ici->ops->cropcap(icd, a);
  723. }
  724. static int soc_camera_g_crop(struct file *file, void *fh,
  725. struct v4l2_crop *a)
  726. {
  727. struct soc_camera_device *icd = file->private_data;
  728. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  729. int ret;
  730. ret = ici->ops->get_crop(icd, a);
  731. return ret;
  732. }
  733. /*
  734. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  735. * argument with the actual geometry, instead, the user shall use G_CROP to
  736. * retrieve it.
  737. */
  738. static int soc_camera_s_crop(struct file *file, void *fh,
  739. const struct v4l2_crop *a)
  740. {
  741. struct soc_camera_device *icd = file->private_data;
  742. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  743. const struct v4l2_rect *rect = &a->c;
  744. struct v4l2_crop current_crop;
  745. int ret;
  746. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  747. return -EINVAL;
  748. dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
  749. rect->width, rect->height, rect->left, rect->top);
  750. current_crop.type = a->type;
  751. /* If get_crop fails, we'll let host and / or client drivers decide */
  752. ret = ici->ops->get_crop(icd, &current_crop);
  753. /* Prohibit window size change with initialised buffers */
  754. if (ret < 0) {
  755. dev_err(icd->pdev,
  756. "S_CROP denied: getting current crop failed\n");
  757. } else if ((a->c.width == current_crop.c.width &&
  758. a->c.height == current_crop.c.height) ||
  759. !is_streaming(ici, icd)) {
  760. /* same size or not streaming - use .set_crop() */
  761. ret = ici->ops->set_crop(icd, a);
  762. } else if (ici->ops->set_livecrop) {
  763. ret = ici->ops->set_livecrop(icd, a);
  764. } else {
  765. dev_err(icd->pdev,
  766. "S_CROP denied: queue initialised and sizes differ\n");
  767. ret = -EBUSY;
  768. }
  769. return ret;
  770. }
  771. static int soc_camera_g_selection(struct file *file, void *fh,
  772. struct v4l2_selection *s)
  773. {
  774. struct soc_camera_device *icd = file->private_data;
  775. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  776. /* With a wrong type no need to try to fall back to cropping */
  777. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  778. return -EINVAL;
  779. if (!ici->ops->get_selection)
  780. return -ENOTTY;
  781. return ici->ops->get_selection(icd, s);
  782. }
  783. static int soc_camera_s_selection(struct file *file, void *fh,
  784. struct v4l2_selection *s)
  785. {
  786. struct soc_camera_device *icd = file->private_data;
  787. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  788. int ret;
  789. /* In all these cases cropping emulation will not help */
  790. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  791. (s->target != V4L2_SEL_TGT_COMPOSE &&
  792. s->target != V4L2_SEL_TGT_CROP))
  793. return -EINVAL;
  794. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  795. /* No output size change during a running capture! */
  796. if (is_streaming(ici, icd) &&
  797. (icd->user_width != s->r.width ||
  798. icd->user_height != s->r.height))
  799. return -EBUSY;
  800. /*
  801. * Only one user is allowed to change the output format, touch
  802. * buffers, start / stop streaming, poll for data
  803. */
  804. if (icd->streamer && icd->streamer != file)
  805. return -EBUSY;
  806. }
  807. if (!ici->ops->set_selection)
  808. return -ENOTTY;
  809. ret = ici->ops->set_selection(icd, s);
  810. if (!ret &&
  811. s->target == V4L2_SEL_TGT_COMPOSE) {
  812. icd->user_width = s->r.width;
  813. icd->user_height = s->r.height;
  814. if (!icd->streamer)
  815. icd->streamer = file;
  816. }
  817. return ret;
  818. }
  819. static int soc_camera_g_parm(struct file *file, void *fh,
  820. struct v4l2_streamparm *a)
  821. {
  822. struct soc_camera_device *icd = file->private_data;
  823. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  824. if (ici->ops->get_parm)
  825. return ici->ops->get_parm(icd, a);
  826. return -ENOIOCTLCMD;
  827. }
  828. static int soc_camera_s_parm(struct file *file, void *fh,
  829. struct v4l2_streamparm *a)
  830. {
  831. struct soc_camera_device *icd = file->private_data;
  832. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  833. if (ici->ops->set_parm)
  834. return ici->ops->set_parm(icd, a);
  835. return -ENOIOCTLCMD;
  836. }
  837. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  838. struct v4l2_dbg_chip_ident *id)
  839. {
  840. struct soc_camera_device *icd = file->private_data;
  841. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  842. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  843. }
  844. #ifdef CONFIG_VIDEO_ADV_DEBUG
  845. static int soc_camera_g_register(struct file *file, void *fh,
  846. struct v4l2_dbg_register *reg)
  847. {
  848. struct soc_camera_device *icd = file->private_data;
  849. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  850. return v4l2_subdev_call(sd, core, g_register, reg);
  851. }
  852. static int soc_camera_s_register(struct file *file, void *fh,
  853. const struct v4l2_dbg_register *reg)
  854. {
  855. struct soc_camera_device *icd = file->private_data;
  856. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  857. return v4l2_subdev_call(sd, core, s_register, reg);
  858. }
  859. #endif
  860. static int soc_camera_probe(struct soc_camera_device *icd);
  861. /* So far this function cannot fail */
  862. static void scan_add_host(struct soc_camera_host *ici)
  863. {
  864. struct soc_camera_device *icd;
  865. mutex_lock(&list_lock);
  866. list_for_each_entry(icd, &devices, list) {
  867. if (icd->iface == ici->nr) {
  868. icd->parent = ici->v4l2_dev.dev;
  869. soc_camera_probe(icd);
  870. }
  871. }
  872. mutex_unlock(&list_lock);
  873. }
  874. #ifdef CONFIG_I2C_BOARDINFO
  875. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  876. struct soc_camera_desc *sdesc)
  877. {
  878. struct i2c_client *client;
  879. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  880. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  881. struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id);
  882. struct v4l2_subdev *subdev;
  883. if (!adap) {
  884. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  885. shd->i2c_adapter_id);
  886. goto ei2cga;
  887. }
  888. shd->board_info->platform_data = &sdesc->subdev_desc;
  889. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  890. shd->board_info, NULL);
  891. if (!subdev)
  892. goto ei2cnd;
  893. client = v4l2_get_subdevdata(subdev);
  894. /* Use to_i2c_client(dev) to recover the i2c client */
  895. icd->control = &client->dev;
  896. return 0;
  897. ei2cnd:
  898. i2c_put_adapter(adap);
  899. ei2cga:
  900. return -ENODEV;
  901. }
  902. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  903. {
  904. struct i2c_client *client =
  905. to_i2c_client(to_soc_camera_control(icd));
  906. struct i2c_adapter *adap = client->adapter;
  907. icd->control = NULL;
  908. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  909. i2c_unregister_device(client);
  910. i2c_put_adapter(adap);
  911. }
  912. #else
  913. #define soc_camera_init_i2c(icd, sdesc) (-ENODEV)
  914. #define soc_camera_free_i2c(icd) do {} while (0)
  915. #endif
  916. static int soc_camera_video_start(struct soc_camera_device *icd);
  917. static int video_dev_create(struct soc_camera_device *icd);
  918. /* Called during host-driver probe */
  919. static int soc_camera_probe(struct soc_camera_device *icd)
  920. {
  921. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  922. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  923. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  924. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  925. struct device *control = NULL;
  926. struct v4l2_subdev *sd;
  927. struct v4l2_mbus_framefmt mf;
  928. int ret;
  929. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  930. /*
  931. * Currently the subdev with the largest number of controls (13) is
  932. * ov6550. So let's pick 16 as a hint for the control handler. Note
  933. * that this is a hint only: too large and you waste some memory, too
  934. * small and there is a (very) small performance hit when looking up
  935. * controls in the internal hash.
  936. */
  937. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  938. if (ret < 0)
  939. return ret;
  940. /* The camera could have been already on, try to reset */
  941. if (ssdd->reset)
  942. ssdd->reset(icd->pdev);
  943. mutex_lock(&ici->host_lock);
  944. ret = ici->ops->add(icd);
  945. mutex_unlock(&ici->host_lock);
  946. if (ret < 0)
  947. goto eadd;
  948. /* Must have icd->vdev before registering the device */
  949. ret = video_dev_create(icd);
  950. if (ret < 0)
  951. goto evdc;
  952. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  953. if (shd->board_info) {
  954. ret = soc_camera_init_i2c(icd, sdesc);
  955. if (ret < 0)
  956. goto eadddev;
  957. } else if (!shd->add_device || !shd->del_device) {
  958. ret = -EINVAL;
  959. goto eadddev;
  960. } else {
  961. if (shd->module_name)
  962. ret = request_module(shd->module_name);
  963. ret = shd->add_device(icd);
  964. if (ret < 0)
  965. goto eadddev;
  966. /*
  967. * FIXME: this is racy, have to use driver-binding notification,
  968. * when it is available
  969. */
  970. control = to_soc_camera_control(icd);
  971. if (!control || !control->driver || !dev_get_drvdata(control) ||
  972. !try_module_get(control->driver->owner)) {
  973. shd->del_device(icd);
  974. ret = -ENODEV;
  975. goto enodrv;
  976. }
  977. }
  978. sd = soc_camera_to_subdev(icd);
  979. sd->grp_id = soc_camera_grp_id(icd);
  980. v4l2_set_subdev_hostdata(sd, icd);
  981. ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
  982. if (ret < 0)
  983. goto ectrl;
  984. /* At this point client .probe() should have run already */
  985. ret = soc_camera_init_user_formats(icd);
  986. if (ret < 0)
  987. goto eiufmt;
  988. icd->field = V4L2_FIELD_ANY;
  989. /*
  990. * ..._video_start() will create a device node, video_register_device()
  991. * itself is protected against concurrent open() calls, but we also have
  992. * to protect our data.
  993. */
  994. mutex_lock(&ici->host_lock);
  995. ret = soc_camera_video_start(icd);
  996. if (ret < 0)
  997. goto evidstart;
  998. /* Try to improve our guess of a reasonable window format */
  999. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  1000. icd->user_width = mf.width;
  1001. icd->user_height = mf.height;
  1002. icd->colorspace = mf.colorspace;
  1003. icd->field = mf.field;
  1004. }
  1005. ici->ops->remove(icd);
  1006. mutex_unlock(&ici->host_lock);
  1007. return 0;
  1008. evidstart:
  1009. mutex_unlock(&ici->host_lock);
  1010. soc_camera_free_user_formats(icd);
  1011. eiufmt:
  1012. ectrl:
  1013. if (shd->board_info) {
  1014. soc_camera_free_i2c(icd);
  1015. } else {
  1016. shd->del_device(icd);
  1017. module_put(control->driver->owner);
  1018. }
  1019. enodrv:
  1020. eadddev:
  1021. video_device_release(icd->vdev);
  1022. icd->vdev = NULL;
  1023. evdc:
  1024. mutex_lock(&ici->host_lock);
  1025. ici->ops->remove(icd);
  1026. mutex_unlock(&ici->host_lock);
  1027. eadd:
  1028. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1029. return ret;
  1030. }
  1031. /*
  1032. * This is called on device_unregister, which only means we have to disconnect
  1033. * from the host, but not remove ourselves from the device list
  1034. */
  1035. static int soc_camera_remove(struct soc_camera_device *icd)
  1036. {
  1037. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1038. struct video_device *vdev = icd->vdev;
  1039. BUG_ON(!icd->parent);
  1040. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1041. if (vdev) {
  1042. video_unregister_device(vdev);
  1043. icd->vdev = NULL;
  1044. }
  1045. if (sdesc->host_desc.board_info) {
  1046. soc_camera_free_i2c(icd);
  1047. } else {
  1048. struct device_driver *drv = to_soc_camera_control(icd)->driver;
  1049. if (drv) {
  1050. sdesc->host_desc.del_device(icd);
  1051. module_put(drv->owner);
  1052. }
  1053. }
  1054. soc_camera_free_user_formats(icd);
  1055. return 0;
  1056. }
  1057. static int default_cropcap(struct soc_camera_device *icd,
  1058. struct v4l2_cropcap *a)
  1059. {
  1060. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1061. return v4l2_subdev_call(sd, video, cropcap, a);
  1062. }
  1063. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1064. {
  1065. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1066. return v4l2_subdev_call(sd, video, g_crop, a);
  1067. }
  1068. static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
  1069. {
  1070. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1071. return v4l2_subdev_call(sd, video, s_crop, a);
  1072. }
  1073. static int default_g_parm(struct soc_camera_device *icd,
  1074. struct v4l2_streamparm *parm)
  1075. {
  1076. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1077. return v4l2_subdev_call(sd, video, g_parm, parm);
  1078. }
  1079. static int default_s_parm(struct soc_camera_device *icd,
  1080. struct v4l2_streamparm *parm)
  1081. {
  1082. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1083. return v4l2_subdev_call(sd, video, s_parm, parm);
  1084. }
  1085. static int default_enum_framesizes(struct soc_camera_device *icd,
  1086. struct v4l2_frmsizeenum *fsize)
  1087. {
  1088. int ret;
  1089. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1090. const struct soc_camera_format_xlate *xlate;
  1091. __u32 pixfmt = fsize->pixel_format;
  1092. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1093. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1094. if (!xlate)
  1095. return -EINVAL;
  1096. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1097. fsize_mbus.pixel_format = xlate->code;
  1098. ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
  1099. if (ret < 0)
  1100. return ret;
  1101. *fsize = fsize_mbus;
  1102. fsize->pixel_format = pixfmt;
  1103. return 0;
  1104. }
  1105. int soc_camera_host_register(struct soc_camera_host *ici)
  1106. {
  1107. struct soc_camera_host *ix;
  1108. int ret;
  1109. if (!ici || !ici->ops ||
  1110. !ici->ops->try_fmt ||
  1111. !ici->ops->set_fmt ||
  1112. !ici->ops->set_bus_param ||
  1113. !ici->ops->querycap ||
  1114. ((!ici->ops->init_videobuf ||
  1115. !ici->ops->reqbufs) &&
  1116. !ici->ops->init_videobuf2) ||
  1117. !ici->ops->add ||
  1118. !ici->ops->remove ||
  1119. !ici->ops->poll ||
  1120. !ici->v4l2_dev.dev)
  1121. return -EINVAL;
  1122. if (!ici->ops->set_crop)
  1123. ici->ops->set_crop = default_s_crop;
  1124. if (!ici->ops->get_crop)
  1125. ici->ops->get_crop = default_g_crop;
  1126. if (!ici->ops->cropcap)
  1127. ici->ops->cropcap = default_cropcap;
  1128. if (!ici->ops->set_parm)
  1129. ici->ops->set_parm = default_s_parm;
  1130. if (!ici->ops->get_parm)
  1131. ici->ops->get_parm = default_g_parm;
  1132. if (!ici->ops->enum_framesizes)
  1133. ici->ops->enum_framesizes = default_enum_framesizes;
  1134. mutex_lock(&list_lock);
  1135. list_for_each_entry(ix, &hosts, list) {
  1136. if (ix->nr == ici->nr) {
  1137. ret = -EBUSY;
  1138. goto edevreg;
  1139. }
  1140. }
  1141. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1142. if (ret < 0)
  1143. goto edevreg;
  1144. list_add_tail(&ici->list, &hosts);
  1145. mutex_unlock(&list_lock);
  1146. mutex_init(&ici->host_lock);
  1147. scan_add_host(ici);
  1148. return 0;
  1149. edevreg:
  1150. mutex_unlock(&list_lock);
  1151. return ret;
  1152. }
  1153. EXPORT_SYMBOL(soc_camera_host_register);
  1154. /* Unregister all clients! */
  1155. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1156. {
  1157. struct soc_camera_device *icd;
  1158. mutex_lock(&list_lock);
  1159. list_del(&ici->list);
  1160. list_for_each_entry(icd, &devices, list)
  1161. if (icd->iface == ici->nr && to_soc_camera_control(icd))
  1162. soc_camera_remove(icd);
  1163. mutex_unlock(&list_lock);
  1164. v4l2_device_unregister(&ici->v4l2_dev);
  1165. }
  1166. EXPORT_SYMBOL(soc_camera_host_unregister);
  1167. /* Image capture device */
  1168. static int soc_camera_device_register(struct soc_camera_device *icd)
  1169. {
  1170. struct soc_camera_device *ix;
  1171. int num = -1, i;
  1172. for (i = 0; i < 256 && num < 0; i++) {
  1173. num = i;
  1174. /* Check if this index is available on this interface */
  1175. list_for_each_entry(ix, &devices, list) {
  1176. if (ix->iface == icd->iface && ix->devnum == i) {
  1177. num = -1;
  1178. break;
  1179. }
  1180. }
  1181. }
  1182. if (num < 0)
  1183. /*
  1184. * ok, we have 256 cameras on this host...
  1185. * man, stay reasonable...
  1186. */
  1187. return -ENOMEM;
  1188. icd->devnum = num;
  1189. icd->use_count = 0;
  1190. icd->host_priv = NULL;
  1191. list_add_tail(&icd->list, &devices);
  1192. return 0;
  1193. }
  1194. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1195. .vidioc_querycap = soc_camera_querycap,
  1196. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1197. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1198. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1199. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1200. .vidioc_enum_input = soc_camera_enum_input,
  1201. .vidioc_g_input = soc_camera_g_input,
  1202. .vidioc_s_input = soc_camera_s_input,
  1203. .vidioc_s_std = soc_camera_s_std,
  1204. .vidioc_g_std = soc_camera_g_std,
  1205. .vidioc_enum_framesizes = soc_camera_enum_framesizes,
  1206. .vidioc_reqbufs = soc_camera_reqbufs,
  1207. .vidioc_querybuf = soc_camera_querybuf,
  1208. .vidioc_qbuf = soc_camera_qbuf,
  1209. .vidioc_dqbuf = soc_camera_dqbuf,
  1210. .vidioc_create_bufs = soc_camera_create_bufs,
  1211. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1212. .vidioc_streamon = soc_camera_streamon,
  1213. .vidioc_streamoff = soc_camera_streamoff,
  1214. .vidioc_cropcap = soc_camera_cropcap,
  1215. .vidioc_g_crop = soc_camera_g_crop,
  1216. .vidioc_s_crop = soc_camera_s_crop,
  1217. .vidioc_g_selection = soc_camera_g_selection,
  1218. .vidioc_s_selection = soc_camera_s_selection,
  1219. .vidioc_g_parm = soc_camera_g_parm,
  1220. .vidioc_s_parm = soc_camera_s_parm,
  1221. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1222. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1223. .vidioc_g_register = soc_camera_g_register,
  1224. .vidioc_s_register = soc_camera_s_register,
  1225. #endif
  1226. };
  1227. static int video_dev_create(struct soc_camera_device *icd)
  1228. {
  1229. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1230. struct video_device *vdev = video_device_alloc();
  1231. if (!vdev)
  1232. return -ENOMEM;
  1233. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1234. vdev->parent = icd->pdev;
  1235. vdev->current_norm = V4L2_STD_UNKNOWN;
  1236. vdev->fops = &soc_camera_fops;
  1237. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1238. vdev->release = video_device_release;
  1239. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1240. vdev->ctrl_handler = &icd->ctrl_handler;
  1241. vdev->lock = &ici->host_lock;
  1242. icd->vdev = vdev;
  1243. return 0;
  1244. }
  1245. /*
  1246. * Called from soc_camera_probe() above with .host_lock held
  1247. */
  1248. static int soc_camera_video_start(struct soc_camera_device *icd)
  1249. {
  1250. const struct device_type *type = icd->vdev->dev.type;
  1251. int ret;
  1252. if (!icd->parent)
  1253. return -ENODEV;
  1254. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1255. if (ret < 0) {
  1256. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1257. return ret;
  1258. }
  1259. /* Restore device type, possibly set by the subdevice driver */
  1260. icd->vdev->dev.type = type;
  1261. return 0;
  1262. }
  1263. static int soc_camera_pdrv_probe(struct platform_device *pdev)
  1264. {
  1265. struct soc_camera_desc *sdesc = pdev->dev.platform_data;
  1266. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  1267. struct soc_camera_device *icd;
  1268. int ret;
  1269. if (!sdesc)
  1270. return -EINVAL;
  1271. icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
  1272. if (!icd)
  1273. return -ENOMEM;
  1274. ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
  1275. ssdd->regulators);
  1276. if (ret < 0)
  1277. return ret;
  1278. icd->iface = sdesc->host_desc.bus_id;
  1279. icd->sdesc = sdesc;
  1280. icd->pdev = &pdev->dev;
  1281. platform_set_drvdata(pdev, icd);
  1282. icd->user_width = DEFAULT_WIDTH;
  1283. icd->user_height = DEFAULT_HEIGHT;
  1284. return soc_camera_device_register(icd);
  1285. }
  1286. /*
  1287. * Only called on rmmod for each platform device, since they are not
  1288. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1289. * been unloaded already
  1290. */
  1291. static int soc_camera_pdrv_remove(struct platform_device *pdev)
  1292. {
  1293. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1294. if (!icd)
  1295. return -EINVAL;
  1296. list_del(&icd->list);
  1297. return 0;
  1298. }
  1299. static struct platform_driver __refdata soc_camera_pdrv = {
  1300. .probe = soc_camera_pdrv_probe,
  1301. .remove = soc_camera_pdrv_remove,
  1302. .driver = {
  1303. .name = "soc-camera-pdrv",
  1304. .owner = THIS_MODULE,
  1305. },
  1306. };
  1307. module_platform_driver(soc_camera_pdrv);
  1308. MODULE_DESCRIPTION("Image capture bus driver");
  1309. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1310. MODULE_LICENSE("GPL");
  1311. MODULE_ALIAS("platform:soc-camera-pdrv");