soc_camera.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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/mutex.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/vmalloc.h>
  30. #include <media/soc_camera.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-dev.h>
  34. #include <media/videobuf-core.h>
  35. #include <media/videobuf2-core.h>
  36. #include <media/soc_mediabus.h>
  37. /* Default to VGA resolution */
  38. #define DEFAULT_WIDTH 640
  39. #define DEFAULT_HEIGHT 480
  40. #define is_streaming(ici, icd) \
  41. (((ici)->ops->init_videobuf) ? \
  42. (icd)->vb_vidq.streaming : \
  43. vb2_is_streaming(&(icd)->vb2_vidq))
  44. static LIST_HEAD(hosts);
  45. static LIST_HEAD(devices);
  46. static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
  47. int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd)
  48. {
  49. int ret = regulator_bulk_enable(ssdd->num_regulators,
  50. ssdd->regulators);
  51. if (ret < 0) {
  52. dev_err(dev, "Cannot enable regulators\n");
  53. return ret;
  54. }
  55. if (ssdd->power) {
  56. ret = ssdd->power(dev, 1);
  57. if (ret < 0) {
  58. dev_err(dev,
  59. "Platform failed to power-on the camera.\n");
  60. regulator_bulk_disable(ssdd->num_regulators,
  61. ssdd->regulators);
  62. }
  63. }
  64. return ret;
  65. }
  66. EXPORT_SYMBOL(soc_camera_power_on);
  67. int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd)
  68. {
  69. int ret = 0;
  70. int err;
  71. if (ssdd->power) {
  72. err = ssdd->power(dev, 0);
  73. if (err < 0) {
  74. dev_err(dev,
  75. "Platform failed to power-off the camera.\n");
  76. ret = err;
  77. }
  78. }
  79. err = regulator_bulk_disable(ssdd->num_regulators,
  80. ssdd->regulators);
  81. if (err < 0) {
  82. dev_err(dev, "Cannot disable regulators\n");
  83. ret = ret ? : err;
  84. }
  85. return ret;
  86. }
  87. EXPORT_SYMBOL(soc_camera_power_off);
  88. static int __soc_camera_power_on(struct soc_camera_device *icd)
  89. {
  90. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  91. int ret;
  92. ret = v4l2_subdev_call(sd, core, s_power, 1);
  93. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  94. return ret;
  95. return 0;
  96. }
  97. static int __soc_camera_power_off(struct soc_camera_device *icd)
  98. {
  99. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  100. int ret;
  101. ret = v4l2_subdev_call(sd, core, s_power, 0);
  102. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  103. return ret;
  104. return 0;
  105. }
  106. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  107. struct soc_camera_device *icd, unsigned int fourcc)
  108. {
  109. unsigned int i;
  110. for (i = 0; i < icd->num_user_formats; i++)
  111. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  112. return icd->user_formats + i;
  113. return NULL;
  114. }
  115. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  116. /**
  117. * soc_camera_apply_board_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  118. * @ssdd: camera platform parameters
  119. * @cfg: media bus configuration
  120. * @return: resulting flags
  121. */
  122. unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd,
  123. const struct v4l2_mbus_config *cfg)
  124. {
  125. unsigned long f, flags = cfg->flags;
  126. /* If only one of the two polarities is supported, switch to the opposite */
  127. if (ssdd->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  128. f = flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW);
  129. if (f == V4L2_MBUS_HSYNC_ACTIVE_HIGH || f == V4L2_MBUS_HSYNC_ACTIVE_LOW)
  130. flags ^= V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW;
  131. }
  132. if (ssdd->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  133. f = flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW);
  134. if (f == V4L2_MBUS_VSYNC_ACTIVE_HIGH || f == V4L2_MBUS_VSYNC_ACTIVE_LOW)
  135. flags ^= V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW;
  136. }
  137. if (ssdd->flags & SOCAM_SENSOR_INVERT_PCLK) {
  138. f = flags & (V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING);
  139. if (f == V4L2_MBUS_PCLK_SAMPLE_RISING || f == V4L2_MBUS_PCLK_SAMPLE_FALLING)
  140. flags ^= V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING;
  141. }
  142. return flags;
  143. }
  144. EXPORT_SYMBOL(soc_camera_apply_board_flags);
  145. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  146. ((x) >> 24) & 0xff
  147. static int soc_camera_try_fmt(struct soc_camera_device *icd,
  148. struct v4l2_format *f)
  149. {
  150. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  151. const struct soc_camera_format_xlate *xlate;
  152. struct v4l2_pix_format *pix = &f->fmt.pix;
  153. int ret;
  154. dev_dbg(icd->pdev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
  155. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  156. if (pix->pixelformat != V4L2_PIX_FMT_JPEG &&
  157. !(ici->capabilities & SOCAM_HOST_CAP_STRIDE)) {
  158. pix->bytesperline = 0;
  159. pix->sizeimage = 0;
  160. }
  161. ret = ici->ops->try_fmt(icd, f);
  162. if (ret < 0)
  163. return ret;
  164. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  165. if (!xlate)
  166. return -EINVAL;
  167. ret = soc_mbus_bytes_per_line(pix->width, xlate->host_fmt);
  168. if (ret < 0)
  169. return ret;
  170. pix->bytesperline = max_t(u32, pix->bytesperline, ret);
  171. ret = soc_mbus_image_size(xlate->host_fmt, pix->bytesperline,
  172. pix->height);
  173. if (ret < 0)
  174. return ret;
  175. pix->sizeimage = max_t(u32, pix->sizeimage, ret);
  176. return 0;
  177. }
  178. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  179. struct v4l2_format *f)
  180. {
  181. struct soc_camera_device *icd = file->private_data;
  182. WARN_ON(priv != file->private_data);
  183. /* Only single-plane capture is supported so far */
  184. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  185. return -EINVAL;
  186. /* limit format to hardware capabilities */
  187. return soc_camera_try_fmt(icd, f);
  188. }
  189. static int soc_camera_enum_input(struct file *file, void *priv,
  190. struct v4l2_input *inp)
  191. {
  192. if (inp->index != 0)
  193. return -EINVAL;
  194. /* default is camera */
  195. inp->type = V4L2_INPUT_TYPE_CAMERA;
  196. strcpy(inp->name, "Camera");
  197. return 0;
  198. }
  199. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  200. {
  201. *i = 0;
  202. return 0;
  203. }
  204. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  205. {
  206. if (i > 0)
  207. return -EINVAL;
  208. return 0;
  209. }
  210. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id a)
  211. {
  212. struct soc_camera_device *icd = file->private_data;
  213. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  214. return v4l2_subdev_call(sd, core, s_std, a);
  215. }
  216. static int soc_camera_g_std(struct file *file, void *priv, v4l2_std_id *a)
  217. {
  218. struct soc_camera_device *icd = file->private_data;
  219. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  220. return v4l2_subdev_call(sd, core, g_std, a);
  221. }
  222. static int soc_camera_enum_framesizes(struct file *file, void *fh,
  223. struct v4l2_frmsizeenum *fsize)
  224. {
  225. struct soc_camera_device *icd = file->private_data;
  226. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  227. return ici->ops->enum_framesizes(icd, fsize);
  228. }
  229. static int soc_camera_reqbufs(struct file *file, void *priv,
  230. struct v4l2_requestbuffers *p)
  231. {
  232. int ret;
  233. struct soc_camera_device *icd = file->private_data;
  234. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  235. WARN_ON(priv != file->private_data);
  236. if (icd->streamer && icd->streamer != file)
  237. return -EBUSY;
  238. if (ici->ops->init_videobuf) {
  239. ret = videobuf_reqbufs(&icd->vb_vidq, p);
  240. if (ret < 0)
  241. return ret;
  242. ret = ici->ops->reqbufs(icd, p);
  243. } else {
  244. ret = vb2_reqbufs(&icd->vb2_vidq, p);
  245. }
  246. if (!ret && !icd->streamer)
  247. icd->streamer = file;
  248. return ret;
  249. }
  250. static int soc_camera_querybuf(struct file *file, void *priv,
  251. struct v4l2_buffer *p)
  252. {
  253. struct soc_camera_device *icd = file->private_data;
  254. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  255. WARN_ON(priv != file->private_data);
  256. if (ici->ops->init_videobuf)
  257. return videobuf_querybuf(&icd->vb_vidq, p);
  258. else
  259. return vb2_querybuf(&icd->vb2_vidq, p);
  260. }
  261. static int soc_camera_qbuf(struct file *file, void *priv,
  262. struct v4l2_buffer *p)
  263. {
  264. struct soc_camera_device *icd = file->private_data;
  265. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  266. WARN_ON(priv != file->private_data);
  267. if (icd->streamer != file)
  268. return -EBUSY;
  269. if (ici->ops->init_videobuf)
  270. return videobuf_qbuf(&icd->vb_vidq, p);
  271. else
  272. return vb2_qbuf(&icd->vb2_vidq, p);
  273. }
  274. static int soc_camera_dqbuf(struct file *file, void *priv,
  275. struct v4l2_buffer *p)
  276. {
  277. struct soc_camera_device *icd = file->private_data;
  278. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  279. WARN_ON(priv != file->private_data);
  280. if (icd->streamer != file)
  281. return -EBUSY;
  282. if (ici->ops->init_videobuf)
  283. return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
  284. else
  285. return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
  286. }
  287. static int soc_camera_create_bufs(struct file *file, void *priv,
  288. struct v4l2_create_buffers *create)
  289. {
  290. struct soc_camera_device *icd = file->private_data;
  291. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  292. /* videobuf2 only */
  293. if (ici->ops->init_videobuf)
  294. return -EINVAL;
  295. else
  296. return vb2_create_bufs(&icd->vb2_vidq, create);
  297. }
  298. static int soc_camera_prepare_buf(struct file *file, void *priv,
  299. struct v4l2_buffer *b)
  300. {
  301. struct soc_camera_device *icd = file->private_data;
  302. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  303. /* videobuf2 only */
  304. if (ici->ops->init_videobuf)
  305. return -EINVAL;
  306. else
  307. return vb2_prepare_buf(&icd->vb2_vidq, b);
  308. }
  309. /* Always entered with .host_lock held */
  310. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  311. {
  312. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  313. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  314. unsigned int i, fmts = 0, raw_fmts = 0;
  315. int ret;
  316. enum v4l2_mbus_pixelcode code;
  317. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  318. raw_fmts++;
  319. if (!ici->ops->get_formats)
  320. /*
  321. * Fallback mode - the host will have to serve all
  322. * sensor-provided formats one-to-one to the user
  323. */
  324. fmts = raw_fmts;
  325. else
  326. /*
  327. * First pass - only count formats this host-sensor
  328. * configuration can provide
  329. */
  330. for (i = 0; i < raw_fmts; i++) {
  331. ret = ici->ops->get_formats(icd, i, NULL);
  332. if (ret < 0)
  333. return ret;
  334. fmts += ret;
  335. }
  336. if (!fmts)
  337. return -ENXIO;
  338. icd->user_formats =
  339. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  340. if (!icd->user_formats)
  341. return -ENOMEM;
  342. dev_dbg(icd->pdev, "Found %d supported formats.\n", fmts);
  343. /* Second pass - actually fill data formats */
  344. fmts = 0;
  345. for (i = 0; i < raw_fmts; i++)
  346. if (!ici->ops->get_formats) {
  347. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  348. icd->user_formats[fmts].host_fmt =
  349. soc_mbus_get_fmtdesc(code);
  350. if (icd->user_formats[fmts].host_fmt)
  351. icd->user_formats[fmts++].code = code;
  352. } else {
  353. ret = ici->ops->get_formats(icd, i,
  354. &icd->user_formats[fmts]);
  355. if (ret < 0)
  356. goto egfmt;
  357. fmts += ret;
  358. }
  359. icd->num_user_formats = fmts;
  360. icd->current_fmt = &icd->user_formats[0];
  361. return 0;
  362. egfmt:
  363. vfree(icd->user_formats);
  364. return ret;
  365. }
  366. /* Always entered with .host_lock held */
  367. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  368. {
  369. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  370. if (ici->ops->put_formats)
  371. ici->ops->put_formats(icd);
  372. icd->current_fmt = NULL;
  373. icd->num_user_formats = 0;
  374. vfree(icd->user_formats);
  375. icd->user_formats = NULL;
  376. }
  377. /* Called with .vb_lock held, or from the first open(2), see comment there */
  378. static int soc_camera_set_fmt(struct soc_camera_device *icd,
  379. struct v4l2_format *f)
  380. {
  381. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  382. struct v4l2_pix_format *pix = &f->fmt.pix;
  383. int ret;
  384. dev_dbg(icd->pdev, "S_FMT(%c%c%c%c, %ux%u)\n",
  385. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  386. /* We always call try_fmt() before set_fmt() or set_crop() */
  387. ret = soc_camera_try_fmt(icd, f);
  388. if (ret < 0)
  389. return ret;
  390. ret = ici->ops->set_fmt(icd, f);
  391. if (ret < 0) {
  392. return ret;
  393. } else if (!icd->current_fmt ||
  394. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  395. dev_err(icd->pdev,
  396. "Host driver hasn't set up current format correctly!\n");
  397. return -EINVAL;
  398. }
  399. icd->user_width = pix->width;
  400. icd->user_height = pix->height;
  401. icd->bytesperline = pix->bytesperline;
  402. icd->sizeimage = pix->sizeimage;
  403. icd->colorspace = pix->colorspace;
  404. icd->field = pix->field;
  405. if (ici->ops->init_videobuf)
  406. icd->vb_vidq.field = pix->field;
  407. dev_dbg(icd->pdev, "set width: %d height: %d\n",
  408. icd->user_width, icd->user_height);
  409. /* set physical bus parameters */
  410. return ici->ops->set_bus_param(icd);
  411. }
  412. static int soc_camera_add_device(struct soc_camera_device *icd)
  413. {
  414. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  415. int ret;
  416. if (ici->icd)
  417. return -EBUSY;
  418. ret = ici->ops->clock_start(ici);
  419. if (ret < 0)
  420. return ret;
  421. if (ici->ops->add) {
  422. ret = ici->ops->add(icd);
  423. if (ret < 0)
  424. goto eadd;
  425. }
  426. ici->icd = icd;
  427. return 0;
  428. eadd:
  429. ici->ops->clock_stop(ici);
  430. return ret;
  431. }
  432. static void soc_camera_remove_device(struct soc_camera_device *icd)
  433. {
  434. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  435. if (WARN_ON(icd != ici->icd))
  436. return;
  437. if (ici->ops->remove)
  438. ici->ops->remove(icd);
  439. ici->ops->clock_stop(ici);
  440. ici->icd = NULL;
  441. }
  442. static int soc_camera_open(struct file *file)
  443. {
  444. struct video_device *vdev = video_devdata(file);
  445. struct soc_camera_device *icd;
  446. struct soc_camera_host *ici;
  447. int ret;
  448. /*
  449. * Don't mess with the host during probe: wait until the loop in
  450. * scan_add_host() completes. Also protect against a race with
  451. * soc_camera_host_unregister().
  452. */
  453. if (mutex_lock_interruptible(&list_lock))
  454. return -ERESTARTSYS;
  455. if (!vdev || !video_is_registered(vdev)) {
  456. mutex_unlock(&list_lock);
  457. return -ENODEV;
  458. }
  459. icd = video_get_drvdata(vdev);
  460. ici = to_soc_camera_host(icd->parent);
  461. ret = try_module_get(ici->ops->owner) ? 0 : -ENODEV;
  462. mutex_unlock(&list_lock);
  463. if (ret < 0) {
  464. dev_err(icd->pdev, "Couldn't lock capture bus driver.\n");
  465. return ret;
  466. }
  467. if (!to_soc_camera_control(icd)) {
  468. /* No device driver attached */
  469. ret = -ENODEV;
  470. goto econtrol;
  471. }
  472. if (mutex_lock_interruptible(&ici->host_lock)) {
  473. ret = -ERESTARTSYS;
  474. goto elockhost;
  475. }
  476. icd->use_count++;
  477. /* Now we really have to activate the camera */
  478. if (icd->use_count == 1) {
  479. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  480. /* Restore parameters before the last close() per V4L2 API */
  481. struct v4l2_format f = {
  482. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  483. .fmt.pix = {
  484. .width = icd->user_width,
  485. .height = icd->user_height,
  486. .field = icd->field,
  487. .colorspace = icd->colorspace,
  488. .pixelformat =
  489. icd->current_fmt->host_fmt->fourcc,
  490. },
  491. };
  492. /* The camera could have been already on, try to reset */
  493. if (sdesc->subdev_desc.reset)
  494. sdesc->subdev_desc.reset(icd->pdev);
  495. ret = soc_camera_add_device(icd);
  496. if (ret < 0) {
  497. dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret);
  498. goto eiciadd;
  499. }
  500. ret = __soc_camera_power_on(icd);
  501. if (ret < 0)
  502. goto epower;
  503. pm_runtime_enable(&icd->vdev->dev);
  504. ret = pm_runtime_resume(&icd->vdev->dev);
  505. if (ret < 0 && ret != -ENOSYS)
  506. goto eresume;
  507. /*
  508. * Try to configure with default parameters. Notice: this is the
  509. * very first open, so, we cannot race against other calls,
  510. * apart from someone else calling open() simultaneously, but
  511. * .host_lock is protecting us against it.
  512. */
  513. ret = soc_camera_set_fmt(icd, &f);
  514. if (ret < 0)
  515. goto esfmt;
  516. if (ici->ops->init_videobuf) {
  517. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  518. } else {
  519. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  520. if (ret < 0)
  521. goto einitvb;
  522. }
  523. v4l2_ctrl_handler_setup(&icd->ctrl_handler);
  524. }
  525. mutex_unlock(&ici->host_lock);
  526. file->private_data = icd;
  527. dev_dbg(icd->pdev, "camera device open\n");
  528. return 0;
  529. /*
  530. * First four errors are entered with the .host_lock held
  531. * and use_count == 1
  532. */
  533. einitvb:
  534. esfmt:
  535. pm_runtime_disable(&icd->vdev->dev);
  536. eresume:
  537. __soc_camera_power_off(icd);
  538. epower:
  539. soc_camera_remove_device(icd);
  540. eiciadd:
  541. icd->use_count--;
  542. mutex_unlock(&ici->host_lock);
  543. elockhost:
  544. econtrol:
  545. module_put(ici->ops->owner);
  546. return ret;
  547. }
  548. static int soc_camera_close(struct file *file)
  549. {
  550. struct soc_camera_device *icd = file->private_data;
  551. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  552. mutex_lock(&ici->host_lock);
  553. icd->use_count--;
  554. if (!icd->use_count) {
  555. pm_runtime_suspend(&icd->vdev->dev);
  556. pm_runtime_disable(&icd->vdev->dev);
  557. if (ici->ops->init_videobuf2)
  558. vb2_queue_release(&icd->vb2_vidq);
  559. __soc_camera_power_off(icd);
  560. soc_camera_remove_device(icd);
  561. }
  562. if (icd->streamer == file)
  563. icd->streamer = NULL;
  564. mutex_unlock(&ici->host_lock);
  565. module_put(ici->ops->owner);
  566. dev_dbg(icd->pdev, "camera device close\n");
  567. return 0;
  568. }
  569. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  570. size_t count, loff_t *ppos)
  571. {
  572. struct soc_camera_device *icd = file->private_data;
  573. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  574. dev_dbg(icd->pdev, "read called, buf %p\n", buf);
  575. if (ici->ops->init_videobuf2 && icd->vb2_vidq.io_modes & VB2_READ)
  576. return vb2_read(&icd->vb2_vidq, buf, count, ppos,
  577. file->f_flags & O_NONBLOCK);
  578. dev_err(icd->pdev, "camera device read not implemented\n");
  579. return -EINVAL;
  580. }
  581. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  582. {
  583. struct soc_camera_device *icd = file->private_data;
  584. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  585. int err;
  586. dev_dbg(icd->pdev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  587. if (icd->streamer != file)
  588. return -EBUSY;
  589. if (mutex_lock_interruptible(&ici->host_lock))
  590. return -ERESTARTSYS;
  591. if (ici->ops->init_videobuf)
  592. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  593. else
  594. err = vb2_mmap(&icd->vb2_vidq, vma);
  595. mutex_unlock(&ici->host_lock);
  596. dev_dbg(icd->pdev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  597. (unsigned long)vma->vm_start,
  598. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  599. err);
  600. return err;
  601. }
  602. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  603. {
  604. struct soc_camera_device *icd = file->private_data;
  605. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  606. unsigned res = POLLERR;
  607. if (icd->streamer != file)
  608. return POLLERR;
  609. mutex_lock(&ici->host_lock);
  610. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream))
  611. dev_err(icd->pdev, "Trying to poll with no queued buffers!\n");
  612. else
  613. res = ici->ops->poll(file, pt);
  614. mutex_unlock(&ici->host_lock);
  615. return res;
  616. }
  617. void soc_camera_lock(struct vb2_queue *vq)
  618. {
  619. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  620. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  621. mutex_lock(&ici->host_lock);
  622. }
  623. EXPORT_SYMBOL(soc_camera_lock);
  624. void soc_camera_unlock(struct vb2_queue *vq)
  625. {
  626. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  627. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  628. mutex_unlock(&ici->host_lock);
  629. }
  630. EXPORT_SYMBOL(soc_camera_unlock);
  631. static struct v4l2_file_operations soc_camera_fops = {
  632. .owner = THIS_MODULE,
  633. .open = soc_camera_open,
  634. .release = soc_camera_close,
  635. .unlocked_ioctl = video_ioctl2,
  636. .read = soc_camera_read,
  637. .mmap = soc_camera_mmap,
  638. .poll = soc_camera_poll,
  639. };
  640. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  641. struct v4l2_format *f)
  642. {
  643. struct soc_camera_device *icd = file->private_data;
  644. int ret;
  645. WARN_ON(priv != file->private_data);
  646. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  647. dev_warn(icd->pdev, "Wrong buf-type %d\n", f->type);
  648. return -EINVAL;
  649. }
  650. if (icd->streamer && icd->streamer != file)
  651. return -EBUSY;
  652. if (is_streaming(to_soc_camera_host(icd->parent), icd)) {
  653. dev_err(icd->pdev, "S_FMT denied: queue initialised\n");
  654. return -EBUSY;
  655. }
  656. ret = soc_camera_set_fmt(icd, f);
  657. if (!ret && !icd->streamer)
  658. icd->streamer = file;
  659. return ret;
  660. }
  661. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  662. struct v4l2_fmtdesc *f)
  663. {
  664. struct soc_camera_device *icd = file->private_data;
  665. const struct soc_mbus_pixelfmt *format;
  666. WARN_ON(priv != file->private_data);
  667. if (f->index >= icd->num_user_formats)
  668. return -EINVAL;
  669. format = icd->user_formats[f->index].host_fmt;
  670. if (format->name)
  671. strlcpy(f->description, format->name, sizeof(f->description));
  672. f->pixelformat = format->fourcc;
  673. return 0;
  674. }
  675. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  676. struct v4l2_format *f)
  677. {
  678. struct soc_camera_device *icd = file->private_data;
  679. struct v4l2_pix_format *pix = &f->fmt.pix;
  680. WARN_ON(priv != file->private_data);
  681. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  682. return -EINVAL;
  683. pix->width = icd->user_width;
  684. pix->height = icd->user_height;
  685. pix->bytesperline = icd->bytesperline;
  686. pix->sizeimage = icd->sizeimage;
  687. pix->field = icd->field;
  688. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  689. pix->colorspace = icd->colorspace;
  690. dev_dbg(icd->pdev, "current_fmt->fourcc: 0x%08x\n",
  691. icd->current_fmt->host_fmt->fourcc);
  692. return 0;
  693. }
  694. static int soc_camera_querycap(struct file *file, void *priv,
  695. struct v4l2_capability *cap)
  696. {
  697. struct soc_camera_device *icd = file->private_data;
  698. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  699. WARN_ON(priv != file->private_data);
  700. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  701. return ici->ops->querycap(ici, cap);
  702. }
  703. static int soc_camera_streamon(struct file *file, void *priv,
  704. enum v4l2_buf_type i)
  705. {
  706. struct soc_camera_device *icd = file->private_data;
  707. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  708. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  709. int ret;
  710. WARN_ON(priv != file->private_data);
  711. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  712. return -EINVAL;
  713. if (icd->streamer != file)
  714. return -EBUSY;
  715. /* This calls buf_queue from host driver's videobuf_queue_ops */
  716. if (ici->ops->init_videobuf)
  717. ret = videobuf_streamon(&icd->vb_vidq);
  718. else
  719. ret = vb2_streamon(&icd->vb2_vidq, i);
  720. if (!ret)
  721. v4l2_subdev_call(sd, video, s_stream, 1);
  722. return ret;
  723. }
  724. static int soc_camera_streamoff(struct file *file, void *priv,
  725. enum v4l2_buf_type i)
  726. {
  727. struct soc_camera_device *icd = file->private_data;
  728. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  729. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  730. WARN_ON(priv != file->private_data);
  731. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  732. return -EINVAL;
  733. if (icd->streamer != file)
  734. return -EBUSY;
  735. /*
  736. * This calls buf_release from host driver's videobuf_queue_ops for all
  737. * remaining buffers. When the last buffer is freed, stop capture
  738. */
  739. if (ici->ops->init_videobuf)
  740. videobuf_streamoff(&icd->vb_vidq);
  741. else
  742. vb2_streamoff(&icd->vb2_vidq, i);
  743. v4l2_subdev_call(sd, video, s_stream, 0);
  744. return 0;
  745. }
  746. static int soc_camera_cropcap(struct file *file, void *fh,
  747. struct v4l2_cropcap *a)
  748. {
  749. struct soc_camera_device *icd = file->private_data;
  750. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  751. return ici->ops->cropcap(icd, a);
  752. }
  753. static int soc_camera_g_crop(struct file *file, void *fh,
  754. struct v4l2_crop *a)
  755. {
  756. struct soc_camera_device *icd = file->private_data;
  757. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  758. int ret;
  759. ret = ici->ops->get_crop(icd, a);
  760. return ret;
  761. }
  762. /*
  763. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  764. * argument with the actual geometry, instead, the user shall use G_CROP to
  765. * retrieve it.
  766. */
  767. static int soc_camera_s_crop(struct file *file, void *fh,
  768. const struct v4l2_crop *a)
  769. {
  770. struct soc_camera_device *icd = file->private_data;
  771. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  772. const struct v4l2_rect *rect = &a->c;
  773. struct v4l2_crop current_crop;
  774. int ret;
  775. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  776. return -EINVAL;
  777. dev_dbg(icd->pdev, "S_CROP(%ux%u@%u:%u)\n",
  778. rect->width, rect->height, rect->left, rect->top);
  779. current_crop.type = a->type;
  780. /* If get_crop fails, we'll let host and / or client drivers decide */
  781. ret = ici->ops->get_crop(icd, &current_crop);
  782. /* Prohibit window size change with initialised buffers */
  783. if (ret < 0) {
  784. dev_err(icd->pdev,
  785. "S_CROP denied: getting current crop failed\n");
  786. } else if ((a->c.width == current_crop.c.width &&
  787. a->c.height == current_crop.c.height) ||
  788. !is_streaming(ici, icd)) {
  789. /* same size or not streaming - use .set_crop() */
  790. ret = ici->ops->set_crop(icd, a);
  791. } else if (ici->ops->set_livecrop) {
  792. ret = ici->ops->set_livecrop(icd, a);
  793. } else {
  794. dev_err(icd->pdev,
  795. "S_CROP denied: queue initialised and sizes differ\n");
  796. ret = -EBUSY;
  797. }
  798. return ret;
  799. }
  800. static int soc_camera_g_selection(struct file *file, void *fh,
  801. struct v4l2_selection *s)
  802. {
  803. struct soc_camera_device *icd = file->private_data;
  804. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  805. /* With a wrong type no need to try to fall back to cropping */
  806. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  807. return -EINVAL;
  808. if (!ici->ops->get_selection)
  809. return -ENOTTY;
  810. return ici->ops->get_selection(icd, s);
  811. }
  812. static int soc_camera_s_selection(struct file *file, void *fh,
  813. struct v4l2_selection *s)
  814. {
  815. struct soc_camera_device *icd = file->private_data;
  816. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  817. int ret;
  818. /* In all these cases cropping emulation will not help */
  819. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  820. (s->target != V4L2_SEL_TGT_COMPOSE &&
  821. s->target != V4L2_SEL_TGT_CROP))
  822. return -EINVAL;
  823. if (s->target == V4L2_SEL_TGT_COMPOSE) {
  824. /* No output size change during a running capture! */
  825. if (is_streaming(ici, icd) &&
  826. (icd->user_width != s->r.width ||
  827. icd->user_height != s->r.height))
  828. return -EBUSY;
  829. /*
  830. * Only one user is allowed to change the output format, touch
  831. * buffers, start / stop streaming, poll for data
  832. */
  833. if (icd->streamer && icd->streamer != file)
  834. return -EBUSY;
  835. }
  836. if (!ici->ops->set_selection)
  837. return -ENOTTY;
  838. ret = ici->ops->set_selection(icd, s);
  839. if (!ret &&
  840. s->target == V4L2_SEL_TGT_COMPOSE) {
  841. icd->user_width = s->r.width;
  842. icd->user_height = s->r.height;
  843. if (!icd->streamer)
  844. icd->streamer = file;
  845. }
  846. return ret;
  847. }
  848. static int soc_camera_g_parm(struct file *file, void *fh,
  849. struct v4l2_streamparm *a)
  850. {
  851. struct soc_camera_device *icd = file->private_data;
  852. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  853. if (ici->ops->get_parm)
  854. return ici->ops->get_parm(icd, a);
  855. return -ENOIOCTLCMD;
  856. }
  857. static int soc_camera_s_parm(struct file *file, void *fh,
  858. struct v4l2_streamparm *a)
  859. {
  860. struct soc_camera_device *icd = file->private_data;
  861. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  862. if (ici->ops->set_parm)
  863. return ici->ops->set_parm(icd, a);
  864. return -ENOIOCTLCMD;
  865. }
  866. static int soc_camera_probe(struct soc_camera_device *icd);
  867. /* So far this function cannot fail */
  868. static void scan_add_host(struct soc_camera_host *ici)
  869. {
  870. struct soc_camera_device *icd;
  871. mutex_lock(&list_lock);
  872. list_for_each_entry(icd, &devices, list) {
  873. if (icd->iface == ici->nr) {
  874. icd->parent = ici->v4l2_dev.dev;
  875. soc_camera_probe(icd);
  876. }
  877. }
  878. mutex_unlock(&list_lock);
  879. }
  880. #ifdef CONFIG_I2C_BOARDINFO
  881. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  882. struct soc_camera_desc *sdesc)
  883. {
  884. struct i2c_client *client;
  885. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  886. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  887. struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id);
  888. struct v4l2_subdev *subdev;
  889. if (!adap) {
  890. dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n",
  891. shd->i2c_adapter_id);
  892. goto ei2cga;
  893. }
  894. shd->board_info->platform_data = &sdesc->subdev_desc;
  895. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  896. shd->board_info, NULL);
  897. if (!subdev)
  898. goto ei2cnd;
  899. client = v4l2_get_subdevdata(subdev);
  900. /* Use to_i2c_client(dev) to recover the i2c client */
  901. icd->control = &client->dev;
  902. return 0;
  903. ei2cnd:
  904. i2c_put_adapter(adap);
  905. ei2cga:
  906. return -ENODEV;
  907. }
  908. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  909. {
  910. struct i2c_client *client =
  911. to_i2c_client(to_soc_camera_control(icd));
  912. struct i2c_adapter *adap = client->adapter;
  913. icd->control = NULL;
  914. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  915. i2c_unregister_device(client);
  916. i2c_put_adapter(adap);
  917. }
  918. #else
  919. #define soc_camera_init_i2c(icd, sdesc) (-ENODEV)
  920. #define soc_camera_free_i2c(icd) do {} while (0)
  921. #endif
  922. static int soc_camera_video_start(struct soc_camera_device *icd);
  923. static int video_dev_create(struct soc_camera_device *icd);
  924. /* Called during host-driver probe */
  925. static int soc_camera_probe(struct soc_camera_device *icd)
  926. {
  927. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  928. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  929. struct soc_camera_host_desc *shd = &sdesc->host_desc;
  930. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  931. struct device *control = NULL;
  932. struct v4l2_subdev *sd;
  933. struct v4l2_mbus_framefmt mf;
  934. int ret;
  935. dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev));
  936. /*
  937. * Currently the subdev with the largest number of controls (13) is
  938. * ov6550. So let's pick 16 as a hint for the control handler. Note
  939. * that this is a hint only: too large and you waste some memory, too
  940. * small and there is a (very) small performance hit when looking up
  941. * controls in the internal hash.
  942. */
  943. ret = v4l2_ctrl_handler_init(&icd->ctrl_handler, 16);
  944. if (ret < 0)
  945. return ret;
  946. /* The camera could have been already on, try to reset */
  947. if (ssdd->reset)
  948. ssdd->reset(icd->pdev);
  949. mutex_lock(&ici->host_lock);
  950. ret = ici->ops->clock_start(ici);
  951. mutex_unlock(&ici->host_lock);
  952. if (ret < 0)
  953. goto eadd;
  954. /* Must have icd->vdev before registering the device */
  955. ret = video_dev_create(icd);
  956. if (ret < 0)
  957. goto evdc;
  958. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  959. if (shd->board_info) {
  960. ret = soc_camera_init_i2c(icd, sdesc);
  961. if (ret < 0)
  962. goto eadddev;
  963. } else if (!shd->add_device || !shd->del_device) {
  964. ret = -EINVAL;
  965. goto eadddev;
  966. } else {
  967. if (shd->module_name)
  968. ret = request_module(shd->module_name);
  969. ret = shd->add_device(icd);
  970. if (ret < 0)
  971. goto eadddev;
  972. /*
  973. * FIXME: this is racy, have to use driver-binding notification,
  974. * when it is available
  975. */
  976. control = to_soc_camera_control(icd);
  977. if (!control || !control->driver || !dev_get_drvdata(control) ||
  978. !try_module_get(control->driver->owner)) {
  979. shd->del_device(icd);
  980. ret = -ENODEV;
  981. goto enodrv;
  982. }
  983. }
  984. sd = soc_camera_to_subdev(icd);
  985. sd->grp_id = soc_camera_grp_id(icd);
  986. v4l2_set_subdev_hostdata(sd, icd);
  987. ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL);
  988. if (ret < 0)
  989. goto ectrl;
  990. /* At this point client .probe() should have run already */
  991. ret = soc_camera_init_user_formats(icd);
  992. if (ret < 0)
  993. goto eiufmt;
  994. icd->field = V4L2_FIELD_ANY;
  995. /*
  996. * ..._video_start() will create a device node, video_register_device()
  997. * itself is protected against concurrent open() calls, but we also have
  998. * to protect our data.
  999. */
  1000. mutex_lock(&ici->host_lock);
  1001. ret = soc_camera_video_start(icd);
  1002. if (ret < 0)
  1003. goto evidstart;
  1004. /* Try to improve our guess of a reasonable window format */
  1005. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  1006. icd->user_width = mf.width;
  1007. icd->user_height = mf.height;
  1008. icd->colorspace = mf.colorspace;
  1009. icd->field = mf.field;
  1010. }
  1011. ici->ops->clock_stop(ici);
  1012. mutex_unlock(&ici->host_lock);
  1013. return 0;
  1014. evidstart:
  1015. mutex_unlock(&ici->host_lock);
  1016. soc_camera_free_user_formats(icd);
  1017. eiufmt:
  1018. ectrl:
  1019. if (shd->board_info) {
  1020. soc_camera_free_i2c(icd);
  1021. } else {
  1022. shd->del_device(icd);
  1023. module_put(control->driver->owner);
  1024. }
  1025. enodrv:
  1026. eadddev:
  1027. video_device_release(icd->vdev);
  1028. icd->vdev = NULL;
  1029. evdc:
  1030. mutex_lock(&ici->host_lock);
  1031. ici->ops->clock_stop(ici);
  1032. mutex_unlock(&ici->host_lock);
  1033. eadd:
  1034. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1035. return ret;
  1036. }
  1037. /*
  1038. * This is called on device_unregister, which only means we have to disconnect
  1039. * from the host, but not remove ourselves from the device list
  1040. */
  1041. static int soc_camera_remove(struct soc_camera_device *icd)
  1042. {
  1043. struct soc_camera_desc *sdesc = to_soc_camera_desc(icd);
  1044. struct video_device *vdev = icd->vdev;
  1045. BUG_ON(!icd->parent);
  1046. v4l2_ctrl_handler_free(&icd->ctrl_handler);
  1047. if (vdev) {
  1048. video_unregister_device(vdev);
  1049. icd->vdev = NULL;
  1050. }
  1051. if (sdesc->host_desc.board_info) {
  1052. soc_camera_free_i2c(icd);
  1053. } else {
  1054. struct device_driver *drv = to_soc_camera_control(icd)->driver;
  1055. if (drv) {
  1056. sdesc->host_desc.del_device(icd);
  1057. module_put(drv->owner);
  1058. }
  1059. }
  1060. soc_camera_free_user_formats(icd);
  1061. return 0;
  1062. }
  1063. static int default_cropcap(struct soc_camera_device *icd,
  1064. struct v4l2_cropcap *a)
  1065. {
  1066. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1067. return v4l2_subdev_call(sd, video, cropcap, a);
  1068. }
  1069. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1070. {
  1071. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1072. return v4l2_subdev_call(sd, video, g_crop, a);
  1073. }
  1074. static int default_s_crop(struct soc_camera_device *icd, const struct v4l2_crop *a)
  1075. {
  1076. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1077. return v4l2_subdev_call(sd, video, s_crop, a);
  1078. }
  1079. static int default_g_parm(struct soc_camera_device *icd,
  1080. struct v4l2_streamparm *parm)
  1081. {
  1082. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1083. return v4l2_subdev_call(sd, video, g_parm, parm);
  1084. }
  1085. static int default_s_parm(struct soc_camera_device *icd,
  1086. struct v4l2_streamparm *parm)
  1087. {
  1088. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1089. return v4l2_subdev_call(sd, video, s_parm, parm);
  1090. }
  1091. static int default_enum_framesizes(struct soc_camera_device *icd,
  1092. struct v4l2_frmsizeenum *fsize)
  1093. {
  1094. int ret;
  1095. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1096. const struct soc_camera_format_xlate *xlate;
  1097. __u32 pixfmt = fsize->pixel_format;
  1098. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1099. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1100. if (!xlate)
  1101. return -EINVAL;
  1102. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1103. fsize_mbus.pixel_format = xlate->code;
  1104. ret = v4l2_subdev_call(sd, video, enum_framesizes, &fsize_mbus);
  1105. if (ret < 0)
  1106. return ret;
  1107. *fsize = fsize_mbus;
  1108. fsize->pixel_format = pixfmt;
  1109. return 0;
  1110. }
  1111. int soc_camera_host_register(struct soc_camera_host *ici)
  1112. {
  1113. struct soc_camera_host *ix;
  1114. int ret;
  1115. if (!ici || !ici->ops ||
  1116. !ici->ops->try_fmt ||
  1117. !ici->ops->set_fmt ||
  1118. !ici->ops->set_bus_param ||
  1119. !ici->ops->querycap ||
  1120. ((!ici->ops->init_videobuf ||
  1121. !ici->ops->reqbufs) &&
  1122. !ici->ops->init_videobuf2) ||
  1123. !ici->ops->clock_start ||
  1124. !ici->ops->clock_stop ||
  1125. !ici->ops->poll ||
  1126. !ici->v4l2_dev.dev)
  1127. return -EINVAL;
  1128. if (!ici->ops->set_crop)
  1129. ici->ops->set_crop = default_s_crop;
  1130. if (!ici->ops->get_crop)
  1131. ici->ops->get_crop = default_g_crop;
  1132. if (!ici->ops->cropcap)
  1133. ici->ops->cropcap = default_cropcap;
  1134. if (!ici->ops->set_parm)
  1135. ici->ops->set_parm = default_s_parm;
  1136. if (!ici->ops->get_parm)
  1137. ici->ops->get_parm = default_g_parm;
  1138. if (!ici->ops->enum_framesizes)
  1139. ici->ops->enum_framesizes = default_enum_framesizes;
  1140. mutex_lock(&list_lock);
  1141. list_for_each_entry(ix, &hosts, list) {
  1142. if (ix->nr == ici->nr) {
  1143. ret = -EBUSY;
  1144. goto edevreg;
  1145. }
  1146. }
  1147. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1148. if (ret < 0)
  1149. goto edevreg;
  1150. list_add_tail(&ici->list, &hosts);
  1151. mutex_unlock(&list_lock);
  1152. mutex_init(&ici->host_lock);
  1153. scan_add_host(ici);
  1154. return 0;
  1155. edevreg:
  1156. mutex_unlock(&list_lock);
  1157. return ret;
  1158. }
  1159. EXPORT_SYMBOL(soc_camera_host_register);
  1160. /* Unregister all clients! */
  1161. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1162. {
  1163. struct soc_camera_device *icd;
  1164. mutex_lock(&list_lock);
  1165. list_del(&ici->list);
  1166. list_for_each_entry(icd, &devices, list)
  1167. if (icd->iface == ici->nr && to_soc_camera_control(icd))
  1168. soc_camera_remove(icd);
  1169. mutex_unlock(&list_lock);
  1170. v4l2_device_unregister(&ici->v4l2_dev);
  1171. }
  1172. EXPORT_SYMBOL(soc_camera_host_unregister);
  1173. /* Image capture device */
  1174. static int soc_camera_device_register(struct soc_camera_device *icd)
  1175. {
  1176. struct soc_camera_device *ix;
  1177. int num = -1, i;
  1178. for (i = 0; i < 256 && num < 0; i++) {
  1179. num = i;
  1180. /* Check if this index is available on this interface */
  1181. list_for_each_entry(ix, &devices, list) {
  1182. if (ix->iface == icd->iface && ix->devnum == i) {
  1183. num = -1;
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. if (num < 0)
  1189. /*
  1190. * ok, we have 256 cameras on this host...
  1191. * man, stay reasonable...
  1192. */
  1193. return -ENOMEM;
  1194. icd->devnum = num;
  1195. icd->use_count = 0;
  1196. icd->host_priv = NULL;
  1197. list_add_tail(&icd->list, &devices);
  1198. return 0;
  1199. }
  1200. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1201. .vidioc_querycap = soc_camera_querycap,
  1202. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1203. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1204. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1205. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1206. .vidioc_enum_input = soc_camera_enum_input,
  1207. .vidioc_g_input = soc_camera_g_input,
  1208. .vidioc_s_input = soc_camera_s_input,
  1209. .vidioc_s_std = soc_camera_s_std,
  1210. .vidioc_g_std = soc_camera_g_std,
  1211. .vidioc_enum_framesizes = soc_camera_enum_framesizes,
  1212. .vidioc_reqbufs = soc_camera_reqbufs,
  1213. .vidioc_querybuf = soc_camera_querybuf,
  1214. .vidioc_qbuf = soc_camera_qbuf,
  1215. .vidioc_dqbuf = soc_camera_dqbuf,
  1216. .vidioc_create_bufs = soc_camera_create_bufs,
  1217. .vidioc_prepare_buf = soc_camera_prepare_buf,
  1218. .vidioc_streamon = soc_camera_streamon,
  1219. .vidioc_streamoff = soc_camera_streamoff,
  1220. .vidioc_cropcap = soc_camera_cropcap,
  1221. .vidioc_g_crop = soc_camera_g_crop,
  1222. .vidioc_s_crop = soc_camera_s_crop,
  1223. .vidioc_g_selection = soc_camera_g_selection,
  1224. .vidioc_s_selection = soc_camera_s_selection,
  1225. .vidioc_g_parm = soc_camera_g_parm,
  1226. .vidioc_s_parm = soc_camera_s_parm,
  1227. };
  1228. static int video_dev_create(struct soc_camera_device *icd)
  1229. {
  1230. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1231. struct video_device *vdev = video_device_alloc();
  1232. if (!vdev)
  1233. return -ENOMEM;
  1234. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1235. vdev->v4l2_dev = &ici->v4l2_dev;
  1236. vdev->fops = &soc_camera_fops;
  1237. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1238. vdev->release = video_device_release;
  1239. vdev->ctrl_handler = &icd->ctrl_handler;
  1240. vdev->lock = &ici->host_lock;
  1241. icd->vdev = vdev;
  1242. return 0;
  1243. }
  1244. /*
  1245. * Called from soc_camera_probe() above with .host_lock held
  1246. */
  1247. static int soc_camera_video_start(struct soc_camera_device *icd)
  1248. {
  1249. const struct device_type *type = icd->vdev->dev.type;
  1250. int ret;
  1251. if (!icd->parent)
  1252. return -ENODEV;
  1253. video_set_drvdata(icd->vdev, icd);
  1254. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1255. if (ret < 0) {
  1256. dev_err(icd->pdev, "video_register_device failed: %d\n", ret);
  1257. return ret;
  1258. }
  1259. /* Restore device type, possibly set by the subdevice driver */
  1260. icd->vdev->dev.type = type;
  1261. return 0;
  1262. }
  1263. static int soc_camera_pdrv_probe(struct platform_device *pdev)
  1264. {
  1265. struct soc_camera_desc *sdesc = pdev->dev.platform_data;
  1266. struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc;
  1267. struct soc_camera_device *icd;
  1268. int ret;
  1269. if (!sdesc)
  1270. return -EINVAL;
  1271. icd = devm_kzalloc(&pdev->dev, sizeof(*icd), GFP_KERNEL);
  1272. if (!icd)
  1273. return -ENOMEM;
  1274. ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators,
  1275. ssdd->regulators);
  1276. if (ret < 0)
  1277. return ret;
  1278. icd->iface = sdesc->host_desc.bus_id;
  1279. icd->sdesc = sdesc;
  1280. icd->pdev = &pdev->dev;
  1281. platform_set_drvdata(pdev, icd);
  1282. icd->user_width = DEFAULT_WIDTH;
  1283. icd->user_height = DEFAULT_HEIGHT;
  1284. return soc_camera_device_register(icd);
  1285. }
  1286. /*
  1287. * Only called on rmmod for each platform device, since they are not
  1288. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1289. * been unloaded already
  1290. */
  1291. static int soc_camera_pdrv_remove(struct platform_device *pdev)
  1292. {
  1293. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1294. if (!icd)
  1295. return -EINVAL;
  1296. list_del(&icd->list);
  1297. return 0;
  1298. }
  1299. static struct platform_driver __refdata soc_camera_pdrv = {
  1300. .probe = soc_camera_pdrv_probe,
  1301. .remove = soc_camera_pdrv_remove,
  1302. .driver = {
  1303. .name = "soc-camera-pdrv",
  1304. .owner = THIS_MODULE,
  1305. },
  1306. };
  1307. module_platform_driver(soc_camera_pdrv);
  1308. MODULE_DESCRIPTION("Image capture bus driver");
  1309. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1310. MODULE_LICENSE("GPL");
  1311. MODULE_ALIAS("platform:soc-camera-pdrv");