sh_vou.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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 480
  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, width_max, height_max;
  453. int i, idx = 0;
  454. if (std & V4L2_STD_525_60) {
  455. width_max = 858;
  456. height_max = 262;
  457. } else {
  458. width_max = 864;
  459. height_max = 312;
  460. }
  461. /* Image width must be a multiple of 4 */
  462. v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  463. &geo->in_height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  464. /* Select scales to come as close as possible to the output image */
  465. for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
  466. unsigned int err;
  467. unsigned int found = geo->output.width * vou_scale_h_den[i] /
  468. vou_scale_h_num[i];
  469. if (found > VOU_MAX_IMAGE_WIDTH)
  470. /* scales increase */
  471. break;
  472. err = abs(found - geo->in_width);
  473. if (err < best_err) {
  474. best_err = err;
  475. idx = i;
  476. best = found;
  477. }
  478. if (!err)
  479. break;
  480. }
  481. geo->in_width = best;
  482. geo->scale_idx_h = idx;
  483. best_err = UINT_MAX;
  484. /* This loop can be replaced with one division */
  485. for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
  486. unsigned int err;
  487. unsigned int found = geo->output.height * vou_scale_v_den[i] /
  488. vou_scale_v_num[i];
  489. if (found > VOU_MAX_IMAGE_HEIGHT)
  490. /* scales increase */
  491. break;
  492. err = abs(found - geo->in_height);
  493. if (err < best_err) {
  494. best_err = err;
  495. idx = i;
  496. best = found;
  497. }
  498. if (!err)
  499. break;
  500. }
  501. geo->in_height = best;
  502. geo->scale_idx_v = idx;
  503. }
  504. /*
  505. * Find output geometry, that we can produce, using VOU scaling, closest to
  506. * the requested rectangle
  507. */
  508. static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
  509. {
  510. unsigned int best_err = UINT_MAX, best, width_max, height_max;
  511. int i, idx;
  512. if (std & V4L2_STD_525_60) {
  513. width_max = 858;
  514. height_max = 262 * 2;
  515. } else {
  516. width_max = 864;
  517. height_max = 312 * 2;
  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 > VOU_MAX_IMAGE_HEIGHT)
  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. int pix_idx;
  574. struct sh_vou_geometry geo;
  575. struct v4l2_mbus_framefmt mbfmt = {
  576. /* Revisit: is this the correct code? */
  577. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  578. .field = V4L2_FIELD_INTERLACED,
  579. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  580. };
  581. int ret;
  582. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  583. vou_dev->rect.width, vou_dev->rect.height,
  584. pix->width, pix->height);
  585. if (pix->field == V4L2_FIELD_ANY)
  586. pix->field = V4L2_FIELD_NONE;
  587. if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  588. pix->field != V4L2_FIELD_NONE)
  589. return -EINVAL;
  590. for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
  591. if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
  592. break;
  593. if (pix_idx == ARRAY_SIZE(vou_fmt))
  594. return -EINVAL;
  595. /* Image width must be a multiple of 4 */
  596. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  597. &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  598. geo.in_width = pix->width;
  599. geo.in_height = pix->height;
  600. geo.output = vou_dev->rect;
  601. vou_adjust_output(&geo, vou_dev->std);
  602. mbfmt.width = geo.output.width;
  603. mbfmt.height = geo.output.height;
  604. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  605. s_mbus_fmt, &mbfmt);
  606. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  607. if (ret < 0)
  608. return ret;
  609. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  610. geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
  611. /* Sanity checks */
  612. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  613. (unsigned)mbfmt.height > VOU_MAX_IMAGE_HEIGHT ||
  614. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  615. return -EIO;
  616. if (mbfmt.width != geo.output.width ||
  617. mbfmt.height != geo.output.height) {
  618. geo.output.width = mbfmt.width;
  619. geo.output.height = mbfmt.height;
  620. vou_adjust_input(&geo, vou_dev->std);
  621. }
  622. /* We tried to preserve output rectangle, but it could have changed */
  623. vou_dev->rect = geo.output;
  624. pix->width = geo.in_width;
  625. pix->height = geo.in_height;
  626. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
  627. pix->width, pix->height);
  628. vou_dev->pix_idx = pix_idx;
  629. vou_dev->pix = *pix;
  630. sh_vou_configure_geometry(vou_dev, pix_idx,
  631. geo.scale_idx_h, geo.scale_idx_v);
  632. return 0;
  633. }
  634. static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
  635. struct v4l2_format *fmt)
  636. {
  637. struct sh_vou_file *vou_file = priv;
  638. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  639. int i;
  640. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  641. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  642. pix->field = V4L2_FIELD_NONE;
  643. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  644. &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  645. for (i = 0; ARRAY_SIZE(vou_fmt); i++)
  646. if (vou_fmt[i].pfmt == pix->pixelformat)
  647. return 0;
  648. pix->pixelformat = vou_fmt[0].pfmt;
  649. return 0;
  650. }
  651. static int sh_vou_reqbufs(struct file *file, void *priv,
  652. struct v4l2_requestbuffers *req)
  653. {
  654. struct sh_vou_file *vou_file = priv;
  655. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  656. if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  657. return -EINVAL;
  658. return videobuf_reqbufs(&vou_file->vbq, req);
  659. }
  660. static int sh_vou_querybuf(struct file *file, void *priv,
  661. struct v4l2_buffer *b)
  662. {
  663. struct sh_vou_file *vou_file = priv;
  664. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  665. return videobuf_querybuf(&vou_file->vbq, b);
  666. }
  667. static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  668. {
  669. struct sh_vou_file *vou_file = priv;
  670. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  671. return videobuf_qbuf(&vou_file->vbq, b);
  672. }
  673. static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  674. {
  675. struct sh_vou_file *vou_file = priv;
  676. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  677. return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
  678. }
  679. static int sh_vou_streamon(struct file *file, void *priv,
  680. enum v4l2_buf_type buftype)
  681. {
  682. struct video_device *vdev = video_devdata(file);
  683. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  684. struct sh_vou_file *vou_file = priv;
  685. int ret;
  686. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  687. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
  688. video, s_stream, 1);
  689. if (ret < 0 && ret != -ENOIOCTLCMD)
  690. return ret;
  691. /* This calls our .buf_queue() (== sh_vou_buf_queue) */
  692. return videobuf_streamon(&vou_file->vbq);
  693. }
  694. static int sh_vou_streamoff(struct file *file, void *priv,
  695. enum v4l2_buf_type buftype)
  696. {
  697. struct video_device *vdev = video_devdata(file);
  698. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  699. struct sh_vou_file *vou_file = priv;
  700. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  701. /*
  702. * This calls buf_release from host driver's videobuf_queue_ops for all
  703. * remaining buffers. When the last buffer is freed, stop streaming
  704. */
  705. videobuf_streamoff(&vou_file->vbq);
  706. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
  707. return 0;
  708. }
  709. static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
  710. {
  711. switch (bus_fmt) {
  712. default:
  713. pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
  714. __func__, bus_fmt);
  715. case SH_VOU_BUS_8BIT:
  716. return 1;
  717. case SH_VOU_BUS_16BIT:
  718. return 0;
  719. case SH_VOU_BUS_BT656:
  720. return 3;
  721. }
  722. }
  723. static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  724. {
  725. struct video_device *vdev = video_devdata(file);
  726. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  727. int ret;
  728. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, *std_id);
  729. if (*std_id & ~vdev->tvnorms)
  730. return -EINVAL;
  731. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  732. s_std_output, *std_id);
  733. /* Shall we continue, if the subdev doesn't support .s_std_output()? */
  734. if (ret < 0 && ret != -ENOIOCTLCMD)
  735. return ret;
  736. if (*std_id & V4L2_STD_525_60)
  737. sh_vou_reg_ab_set(vou_dev, VOUCR,
  738. sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
  739. else
  740. sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
  741. vou_dev->std = *std_id;
  742. return 0;
  743. }
  744. static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
  745. {
  746. struct video_device *vdev = video_devdata(file);
  747. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  748. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  749. *std = vou_dev->std;
  750. return 0;
  751. }
  752. static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
  753. {
  754. struct video_device *vdev = video_devdata(file);
  755. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  756. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  757. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  758. a->c = vou_dev->rect;
  759. return 0;
  760. }
  761. /* Assume a dull encoder, do all the work ourselves. */
  762. static int sh_vou_s_crop(struct file *file, void *fh, struct v4l2_crop *a)
  763. {
  764. struct video_device *vdev = video_devdata(file);
  765. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  766. struct v4l2_rect *rect = &a->c;
  767. struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
  768. struct v4l2_pix_format *pix = &vou_dev->pix;
  769. struct sh_vou_geometry geo;
  770. struct v4l2_mbus_framefmt mbfmt = {
  771. /* Revisit: is this the correct code? */
  772. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  773. .field = V4L2_FIELD_INTERLACED,
  774. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  775. };
  776. int ret;
  777. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
  778. rect->width, rect->height, rect->left, rect->top);
  779. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  780. return -EINVAL;
  781. v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  782. &rect->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  783. if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
  784. rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
  785. if (rect->height + rect->top > VOU_MAX_IMAGE_HEIGHT)
  786. rect->top = VOU_MAX_IMAGE_HEIGHT - rect->height;
  787. geo.output = *rect;
  788. geo.in_width = pix->width;
  789. geo.in_height = pix->height;
  790. /* Configure the encoder one-to-one, position at 0, ignore errors */
  791. sd_crop.c.width = geo.output.width;
  792. sd_crop.c.height = geo.output.height;
  793. /*
  794. * We first issue a S_CROP, so that the subsequent S_FMT delivers the
  795. * final encoder configuration.
  796. */
  797. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  798. s_crop, &sd_crop);
  799. mbfmt.width = geo.output.width;
  800. mbfmt.height = geo.output.height;
  801. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  802. s_mbus_fmt, &mbfmt);
  803. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  804. if (ret < 0)
  805. return ret;
  806. /* Sanity checks */
  807. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  808. (unsigned)mbfmt.height > VOU_MAX_IMAGE_HEIGHT ||
  809. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  810. return -EIO;
  811. geo.output.width = mbfmt.width;
  812. geo.output.height = mbfmt.height;
  813. /*
  814. * No down-scaling. According to the API, current call has precedence:
  815. * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
  816. */
  817. vou_adjust_input(&geo, vou_dev->std);
  818. /* We tried to preserve output rectangle, but it could have changed */
  819. vou_dev->rect = geo.output;
  820. pix->width = geo.in_width;
  821. pix->height = geo.in_height;
  822. sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
  823. geo.scale_idx_h, geo.scale_idx_v);
  824. return 0;
  825. }
  826. /*
  827. * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
  828. * is the initial register values, height takes the interlaced format into
  829. * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
  830. * actually only meaningfully contain values <= 720 and <= 240 respectively, and
  831. * not <= 864 and <= 312.
  832. */
  833. static int sh_vou_cropcap(struct file *file, void *priv,
  834. struct v4l2_cropcap *a)
  835. {
  836. struct sh_vou_file *vou_file = priv;
  837. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  838. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  839. a->bounds.left = 0;
  840. a->bounds.top = 0;
  841. a->bounds.width = VOU_MAX_IMAGE_WIDTH;
  842. a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
  843. /* Default = max, set VOUDPR = 0, which is not hardware default */
  844. a->defrect.left = 0;
  845. a->defrect.top = 0;
  846. a->defrect.width = VOU_MAX_IMAGE_WIDTH;
  847. a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
  848. a->pixelaspect.numerator = 1;
  849. a->pixelaspect.denominator = 1;
  850. return 0;
  851. }
  852. static irqreturn_t sh_vou_isr(int irq, void *dev_id)
  853. {
  854. struct sh_vou_device *vou_dev = dev_id;
  855. static unsigned long j;
  856. struct videobuf_buffer *vb;
  857. static int cnt;
  858. static int side;
  859. u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
  860. u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
  861. if (!(irq_status & 0x300)) {
  862. if (printk_timed_ratelimit(&j, 500))
  863. dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
  864. irq_status);
  865. return IRQ_NONE;
  866. }
  867. spin_lock(&vou_dev->lock);
  868. if (!vou_dev->active || list_empty(&vou_dev->queue)) {
  869. if (printk_timed_ratelimit(&j, 500))
  870. dev_warn(vou_dev->v4l2_dev.dev,
  871. "IRQ without active buffer: %x!\n", irq_status);
  872. /* Just ack: buf_release will disable further interrupts */
  873. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
  874. spin_unlock(&vou_dev->lock);
  875. return IRQ_HANDLED;
  876. }
  877. masked = ~(0x300 & irq_status) & irq_status & 0x30304;
  878. dev_dbg(vou_dev->v4l2_dev.dev,
  879. "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
  880. irq_status, masked, vou_status, cnt);
  881. cnt++;
  882. side = vou_status & 0x10000;
  883. /* Clear only set interrupts */
  884. sh_vou_reg_a_write(vou_dev, VOUIR, masked);
  885. vb = vou_dev->active;
  886. list_del(&vb->queue);
  887. vb->state = VIDEOBUF_DONE;
  888. do_gettimeofday(&vb->ts);
  889. vb->field_count++;
  890. wake_up(&vb->done);
  891. if (list_empty(&vou_dev->queue)) {
  892. /* Stop VOU */
  893. dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
  894. __func__, cnt);
  895. sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
  896. vou_dev->active = NULL;
  897. vou_dev->status = SH_VOU_INITIALISING;
  898. /* Disable End-of-Frame (VSYNC) interrupts */
  899. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
  900. spin_unlock(&vou_dev->lock);
  901. return IRQ_HANDLED;
  902. }
  903. vou_dev->active = list_entry(vou_dev->queue.next,
  904. struct videobuf_buffer, queue);
  905. if (vou_dev->active->queue.next != &vou_dev->queue) {
  906. struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
  907. struct videobuf_buffer, queue);
  908. sh_vou_schedule_next(vou_dev, new);
  909. }
  910. spin_unlock(&vou_dev->lock);
  911. return IRQ_HANDLED;
  912. }
  913. static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
  914. {
  915. struct sh_vou_pdata *pdata = vou_dev->pdata;
  916. u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
  917. int i = 100;
  918. /* Disable all IRQs */
  919. sh_vou_reg_a_write(vou_dev, VOUIR, 0);
  920. /* Reset VOU interfaces - registers unaffected */
  921. sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
  922. while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
  923. udelay(1);
  924. if (!i)
  925. return -ETIMEDOUT;
  926. dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
  927. if (pdata->flags & SH_VOU_PCLK_FALLING)
  928. voucr |= 1 << 28;
  929. if (pdata->flags & SH_VOU_HSYNC_LOW)
  930. voucr |= 1 << 27;
  931. if (pdata->flags & SH_VOU_VSYNC_LOW)
  932. voucr |= 1 << 26;
  933. sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
  934. /* Manual register side switching at first */
  935. sh_vou_reg_a_write(vou_dev, VOURCR, 4);
  936. /* Default - fixed HSYNC length, can be made configurable is required */
  937. sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
  938. return 0;
  939. }
  940. /* File operations */
  941. static int sh_vou_open(struct file *file)
  942. {
  943. struct video_device *vdev = video_devdata(file);
  944. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  945. struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
  946. GFP_KERNEL);
  947. if (!vou_file)
  948. return -ENOMEM;
  949. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  950. file->private_data = vou_file;
  951. if (atomic_inc_return(&vou_dev->use_count) == 1) {
  952. int ret;
  953. /* First open */
  954. vou_dev->status = SH_VOU_INITIALISING;
  955. pm_runtime_get_sync(vdev->v4l2_dev->dev);
  956. ret = sh_vou_hw_init(vou_dev);
  957. if (ret < 0) {
  958. atomic_dec(&vou_dev->use_count);
  959. pm_runtime_put(vdev->v4l2_dev->dev);
  960. vou_dev->status = SH_VOU_IDLE;
  961. return ret;
  962. }
  963. }
  964. videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
  965. vou_dev->v4l2_dev.dev, &vou_dev->lock,
  966. V4L2_BUF_TYPE_VIDEO_OUTPUT,
  967. V4L2_FIELD_NONE,
  968. sizeof(struct videobuf_buffer), vdev);
  969. return 0;
  970. }
  971. static int sh_vou_release(struct file *file)
  972. {
  973. struct video_device *vdev = video_devdata(file);
  974. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  975. struct sh_vou_file *vou_file = file->private_data;
  976. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  977. if (!atomic_dec_return(&vou_dev->use_count)) {
  978. /* Last close */
  979. vou_dev->status = SH_VOU_IDLE;
  980. sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
  981. pm_runtime_put(vdev->v4l2_dev->dev);
  982. }
  983. file->private_data = NULL;
  984. kfree(vou_file);
  985. return 0;
  986. }
  987. static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
  988. {
  989. struct sh_vou_file *vou_file = file->private_data;
  990. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  991. return videobuf_mmap_mapper(&vou_file->vbq, vma);
  992. }
  993. static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
  994. {
  995. struct sh_vou_file *vou_file = file->private_data;
  996. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  997. return videobuf_poll_stream(file, &vou_file->vbq, wait);
  998. }
  999. static int sh_vou_g_chip_ident(struct file *file, void *fh,
  1000. struct v4l2_dbg_chip_ident *id)
  1001. {
  1002. struct video_device *vdev = video_devdata(file);
  1003. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1004. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_chip_ident, id);
  1005. }
  1006. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1007. static int sh_vou_g_register(struct file *file, void *fh,
  1008. struct v4l2_dbg_register *reg)
  1009. {
  1010. struct video_device *vdev = video_devdata(file);
  1011. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1012. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_register, reg);
  1013. }
  1014. static int sh_vou_s_register(struct file *file, void *fh,
  1015. struct v4l2_dbg_register *reg)
  1016. {
  1017. struct video_device *vdev = video_devdata(file);
  1018. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1019. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, s_register, reg);
  1020. }
  1021. #endif
  1022. /* sh_vou display ioctl operations */
  1023. static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
  1024. .vidioc_querycap = sh_vou_querycap,
  1025. .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
  1026. .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
  1027. .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
  1028. .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
  1029. .vidioc_reqbufs = sh_vou_reqbufs,
  1030. .vidioc_querybuf = sh_vou_querybuf,
  1031. .vidioc_qbuf = sh_vou_qbuf,
  1032. .vidioc_dqbuf = sh_vou_dqbuf,
  1033. .vidioc_streamon = sh_vou_streamon,
  1034. .vidioc_streamoff = sh_vou_streamoff,
  1035. .vidioc_s_std = sh_vou_s_std,
  1036. .vidioc_g_std = sh_vou_g_std,
  1037. .vidioc_cropcap = sh_vou_cropcap,
  1038. .vidioc_g_crop = sh_vou_g_crop,
  1039. .vidioc_s_crop = sh_vou_s_crop,
  1040. .vidioc_g_chip_ident = sh_vou_g_chip_ident,
  1041. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1042. .vidioc_g_register = sh_vou_g_register,
  1043. .vidioc_s_register = sh_vou_s_register,
  1044. #endif
  1045. };
  1046. static const struct v4l2_file_operations sh_vou_fops = {
  1047. .owner = THIS_MODULE,
  1048. .open = sh_vou_open,
  1049. .release = sh_vou_release,
  1050. .ioctl = video_ioctl2,
  1051. .mmap = sh_vou_mmap,
  1052. .poll = sh_vou_poll,
  1053. };
  1054. static const struct video_device sh_vou_video_template = {
  1055. .name = "sh_vou",
  1056. .fops = &sh_vou_fops,
  1057. .ioctl_ops = &sh_vou_ioctl_ops,
  1058. .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
  1059. .current_norm = V4L2_STD_NTSC_M,
  1060. };
  1061. static int __devinit sh_vou_probe(struct platform_device *pdev)
  1062. {
  1063. struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
  1064. struct v4l2_rect *rect;
  1065. struct v4l2_pix_format *pix;
  1066. struct i2c_adapter *i2c_adap;
  1067. struct video_device *vdev;
  1068. struct sh_vou_device *vou_dev;
  1069. struct resource *reg_res, *region;
  1070. struct v4l2_subdev *subdev;
  1071. int irq, ret;
  1072. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1073. irq = platform_get_irq(pdev, 0);
  1074. if (!vou_pdata || !reg_res || irq <= 0) {
  1075. dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
  1076. return -ENODEV;
  1077. }
  1078. vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
  1079. if (!vou_dev)
  1080. return -ENOMEM;
  1081. INIT_LIST_HEAD(&vou_dev->queue);
  1082. spin_lock_init(&vou_dev->lock);
  1083. atomic_set(&vou_dev->use_count, 0);
  1084. vou_dev->pdata = vou_pdata;
  1085. vou_dev->status = SH_VOU_IDLE;
  1086. rect = &vou_dev->rect;
  1087. pix = &vou_dev->pix;
  1088. /* Fill in defaults */
  1089. vou_dev->std = sh_vou_video_template.current_norm;
  1090. rect->left = 0;
  1091. rect->top = 0;
  1092. rect->width = VOU_MAX_IMAGE_WIDTH;
  1093. rect->height = VOU_MAX_IMAGE_HEIGHT;
  1094. pix->width = VOU_MAX_IMAGE_WIDTH;
  1095. pix->height = VOU_MAX_IMAGE_HEIGHT;
  1096. pix->pixelformat = V4L2_PIX_FMT_YVYU;
  1097. pix->field = V4L2_FIELD_NONE;
  1098. pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
  1099. pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * VOU_MAX_IMAGE_HEIGHT;
  1100. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1101. region = request_mem_region(reg_res->start, resource_size(reg_res),
  1102. pdev->name);
  1103. if (!region) {
  1104. dev_err(&pdev->dev, "VOU region already claimed\n");
  1105. ret = -EBUSY;
  1106. goto ereqmemreg;
  1107. }
  1108. vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
  1109. if (!vou_dev->base) {
  1110. ret = -ENOMEM;
  1111. goto emap;
  1112. }
  1113. ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
  1114. if (ret < 0)
  1115. goto ereqirq;
  1116. ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
  1117. if (ret < 0) {
  1118. dev_err(&pdev->dev, "Error registering v4l2 device\n");
  1119. goto ev4l2devreg;
  1120. }
  1121. /* Allocate memory for video device */
  1122. vdev = video_device_alloc();
  1123. if (vdev == NULL) {
  1124. ret = -ENOMEM;
  1125. goto evdevalloc;
  1126. }
  1127. *vdev = sh_vou_video_template;
  1128. if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
  1129. vdev->tvnorms |= V4L2_STD_PAL;
  1130. vdev->v4l2_dev = &vou_dev->v4l2_dev;
  1131. vdev->release = video_device_release;
  1132. vou_dev->vdev = vdev;
  1133. video_set_drvdata(vdev, vou_dev);
  1134. pm_runtime_enable(&pdev->dev);
  1135. pm_runtime_resume(&pdev->dev);
  1136. i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
  1137. if (!i2c_adap) {
  1138. ret = -ENODEV;
  1139. goto ei2cgadap;
  1140. }
  1141. ret = sh_vou_hw_init(vou_dev);
  1142. if (ret < 0)
  1143. goto ereset;
  1144. subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
  1145. vou_pdata->module_name, vou_pdata->board_info, NULL);
  1146. if (!subdev) {
  1147. ret = -ENOMEM;
  1148. goto ei2cnd;
  1149. }
  1150. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1151. if (ret < 0)
  1152. goto evregdev;
  1153. return 0;
  1154. evregdev:
  1155. ei2cnd:
  1156. ereset:
  1157. i2c_put_adapter(i2c_adap);
  1158. ei2cgadap:
  1159. video_device_release(vdev);
  1160. pm_runtime_disable(&pdev->dev);
  1161. evdevalloc:
  1162. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1163. ev4l2devreg:
  1164. free_irq(irq, vou_dev);
  1165. ereqirq:
  1166. iounmap(vou_dev->base);
  1167. emap:
  1168. release_mem_region(reg_res->start, resource_size(reg_res));
  1169. ereqmemreg:
  1170. kfree(vou_dev);
  1171. return ret;
  1172. }
  1173. static int __devexit sh_vou_remove(struct platform_device *pdev)
  1174. {
  1175. int irq = platform_get_irq(pdev, 0);
  1176. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1177. struct sh_vou_device *vou_dev = container_of(v4l2_dev,
  1178. struct sh_vou_device, v4l2_dev);
  1179. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  1180. struct v4l2_subdev, list);
  1181. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1182. struct resource *reg_res;
  1183. if (irq > 0)
  1184. free_irq(irq, vou_dev);
  1185. pm_runtime_disable(&pdev->dev);
  1186. video_unregister_device(vou_dev->vdev);
  1187. i2c_put_adapter(client->adapter);
  1188. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1189. iounmap(vou_dev->base);
  1190. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1191. if (reg_res)
  1192. release_mem_region(reg_res->start, resource_size(reg_res));
  1193. kfree(vou_dev);
  1194. return 0;
  1195. }
  1196. static struct platform_driver __refdata sh_vou = {
  1197. .remove = __devexit_p(sh_vou_remove),
  1198. .driver = {
  1199. .name = "sh-vou",
  1200. .owner = THIS_MODULE,
  1201. },
  1202. };
  1203. static int __init sh_vou_init(void)
  1204. {
  1205. return platform_driver_probe(&sh_vou, sh_vou_probe);
  1206. }
  1207. static void __exit sh_vou_exit(void)
  1208. {
  1209. platform_driver_unregister(&sh_vou);
  1210. }
  1211. module_init(sh_vou_init);
  1212. module_exit(sh_vou_exit);
  1213. MODULE_DESCRIPTION("SuperH VOU driver");
  1214. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1215. MODULE_LICENSE("GPL v2");
  1216. MODULE_ALIAS("platform:sh-vou");