sh_vou.c 37 KB

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