sh_vou.c 40 KB

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