soc_camera.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  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/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_crop() */
  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_cropcap(struct file *file, void *fh,
  829. struct v4l2_cropcap *a)
  830. {
  831. struct soc_camera_device *icd = file->private_data;
  832. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  833. return ici->ops->cropcap(icd, a);
  834. }
  835. static int soc_camera_g_crop(struct file *file, void *fh,
  836. struct v4l2_crop *a)
  837. {
  838. struct soc_camera_device *icd = file->private_data;
  839. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  840. int ret;
  841. ret = ici->ops->get_crop(icd, a);
  842. return ret;
  843. }
  844. /*
  845. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  846. * argument with the actual geometry, instead, the user shall use G_CROP to
  847. * retrieve it.
  848. */
  849. static int soc_camera_s_crop(struct file *file, void *fh,
  850. const struct v4l2_crop *a)
  851. {
  852. struct soc_camera_device *icd = file->private_data;
  853. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  854. const struct v4l2_rect *rect = &a->c;
  855. struct v4l2_crop current_crop;
  856. int ret;
  857. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  858. return -EINVAL;
  859. dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
  860. rect->width, rect->height, rect->left, rect->top);
  861. current_crop.type = a->type;
  862. /* If get_crop fails, we'll let host and / or client drivers decide */
  863. ret = ici->ops->get_crop(icd, &current_crop);
  864. /* Prohibit window size change with initialised buffers */
  865. if (ret < 0) {
  866. dev_err(icd->pdev,
  867. "S_CROP denied: getting current crop failed\n");
  868. } else if ((a->c.width == current_crop.c.width &&
  869. a->c.height == current_crop.c.height) ||
  870. !is_streaming(ici, icd)) {
  871. /* same size or not streaming - use .set_crop() */
  872. ret = ici->ops->set_crop(icd, a);
  873. } else if (ici->ops->set_livecrop) {
  874. ret = ici->ops->set_livecrop(icd, a);
  875. } else {
  876. dev_err(icd->pdev,
  877. "S_CROP denied: queue initialised and sizes differ\n");
  878. ret = -EBUSY;
  879. }
  880. return ret;
  881. }
  882. static int soc_camera_g_selection(struct file *file, void *fh,
  883. struct v4l2_selection *s)
  884. {
  885. struct soc_camera_device *icd = file->private_data;
  886. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  887. /* With a wrong type no need to try to fall back to cropping */
  888. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  889. return -EINVAL;
  890. if (!ici->ops->get_selection)
  891. return -ENOTTY;
  892. return ici->ops->get_selection(icd, s);
  893. }
  894. static int soc_camera_s_selection(struct file *file, void *fh,
  895. struct v4l2_selection *s)
  896. {
  897. struct soc_camera_device *icd = file->private_data;
  898. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  899. int ret;
  900. /* In all these cases cropping emulation will not help */
  901. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  902. (s->target != V4L2_SEL_TGT_COMPOSE &&
  903. s->target != V4L2_SEL_TGT_CROP))
  904. return -EINVAL;
  905. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  906. /* No output size change during a running capture! */
  907. if (is_streaming(ici, icd) &&
  908. (icd->user_width != s->r.width ||
  909. icd->user_height != s->r.height))
  910. return -EBUSY;
  911. /*
  912. * Only one user is allowed to change the output format, touch
  913. * buffers, start / stop streaming, poll for data
  914. */
  915. if (icd->streamer && icd->streamer != file)
  916. return -EBUSY;
  917. }
  918. if (!ici->ops->set_selection)
  919. return -ENOTTY;
  920. ret = ici->ops->set_selection(icd, s);
  921. if (!ret &&
  922. s->target == V4L2_SEL_TGT_COMPOSE) {
  923. icd->user_width = s->r.width;
  924. icd->user_height = s->r.height;
  925. if (!icd->streamer)
  926. icd->streamer = file;
  927. }
  928. return ret;
  929. }
  930. static int soc_camera_g_parm(struct file *file, void *fh,
  931. struct v4l2_streamparm *a)
  932. {
  933. struct soc_camera_device *icd = file->private_data;
  934. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  935. if (ici->ops->get_parm)
  936. return ici->ops->get_parm(icd, a);
  937. return -ENOIOCTLCMD;
  938. }
  939. static int soc_camera_s_parm(struct file *file, void *fh,
  940. struct v4l2_streamparm *a)
  941. {
  942. struct soc_camera_device *icd = file->private_data;
  943. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  944. if (ici->ops->set_parm)
  945. return ici->ops->set_parm(icd, a);
  946. return -ENOIOCTLCMD;
  947. }
  948. static int soc_camera_probe(struct soc_camera_host *ici,
  949. struct soc_camera_device *icd);
  950. /* So far this function cannot fail */
  951. static void scan_add_host(struct soc_camera_host *ici)
  952. {
  953. struct soc_camera_device *icd;
  954. mutex_lock(&list_lock);
  955. list_for_each_entry(icd, &devices, list)
  956. if (icd->iface == ici->nr) {
  957. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  958. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  959. /* The camera could have been already on, try to reset */
  960. if (ssdd->reset)
  961. if (icd->control)
  962. ssdd->reset(icd->control);
  963. icd->parent = ici->v4l2_dev.dev;
  964. /* Ignore errors */
  965. soc_camera_probe(ici, icd);
  966. }
  967. mutex_unlock(&list_lock);
  968. }
  969. /*
  970. * It is invalid to call v4l2_clk_enable() after a successful probing
  971. * asynchronously outside of V4L2 operations, i.e. with .host_lock not held.
  972. */
  973. static int soc_camera_clk_enable(struct v4l2_clk *clk)
  974. {
  975. struct soc_camera_device *icd = clk->priv;
  976. struct soc_camera_host *ici;
  977. if (!icd || !icd->parent)
  978. return -ENODEV;
  979. ici = to_soc_camera_host(icd->parent);
  980. if (!try_module_get(ici->ops->owner))
  981. return -ENODEV;
  982. /*
  983. * If a different client is currently being probed, the host will tell
  984. * you to go
  985. */
  986. return soc_camera_clock_start(ici);
  987. }
  988. static void soc_camera_clk_disable(struct v4l2_clk *clk)
  989. {
  990. struct soc_camera_device *icd = clk->priv;
  991. struct soc_camera_host *ici;
  992. if (!icd || !icd->parent)
  993. return;
  994. ici = to_soc_camera_host(icd->parent);
  995. soc_camera_clock_stop(ici);
  996. module_put(ici->ops->owner);
  997. }
  998. /*
  999. * Eventually, it would be more logical to make the respective host the clock
  1000. * owner, but then we would have to copy this struct for each ici. Besides, it
  1001. * would introduce the circular dependency problem, unless we port all client
  1002. * drivers to release the clock, when not in use.
  1003. */
  1004. static const struct v4l2_clk_ops soc_camera_clk_ops = {
  1005. .owner = THIS_MODULE,
  1006. .enable = soc_camera_clk_enable,
  1007. .disable = soc_camera_clk_disable,
  1008. };
  1009. static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc,
  1010. struct soc_camera_async_client *sasc)
  1011. {
  1012. struct platform_device *pdev;
  1013. int ret, i;
  1014. mutex_lock(&list_lock);
  1015. i = find_first_zero_bit(device_map, MAP_MAX_NUM);
  1016. if (i < MAP_MAX_NUM)
  1017. set_bit(i, device_map);
  1018. mutex_unlock(&list_lock);
  1019. if (i >= MAP_MAX_NUM)
  1020. return -ENOMEM;
  1021. pdev = platform_device_alloc("soc-camera-pdrv", i);
  1022. if (!pdev)
  1023. return -ENOMEM;
  1024. ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc));
  1025. if (ret < 0) {
  1026. platform_device_put(pdev);
  1027. return ret;
  1028. }
  1029. sasc->pdev = pdev;
  1030. return 0;
  1031. }
  1032. static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc)
  1033. {
  1034. struct platform_device *pdev = sasc->pdev;
  1035. int ret;
  1036. ret = platform_device_add(pdev);
  1037. if (ret < 0 || !pdev->dev.driver)
  1038. return NULL;
  1039. return platform_get_drvdata(pdev);
  1040. }
  1041. /* Locking: called with .host_lock held */
  1042. static int soc_camera_probe_finish(struct soc_camera_device *icd)
  1043. {
  1044. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1045. struct v4l2_subdev_format fmt = {
  1046. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1047. };
  1048. struct v4l2_mbus_framefmt *mf = &fmt.format;
  1049. int ret;
  1050. sd->grp_id = soc_camera_grp_id(icd);
  1051. v4l2_set_subdev_hostdata(sd, icd);
  1052. v4l2_subdev_call(sd, video, g_tvnorms, &icd->vdev->tvnorms);
  1053. ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
  1054. if (ret < 0)
  1055. return ret;
  1056. ret = soc_camera_add_device(icd);
  1057. if (ret < 0) {
  1058. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  1059. return ret;
  1060. }
  1061. /* At this point client .probe() should have run already */
  1062. ret = soc_camera_init_user_formats(icd);
  1063. if (ret < 0)
  1064. goto eusrfmt;
  1065. icd->field = V4L2_FIELD_ANY;
  1066. ret = soc_camera_video_start(icd);
  1067. if (ret < 0)
  1068. goto evidstart;
  1069. /* Try to improve our guess of a reasonable window format */
  1070. if (!v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt)) {
  1071. icd->user_width = mf->width;
  1072. icd->user_height = mf->height;
  1073. icd->colorspace = mf->colorspace;
  1074. icd->field = mf->field;
  1075. }
  1076. soc_camera_remove_device(icd);
  1077. return 0;
  1078. evidstart:
  1079. soc_camera_free_user_formats(icd);
  1080. eusrfmt:
  1081. soc_camera_remove_device(icd);
  1082. return ret;
  1083. }
  1084. #ifdef CONFIG_I2C_BOARDINFO
  1085. static int soc_camera_i2c_init(struct soc_camera_device *icd,
  1086. struct soc_camera_desc *sdesc)
  1087. {
  1088. struct soc_camera_subdev_desc *ssdd;
  1089. struct i2c_client *client;
  1090. struct soc_camera_host *ici;
  1091. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  1092. struct i2c_adapter *adap;
  1093. struct v4l2_subdev *subdev;
  1094. char clk_name[V4L2_SUBDEV_NAME_SIZE];
  1095. int ret;
  1096. /* First find out how we link the main client */
  1097. if (icd->sasc) {
  1098. /* Async non-OF probing handled by the subdevice list */
  1099. return -EPROBE_DEFER;
  1100. }
  1101. ici = to_soc_camera_host(icd->parent);
  1102. adap = i2c_get_adapter(shd->i2c_adapter_id);
  1103. if (!adap) {
  1104. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  1105. shd->i2c_adapter_id);
  1106. return -ENODEV;
  1107. }
  1108. ssdd = kmemdup(&sdesc->subdev_desc, sizeof(*ssdd), GFP_KERNEL);
  1109. if (!ssdd) {
  1110. ret = -ENOMEM;
  1111. goto ealloc;
  1112. }
  1113. /*
  1114. * In synchronous case we request regulators ourselves in
  1115. * soc_camera_pdrv_probe(), make sure the subdevice driver doesn't try
  1116. * to allocate them again.
  1117. */
  1118. ssdd->sd_pdata.num_regulators = 0;
  1119. ssdd->sd_pdata.regulators = NULL;
  1120. shd->board_info->platform_data = ssdd;
  1121. snprintf(clk_name, sizeof(clk_name), "%d-%04x",
  1122. shd->i2c_adapter_id, shd->board_info->addr);
  1123. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1124. if (IS_ERR(icd->clk)) {
  1125. ret = PTR_ERR(icd->clk);
  1126. goto eclkreg;
  1127. }
  1128. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  1129. shd->board_info, NULL);
  1130. if (!subdev) {
  1131. ret = -ENODEV;
  1132. goto ei2cnd;
  1133. }
  1134. client = v4l2_get_subdevdata(subdev);
  1135. /* Use to_i2c_client(dev) to recover the i2c client */
  1136. icd->control = &client->dev;
  1137. return 0;
  1138. ei2cnd:
  1139. v4l2_clk_unregister(icd->clk);
  1140. icd->clk = NULL;
  1141. eclkreg:
  1142. kfree(ssdd);
  1143. ealloc:
  1144. i2c_put_adapter(adap);
  1145. return ret;
  1146. }
  1147. static void soc_camera_i2c_free(struct soc_camera_device *icd)
  1148. {
  1149. struct i2c_client *client =
  1150. to_i2c_client(to_soc_camera_control(icd));
  1151. struct i2c_adapter *adap;
  1152. struct soc_camera_subdev_desc *ssdd;
  1153. icd->control = NULL;
  1154. if (icd->sasc)
  1155. return;
  1156. adap = client->adapter;
  1157. ssdd = client->dev.platform_data;
  1158. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  1159. i2c_unregister_device(client);
  1160. i2c_put_adapter(adap);
  1161. kfree(ssdd);
  1162. v4l2_clk_unregister(icd->clk);
  1163. icd->clk = NULL;
  1164. }
  1165. /*
  1166. * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async
  1167. * internal global mutex, therefore cannot race against other asynchronous
  1168. * events. Until notifier->complete() (soc_camera_async_complete()) is called,
  1169. * the video device node is not registered and no V4L fops can occur. Unloading
  1170. * of the host driver also calls a v4l2-async function, so also there we're
  1171. * protected.
  1172. */
  1173. static int soc_camera_async_bound(struct v4l2_async_notifier *notifier,
  1174. struct v4l2_subdev *sd,
  1175. struct v4l2_async_subdev *asd)
  1176. {
  1177. struct soc_camera_async_client *sasc = container_of(notifier,
  1178. struct soc_camera_async_client, notifier);
  1179. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1180. if (asd == sasc->sensor && !WARN_ON(icd->control)) {
  1181. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1182. /*
  1183. * Only now we get subdevice-specific information like
  1184. * regulators, flags, callbacks, etc.
  1185. */
  1186. if (client) {
  1187. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1188. struct soc_camera_subdev_desc *ssdd =
  1189. soc_camera_i2c_to_desc(client);
  1190. if (ssdd) {
  1191. memcpy(&sdesc->subdev_desc, ssdd,
  1192. sizeof(sdesc->subdev_desc));
  1193. if (ssdd->reset)
  1194. ssdd->reset(&client->dev);
  1195. }
  1196. icd->control = &client->dev;
  1197. }
  1198. }
  1199. return 0;
  1200. }
  1201. static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier,
  1202. struct v4l2_subdev *sd,
  1203. struct v4l2_async_subdev *asd)
  1204. {
  1205. struct soc_camera_async_client *sasc = container_of(notifier,
  1206. struct soc_camera_async_client, notifier);
  1207. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1208. if (icd->clk) {
  1209. v4l2_clk_unregister(icd->clk);
  1210. icd->clk = NULL;
  1211. }
  1212. }
  1213. static int soc_camera_async_complete(struct v4l2_async_notifier *notifier)
  1214. {
  1215. struct soc_camera_async_client *sasc = container_of(notifier,
  1216. struct soc_camera_async_client, notifier);
  1217. struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev);
  1218. if (to_soc_camera_control(icd)) {
  1219. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1220. int ret;
  1221. mutex_lock(&list_lock);
  1222. ret = soc_camera_probe(ici, icd);
  1223. mutex_unlock(&list_lock);
  1224. if (ret < 0)
  1225. return ret;
  1226. }
  1227. return 0;
  1228. }
  1229. static int scan_async_group(struct soc_camera_host *ici,
  1230. struct v4l2_async_subdev **asd, unsigned int size)
  1231. {
  1232. struct soc_camera_async_subdev *sasd;
  1233. struct soc_camera_async_client *sasc;
  1234. struct soc_camera_device *icd;
  1235. struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
  1236. char clk_name[V4L2_SUBDEV_NAME_SIZE];
  1237. unsigned int i;
  1238. int ret;
  1239. /* First look for a sensor */
  1240. for (i = 0; i < size; i++) {
  1241. sasd = container_of(asd[i], struct soc_camera_async_subdev, asd);
  1242. if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE)
  1243. break;
  1244. }
  1245. if (i >= size || asd[i]->match_type != V4L2_ASYNC_MATCH_I2C) {
  1246. /* All useless */
  1247. dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n");
  1248. return -ENODEV;
  1249. }
  1250. /* Or shall this be managed by the soc-camera device? */
  1251. sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL);
  1252. if (!sasc)
  1253. return -ENOMEM;
  1254. /* HACK: just need a != NULL */
  1255. sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
  1256. ret = soc_camera_dyn_pdev(&sdesc, sasc);
  1257. if (ret < 0)
  1258. goto eallocpdev;
  1259. sasc->sensor = &sasd->asd;
  1260. icd = soc_camera_add_pdev(sasc);
  1261. if (!icd) {
  1262. ret = -ENOMEM;
  1263. goto eaddpdev;
  1264. }
  1265. sasc->notifier.subdevs = asd;
  1266. sasc->notifier.num_subdevs = size;
  1267. sasc->notifier.bound = soc_camera_async_bound;
  1268. sasc->notifier.unbind = soc_camera_async_unbind;
  1269. sasc->notifier.complete = soc_camera_async_complete;
  1270. icd->sasc = sasc;
  1271. icd->parent = ici->v4l2_dev.dev;
  1272. snprintf(clk_name, sizeof(clk_name), "%d-%04x",
  1273. sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address);
  1274. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1275. if (IS_ERR(icd->clk)) {
  1276. ret = PTR_ERR(icd->clk);
  1277. goto eclkreg;
  1278. }
  1279. ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
  1280. if (!ret)
  1281. return 0;
  1282. v4l2_clk_unregister(icd->clk);
  1283. eclkreg:
  1284. icd->clk = NULL;
  1285. platform_device_del(sasc->pdev);
  1286. eaddpdev:
  1287. platform_device_put(sasc->pdev);
  1288. eallocpdev:
  1289. devm_kfree(ici->v4l2_dev.dev, sasc);
  1290. dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
  1291. return ret;
  1292. }
  1293. static void scan_async_host(struct soc_camera_host *ici)
  1294. {
  1295. struct v4l2_async_subdev **asd;
  1296. int j;
  1297. for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) {
  1298. scan_async_group(ici, asd, ici->asd_sizes[j]);
  1299. asd += ici->asd_sizes[j];
  1300. }
  1301. }
  1302. #else
  1303. #define soc_camera_i2c_init(icd, sdesc) (-ENODEV)
  1304. #define soc_camera_i2c_free(icd) do {} while (0)
  1305. #define scan_async_host(ici) do {} while (0)
  1306. #endif
  1307. #ifdef CONFIG_OF
  1308. struct soc_of_info {
  1309. struct soc_camera_async_subdev sasd;
  1310. struct soc_camera_async_client sasc;
  1311. struct v4l2_async_subdev *subdev;
  1312. };
  1313. static int soc_of_bind(struct soc_camera_host *ici,
  1314. struct device_node *ep,
  1315. struct device_node *remote)
  1316. {
  1317. struct soc_camera_device *icd;
  1318. struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,};
  1319. struct soc_camera_async_client *sasc;
  1320. struct soc_of_info *info;
  1321. struct i2c_client *client;
  1322. char clk_name[V4L2_SUBDEV_NAME_SIZE + 32];
  1323. int ret;
  1324. /* allocate a new subdev and add match info to it */
  1325. info = devm_kzalloc(ici->v4l2_dev.dev, sizeof(struct soc_of_info),
  1326. GFP_KERNEL);
  1327. if (!info)
  1328. return -ENOMEM;
  1329. info->sasd.asd.match.of.node = remote;
  1330. info->sasd.asd.match_type = V4L2_ASYNC_MATCH_OF;
  1331. info->subdev = &info->sasd.asd;
  1332. /* Or shall this be managed by the soc-camera device? */
  1333. sasc = &info->sasc;
  1334. /* HACK: just need a != NULL */
  1335. sdesc.host_desc.board_info = ERR_PTR(-ENODATA);
  1336. ret = soc_camera_dyn_pdev(&sdesc, sasc);
  1337. if (ret < 0)
  1338. goto eallocpdev;
  1339. sasc->sensor = &info->sasd.asd;
  1340. icd = soc_camera_add_pdev(sasc);
  1341. if (!icd) {
  1342. ret = -ENOMEM;
  1343. goto eaddpdev;
  1344. }
  1345. sasc->notifier.subdevs = &info->subdev;
  1346. sasc->notifier.num_subdevs = 1;
  1347. sasc->notifier.bound = soc_camera_async_bound;
  1348. sasc->notifier.unbind = soc_camera_async_unbind;
  1349. sasc->notifier.complete = soc_camera_async_complete;
  1350. icd->sasc = sasc;
  1351. icd->parent = ici->v4l2_dev.dev;
  1352. client = of_find_i2c_device_by_node(remote);
  1353. if (client)
  1354. snprintf(clk_name, sizeof(clk_name), "%d-%04x",
  1355. client->adapter->nr, client->addr);
  1356. else
  1357. snprintf(clk_name, sizeof(clk_name), "of-%s",
  1358. of_node_full_name(remote));
  1359. icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, icd);
  1360. if (IS_ERR(icd->clk)) {
  1361. ret = PTR_ERR(icd->clk);
  1362. goto eclkreg;
  1363. }
  1364. ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier);
  1365. if (!ret)
  1366. return 0;
  1367. v4l2_clk_unregister(icd->clk);
  1368. eclkreg:
  1369. icd->clk = NULL;
  1370. platform_device_del(sasc->pdev);
  1371. eaddpdev:
  1372. platform_device_put(sasc->pdev);
  1373. eallocpdev:
  1374. devm_kfree(ici->v4l2_dev.dev, info);
  1375. dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret);
  1376. return ret;
  1377. }
  1378. static void scan_of_host(struct soc_camera_host *ici)
  1379. {
  1380. struct device *dev = ici->v4l2_dev.dev;
  1381. struct device_node *np = dev->of_node;
  1382. struct device_node *epn = NULL, *ren;
  1383. unsigned int i;
  1384. for (i = 0; ; i++) {
  1385. epn = of_graph_get_next_endpoint(np, epn);
  1386. if (!epn)
  1387. break;
  1388. ren = of_graph_get_remote_port(epn);
  1389. if (!ren) {
  1390. dev_notice(dev, "no remote for %s\n",
  1391. of_node_full_name(epn));
  1392. continue;
  1393. }
  1394. /* so we now have a remote node to connect */
  1395. if (!i)
  1396. soc_of_bind(ici, epn, ren->parent);
  1397. of_node_put(ren);
  1398. if (i) {
  1399. dev_err(dev, "multiple subdevices aren't supported yet!\n");
  1400. break;
  1401. }
  1402. }
  1403. of_node_put(epn);
  1404. }
  1405. #else
  1406. static inline void scan_of_host(struct soc_camera_host *ici) { }
  1407. #endif
  1408. /* Called during host-driver probe */
  1409. static int soc_camera_probe(struct soc_camera_host *ici,
  1410. struct soc_camera_device *icd)
  1411. {
  1412. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1413. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  1414. struct device *control = NULL;
  1415. int ret;
  1416. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  1417. /*
  1418. * Currently the subdev with the largest number of controls (13) is
  1419. * ov6550. So let's pick 16 as a hint for the control handler. Note
  1420. * that this is a hint only: too large and you waste some memory, too
  1421. * small and there is a (very) small performance hit when looking up
  1422. * controls in the internal hash.
  1423. */
  1424. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  1425. if (ret < 0)
  1426. return ret;
  1427. /* Must have icd->vdev before registering the device */
  1428. ret = video_dev_create(icd);
  1429. if (ret < 0)
  1430. goto evdc;
  1431. /*
  1432. * ..._video_start() will create a device node, video_register_device()
  1433. * itself is protected against concurrent open() calls, but we also have
  1434. * to protect our data also during client probing.
  1435. */
  1436. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  1437. if (shd->board_info) {
  1438. ret = soc_camera_i2c_init(icd, sdesc);
  1439. if (ret < 0 && ret != -EPROBE_DEFER)
  1440. goto eadd;
  1441. } else if (!shd->add_device || !shd->del_device) {
  1442. ret = -EINVAL;
  1443. goto eadd;
  1444. } else {
  1445. ret = soc_camera_clock_start(ici);
  1446. if (ret < 0)
  1447. goto eadd;
  1448. if (shd->module_name)
  1449. ret = request_module(shd->module_name);
  1450. ret = shd->add_device(icd);
  1451. if (ret < 0)
  1452. goto eadddev;
  1453. /*
  1454. * FIXME: this is racy, have to use driver-binding notification,
  1455. * when it is available
  1456. */
  1457. control = to_soc_camera_control(icd);
  1458. if (!control || !control->driver || !dev_get_drvdata(control) ||
  1459. !try_module_get(control->driver->owner)) {
  1460. shd->del_device(icd);
  1461. ret = -ENODEV;
  1462. goto enodrv;
  1463. }
  1464. }
  1465. mutex_lock(&ici->host_lock);
  1466. ret = soc_camera_probe_finish(icd);
  1467. mutex_unlock(&ici->host_lock);
  1468. if (ret < 0)
  1469. goto efinish;
  1470. return 0;
  1471. efinish:
  1472. if (shd->board_info) {
  1473. soc_camera_i2c_free(icd);
  1474. } else {
  1475. shd->del_device(icd);
  1476. module_put(control->driver->owner);
  1477. enodrv:
  1478. eadddev:
  1479. soc_camera_clock_stop(ici);
  1480. }
  1481. eadd:
  1482. if (icd->vdev) {
  1483. video_device_release(icd->vdev);
  1484. icd->vdev = NULL;
  1485. }
  1486. evdc:
  1487. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1488. return ret;
  1489. }
  1490. /*
  1491. * This is called on device_unregister, which only means we have to disconnect
  1492. * from the host, but not remove ourselves from the device list. With
  1493. * asynchronous client probing this can also be called without
  1494. * soc_camera_probe_finish() having run. Careful with clean up.
  1495. */
  1496. static int soc_camera_remove(struct soc_camera_device *icd)
  1497. {
  1498. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1499. struct video_device *vdev = icd->vdev;
  1500. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1501. if (vdev) {
  1502. video_unregister_device(vdev);
  1503. icd->vdev = NULL;
  1504. }
  1505. if (sdesc->host_desc.board_info) {
  1506. soc_camera_i2c_free(icd);
  1507. } else {
  1508. struct device *dev = to_soc_camera_control(icd);
  1509. struct device_driver *drv = dev ? dev->driver : NULL;
  1510. if (drv) {
  1511. sdesc->host_desc.del_device(icd);
  1512. module_put(drv->owner);
  1513. }
  1514. }
  1515. if (icd->num_user_formats)
  1516. soc_camera_free_user_formats(icd);
  1517. if (icd->clk) {
  1518. /* For the synchronous case */
  1519. v4l2_clk_unregister(icd->clk);
  1520. icd->clk = NULL;
  1521. }
  1522. if (icd->sasc)
  1523. platform_device_unregister(icd->sasc->pdev);
  1524. return 0;
  1525. }
  1526. static int default_cropcap(struct soc_camera_device *icd,
  1527. struct v4l2_cropcap *a)
  1528. {
  1529. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1530. return v4l2_subdev_call(sd, video, cropcap, a);
  1531. }
  1532. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1533. {
  1534. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1535. return v4l2_subdev_call(sd, video, g_crop, a);
  1536. }
  1537. static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
  1538. {
  1539. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1540. return v4l2_subdev_call(sd, video, s_crop, a);
  1541. }
  1542. static int default_g_parm(struct soc_camera_device *icd,
  1543. struct v4l2_streamparm *parm)
  1544. {
  1545. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1546. return v4l2_subdev_call(sd, video, g_parm, parm);
  1547. }
  1548. static int default_s_parm(struct soc_camera_device *icd,
  1549. struct v4l2_streamparm *parm)
  1550. {
  1551. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1552. return v4l2_subdev_call(sd, video, s_parm, parm);
  1553. }
  1554. static int default_enum_framesizes(struct soc_camera_device *icd,
  1555. struct v4l2_frmsizeenum *fsize)
  1556. {
  1557. int ret;
  1558. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1559. const struct soc_camera_format_xlate *xlate;
  1560. struct v4l2_subdev_frame_size_enum fse = {
  1561. .index = fsize->index,
  1562. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1563. };
  1564. xlate = soc_camera_xlate_by_fourcc(icd, fsize->pixel_format);
  1565. if (!xlate)
  1566. return -EINVAL;
  1567. fse.code = xlate->code;
  1568. ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
  1569. if (ret < 0)
  1570. return ret;
  1571. if (fse.min_width == fse.max_width &&
  1572. fse.min_height == fse.max_height) {
  1573. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1574. fsize->discrete.width = fse.min_width;
  1575. fsize->discrete.height = fse.min_height;
  1576. return 0;
  1577. }
  1578. fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
  1579. fsize->stepwise.min_width = fse.min_width;
  1580. fsize->stepwise.max_width = fse.max_width;
  1581. fsize->stepwise.min_height = fse.min_height;
  1582. fsize->stepwise.max_height = fse.max_height;
  1583. fsize->stepwise.step_width = 1;
  1584. fsize->stepwise.step_height = 1;
  1585. return 0;
  1586. }
  1587. int soc_camera_host_register(struct soc_camera_host *ici)
  1588. {
  1589. struct soc_camera_host *ix;
  1590. int ret;
  1591. if (!ici || !ici->ops ||
  1592. !ici->ops->try_fmt ||
  1593. !ici->ops->set_fmt ||
  1594. !ici->ops->set_bus_param ||
  1595. !ici->ops->querycap ||
  1596. ((!ici->ops->init_videobuf ||
  1597. !ici->ops->reqbufs) &&
  1598. !ici->ops->init_videobuf2) ||
  1599. !ici->ops->poll ||
  1600. !ici->v4l2_dev.dev)
  1601. return -EINVAL;
  1602. if (!ici->ops->set_crop)
  1603. ici->ops->set_crop = default_s_crop;
  1604. if (!ici->ops->get_crop)
  1605. ici->ops->get_crop = default_g_crop;
  1606. if (!ici->ops->cropcap)
  1607. ici->ops->cropcap = default_cropcap;
  1608. if (!ici->ops->set_parm)
  1609. ici->ops->set_parm = default_s_parm;
  1610. if (!ici->ops->get_parm)
  1611. ici->ops->get_parm = default_g_parm;
  1612. if (!ici->ops->enum_framesizes)
  1613. ici->ops->enum_framesizes = default_enum_framesizes;
  1614. mutex_lock(&list_lock);
  1615. list_for_each_entry(ix, &hosts, list) {
  1616. if (ix->nr == ici->nr) {
  1617. ret = -EBUSY;
  1618. goto edevreg;
  1619. }
  1620. }
  1621. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1622. if (ret < 0)
  1623. goto edevreg;
  1624. list_add_tail(&ici->list, &hosts);
  1625. mutex_unlock(&list_lock);
  1626. mutex_init(&ici->host_lock);
  1627. mutex_init(&ici->clk_lock);
  1628. if (ici->v4l2_dev.dev->of_node)
  1629. scan_of_host(ici);
  1630. else if (ici->asd_sizes)
  1631. /*
  1632. * No OF, host with a list of subdevices. Don't try to mix
  1633. * modes by initialising some groups statically and some
  1634. * dynamically!
  1635. */
  1636. scan_async_host(ici);
  1637. else
  1638. /* Legacy: static platform devices from board data */
  1639. scan_add_host(ici);
  1640. return 0;
  1641. edevreg:
  1642. mutex_unlock(&list_lock);
  1643. return ret;
  1644. }
  1645. EXPORT_SYMBOL(soc_camera_host_register);
  1646. /* Unregister all clients! */
  1647. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1648. {
  1649. struct soc_camera_device *icd, *tmp;
  1650. struct soc_camera_async_client *sasc;
  1651. LIST_HEAD(notifiers);
  1652. mutex_lock(&list_lock);
  1653. list_del(&ici->list);
  1654. list_for_each_entry(icd, &devices, list)
  1655. if (icd->iface == ici->nr && icd->sasc) {
  1656. /* as long as we hold the device, sasc won't be freed */
  1657. get_device(icd->pdev);
  1658. list_add(&icd->sasc->list, &notifiers);
  1659. }
  1660. mutex_unlock(&list_lock);
  1661. list_for_each_entry(sasc, &notifiers, list) {
  1662. /* Must call unlocked to avoid AB-BA dead-lock */
  1663. v4l2_async_notifier_unregister(&sasc->notifier);
  1664. put_device(&sasc->pdev->dev);
  1665. }
  1666. mutex_lock(&list_lock);
  1667. list_for_each_entry_safe(icd, tmp, &devices, list)
  1668. if (icd->iface == ici->nr)
  1669. soc_camera_remove(icd);
  1670. mutex_unlock(&list_lock);
  1671. v4l2_device_unregister(&ici->v4l2_dev);
  1672. }
  1673. EXPORT_SYMBOL(soc_camera_host_unregister);
  1674. /* Image capture device */
  1675. static int soc_camera_device_register(struct soc_camera_device *icd)
  1676. {
  1677. struct soc_camera_device *ix;
  1678. int num = -1, i;
  1679. mutex_lock(&list_lock);
  1680. for (i = 0; i < 256 && num < 0; i++) {
  1681. num = i;
  1682. /* Check if this index is available on this interface */
  1683. list_for_each_entry(ix, &devices, list) {
  1684. if (ix->iface == icd->iface && ix->devnum == i) {
  1685. num = -1;
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. if (num < 0) {
  1691. /*
  1692. * ok, we have 256 cameras on this host...
  1693. * man, stay reasonable...
  1694. */
  1695. mutex_unlock(&list_lock);
  1696. return -ENOMEM;
  1697. }
  1698. icd->devnum = num;
  1699. icd->use_count = 0;
  1700. icd->host_priv = NULL;
  1701. /*
  1702. * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting
  1703. * it again
  1704. */
  1705. i = to_platform_device(icd->pdev)->id;
  1706. if (i < 0)
  1707. /* One static (legacy) soc-camera platform device */
  1708. i = 0;
  1709. if (i >= MAP_MAX_NUM) {
  1710. mutex_unlock(&list_lock);
  1711. return -EBUSY;
  1712. }
  1713. set_bit(i, device_map);
  1714. list_add_tail(&icd->list, &devices);
  1715. mutex_unlock(&list_lock);
  1716. return 0;
  1717. }
  1718. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1719. .vidioc_querycap = soc_camera_querycap,
  1720. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1721. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1722. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1723. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1724. .vidioc_enum_input = soc_camera_enum_input,
  1725. .vidioc_g_input = soc_camera_g_input,
  1726. .vidioc_s_input = soc_camera_s_input,
  1727. .vidioc_s_std = soc_camera_s_std,
  1728. .vidioc_g_std = soc_camera_g_std,
  1729. .vidioc_enum_framesizes = soc_camera_enum_framesizes,
  1730. .vidioc_reqbufs = soc_camera_reqbufs,
  1731. .vidioc_querybuf = soc_camera_querybuf,
  1732. .vidioc_qbuf = soc_camera_qbuf,
  1733. .vidioc_dqbuf = soc_camera_dqbuf,
  1734. .vidioc_create_bufs = soc_camera_create_bufs,
  1735. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1736. .vidioc_expbuf = soc_camera_expbuf,
  1737. .vidioc_streamon = soc_camera_streamon,
  1738. .vidioc_streamoff = soc_camera_streamoff,
  1739. .vidioc_cropcap = soc_camera_cropcap,
  1740. .vidioc_g_crop = soc_camera_g_crop,
  1741. .vidioc_s_crop = soc_camera_s_crop,
  1742. .vidioc_g_selection = soc_camera_g_selection,
  1743. .vidioc_s_selection = soc_camera_s_selection,
  1744. .vidioc_g_parm = soc_camera_g_parm,
  1745. .vidioc_s_parm = soc_camera_s_parm,
  1746. };
  1747. static int video_dev_create(struct soc_camera_device *icd)
  1748. {
  1749. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1750. struct video_device *vdev = video_device_alloc();
  1751. if (!vdev)
  1752. return -ENOMEM;
  1753. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1754. vdev->v4l2_dev = &ici->v4l2_dev;
  1755. vdev->fops = &soc_camera_fops;
  1756. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1757. vdev->release = video_device_release;
  1758. vdev->ctrl_handler = &icd->ctrl_handler;
  1759. vdev->lock = &ici->host_lock;
  1760. icd->vdev = vdev;
  1761. return 0;
  1762. }
  1763. /*
  1764. * Called from soc_camera_probe() above with .host_lock held
  1765. */
  1766. static int soc_camera_video_start(struct soc_camera_device *icd)
  1767. {
  1768. const struct device_type *type = icd->vdev->dev.type;
  1769. int ret;
  1770. if (!icd->parent)
  1771. return -ENODEV;
  1772. video_set_drvdata(icd->vdev, icd);
  1773. if (icd->vdev->tvnorms == 0) {
  1774. /* disable the STD API if there are no tvnorms defined */
  1775. v4l2_disable_ioctl(icd->vdev, VIDIOC_G_STD);
  1776. v4l2_disable_ioctl(icd->vdev, VIDIOC_S_STD);
  1777. v4l2_disable_ioctl(icd->vdev, VIDIOC_ENUMSTD);
  1778. }
  1779. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1780. if (ret < 0) {
  1781. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1782. return ret;
  1783. }
  1784. /* Restore device type, possibly set by the subdevice driver */
  1785. icd->vdev->dev.type = type;
  1786. return 0;
  1787. }
  1788. static int soc_camera_pdrv_probe(struct platform_device *pdev)
  1789. {
  1790. struct soc_camera_desc *sdesc = pdev->dev.platform_data;
  1791. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  1792. struct soc_camera_device *icd;
  1793. int ret;
  1794. if (!sdesc)
  1795. return -EINVAL;
  1796. icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
  1797. if (!icd)
  1798. return -ENOMEM;
  1799. /*
  1800. * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below
  1801. * regulator allocation is a dummy. They are actually requested by the
  1802. * subdevice driver, using soc_camera_power_init(). Also note, that in
  1803. * that case regulators are attached to the I2C device and not to the
  1804. * camera platform device.
  1805. */
  1806. ret = devm_regulator_bulk_get(&pdev->dev, ssdd->sd_pdata.num_regulators,
  1807. ssdd->sd_pdata.regulators);
  1808. if (ret < 0)
  1809. return ret;
  1810. icd->iface = sdesc->host_desc.bus_id;
  1811. icd->sdesc = sdesc;
  1812. icd->pdev = &pdev->dev;
  1813. platform_set_drvdata(pdev, icd);
  1814. icd->user_width = DEFAULT_WIDTH;
  1815. icd->user_height = DEFAULT_HEIGHT;
  1816. return soc_camera_device_register(icd);
  1817. }
  1818. /*
  1819. * Only called on rmmod for each platform device, since they are not
  1820. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1821. * been unloaded already
  1822. */
  1823. static int soc_camera_pdrv_remove(struct platform_device *pdev)
  1824. {
  1825. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1826. int i;
  1827. if (!icd)
  1828. return -EINVAL;
  1829. i = pdev->id;
  1830. if (i < 0)
  1831. i = 0;
  1832. /*
  1833. * In synchronous mode with static platform devices this is called in a
  1834. * loop from drivers/base/dd.c::driver_detach(), no parallel execution,
  1835. * no need to lock. In asynchronous case the caller -
  1836. * soc_camera_host_unregister() - already holds the lock
  1837. */
  1838. if (test_bit(i, device_map)) {
  1839. clear_bit(i, device_map);
  1840. list_del(&icd->list);
  1841. }
  1842. return 0;
  1843. }
  1844. static struct platform_driver __refdata soc_camera_pdrv = {
  1845. .probe = soc_camera_pdrv_probe,
  1846. .remove = soc_camera_pdrv_remove,
  1847. .driver = {
  1848. .name = "soc-camera-pdrv",
  1849. },
  1850. };
  1851. module_platform_driver(soc_camera_pdrv);
  1852. MODULE_DESCRIPTION("Image capture bus driver");
  1853. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1854. MODULE_LICENSE("GPL");
  1855. MODULE_ALIAS("platform:soc-camera-pdrv");