soc_camera.c 38 KB

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