soc_camera.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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 v4l2_subdev *sd = soc_camera_to_subdev(icd);
  131. return v4l2_subdev_call(sd, 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(&icd->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. mutex_lock(&icd->video_lock);
  353. icd->use_count--;
  354. if (!icd->use_count) {
  355. struct soc_camera_link *icl = to_soc_camera_link(icd);
  356. if (icd->ops->release)
  357. icd->ops->release(icd);
  358. ici->ops->remove(icd);
  359. if (icl->power)
  360. icl->power(icd->pdev, 0);
  361. }
  362. mutex_unlock(&icd->video_lock);
  363. module_put(ici->ops->owner);
  364. vfree(icf);
  365. dev_dbg(&icd->dev, "camera device close\n");
  366. return 0;
  367. }
  368. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  369. size_t count, loff_t *ppos)
  370. {
  371. struct soc_camera_file *icf = file->private_data;
  372. struct soc_camera_device *icd = icf->icd;
  373. int err = -EINVAL;
  374. dev_err(&icd->dev, "camera device read not implemented\n");
  375. return err;
  376. }
  377. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  378. {
  379. struct soc_camera_file *icf = file->private_data;
  380. struct soc_camera_device *icd = icf->icd;
  381. int err;
  382. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  383. err = videobuf_mmap_mapper(&icf->vb_vidq, vma);
  384. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  385. (unsigned long)vma->vm_start,
  386. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  387. err);
  388. return err;
  389. }
  390. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  391. {
  392. struct soc_camera_file *icf = file->private_data;
  393. struct soc_camera_device *icd = icf->icd;
  394. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  395. if (list_empty(&icf->vb_vidq.stream)) {
  396. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  397. return POLLERR;
  398. }
  399. return ici->ops->poll(file, pt);
  400. }
  401. static struct v4l2_file_operations soc_camera_fops = {
  402. .owner = THIS_MODULE,
  403. .open = soc_camera_open,
  404. .release = soc_camera_close,
  405. .ioctl = video_ioctl2,
  406. .read = soc_camera_read,
  407. .mmap = soc_camera_mmap,
  408. .poll = soc_camera_poll,
  409. };
  410. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  411. struct v4l2_format *f)
  412. {
  413. struct soc_camera_file *icf = file->private_data;
  414. struct soc_camera_device *icd = icf->icd;
  415. int ret;
  416. WARN_ON(priv != file->private_data);
  417. mutex_lock(&icf->vb_vidq.vb_lock);
  418. if (icf->vb_vidq.bufs[0]) {
  419. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  420. ret = -EBUSY;
  421. goto unlock;
  422. }
  423. ret = soc_camera_set_fmt(icf, f);
  424. unlock:
  425. mutex_unlock(&icf->vb_vidq.vb_lock);
  426. return ret;
  427. }
  428. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  429. struct v4l2_fmtdesc *f)
  430. {
  431. struct soc_camera_file *icf = file->private_data;
  432. struct soc_camera_device *icd = icf->icd;
  433. const struct soc_camera_data_format *format;
  434. WARN_ON(priv != file->private_data);
  435. if (f->index >= icd->num_user_formats)
  436. return -EINVAL;
  437. format = icd->user_formats[f->index].host_fmt;
  438. strlcpy(f->description, format->name, sizeof(f->description));
  439. f->pixelformat = format->fourcc;
  440. return 0;
  441. }
  442. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  443. struct v4l2_format *f)
  444. {
  445. struct soc_camera_file *icf = file->private_data;
  446. struct soc_camera_device *icd = icf->icd;
  447. struct v4l2_pix_format *pix = &f->fmt.pix;
  448. WARN_ON(priv != file->private_data);
  449. pix->width = icd->rect_current.width;
  450. pix->height = icd->rect_current.height;
  451. pix->field = icf->vb_vidq.field;
  452. pix->pixelformat = icd->current_fmt->fourcc;
  453. pix->bytesperline = pix->width *
  454. DIV_ROUND_UP(icd->current_fmt->depth, 8);
  455. pix->sizeimage = pix->height * pix->bytesperline;
  456. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  457. icd->current_fmt->fourcc);
  458. return 0;
  459. }
  460. static int soc_camera_querycap(struct file *file, void *priv,
  461. struct v4l2_capability *cap)
  462. {
  463. struct soc_camera_file *icf = file->private_data;
  464. struct soc_camera_device *icd = icf->icd;
  465. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  466. WARN_ON(priv != file->private_data);
  467. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  468. return ici->ops->querycap(ici, cap);
  469. }
  470. static int soc_camera_streamon(struct file *file, void *priv,
  471. enum v4l2_buf_type i)
  472. {
  473. struct soc_camera_file *icf = file->private_data;
  474. struct soc_camera_device *icd = icf->icd;
  475. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  476. int ret;
  477. WARN_ON(priv != file->private_data);
  478. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  479. return -EINVAL;
  480. mutex_lock(&icd->video_lock);
  481. v4l2_subdev_call(sd, video, s_stream, 1);
  482. /* This calls buf_queue from host driver's videobuf_queue_ops */
  483. ret = videobuf_streamon(&icf->vb_vidq);
  484. mutex_unlock(&icd->video_lock);
  485. return ret;
  486. }
  487. static int soc_camera_streamoff(struct file *file, void *priv,
  488. enum v4l2_buf_type i)
  489. {
  490. struct soc_camera_file *icf = file->private_data;
  491. struct soc_camera_device *icd = icf->icd;
  492. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  493. WARN_ON(priv != file->private_data);
  494. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  495. return -EINVAL;
  496. mutex_lock(&icd->video_lock);
  497. /* This calls buf_release from host driver's videobuf_queue_ops for all
  498. * remaining buffers. When the last buffer is freed, stop capture */
  499. videobuf_streamoff(&icf->vb_vidq);
  500. v4l2_subdev_call(sd, video, s_stream, 0);
  501. mutex_unlock(&icd->video_lock);
  502. return 0;
  503. }
  504. static int soc_camera_queryctrl(struct file *file, void *priv,
  505. struct v4l2_queryctrl *qc)
  506. {
  507. struct soc_camera_file *icf = file->private_data;
  508. struct soc_camera_device *icd = icf->icd;
  509. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  510. int i;
  511. WARN_ON(priv != file->private_data);
  512. if (!qc->id)
  513. return -EINVAL;
  514. /* First check host controls */
  515. for (i = 0; i < ici->ops->num_controls; i++)
  516. if (qc->id == ici->ops->controls[i].id) {
  517. memcpy(qc, &(ici->ops->controls[i]),
  518. sizeof(*qc));
  519. return 0;
  520. }
  521. /* Then device controls */
  522. for (i = 0; i < icd->ops->num_controls; i++)
  523. if (qc->id == icd->ops->controls[i].id) {
  524. memcpy(qc, &(icd->ops->controls[i]),
  525. sizeof(*qc));
  526. return 0;
  527. }
  528. return -EINVAL;
  529. }
  530. static int soc_camera_g_ctrl(struct file *file, void *priv,
  531. struct v4l2_control *ctrl)
  532. {
  533. struct soc_camera_file *icf = file->private_data;
  534. struct soc_camera_device *icd = icf->icd;
  535. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  536. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  537. int ret;
  538. WARN_ON(priv != file->private_data);
  539. switch (ctrl->id) {
  540. case V4L2_CID_GAIN:
  541. if (icd->gain == (unsigned short)~0)
  542. return -EINVAL;
  543. ctrl->value = icd->gain;
  544. return 0;
  545. case V4L2_CID_EXPOSURE:
  546. if (icd->exposure == (unsigned short)~0)
  547. return -EINVAL;
  548. ctrl->value = icd->exposure;
  549. return 0;
  550. }
  551. if (ici->ops->get_ctrl) {
  552. ret = ici->ops->get_ctrl(icd, ctrl);
  553. if (ret != -ENOIOCTLCMD)
  554. return ret;
  555. }
  556. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  557. }
  558. static int soc_camera_s_ctrl(struct file *file, void *priv,
  559. struct v4l2_control *ctrl)
  560. {
  561. struct soc_camera_file *icf = file->private_data;
  562. struct soc_camera_device *icd = icf->icd;
  563. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  564. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  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_subdev_call(sd, 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. /* Prohibit window size change with initialised buffers */
  617. if (icf->vb_vidq.bufs[0] && (rect.width != icd->rect_current.width ||
  618. rect.height != icd->rect_current.height)) {
  619. dev_err(&icd->dev,
  620. "S_CROP denied: queue initialised and sizes differ\n");
  621. ret = -EBUSY;
  622. goto unlock;
  623. }
  624. if (rect.width > icd->rect_max.width)
  625. rect.width = icd->rect_max.width;
  626. if (rect.width < icd->width_min)
  627. rect.width = icd->width_min;
  628. if (rect.height > icd->rect_max.height)
  629. rect.height = icd->rect_max.height;
  630. if (rect.height < icd->height_min)
  631. rect.height = icd->height_min;
  632. if (rect.width + rect.left > icd->rect_max.width + icd->rect_max.left)
  633. rect.left = icd->rect_max.width + icd->rect_max.left -
  634. rect.width;
  635. if (rect.height + rect.top > icd->rect_max.height + icd->rect_max.top)
  636. rect.top = icd->rect_max.height + icd->rect_max.top -
  637. rect.height;
  638. ret = ici->ops->set_crop(icd, a);
  639. if (!ret)
  640. icd->rect_current = rect;
  641. unlock:
  642. mutex_unlock(&icf->vb_vidq.vb_lock);
  643. return ret;
  644. }
  645. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  646. struct v4l2_dbg_chip_ident *id)
  647. {
  648. struct soc_camera_file *icf = file->private_data;
  649. struct soc_camera_device *icd = icf->icd;
  650. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  651. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  652. }
  653. #ifdef CONFIG_VIDEO_ADV_DEBUG
  654. static int soc_camera_g_register(struct file *file, void *fh,
  655. struct v4l2_dbg_register *reg)
  656. {
  657. struct soc_camera_file *icf = file->private_data;
  658. struct soc_camera_device *icd = icf->icd;
  659. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  660. return v4l2_subdev_call(sd, core, g_register, reg);
  661. }
  662. static int soc_camera_s_register(struct file *file, void *fh,
  663. struct v4l2_dbg_register *reg)
  664. {
  665. struct soc_camera_file *icf = file->private_data;
  666. struct soc_camera_device *icd = icf->icd;
  667. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  668. return v4l2_subdev_call(sd, core, s_register, reg);
  669. }
  670. #endif
  671. /* So far this function cannot fail */
  672. static void scan_add_host(struct soc_camera_host *ici)
  673. {
  674. struct soc_camera_device *icd;
  675. mutex_lock(&list_lock);
  676. list_for_each_entry(icd, &devices, list) {
  677. if (icd->iface == ici->nr) {
  678. int ret;
  679. icd->dev.parent = ici->v4l2_dev.dev;
  680. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  681. icd->devnum);
  682. ret = device_register(&icd->dev);
  683. if (ret < 0) {
  684. icd->dev.parent = NULL;
  685. dev_err(&icd->dev,
  686. "Cannot register device: %d\n", ret);
  687. }
  688. }
  689. }
  690. mutex_unlock(&list_lock);
  691. }
  692. #ifdef CONFIG_I2C_BOARDINFO
  693. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  694. struct soc_camera_link *icl)
  695. {
  696. struct i2c_client *client;
  697. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  698. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  699. struct v4l2_subdev *subdev;
  700. int ret;
  701. if (!adap) {
  702. ret = -ENODEV;
  703. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  704. icl->i2c_adapter_id);
  705. goto ei2cga;
  706. }
  707. icl->board_info->platform_data = icd;
  708. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  709. icl->module_name, icl->board_info, NULL);
  710. if (!subdev) {
  711. ret = -ENOMEM;
  712. goto ei2cnd;
  713. }
  714. subdev->grp_id = (__u32)icd;
  715. client = subdev->priv;
  716. /* Use to_i2c_client(dev) to recover the i2c client */
  717. dev_set_drvdata(&icd->dev, &client->dev);
  718. return 0;
  719. ei2cnd:
  720. i2c_put_adapter(adap);
  721. ei2cga:
  722. return ret;
  723. }
  724. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  725. {
  726. struct i2c_client *client =
  727. to_i2c_client(to_soc_camera_control(icd));
  728. dev_set_drvdata(&icd->dev, NULL);
  729. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  730. i2c_unregister_device(client);
  731. i2c_put_adapter(client->adapter);
  732. }
  733. #else
  734. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  735. #define soc_camera_free_i2c(icd) do {} while (0)
  736. #endif
  737. static int soc_camera_video_start(struct soc_camera_device *icd);
  738. static int video_dev_create(struct soc_camera_device *icd);
  739. /* Called during host-driver probe */
  740. static int soc_camera_probe(struct device *dev)
  741. {
  742. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  743. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  744. struct soc_camera_link *icl = to_soc_camera_link(icd);
  745. struct device *control = NULL;
  746. int ret;
  747. dev_info(dev, "Probing %s\n", dev_name(dev));
  748. if (icl->power) {
  749. ret = icl->power(icd->pdev, 1);
  750. if (ret < 0) {
  751. dev_err(dev,
  752. "Platform failed to power-on the camera.\n");
  753. goto epower;
  754. }
  755. }
  756. /* The camera could have been already on, try to reset */
  757. if (icl->reset)
  758. icl->reset(icd->pdev);
  759. ret = ici->ops->add(icd);
  760. if (ret < 0)
  761. goto eadd;
  762. /* Must have icd->vdev before registering the device */
  763. ret = video_dev_create(icd);
  764. if (ret < 0)
  765. goto evdc;
  766. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  767. if (icl->board_info) {
  768. ret = soc_camera_init_i2c(icd, icl);
  769. if (ret < 0)
  770. goto eadddev;
  771. } else if (!icl->add_device || !icl->del_device) {
  772. ret = -EINVAL;
  773. goto eadddev;
  774. } else {
  775. if (icl->module_name)
  776. ret = request_module(icl->module_name);
  777. ret = icl->add_device(icl, &icd->dev);
  778. if (ret < 0)
  779. goto eadddev;
  780. /* FIXME: this is racy, have to use driver-binding notification */
  781. control = to_soc_camera_control(icd);
  782. if (!control || !control->driver || !dev_get_drvdata(control) ||
  783. !try_module_get(control->driver->owner)) {
  784. icl->del_device(icl);
  785. goto enodrv;
  786. }
  787. }
  788. /* At this point client .probe() should have run already */
  789. ret = soc_camera_init_user_formats(icd);
  790. if (ret < 0)
  791. goto eiufmt;
  792. icd->rect_current = icd->rect_max;
  793. icd->field = V4L2_FIELD_ANY;
  794. /* ..._video_start() will create a device node, so we have to protect */
  795. mutex_lock(&icd->video_lock);
  796. ret = soc_camera_video_start(icd);
  797. if (ret < 0)
  798. goto evidstart;
  799. /* Do we have to sysfs_remove_link() before device_unregister()? */
  800. if (to_soc_camera_control(icd) &&
  801. sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  802. "control"))
  803. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  804. ici->ops->remove(icd);
  805. if (icl->power)
  806. icl->power(icd->pdev, 0);
  807. mutex_unlock(&icd->video_lock);
  808. return 0;
  809. evidstart:
  810. mutex_unlock(&icd->video_lock);
  811. soc_camera_free_user_formats(icd);
  812. eiufmt:
  813. if (icl->board_info) {
  814. soc_camera_free_i2c(icd);
  815. } else {
  816. icl->del_device(icl);
  817. module_put(control->driver->owner);
  818. }
  819. enodrv:
  820. eadddev:
  821. video_device_release(icd->vdev);
  822. evdc:
  823. ici->ops->remove(icd);
  824. eadd:
  825. if (icl->power)
  826. icl->power(icd->pdev, 0);
  827. epower:
  828. return ret;
  829. }
  830. /* This is called on device_unregister, which only means we have to disconnect
  831. * from the host, but not remove ourselves from the device list */
  832. static int soc_camera_remove(struct device *dev)
  833. {
  834. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  835. struct soc_camera_link *icl = to_soc_camera_link(icd);
  836. struct video_device *vdev = icd->vdev;
  837. BUG_ON(!dev->parent);
  838. if (vdev) {
  839. mutex_lock(&icd->video_lock);
  840. video_unregister_device(vdev);
  841. icd->vdev = NULL;
  842. mutex_unlock(&icd->video_lock);
  843. }
  844. if (icl->board_info) {
  845. soc_camera_free_i2c(icd);
  846. } else {
  847. struct device_driver *drv = to_soc_camera_control(icd) ?
  848. to_soc_camera_control(icd)->driver : NULL;
  849. if (drv) {
  850. icl->del_device(icl);
  851. module_put(drv->owner);
  852. }
  853. }
  854. soc_camera_free_user_formats(icd);
  855. return 0;
  856. }
  857. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  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->suspend)
  863. ret = ici->ops->suspend(icd, state);
  864. return ret;
  865. }
  866. static int soc_camera_resume(struct device *dev)
  867. {
  868. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  869. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  870. int ret = 0;
  871. if (ici->ops->resume)
  872. ret = ici->ops->resume(icd);
  873. return ret;
  874. }
  875. static struct bus_type soc_camera_bus_type = {
  876. .name = "soc-camera",
  877. .probe = soc_camera_probe,
  878. .remove = soc_camera_remove,
  879. .suspend = soc_camera_suspend,
  880. .resume = soc_camera_resume,
  881. };
  882. static struct device_driver ic_drv = {
  883. .name = "camera",
  884. .bus = &soc_camera_bus_type,
  885. .owner = THIS_MODULE,
  886. };
  887. static void dummy_release(struct device *dev)
  888. {
  889. }
  890. int soc_camera_host_register(struct soc_camera_host *ici)
  891. {
  892. struct soc_camera_host *ix;
  893. int ret;
  894. if (!ici || !ici->ops ||
  895. !ici->ops->try_fmt ||
  896. !ici->ops->set_fmt ||
  897. !ici->ops->set_crop ||
  898. !ici->ops->set_bus_param ||
  899. !ici->ops->querycap ||
  900. !ici->ops->init_videobuf ||
  901. !ici->ops->reqbufs ||
  902. !ici->ops->add ||
  903. !ici->ops->remove ||
  904. !ici->ops->poll ||
  905. !ici->v4l2_dev.dev)
  906. return -EINVAL;
  907. mutex_lock(&list_lock);
  908. list_for_each_entry(ix, &hosts, list) {
  909. if (ix->nr == ici->nr) {
  910. ret = -EBUSY;
  911. goto edevreg;
  912. }
  913. }
  914. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  915. if (ret < 0)
  916. goto edevreg;
  917. list_add_tail(&ici->list, &hosts);
  918. mutex_unlock(&list_lock);
  919. scan_add_host(ici);
  920. return 0;
  921. edevreg:
  922. mutex_unlock(&list_lock);
  923. return ret;
  924. }
  925. EXPORT_SYMBOL(soc_camera_host_register);
  926. /* Unregister all clients! */
  927. void soc_camera_host_unregister(struct soc_camera_host *ici)
  928. {
  929. struct soc_camera_device *icd;
  930. mutex_lock(&list_lock);
  931. list_del(&ici->list);
  932. list_for_each_entry(icd, &devices, list) {
  933. if (icd->iface == ici->nr) {
  934. /* The bus->remove will be called */
  935. device_unregister(&icd->dev);
  936. /* Not before device_unregister(), .remove
  937. * needs parent to call ici->ops->remove() */
  938. icd->dev.parent = NULL;
  939. /* If the host module is loaded again, device_register()
  940. * would complain "already initialised" */
  941. memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
  942. }
  943. }
  944. mutex_unlock(&list_lock);
  945. v4l2_device_unregister(&ici->v4l2_dev);
  946. }
  947. EXPORT_SYMBOL(soc_camera_host_unregister);
  948. /* Image capture device */
  949. static int soc_camera_device_register(struct soc_camera_device *icd)
  950. {
  951. struct soc_camera_device *ix;
  952. int num = -1, i;
  953. for (i = 0; i < 256 && num < 0; i++) {
  954. num = i;
  955. /* Check if this index is available on this interface */
  956. list_for_each_entry(ix, &devices, list) {
  957. if (ix->iface == icd->iface && ix->devnum == i) {
  958. num = -1;
  959. break;
  960. }
  961. }
  962. }
  963. if (num < 0)
  964. /* ok, we have 256 cameras on this host...
  965. * man, stay reasonable... */
  966. return -ENOMEM;
  967. icd->devnum = num;
  968. icd->dev.bus = &soc_camera_bus_type;
  969. icd->dev.release = dummy_release;
  970. icd->use_count = 0;
  971. icd->host_priv = NULL;
  972. mutex_init(&icd->video_lock);
  973. list_add_tail(&icd->list, &devices);
  974. return 0;
  975. }
  976. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  977. {
  978. list_del(&icd->list);
  979. }
  980. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  981. .vidioc_querycap = soc_camera_querycap,
  982. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  983. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  984. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  985. .vidioc_enum_input = soc_camera_enum_input,
  986. .vidioc_g_input = soc_camera_g_input,
  987. .vidioc_s_input = soc_camera_s_input,
  988. .vidioc_s_std = soc_camera_s_std,
  989. .vidioc_reqbufs = soc_camera_reqbufs,
  990. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  991. .vidioc_querybuf = soc_camera_querybuf,
  992. .vidioc_qbuf = soc_camera_qbuf,
  993. .vidioc_dqbuf = soc_camera_dqbuf,
  994. .vidioc_streamon = soc_camera_streamon,
  995. .vidioc_streamoff = soc_camera_streamoff,
  996. .vidioc_queryctrl = soc_camera_queryctrl,
  997. .vidioc_g_ctrl = soc_camera_g_ctrl,
  998. .vidioc_s_ctrl = soc_camera_s_ctrl,
  999. .vidioc_cropcap = soc_camera_cropcap,
  1000. .vidioc_g_crop = soc_camera_g_crop,
  1001. .vidioc_s_crop = soc_camera_s_crop,
  1002. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1003. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1004. .vidioc_g_register = soc_camera_g_register,
  1005. .vidioc_s_register = soc_camera_s_register,
  1006. #endif
  1007. };
  1008. static int video_dev_create(struct soc_camera_device *icd)
  1009. {
  1010. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1011. struct video_device *vdev = video_device_alloc();
  1012. if (!vdev)
  1013. return -ENOMEM;
  1014. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1015. vdev->parent = &icd->dev;
  1016. vdev->current_norm = V4L2_STD_UNKNOWN;
  1017. vdev->fops = &soc_camera_fops;
  1018. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1019. vdev->release = video_device_release;
  1020. vdev->minor = -1;
  1021. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1022. icd->vdev = vdev;
  1023. return 0;
  1024. }
  1025. /*
  1026. * Called from soc_camera_probe() above (with .video_lock held???)
  1027. */
  1028. static int soc_camera_video_start(struct soc_camera_device *icd)
  1029. {
  1030. const struct v4l2_queryctrl *qctrl;
  1031. int ret;
  1032. if (!icd->dev.parent)
  1033. return -ENODEV;
  1034. if (!icd->ops ||
  1035. !icd->ops->query_bus_param ||
  1036. !icd->ops->set_bus_param)
  1037. return -EINVAL;
  1038. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER,
  1039. icd->vdev->minor);
  1040. if (ret < 0) {
  1041. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1042. return ret;
  1043. }
  1044. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
  1045. icd->gain = qctrl ? qctrl->default_value : (unsigned short)~0;
  1046. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  1047. icd->exposure = qctrl ? qctrl->default_value : (unsigned short)~0;
  1048. return 0;
  1049. }
  1050. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1051. {
  1052. struct soc_camera_link *icl = pdev->dev.platform_data;
  1053. struct soc_camera_device *icd;
  1054. int ret;
  1055. if (!icl)
  1056. return -EINVAL;
  1057. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1058. if (!icd)
  1059. return -ENOMEM;
  1060. icd->iface = icl->bus_id;
  1061. icd->pdev = &pdev->dev;
  1062. platform_set_drvdata(pdev, icd);
  1063. icd->dev.platform_data = icl;
  1064. ret = soc_camera_device_register(icd);
  1065. if (ret < 0)
  1066. goto escdevreg;
  1067. return 0;
  1068. escdevreg:
  1069. kfree(icd);
  1070. return ret;
  1071. }
  1072. /* Only called on rmmod for each platform device, since they are not
  1073. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1074. * been unloaded already */
  1075. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1076. {
  1077. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1078. if (!icd)
  1079. return -EINVAL;
  1080. soc_camera_device_unregister(icd);
  1081. kfree(icd);
  1082. return 0;
  1083. }
  1084. static struct platform_driver __refdata soc_camera_pdrv = {
  1085. .remove = __devexit_p(soc_camera_pdrv_remove),
  1086. .driver = {
  1087. .name = "soc-camera-pdrv",
  1088. .owner = THIS_MODULE,
  1089. },
  1090. };
  1091. static int __init soc_camera_init(void)
  1092. {
  1093. int ret = bus_register(&soc_camera_bus_type);
  1094. if (ret)
  1095. return ret;
  1096. ret = driver_register(&ic_drv);
  1097. if (ret)
  1098. goto edrvr;
  1099. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1100. if (ret)
  1101. goto epdr;
  1102. return 0;
  1103. epdr:
  1104. driver_unregister(&ic_drv);
  1105. edrvr:
  1106. bus_unregister(&soc_camera_bus_type);
  1107. return ret;
  1108. }
  1109. static void __exit soc_camera_exit(void)
  1110. {
  1111. platform_driver_unregister(&soc_camera_pdrv);
  1112. driver_unregister(&ic_drv);
  1113. bus_unregister(&soc_camera_bus_type);
  1114. }
  1115. module_init(soc_camera_init);
  1116. module_exit(soc_camera_exit);
  1117. MODULE_DESCRIPTION("Image capture bus driver");
  1118. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1119. MODULE_LICENSE("GPL");
  1120. MODULE_ALIAS("platform:soc-camera-pdrv");