soc_camera.c 34 KB

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