soc_camera.c 52 KB

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