soc_camera.c 40 KB

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