sh_vou.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. case V4L2_PIX_FMT_NV12:
  189. case V4L2_PIX_FMT_NV16:
  190. row_coeff = 1;
  191. break;
  192. case V4L2_PIX_FMT_RGB565:
  193. dataswap ^= 1;
  194. case V4L2_PIX_FMT_RGB565X:
  195. row_coeff = 2;
  196. break;
  197. case V4L2_PIX_FMT_RGB24:
  198. row_coeff = 3;
  199. break;
  200. }
  201. sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
  202. sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
  203. sh_vou_schedule_next(vou_dev, vb);
  204. }
  205. static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  206. {
  207. BUG_ON(in_interrupt());
  208. /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
  209. videobuf_waiton(vq, vb, 0, 0);
  210. videobuf_dma_contig_free(vq, vb);
  211. vb->state = VIDEOBUF_NEEDS_INIT;
  212. }
  213. /* Locking: caller holds fop_lock mutex */
  214. static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  215. unsigned int *size)
  216. {
  217. struct video_device *vdev = vq->priv_data;
  218. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  219. *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
  220. vou_dev->pix.height / 8;
  221. if (*count < 2)
  222. *count = 2;
  223. /* Taking into account maximum frame size, *count will stay >= 2 */
  224. if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
  225. *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
  226. dev_dbg(vq->dev, "%s(): count=%d, size=%d\n", __func__, *count, *size);
  227. return 0;
  228. }
  229. /* Locking: caller holds fop_lock mutex */
  230. static int sh_vou_buf_prepare(struct videobuf_queue *vq,
  231. struct videobuf_buffer *vb,
  232. enum v4l2_field field)
  233. {
  234. struct video_device *vdev = vq->priv_data;
  235. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  236. struct v4l2_pix_format *pix = &vou_dev->pix;
  237. int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
  238. int ret;
  239. dev_dbg(vq->dev, "%s()\n", __func__);
  240. if (vb->width != pix->width ||
  241. vb->height != pix->height ||
  242. vb->field != pix->field) {
  243. vb->width = pix->width;
  244. vb->height = pix->height;
  245. vb->field = field;
  246. if (vb->state != VIDEOBUF_NEEDS_INIT)
  247. free_buffer(vq, vb);
  248. }
  249. vb->size = vb->height * bytes_per_line;
  250. if (vb->baddr && vb->bsize < vb->size) {
  251. /* User buffer too small */
  252. dev_warn(vq->dev, "User buffer too small: [%u] @ %lx\n",
  253. vb->bsize, vb->baddr);
  254. return -EINVAL;
  255. }
  256. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  257. ret = videobuf_iolock(vq, vb, NULL);
  258. if (ret < 0) {
  259. dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
  260. vb->memory, ret);
  261. return ret;
  262. }
  263. vb->state = VIDEOBUF_PREPARED;
  264. }
  265. dev_dbg(vq->dev,
  266. "%s(): fmt #%d, %u bytes per line, phys 0x%x, type %d, state %d\n",
  267. __func__, vou_dev->pix_idx, bytes_per_line,
  268. videobuf_to_dma_contig(vb), vb->memory, vb->state);
  269. return 0;
  270. }
  271. /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
  272. static void sh_vou_buf_queue(struct videobuf_queue *vq,
  273. struct videobuf_buffer *vb)
  274. {
  275. struct video_device *vdev = vq->priv_data;
  276. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  277. dev_dbg(vq->dev, "%s()\n", __func__);
  278. vb->state = VIDEOBUF_QUEUED;
  279. list_add_tail(&vb->queue, &vou_dev->queue);
  280. if (vou_dev->status == SH_VOU_RUNNING) {
  281. return;
  282. } else if (!vou_dev->active) {
  283. vou_dev->active = vb;
  284. /* Start from side A: we use mirror addresses, so, set B */
  285. sh_vou_reg_a_write(vou_dev, VOURPR, 1);
  286. dev_dbg(vq->dev, "%s: first buffer status 0x%x\n", __func__,
  287. sh_vou_reg_a_read(vou_dev, VOUSTR));
  288. sh_vou_schedule_next(vou_dev, vb);
  289. /* Only activate VOU after the second buffer */
  290. } else if (vou_dev->active->queue.next == &vb->queue) {
  291. /* Second buffer - initialise register side B */
  292. sh_vou_reg_a_write(vou_dev, VOURPR, 0);
  293. sh_vou_stream_start(vou_dev, vb);
  294. /* Register side switching with frame VSYNC */
  295. sh_vou_reg_a_write(vou_dev, VOURCR, 5);
  296. dev_dbg(vq->dev, "%s: second buffer status 0x%x\n", __func__,
  297. sh_vou_reg_a_read(vou_dev, VOUSTR));
  298. /* Enable End-of-Frame (VSYNC) interrupts */
  299. sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
  300. /* Two buffers on the queue - activate the hardware */
  301. vou_dev->status = SH_VOU_RUNNING;
  302. sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
  303. }
  304. }
  305. static void sh_vou_buf_release(struct videobuf_queue *vq,
  306. struct videobuf_buffer *vb)
  307. {
  308. struct video_device *vdev = vq->priv_data;
  309. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  310. unsigned long flags;
  311. dev_dbg(vq->dev, "%s()\n", __func__);
  312. spin_lock_irqsave(&vou_dev->lock, flags);
  313. if (vou_dev->active == vb) {
  314. /* disable output */
  315. sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
  316. /* ...but the current frame will complete */
  317. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
  318. vou_dev->active = NULL;
  319. }
  320. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
  321. vb->state = VIDEOBUF_ERROR;
  322. list_del(&vb->queue);
  323. }
  324. spin_unlock_irqrestore(&vou_dev->lock, flags);
  325. free_buffer(vq, vb);
  326. }
  327. static struct videobuf_queue_ops sh_vou_video_qops = {
  328. .buf_setup = sh_vou_buf_setup,
  329. .buf_prepare = sh_vou_buf_prepare,
  330. .buf_queue = sh_vou_buf_queue,
  331. .buf_release = sh_vou_buf_release,
  332. };
  333. /* Video IOCTLs */
  334. static int sh_vou_querycap(struct file *file, void *priv,
  335. struct v4l2_capability *cap)
  336. {
  337. struct sh_vou_file *vou_file = priv;
  338. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  339. strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
  340. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  341. return 0;
  342. }
  343. /* Enumerate formats, that the device can accept from the user */
  344. static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
  345. struct v4l2_fmtdesc *fmt)
  346. {
  347. struct sh_vou_file *vou_file = priv;
  348. if (fmt->index >= ARRAY_SIZE(vou_fmt))
  349. return -EINVAL;
  350. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  351. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  352. strlcpy(fmt->description, vou_fmt[fmt->index].desc,
  353. sizeof(fmt->description));
  354. fmt->pixelformat = vou_fmt[fmt->index].pfmt;
  355. return 0;
  356. }
  357. static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
  358. struct v4l2_format *fmt)
  359. {
  360. struct video_device *vdev = video_devdata(file);
  361. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  362. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  363. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  364. fmt->fmt.pix = vou_dev->pix;
  365. return 0;
  366. }
  367. static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
  368. static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
  369. static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
  370. static const unsigned char vou_scale_v_num[] = {1, 2, 4};
  371. static const unsigned char vou_scale_v_den[] = {1, 1, 1};
  372. static const unsigned char vou_scale_v_fld[] = {0, 1};
  373. static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
  374. int pix_idx, int w_idx, int h_idx)
  375. {
  376. struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
  377. unsigned int black_left, black_top, width_max, height_max,
  378. frame_in_height, frame_out_height, frame_out_top;
  379. struct v4l2_rect *rect = &vou_dev->rect;
  380. struct v4l2_pix_format *pix = &vou_dev->pix;
  381. u32 vouvcr = 0, dsr_h, dsr_v;
  382. if (vou_dev->std & V4L2_STD_525_60) {
  383. width_max = 858;
  384. height_max = 262;
  385. } else {
  386. width_max = 864;
  387. height_max = 312;
  388. }
  389. frame_in_height = pix->height / 2;
  390. frame_out_height = rect->height / 2;
  391. frame_out_top = rect->top / 2;
  392. /*
  393. * Cropping scheme: max useful image is 720x480, and the total video
  394. * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
  395. * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
  396. * of which the first 33 / 25 clocks HSYNC must be held active. This
  397. * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
  398. * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
  399. * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
  400. * beyond DSR, specified on the left and top by the VPR register "black
  401. * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
  402. * at 138 / 144 : 20, because that's the HSYNC timing, that our first
  403. * client requires, and that's exactly what leaves us 720 pixels for the
  404. * image; we leave VPR[VVP] at default 20 for now, because the client
  405. * doesn't seem to have any special requirements for it. Otherwise we
  406. * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
  407. * the selected standard, and DPR and DSR are selected according to
  408. * cropping. Q: how does the client detect the first valid line? Does
  409. * HSYNC stay inactive during invalid (black) lines?
  410. */
  411. black_left = width_max - VOU_MAX_IMAGE_WIDTH;
  412. black_top = 20;
  413. dsr_h = rect->width + rect->left;
  414. dsr_v = frame_out_height + frame_out_top;
  415. dev_dbg(vou_dev->v4l2_dev.dev,
  416. "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
  417. pix->width, frame_in_height, black_left, black_top,
  418. rect->left, frame_out_top, dsr_h, dsr_v);
  419. /* VOUISR height - half of a frame height in frame mode */
  420. sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
  421. sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
  422. sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
  423. sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
  424. /*
  425. * if necessary, we could set VOUHIR to
  426. * max(black_left + dsr_h, width_max) here
  427. */
  428. if (w_idx)
  429. vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
  430. if (h_idx)
  431. vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
  432. dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
  433. /* To produce a colour bar for testing set bit 23 of VOUVCR */
  434. sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
  435. sh_vou_reg_ab_write(vou_dev, VOUDFR,
  436. fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
  437. }
  438. struct sh_vou_geometry {
  439. struct v4l2_rect output;
  440. unsigned int in_width;
  441. unsigned int in_height;
  442. int scale_idx_h;
  443. int scale_idx_v;
  444. };
  445. /*
  446. * Find input geometry, that we can use to produce output, closest to the
  447. * requested rectangle, using VOU scaling
  448. */
  449. static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
  450. {
  451. /* The compiler cannot know, that best and idx will indeed be set */
  452. unsigned int best_err = UINT_MAX, best = 0, img_height_max;
  453. int i, idx = 0;
  454. if (std & V4L2_STD_525_60)
  455. img_height_max = 480;
  456. else
  457. img_height_max = 576;
  458. /* Image width must be a multiple of 4 */
  459. v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  460. &geo->in_height, 0, img_height_max, 1, 0);
  461. /* Select scales to come as close as possible to the output image */
  462. for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
  463. unsigned int err;
  464. unsigned int found = geo->output.width * vou_scale_h_den[i] /
  465. vou_scale_h_num[i];
  466. if (found > VOU_MAX_IMAGE_WIDTH)
  467. /* scales increase */
  468. break;
  469. err = abs(found - geo->in_width);
  470. if (err < best_err) {
  471. best_err = err;
  472. idx = i;
  473. best = found;
  474. }
  475. if (!err)
  476. break;
  477. }
  478. geo->in_width = best;
  479. geo->scale_idx_h = idx;
  480. best_err = UINT_MAX;
  481. /* This loop can be replaced with one division */
  482. for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
  483. unsigned int err;
  484. unsigned int found = geo->output.height * vou_scale_v_den[i] /
  485. vou_scale_v_num[i];
  486. if (found > img_height_max)
  487. /* scales increase */
  488. break;
  489. err = abs(found - geo->in_height);
  490. if (err < best_err) {
  491. best_err = err;
  492. idx = i;
  493. best = found;
  494. }
  495. if (!err)
  496. break;
  497. }
  498. geo->in_height = best;
  499. geo->scale_idx_v = idx;
  500. }
  501. /*
  502. * Find output geometry, that we can produce, using VOU scaling, closest to
  503. * the requested rectangle
  504. */
  505. static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
  506. {
  507. unsigned int best_err = UINT_MAX, best, width_max, height_max,
  508. img_height_max;
  509. int i, idx;
  510. if (std & V4L2_STD_525_60) {
  511. width_max = 858;
  512. height_max = 262 * 2;
  513. img_height_max = 480;
  514. } else {
  515. width_max = 864;
  516. height_max = 312 * 2;
  517. img_height_max = 576;
  518. }
  519. /* Select scales to come as close as possible to the output image */
  520. for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
  521. unsigned int err;
  522. unsigned int found = geo->in_width * vou_scale_h_num[i] /
  523. vou_scale_h_den[i];
  524. if (found > VOU_MAX_IMAGE_WIDTH)
  525. /* scales increase */
  526. break;
  527. err = abs(found - geo->output.width);
  528. if (err < best_err) {
  529. best_err = err;
  530. idx = i;
  531. best = found;
  532. }
  533. if (!err)
  534. break;
  535. }
  536. geo->output.width = best;
  537. geo->scale_idx_h = idx;
  538. if (geo->output.left + best > width_max)
  539. geo->output.left = width_max - best;
  540. pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
  541. vou_scale_h_num[idx], vou_scale_h_den[idx], best);
  542. best_err = UINT_MAX;
  543. /* This loop can be replaced with one division */
  544. for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
  545. unsigned int err;
  546. unsigned int found = geo->in_height * vou_scale_v_num[i] /
  547. vou_scale_v_den[i];
  548. if (found > img_height_max)
  549. /* scales increase */
  550. break;
  551. err = abs(found - geo->output.height);
  552. if (err < best_err) {
  553. best_err = err;
  554. idx = i;
  555. best = found;
  556. }
  557. if (!err)
  558. break;
  559. }
  560. geo->output.height = best;
  561. geo->scale_idx_v = idx;
  562. if (geo->output.top + best > height_max)
  563. geo->output.top = height_max - best;
  564. pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
  565. vou_scale_v_num[idx], vou_scale_v_den[idx], best);
  566. }
  567. static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
  568. struct v4l2_format *fmt)
  569. {
  570. struct video_device *vdev = video_devdata(file);
  571. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  572. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  573. unsigned int img_height_max;
  574. int pix_idx;
  575. struct sh_vou_geometry geo;
  576. struct v4l2_mbus_framefmt mbfmt = {
  577. /* Revisit: is this the correct code? */
  578. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  579. .field = V4L2_FIELD_INTERLACED,
  580. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  581. };
  582. int ret;
  583. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  584. vou_dev->rect.width, vou_dev->rect.height,
  585. pix->width, pix->height);
  586. if (pix->field == V4L2_FIELD_ANY)
  587. pix->field = V4L2_FIELD_NONE;
  588. if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  589. pix->field != V4L2_FIELD_NONE)
  590. return -EINVAL;
  591. for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
  592. if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
  593. break;
  594. if (pix_idx == ARRAY_SIZE(vou_fmt))
  595. return -EINVAL;
  596. if (vou_dev->std & V4L2_STD_525_60)
  597. img_height_max = 480;
  598. else
  599. img_height_max = 576;
  600. /* Image width must be a multiple of 4 */
  601. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
  602. &pix->height, 0, img_height_max, 1, 0);
  603. geo.in_width = pix->width;
  604. geo.in_height = pix->height;
  605. geo.output = vou_dev->rect;
  606. vou_adjust_output(&geo, vou_dev->std);
  607. mbfmt.width = geo.output.width;
  608. mbfmt.height = geo.output.height;
  609. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  610. s_mbus_fmt, &mbfmt);
  611. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  612. if (ret < 0)
  613. return ret;
  614. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
  615. geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
  616. /* Sanity checks */
  617. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  618. (unsigned)mbfmt.height > img_height_max ||
  619. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  620. return -EIO;
  621. if (mbfmt.width != geo.output.width ||
  622. mbfmt.height != geo.output.height) {
  623. geo.output.width = mbfmt.width;
  624. geo.output.height = mbfmt.height;
  625. vou_adjust_input(&geo, vou_dev->std);
  626. }
  627. /* We tried to preserve output rectangle, but it could have changed */
  628. vou_dev->rect = geo.output;
  629. pix->width = geo.in_width;
  630. pix->height = geo.in_height;
  631. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
  632. pix->width, pix->height);
  633. vou_dev->pix_idx = pix_idx;
  634. vou_dev->pix = *pix;
  635. sh_vou_configure_geometry(vou_dev, pix_idx,
  636. geo.scale_idx_h, geo.scale_idx_v);
  637. return 0;
  638. }
  639. static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
  640. struct v4l2_format *fmt)
  641. {
  642. struct sh_vou_file *vou_file = priv;
  643. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  644. int i;
  645. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  646. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  647. pix->field = V4L2_FIELD_NONE;
  648. v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  649. &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
  650. for (i = 0; ARRAY_SIZE(vou_fmt); i++)
  651. if (vou_fmt[i].pfmt == pix->pixelformat)
  652. return 0;
  653. pix->pixelformat = vou_fmt[0].pfmt;
  654. return 0;
  655. }
  656. static int sh_vou_reqbufs(struct file *file, void *priv,
  657. struct v4l2_requestbuffers *req)
  658. {
  659. struct sh_vou_file *vou_file = priv;
  660. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  661. if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  662. return -EINVAL;
  663. return videobuf_reqbufs(&vou_file->vbq, req);
  664. }
  665. static int sh_vou_querybuf(struct file *file, void *priv,
  666. struct v4l2_buffer *b)
  667. {
  668. struct sh_vou_file *vou_file = priv;
  669. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  670. return videobuf_querybuf(&vou_file->vbq, b);
  671. }
  672. static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  673. {
  674. struct sh_vou_file *vou_file = priv;
  675. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  676. return videobuf_qbuf(&vou_file->vbq, b);
  677. }
  678. static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  679. {
  680. struct sh_vou_file *vou_file = priv;
  681. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  682. return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
  683. }
  684. static int sh_vou_streamon(struct file *file, void *priv,
  685. enum v4l2_buf_type buftype)
  686. {
  687. struct video_device *vdev = video_devdata(file);
  688. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  689. struct sh_vou_file *vou_file = priv;
  690. int ret;
  691. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  692. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
  693. video, s_stream, 1);
  694. if (ret < 0 && ret != -ENOIOCTLCMD)
  695. return ret;
  696. /* This calls our .buf_queue() (== sh_vou_buf_queue) */
  697. return videobuf_streamon(&vou_file->vbq);
  698. }
  699. static int sh_vou_streamoff(struct file *file, void *priv,
  700. enum v4l2_buf_type buftype)
  701. {
  702. struct video_device *vdev = video_devdata(file);
  703. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  704. struct sh_vou_file *vou_file = priv;
  705. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  706. /*
  707. * This calls buf_release from host driver's videobuf_queue_ops for all
  708. * remaining buffers. When the last buffer is freed, stop streaming
  709. */
  710. videobuf_streamoff(&vou_file->vbq);
  711. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
  712. return 0;
  713. }
  714. static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
  715. {
  716. switch (bus_fmt) {
  717. default:
  718. pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
  719. __func__, bus_fmt);
  720. case SH_VOU_BUS_8BIT:
  721. return 1;
  722. case SH_VOU_BUS_16BIT:
  723. return 0;
  724. case SH_VOU_BUS_BT656:
  725. return 3;
  726. }
  727. }
  728. static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  729. {
  730. struct video_device *vdev = video_devdata(file);
  731. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  732. int ret;
  733. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, *std_id);
  734. if (*std_id & ~vdev->tvnorms)
  735. return -EINVAL;
  736. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  737. s_std_output, *std_id);
  738. /* Shall we continue, if the subdev doesn't support .s_std_output()? */
  739. if (ret < 0 && ret != -ENOIOCTLCMD)
  740. return ret;
  741. if (*std_id & V4L2_STD_525_60)
  742. sh_vou_reg_ab_set(vou_dev, VOUCR,
  743. sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
  744. else
  745. sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
  746. vou_dev->std = *std_id;
  747. return 0;
  748. }
  749. static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
  750. {
  751. struct video_device *vdev = video_devdata(file);
  752. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  753. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  754. *std = vou_dev->std;
  755. return 0;
  756. }
  757. static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
  758. {
  759. struct video_device *vdev = video_devdata(file);
  760. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  761. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  762. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  763. a->c = vou_dev->rect;
  764. return 0;
  765. }
  766. /* Assume a dull encoder, do all the work ourselves. */
  767. static int sh_vou_s_crop(struct file *file, void *fh, struct v4l2_crop *a)
  768. {
  769. struct video_device *vdev = video_devdata(file);
  770. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  771. struct v4l2_rect *rect = &a->c;
  772. struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
  773. struct v4l2_pix_format *pix = &vou_dev->pix;
  774. struct sh_vou_geometry geo;
  775. struct v4l2_mbus_framefmt mbfmt = {
  776. /* Revisit: is this the correct code? */
  777. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  778. .field = V4L2_FIELD_INTERLACED,
  779. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  780. };
  781. unsigned int img_height_max;
  782. int ret;
  783. dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
  784. rect->width, rect->height, rect->left, rect->top);
  785. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  786. return -EINVAL;
  787. if (vou_dev->std & V4L2_STD_525_60)
  788. img_height_max = 480;
  789. else
  790. img_height_max = 576;
  791. v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
  792. &rect->height, 0, img_height_max, 1, 0);
  793. if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
  794. rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
  795. if (rect->height + rect->top > img_height_max)
  796. rect->top = img_height_max - rect->height;
  797. geo.output = *rect;
  798. geo.in_width = pix->width;
  799. geo.in_height = pix->height;
  800. /* Configure the encoder one-to-one, position at 0, ignore errors */
  801. sd_crop.c.width = geo.output.width;
  802. sd_crop.c.height = geo.output.height;
  803. /*
  804. * We first issue a S_CROP, so that the subsequent S_FMT delivers the
  805. * final encoder configuration.
  806. */
  807. v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  808. s_crop, &sd_crop);
  809. mbfmt.width = geo.output.width;
  810. mbfmt.height = geo.output.height;
  811. ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
  812. s_mbus_fmt, &mbfmt);
  813. /* Must be implemented, so, don't check for -ENOIOCTLCMD */
  814. if (ret < 0)
  815. return ret;
  816. /* Sanity checks */
  817. if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
  818. (unsigned)mbfmt.height > img_height_max ||
  819. mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
  820. return -EIO;
  821. geo.output.width = mbfmt.width;
  822. geo.output.height = mbfmt.height;
  823. /*
  824. * No down-scaling. According to the API, current call has precedence:
  825. * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
  826. */
  827. vou_adjust_input(&geo, vou_dev->std);
  828. /* We tried to preserve output rectangle, but it could have changed */
  829. vou_dev->rect = geo.output;
  830. pix->width = geo.in_width;
  831. pix->height = geo.in_height;
  832. sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
  833. geo.scale_idx_h, geo.scale_idx_v);
  834. return 0;
  835. }
  836. /*
  837. * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
  838. * is the initial register values, height takes the interlaced format into
  839. * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
  840. * actually only meaningfully contain values <= 720 and <= 240 respectively, and
  841. * not <= 864 and <= 312.
  842. */
  843. static int sh_vou_cropcap(struct file *file, void *priv,
  844. struct v4l2_cropcap *a)
  845. {
  846. struct sh_vou_file *vou_file = priv;
  847. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  848. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  849. a->bounds.left = 0;
  850. a->bounds.top = 0;
  851. a->bounds.width = VOU_MAX_IMAGE_WIDTH;
  852. a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
  853. /* Default = max, set VOUDPR = 0, which is not hardware default */
  854. a->defrect.left = 0;
  855. a->defrect.top = 0;
  856. a->defrect.width = VOU_MAX_IMAGE_WIDTH;
  857. a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
  858. a->pixelaspect.numerator = 1;
  859. a->pixelaspect.denominator = 1;
  860. return 0;
  861. }
  862. static irqreturn_t sh_vou_isr(int irq, void *dev_id)
  863. {
  864. struct sh_vou_device *vou_dev = dev_id;
  865. static unsigned long j;
  866. struct videobuf_buffer *vb;
  867. static int cnt;
  868. static int side;
  869. u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
  870. u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
  871. if (!(irq_status & 0x300)) {
  872. if (printk_timed_ratelimit(&j, 500))
  873. dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
  874. irq_status);
  875. return IRQ_NONE;
  876. }
  877. spin_lock(&vou_dev->lock);
  878. if (!vou_dev->active || list_empty(&vou_dev->queue)) {
  879. if (printk_timed_ratelimit(&j, 500))
  880. dev_warn(vou_dev->v4l2_dev.dev,
  881. "IRQ without active buffer: %x!\n", irq_status);
  882. /* Just ack: buf_release will disable further interrupts */
  883. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
  884. spin_unlock(&vou_dev->lock);
  885. return IRQ_HANDLED;
  886. }
  887. masked = ~(0x300 & irq_status) & irq_status & 0x30304;
  888. dev_dbg(vou_dev->v4l2_dev.dev,
  889. "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
  890. irq_status, masked, vou_status, cnt);
  891. cnt++;
  892. side = vou_status & 0x10000;
  893. /* Clear only set interrupts */
  894. sh_vou_reg_a_write(vou_dev, VOUIR, masked);
  895. vb = vou_dev->active;
  896. list_del(&vb->queue);
  897. vb->state = VIDEOBUF_DONE;
  898. do_gettimeofday(&vb->ts);
  899. vb->field_count++;
  900. wake_up(&vb->done);
  901. if (list_empty(&vou_dev->queue)) {
  902. /* Stop VOU */
  903. dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
  904. __func__, cnt);
  905. sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
  906. vou_dev->active = NULL;
  907. vou_dev->status = SH_VOU_INITIALISING;
  908. /* Disable End-of-Frame (VSYNC) interrupts */
  909. sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
  910. spin_unlock(&vou_dev->lock);
  911. return IRQ_HANDLED;
  912. }
  913. vou_dev->active = list_entry(vou_dev->queue.next,
  914. struct videobuf_buffer, queue);
  915. if (vou_dev->active->queue.next != &vou_dev->queue) {
  916. struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
  917. struct videobuf_buffer, queue);
  918. sh_vou_schedule_next(vou_dev, new);
  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. return 0;
  949. }
  950. /* File operations */
  951. static int sh_vou_open(struct file *file)
  952. {
  953. struct video_device *vdev = video_devdata(file);
  954. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  955. struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
  956. GFP_KERNEL);
  957. if (!vou_file)
  958. return -ENOMEM;
  959. dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
  960. file->private_data = vou_file;
  961. if (atomic_inc_return(&vou_dev->use_count) == 1) {
  962. int ret;
  963. /* First open */
  964. vou_dev->status = SH_VOU_INITIALISING;
  965. pm_runtime_get_sync(vdev->v4l2_dev->dev);
  966. ret = sh_vou_hw_init(vou_dev);
  967. if (ret < 0) {
  968. atomic_dec(&vou_dev->use_count);
  969. pm_runtime_put(vdev->v4l2_dev->dev);
  970. vou_dev->status = SH_VOU_IDLE;
  971. return ret;
  972. }
  973. }
  974. videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
  975. vou_dev->v4l2_dev.dev, &vou_dev->lock,
  976. V4L2_BUF_TYPE_VIDEO_OUTPUT,
  977. V4L2_FIELD_NONE,
  978. sizeof(struct videobuf_buffer), vdev,
  979. &vou_dev->fop_lock);
  980. return 0;
  981. }
  982. static int sh_vou_release(struct file *file)
  983. {
  984. struct video_device *vdev = video_devdata(file);
  985. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  986. struct sh_vou_file *vou_file = file->private_data;
  987. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  988. if (!atomic_dec_return(&vou_dev->use_count)) {
  989. /* Last close */
  990. vou_dev->status = SH_VOU_IDLE;
  991. sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
  992. pm_runtime_put(vdev->v4l2_dev->dev);
  993. }
  994. file->private_data = NULL;
  995. kfree(vou_file);
  996. return 0;
  997. }
  998. static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
  999. {
  1000. struct sh_vou_file *vou_file = file->private_data;
  1001. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  1002. return videobuf_mmap_mapper(&vou_file->vbq, vma);
  1003. }
  1004. static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
  1005. {
  1006. struct sh_vou_file *vou_file = file->private_data;
  1007. dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
  1008. return videobuf_poll_stream(file, &vou_file->vbq, wait);
  1009. }
  1010. static int sh_vou_g_chip_ident(struct file *file, void *fh,
  1011. struct v4l2_dbg_chip_ident *id)
  1012. {
  1013. struct video_device *vdev = video_devdata(file);
  1014. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1015. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_chip_ident, id);
  1016. }
  1017. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1018. static int sh_vou_g_register(struct file *file, void *fh,
  1019. struct v4l2_dbg_register *reg)
  1020. {
  1021. struct video_device *vdev = video_devdata(file);
  1022. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1023. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_register, reg);
  1024. }
  1025. static int sh_vou_s_register(struct file *file, void *fh,
  1026. struct v4l2_dbg_register *reg)
  1027. {
  1028. struct video_device *vdev = video_devdata(file);
  1029. struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
  1030. return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, s_register, reg);
  1031. }
  1032. #endif
  1033. /* sh_vou display ioctl operations */
  1034. static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
  1035. .vidioc_querycap = sh_vou_querycap,
  1036. .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
  1037. .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
  1038. .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
  1039. .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
  1040. .vidioc_reqbufs = sh_vou_reqbufs,
  1041. .vidioc_querybuf = sh_vou_querybuf,
  1042. .vidioc_qbuf = sh_vou_qbuf,
  1043. .vidioc_dqbuf = sh_vou_dqbuf,
  1044. .vidioc_streamon = sh_vou_streamon,
  1045. .vidioc_streamoff = sh_vou_streamoff,
  1046. .vidioc_s_std = sh_vou_s_std,
  1047. .vidioc_g_std = sh_vou_g_std,
  1048. .vidioc_cropcap = sh_vou_cropcap,
  1049. .vidioc_g_crop = sh_vou_g_crop,
  1050. .vidioc_s_crop = sh_vou_s_crop,
  1051. .vidioc_g_chip_ident = sh_vou_g_chip_ident,
  1052. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1053. .vidioc_g_register = sh_vou_g_register,
  1054. .vidioc_s_register = sh_vou_s_register,
  1055. #endif
  1056. };
  1057. static const struct v4l2_file_operations sh_vou_fops = {
  1058. .owner = THIS_MODULE,
  1059. .open = sh_vou_open,
  1060. .release = sh_vou_release,
  1061. .unlocked_ioctl = video_ioctl2,
  1062. .mmap = sh_vou_mmap,
  1063. .poll = sh_vou_poll,
  1064. };
  1065. static const struct video_device sh_vou_video_template = {
  1066. .name = "sh_vou",
  1067. .fops = &sh_vou_fops,
  1068. .ioctl_ops = &sh_vou_ioctl_ops,
  1069. .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
  1070. .current_norm = V4L2_STD_NTSC_M,
  1071. };
  1072. static int __devinit sh_vou_probe(struct platform_device *pdev)
  1073. {
  1074. struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
  1075. struct v4l2_rect *rect;
  1076. struct v4l2_pix_format *pix;
  1077. struct i2c_adapter *i2c_adap;
  1078. struct video_device *vdev;
  1079. struct sh_vou_device *vou_dev;
  1080. struct resource *reg_res, *region;
  1081. struct v4l2_subdev *subdev;
  1082. int irq, ret;
  1083. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1084. irq = platform_get_irq(pdev, 0);
  1085. if (!vou_pdata || !reg_res || irq <= 0) {
  1086. dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
  1087. return -ENODEV;
  1088. }
  1089. vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
  1090. if (!vou_dev)
  1091. return -ENOMEM;
  1092. INIT_LIST_HEAD(&vou_dev->queue);
  1093. spin_lock_init(&vou_dev->lock);
  1094. mutex_init(&vou_dev->fop_lock);
  1095. atomic_set(&vou_dev->use_count, 0);
  1096. vou_dev->pdata = vou_pdata;
  1097. vou_dev->status = SH_VOU_IDLE;
  1098. rect = &vou_dev->rect;
  1099. pix = &vou_dev->pix;
  1100. /* Fill in defaults */
  1101. vou_dev->std = sh_vou_video_template.current_norm;
  1102. rect->left = 0;
  1103. rect->top = 0;
  1104. rect->width = VOU_MAX_IMAGE_WIDTH;
  1105. rect->height = 480;
  1106. pix->width = VOU_MAX_IMAGE_WIDTH;
  1107. pix->height = 480;
  1108. pix->pixelformat = V4L2_PIX_FMT_YVYU;
  1109. pix->field = V4L2_FIELD_NONE;
  1110. pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
  1111. pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
  1112. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1113. region = request_mem_region(reg_res->start, resource_size(reg_res),
  1114. pdev->name);
  1115. if (!region) {
  1116. dev_err(&pdev->dev, "VOU region already claimed\n");
  1117. ret = -EBUSY;
  1118. goto ereqmemreg;
  1119. }
  1120. vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
  1121. if (!vou_dev->base) {
  1122. ret = -ENOMEM;
  1123. goto emap;
  1124. }
  1125. ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
  1126. if (ret < 0)
  1127. goto ereqirq;
  1128. ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
  1129. if (ret < 0) {
  1130. dev_err(&pdev->dev, "Error registering v4l2 device\n");
  1131. goto ev4l2devreg;
  1132. }
  1133. /* Allocate memory for video device */
  1134. vdev = video_device_alloc();
  1135. if (vdev == NULL) {
  1136. ret = -ENOMEM;
  1137. goto evdevalloc;
  1138. }
  1139. *vdev = sh_vou_video_template;
  1140. if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
  1141. vdev->tvnorms |= V4L2_STD_PAL;
  1142. vdev->v4l2_dev = &vou_dev->v4l2_dev;
  1143. vdev->release = video_device_release;
  1144. vdev->lock = &vou_dev->fop_lock;
  1145. vou_dev->vdev = vdev;
  1146. video_set_drvdata(vdev, vou_dev);
  1147. pm_runtime_enable(&pdev->dev);
  1148. pm_runtime_resume(&pdev->dev);
  1149. i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
  1150. if (!i2c_adap) {
  1151. ret = -ENODEV;
  1152. goto ei2cgadap;
  1153. }
  1154. ret = sh_vou_hw_init(vou_dev);
  1155. if (ret < 0)
  1156. goto ereset;
  1157. subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
  1158. vou_pdata->board_info, NULL);
  1159. if (!subdev) {
  1160. ret = -ENOMEM;
  1161. goto ei2cnd;
  1162. }
  1163. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1164. if (ret < 0)
  1165. goto evregdev;
  1166. return 0;
  1167. evregdev:
  1168. ei2cnd:
  1169. ereset:
  1170. i2c_put_adapter(i2c_adap);
  1171. ei2cgadap:
  1172. video_device_release(vdev);
  1173. pm_runtime_disable(&pdev->dev);
  1174. evdevalloc:
  1175. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1176. ev4l2devreg:
  1177. free_irq(irq, vou_dev);
  1178. ereqirq:
  1179. iounmap(vou_dev->base);
  1180. emap:
  1181. release_mem_region(reg_res->start, resource_size(reg_res));
  1182. ereqmemreg:
  1183. kfree(vou_dev);
  1184. return ret;
  1185. }
  1186. static int __devexit sh_vou_remove(struct platform_device *pdev)
  1187. {
  1188. int irq = platform_get_irq(pdev, 0);
  1189. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1190. struct sh_vou_device *vou_dev = container_of(v4l2_dev,
  1191. struct sh_vou_device, v4l2_dev);
  1192. struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
  1193. struct v4l2_subdev, list);
  1194. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1195. struct resource *reg_res;
  1196. if (irq > 0)
  1197. free_irq(irq, vou_dev);
  1198. pm_runtime_disable(&pdev->dev);
  1199. video_unregister_device(vou_dev->vdev);
  1200. i2c_put_adapter(client->adapter);
  1201. v4l2_device_unregister(&vou_dev->v4l2_dev);
  1202. iounmap(vou_dev->base);
  1203. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1204. if (reg_res)
  1205. release_mem_region(reg_res->start, resource_size(reg_res));
  1206. kfree(vou_dev);
  1207. return 0;
  1208. }
  1209. static struct platform_driver __refdata sh_vou = {
  1210. .remove = __devexit_p(sh_vou_remove),
  1211. .driver = {
  1212. .name = "sh-vou",
  1213. .owner = THIS_MODULE,
  1214. },
  1215. };
  1216. static int __init sh_vou_init(void)
  1217. {
  1218. return platform_driver_probe(&sh_vou, sh_vou_probe);
  1219. }
  1220. static void __exit sh_vou_exit(void)
  1221. {
  1222. platform_driver_unregister(&sh_vou);
  1223. }
  1224. module_init(sh_vou_init);
  1225. module_exit(sh_vou_exit);
  1226. MODULE_DESCRIPTION("SuperH VOU driver");
  1227. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  1228. MODULE_LICENSE("GPL v2");
  1229. MODULE_VERSION("0.1.0");
  1230. MODULE_ALIAS("platform:sh-vou");