soc_camera.c 39 KB

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