soc_camera.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. fmts = 0;
  195. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  196. /* Second pass - actually fill data formats */
  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. goto eiufmt;
  697. icd->height = DEFAULT_HEIGHT;
  698. icd->width = DEFAULT_WIDTH;
  699. icd->field = V4L2_FIELD_ANY;
  700. }
  701. eiufmt:
  702. ici->ops->remove(icd);
  703. eiadd:
  704. mutex_unlock(&icd->video_lock);
  705. module_put(ici->ops->owner);
  706. emgi:
  707. module_put(icd->ops->owner);
  708. emgd:
  709. return ret;
  710. }
  711. /* This is called on device_unregister, which only means we have to disconnect
  712. * from the host, but not remove ourselves from the device list */
  713. static int soc_camera_remove(struct device *dev)
  714. {
  715. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  716. if (icd->ops->remove)
  717. icd->ops->remove(icd);
  718. soc_camera_free_user_formats(icd);
  719. return 0;
  720. }
  721. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  722. {
  723. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  724. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  725. int ret = 0;
  726. if (ici->ops->suspend)
  727. ret = ici->ops->suspend(icd, state);
  728. return ret;
  729. }
  730. static int soc_camera_resume(struct device *dev)
  731. {
  732. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  733. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  734. int ret = 0;
  735. if (ici->ops->resume)
  736. ret = ici->ops->resume(icd);
  737. return ret;
  738. }
  739. static struct bus_type soc_camera_bus_type = {
  740. .name = "soc-camera",
  741. .probe = soc_camera_probe,
  742. .remove = soc_camera_remove,
  743. .suspend = soc_camera_suspend,
  744. .resume = soc_camera_resume,
  745. };
  746. static struct device_driver ic_drv = {
  747. .name = "camera",
  748. .bus = &soc_camera_bus_type,
  749. .owner = THIS_MODULE,
  750. };
  751. static void dummy_release(struct device *dev)
  752. {
  753. }
  754. int soc_camera_host_register(struct soc_camera_host *ici)
  755. {
  756. struct soc_camera_host *ix;
  757. if (!ici || !ici->ops ||
  758. !ici->ops->try_fmt ||
  759. !ici->ops->set_fmt ||
  760. !ici->ops->set_crop ||
  761. !ici->ops->set_bus_param ||
  762. !ici->ops->querycap ||
  763. !ici->ops->init_videobuf ||
  764. !ici->ops->reqbufs ||
  765. !ici->ops->add ||
  766. !ici->ops->remove ||
  767. !ici->ops->poll ||
  768. !ici->dev)
  769. return -EINVAL;
  770. mutex_lock(&list_lock);
  771. list_for_each_entry(ix, &hosts, list) {
  772. if (ix->nr == ici->nr) {
  773. mutex_unlock(&list_lock);
  774. return -EBUSY;
  775. }
  776. }
  777. dev_set_drvdata(ici->dev, ici);
  778. list_add_tail(&ici->list, &hosts);
  779. mutex_unlock(&list_lock);
  780. scan_add_host(ici);
  781. return 0;
  782. }
  783. EXPORT_SYMBOL(soc_camera_host_register);
  784. /* Unregister all clients! */
  785. void soc_camera_host_unregister(struct soc_camera_host *ici)
  786. {
  787. struct soc_camera_device *icd;
  788. mutex_lock(&list_lock);
  789. list_del(&ici->list);
  790. list_for_each_entry(icd, &devices, list) {
  791. if (icd->dev.parent == ici->dev) {
  792. device_unregister(&icd->dev);
  793. /* Not before device_unregister(), .remove
  794. * needs parent to call ici->ops->remove() */
  795. icd->dev.parent = NULL;
  796. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  797. }
  798. }
  799. mutex_unlock(&list_lock);
  800. dev_set_drvdata(ici->dev, NULL);
  801. }
  802. EXPORT_SYMBOL(soc_camera_host_unregister);
  803. /* Image capture device */
  804. int soc_camera_device_register(struct soc_camera_device *icd)
  805. {
  806. struct soc_camera_device *ix;
  807. int num = -1, i;
  808. if (!icd || !icd->ops ||
  809. !icd->ops->probe ||
  810. !icd->ops->init ||
  811. !icd->ops->release ||
  812. !icd->ops->start_capture ||
  813. !icd->ops->stop_capture ||
  814. !icd->ops->set_crop ||
  815. !icd->ops->set_fmt ||
  816. !icd->ops->try_fmt ||
  817. !icd->ops->query_bus_param ||
  818. !icd->ops->set_bus_param)
  819. return -EINVAL;
  820. for (i = 0; i < 256 && num < 0; i++) {
  821. num = i;
  822. list_for_each_entry(ix, &devices, list) {
  823. if (ix->iface == icd->iface && ix->devnum == i) {
  824. num = -1;
  825. break;
  826. }
  827. }
  828. }
  829. if (num < 0)
  830. /* ok, we have 256 cameras on this host...
  831. * man, stay reasonable... */
  832. return -ENOMEM;
  833. icd->devnum = num;
  834. icd->dev.bus = &soc_camera_bus_type;
  835. icd->dev.release = dummy_release;
  836. icd->use_count = 0;
  837. icd->host_priv = NULL;
  838. mutex_init(&icd->video_lock);
  839. return scan_add_device(icd);
  840. }
  841. EXPORT_SYMBOL(soc_camera_device_register);
  842. void soc_camera_device_unregister(struct soc_camera_device *icd)
  843. {
  844. mutex_lock(&list_lock);
  845. list_del(&icd->list);
  846. /* The bus->remove will be eventually called */
  847. if (icd->dev.parent)
  848. device_unregister(&icd->dev);
  849. mutex_unlock(&list_lock);
  850. }
  851. EXPORT_SYMBOL(soc_camera_device_unregister);
  852. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  853. .vidioc_querycap = soc_camera_querycap,
  854. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  855. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  856. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  857. .vidioc_enum_input = soc_camera_enum_input,
  858. .vidioc_g_input = soc_camera_g_input,
  859. .vidioc_s_input = soc_camera_s_input,
  860. .vidioc_s_std = soc_camera_s_std,
  861. .vidioc_reqbufs = soc_camera_reqbufs,
  862. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  863. .vidioc_querybuf = soc_camera_querybuf,
  864. .vidioc_qbuf = soc_camera_qbuf,
  865. .vidioc_dqbuf = soc_camera_dqbuf,
  866. .vidioc_streamon = soc_camera_streamon,
  867. .vidioc_streamoff = soc_camera_streamoff,
  868. .vidioc_queryctrl = soc_camera_queryctrl,
  869. .vidioc_g_ctrl = soc_camera_g_ctrl,
  870. .vidioc_s_ctrl = soc_camera_s_ctrl,
  871. .vidioc_cropcap = soc_camera_cropcap,
  872. .vidioc_g_crop = soc_camera_g_crop,
  873. .vidioc_s_crop = soc_camera_s_crop,
  874. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  875. #ifdef CONFIG_VIDEO_ADV_DEBUG
  876. .vidioc_g_register = soc_camera_g_register,
  877. .vidioc_s_register = soc_camera_s_register,
  878. #endif
  879. };
  880. /*
  881. * Usually called from the struct soc_camera_ops .probe() method, i.e., from
  882. * soc_camera_probe() above with .video_lock held
  883. */
  884. int soc_camera_video_start(struct soc_camera_device *icd)
  885. {
  886. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  887. int err = -ENOMEM;
  888. struct video_device *vdev;
  889. if (!icd->dev.parent)
  890. return -ENODEV;
  891. vdev = video_device_alloc();
  892. if (!vdev)
  893. goto evidallocd;
  894. dev_dbg(ici->dev, "Allocated video_device %p\n", vdev);
  895. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  896. vdev->parent = &icd->dev;
  897. vdev->current_norm = V4L2_STD_UNKNOWN;
  898. vdev->fops = &soc_camera_fops;
  899. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  900. vdev->release = video_device_release;
  901. vdev->minor = -1;
  902. vdev->tvnorms = V4L2_STD_UNKNOWN,
  903. err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
  904. if (err < 0) {
  905. dev_err(vdev->parent, "video_register_device failed\n");
  906. goto evidregd;
  907. }
  908. icd->vdev = vdev;
  909. return 0;
  910. evidregd:
  911. video_device_release(vdev);
  912. evidallocd:
  913. return err;
  914. }
  915. EXPORT_SYMBOL(soc_camera_video_start);
  916. void soc_camera_video_stop(struct soc_camera_device *icd)
  917. {
  918. struct video_device *vdev = icd->vdev;
  919. dev_dbg(&icd->dev, "%s\n", __func__);
  920. if (!icd->dev.parent || !vdev)
  921. return;
  922. mutex_lock(&icd->video_lock);
  923. video_unregister_device(vdev);
  924. icd->vdev = NULL;
  925. mutex_unlock(&icd->video_lock);
  926. }
  927. EXPORT_SYMBOL(soc_camera_video_stop);
  928. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  929. {
  930. struct soc_camera_link *icl = pdev->dev.platform_data;
  931. struct i2c_adapter *adap;
  932. struct i2c_client *client;
  933. if (!icl)
  934. return -EINVAL;
  935. adap = i2c_get_adapter(icl->i2c_adapter_id);
  936. if (!adap) {
  937. dev_warn(&pdev->dev, "Cannot get adapter #%d. No driver?\n",
  938. icl->i2c_adapter_id);
  939. /* -ENODEV and -ENXIO do not produce an error on probe()... */
  940. return -ENOENT;
  941. }
  942. icl->board_info->platform_data = icl;
  943. client = i2c_new_device(adap, icl->board_info);
  944. if (!client) {
  945. i2c_put_adapter(adap);
  946. return -ENOMEM;
  947. }
  948. platform_set_drvdata(pdev, client);
  949. return 0;
  950. }
  951. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  952. {
  953. struct i2c_client *client = platform_get_drvdata(pdev);
  954. if (!client)
  955. return -ENODEV;
  956. i2c_unregister_device(client);
  957. i2c_put_adapter(client->adapter);
  958. return 0;
  959. }
  960. static struct platform_driver __refdata soc_camera_pdrv = {
  961. .probe = soc_camera_pdrv_probe,
  962. .remove = __exit_p(soc_camera_pdrv_remove),
  963. .driver = {
  964. .name = "soc-camera-pdrv",
  965. .owner = THIS_MODULE,
  966. },
  967. };
  968. static int __init soc_camera_init(void)
  969. {
  970. int ret = bus_register(&soc_camera_bus_type);
  971. if (ret)
  972. return ret;
  973. ret = driver_register(&ic_drv);
  974. if (ret)
  975. goto edrvr;
  976. ret = platform_driver_register(&soc_camera_pdrv);
  977. if (ret)
  978. goto epdr;
  979. return 0;
  980. epdr:
  981. driver_unregister(&ic_drv);
  982. edrvr:
  983. bus_unregister(&soc_camera_bus_type);
  984. return ret;
  985. }
  986. static void __exit soc_camera_exit(void)
  987. {
  988. platform_driver_unregister(&soc_camera_pdrv);
  989. driver_unregister(&ic_drv);
  990. bus_unregister(&soc_camera_bus_type);
  991. }
  992. module_init(soc_camera_init);
  993. module_exit(soc_camera_exit);
  994. MODULE_DESCRIPTION("Image capture bus driver");
  995. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  996. MODULE_LICENSE("GPL");
  997. MODULE_ALIAS("platform:soc-camera-pdrv");