soc_camera.c 55 KB

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