sh_vou.c 39 KB

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