sh_vou.c 38 KB

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