sh_vou.c 39 KB

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