soc_camera.c 39 KB

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