soc_camera.c 40 KB

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