sh_vou.c 37 KB

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