rcar_fdp1.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. /*
  2. * Renesas R-Car Fine Display Processor
  3. *
  4. * Video format converter and frame deinterlacer device.
  5. *
  6. * Author: Kieran Bingham, <kieran@bingham.xyz>
  7. * Copyright (c) 2016 Renesas Electronics Corporation.
  8. *
  9. * This code is developed and inspired from the vim2m, rcar_jpu,
  10. * m2m-deinterlace, and vsp1 drivers.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the
  15. * License, or (at your option) any later version
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/fs.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/timer.h>
  30. #include <media/rcar-fcp.h>
  31. #include <media/v4l2-ctrls.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-event.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-mem2mem.h>
  36. #include <media/videobuf2-dma-contig.h>
  37. static unsigned int debug;
  38. module_param(debug, uint, 0644);
  39. MODULE_PARM_DESC(debug, "activate debug info");
  40. /* Minimum and maximum frame width/height */
  41. #define FDP1_MIN_W 80U
  42. #define FDP1_MIN_H 80U
  43. #define FDP1_MAX_W 3840U
  44. #define FDP1_MAX_H 2160U
  45. #define FDP1_MAX_PLANES 3U
  46. #define FDP1_MAX_STRIDE 8190U
  47. /* Flags that indicate a format can be used for capture/output */
  48. #define FDP1_CAPTURE BIT(0)
  49. #define FDP1_OUTPUT BIT(1)
  50. #define DRIVER_NAME "rcar_fdp1"
  51. /* Number of Job's to have available on the processing queue */
  52. #define FDP1_NUMBER_JOBS 8
  53. #define dprintk(fdp1, fmt, arg...) \
  54. v4l2_dbg(1, debug, &fdp1->v4l2_dev, "%s: " fmt, __func__, ## arg)
  55. /*
  56. * FDP1 registers and bits
  57. */
  58. /* FDP1 start register - Imm */
  59. #define FD1_CTL_CMD 0x0000
  60. #define FD1_CTL_CMD_STRCMD BIT(0)
  61. /* Sync generator register - Imm */
  62. #define FD1_CTL_SGCMD 0x0004
  63. #define FD1_CTL_SGCMD_SGEN BIT(0)
  64. /* Register set end register - Imm */
  65. #define FD1_CTL_REGEND 0x0008
  66. #define FD1_CTL_REGEND_REGEND BIT(0)
  67. /* Channel activation register - Vupdt */
  68. #define FD1_CTL_CHACT 0x000c
  69. #define FD1_CTL_CHACT_SMW BIT(9)
  70. #define FD1_CTL_CHACT_WR BIT(8)
  71. #define FD1_CTL_CHACT_SMR BIT(3)
  72. #define FD1_CTL_CHACT_RD2 BIT(2)
  73. #define FD1_CTL_CHACT_RD1 BIT(1)
  74. #define FD1_CTL_CHACT_RD0 BIT(0)
  75. /* Operation Mode Register - Vupdt */
  76. #define FD1_CTL_OPMODE 0x0010
  77. #define FD1_CTL_OPMODE_PRG BIT(4)
  78. #define FD1_CTL_OPMODE_VIMD_INTERRUPT (0 << 0)
  79. #define FD1_CTL_OPMODE_VIMD_BESTEFFORT (1 << 0)
  80. #define FD1_CTL_OPMODE_VIMD_NOINTERRUPT (2 << 0)
  81. #define FD1_CTL_VPERIOD 0x0014
  82. #define FD1_CTL_CLKCTRL 0x0018
  83. #define FD1_CTL_CLKCTRL_CSTP_N BIT(0)
  84. /* Software reset register */
  85. #define FD1_CTL_SRESET 0x001c
  86. #define FD1_CTL_SRESET_SRST BIT(0)
  87. /* Control status register (V-update-status) */
  88. #define FD1_CTL_STATUS 0x0024
  89. #define FD1_CTL_STATUS_VINT_CNT_MASK GENMASK(31, 16)
  90. #define FD1_CTL_STATUS_VINT_CNT_SHIFT 16
  91. #define FD1_CTL_STATUS_SGREGSET BIT(10)
  92. #define FD1_CTL_STATUS_SGVERR BIT(9)
  93. #define FD1_CTL_STATUS_SGFREND BIT(8)
  94. #define FD1_CTL_STATUS_BSY BIT(0)
  95. #define FD1_CTL_VCYCLE_STAT 0x0028
  96. /* Interrupt enable register */
  97. #define FD1_CTL_IRQENB 0x0038
  98. /* Interrupt status register */
  99. #define FD1_CTL_IRQSTA 0x003c
  100. /* Interrupt control register */
  101. #define FD1_CTL_IRQFSET 0x0040
  102. /* Common IRQ Bit settings */
  103. #define FD1_CTL_IRQ_VERE BIT(16)
  104. #define FD1_CTL_IRQ_VINTE BIT(4)
  105. #define FD1_CTL_IRQ_FREE BIT(0)
  106. #define FD1_CTL_IRQ_MASK (FD1_CTL_IRQ_VERE | \
  107. FD1_CTL_IRQ_VINTE | \
  108. FD1_CTL_IRQ_FREE)
  109. /* RPF */
  110. #define FD1_RPF_SIZE 0x0060
  111. #define FD1_RPF_SIZE_MASK GENMASK(12, 0)
  112. #define FD1_RPF_SIZE_H_SHIFT 16
  113. #define FD1_RPF_SIZE_V_SHIFT 0
  114. #define FD1_RPF_FORMAT 0x0064
  115. #define FD1_RPF_FORMAT_CIPM BIT(16)
  116. #define FD1_RPF_FORMAT_RSPYCS BIT(13)
  117. #define FD1_RPF_FORMAT_RSPUVS BIT(12)
  118. #define FD1_RPF_FORMAT_CF BIT(8)
  119. #define FD1_RPF_PSTRIDE 0x0068
  120. #define FD1_RPF_PSTRIDE_Y_SHIFT 16
  121. #define FD1_RPF_PSTRIDE_C_SHIFT 0
  122. /* RPF0 Source Component Y Address register */
  123. #define FD1_RPF0_ADDR_Y 0x006c
  124. /* RPF1 Current Picture Registers */
  125. #define FD1_RPF1_ADDR_Y 0x0078
  126. #define FD1_RPF1_ADDR_C0 0x007c
  127. #define FD1_RPF1_ADDR_C1 0x0080
  128. /* RPF2 next picture register */
  129. #define FD1_RPF2_ADDR_Y 0x0084
  130. #define FD1_RPF_SMSK_ADDR 0x0090
  131. #define FD1_RPF_SWAP 0x0094
  132. /* WPF */
  133. #define FD1_WPF_FORMAT 0x00c0
  134. #define FD1_WPF_FORMAT_PDV_SHIFT 24
  135. #define FD1_WPF_FORMAT_FCNL BIT(20)
  136. #define FD1_WPF_FORMAT_WSPYCS BIT(15)
  137. #define FD1_WPF_FORMAT_WSPUVS BIT(14)
  138. #define FD1_WPF_FORMAT_WRTM_601_16 (0 << 9)
  139. #define FD1_WPF_FORMAT_WRTM_601_0 (1 << 9)
  140. #define FD1_WPF_FORMAT_WRTM_709_16 (2 << 9)
  141. #define FD1_WPF_FORMAT_CSC BIT(8)
  142. #define FD1_WPF_RNDCTL 0x00c4
  143. #define FD1_WPF_RNDCTL_CBRM BIT(28)
  144. #define FD1_WPF_RNDCTL_CLMD_NOCLIP (0 << 12)
  145. #define FD1_WPF_RNDCTL_CLMD_CLIP_16_235 (1 << 12)
  146. #define FD1_WPF_RNDCTL_CLMD_CLIP_1_254 (2 << 12)
  147. #define FD1_WPF_PSTRIDE 0x00c8
  148. #define FD1_WPF_PSTRIDE_Y_SHIFT 16
  149. #define FD1_WPF_PSTRIDE_C_SHIFT 0
  150. /* WPF Destination picture */
  151. #define FD1_WPF_ADDR_Y 0x00cc
  152. #define FD1_WPF_ADDR_C0 0x00d0
  153. #define FD1_WPF_ADDR_C1 0x00d4
  154. #define FD1_WPF_SWAP 0x00d8
  155. #define FD1_WPF_SWAP_OSWAP_SHIFT 0
  156. #define FD1_WPF_SWAP_SSWAP_SHIFT 4
  157. /* WPF/RPF Common */
  158. #define FD1_RWPF_SWAP_BYTE BIT(0)
  159. #define FD1_RWPF_SWAP_WORD BIT(1)
  160. #define FD1_RWPF_SWAP_LWRD BIT(2)
  161. #define FD1_RWPF_SWAP_LLWD BIT(3)
  162. /* IPC */
  163. #define FD1_IPC_MODE 0x0100
  164. #define FD1_IPC_MODE_DLI BIT(8)
  165. #define FD1_IPC_MODE_DIM_ADAPT2D3D (0 << 0)
  166. #define FD1_IPC_MODE_DIM_FIXED2D (1 << 0)
  167. #define FD1_IPC_MODE_DIM_FIXED3D (2 << 0)
  168. #define FD1_IPC_MODE_DIM_PREVFIELD (3 << 0)
  169. #define FD1_IPC_MODE_DIM_NEXTFIELD (4 << 0)
  170. #define FD1_IPC_SMSK_THRESH 0x0104
  171. #define FD1_IPC_SMSK_THRESH_CONST 0x00010002
  172. #define FD1_IPC_COMB_DET 0x0108
  173. #define FD1_IPC_COMB_DET_CONST 0x00200040
  174. #define FD1_IPC_MOTDEC 0x010c
  175. #define FD1_IPC_MOTDEC_CONST 0x00008020
  176. /* DLI registers */
  177. #define FD1_IPC_DLI_BLEND 0x0120
  178. #define FD1_IPC_DLI_BLEND_CONST 0x0080ff02
  179. #define FD1_IPC_DLI_HGAIN 0x0124
  180. #define FD1_IPC_DLI_HGAIN_CONST 0x001000ff
  181. #define FD1_IPC_DLI_SPRS 0x0128
  182. #define FD1_IPC_DLI_SPRS_CONST 0x009004ff
  183. #define FD1_IPC_DLI_ANGLE 0x012c
  184. #define FD1_IPC_DLI_ANGLE_CONST 0x0004080c
  185. #define FD1_IPC_DLI_ISOPIX0 0x0130
  186. #define FD1_IPC_DLI_ISOPIX0_CONST 0xff10ff10
  187. #define FD1_IPC_DLI_ISOPIX1 0x0134
  188. #define FD1_IPC_DLI_ISOPIX1_CONST 0x0000ff10
  189. /* Sensor registers */
  190. #define FD1_IPC_SENSOR_TH0 0x0140
  191. #define FD1_IPC_SENSOR_TH0_CONST 0x20208080
  192. #define FD1_IPC_SENSOR_TH1 0x0144
  193. #define FD1_IPC_SENSOR_TH1_CONST 0
  194. #define FD1_IPC_SENSOR_CTL0 0x0170
  195. #define FD1_IPC_SENSOR_CTL0_CONST 0x00002201
  196. #define FD1_IPC_SENSOR_CTL1 0x0174
  197. #define FD1_IPC_SENSOR_CTL1_CONST 0
  198. #define FD1_IPC_SENSOR_CTL2 0x0178
  199. #define FD1_IPC_SENSOR_CTL2_X_SHIFT 16
  200. #define FD1_IPC_SENSOR_CTL2_Y_SHIFT 0
  201. #define FD1_IPC_SENSOR_CTL3 0x017c
  202. #define FD1_IPC_SENSOR_CTL3_0_SHIFT 16
  203. #define FD1_IPC_SENSOR_CTL3_1_SHIFT 0
  204. /* Line memory pixel number register */
  205. #define FD1_IPC_LMEM 0x01e0
  206. #define FD1_IPC_LMEM_LINEAR 1024
  207. #define FD1_IPC_LMEM_TILE 960
  208. /* Internal Data (HW Version) */
  209. #define FD1_IP_INTDATA 0x0800
  210. #define FD1_IP_H3_ES1 0x02010101
  211. #define FD1_IP_M3W 0x02010202
  212. #define FD1_IP_H3 0x02010203
  213. /* LUTs */
  214. #define FD1_LUT_DIF_ADJ 0x1000
  215. #define FD1_LUT_SAD_ADJ 0x1400
  216. #define FD1_LUT_BLD_GAIN 0x1800
  217. #define FD1_LUT_DIF_GAIN 0x1c00
  218. #define FD1_LUT_MDET 0x2000
  219. /**
  220. * struct fdp1_fmt - The FDP1 internal format data
  221. * @fourcc: the fourcc code, to match the V4L2 API
  222. * @bpp: bits per pixel per plane
  223. * @num_planes: number of planes
  224. * @hsub: horizontal subsampling factor
  225. * @vsub: vertical subsampling factor
  226. * @fmt: 7-bit format code for the fdp1 hardware
  227. * @swap_yc: the Y and C components are swapped (Y comes before C)
  228. * @swap_uv: the U and V components are swapped (V comes before U)
  229. * @swap: swap register control
  230. * @types: types of queue this format is applicable to
  231. */
  232. struct fdp1_fmt {
  233. u32 fourcc;
  234. u8 bpp[3];
  235. u8 num_planes;
  236. u8 hsub;
  237. u8 vsub;
  238. u8 fmt;
  239. bool swap_yc;
  240. bool swap_uv;
  241. u8 swap;
  242. u8 types;
  243. };
  244. static const struct fdp1_fmt fdp1_formats[] = {
  245. /* RGB formats are only supported by the Write Pixel Formatter */
  246. { V4L2_PIX_FMT_RGB332, { 8, 0, 0 }, 1, 1, 1, 0x00, false, false,
  247. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  248. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  249. FDP1_CAPTURE },
  250. { V4L2_PIX_FMT_XRGB444, { 16, 0, 0 }, 1, 1, 1, 0x01, false, false,
  251. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  252. FD1_RWPF_SWAP_WORD,
  253. FDP1_CAPTURE },
  254. { V4L2_PIX_FMT_XRGB555, { 16, 0, 0 }, 1, 1, 1, 0x04, false, false,
  255. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  256. FD1_RWPF_SWAP_WORD,
  257. FDP1_CAPTURE },
  258. { V4L2_PIX_FMT_RGB565, { 16, 0, 0 }, 1, 1, 1, 0x06, false, false,
  259. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  260. FD1_RWPF_SWAP_WORD,
  261. FDP1_CAPTURE },
  262. { V4L2_PIX_FMT_ABGR32, { 32, 0, 0 }, 1, 1, 1, 0x13, false, false,
  263. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD,
  264. FDP1_CAPTURE },
  265. { V4L2_PIX_FMT_XBGR32, { 32, 0, 0 }, 1, 1, 1, 0x13, false, false,
  266. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD,
  267. FDP1_CAPTURE },
  268. { V4L2_PIX_FMT_ARGB32, { 32, 0, 0 }, 1, 1, 1, 0x13, false, false,
  269. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  270. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  271. FDP1_CAPTURE },
  272. { V4L2_PIX_FMT_XRGB32, { 32, 0, 0 }, 1, 1, 1, 0x13, false, false,
  273. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  274. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  275. FDP1_CAPTURE },
  276. { V4L2_PIX_FMT_RGB24, { 24, 0, 0 }, 1, 1, 1, 0x15, false, false,
  277. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  278. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  279. FDP1_CAPTURE },
  280. { V4L2_PIX_FMT_BGR24, { 24, 0, 0 }, 1, 1, 1, 0x18, false, false,
  281. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  282. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  283. FDP1_CAPTURE },
  284. { V4L2_PIX_FMT_ARGB444, { 16, 0, 0 }, 1, 1, 1, 0x19, false, false,
  285. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  286. FD1_RWPF_SWAP_WORD,
  287. FDP1_CAPTURE },
  288. { V4L2_PIX_FMT_ARGB555, { 16, 0, 0 }, 1, 1, 1, 0x1b, false, false,
  289. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  290. FD1_RWPF_SWAP_WORD,
  291. FDP1_CAPTURE },
  292. /* YUV Formats are supported by Read and Write Pixel Formatters */
  293. { V4L2_PIX_FMT_NV16M, { 8, 16, 0 }, 2, 2, 1, 0x41, false, false,
  294. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  295. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  296. FDP1_CAPTURE | FDP1_OUTPUT },
  297. { V4L2_PIX_FMT_NV61M, { 8, 16, 0 }, 2, 2, 1, 0x41, false, true,
  298. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  299. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  300. FDP1_CAPTURE | FDP1_OUTPUT },
  301. { V4L2_PIX_FMT_NV12M, { 8, 16, 0 }, 2, 2, 2, 0x42, false, false,
  302. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  303. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  304. FDP1_CAPTURE | FDP1_OUTPUT },
  305. { V4L2_PIX_FMT_NV21M, { 8, 16, 0 }, 2, 2, 2, 0x42, false, true,
  306. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  307. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  308. FDP1_CAPTURE | FDP1_OUTPUT },
  309. { V4L2_PIX_FMT_UYVY, { 16, 0, 0 }, 1, 2, 1, 0x47, false, false,
  310. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  311. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  312. FDP1_CAPTURE | FDP1_OUTPUT },
  313. { V4L2_PIX_FMT_VYUY, { 16, 0, 0 }, 1, 2, 1, 0x47, false, true,
  314. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  315. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  316. FDP1_CAPTURE | FDP1_OUTPUT },
  317. { V4L2_PIX_FMT_YUYV, { 16, 0, 0 }, 1, 2, 1, 0x47, true, false,
  318. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  319. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  320. FDP1_CAPTURE | FDP1_OUTPUT },
  321. { V4L2_PIX_FMT_YVYU, { 16, 0, 0 }, 1, 2, 1, 0x47, true, true,
  322. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  323. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  324. FDP1_CAPTURE | FDP1_OUTPUT },
  325. { V4L2_PIX_FMT_YUV444M, { 8, 8, 8 }, 3, 1, 1, 0x4a, false, false,
  326. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  327. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  328. FDP1_CAPTURE | FDP1_OUTPUT },
  329. { V4L2_PIX_FMT_YVU444M, { 8, 8, 8 }, 3, 1, 1, 0x4a, false, true,
  330. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  331. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  332. FDP1_CAPTURE | FDP1_OUTPUT },
  333. { V4L2_PIX_FMT_YUV422M, { 8, 8, 8 }, 3, 2, 1, 0x4b, false, false,
  334. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  335. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  336. FDP1_CAPTURE | FDP1_OUTPUT },
  337. { V4L2_PIX_FMT_YVU422M, { 8, 8, 8 }, 3, 2, 1, 0x4b, false, true,
  338. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  339. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  340. FDP1_CAPTURE | FDP1_OUTPUT },
  341. { V4L2_PIX_FMT_YUV420M, { 8, 8, 8 }, 3, 2, 2, 0x4c, false, false,
  342. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  343. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  344. FDP1_CAPTURE | FDP1_OUTPUT },
  345. { V4L2_PIX_FMT_YVU420M, { 8, 8, 8 }, 3, 2, 2, 0x4c, false, true,
  346. FD1_RWPF_SWAP_LLWD | FD1_RWPF_SWAP_LWRD |
  347. FD1_RWPF_SWAP_WORD | FD1_RWPF_SWAP_BYTE,
  348. FDP1_CAPTURE | FDP1_OUTPUT },
  349. };
  350. static int fdp1_fmt_is_rgb(const struct fdp1_fmt *fmt)
  351. {
  352. return fmt->fmt <= 0x1b; /* Last RGB code */
  353. }
  354. /*
  355. * FDP1 Lookup tables range from 0...255 only
  356. *
  357. * Each table must be less than 256 entries, and all tables
  358. * are padded out to 256 entries by duplicating the last value.
  359. */
  360. static const u8 fdp1_diff_adj[] = {
  361. 0x00, 0x24, 0x43, 0x5e, 0x76, 0x8c, 0x9e, 0xaf,
  362. 0xbd, 0xc9, 0xd4, 0xdd, 0xe4, 0xea, 0xef, 0xf3,
  363. 0xf6, 0xf9, 0xfb, 0xfc, 0xfd, 0xfe, 0xfe, 0xff,
  364. };
  365. static const u8 fdp1_sad_adj[] = {
  366. 0x00, 0x24, 0x43, 0x5e, 0x76, 0x8c, 0x9e, 0xaf,
  367. 0xbd, 0xc9, 0xd4, 0xdd, 0xe4, 0xea, 0xef, 0xf3,
  368. 0xf6, 0xf9, 0xfb, 0xfc, 0xfd, 0xfe, 0xfe, 0xff,
  369. };
  370. static const u8 fdp1_bld_gain[] = {
  371. 0x80,
  372. };
  373. static const u8 fdp1_dif_gain[] = {
  374. 0x80,
  375. };
  376. static const u8 fdp1_mdet[] = {
  377. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  378. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  379. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  380. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  381. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  382. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  383. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  384. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  385. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  386. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  387. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  388. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  389. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  390. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  391. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  392. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  393. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  394. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  395. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  396. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  397. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  398. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  399. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  400. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  401. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  402. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  403. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  404. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  405. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  406. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  407. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  408. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  409. };
  410. /* Per-queue, driver-specific private data */
  411. struct fdp1_q_data {
  412. const struct fdp1_fmt *fmt;
  413. struct v4l2_pix_format_mplane format;
  414. unsigned int vsize;
  415. unsigned int stride_y;
  416. unsigned int stride_c;
  417. };
  418. static const struct fdp1_fmt *fdp1_find_format(u32 pixelformat)
  419. {
  420. const struct fdp1_fmt *fmt;
  421. unsigned int i;
  422. for (i = 0; i < ARRAY_SIZE(fdp1_formats); i++) {
  423. fmt = &fdp1_formats[i];
  424. if (fmt->fourcc == pixelformat)
  425. return fmt;
  426. }
  427. return NULL;
  428. }
  429. enum fdp1_deint_mode {
  430. FDP1_PROGRESSIVE = 0, /* Must be zero when !deinterlacing */
  431. FDP1_ADAPT2D3D,
  432. FDP1_FIXED2D,
  433. FDP1_FIXED3D,
  434. FDP1_PREVFIELD,
  435. FDP1_NEXTFIELD,
  436. };
  437. #define FDP1_DEINT_MODE_USES_NEXT(mode) \
  438. (mode == FDP1_ADAPT2D3D || \
  439. mode == FDP1_FIXED3D || \
  440. mode == FDP1_NEXTFIELD)
  441. #define FDP1_DEINT_MODE_USES_PREV(mode) \
  442. (mode == FDP1_ADAPT2D3D || \
  443. mode == FDP1_FIXED3D || \
  444. mode == FDP1_PREVFIELD)
  445. /*
  446. * FDP1 operates on potentially 3 fields, which are tracked
  447. * from the VB buffers using this context structure.
  448. * Will always be a field or a full frame, never two fields.
  449. */
  450. struct fdp1_field_buffer {
  451. struct vb2_v4l2_buffer *vb;
  452. dma_addr_t addrs[3];
  453. /* Should be NONE:TOP:BOTTOM only */
  454. enum v4l2_field field;
  455. /* Flag to indicate this is the last field in the vb */
  456. bool last_field;
  457. /* Buffer queue lists */
  458. struct list_head list;
  459. };
  460. struct fdp1_buffer {
  461. struct v4l2_m2m_buffer m2m_buf;
  462. struct fdp1_field_buffer fields[2];
  463. unsigned int num_fields;
  464. };
  465. static inline struct fdp1_buffer *to_fdp1_buffer(struct vb2_v4l2_buffer *vb)
  466. {
  467. return container_of(vb, struct fdp1_buffer, m2m_buf.vb);
  468. }
  469. struct fdp1_job {
  470. struct fdp1_field_buffer *previous;
  471. struct fdp1_field_buffer *active;
  472. struct fdp1_field_buffer *next;
  473. struct fdp1_field_buffer *dst;
  474. /* A job can only be on one list at a time */
  475. struct list_head list;
  476. };
  477. struct fdp1_dev {
  478. struct v4l2_device v4l2_dev;
  479. struct video_device vfd;
  480. struct mutex dev_mutex;
  481. spinlock_t irqlock;
  482. spinlock_t device_process_lock;
  483. void __iomem *regs;
  484. unsigned int irq;
  485. struct device *dev;
  486. /* Job Queues */
  487. struct fdp1_job jobs[FDP1_NUMBER_JOBS];
  488. struct list_head free_job_list;
  489. struct list_head queued_job_list;
  490. struct list_head hw_job_list;
  491. unsigned int clk_rate;
  492. struct rcar_fcp_device *fcp;
  493. struct v4l2_m2m_dev *m2m_dev;
  494. };
  495. struct fdp1_ctx {
  496. struct v4l2_fh fh;
  497. struct fdp1_dev *fdp1;
  498. struct v4l2_ctrl_handler hdl;
  499. unsigned int sequence;
  500. /* Processed buffers in this transaction */
  501. u8 num_processed;
  502. /* Transaction length (i.e. how many buffers per transaction) */
  503. u32 translen;
  504. /* Abort requested by m2m */
  505. int aborting;
  506. /* Deinterlace processing mode */
  507. enum fdp1_deint_mode deint_mode;
  508. /*
  509. * Adaptive 2D/3D mode uses a shared mask
  510. * This is allocated at streamon, if the ADAPT2D3D mode
  511. * is requested
  512. */
  513. unsigned int smsk_size;
  514. dma_addr_t smsk_addr[2];
  515. void *smsk_cpu;
  516. /* Capture pipeline, can specify an alpha value
  517. * for supported formats. 0-255 only
  518. */
  519. unsigned char alpha;
  520. /* Source and destination queue data */
  521. struct fdp1_q_data out_q; /* HW Source */
  522. struct fdp1_q_data cap_q; /* HW Destination */
  523. /*
  524. * Field Queues
  525. * Interlaced fields are used on 3 occasions, and tracked in this list.
  526. *
  527. * V4L2 Buffers are tracked inside the fdp1_buffer
  528. * and released when the last 'field' completes
  529. */
  530. struct list_head fields_queue;
  531. unsigned int buffers_queued;
  532. /*
  533. * For de-interlacing we need to track our previous buffer
  534. * while preparing our job lists.
  535. */
  536. struct fdp1_field_buffer *previous;
  537. };
  538. static inline struct fdp1_ctx *fh_to_ctx(struct v4l2_fh *fh)
  539. {
  540. return container_of(fh, struct fdp1_ctx, fh);
  541. }
  542. static struct fdp1_q_data *get_q_data(struct fdp1_ctx *ctx,
  543. enum v4l2_buf_type type)
  544. {
  545. if (V4L2_TYPE_IS_OUTPUT(type))
  546. return &ctx->out_q;
  547. else
  548. return &ctx->cap_q;
  549. }
  550. /*
  551. * list_remove_job: Take the first item off the specified job list
  552. *
  553. * Returns: pointer to a job, or NULL if the list is empty.
  554. */
  555. static struct fdp1_job *list_remove_job(struct fdp1_dev *fdp1,
  556. struct list_head *list)
  557. {
  558. struct fdp1_job *job;
  559. unsigned long flags;
  560. spin_lock_irqsave(&fdp1->irqlock, flags);
  561. job = list_first_entry_or_null(list, struct fdp1_job, list);
  562. if (job)
  563. list_del(&job->list);
  564. spin_unlock_irqrestore(&fdp1->irqlock, flags);
  565. return job;
  566. }
  567. /*
  568. * list_add_job: Add a job to the specified job list
  569. *
  570. * Returns: void - always succeeds
  571. */
  572. static void list_add_job(struct fdp1_dev *fdp1,
  573. struct list_head *list,
  574. struct fdp1_job *job)
  575. {
  576. unsigned long flags;
  577. spin_lock_irqsave(&fdp1->irqlock, flags);
  578. list_add_tail(&job->list, list);
  579. spin_unlock_irqrestore(&fdp1->irqlock, flags);
  580. }
  581. static struct fdp1_job *fdp1_job_alloc(struct fdp1_dev *fdp1)
  582. {
  583. return list_remove_job(fdp1, &fdp1->free_job_list);
  584. }
  585. static void fdp1_job_free(struct fdp1_dev *fdp1, struct fdp1_job *job)
  586. {
  587. /* Ensure that all residue from previous jobs is gone */
  588. memset(job, 0, sizeof(struct fdp1_job));
  589. list_add_job(fdp1, &fdp1->free_job_list, job);
  590. }
  591. static void queue_job(struct fdp1_dev *fdp1, struct fdp1_job *job)
  592. {
  593. list_add_job(fdp1, &fdp1->queued_job_list, job);
  594. }
  595. static struct fdp1_job *get_queued_job(struct fdp1_dev *fdp1)
  596. {
  597. return list_remove_job(fdp1, &fdp1->queued_job_list);
  598. }
  599. static void queue_hw_job(struct fdp1_dev *fdp1, struct fdp1_job *job)
  600. {
  601. list_add_job(fdp1, &fdp1->hw_job_list, job);
  602. }
  603. static struct fdp1_job *get_hw_queued_job(struct fdp1_dev *fdp1)
  604. {
  605. return list_remove_job(fdp1, &fdp1->hw_job_list);
  606. }
  607. /*
  608. * Buffer lists handling
  609. */
  610. static void fdp1_field_complete(struct fdp1_ctx *ctx,
  611. struct fdp1_field_buffer *fbuf)
  612. {
  613. /* job->previous may be on the first field */
  614. if (!fbuf)
  615. return;
  616. if (fbuf->last_field)
  617. v4l2_m2m_buf_done(fbuf->vb, VB2_BUF_STATE_DONE);
  618. }
  619. static void fdp1_queue_field(struct fdp1_ctx *ctx,
  620. struct fdp1_field_buffer *fbuf)
  621. {
  622. unsigned long flags;
  623. spin_lock_irqsave(&ctx->fdp1->irqlock, flags);
  624. list_add_tail(&fbuf->list, &ctx->fields_queue);
  625. spin_unlock_irqrestore(&ctx->fdp1->irqlock, flags);
  626. ctx->buffers_queued++;
  627. }
  628. static struct fdp1_field_buffer *fdp1_dequeue_field(struct fdp1_ctx *ctx)
  629. {
  630. struct fdp1_field_buffer *fbuf;
  631. unsigned long flags;
  632. ctx->buffers_queued--;
  633. spin_lock_irqsave(&ctx->fdp1->irqlock, flags);
  634. fbuf = list_first_entry_or_null(&ctx->fields_queue,
  635. struct fdp1_field_buffer, list);
  636. if (fbuf)
  637. list_del(&fbuf->list);
  638. spin_unlock_irqrestore(&ctx->fdp1->irqlock, flags);
  639. return fbuf;
  640. }
  641. /*
  642. * Return the next field in the queue - or NULL,
  643. * without removing the item from the list
  644. */
  645. static struct fdp1_field_buffer *fdp1_peek_queued_field(struct fdp1_ctx *ctx)
  646. {
  647. struct fdp1_field_buffer *fbuf;
  648. unsigned long flags;
  649. spin_lock_irqsave(&ctx->fdp1->irqlock, flags);
  650. fbuf = list_first_entry_or_null(&ctx->fields_queue,
  651. struct fdp1_field_buffer, list);
  652. spin_unlock_irqrestore(&ctx->fdp1->irqlock, flags);
  653. return fbuf;
  654. }
  655. static u32 fdp1_read(struct fdp1_dev *fdp1, unsigned int reg)
  656. {
  657. u32 value = ioread32(fdp1->regs + reg);
  658. if (debug >= 2)
  659. dprintk(fdp1, "Read 0x%08x from 0x%04x\n", value, reg);
  660. return value;
  661. }
  662. static void fdp1_write(struct fdp1_dev *fdp1, u32 val, unsigned int reg)
  663. {
  664. if (debug >= 2)
  665. dprintk(fdp1, "Write 0x%08x to 0x%04x\n", val, reg);
  666. iowrite32(val, fdp1->regs + reg);
  667. }
  668. /* IPC registers are to be programmed with constant values */
  669. static void fdp1_set_ipc_dli(struct fdp1_ctx *ctx)
  670. {
  671. struct fdp1_dev *fdp1 = ctx->fdp1;
  672. fdp1_write(fdp1, FD1_IPC_SMSK_THRESH_CONST, FD1_IPC_SMSK_THRESH);
  673. fdp1_write(fdp1, FD1_IPC_COMB_DET_CONST, FD1_IPC_COMB_DET);
  674. fdp1_write(fdp1, FD1_IPC_MOTDEC_CONST, FD1_IPC_MOTDEC);
  675. fdp1_write(fdp1, FD1_IPC_DLI_BLEND_CONST, FD1_IPC_DLI_BLEND);
  676. fdp1_write(fdp1, FD1_IPC_DLI_HGAIN_CONST, FD1_IPC_DLI_HGAIN);
  677. fdp1_write(fdp1, FD1_IPC_DLI_SPRS_CONST, FD1_IPC_DLI_SPRS);
  678. fdp1_write(fdp1, FD1_IPC_DLI_ANGLE_CONST, FD1_IPC_DLI_ANGLE);
  679. fdp1_write(fdp1, FD1_IPC_DLI_ISOPIX0_CONST, FD1_IPC_DLI_ISOPIX0);
  680. fdp1_write(fdp1, FD1_IPC_DLI_ISOPIX1_CONST, FD1_IPC_DLI_ISOPIX1);
  681. }
  682. static void fdp1_set_ipc_sensor(struct fdp1_ctx *ctx)
  683. {
  684. struct fdp1_dev *fdp1 = ctx->fdp1;
  685. struct fdp1_q_data *src_q_data = &ctx->out_q;
  686. unsigned int x0, x1;
  687. unsigned int hsize = src_q_data->format.width;
  688. unsigned int vsize = src_q_data->format.height;
  689. x0 = hsize / 3;
  690. x1 = 2 * hsize / 3;
  691. fdp1_write(fdp1, FD1_IPC_SENSOR_TH0_CONST, FD1_IPC_SENSOR_TH0);
  692. fdp1_write(fdp1, FD1_IPC_SENSOR_TH1_CONST, FD1_IPC_SENSOR_TH1);
  693. fdp1_write(fdp1, FD1_IPC_SENSOR_CTL0_CONST, FD1_IPC_SENSOR_CTL0);
  694. fdp1_write(fdp1, FD1_IPC_SENSOR_CTL1_CONST, FD1_IPC_SENSOR_CTL1);
  695. fdp1_write(fdp1, ((hsize - 1) << FD1_IPC_SENSOR_CTL2_X_SHIFT) |
  696. ((vsize - 1) << FD1_IPC_SENSOR_CTL2_Y_SHIFT),
  697. FD1_IPC_SENSOR_CTL2);
  698. fdp1_write(fdp1, (x0 << FD1_IPC_SENSOR_CTL3_0_SHIFT) |
  699. (x1 << FD1_IPC_SENSOR_CTL3_1_SHIFT),
  700. FD1_IPC_SENSOR_CTL3);
  701. }
  702. /*
  703. * fdp1_write_lut: Write a padded LUT to the hw
  704. *
  705. * FDP1 uses constant data for de-interlacing processing,
  706. * with large tables. These hardware tables are all 256 bytes
  707. * long, however they often contain repeated data at the end.
  708. *
  709. * The last byte of the table is written to all remaining entries.
  710. */
  711. static void fdp1_write_lut(struct fdp1_dev *fdp1, const u8 *lut,
  712. unsigned int len, unsigned int base)
  713. {
  714. unsigned int i;
  715. u8 pad;
  716. /* Tables larger than the hw are clipped */
  717. len = min(len, 256u);
  718. for (i = 0; i < len; i++)
  719. fdp1_write(fdp1, lut[i], base + (i*4));
  720. /* Tables are padded with the last entry */
  721. pad = lut[i-1];
  722. for (; i < 256; i++)
  723. fdp1_write(fdp1, pad, base + (i*4));
  724. }
  725. static void fdp1_set_lut(struct fdp1_dev *fdp1)
  726. {
  727. fdp1_write_lut(fdp1, fdp1_diff_adj, ARRAY_SIZE(fdp1_diff_adj),
  728. FD1_LUT_DIF_ADJ);
  729. fdp1_write_lut(fdp1, fdp1_sad_adj, ARRAY_SIZE(fdp1_sad_adj),
  730. FD1_LUT_SAD_ADJ);
  731. fdp1_write_lut(fdp1, fdp1_bld_gain, ARRAY_SIZE(fdp1_bld_gain),
  732. FD1_LUT_BLD_GAIN);
  733. fdp1_write_lut(fdp1, fdp1_dif_gain, ARRAY_SIZE(fdp1_dif_gain),
  734. FD1_LUT_DIF_GAIN);
  735. fdp1_write_lut(fdp1, fdp1_mdet, ARRAY_SIZE(fdp1_mdet),
  736. FD1_LUT_MDET);
  737. }
  738. static void fdp1_configure_rpf(struct fdp1_ctx *ctx,
  739. struct fdp1_job *job)
  740. {
  741. struct fdp1_dev *fdp1 = ctx->fdp1;
  742. u32 picture_size;
  743. u32 pstride;
  744. u32 format;
  745. u32 smsk_addr;
  746. struct fdp1_q_data *q_data = &ctx->out_q;
  747. /* Picture size is common to Source and Destination frames */
  748. picture_size = (q_data->format.width << FD1_RPF_SIZE_H_SHIFT)
  749. | (q_data->vsize << FD1_RPF_SIZE_V_SHIFT);
  750. /* Strides */
  751. pstride = q_data->stride_y << FD1_RPF_PSTRIDE_Y_SHIFT;
  752. if (q_data->format.num_planes > 1)
  753. pstride |= q_data->stride_c << FD1_RPF_PSTRIDE_C_SHIFT;
  754. /* Format control */
  755. format = q_data->fmt->fmt;
  756. if (q_data->fmt->swap_yc)
  757. format |= FD1_RPF_FORMAT_RSPYCS;
  758. if (q_data->fmt->swap_uv)
  759. format |= FD1_RPF_FORMAT_RSPUVS;
  760. if (job->active->field == V4L2_FIELD_BOTTOM) {
  761. format |= FD1_RPF_FORMAT_CF; /* Set for Bottom field */
  762. smsk_addr = ctx->smsk_addr[0];
  763. } else {
  764. smsk_addr = ctx->smsk_addr[1];
  765. }
  766. /* Deint mode is non-zero when deinterlacing */
  767. if (ctx->deint_mode)
  768. format |= FD1_RPF_FORMAT_CIPM;
  769. fdp1_write(fdp1, format, FD1_RPF_FORMAT);
  770. fdp1_write(fdp1, q_data->fmt->swap, FD1_RPF_SWAP);
  771. fdp1_write(fdp1, picture_size, FD1_RPF_SIZE);
  772. fdp1_write(fdp1, pstride, FD1_RPF_PSTRIDE);
  773. fdp1_write(fdp1, smsk_addr, FD1_RPF_SMSK_ADDR);
  774. /* Previous Field Channel (CH0) */
  775. if (job->previous)
  776. fdp1_write(fdp1, job->previous->addrs[0], FD1_RPF0_ADDR_Y);
  777. /* Current Field Channel (CH1) */
  778. fdp1_write(fdp1, job->active->addrs[0], FD1_RPF1_ADDR_Y);
  779. fdp1_write(fdp1, job->active->addrs[1], FD1_RPF1_ADDR_C0);
  780. fdp1_write(fdp1, job->active->addrs[2], FD1_RPF1_ADDR_C1);
  781. /* Next Field Channel (CH2) */
  782. if (job->next)
  783. fdp1_write(fdp1, job->next->addrs[0], FD1_RPF2_ADDR_Y);
  784. }
  785. static void fdp1_configure_wpf(struct fdp1_ctx *ctx,
  786. struct fdp1_job *job)
  787. {
  788. struct fdp1_dev *fdp1 = ctx->fdp1;
  789. struct fdp1_q_data *src_q_data = &ctx->out_q;
  790. struct fdp1_q_data *q_data = &ctx->cap_q;
  791. u32 pstride;
  792. u32 format;
  793. u32 swap;
  794. u32 rndctl;
  795. pstride = q_data->format.plane_fmt[0].bytesperline
  796. << FD1_WPF_PSTRIDE_Y_SHIFT;
  797. if (q_data->format.num_planes > 1)
  798. pstride |= q_data->format.plane_fmt[1].bytesperline
  799. << FD1_WPF_PSTRIDE_C_SHIFT;
  800. format = q_data->fmt->fmt; /* Output Format Code */
  801. if (q_data->fmt->swap_yc)
  802. format |= FD1_WPF_FORMAT_WSPYCS;
  803. if (q_data->fmt->swap_uv)
  804. format |= FD1_WPF_FORMAT_WSPUVS;
  805. if (fdp1_fmt_is_rgb(q_data->fmt)) {
  806. /* Enable Colour Space conversion */
  807. format |= FD1_WPF_FORMAT_CSC;
  808. /* Set WRTM */
  809. if (src_q_data->format.ycbcr_enc == V4L2_YCBCR_ENC_709)
  810. format |= FD1_WPF_FORMAT_WRTM_709_16;
  811. else if (src_q_data->format.quantization ==
  812. V4L2_QUANTIZATION_FULL_RANGE)
  813. format |= FD1_WPF_FORMAT_WRTM_601_0;
  814. else
  815. format |= FD1_WPF_FORMAT_WRTM_601_16;
  816. }
  817. /* Set an alpha value into the Pad Value */
  818. format |= ctx->alpha << FD1_WPF_FORMAT_PDV_SHIFT;
  819. /* Determine picture rounding and clipping */
  820. rndctl = FD1_WPF_RNDCTL_CBRM; /* Rounding Off */
  821. rndctl |= FD1_WPF_RNDCTL_CLMD_NOCLIP;
  822. /* WPF Swap needs both ISWAP and OSWAP setting */
  823. swap = q_data->fmt->swap << FD1_WPF_SWAP_OSWAP_SHIFT;
  824. swap |= src_q_data->fmt->swap << FD1_WPF_SWAP_SSWAP_SHIFT;
  825. fdp1_write(fdp1, format, FD1_WPF_FORMAT);
  826. fdp1_write(fdp1, rndctl, FD1_WPF_RNDCTL);
  827. fdp1_write(fdp1, swap, FD1_WPF_SWAP);
  828. fdp1_write(fdp1, pstride, FD1_WPF_PSTRIDE);
  829. fdp1_write(fdp1, job->dst->addrs[0], FD1_WPF_ADDR_Y);
  830. fdp1_write(fdp1, job->dst->addrs[1], FD1_WPF_ADDR_C0);
  831. fdp1_write(fdp1, job->dst->addrs[2], FD1_WPF_ADDR_C1);
  832. }
  833. static void fdp1_configure_deint_mode(struct fdp1_ctx *ctx,
  834. struct fdp1_job *job)
  835. {
  836. struct fdp1_dev *fdp1 = ctx->fdp1;
  837. u32 opmode = FD1_CTL_OPMODE_VIMD_NOINTERRUPT;
  838. u32 ipcmode = FD1_IPC_MODE_DLI; /* Always set */
  839. u32 channels = FD1_CTL_CHACT_WR | FD1_CTL_CHACT_RD1; /* Always on */
  840. /* De-interlacing Mode */
  841. switch (ctx->deint_mode) {
  842. default:
  843. case FDP1_PROGRESSIVE:
  844. dprintk(fdp1, "Progressive Mode\n");
  845. opmode |= FD1_CTL_OPMODE_PRG;
  846. ipcmode |= FD1_IPC_MODE_DIM_FIXED2D;
  847. break;
  848. case FDP1_ADAPT2D3D:
  849. dprintk(fdp1, "Adapt2D3D Mode\n");
  850. if (ctx->sequence == 0 || ctx->aborting)
  851. ipcmode |= FD1_IPC_MODE_DIM_FIXED2D;
  852. else
  853. ipcmode |= FD1_IPC_MODE_DIM_ADAPT2D3D;
  854. if (ctx->sequence > 1) {
  855. channels |= FD1_CTL_CHACT_SMW;
  856. channels |= FD1_CTL_CHACT_RD0 | FD1_CTL_CHACT_RD2;
  857. }
  858. if (ctx->sequence > 2)
  859. channels |= FD1_CTL_CHACT_SMR;
  860. break;
  861. case FDP1_FIXED3D:
  862. dprintk(fdp1, "Fixed 3D Mode\n");
  863. ipcmode |= FD1_IPC_MODE_DIM_FIXED3D;
  864. /* Except for first and last frame, enable all channels */
  865. if (!(ctx->sequence == 0 || ctx->aborting))
  866. channels |= FD1_CTL_CHACT_RD0 | FD1_CTL_CHACT_RD2;
  867. break;
  868. case FDP1_FIXED2D:
  869. dprintk(fdp1, "Fixed 2D Mode\n");
  870. ipcmode |= FD1_IPC_MODE_DIM_FIXED2D;
  871. /* No extra channels enabled */
  872. break;
  873. case FDP1_PREVFIELD:
  874. dprintk(fdp1, "Previous Field Mode\n");
  875. ipcmode |= FD1_IPC_MODE_DIM_PREVFIELD;
  876. channels |= FD1_CTL_CHACT_RD0; /* Previous */
  877. break;
  878. case FDP1_NEXTFIELD:
  879. dprintk(fdp1, "Next Field Mode\n");
  880. ipcmode |= FD1_IPC_MODE_DIM_NEXTFIELD;
  881. channels |= FD1_CTL_CHACT_RD2; /* Next */
  882. break;
  883. }
  884. fdp1_write(fdp1, channels, FD1_CTL_CHACT);
  885. fdp1_write(fdp1, opmode, FD1_CTL_OPMODE);
  886. fdp1_write(fdp1, ipcmode, FD1_IPC_MODE);
  887. }
  888. /*
  889. * fdp1_device_process() - Run the hardware
  890. *
  891. * Configure and start the hardware to generate a single frame
  892. * of output given our input parameters.
  893. */
  894. static int fdp1_device_process(struct fdp1_ctx *ctx)
  895. {
  896. struct fdp1_dev *fdp1 = ctx->fdp1;
  897. struct fdp1_job *job;
  898. unsigned long flags;
  899. spin_lock_irqsave(&fdp1->device_process_lock, flags);
  900. /* Get a job to process */
  901. job = get_queued_job(fdp1);
  902. if (!job) {
  903. /*
  904. * VINT can call us to see if we can queue another job.
  905. * If we have no work to do, we simply return.
  906. */
  907. spin_unlock_irqrestore(&fdp1->device_process_lock, flags);
  908. return 0;
  909. }
  910. /* First Frame only? ... */
  911. fdp1_write(fdp1, FD1_CTL_CLKCTRL_CSTP_N, FD1_CTL_CLKCTRL);
  912. /* Set the mode, and configuration */
  913. fdp1_configure_deint_mode(ctx, job);
  914. /* DLI Static Configuration */
  915. fdp1_set_ipc_dli(ctx);
  916. /* Sensor Configuration */
  917. fdp1_set_ipc_sensor(ctx);
  918. /* Setup the source picture */
  919. fdp1_configure_rpf(ctx, job);
  920. /* Setup the destination picture */
  921. fdp1_configure_wpf(ctx, job);
  922. /* Line Memory Pixel Number Register for linear access */
  923. fdp1_write(fdp1, FD1_IPC_LMEM_LINEAR, FD1_IPC_LMEM);
  924. /* Enable Interrupts */
  925. fdp1_write(fdp1, FD1_CTL_IRQ_MASK, FD1_CTL_IRQENB);
  926. /* Finally, the Immediate Registers */
  927. /* This job is now in the HW queue */
  928. queue_hw_job(fdp1, job);
  929. /* Start the command */
  930. fdp1_write(fdp1, FD1_CTL_CMD_STRCMD, FD1_CTL_CMD);
  931. /* Registers will update to HW at next VINT */
  932. fdp1_write(fdp1, FD1_CTL_REGEND_REGEND, FD1_CTL_REGEND);
  933. /* Enable VINT Generator */
  934. fdp1_write(fdp1, FD1_CTL_SGCMD_SGEN, FD1_CTL_SGCMD);
  935. spin_unlock_irqrestore(&fdp1->device_process_lock, flags);
  936. return 0;
  937. }
  938. /*
  939. * mem2mem callbacks
  940. */
  941. /*
  942. * job_ready() - check whether an instance is ready to be scheduled to run
  943. */
  944. static int fdp1_m2m_job_ready(void *priv)
  945. {
  946. struct fdp1_ctx *ctx = priv;
  947. struct fdp1_q_data *src_q_data = &ctx->out_q;
  948. int srcbufs = 1;
  949. int dstbufs = 1;
  950. dprintk(ctx->fdp1, "+ Src: %d : Dst: %d\n",
  951. v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx),
  952. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx));
  953. /* One output buffer is required for each field */
  954. if (V4L2_FIELD_HAS_BOTH(src_q_data->format.field))
  955. dstbufs = 2;
  956. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < srcbufs
  957. || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < dstbufs) {
  958. dprintk(ctx->fdp1, "Not enough buffers available\n");
  959. return 0;
  960. }
  961. return 1;
  962. }
  963. static void fdp1_m2m_job_abort(void *priv)
  964. {
  965. struct fdp1_ctx *ctx = priv;
  966. dprintk(ctx->fdp1, "+\n");
  967. /* Will cancel the transaction in the next interrupt handler */
  968. ctx->aborting = 1;
  969. /* Immediate abort sequence */
  970. fdp1_write(ctx->fdp1, 0, FD1_CTL_SGCMD);
  971. fdp1_write(ctx->fdp1, FD1_CTL_SRESET_SRST, FD1_CTL_SRESET);
  972. }
  973. /*
  974. * fdp1_prepare_job: Prepare and queue a new job for a single action of work
  975. *
  976. * Prepare the next field, (or frame in progressive) and an output
  977. * buffer for the hardware to perform a single operation.
  978. */
  979. static struct fdp1_job *fdp1_prepare_job(struct fdp1_ctx *ctx)
  980. {
  981. struct vb2_v4l2_buffer *vbuf;
  982. struct fdp1_buffer *fbuf;
  983. struct fdp1_dev *fdp1 = ctx->fdp1;
  984. struct fdp1_job *job;
  985. unsigned int buffers_required = 1;
  986. dprintk(fdp1, "+\n");
  987. if (FDP1_DEINT_MODE_USES_NEXT(ctx->deint_mode))
  988. buffers_required = 2;
  989. if (ctx->buffers_queued < buffers_required)
  990. return NULL;
  991. job = fdp1_job_alloc(fdp1);
  992. if (!job) {
  993. dprintk(fdp1, "No free jobs currently available\n");
  994. return NULL;
  995. }
  996. job->active = fdp1_dequeue_field(ctx);
  997. if (!job->active) {
  998. /* Buffer check should prevent this ever happening */
  999. dprintk(fdp1, "No input buffers currently available\n");
  1000. fdp1_job_free(fdp1, job);
  1001. return NULL;
  1002. }
  1003. dprintk(fdp1, "+ Buffer en-route...\n");
  1004. /* Source buffers have been prepared on our buffer_queue
  1005. * Prepare our Output buffer
  1006. */
  1007. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1008. fbuf = to_fdp1_buffer(vbuf);
  1009. job->dst = &fbuf->fields[0];
  1010. job->active->vb->sequence = ctx->sequence;
  1011. job->dst->vb->sequence = ctx->sequence;
  1012. ctx->sequence++;
  1013. if (FDP1_DEINT_MODE_USES_PREV(ctx->deint_mode)) {
  1014. job->previous = ctx->previous;
  1015. /* Active buffer becomes the next job's previous buffer */
  1016. ctx->previous = job->active;
  1017. }
  1018. if (FDP1_DEINT_MODE_USES_NEXT(ctx->deint_mode)) {
  1019. /* Must be called after 'active' is dequeued */
  1020. job->next = fdp1_peek_queued_field(ctx);
  1021. }
  1022. /* Transfer timestamps and flags from src->dst */
  1023. job->dst->vb->vb2_buf.timestamp = job->active->vb->vb2_buf.timestamp;
  1024. job->dst->vb->flags = job->active->vb->flags &
  1025. V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  1026. /* Ideally, the frame-end function will just 'check' to see
  1027. * if there are more jobs instead
  1028. */
  1029. ctx->translen++;
  1030. /* Finally, Put this job on the processing queue */
  1031. queue_job(fdp1, job);
  1032. dprintk(fdp1, "Job Queued translen = %d\n", ctx->translen);
  1033. return job;
  1034. }
  1035. /* fdp1_m2m_device_run() - prepares and starts the device for an M2M task
  1036. *
  1037. * A single input buffer is taken and serialised into our fdp1_buffer
  1038. * queue. The queue is then processed to create as many jobs as possible
  1039. * from our available input.
  1040. */
  1041. static void fdp1_m2m_device_run(void *priv)
  1042. {
  1043. struct fdp1_ctx *ctx = priv;
  1044. struct fdp1_dev *fdp1 = ctx->fdp1;
  1045. struct vb2_v4l2_buffer *src_vb;
  1046. struct fdp1_buffer *buf;
  1047. unsigned int i;
  1048. dprintk(fdp1, "+\n");
  1049. ctx->translen = 0;
  1050. /* Get our incoming buffer of either one or two fields, or one frame */
  1051. src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1052. buf = to_fdp1_buffer(src_vb);
  1053. for (i = 0; i < buf->num_fields; i++) {
  1054. struct fdp1_field_buffer *fbuf = &buf->fields[i];
  1055. fdp1_queue_field(ctx, fbuf);
  1056. dprintk(fdp1, "Queued Buffer [%d] last_field:%d\n",
  1057. i, fbuf->last_field);
  1058. }
  1059. /* Queue as many jobs as our data provides for */
  1060. while (fdp1_prepare_job(ctx))
  1061. ;
  1062. if (ctx->translen == 0) {
  1063. dprintk(fdp1, "No jobs were processed. M2M action complete\n");
  1064. v4l2_m2m_job_finish(fdp1->m2m_dev, ctx->fh.m2m_ctx);
  1065. return;
  1066. }
  1067. /* Kick the job processing action */
  1068. fdp1_device_process(ctx);
  1069. }
  1070. /*
  1071. * device_frame_end:
  1072. *
  1073. * Handles the M2M level after a buffer completion event.
  1074. */
  1075. static void device_frame_end(struct fdp1_dev *fdp1,
  1076. enum vb2_buffer_state state)
  1077. {
  1078. struct fdp1_ctx *ctx;
  1079. unsigned long flags;
  1080. struct fdp1_job *job = get_hw_queued_job(fdp1);
  1081. dprintk(fdp1, "+\n");
  1082. ctx = v4l2_m2m_get_curr_priv(fdp1->m2m_dev);
  1083. if (ctx == NULL) {
  1084. v4l2_err(&fdp1->v4l2_dev,
  1085. "Instance released before the end of transaction\n");
  1086. return;
  1087. }
  1088. ctx->num_processed++;
  1089. /*
  1090. * fdp1_field_complete will call buf_done only when the last vb2_buffer
  1091. * reference is complete
  1092. */
  1093. if (FDP1_DEINT_MODE_USES_PREV(ctx->deint_mode))
  1094. fdp1_field_complete(ctx, job->previous);
  1095. else
  1096. fdp1_field_complete(ctx, job->active);
  1097. spin_lock_irqsave(&fdp1->irqlock, flags);
  1098. v4l2_m2m_buf_done(job->dst->vb, state);
  1099. job->dst = NULL;
  1100. spin_unlock_irqrestore(&fdp1->irqlock, flags);
  1101. /* Move this job back to the free job list */
  1102. fdp1_job_free(fdp1, job);
  1103. dprintk(fdp1, "curr_ctx->num_processed %d curr_ctx->translen %d\n",
  1104. ctx->num_processed, ctx->translen);
  1105. if (ctx->num_processed == ctx->translen ||
  1106. ctx->aborting) {
  1107. dprintk(ctx->fdp1, "Finishing transaction\n");
  1108. ctx->num_processed = 0;
  1109. v4l2_m2m_job_finish(fdp1->m2m_dev, ctx->fh.m2m_ctx);
  1110. } else {
  1111. /*
  1112. * For pipelined performance support, this would
  1113. * be called from a VINT handler
  1114. */
  1115. fdp1_device_process(ctx);
  1116. }
  1117. }
  1118. /*
  1119. * video ioctls
  1120. */
  1121. static int fdp1_vidioc_querycap(struct file *file, void *priv,
  1122. struct v4l2_capability *cap)
  1123. {
  1124. strlcpy(cap->driver, DRIVER_NAME, sizeof(cap->driver));
  1125. strlcpy(cap->card, DRIVER_NAME, sizeof(cap->card));
  1126. snprintf(cap->bus_info, sizeof(cap->bus_info),
  1127. "platform:%s", DRIVER_NAME);
  1128. return 0;
  1129. }
  1130. static int fdp1_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  1131. {
  1132. unsigned int i, num;
  1133. num = 0;
  1134. for (i = 0; i < ARRAY_SIZE(fdp1_formats); ++i) {
  1135. if (fdp1_formats[i].types & type) {
  1136. if (num == f->index)
  1137. break;
  1138. ++num;
  1139. }
  1140. }
  1141. /* Format not found */
  1142. if (i >= ARRAY_SIZE(fdp1_formats))
  1143. return -EINVAL;
  1144. /* Format found */
  1145. f->pixelformat = fdp1_formats[i].fourcc;
  1146. return 0;
  1147. }
  1148. static int fdp1_enum_fmt_vid_cap(struct file *file, void *priv,
  1149. struct v4l2_fmtdesc *f)
  1150. {
  1151. return fdp1_enum_fmt(f, FDP1_CAPTURE);
  1152. }
  1153. static int fdp1_enum_fmt_vid_out(struct file *file, void *priv,
  1154. struct v4l2_fmtdesc *f)
  1155. {
  1156. return fdp1_enum_fmt(f, FDP1_OUTPUT);
  1157. }
  1158. static int fdp1_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1159. {
  1160. struct fdp1_q_data *q_data;
  1161. struct fdp1_ctx *ctx = fh_to_ctx(priv);
  1162. if (!v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type))
  1163. return -EINVAL;
  1164. q_data = get_q_data(ctx, f->type);
  1165. f->fmt.pix_mp = q_data->format;
  1166. return 0;
  1167. }
  1168. static void fdp1_compute_stride(struct v4l2_pix_format_mplane *pix,
  1169. const struct fdp1_fmt *fmt)
  1170. {
  1171. unsigned int i;
  1172. /* Compute and clamp the stride and image size. */
  1173. for (i = 0; i < min_t(unsigned int, fmt->num_planes, 2U); ++i) {
  1174. unsigned int hsub = i > 0 ? fmt->hsub : 1;
  1175. unsigned int vsub = i > 0 ? fmt->vsub : 1;
  1176. /* From VSP : TODO: Confirm alignment limits for FDP1 */
  1177. unsigned int align = 128;
  1178. unsigned int bpl;
  1179. bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
  1180. pix->width / hsub * fmt->bpp[i] / 8,
  1181. round_down(FDP1_MAX_STRIDE, align));
  1182. pix->plane_fmt[i].bytesperline = round_up(bpl, align);
  1183. pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
  1184. * pix->height / vsub;
  1185. memset(pix->plane_fmt[i].reserved, 0,
  1186. sizeof(pix->plane_fmt[i].reserved));
  1187. }
  1188. if (fmt->num_planes == 3) {
  1189. /* The two chroma planes must have the same stride. */
  1190. pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
  1191. pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
  1192. memset(pix->plane_fmt[2].reserved, 0,
  1193. sizeof(pix->plane_fmt[2].reserved));
  1194. }
  1195. }
  1196. static void fdp1_try_fmt_output(struct fdp1_ctx *ctx,
  1197. const struct fdp1_fmt **fmtinfo,
  1198. struct v4l2_pix_format_mplane *pix)
  1199. {
  1200. const struct fdp1_fmt *fmt;
  1201. unsigned int width;
  1202. unsigned int height;
  1203. /* Validate the pixel format to ensure the output queue supports it. */
  1204. fmt = fdp1_find_format(pix->pixelformat);
  1205. if (!fmt || !(fmt->types & FDP1_OUTPUT))
  1206. fmt = fdp1_find_format(V4L2_PIX_FMT_YUYV);
  1207. if (fmtinfo)
  1208. *fmtinfo = fmt;
  1209. pix->pixelformat = fmt->fourcc;
  1210. pix->num_planes = fmt->num_planes;
  1211. /*
  1212. * Progressive video and all interlaced field orders are acceptable.
  1213. * Default to V4L2_FIELD_INTERLACED.
  1214. */
  1215. if (pix->field != V4L2_FIELD_NONE &&
  1216. pix->field != V4L2_FIELD_ALTERNATE &&
  1217. !V4L2_FIELD_HAS_BOTH(pix->field))
  1218. pix->field = V4L2_FIELD_INTERLACED;
  1219. /*
  1220. * The deinterlacer doesn't care about the colorspace, accept all values
  1221. * and default to V4L2_COLORSPACE_SMPTE170M. The YUV to RGB conversion
  1222. * at the output of the deinterlacer supports a subset of encodings and
  1223. * quantization methods and will only be available when the colorspace
  1224. * allows it.
  1225. */
  1226. if (pix->colorspace == V4L2_COLORSPACE_DEFAULT)
  1227. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1228. /*
  1229. * Align the width and height for YUV 4:2:2 and 4:2:0 formats and clamp
  1230. * them to the supported frame size range. The height boundary are
  1231. * related to the full frame, divide them by two when the format passes
  1232. * fields in separate buffers.
  1233. */
  1234. width = round_down(pix->width, fmt->hsub);
  1235. pix->width = clamp(width, FDP1_MIN_W, FDP1_MAX_W);
  1236. height = round_down(pix->height, fmt->vsub);
  1237. if (pix->field == V4L2_FIELD_ALTERNATE)
  1238. pix->height = clamp(height, FDP1_MIN_H / 2, FDP1_MAX_H / 2);
  1239. else
  1240. pix->height = clamp(height, FDP1_MIN_H, FDP1_MAX_H);
  1241. fdp1_compute_stride(pix, fmt);
  1242. }
  1243. static void fdp1_try_fmt_capture(struct fdp1_ctx *ctx,
  1244. const struct fdp1_fmt **fmtinfo,
  1245. struct v4l2_pix_format_mplane *pix)
  1246. {
  1247. struct fdp1_q_data *src_data = &ctx->out_q;
  1248. enum v4l2_colorspace colorspace;
  1249. enum v4l2_ycbcr_encoding ycbcr_enc;
  1250. enum v4l2_quantization quantization;
  1251. const struct fdp1_fmt *fmt;
  1252. bool allow_rgb;
  1253. /*
  1254. * Validate the pixel format. We can only accept RGB output formats if
  1255. * the input encoding and quantization are compatible with the format
  1256. * conversions supported by the hardware. The supported combinations are
  1257. *
  1258. * V4L2_YCBCR_ENC_601 + V4L2_QUANTIZATION_LIM_RANGE
  1259. * V4L2_YCBCR_ENC_601 + V4L2_QUANTIZATION_FULL_RANGE
  1260. * V4L2_YCBCR_ENC_709 + V4L2_QUANTIZATION_LIM_RANGE
  1261. */
  1262. colorspace = src_data->format.colorspace;
  1263. ycbcr_enc = src_data->format.ycbcr_enc;
  1264. if (ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
  1265. ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(colorspace);
  1266. quantization = src_data->format.quantization;
  1267. if (quantization == V4L2_QUANTIZATION_DEFAULT)
  1268. quantization = V4L2_MAP_QUANTIZATION_DEFAULT(false, colorspace,
  1269. ycbcr_enc);
  1270. allow_rgb = ycbcr_enc == V4L2_YCBCR_ENC_601 ||
  1271. (ycbcr_enc == V4L2_YCBCR_ENC_709 &&
  1272. quantization == V4L2_QUANTIZATION_LIM_RANGE);
  1273. fmt = fdp1_find_format(pix->pixelformat);
  1274. if (!fmt || (!allow_rgb && fdp1_fmt_is_rgb(fmt)))
  1275. fmt = fdp1_find_format(V4L2_PIX_FMT_YUYV);
  1276. if (fmtinfo)
  1277. *fmtinfo = fmt;
  1278. pix->pixelformat = fmt->fourcc;
  1279. pix->num_planes = fmt->num_planes;
  1280. pix->field = V4L2_FIELD_NONE;
  1281. /*
  1282. * The colorspace on the capture queue is copied from the output queue
  1283. * as the hardware can't change the colorspace. It can convert YCbCr to
  1284. * RGB though, in which case the encoding and quantization are set to
  1285. * default values as anything else wouldn't make sense.
  1286. */
  1287. pix->colorspace = src_data->format.colorspace;
  1288. pix->xfer_func = src_data->format.xfer_func;
  1289. if (fdp1_fmt_is_rgb(fmt)) {
  1290. pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1291. pix->quantization = V4L2_QUANTIZATION_DEFAULT;
  1292. } else {
  1293. pix->ycbcr_enc = src_data->format.ycbcr_enc;
  1294. pix->quantization = src_data->format.quantization;
  1295. }
  1296. /*
  1297. * The frame width is identical to the output queue, and the height is
  1298. * either doubled or identical depending on whether the output queue
  1299. * field order contains one or two fields per frame.
  1300. */
  1301. pix->width = src_data->format.width;
  1302. if (src_data->format.field == V4L2_FIELD_ALTERNATE)
  1303. pix->height = 2 * src_data->format.height;
  1304. else
  1305. pix->height = src_data->format.height;
  1306. fdp1_compute_stride(pix, fmt);
  1307. }
  1308. static int fdp1_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1309. {
  1310. struct fdp1_ctx *ctx = fh_to_ctx(priv);
  1311. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1312. fdp1_try_fmt_output(ctx, NULL, &f->fmt.pix_mp);
  1313. else
  1314. fdp1_try_fmt_capture(ctx, NULL, &f->fmt.pix_mp);
  1315. dprintk(ctx->fdp1, "Try %s format: %4.4s (0x%08x) %ux%u field %u\n",
  1316. V4L2_TYPE_IS_OUTPUT(f->type) ? "output" : "capture",
  1317. (char *)&f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.pixelformat,
  1318. f->fmt.pix_mp.width, f->fmt.pix_mp.height, f->fmt.pix_mp.field);
  1319. return 0;
  1320. }
  1321. static void fdp1_set_format(struct fdp1_ctx *ctx,
  1322. struct v4l2_pix_format_mplane *pix,
  1323. enum v4l2_buf_type type)
  1324. {
  1325. struct fdp1_q_data *q_data = get_q_data(ctx, type);
  1326. const struct fdp1_fmt *fmtinfo;
  1327. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1328. fdp1_try_fmt_output(ctx, &fmtinfo, pix);
  1329. else
  1330. fdp1_try_fmt_capture(ctx, &fmtinfo, pix);
  1331. q_data->fmt = fmtinfo;
  1332. q_data->format = *pix;
  1333. q_data->vsize = pix->height;
  1334. if (pix->field != V4L2_FIELD_NONE)
  1335. q_data->vsize /= 2;
  1336. q_data->stride_y = pix->plane_fmt[0].bytesperline;
  1337. q_data->stride_c = pix->plane_fmt[1].bytesperline;
  1338. /* Adjust strides for interleaved buffers */
  1339. if (pix->field == V4L2_FIELD_INTERLACED ||
  1340. pix->field == V4L2_FIELD_INTERLACED_TB ||
  1341. pix->field == V4L2_FIELD_INTERLACED_BT) {
  1342. q_data->stride_y *= 2;
  1343. q_data->stride_c *= 2;
  1344. }
  1345. /* Propagate the format from the output node to the capture node. */
  1346. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1347. struct fdp1_q_data *dst_data = &ctx->cap_q;
  1348. /*
  1349. * Copy the format, clear the per-plane bytes per line and image
  1350. * size, override the field and double the height if needed.
  1351. */
  1352. dst_data->format = q_data->format;
  1353. memset(dst_data->format.plane_fmt, 0,
  1354. sizeof(dst_data->format.plane_fmt));
  1355. dst_data->format.field = V4L2_FIELD_NONE;
  1356. if (pix->field == V4L2_FIELD_ALTERNATE)
  1357. dst_data->format.height *= 2;
  1358. fdp1_try_fmt_capture(ctx, &dst_data->fmt, &dst_data->format);
  1359. dst_data->vsize = dst_data->format.height;
  1360. dst_data->stride_y = dst_data->format.plane_fmt[0].bytesperline;
  1361. dst_data->stride_c = dst_data->format.plane_fmt[1].bytesperline;
  1362. }
  1363. }
  1364. static int fdp1_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1365. {
  1366. struct fdp1_ctx *ctx = fh_to_ctx(priv);
  1367. struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  1368. struct vb2_queue *vq = v4l2_m2m_get_vq(m2m_ctx, f->type);
  1369. if (vb2_is_busy(vq)) {
  1370. v4l2_err(&ctx->fdp1->v4l2_dev, "%s queue busy\n", __func__);
  1371. return -EBUSY;
  1372. }
  1373. fdp1_set_format(ctx, &f->fmt.pix_mp, f->type);
  1374. dprintk(ctx->fdp1, "Set %s format: %4.4s (0x%08x) %ux%u field %u\n",
  1375. V4L2_TYPE_IS_OUTPUT(f->type) ? "output" : "capture",
  1376. (char *)&f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.pixelformat,
  1377. f->fmt.pix_mp.width, f->fmt.pix_mp.height, f->fmt.pix_mp.field);
  1378. return 0;
  1379. }
  1380. static int fdp1_g_ctrl(struct v4l2_ctrl *ctrl)
  1381. {
  1382. struct fdp1_ctx *ctx =
  1383. container_of(ctrl->handler, struct fdp1_ctx, hdl);
  1384. struct fdp1_q_data *src_q_data = &ctx->out_q;
  1385. switch (ctrl->id) {
  1386. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1387. if (V4L2_FIELD_HAS_BOTH(src_q_data->format.field))
  1388. ctrl->val = 2;
  1389. else
  1390. ctrl->val = 1;
  1391. return 0;
  1392. }
  1393. return 1;
  1394. }
  1395. static int fdp1_s_ctrl(struct v4l2_ctrl *ctrl)
  1396. {
  1397. struct fdp1_ctx *ctx =
  1398. container_of(ctrl->handler, struct fdp1_ctx, hdl);
  1399. switch (ctrl->id) {
  1400. case V4L2_CID_ALPHA_COMPONENT:
  1401. ctx->alpha = ctrl->val;
  1402. break;
  1403. case V4L2_CID_DEINTERLACING_MODE:
  1404. ctx->deint_mode = ctrl->val;
  1405. break;
  1406. }
  1407. return 0;
  1408. }
  1409. static const struct v4l2_ctrl_ops fdp1_ctrl_ops = {
  1410. .s_ctrl = fdp1_s_ctrl,
  1411. .g_volatile_ctrl = fdp1_g_ctrl,
  1412. };
  1413. static const char * const fdp1_ctrl_deint_menu[] = {
  1414. "Progressive",
  1415. "Adaptive 2D/3D",
  1416. "Fixed 2D",
  1417. "Fixed 3D",
  1418. "Previous field",
  1419. "Next field",
  1420. NULL
  1421. };
  1422. static const struct v4l2_ioctl_ops fdp1_ioctl_ops = {
  1423. .vidioc_querycap = fdp1_vidioc_querycap,
  1424. .vidioc_enum_fmt_vid_cap_mplane = fdp1_enum_fmt_vid_cap,
  1425. .vidioc_enum_fmt_vid_out_mplane = fdp1_enum_fmt_vid_out,
  1426. .vidioc_g_fmt_vid_cap_mplane = fdp1_g_fmt,
  1427. .vidioc_g_fmt_vid_out_mplane = fdp1_g_fmt,
  1428. .vidioc_try_fmt_vid_cap_mplane = fdp1_try_fmt,
  1429. .vidioc_try_fmt_vid_out_mplane = fdp1_try_fmt,
  1430. .vidioc_s_fmt_vid_cap_mplane = fdp1_s_fmt,
  1431. .vidioc_s_fmt_vid_out_mplane = fdp1_s_fmt,
  1432. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1433. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1434. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1435. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1436. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  1437. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  1438. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1439. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1440. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1441. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1442. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1443. };
  1444. /*
  1445. * Queue operations
  1446. */
  1447. static int fdp1_queue_setup(struct vb2_queue *vq,
  1448. unsigned int *nbuffers, unsigned int *nplanes,
  1449. unsigned int sizes[],
  1450. struct device *alloc_ctxs[])
  1451. {
  1452. struct fdp1_ctx *ctx = vb2_get_drv_priv(vq);
  1453. struct fdp1_q_data *q_data;
  1454. unsigned int i;
  1455. q_data = get_q_data(ctx, vq->type);
  1456. if (*nplanes) {
  1457. if (*nplanes > FDP1_MAX_PLANES)
  1458. return -EINVAL;
  1459. return 0;
  1460. }
  1461. *nplanes = q_data->format.num_planes;
  1462. for (i = 0; i < *nplanes; i++)
  1463. sizes[i] = q_data->format.plane_fmt[i].sizeimage;
  1464. return 0;
  1465. }
  1466. static void fdp1_buf_prepare_field(struct fdp1_q_data *q_data,
  1467. struct vb2_v4l2_buffer *vbuf,
  1468. unsigned int field_num)
  1469. {
  1470. struct fdp1_buffer *buf = to_fdp1_buffer(vbuf);
  1471. struct fdp1_field_buffer *fbuf = &buf->fields[field_num];
  1472. unsigned int num_fields;
  1473. unsigned int i;
  1474. num_fields = V4L2_FIELD_HAS_BOTH(vbuf->field) ? 2 : 1;
  1475. fbuf->vb = vbuf;
  1476. fbuf->last_field = (field_num + 1) == num_fields;
  1477. for (i = 0; i < vbuf->vb2_buf.num_planes; ++i)
  1478. fbuf->addrs[i] = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, i);
  1479. switch (vbuf->field) {
  1480. case V4L2_FIELD_INTERLACED:
  1481. /*
  1482. * Interlaced means bottom-top for 60Hz TV standards (NTSC) and
  1483. * top-bottom for 50Hz. As TV standards are not applicable to
  1484. * the mem-to-mem API, use the height as a heuristic.
  1485. */
  1486. fbuf->field = (q_data->format.height < 576) == field_num
  1487. ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1488. break;
  1489. case V4L2_FIELD_INTERLACED_TB:
  1490. case V4L2_FIELD_SEQ_TB:
  1491. fbuf->field = field_num ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
  1492. break;
  1493. case V4L2_FIELD_INTERLACED_BT:
  1494. case V4L2_FIELD_SEQ_BT:
  1495. fbuf->field = field_num ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1496. break;
  1497. default:
  1498. fbuf->field = vbuf->field;
  1499. break;
  1500. }
  1501. /* Buffer is completed */
  1502. if (!field_num)
  1503. return;
  1504. /* Adjust buffer addresses for second field */
  1505. switch (vbuf->field) {
  1506. case V4L2_FIELD_INTERLACED:
  1507. case V4L2_FIELD_INTERLACED_TB:
  1508. case V4L2_FIELD_INTERLACED_BT:
  1509. for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
  1510. fbuf->addrs[i] +=
  1511. (i == 0 ? q_data->stride_y : q_data->stride_c);
  1512. break;
  1513. case V4L2_FIELD_SEQ_TB:
  1514. case V4L2_FIELD_SEQ_BT:
  1515. for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
  1516. fbuf->addrs[i] += q_data->vsize *
  1517. (i == 0 ? q_data->stride_y : q_data->stride_c);
  1518. break;
  1519. }
  1520. }
  1521. static int fdp1_buf_prepare(struct vb2_buffer *vb)
  1522. {
  1523. struct fdp1_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1524. struct fdp1_q_data *q_data = get_q_data(ctx, vb->vb2_queue->type);
  1525. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1526. struct fdp1_buffer *buf = to_fdp1_buffer(vbuf);
  1527. unsigned int i;
  1528. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  1529. bool field_valid = true;
  1530. /* Validate the buffer field. */
  1531. switch (q_data->format.field) {
  1532. case V4L2_FIELD_NONE:
  1533. if (vbuf->field != V4L2_FIELD_NONE)
  1534. field_valid = false;
  1535. break;
  1536. case V4L2_FIELD_ALTERNATE:
  1537. if (vbuf->field != V4L2_FIELD_TOP &&
  1538. vbuf->field != V4L2_FIELD_BOTTOM)
  1539. field_valid = false;
  1540. break;
  1541. case V4L2_FIELD_INTERLACED:
  1542. case V4L2_FIELD_SEQ_TB:
  1543. case V4L2_FIELD_SEQ_BT:
  1544. case V4L2_FIELD_INTERLACED_TB:
  1545. case V4L2_FIELD_INTERLACED_BT:
  1546. if (vbuf->field != q_data->format.field)
  1547. field_valid = false;
  1548. break;
  1549. }
  1550. if (!field_valid) {
  1551. dprintk(ctx->fdp1,
  1552. "buffer field %u invalid for format field %u\n",
  1553. vbuf->field, q_data->format.field);
  1554. return -EINVAL;
  1555. }
  1556. } else {
  1557. vbuf->field = V4L2_FIELD_NONE;
  1558. }
  1559. /* Validate the planes sizes. */
  1560. for (i = 0; i < q_data->format.num_planes; i++) {
  1561. unsigned long size = q_data->format.plane_fmt[i].sizeimage;
  1562. if (vb2_plane_size(vb, i) < size) {
  1563. dprintk(ctx->fdp1,
  1564. "data will not fit into plane [%u/%u] (%lu < %lu)\n",
  1565. i, q_data->format.num_planes,
  1566. vb2_plane_size(vb, i), size);
  1567. return -EINVAL;
  1568. }
  1569. /* We have known size formats all around */
  1570. vb2_set_plane_payload(vb, i, size);
  1571. }
  1572. buf->num_fields = V4L2_FIELD_HAS_BOTH(vbuf->field) ? 2 : 1;
  1573. for (i = 0; i < buf->num_fields; ++i)
  1574. fdp1_buf_prepare_field(q_data, vbuf, i);
  1575. return 0;
  1576. }
  1577. static void fdp1_buf_queue(struct vb2_buffer *vb)
  1578. {
  1579. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1580. struct fdp1_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1581. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1582. }
  1583. static int fdp1_start_streaming(struct vb2_queue *q, unsigned int count)
  1584. {
  1585. struct fdp1_ctx *ctx = vb2_get_drv_priv(q);
  1586. struct fdp1_q_data *q_data = get_q_data(ctx, q->type);
  1587. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1588. /*
  1589. * Force our deint_mode when we are progressive,
  1590. * ignoring any setting on the device from the user,
  1591. * Otherwise, lock in the requested de-interlace mode.
  1592. */
  1593. if (q_data->format.field == V4L2_FIELD_NONE)
  1594. ctx->deint_mode = FDP1_PROGRESSIVE;
  1595. if (ctx->deint_mode == FDP1_ADAPT2D3D) {
  1596. u32 stride;
  1597. dma_addr_t smsk_base;
  1598. const u32 bpp = 2; /* bytes per pixel */
  1599. stride = round_up(q_data->format.width, 8);
  1600. ctx->smsk_size = bpp * stride * q_data->vsize;
  1601. ctx->smsk_cpu = dma_alloc_coherent(ctx->fdp1->dev,
  1602. ctx->smsk_size, &smsk_base, GFP_KERNEL);
  1603. if (ctx->smsk_cpu == NULL) {
  1604. dprintk(ctx->fdp1, "Failed to alloc smsk\n");
  1605. return -ENOMEM;
  1606. }
  1607. ctx->smsk_addr[0] = smsk_base;
  1608. ctx->smsk_addr[1] = smsk_base + (ctx->smsk_size/2);
  1609. }
  1610. }
  1611. return 0;
  1612. }
  1613. static void fdp1_stop_streaming(struct vb2_queue *q)
  1614. {
  1615. struct fdp1_ctx *ctx = vb2_get_drv_priv(q);
  1616. struct vb2_v4l2_buffer *vbuf;
  1617. unsigned long flags;
  1618. while (1) {
  1619. if (V4L2_TYPE_IS_OUTPUT(q->type))
  1620. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1621. else
  1622. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1623. if (vbuf == NULL)
  1624. break;
  1625. spin_lock_irqsave(&ctx->fdp1->irqlock, flags);
  1626. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  1627. spin_unlock_irqrestore(&ctx->fdp1->irqlock, flags);
  1628. }
  1629. /* Empty Output queues */
  1630. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1631. /* Empty our internal queues */
  1632. struct fdp1_field_buffer *fbuf;
  1633. /* Free any queued buffers */
  1634. fbuf = fdp1_dequeue_field(ctx);
  1635. while (fbuf != NULL) {
  1636. fdp1_field_complete(ctx, fbuf);
  1637. fbuf = fdp1_dequeue_field(ctx);
  1638. }
  1639. /* Free smsk_data */
  1640. if (ctx->smsk_cpu) {
  1641. dma_free_coherent(ctx->fdp1->dev, ctx->smsk_size,
  1642. ctx->smsk_cpu, ctx->smsk_addr[0]);
  1643. ctx->smsk_addr[0] = ctx->smsk_addr[1] = 0;
  1644. ctx->smsk_cpu = NULL;
  1645. }
  1646. WARN(!list_empty(&ctx->fields_queue),
  1647. "Buffer queue not empty");
  1648. } else {
  1649. /* Empty Capture queues (Jobs) */
  1650. struct fdp1_job *job;
  1651. job = get_queued_job(ctx->fdp1);
  1652. while (job) {
  1653. if (FDP1_DEINT_MODE_USES_PREV(ctx->deint_mode))
  1654. fdp1_field_complete(ctx, job->previous);
  1655. else
  1656. fdp1_field_complete(ctx, job->active);
  1657. v4l2_m2m_buf_done(job->dst->vb, VB2_BUF_STATE_ERROR);
  1658. job->dst = NULL;
  1659. job = get_queued_job(ctx->fdp1);
  1660. }
  1661. /* Free any held buffer in the ctx */
  1662. fdp1_field_complete(ctx, ctx->previous);
  1663. WARN(!list_empty(&ctx->fdp1->queued_job_list),
  1664. "Queued Job List not empty");
  1665. WARN(!list_empty(&ctx->fdp1->hw_job_list),
  1666. "HW Job list not empty");
  1667. }
  1668. }
  1669. static const struct vb2_ops fdp1_qops = {
  1670. .queue_setup = fdp1_queue_setup,
  1671. .buf_prepare = fdp1_buf_prepare,
  1672. .buf_queue = fdp1_buf_queue,
  1673. .start_streaming = fdp1_start_streaming,
  1674. .stop_streaming = fdp1_stop_streaming,
  1675. .wait_prepare = vb2_ops_wait_prepare,
  1676. .wait_finish = vb2_ops_wait_finish,
  1677. };
  1678. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1679. struct vb2_queue *dst_vq)
  1680. {
  1681. struct fdp1_ctx *ctx = priv;
  1682. int ret;
  1683. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1684. src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1685. src_vq->drv_priv = ctx;
  1686. src_vq->buf_struct_size = sizeof(struct fdp1_buffer);
  1687. src_vq->ops = &fdp1_qops;
  1688. src_vq->mem_ops = &vb2_dma_contig_memops;
  1689. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1690. src_vq->lock = &ctx->fdp1->dev_mutex;
  1691. src_vq->dev = ctx->fdp1->dev;
  1692. ret = vb2_queue_init(src_vq);
  1693. if (ret)
  1694. return ret;
  1695. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1696. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1697. dst_vq->drv_priv = ctx;
  1698. dst_vq->buf_struct_size = sizeof(struct fdp1_buffer);
  1699. dst_vq->ops = &fdp1_qops;
  1700. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1701. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1702. dst_vq->lock = &ctx->fdp1->dev_mutex;
  1703. dst_vq->dev = ctx->fdp1->dev;
  1704. return vb2_queue_init(dst_vq);
  1705. }
  1706. /*
  1707. * File operations
  1708. */
  1709. static int fdp1_open(struct file *file)
  1710. {
  1711. struct fdp1_dev *fdp1 = video_drvdata(file);
  1712. struct v4l2_pix_format_mplane format;
  1713. struct fdp1_ctx *ctx = NULL;
  1714. struct v4l2_ctrl *ctrl;
  1715. int ret = 0;
  1716. if (mutex_lock_interruptible(&fdp1->dev_mutex))
  1717. return -ERESTARTSYS;
  1718. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1719. if (!ctx) {
  1720. ret = -ENOMEM;
  1721. goto done;
  1722. }
  1723. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1724. file->private_data = &ctx->fh;
  1725. ctx->fdp1 = fdp1;
  1726. /* Initialise Queues */
  1727. INIT_LIST_HEAD(&ctx->fields_queue);
  1728. ctx->translen = 1;
  1729. ctx->sequence = 0;
  1730. /* Initialise controls */
  1731. v4l2_ctrl_handler_init(&ctx->hdl, 3);
  1732. v4l2_ctrl_new_std_menu_items(&ctx->hdl, &fdp1_ctrl_ops,
  1733. V4L2_CID_DEINTERLACING_MODE,
  1734. FDP1_NEXTFIELD, BIT(0), FDP1_FIXED3D,
  1735. fdp1_ctrl_deint_menu);
  1736. ctrl = v4l2_ctrl_new_std(&ctx->hdl, &fdp1_ctrl_ops,
  1737. V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 2, 1, 1);
  1738. if (ctrl)
  1739. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1740. v4l2_ctrl_new_std(&ctx->hdl, &fdp1_ctrl_ops,
  1741. V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
  1742. if (ctx->hdl.error) {
  1743. ret = ctx->hdl.error;
  1744. v4l2_ctrl_handler_free(&ctx->hdl);
  1745. goto done;
  1746. }
  1747. ctx->fh.ctrl_handler = &ctx->hdl;
  1748. v4l2_ctrl_handler_setup(&ctx->hdl);
  1749. /* Configure default parameters. */
  1750. memset(&format, 0, sizeof(format));
  1751. fdp1_set_format(ctx, &format, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1752. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(fdp1->m2m_dev, ctx, &queue_init);
  1753. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1754. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1755. v4l2_ctrl_handler_free(&ctx->hdl);
  1756. kfree(ctx);
  1757. goto done;
  1758. }
  1759. /* Perform any power management required */
  1760. pm_runtime_get_sync(fdp1->dev);
  1761. v4l2_fh_add(&ctx->fh);
  1762. dprintk(fdp1, "Created instance: %p, m2m_ctx: %p\n",
  1763. ctx, ctx->fh.m2m_ctx);
  1764. done:
  1765. mutex_unlock(&fdp1->dev_mutex);
  1766. return ret;
  1767. }
  1768. static int fdp1_release(struct file *file)
  1769. {
  1770. struct fdp1_dev *fdp1 = video_drvdata(file);
  1771. struct fdp1_ctx *ctx = fh_to_ctx(file->private_data);
  1772. dprintk(fdp1, "Releasing instance %p\n", ctx);
  1773. v4l2_fh_del(&ctx->fh);
  1774. v4l2_fh_exit(&ctx->fh);
  1775. v4l2_ctrl_handler_free(&ctx->hdl);
  1776. mutex_lock(&fdp1->dev_mutex);
  1777. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1778. mutex_unlock(&fdp1->dev_mutex);
  1779. kfree(ctx);
  1780. pm_runtime_put(fdp1->dev);
  1781. return 0;
  1782. }
  1783. static const struct v4l2_file_operations fdp1_fops = {
  1784. .owner = THIS_MODULE,
  1785. .open = fdp1_open,
  1786. .release = fdp1_release,
  1787. .poll = v4l2_m2m_fop_poll,
  1788. .unlocked_ioctl = video_ioctl2,
  1789. .mmap = v4l2_m2m_fop_mmap,
  1790. };
  1791. static const struct video_device fdp1_videodev = {
  1792. .name = DRIVER_NAME,
  1793. .vfl_dir = VFL_DIR_M2M,
  1794. .fops = &fdp1_fops,
  1795. .device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
  1796. .ioctl_ops = &fdp1_ioctl_ops,
  1797. .minor = -1,
  1798. .release = video_device_release_empty,
  1799. };
  1800. static const struct v4l2_m2m_ops m2m_ops = {
  1801. .device_run = fdp1_m2m_device_run,
  1802. .job_ready = fdp1_m2m_job_ready,
  1803. .job_abort = fdp1_m2m_job_abort,
  1804. };
  1805. static irqreturn_t fdp1_irq_handler(int irq, void *dev_id)
  1806. {
  1807. struct fdp1_dev *fdp1 = dev_id;
  1808. u32 int_status;
  1809. u32 ctl_status;
  1810. u32 vint_cnt;
  1811. u32 cycles;
  1812. int_status = fdp1_read(fdp1, FD1_CTL_IRQSTA);
  1813. cycles = fdp1_read(fdp1, FD1_CTL_VCYCLE_STAT);
  1814. ctl_status = fdp1_read(fdp1, FD1_CTL_STATUS);
  1815. vint_cnt = (ctl_status & FD1_CTL_STATUS_VINT_CNT_MASK) >>
  1816. FD1_CTL_STATUS_VINT_CNT_SHIFT;
  1817. /* Clear interrupts */
  1818. fdp1_write(fdp1, ~(int_status) & FD1_CTL_IRQ_MASK, FD1_CTL_IRQSTA);
  1819. if (debug >= 2) {
  1820. dprintk(fdp1, "IRQ: 0x%x %s%s%s\n", int_status,
  1821. int_status & FD1_CTL_IRQ_VERE ? "[Error]" : "[!E]",
  1822. int_status & FD1_CTL_IRQ_VINTE ? "[VSync]" : "[!V]",
  1823. int_status & FD1_CTL_IRQ_FREE ? "[FrameEnd]" : "[!F]");
  1824. dprintk(fdp1, "CycleStatus = %d (%dms)\n",
  1825. cycles, cycles/(fdp1->clk_rate/1000));
  1826. dprintk(fdp1,
  1827. "Control Status = 0x%08x : VINT_CNT = %d %s:%s:%s:%s\n",
  1828. ctl_status, vint_cnt,
  1829. ctl_status & FD1_CTL_STATUS_SGREGSET ? "RegSet" : "",
  1830. ctl_status & FD1_CTL_STATUS_SGVERR ? "Vsync Error" : "",
  1831. ctl_status & FD1_CTL_STATUS_SGFREND ? "FrameEnd" : "",
  1832. ctl_status & FD1_CTL_STATUS_BSY ? "Busy" : "");
  1833. dprintk(fdp1, "***********************************\n");
  1834. }
  1835. /* Spurious interrupt */
  1836. if (!(FD1_CTL_IRQ_MASK & int_status))
  1837. return IRQ_NONE;
  1838. /* Work completed, release the frame */
  1839. if (FD1_CTL_IRQ_VERE & int_status)
  1840. device_frame_end(fdp1, VB2_BUF_STATE_ERROR);
  1841. else if (FD1_CTL_IRQ_FREE & int_status)
  1842. device_frame_end(fdp1, VB2_BUF_STATE_DONE);
  1843. return IRQ_HANDLED;
  1844. }
  1845. static int fdp1_probe(struct platform_device *pdev)
  1846. {
  1847. struct fdp1_dev *fdp1;
  1848. struct video_device *vfd;
  1849. struct device_node *fcp_node;
  1850. struct resource *res;
  1851. struct clk *clk;
  1852. unsigned int i;
  1853. int ret;
  1854. int hw_version;
  1855. fdp1 = devm_kzalloc(&pdev->dev, sizeof(*fdp1), GFP_KERNEL);
  1856. if (!fdp1)
  1857. return -ENOMEM;
  1858. INIT_LIST_HEAD(&fdp1->free_job_list);
  1859. INIT_LIST_HEAD(&fdp1->queued_job_list);
  1860. INIT_LIST_HEAD(&fdp1->hw_job_list);
  1861. /* Initialise the jobs on the free list */
  1862. for (i = 0; i < ARRAY_SIZE(fdp1->jobs); i++)
  1863. list_add(&fdp1->jobs[i].list, &fdp1->free_job_list);
  1864. mutex_init(&fdp1->dev_mutex);
  1865. spin_lock_init(&fdp1->irqlock);
  1866. spin_lock_init(&fdp1->device_process_lock);
  1867. fdp1->dev = &pdev->dev;
  1868. platform_set_drvdata(pdev, fdp1);
  1869. /* Memory-mapped registers */
  1870. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1871. fdp1->regs = devm_ioremap_resource(&pdev->dev, res);
  1872. if (IS_ERR(fdp1->regs))
  1873. return PTR_ERR(fdp1->regs);
  1874. /* Interrupt service routine registration */
  1875. fdp1->irq = ret = platform_get_irq(pdev, 0);
  1876. if (ret < 0) {
  1877. dev_err(&pdev->dev, "cannot find IRQ\n");
  1878. return ret;
  1879. }
  1880. ret = devm_request_irq(&pdev->dev, fdp1->irq, fdp1_irq_handler, 0,
  1881. dev_name(&pdev->dev), fdp1);
  1882. if (ret) {
  1883. dev_err(&pdev->dev, "cannot claim IRQ %d\n", fdp1->irq);
  1884. return ret;
  1885. }
  1886. /* FCP */
  1887. fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
  1888. if (fcp_node) {
  1889. fdp1->fcp = rcar_fcp_get(fcp_node);
  1890. of_node_put(fcp_node);
  1891. if (IS_ERR(fdp1->fcp)) {
  1892. dev_err(&pdev->dev, "FCP not found (%ld)\n",
  1893. PTR_ERR(fdp1->fcp));
  1894. return PTR_ERR(fdp1->fcp);
  1895. }
  1896. }
  1897. /* Determine our clock rate */
  1898. clk = clk_get(&pdev->dev, NULL);
  1899. if (IS_ERR(clk))
  1900. return PTR_ERR(clk);
  1901. fdp1->clk_rate = clk_get_rate(clk);
  1902. clk_put(clk);
  1903. /* V4L2 device registration */
  1904. ret = v4l2_device_register(&pdev->dev, &fdp1->v4l2_dev);
  1905. if (ret) {
  1906. v4l2_err(&fdp1->v4l2_dev, "Failed to register video device\n");
  1907. return ret;
  1908. }
  1909. /* M2M registration */
  1910. fdp1->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1911. if (IS_ERR(fdp1->m2m_dev)) {
  1912. v4l2_err(&fdp1->v4l2_dev, "Failed to init mem2mem device\n");
  1913. ret = PTR_ERR(fdp1->m2m_dev);
  1914. goto unreg_dev;
  1915. }
  1916. /* Video registration */
  1917. fdp1->vfd = fdp1_videodev;
  1918. vfd = &fdp1->vfd;
  1919. vfd->lock = &fdp1->dev_mutex;
  1920. vfd->v4l2_dev = &fdp1->v4l2_dev;
  1921. video_set_drvdata(vfd, fdp1);
  1922. strlcpy(vfd->name, fdp1_videodev.name, sizeof(vfd->name));
  1923. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1924. if (ret) {
  1925. v4l2_err(&fdp1->v4l2_dev, "Failed to register video device\n");
  1926. goto release_m2m;
  1927. }
  1928. v4l2_info(&fdp1->v4l2_dev,
  1929. "Device registered as /dev/video%d\n", vfd->num);
  1930. /* Power up the cells to read HW */
  1931. pm_runtime_enable(&pdev->dev);
  1932. pm_runtime_get_sync(fdp1->dev);
  1933. hw_version = fdp1_read(fdp1, FD1_IP_INTDATA);
  1934. switch (hw_version) {
  1935. case FD1_IP_H3_ES1:
  1936. dprintk(fdp1, "FDP1 Version R-Car H3 ES1\n");
  1937. break;
  1938. case FD1_IP_M3W:
  1939. dprintk(fdp1, "FDP1 Version R-Car M3-W\n");
  1940. break;
  1941. case FD1_IP_H3:
  1942. dprintk(fdp1, "FDP1 Version R-Car H3\n");
  1943. break;
  1944. default:
  1945. dev_err(fdp1->dev, "FDP1 Unidentifiable (0x%08x)\n",
  1946. hw_version);
  1947. }
  1948. /* Allow the hw to sleep until an open call puts it to use */
  1949. pm_runtime_put(fdp1->dev);
  1950. return 0;
  1951. release_m2m:
  1952. v4l2_m2m_release(fdp1->m2m_dev);
  1953. unreg_dev:
  1954. v4l2_device_unregister(&fdp1->v4l2_dev);
  1955. return ret;
  1956. }
  1957. static int fdp1_remove(struct platform_device *pdev)
  1958. {
  1959. struct fdp1_dev *fdp1 = platform_get_drvdata(pdev);
  1960. v4l2_m2m_release(fdp1->m2m_dev);
  1961. video_unregister_device(&fdp1->vfd);
  1962. v4l2_device_unregister(&fdp1->v4l2_dev);
  1963. pm_runtime_disable(&pdev->dev);
  1964. return 0;
  1965. }
  1966. static int __maybe_unused fdp1_pm_runtime_suspend(struct device *dev)
  1967. {
  1968. struct fdp1_dev *fdp1 = dev_get_drvdata(dev);
  1969. rcar_fcp_disable(fdp1->fcp);
  1970. return 0;
  1971. }
  1972. static int __maybe_unused fdp1_pm_runtime_resume(struct device *dev)
  1973. {
  1974. struct fdp1_dev *fdp1 = dev_get_drvdata(dev);
  1975. /* Program in the static LUTs */
  1976. fdp1_set_lut(fdp1);
  1977. return rcar_fcp_enable(fdp1->fcp);
  1978. }
  1979. static const struct dev_pm_ops fdp1_pm_ops = {
  1980. SET_RUNTIME_PM_OPS(fdp1_pm_runtime_suspend,
  1981. fdp1_pm_runtime_resume,
  1982. NULL)
  1983. };
  1984. static const struct of_device_id fdp1_dt_ids[] = {
  1985. { .compatible = "renesas,fdp1" },
  1986. { },
  1987. };
  1988. MODULE_DEVICE_TABLE(of, fdp1_dt_ids);
  1989. static struct platform_driver fdp1_pdrv = {
  1990. .probe = fdp1_probe,
  1991. .remove = fdp1_remove,
  1992. .driver = {
  1993. .name = DRIVER_NAME,
  1994. .of_match_table = fdp1_dt_ids,
  1995. .pm = &fdp1_pm_ops,
  1996. },
  1997. };
  1998. module_platform_driver(fdp1_pdrv);
  1999. MODULE_DESCRIPTION("Renesas R-Car Fine Display Processor Driver");
  2000. MODULE_AUTHOR("Kieran Bingham <kieran@bingham.xyz>");
  2001. MODULE_LICENSE("GPL");
  2002. MODULE_ALIAS("platform:" DRIVER_NAME);