sh_vou.c 37 KB

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