soc_camera.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include <media/soc_camera.h>
  31. #include <media/soc_mediabus.h>
  32. #include <media/v4l2-async.h>
  33. #include <media/v4l2-clk.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-dev.h>
  37. #include <media/videobuf-core.h>
  38. #include <media/videobuf2-core.h>
  39. /* Default to VGA resolution */
  40. #define DEFAULT_WIDTH 640
  41. #define DEFAULT_HEIGHT 480
  42. #define is_streaming(ici, icd) \
  43. (((ici)->ops->init_videobuf) ? \
  44. (icd)->vb_vidq.streaming : \
  45. vb2_is_streaming(&(icd)->vb2_vidq))
  46. #define MAP_MAX_NUM 32
  47. static DECLARE_BITMAP(device_map, MAP_MAX_NUM);
  48. static LIST_HEAD(hosts);
  49. static LIST_HEAD(devices);
  50. /*
  51. * Protects lists and bitmaps of hosts and devices.
  52. * Lock nesting: Ok to take ->host_lock under list_lock.
  53. */
  54. static DEFINE_MUTEX(list_lock);
  55. struct soc_camera_async_client {
  56. struct v4l2_async_subdev *sensor;
  57. struct v4l2_async_notifier notifier;
  58. struct platform_device *pdev;
  59. struct list_head list; /* needed for clean up */
  60. };
  61. static int soc_camera_video_start(struct soc_camera_device *icd);
  62. static int video_dev_create(struct soc_camera_device *icd);
  63. int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd,
  64. struct v4l2_clk *clk)
  65. {
  66. int ret = clk ? v4l2_clk_enable(clk) : 0;
  67. if (ret < 0) {
  68. dev_err(dev, "Cannot enable clock: %d\n", ret);
  69. return ret;
  70. }
  71. ret = regulator_bulk_enable(ssdd->num_regulators,
  72. ssdd->regulators);
  73. if (ret < 0) {
  74. dev_err(dev, "Cannot enable regulators\n");
  75. goto eregenable;
  76. }
  77. if (ssdd->power) {
  78. ret = ssdd->power(dev, 1);
  79. if (ret < 0) {
  80. dev_err(dev,
  81. "Platform failed to power-on the camera.\n");
  82. goto epwron;
  83. }
  84. }
  85. return 0;
  86. epwron:
  87. regulator_bulk_disable(ssdd->num_regulators,
  88. ssdd->regulators);
  89. eregenable:
  90. if (clk)
  91. v4l2_clk_disable(clk);
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(soc_camera_power_on);
  95. int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,
  96. struct v4l2_clk *clk)
  97. {
  98. int ret = 0;
  99. int err;
  100. if (ssdd->power) {
  101. err = ssdd->power(dev, 0);
  102. if (err < 0) {
  103. dev_err(dev,
  104. "Platform failed to power-off the camera.\n");
  105. ret = err;
  106. }
  107. }
  108. err = regulator_bulk_disable(ssdd->num_regulators,
  109. ssdd->regulators);
  110. if (err < 0) {
  111. dev_err(dev, "Cannot disable regulators\n");
  112. ret = ret ? : err;
  113. }
  114. if (clk)
  115. v4l2_clk_disable(clk);
  116. return ret;
  117. }
  118. EXPORT_SYMBOL(soc_camera_power_off);
  119. int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd)
  120. {
  121. return devm_regulator_bulk_get(dev, ssdd->num_regulators,
  122. ssdd->regulators);
  123. }
  124. EXPORT_SYMBOL(soc_camera_power_init);
  125. static int __soc_camera_power_on(struct soc_camera_device *icd)
  126. {
  127. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  128. int ret;
  129. ret = v4l2_subdev_call(sd, core, s_power, 1);
  130. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  131. return ret;
  132. return 0;
  133. }
  134. static int __soc_camera_power_off(struct soc_camera_device *icd)
  135. {
  136. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  137. int ret;
  138. ret = v4l2_subdev_call(sd, core, s_power, 0);
  139. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  140. return ret;
  141. return 0;
  142. }
  143. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  144. struct soc_camera_device *icd, unsigned int fourcc)
  145. {
  146. unsigned int i;
  147. for (i = 0; i < icd->num_user_formats; i++)
  148. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  149. return icd->user_formats + i;
  150. return NULL;
  151. }
  152. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  153. /**
  154. * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  155. * @ssdd: camera platform parameters
  156. * @cfg: media bus configuration
  157. * @return: resulting flags
  158. */
  159. unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
  160. const struct v4l2_mbus_config *cfg)
  161. {
  162. unsigned long f, flags = cfg->flags;
  163. /* If only one of the two polarities is supported, switch to the opposite */
  164. if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  165. f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
  166. if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
  167. flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
  168. }
  169. if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  170. f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
  171. if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
  172. flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
  173. }
  174. if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
  175. f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
  176. if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
  177. flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  178. }
  179. return flags;
  180. }
  181. EXPORT_SYMBOL(soc_camera_apply_board_flags);
  182. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  183. ((x) >> 24) & 0xff
  184. static int soc_camera_try_fmt(struct soc_camera_device *icd,
  185. struct v4l2_format *f)
  186. {
  187. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  188. const struct soc_camera_format_xlate *xlate;
  189. struct v4l2_pix_format *pix = &f->fmt.pix;
  190. int ret;
  191. dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
  192. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  193. if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
  194. !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
  195. pix->bytesperline = 0;
  196. pix->sizeimage = 0;
  197. }
  198. ret = ici->ops->try_fmt(icd, f);
  199. if (ret < 0)
  200. return ret;
  201. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  202. if (!xlate)
  203. return -EINVAL;
  204. ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
  205. if (ret < 0)
  206. return ret;
  207. pix->bytesperline = max_t(u32, pix->bytesperline, ret);
  208. ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
  209. pix->height);
  210. if (ret < 0)
  211. return ret;
  212. pix->sizeimage = max_t(u32, pix->sizeimage, ret);
  213. return 0;
  214. }
  215. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  216. struct v4l2_format *f)
  217. {
  218. struct soc_camera_device *icd = file->private_data;
  219. WARN_ON(priv != file->private_data);
  220. /* Only single-plane capture is supported so far */
  221. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  222. return -EINVAL;
  223. /* limit format to hardware capabilities */
  224. return soc_camera_try_fmt(icd, f);
  225. }
  226. static int soc_camera_enum_input(struct file *file, void *priv,
  227. struct v4l2_input *inp)
  228. {
  229. if (inp->index != 0)
  230. return -EINVAL;
  231. /* default is camera */
  232. inp->type = V4L2_INPUT_TYPE_CAMERA;
  233. strcpy(inp->name, "Camera");
  234. return 0;
  235. }
  236. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  237. {
  238. *i = 0;
  239. return 0;
  240. }
  241. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  242. {
  243. if (i > 0)
  244. return -EINVAL;
  245. return 0;
  246. }
  247. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
  248. {
  249. struct soc_camera_device *icd = file->private_data;
  250. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  251. return v4l2_subdev_call(sd, core, s_std, a);
  252. }
  253. static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
  254. {
  255. struct soc_camera_device *icd = file->private_data;
  256. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  257. return v4l2_subdev_call(sd, core, g_std, a);
  258. }
  259. static int soc_camera_enum_framesizes(struct file *file, void *fh,
  260. struct v4l2_frmsizeenum *fsize)
  261. {
  262. struct soc_camera_device *icd = file->private_data;
  263. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  264. return ici->ops->enum_framesizes(icd, fsize);
  265. }
  266. static int soc_camera_reqbufs(struct file *file, void *priv,
  267. struct v4l2_requestbuffers *p)
  268. {
  269. int ret;
  270. struct soc_camera_device *icd = file->private_data;
  271. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  272. WARN_ON(priv != file->private_data);
  273. if (icd->streamer && icd->streamer != file)
  274. return -EBUSY;
  275. if (ici->ops->init_videobuf) {
  276. ret = videobuf_reqbufs(&icd->vb_vidq, p);
  277. if (ret < 0)
  278. return ret;
  279. ret = ici->ops->reqbufs(icd, p);
  280. } else {
  281. ret = vb2_reqbufs(&icd->vb2_vidq, p);
  282. }
  283. if (!ret && !icd->streamer)
  284. icd->streamer = file;
  285. return ret;
  286. }
  287. static int soc_camera_querybuf(struct file *file, void *priv,
  288. struct v4l2_buffer *p)
  289. {
  290. struct soc_camera_device *icd = file->private_data;
  291. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  292. WARN_ON(priv != file->private_data);
  293. if (ici->ops->init_videobuf)
  294. return videobuf_querybuf(&icd->vb_vidq, p);
  295. else
  296. return vb2_querybuf(&icd->vb2_vidq, p);
  297. }
  298. static int soc_camera_qbuf(struct file *file, void *priv,
  299. struct v4l2_buffer *p)
  300. {
  301. struct soc_camera_device *icd = file->private_data;
  302. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  303. WARN_ON(priv != file->private_data);
  304. if (icd->streamer != file)
  305. return -EBUSY;
  306. if (ici->ops->init_videobuf)
  307. return videobuf_qbuf(&icd->vb_vidq, p);
  308. else
  309. return vb2_qbuf(&icd->vb2_vidq, p);
  310. }
  311. static int soc_camera_dqbuf(struct file *file, void *priv,
  312. struct v4l2_buffer *p)
  313. {
  314. struct soc_camera_device *icd = file->private_data;
  315. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  316. WARN_ON(priv != file->private_data);
  317. if (icd->streamer != file)
  318. return -EBUSY;
  319. if (ici->ops->init_videobuf)
  320. return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
  321. else
  322. return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
  323. }
  324. static int soc_camera_create_bufs(struct file *file, void *priv,
  325. struct v4l2_create_buffers *create)
  326. {
  327. struct soc_camera_device *icd = file->private_data;
  328. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  329. /* videobuf2 only */
  330. if (ici->ops->init_videobuf)
  331. return -EINVAL;
  332. else
  333. return vb2_create_bufs(&icd->vb2_vidq, create);
  334. }
  335. static int soc_camera_prepare_buf(struct file *file, void *priv,
  336. struct v4l2_buffer *b)
  337. {
  338. struct soc_camera_device *icd = file->private_data;
  339. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  340. /* videobuf2 only */
  341. if (ici->ops->init_videobuf)
  342. return -EINVAL;
  343. else
  344. return vb2_prepare_buf(&icd->vb2_vidq, b);
  345. }
  346. /* Always entered with .host_lock held */
  347. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  348. {
  349. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  350. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  351. unsigned int i, fmts = 0, raw_fmts = 0;
  352. int ret;
  353. enum v4l2_mbus_pixelcode code;
  354. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  355. raw_fmts++;
  356. if (!ici->ops->get_formats)
  357. /*
  358. * Fallback mode - the host will have to serve all
  359. * sensor-provided formats one-to-one to the user
  360. */
  361. fmts = raw_fmts;
  362. else
  363. /*
  364. * First pass - only count formats this host-sensor
  365. * configuration can provide
  366. */
  367. for (i = 0; i < raw_fmts; i++) {
  368. ret = ici->ops->get_formats(icd, i, NULL);
  369. if (ret < 0)
  370. return ret;
  371. fmts += ret;
  372. }
  373. if (!fmts)
  374. return -ENXIO;
  375. icd->user_formats =
  376. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  377. if (!icd->user_formats)
  378. return -ENOMEM;
  379. dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
  380. /* Second pass - actually fill data formats */
  381. fmts = 0;
  382. for (i = 0; i < raw_fmts; i++)
  383. if (!ici->ops->get_formats) {
  384. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  385. icd->user_formats[fmts].host_fmt =
  386. soc_mbus_get_fmtdesc(code);
  387. if (icd->user_formats[fmts].host_fmt)
  388. icd->user_formats[fmts++].code = code;
  389. } else {
  390. ret = ici->ops->get_formats(icd, i,
  391. &icd->user_formats[fmts]);
  392. if (ret < 0)
  393. goto egfmt;
  394. fmts += ret;
  395. }
  396. icd->num_user_formats = fmts;
  397. icd->current_fmt = &icd->user_formats[0];
  398. return 0;
  399. egfmt:
  400. vfree(icd->user_formats);
  401. return ret;
  402. }
  403. /* Always entered with .host_lock held */
  404. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  405. {
  406. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  407. if (ici->ops->put_formats)
  408. ici->ops->put_formats(icd);
  409. icd->current_fmt = NULL;
  410. icd->num_user_formats = 0;
  411. vfree(icd->user_formats);
  412. icd->user_formats = NULL;
  413. }
  414. /* Called with .vb_lock held, or from the first open(2), see comment there */
  415. static int soc_camera_set_fmt(struct soc_camera_device *icd,
  416. struct v4l2_format *f)
  417. {
  418. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  419. struct v4l2_pix_format *pix = &f->fmt.pix;
  420. int ret;
  421. dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
  422. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  423. /* We always call try_fmt() before set_fmt() or set_crop() */
  424. ret = soc_camera_try_fmt(icd, f);
  425. if (ret < 0)
  426. return ret;
  427. ret = ici->ops->set_fmt(icd, f);
  428. if (ret < 0) {
  429. return ret;
  430. } else if (!icd->current_fmt ||
  431. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  432. dev_err(icd->pdev,
  433. "Host driver hasn't set up current format correctly!\n");
  434. return -EINVAL;
  435. }
  436. icd->user_width = pix->width;
  437. icd->user_height = pix->height;
  438. icd->bytesperline = pix->bytesperline;
  439. icd->sizeimage = pix->sizeimage;
  440. icd->colorspace = pix->colorspace;
  441. icd->field = pix->field;
  442. if (ici->ops->init_videobuf)
  443. icd->vb_vidq.field = pix->field;
  444. dev_dbg(icd->pdev, "set width: %d height: %d\n",
  445. icd->user_width, icd->user_height);
  446. /* set physical bus parameters */
  447. return ici->ops->set_bus_param(icd);
  448. }
  449. static int soc_camera_add_device(struct soc_camera_device *icd)
  450. {
  451. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  452. int ret;
  453. if (ici->icd)
  454. return -EBUSY;
  455. if (!icd->clk) {
  456. mutex_lock(&ici->clk_lock);
  457. ret = ici->ops->clock_start(ici);
  458. mutex_unlock(&ici->clk_lock);
  459. if (ret < 0)
  460. return ret;
  461. }
  462. if (ici->ops->add) {
  463. ret = ici->ops->add(icd);
  464. if (ret < 0)
  465. goto eadd;
  466. }
  467. ici->icd = icd;
  468. return 0;
  469. eadd:
  470. if (!icd->clk) {
  471. mutex_lock(&ici->clk_lock);
  472. ici->ops->clock_stop(ici);
  473. mutex_unlock(&ici->clk_lock);
  474. }
  475. return ret;
  476. }
  477. static void soc_camera_remove_device(struct soc_camera_device *icd)
  478. {
  479. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  480. if (WARN_ON(icd != ici->icd))
  481. return;
  482. if (ici->ops->remove)
  483. ici->ops->remove(icd);
  484. if (!icd->clk) {
  485. mutex_lock(&ici->clk_lock);
  486. ici->ops->clock_stop(ici);
  487. mutex_unlock(&ici->clk_lock);
  488. }
  489. ici->icd = NULL;
  490. }
  491. static int soc_camera_open(struct file *file)
  492. {
  493. struct video_device *vdev = video_devdata(file);
  494. struct soc_camera_device *icd;
  495. struct soc_camera_host *ici;
  496. int ret;
  497. /*
  498. * Don't mess with the host during probe: wait until the loop in
  499. * scan_add_host() completes. Also protect against a race with
  500. * soc_camera_host_unregister().
  501. */
  502. if (mutex_lock_interruptible(&list_lock))
  503. return -ERESTARTSYS;
  504. if (!vdev || !video_is_registered(vdev)) {
  505. mutex_unlock(&list_lock);
  506. return -ENODEV;
  507. }
  508. icd = video_get_drvdata(vdev);
  509. ici = to_soc_camera_host(icd->parent);
  510. ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
  511. mutex_unlock(&list_lock);
  512. if (ret < 0) {
  513. dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
  514. return ret;
  515. }
  516. if (!to_soc_camera_control(icd)) {
  517. /* No device driver attached */
  518. ret = -ENODEV;
  519. goto econtrol;
  520. }
  521. if (mutex_lock_interruptible(&ici->host_lock)) {
  522. ret = -ERESTARTSYS;
  523. goto elockhost;
  524. }
  525. icd->use_count++;
  526. /* Now we really have to activate the camera */
  527. if (icd->use_count == 1) {
  528. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  529. /* Restore parameters before the last close() per V4L2 API */
  530. struct v4l2_format f = {
  531. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  532. .fmt.pix = {
  533. .width = icd->user_width,
  534. .height = icd->user_height,
  535. .field = icd->field,
  536. .colorspace = icd->colorspace,
  537. .pixelformat =
  538. icd->current_fmt->host_fmt->fourcc,
  539. },
  540. };
  541. /* The camera could have been already on, try to reset */
  542. if (sdesc->subdev_desc.reset)
  543. sdesc->subdev_desc.reset(icd->pdev);
  544. ret = soc_camera_add_device(icd);
  545. if (ret < 0) {
  546. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  547. goto eiciadd;
  548. }
  549. ret = __soc_camera_power_on(icd);
  550. if (ret < 0)
  551. goto epower;
  552. pm_runtime_enable(&icd->vdev->dev);
  553. ret = pm_runtime_resume(&icd->vdev->dev);
  554. if (ret < 0 && ret != -ENOSYS)
  555. goto eresume;
  556. /*
  557. * Try to configure with default parameters. Notice: this is the
  558. * very first open, so, we cannot race against other calls,
  559. * apart from someone else calling open() simultaneously, but
  560. * .host_lock is protecting us against it.
  561. */
  562. ret = soc_camera_set_fmt(icd, &f);
  563. if (ret < 0)
  564. goto esfmt;
  565. if (ici->ops->init_videobuf) {
  566. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  567. } else {
  568. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  569. if (ret < 0)
  570. goto einitvb;
  571. }
  572. v4l2_ctrl_handler_setup(&icd->ctrl_handler);
  573. }
  574. mutex_unlock(&ici->host_lock);
  575. file->private_data = icd;
  576. dev_dbg(icd->pdev, "camera device open\n");
  577. return 0;
  578. /*
  579. * All errors are entered with the .host_lock held, first four also
  580. * with use_count == 1
  581. */
  582. einitvb:
  583. esfmt:
  584. pm_runtime_disable(&icd->vdev->dev);
  585. eresume:
  586. __soc_camera_power_off(icd);
  587. epower:
  588. soc_camera_remove_device(icd);
  589. eiciadd:
  590. icd->use_count--;
  591. mutex_unlock(&ici->host_lock);
  592. elockhost:
  593. econtrol:
  594. module_put(ici->ops->owner);
  595. return ret;
  596. }
  597. static int soc_camera_close(struct file *file)
  598. {
  599. struct soc_camera_device *icd = file->private_data;
  600. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  601. mutex_lock(&ici->host_lock);
  602. icd->use_count--;
  603. if (!icd->use_count) {
  604. pm_runtime_suspend(&icd->vdev->dev);
  605. pm_runtime_disable(&icd->vdev->dev);
  606. if (ici->ops->init_videobuf2)
  607. vb2_queue_release(&icd->vb2_vidq);
  608. __soc_camera_power_off(icd);
  609. soc_camera_remove_device(icd);
  610. }
  611. if (icd->streamer == file)
  612. icd->streamer = NULL;
  613. mutex_unlock(&ici->host_lock);
  614. module_put(ici->ops->owner);
  615. dev_dbg(icd->pdev, "camera device close\n");
  616. return 0;
  617. }
  618. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  619. size_t count, loff_t *ppos)
  620. {
  621. struct soc_camera_device *icd = file->private_data;
  622. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  623. dev_dbg(icd->pdev, "read called, buf %p\n", buf);
  624. if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
  625. return vb2_read(&icd->vb2_vidq, buf, count, ppos,
  626. file->f_flags & O_NONBLOCK);
  627. dev_err(icd->pdev, "camera device read not implemented\n");
  628. return -EINVAL;
  629. }
  630. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  631. {
  632. struct soc_camera_device *icd = file->private_data;
  633. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  634. int err;
  635. dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  636. if (icd->streamer != file)
  637. return -EBUSY;
  638. if (mutex_lock_interruptible(&ici->host_lock))
  639. return -ERESTARTSYS;
  640. if (ici->ops->init_videobuf)
  641. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  642. else
  643. err = vb2_mmap(&icd->vb2_vidq, vma);
  644. mutex_unlock(&ici->host_lock);
  645. dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  646. (unsigned long)vma->vm_start,
  647. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  648. err);
  649. return err;
  650. }
  651. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  652. {
  653. struct soc_camera_device *icd = file->private_data;
  654. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  655. unsigned res = POLLERR;
  656. if (icd->streamer != file)
  657. return POLLERR;
  658. mutex_lock(&ici->host_lock);
  659. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
  660. dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
  661. else
  662. res = ici->ops->poll(file, pt);
  663. mutex_unlock(&ici->host_lock);
  664. return res;
  665. }
  666. void soc_camera_lock(struct vb2_queue *vq)
  667. {
  668. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  669. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  670. mutex_lock(&ici->host_lock);
  671. }
  672. EXPORT_SYMBOL(soc_camera_lock);
  673. void soc_camera_unlock(struct vb2_queue *vq)
  674. {
  675. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  676. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  677. mutex_unlock(&ici->host_lock);
  678. }
  679. EXPORT_SYMBOL(soc_camera_unlock);
  680. static struct v4l2_file_operations soc_camera_fops = {
  681. .owner = THIS_MODULE,
  682. .open = soc_camera_open,
  683. .release = soc_camera_close,
  684. .unlocked_ioctl = video_ioctl2,
  685. .read = soc_camera_read,
  686. .mmap = soc_camera_mmap,
  687. .poll = soc_camera_poll,
  688. };
  689. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  690. struct v4l2_format *f)
  691. {
  692. struct soc_camera_device *icd = file->private_data;
  693. int ret;
  694. WARN_ON(priv != file->private_data);
  695. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  696. dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
  697. return -EINVAL;
  698. }
  699. if (icd->streamer && icd->streamer != file)
  700. return -EBUSY;
  701. if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
  702. dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
  703. return -EBUSY;
  704. }
  705. ret = soc_camera_set_fmt(icd, f);
  706. if (!ret && !icd->streamer)
  707. icd->streamer = file;
  708. return ret;
  709. }
  710. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  711. struct v4l2_fmtdesc *f)
  712. {
  713. struct soc_camera_device *icd = file->private_data;
  714. const struct soc_mbus_pixelfmt *format;
  715. WARN_ON(priv != file->private_data);
  716. if (f->index >= icd->num_user_formats)
  717. return -EINVAL;
  718. format = icd->user_formats[f->index].host_fmt;
  719. if (format->name)
  720. strlcpy(f->description, format->name, sizeof(f->description));
  721. f->pixelformat = format->fourcc;
  722. return 0;
  723. }
  724. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  725. struct v4l2_format *f)
  726. {
  727. struct soc_camera_device *icd = file->private_data;
  728. struct v4l2_pix_format *pix = &f->fmt.pix;
  729. WARN_ON(priv != file->private_data);
  730. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  731. return -EINVAL;
  732. pix->width = icd->user_width;
  733. pix->height = icd->user_height;
  734. pix->bytesperline = icd->bytesperline;
  735. pix->sizeimage = icd->sizeimage;
  736. pix->field = icd->field;
  737. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  738. pix->colorspace = icd->colorspace;
  739. dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
  740. icd->current_fmt->host_fmt->fourcc);
  741. return 0;
  742. }
  743. static int soc_camera_querycap(struct file *file, void *priv,
  744. struct v4l2_capability *cap)
  745. {
  746. struct soc_camera_device *icd = file->private_data;
  747. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  748. WARN_ON(priv != file->private_data);
  749. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  750. return ici->ops->querycap(ici, cap);
  751. }
  752. static int soc_camera_streamon(struct file *file, void *priv,
  753. enum v4l2_buf_type i)
  754. {
  755. struct soc_camera_device *icd = file->private_data;
  756. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  757. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  758. int ret;
  759. WARN_ON(priv != file->private_data);
  760. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  761. return -EINVAL;
  762. if (icd->streamer != file)
  763. return -EBUSY;
  764. /* This calls buf_queue from host driver's videobuf_queue_ops */
  765. if (ici->ops->init_videobuf)
  766. ret = videobuf_streamon(&icd->vb_vidq);
  767. else
  768. ret = vb2_streamon(&icd->vb2_vidq, i);
  769. if (!ret)
  770. v4l2_subdev_call(sd, video, s_stream, 1);
  771. return ret;
  772. }
  773. static int soc_camera_streamoff(struct file *file, void *priv,
  774. enum v4l2_buf_type i)
  775. {
  776. struct soc_camera_device *icd = file->private_data;
  777. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  778. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  779. WARN_ON(priv != file->private_data);
  780. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  781. return -EINVAL;
  782. if (icd->streamer != file)
  783. return -EBUSY;
  784. /*
  785. * This calls buf_release from host driver's videobuf_queue_ops for all
  786. * remaining buffers. When the last buffer is freed, stop capture
  787. */
  788. if (ici->ops->init_videobuf)
  789. videobuf_streamoff(&icd->vb_vidq);
  790. else
  791. vb2_streamoff(&icd->vb2_vidq, i);
  792. v4l2_subdev_call(sd, video, s_stream, 0);
  793. return 0;
  794. }
  795. static int soc_camera_cropcap(struct file *file, void *fh,
  796. struct v4l2_cropcap *a)
  797. {
  798. struct soc_camera_device *icd = file->private_data;
  799. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  800. return ici->ops->cropcap(icd, a);
  801. }
  802. static int soc_camera_g_crop(struct file *file, void *fh,
  803. struct v4l2_crop *a)
  804. {
  805. struct soc_camera_device *icd = file->private_data;
  806. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  807. int ret;
  808. ret = ici->ops->get_crop(icd, a);
  809. return ret;
  810. }
  811. /*
  812. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  813. * argument with the actual geometry, instead, the user shall use G_CROP to
  814. * retrieve it.
  815. */
  816. static int soc_camera_s_crop(struct file *file, void *fh,
  817. const struct v4l2_crop *a)
  818. {
  819. struct soc_camera_device *icd = file->private_data;
  820. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  821. const struct v4l2_rect *rect = &a->c;
  822. struct v4l2_crop current_crop;
  823. int ret;
  824. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  825. return -EINVAL;
  826. dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
  827. rect->width, rect->height, rect->left, rect->top);
  828. current_crop.type = a->type;
  829. /* If get_crop fails, we'll let host and / or client drivers decide */
  830. ret = ici->ops->get_crop(icd, &current_crop);
  831. /* Prohibit window size change with initialised buffers */
  832. if (ret < 0) {
  833. dev_err(icd->pdev,
  834. "S_CROP denied: getting current crop failed\n");
  835. } else if ((a->c.width == current_crop.c.width &&
  836. a->c.height == current_crop.c.height) ||
  837. !is_streaming(ici, icd)) {
  838. /* same size or not streaming - use .set_crop() */
  839. ret = ici->ops->set_crop(icd, a);
  840. } else if (ici->ops->set_livecrop) {
  841. ret = ici->ops->set_livecrop(icd, a);
  842. } else {
  843. dev_err(icd->pdev,
  844. "S_CROP denied: queue initialised and sizes differ\n");
  845. ret = -EBUSY;
  846. }
  847. return ret;
  848. }
  849. static int soc_camera_g_selection(struct file *file, void *fh,
  850. struct v4l2_selection *s)
  851. {
  852. struct soc_camera_device *icd = file->private_data;
  853. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  854. /* With a wrong type no need to try to fall back to cropping */
  855. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  856. return -EINVAL;
  857. if (!ici->ops->get_selection)
  858. return -ENOTTY;
  859. return ici->ops->get_selection(icd, s);
  860. }
  861. static int soc_camera_s_selection(struct file *file, void *fh,
  862. struct v4l2_selection *s)
  863. {
  864. struct soc_camera_device *icd = file->private_data;
  865. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  866. int ret;
  867. /* In all these cases cropping emulation will not help */
  868. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  869. (s->target != V4L2_SEL_TGT_COMPOSE &&
  870. s->target != V4L2_SEL_TGT_CROP))
  871. return -EINVAL;
  872. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  873. /* No output size change during a running capture! */
  874. if (is_streaming(ici, icd) &&
  875. (icd->user_width != s->r.width ||
  876. icd->user_height != s->r.height))
  877. return -EBUSY;
  878. /*
  879. * Only one user is allowed to change the output format, touch
  880. * buffers, start / stop streaming, poll for data
  881. */
  882. if (icd->streamer && icd->streamer != file)
  883. return -EBUSY;
  884. }
  885. if (!ici->ops->set_selection)
  886. return -ENOTTY;
  887. ret = ici->ops->set_selection(icd, s);
  888. if (!ret &&
  889. s->target == V4L2_SEL_TGT_COMPOSE) {
  890. icd->user_width = s->r.width;
  891. icd->user_height = s->r.height;
  892. if (!icd->streamer)
  893. icd->streamer = file;
  894. }
  895. return ret;
  896. }
  897. static int soc_camera_g_parm(struct file *file, void *fh,
  898. struct v4l2_streamparm *a)
  899. {
  900. struct soc_camera_device *icd = file->private_data;
  901. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  902. if (ici->ops->get_parm)
  903. return ici->ops->get_parm(icd, a);
  904. return -ENOIOCTLCMD;
  905. }
  906. static int soc_camera_s_parm(struct file *file, void *fh,
  907. struct v4l2_streamparm *a)
  908. {
  909. struct soc_camera_device *icd = file->private_data;
  910. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  911. if (ici->ops->set_parm)
  912. return ici->ops->set_parm(icd, a);
  913. return -ENOIOCTLCMD;
  914. }
  915. static int soc_camera_probe(struct soc_camera_host *ici,
  916. struct soc_camera_device *icd);
  917. /* So far this function cannot fail */
  918. static void scan_add_host(struct soc_camera_host *ici)
  919. {
  920. struct soc_camera_device *icd;
  921. mutex_lock(&list_lock);
  922. list_for_each_entry(icd, &devices, list)
  923. if (icd->iface == ici->nr) {
  924. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  925. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  926. /* The camera could have been already on, try to reset */
  927. if (ssdd->reset)
  928. ssdd->reset(icd->pdev);
  929. icd->parent = ici->v4l2_dev.dev;
  930. /* Ignore errors */
  931. soc_camera_probe(ici, icd);
  932. }
  933. mutex_unlock(&list_lock);
  934. }
  935. /*
  936. * It is invalid to call v4l2_clk_enable() after a successful probing
  937. * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
  938. */
  939. static int soc_camera_clk_enable(struct v4l2_clk *clk)
  940. {
  941. struct soc_camera_device *icd = clk->priv;
  942. struct soc_camera_host *ici;
  943. int ret;
  944. if (!icd || !icd->parent)
  945. return -ENODEV;
  946. ici = to_soc_camera_host(icd->parent);
  947. if (!try_module_get(ici->ops->owner))
  948. return -ENODEV;
  949. /*
  950. * If a different client is currently being probed, the host will tell
  951. * you to go
  952. */
  953. mutex_lock(&ici->clk_lock);
  954. ret = ici->ops->clock_start(ici);
  955. mutex_unlock(&ici->clk_lock);
  956. return ret;
  957. }
  958. static void soc_camera_clk_disable(struct v4l2_clk *clk)
  959. {
  960. struct soc_camera_device *icd = clk->priv;
  961. struct soc_camera_host *ici;
  962. if (!icd || !icd->parent)
  963. return;
  964. ici = to_soc_camera_host(icd->parent);
  965. mutex_lock(&ici->clk_lock);
  966. ici->ops->clock_stop(ici);
  967. mutex_unlock(&ici->clk_lock);
  968. module_put(ici->ops->owner);
  969. }
  970. /*
  971. * Eventually, it would be more logical to make the respective host the clock
  972. * owner, but then we would have to copy this struct for each ici. Besides, it
  973. * would introduce the circular dependency problem, unless we port all client
  974. * drivers to release the clock, when not in use.
  975. */
  976. static const struct v4l2_clk_ops soc_camera_clk_ops = {
  977. .owner = THIS_MODULE,
  978. .enable = soc_camera_clk_enable,
  979. .disable = soc_camera_clk_disable,
  980. };
  981. static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
  982. struct soc_camera_async_client *sasc)
  983. {
  984. struct platform_device *pdev;
  985. int ret, i;
  986. mutex_lock(&list_lock);
  987. i = find_first_zero_bit(device_map, MAP_MAX_NUM);
  988. if (i < MAP_MAX_NUM)
  989. set_bit(i, device_map);
  990. mutex_unlock(&list_lock);
  991. if (i >= MAP_MAX_NUM)
  992. return -ENOMEM;
  993. pdev = platform_device_alloc("soc-camera-pdrv", i);
  994. if (!pdev)
  995. return -ENOMEM;
  996. ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
  997. if (ret < 0) {
  998. platform_device_put(pdev);
  999. return ret;
  1000. }
  1001. sasc->pdev = pdev;
  1002. return 0;
  1003. }
  1004. static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
  1005. {
  1006. struct platform_device *pdev = sasc->pdev;
  1007. int ret;
  1008. ret = platform_device_add(pdev);
  1009. if (ret < 0 || !pdev->dev.driver)
  1010. return NULL;
  1011. return platform_get_drvdata(pdev);
  1012. }
  1013. /* Locking: called with .host_lock held */
  1014. static int soc_camera_probe_finish(struct soc_camera_device *icd)
  1015. {
  1016. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1017. struct v4l2_mbus_framefmt mf;
  1018. int ret;
  1019. sd->grp_id = soc_camera_grp_id(icd);
  1020. v4l2_set_subdev_hostdata(sd, icd);
  1021. ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
  1022. if (ret < 0)
  1023. return ret;
  1024. ret = soc_camera_add_device(icd);
  1025. if (ret < 0) {
  1026. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  1027. return ret;
  1028. }
  1029. /* At this point client .probe() should have run already */
  1030. ret = soc_camera_init_user_formats(icd);
  1031. if (ret < 0)
  1032. goto eusrfmt;
  1033. icd->field = V4L2_FIELD_ANY;
  1034. ret = soc_camera_video_start(icd);
  1035. if (ret < 0)
  1036. goto evidstart;
  1037. /* Try to improve our guess of a reasonable window format */
  1038. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  1039. icd->user_width = mf.width;
  1040. icd->user_height = mf.height;
  1041. icd->colorspace = mf.colorspace;
  1042. icd->field = mf.field;
  1043. }
  1044. soc_camera_remove_device(icd);
  1045. return 0;
  1046. evidstart:
  1047. soc_camera_free_user_formats(icd);
  1048. eusrfmt:
  1049. soc_camera_remove_device(icd);
  1050. return ret;
  1051. }
  1052. #ifdef CONFIG_I2C_BOARDINFO
  1053. static int soc_camera_i2c_init(struct soc_camera_device *icd,
  1054. struct soc_camera_desc *sdesc)
  1055. {
  1056. struct i2c_client *client;
  1057. struct soc_camera_host *ici;
  1058. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  1059. struct i2c_adapter *adap;
  1060. struct v4l2_subdev *subdev;
  1061. char clk_name[V4L2_SUBDEV_NAME_SIZE];
  1062. int ret;
  1063. /* First find out how we link the main client */
  1064. if (icd->sasc) {
  1065. /* Async non-OF probing handled by the subdevice list */
  1066. return -EPROBE_DEFER;
  1067. }
  1068. ici = to_soc_camera_host(icd->parent);
  1069. adap = i2c_get_adapter(shd->i2c_adapter_id);
  1070. if (!adap) {
  1071. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  1072. shd->i2c_adapter_id);
  1073. return -ENODEV;
  1074. }
  1075. shd->board_info->platform_data = &sdesc->subdev_desc;
  1076. snprintf(clk_name, sizeof(clk_name), "%d-%04x",
  1077. shd->i2c_adapter_id, shd->board_info->addr);
  1078. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
  1079. if (IS_ERR(icd->clk)) {
  1080. ret = PTR_ERR(icd->clk);
  1081. goto eclkreg;
  1082. }
  1083. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  1084. shd->board_info, NULL);
  1085. if (!subdev) {
  1086. ret = -ENODEV;
  1087. goto ei2cnd;
  1088. }
  1089. client = v4l2_get_subdevdata(subdev);
  1090. /* Use to_i2c_client(dev) to recover the i2c client */
  1091. icd->control = &client->dev;
  1092. return 0;
  1093. ei2cnd:
  1094. v4l2_clk_unregister(icd->clk);
  1095. eclkreg:
  1096. icd->clk = NULL;
  1097. i2c_put_adapter(adap);
  1098. return ret;
  1099. }
  1100. static void soc_camera_i2c_free(struct soc_camera_device *icd)
  1101. {
  1102. struct i2c_client *client =
  1103. to_i2c_client(to_soc_camera_control(icd));
  1104. struct i2c_adapter *adap;
  1105. icd->control = NULL;
  1106. if (icd->sasc)
  1107. return;
  1108. adap = client->adapter;
  1109. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  1110. i2c_unregister_device(client);
  1111. i2c_put_adapter(adap);
  1112. v4l2_clk_unregister(icd->clk);
  1113. icd->clk = NULL;
  1114. }
  1115. /*
  1116. * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
  1117. * internal global mutex, therefore cannot race against other asynchronous
  1118. * events. Until notifier->complete() (soc_camera_async_complete()) is called,
  1119. * the video device node is not registered and no V4L fops can occur. Unloading
  1120. * of the host driver also calls a v4l2-async function, so also there we're
  1121. * protected.
  1122. */
  1123. static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
  1124. struct v4l2_subdev *sd,
  1125. struct v4l2_async_subdev *asd)
  1126. {
  1127. struct soc_camera_async_client *sasc = container_of(notifier,
  1128. struct soc_camera_async_client, notifier);
  1129. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1130. if (asd == sasc->sensor && !WARN_ON(icd->control)) {
  1131. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1132. /*
  1133. * Only now we get subdevice-specific information like
  1134. * regulators, flags, callbacks, etc.
  1135. */
  1136. if (client) {
  1137. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1138. struct soc_camera_subdev_desc *ssdd =
  1139. soc_camera_i2c_to_desc(client);
  1140. if (ssdd) {
  1141. memcpy(&sdesc->subdev_desc, ssdd,
  1142. sizeof(sdesc->subdev_desc));
  1143. if (ssdd->reset)
  1144. ssdd->reset(icd->pdev);
  1145. }
  1146. icd->control = &client->dev;
  1147. }
  1148. }
  1149. return 0;
  1150. }
  1151. static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
  1152. struct v4l2_subdev *sd,
  1153. struct v4l2_async_subdev *asd)
  1154. {
  1155. struct soc_camera_async_client *sasc = container_of(notifier,
  1156. struct soc_camera_async_client, notifier);
  1157. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1158. if (icd->clk) {
  1159. v4l2_clk_unregister(icd->clk);
  1160. icd->clk = NULL;
  1161. }
  1162. }
  1163. static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
  1164. {
  1165. struct soc_camera_async_client *sasc = container_of(notifier,
  1166. struct soc_camera_async_client, notifier);
  1167. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1168. if (to_soc_camera_control(icd)) {
  1169. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1170. int ret;
  1171. mutex_lock(&list_lock);
  1172. ret = soc_camera_probe(ici, icd);
  1173. mutex_unlock(&list_lock);
  1174. if (ret < 0)
  1175. return ret;
  1176. }
  1177. return 0;
  1178. }
  1179. static int scan_async_group(struct soc_camera_host *ici,
  1180. struct v4l2_async_subdev **asd, unsigned int size)
  1181. {
  1182. struct soc_camera_async_subdev *sasd;
  1183. struct soc_camera_async_client *sasc;
  1184. struct soc_camera_device *icd;
  1185. struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
  1186. char clk_name[V4L2_SUBDEV_NAME_SIZE];
  1187. int ret, i;
  1188. /* First look for a sensor */
  1189. for (i = 0; i < size; i++) {
  1190. sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
  1191. if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
  1192. break;
  1193. }
  1194. if (i == size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
  1195. /* All useless */
  1196. dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
  1197. return -ENODEV;
  1198. }
  1199. /* Or shall this be managed by the soc-camera device? */
  1200. sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
  1201. if (!sasc)
  1202. return -ENOMEM;
  1203. /* HACK: just need a != NULL */
  1204. sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
  1205. ret = soc_camera_dyn_pdev(&sdesc, sasc);
  1206. if (ret < 0)
  1207. return ret;
  1208. sasc->sensor = &sasd->asd;
  1209. icd = soc_camera_add_pdev(sasc);
  1210. if (!icd) {
  1211. platform_device_put(sasc->pdev);
  1212. return -ENOMEM;
  1213. }
  1214. sasc->notifier.subdev = asd;
  1215. sasc->notifier.num_subdevs = size;
  1216. sasc->notifier.bound = soc_camera_async_bound;
  1217. sasc->notifier.unbind = soc_camera_async_unbind;
  1218. sasc->notifier.complete = soc_camera_async_complete;
  1219. icd->sasc = sasc;
  1220. icd->parent = ici->v4l2_dev.dev;
  1221. snprintf(clk_name, sizeof(clk_name), "%d-%04x",
  1222. sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
  1223. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd);
  1224. if (IS_ERR(icd->clk)) {
  1225. ret = PTR_ERR(icd->clk);
  1226. goto eclkreg;
  1227. }
  1228. ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
  1229. if (!ret)
  1230. return 0;
  1231. v4l2_clk_unregister(icd->clk);
  1232. eclkreg:
  1233. icd->clk = NULL;
  1234. platform_device_unregister(sasc->pdev);
  1235. dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
  1236. return ret;
  1237. }
  1238. static void scan_async_host(struct soc_camera_host *ici)
  1239. {
  1240. struct v4l2_async_subdev **asd;
  1241. int j;
  1242. for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
  1243. scan_async_group(ici, asd, ici->asd_sizes[j]);
  1244. asd += ici->asd_sizes[j];
  1245. }
  1246. }
  1247. #else
  1248. #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
  1249. #define soc_camera_i2c_free(icd) do {} while (0)
  1250. #define scan_async_host(ici) do {} while (0)
  1251. #endif
  1252. /* Called during host-driver probe */
  1253. static int soc_camera_probe(struct soc_camera_host *ici,
  1254. struct soc_camera_device *icd)
  1255. {
  1256. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1257. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  1258. struct device *control = NULL;
  1259. int ret;
  1260. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  1261. /*
  1262. * Currently the subdev with the largest number of controls (13) is
  1263. * ov6550. So let's pick 16 as a hint for the control handler. Note
  1264. * that this is a hint only: too large and you waste some memory, too
  1265. * small and there is a (very) small performance hit when looking up
  1266. * controls in the internal hash.
  1267. */
  1268. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  1269. if (ret < 0)
  1270. return ret;
  1271. /* Must have icd->vdev before registering the device */
  1272. ret = video_dev_create(icd);
  1273. if (ret < 0)
  1274. goto evdc;
  1275. /*
  1276. * ..._video_start() will create a device node, video_register_device()
  1277. * itself is protected against concurrent open() calls, but we also have
  1278. * to protect our data also during client probing.
  1279. */
  1280. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  1281. if (shd->board_info) {
  1282. ret = soc_camera_i2c_init(icd, sdesc);
  1283. if (ret < 0 && ret != -EPROBE_DEFER)
  1284. goto eadd;
  1285. } else if (!shd->add_device || !shd->del_device) {
  1286. ret = -EINVAL;
  1287. goto eadd;
  1288. } else {
  1289. mutex_lock(&ici->clk_lock);
  1290. ret = ici->ops->clock_start(ici);
  1291. mutex_unlock(&ici->clk_lock);
  1292. if (ret < 0)
  1293. goto eadd;
  1294. if (shd->module_name)
  1295. ret = request_module(shd->module_name);
  1296. ret = shd->add_device(icd);
  1297. if (ret < 0)
  1298. goto eadddev;
  1299. /*
  1300. * FIXME: this is racy, have to use driver-binding notification,
  1301. * when it is available
  1302. */
  1303. control = to_soc_camera_control(icd);
  1304. if (!control || !control->driver || !dev_get_drvdata(control) ||
  1305. !try_module_get(control->driver->owner)) {
  1306. shd->del_device(icd);
  1307. ret = -ENODEV;
  1308. goto enodrv;
  1309. }
  1310. }
  1311. mutex_lock(&ici->host_lock);
  1312. ret = soc_camera_probe_finish(icd);
  1313. mutex_unlock(&ici->host_lock);
  1314. if (ret < 0)
  1315. goto efinish;
  1316. return 0;
  1317. efinish:
  1318. if (shd->board_info) {
  1319. soc_camera_i2c_free(icd);
  1320. } else {
  1321. shd->del_device(icd);
  1322. module_put(control->driver->owner);
  1323. enodrv:
  1324. eadddev:
  1325. mutex_lock(&ici->clk_lock);
  1326. ici->ops->clock_stop(ici);
  1327. mutex_unlock(&ici->clk_lock);
  1328. }
  1329. eadd:
  1330. video_device_release(icd->vdev);
  1331. icd->vdev = NULL;
  1332. if (icd->vdev) {
  1333. video_device_release(icd->vdev);
  1334. icd->vdev = NULL;
  1335. }
  1336. evdc:
  1337. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1338. return ret;
  1339. }
  1340. /*
  1341. * This is called on device_unregister, which only means we have to disconnect
  1342. * from the host, but not remove ourselves from the device list. With
  1343. * asynchronous client probing this can also be called without
  1344. * soc_camera_probe_finish() having run. Careful with clean up.
  1345. */
  1346. static int soc_camera_remove(struct soc_camera_device *icd)
  1347. {
  1348. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1349. struct video_device *vdev = icd->vdev;
  1350. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1351. if (vdev) {
  1352. video_unregister_device(vdev);
  1353. icd->vdev = NULL;
  1354. }
  1355. if (sdesc->host_desc.board_info) {
  1356. soc_camera_i2c_free(icd);
  1357. } else {
  1358. struct device *dev = to_soc_camera_control(icd);
  1359. struct device_driver *drv = dev ? dev->driver : NULL;
  1360. if (drv) {
  1361. sdesc->host_desc.del_device(icd);
  1362. module_put(drv->owner);
  1363. }
  1364. }
  1365. if (icd->num_user_formats)
  1366. soc_camera_free_user_formats(icd);
  1367. if (icd->clk) {
  1368. /* For the synchronous case */
  1369. v4l2_clk_unregister(icd->clk);
  1370. icd->clk = NULL;
  1371. }
  1372. if (icd->sasc)
  1373. platform_device_unregister(icd->sasc->pdev);
  1374. return 0;
  1375. }
  1376. static int default_cropcap(struct soc_camera_device *icd,
  1377. struct v4l2_cropcap *a)
  1378. {
  1379. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1380. return v4l2_subdev_call(sd, video, cropcap, a);
  1381. }
  1382. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1383. {
  1384. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1385. return v4l2_subdev_call(sd, video, g_crop, a);
  1386. }
  1387. static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
  1388. {
  1389. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1390. return v4l2_subdev_call(sd, video, s_crop, a);
  1391. }
  1392. static int default_g_parm(struct soc_camera_device *icd,
  1393. struct v4l2_streamparm *parm)
  1394. {
  1395. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1396. return v4l2_subdev_call(sd, video, g_parm, parm);
  1397. }
  1398. static int default_s_parm(struct soc_camera_device *icd,
  1399. struct v4l2_streamparm *parm)
  1400. {
  1401. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1402. return v4l2_subdev_call(sd, video, s_parm, parm);
  1403. }
  1404. static int default_enum_framesizes(struct soc_camera_device *icd,
  1405. struct v4l2_frmsizeenum *fsize)
  1406. {
  1407. int ret;
  1408. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1409. const struct soc_camera_format_xlate *xlate;
  1410. __u32 pixfmt = fsize->pixel_format;
  1411. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1412. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1413. if (!xlate)
  1414. return -EINVAL;
  1415. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1416. fsize_mbus.pixel_format = xlate->code;
  1417. ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
  1418. if (ret < 0)
  1419. return ret;
  1420. *fsize = fsize_mbus;
  1421. fsize->pixel_format = pixfmt;
  1422. return 0;
  1423. }
  1424. int soc_camera_host_register(struct soc_camera_host *ici)
  1425. {
  1426. struct soc_camera_host *ix;
  1427. int ret;
  1428. if (!ici || !ici->ops ||
  1429. !ici->ops->try_fmt ||
  1430. !ici->ops->set_fmt ||
  1431. !ici->ops->set_bus_param ||
  1432. !ici->ops->querycap ||
  1433. ((!ici->ops->init_videobuf ||
  1434. !ici->ops->reqbufs) &&
  1435. !ici->ops->init_videobuf2) ||
  1436. !ici->ops->clock_start ||
  1437. !ici->ops->clock_stop ||
  1438. !ici->ops->poll ||
  1439. !ici->v4l2_dev.dev)
  1440. return -EINVAL;
  1441. if (!ici->ops->set_crop)
  1442. ici->ops->set_crop = default_s_crop;
  1443. if (!ici->ops->get_crop)
  1444. ici->ops->get_crop = default_g_crop;
  1445. if (!ici->ops->cropcap)
  1446. ici->ops->cropcap = default_cropcap;
  1447. if (!ici->ops->set_parm)
  1448. ici->ops->set_parm = default_s_parm;
  1449. if (!ici->ops->get_parm)
  1450. ici->ops->get_parm = default_g_parm;
  1451. if (!ici->ops->enum_framesizes)
  1452. ici->ops->enum_framesizes = default_enum_framesizes;
  1453. mutex_lock(&list_lock);
  1454. list_for_each_entry(ix, &hosts, list) {
  1455. if (ix->nr == ici->nr) {
  1456. ret = -EBUSY;
  1457. goto edevreg;
  1458. }
  1459. }
  1460. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1461. if (ret < 0)
  1462. goto edevreg;
  1463. list_add_tail(&ici->list, &hosts);
  1464. mutex_unlock(&list_lock);
  1465. mutex_init(&ici->host_lock);
  1466. mutex_init(&ici->clk_lock);
  1467. if (ici->asd_sizes)
  1468. /*
  1469. * No OF, host with a list of subdevices. Don't try to mix
  1470. * modes by initialising some groups statically and some
  1471. * dynamically!
  1472. */
  1473. scan_async_host(ici);
  1474. else
  1475. /* Legacy: static platform devices from board data */
  1476. scan_add_host(ici);
  1477. return 0;
  1478. edevreg:
  1479. mutex_unlock(&list_lock);
  1480. return ret;
  1481. }
  1482. EXPORT_SYMBOL(soc_camera_host_register);
  1483. /* Unregister all clients! */
  1484. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1485. {
  1486. struct soc_camera_device *icd, *tmp;
  1487. struct soc_camera_async_client *sasc;
  1488. LIST_HEAD(notifiers);
  1489. mutex_lock(&list_lock);
  1490. list_del(&ici->list);
  1491. list_for_each_entry(icd, &devices, list)
  1492. if (icd->iface == ici->nr && icd->sasc) {
  1493. /* as long as we hold the device, sasc won't be freed */
  1494. get_device(icd->pdev);
  1495. list_add(&icd->sasc->list, &notifiers);
  1496. }
  1497. mutex_unlock(&list_lock);
  1498. list_for_each_entry(sasc, &notifiers, list) {
  1499. /* Must call unlocked to avoid AB-BA dead-lock */
  1500. v4l2_async_notifier_unregister(&sasc->notifier);
  1501. put_device(&sasc->pdev->dev);
  1502. }
  1503. mutex_lock(&list_lock);
  1504. list_for_each_entry_safe(icd, tmp, &devices, list)
  1505. if (icd->iface == ici->nr)
  1506. soc_camera_remove(icd);
  1507. mutex_unlock(&list_lock);
  1508. v4l2_device_unregister(&ici->v4l2_dev);
  1509. }
  1510. EXPORT_SYMBOL(soc_camera_host_unregister);
  1511. /* Image capture device */
  1512. static int soc_camera_device_register(struct soc_camera_device *icd)
  1513. {
  1514. struct soc_camera_device *ix;
  1515. int num = -1, i;
  1516. mutex_lock(&list_lock);
  1517. for (i = 0; i < 256 && num < 0; i++) {
  1518. num = i;
  1519. /* Check if this index is available on this interface */
  1520. list_for_each_entry(ix, &devices, list) {
  1521. if (ix->iface == icd->iface && ix->devnum == i) {
  1522. num = -1;
  1523. break;
  1524. }
  1525. }
  1526. }
  1527. if (num < 0) {
  1528. /*
  1529. * ok, we have 256 cameras on this host...
  1530. * man, stay reasonable...
  1531. */
  1532. mutex_unlock(&list_lock);
  1533. return -ENOMEM;
  1534. }
  1535. icd->devnum = num;
  1536. icd->use_count = 0;
  1537. icd->host_priv = NULL;
  1538. /*
  1539. * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
  1540. * it again
  1541. */
  1542. i = to_platform_device(icd->pdev)->id;
  1543. if (i < 0)
  1544. /* One static (legacy) soc-camera platform device */
  1545. i = 0;
  1546. if (i >= MAP_MAX_NUM) {
  1547. mutex_unlock(&list_lock);
  1548. return -EBUSY;
  1549. }
  1550. set_bit(i, device_map);
  1551. list_add_tail(&icd->list, &devices);
  1552. mutex_unlock(&list_lock);
  1553. return 0;
  1554. }
  1555. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1556. .vidioc_querycap = soc_camera_querycap,
  1557. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1558. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1559. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1560. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1561. .vidioc_enum_input = soc_camera_enum_input,
  1562. .vidioc_g_input = soc_camera_g_input,
  1563. .vidioc_s_input = soc_camera_s_input,
  1564. .vidioc_s_std = soc_camera_s_std,
  1565. .vidioc_g_std = soc_camera_g_std,
  1566. .vidioc_enum_framesizes = soc_camera_enum_framesizes,
  1567. .vidioc_reqbufs = soc_camera_reqbufs,
  1568. .vidioc_querybuf = soc_camera_querybuf,
  1569. .vidioc_qbuf = soc_camera_qbuf,
  1570. .vidioc_dqbuf = soc_camera_dqbuf,
  1571. .vidioc_create_bufs = soc_camera_create_bufs,
  1572. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1573. .vidioc_streamon = soc_camera_streamon,
  1574. .vidioc_streamoff = soc_camera_streamoff,
  1575. .vidioc_cropcap = soc_camera_cropcap,
  1576. .vidioc_g_crop = soc_camera_g_crop,
  1577. .vidioc_s_crop = soc_camera_s_crop,
  1578. .vidioc_g_selection = soc_camera_g_selection,
  1579. .vidioc_s_selection = soc_camera_s_selection,
  1580. .vidioc_g_parm = soc_camera_g_parm,
  1581. .vidioc_s_parm = soc_camera_s_parm,
  1582. };
  1583. static int video_dev_create(struct soc_camera_device *icd)
  1584. {
  1585. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1586. struct video_device *vdev = video_device_alloc();
  1587. if (!vdev)
  1588. return -ENOMEM;
  1589. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1590. vdev->v4l2_dev = &ici->v4l2_dev;
  1591. vdev->fops = &soc_camera_fops;
  1592. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1593. vdev->release = video_device_release;
  1594. vdev->ctrl_handler = &icd->ctrl_handler;
  1595. vdev->lock = &ici->host_lock;
  1596. icd->vdev = vdev;
  1597. return 0;
  1598. }
  1599. /*
  1600. * Called from soc_camera_probe() above with .host_lock held
  1601. */
  1602. static int soc_camera_video_start(struct soc_camera_device *icd)
  1603. {
  1604. const struct device_type *type = icd->vdev->dev.type;
  1605. int ret;
  1606. if (!icd->parent)
  1607. return -ENODEV;
  1608. video_set_drvdata(icd->vdev, icd);
  1609. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1610. if (ret < 0) {
  1611. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1612. return ret;
  1613. }
  1614. /* Restore device type, possibly set by the subdevice driver */
  1615. icd->vdev->dev.type = type;
  1616. return 0;
  1617. }
  1618. static int soc_camera_pdrv_probe(struct platform_device *pdev)
  1619. {
  1620. struct soc_camera_desc *sdesc = pdev->dev.platform_data;
  1621. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  1622. struct soc_camera_device *icd;
  1623. int ret;
  1624. if (!sdesc)
  1625. return -EINVAL;
  1626. icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
  1627. if (!icd)
  1628. return -ENOMEM;
  1629. /*
  1630. * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
  1631. * regulator allocation is a dummy. They will be really requested later
  1632. * in soc_camera_async_bind(). Also note, that in that case regulators
  1633. * are attached to the I2C device and not to the camera platform device.
  1634. */
  1635. ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
  1636. ssdd->regulators);
  1637. if (ret < 0)
  1638. return ret;
  1639. icd->iface = sdesc->host_desc.bus_id;
  1640. icd->sdesc = sdesc;
  1641. icd->pdev = &pdev->dev;
  1642. platform_set_drvdata(pdev, icd);
  1643. icd->user_width = DEFAULT_WIDTH;
  1644. icd->user_height = DEFAULT_HEIGHT;
  1645. return soc_camera_device_register(icd);
  1646. }
  1647. /*
  1648. * Only called on rmmod for each platform device, since they are not
  1649. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1650. * been unloaded already
  1651. */
  1652. static int soc_camera_pdrv_remove(struct platform_device *pdev)
  1653. {
  1654. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1655. int i;
  1656. if (!icd)
  1657. return -EINVAL;
  1658. i = pdev->id;
  1659. if (i < 0)
  1660. i = 0;
  1661. /*
  1662. * In synchronous mode with static platform devices this is called in a
  1663. * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
  1664. * no need to lock. In asynchronous case the caller -
  1665. * soc_camera_host_unregister() - already holds the lock
  1666. */
  1667. if (test_bit(i, device_map)) {
  1668. clear_bit(i, device_map);
  1669. list_del(&icd->list);
  1670. }
  1671. return 0;
  1672. }
  1673. static struct platform_driver __refdata soc_camera_pdrv = {
  1674. .probe = soc_camera_pdrv_probe,
  1675. .remove = soc_camera_pdrv_remove,
  1676. .driver = {
  1677. .name = "soc-camera-pdrv",
  1678. .owner = THIS_MODULE,
  1679. },
  1680. };
  1681. module_platform_driver(soc_camera_pdrv);
  1682. MODULE_DESCRIPTION("Image capture bus driver");
  1683. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1684. MODULE_LICENSE("GPL");
  1685. MODULE_ALIAS("platform:soc-camera-pdrv");