sh_vou.c 37 KB

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