soc_camera.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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/vmalloc.h>
  27. #include <media/soc_camera.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-dev.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); /* Protects the list of hosts */
  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. /* Always entered with .video_lock held */
  171. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  172. {
  173. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  174. int i, fmts = 0;
  175. if (!ici->ops->get_formats)
  176. /*
  177. * Fallback mode - the host will have to serve all
  178. * sensor-provided formats one-to-one to the user
  179. */
  180. fmts = icd->num_formats;
  181. else
  182. /*
  183. * First pass - only count formats this host-sensor
  184. * configuration can provide
  185. */
  186. for (i = 0; i < icd->num_formats; i++)
  187. fmts += ici->ops->get_formats(icd, i, NULL);
  188. if (!fmts)
  189. return -ENXIO;
  190. icd->user_formats =
  191. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  192. if (!icd->user_formats)
  193. return -ENOMEM;
  194. icd->num_user_formats = fmts;
  195. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  196. /* Second pass - actually fill data formats */
  197. fmts = 0;
  198. for (i = 0; i < icd->num_formats; i++)
  199. if (!ici->ops->get_formats) {
  200. icd->user_formats[i].host_fmt = icd->formats + i;
  201. icd->user_formats[i].cam_fmt = icd->formats + i;
  202. icd->user_formats[i].buswidth = icd->formats[i].depth;
  203. } else {
  204. fmts += ici->ops->get_formats(icd, i,
  205. &icd->user_formats[fmts]);
  206. }
  207. icd->current_fmt = icd->user_formats[0].host_fmt;
  208. return 0;
  209. }
  210. /* Always entered with .video_lock held */
  211. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  212. {
  213. icd->current_fmt = NULL;
  214. vfree(icd->user_formats);
  215. icd->user_formats = NULL;
  216. }
  217. /* Called with .vb_lock held */
  218. static int soc_camera_set_fmt(struct soc_camera_file *icf,
  219. struct v4l2_format *f)
  220. {
  221. struct soc_camera_device *icd = icf->icd;
  222. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  223. struct v4l2_pix_format *pix = &f->fmt.pix;
  224. int ret;
  225. /* We always call try_fmt() before set_fmt() or set_crop() */
  226. ret = ici->ops->try_fmt(icd, f);
  227. if (ret < 0)
  228. return ret;
  229. ret = ici->ops->set_fmt(icd, f);
  230. if (ret < 0) {
  231. return ret;
  232. } else if (!icd->current_fmt ||
  233. icd->current_fmt->fourcc != pix->pixelformat) {
  234. dev_err(ici->dev,
  235. "Host driver hasn't set up current format correctly!\n");
  236. return -EINVAL;
  237. }
  238. icd->width = pix->width;
  239. icd->height = pix->height;
  240. icf->vb_vidq.field =
  241. icd->field = pix->field;
  242. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  243. dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
  244. f->type);
  245. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  246. icd->width, icd->height);
  247. /* set physical bus parameters */
  248. return ici->ops->set_bus_param(icd, pix->pixelformat);
  249. }
  250. static int soc_camera_open(struct file *file)
  251. {
  252. struct video_device *vdev;
  253. struct soc_camera_device *icd;
  254. struct soc_camera_host *ici;
  255. struct soc_camera_file *icf;
  256. int ret;
  257. /*
  258. * It is safe to dereference these pointers now as long as a user has
  259. * the video device open - we are protected by the held cdev reference.
  260. */
  261. vdev = video_devdata(file);
  262. icd = container_of(vdev->parent, struct soc_camera_device, dev);
  263. if (!icd->ops)
  264. /* No device driver attached */
  265. return -ENODEV;
  266. ici = to_soc_camera_host(icd->dev.parent);
  267. icf = vmalloc(sizeof(*icf));
  268. if (!icf)
  269. return -ENOMEM;
  270. if (!try_module_get(icd->ops->owner)) {
  271. dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
  272. ret = -EINVAL;
  273. goto emgd;
  274. }
  275. if (!try_module_get(ici->ops->owner)) {
  276. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  277. ret = -EINVAL;
  278. goto emgi;
  279. }
  280. /* Protect against icd->ops->remove() until we module_get() both drivers. */
  281. mutex_lock(&icd->video_lock);
  282. icf->icd = icd;
  283. icd->use_count++;
  284. /* Now we really have to activate the camera */
  285. if (icd->use_count == 1) {
  286. /* Restore parameters before the last close() per V4L2 API */
  287. struct v4l2_format f = {
  288. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  289. .fmt.pix = {
  290. .width = icd->width,
  291. .height = icd->height,
  292. .field = icd->field,
  293. },
  294. };
  295. ret = soc_camera_init_user_formats(icd);
  296. if (ret < 0)
  297. goto eiufmt;
  298. dev_dbg(&icd->dev, "Using fmt %x\n", icd->current_fmt->fourcc);
  299. f.fmt.pix.pixelformat = icd->current_fmt->fourcc;
  300. f.fmt.pix.colorspace = icd->current_fmt->colorspace;
  301. ret = ici->ops->add(icd);
  302. if (ret < 0) {
  303. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  304. goto eiciadd;
  305. }
  306. /* Try to configure with default parameters */
  307. ret = soc_camera_set_fmt(icf, &f);
  308. if (ret < 0)
  309. goto esfmt;
  310. }
  311. mutex_unlock(&icd->video_lock);
  312. file->private_data = icf;
  313. dev_dbg(&icd->dev, "camera device open\n");
  314. ici->ops->init_videobuf(&icf->vb_vidq, icd);
  315. return 0;
  316. /*
  317. * First three errors are entered with the .video_lock held
  318. * and use_count == 1
  319. */
  320. esfmt:
  321. ici->ops->remove(icd);
  322. eiciadd:
  323. soc_camera_free_user_formats(icd);
  324. eiufmt:
  325. icd->use_count--;
  326. mutex_unlock(&icd->video_lock);
  327. module_put(ici->ops->owner);
  328. emgi:
  329. module_put(icd->ops->owner);
  330. emgd:
  331. vfree(icf);
  332. return ret;
  333. }
  334. static int soc_camera_close(struct file *file)
  335. {
  336. struct soc_camera_file *icf = file->private_data;
  337. struct soc_camera_device *icd = icf->icd;
  338. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  339. struct video_device *vdev = icd->vdev;
  340. mutex_lock(&icd->video_lock);
  341. icd->use_count--;
  342. if (!icd->use_count) {
  343. ici->ops->remove(icd);
  344. soc_camera_free_user_formats(icd);
  345. }
  346. mutex_unlock(&icd->video_lock);
  347. module_put(icd->ops->owner);
  348. module_put(ici->ops->owner);
  349. vfree(icf);
  350. dev_dbg(vdev->parent, "camera device close\n");
  351. return 0;
  352. }
  353. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  354. size_t count, loff_t *ppos)
  355. {
  356. struct soc_camera_file *icf = file->private_data;
  357. struct soc_camera_device *icd = icf->icd;
  358. struct video_device *vdev = icd->vdev;
  359. int err = -EINVAL;
  360. dev_err(vdev->parent, "camera device read not implemented\n");
  361. return err;
  362. }
  363. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  364. {
  365. struct soc_camera_file *icf = file->private_data;
  366. struct soc_camera_device *icd = icf->icd;
  367. int err;
  368. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  369. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  370. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  371. (unsigned long)vma->vm_start,
  372. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  373. err);
  374. return err;
  375. }
  376. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  377. {
  378. struct soc_camera_file *icf = file->private_data;
  379. struct soc_camera_device *icd = icf->icd;
  380. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  381. if (list_empty(&icf->vb_vidq.stream)) {
  382. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  383. return POLLERR;
  384. }
  385. return ici->ops->poll(file, pt);
  386. }
  387. static struct v4l2_file_operations soc_camera_fops = {
  388. .owner = THIS_MODULE,
  389. .open = soc_camera_open,
  390. .release = soc_camera_close,
  391. .ioctl = video_ioctl2,
  392. .read = soc_camera_read,
  393. .mmap = soc_camera_mmap,
  394. .poll = soc_camera_poll,
  395. };
  396. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  397. struct v4l2_format *f)
  398. {
  399. struct soc_camera_file *icf = file->private_data;
  400. struct soc_camera_device *icd = icf->icd;
  401. int ret;
  402. WARN_ON(priv != file->private_data);
  403. mutex_lock(&icf->vb_vidq.vb_lock);
  404. if (videobuf_queue_is_busy(&icf->vb_vidq)) {
  405. dev_err(&icd->dev, "S_FMT denied: queue busy\n");
  406. ret = -EBUSY;
  407. goto unlock;
  408. }
  409. ret = soc_camera_set_fmt(icf, f);
  410. unlock:
  411. mutex_unlock(&icf->vb_vidq.vb_lock);
  412. return ret;
  413. }
  414. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  415. struct v4l2_fmtdesc *f)
  416. {
  417. struct soc_camera_file *icf = file->private_data;
  418. struct soc_camera_device *icd = icf->icd;
  419. const struct soc_camera_data_format *format;
  420. WARN_ON(priv != file->private_data);
  421. if (f->index >= icd->num_user_formats)
  422. return -EINVAL;
  423. format = icd->user_formats[f->index].host_fmt;
  424. strlcpy(f->description, format->name, sizeof(f->description));
  425. f->pixelformat = format->fourcc;
  426. return 0;
  427. }
  428. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  429. struct v4l2_format *f)
  430. {
  431. struct soc_camera_file *icf = file->private_data;
  432. struct soc_camera_device *icd = icf->icd;
  433. struct v4l2_pix_format *pix = &f->fmt.pix;
  434. WARN_ON(priv != file->private_data);
  435. pix->width = icd->width;
  436. pix->height = icd->height;
  437. pix->field = icf->vb_vidq.field;
  438. pix->pixelformat = icd->current_fmt->fourcc;
  439. pix->bytesperline = pix->width *
  440. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  441. pix->sizeimage = pix->height * pix->bytesperline;
  442. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  443. icd->current_fmt->fourcc);
  444. return 0;
  445. }
  446. static int soc_camera_querycap(struct file *file, void *priv,
  447. struct v4l2_capability *cap)
  448. {
  449. struct soc_camera_file *icf = file->private_data;
  450. struct soc_camera_device *icd = icf->icd;
  451. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  452. WARN_ON(priv != file->private_data);
  453. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  454. return ici->ops->querycap(ici, cap);
  455. }
  456. static int soc_camera_streamon(struct file *file, void *priv,
  457. enum v4l2_buf_type i)
  458. {
  459. struct soc_camera_file *icf = file->private_data;
  460. struct soc_camera_device *icd = icf->icd;
  461. int ret;
  462. WARN_ON(priv != file->private_data);
  463. dev_dbg(&icd->dev, "%s\n", __func__);
  464. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  465. return -EINVAL;
  466. mutex_lock(&icd->video_lock);
  467. icd->ops->start_capture(icd);
  468. /* This calls buf_queue from host driver's videobuf_queue_ops */
  469. ret = videobuf_streamon(&icf->vb_vidq);
  470. mutex_unlock(&icd->video_lock);
  471. return ret;
  472. }
  473. static int soc_camera_streamoff(struct file *file, void *priv,
  474. enum v4l2_buf_type i)
  475. {
  476. struct soc_camera_file *icf = file->private_data;
  477. struct soc_camera_device *icd = icf->icd;
  478. WARN_ON(priv != file->private_data);
  479. dev_dbg(&icd->dev, "%s\n", __func__);
  480. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  481. return -EINVAL;
  482. mutex_lock(&icd->video_lock);
  483. /* This calls buf_release from host driver's videobuf_queue_ops for all
  484. * remaining buffers. When the last buffer is freed, stop capture */
  485. videobuf_streamoff(&icf->vb_vidq);
  486. icd->ops->stop_capture(icd);
  487. mutex_unlock(&icd->video_lock);
  488. return 0;
  489. }
  490. static int soc_camera_queryctrl(struct file *file, void *priv,
  491. struct v4l2_queryctrl *qc)
  492. {
  493. struct soc_camera_file *icf = file->private_data;
  494. struct soc_camera_device *icd = icf->icd;
  495. int i;
  496. WARN_ON(priv != file->private_data);
  497. if (!qc->id)
  498. return -EINVAL;
  499. for (i = 0; i < icd->ops->num_controls; i++)
  500. if (qc->id == icd->ops->controls[i].id) {
  501. memcpy(qc, &(icd->ops->controls[i]),
  502. sizeof(*qc));
  503. return 0;
  504. }
  505. return -EINVAL;
  506. }
  507. static int soc_camera_g_ctrl(struct file *file, void *priv,
  508. struct v4l2_control *ctrl)
  509. {
  510. struct soc_camera_file *icf = file->private_data;
  511. struct soc_camera_device *icd = icf->icd;
  512. WARN_ON(priv != file->private_data);
  513. switch (ctrl->id) {
  514. case V4L2_CID_GAIN:
  515. if (icd->gain == (unsigned short)~0)
  516. return -EINVAL;
  517. ctrl->value = icd->gain;
  518. return 0;
  519. case V4L2_CID_EXPOSURE:
  520. if (icd->exposure == (unsigned short)~0)
  521. return -EINVAL;
  522. ctrl->value = icd->exposure;
  523. return 0;
  524. }
  525. if (icd->ops->get_control)
  526. return icd->ops->get_control(icd, ctrl);
  527. return -EINVAL;
  528. }
  529. static int soc_camera_s_ctrl(struct file *file, void *priv,
  530. struct v4l2_control *ctrl)
  531. {
  532. struct soc_camera_file *icf = file->private_data;
  533. struct soc_camera_device *icd = icf->icd;
  534. WARN_ON(priv != file->private_data);
  535. if (icd->ops->set_control)
  536. return icd->ops->set_control(icd, ctrl);
  537. return -EINVAL;
  538. }
  539. static int soc_camera_cropcap(struct file *file, void *fh,
  540. struct v4l2_cropcap *a)
  541. {
  542. struct soc_camera_file *icf = file->private_data;
  543. struct soc_camera_device *icd = icf->icd;
  544. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  545. a->bounds.left = icd->x_min;
  546. a->bounds.top = icd->y_min;
  547. a->bounds.width = icd->width_max;
  548. a->bounds.height = icd->height_max;
  549. a->defrect.left = icd->x_min;
  550. a->defrect.top = icd->y_min;
  551. a->defrect.width = DEFAULT_WIDTH;
  552. a->defrect.height = DEFAULT_HEIGHT;
  553. a->pixelaspect.numerator = 1;
  554. a->pixelaspect.denominator = 1;
  555. return 0;
  556. }
  557. static int soc_camera_g_crop(struct file *file, void *fh,
  558. struct v4l2_crop *a)
  559. {
  560. struct soc_camera_file *icf = file->private_data;
  561. struct soc_camera_device *icd = icf->icd;
  562. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  563. a->c.left = icd->x_current;
  564. a->c.top = icd->y_current;
  565. a->c.width = icd->width;
  566. a->c.height = icd->height;
  567. return 0;
  568. }
  569. static int soc_camera_s_crop(struct file *file, void *fh,
  570. struct v4l2_crop *a)
  571. {
  572. struct soc_camera_file *icf = file->private_data;
  573. struct soc_camera_device *icd = icf->icd;
  574. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  575. int ret;
  576. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  577. return -EINVAL;
  578. /* Cropping is allowed during a running capture, guard consistency */
  579. mutex_lock(&icf->vb_vidq.vb_lock);
  580. ret = ici->ops->set_crop(icd, &a->c);
  581. if (!ret) {
  582. icd->width = a->c.width;
  583. icd->height = a->c.height;
  584. icd->x_current = a->c.left;
  585. icd->y_current = a->c.top;
  586. }
  587. mutex_unlock(&icf->vb_vidq.vb_lock);
  588. return ret;
  589. }
  590. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  591. struct v4l2_dbg_chip_ident *id)
  592. {
  593. struct soc_camera_file *icf = file->private_data;
  594. struct soc_camera_device *icd = icf->icd;
  595. if (!icd->ops->get_chip_id)
  596. return -EINVAL;
  597. return icd->ops->get_chip_id(icd, id);
  598. }
  599. #ifdef CONFIG_VIDEO_ADV_DEBUG
  600. static int soc_camera_g_register(struct file *file, void *fh,
  601. struct v4l2_dbg_register *reg)
  602. {
  603. struct soc_camera_file *icf = file->private_data;
  604. struct soc_camera_device *icd = icf->icd;
  605. if (!icd->ops->get_register)
  606. return -EINVAL;
  607. return icd->ops->get_register(icd, reg);
  608. }
  609. static int soc_camera_s_register(struct file *file, void *fh,
  610. struct v4l2_dbg_register *reg)
  611. {
  612. struct soc_camera_file *icf = file->private_data;
  613. struct soc_camera_device *icd = icf->icd;
  614. if (!icd->ops->set_register)
  615. return -EINVAL;
  616. return icd->ops->set_register(icd, reg);
  617. }
  618. #endif
  619. /* So far this function cannot fail */
  620. static void scan_add_host(struct soc_camera_host *ici)
  621. {
  622. struct soc_camera_device *icd;
  623. mutex_lock(&list_lock);
  624. list_for_each_entry(icd, &devices, list) {
  625. if (icd->iface == ici->nr) {
  626. int ret;
  627. icd->dev.parent = ici->dev;
  628. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  629. icd->devnum);
  630. ret = device_register(&icd->dev);
  631. if (ret < 0) {
  632. icd->dev.parent = NULL;
  633. dev_err(&icd->dev,
  634. "Cannot register device: %d\n", ret);
  635. }
  636. }
  637. }
  638. mutex_unlock(&list_lock);
  639. }
  640. #ifdef CONFIG_I2C_BOARDINFO
  641. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  642. struct soc_camera_link *icl)
  643. {
  644. struct i2c_client *client;
  645. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  646. int ret;
  647. if (!adap) {
  648. ret = -ENODEV;
  649. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  650. icl->i2c_adapter_id);
  651. goto ei2cga;
  652. }
  653. icl->board_info->platform_data = icd;
  654. client = i2c_new_device(adap, icl->board_info);
  655. if (!client) {
  656. ret = -ENOMEM;
  657. goto ei2cnd;
  658. }
  659. /*
  660. * We set icd drvdata at two locations - here and in
  661. * soc_camera_video_start(). Depending on the module loading /
  662. * initialisation order one of these locations will be entered first
  663. */
  664. /* Use to_i2c_client(dev) to recover the i2c client */
  665. dev_set_drvdata(&icd->dev, &client->dev);
  666. return 0;
  667. ei2cnd:
  668. i2c_put_adapter(adap);
  669. ei2cga:
  670. return ret;
  671. }
  672. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  673. {
  674. struct i2c_client *client =
  675. to_i2c_client(to_soc_camera_control(icd));
  676. dev_set_drvdata(&icd->dev, NULL);
  677. i2c_unregister_device(client);
  678. i2c_put_adapter(client->adapter);
  679. }
  680. #else
  681. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  682. #define soc_camera_free_i2c(icd) do {} while (0)
  683. #endif
  684. static int video_dev_create(struct soc_camera_device *icd);
  685. /* Called during host-driver probe */
  686. static int soc_camera_probe(struct device *dev)
  687. {
  688. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  689. struct soc_camera_link *icl = to_soc_camera_link(icd);
  690. int ret;
  691. dev_info(dev, "Probing %s\n", dev_name(dev));
  692. ret = video_dev_create(icd);
  693. if (ret < 0)
  694. goto evdc;
  695. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  696. if (icl->board_info) {
  697. ret = soc_camera_init_i2c(icd, icl);
  698. if (ret < 0)
  699. goto eadddev;
  700. } else if (!icl->add_device || !icl->del_device) {
  701. ret = -EINVAL;
  702. goto eadddev;
  703. } else {
  704. ret = icl->add_device(icl, &icd->dev);
  705. if (ret < 0)
  706. goto eadddev;
  707. }
  708. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, icd->vdev->minor);
  709. if (ret < 0) {
  710. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  711. goto evidregd;
  712. }
  713. /* Do we have to sysfs_remove_link() before device_unregister()? */
  714. if (to_soc_camera_control(icd) &&
  715. sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  716. "control"))
  717. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  718. return 0;
  719. evidregd:
  720. if (icl->board_info)
  721. soc_camera_free_i2c(icd);
  722. else
  723. icl->del_device(icl);
  724. eadddev:
  725. video_device_release(icd->vdev);
  726. evdc:
  727. return ret;
  728. }
  729. /* This is called on device_unregister, which only means we have to disconnect
  730. * from the host, but not remove ourselves from the device list */
  731. static int soc_camera_remove(struct device *dev)
  732. {
  733. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  734. struct soc_camera_link *icl = to_soc_camera_link(icd);
  735. struct video_device *vdev = icd->vdev;
  736. BUG_ON(!dev->parent);
  737. if (vdev) {
  738. mutex_lock(&icd->video_lock);
  739. video_unregister_device(vdev);
  740. icd->vdev = NULL;
  741. mutex_unlock(&icd->video_lock);
  742. }
  743. if (icl->board_info)
  744. soc_camera_free_i2c(icd);
  745. else
  746. icl->del_device(icl);
  747. return 0;
  748. }
  749. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  750. {
  751. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  752. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  753. int ret = 0;
  754. if (ici->ops->suspend)
  755. ret = ici->ops->suspend(icd, state);
  756. return ret;
  757. }
  758. static int soc_camera_resume(struct device *dev)
  759. {
  760. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  761. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  762. int ret = 0;
  763. if (ici->ops->resume)
  764. ret = ici->ops->resume(icd);
  765. return ret;
  766. }
  767. static struct bus_type soc_camera_bus_type = {
  768. .name = "soc-camera",
  769. .probe = soc_camera_probe,
  770. .remove = soc_camera_remove,
  771. .suspend = soc_camera_suspend,
  772. .resume = soc_camera_resume,
  773. };
  774. static struct device_driver ic_drv = {
  775. .name = "camera",
  776. .bus = &soc_camera_bus_type,
  777. .owner = THIS_MODULE,
  778. };
  779. static void dummy_release(struct device *dev)
  780. {
  781. }
  782. int soc_camera_host_register(struct soc_camera_host *ici)
  783. {
  784. struct soc_camera_host *ix;
  785. if (!ici || !ici->ops ||
  786. !ici->ops->try_fmt ||
  787. !ici->ops->set_fmt ||
  788. !ici->ops->set_crop ||
  789. !ici->ops->set_bus_param ||
  790. !ici->ops->querycap ||
  791. !ici->ops->init_videobuf ||
  792. !ici->ops->reqbufs ||
  793. !ici->ops->add ||
  794. !ici->ops->remove ||
  795. !ici->ops->poll ||
  796. !ici->dev)
  797. return -EINVAL;
  798. mutex_lock(&list_lock);
  799. list_for_each_entry(ix, &hosts, list) {
  800. if (ix->nr == ici->nr) {
  801. mutex_unlock(&list_lock);
  802. return -EBUSY;
  803. }
  804. }
  805. dev_set_drvdata(ici->dev, ici);
  806. list_add_tail(&ici->list, &hosts);
  807. mutex_unlock(&list_lock);
  808. scan_add_host(ici);
  809. return 0;
  810. }
  811. EXPORT_SYMBOL(soc_camera_host_register);
  812. /* Unregister all clients! */
  813. void soc_camera_host_unregister(struct soc_camera_host *ici)
  814. {
  815. struct soc_camera_device *icd;
  816. mutex_lock(&list_lock);
  817. list_del(&ici->list);
  818. list_for_each_entry(icd, &devices, list) {
  819. if (icd->dev.parent == ici->dev) {
  820. /* The bus->remove will be called */
  821. device_unregister(&icd->dev);
  822. /* Not before device_unregister(), .remove
  823. * needs parent to call ici->ops->remove() */
  824. icd->dev.parent = NULL;
  825. /* If the host module is loaded again, device_register()
  826. * would complain "already initialised" */
  827. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  828. }
  829. }
  830. mutex_unlock(&list_lock);
  831. dev_set_drvdata(ici->dev, NULL);
  832. }
  833. EXPORT_SYMBOL(soc_camera_host_unregister);
  834. /* Image capture device */
  835. static int soc_camera_device_register(struct soc_camera_device *icd)
  836. {
  837. struct soc_camera_device *ix;
  838. int num = -1, i;
  839. for (i = 0; i < 256 && num < 0; i++) {
  840. num = i;
  841. /* Check if this index is available on this interface */
  842. list_for_each_entry(ix, &devices, list) {
  843. if (ix->iface == icd->iface && ix->devnum == i) {
  844. num = -1;
  845. break;
  846. }
  847. }
  848. }
  849. if (num < 0)
  850. /* ok, we have 256 cameras on this host...
  851. * man, stay reasonable... */
  852. return -ENOMEM;
  853. icd->devnum = num;
  854. icd->dev.bus = &soc_camera_bus_type;
  855. icd->dev.release = dummy_release;
  856. icd->use_count = 0;
  857. icd->host_priv = NULL;
  858. mutex_init(&icd->video_lock);
  859. list_add_tail(&icd->list, &devices);
  860. return 0;
  861. }
  862. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  863. {
  864. list_del(&icd->list);
  865. }
  866. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  867. .vidioc_querycap = soc_camera_querycap,
  868. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  869. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  870. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  871. .vidioc_enum_input = soc_camera_enum_input,
  872. .vidioc_g_input = soc_camera_g_input,
  873. .vidioc_s_input = soc_camera_s_input,
  874. .vidioc_s_std = soc_camera_s_std,
  875. .vidioc_reqbufs = soc_camera_reqbufs,
  876. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  877. .vidioc_querybuf = soc_camera_querybuf,
  878. .vidioc_qbuf = soc_camera_qbuf,
  879. .vidioc_dqbuf = soc_camera_dqbuf,
  880. .vidioc_streamon = soc_camera_streamon,
  881. .vidioc_streamoff = soc_camera_streamoff,
  882. .vidioc_queryctrl = soc_camera_queryctrl,
  883. .vidioc_g_ctrl = soc_camera_g_ctrl,
  884. .vidioc_s_ctrl = soc_camera_s_ctrl,
  885. .vidioc_cropcap = soc_camera_cropcap,
  886. .vidioc_g_crop = soc_camera_g_crop,
  887. .vidioc_s_crop = soc_camera_s_crop,
  888. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  889. #ifdef CONFIG_VIDEO_ADV_DEBUG
  890. .vidioc_g_register = soc_camera_g_register,
  891. .vidioc_s_register = soc_camera_s_register,
  892. #endif
  893. };
  894. static int video_dev_create(struct soc_camera_device *icd)
  895. {
  896. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  897. struct video_device *vdev = video_device_alloc();
  898. if (!vdev)
  899. return -ENOMEM;
  900. dev_dbg(ici->dev, "Allocated video_device %p\n", vdev);
  901. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  902. vdev->parent = &icd->dev;
  903. vdev->current_norm = V4L2_STD_UNKNOWN;
  904. vdev->fops = &soc_camera_fops;
  905. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  906. vdev->release = video_device_release;
  907. vdev->minor = -1;
  908. vdev->tvnorms = V4L2_STD_UNKNOWN;
  909. icd->vdev = vdev;
  910. return 0;
  911. }
  912. /*
  913. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  914. * soc_camera_probe() above with .video_lock held
  915. */
  916. int soc_camera_video_start(struct soc_camera_device *icd, struct device *dev)
  917. {
  918. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  919. const struct v4l2_queryctrl *qctrl;
  920. if (!icd->dev.parent)
  921. return -ENODEV;
  922. if (!icd->ops ||
  923. !icd->ops->init ||
  924. !icd->ops->release ||
  925. !icd->ops->start_capture ||
  926. !icd->ops->stop_capture ||
  927. !icd->ops->set_fmt ||
  928. !icd->ops->try_fmt ||
  929. !icd->ops->query_bus_param ||
  930. !icd->ops->set_bus_param)
  931. return -EINVAL;
  932. /* See comment in soc_camera_probe() */
  933. dev_set_drvdata(&icd->dev, dev);
  934. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  935. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  936. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  937. icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
  938. return ici->ops->add(icd);
  939. }
  940. EXPORT_SYMBOL(soc_camera_video_start);
  941. /* Called from client .remove() methods with .video_lock held */
  942. void soc_camera_video_stop(struct soc_camera_device *icd)
  943. {
  944. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  945. dev_dbg(&icd->dev, "%s\n", __func__);
  946. ici->ops->remove(icd);
  947. }
  948. EXPORT_SYMBOL(soc_camera_video_stop);
  949. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  950. {
  951. struct soc_camera_link *icl = pdev->dev.platform_data;
  952. struct soc_camera_device *icd;
  953. int ret;
  954. if (!icl)
  955. return -EINVAL;
  956. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  957. if (!icd)
  958. return -ENOMEM;
  959. icd->iface = icl->bus_id;
  960. platform_set_drvdata(pdev, icd);
  961. icd->dev.platform_data = icl;
  962. ret = soc_camera_device_register(icd);
  963. if (ret < 0)
  964. goto escdevreg;
  965. return 0;
  966. escdevreg:
  967. kfree(icd);
  968. return ret;
  969. }
  970. /* Only called on rmmod for each platform device, since they are not
  971. * hot-pluggable. Now we know, that all our users - hosts and devices have
  972. * been unloaded already */
  973. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  974. {
  975. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  976. if (!icd)
  977. return -EINVAL;
  978. soc_camera_device_unregister(icd);
  979. kfree(icd);
  980. return 0;
  981. }
  982. static struct platform_driver __refdata soc_camera_pdrv = {
  983. .remove = __devexit_p(soc_camera_pdrv_remove),
  984. .driver = {
  985. .name = "soc-camera-pdrv",
  986. .owner = THIS_MODULE,
  987. },
  988. };
  989. static int __init soc_camera_init(void)
  990. {
  991. int ret = bus_register(&soc_camera_bus_type);
  992. if (ret)
  993. return ret;
  994. ret = driver_register(&ic_drv);
  995. if (ret)
  996. goto edrvr;
  997. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  998. if (ret)
  999. goto epdr;
  1000. return 0;
  1001. epdr:
  1002. driver_unregister(&ic_drv);
  1003. edrvr:
  1004. bus_unregister(&soc_camera_bus_type);
  1005. return ret;
  1006. }
  1007. static void __exit soc_camera_exit(void)
  1008. {
  1009. platform_driver_unregister(&soc_camera_pdrv);
  1010. driver_unregister(&ic_drv);
  1011. bus_unregister(&soc_camera_bus_type);
  1012. }
  1013. module_init(soc_camera_init);
  1014. module_exit(soc_camera_exit);
  1015. MODULE_DESCRIPTION("Image capture bus driver");
  1016. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1017. MODULE_LICENSE("GPL");
  1018. MODULE_ALIAS("platform:soc-camera-pdrv");