soc_camera.c 56 KB

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