soc_camera.c 40 KB

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