sh_mobile_ceu_camera.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /*
  2. * V4L2 Driver for SuperH Mobile CEU interface
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
  7. *
  8. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/io.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/time.h>
  28. #include <linux/version.h>
  29. #include <linux/device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/sched.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-dev.h>
  36. #include <media/soc_camera.h>
  37. #include <media/sh_mobile_ceu.h>
  38. #include <media/videobuf-dma-contig.h>
  39. #include <media/v4l2-mediabus.h>
  40. #include <media/soc_mediabus.h>
  41. /* register offsets for sh7722 / sh7723 */
  42. #define CAPSR 0x00 /* Capture start register */
  43. #define CAPCR 0x04 /* Capture control register */
  44. #define CAMCR 0x08 /* Capture interface control register */
  45. #define CMCYR 0x0c /* Capture interface cycle register */
  46. #define CAMOR 0x10 /* Capture interface offset register */
  47. #define CAPWR 0x14 /* Capture interface width register */
  48. #define CAIFR 0x18 /* Capture interface input format register */
  49. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  50. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  51. #define CRCNTR 0x28 /* CEU register control register */
  52. #define CRCMPR 0x2c /* CEU register forcible control register */
  53. #define CFLCR 0x30 /* Capture filter control register */
  54. #define CFSZR 0x34 /* Capture filter size clip register */
  55. #define CDWDR 0x38 /* Capture destination width register */
  56. #define CDAYR 0x3c /* Capture data address Y register */
  57. #define CDACR 0x40 /* Capture data address C register */
  58. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  59. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  60. #define CBDSR 0x4c /* Capture bundle destination size register */
  61. #define CFWCR 0x5c /* Firewall operation control register */
  62. #define CLFCR 0x60 /* Capture low-pass filter control register */
  63. #define CDOCR 0x64 /* Capture data output control register */
  64. #define CDDCR 0x68 /* Capture data complexity level register */
  65. #define CDDAR 0x6c /* Capture data complexity level address register */
  66. #define CEIER 0x70 /* Capture event interrupt enable register */
  67. #define CETCR 0x74 /* Capture event flag clear register */
  68. #define CSTSR 0x7c /* Capture status register */
  69. #define CSRTR 0x80 /* Capture software reset register */
  70. #define CDSSR 0x84 /* Capture data size register */
  71. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  72. #define CDACR2 0x94 /* Capture data address C register 2 */
  73. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  74. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  75. #undef DEBUG_GEOMETRY
  76. #ifdef DEBUG_GEOMETRY
  77. #define dev_geo dev_info
  78. #else
  79. #define dev_geo dev_dbg
  80. #endif
  81. /* per video frame buffer */
  82. struct sh_mobile_ceu_buffer {
  83. struct videobuf_buffer vb; /* v4l buffer must be first */
  84. enum v4l2_mbus_pixelcode code;
  85. };
  86. struct sh_mobile_ceu_dev {
  87. struct soc_camera_host ici;
  88. struct soc_camera_device *icd;
  89. unsigned int irq;
  90. void __iomem *base;
  91. unsigned long video_limit;
  92. /* lock used to protect videobuf */
  93. spinlock_t lock;
  94. struct list_head capture;
  95. struct videobuf_buffer *active;
  96. struct sh_mobile_ceu_info *pdata;
  97. u32 cflcr;
  98. enum v4l2_field field;
  99. unsigned int image_mode:1;
  100. unsigned int is_16bit:1;
  101. };
  102. struct sh_mobile_ceu_cam {
  103. struct v4l2_rect ceu_rect;
  104. unsigned int cam_width;
  105. unsigned int cam_height;
  106. const struct soc_mbus_pixelfmt *extra_fmt;
  107. enum v4l2_mbus_pixelcode code;
  108. };
  109. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  110. {
  111. unsigned long flags;
  112. flags = SOCAM_MASTER |
  113. SOCAM_PCLK_SAMPLE_RISING |
  114. SOCAM_HSYNC_ACTIVE_HIGH |
  115. SOCAM_HSYNC_ACTIVE_LOW |
  116. SOCAM_VSYNC_ACTIVE_HIGH |
  117. SOCAM_VSYNC_ACTIVE_LOW |
  118. SOCAM_DATA_ACTIVE_HIGH;
  119. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  120. flags |= SOCAM_DATAWIDTH_8;
  121. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  122. flags |= SOCAM_DATAWIDTH_16;
  123. if (flags & SOCAM_DATAWIDTH_MASK)
  124. return flags;
  125. return 0;
  126. }
  127. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  128. unsigned long reg_offs, u32 data)
  129. {
  130. iowrite32(data, priv->base + reg_offs);
  131. }
  132. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  133. {
  134. return ioread32(priv->base + reg_offs);
  135. }
  136. static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
  137. {
  138. int i, success = 0;
  139. struct soc_camera_device *icd = pcdev->icd;
  140. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  141. /* wait CSTSR.CPTON bit */
  142. for (i = 0; i < 1000; i++) {
  143. if (!(ceu_read(pcdev, CSTSR) & 1)) {
  144. success++;
  145. break;
  146. }
  147. udelay(1);
  148. }
  149. /* wait CAPSR.CPKIL bit */
  150. for (i = 0; i < 1000; i++) {
  151. if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
  152. success++;
  153. break;
  154. }
  155. udelay(1);
  156. }
  157. if (2 != success) {
  158. dev_warn(&icd->dev, "soft reset time out\n");
  159. return -EIO;
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Videobuf operations
  165. */
  166. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  167. unsigned int *count,
  168. unsigned int *size)
  169. {
  170. struct soc_camera_device *icd = vq->priv_data;
  171. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  172. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  173. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  174. icd->current_fmt->host_fmt);
  175. if (bytes_per_line < 0)
  176. return bytes_per_line;
  177. *size = bytes_per_line * icd->user_height;
  178. if (0 == *count)
  179. *count = 2;
  180. if (pcdev->video_limit) {
  181. while (PAGE_ALIGN(*size) * *count > pcdev->video_limit)
  182. (*count)--;
  183. }
  184. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  185. return 0;
  186. }
  187. static void free_buffer(struct videobuf_queue *vq,
  188. struct sh_mobile_ceu_buffer *buf)
  189. {
  190. struct soc_camera_device *icd = vq->priv_data;
  191. struct device *dev = icd->dev.parent;
  192. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  193. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  194. if (in_interrupt())
  195. BUG();
  196. videobuf_waiton(&buf->vb, 0, 0);
  197. videobuf_dma_contig_free(vq, &buf->vb);
  198. dev_dbg(dev, "%s freed\n", __func__);
  199. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  200. }
  201. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  202. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  203. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  204. #define CEU_CEIER_VBP (1 << 20) /* vbp error */
  205. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  206. #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
  207. /*
  208. * return value doesn't reflex the success/failure to queue the new buffer,
  209. * but rather the status of the previous buffer.
  210. */
  211. static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  212. {
  213. struct soc_camera_device *icd = pcdev->icd;
  214. dma_addr_t phys_addr_top, phys_addr_bottom;
  215. unsigned long top1, top2;
  216. unsigned long bottom1, bottom2;
  217. u32 status;
  218. int ret = 0;
  219. /*
  220. * The hardware is _very_ picky about this sequence. Especially
  221. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  222. * several not-so-well documented interrupt sources in CETCR.
  223. */
  224. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
  225. status = ceu_read(pcdev, CETCR);
  226. ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
  227. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
  228. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  229. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  230. /*
  231. * When a VBP interrupt occurs, a capture end interrupt does not occur
  232. * and the image of that frame is not captured correctly. So, soft reset
  233. * is needed here.
  234. */
  235. if (status & CEU_CEIER_VBP) {
  236. sh_mobile_ceu_soft_reset(pcdev);
  237. ret = -EIO;
  238. }
  239. if (!pcdev->active)
  240. return ret;
  241. if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
  242. top1 = CDBYR;
  243. top2 = CDBCR;
  244. bottom1 = CDAYR;
  245. bottom2 = CDACR;
  246. } else {
  247. top1 = CDAYR;
  248. top2 = CDACR;
  249. bottom1 = CDBYR;
  250. bottom2 = CDBCR;
  251. }
  252. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  253. ceu_write(pcdev, top1, phys_addr_top);
  254. if (V4L2_FIELD_NONE != pcdev->field) {
  255. phys_addr_bottom = phys_addr_top + icd->user_width;
  256. ceu_write(pcdev, bottom1, phys_addr_bottom);
  257. }
  258. switch (icd->current_fmt->host_fmt->fourcc) {
  259. case V4L2_PIX_FMT_NV12:
  260. case V4L2_PIX_FMT_NV21:
  261. case V4L2_PIX_FMT_NV16:
  262. case V4L2_PIX_FMT_NV61:
  263. phys_addr_top += icd->user_width *
  264. icd->user_height;
  265. ceu_write(pcdev, top2, phys_addr_top);
  266. if (V4L2_FIELD_NONE != pcdev->field) {
  267. phys_addr_bottom = phys_addr_top + icd->user_width;
  268. ceu_write(pcdev, bottom2, phys_addr_bottom);
  269. }
  270. }
  271. pcdev->active->state = VIDEOBUF_ACTIVE;
  272. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  273. return ret;
  274. }
  275. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  276. struct videobuf_buffer *vb,
  277. enum v4l2_field field)
  278. {
  279. struct soc_camera_device *icd = vq->priv_data;
  280. struct sh_mobile_ceu_buffer *buf;
  281. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  282. icd->current_fmt->host_fmt);
  283. int ret;
  284. if (bytes_per_line < 0)
  285. return bytes_per_line;
  286. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  287. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  288. vb, vb->baddr, vb->bsize);
  289. /* Added list head initialization on alloc */
  290. WARN_ON(!list_empty(&vb->queue));
  291. #ifdef DEBUG
  292. /*
  293. * This can be useful if you want to see if we actually fill
  294. * the buffer with something
  295. */
  296. memset((void *)vb->baddr, 0xaa, vb->bsize);
  297. #endif
  298. BUG_ON(NULL == icd->current_fmt);
  299. if (buf->code != icd->current_fmt->code ||
  300. vb->width != icd->user_width ||
  301. vb->height != icd->user_height ||
  302. vb->field != field) {
  303. buf->code = icd->current_fmt->code;
  304. vb->width = icd->user_width;
  305. vb->height = icd->user_height;
  306. vb->field = field;
  307. vb->state = VIDEOBUF_NEEDS_INIT;
  308. }
  309. vb->size = vb->height * bytes_per_line;
  310. if (0 != vb->baddr && vb->bsize < vb->size) {
  311. ret = -EINVAL;
  312. goto out;
  313. }
  314. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  315. ret = videobuf_iolock(vq, vb, NULL);
  316. if (ret)
  317. goto fail;
  318. vb->state = VIDEOBUF_PREPARED;
  319. }
  320. return 0;
  321. fail:
  322. free_buffer(vq, buf);
  323. out:
  324. return ret;
  325. }
  326. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  327. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  328. struct videobuf_buffer *vb)
  329. {
  330. struct soc_camera_device *icd = vq->priv_data;
  331. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  332. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  333. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  334. vb, vb->baddr, vb->bsize);
  335. vb->state = VIDEOBUF_QUEUED;
  336. list_add_tail(&vb->queue, &pcdev->capture);
  337. if (!pcdev->active) {
  338. /*
  339. * Because there were no active buffer at this moment,
  340. * we are not interested in the return value of
  341. * sh_mobile_ceu_capture here.
  342. */
  343. pcdev->active = vb;
  344. sh_mobile_ceu_capture(pcdev);
  345. }
  346. }
  347. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  348. struct videobuf_buffer *vb)
  349. {
  350. struct soc_camera_device *icd = vq->priv_data;
  351. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  352. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  353. unsigned long flags;
  354. spin_lock_irqsave(&pcdev->lock, flags);
  355. if (pcdev->active == vb) {
  356. /* disable capture (release DMA buffer), reset */
  357. ceu_write(pcdev, CAPSR, 1 << 16);
  358. pcdev->active = NULL;
  359. }
  360. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
  361. !list_empty(&vb->queue)) {
  362. vb->state = VIDEOBUF_ERROR;
  363. list_del_init(&vb->queue);
  364. }
  365. spin_unlock_irqrestore(&pcdev->lock, flags);
  366. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  367. }
  368. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  369. .buf_setup = sh_mobile_ceu_videobuf_setup,
  370. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  371. .buf_queue = sh_mobile_ceu_videobuf_queue,
  372. .buf_release = sh_mobile_ceu_videobuf_release,
  373. };
  374. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  375. {
  376. struct sh_mobile_ceu_dev *pcdev = data;
  377. struct videobuf_buffer *vb;
  378. unsigned long flags;
  379. spin_lock_irqsave(&pcdev->lock, flags);
  380. vb = pcdev->active;
  381. if (!vb)
  382. /* Stale interrupt from a released buffer */
  383. goto out;
  384. list_del_init(&vb->queue);
  385. if (!list_empty(&pcdev->capture))
  386. pcdev->active = list_entry(pcdev->capture.next,
  387. struct videobuf_buffer, queue);
  388. else
  389. pcdev->active = NULL;
  390. vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
  391. VIDEOBUF_ERROR : VIDEOBUF_DONE;
  392. do_gettimeofday(&vb->ts);
  393. vb->field_count++;
  394. wake_up(&vb->done);
  395. out:
  396. spin_unlock_irqrestore(&pcdev->lock, flags);
  397. return IRQ_HANDLED;
  398. }
  399. /* Called with .video_lock held */
  400. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  401. {
  402. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  403. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  404. int ret;
  405. if (pcdev->icd)
  406. return -EBUSY;
  407. dev_info(icd->dev.parent,
  408. "SuperH Mobile CEU driver attached to camera %d\n",
  409. icd->devnum);
  410. pm_runtime_get_sync(ici->v4l2_dev.dev);
  411. ret = sh_mobile_ceu_soft_reset(pcdev);
  412. if (!ret)
  413. pcdev->icd = icd;
  414. return ret;
  415. }
  416. /* Called with .video_lock held */
  417. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  418. {
  419. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  420. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  421. unsigned long flags;
  422. BUG_ON(icd != pcdev->icd);
  423. /* disable capture, disable interrupts */
  424. ceu_write(pcdev, CEIER, 0);
  425. sh_mobile_ceu_soft_reset(pcdev);
  426. /* make sure active buffer is canceled */
  427. spin_lock_irqsave(&pcdev->lock, flags);
  428. if (pcdev->active) {
  429. list_del(&pcdev->active->queue);
  430. pcdev->active->state = VIDEOBUF_ERROR;
  431. wake_up_all(&pcdev->active->done);
  432. pcdev->active = NULL;
  433. }
  434. spin_unlock_irqrestore(&pcdev->lock, flags);
  435. pm_runtime_put_sync(ici->v4l2_dev.dev);
  436. dev_info(icd->dev.parent,
  437. "SuperH Mobile CEU driver detached from camera %d\n",
  438. icd->devnum);
  439. pcdev->icd = NULL;
  440. }
  441. /*
  442. * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
  443. * in SH7722 Hardware Manual
  444. */
  445. static unsigned int size_dst(unsigned int src, unsigned int scale)
  446. {
  447. unsigned int mant_pre = scale >> 12;
  448. if (!src || !scale)
  449. return src;
  450. return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
  451. mant_pre * 4096 / scale + 1;
  452. }
  453. static u16 calc_scale(unsigned int src, unsigned int *dst)
  454. {
  455. u16 scale;
  456. if (src == *dst)
  457. return 0;
  458. scale = (src * 4096 / *dst) & ~7;
  459. while (scale > 4096 && size_dst(src, scale) < *dst)
  460. scale -= 8;
  461. *dst = size_dst(src, scale);
  462. return scale;
  463. }
  464. /* rect is guaranteed to not exceed the scaled camera rectangle */
  465. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
  466. unsigned int out_width,
  467. unsigned int out_height)
  468. {
  469. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  470. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  471. struct v4l2_rect *rect = &cam->ceu_rect;
  472. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  473. unsigned int height, width, cdwdr_width, in_width, in_height;
  474. unsigned int left_offset, top_offset;
  475. u32 camor;
  476. dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
  477. rect->width, rect->height, rect->left, rect->top);
  478. left_offset = rect->left;
  479. top_offset = rect->top;
  480. if (pcdev->image_mode) {
  481. in_width = rect->width;
  482. if (!pcdev->is_16bit) {
  483. in_width *= 2;
  484. left_offset *= 2;
  485. }
  486. width = out_width;
  487. cdwdr_width = out_width;
  488. } else {
  489. int bytes_per_line = soc_mbus_bytes_per_line(out_width,
  490. icd->current_fmt->host_fmt);
  491. unsigned int w_factor;
  492. width = out_width;
  493. switch (icd->current_fmt->host_fmt->packing) {
  494. case SOC_MBUS_PACKING_2X8_PADHI:
  495. w_factor = 2;
  496. break;
  497. default:
  498. w_factor = 1;
  499. }
  500. in_width = rect->width * w_factor;
  501. left_offset = left_offset * w_factor;
  502. if (bytes_per_line < 0)
  503. cdwdr_width = out_width;
  504. else
  505. cdwdr_width = bytes_per_line;
  506. }
  507. height = out_height;
  508. in_height = rect->height;
  509. if (V4L2_FIELD_NONE != pcdev->field) {
  510. height /= 2;
  511. in_height /= 2;
  512. top_offset /= 2;
  513. cdwdr_width *= 2;
  514. }
  515. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  516. camor = left_offset | (top_offset << 16);
  517. dev_geo(icd->dev.parent,
  518. "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
  519. (in_height << 16) | in_width, (height << 16) | width,
  520. cdwdr_width);
  521. ceu_write(pcdev, CAMOR, camor);
  522. ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
  523. ceu_write(pcdev, CFSZR, (height << 16) | width);
  524. ceu_write(pcdev, CDWDR, cdwdr_width);
  525. }
  526. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  527. {
  528. u32 capsr = ceu_read(pcdev, CAPSR);
  529. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  530. return capsr;
  531. }
  532. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  533. {
  534. unsigned long timeout = jiffies + 10 * HZ;
  535. /*
  536. * Wait until the end of the current frame. It can take a long time,
  537. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  538. */
  539. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  540. msleep(1);
  541. if (time_after(jiffies, timeout)) {
  542. dev_err(pcdev->ici.v4l2_dev.dev,
  543. "Timeout waiting for frame end! Interface problem?\n");
  544. return;
  545. }
  546. /* Wait until reset clears, this shall not hang... */
  547. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  548. udelay(10);
  549. /* Anything to restore? */
  550. if (capsr & ~(1 << 16))
  551. ceu_write(pcdev, CAPSR, capsr);
  552. }
  553. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  554. __u32 pixfmt)
  555. {
  556. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  557. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  558. int ret;
  559. unsigned long camera_flags, common_flags, value;
  560. int yuv_lineskip;
  561. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  562. u32 capsr = capture_save_reset(pcdev);
  563. camera_flags = icd->ops->query_bus_param(icd);
  564. common_flags = soc_camera_bus_param_compatible(camera_flags,
  565. make_bus_param(pcdev));
  566. if (!common_flags)
  567. return -EINVAL;
  568. /* Make choises, based on platform preferences */
  569. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  570. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  571. if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
  572. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  573. else
  574. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  575. }
  576. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  577. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  578. if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
  579. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  580. else
  581. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  582. }
  583. ret = icd->ops->set_bus_param(icd, common_flags);
  584. if (ret < 0)
  585. return ret;
  586. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  587. case SOCAM_DATAWIDTH_8:
  588. pcdev->is_16bit = 0;
  589. break;
  590. case SOCAM_DATAWIDTH_16:
  591. pcdev->is_16bit = 1;
  592. break;
  593. default:
  594. return -EINVAL;
  595. }
  596. ceu_write(pcdev, CRCNTR, 0);
  597. ceu_write(pcdev, CRCMPR, 0);
  598. value = 0x00000010; /* data fetch by default */
  599. yuv_lineskip = 0;
  600. switch (icd->current_fmt->host_fmt->fourcc) {
  601. case V4L2_PIX_FMT_NV12:
  602. case V4L2_PIX_FMT_NV21:
  603. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  604. /* fall-through */
  605. case V4L2_PIX_FMT_NV16:
  606. case V4L2_PIX_FMT_NV61:
  607. switch (cam->code) {
  608. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  609. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  610. break;
  611. case V4L2_MBUS_FMT_YVYU8_2X8_BE:
  612. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  613. break;
  614. case V4L2_MBUS_FMT_YUYV8_2X8_LE:
  615. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  616. break;
  617. case V4L2_MBUS_FMT_YVYU8_2X8_LE:
  618. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  619. break;
  620. default:
  621. BUG();
  622. }
  623. }
  624. if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  625. icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
  626. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  627. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  628. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  629. value |= pcdev->is_16bit ? 1 << 12 : 0;
  630. ceu_write(pcdev, CAMCR, value);
  631. ceu_write(pcdev, CAPCR, 0x00300000);
  632. switch (pcdev->field) {
  633. case V4L2_FIELD_INTERLACED_TB:
  634. value = 0x101;
  635. break;
  636. case V4L2_FIELD_INTERLACED_BT:
  637. value = 0x102;
  638. break;
  639. default:
  640. value = 0;
  641. break;
  642. }
  643. ceu_write(pcdev, CAIFR, value);
  644. sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
  645. mdelay(1);
  646. ceu_write(pcdev, CFLCR, pcdev->cflcr);
  647. /*
  648. * A few words about byte order (observed in Big Endian mode)
  649. *
  650. * In data fetch mode bytes are received in chunks of 8 bytes.
  651. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  652. *
  653. * The data is however by default written to memory in reverse order:
  654. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  655. *
  656. * The lowest three bits of CDOCR allows us to do swapping,
  657. * using 7 we swap the data bytes to match the incoming order:
  658. * D0, D1, D2, D3, D4, D5, D6, D7
  659. */
  660. value = 0x00000017;
  661. if (yuv_lineskip)
  662. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  663. ceu_write(pcdev, CDOCR, value);
  664. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  665. dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
  666. pixfmt & 0xff, (pixfmt >> 8) & 0xff,
  667. (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
  668. icd->user_width, icd->user_height);
  669. capture_restore(pcdev, capsr);
  670. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  671. return 0;
  672. }
  673. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
  674. unsigned char buswidth)
  675. {
  676. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  677. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  678. unsigned long camera_flags, common_flags;
  679. camera_flags = icd->ops->query_bus_param(icd);
  680. common_flags = soc_camera_bus_param_compatible(camera_flags,
  681. make_bus_param(pcdev));
  682. if (!common_flags || buswidth > 16 ||
  683. (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
  684. return -EINVAL;
  685. return 0;
  686. }
  687. static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
  688. {
  689. .fourcc = V4L2_PIX_FMT_NV12,
  690. .name = "NV12",
  691. .bits_per_sample = 12,
  692. .packing = SOC_MBUS_PACKING_NONE,
  693. .order = SOC_MBUS_ORDER_LE,
  694. }, {
  695. .fourcc = V4L2_PIX_FMT_NV21,
  696. .name = "NV21",
  697. .bits_per_sample = 12,
  698. .packing = SOC_MBUS_PACKING_NONE,
  699. .order = SOC_MBUS_ORDER_LE,
  700. }, {
  701. .fourcc = V4L2_PIX_FMT_NV16,
  702. .name = "NV16",
  703. .bits_per_sample = 16,
  704. .packing = SOC_MBUS_PACKING_NONE,
  705. .order = SOC_MBUS_ORDER_LE,
  706. }, {
  707. .fourcc = V4L2_PIX_FMT_NV61,
  708. .name = "NV61",
  709. .bits_per_sample = 16,
  710. .packing = SOC_MBUS_PACKING_NONE,
  711. .order = SOC_MBUS_ORDER_LE,
  712. },
  713. };
  714. /* This will be corrected as we get more formats */
  715. static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  716. {
  717. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  718. (fmt->bits_per_sample == 8 &&
  719. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  720. (fmt->bits_per_sample > 8 &&
  721. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  722. }
  723. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  724. struct soc_camera_format_xlate *xlate)
  725. {
  726. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  727. struct device *dev = icd->dev.parent;
  728. int ret, k, n;
  729. int formats = 0;
  730. struct sh_mobile_ceu_cam *cam;
  731. enum v4l2_mbus_pixelcode code;
  732. const struct soc_mbus_pixelfmt *fmt;
  733. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  734. if (ret < 0)
  735. /* No more formats */
  736. return 0;
  737. fmt = soc_mbus_get_fmtdesc(code);
  738. if (!fmt) {
  739. dev_err(icd->dev.parent,
  740. "Invalid format code #%d: %d\n", idx, code);
  741. return -EINVAL;
  742. }
  743. ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
  744. if (ret < 0)
  745. return 0;
  746. if (!icd->host_priv) {
  747. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  748. if (!cam)
  749. return -ENOMEM;
  750. icd->host_priv = cam;
  751. } else {
  752. cam = icd->host_priv;
  753. }
  754. /* Beginning of a pass */
  755. if (!idx)
  756. cam->extra_fmt = NULL;
  757. switch (code) {
  758. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  759. case V4L2_MBUS_FMT_YVYU8_2X8_BE:
  760. case V4L2_MBUS_FMT_YUYV8_2X8_LE:
  761. case V4L2_MBUS_FMT_YVYU8_2X8_LE:
  762. if (cam->extra_fmt)
  763. break;
  764. /*
  765. * Our case is simple so far: for any of the above four camera
  766. * formats we add all our four synthesized NV* formats, so,
  767. * just marking the device with a single flag suffices. If
  768. * the format generation rules are more complex, you would have
  769. * to actually hang your already added / counted formats onto
  770. * the host_priv pointer and check whether the format you're
  771. * going to add now is already there.
  772. */
  773. cam->extra_fmt = sh_mobile_ceu_formats;
  774. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  775. formats += n;
  776. for (k = 0; xlate && k < n; k++) {
  777. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  778. xlate->code = code;
  779. xlate++;
  780. dev_dbg(dev, "Providing format %s using code %d\n",
  781. sh_mobile_ceu_formats[k].name, code);
  782. }
  783. break;
  784. default:
  785. if (!sh_mobile_ceu_packing_supported(fmt))
  786. return 0;
  787. }
  788. /* Generic pass-through */
  789. formats++;
  790. if (xlate) {
  791. xlate->host_fmt = fmt;
  792. xlate->code = code;
  793. xlate++;
  794. dev_dbg(dev, "Providing format %s in pass-through mode\n",
  795. xlate->host_fmt->name);
  796. }
  797. return formats;
  798. }
  799. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  800. {
  801. kfree(icd->host_priv);
  802. icd->host_priv = NULL;
  803. }
  804. /* Check if any dimension of r1 is smaller than respective one of r2 */
  805. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  806. {
  807. return r1->width < r2->width || r1->height < r2->height;
  808. }
  809. /* Check if r1 fails to cover r2 */
  810. static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
  811. {
  812. return r1->left > r2->left || r1->top > r2->top ||
  813. r1->left + r1->width < r2->left + r2->width ||
  814. r1->top + r1->height < r2->top + r2->height;
  815. }
  816. static unsigned int scale_down(unsigned int size, unsigned int scale)
  817. {
  818. return (size * 4096 + scale / 2) / scale;
  819. }
  820. static unsigned int scale_up(unsigned int size, unsigned int scale)
  821. {
  822. return (size * scale + 2048) / 4096;
  823. }
  824. static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
  825. {
  826. return (input * 4096 + output / 2) / output;
  827. }
  828. static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
  829. {
  830. struct v4l2_crop crop;
  831. struct v4l2_cropcap cap;
  832. int ret;
  833. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  834. ret = v4l2_subdev_call(sd, video, g_crop, &crop);
  835. if (!ret) {
  836. *rect = crop.c;
  837. return ret;
  838. }
  839. /* Camera driver doesn't support .g_crop(), assume default rectangle */
  840. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  841. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  842. if (ret < 0)
  843. return ret;
  844. *rect = cap.defrect;
  845. return ret;
  846. }
  847. /*
  848. * The common for both scaling and cropping iterative approach is:
  849. * 1. try if the client can produce exactly what requested by the user
  850. * 2. if (1) failed, try to double the client image until we get one big enough
  851. * 3. if (2) failed, try to request the maximum image
  852. */
  853. static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
  854. struct v4l2_crop *cam_crop)
  855. {
  856. struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
  857. struct device *dev = sd->v4l2_dev->dev;
  858. struct v4l2_cropcap cap;
  859. int ret;
  860. unsigned int width, height;
  861. v4l2_subdev_call(sd, video, s_crop, crop);
  862. ret = client_g_rect(sd, cam_rect);
  863. if (ret < 0)
  864. return ret;
  865. /*
  866. * Now cam_crop contains the current camera input rectangle, and it must
  867. * be within camera cropcap bounds
  868. */
  869. if (!memcmp(rect, cam_rect, sizeof(*rect))) {
  870. /* Even if camera S_CROP failed, but camera rectangle matches */
  871. dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
  872. rect->width, rect->height, rect->left, rect->top);
  873. return 0;
  874. }
  875. /* Try to fix cropping, that camera hasn't managed to set */
  876. dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
  877. cam_rect->width, cam_rect->height,
  878. cam_rect->left, cam_rect->top,
  879. rect->width, rect->height, rect->left, rect->top);
  880. /* We need sensor maximum rectangle */
  881. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  882. if (ret < 0)
  883. return ret;
  884. soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
  885. cap.bounds.width);
  886. soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
  887. cap.bounds.height);
  888. /*
  889. * Popular special case - some cameras can only handle fixed sizes like
  890. * QVGA, VGA,... Take care to avoid infinite loop.
  891. */
  892. width = max(cam_rect->width, 2);
  893. height = max(cam_rect->height, 2);
  894. while (!ret && (is_smaller(cam_rect, rect) ||
  895. is_inside(cam_rect, rect)) &&
  896. (cap.bounds.width > width || cap.bounds.height > height)) {
  897. width *= 2;
  898. height *= 2;
  899. cam_rect->width = width;
  900. cam_rect->height = height;
  901. /*
  902. * We do not know what capabilities the camera has to set up
  903. * left and top borders. We could try to be smarter in iterating
  904. * them, e.g., if camera current left is to the right of the
  905. * target left, set it to the middle point between the current
  906. * left and minimum left. But that would add too much
  907. * complexity: we would have to iterate each border separately.
  908. */
  909. if (cam_rect->left > rect->left)
  910. cam_rect->left = cap.bounds.left;
  911. if (cam_rect->left + cam_rect->width < rect->left + rect->width)
  912. cam_rect->width = rect->left + rect->width -
  913. cam_rect->left;
  914. if (cam_rect->top > rect->top)
  915. cam_rect->top = cap.bounds.top;
  916. if (cam_rect->top + cam_rect->height < rect->top + rect->height)
  917. cam_rect->height = rect->top + rect->height -
  918. cam_rect->top;
  919. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  920. ret = client_g_rect(sd, cam_rect);
  921. dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
  922. cam_rect->width, cam_rect->height,
  923. cam_rect->left, cam_rect->top);
  924. }
  925. /* S_CROP must not modify the rectangle */
  926. if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
  927. /*
  928. * The camera failed to configure a suitable cropping,
  929. * we cannot use the current rectangle, set to max
  930. */
  931. *cam_rect = cap.bounds;
  932. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  933. ret = client_g_rect(sd, cam_rect);
  934. dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
  935. cam_rect->width, cam_rect->height,
  936. cam_rect->left, cam_rect->top);
  937. }
  938. return ret;
  939. }
  940. static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
  941. unsigned int *scale_h, unsigned int *scale_v)
  942. {
  943. struct v4l2_mbus_framefmt mf;
  944. int ret;
  945. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  946. if (ret < 0)
  947. return ret;
  948. *scale_h = calc_generic_scale(rect->width, mf.width);
  949. *scale_v = calc_generic_scale(rect->height, mf.height);
  950. return 0;
  951. }
  952. static int get_camera_subwin(struct soc_camera_device *icd,
  953. struct v4l2_rect *cam_subrect,
  954. unsigned int cam_hscale, unsigned int cam_vscale)
  955. {
  956. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  957. struct v4l2_rect *ceu_rect = &cam->ceu_rect;
  958. if (!ceu_rect->width) {
  959. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  960. struct device *dev = icd->dev.parent;
  961. struct v4l2_mbus_framefmt mf;
  962. int ret;
  963. /* First time */
  964. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  965. if (ret < 0)
  966. return ret;
  967. dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
  968. if (mf.width > 2560) {
  969. ceu_rect->width = 2560;
  970. ceu_rect->left = (mf.width - 2560) / 2;
  971. } else {
  972. ceu_rect->width = mf.width;
  973. ceu_rect->left = 0;
  974. }
  975. if (mf.height > 1920) {
  976. ceu_rect->height = 1920;
  977. ceu_rect->top = (mf.height - 1920) / 2;
  978. } else {
  979. ceu_rect->height = mf.height;
  980. ceu_rect->top = 0;
  981. }
  982. dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
  983. ceu_rect->width, ceu_rect->height,
  984. ceu_rect->left, ceu_rect->top);
  985. }
  986. cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
  987. cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
  988. cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
  989. cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
  990. return 0;
  991. }
  992. static int client_s_fmt(struct soc_camera_device *icd,
  993. struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
  994. {
  995. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  996. struct device *dev = icd->dev.parent;
  997. unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
  998. unsigned int max_width, max_height;
  999. struct v4l2_cropcap cap;
  1000. int ret;
  1001. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1002. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  1003. if (ret < 0)
  1004. return ret;
  1005. max_width = min(cap.bounds.width, 2560);
  1006. max_height = min(cap.bounds.height, 1920);
  1007. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
  1008. if (ret < 0)
  1009. return ret;
  1010. dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
  1011. if ((width == mf->width && height == mf->height) || !ceu_can_scale)
  1012. return 0;
  1013. /* Camera set a format, but geometry is not precise, try to improve */
  1014. tmp_w = mf->width;
  1015. tmp_h = mf->height;
  1016. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  1017. while ((width > tmp_w || height > tmp_h) &&
  1018. tmp_w < max_width && tmp_h < max_height) {
  1019. tmp_w = min(2 * tmp_w, max_width);
  1020. tmp_h = min(2 * tmp_h, max_height);
  1021. mf->width = tmp_w;
  1022. mf->height = tmp_h;
  1023. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
  1024. dev_geo(dev, "Camera scaled to %ux%u\n",
  1025. mf->width, mf->height);
  1026. if (ret < 0) {
  1027. /* This shouldn't happen */
  1028. dev_err(dev, "Client failed to set format: %d\n", ret);
  1029. return ret;
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. /**
  1035. * @rect - camera cropped rectangle
  1036. * @sub_rect - CEU cropped rectangle, mapped back to camera input area
  1037. * @ceu_rect - on output calculated CEU crop rectangle
  1038. */
  1039. static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
  1040. struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
  1041. struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
  1042. {
  1043. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1044. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1045. struct device *dev = icd->dev.parent;
  1046. struct v4l2_mbus_framefmt mf_tmp = *mf;
  1047. unsigned int scale_h, scale_v;
  1048. int ret;
  1049. /* 5. Apply iterative camera S_FMT for camera user window. */
  1050. ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
  1051. if (ret < 0)
  1052. return ret;
  1053. dev_geo(dev, "5: camera scaled to %ux%u\n",
  1054. mf_tmp.width, mf_tmp.height);
  1055. /* 6. Retrieve camera output window (g_fmt) */
  1056. /* unneeded - it is already in "mf_tmp" */
  1057. /* 7. Calculate new camera scales. */
  1058. ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
  1059. if (ret < 0)
  1060. return ret;
  1061. dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
  1062. cam->cam_width = mf_tmp.width;
  1063. cam->cam_height = mf_tmp.height;
  1064. mf->width = mf_tmp.width;
  1065. mf->height = mf_tmp.height;
  1066. mf->colorspace = mf_tmp.colorspace;
  1067. /*
  1068. * 8. Calculate new CEU crop - apply camera scales to previously
  1069. * calculated "effective" crop.
  1070. */
  1071. ceu_rect->left = scale_down(sub_rect->left, scale_h);
  1072. ceu_rect->width = scale_down(sub_rect->width, scale_h);
  1073. ceu_rect->top = scale_down(sub_rect->top, scale_v);
  1074. ceu_rect->height = scale_down(sub_rect->height, scale_v);
  1075. dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
  1076. ceu_rect->width, ceu_rect->height,
  1077. ceu_rect->left, ceu_rect->top);
  1078. return 0;
  1079. }
  1080. /* Get combined scales */
  1081. static int get_scales(struct soc_camera_device *icd,
  1082. unsigned int *scale_h, unsigned int *scale_v)
  1083. {
  1084. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1085. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1086. struct v4l2_crop cam_crop;
  1087. unsigned int width_in, height_in;
  1088. int ret;
  1089. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1090. ret = client_g_rect(sd, &cam_crop.c);
  1091. if (ret < 0)
  1092. return ret;
  1093. ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
  1094. if (ret < 0)
  1095. return ret;
  1096. width_in = scale_up(cam->ceu_rect.width, *scale_h);
  1097. height_in = scale_up(cam->ceu_rect.height, *scale_v);
  1098. *scale_h = calc_generic_scale(width_in, icd->user_width);
  1099. *scale_v = calc_generic_scale(height_in, icd->user_height);
  1100. return 0;
  1101. }
  1102. /*
  1103. * CEU can scale and crop, but we don't want to waste bandwidth and kill the
  1104. * framerate by always requesting the maximum image from the client. See
  1105. * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
  1106. * scaling and cropping algorithms and for the meaning of referenced here steps.
  1107. */
  1108. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  1109. struct v4l2_crop *a)
  1110. {
  1111. struct v4l2_rect *rect = &a->c;
  1112. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1113. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1114. struct v4l2_crop cam_crop;
  1115. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1116. struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
  1117. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1118. struct device *dev = icd->dev.parent;
  1119. struct v4l2_mbus_framefmt mf;
  1120. unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
  1121. out_width, out_height;
  1122. u32 capsr, cflcr;
  1123. int ret;
  1124. /* 1. Calculate current combined scales. */
  1125. ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
  1126. if (ret < 0)
  1127. return ret;
  1128. dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
  1129. /* 2. Apply iterative camera S_CROP for new input window. */
  1130. ret = client_s_crop(sd, a, &cam_crop);
  1131. if (ret < 0)
  1132. return ret;
  1133. dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
  1134. cam_rect->width, cam_rect->height,
  1135. cam_rect->left, cam_rect->top);
  1136. /* On success cam_crop contains current camera crop */
  1137. /*
  1138. * 3. If old combined scales applied to new crop produce an impossible
  1139. * user window, adjust scales to produce nearest possible window.
  1140. */
  1141. out_width = scale_down(rect->width, scale_comb_h);
  1142. out_height = scale_down(rect->height, scale_comb_v);
  1143. if (out_width > 2560)
  1144. out_width = 2560;
  1145. else if (out_width < 2)
  1146. out_width = 2;
  1147. if (out_height > 1920)
  1148. out_height = 1920;
  1149. else if (out_height < 4)
  1150. out_height = 4;
  1151. dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
  1152. /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
  1153. /*
  1154. * 5. Using actual input window and calculated combined scales calculate
  1155. * camera target output window.
  1156. */
  1157. mf.width = scale_down(cam_rect->width, scale_comb_h);
  1158. mf.height = scale_down(cam_rect->height, scale_comb_v);
  1159. dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height);
  1160. /* 6. - 9. */
  1161. mf.code = cam->code;
  1162. mf.field = pcdev->field;
  1163. capsr = capture_save_reset(pcdev);
  1164. dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
  1165. /* Make relative to camera rectangle */
  1166. rect->left -= cam_rect->left;
  1167. rect->top -= cam_rect->top;
  1168. ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf,
  1169. pcdev->image_mode &&
  1170. V4L2_FIELD_NONE == pcdev->field);
  1171. dev_geo(dev, "6-9: %d\n", ret);
  1172. /* 10. Use CEU cropping to crop to the new window. */
  1173. sh_mobile_ceu_set_rect(icd, out_width, out_height);
  1174. dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
  1175. ceu_rect->width, ceu_rect->height,
  1176. ceu_rect->left, ceu_rect->top);
  1177. /*
  1178. * 11. Calculate CEU scales from camera scales from results of (10) and
  1179. * user window from (3)
  1180. */
  1181. scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
  1182. scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
  1183. dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
  1184. /* 12. Apply CEU scales. */
  1185. cflcr = scale_ceu_h | (scale_ceu_v << 16);
  1186. if (cflcr != pcdev->cflcr) {
  1187. pcdev->cflcr = cflcr;
  1188. ceu_write(pcdev, CFLCR, cflcr);
  1189. }
  1190. /* Restore capture */
  1191. if (pcdev->active)
  1192. capsr |= 1;
  1193. capture_restore(pcdev, capsr);
  1194. icd->user_width = out_width;
  1195. icd->user_height = out_height;
  1196. /* Even if only camera cropping succeeded */
  1197. return ret;
  1198. }
  1199. /* Similar to set_crop multistage iterative algorithm */
  1200. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  1201. struct v4l2_format *f)
  1202. {
  1203. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1204. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1205. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1206. struct v4l2_pix_format *pix = &f->fmt.pix;
  1207. struct v4l2_mbus_framefmt mf;
  1208. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1209. struct device *dev = icd->dev.parent;
  1210. __u32 pixfmt = pix->pixelformat;
  1211. const struct soc_camera_format_xlate *xlate;
  1212. struct v4l2_crop cam_crop;
  1213. struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
  1214. unsigned int scale_cam_h, scale_cam_v;
  1215. u16 scale_v, scale_h;
  1216. int ret;
  1217. bool image_mode;
  1218. enum v4l2_field field;
  1219. switch (pix->field) {
  1220. default:
  1221. pix->field = V4L2_FIELD_NONE;
  1222. /* fall-through */
  1223. case V4L2_FIELD_INTERLACED_TB:
  1224. case V4L2_FIELD_INTERLACED_BT:
  1225. case V4L2_FIELD_NONE:
  1226. field = pix->field;
  1227. break;
  1228. case V4L2_FIELD_INTERLACED:
  1229. field = V4L2_FIELD_INTERLACED_TB;
  1230. break;
  1231. }
  1232. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1233. if (!xlate) {
  1234. dev_warn(dev, "Format %x not found\n", pixfmt);
  1235. return -EINVAL;
  1236. }
  1237. /* 1. Calculate current camera scales. */
  1238. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1239. ret = client_g_rect(sd, cam_rect);
  1240. if (ret < 0)
  1241. return ret;
  1242. ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
  1243. if (ret < 0)
  1244. return ret;
  1245. dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
  1246. /*
  1247. * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
  1248. * scaled back at current camera scales onto input window.
  1249. */
  1250. ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
  1251. if (ret < 0)
  1252. return ret;
  1253. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  1254. cam_subrect.width, cam_subrect.height,
  1255. cam_subrect.left, cam_subrect.top);
  1256. /*
  1257. * 3. Calculate new combined scales from "effective" input window to
  1258. * requested user window.
  1259. */
  1260. scale_h = calc_generic_scale(cam_subrect.width, pix->width);
  1261. scale_v = calc_generic_scale(cam_subrect.height, pix->height);
  1262. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  1263. /*
  1264. * 4. Calculate camera output window by applying combined scales to real
  1265. * input window.
  1266. */
  1267. mf.width = scale_down(cam_rect->width, scale_h);
  1268. mf.height = scale_down(cam_rect->height, scale_v);
  1269. mf.field = pix->field;
  1270. mf.colorspace = pix->colorspace;
  1271. mf.code = xlate->code;
  1272. switch (pixfmt) {
  1273. case V4L2_PIX_FMT_NV12:
  1274. case V4L2_PIX_FMT_NV21:
  1275. case V4L2_PIX_FMT_NV16:
  1276. case V4L2_PIX_FMT_NV61:
  1277. image_mode = true;
  1278. break;
  1279. default:
  1280. image_mode = false;
  1281. }
  1282. dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height);
  1283. /* 5. - 9. */
  1284. ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf,
  1285. image_mode && V4L2_FIELD_NONE == field);
  1286. dev_geo(dev, "5-9: client scale %d\n", ret);
  1287. /* Done with the camera. Now see if we can improve the result */
  1288. dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
  1289. ret, mf.width, mf.height, pix->width, pix->height);
  1290. if (ret < 0)
  1291. return ret;
  1292. if (mf.code != xlate->code)
  1293. return -EINVAL;
  1294. /* 10. Use CEU scaling to scale to the requested user window. */
  1295. /* We cannot scale up */
  1296. if (pix->width > mf.width)
  1297. pix->width = mf.width;
  1298. if (pix->width > ceu_rect.width)
  1299. pix->width = ceu_rect.width;
  1300. if (pix->height > mf.height)
  1301. pix->height = mf.height;
  1302. if (pix->height > ceu_rect.height)
  1303. pix->height = ceu_rect.height;
  1304. pix->colorspace = mf.colorspace;
  1305. if (image_mode) {
  1306. /* Scale pix->{width x height} down to width x height */
  1307. scale_h = calc_scale(ceu_rect.width, &pix->width);
  1308. scale_v = calc_scale(ceu_rect.height, &pix->height);
  1309. pcdev->cflcr = scale_h | (scale_v << 16);
  1310. } else {
  1311. pix->width = ceu_rect.width;
  1312. pix->height = ceu_rect.height;
  1313. scale_h = scale_v = 0;
  1314. pcdev->cflcr = 0;
  1315. }
  1316. dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
  1317. ceu_rect.width, scale_h, pix->width,
  1318. ceu_rect.height, scale_v, pix->height);
  1319. cam->code = xlate->code;
  1320. cam->ceu_rect = ceu_rect;
  1321. icd->current_fmt = xlate;
  1322. pcdev->field = field;
  1323. pcdev->image_mode = image_mode;
  1324. return 0;
  1325. }
  1326. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  1327. struct v4l2_format *f)
  1328. {
  1329. const struct soc_camera_format_xlate *xlate;
  1330. struct v4l2_pix_format *pix = &f->fmt.pix;
  1331. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1332. struct v4l2_mbus_framefmt mf;
  1333. __u32 pixfmt = pix->pixelformat;
  1334. int width, height;
  1335. int ret;
  1336. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1337. if (!xlate) {
  1338. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  1339. return -EINVAL;
  1340. }
  1341. /* FIXME: calculate using depth and bus width */
  1342. v4l_bound_align_image(&pix->width, 2, 2560, 1,
  1343. &pix->height, 4, 1920, 2, 0);
  1344. width = pix->width;
  1345. height = pix->height;
  1346. pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
  1347. if (pix->bytesperline < 0)
  1348. return pix->bytesperline;
  1349. pix->sizeimage = height * pix->bytesperline;
  1350. /* limit to sensor capabilities */
  1351. mf.width = pix->width;
  1352. mf.height = pix->height;
  1353. mf.field = pix->field;
  1354. mf.code = xlate->code;
  1355. mf.colorspace = pix->colorspace;
  1356. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1357. if (ret < 0)
  1358. return ret;
  1359. pix->width = mf.width;
  1360. pix->height = mf.height;
  1361. pix->field = mf.field;
  1362. pix->colorspace = mf.colorspace;
  1363. switch (pixfmt) {
  1364. case V4L2_PIX_FMT_NV12:
  1365. case V4L2_PIX_FMT_NV21:
  1366. case V4L2_PIX_FMT_NV16:
  1367. case V4L2_PIX_FMT_NV61:
  1368. /* FIXME: check against rect_max after converting soc-camera */
  1369. /* We can scale precisely, need a bigger image from camera */
  1370. if (pix->width < width || pix->height < height) {
  1371. /*
  1372. * We presume, the sensor behaves sanely, i.e., if
  1373. * requested a bigger rectangle, it will not return a
  1374. * smaller one.
  1375. */
  1376. mf.width = 2560;
  1377. mf.height = 1920;
  1378. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1379. if (ret < 0) {
  1380. /* Shouldn't actually happen... */
  1381. dev_err(icd->dev.parent,
  1382. "FIXME: client try_fmt() = %d\n", ret);
  1383. return ret;
  1384. }
  1385. }
  1386. /* We will scale exactly */
  1387. if (mf.width > width)
  1388. pix->width = width;
  1389. if (mf.height > height)
  1390. pix->height = height;
  1391. }
  1392. return ret;
  1393. }
  1394. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  1395. struct v4l2_requestbuffers *p)
  1396. {
  1397. int i;
  1398. /*
  1399. * This is for locking debugging only. I removed spinlocks and now I
  1400. * check whether .prepare is ever called on a linked buffer, or whether
  1401. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1402. * it hadn't triggered
  1403. */
  1404. for (i = 0; i < p->count; i++) {
  1405. struct sh_mobile_ceu_buffer *buf;
  1406. buf = container_of(icf->vb_vidq.bufs[i],
  1407. struct sh_mobile_ceu_buffer, vb);
  1408. INIT_LIST_HEAD(&buf->vb.queue);
  1409. }
  1410. return 0;
  1411. }
  1412. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  1413. {
  1414. struct soc_camera_file *icf = file->private_data;
  1415. struct sh_mobile_ceu_buffer *buf;
  1416. buf = list_entry(icf->vb_vidq.stream.next,
  1417. struct sh_mobile_ceu_buffer, vb.stream);
  1418. poll_wait(file, &buf->vb.done, pt);
  1419. if (buf->vb.state == VIDEOBUF_DONE ||
  1420. buf->vb.state == VIDEOBUF_ERROR)
  1421. return POLLIN|POLLRDNORM;
  1422. return 0;
  1423. }
  1424. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  1425. struct v4l2_capability *cap)
  1426. {
  1427. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  1428. cap->version = KERNEL_VERSION(0, 0, 5);
  1429. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1430. return 0;
  1431. }
  1432. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  1433. struct soc_camera_device *icd)
  1434. {
  1435. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1436. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1437. videobuf_queue_dma_contig_init(q,
  1438. &sh_mobile_ceu_videobuf_ops,
  1439. icd->dev.parent, &pcdev->lock,
  1440. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1441. pcdev->field,
  1442. sizeof(struct sh_mobile_ceu_buffer),
  1443. icd);
  1444. }
  1445. static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
  1446. struct v4l2_control *ctrl)
  1447. {
  1448. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1449. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1450. u32 val;
  1451. switch (ctrl->id) {
  1452. case V4L2_CID_SHARPNESS:
  1453. val = ceu_read(pcdev, CLFCR);
  1454. ctrl->value = val ^ 1;
  1455. return 0;
  1456. }
  1457. return -ENOIOCTLCMD;
  1458. }
  1459. static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
  1460. struct v4l2_control *ctrl)
  1461. {
  1462. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1463. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1464. switch (ctrl->id) {
  1465. case V4L2_CID_SHARPNESS:
  1466. switch (icd->current_fmt->host_fmt->fourcc) {
  1467. case V4L2_PIX_FMT_NV12:
  1468. case V4L2_PIX_FMT_NV21:
  1469. case V4L2_PIX_FMT_NV16:
  1470. case V4L2_PIX_FMT_NV61:
  1471. ceu_write(pcdev, CLFCR, !ctrl->value);
  1472. return 0;
  1473. }
  1474. return -EINVAL;
  1475. }
  1476. return -ENOIOCTLCMD;
  1477. }
  1478. static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
  1479. {
  1480. .id = V4L2_CID_SHARPNESS,
  1481. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1482. .name = "Low-pass filter",
  1483. .minimum = 0,
  1484. .maximum = 1,
  1485. .step = 1,
  1486. .default_value = 0,
  1487. },
  1488. };
  1489. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  1490. .owner = THIS_MODULE,
  1491. .add = sh_mobile_ceu_add_device,
  1492. .remove = sh_mobile_ceu_remove_device,
  1493. .get_formats = sh_mobile_ceu_get_formats,
  1494. .put_formats = sh_mobile_ceu_put_formats,
  1495. .set_crop = sh_mobile_ceu_set_crop,
  1496. .set_fmt = sh_mobile_ceu_set_fmt,
  1497. .try_fmt = sh_mobile_ceu_try_fmt,
  1498. .set_ctrl = sh_mobile_ceu_set_ctrl,
  1499. .get_ctrl = sh_mobile_ceu_get_ctrl,
  1500. .reqbufs = sh_mobile_ceu_reqbufs,
  1501. .poll = sh_mobile_ceu_poll,
  1502. .querycap = sh_mobile_ceu_querycap,
  1503. .set_bus_param = sh_mobile_ceu_set_bus_param,
  1504. .init_videobuf = sh_mobile_ceu_init_videobuf,
  1505. .controls = sh_mobile_ceu_controls,
  1506. .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
  1507. };
  1508. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  1509. {
  1510. struct sh_mobile_ceu_dev *pcdev;
  1511. struct resource *res;
  1512. void __iomem *base;
  1513. unsigned int irq;
  1514. int err = 0;
  1515. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1516. irq = platform_get_irq(pdev, 0);
  1517. if (!res || !irq) {
  1518. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  1519. err = -ENODEV;
  1520. goto exit;
  1521. }
  1522. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1523. if (!pcdev) {
  1524. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1525. err = -ENOMEM;
  1526. goto exit;
  1527. }
  1528. INIT_LIST_HEAD(&pcdev->capture);
  1529. spin_lock_init(&pcdev->lock);
  1530. pcdev->pdata = pdev->dev.platform_data;
  1531. if (!pcdev->pdata) {
  1532. err = -EINVAL;
  1533. dev_err(&pdev->dev, "CEU platform data not set.\n");
  1534. goto exit_kfree;
  1535. }
  1536. base = ioremap_nocache(res->start, resource_size(res));
  1537. if (!base) {
  1538. err = -ENXIO;
  1539. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  1540. goto exit_kfree;
  1541. }
  1542. pcdev->irq = irq;
  1543. pcdev->base = base;
  1544. pcdev->video_limit = 0; /* only enabled if second resource exists */
  1545. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1546. if (res) {
  1547. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  1548. res->start,
  1549. resource_size(res),
  1550. DMA_MEMORY_MAP |
  1551. DMA_MEMORY_EXCLUSIVE);
  1552. if (!err) {
  1553. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  1554. err = -ENXIO;
  1555. goto exit_iounmap;
  1556. }
  1557. pcdev->video_limit = resource_size(res);
  1558. }
  1559. /* request irq */
  1560. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  1561. dev_name(&pdev->dev), pcdev);
  1562. if (err) {
  1563. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  1564. goto exit_release_mem;
  1565. }
  1566. pm_suspend_ignore_children(&pdev->dev, true);
  1567. pm_runtime_enable(&pdev->dev);
  1568. pm_runtime_resume(&pdev->dev);
  1569. pcdev->ici.priv = pcdev;
  1570. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  1571. pcdev->ici.nr = pdev->id;
  1572. pcdev->ici.drv_name = dev_name(&pdev->dev);
  1573. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  1574. err = soc_camera_host_register(&pcdev->ici);
  1575. if (err)
  1576. goto exit_free_clk;
  1577. return 0;
  1578. exit_free_clk:
  1579. pm_runtime_disable(&pdev->dev);
  1580. free_irq(pcdev->irq, pcdev);
  1581. exit_release_mem:
  1582. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1583. dma_release_declared_memory(&pdev->dev);
  1584. exit_iounmap:
  1585. iounmap(base);
  1586. exit_kfree:
  1587. kfree(pcdev);
  1588. exit:
  1589. return err;
  1590. }
  1591. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  1592. {
  1593. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1594. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  1595. struct sh_mobile_ceu_dev, ici);
  1596. soc_camera_host_unregister(soc_host);
  1597. pm_runtime_disable(&pdev->dev);
  1598. free_irq(pcdev->irq, pcdev);
  1599. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1600. dma_release_declared_memory(&pdev->dev);
  1601. iounmap(pcdev->base);
  1602. kfree(pcdev);
  1603. return 0;
  1604. }
  1605. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1606. {
  1607. /* Runtime PM callback shared between ->runtime_suspend()
  1608. * and ->runtime_resume(). Simply returns success.
  1609. *
  1610. * This driver re-initializes all registers after
  1611. * pm_runtime_get_sync() anyway so there is no need
  1612. * to save and restore registers here.
  1613. */
  1614. return 0;
  1615. }
  1616. static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1617. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1618. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1619. };
  1620. static struct platform_driver sh_mobile_ceu_driver = {
  1621. .driver = {
  1622. .name = "sh_mobile_ceu",
  1623. .pm = &sh_mobile_ceu_dev_pm_ops,
  1624. },
  1625. .probe = sh_mobile_ceu_probe,
  1626. .remove = __devexit_p(sh_mobile_ceu_remove),
  1627. };
  1628. static int __init sh_mobile_ceu_init(void)
  1629. {
  1630. return platform_driver_register(&sh_mobile_ceu_driver);
  1631. }
  1632. static void __exit sh_mobile_ceu_exit(void)
  1633. {
  1634. platform_driver_unregister(&sh_mobile_ceu_driver);
  1635. }
  1636. module_init(sh_mobile_ceu_init);
  1637. module_exit(sh_mobile_ceu_exit);
  1638. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1639. MODULE_AUTHOR("Magnus Damm");
  1640. MODULE_LICENSE("GPL");
  1641. MODULE_ALIAS("platform:sh_mobile_ceu");