sh_vou.c 39 KB

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