soc_camera.c 56 KB

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