soc_camera.c 53 KB

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