sh_vou.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * SuperH Video Output Unit (VOU) driver
  3. *
  4. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/dma-mapping.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/module.h>
  23. #include <media/sh_vou.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-mediabus.h>
  28. #include <media/videobuf-dma-contig.h>
  29. /* Mirror addresses are not available for all registers */
  30. #define VOUER 0
  31. #define VOUCR 4
  32. #define VOUSTR 8
  33. #define VOUVCR 0xc
  34. #define VOUISR 0x10
  35. #define VOUBCR 0x14
  36. #define VOUDPR 0x18
  37. #define VOUDSR 0x1c
  38. #define VOUVPR 0x20
  39. #define VOUIR 0x24
  40. #define VOUSRR 0x28
  41. #define VOUMSR 0x2c
  42. #define VOUHIR 0x30
  43. #define VOUDFR 0x34
  44. #define VOUAD1R 0x38
  45. #define VOUAD2R 0x3c
  46. #define VOUAIR 0x40
  47. #define VOUSWR 0x44
  48. #define VOURCR 0x48
  49. #define VOURPR 0x50
  50. enum sh_vou_status {
  51. SH_VOU_IDLE,
  52. SH_VOU_INITIALISING,
  53. SH_VOU_RUNNING,
  54. };
  55. #define VOU_MAX_IMAGE_WIDTH 720
  56. #define VOU_MAX_IMAGE_HEIGHT 576
  57. struct sh_vou_device {
  58. struct v4l2_device v4l2_dev;
  59. struct video_device *vdev;
  60. atomic_t use_count;
  61. struct sh_vou_pdata *pdata;
  62. spinlock_t lock;
  63. void __iomem *base;
  64. /* State information */
  65. struct v4l2_pix_format pix;
  66. struct v4l2_rect rect;
  67. struct list_head queue;
  68. v4l2_std_id std;
  69. int pix_idx;
  70. struct videobuf_buffer *active;
  71. enum sh_vou_status status;
  72. struct mutex fop_lock;
  73. };
  74. struct sh_vou_file {
  75. struct videobuf_queue vbq;
  76. };
  77. /* Register access routines for sides A, B and mirror addresses */
  78. static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
  79. u32 value)
  80. {
  81. __raw_writel(value, vou_dev->base + reg);
  82. }
  83. static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
  84. u32 value)
  85. {
  86. __raw_writel(value, vou_dev->base + reg);
  87. __raw_writel(value, vou_dev->base + reg + 0x1000);
  88. }
  89. static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
  90. u32 value)
  91. {
  92. __raw_writel(value, vou_dev->base + reg + 0x2000);
  93. }
  94. static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
  95. {
  96. return __raw_readl(vou_dev->base + reg);
  97. }
  98. static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
  99. u32 value, u32 mask)
  100. {
  101. u32 old = __raw_readl(vou_dev->base + reg);
  102. value = (value & mask) | (old & ~mask);
  103. __raw_writel(value, vou_dev->base + reg);
  104. }
  105. static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
  106. u32 value, u32 mask)
  107. {
  108. sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
  109. }
  110. static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
  111. u32 value, u32 mask)
  112. {
  113. sh_vou_reg_a_set(vou_dev, reg, value, mask);
  114. sh_vou_reg_b_set(vou_dev, reg, value, mask);
  115. }
  116. struct sh_vou_fmt {
  117. u32 pfmt;
  118. char *desc;
  119. unsigned char bpp;
  120. unsigned char rgb;
  121. unsigned char yf;
  122. unsigned char pkf;
  123. };
  124. /* Further pixel formats can be added */
  125. static struct sh_vou_fmt vou_fmt[] = {
  126. {
  127. .pfmt = V4L2_PIX_FMT_NV12,
  128. .bpp = 12,
  129. .desc = "YVU420 planar",
  130. .yf = 0,
  131. .rgb = 0,
  132. },
  133. {
  134. .pfmt = V4L2_PIX_FMT_NV16,
  135. .bpp = 16,
  136. .desc = "YVYU planar",
  137. .yf = 1,
  138. .rgb = 0,
  139. },
  140. {
  141. .pfmt = V4L2_PIX_FMT_RGB24,
  142. .bpp = 24,
  143. .desc = "RGB24",
  144. .pkf = 2,
  145. .rgb = 1,
  146. },
  147. {
  148. .pfmt = V4L2_PIX_FMT_RGB565,
  149. .bpp = 16,
  150. .desc = "RGB565",
  151. .pkf = 3,
  152. .rgb = 1,
  153. },
  154. {
  155. .pfmt = V4L2_PIX_FMT_RGB565X,
  156. .bpp = 16,
  157. .desc = "RGB565 byteswapped",
  158. .pkf = 3,
  159. .rgb = 1,
  160. },
  161. };
  162. static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
  163. struct videobuf_buffer *vb)
  164. {
  165. dma_addr_t addr1, addr2;
  166. addr1 = videobuf_to_dma_contig(vb);
  167. switch (vou_dev->pix.pixelformat) {
  168. case V4L2_PIX_FMT_NV12:
  169. case V4L2_PIX_FMT_NV16:
  170. addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
  171. break;
  172. default:
  173. addr2 = 0;
  174. }
  175. sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
  176. sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
  177. }
  178. static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
  179. struct videobuf_buffer *vb)
  180. {
  181. unsigned int row_coeff;
  182. #ifdef __LITTLE_ENDIAN
  183. u32 dataswap = 7;
  184. #else
  185. u32 dataswap = 0;
  186. #endif
  187. switch (vou_dev->pix.pixelformat) {
  188. case V4L2_PIX_FMT_NV12:
  189. case V4L2_PIX_FMT_NV16:
  190. row_coeff = 1;
  191. break;
  192. case V4L2_PIX_FMT_RGB565:
  193. dataswap ^= 1;
  194. case V4L2_PIX_FMT_RGB565X:
  195. row_coeff = 2;
  196. break;
  197. case V4L2_PIX_FMT_RGB24:
  198. row_coeff = 3;
  199. break;
  200. }
  201. sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
  202. sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
  203. sh_vou_schedule_next(vou_dev, vb);
  204. }
  205. static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  206. {
  207. BUG_ON(in_interrupt());
  208. /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
  209. videobuf_waiton(vq, vb, 0, 0);
  210. videobuf_dma_contig_free(vq, vb);
  211. vb->state = VIDEOBUF_NEEDS_INIT;
  212. }
  213. /* Locking: caller holds fop_lock mutex */
  214. static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  215. unsigned int *size)
  216. {
  217. struct video_device *vdev = vq->priv_data;
  218. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  219. *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
  220. vou_dev->pix.height / 8;
  221. if (*count < 2)
  222. *count = 2;
  223. /* Taking into account maximum frame size, *count will stay >= 2 */
  224. if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
  225. *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
  226. dev_dbg(vq->dev, "%s(): count=%d, size=%d\n", __func__, *count, *size);
  227. return 0;
  228. }
  229. /* Locking: caller holds fop_lock mutex */
  230. static int sh_vou_buf_prepare(struct videobuf_queue *vq,
  231. struct videobuf_buffer *vb,
  232. enum v4l2_field field)
  233. {
  234. struct video_device *vdev = vq->priv_data;
  235. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  236. struct v4l2_pix_format *pix = &vou_dev->pix;
  237. int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
  238. int ret;
  239. dev_dbg(vq->dev, "%s()\n", __func__);
  240. if (vb->width != pix->width ||
  241. vb->height != pix->height ||
  242. vb->field != pix->field) {
  243. vb->width = pix->width;
  244. vb->height = pix->height;
  245. vb->field = field;
  246. if (vb->state != VIDEOBUF_NEEDS_INIT)
  247. free_buffer(vq, vb);
  248. }
  249. vb->size = vb->height * bytes_per_line;
  250. if (vb->baddr && vb->bsize < vb->size) {
  251. /* User buffer too small */
  252. dev_warn(vq->dev, "User buffer too small: [%u] @ %lx\n",
  253. vb->bsize, vb->baddr);
  254. return -EINVAL;
  255. }
  256. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  257. ret = videobuf_iolock(vq, vb, NULL);
  258. if (ret < 0) {
  259. dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
  260. vb->memory, ret);
  261. return ret;
  262. }
  263. vb->state = VIDEOBUF_PREPARED;
  264. }
  265. dev_dbg(vq->dev,
  266. "%s(): fmt #%d, %u bytes per line, phys 0x%x, type %d, state %d\n",
  267. __func__, vou_dev->pix_idx, bytes_per_line,
  268. videobuf_to_dma_contig(vb), vb->memory, vb->state);
  269. return 0;
  270. }
  271. /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
  272. static void sh_vou_buf_queue(struct videobuf_queue *vq,
  273. struct videobuf_buffer *vb)
  274. {
  275. struct video_device *vdev = vq->priv_data;
  276. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  277. dev_dbg(vq->dev, "%s()\n", __func__);
  278. vb->state = VIDEOBUF_QUEUED;
  279. list_add_tail(&vb->queue, &vou_dev->queue);
  280. if (vou_dev->status == SH_VOU_RUNNING) {
  281. return;
  282. } else if (!vou_dev->active) {
  283. vou_dev->active = vb;
  284. /* Start from side A: we use mirror addresses, so, set B */
  285. sh_vou_reg_a_write(vou_dev, VOURPR, 1);
  286. dev_dbg(vq->dev, "%s: first buffer status 0x%x\n", __func__,
  287. sh_vou_reg_a_read(vou_dev, VOUSTR));
  288. sh_vou_schedule_next(vou_dev, vb);
  289. /* Only activate VOU after the second buffer */
  290. } else if (vou_dev->active->queue.next == &vb->queue) {
  291. /* Second buffer - initialise register side B */
  292. sh_vou_reg_a_write(vou_dev, VOURPR, 0);
  293. sh_vou_stream_start(vou_dev, vb);
  294. /* Register side switching with frame VSYNC */
  295. sh_vou_reg_a_write(vou_dev, VOURCR, 5);
  296. dev_dbg(vq->dev, "%s: second buffer status 0x%x\n", __func__,
  297. sh_vou_reg_a_read(vou_dev, VOUSTR));
  298. /* Enable End-of-Frame (VSYNC) interrupts */
  299. sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
  300. /* Two buffers on the queue - activate the hardware */
  301. vou_dev->status = SH_VOU_RUNNING;
  302. sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
  303. }
  304. }
  305. static void sh_vou_buf_release(struct videobuf_queue *vq,
  306. struct videobuf_buffer *vb)
  307. {
  308. struct video_device *vdev = vq->priv_data;
  309. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  310. unsigned long flags;
  311. dev_dbg(vq->dev, "%s()\n", __func__);
  312. spin_lock_irqsave(&vou_dev->lock, flags);
  313. if (vou_dev->active == vb) {
  314. /* disable output */
  315. sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
  316. /* ...but the current frame will complete */
  317. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
  318. vou_dev->active = NULL;
  319. }
  320. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
  321. vb->state = VIDEOBUF_ERROR;
  322. list_del(&vb->queue);
  323. }
  324. spin_unlock_irqrestore(&vou_dev->lock, flags);
  325. free_buffer(vq, vb);
  326. }
  327. static struct videobuf_queue_ops sh_vou_video_qops = {
  328. .buf_setup = sh_vou_buf_setup,
  329. .buf_prepare = sh_vou_buf_prepare,
  330. .buf_queue = sh_vou_buf_queue,
  331. .buf_release = sh_vou_buf_release,
  332. };
  333. /* Video IOCTLs */
  334. static int sh_vou_querycap(struct file *file, void *priv,
  335. struct v4l2_capability *cap)
  336. {
  337. struct sh_vou_file *vou_file = priv;
  338. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  339. strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
  340. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  341. return 0;
  342. }
  343. /* Enumerate formats, that the device can accept from the user */
  344. static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
  345. struct v4l2_fmtdesc *fmt)
  346. {
  347. struct sh_vou_file *vou_file = priv;
  348. if (fmt->index >= ARRAY_SIZE(vou_fmt))
  349. return -EINVAL;
  350. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  351. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  352. strlcpy(fmt->description, vou_fmt[fmt->index].desc,
  353. sizeof(fmt->description));
  354. fmt->pixelformat = vou_fmt[fmt->index].pfmt;
  355. return 0;
  356. }
  357. static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
  358. struct v4l2_format *fmt)
  359. {
  360. struct video_device *vdev = video_devdata(file);
  361. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  362. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  363. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  364. fmt->fmt.pix = vou_dev->pix;
  365. return 0;
  366. }
  367. static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
  368. static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
  369. static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
  370. static const unsigned char vou_scale_v_num[] = {1, 2, 4};
  371. static const unsigned char vou_scale_v_den[] = {1, 1, 1};
  372. static const unsigned char vou_scale_v_fld[] = {0, 1};
  373. static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
  374. int pix_idx, int w_idx, int h_idx)
  375. {
  376. struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
  377. unsigned int black_left, black_top, width_max, height_max,
  378. frame_in_height, frame_out_height, frame_out_top;
  379. struct v4l2_rect *rect = &vou_dev->rect;
  380. struct v4l2_pix_format *pix = &vou_dev->pix;
  381. u32 vouvcr = 0, dsr_h, dsr_v;
  382. if (vou_dev->std & V4L2_STD_525_60) {
  383. width_max = 858;
  384. height_max = 262;
  385. } else {
  386. width_max = 864;
  387. height_max = 312;
  388. }
  389. frame_in_height = pix->height / 2;
  390. frame_out_height = rect->height / 2;
  391. frame_out_top = rect->top / 2;
  392. /*
  393. * Cropping scheme: max useful image is 720x480, and the total video
  394. * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
  395. * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
  396. * of which the first 33 / 25 clocks HSYNC must be held active. This
  397. * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
  398. * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
  399. * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
  400. * beyond DSR, specified on the left and top by the VPR register "black
  401. * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
  402. * at 138 / 144 : 20, because that's the HSYNC timing, that our first
  403. * client requires, and that's exactly what leaves us 720 pixels for the
  404. * image; we leave VPR[VVP] at default 20 for now, because the client
  405. * doesn't seem to have any special requirements for it. Otherwise we
  406. * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
  407. * the selected standard, and DPR and DSR are selected according to
  408. * cropping. Q: how does the client detect the first valid line? Does
  409. * HSYNC stay inactive during invalid (black) lines?
  410. */
  411. black_left = width_max - VOU_MAX_IMAGE_WIDTH;
  412. black_top = 20;
  413. dsr_h = rect->width + rect->left;
  414. dsr_v = frame_out_height + frame_out_top;
  415. dev_dbg(vou_dev->v4l2_dev.dev,
  416. "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
  417. pix->width, frame_in_height, black_left, black_top,
  418. rect->left, frame_out_top, dsr_h, dsr_v);
  419. /* VOUISR height - half of a frame height in frame mode */
  420. sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
  421. sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
  422. sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
  423. sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
  424. /*
  425. * if necessary, we could set VOUHIR to
  426. * max(black_left + dsr_h, width_max) here
  427. */
  428. if (w_idx)
  429. vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
  430. if (h_idx)
  431. vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
  432. dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
  433. /* To produce a colour bar for testing set bit 23 of VOUVCR */
  434. sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
  435. sh_vou_reg_ab_write(vou_dev, VOUDFR,
  436. fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
  437. }
  438. struct sh_vou_geometry {
  439. struct v4l2_rect output;
  440. unsigned int in_width;
  441. unsigned int in_height;
  442. int scale_idx_h;
  443. int scale_idx_v;
  444. };
  445. /*
  446. * Find input geometry, that we can use to produce output, closest to the
  447. * requested rectangle, using VOU scaling
  448. */
  449. static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
  450. {
  451. /* The compiler cannot know, that best and idx will indeed be set */
  452. unsigned int best_err = UINT_MAX, best = 0, img_height_max;
  453. int i, idx = 0;
  454. if (std & V4L2_STD_525_60)
  455. img_height_max = 480;
  456. else
  457. img_height_max = 576;
  458. /* Image width must be a multiple of 4 */
  459. v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  460. &geo->in_height, 0, img_height_max, 1, 0);
  461. /* Select scales to come as close as possible to the output image */
  462. for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
  463. unsigned int err;
  464. unsigned int found = geo->output.width * vou_scale_h_den[i] /
  465. vou_scale_h_num[i];
  466. if (found > VOU_MAX_IMAGE_WIDTH)
  467. /* scales increase */
  468. break;
  469. err = abs(found - geo->in_width);
  470. if (err < best_err) {
  471. best_err = err;
  472. idx = i;
  473. best = found;
  474. }
  475. if (!err)
  476. break;
  477. }
  478. geo->in_width = best;
  479. geo->scale_idx_h = idx;
  480. best_err = UINT_MAX;
  481. /* This loop can be replaced with one division */
  482. for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
  483. unsigned int err;
  484. unsigned int found = geo->output.height * vou_scale_v_den[i] /
  485. vou_scale_v_num[i];
  486. if (found > img_height_max)
  487. /* scales increase */
  488. break;
  489. err = abs(found - geo->in_height);
  490. if (err < best_err) {
  491. best_err = err;
  492. idx = i;
  493. best = found;
  494. }
  495. if (!err)
  496. break;
  497. }
  498. geo->in_height = best;
  499. geo->scale_idx_v = idx;
  500. }
  501. /*
  502. * Find output geometry, that we can produce, using VOU scaling, closest to
  503. * the requested rectangle
  504. */
  505. static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
  506. {
  507. unsigned int best_err = UINT_MAX, best, width_max, height_max,
  508. img_height_max;
  509. int i, idx;
  510. if (std & V4L2_STD_525_60) {
  511. width_max = 858;
  512. height_max = 262 * 2;
  513. img_height_max = 480;
  514. } else {
  515. width_max = 864;
  516. height_max = 312 * 2;
  517. img_height_max = 576;
  518. }
  519. /* Select scales to come as close as possible to the output image */
  520. for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
  521. unsigned int err;
  522. unsigned int found = geo->in_width * vou_scale_h_num[i] /
  523. vou_scale_h_den[i];
  524. if (found > VOU_MAX_IMAGE_WIDTH)
  525. /* scales increase */
  526. break;
  527. err = abs(found - geo->output.width);
  528. if (err < best_err) {
  529. best_err = err;
  530. idx = i;
  531. best = found;
  532. }
  533. if (!err)
  534. break;
  535. }
  536. geo->output.width = best;
  537. geo->scale_idx_h = idx;
  538. if (geo->output.left + best > width_max)
  539. geo->output.left = width_max - best;
  540. pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
  541. vou_scale_h_num[idx], vou_scale_h_den[idx], best);
  542. best_err = UINT_MAX;
  543. /* This loop can be replaced with one division */
  544. for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
  545. unsigned int err;
  546. unsigned int found = geo->in_height * vou_scale_v_num[i] /
  547. vou_scale_v_den[i];
  548. if (found > img_height_max)
  549. /* scales increase */
  550. break;
  551. err = abs(found - geo->output.height);
  552. if (err < best_err) {
  553. best_err = err;
  554. idx = i;
  555. best = found;
  556. }
  557. if (!err)
  558. break;
  559. }
  560. geo->output.height = best;
  561. geo->scale_idx_v = idx;
  562. if (geo->output.top + best > height_max)
  563. geo->output.top = height_max - best;
  564. pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
  565. vou_scale_v_num[idx], vou_scale_v_den[idx], best);
  566. }
  567. static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
  568. struct v4l2_format *fmt)
  569. {
  570. struct video_device *vdev = video_devdata(file);
  571. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  572. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  573. unsigned int img_height_max;
  574. int pix_idx;
  575. struct sh_vou_geometry geo;
  576. struct v4l2_mbus_framefmt mbfmt = {
  577. /* Revisit: is this the correct code? */
  578. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  579. .field = V4L2_FIELD_INTERLACED,
  580. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  581. };
  582. int ret;
  583. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  584. vou_dev->rect.width, vou_dev->rect.height,
  585. pix->width, pix->height);
  586. if (pix->field == V4L2_FIELD_ANY)
  587. pix->field = V4L2_FIELD_NONE;
  588. if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  589. pix->field != V4L2_FIELD_NONE)
  590. return -EINVAL;
  591. for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
  592. if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
  593. break;
  594. if (pix_idx == ARRAY_SIZE(vou_fmt))
  595. return -EINVAL;
  596. if (vou_dev->std & V4L2_STD_525_60)
  597. img_height_max = 480;
  598. else
  599. img_height_max = 576;
  600. /* Image width must be a multiple of 4 */
  601. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  602. &pix->height, 0, img_height_max, 1, 0);
  603. geo.in_width = pix->width;
  604. geo.in_height = pix->height;
  605. geo.output = vou_dev->rect;
  606. vou_adjust_output(&geo, vou_dev->std);
  607. mbfmt.width = geo.output.width;
  608. mbfmt.height = geo.output.height;
  609. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  610. s_mbus_fmt, &mbfmt);
  611. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  612. if (ret < 0)
  613. return ret;
  614. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  615. geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
  616. /* Sanity checks */
  617. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  618. (unsigned)mbfmt.height > img_height_max ||
  619. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  620. return -EIO;
  621. if (mbfmt.width != geo.output.width ||
  622. mbfmt.height != geo.output.height) {
  623. geo.output.width = mbfmt.width;
  624. geo.output.height = mbfmt.height;
  625. vou_adjust_input(&geo, vou_dev->std);
  626. }
  627. /* We tried to preserve output rectangle, but it could have changed */
  628. vou_dev->rect = geo.output;
  629. pix->width = geo.in_width;
  630. pix->height = geo.in_height;
  631. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
  632. pix->width, pix->height);
  633. vou_dev->pix_idx = pix_idx;
  634. vou_dev->pix = *pix;
  635. sh_vou_configure_geometry(vou_dev, pix_idx,
  636. geo.scale_idx_h, geo.scale_idx_v);
  637. return 0;
  638. }
  639. static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
  640. struct v4l2_format *fmt)
  641. {
  642. struct sh_vou_file *vou_file = priv;
  643. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  644. int i;
  645. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  646. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  647. pix->field = V4L2_FIELD_NONE;
  648. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  649. &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  650. for (i = 0; ARRAY_SIZE(vou_fmt); i++)
  651. if (vou_fmt[i].pfmt == pix->pixelformat)
  652. return 0;
  653. pix->pixelformat = vou_fmt[0].pfmt;
  654. return 0;
  655. }
  656. static int sh_vou_reqbufs(struct file *file, void *priv,
  657. struct v4l2_requestbuffers *req)
  658. {
  659. struct sh_vou_file *vou_file = priv;
  660. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  661. if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  662. return -EINVAL;
  663. return videobuf_reqbufs(&vou_file->vbq, req);
  664. }
  665. static int sh_vou_querybuf(struct file *file, void *priv,
  666. struct v4l2_buffer *b)
  667. {
  668. struct sh_vou_file *vou_file = priv;
  669. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  670. return videobuf_querybuf(&vou_file->vbq, b);
  671. }
  672. static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  673. {
  674. struct sh_vou_file *vou_file = priv;
  675. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  676. return videobuf_qbuf(&vou_file->vbq, b);
  677. }
  678. static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  679. {
  680. struct sh_vou_file *vou_file = priv;
  681. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  682. return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
  683. }
  684. static int sh_vou_streamon(struct file *file, void *priv,
  685. enum v4l2_buf_type buftype)
  686. {
  687. struct video_device *vdev = video_devdata(file);
  688. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  689. struct sh_vou_file *vou_file = priv;
  690. int ret;
  691. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  692. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
  693. video, s_stream, 1);
  694. if (ret < 0 && ret != -ENOIOCTLCMD)
  695. return ret;
  696. /* This calls our .buf_queue() (== sh_vou_buf_queue) */
  697. return videobuf_streamon(&vou_file->vbq);
  698. }
  699. static int sh_vou_streamoff(struct file *file, void *priv,
  700. enum v4l2_buf_type buftype)
  701. {
  702. struct video_device *vdev = video_devdata(file);
  703. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  704. struct sh_vou_file *vou_file = priv;
  705. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  706. /*
  707. * This calls buf_release from host driver's videobuf_queue_ops for all
  708. * remaining buffers. When the last buffer is freed, stop streaming
  709. */
  710. videobuf_streamoff(&vou_file->vbq);
  711. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
  712. return 0;
  713. }
  714. static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
  715. {
  716. switch (bus_fmt) {
  717. default:
  718. pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
  719. __func__, bus_fmt);
  720. case SH_VOU_BUS_8BIT:
  721. return 1;
  722. case SH_VOU_BUS_16BIT:
  723. return 0;
  724. case SH_VOU_BUS_BT656:
  725. return 3;
  726. }
  727. }
  728. static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  729. {
  730. struct video_device *vdev = video_devdata(file);
  731. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  732. int ret;
  733. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, *std_id);
  734. if (*std_id & ~vdev->tvnorms)
  735. return -EINVAL;
  736. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  737. s_std_output, *std_id);
  738. /* Shall we continue, if the subdev doesn't support .s_std_output()? */
  739. if (ret < 0 && ret != -ENOIOCTLCMD)
  740. return ret;
  741. if (*std_id & V4L2_STD_525_60)
  742. sh_vou_reg_ab_set(vou_dev, VOUCR,
  743. sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
  744. else
  745. sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
  746. vou_dev->std = *std_id;
  747. return 0;
  748. }
  749. static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
  750. {
  751. struct video_device *vdev = video_devdata(file);
  752. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  753. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  754. *std = vou_dev->std;
  755. return 0;
  756. }
  757. static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
  758. {
  759. struct video_device *vdev = video_devdata(file);
  760. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  761. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  762. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  763. a->c = vou_dev->rect;
  764. return 0;
  765. }
  766. /* Assume a dull encoder, do all the work ourselves. */
  767. static int sh_vou_s_crop(struct file *file, void *fh, const struct v4l2_crop *a)
  768. {
  769. struct v4l2_crop a_writable = *a;
  770. struct video_device *vdev = video_devdata(file);
  771. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  772. struct v4l2_rect *rect = &a_writable.c;
  773. struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
  774. struct v4l2_pix_format *pix = &vou_dev->pix;
  775. struct sh_vou_geometry geo;
  776. struct v4l2_mbus_framefmt mbfmt = {
  777. /* Revisit: is this the correct code? */
  778. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  779. .field = V4L2_FIELD_INTERLACED,
  780. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  781. };
  782. unsigned int img_height_max;
  783. int ret;
  784. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
  785. rect->width, rect->height, rect->left, rect->top);
  786. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  787. return -EINVAL;
  788. if (vou_dev->std & V4L2_STD_525_60)
  789. img_height_max = 480;
  790. else
  791. img_height_max = 576;
  792. v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  793. &rect->height, 0, img_height_max, 1, 0);
  794. if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
  795. rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
  796. if (rect->height + rect->top > img_height_max)
  797. rect->top = img_height_max - rect->height;
  798. geo.output = *rect;
  799. geo.in_width = pix->width;
  800. geo.in_height = pix->height;
  801. /* Configure the encoder one-to-one, position at 0, ignore errors */
  802. sd_crop.c.width = geo.output.width;
  803. sd_crop.c.height = geo.output.height;
  804. /*
  805. * We first issue a S_CROP, so that the subsequent S_FMT delivers the
  806. * final encoder configuration.
  807. */
  808. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  809. s_crop, &sd_crop);
  810. mbfmt.width = geo.output.width;
  811. mbfmt.height = geo.output.height;
  812. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  813. s_mbus_fmt, &mbfmt);
  814. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  815. if (ret < 0)
  816. return ret;
  817. /* Sanity checks */
  818. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  819. (unsigned)mbfmt.height > img_height_max ||
  820. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  821. return -EIO;
  822. geo.output.width = mbfmt.width;
  823. geo.output.height = mbfmt.height;
  824. /*
  825. * No down-scaling. According to the API, current call has precedence:
  826. * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
  827. */
  828. vou_adjust_input(&geo, vou_dev->std);
  829. /* We tried to preserve output rectangle, but it could have changed */
  830. vou_dev->rect = geo.output;
  831. pix->width = geo.in_width;
  832. pix->height = geo.in_height;
  833. sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
  834. geo.scale_idx_h, geo.scale_idx_v);
  835. return 0;
  836. }
  837. /*
  838. * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
  839. * is the initial register values, height takes the interlaced format into
  840. * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
  841. * actually only meaningfully contain values <= 720 and <= 240 respectively, and
  842. * not <= 864 and <= 312.
  843. */
  844. static int sh_vou_cropcap(struct file *file, void *priv,
  845. struct v4l2_cropcap *a)
  846. {
  847. struct sh_vou_file *vou_file = priv;
  848. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  849. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  850. a->bounds.left = 0;
  851. a->bounds.top = 0;
  852. a->bounds.width = VOU_MAX_IMAGE_WIDTH;
  853. a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
  854. /* Default = max, set VOUDPR = 0, which is not hardware default */
  855. a->defrect.left = 0;
  856. a->defrect.top = 0;
  857. a->defrect.width = VOU_MAX_IMAGE_WIDTH;
  858. a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
  859. a->pixelaspect.numerator = 1;
  860. a->pixelaspect.denominator = 1;
  861. return 0;
  862. }
  863. static irqreturn_t sh_vou_isr(int irq, void *dev_id)
  864. {
  865. struct sh_vou_device *vou_dev = dev_id;
  866. static unsigned long j;
  867. struct videobuf_buffer *vb;
  868. static int cnt;
  869. static int side;
  870. u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
  871. u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
  872. if (!(irq_status & 0x300)) {
  873. if (printk_timed_ratelimit(&j, 500))
  874. dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
  875. irq_status);
  876. return IRQ_NONE;
  877. }
  878. spin_lock(&vou_dev->lock);
  879. if (!vou_dev->active || list_empty(&vou_dev->queue)) {
  880. if (printk_timed_ratelimit(&j, 500))
  881. dev_warn(vou_dev->v4l2_dev.dev,
  882. "IRQ without active buffer: %x!\n", irq_status);
  883. /* Just ack: buf_release will disable further interrupts */
  884. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
  885. spin_unlock(&vou_dev->lock);
  886. return IRQ_HANDLED;
  887. }
  888. masked = ~(0x300 & irq_status) & irq_status & 0x30304;
  889. dev_dbg(vou_dev->v4l2_dev.dev,
  890. "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
  891. irq_status, masked, vou_status, cnt);
  892. cnt++;
  893. side = vou_status & 0x10000;
  894. /* Clear only set interrupts */
  895. sh_vou_reg_a_write(vou_dev, VOUIR, masked);
  896. vb = vou_dev->active;
  897. list_del(&vb->queue);
  898. vb->state = VIDEOBUF_DONE;
  899. do_gettimeofday(&vb->ts);
  900. vb->field_count++;
  901. wake_up(&vb->done);
  902. if (list_empty(&vou_dev->queue)) {
  903. /* Stop VOU */
  904. dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
  905. __func__, cnt);
  906. sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
  907. vou_dev->active = NULL;
  908. vou_dev->status = SH_VOU_INITIALISING;
  909. /* Disable End-of-Frame (VSYNC) interrupts */
  910. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
  911. spin_unlock(&vou_dev->lock);
  912. return IRQ_HANDLED;
  913. }
  914. vou_dev->active = list_entry(vou_dev->queue.next,
  915. struct videobuf_buffer, queue);
  916. if (vou_dev->active->queue.next != &vou_dev->queue) {
  917. struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
  918. struct videobuf_buffer, queue);
  919. sh_vou_schedule_next(vou_dev, new);
  920. }
  921. spin_unlock(&vou_dev->lock);
  922. return IRQ_HANDLED;
  923. }
  924. static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
  925. {
  926. struct sh_vou_pdata *pdata = vou_dev->pdata;
  927. u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
  928. int i = 100;
  929. /* Disable all IRQs */
  930. sh_vou_reg_a_write(vou_dev, VOUIR, 0);
  931. /* Reset VOU interfaces - registers unaffected */
  932. sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
  933. while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
  934. udelay(1);
  935. if (!i)
  936. return -ETIMEDOUT;
  937. dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
  938. if (pdata->flags & SH_VOU_PCLK_FALLING)
  939. voucr |= 1 << 28;
  940. if (pdata->flags & SH_VOU_HSYNC_LOW)
  941. voucr |= 1 << 27;
  942. if (pdata->flags & SH_VOU_VSYNC_LOW)
  943. voucr |= 1 << 26;
  944. sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
  945. /* Manual register side switching at first */
  946. sh_vou_reg_a_write(vou_dev, VOURCR, 4);
  947. /* Default - fixed HSYNC length, can be made configurable is required */
  948. sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
  949. return 0;
  950. }
  951. /* File operations */
  952. static int sh_vou_open(struct file *file)
  953. {
  954. struct video_device *vdev = video_devdata(file);
  955. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  956. struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
  957. GFP_KERNEL);
  958. if (!vou_file)
  959. return -ENOMEM;
  960. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  961. file->private_data = vou_file;
  962. if (mutex_lock_interruptible(&vou_dev->fop_lock))
  963. return -ERESTARTSYS;
  964. if (atomic_inc_return(&vou_dev->use_count) == 1) {
  965. int ret;
  966. /* First open */
  967. vou_dev->status = SH_VOU_INITIALISING;
  968. pm_runtime_get_sync(vdev->v4l2_dev->dev);
  969. ret = sh_vou_hw_init(vou_dev);
  970. if (ret < 0) {
  971. atomic_dec(&vou_dev->use_count);
  972. pm_runtime_put(vdev->v4l2_dev->dev);
  973. vou_dev->status = SH_VOU_IDLE;
  974. mutex_unlock(&vou_dev->fop_lock);
  975. return ret;
  976. }
  977. }
  978. videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
  979. vou_dev->v4l2_dev.dev, &vou_dev->lock,
  980. V4L2_BUF_TYPE_VIDEO_OUTPUT,
  981. V4L2_FIELD_NONE,
  982. sizeof(struct videobuf_buffer), vdev,
  983. &vou_dev->fop_lock);
  984. mutex_unlock(&vou_dev->fop_lock);
  985. return 0;
  986. }
  987. static int sh_vou_release(struct file *file)
  988. {
  989. struct video_device *vdev = video_devdata(file);
  990. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  991. struct sh_vou_file *vou_file = file->private_data;
  992. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  993. if (!atomic_dec_return(&vou_dev->use_count)) {
  994. mutex_lock(&vou_dev->fop_lock);
  995. /* Last close */
  996. vou_dev->status = SH_VOU_IDLE;
  997. sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
  998. pm_runtime_put(vdev->v4l2_dev->dev);
  999. mutex_unlock(&vou_dev->fop_lock);
  1000. }
  1001. file->private_data = NULL;
  1002. kfree(vou_file);
  1003. return 0;
  1004. }
  1005. static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
  1006. {
  1007. struct video_device *vdev = video_devdata(file);
  1008. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1009. struct sh_vou_file *vou_file = file->private_data;
  1010. int ret;
  1011. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  1012. if (mutex_lock_interruptible(&vou_dev->fop_lock))
  1013. return -ERESTARTSYS;
  1014. ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
  1015. mutex_unlock(&vou_dev->fop_lock);
  1016. return ret;
  1017. }
  1018. static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
  1019. {
  1020. struct video_device *vdev = video_devdata(file);
  1021. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1022. struct sh_vou_file *vou_file = file->private_data;
  1023. unsigned int res;
  1024. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  1025. mutex_lock(&vou_dev->fop_lock);
  1026. res = videobuf_poll_stream(file, &vou_file->vbq, wait);
  1027. mutex_unlock(&vou_dev->fop_lock);
  1028. return res;
  1029. }
  1030. static int sh_vou_g_chip_ident(struct file *file, void *fh,
  1031. struct v4l2_dbg_chip_ident *id)
  1032. {
  1033. struct video_device *vdev = video_devdata(file);
  1034. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1035. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_chip_ident, id);
  1036. }
  1037. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1038. static int sh_vou_g_register(struct file *file, void *fh,
  1039. struct v4l2_dbg_register *reg)
  1040. {
  1041. struct video_device *vdev = video_devdata(file);
  1042. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1043. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_register, reg);
  1044. }
  1045. static int sh_vou_s_register(struct file *file, void *fh,
  1046. struct v4l2_dbg_register *reg)
  1047. {
  1048. struct video_device *vdev = video_devdata(file);
  1049. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1050. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, s_register, reg);
  1051. }
  1052. #endif
  1053. /* sh_vou display ioctl operations */
  1054. static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
  1055. .vidioc_querycap = sh_vou_querycap,
  1056. .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
  1057. .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
  1058. .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
  1059. .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
  1060. .vidioc_reqbufs = sh_vou_reqbufs,
  1061. .vidioc_querybuf = sh_vou_querybuf,
  1062. .vidioc_qbuf = sh_vou_qbuf,
  1063. .vidioc_dqbuf = sh_vou_dqbuf,
  1064. .vidioc_streamon = sh_vou_streamon,
  1065. .vidioc_streamoff = sh_vou_streamoff,
  1066. .vidioc_s_std = sh_vou_s_std,
  1067. .vidioc_g_std = sh_vou_g_std,
  1068. .vidioc_cropcap = sh_vou_cropcap,
  1069. .vidioc_g_crop = sh_vou_g_crop,
  1070. .vidioc_s_crop = sh_vou_s_crop,
  1071. .vidioc_g_chip_ident = sh_vou_g_chip_ident,
  1072. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1073. .vidioc_g_register = sh_vou_g_register,
  1074. .vidioc_s_register = sh_vou_s_register,
  1075. #endif
  1076. };
  1077. static const struct v4l2_file_operations sh_vou_fops = {
  1078. .owner = THIS_MODULE,
  1079. .open = sh_vou_open,
  1080. .release = sh_vou_release,
  1081. .unlocked_ioctl = video_ioctl2,
  1082. .mmap = sh_vou_mmap,
  1083. .poll = sh_vou_poll,
  1084. };
  1085. static const struct video_device sh_vou_video_template = {
  1086. .name = "sh_vou",
  1087. .fops = &sh_vou_fops,
  1088. .ioctl_ops = &sh_vou_ioctl_ops,
  1089. .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
  1090. .current_norm = V4L2_STD_NTSC_M,
  1091. .vfl_dir = VFL_DIR_TX,
  1092. };
  1093. static int sh_vou_probe(struct platform_device *pdev)
  1094. {
  1095. struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
  1096. struct v4l2_rect *rect;
  1097. struct v4l2_pix_format *pix;
  1098. struct i2c_adapter *i2c_adap;
  1099. struct video_device *vdev;
  1100. struct sh_vou_device *vou_dev;
  1101. struct resource *reg_res, *region;
  1102. struct v4l2_subdev *subdev;
  1103. int irq, ret;
  1104. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1105. irq = platform_get_irq(pdev, 0);
  1106. if (!vou_pdata || !reg_res || irq <= 0) {
  1107. dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
  1108. return -ENODEV;
  1109. }
  1110. vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
  1111. if (!vou_dev)
  1112. return -ENOMEM;
  1113. INIT_LIST_HEAD(&vou_dev->queue);
  1114. spin_lock_init(&vou_dev->lock);
  1115. mutex_init(&vou_dev->fop_lock);
  1116. atomic_set(&vou_dev->use_count, 0);
  1117. vou_dev->pdata = vou_pdata;
  1118. vou_dev->status = SH_VOU_IDLE;
  1119. rect = &vou_dev->rect;
  1120. pix = &vou_dev->pix;
  1121. /* Fill in defaults */
  1122. vou_dev->std = sh_vou_video_template.current_norm;
  1123. rect->left = 0;
  1124. rect->top = 0;
  1125. rect->width = VOU_MAX_IMAGE_WIDTH;
  1126. rect->height = 480;
  1127. pix->width = VOU_MAX_IMAGE_WIDTH;
  1128. pix->height = 480;
  1129. pix->pixelformat = V4L2_PIX_FMT_YVYU;
  1130. pix->field = V4L2_FIELD_NONE;
  1131. pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
  1132. pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
  1133. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1134. region = request_mem_region(reg_res->start, resource_size(reg_res),
  1135. pdev->name);
  1136. if (!region) {
  1137. dev_err(&pdev->dev, "VOU region already claimed\n");
  1138. ret = -EBUSY;
  1139. goto ereqmemreg;
  1140. }
  1141. vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
  1142. if (!vou_dev->base) {
  1143. ret = -ENOMEM;
  1144. goto emap;
  1145. }
  1146. ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
  1147. if (ret < 0)
  1148. goto ereqirq;
  1149. ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
  1150. if (ret < 0) {
  1151. dev_err(&pdev->dev, "Error registering v4l2 device\n");
  1152. goto ev4l2devreg;
  1153. }
  1154. /* Allocate memory for video device */
  1155. vdev = video_device_alloc();
  1156. if (vdev == NULL) {
  1157. ret = -ENOMEM;
  1158. goto evdevalloc;
  1159. }
  1160. *vdev = sh_vou_video_template;
  1161. if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
  1162. vdev->tvnorms |= V4L2_STD_PAL;
  1163. vdev->v4l2_dev = &vou_dev->v4l2_dev;
  1164. vdev->release = video_device_release;
  1165. vdev->lock = &vou_dev->fop_lock;
  1166. vou_dev->vdev = vdev;
  1167. video_set_drvdata(vdev, vou_dev);
  1168. pm_runtime_enable(&pdev->dev);
  1169. pm_runtime_resume(&pdev->dev);
  1170. i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
  1171. if (!i2c_adap) {
  1172. ret = -ENODEV;
  1173. goto ei2cgadap;
  1174. }
  1175. ret = sh_vou_hw_init(vou_dev);
  1176. if (ret < 0)
  1177. goto ereset;
  1178. subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
  1179. vou_pdata->board_info, NULL);
  1180. if (!subdev) {
  1181. ret = -ENOMEM;
  1182. goto ei2cnd;
  1183. }
  1184. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1185. if (ret < 0)
  1186. goto evregdev;
  1187. return 0;
  1188. evregdev:
  1189. ei2cnd:
  1190. ereset:
  1191. i2c_put_adapter(i2c_adap);
  1192. ei2cgadap:
  1193. video_device_release(vdev);
  1194. pm_runtime_disable(&pdev->dev);
  1195. evdevalloc:
  1196. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1197. ev4l2devreg:
  1198. free_irq(irq, vou_dev);
  1199. ereqirq:
  1200. iounmap(vou_dev->base);
  1201. emap:
  1202. release_mem_region(reg_res->start, resource_size(reg_res));
  1203. ereqmemreg:
  1204. kfree(vou_dev);
  1205. return ret;
  1206. }
  1207. static int sh_vou_remove(struct platform_device *pdev)
  1208. {
  1209. int irq = platform_get_irq(pdev, 0);
  1210. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1211. struct sh_vou_device *vou_dev = container_of(v4l2_dev,
  1212. struct sh_vou_device, v4l2_dev);
  1213. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  1214. struct v4l2_subdev, list);
  1215. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1216. struct resource *reg_res;
  1217. if (irq > 0)
  1218. free_irq(irq, vou_dev);
  1219. pm_runtime_disable(&pdev->dev);
  1220. video_unregister_device(vou_dev->vdev);
  1221. i2c_put_adapter(client->adapter);
  1222. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1223. iounmap(vou_dev->base);
  1224. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1225. if (reg_res)
  1226. release_mem_region(reg_res->start, resource_size(reg_res));
  1227. kfree(vou_dev);
  1228. return 0;
  1229. }
  1230. static struct platform_driver __refdata sh_vou = {
  1231. .remove = sh_vou_remove,
  1232. .driver = {
  1233. .name = "sh-vou",
  1234. .owner = THIS_MODULE,
  1235. },
  1236. };
  1237. static int __init sh_vou_init(void)
  1238. {
  1239. return platform_driver_probe(&sh_vou, sh_vou_probe);
  1240. }
  1241. static void __exit sh_vou_exit(void)
  1242. {
  1243. platform_driver_unregister(&sh_vou);
  1244. }
  1245. module_init(sh_vou_init);
  1246. module_exit(sh_vou_exit);
  1247. MODULE_DESCRIPTION("SuperH VOU driver");
  1248. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1249. MODULE_LICENSE("GPL v2");
  1250. MODULE_VERSION("0.1.0");
  1251. MODULE_ALIAS("platform:sh-vou");