sh_vou.c 37 KB

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