soc_camera.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/vmalloc.h>
  30. #include <media/soc_camera.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-dev.h>
  34. #include <media/videobuf-core.h>
  35. #include <media/videobuf2-core.h>
  36. #include <media/soc_mediabus.h>
  37. /* Default to VGA resolution */
  38. #define DEFAULT_WIDTH 640
  39. #define DEFAULT_HEIGHT 480
  40. #define is_streaming(ici, icd) \
  41. (((ici)->ops->init_videobuf) ? \
  42. (icd)->vb_vidq.streaming : \
  43. vb2_is_streaming(&(icd)->vb2_vidq))
  44. static LIST_HEAD(hosts);
  45. static LIST_HEAD(devices);
  46. static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */
  47. static int soc_camera_power_set(struct soc_camera_device *icd,
  48. struct soc_camera_link *icl,
  49. int power_on)
  50. {
  51. int ret;
  52. if (power_on) {
  53. ret = regulator_bulk_enable(icl->num_regulators,
  54. icl->regulators);
  55. if (ret < 0) {
  56. dev_err(&icd->dev, "Cannot enable regulators\n");
  57. return ret;
  58. }
  59. if (icl->power)
  60. ret = icl->power(icd->pdev, power_on);
  61. if (ret < 0) {
  62. dev_err(&icd->dev,
  63. "Platform failed to power-on the camera.\n");
  64. regulator_bulk_disable(icl->num_regulators,
  65. icl->regulators);
  66. return ret;
  67. }
  68. } else {
  69. ret = 0;
  70. if (icl->power)
  71. ret = icl->power(icd->pdev, 0);
  72. if (ret < 0) {
  73. dev_err(&icd->dev,
  74. "Platform failed to power-off the camera.\n");
  75. return ret;
  76. }
  77. ret = regulator_bulk_disable(icl->num_regulators,
  78. icl->regulators);
  79. if (ret < 0) {
  80. dev_err(&icd->dev, "Cannot disable regulators\n");
  81. return ret;
  82. }
  83. }
  84. return 0;
  85. }
  86. const struct soc_camera_format_xlate *soc_camera_xlate_by_fourcc(
  87. struct soc_camera_device *icd, unsigned int fourcc)
  88. {
  89. unsigned int i;
  90. for (i = 0; i < icd->num_user_formats; i++)
  91. if (icd->user_formats[i].host_fmt->fourcc == fourcc)
  92. return icd->user_formats + i;
  93. return NULL;
  94. }
  95. EXPORT_SYMBOL(soc_camera_xlate_by_fourcc);
  96. /**
  97. * soc_camera_apply_sensor_flags() - apply platform SOCAM_SENSOR_INVERT_* flags
  98. * @icl: camera platform parameters
  99. * @flags: flags to be inverted according to platform configuration
  100. * @return: resulting flags
  101. */
  102. unsigned long soc_camera_apply_sensor_flags(struct soc_camera_link *icl,
  103. unsigned long flags)
  104. {
  105. unsigned long f;
  106. /* If only one of the two polarities is supported, switch to the opposite */
  107. if (icl->flags & SOCAM_SENSOR_INVERT_HSYNC) {
  108. f = flags & (SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW);
  109. if (f == SOCAM_HSYNC_ACTIVE_HIGH || f == SOCAM_HSYNC_ACTIVE_LOW)
  110. flags ^= SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW;
  111. }
  112. if (icl->flags & SOCAM_SENSOR_INVERT_VSYNC) {
  113. f = flags & (SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW);
  114. if (f == SOCAM_VSYNC_ACTIVE_HIGH || f == SOCAM_VSYNC_ACTIVE_LOW)
  115. flags ^= SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW;
  116. }
  117. if (icl->flags & SOCAM_SENSOR_INVERT_PCLK) {
  118. f = flags & (SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING);
  119. if (f == SOCAM_PCLK_SAMPLE_RISING || f == SOCAM_PCLK_SAMPLE_FALLING)
  120. flags ^= SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING;
  121. }
  122. return flags;
  123. }
  124. EXPORT_SYMBOL(soc_camera_apply_sensor_flags);
  125. #define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
  126. ((x) >> 24) & 0xff
  127. static int soc_camera_try_fmt(struct soc_camera_device *icd,
  128. struct v4l2_format *f)
  129. {
  130. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  131. struct v4l2_pix_format *pix = &f->fmt.pix;
  132. int ret;
  133. dev_dbg(&icd->dev, "TRY_FMT(%c%c%c%c, %ux%u)\n",
  134. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  135. pix->bytesperline = 0;
  136. pix->sizeimage = 0;
  137. ret = ici->ops->try_fmt(icd, f);
  138. if (ret < 0)
  139. return ret;
  140. if (!pix->sizeimage) {
  141. if (!pix->bytesperline) {
  142. const struct soc_camera_format_xlate *xlate;
  143. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  144. if (!xlate)
  145. return -EINVAL;
  146. ret = soc_mbus_bytes_per_line(pix->width,
  147. xlate->host_fmt);
  148. if (ret > 0)
  149. pix->bytesperline = ret;
  150. }
  151. if (pix->bytesperline)
  152. pix->sizeimage = pix->bytesperline * pix->height;
  153. }
  154. return 0;
  155. }
  156. static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
  157. struct v4l2_format *f)
  158. {
  159. struct soc_camera_device *icd = file->private_data;
  160. WARN_ON(priv != file->private_data);
  161. /* Only single-plane capture is supported so far */
  162. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  163. return -EINVAL;
  164. /* limit format to hardware capabilities */
  165. return soc_camera_try_fmt(icd, f);
  166. }
  167. static int soc_camera_enum_input(struct file *file, void *priv,
  168. struct v4l2_input *inp)
  169. {
  170. struct soc_camera_device *icd = file->private_data;
  171. int ret = 0;
  172. if (inp->index != 0)
  173. return -EINVAL;
  174. if (icd->ops->enum_input)
  175. ret = icd->ops->enum_input(icd, inp);
  176. else {
  177. /* default is camera */
  178. inp->type = V4L2_INPUT_TYPE_CAMERA;
  179. inp->std = V4L2_STD_UNKNOWN;
  180. strcpy(inp->name, "Camera");
  181. }
  182. return ret;
  183. }
  184. static int soc_camera_g_input(struct file *file, void *priv, unsigned int *i)
  185. {
  186. *i = 0;
  187. return 0;
  188. }
  189. static int soc_camera_s_input(struct file *file, void *priv, unsigned int i)
  190. {
  191. if (i > 0)
  192. return -EINVAL;
  193. return 0;
  194. }
  195. static int soc_camera_s_std(struct file *file, void *priv, v4l2_std_id *a)
  196. {
  197. struct soc_camera_device *icd = file->private_data;
  198. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  199. return v4l2_subdev_call(sd, core, s_std, *a);
  200. }
  201. static int soc_camera_enum_fsizes(struct file *file, void *fh,
  202. struct v4l2_frmsizeenum *fsize)
  203. {
  204. struct soc_camera_device *icd = file->private_data;
  205. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  206. return ici->ops->enum_fsizes(icd, fsize);
  207. }
  208. static int soc_camera_reqbufs(struct file *file, void *priv,
  209. struct v4l2_requestbuffers *p)
  210. {
  211. int ret;
  212. struct soc_camera_device *icd = file->private_data;
  213. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  214. WARN_ON(priv != file->private_data);
  215. if (icd->streamer && icd->streamer != file)
  216. return -EBUSY;
  217. if (ici->ops->init_videobuf) {
  218. ret = videobuf_reqbufs(&icd->vb_vidq, p);
  219. if (ret < 0)
  220. return ret;
  221. ret = ici->ops->reqbufs(icd, p);
  222. } else {
  223. ret = vb2_reqbufs(&icd->vb2_vidq, p);
  224. }
  225. if (!ret && !icd->streamer)
  226. icd->streamer = file;
  227. return ret;
  228. }
  229. static int soc_camera_querybuf(struct file *file, void *priv,
  230. struct v4l2_buffer *p)
  231. {
  232. struct soc_camera_device *icd = file->private_data;
  233. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  234. WARN_ON(priv != file->private_data);
  235. if (ici->ops->init_videobuf)
  236. return videobuf_querybuf(&icd->vb_vidq, p);
  237. else
  238. return vb2_querybuf(&icd->vb2_vidq, p);
  239. }
  240. static int soc_camera_qbuf(struct file *file, void *priv,
  241. struct v4l2_buffer *p)
  242. {
  243. struct soc_camera_device *icd = file->private_data;
  244. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  245. WARN_ON(priv != file->private_data);
  246. if (icd->streamer != file)
  247. return -EBUSY;
  248. if (ici->ops->init_videobuf)
  249. return videobuf_qbuf(&icd->vb_vidq, p);
  250. else
  251. return vb2_qbuf(&icd->vb2_vidq, p);
  252. }
  253. static int soc_camera_dqbuf(struct file *file, void *priv,
  254. struct v4l2_buffer *p)
  255. {
  256. struct soc_camera_device *icd = file->private_data;
  257. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  258. WARN_ON(priv != file->private_data);
  259. if (icd->streamer != file)
  260. return -EBUSY;
  261. if (ici->ops->init_videobuf)
  262. return videobuf_dqbuf(&icd->vb_vidq, p, file->f_flags & O_NONBLOCK);
  263. else
  264. return vb2_dqbuf(&icd->vb2_vidq, p, file->f_flags & O_NONBLOCK);
  265. }
  266. /* Always entered with .video_lock held */
  267. static int soc_camera_init_user_formats(struct soc_camera_device *icd)
  268. {
  269. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  270. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  271. unsigned int i, fmts = 0, raw_fmts = 0;
  272. int ret;
  273. enum v4l2_mbus_pixelcode code;
  274. while (!v4l2_subdev_call(sd, video, enum_mbus_fmt, raw_fmts, &code))
  275. raw_fmts++;
  276. if (!ici->ops->get_formats)
  277. /*
  278. * Fallback mode - the host will have to serve all
  279. * sensor-provided formats one-to-one to the user
  280. */
  281. fmts = raw_fmts;
  282. else
  283. /*
  284. * First pass - only count formats this host-sensor
  285. * configuration can provide
  286. */
  287. for (i = 0; i < raw_fmts; i++) {
  288. ret = ici->ops->get_formats(icd, i, NULL);
  289. if (ret < 0)
  290. return ret;
  291. fmts += ret;
  292. }
  293. if (!fmts)
  294. return -ENXIO;
  295. icd->user_formats =
  296. vmalloc(fmts * sizeof(struct soc_camera_format_xlate));
  297. if (!icd->user_formats)
  298. return -ENOMEM;
  299. dev_dbg(&icd->dev, "Found %d supported formats.\n", fmts);
  300. /* Second pass - actually fill data formats */
  301. fmts = 0;
  302. for (i = 0; i < raw_fmts; i++)
  303. if (!ici->ops->get_formats) {
  304. v4l2_subdev_call(sd, video, enum_mbus_fmt, i, &code);
  305. icd->user_formats[fmts].host_fmt =
  306. soc_mbus_get_fmtdesc(code);
  307. if (icd->user_formats[fmts].host_fmt)
  308. icd->user_formats[fmts++].code = code;
  309. } else {
  310. ret = ici->ops->get_formats(icd, i,
  311. &icd->user_formats[fmts]);
  312. if (ret < 0)
  313. goto egfmt;
  314. fmts += ret;
  315. }
  316. icd->num_user_formats = fmts;
  317. icd->current_fmt = &icd->user_formats[0];
  318. return 0;
  319. egfmt:
  320. vfree(icd->user_formats);
  321. return ret;
  322. }
  323. /* Always entered with .video_lock held */
  324. static void soc_camera_free_user_formats(struct soc_camera_device *icd)
  325. {
  326. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  327. if (ici->ops->put_formats)
  328. ici->ops->put_formats(icd);
  329. icd->current_fmt = NULL;
  330. icd->num_user_formats = 0;
  331. vfree(icd->user_formats);
  332. icd->user_formats = NULL;
  333. }
  334. /* Called with .vb_lock held, or from the first open(2), see comment there */
  335. static int soc_camera_set_fmt(struct soc_camera_device *icd,
  336. struct v4l2_format *f)
  337. {
  338. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  339. struct v4l2_pix_format *pix = &f->fmt.pix;
  340. int ret;
  341. dev_dbg(&icd->dev, "S_FMT(%c%c%c%c, %ux%u)\n",
  342. pixfmtstr(pix->pixelformat), pix->width, pix->height);
  343. /* We always call try_fmt() before set_fmt() or set_crop() */
  344. ret = soc_camera_try_fmt(icd, f);
  345. if (ret < 0)
  346. return ret;
  347. ret = ici->ops->set_fmt(icd, f);
  348. if (ret < 0) {
  349. return ret;
  350. } else if (!icd->current_fmt ||
  351. icd->current_fmt->host_fmt->fourcc != pix->pixelformat) {
  352. dev_err(&icd->dev,
  353. "Host driver hasn't set up current format correctly!\n");
  354. return -EINVAL;
  355. }
  356. icd->user_width = pix->width;
  357. icd->user_height = pix->height;
  358. icd->bytesperline = pix->bytesperline;
  359. icd->sizeimage = pix->sizeimage;
  360. icd->colorspace = pix->colorspace;
  361. icd->field = pix->field;
  362. if (ici->ops->init_videobuf)
  363. icd->vb_vidq.field = pix->field;
  364. dev_dbg(&icd->dev, "set width: %d height: %d\n",
  365. icd->user_width, icd->user_height);
  366. /* set physical bus parameters */
  367. return ici->ops->set_bus_param(icd, pix->pixelformat);
  368. }
  369. static int soc_camera_open(struct file *file)
  370. {
  371. struct video_device *vdev = video_devdata(file);
  372. struct soc_camera_device *icd = container_of(vdev->parent,
  373. struct soc_camera_device,
  374. dev);
  375. struct soc_camera_link *icl = to_soc_camera_link(icd);
  376. struct soc_camera_host *ici;
  377. int ret;
  378. if (!icd->ops)
  379. /* No device driver attached */
  380. return -ENODEV;
  381. ici = to_soc_camera_host(icd->dev.parent);
  382. if (!try_module_get(ici->ops->owner)) {
  383. dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
  384. return -EINVAL;
  385. }
  386. icd->use_count++;
  387. /* Now we really have to activate the camera */
  388. if (icd->use_count == 1) {
  389. /* Restore parameters before the last close() per V4L2 API */
  390. struct v4l2_format f = {
  391. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  392. .fmt.pix = {
  393. .width = icd->user_width,
  394. .height = icd->user_height,
  395. .field = icd->field,
  396. .colorspace = icd->colorspace,
  397. .pixelformat =
  398. icd->current_fmt->host_fmt->fourcc,
  399. },
  400. };
  401. ret = soc_camera_power_set(icd, icl, 1);
  402. if (ret < 0)
  403. goto epower;
  404. /* The camera could have been already on, try to reset */
  405. if (icl->reset)
  406. icl->reset(icd->pdev);
  407. ret = ici->ops->add(icd);
  408. if (ret < 0) {
  409. dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
  410. goto eiciadd;
  411. }
  412. pm_runtime_enable(&icd->vdev->dev);
  413. ret = pm_runtime_resume(&icd->vdev->dev);
  414. if (ret < 0 && ret != -ENOSYS)
  415. goto eresume;
  416. /*
  417. * Try to configure with default parameters. Notice: this is the
  418. * very first open, so, we cannot race against other calls,
  419. * apart from someone else calling open() simultaneously, but
  420. * .video_lock is protecting us against it.
  421. */
  422. ret = soc_camera_set_fmt(icd, &f);
  423. if (ret < 0)
  424. goto esfmt;
  425. if (ici->ops->init_videobuf) {
  426. ici->ops->init_videobuf(&icd->vb_vidq, icd);
  427. } else {
  428. ret = ici->ops->init_videobuf2(&icd->vb2_vidq, icd);
  429. if (ret < 0)
  430. goto einitvb;
  431. }
  432. }
  433. file->private_data = icd;
  434. dev_dbg(&icd->dev, "camera device open\n");
  435. return 0;
  436. /*
  437. * First four errors are entered with the .video_lock held
  438. * and use_count == 1
  439. */
  440. einitvb:
  441. esfmt:
  442. pm_runtime_disable(&icd->vdev->dev);
  443. eresume:
  444. ici->ops->remove(icd);
  445. eiciadd:
  446. soc_camera_power_set(icd, icl, 0);
  447. epower:
  448. icd->use_count--;
  449. module_put(ici->ops->owner);
  450. return ret;
  451. }
  452. static int soc_camera_close(struct file *file)
  453. {
  454. struct soc_camera_device *icd = file->private_data;
  455. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  456. icd->use_count--;
  457. if (!icd->use_count) {
  458. struct soc_camera_link *icl = to_soc_camera_link(icd);
  459. pm_runtime_suspend(&icd->vdev->dev);
  460. pm_runtime_disable(&icd->vdev->dev);
  461. ici->ops->remove(icd);
  462. if (ici->ops->init_videobuf2)
  463. vb2_queue_release(&icd->vb2_vidq);
  464. soc_camera_power_set(icd, icl, 0);
  465. }
  466. if (icd->streamer == file)
  467. icd->streamer = NULL;
  468. module_put(ici->ops->owner);
  469. dev_dbg(&icd->dev, "camera device close\n");
  470. return 0;
  471. }
  472. static ssize_t soc_camera_read(struct file *file, char __user *buf,
  473. size_t count, loff_t *ppos)
  474. {
  475. struct soc_camera_device *icd = file->private_data;
  476. int err = -EINVAL;
  477. dev_err(&icd->dev, "camera device read not implemented\n");
  478. return err;
  479. }
  480. static int soc_camera_mmap(struct file *file, struct vm_area_struct *vma)
  481. {
  482. struct soc_camera_device *icd = file->private_data;
  483. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  484. int err;
  485. dev_dbg(&icd->dev, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  486. if (icd->streamer != file)
  487. return -EBUSY;
  488. if (ici->ops->init_videobuf)
  489. err = videobuf_mmap_mapper(&icd->vb_vidq, vma);
  490. else
  491. err = vb2_mmap(&icd->vb2_vidq, vma);
  492. dev_dbg(&icd->dev, "vma start=0x%08lx, size=%ld, ret=%d\n",
  493. (unsigned long)vma->vm_start,
  494. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
  495. err);
  496. return err;
  497. }
  498. static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
  499. {
  500. struct soc_camera_device *icd = file->private_data;
  501. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  502. if (icd->streamer != file)
  503. return -EBUSY;
  504. if (ici->ops->init_videobuf && list_empty(&icd->vb_vidq.stream)) {
  505. dev_err(&icd->dev, "Trying to poll with no queued buffers!\n");
  506. return POLLERR;
  507. }
  508. return ici->ops->poll(file, pt);
  509. }
  510. void soc_camera_lock(struct vb2_queue *vq)
  511. {
  512. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  513. mutex_lock(&icd->video_lock);
  514. }
  515. EXPORT_SYMBOL(soc_camera_lock);
  516. void soc_camera_unlock(struct vb2_queue *vq)
  517. {
  518. struct soc_camera_device *icd = vb2_get_drv_priv(vq);
  519. mutex_unlock(&icd->video_lock);
  520. }
  521. EXPORT_SYMBOL(soc_camera_unlock);
  522. static struct v4l2_file_operations soc_camera_fops = {
  523. .owner = THIS_MODULE,
  524. .open = soc_camera_open,
  525. .release = soc_camera_close,
  526. .unlocked_ioctl = video_ioctl2,
  527. .read = soc_camera_read,
  528. .mmap = soc_camera_mmap,
  529. .poll = soc_camera_poll,
  530. };
  531. static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
  532. struct v4l2_format *f)
  533. {
  534. struct soc_camera_device *icd = file->private_data;
  535. int ret;
  536. WARN_ON(priv != file->private_data);
  537. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  538. dev_warn(&icd->dev, "Wrong buf-type %d\n", f->type);
  539. return -EINVAL;
  540. }
  541. if (icd->streamer && icd->streamer != file)
  542. return -EBUSY;
  543. if (is_streaming(to_soc_camera_host(icd->dev.parent), icd)) {
  544. dev_err(&icd->dev, "S_FMT denied: queue initialised\n");
  545. return -EBUSY;
  546. }
  547. ret = soc_camera_set_fmt(icd, f);
  548. if (!ret && !icd->streamer)
  549. icd->streamer = file;
  550. return ret;
  551. }
  552. static int soc_camera_enum_fmt_vid_cap(struct file *file, void *priv,
  553. struct v4l2_fmtdesc *f)
  554. {
  555. struct soc_camera_device *icd = file->private_data;
  556. const struct soc_mbus_pixelfmt *format;
  557. WARN_ON(priv != file->private_data);
  558. if (f->index >= icd->num_user_formats)
  559. return -EINVAL;
  560. format = icd->user_formats[f->index].host_fmt;
  561. if (format->name)
  562. strlcpy(f->description, format->name, sizeof(f->description));
  563. f->pixelformat = format->fourcc;
  564. return 0;
  565. }
  566. static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
  567. struct v4l2_format *f)
  568. {
  569. struct soc_camera_device *icd = file->private_data;
  570. struct v4l2_pix_format *pix = &f->fmt.pix;
  571. WARN_ON(priv != file->private_data);
  572. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  573. return -EINVAL;
  574. pix->width = icd->user_width;
  575. pix->height = icd->user_height;
  576. pix->bytesperline = icd->bytesperline;
  577. pix->sizeimage = icd->sizeimage;
  578. pix->field = icd->field;
  579. pix->pixelformat = icd->current_fmt->host_fmt->fourcc;
  580. pix->colorspace = icd->colorspace;
  581. dev_dbg(&icd->dev, "current_fmt->fourcc: 0x%08x\n",
  582. icd->current_fmt->host_fmt->fourcc);
  583. return 0;
  584. }
  585. static int soc_camera_querycap(struct file *file, void *priv,
  586. struct v4l2_capability *cap)
  587. {
  588. struct soc_camera_device *icd = file->private_data;
  589. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  590. WARN_ON(priv != file->private_data);
  591. strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
  592. return ici->ops->querycap(ici, cap);
  593. }
  594. static int soc_camera_streamon(struct file *file, void *priv,
  595. enum v4l2_buf_type i)
  596. {
  597. struct soc_camera_device *icd = file->private_data;
  598. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  599. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  600. int ret;
  601. WARN_ON(priv != file->private_data);
  602. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  603. return -EINVAL;
  604. if (icd->streamer != file)
  605. return -EBUSY;
  606. /* This calls buf_queue from host driver's videobuf_queue_ops */
  607. if (ici->ops->init_videobuf)
  608. ret = videobuf_streamon(&icd->vb_vidq);
  609. else
  610. ret = vb2_streamon(&icd->vb2_vidq, i);
  611. if (!ret)
  612. v4l2_subdev_call(sd, video, s_stream, 1);
  613. return ret;
  614. }
  615. static int soc_camera_streamoff(struct file *file, void *priv,
  616. enum v4l2_buf_type i)
  617. {
  618. struct soc_camera_device *icd = file->private_data;
  619. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  620. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  621. WARN_ON(priv != file->private_data);
  622. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  623. return -EINVAL;
  624. if (icd->streamer != file)
  625. return -EBUSY;
  626. /*
  627. * This calls buf_release from host driver's videobuf_queue_ops for all
  628. * remaining buffers. When the last buffer is freed, stop capture
  629. */
  630. if (ici->ops->init_videobuf)
  631. videobuf_streamoff(&icd->vb_vidq);
  632. else
  633. vb2_streamoff(&icd->vb2_vidq, i);
  634. v4l2_subdev_call(sd, video, s_stream, 0);
  635. return 0;
  636. }
  637. static int soc_camera_queryctrl(struct file *file, void *priv,
  638. struct v4l2_queryctrl *qc)
  639. {
  640. struct soc_camera_device *icd = file->private_data;
  641. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  642. int i;
  643. WARN_ON(priv != file->private_data);
  644. if (!qc->id)
  645. return -EINVAL;
  646. /* First check host controls */
  647. for (i = 0; i < ici->ops->num_controls; i++)
  648. if (qc->id == ici->ops->controls[i].id) {
  649. memcpy(qc, &(ici->ops->controls[i]),
  650. sizeof(*qc));
  651. return 0;
  652. }
  653. /* Then device controls */
  654. for (i = 0; i < icd->ops->num_controls; i++)
  655. if (qc->id == icd->ops->controls[i].id) {
  656. memcpy(qc, &(icd->ops->controls[i]),
  657. sizeof(*qc));
  658. return 0;
  659. }
  660. return -EINVAL;
  661. }
  662. static int soc_camera_g_ctrl(struct file *file, void *priv,
  663. struct v4l2_control *ctrl)
  664. {
  665. struct soc_camera_device *icd = file->private_data;
  666. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  667. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  668. int ret;
  669. WARN_ON(priv != file->private_data);
  670. if (ici->ops->get_ctrl) {
  671. ret = ici->ops->get_ctrl(icd, ctrl);
  672. if (ret != -ENOIOCTLCMD)
  673. return ret;
  674. }
  675. return v4l2_subdev_call(sd, core, g_ctrl, ctrl);
  676. }
  677. static int soc_camera_s_ctrl(struct file *file, void *priv,
  678. struct v4l2_control *ctrl)
  679. {
  680. struct soc_camera_device *icd = file->private_data;
  681. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  682. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  683. int ret;
  684. WARN_ON(priv != file->private_data);
  685. if (ici->ops->set_ctrl) {
  686. ret = ici->ops->set_ctrl(icd, ctrl);
  687. if (ret != -ENOIOCTLCMD)
  688. return ret;
  689. }
  690. return v4l2_subdev_call(sd, core, s_ctrl, ctrl);
  691. }
  692. static int soc_camera_cropcap(struct file *file, void *fh,
  693. struct v4l2_cropcap *a)
  694. {
  695. struct soc_camera_device *icd = file->private_data;
  696. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  697. return ici->ops->cropcap(icd, a);
  698. }
  699. static int soc_camera_g_crop(struct file *file, void *fh,
  700. struct v4l2_crop *a)
  701. {
  702. struct soc_camera_device *icd = file->private_data;
  703. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  704. int ret;
  705. ret = ici->ops->get_crop(icd, a);
  706. return ret;
  707. }
  708. /*
  709. * According to the V4L2 API, drivers shall not update the struct v4l2_crop
  710. * argument with the actual geometry, instead, the user shall use G_CROP to
  711. * retrieve it.
  712. */
  713. static int soc_camera_s_crop(struct file *file, void *fh,
  714. struct v4l2_crop *a)
  715. {
  716. struct soc_camera_device *icd = file->private_data;
  717. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  718. struct v4l2_rect *rect = &a->c;
  719. struct v4l2_crop current_crop;
  720. int ret;
  721. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  722. return -EINVAL;
  723. dev_dbg(&icd->dev, "S_CROP(%ux%u@%u:%u)\n",
  724. rect->width, rect->height, rect->left, rect->top);
  725. /* If get_crop fails, we'll let host and / or client drivers decide */
  726. ret = ici->ops->get_crop(icd, &current_crop);
  727. /* Prohibit window size change with initialised buffers */
  728. if (ret < 0) {
  729. dev_err(&icd->dev,
  730. "S_CROP denied: getting current crop failed\n");
  731. } else if ((a->c.width == current_crop.c.width &&
  732. a->c.height == current_crop.c.height) ||
  733. !is_streaming(ici, icd)) {
  734. /* same size or not streaming - use .set_crop() */
  735. ret = ici->ops->set_crop(icd, a);
  736. } else if (ici->ops->set_livecrop) {
  737. ret = ici->ops->set_livecrop(icd, a);
  738. } else {
  739. dev_err(&icd->dev,
  740. "S_CROP denied: queue initialised and sizes differ\n");
  741. ret = -EBUSY;
  742. }
  743. return ret;
  744. }
  745. static int soc_camera_g_parm(struct file *file, void *fh,
  746. struct v4l2_streamparm *a)
  747. {
  748. struct soc_camera_device *icd = file->private_data;
  749. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  750. if (ici->ops->get_parm)
  751. return ici->ops->get_parm(icd, a);
  752. return -ENOIOCTLCMD;
  753. }
  754. static int soc_camera_s_parm(struct file *file, void *fh,
  755. struct v4l2_streamparm *a)
  756. {
  757. struct soc_camera_device *icd = file->private_data;
  758. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  759. if (ici->ops->set_parm)
  760. return ici->ops->set_parm(icd, a);
  761. return -ENOIOCTLCMD;
  762. }
  763. static int soc_camera_g_chip_ident(struct file *file, void *fh,
  764. struct v4l2_dbg_chip_ident *id)
  765. {
  766. struct soc_camera_device *icd = file->private_data;
  767. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  768. return v4l2_subdev_call(sd, core, g_chip_ident, id);
  769. }
  770. #ifdef CONFIG_VIDEO_ADV_DEBUG
  771. static int soc_camera_g_register(struct file *file, void *fh,
  772. struct v4l2_dbg_register *reg)
  773. {
  774. struct soc_camera_device *icd = file->private_data;
  775. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  776. return v4l2_subdev_call(sd, core, g_register, reg);
  777. }
  778. static int soc_camera_s_register(struct file *file, void *fh,
  779. struct v4l2_dbg_register *reg)
  780. {
  781. struct soc_camera_device *icd = file->private_data;
  782. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  783. return v4l2_subdev_call(sd, core, s_register, reg);
  784. }
  785. #endif
  786. /* So far this function cannot fail */
  787. static void scan_add_host(struct soc_camera_host *ici)
  788. {
  789. struct soc_camera_device *icd;
  790. mutex_lock(&list_lock);
  791. list_for_each_entry(icd, &devices, list) {
  792. if (icd->iface == ici->nr) {
  793. int ret;
  794. icd->dev.parent = ici->v4l2_dev.dev;
  795. dev_set_name(&icd->dev, "%u-%u", icd->iface,
  796. icd->devnum);
  797. ret = device_register(&icd->dev);
  798. if (ret < 0) {
  799. icd->dev.parent = NULL;
  800. dev_err(&icd->dev,
  801. "Cannot register device: %d\n", ret);
  802. }
  803. }
  804. }
  805. mutex_unlock(&list_lock);
  806. }
  807. #ifdef CONFIG_I2C_BOARDINFO
  808. static int soc_camera_init_i2c(struct soc_camera_device *icd,
  809. struct soc_camera_link *icl)
  810. {
  811. struct i2c_client *client;
  812. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  813. struct i2c_adapter *adap = i2c_get_adapter(icl->i2c_adapter_id);
  814. struct v4l2_subdev *subdev;
  815. if (!adap) {
  816. dev_err(&icd->dev, "Cannot get I2C adapter #%d. No driver?\n",
  817. icl->i2c_adapter_id);
  818. goto ei2cga;
  819. }
  820. icl->board_info->platform_data = icd;
  821. subdev = v4l2_i2c_new_subdev_board(&ici->v4l2_dev, adap,
  822. icl->board_info, NULL);
  823. if (!subdev)
  824. goto ei2cnd;
  825. client = v4l2_get_subdevdata(subdev);
  826. /* Use to_i2c_client(dev) to recover the i2c client */
  827. dev_set_drvdata(&icd->dev, &client->dev);
  828. return 0;
  829. ei2cnd:
  830. i2c_put_adapter(adap);
  831. ei2cga:
  832. return -ENODEV;
  833. }
  834. static void soc_camera_free_i2c(struct soc_camera_device *icd)
  835. {
  836. struct i2c_client *client =
  837. to_i2c_client(to_soc_camera_control(icd));
  838. struct i2c_adapter *adap = client->adapter;
  839. dev_set_drvdata(&icd->dev, NULL);
  840. v4l2_device_unregister_subdev(i2c_get_clientdata(client));
  841. i2c_unregister_device(client);
  842. i2c_put_adapter(adap);
  843. }
  844. #else
  845. #define soc_camera_init_i2c(icd, icl) (-ENODEV)
  846. #define soc_camera_free_i2c(icd) do {} while (0)
  847. #endif
  848. static int soc_camera_video_start(struct soc_camera_device *icd);
  849. static int video_dev_create(struct soc_camera_device *icd);
  850. /* Called during host-driver probe */
  851. static int soc_camera_probe(struct device *dev)
  852. {
  853. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  854. struct soc_camera_host *ici = to_soc_camera_host(dev->parent);
  855. struct soc_camera_link *icl = to_soc_camera_link(icd);
  856. struct device *control = NULL;
  857. struct v4l2_subdev *sd;
  858. struct v4l2_mbus_framefmt mf;
  859. int ret;
  860. dev_info(dev, "Probing %s\n", dev_name(dev));
  861. ret = regulator_bulk_get(icd->pdev, icl->num_regulators,
  862. icl->regulators);
  863. if (ret < 0)
  864. goto ereg;
  865. ret = soc_camera_power_set(icd, icl, 1);
  866. if (ret < 0)
  867. goto epower;
  868. /* The camera could have been already on, try to reset */
  869. if (icl->reset)
  870. icl->reset(icd->pdev);
  871. ret = ici->ops->add(icd);
  872. if (ret < 0)
  873. goto eadd;
  874. /* Must have icd->vdev before registering the device */
  875. ret = video_dev_create(icd);
  876. if (ret < 0)
  877. goto evdc;
  878. /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */
  879. if (icl->board_info) {
  880. ret = soc_camera_init_i2c(icd, icl);
  881. if (ret < 0)
  882. goto eadddev;
  883. } else if (!icl->add_device || !icl->del_device) {
  884. ret = -EINVAL;
  885. goto eadddev;
  886. } else {
  887. if (icl->module_name)
  888. ret = request_module(icl->module_name);
  889. ret = icl->add_device(icl, &icd->dev);
  890. if (ret < 0)
  891. goto eadddev;
  892. /*
  893. * FIXME: this is racy, have to use driver-binding notification,
  894. * when it is available
  895. */
  896. control = to_soc_camera_control(icd);
  897. if (!control || !control->driver || !dev_get_drvdata(control) ||
  898. !try_module_get(control->driver->owner)) {
  899. icl->del_device(icl);
  900. goto enodrv;
  901. }
  902. }
  903. sd = soc_camera_to_subdev(icd);
  904. sd->grp_id = (long)icd;
  905. /* At this point client .probe() should have run already */
  906. ret = soc_camera_init_user_formats(icd);
  907. if (ret < 0)
  908. goto eiufmt;
  909. icd->field = V4L2_FIELD_ANY;
  910. icd->vdev->lock = &icd->video_lock;
  911. /*
  912. * ..._video_start() will create a device node, video_register_device()
  913. * itself is protected against concurrent open() calls, but we also have
  914. * to protect our data.
  915. */
  916. mutex_lock(&icd->video_lock);
  917. ret = soc_camera_video_start(icd);
  918. if (ret < 0)
  919. goto evidstart;
  920. /* Try to improve our guess of a reasonable window format */
  921. if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) {
  922. icd->user_width = mf.width;
  923. icd->user_height = mf.height;
  924. icd->colorspace = mf.colorspace;
  925. icd->field = mf.field;
  926. }
  927. /* Do we have to sysfs_remove_link() before device_unregister()? */
  928. if (sysfs_create_link(&icd->dev.kobj, &to_soc_camera_control(icd)->kobj,
  929. "control"))
  930. dev_warn(&icd->dev, "Failed creating the control symlink\n");
  931. ici->ops->remove(icd);
  932. soc_camera_power_set(icd, icl, 0);
  933. mutex_unlock(&icd->video_lock);
  934. return 0;
  935. evidstart:
  936. mutex_unlock(&icd->video_lock);
  937. soc_camera_free_user_formats(icd);
  938. eiufmt:
  939. if (icl->board_info) {
  940. soc_camera_free_i2c(icd);
  941. } else {
  942. icl->del_device(icl);
  943. module_put(control->driver->owner);
  944. }
  945. enodrv:
  946. eadddev:
  947. video_device_release(icd->vdev);
  948. evdc:
  949. ici->ops->remove(icd);
  950. eadd:
  951. soc_camera_power_set(icd, icl, 0);
  952. epower:
  953. regulator_bulk_free(icl->num_regulators, icl->regulators);
  954. ereg:
  955. return ret;
  956. }
  957. /*
  958. * This is called on device_unregister, which only means we have to disconnect
  959. * from the host, but not remove ourselves from the device list
  960. */
  961. static int soc_camera_remove(struct device *dev)
  962. {
  963. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  964. struct soc_camera_link *icl = to_soc_camera_link(icd);
  965. struct video_device *vdev = icd->vdev;
  966. BUG_ON(!dev->parent);
  967. if (vdev) {
  968. video_unregister_device(vdev);
  969. icd->vdev = NULL;
  970. }
  971. if (icl->board_info) {
  972. soc_camera_free_i2c(icd);
  973. } else {
  974. struct device_driver *drv = to_soc_camera_control(icd) ?
  975. to_soc_camera_control(icd)->driver : NULL;
  976. if (drv) {
  977. icl->del_device(icl);
  978. module_put(drv->owner);
  979. }
  980. }
  981. soc_camera_free_user_formats(icd);
  982. regulator_bulk_free(icl->num_regulators, icl->regulators);
  983. return 0;
  984. }
  985. static int soc_camera_suspend(struct device *dev, pm_message_t state)
  986. {
  987. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  988. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  989. int ret = 0;
  990. if (ici->ops->suspend)
  991. ret = ici->ops->suspend(icd, state);
  992. return ret;
  993. }
  994. static int soc_camera_resume(struct device *dev)
  995. {
  996. struct soc_camera_device *icd = to_soc_camera_dev(dev);
  997. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  998. int ret = 0;
  999. if (ici->ops->resume)
  1000. ret = ici->ops->resume(icd);
  1001. return ret;
  1002. }
  1003. struct bus_type soc_camera_bus_type = {
  1004. .name = "soc-camera",
  1005. .probe = soc_camera_probe,
  1006. .remove = soc_camera_remove,
  1007. .suspend = soc_camera_suspend,
  1008. .resume = soc_camera_resume,
  1009. };
  1010. EXPORT_SYMBOL_GPL(soc_camera_bus_type);
  1011. static struct device_driver ic_drv = {
  1012. .name = "camera",
  1013. .bus = &soc_camera_bus_type,
  1014. .owner = THIS_MODULE,
  1015. };
  1016. static void dummy_release(struct device *dev)
  1017. {
  1018. }
  1019. static int default_cropcap(struct soc_camera_device *icd,
  1020. struct v4l2_cropcap *a)
  1021. {
  1022. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1023. return v4l2_subdev_call(sd, video, cropcap, a);
  1024. }
  1025. static int default_g_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1026. {
  1027. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1028. return v4l2_subdev_call(sd, video, g_crop, a);
  1029. }
  1030. static int default_s_crop(struct soc_camera_device *icd, struct v4l2_crop *a)
  1031. {
  1032. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1033. return v4l2_subdev_call(sd, video, s_crop, a);
  1034. }
  1035. static int default_g_parm(struct soc_camera_device *icd,
  1036. struct v4l2_streamparm *parm)
  1037. {
  1038. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1039. return v4l2_subdev_call(sd, video, g_parm, parm);
  1040. }
  1041. static int default_s_parm(struct soc_camera_device *icd,
  1042. struct v4l2_streamparm *parm)
  1043. {
  1044. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1045. return v4l2_subdev_call(sd, video, s_parm, parm);
  1046. }
  1047. static int default_enum_fsizes(struct soc_camera_device *icd,
  1048. struct v4l2_frmsizeenum *fsize)
  1049. {
  1050. int ret;
  1051. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1052. const struct soc_camera_format_xlate *xlate;
  1053. __u32 pixfmt = fsize->pixel_format;
  1054. struct v4l2_frmsizeenum fsize_mbus = *fsize;
  1055. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1056. if (!xlate)
  1057. return -EINVAL;
  1058. /* map xlate-code to pixel_format, sensor only handle xlate-code*/
  1059. fsize_mbus.pixel_format = xlate->code;
  1060. ret = v4l2_subdev_call(sd, video, enum_mbus_fsizes, &fsize_mbus);
  1061. if (ret < 0)
  1062. return ret;
  1063. *fsize = fsize_mbus;
  1064. fsize->pixel_format = pixfmt;
  1065. return 0;
  1066. }
  1067. static void soc_camera_device_init(struct device *dev, void *pdata)
  1068. {
  1069. dev->platform_data = pdata;
  1070. dev->bus = &soc_camera_bus_type;
  1071. dev->release = dummy_release;
  1072. }
  1073. int soc_camera_host_register(struct soc_camera_host *ici)
  1074. {
  1075. struct soc_camera_host *ix;
  1076. int ret;
  1077. if (!ici || !ici->ops ||
  1078. !ici->ops->try_fmt ||
  1079. !ici->ops->set_fmt ||
  1080. !ici->ops->set_bus_param ||
  1081. !ici->ops->querycap ||
  1082. ((!ici->ops->init_videobuf ||
  1083. !ici->ops->reqbufs) &&
  1084. !ici->ops->init_videobuf2) ||
  1085. !ici->ops->add ||
  1086. !ici->ops->remove ||
  1087. !ici->ops->poll ||
  1088. !ici->v4l2_dev.dev)
  1089. return -EINVAL;
  1090. if (!ici->ops->set_crop)
  1091. ici->ops->set_crop = default_s_crop;
  1092. if (!ici->ops->get_crop)
  1093. ici->ops->get_crop = default_g_crop;
  1094. if (!ici->ops->cropcap)
  1095. ici->ops->cropcap = default_cropcap;
  1096. if (!ici->ops->set_parm)
  1097. ici->ops->set_parm = default_s_parm;
  1098. if (!ici->ops->get_parm)
  1099. ici->ops->get_parm = default_g_parm;
  1100. if (!ici->ops->enum_fsizes)
  1101. ici->ops->enum_fsizes = default_enum_fsizes;
  1102. mutex_lock(&list_lock);
  1103. list_for_each_entry(ix, &hosts, list) {
  1104. if (ix->nr == ici->nr) {
  1105. ret = -EBUSY;
  1106. goto edevreg;
  1107. }
  1108. }
  1109. ret = v4l2_device_register(ici->v4l2_dev.dev, &ici->v4l2_dev);
  1110. if (ret < 0)
  1111. goto edevreg;
  1112. list_add_tail(&ici->list, &hosts);
  1113. mutex_unlock(&list_lock);
  1114. scan_add_host(ici);
  1115. return 0;
  1116. edevreg:
  1117. mutex_unlock(&list_lock);
  1118. return ret;
  1119. }
  1120. EXPORT_SYMBOL(soc_camera_host_register);
  1121. /* Unregister all clients! */
  1122. void soc_camera_host_unregister(struct soc_camera_host *ici)
  1123. {
  1124. struct soc_camera_device *icd;
  1125. mutex_lock(&list_lock);
  1126. list_del(&ici->list);
  1127. list_for_each_entry(icd, &devices, list) {
  1128. if (icd->iface == ici->nr) {
  1129. void *pdata = icd->dev.platform_data;
  1130. /* The bus->remove will be called */
  1131. device_unregister(&icd->dev);
  1132. /*
  1133. * Not before device_unregister(), .remove
  1134. * needs parent to call ici->ops->remove().
  1135. * If the host module is loaded again, device_register()
  1136. * would complain "already initialised," since 2.6.32
  1137. * this is also needed to prevent use-after-free of the
  1138. * device private data.
  1139. */
  1140. memset(&icd->dev, 0, sizeof(icd->dev));
  1141. soc_camera_device_init(&icd->dev, pdata);
  1142. }
  1143. }
  1144. mutex_unlock(&list_lock);
  1145. v4l2_device_unregister(&ici->v4l2_dev);
  1146. }
  1147. EXPORT_SYMBOL(soc_camera_host_unregister);
  1148. /* Image capture device */
  1149. static int soc_camera_device_register(struct soc_camera_device *icd)
  1150. {
  1151. struct soc_camera_device *ix;
  1152. int num = -1, i;
  1153. for (i = 0; i < 256 && num < 0; i++) {
  1154. num = i;
  1155. /* Check if this index is available on this interface */
  1156. list_for_each_entry(ix, &devices, list) {
  1157. if (ix->iface == icd->iface && ix->devnum == i) {
  1158. num = -1;
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. if (num < 0)
  1164. /*
  1165. * ok, we have 256 cameras on this host...
  1166. * man, stay reasonable...
  1167. */
  1168. return -ENOMEM;
  1169. icd->devnum = num;
  1170. icd->use_count = 0;
  1171. icd->host_priv = NULL;
  1172. mutex_init(&icd->video_lock);
  1173. list_add_tail(&icd->list, &devices);
  1174. return 0;
  1175. }
  1176. static void soc_camera_device_unregister(struct soc_camera_device *icd)
  1177. {
  1178. list_del(&icd->list);
  1179. }
  1180. static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
  1181. .vidioc_querycap = soc_camera_querycap,
  1182. .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap,
  1183. .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
  1184. .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap,
  1185. .vidioc_enum_input = soc_camera_enum_input,
  1186. .vidioc_g_input = soc_camera_g_input,
  1187. .vidioc_s_input = soc_camera_s_input,
  1188. .vidioc_s_std = soc_camera_s_std,
  1189. .vidioc_enum_framesizes = soc_camera_enum_fsizes,
  1190. .vidioc_reqbufs = soc_camera_reqbufs,
  1191. .vidioc_try_fmt_vid_cap = soc_camera_try_fmt_vid_cap,
  1192. .vidioc_querybuf = soc_camera_querybuf,
  1193. .vidioc_qbuf = soc_camera_qbuf,
  1194. .vidioc_dqbuf = soc_camera_dqbuf,
  1195. .vidioc_streamon = soc_camera_streamon,
  1196. .vidioc_streamoff = soc_camera_streamoff,
  1197. .vidioc_queryctrl = soc_camera_queryctrl,
  1198. .vidioc_g_ctrl = soc_camera_g_ctrl,
  1199. .vidioc_s_ctrl = soc_camera_s_ctrl,
  1200. .vidioc_cropcap = soc_camera_cropcap,
  1201. .vidioc_g_crop = soc_camera_g_crop,
  1202. .vidioc_s_crop = soc_camera_s_crop,
  1203. .vidioc_g_parm = soc_camera_g_parm,
  1204. .vidioc_s_parm = soc_camera_s_parm,
  1205. .vidioc_g_chip_ident = soc_camera_g_chip_ident,
  1206. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1207. .vidioc_g_register = soc_camera_g_register,
  1208. .vidioc_s_register = soc_camera_s_register,
  1209. #endif
  1210. };
  1211. static int video_dev_create(struct soc_camera_device *icd)
  1212. {
  1213. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1214. struct video_device *vdev = video_device_alloc();
  1215. if (!vdev)
  1216. return -ENOMEM;
  1217. strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
  1218. vdev->parent = &icd->dev;
  1219. vdev->current_norm = V4L2_STD_UNKNOWN;
  1220. vdev->fops = &soc_camera_fops;
  1221. vdev->ioctl_ops = &soc_camera_ioctl_ops;
  1222. vdev->release = video_device_release;
  1223. vdev->tvnorms = V4L2_STD_UNKNOWN;
  1224. icd->vdev = vdev;
  1225. return 0;
  1226. }
  1227. /*
  1228. * Called from soc_camera_probe() above (with .video_lock held???)
  1229. */
  1230. static int soc_camera_video_start(struct soc_camera_device *icd)
  1231. {
  1232. const struct device_type *type = icd->vdev->dev.type;
  1233. int ret;
  1234. if (!icd->dev.parent)
  1235. return -ENODEV;
  1236. if (!icd->ops ||
  1237. !icd->ops->query_bus_param ||
  1238. !icd->ops->set_bus_param)
  1239. return -EINVAL;
  1240. ret = video_register_device(icd->vdev, VFL_TYPE_GRABBER, -1);
  1241. if (ret < 0) {
  1242. dev_err(&icd->dev, "video_register_device failed: %d\n", ret);
  1243. return ret;
  1244. }
  1245. /* Restore device type, possibly set by the subdevice driver */
  1246. icd->vdev->dev.type = type;
  1247. return 0;
  1248. }
  1249. static int __devinit soc_camera_pdrv_probe(struct platform_device *pdev)
  1250. {
  1251. struct soc_camera_link *icl = pdev->dev.platform_data;
  1252. struct soc_camera_device *icd;
  1253. int ret;
  1254. if (!icl)
  1255. return -EINVAL;
  1256. icd = kzalloc(sizeof(*icd), GFP_KERNEL);
  1257. if (!icd)
  1258. return -ENOMEM;
  1259. icd->iface = icl->bus_id;
  1260. icd->pdev = &pdev->dev;
  1261. platform_set_drvdata(pdev, icd);
  1262. ret = soc_camera_device_register(icd);
  1263. if (ret < 0)
  1264. goto escdevreg;
  1265. soc_camera_device_init(&icd->dev, icl);
  1266. icd->user_width = DEFAULT_WIDTH;
  1267. icd->user_height = DEFAULT_HEIGHT;
  1268. return 0;
  1269. escdevreg:
  1270. kfree(icd);
  1271. return ret;
  1272. }
  1273. /*
  1274. * Only called on rmmod for each platform device, since they are not
  1275. * hot-pluggable. Now we know, that all our users - hosts and devices have
  1276. * been unloaded already
  1277. */
  1278. static int __devexit soc_camera_pdrv_remove(struct platform_device *pdev)
  1279. {
  1280. struct soc_camera_device *icd = platform_get_drvdata(pdev);
  1281. if (!icd)
  1282. return -EINVAL;
  1283. soc_camera_device_unregister(icd);
  1284. kfree(icd);
  1285. return 0;
  1286. }
  1287. static struct platform_driver __refdata soc_camera_pdrv = {
  1288. .remove = __devexit_p(soc_camera_pdrv_remove),
  1289. .driver = {
  1290. .name = "soc-camera-pdrv",
  1291. .owner = THIS_MODULE,
  1292. },
  1293. };
  1294. static int __init soc_camera_init(void)
  1295. {
  1296. int ret = bus_register(&soc_camera_bus_type);
  1297. if (ret)
  1298. return ret;
  1299. ret = driver_register(&ic_drv);
  1300. if (ret)
  1301. goto edrvr;
  1302. ret = platform_driver_probe(&soc_camera_pdrv, soc_camera_pdrv_probe);
  1303. if (ret)
  1304. goto epdr;
  1305. return 0;
  1306. epdr:
  1307. driver_unregister(&ic_drv);
  1308. edrvr:
  1309. bus_unregister(&soc_camera_bus_type);
  1310. return ret;
  1311. }
  1312. static void __exit soc_camera_exit(void)
  1313. {
  1314. platform_driver_unregister(&soc_camera_pdrv);
  1315. driver_unregister(&ic_drv);
  1316. bus_unregister(&soc_camera_bus_type);
  1317. }
  1318. module_init(soc_camera_init);
  1319. module_exit(soc_camera_exit);
  1320. MODULE_DESCRIPTION("Image capture bus driver");
  1321. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1322. MODULE_LICENSE("GPL");
  1323. MODULE_ALIAS("platform:soc-camera-pdrv");