sh_mobile_ceu_camera.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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 <media/v4l2-common.h>
  34. #include <media/v4l2-dev.h>
  35. #include <media/soc_camera.h>
  36. #include <media/sh_mobile_ceu.h>
  37. #include <media/videobuf-dma-contig.h>
  38. /* register offsets for sh7722 / sh7723 */
  39. #define CAPSR 0x00 /* Capture start register */
  40. #define CAPCR 0x04 /* Capture control register */
  41. #define CAMCR 0x08 /* Capture interface control register */
  42. #define CMCYR 0x0c /* Capture interface cycle register */
  43. #define CAMOR 0x10 /* Capture interface offset register */
  44. #define CAPWR 0x14 /* Capture interface width register */
  45. #define CAIFR 0x18 /* Capture interface input format register */
  46. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  47. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  48. #define CRCNTR 0x28 /* CEU register control register */
  49. #define CRCMPR 0x2c /* CEU register forcible control register */
  50. #define CFLCR 0x30 /* Capture filter control register */
  51. #define CFSZR 0x34 /* Capture filter size clip register */
  52. #define CDWDR 0x38 /* Capture destination width register */
  53. #define CDAYR 0x3c /* Capture data address Y register */
  54. #define CDACR 0x40 /* Capture data address C register */
  55. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  56. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  57. #define CBDSR 0x4c /* Capture bundle destination size register */
  58. #define CFWCR 0x5c /* Firewall operation control register */
  59. #define CLFCR 0x60 /* Capture low-pass filter control register */
  60. #define CDOCR 0x64 /* Capture data output control register */
  61. #define CDDCR 0x68 /* Capture data complexity level register */
  62. #define CDDAR 0x6c /* Capture data complexity level address register */
  63. #define CEIER 0x70 /* Capture event interrupt enable register */
  64. #define CETCR 0x74 /* Capture event flag clear register */
  65. #define CSTSR 0x7c /* Capture status register */
  66. #define CSRTR 0x80 /* Capture software reset register */
  67. #define CDSSR 0x84 /* Capture data size register */
  68. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  69. #define CDACR2 0x94 /* Capture data address C register 2 */
  70. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  71. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  72. /* per video frame buffer */
  73. struct sh_mobile_ceu_buffer {
  74. struct videobuf_buffer vb; /* v4l buffer must be first */
  75. const struct soc_camera_data_format *fmt;
  76. };
  77. struct sh_mobile_ceu_dev {
  78. struct soc_camera_host ici;
  79. struct soc_camera_device *icd;
  80. unsigned int irq;
  81. void __iomem *base;
  82. unsigned long video_limit;
  83. /* lock used to protect videobuf */
  84. spinlock_t lock;
  85. struct list_head capture;
  86. struct videobuf_buffer *active;
  87. struct sh_mobile_ceu_info *pdata;
  88. u32 cflcr;
  89. unsigned int is_interlaced:1;
  90. unsigned int image_mode:1;
  91. unsigned int is_16bit:1;
  92. };
  93. struct sh_mobile_ceu_cam {
  94. struct v4l2_rect camera_rect;
  95. struct v4l2_rect camera_max;
  96. const struct soc_camera_data_format *extra_fmt;
  97. const struct soc_camera_data_format *camera_fmt;
  98. };
  99. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  100. {
  101. unsigned long flags;
  102. flags = SOCAM_MASTER |
  103. SOCAM_PCLK_SAMPLE_RISING |
  104. SOCAM_HSYNC_ACTIVE_HIGH |
  105. SOCAM_HSYNC_ACTIVE_LOW |
  106. SOCAM_VSYNC_ACTIVE_HIGH |
  107. SOCAM_VSYNC_ACTIVE_LOW |
  108. SOCAM_DATA_ACTIVE_HIGH;
  109. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  110. flags |= SOCAM_DATAWIDTH_8;
  111. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  112. flags |= SOCAM_DATAWIDTH_16;
  113. if (flags & SOCAM_DATAWIDTH_MASK)
  114. return flags;
  115. return 0;
  116. }
  117. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  118. unsigned long reg_offs, u32 data)
  119. {
  120. iowrite32(data, priv->base + reg_offs);
  121. }
  122. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  123. {
  124. return ioread32(priv->base + reg_offs);
  125. }
  126. /*
  127. * Videobuf operations
  128. */
  129. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  130. unsigned int *count,
  131. unsigned int *size)
  132. {
  133. struct soc_camera_device *icd = vq->priv_data;
  134. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  135. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  136. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  137. *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height *
  138. bytes_per_pixel);
  139. if (0 == *count)
  140. *count = 2;
  141. if (pcdev->video_limit) {
  142. while (*size * *count > pcdev->video_limit)
  143. (*count)--;
  144. }
  145. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  146. return 0;
  147. }
  148. static void free_buffer(struct videobuf_queue *vq,
  149. struct sh_mobile_ceu_buffer *buf)
  150. {
  151. struct soc_camera_device *icd = vq->priv_data;
  152. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  153. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  154. if (in_interrupt())
  155. BUG();
  156. videobuf_waiton(&buf->vb, 0, 0);
  157. videobuf_dma_contig_free(vq, &buf->vb);
  158. dev_dbg(icd->dev.parent, "%s freed\n", __func__);
  159. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  160. }
  161. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  162. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  163. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  164. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  165. static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  166. {
  167. struct soc_camera_device *icd = pcdev->icd;
  168. dma_addr_t phys_addr_top, phys_addr_bottom;
  169. /* The hardware is _very_ picky about this sequence. Especially
  170. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  171. * several not-so-well documented interrupt sources in CETCR.
  172. */
  173. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
  174. ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
  175. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
  176. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  177. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  178. if (!pcdev->active)
  179. return;
  180. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  181. ceu_write(pcdev, CDAYR, phys_addr_top);
  182. if (pcdev->is_interlaced) {
  183. phys_addr_bottom = phys_addr_top + icd->rect_current.width;
  184. ceu_write(pcdev, CDBYR, phys_addr_bottom);
  185. }
  186. switch (icd->current_fmt->fourcc) {
  187. case V4L2_PIX_FMT_NV12:
  188. case V4L2_PIX_FMT_NV21:
  189. case V4L2_PIX_FMT_NV16:
  190. case V4L2_PIX_FMT_NV61:
  191. phys_addr_top += icd->rect_current.width *
  192. icd->rect_current.height;
  193. ceu_write(pcdev, CDACR, phys_addr_top);
  194. if (pcdev->is_interlaced) {
  195. phys_addr_bottom = phys_addr_top +
  196. icd->rect_current.width;
  197. ceu_write(pcdev, CDBCR, phys_addr_bottom);
  198. }
  199. }
  200. pcdev->active->state = VIDEOBUF_ACTIVE;
  201. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  202. }
  203. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  204. struct videobuf_buffer *vb,
  205. enum v4l2_field field)
  206. {
  207. struct soc_camera_device *icd = vq->priv_data;
  208. struct sh_mobile_ceu_buffer *buf;
  209. int ret;
  210. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  211. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  212. vb, vb->baddr, vb->bsize);
  213. /* Added list head initialization on alloc */
  214. WARN_ON(!list_empty(&vb->queue));
  215. #ifdef DEBUG
  216. /* This can be useful if you want to see if we actually fill
  217. * the buffer with something */
  218. memset((void *)vb->baddr, 0xaa, vb->bsize);
  219. #endif
  220. BUG_ON(NULL == icd->current_fmt);
  221. if (buf->fmt != icd->current_fmt ||
  222. vb->width != icd->rect_current.width ||
  223. vb->height != icd->rect_current.height ||
  224. vb->field != field) {
  225. buf->fmt = icd->current_fmt;
  226. vb->width = icd->rect_current.width;
  227. vb->height = icd->rect_current.height;
  228. vb->field = field;
  229. vb->state = VIDEOBUF_NEEDS_INIT;
  230. }
  231. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  232. if (0 != vb->baddr && vb->bsize < vb->size) {
  233. ret = -EINVAL;
  234. goto out;
  235. }
  236. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  237. ret = videobuf_iolock(vq, vb, NULL);
  238. if (ret)
  239. goto fail;
  240. vb->state = VIDEOBUF_PREPARED;
  241. }
  242. return 0;
  243. fail:
  244. free_buffer(vq, buf);
  245. out:
  246. return ret;
  247. }
  248. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  249. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  250. struct videobuf_buffer *vb)
  251. {
  252. struct soc_camera_device *icd = vq->priv_data;
  253. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  254. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  255. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  256. vb, vb->baddr, vb->bsize);
  257. vb->state = VIDEOBUF_QUEUED;
  258. list_add_tail(&vb->queue, &pcdev->capture);
  259. if (!pcdev->active) {
  260. pcdev->active = vb;
  261. sh_mobile_ceu_capture(pcdev);
  262. }
  263. }
  264. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  265. struct videobuf_buffer *vb)
  266. {
  267. struct soc_camera_device *icd = vq->priv_data;
  268. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  269. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  270. unsigned long flags;
  271. spin_lock_irqsave(&pcdev->lock, flags);
  272. if (pcdev->active == vb) {
  273. /* disable capture (release DMA buffer), reset */
  274. ceu_write(pcdev, CAPSR, 1 << 16);
  275. pcdev->active = NULL;
  276. }
  277. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
  278. !list_empty(&vb->queue)) {
  279. vb->state = VIDEOBUF_ERROR;
  280. list_del_init(&vb->queue);
  281. }
  282. spin_unlock_irqrestore(&pcdev->lock, flags);
  283. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  284. }
  285. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  286. .buf_setup = sh_mobile_ceu_videobuf_setup,
  287. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  288. .buf_queue = sh_mobile_ceu_videobuf_queue,
  289. .buf_release = sh_mobile_ceu_videobuf_release,
  290. };
  291. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  292. {
  293. struct sh_mobile_ceu_dev *pcdev = data;
  294. struct videobuf_buffer *vb;
  295. unsigned long flags;
  296. spin_lock_irqsave(&pcdev->lock, flags);
  297. vb = pcdev->active;
  298. if (!vb)
  299. /* Stale interrupt from a released buffer */
  300. goto out;
  301. list_del_init(&vb->queue);
  302. if (!list_empty(&pcdev->capture))
  303. pcdev->active = list_entry(pcdev->capture.next,
  304. struct videobuf_buffer, queue);
  305. else
  306. pcdev->active = NULL;
  307. sh_mobile_ceu_capture(pcdev);
  308. vb->state = VIDEOBUF_DONE;
  309. do_gettimeofday(&vb->ts);
  310. vb->field_count++;
  311. wake_up(&vb->done);
  312. out:
  313. spin_unlock_irqrestore(&pcdev->lock, flags);
  314. return IRQ_HANDLED;
  315. }
  316. /* Called with .video_lock held */
  317. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  318. {
  319. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  320. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  321. if (pcdev->icd)
  322. return -EBUSY;
  323. dev_info(icd->dev.parent,
  324. "SuperH Mobile CEU driver attached to camera %d\n",
  325. icd->devnum);
  326. clk_enable(pcdev->clk);
  327. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  328. while (ceu_read(pcdev, CSTSR) & 1)
  329. msleep(1);
  330. pcdev->icd = icd;
  331. return 0;
  332. }
  333. /* Called with .video_lock held */
  334. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  335. {
  336. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  337. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  338. unsigned long flags;
  339. BUG_ON(icd != pcdev->icd);
  340. /* disable capture, disable interrupts */
  341. ceu_write(pcdev, CEIER, 0);
  342. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  343. /* make sure active buffer is canceled */
  344. spin_lock_irqsave(&pcdev->lock, flags);
  345. if (pcdev->active) {
  346. list_del(&pcdev->active->queue);
  347. pcdev->active->state = VIDEOBUF_ERROR;
  348. wake_up_all(&pcdev->active->done);
  349. pcdev->active = NULL;
  350. }
  351. spin_unlock_irqrestore(&pcdev->lock, flags);
  352. clk_disable(pcdev->clk);
  353. dev_info(icd->dev.parent,
  354. "SuperH Mobile CEU driver detached from camera %d\n",
  355. icd->devnum);
  356. pcdev->icd = NULL;
  357. }
  358. /*
  359. * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
  360. * in SH7722 Hardware Manual
  361. */
  362. static unsigned int size_dst(unsigned int src, unsigned int scale)
  363. {
  364. unsigned int mant_pre = scale >> 12;
  365. if (!src || !scale)
  366. return src;
  367. return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
  368. mant_pre * 4096 / scale + 1;
  369. }
  370. static unsigned int size_src(unsigned int dst, unsigned int scale)
  371. {
  372. unsigned int mant_pre = scale >> 12, tmp;
  373. if (!dst || !scale)
  374. return dst;
  375. for (tmp = ((dst - 1) * scale + 2048 * mant_pre) / 4096 + 1;
  376. size_dst(tmp, scale) < dst;
  377. tmp++)
  378. ;
  379. return tmp;
  380. }
  381. static u16 calc_scale(unsigned int src, unsigned int *dst)
  382. {
  383. u16 scale;
  384. if (src == *dst)
  385. return 0;
  386. scale = (src * 4096 / *dst) & ~7;
  387. while (scale > 4096 && size_dst(src, scale) < *dst)
  388. scale -= 8;
  389. *dst = size_dst(src, scale);
  390. return scale;
  391. }
  392. /* rect is guaranteed to not exceed the scaled camera rectangle */
  393. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
  394. struct v4l2_rect *rect)
  395. {
  396. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  397. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  398. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  399. int width, height, cfszr_width, cdwdr_width, in_width, in_height;
  400. unsigned int left_offset, top_offset, left, top;
  401. unsigned int hscale = pcdev->cflcr & 0xffff;
  402. unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff;
  403. u32 camor;
  404. /* Switch to the camera scale */
  405. left = size_src(rect->left, hscale);
  406. top = size_src(rect->top, vscale);
  407. dev_dbg(icd->dev.parent, "Left %u * 0x%x = %u, top %u * 0x%x = %u\n",
  408. rect->left, hscale, left, rect->top, vscale, top);
  409. if (left > cam->camera_rect.left) {
  410. left_offset = left - cam->camera_rect.left;
  411. } else {
  412. left_offset = 0;
  413. left = cam->camera_rect.left;
  414. }
  415. if (top > cam->camera_rect.top) {
  416. top_offset = top - cam->camera_rect.top;
  417. } else {
  418. top_offset = 0;
  419. top = cam->camera_rect.top;
  420. }
  421. dev_dbg(icd->dev.parent, "New left %u, top %u, offsets %u:%u\n",
  422. rect->left, rect->top, left_offset, top_offset);
  423. if (pcdev->image_mode) {
  424. width = rect->width;
  425. in_width = cam->camera_rect.width;
  426. if (!pcdev->is_16bit) {
  427. width *= 2;
  428. in_width *= 2;
  429. left_offset *= 2;
  430. }
  431. cfszr_width = cdwdr_width = rect->width;
  432. } else {
  433. unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
  434. if (!pcdev->is_16bit)
  435. w_factor *= 2;
  436. width = rect->width * w_factor / 2;
  437. in_width = cam->camera_rect.width * w_factor / 2;
  438. left_offset = left_offset * w_factor / 2;
  439. cfszr_width = pcdev->is_16bit ? width : width / 2;
  440. cdwdr_width = pcdev->is_16bit ? width * 2 : width;
  441. }
  442. height = rect->height;
  443. in_height = cam->camera_rect.height;
  444. if (pcdev->is_interlaced) {
  445. height /= 2;
  446. in_height /= 2;
  447. top_offset /= 2;
  448. cdwdr_width *= 2;
  449. }
  450. camor = left_offset | (top_offset << 16);
  451. ceu_write(pcdev, CAMOR, camor);
  452. ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
  453. ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
  454. ceu_write(pcdev, CDWDR, cdwdr_width);
  455. }
  456. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  457. {
  458. u32 capsr = ceu_read(pcdev, CAPSR);
  459. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  460. return capsr;
  461. }
  462. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  463. {
  464. unsigned long timeout = jiffies + 10 * HZ;
  465. /*
  466. * Wait until the end of the current frame. It can take a long time,
  467. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  468. */
  469. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  470. msleep(1);
  471. if (time_after(jiffies, timeout)) {
  472. dev_err(pcdev->ici.v4l2_dev.dev,
  473. "Timeout waiting for frame end! Interface problem?\n");
  474. return;
  475. }
  476. /* Wait until reset clears, this shall not hang... */
  477. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  478. udelay(10);
  479. /* Anything to restore? */
  480. if (capsr & ~(1 << 16))
  481. ceu_write(pcdev, CAPSR, capsr);
  482. }
  483. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  484. __u32 pixfmt)
  485. {
  486. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  487. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  488. int ret;
  489. unsigned long camera_flags, common_flags, value;
  490. int yuv_lineskip;
  491. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  492. u32 capsr = capture_save_reset(pcdev);
  493. camera_flags = icd->ops->query_bus_param(icd);
  494. common_flags = soc_camera_bus_param_compatible(camera_flags,
  495. make_bus_param(pcdev));
  496. if (!common_flags)
  497. return -EINVAL;
  498. ret = icd->ops->set_bus_param(icd, common_flags);
  499. if (ret < 0)
  500. return ret;
  501. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  502. case SOCAM_DATAWIDTH_8:
  503. pcdev->is_16bit = 0;
  504. break;
  505. case SOCAM_DATAWIDTH_16:
  506. pcdev->is_16bit = 1;
  507. break;
  508. default:
  509. return -EINVAL;
  510. }
  511. ceu_write(pcdev, CRCNTR, 0);
  512. ceu_write(pcdev, CRCMPR, 0);
  513. value = 0x00000010; /* data fetch by default */
  514. yuv_lineskip = 0;
  515. switch (icd->current_fmt->fourcc) {
  516. case V4L2_PIX_FMT_NV12:
  517. case V4L2_PIX_FMT_NV21:
  518. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  519. /* fall-through */
  520. case V4L2_PIX_FMT_NV16:
  521. case V4L2_PIX_FMT_NV61:
  522. switch (cam->camera_fmt->fourcc) {
  523. case V4L2_PIX_FMT_UYVY:
  524. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  525. break;
  526. case V4L2_PIX_FMT_VYUY:
  527. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  528. break;
  529. case V4L2_PIX_FMT_YUYV:
  530. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  531. break;
  532. case V4L2_PIX_FMT_YVYU:
  533. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  534. break;
  535. default:
  536. BUG();
  537. }
  538. }
  539. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  540. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  541. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  542. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  543. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  544. value |= pcdev->is_16bit ? 1 << 12 : 0;
  545. ceu_write(pcdev, CAMCR, value);
  546. ceu_write(pcdev, CAPCR, 0x00300000);
  547. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  548. mdelay(1);
  549. sh_mobile_ceu_set_rect(icd, &icd->rect_current);
  550. ceu_write(pcdev, CFLCR, pcdev->cflcr);
  551. /* A few words about byte order (observed in Big Endian mode)
  552. *
  553. * In data fetch mode bytes are received in chunks of 8 bytes.
  554. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  555. *
  556. * The data is however by default written to memory in reverse order:
  557. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  558. *
  559. * The lowest three bits of CDOCR allows us to do swapping,
  560. * using 7 we swap the data bytes to match the incoming order:
  561. * D0, D1, D2, D3, D4, D5, D6, D7
  562. */
  563. value = 0x00000017;
  564. if (yuv_lineskip)
  565. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  566. ceu_write(pcdev, CDOCR, value);
  567. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  568. dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u@%u:%u\n",
  569. pixfmt & 0xff, (pixfmt >> 8) & 0xff,
  570. (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
  571. icd->rect_current.width, icd->rect_current.height,
  572. icd->rect_current.left, icd->rect_current.top);
  573. capture_restore(pcdev, capsr);
  574. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  575. return 0;
  576. }
  577. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  578. {
  579. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  580. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  581. unsigned long camera_flags, common_flags;
  582. camera_flags = icd->ops->query_bus_param(icd);
  583. common_flags = soc_camera_bus_param_compatible(camera_flags,
  584. make_bus_param(pcdev));
  585. if (!common_flags)
  586. return -EINVAL;
  587. return 0;
  588. }
  589. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  590. {
  591. .name = "NV12",
  592. .depth = 12,
  593. .fourcc = V4L2_PIX_FMT_NV12,
  594. .colorspace = V4L2_COLORSPACE_JPEG,
  595. },
  596. {
  597. .name = "NV21",
  598. .depth = 12,
  599. .fourcc = V4L2_PIX_FMT_NV21,
  600. .colorspace = V4L2_COLORSPACE_JPEG,
  601. },
  602. {
  603. .name = "NV16",
  604. .depth = 16,
  605. .fourcc = V4L2_PIX_FMT_NV16,
  606. .colorspace = V4L2_COLORSPACE_JPEG,
  607. },
  608. {
  609. .name = "NV61",
  610. .depth = 16,
  611. .fourcc = V4L2_PIX_FMT_NV61,
  612. .colorspace = V4L2_COLORSPACE_JPEG,
  613. },
  614. };
  615. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  616. struct soc_camera_format_xlate *xlate)
  617. {
  618. int ret, k, n;
  619. int formats = 0;
  620. struct sh_mobile_ceu_cam *cam;
  621. ret = sh_mobile_ceu_try_bus_param(icd);
  622. if (ret < 0)
  623. return 0;
  624. if (!icd->host_priv) {
  625. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  626. if (!cam)
  627. return -ENOMEM;
  628. icd->host_priv = cam;
  629. cam->camera_max = icd->rect_max;
  630. } else {
  631. cam = icd->host_priv;
  632. }
  633. /* Beginning of a pass */
  634. if (!idx)
  635. cam->extra_fmt = NULL;
  636. switch (icd->formats[idx].fourcc) {
  637. case V4L2_PIX_FMT_UYVY:
  638. case V4L2_PIX_FMT_VYUY:
  639. case V4L2_PIX_FMT_YUYV:
  640. case V4L2_PIX_FMT_YVYU:
  641. if (cam->extra_fmt)
  642. goto add_single_format;
  643. /*
  644. * Our case is simple so far: for any of the above four camera
  645. * formats we add all our four synthesized NV* formats, so,
  646. * just marking the device with a single flag suffices. If
  647. * the format generation rules are more complex, you would have
  648. * to actually hang your already added / counted formats onto
  649. * the host_priv pointer and check whether the format you're
  650. * going to add now is already there.
  651. */
  652. cam->extra_fmt = (void *)sh_mobile_ceu_formats;
  653. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  654. formats += n;
  655. for (k = 0; xlate && k < n; k++) {
  656. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  657. xlate->cam_fmt = icd->formats + idx;
  658. xlate->buswidth = icd->formats[idx].depth;
  659. xlate++;
  660. dev_dbg(icd->dev.parent,
  661. "Providing format %s using %s\n",
  662. sh_mobile_ceu_formats[k].name,
  663. icd->formats[idx].name);
  664. }
  665. default:
  666. add_single_format:
  667. /* Generic pass-through */
  668. formats++;
  669. if (xlate) {
  670. xlate->host_fmt = icd->formats + idx;
  671. xlate->cam_fmt = icd->formats + idx;
  672. xlate->buswidth = icd->formats[idx].depth;
  673. xlate++;
  674. dev_dbg(icd->dev.parent,
  675. "Providing format %s in pass-through mode\n",
  676. icd->formats[idx].name);
  677. }
  678. }
  679. return formats;
  680. }
  681. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  682. {
  683. kfree(icd->host_priv);
  684. icd->host_priv = NULL;
  685. }
  686. /* Check if any dimension of r1 is smaller than respective one of r2 */
  687. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  688. {
  689. return r1->width < r2->width || r1->height < r2->height;
  690. }
  691. /* Check if r1 fails to cover r2 */
  692. static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
  693. {
  694. return r1->left > r2->left || r1->top > r2->top ||
  695. r1->left + r1->width < r2->left + r2->width ||
  696. r1->top + r1->height < r2->top + r2->height;
  697. }
  698. /*
  699. * CEU can scale and crop, but we don't want to waste bandwidth and kill the
  700. * framerate by always requesting the maximum image from the client. For
  701. * cropping we also have to take care of the current scale. The common for both
  702. * scaling and cropping approach is:
  703. * 1. try if the client can produce exactly what requested by the user
  704. * 2. if (1) failed, try to double the client image until we get one big enough
  705. * 3. if (2) failed, try to request the maximum image
  706. */
  707. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  708. struct v4l2_crop *a)
  709. {
  710. struct v4l2_rect *rect = &a->c;
  711. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  712. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  713. struct v4l2_crop cam_crop;
  714. struct v4l2_rect *cam_rect = &cam_crop.c, target, cam_max;
  715. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  716. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  717. unsigned int hscale = pcdev->cflcr & 0xffff;
  718. unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff;
  719. unsigned short width, height;
  720. u32 capsr;
  721. int ret;
  722. /* Scale back up into client units */
  723. cam_rect->left = size_src(rect->left, hscale);
  724. cam_rect->width = size_src(rect->width, hscale);
  725. cam_rect->top = size_src(rect->top, vscale);
  726. cam_rect->height = size_src(rect->height, vscale);
  727. target = *cam_rect;
  728. capsr = capture_save_reset(pcdev);
  729. dev_dbg(icd->dev.parent, "CAPSR 0x%x, CFLCR 0x%x\n",
  730. capsr, pcdev->cflcr);
  731. /* First attempt - see if the client can deliver a perfect result */
  732. ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
  733. if (!ret && !memcmp(&target, &cam_rect, sizeof(target))) {
  734. dev_dbg(icd->dev.parent,
  735. "Camera S_CROP successful for %ux%u@%u:%u\n",
  736. cam_rect->width, cam_rect->height,
  737. cam_rect->left, cam_rect->top);
  738. goto ceu_set_rect;
  739. }
  740. /* Try to fix cropping, that camera hasn't managed to do */
  741. dev_dbg(icd->dev.parent, "Fix camera S_CROP %d for %ux%u@%u:%u"
  742. " to %ux%u@%u:%u\n",
  743. ret, cam_rect->width, cam_rect->height,
  744. cam_rect->left, cam_rect->top,
  745. target.width, target.height, target.left, target.top);
  746. /*
  747. * Popular special case - some cameras can only handle fixed sizes like
  748. * QVGA, VGA,... Take care to avoid infinite loop.
  749. */
  750. width = max(cam_rect->width, 1);
  751. height = max(cam_rect->height, 1);
  752. cam_max.width = size_src(icd->rect_max.width, hscale);
  753. cam_max.left = size_src(icd->rect_max.left, hscale);
  754. cam_max.height = size_src(icd->rect_max.height, vscale);
  755. cam_max.top = size_src(icd->rect_max.top, vscale);
  756. while (!ret && (is_smaller(cam_rect, &target) ||
  757. is_inside(cam_rect, &target)) &&
  758. cam_max.width >= width && cam_max.height >= height) {
  759. width *= 2;
  760. height *= 2;
  761. cam_rect->width = width;
  762. cam_rect->height = height;
  763. /* We do not know what the camera is capable of, play safe */
  764. if (cam_rect->left > target.left)
  765. cam_rect->left = cam_max.left;
  766. if (cam_rect->left + cam_rect->width < target.left + target.width)
  767. cam_rect->width = target.left + target.width -
  768. cam_rect->left;
  769. if (cam_rect->top > target.top)
  770. cam_rect->top = cam_max.top;
  771. if (cam_rect->top + cam_rect->height < target.top + target.height)
  772. cam_rect->height = target.top + target.height -
  773. cam_rect->top;
  774. if (cam_rect->width + cam_rect->left >
  775. cam_max.width + cam_max.left)
  776. cam_rect->left = max(cam_max.width + cam_max.left -
  777. cam_rect->width, cam_max.left);
  778. if (cam_rect->height + cam_rect->top >
  779. cam_max.height + cam_max.top)
  780. cam_rect->top = max(cam_max.height + cam_max.top -
  781. cam_rect->height, cam_max.top);
  782. ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
  783. dev_dbg(icd->dev.parent, "Camera S_CROP %d for %ux%u@%u:%u\n",
  784. ret, cam_rect->width, cam_rect->height,
  785. cam_rect->left, cam_rect->top);
  786. }
  787. /*
  788. * If the camera failed to configure cropping, it should not modify the
  789. * rectangle
  790. */
  791. if ((ret < 0 && (is_smaller(&icd->rect_current, rect) ||
  792. is_inside(&icd->rect_current, rect))) ||
  793. is_smaller(cam_rect, &target) || is_inside(cam_rect, &target)) {
  794. /*
  795. * The camera failed to configure a suitable cropping,
  796. * we cannot use the current rectangle, set to max
  797. */
  798. *cam_rect = cam_max;
  799. ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
  800. dev_dbg(icd->dev.parent,
  801. "Camera S_CROP %d for max %ux%u@%u:%u\n",
  802. ret, cam_rect->width, cam_rect->height,
  803. cam_rect->left, cam_rect->top);
  804. if (ret < 0 && ret != -ENOIOCTLCMD)
  805. /* All failed, hopefully resume current capture */
  806. goto resume_capture;
  807. /* Finally, adjust the target rectangle */
  808. if (target.width > cam_rect->width)
  809. target.width = cam_rect->width;
  810. if (target.height > cam_rect->height)
  811. target.height = cam_rect->height;
  812. if (target.left + target.width > cam_rect->left + cam_rect->width)
  813. target.left = cam_rect->left + cam_rect->width -
  814. target.width;
  815. if (target.top + target.height > cam_rect->top + cam_rect->height)
  816. target.top = cam_rect->top + cam_rect->height -
  817. target.height;
  818. }
  819. /* We now have a rectangle, larger than requested, let's crop */
  820. /*
  821. * We have to preserve camera rectangle between close() / open(),
  822. * because soc-camera core calls .set_fmt() on each first open() with
  823. * last before last close() _user_ rectangle, which can be different
  824. * from camera rectangle.
  825. */
  826. dev_dbg(icd->dev.parent,
  827. "SH S_CROP from %ux%u@%u:%u to %ux%u@%u:%u, scale to %ux%u@%u:%u\n",
  828. cam_rect->width, cam_rect->height, cam_rect->left, cam_rect->top,
  829. target.width, target.height, target.left, target.top,
  830. rect->width, rect->height, rect->left, rect->top);
  831. ret = 0;
  832. ceu_set_rect:
  833. cam->camera_rect = *cam_rect;
  834. rect->width = size_dst(target.width, hscale);
  835. rect->left = size_dst(target.left, hscale);
  836. rect->height = size_dst(target.height, vscale);
  837. rect->top = size_dst(target.top, vscale);
  838. sh_mobile_ceu_set_rect(icd, rect);
  839. resume_capture:
  840. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  841. if (pcdev->active)
  842. capsr |= 1;
  843. capture_restore(pcdev, capsr);
  844. /* Even if only camera cropping succeeded */
  845. return ret;
  846. }
  847. /* Similar to set_crop multistage iterative algorithm */
  848. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  849. struct v4l2_format *f)
  850. {
  851. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  852. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  853. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  854. struct v4l2_pix_format *pix = &f->fmt.pix;
  855. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  856. __u32 pixfmt = pix->pixelformat;
  857. const struct soc_camera_format_xlate *xlate;
  858. unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
  859. u16 vscale, hscale;
  860. int ret, is_interlaced;
  861. switch (pix->field) {
  862. case V4L2_FIELD_INTERLACED:
  863. is_interlaced = 1;
  864. break;
  865. case V4L2_FIELD_ANY:
  866. default:
  867. pix->field = V4L2_FIELD_NONE;
  868. /* fall-through */
  869. case V4L2_FIELD_NONE:
  870. is_interlaced = 0;
  871. break;
  872. }
  873. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  874. if (!xlate) {
  875. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  876. return -EINVAL;
  877. }
  878. pix->pixelformat = xlate->cam_fmt->fourcc;
  879. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  880. pix->pixelformat = pixfmt;
  881. dev_dbg(icd->dev.parent,
  882. "Camera %d fmt %ux%u, requested %ux%u, max %ux%u\n",
  883. ret, pix->width, pix->height, width, height,
  884. icd->rect_max.width, icd->rect_max.height);
  885. if (ret < 0)
  886. return ret;
  887. switch (pixfmt) {
  888. case V4L2_PIX_FMT_NV12:
  889. case V4L2_PIX_FMT_NV21:
  890. case V4L2_PIX_FMT_NV16:
  891. case V4L2_PIX_FMT_NV61:
  892. pcdev->image_mode = 1;
  893. break;
  894. default:
  895. pcdev->image_mode = 0;
  896. }
  897. if ((abs(width - pix->width) < 4 && abs(height - pix->height) < 4) ||
  898. !pcdev->image_mode || is_interlaced) {
  899. hscale = 0;
  900. vscale = 0;
  901. goto out;
  902. }
  903. /* Camera set a format, but geometry is not precise, try to improve */
  904. /*
  905. * FIXME: when soc-camera is converted to implement traditional S_FMT
  906. * and S_CROP semantics, replace CEU limits with camera maxima
  907. */
  908. tmp_w = pix->width;
  909. tmp_h = pix->height;
  910. while ((width > tmp_w || height > tmp_h) &&
  911. tmp_w < 2560 && tmp_h < 1920) {
  912. tmp_w = min(2 * tmp_w, (__u32)2560);
  913. tmp_h = min(2 * tmp_h, (__u32)1920);
  914. pix->width = tmp_w;
  915. pix->height = tmp_h;
  916. pix->pixelformat = xlate->cam_fmt->fourcc;
  917. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  918. pix->pixelformat = pixfmt;
  919. dev_dbg(icd->dev.parent, "Camera scaled to %ux%u\n",
  920. pix->width, pix->height);
  921. if (ret < 0) {
  922. /* This shouldn't happen */
  923. dev_err(icd->dev.parent,
  924. "Client failed to set format: %d\n", ret);
  925. return ret;
  926. }
  927. }
  928. /* We cannot scale up */
  929. if (width > pix->width)
  930. width = pix->width;
  931. if (height > pix->height)
  932. height = pix->height;
  933. /* Let's rock: scale pix->{width x height} down to width x height */
  934. hscale = calc_scale(pix->width, &width);
  935. vscale = calc_scale(pix->height, &height);
  936. dev_dbg(icd->dev.parent, "W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
  937. pix->width, hscale, width, pix->height, vscale, height);
  938. out:
  939. pcdev->cflcr = hscale | (vscale << 16);
  940. icd->buswidth = xlate->buswidth;
  941. icd->current_fmt = xlate->host_fmt;
  942. cam->camera_fmt = xlate->cam_fmt;
  943. cam->camera_rect.width = pix->width;
  944. cam->camera_rect.height = pix->height;
  945. icd->rect_max.left = size_dst(cam->camera_max.left, hscale);
  946. icd->rect_max.width = size_dst(cam->camera_max.width, hscale);
  947. icd->rect_max.top = size_dst(cam->camera_max.top, vscale);
  948. icd->rect_max.height = size_dst(cam->camera_max.height, vscale);
  949. icd->rect_current.left = icd->rect_max.left;
  950. icd->rect_current.top = icd->rect_max.top;
  951. pcdev->is_interlaced = is_interlaced;
  952. pix->width = width;
  953. pix->height = height;
  954. return 0;
  955. }
  956. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  957. struct v4l2_format *f)
  958. {
  959. const struct soc_camera_format_xlate *xlate;
  960. struct v4l2_pix_format *pix = &f->fmt.pix;
  961. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  962. __u32 pixfmt = pix->pixelformat;
  963. int width, height;
  964. int ret;
  965. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  966. if (!xlate) {
  967. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  968. return -EINVAL;
  969. }
  970. /* FIXME: calculate using depth and bus width */
  971. v4l_bound_align_image(&pix->width, 2, 2560, 1,
  972. &pix->height, 4, 1920, 2, 0);
  973. width = pix->width;
  974. height = pix->height;
  975. pix->bytesperline = pix->width *
  976. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  977. pix->sizeimage = pix->height * pix->bytesperline;
  978. pix->pixelformat = xlate->cam_fmt->fourcc;
  979. /* limit to sensor capabilities */
  980. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  981. pix->pixelformat = pixfmt;
  982. if (ret < 0)
  983. return ret;
  984. switch (pixfmt) {
  985. case V4L2_PIX_FMT_NV12:
  986. case V4L2_PIX_FMT_NV21:
  987. case V4L2_PIX_FMT_NV16:
  988. case V4L2_PIX_FMT_NV61:
  989. /* FIXME: check against rect_max after converting soc-camera */
  990. /* We can scale precisely, need a bigger image from camera */
  991. if (pix->width < width || pix->height < height) {
  992. int tmp_w = pix->width, tmp_h = pix->height;
  993. pix->width = 2560;
  994. pix->height = 1920;
  995. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  996. if (ret < 0) {
  997. /* Shouldn't actually happen... */
  998. dev_err(icd->dev.parent,
  999. "FIXME: try_fmt() returned %d\n", ret);
  1000. pix->width = tmp_w;
  1001. pix->height = tmp_h;
  1002. }
  1003. }
  1004. if (pix->width > width)
  1005. pix->width = width;
  1006. if (pix->height > height)
  1007. pix->height = height;
  1008. }
  1009. return ret;
  1010. }
  1011. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  1012. struct v4l2_requestbuffers *p)
  1013. {
  1014. int i;
  1015. /* This is for locking debugging only. I removed spinlocks and now I
  1016. * check whether .prepare is ever called on a linked buffer, or whether
  1017. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1018. * it hadn't triggered */
  1019. for (i = 0; i < p->count; i++) {
  1020. struct sh_mobile_ceu_buffer *buf;
  1021. buf = container_of(icf->vb_vidq.bufs[i],
  1022. struct sh_mobile_ceu_buffer, vb);
  1023. INIT_LIST_HEAD(&buf->vb.queue);
  1024. }
  1025. return 0;
  1026. }
  1027. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  1028. {
  1029. struct soc_camera_file *icf = file->private_data;
  1030. struct sh_mobile_ceu_buffer *buf;
  1031. buf = list_entry(icf->vb_vidq.stream.next,
  1032. struct sh_mobile_ceu_buffer, vb.stream);
  1033. poll_wait(file, &buf->vb.done, pt);
  1034. if (buf->vb.state == VIDEOBUF_DONE ||
  1035. buf->vb.state == VIDEOBUF_ERROR)
  1036. return POLLIN|POLLRDNORM;
  1037. return 0;
  1038. }
  1039. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  1040. struct v4l2_capability *cap)
  1041. {
  1042. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  1043. cap->version = KERNEL_VERSION(0, 0, 5);
  1044. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1045. return 0;
  1046. }
  1047. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  1048. struct soc_camera_device *icd)
  1049. {
  1050. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1051. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1052. videobuf_queue_dma_contig_init(q,
  1053. &sh_mobile_ceu_videobuf_ops,
  1054. icd->dev.parent, &pcdev->lock,
  1055. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1056. pcdev->is_interlaced ?
  1057. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  1058. sizeof(struct sh_mobile_ceu_buffer),
  1059. icd);
  1060. }
  1061. static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
  1062. struct v4l2_control *ctrl)
  1063. {
  1064. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1065. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1066. u32 val;
  1067. switch (ctrl->id) {
  1068. case V4L2_CID_SHARPNESS:
  1069. val = ceu_read(pcdev, CLFCR);
  1070. ctrl->value = val ^ 1;
  1071. return 0;
  1072. }
  1073. return -ENOIOCTLCMD;
  1074. }
  1075. static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
  1076. struct v4l2_control *ctrl)
  1077. {
  1078. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1079. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1080. switch (ctrl->id) {
  1081. case V4L2_CID_SHARPNESS:
  1082. switch (icd->current_fmt->fourcc) {
  1083. case V4L2_PIX_FMT_NV12:
  1084. case V4L2_PIX_FMT_NV21:
  1085. case V4L2_PIX_FMT_NV16:
  1086. case V4L2_PIX_FMT_NV61:
  1087. ceu_write(pcdev, CLFCR, !ctrl->value);
  1088. return 0;
  1089. }
  1090. return -EINVAL;
  1091. }
  1092. return -ENOIOCTLCMD;
  1093. }
  1094. static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
  1095. {
  1096. .id = V4L2_CID_SHARPNESS,
  1097. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1098. .name = "Low-pass filter",
  1099. .minimum = 0,
  1100. .maximum = 1,
  1101. .step = 1,
  1102. .default_value = 0,
  1103. },
  1104. };
  1105. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  1106. .owner = THIS_MODULE,
  1107. .add = sh_mobile_ceu_add_device,
  1108. .remove = sh_mobile_ceu_remove_device,
  1109. .get_formats = sh_mobile_ceu_get_formats,
  1110. .put_formats = sh_mobile_ceu_put_formats,
  1111. .set_crop = sh_mobile_ceu_set_crop,
  1112. .set_fmt = sh_mobile_ceu_set_fmt,
  1113. .try_fmt = sh_mobile_ceu_try_fmt,
  1114. .set_ctrl = sh_mobile_ceu_set_ctrl,
  1115. .get_ctrl = sh_mobile_ceu_get_ctrl,
  1116. .reqbufs = sh_mobile_ceu_reqbufs,
  1117. .poll = sh_mobile_ceu_poll,
  1118. .querycap = sh_mobile_ceu_querycap,
  1119. .set_bus_param = sh_mobile_ceu_set_bus_param,
  1120. .init_videobuf = sh_mobile_ceu_init_videobuf,
  1121. .controls = sh_mobile_ceu_controls,
  1122. .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
  1123. };
  1124. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  1125. {
  1126. struct sh_mobile_ceu_dev *pcdev;
  1127. struct resource *res;
  1128. void __iomem *base;
  1129. unsigned int irq;
  1130. int err = 0;
  1131. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1132. irq = platform_get_irq(pdev, 0);
  1133. if (!res || !irq) {
  1134. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  1135. err = -ENODEV;
  1136. goto exit;
  1137. }
  1138. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1139. if (!pcdev) {
  1140. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1141. err = -ENOMEM;
  1142. goto exit;
  1143. }
  1144. INIT_LIST_HEAD(&pcdev->capture);
  1145. spin_lock_init(&pcdev->lock);
  1146. pcdev->pdata = pdev->dev.platform_data;
  1147. if (!pcdev->pdata) {
  1148. err = -EINVAL;
  1149. dev_err(&pdev->dev, "CEU platform data not set.\n");
  1150. goto exit_kfree;
  1151. }
  1152. base = ioremap_nocache(res->start, resource_size(res));
  1153. if (!base) {
  1154. err = -ENXIO;
  1155. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  1156. goto exit_kfree;
  1157. }
  1158. pcdev->irq = irq;
  1159. pcdev->base = base;
  1160. pcdev->video_limit = 0; /* only enabled if second resource exists */
  1161. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1162. if (res) {
  1163. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  1164. res->start,
  1165. resource_size(res),
  1166. DMA_MEMORY_MAP |
  1167. DMA_MEMORY_EXCLUSIVE);
  1168. if (!err) {
  1169. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  1170. err = -ENXIO;
  1171. goto exit_iounmap;
  1172. }
  1173. pcdev->video_limit = resource_size(res);
  1174. }
  1175. /* request irq */
  1176. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  1177. dev_name(&pdev->dev), pcdev);
  1178. if (err) {
  1179. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  1180. goto exit_release_mem;
  1181. }
  1182. pm_suspend_ignore_children(&pdev->dev, true);
  1183. pm_runtime_enable(&pdev->dev);
  1184. pm_runtime_resume(&pdev->dev);
  1185. pcdev->ici.priv = pcdev;
  1186. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  1187. pcdev->ici.nr = pdev->id;
  1188. pcdev->ici.drv_name = dev_name(&pdev->dev);
  1189. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  1190. err = soc_camera_host_register(&pcdev->ici);
  1191. if (err)
  1192. goto exit_free_irq;
  1193. return 0;
  1194. exit_free_irq:
  1195. free_irq(pcdev->irq, pcdev);
  1196. exit_release_mem:
  1197. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1198. dma_release_declared_memory(&pdev->dev);
  1199. exit_iounmap:
  1200. iounmap(base);
  1201. exit_kfree:
  1202. kfree(pcdev);
  1203. exit:
  1204. return err;
  1205. }
  1206. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  1207. {
  1208. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1209. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  1210. struct sh_mobile_ceu_dev, ici);
  1211. soc_camera_host_unregister(soc_host);
  1212. free_irq(pcdev->irq, pcdev);
  1213. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1214. dma_release_declared_memory(&pdev->dev);
  1215. iounmap(pcdev->base);
  1216. kfree(pcdev);
  1217. return 0;
  1218. }
  1219. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1220. {
  1221. /* Runtime PM callback shared between ->runtime_suspend()
  1222. * and ->runtime_resume(). Simply returns success.
  1223. *
  1224. * This driver re-initializes all registers after
  1225. * pm_runtime_get_sync() anyway so there is no need
  1226. * to save and restore registers here.
  1227. */
  1228. return 0;
  1229. }
  1230. static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1231. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1232. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1233. };
  1234. static struct platform_driver sh_mobile_ceu_driver = {
  1235. .driver = {
  1236. .name = "sh_mobile_ceu",
  1237. .pm = &sh_mobile_ceu_dev_pm_ops,
  1238. },
  1239. .probe = sh_mobile_ceu_probe,
  1240. .remove = __exit_p(sh_mobile_ceu_remove),
  1241. };
  1242. static int __init sh_mobile_ceu_init(void)
  1243. {
  1244. return platform_driver_register(&sh_mobile_ceu_driver);
  1245. }
  1246. static void __exit sh_mobile_ceu_exit(void)
  1247. {
  1248. platform_driver_unregister(&sh_mobile_ceu_driver);
  1249. }
  1250. module_init(sh_mobile_ceu_init);
  1251. module_exit(sh_mobile_ceu_exit);
  1252. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1253. MODULE_AUTHOR("Magnus Damm");
  1254. MODULE_LICENSE("GPL");
  1255. MODULE_ALIAS("platform:sh_mobile_ceu");