rcar_fdp1.c 66 KB

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