soc_camera.c 40 KB

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