soc_camera.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * camera image capture (abstract) bus driver
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  5. *
  6. * This driver provides an interface between platform-specific camera
  7. * busses and camera devices. It should be used if the camera is
  8. * connected not over a "proper" bus like PCI or USB, but over a
  9. * special bus, like, for example, the Quick Capture interface on PXA270
  10. * SoCs. Later it should also be used for i.MX31 SoCs from Freescale.
  11. * It can handle multiple cameras and / or multiple busses, which can
  12. * be used, e.g., in stereo-vision applications.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/vmalloc.h>
  27. #include <media/soc_camera.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-ioctl.h>
  31. #include <media/videobuf-core.h>
  32. /* Default to VGA resolution */
  33. #define DEFAULT_WIDTH 640
  34. #define DEFAULT_HEIGHT 480
  35. static LIST_HEAD(hosts);
  36. static LIST_HEAD(devices);
  37. static DEFINE_MUTEX(list_lock);
  38. const struct soc_camera_data_format *soc_camera_format_by_fourcc(
  39. struct soc_camera_device *icd, unsigned int fourcc)
  40. {
  41. unsigned int i;
  42. for (i = 0; i < icd->num_formats; i++)
  43. if (icd->formats[i].fourcc == fourcc)
  44. return icd->formats + i;
  45. return NULL;
  46. }
  47. EXPORT_SYMBOL(soc_camera_format_by_fourcc);
  48. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  49. struct soc_camera_device *icd, unsigned int fourcc)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < icd->num_user_formats; i++)
  53. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  54. return icd->user_formats + i;
  55. return NULL;
  56. }
  57. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  58. /**
  59. * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  60. * @icl: camera platform parameters
  61. * @flags: flags to be inverted according to platform configuration
  62. * @return: resulting flags
  63. */
  64. unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  65. unsigned long flags)
  66. {
  67. unsigned long f;
  68. /* If only one of the two polarities is supported, switch to the opposite */
  69. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  70. f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  71. if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
  72. flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
  73. }
  74. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  75. f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  76. if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
  77. flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
  78. }
  79. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  80. f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  81. if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
  82. flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
  83. }
  84. return flags;
  85. }
  86. EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
  87. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  88. struct v4l2_format *f)
  89. {
  90. struct soc_camera_file *icf = file->private_data;
  91. struct soc_camera_device *icd = icf->icd;
  92. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  93. WARN_ON(priv != file->private_data);
  94. /* limit format to hardware capabilities */
  95. return ici->ops->try_fmt(icd, f);
  96. }
  97. static int soc_camera_enum_input(struct file *file, void *priv,
  98. struct v4l2_input *inp)
  99. {
  100. struct soc_camera_file *icf = file->private_data;
  101. struct soc_camera_device *icd = icf->icd;
  102. int ret = 0;
  103. if (inp->index != 0)
  104. return -EINVAL;
  105. if (icd->ops->enum_input)
  106. ret = icd->ops->enum_input(icd, inp);
  107. else {
  108. /* default is camera */
  109. inp->type = V4L2_INPUT_TYPE_CAMERA;
  110. inp->std = V4L2_STD_UNKNOWN;
  111. strcpy(inp->name, "Camera");
  112. }
  113. return ret;
  114. }
  115. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  116. {
  117. *i = 0;
  118. return 0;
  119. }
  120. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  121. {
  122. if (i > 0)
  123. return -EINVAL;
  124. return 0;
  125. }
  126. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  127. {
  128. struct soc_camera_file *icf = file->private_data;
  129. struct soc_camera_device *icd = icf->icd;
  130. int ret = 0;
  131. if (icd->ops->set_std)
  132. ret = icd->ops->set_std(icd, a);
  133. return ret;
  134. }
  135. static int soc_camera_reqbufs(struct file *file, void *priv,
  136. struct v4l2_requestbuffers *p)
  137. {
  138. int ret;
  139. struct soc_camera_file *icf = file->private_data;
  140. struct soc_camera_device *icd = icf->icd;
  141. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  142. WARN_ON(priv != file->private_data);
  143. dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
  144. ret = videobuf_reqbufs(&icf->vb_vidq, p);
  145. if (ret < 0)
  146. return ret;
  147. return ici->ops->reqbufs(icf, p);
  148. }
  149. static int soc_camera_querybuf(struct file *file, void *priv,
  150. struct v4l2_buffer *p)
  151. {
  152. struct soc_camera_file *icf = file->private_data;
  153. WARN_ON(priv != file->private_data);
  154. return videobuf_querybuf(&icf->vb_vidq, p);
  155. }
  156. static int soc_camera_qbuf(struct file *file, void *priv,
  157. struct v4l2_buffer *p)
  158. {
  159. struct soc_camera_file *icf = file->private_data;
  160. WARN_ON(priv != file->private_data);
  161. return videobuf_qbuf(&icf->vb_vidq, p);
  162. }
  163. static int soc_camera_dqbuf(struct file *file, void *priv,
  164. struct v4l2_buffer *p)
  165. {
  166. struct soc_camera_file *icf = file->private_data;
  167. WARN_ON(priv != file->private_data);
  168. return videobuf_dqbuf(&icf->vb_vidq, p, file->f_flags & O_NONBLOCK);
  169. }
  170. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  171. {
  172. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  173. int i, fmts = 0;
  174. if (!ici->ops->get_formats)
  175. /*
  176. * Fallback mode - the host will have to serve all
  177. * sensor-provided formats one-to-one to the user
  178. */
  179. fmts = icd->num_formats;
  180. else
  181. /*
  182. * First pass - only count formats this host-sensor
  183. * configuration can provide
  184. */
  185. for (i = 0; i < icd->num_formats; i++)
  186. fmts += ici->ops->get_formats(icd, i, NULL);
  187. if (!fmts)
  188. return -ENXIO;
  189. icd->user_formats =
  190. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  191. if (!icd->user_formats)
  192. return -ENOMEM;
  193. icd->num_user_formats = fmts;
  194. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  195. /* Second pass - actually fill data formats */
  196. fmts = 0;
  197. for (i = 0; i < icd->num_formats; i++)
  198. if (!ici->ops->get_formats) {
  199. icd->user_formats[i].host_fmt = icd->formats + i;
  200. icd->user_formats[i].cam_fmt = icd->formats + i;
  201. icd->user_formats[i].buswidth = icd->formats[i].depth;
  202. } else {
  203. fmts += ici->ops->get_formats(icd, i,
  204. &icd->user_formats[fmts]);
  205. }
  206. icd->current_fmt = icd->user_formats[0].host_fmt;
  207. return 0;
  208. }
  209. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  210. {
  211. vfree(icd->user_formats);
  212. }
  213. /* Called with .vb_lock held */
  214. static int soc_camera_set_fmt(struct soc_camera_file *icf,
  215. struct v4l2_format *f)
  216. {
  217. struct soc_camera_device *icd = icf->icd;
  218. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  219. struct v4l2_pix_format *pix = &f->fmt.pix;
  220. int ret;
  221. /* We always call try_fmt() before set_fmt() or set_crop() */
  222. ret = ici->ops->try_fmt(icd, f);
  223. if (ret < 0)
  224. return ret;
  225. ret = ici->ops->set_fmt(icd, f);
  226. if (ret < 0) {
  227. return ret;
  228. } else if (!icd->current_fmt ||
  229. icd->current_fmt->fourcc != pix->pixelformat) {
  230. dev_err(ici->dev,
  231. "Host driver hasn't set up current format correctly!\n");
  232. return -EINVAL;
  233. }
  234. icd->width = pix->width;
  235. icd->height = pix->height;
  236. icf->vb_vidq.field =
  237. icd->field = pix->field;
  238. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  239. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  240. f->type);
  241. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  242. icd->width, icd->height);
  243. /* set physical bus parameters */
  244. return ici->ops->set_bus_param(icd, pix->pixelformat);
  245. }
  246. static int soc_camera_open(struct file *file)
  247. {
  248. struct video_device *vdev;
  249. struct soc_camera_device *icd;
  250. struct soc_camera_host *ici;
  251. struct soc_camera_file *icf;
  252. int ret;
  253. icf = vmalloc(sizeof(*icf));
  254. if (!icf)
  255. return -ENOMEM;
  256. /*
  257. * It is safe to dereference these pointers now as long as a user has
  258. * the video device open - we are protected by the held cdev reference.
  259. */
  260. vdev = video_devdata(file);
  261. icd = container_of(vdev->parent, struct soc_camera_device, dev);
  262. ici = to_soc_camera_host(icd->dev.parent);
  263. if (!try_module_get(icd->ops->owner)) {
  264. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  265. ret = -EINVAL;
  266. goto emgd;
  267. }
  268. if (!try_module_get(ici->ops->owner)) {
  269. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  270. ret = -EINVAL;
  271. goto emgi;
  272. }
  273. /* Protect against icd->remove() until we module_get() both drivers. */
  274. mutex_lock(&icd->video_lock);
  275. icf->icd = icd;
  276. icd->use_count++;
  277. /* Now we really have to activate the camera */
  278. if (icd->use_count == 1) {
  279. /* Restore parameters before the last close() per V4L2 API */
  280. struct v4l2_format f = {
  281. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  282. .fmt.pix = {
  283. .width = icd->width,
  284. .height = icd->height,
  285. .field = icd->field,
  286. .pixelformat = icd->current_fmt->fourcc,
  287. .colorspace = icd->current_fmt->colorspace,
  288. },
  289. };
  290. ret = ici->ops->add(icd);
  291. if (ret < 0) {
  292. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  293. goto eiciadd;
  294. }
  295. /* Try to configure with default parameters */
  296. ret = soc_camera_set_fmt(icf, &f);
  297. if (ret < 0)
  298. goto esfmt;
  299. }
  300. mutex_unlock(&icd->video_lock);
  301. file->private_data = icf;
  302. dev_dbg(&icd->dev, "camera device open\n");
  303. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  304. return 0;
  305. /*
  306. * First three errors are entered with the .video_lock held
  307. * and use_count == 1
  308. */
  309. esfmt:
  310. ici->ops->remove(icd);
  311. eiciadd:
  312. icd->use_count--;
  313. mutex_unlock(&icd->video_lock);
  314. module_put(ici->ops->owner);
  315. emgi:
  316. module_put(icd->ops->owner);
  317. emgd:
  318. vfree(icf);
  319. return ret;
  320. }
  321. static int soc_camera_close(struct file *file)
  322. {
  323. struct soc_camera_file *icf = file->private_data;
  324. struct soc_camera_device *icd = icf->icd;
  325. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  326. struct video_device *vdev = icd->vdev;
  327. mutex_lock(&icd->video_lock);
  328. icd->use_count--;
  329. if (!icd->use_count)
  330. ici->ops->remove(icd);
  331. mutex_unlock(&icd->video_lock);
  332. module_put(icd->ops->owner);
  333. module_put(ici->ops->owner);
  334. vfree(icf);
  335. dev_dbg(vdev->parent, "camera device close\n");
  336. return 0;
  337. }
  338. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  339. size_t count, loff_t *ppos)
  340. {
  341. struct soc_camera_file *icf = file->private_data;
  342. struct soc_camera_device *icd = icf->icd;
  343. struct video_device *vdev = icd->vdev;
  344. int err = -EINVAL;
  345. dev_err(vdev->parent, "camera device read not implemented\n");
  346. return err;
  347. }
  348. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  349. {
  350. struct soc_camera_file *icf = file->private_data;
  351. struct soc_camera_device *icd = icf->icd;
  352. int err;
  353. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  354. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  355. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  356. (unsigned long)vma->vm_start,
  357. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  358. err);
  359. return err;
  360. }
  361. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  362. {
  363. struct soc_camera_file *icf = file->private_data;
  364. struct soc_camera_device *icd = icf->icd;
  365. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  366. if (list_empty(&icf->vb_vidq.stream)) {
  367. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  368. return POLLERR;
  369. }
  370. return ici->ops->poll(file, pt);
  371. }
  372. static struct v4l2_file_operations soc_camera_fops = {
  373. .owner = THIS_MODULE,
  374. .open = soc_camera_open,
  375. .release = soc_camera_close,
  376. .ioctl = video_ioctl2,
  377. .read = soc_camera_read,
  378. .mmap = soc_camera_mmap,
  379. .poll = soc_camera_poll,
  380. };
  381. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  382. struct v4l2_format *f)
  383. {
  384. struct soc_camera_file *icf = file->private_data;
  385. struct soc_camera_device *icd = icf->icd;
  386. int ret;
  387. WARN_ON(priv != file->private_data);
  388. mutex_lock(&icf->vb_vidq.vb_lock);
  389. if (videobuf_queue_is_busy(&icf->vb_vidq)) {
  390. dev_err(&icd->dev, "S_FMT denied: queue busy\n");
  391. ret = -EBUSY;
  392. goto unlock;
  393. }
  394. ret = soc_camera_set_fmt(icf, f);
  395. unlock:
  396. mutex_unlock(&icf->vb_vidq.vb_lock);
  397. return ret;
  398. }
  399. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  400. struct v4l2_fmtdesc *f)
  401. {
  402. struct soc_camera_file *icf = file->private_data;
  403. struct soc_camera_device *icd = icf->icd;
  404. const struct soc_camera_data_format *format;
  405. WARN_ON(priv != file->private_data);
  406. if (f->index >= icd->num_user_formats)
  407. return -EINVAL;
  408. format = icd->user_formats[f->index].host_fmt;
  409. strlcpy(f->description, format->name, sizeof(f->description));
  410. f->pixelformat = format->fourcc;
  411. return 0;
  412. }
  413. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  414. struct v4l2_format *f)
  415. {
  416. struct soc_camera_file *icf = file->private_data;
  417. struct soc_camera_device *icd = icf->icd;
  418. struct v4l2_pix_format *pix = &f->fmt.pix;
  419. WARN_ON(priv != file->private_data);
  420. pix->width = icd->width;
  421. pix->height = icd->height;
  422. pix->field = icf->vb_vidq.field;
  423. pix->pixelformat = icd->current_fmt->fourcc;
  424. pix->bytesperline = pix->width *
  425. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  426. pix->sizeimage = pix->height * pix->bytesperline;
  427. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  428. icd->current_fmt->fourcc);
  429. return 0;
  430. }
  431. static int soc_camera_querycap(struct file *file, void *priv,
  432. struct v4l2_capability *cap)
  433. {
  434. struct soc_camera_file *icf = file->private_data;
  435. struct soc_camera_device *icd = icf->icd;
  436. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  437. WARN_ON(priv != file->private_data);
  438. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  439. return ici->ops->querycap(ici, cap);
  440. }
  441. static int soc_camera_streamon(struct file *file, void *priv,
  442. enum v4l2_buf_type i)
  443. {
  444. struct soc_camera_file *icf = file->private_data;
  445. struct soc_camera_device *icd = icf->icd;
  446. int ret;
  447. WARN_ON(priv != file->private_data);
  448. dev_dbg(&icd->dev, "%s\n", __func__);
  449. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  450. return -EINVAL;
  451. mutex_lock(&icd->video_lock);
  452. icd->ops->start_capture(icd);
  453. /* This calls buf_queue from host driver's videobuf_queue_ops */
  454. ret = videobuf_streamon(&icf->vb_vidq);
  455. mutex_unlock(&icd->video_lock);
  456. return ret;
  457. }
  458. static int soc_camera_streamoff(struct file *file, void *priv,
  459. enum v4l2_buf_type i)
  460. {
  461. struct soc_camera_file *icf = file->private_data;
  462. struct soc_camera_device *icd = icf->icd;
  463. WARN_ON(priv != file->private_data);
  464. dev_dbg(&icd->dev, "%s\n", __func__);
  465. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  466. return -EINVAL;
  467. mutex_lock(&icd->video_lock);
  468. /* This calls buf_release from host driver's videobuf_queue_ops for all
  469. * remaining buffers. When the last buffer is freed, stop capture */
  470. videobuf_streamoff(&icf->vb_vidq);
  471. icd->ops->stop_capture(icd);
  472. mutex_unlock(&icd->video_lock);
  473. return 0;
  474. }
  475. static int soc_camera_queryctrl(struct file *file, void *priv,
  476. struct v4l2_queryctrl *qc)
  477. {
  478. struct soc_camera_file *icf = file->private_data;
  479. struct soc_camera_device *icd = icf->icd;
  480. int i;
  481. WARN_ON(priv != file->private_data);
  482. if (!qc->id)
  483. return -EINVAL;
  484. for (i = 0; i < icd->ops->num_controls; i++)
  485. if (qc->id == icd->ops->controls[i].id) {
  486. memcpy(qc, &(icd->ops->controls[i]),
  487. sizeof(*qc));
  488. return 0;
  489. }
  490. return -EINVAL;
  491. }
  492. static int soc_camera_g_ctrl(struct file *file, void *priv,
  493. struct v4l2_control *ctrl)
  494. {
  495. struct soc_camera_file *icf = file->private_data;
  496. struct soc_camera_device *icd = icf->icd;
  497. WARN_ON(priv != file->private_data);
  498. switch (ctrl->id) {
  499. case V4L2_CID_GAIN:
  500. if (icd->gain == (unsigned short)~0)
  501. return -EINVAL;
  502. ctrl->value = icd->gain;
  503. return 0;
  504. case V4L2_CID_EXPOSURE:
  505. if (icd->exposure == (unsigned short)~0)
  506. return -EINVAL;
  507. ctrl->value = icd->exposure;
  508. return 0;
  509. }
  510. if (icd->ops->get_control)
  511. return icd->ops->get_control(icd, ctrl);
  512. return -EINVAL;
  513. }
  514. static int soc_camera_s_ctrl(struct file *file, void *priv,
  515. struct v4l2_control *ctrl)
  516. {
  517. struct soc_camera_file *icf = file->private_data;
  518. struct soc_camera_device *icd = icf->icd;
  519. WARN_ON(priv != file->private_data);
  520. if (icd->ops->set_control)
  521. return icd->ops->set_control(icd, ctrl);
  522. return -EINVAL;
  523. }
  524. static int soc_camera_cropcap(struct file *file, void *fh,
  525. struct v4l2_cropcap *a)
  526. {
  527. struct soc_camera_file *icf = file->private_data;
  528. struct soc_camera_device *icd = icf->icd;
  529. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  530. a->bounds.left = icd->x_min;
  531. a->bounds.top = icd->y_min;
  532. a->bounds.width = icd->width_max;
  533. a->bounds.height = icd->height_max;
  534. a->defrect.left = icd->x_min;
  535. a->defrect.top = icd->y_min;
  536. a->defrect.width = DEFAULT_WIDTH;
  537. a->defrect.height = DEFAULT_HEIGHT;
  538. a->pixelaspect.numerator = 1;
  539. a->pixelaspect.denominator = 1;
  540. return 0;
  541. }
  542. static int soc_camera_g_crop(struct file *file, void *fh,
  543. struct v4l2_crop *a)
  544. {
  545. struct soc_camera_file *icf = file->private_data;
  546. struct soc_camera_device *icd = icf->icd;
  547. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  548. a->c.left = icd->x_current;
  549. a->c.top = icd->y_current;
  550. a->c.width = icd->width;
  551. a->c.height = icd->height;
  552. return 0;
  553. }
  554. static int soc_camera_s_crop(struct file *file, void *fh,
  555. struct v4l2_crop *a)
  556. {
  557. struct soc_camera_file *icf = file->private_data;
  558. struct soc_camera_device *icd = icf->icd;
  559. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  560. int ret;
  561. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  562. return -EINVAL;
  563. /* Cropping is allowed during a running capture, guard consistency */
  564. mutex_lock(&icf->vb_vidq.vb_lock);
  565. ret = ici->ops->set_crop(icd, &a->c);
  566. if (!ret) {
  567. icd->width = a->c.width;
  568. icd->height = a->c.height;
  569. icd->x_current = a->c.left;
  570. icd->y_current = a->c.top;
  571. }
  572. mutex_unlock(&icf->vb_vidq.vb_lock);
  573. return ret;
  574. }
  575. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  576. struct v4l2_dbg_chip_ident *id)
  577. {
  578. struct soc_camera_file *icf = file->private_data;
  579. struct soc_camera_device *icd = icf->icd;
  580. if (!icd->ops->get_chip_id)
  581. return -EINVAL;
  582. return icd->ops->get_chip_id(icd, id);
  583. }
  584. #ifdef CONFIG_VIDEO_ADV_DEBUG
  585. static int soc_camera_g_register(struct file *file, void *fh,
  586. struct v4l2_dbg_register *reg)
  587. {
  588. struct soc_camera_file *icf = file->private_data;
  589. struct soc_camera_device *icd = icf->icd;
  590. if (!icd->ops->get_register)
  591. return -EINVAL;
  592. return icd->ops->get_register(icd, reg);
  593. }
  594. static int soc_camera_s_register(struct file *file, void *fh,
  595. struct v4l2_dbg_register *reg)
  596. {
  597. struct soc_camera_file *icf = file->private_data;
  598. struct soc_camera_device *icd = icf->icd;
  599. if (!icd->ops->set_register)
  600. return -EINVAL;
  601. return icd->ops->set_register(icd, reg);
  602. }
  603. #endif
  604. static int device_register_link(struct soc_camera_device *icd)
  605. {
  606. int ret = dev_set_name(&icd->dev, "%u-%u", icd->iface, icd->devnum);
  607. if (!ret)
  608. ret = device_register(&icd->dev);
  609. if (ret < 0) {
  610. /* Prevent calling device_unregister() */
  611. icd->dev.parent = NULL;
  612. dev_err(&icd->dev, "Cannot register device: %d\n", ret);
  613. /* Even if probe() was unsuccessful for all registered drivers,
  614. * device_register() returns 0, and we add the link, just to
  615. * document this camera's control device */
  616. } else if (icd->control)
  617. /* Have to sysfs_remove_link() before device_unregister()? */
  618. if (sysfs_create_link(&icd->dev.kobj, &icd->control->kobj,
  619. "control"))
  620. dev_warn(&icd->dev,
  621. "Failed creating the control symlink\n");
  622. return ret;
  623. }
  624. /* So far this function cannot fail */
  625. static void scan_add_host(struct soc_camera_host *ici)
  626. {
  627. struct soc_camera_device *icd;
  628. mutex_lock(&list_lock);
  629. list_for_each_entry(icd, &devices, list) {
  630. if (icd->iface == ici->nr) {
  631. icd->dev.parent = ici->dev;
  632. device_register_link(icd);
  633. }
  634. }
  635. mutex_unlock(&list_lock);
  636. }
  637. /* return: 0 if no match found or a match found and
  638. * device_register() successful, error code otherwise */
  639. static int scan_add_device(struct soc_camera_device *icd)
  640. {
  641. struct soc_camera_host *ici;
  642. int ret = 0;
  643. mutex_lock(&list_lock);
  644. list_add_tail(&icd->list, &devices);
  645. /* Watch out for class_for_each_device / class_find_device API by
  646. * Dave Young <hidave.darkstar@gmail.com> */
  647. list_for_each_entry(ici, &hosts, list) {
  648. if (icd->iface == ici->nr) {
  649. ret = 1;
  650. icd->dev.parent = ici->dev;
  651. break;
  652. }
  653. }
  654. mutex_unlock(&list_lock);
  655. if (ret)
  656. ret = device_register_link(icd);
  657. return ret;
  658. }
  659. static int soc_camera_probe(struct device *dev)
  660. {
  661. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  662. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  663. int ret;
  664. /*
  665. * Possible race scenario:
  666. * modprobe <camera-host-driver> triggers __func__
  667. * at this moment respective <camera-sensor-driver> gets rmmod'ed
  668. * to protect take module references.
  669. */
  670. if (!try_module_get(icd->ops->owner)) {
  671. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  672. ret = -EINVAL;
  673. goto emgd;
  674. }
  675. if (!try_module_get(ici->ops->owner)) {
  676. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  677. ret = -EINVAL;
  678. goto emgi;
  679. }
  680. mutex_lock(&icd->video_lock);
  681. /* We only call ->add() here to activate and probe the camera.
  682. * We shall ->remove() and deactivate it immediately afterwards. */
  683. ret = ici->ops->add(icd);
  684. if (ret < 0)
  685. goto eiadd;
  686. ret = icd->ops->probe(icd);
  687. if (ret >= 0) {
  688. const struct v4l2_queryctrl *qctrl;
  689. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  690. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  691. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  692. icd->exposure = qctrl ? qctrl->default_value :
  693. (unsigned short)~0;
  694. ret = soc_camera_init_user_formats(icd);
  695. if (ret < 0) {
  696. if (icd->ops->remove)
  697. icd->ops->remove(icd);
  698. goto eiufmt;
  699. }
  700. icd->height = DEFAULT_HEIGHT;
  701. icd->width = DEFAULT_WIDTH;
  702. icd->field = V4L2_FIELD_ANY;
  703. }
  704. eiufmt:
  705. ici->ops->remove(icd);
  706. eiadd:
  707. mutex_unlock(&icd->video_lock);
  708. module_put(ici->ops->owner);
  709. emgi:
  710. module_put(icd->ops->owner);
  711. emgd:
  712. return ret;
  713. }
  714. /* This is called on device_unregister, which only means we have to disconnect
  715. * from the host, but not remove ourselves from the device list */
  716. static int soc_camera_remove(struct device *dev)
  717. {
  718. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  719. mutex_lock(&icd->video_lock);
  720. if (icd->ops->remove)
  721. icd->ops->remove(icd);
  722. mutex_unlock(&icd->video_lock);
  723. soc_camera_free_user_formats(icd);
  724. return 0;
  725. }
  726. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  727. {
  728. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  729. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  730. int ret = 0;
  731. if (ici->ops->suspend)
  732. ret = ici->ops->suspend(icd, state);
  733. return ret;
  734. }
  735. static int soc_camera_resume(struct device *dev)
  736. {
  737. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  738. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  739. int ret = 0;
  740. if (ici->ops->resume)
  741. ret = ici->ops->resume(icd);
  742. return ret;
  743. }
  744. static struct bus_type soc_camera_bus_type = {
  745. .name = "soc-camera",
  746. .probe = soc_camera_probe,
  747. .remove = soc_camera_remove,
  748. .suspend = soc_camera_suspend,
  749. .resume = soc_camera_resume,
  750. };
  751. static struct device_driver ic_drv = {
  752. .name = "camera",
  753. .bus = &soc_camera_bus_type,
  754. .owner = THIS_MODULE,
  755. };
  756. static void dummy_release(struct device *dev)
  757. {
  758. }
  759. int soc_camera_host_register(struct soc_camera_host *ici)
  760. {
  761. struct soc_camera_host *ix;
  762. if (!ici || !ici->ops ||
  763. !ici->ops->try_fmt ||
  764. !ici->ops->set_fmt ||
  765. !ici->ops->set_crop ||
  766. !ici->ops->set_bus_param ||
  767. !ici->ops->querycap ||
  768. !ici->ops->init_videobuf ||
  769. !ici->ops->reqbufs ||
  770. !ici->ops->add ||
  771. !ici->ops->remove ||
  772. !ici->ops->poll ||
  773. !ici->dev)
  774. return -EINVAL;
  775. mutex_lock(&list_lock);
  776. list_for_each_entry(ix, &hosts, list) {
  777. if (ix->nr == ici->nr) {
  778. mutex_unlock(&list_lock);
  779. return -EBUSY;
  780. }
  781. }
  782. dev_set_drvdata(ici->dev, ici);
  783. list_add_tail(&ici->list, &hosts);
  784. mutex_unlock(&list_lock);
  785. scan_add_host(ici);
  786. return 0;
  787. }
  788. EXPORT_SYMBOL(soc_camera_host_register);
  789. /* Unregister all clients! */
  790. void soc_camera_host_unregister(struct soc_camera_host *ici)
  791. {
  792. struct soc_camera_device *icd;
  793. mutex_lock(&list_lock);
  794. list_del(&ici->list);
  795. list_for_each_entry(icd, &devices, list) {
  796. if (icd->dev.parent == ici->dev) {
  797. device_unregister(&icd->dev);
  798. /* Not before device_unregister(), .remove
  799. * needs parent to call ici->ops->remove() */
  800. icd->dev.parent = NULL;
  801. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  802. }
  803. }
  804. mutex_unlock(&list_lock);
  805. dev_set_drvdata(ici->dev, NULL);
  806. }
  807. EXPORT_SYMBOL(soc_camera_host_unregister);
  808. /* Image capture device */
  809. int soc_camera_device_register(struct soc_camera_device *icd)
  810. {
  811. struct soc_camera_device *ix;
  812. int num = -1, i;
  813. if (!icd || !icd->ops ||
  814. !icd->ops->probe ||
  815. !icd->ops->init ||
  816. !icd->ops->release ||
  817. !icd->ops->start_capture ||
  818. !icd->ops->stop_capture ||
  819. !icd->ops->set_crop ||
  820. !icd->ops->set_fmt ||
  821. !icd->ops->try_fmt ||
  822. !icd->ops->query_bus_param ||
  823. !icd->ops->set_bus_param)
  824. return -EINVAL;
  825. for (i = 0; i < 256 && num < 0; i++) {
  826. num = i;
  827. list_for_each_entry(ix, &devices, list) {
  828. if (ix->iface == icd->iface && ix->devnum == i) {
  829. num = -1;
  830. break;
  831. }
  832. }
  833. }
  834. if (num < 0)
  835. /* ok, we have 256 cameras on this host...
  836. * man, stay reasonable... */
  837. return -ENOMEM;
  838. icd->devnum = num;
  839. icd->dev.bus = &soc_camera_bus_type;
  840. icd->dev.release = dummy_release;
  841. icd->use_count = 0;
  842. icd->host_priv = NULL;
  843. mutex_init(&icd->video_lock);
  844. return scan_add_device(icd);
  845. }
  846. EXPORT_SYMBOL(soc_camera_device_register);
  847. void soc_camera_device_unregister(struct soc_camera_device *icd)
  848. {
  849. mutex_lock(&list_lock);
  850. list_del(&icd->list);
  851. /* The bus->remove will be eventually called */
  852. if (icd->dev.parent)
  853. device_unregister(&icd->dev);
  854. mutex_unlock(&list_lock);
  855. }
  856. EXPORT_SYMBOL(soc_camera_device_unregister);
  857. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  858. .vidioc_querycap = soc_camera_querycap,
  859. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  860. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  861. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  862. .vidioc_enum_input = soc_camera_enum_input,
  863. .vidioc_g_input = soc_camera_g_input,
  864. .vidioc_s_input = soc_camera_s_input,
  865. .vidioc_s_std = soc_camera_s_std,
  866. .vidioc_reqbufs = soc_camera_reqbufs,
  867. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  868. .vidioc_querybuf = soc_camera_querybuf,
  869. .vidioc_qbuf = soc_camera_qbuf,
  870. .vidioc_dqbuf = soc_camera_dqbuf,
  871. .vidioc_streamon = soc_camera_streamon,
  872. .vidioc_streamoff = soc_camera_streamoff,
  873. .vidioc_queryctrl = soc_camera_queryctrl,
  874. .vidioc_g_ctrl = soc_camera_g_ctrl,
  875. .vidioc_s_ctrl = soc_camera_s_ctrl,
  876. .vidioc_cropcap = soc_camera_cropcap,
  877. .vidioc_g_crop = soc_camera_g_crop,
  878. .vidioc_s_crop = soc_camera_s_crop,
  879. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  880. #ifdef CONFIG_VIDEO_ADV_DEBUG
  881. .vidioc_g_register = soc_camera_g_register,
  882. .vidioc_s_register = soc_camera_s_register,
  883. #endif
  884. };
  885. /*
  886. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  887. * soc_camera_probe() above with .video_lock held
  888. */
  889. int soc_camera_video_start(struct soc_camera_device *icd)
  890. {
  891. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  892. int err = -ENOMEM;
  893. struct video_device *vdev;
  894. if (!icd->dev.parent)
  895. return -ENODEV;
  896. vdev = video_device_alloc();
  897. if (!vdev)
  898. goto evidallocd;
  899. dev_dbg(ici->dev, "Allocated video_device %p\n", vdev);
  900. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  901. vdev->parent = &icd->dev;
  902. vdev->current_norm = V4L2_STD_UNKNOWN;
  903. vdev->fops = &soc_camera_fops;
  904. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  905. vdev->release = video_device_release;
  906. vdev->minor = -1;
  907. vdev->tvnorms = V4L2_STD_UNKNOWN,
  908. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  909. if (err < 0) {
  910. dev_err(vdev->parent, "video_register_device failed\n");
  911. goto evidregd;
  912. }
  913. icd->vdev = vdev;
  914. return 0;
  915. evidregd:
  916. video_device_release(vdev);
  917. evidallocd:
  918. return err;
  919. }
  920. EXPORT_SYMBOL(soc_camera_video_start);
  921. /* Called from client .remove() methods with .video_lock held */
  922. void soc_camera_video_stop(struct soc_camera_device *icd)
  923. {
  924. struct video_device *vdev = icd->vdev;
  925. dev_dbg(&icd->dev, "%s\n", __func__);
  926. if (!icd->dev.parent || !vdev)
  927. return;
  928. video_unregister_device(vdev);
  929. icd->vdev = NULL;
  930. }
  931. EXPORT_SYMBOL(soc_camera_video_stop);
  932. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  933. {
  934. struct soc_camera_link *icl = pdev->dev.platform_data;
  935. struct i2c_adapter *adap;
  936. struct i2c_client *client;
  937. if (!icl)
  938. return -EINVAL;
  939. adap = i2c_get_adapter(icl->i2c_adapter_id);
  940. if (!adap) {
  941. dev_warn(&pdev->dev, "Cannot get adapter #%d. No driver?\n",
  942. icl->i2c_adapter_id);
  943. /* -ENODEV and -ENXIO do not produce an error on probe()... */
  944. return -ENOENT;
  945. }
  946. icl->board_info->platform_data = icl;
  947. client = i2c_new_device(adap, icl->board_info);
  948. if (!client) {
  949. i2c_put_adapter(adap);
  950. return -ENOMEM;
  951. }
  952. platform_set_drvdata(pdev, client);
  953. return 0;
  954. }
  955. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  956. {
  957. struct i2c_client *client = platform_get_drvdata(pdev);
  958. if (!client)
  959. return -ENODEV;
  960. i2c_unregister_device(client);
  961. i2c_put_adapter(client->adapter);
  962. return 0;
  963. }
  964. static struct platform_driver __refdata soc_camera_pdrv = {
  965. .probe = soc_camera_pdrv_probe,
  966. .remove = __devexit_p(soc_camera_pdrv_remove),
  967. .driver = {
  968. .name = "soc-camera-pdrv",
  969. .owner = THIS_MODULE,
  970. },
  971. };
  972. static int __init soc_camera_init(void)
  973. {
  974. int ret = bus_register(&soc_camera_bus_type);
  975. if (ret)
  976. return ret;
  977. ret = driver_register(&ic_drv);
  978. if (ret)
  979. goto edrvr;
  980. ret = platform_driver_register(&soc_camera_pdrv);
  981. if (ret)
  982. goto epdr;
  983. return 0;
  984. epdr:
  985. driver_unregister(&ic_drv);
  986. edrvr:
  987. bus_unregister(&soc_camera_bus_type);
  988. return ret;
  989. }
  990. static void __exit soc_camera_exit(void)
  991. {
  992. platform_driver_unregister(&soc_camera_pdrv);
  993. driver_unregister(&ic_drv);
  994. bus_unregister(&soc_camera_bus_type);
  995. }
  996. module_init(soc_camera_init);
  997. module_exit(soc_camera_exit);
  998. MODULE_DESCRIPTION("Image capture bus driver");
  999. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1000. MODULE_LICENSE("GPL");
  1001. MODULE_ALIAS("platform:soc-camera-pdrv");