vpe.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  1. /*
  2. * TI VPE mem2mem driver, based on the virtual v4l2-mem2mem example driver
  3. *
  4. * Copyright (c) 2013 Texas Instruments Inc.
  5. * David Griego, <dagriego@biglakesoftware.com>
  6. * Dale Farnsworth, <dale@farnsworth.org>
  7. * Archit Taneja, <archit@ti.com>
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <pawel@osciak.com>
  11. * Marek Szyprowski, <m.szyprowski@samsung.com>
  12. *
  13. * Based on the virtual v4l2-mem2mem example device
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published by
  17. * the Free Software Foundation
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/log2.h>
  34. #include <linux/sizes.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ctrls.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-mem2mem.h>
  41. #include <media/videobuf2-v4l2.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include "vpdma.h"
  44. #include "vpdma_priv.h"
  45. #include "vpe_regs.h"
  46. #include "sc.h"
  47. #include "csc.h"
  48. #define VPE_MODULE_NAME "vpe"
  49. /* minimum and maximum frame sizes */
  50. #define MIN_W 32
  51. #define MIN_H 32
  52. #define MAX_W 2048
  53. #define MAX_H 1184
  54. /* required alignments */
  55. #define S_ALIGN 0 /* multiple of 1 */
  56. #define H_ALIGN 1 /* multiple of 2 */
  57. /* flags that indicate a format can be used for capture/output */
  58. #define VPE_FMT_TYPE_CAPTURE (1 << 0)
  59. #define VPE_FMT_TYPE_OUTPUT (1 << 1)
  60. /* used as plane indices */
  61. #define VPE_MAX_PLANES 2
  62. #define VPE_LUMA 0
  63. #define VPE_CHROMA 1
  64. /* per m2m context info */
  65. #define VPE_MAX_SRC_BUFS 3 /* need 3 src fields to de-interlace */
  66. #define VPE_DEF_BUFS_PER_JOB 1 /* default one buffer per batch job */
  67. /*
  68. * each VPE context can need up to 3 config descriptors, 7 input descriptors,
  69. * 3 output descriptors, and 10 control descriptors
  70. */
  71. #define VPE_DESC_LIST_SIZE (10 * VPDMA_DTD_DESC_SIZE + \
  72. 13 * VPDMA_CFD_CTD_DESC_SIZE)
  73. #define vpe_dbg(vpedev, fmt, arg...) \
  74. dev_dbg((vpedev)->v4l2_dev.dev, fmt, ##arg)
  75. #define vpe_err(vpedev, fmt, arg...) \
  76. dev_err((vpedev)->v4l2_dev.dev, fmt, ##arg)
  77. struct vpe_us_coeffs {
  78. unsigned short anchor_fid0_c0;
  79. unsigned short anchor_fid0_c1;
  80. unsigned short anchor_fid0_c2;
  81. unsigned short anchor_fid0_c3;
  82. unsigned short interp_fid0_c0;
  83. unsigned short interp_fid0_c1;
  84. unsigned short interp_fid0_c2;
  85. unsigned short interp_fid0_c3;
  86. unsigned short anchor_fid1_c0;
  87. unsigned short anchor_fid1_c1;
  88. unsigned short anchor_fid1_c2;
  89. unsigned short anchor_fid1_c3;
  90. unsigned short interp_fid1_c0;
  91. unsigned short interp_fid1_c1;
  92. unsigned short interp_fid1_c2;
  93. unsigned short interp_fid1_c3;
  94. };
  95. /*
  96. * Default upsampler coefficients
  97. */
  98. static const struct vpe_us_coeffs us_coeffs[] = {
  99. {
  100. /* Coefficients for progressive input */
  101. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  102. 0x00C8, 0x0348, 0x0018, 0x3FD8, 0x3FB8, 0x0378, 0x00E8, 0x3FE8,
  103. },
  104. {
  105. /* Coefficients for Top Field Interlaced input */
  106. 0x0051, 0x03D5, 0x3FE3, 0x3FF7, 0x3FB5, 0x02E9, 0x018F, 0x3FD3,
  107. /* Coefficients for Bottom Field Interlaced input */
  108. 0x016B, 0x0247, 0x00B1, 0x3F9D, 0x3FCF, 0x03DB, 0x005D, 0x3FF9,
  109. },
  110. };
  111. /*
  112. * the following registers are for configuring some of the parameters of the
  113. * motion and edge detection blocks inside DEI, these generally remain the same,
  114. * these could be passed later via userspace if some one needs to tweak these.
  115. */
  116. struct vpe_dei_regs {
  117. unsigned long mdt_spacial_freq_thr_reg; /* VPE_DEI_REG2 */
  118. unsigned long edi_config_reg; /* VPE_DEI_REG3 */
  119. unsigned long edi_lut_reg0; /* VPE_DEI_REG4 */
  120. unsigned long edi_lut_reg1; /* VPE_DEI_REG5 */
  121. unsigned long edi_lut_reg2; /* VPE_DEI_REG6 */
  122. unsigned long edi_lut_reg3; /* VPE_DEI_REG7 */
  123. };
  124. /*
  125. * default expert DEI register values, unlikely to be modified.
  126. */
  127. static const struct vpe_dei_regs dei_regs = {
  128. .mdt_spacial_freq_thr_reg = 0x020C0804u,
  129. .edi_config_reg = 0x0118100Cu,
  130. .edi_lut_reg0 = 0x08040200u,
  131. .edi_lut_reg1 = 0x1010100Cu,
  132. .edi_lut_reg2 = 0x10101010u,
  133. .edi_lut_reg3 = 0x10101010u,
  134. };
  135. /*
  136. * The port_data structure contains per-port data.
  137. */
  138. struct vpe_port_data {
  139. enum vpdma_channel channel; /* VPDMA channel */
  140. u8 vb_index; /* input frame f, f-1, f-2 index */
  141. u8 vb_part; /* plane index for co-panar formats */
  142. };
  143. /*
  144. * Define indices into the port_data tables
  145. */
  146. #define VPE_PORT_LUMA1_IN 0
  147. #define VPE_PORT_CHROMA1_IN 1
  148. #define VPE_PORT_LUMA2_IN 2
  149. #define VPE_PORT_CHROMA2_IN 3
  150. #define VPE_PORT_LUMA3_IN 4
  151. #define VPE_PORT_CHROMA3_IN 5
  152. #define VPE_PORT_MV_IN 6
  153. #define VPE_PORT_MV_OUT 7
  154. #define VPE_PORT_LUMA_OUT 8
  155. #define VPE_PORT_CHROMA_OUT 9
  156. #define VPE_PORT_RGB_OUT 10
  157. static const struct vpe_port_data port_data[11] = {
  158. [VPE_PORT_LUMA1_IN] = {
  159. .channel = VPE_CHAN_LUMA1_IN,
  160. .vb_index = 0,
  161. .vb_part = VPE_LUMA,
  162. },
  163. [VPE_PORT_CHROMA1_IN] = {
  164. .channel = VPE_CHAN_CHROMA1_IN,
  165. .vb_index = 0,
  166. .vb_part = VPE_CHROMA,
  167. },
  168. [VPE_PORT_LUMA2_IN] = {
  169. .channel = VPE_CHAN_LUMA2_IN,
  170. .vb_index = 1,
  171. .vb_part = VPE_LUMA,
  172. },
  173. [VPE_PORT_CHROMA2_IN] = {
  174. .channel = VPE_CHAN_CHROMA2_IN,
  175. .vb_index = 1,
  176. .vb_part = VPE_CHROMA,
  177. },
  178. [VPE_PORT_LUMA3_IN] = {
  179. .channel = VPE_CHAN_LUMA3_IN,
  180. .vb_index = 2,
  181. .vb_part = VPE_LUMA,
  182. },
  183. [VPE_PORT_CHROMA3_IN] = {
  184. .channel = VPE_CHAN_CHROMA3_IN,
  185. .vb_index = 2,
  186. .vb_part = VPE_CHROMA,
  187. },
  188. [VPE_PORT_MV_IN] = {
  189. .channel = VPE_CHAN_MV_IN,
  190. },
  191. [VPE_PORT_MV_OUT] = {
  192. .channel = VPE_CHAN_MV_OUT,
  193. },
  194. [VPE_PORT_LUMA_OUT] = {
  195. .channel = VPE_CHAN_LUMA_OUT,
  196. .vb_part = VPE_LUMA,
  197. },
  198. [VPE_PORT_CHROMA_OUT] = {
  199. .channel = VPE_CHAN_CHROMA_OUT,
  200. .vb_part = VPE_CHROMA,
  201. },
  202. [VPE_PORT_RGB_OUT] = {
  203. .channel = VPE_CHAN_RGB_OUT,
  204. .vb_part = VPE_LUMA,
  205. },
  206. };
  207. /* driver info for each of the supported video formats */
  208. struct vpe_fmt {
  209. char *name; /* human-readable name */
  210. u32 fourcc; /* standard format identifier */
  211. u8 types; /* CAPTURE and/or OUTPUT */
  212. u8 coplanar; /* set for unpacked Luma and Chroma */
  213. /* vpdma format info for each plane */
  214. struct vpdma_data_format const *vpdma_fmt[VPE_MAX_PLANES];
  215. };
  216. static struct vpe_fmt vpe_formats[] = {
  217. {
  218. .name = "NV16 YUV 422 co-planar",
  219. .fourcc = V4L2_PIX_FMT_NV16,
  220. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  221. .coplanar = 1,
  222. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
  223. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
  224. },
  225. },
  226. {
  227. .name = "NV12 YUV 420 co-planar",
  228. .fourcc = V4L2_PIX_FMT_NV12,
  229. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  230. .coplanar = 1,
  231. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
  232. &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
  233. },
  234. },
  235. {
  236. .name = "YUYV 422 packed",
  237. .fourcc = V4L2_PIX_FMT_YUYV,
  238. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  239. .coplanar = 0,
  240. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCB422],
  241. },
  242. },
  243. {
  244. .name = "UYVY 422 packed",
  245. .fourcc = V4L2_PIX_FMT_UYVY,
  246. .types = VPE_FMT_TYPE_CAPTURE | VPE_FMT_TYPE_OUTPUT,
  247. .coplanar = 0,
  248. .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
  249. },
  250. },
  251. {
  252. .name = "RGB888 packed",
  253. .fourcc = V4L2_PIX_FMT_RGB24,
  254. .types = VPE_FMT_TYPE_CAPTURE,
  255. .coplanar = 0,
  256. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
  257. },
  258. },
  259. {
  260. .name = "ARGB32",
  261. .fourcc = V4L2_PIX_FMT_RGB32,
  262. .types = VPE_FMT_TYPE_CAPTURE,
  263. .coplanar = 0,
  264. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
  265. },
  266. },
  267. {
  268. .name = "BGR888 packed",
  269. .fourcc = V4L2_PIX_FMT_BGR24,
  270. .types = VPE_FMT_TYPE_CAPTURE,
  271. .coplanar = 0,
  272. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
  273. },
  274. },
  275. {
  276. .name = "ABGR32",
  277. .fourcc = V4L2_PIX_FMT_BGR32,
  278. .types = VPE_FMT_TYPE_CAPTURE,
  279. .coplanar = 0,
  280. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
  281. },
  282. },
  283. {
  284. .name = "RGB565",
  285. .fourcc = V4L2_PIX_FMT_RGB565,
  286. .types = VPE_FMT_TYPE_CAPTURE,
  287. .coplanar = 0,
  288. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB565],
  289. },
  290. },
  291. {
  292. .name = "RGB5551",
  293. .fourcc = V4L2_PIX_FMT_RGB555,
  294. .types = VPE_FMT_TYPE_CAPTURE,
  295. .coplanar = 0,
  296. .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGBA16_5551],
  297. },
  298. },
  299. };
  300. /*
  301. * per-queue, driver-specific private data.
  302. * there is one source queue and one destination queue for each m2m context.
  303. */
  304. struct vpe_q_data {
  305. unsigned int width; /* frame width */
  306. unsigned int height; /* frame height */
  307. unsigned int nplanes; /* Current number of planes */
  308. unsigned int bytesperline[VPE_MAX_PLANES]; /* bytes per line in memory */
  309. enum v4l2_colorspace colorspace;
  310. enum v4l2_field field; /* supported field value */
  311. unsigned int flags;
  312. unsigned int sizeimage[VPE_MAX_PLANES]; /* image size in memory */
  313. struct v4l2_rect c_rect; /* crop/compose rectangle */
  314. struct vpe_fmt *fmt; /* format info */
  315. };
  316. /* vpe_q_data flag bits */
  317. #define Q_DATA_FRAME_1D BIT(0)
  318. #define Q_DATA_MODE_TILED BIT(1)
  319. #define Q_DATA_INTERLACED_ALTERNATE BIT(2)
  320. #define Q_DATA_INTERLACED_SEQ_TB BIT(3)
  321. #define Q_IS_INTERLACED (Q_DATA_INTERLACED_ALTERNATE | \
  322. Q_DATA_INTERLACED_SEQ_TB)
  323. enum {
  324. Q_DATA_SRC = 0,
  325. Q_DATA_DST = 1,
  326. };
  327. /* find our format description corresponding to the passed v4l2_format */
  328. static struct vpe_fmt *__find_format(u32 fourcc)
  329. {
  330. struct vpe_fmt *fmt;
  331. unsigned int k;
  332. for (k = 0; k < ARRAY_SIZE(vpe_formats); k++) {
  333. fmt = &vpe_formats[k];
  334. if (fmt->fourcc == fourcc)
  335. return fmt;
  336. }
  337. return NULL;
  338. }
  339. static struct vpe_fmt *find_format(struct v4l2_format *f)
  340. {
  341. return __find_format(f->fmt.pix.pixelformat);
  342. }
  343. /*
  344. * there is one vpe_dev structure in the driver, it is shared by
  345. * all instances.
  346. */
  347. struct vpe_dev {
  348. struct v4l2_device v4l2_dev;
  349. struct video_device vfd;
  350. struct v4l2_m2m_dev *m2m_dev;
  351. atomic_t num_instances; /* count of driver instances */
  352. dma_addr_t loaded_mmrs; /* shadow mmrs in device */
  353. struct mutex dev_mutex;
  354. spinlock_t lock;
  355. int irq;
  356. void __iomem *base;
  357. struct resource *res;
  358. struct vpdma_data vpdma_data;
  359. struct vpdma_data *vpdma; /* vpdma data handle */
  360. struct sc_data *sc; /* scaler data handle */
  361. struct csc_data *csc; /* csc data handle */
  362. };
  363. /*
  364. * There is one vpe_ctx structure for each m2m context.
  365. */
  366. struct vpe_ctx {
  367. struct v4l2_fh fh;
  368. struct vpe_dev *dev;
  369. struct v4l2_ctrl_handler hdl;
  370. unsigned int field; /* current field */
  371. unsigned int sequence; /* current frame/field seq */
  372. unsigned int aborting; /* abort after next irq */
  373. unsigned int bufs_per_job; /* input buffers per batch */
  374. unsigned int bufs_completed; /* bufs done in this batch */
  375. struct vpe_q_data q_data[2]; /* src & dst queue data */
  376. struct vb2_v4l2_buffer *src_vbs[VPE_MAX_SRC_BUFS];
  377. struct vb2_v4l2_buffer *dst_vb;
  378. dma_addr_t mv_buf_dma[2]; /* dma addrs of motion vector in/out bufs */
  379. void *mv_buf[2]; /* virtual addrs of motion vector bufs */
  380. size_t mv_buf_size; /* current motion vector buffer size */
  381. struct vpdma_buf mmr_adb; /* shadow reg addr/data block */
  382. struct vpdma_buf sc_coeff_h; /* h coeff buffer */
  383. struct vpdma_buf sc_coeff_v; /* v coeff buffer */
  384. struct vpdma_desc_list desc_list; /* DMA descriptor list */
  385. bool deinterlacing; /* using de-interlacer */
  386. bool load_mmrs; /* have new shadow reg values */
  387. unsigned int src_mv_buf_selector;
  388. };
  389. /*
  390. * M2M devices get 2 queues.
  391. * Return the queue given the type.
  392. */
  393. static struct vpe_q_data *get_q_data(struct vpe_ctx *ctx,
  394. enum v4l2_buf_type type)
  395. {
  396. switch (type) {
  397. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  398. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  399. return &ctx->q_data[Q_DATA_SRC];
  400. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  401. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  402. return &ctx->q_data[Q_DATA_DST];
  403. default:
  404. return NULL;
  405. }
  406. return NULL;
  407. }
  408. static u32 read_reg(struct vpe_dev *dev, int offset)
  409. {
  410. return ioread32(dev->base + offset);
  411. }
  412. static void write_reg(struct vpe_dev *dev, int offset, u32 value)
  413. {
  414. iowrite32(value, dev->base + offset);
  415. }
  416. /* register field read/write helpers */
  417. static int get_field(u32 value, u32 mask, int shift)
  418. {
  419. return (value & (mask << shift)) >> shift;
  420. }
  421. static int read_field_reg(struct vpe_dev *dev, int offset, u32 mask, int shift)
  422. {
  423. return get_field(read_reg(dev, offset), mask, shift);
  424. }
  425. static void write_field(u32 *valp, u32 field, u32 mask, int shift)
  426. {
  427. u32 val = *valp;
  428. val &= ~(mask << shift);
  429. val |= (field & mask) << shift;
  430. *valp = val;
  431. }
  432. static void write_field_reg(struct vpe_dev *dev, int offset, u32 field,
  433. u32 mask, int shift)
  434. {
  435. u32 val = read_reg(dev, offset);
  436. write_field(&val, field, mask, shift);
  437. write_reg(dev, offset, val);
  438. }
  439. /*
  440. * DMA address/data block for the shadow registers
  441. */
  442. struct vpe_mmr_adb {
  443. struct vpdma_adb_hdr out_fmt_hdr;
  444. u32 out_fmt_reg[1];
  445. u32 out_fmt_pad[3];
  446. struct vpdma_adb_hdr us1_hdr;
  447. u32 us1_regs[8];
  448. struct vpdma_adb_hdr us2_hdr;
  449. u32 us2_regs[8];
  450. struct vpdma_adb_hdr us3_hdr;
  451. u32 us3_regs[8];
  452. struct vpdma_adb_hdr dei_hdr;
  453. u32 dei_regs[8];
  454. struct vpdma_adb_hdr sc_hdr0;
  455. u32 sc_regs0[7];
  456. u32 sc_pad0[1];
  457. struct vpdma_adb_hdr sc_hdr8;
  458. u32 sc_regs8[6];
  459. u32 sc_pad8[2];
  460. struct vpdma_adb_hdr sc_hdr17;
  461. u32 sc_regs17[9];
  462. u32 sc_pad17[3];
  463. struct vpdma_adb_hdr csc_hdr;
  464. u32 csc_regs[6];
  465. u32 csc_pad[2];
  466. };
  467. #define GET_OFFSET_TOP(ctx, obj, reg) \
  468. ((obj)->res->start - ctx->dev->res->start + reg)
  469. #define VPE_SET_MMR_ADB_HDR(ctx, hdr, regs, offset_a) \
  470. VPDMA_SET_MMR_ADB_HDR(ctx->mmr_adb, vpe_mmr_adb, hdr, regs, offset_a)
  471. /*
  472. * Set the headers for all of the address/data block structures.
  473. */
  474. static void init_adb_hdrs(struct vpe_ctx *ctx)
  475. {
  476. VPE_SET_MMR_ADB_HDR(ctx, out_fmt_hdr, out_fmt_reg, VPE_CLK_FORMAT_SELECT);
  477. VPE_SET_MMR_ADB_HDR(ctx, us1_hdr, us1_regs, VPE_US1_R0);
  478. VPE_SET_MMR_ADB_HDR(ctx, us2_hdr, us2_regs, VPE_US2_R0);
  479. VPE_SET_MMR_ADB_HDR(ctx, us3_hdr, us3_regs, VPE_US3_R0);
  480. VPE_SET_MMR_ADB_HDR(ctx, dei_hdr, dei_regs, VPE_DEI_FRAME_SIZE);
  481. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr0, sc_regs0,
  482. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC0));
  483. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr8, sc_regs8,
  484. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC8));
  485. VPE_SET_MMR_ADB_HDR(ctx, sc_hdr17, sc_regs17,
  486. GET_OFFSET_TOP(ctx, ctx->dev->sc, CFG_SC17));
  487. VPE_SET_MMR_ADB_HDR(ctx, csc_hdr, csc_regs,
  488. GET_OFFSET_TOP(ctx, ctx->dev->csc, CSC_CSC00));
  489. };
  490. /*
  491. * Allocate or re-allocate the motion vector DMA buffers
  492. * There are two buffers, one for input and one for output.
  493. * However, the roles are reversed after each field is processed.
  494. * In other words, after each field is processed, the previous
  495. * output (dst) MV buffer becomes the new input (src) MV buffer.
  496. */
  497. static int realloc_mv_buffers(struct vpe_ctx *ctx, size_t size)
  498. {
  499. struct device *dev = ctx->dev->v4l2_dev.dev;
  500. if (ctx->mv_buf_size == size)
  501. return 0;
  502. if (ctx->mv_buf[0])
  503. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[0],
  504. ctx->mv_buf_dma[0]);
  505. if (ctx->mv_buf[1])
  506. dma_free_coherent(dev, ctx->mv_buf_size, ctx->mv_buf[1],
  507. ctx->mv_buf_dma[1]);
  508. if (size == 0)
  509. return 0;
  510. ctx->mv_buf[0] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[0],
  511. GFP_KERNEL);
  512. if (!ctx->mv_buf[0]) {
  513. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  514. return -ENOMEM;
  515. }
  516. ctx->mv_buf[1] = dma_alloc_coherent(dev, size, &ctx->mv_buf_dma[1],
  517. GFP_KERNEL);
  518. if (!ctx->mv_buf[1]) {
  519. vpe_err(ctx->dev, "failed to allocate motion vector buffer\n");
  520. dma_free_coherent(dev, size, ctx->mv_buf[0],
  521. ctx->mv_buf_dma[0]);
  522. return -ENOMEM;
  523. }
  524. ctx->mv_buf_size = size;
  525. ctx->src_mv_buf_selector = 0;
  526. return 0;
  527. }
  528. static void free_mv_buffers(struct vpe_ctx *ctx)
  529. {
  530. realloc_mv_buffers(ctx, 0);
  531. }
  532. /*
  533. * While de-interlacing, we keep the two most recent input buffers
  534. * around. This function frees those two buffers when we have
  535. * finished processing the current stream.
  536. */
  537. static void free_vbs(struct vpe_ctx *ctx)
  538. {
  539. struct vpe_dev *dev = ctx->dev;
  540. unsigned long flags;
  541. if (ctx->src_vbs[2] == NULL)
  542. return;
  543. spin_lock_irqsave(&dev->lock, flags);
  544. if (ctx->src_vbs[2]) {
  545. v4l2_m2m_buf_done(ctx->src_vbs[2], VB2_BUF_STATE_DONE);
  546. if (ctx->src_vbs[1] && (ctx->src_vbs[1] != ctx->src_vbs[2]))
  547. v4l2_m2m_buf_done(ctx->src_vbs[1], VB2_BUF_STATE_DONE);
  548. ctx->src_vbs[2] = NULL;
  549. ctx->src_vbs[1] = NULL;
  550. }
  551. spin_unlock_irqrestore(&dev->lock, flags);
  552. }
  553. /*
  554. * Enable or disable the VPE clocks
  555. */
  556. static void vpe_set_clock_enable(struct vpe_dev *dev, bool on)
  557. {
  558. u32 val = 0;
  559. if (on)
  560. val = VPE_DATA_PATH_CLK_ENABLE | VPE_VPEDMA_CLK_ENABLE;
  561. write_reg(dev, VPE_CLK_ENABLE, val);
  562. }
  563. static void vpe_top_reset(struct vpe_dev *dev)
  564. {
  565. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_DATA_PATH_CLK_RESET_MASK,
  566. VPE_DATA_PATH_CLK_RESET_SHIFT);
  567. usleep_range(100, 150);
  568. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_DATA_PATH_CLK_RESET_MASK,
  569. VPE_DATA_PATH_CLK_RESET_SHIFT);
  570. }
  571. static void vpe_top_vpdma_reset(struct vpe_dev *dev)
  572. {
  573. write_field_reg(dev, VPE_CLK_RESET, 1, VPE_VPDMA_CLK_RESET_MASK,
  574. VPE_VPDMA_CLK_RESET_SHIFT);
  575. usleep_range(100, 150);
  576. write_field_reg(dev, VPE_CLK_RESET, 0, VPE_VPDMA_CLK_RESET_MASK,
  577. VPE_VPDMA_CLK_RESET_SHIFT);
  578. }
  579. /*
  580. * Load the correct of upsampler coefficients into the shadow MMRs
  581. */
  582. static void set_us_coefficients(struct vpe_ctx *ctx)
  583. {
  584. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  585. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  586. u32 *us1_reg = &mmr_adb->us1_regs[0];
  587. u32 *us2_reg = &mmr_adb->us2_regs[0];
  588. u32 *us3_reg = &mmr_adb->us3_regs[0];
  589. const unsigned short *cp, *end_cp;
  590. cp = &us_coeffs[0].anchor_fid0_c0;
  591. if (s_q_data->flags & Q_IS_INTERLACED) /* interlaced */
  592. cp += sizeof(us_coeffs[0]) / sizeof(*cp);
  593. end_cp = cp + sizeof(us_coeffs[0]) / sizeof(*cp);
  594. while (cp < end_cp) {
  595. write_field(us1_reg, *cp++, VPE_US_C0_MASK, VPE_US_C0_SHIFT);
  596. write_field(us1_reg, *cp++, VPE_US_C1_MASK, VPE_US_C1_SHIFT);
  597. *us2_reg++ = *us1_reg;
  598. *us3_reg++ = *us1_reg++;
  599. }
  600. ctx->load_mmrs = true;
  601. }
  602. /*
  603. * Set the upsampler config mode and the VPDMA line mode in the shadow MMRs.
  604. */
  605. static void set_cfg_modes(struct vpe_ctx *ctx)
  606. {
  607. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  608. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  609. u32 *us1_reg0 = &mmr_adb->us1_regs[0];
  610. u32 *us2_reg0 = &mmr_adb->us2_regs[0];
  611. u32 *us3_reg0 = &mmr_adb->us3_regs[0];
  612. int cfg_mode = 1;
  613. /*
  614. * Cfg Mode 0: YUV420 source, enable upsampler, DEI is de-interlacing.
  615. * Cfg Mode 1: YUV422 source, disable upsampler, DEI is de-interlacing.
  616. */
  617. if (fmt->fourcc == V4L2_PIX_FMT_NV12)
  618. cfg_mode = 0;
  619. write_field(us1_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  620. write_field(us2_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  621. write_field(us3_reg0, cfg_mode, VPE_US_MODE_MASK, VPE_US_MODE_SHIFT);
  622. ctx->load_mmrs = true;
  623. }
  624. static void set_line_modes(struct vpe_ctx *ctx)
  625. {
  626. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_SRC].fmt;
  627. int line_mode = 1;
  628. if (fmt->fourcc == V4L2_PIX_FMT_NV12)
  629. line_mode = 0; /* double lines to line buffer */
  630. /* regs for now */
  631. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA1_IN);
  632. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA2_IN);
  633. vpdma_set_line_mode(ctx->dev->vpdma, line_mode, VPE_CHAN_CHROMA3_IN);
  634. /* frame start for input luma */
  635. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  636. VPE_CHAN_LUMA1_IN);
  637. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  638. VPE_CHAN_LUMA2_IN);
  639. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  640. VPE_CHAN_LUMA3_IN);
  641. /* frame start for input chroma */
  642. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  643. VPE_CHAN_CHROMA1_IN);
  644. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  645. VPE_CHAN_CHROMA2_IN);
  646. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  647. VPE_CHAN_CHROMA3_IN);
  648. /* frame start for MV in client */
  649. vpdma_set_frame_start_event(ctx->dev->vpdma, VPDMA_FSEVENT_CHANNEL_ACTIVE,
  650. VPE_CHAN_MV_IN);
  651. }
  652. /*
  653. * Set the shadow registers that are modified when the source
  654. * format changes.
  655. */
  656. static void set_src_registers(struct vpe_ctx *ctx)
  657. {
  658. set_us_coefficients(ctx);
  659. }
  660. /*
  661. * Set the shadow registers that are modified when the destination
  662. * format changes.
  663. */
  664. static void set_dst_registers(struct vpe_ctx *ctx)
  665. {
  666. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  667. enum v4l2_colorspace clrspc = ctx->q_data[Q_DATA_DST].colorspace;
  668. struct vpe_fmt *fmt = ctx->q_data[Q_DATA_DST].fmt;
  669. u32 val = 0;
  670. if (clrspc == V4L2_COLORSPACE_SRGB) {
  671. val |= VPE_RGB_OUT_SELECT;
  672. vpdma_set_bg_color(ctx->dev->vpdma,
  673. (struct vpdma_data_format *)fmt->vpdma_fmt[0], 0xff);
  674. } else if (fmt->fourcc == V4L2_PIX_FMT_NV16)
  675. val |= VPE_COLOR_SEPARATE_422;
  676. /*
  677. * the source of CHR_DS and CSC is always the scaler, irrespective of
  678. * whether it's used or not
  679. */
  680. val |= VPE_DS_SRC_DEI_SCALER | VPE_CSC_SRC_DEI_SCALER;
  681. if (fmt->fourcc != V4L2_PIX_FMT_NV12)
  682. val |= VPE_DS_BYPASS;
  683. mmr_adb->out_fmt_reg[0] = val;
  684. ctx->load_mmrs = true;
  685. }
  686. /*
  687. * Set the de-interlacer shadow register values
  688. */
  689. static void set_dei_regs(struct vpe_ctx *ctx)
  690. {
  691. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  692. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  693. unsigned int src_h = s_q_data->c_rect.height;
  694. unsigned int src_w = s_q_data->c_rect.width;
  695. u32 *dei_mmr0 = &mmr_adb->dei_regs[0];
  696. bool deinterlace = true;
  697. u32 val = 0;
  698. /*
  699. * according to TRM, we should set DEI in progressive bypass mode when
  700. * the input content is progressive, however, DEI is bypassed correctly
  701. * for both progressive and interlace content in interlace bypass mode.
  702. * It has been recommended not to use progressive bypass mode.
  703. */
  704. if (!(s_q_data->flags & Q_IS_INTERLACED) || !ctx->deinterlacing) {
  705. deinterlace = false;
  706. val = VPE_DEI_INTERLACE_BYPASS;
  707. }
  708. src_h = deinterlace ? src_h * 2 : src_h;
  709. val |= (src_h << VPE_DEI_HEIGHT_SHIFT) |
  710. (src_w << VPE_DEI_WIDTH_SHIFT) |
  711. VPE_DEI_FIELD_FLUSH;
  712. *dei_mmr0 = val;
  713. ctx->load_mmrs = true;
  714. }
  715. static void set_dei_shadow_registers(struct vpe_ctx *ctx)
  716. {
  717. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  718. u32 *dei_mmr = &mmr_adb->dei_regs[0];
  719. const struct vpe_dei_regs *cur = &dei_regs;
  720. dei_mmr[2] = cur->mdt_spacial_freq_thr_reg;
  721. dei_mmr[3] = cur->edi_config_reg;
  722. dei_mmr[4] = cur->edi_lut_reg0;
  723. dei_mmr[5] = cur->edi_lut_reg1;
  724. dei_mmr[6] = cur->edi_lut_reg2;
  725. dei_mmr[7] = cur->edi_lut_reg3;
  726. ctx->load_mmrs = true;
  727. }
  728. static void config_edi_input_mode(struct vpe_ctx *ctx, int mode)
  729. {
  730. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  731. u32 *edi_config_reg = &mmr_adb->dei_regs[3];
  732. if (mode & 0x2)
  733. write_field(edi_config_reg, 1, 1, 2); /* EDI_ENABLE_3D */
  734. if (mode & 0x3)
  735. write_field(edi_config_reg, 1, 1, 3); /* EDI_CHROMA_3D */
  736. write_field(edi_config_reg, mode, VPE_EDI_INP_MODE_MASK,
  737. VPE_EDI_INP_MODE_SHIFT);
  738. ctx->load_mmrs = true;
  739. }
  740. /*
  741. * Set the shadow registers whose values are modified when either the
  742. * source or destination format is changed.
  743. */
  744. static int set_srcdst_params(struct vpe_ctx *ctx)
  745. {
  746. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  747. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  748. struct vpe_mmr_adb *mmr_adb = ctx->mmr_adb.addr;
  749. unsigned int src_w = s_q_data->c_rect.width;
  750. unsigned int src_h = s_q_data->c_rect.height;
  751. unsigned int dst_w = d_q_data->c_rect.width;
  752. unsigned int dst_h = d_q_data->c_rect.height;
  753. size_t mv_buf_size;
  754. int ret;
  755. ctx->sequence = 0;
  756. ctx->field = V4L2_FIELD_TOP;
  757. if ((s_q_data->flags & Q_IS_INTERLACED) &&
  758. !(d_q_data->flags & Q_IS_INTERLACED)) {
  759. int bytes_per_line;
  760. const struct vpdma_data_format *mv =
  761. &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  762. /*
  763. * we make sure that the source image has a 16 byte aligned
  764. * stride, we need to do the same for the motion vector buffer
  765. * by aligning it's stride to the next 16 byte boundry. this
  766. * extra space will not be used by the de-interlacer, but will
  767. * ensure that vpdma operates correctly
  768. */
  769. bytes_per_line = ALIGN((s_q_data->width * mv->depth) >> 3,
  770. VPDMA_STRIDE_ALIGN);
  771. mv_buf_size = bytes_per_line * s_q_data->height;
  772. ctx->deinterlacing = true;
  773. src_h <<= 1;
  774. } else {
  775. ctx->deinterlacing = false;
  776. mv_buf_size = 0;
  777. }
  778. free_vbs(ctx);
  779. ctx->src_vbs[2] = ctx->src_vbs[1] = ctx->src_vbs[0] = NULL;
  780. ret = realloc_mv_buffers(ctx, mv_buf_size);
  781. if (ret)
  782. return ret;
  783. set_cfg_modes(ctx);
  784. set_dei_regs(ctx);
  785. csc_set_coeff(ctx->dev->csc, &mmr_adb->csc_regs[0],
  786. s_q_data->colorspace, d_q_data->colorspace);
  787. sc_set_hs_coeffs(ctx->dev->sc, ctx->sc_coeff_h.addr, src_w, dst_w);
  788. sc_set_vs_coeffs(ctx->dev->sc, ctx->sc_coeff_v.addr, src_h, dst_h);
  789. sc_config_scaler(ctx->dev->sc, &mmr_adb->sc_regs0[0],
  790. &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
  791. src_w, src_h, dst_w, dst_h);
  792. return 0;
  793. }
  794. /*
  795. * Return the vpe_ctx structure for a given struct file
  796. */
  797. static struct vpe_ctx *file2ctx(struct file *file)
  798. {
  799. return container_of(file->private_data, struct vpe_ctx, fh);
  800. }
  801. /*
  802. * mem2mem callbacks
  803. */
  804. /*
  805. * job_ready() - check whether an instance is ready to be scheduled to run
  806. */
  807. static int job_ready(void *priv)
  808. {
  809. struct vpe_ctx *ctx = priv;
  810. /*
  811. * This check is needed as this might be called directly from driver
  812. * When called by m2m framework, this will always satisfy, but when
  813. * called from vpe_irq, this might fail. (src stream with zero buffers)
  814. */
  815. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) <= 0 ||
  816. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) <= 0)
  817. return 0;
  818. return 1;
  819. }
  820. static void job_abort(void *priv)
  821. {
  822. struct vpe_ctx *ctx = priv;
  823. /* Will cancel the transaction in the next interrupt handler */
  824. ctx->aborting = 1;
  825. }
  826. static void vpe_dump_regs(struct vpe_dev *dev)
  827. {
  828. #define DUMPREG(r) vpe_dbg(dev, "%-35s %08x\n", #r, read_reg(dev, VPE_##r))
  829. vpe_dbg(dev, "VPE Registers:\n");
  830. DUMPREG(PID);
  831. DUMPREG(SYSCONFIG);
  832. DUMPREG(INT0_STATUS0_RAW);
  833. DUMPREG(INT0_STATUS0);
  834. DUMPREG(INT0_ENABLE0);
  835. DUMPREG(INT0_STATUS1_RAW);
  836. DUMPREG(INT0_STATUS1);
  837. DUMPREG(INT0_ENABLE1);
  838. DUMPREG(CLK_ENABLE);
  839. DUMPREG(CLK_RESET);
  840. DUMPREG(CLK_FORMAT_SELECT);
  841. DUMPREG(CLK_RANGE_MAP);
  842. DUMPREG(US1_R0);
  843. DUMPREG(US1_R1);
  844. DUMPREG(US1_R2);
  845. DUMPREG(US1_R3);
  846. DUMPREG(US1_R4);
  847. DUMPREG(US1_R5);
  848. DUMPREG(US1_R6);
  849. DUMPREG(US1_R7);
  850. DUMPREG(US2_R0);
  851. DUMPREG(US2_R1);
  852. DUMPREG(US2_R2);
  853. DUMPREG(US2_R3);
  854. DUMPREG(US2_R4);
  855. DUMPREG(US2_R5);
  856. DUMPREG(US2_R6);
  857. DUMPREG(US2_R7);
  858. DUMPREG(US3_R0);
  859. DUMPREG(US3_R1);
  860. DUMPREG(US3_R2);
  861. DUMPREG(US3_R3);
  862. DUMPREG(US3_R4);
  863. DUMPREG(US3_R5);
  864. DUMPREG(US3_R6);
  865. DUMPREG(US3_R7);
  866. DUMPREG(DEI_FRAME_SIZE);
  867. DUMPREG(MDT_BYPASS);
  868. DUMPREG(MDT_SF_THRESHOLD);
  869. DUMPREG(EDI_CONFIG);
  870. DUMPREG(DEI_EDI_LUT_R0);
  871. DUMPREG(DEI_EDI_LUT_R1);
  872. DUMPREG(DEI_EDI_LUT_R2);
  873. DUMPREG(DEI_EDI_LUT_R3);
  874. DUMPREG(DEI_FMD_WINDOW_R0);
  875. DUMPREG(DEI_FMD_WINDOW_R1);
  876. DUMPREG(DEI_FMD_CONTROL_R0);
  877. DUMPREG(DEI_FMD_CONTROL_R1);
  878. DUMPREG(DEI_FMD_STATUS_R0);
  879. DUMPREG(DEI_FMD_STATUS_R1);
  880. DUMPREG(DEI_FMD_STATUS_R2);
  881. #undef DUMPREG
  882. sc_dump_regs(dev->sc);
  883. csc_dump_regs(dev->csc);
  884. }
  885. static void add_out_dtd(struct vpe_ctx *ctx, int port)
  886. {
  887. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_DST];
  888. const struct vpe_port_data *p_data = &port_data[port];
  889. struct vb2_buffer *vb = &ctx->dst_vb->vb2_buf;
  890. struct vpe_fmt *fmt = q_data->fmt;
  891. const struct vpdma_data_format *vpdma_fmt;
  892. int mv_buf_selector = !ctx->src_mv_buf_selector;
  893. dma_addr_t dma_addr;
  894. u32 flags = 0;
  895. u32 offset = 0;
  896. u32 stride;
  897. if (port == VPE_PORT_MV_OUT) {
  898. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  899. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  900. q_data = &ctx->q_data[Q_DATA_SRC];
  901. stride = ALIGN((q_data->width * vpdma_fmt->depth) >> 3,
  902. VPDMA_STRIDE_ALIGN);
  903. } else {
  904. /* to incorporate interleaved formats */
  905. int plane = fmt->coplanar ? p_data->vb_part : 0;
  906. vpdma_fmt = fmt->vpdma_fmt[plane];
  907. /*
  908. * If we are using a single plane buffer and
  909. * we need to set a separate vpdma chroma channel.
  910. */
  911. if (q_data->nplanes == 1 && plane) {
  912. dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  913. /* Compute required offset */
  914. offset = q_data->bytesperline[0] * q_data->height;
  915. } else {
  916. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  917. /* Use address as is, no offset */
  918. offset = 0;
  919. }
  920. if (!dma_addr) {
  921. vpe_err(ctx->dev,
  922. "acquiring output buffer(%d) dma_addr failed\n",
  923. port);
  924. return;
  925. }
  926. /* Apply the offset */
  927. dma_addr += offset;
  928. stride = q_data->bytesperline[VPE_LUMA];
  929. }
  930. if (q_data->flags & Q_DATA_FRAME_1D)
  931. flags |= VPDMA_DATA_FRAME_1D;
  932. if (q_data->flags & Q_DATA_MODE_TILED)
  933. flags |= VPDMA_DATA_MODE_TILED;
  934. vpdma_set_max_size(ctx->dev->vpdma, VPDMA_MAX_SIZE1,
  935. MAX_W, MAX_H);
  936. vpdma_add_out_dtd(&ctx->desc_list, q_data->width,
  937. stride, &q_data->c_rect,
  938. vpdma_fmt, dma_addr, MAX_OUT_WIDTH_REG1,
  939. MAX_OUT_HEIGHT_REG1, p_data->channel, flags);
  940. }
  941. static void add_in_dtd(struct vpe_ctx *ctx, int port)
  942. {
  943. struct vpe_q_data *q_data = &ctx->q_data[Q_DATA_SRC];
  944. const struct vpe_port_data *p_data = &port_data[port];
  945. struct vb2_buffer *vb = &ctx->src_vbs[p_data->vb_index]->vb2_buf;
  946. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  947. struct vpe_fmt *fmt = q_data->fmt;
  948. const struct vpdma_data_format *vpdma_fmt;
  949. int mv_buf_selector = ctx->src_mv_buf_selector;
  950. int field = vbuf->field == V4L2_FIELD_BOTTOM;
  951. int frame_width, frame_height;
  952. dma_addr_t dma_addr;
  953. u32 flags = 0;
  954. u32 offset = 0;
  955. u32 stride;
  956. if (port == VPE_PORT_MV_IN) {
  957. vpdma_fmt = &vpdma_misc_fmts[VPDMA_DATA_FMT_MV];
  958. dma_addr = ctx->mv_buf_dma[mv_buf_selector];
  959. stride = ALIGN((q_data->width * vpdma_fmt->depth) >> 3,
  960. VPDMA_STRIDE_ALIGN);
  961. } else {
  962. /* to incorporate interleaved formats */
  963. int plane = fmt->coplanar ? p_data->vb_part : 0;
  964. vpdma_fmt = fmt->vpdma_fmt[plane];
  965. /*
  966. * If we are using a single plane buffer and
  967. * we need to set a separate vpdma chroma channel.
  968. */
  969. if (q_data->nplanes == 1 && plane) {
  970. dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  971. /* Compute required offset */
  972. offset = q_data->bytesperline[0] * q_data->height;
  973. } else {
  974. dma_addr = vb2_dma_contig_plane_dma_addr(vb, plane);
  975. /* Use address as is, no offset */
  976. offset = 0;
  977. }
  978. if (!dma_addr) {
  979. vpe_err(ctx->dev,
  980. "acquiring output buffer(%d) dma_addr failed\n",
  981. port);
  982. return;
  983. }
  984. /* Apply the offset */
  985. dma_addr += offset;
  986. stride = q_data->bytesperline[VPE_LUMA];
  987. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB) {
  988. /*
  989. * Use top or bottom field from same vb alternately
  990. * f,f-1,f-2 = TBT when seq is even
  991. * f,f-1,f-2 = BTB when seq is odd
  992. */
  993. field = (p_data->vb_index + (ctx->sequence % 2)) % 2;
  994. if (field) {
  995. /*
  996. * bottom field of a SEQ_TB buffer
  997. * Skip the top field data by
  998. */
  999. int height = q_data->height / 2;
  1000. int bpp = fmt->fourcc == V4L2_PIX_FMT_NV12 ?
  1001. 1 : (vpdma_fmt->depth >> 3);
  1002. if (plane)
  1003. height /= 2;
  1004. dma_addr += q_data->width * height * bpp;
  1005. }
  1006. }
  1007. }
  1008. if (q_data->flags & Q_DATA_FRAME_1D)
  1009. flags |= VPDMA_DATA_FRAME_1D;
  1010. if (q_data->flags & Q_DATA_MODE_TILED)
  1011. flags |= VPDMA_DATA_MODE_TILED;
  1012. frame_width = q_data->c_rect.width;
  1013. frame_height = q_data->c_rect.height;
  1014. if (p_data->vb_part && fmt->fourcc == V4L2_PIX_FMT_NV12)
  1015. frame_height /= 2;
  1016. vpdma_add_in_dtd(&ctx->desc_list, q_data->width, stride,
  1017. &q_data->c_rect, vpdma_fmt, dma_addr,
  1018. p_data->channel, field, flags, frame_width,
  1019. frame_height, 0, 0);
  1020. }
  1021. /*
  1022. * Enable the expected IRQ sources
  1023. */
  1024. static void enable_irqs(struct vpe_ctx *ctx)
  1025. {
  1026. write_reg(ctx->dev, VPE_INT0_ENABLE0_SET, VPE_INT0_LIST0_COMPLETE);
  1027. write_reg(ctx->dev, VPE_INT0_ENABLE1_SET, VPE_DEI_ERROR_INT |
  1028. VPE_DS1_UV_ERROR_INT);
  1029. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, true);
  1030. }
  1031. static void disable_irqs(struct vpe_ctx *ctx)
  1032. {
  1033. write_reg(ctx->dev, VPE_INT0_ENABLE0_CLR, 0xffffffff);
  1034. write_reg(ctx->dev, VPE_INT0_ENABLE1_CLR, 0xffffffff);
  1035. vpdma_enable_list_complete_irq(ctx->dev->vpdma, 0, 0, false);
  1036. }
  1037. /* device_run() - prepares and starts the device
  1038. *
  1039. * This function is only called when both the source and destination
  1040. * buffers are in place.
  1041. */
  1042. static void device_run(void *priv)
  1043. {
  1044. struct vpe_ctx *ctx = priv;
  1045. struct sc_data *sc = ctx->dev->sc;
  1046. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  1047. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  1048. if (ctx->deinterlacing && s_q_data->flags & Q_DATA_INTERLACED_SEQ_TB &&
  1049. ctx->sequence % 2 == 0) {
  1050. /* When using SEQ_TB buffers, When using it first time,
  1051. * No need to remove the buffer as the next field is present
  1052. * in the same buffer. (so that job_ready won't fail)
  1053. * It will be removed when using bottom field
  1054. */
  1055. ctx->src_vbs[0] = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1056. WARN_ON(ctx->src_vbs[0] == NULL);
  1057. } else {
  1058. ctx->src_vbs[0] = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1059. WARN_ON(ctx->src_vbs[0] == NULL);
  1060. }
  1061. ctx->dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1062. WARN_ON(ctx->dst_vb == NULL);
  1063. if (ctx->deinterlacing) {
  1064. if (ctx->src_vbs[2] == NULL) {
  1065. ctx->src_vbs[2] = ctx->src_vbs[0];
  1066. WARN_ON(ctx->src_vbs[2] == NULL);
  1067. ctx->src_vbs[1] = ctx->src_vbs[0];
  1068. WARN_ON(ctx->src_vbs[1] == NULL);
  1069. }
  1070. /*
  1071. * we have output the first 2 frames through line average, we
  1072. * now switch to EDI de-interlacer
  1073. */
  1074. if (ctx->sequence == 2)
  1075. config_edi_input_mode(ctx, 0x3); /* EDI (Y + UV) */
  1076. }
  1077. /* config descriptors */
  1078. if (ctx->dev->loaded_mmrs != ctx->mmr_adb.dma_addr || ctx->load_mmrs) {
  1079. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->mmr_adb);
  1080. vpdma_add_cfd_adb(&ctx->desc_list, CFD_MMR_CLIENT, &ctx->mmr_adb);
  1081. set_line_modes(ctx);
  1082. ctx->dev->loaded_mmrs = ctx->mmr_adb.dma_addr;
  1083. ctx->load_mmrs = false;
  1084. }
  1085. if (sc->loaded_coeff_h != ctx->sc_coeff_h.dma_addr ||
  1086. sc->load_coeff_h) {
  1087. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_h);
  1088. vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
  1089. &ctx->sc_coeff_h, 0);
  1090. sc->loaded_coeff_h = ctx->sc_coeff_h.dma_addr;
  1091. sc->load_coeff_h = false;
  1092. }
  1093. if (sc->loaded_coeff_v != ctx->sc_coeff_v.dma_addr ||
  1094. sc->load_coeff_v) {
  1095. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->sc_coeff_v);
  1096. vpdma_add_cfd_block(&ctx->desc_list, CFD_SC_CLIENT,
  1097. &ctx->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
  1098. sc->loaded_coeff_v = ctx->sc_coeff_v.dma_addr;
  1099. sc->load_coeff_v = false;
  1100. }
  1101. /* output data descriptors */
  1102. if (ctx->deinterlacing)
  1103. add_out_dtd(ctx, VPE_PORT_MV_OUT);
  1104. if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
  1105. add_out_dtd(ctx, VPE_PORT_RGB_OUT);
  1106. } else {
  1107. add_out_dtd(ctx, VPE_PORT_LUMA_OUT);
  1108. if (d_q_data->fmt->coplanar)
  1109. add_out_dtd(ctx, VPE_PORT_CHROMA_OUT);
  1110. }
  1111. /* input data descriptors */
  1112. if (ctx->deinterlacing) {
  1113. add_in_dtd(ctx, VPE_PORT_LUMA3_IN);
  1114. add_in_dtd(ctx, VPE_PORT_CHROMA3_IN);
  1115. add_in_dtd(ctx, VPE_PORT_LUMA2_IN);
  1116. add_in_dtd(ctx, VPE_PORT_CHROMA2_IN);
  1117. }
  1118. add_in_dtd(ctx, VPE_PORT_LUMA1_IN);
  1119. add_in_dtd(ctx, VPE_PORT_CHROMA1_IN);
  1120. if (ctx->deinterlacing)
  1121. add_in_dtd(ctx, VPE_PORT_MV_IN);
  1122. /* sync on channel control descriptors for input ports */
  1123. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_LUMA1_IN);
  1124. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_CHROMA1_IN);
  1125. if (ctx->deinterlacing) {
  1126. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1127. VPE_CHAN_LUMA2_IN);
  1128. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1129. VPE_CHAN_CHROMA2_IN);
  1130. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1131. VPE_CHAN_LUMA3_IN);
  1132. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1133. VPE_CHAN_CHROMA3_IN);
  1134. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_IN);
  1135. }
  1136. /* sync on channel control descriptors for output ports */
  1137. if (d_q_data->colorspace == V4L2_COLORSPACE_SRGB) {
  1138. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1139. VPE_CHAN_RGB_OUT);
  1140. } else {
  1141. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1142. VPE_CHAN_LUMA_OUT);
  1143. if (d_q_data->fmt->coplanar)
  1144. vpdma_add_sync_on_channel_ctd(&ctx->desc_list,
  1145. VPE_CHAN_CHROMA_OUT);
  1146. }
  1147. if (ctx->deinterlacing)
  1148. vpdma_add_sync_on_channel_ctd(&ctx->desc_list, VPE_CHAN_MV_OUT);
  1149. enable_irqs(ctx);
  1150. vpdma_map_desc_buf(ctx->dev->vpdma, &ctx->desc_list.buf);
  1151. vpdma_submit_descs(ctx->dev->vpdma, &ctx->desc_list, 0);
  1152. }
  1153. static void dei_error(struct vpe_ctx *ctx)
  1154. {
  1155. dev_warn(ctx->dev->v4l2_dev.dev,
  1156. "received DEI error interrupt\n");
  1157. }
  1158. static void ds1_uv_error(struct vpe_ctx *ctx)
  1159. {
  1160. dev_warn(ctx->dev->v4l2_dev.dev,
  1161. "received downsampler error interrupt\n");
  1162. }
  1163. static irqreturn_t vpe_irq(int irq_vpe, void *data)
  1164. {
  1165. struct vpe_dev *dev = (struct vpe_dev *)data;
  1166. struct vpe_ctx *ctx;
  1167. struct vpe_q_data *d_q_data;
  1168. struct vb2_v4l2_buffer *s_vb, *d_vb;
  1169. unsigned long flags;
  1170. u32 irqst0, irqst1;
  1171. bool list_complete = false;
  1172. irqst0 = read_reg(dev, VPE_INT0_STATUS0);
  1173. if (irqst0) {
  1174. write_reg(dev, VPE_INT0_STATUS0_CLR, irqst0);
  1175. vpe_dbg(dev, "INT0_STATUS0 = 0x%08x\n", irqst0);
  1176. }
  1177. irqst1 = read_reg(dev, VPE_INT0_STATUS1);
  1178. if (irqst1) {
  1179. write_reg(dev, VPE_INT0_STATUS1_CLR, irqst1);
  1180. vpe_dbg(dev, "INT0_STATUS1 = 0x%08x\n", irqst1);
  1181. }
  1182. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  1183. if (!ctx) {
  1184. vpe_err(dev, "instance released before end of transaction\n");
  1185. goto handled;
  1186. }
  1187. if (irqst1) {
  1188. if (irqst1 & VPE_DEI_ERROR_INT) {
  1189. irqst1 &= ~VPE_DEI_ERROR_INT;
  1190. dei_error(ctx);
  1191. }
  1192. if (irqst1 & VPE_DS1_UV_ERROR_INT) {
  1193. irqst1 &= ~VPE_DS1_UV_ERROR_INT;
  1194. ds1_uv_error(ctx);
  1195. }
  1196. }
  1197. if (irqst0) {
  1198. if (irqst0 & VPE_INT0_LIST0_COMPLETE)
  1199. vpdma_clear_list_stat(ctx->dev->vpdma, 0, 0);
  1200. irqst0 &= ~(VPE_INT0_LIST0_COMPLETE);
  1201. list_complete = true;
  1202. }
  1203. if (irqst0 | irqst1) {
  1204. dev_warn(dev->v4l2_dev.dev, "Unexpected interrupt: INT0_STATUS0 = 0x%08x, INT0_STATUS1 = 0x%08x\n",
  1205. irqst0, irqst1);
  1206. }
  1207. /*
  1208. * Setup next operation only when list complete IRQ occurs
  1209. * otherwise, skip the following code
  1210. */
  1211. if (!list_complete)
  1212. goto handled;
  1213. disable_irqs(ctx);
  1214. vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
  1215. vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
  1216. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
  1217. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
  1218. vpdma_reset_desc_list(&ctx->desc_list);
  1219. /* the previous dst mv buffer becomes the next src mv buffer */
  1220. ctx->src_mv_buf_selector = !ctx->src_mv_buf_selector;
  1221. s_vb = ctx->src_vbs[0];
  1222. d_vb = ctx->dst_vb;
  1223. d_vb->flags = s_vb->flags;
  1224. d_vb->vb2_buf.timestamp = s_vb->vb2_buf.timestamp;
  1225. if (s_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  1226. d_vb->timecode = s_vb->timecode;
  1227. d_vb->sequence = ctx->sequence;
  1228. s_vb->sequence = ctx->sequence;
  1229. d_q_data = &ctx->q_data[Q_DATA_DST];
  1230. if (d_q_data->flags & Q_IS_INTERLACED) {
  1231. d_vb->field = ctx->field;
  1232. if (ctx->field == V4L2_FIELD_BOTTOM) {
  1233. ctx->sequence++;
  1234. ctx->field = V4L2_FIELD_TOP;
  1235. } else {
  1236. WARN_ON(ctx->field != V4L2_FIELD_TOP);
  1237. ctx->field = V4L2_FIELD_BOTTOM;
  1238. }
  1239. } else {
  1240. d_vb->field = V4L2_FIELD_NONE;
  1241. ctx->sequence++;
  1242. }
  1243. if (ctx->deinterlacing) {
  1244. /*
  1245. * Allow source buffer to be dequeued only if it won't be used
  1246. * in the next iteration. All vbs are initialized to first
  1247. * buffer and we are shifting buffers every iteration, for the
  1248. * first two iterations, no buffer will be dequeued.
  1249. * This ensures that driver will keep (n-2)th (n-1)th and (n)th
  1250. * field when deinterlacing is enabled
  1251. */
  1252. if (ctx->src_vbs[2] != ctx->src_vbs[1])
  1253. s_vb = ctx->src_vbs[2];
  1254. else
  1255. s_vb = NULL;
  1256. }
  1257. spin_lock_irqsave(&dev->lock, flags);
  1258. if (s_vb)
  1259. v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_DONE);
  1260. v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_DONE);
  1261. spin_unlock_irqrestore(&dev->lock, flags);
  1262. if (ctx->deinterlacing) {
  1263. ctx->src_vbs[2] = ctx->src_vbs[1];
  1264. ctx->src_vbs[1] = ctx->src_vbs[0];
  1265. }
  1266. /*
  1267. * Since the vb2_buf_done has already been called fir therse
  1268. * buffer we can now NULL them out so that we won't try
  1269. * to clean out stray pointer later on.
  1270. */
  1271. ctx->src_vbs[0] = NULL;
  1272. ctx->dst_vb = NULL;
  1273. if (ctx->aborting)
  1274. goto finished;
  1275. ctx->bufs_completed++;
  1276. if (ctx->bufs_completed < ctx->bufs_per_job && job_ready(ctx)) {
  1277. device_run(ctx);
  1278. goto handled;
  1279. }
  1280. finished:
  1281. vpe_dbg(ctx->dev, "finishing transaction\n");
  1282. ctx->bufs_completed = 0;
  1283. v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  1284. handled:
  1285. return IRQ_HANDLED;
  1286. }
  1287. /*
  1288. * video ioctls
  1289. */
  1290. static int vpe_querycap(struct file *file, void *priv,
  1291. struct v4l2_capability *cap)
  1292. {
  1293. strncpy(cap->driver, VPE_MODULE_NAME, sizeof(cap->driver) - 1);
  1294. strncpy(cap->card, VPE_MODULE_NAME, sizeof(cap->card) - 1);
  1295. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  1296. VPE_MODULE_NAME);
  1297. cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1298. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1299. return 0;
  1300. }
  1301. static int __enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  1302. {
  1303. int i, index;
  1304. struct vpe_fmt *fmt = NULL;
  1305. index = 0;
  1306. for (i = 0; i < ARRAY_SIZE(vpe_formats); ++i) {
  1307. if (vpe_formats[i].types & type) {
  1308. if (index == f->index) {
  1309. fmt = &vpe_formats[i];
  1310. break;
  1311. }
  1312. index++;
  1313. }
  1314. }
  1315. if (!fmt)
  1316. return -EINVAL;
  1317. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  1318. f->pixelformat = fmt->fourcc;
  1319. return 0;
  1320. }
  1321. static int vpe_enum_fmt(struct file *file, void *priv,
  1322. struct v4l2_fmtdesc *f)
  1323. {
  1324. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1325. return __enum_fmt(f, VPE_FMT_TYPE_OUTPUT);
  1326. return __enum_fmt(f, VPE_FMT_TYPE_CAPTURE);
  1327. }
  1328. static int vpe_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1329. {
  1330. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1331. struct vpe_ctx *ctx = file2ctx(file);
  1332. struct vb2_queue *vq;
  1333. struct vpe_q_data *q_data;
  1334. int i;
  1335. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1336. if (!vq)
  1337. return -EINVAL;
  1338. q_data = get_q_data(ctx, f->type);
  1339. pix->width = q_data->width;
  1340. pix->height = q_data->height;
  1341. pix->pixelformat = q_data->fmt->fourcc;
  1342. pix->field = q_data->field;
  1343. if (V4L2_TYPE_IS_OUTPUT(f->type)) {
  1344. pix->colorspace = q_data->colorspace;
  1345. } else {
  1346. struct vpe_q_data *s_q_data;
  1347. /* get colorspace from the source queue */
  1348. s_q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  1349. pix->colorspace = s_q_data->colorspace;
  1350. }
  1351. pix->num_planes = q_data->nplanes;
  1352. for (i = 0; i < pix->num_planes; i++) {
  1353. pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
  1354. pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
  1355. }
  1356. return 0;
  1357. }
  1358. static int __vpe_try_fmt(struct vpe_ctx *ctx, struct v4l2_format *f,
  1359. struct vpe_fmt *fmt, int type)
  1360. {
  1361. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1362. struct v4l2_plane_pix_format *plane_fmt;
  1363. unsigned int w_align;
  1364. int i, depth, depth_bytes, height;
  1365. unsigned int stride = 0;
  1366. if (!fmt || !(fmt->types & type)) {
  1367. vpe_dbg(ctx->dev, "Fourcc format (0x%08x) invalid.\n",
  1368. pix->pixelformat);
  1369. fmt = __find_format(V4L2_PIX_FMT_YUYV);
  1370. }
  1371. if (pix->field != V4L2_FIELD_NONE && pix->field != V4L2_FIELD_ALTERNATE
  1372. && pix->field != V4L2_FIELD_SEQ_TB)
  1373. pix->field = V4L2_FIELD_NONE;
  1374. depth = fmt->vpdma_fmt[VPE_LUMA]->depth;
  1375. /*
  1376. * the line stride should 16 byte aligned for VPDMA to work, based on
  1377. * the bytes per pixel, figure out how much the width should be aligned
  1378. * to make sure line stride is 16 byte aligned
  1379. */
  1380. depth_bytes = depth >> 3;
  1381. if (depth_bytes == 3) {
  1382. /*
  1383. * if bpp is 3(as in some RGB formats), the pixel width doesn't
  1384. * really help in ensuring line stride is 16 byte aligned
  1385. */
  1386. w_align = 4;
  1387. } else {
  1388. /*
  1389. * for the remainder bpp(4, 2 and 1), the pixel width alignment
  1390. * can ensure a line stride alignment of 16 bytes. For example,
  1391. * if bpp is 2, then the line stride can be 16 byte aligned if
  1392. * the width is 8 byte aligned
  1393. */
  1394. /*
  1395. * HACK: using order_base_2() here causes lots of asm output
  1396. * errors with smatch, on i386:
  1397. * ./arch/x86/include/asm/bitops.h:457:22:
  1398. * warning: asm output is not an lvalue
  1399. * Perhaps some gcc optimization is doing the wrong thing
  1400. * there.
  1401. * Let's get rid of them by doing the calculus on two steps
  1402. */
  1403. w_align = roundup_pow_of_two(VPDMA_DESC_ALIGN / depth_bytes);
  1404. w_align = ilog2(w_align);
  1405. }
  1406. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, w_align,
  1407. &pix->height, MIN_H, MAX_H, H_ALIGN,
  1408. S_ALIGN);
  1409. if (!pix->num_planes || pix->num_planes > 2)
  1410. pix->num_planes = fmt->coplanar ? 2 : 1;
  1411. else if (pix->num_planes > 1 && !fmt->coplanar)
  1412. pix->num_planes = 1;
  1413. pix->pixelformat = fmt->fourcc;
  1414. /*
  1415. * For the actual image parameters, we need to consider the field
  1416. * height of the image for SEQ_TB buffers.
  1417. */
  1418. if (pix->field == V4L2_FIELD_SEQ_TB)
  1419. height = pix->height / 2;
  1420. else
  1421. height = pix->height;
  1422. if (!pix->colorspace) {
  1423. if (fmt->fourcc == V4L2_PIX_FMT_RGB24 ||
  1424. fmt->fourcc == V4L2_PIX_FMT_BGR24 ||
  1425. fmt->fourcc == V4L2_PIX_FMT_RGB32 ||
  1426. fmt->fourcc == V4L2_PIX_FMT_BGR32) {
  1427. pix->colorspace = V4L2_COLORSPACE_SRGB;
  1428. } else {
  1429. if (height > 1280) /* HD */
  1430. pix->colorspace = V4L2_COLORSPACE_REC709;
  1431. else /* SD */
  1432. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1433. }
  1434. }
  1435. memset(pix->reserved, 0, sizeof(pix->reserved));
  1436. for (i = 0; i < pix->num_planes; i++) {
  1437. plane_fmt = &pix->plane_fmt[i];
  1438. depth = fmt->vpdma_fmt[i]->depth;
  1439. stride = (pix->width * fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1440. if (stride > plane_fmt->bytesperline)
  1441. plane_fmt->bytesperline = stride;
  1442. plane_fmt->bytesperline = clamp_t(u32, plane_fmt->bytesperline,
  1443. stride,
  1444. VPDMA_MAX_STRIDE);
  1445. plane_fmt->bytesperline = ALIGN(plane_fmt->bytesperline,
  1446. VPDMA_STRIDE_ALIGN);
  1447. if (i == VPE_LUMA) {
  1448. plane_fmt->sizeimage = pix->height *
  1449. plane_fmt->bytesperline;
  1450. if (pix->num_planes == 1 && fmt->coplanar)
  1451. plane_fmt->sizeimage += pix->height *
  1452. plane_fmt->bytesperline *
  1453. fmt->vpdma_fmt[VPE_CHROMA]->depth >> 3;
  1454. } else { /* i == VIP_CHROMA */
  1455. plane_fmt->sizeimage = (pix->height *
  1456. plane_fmt->bytesperline *
  1457. depth) >> 3;
  1458. }
  1459. memset(plane_fmt->reserved, 0, sizeof(plane_fmt->reserved));
  1460. }
  1461. return 0;
  1462. }
  1463. static int vpe_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1464. {
  1465. struct vpe_ctx *ctx = file2ctx(file);
  1466. struct vpe_fmt *fmt = find_format(f);
  1467. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1468. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_OUTPUT);
  1469. else
  1470. return __vpe_try_fmt(ctx, f, fmt, VPE_FMT_TYPE_CAPTURE);
  1471. }
  1472. static int __vpe_s_fmt(struct vpe_ctx *ctx, struct v4l2_format *f)
  1473. {
  1474. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  1475. struct v4l2_plane_pix_format *plane_fmt;
  1476. struct vpe_q_data *q_data;
  1477. struct vb2_queue *vq;
  1478. int i;
  1479. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1480. if (!vq)
  1481. return -EINVAL;
  1482. if (vb2_is_busy(vq)) {
  1483. vpe_err(ctx->dev, "queue busy\n");
  1484. return -EBUSY;
  1485. }
  1486. q_data = get_q_data(ctx, f->type);
  1487. if (!q_data)
  1488. return -EINVAL;
  1489. q_data->fmt = find_format(f);
  1490. q_data->width = pix->width;
  1491. q_data->height = pix->height;
  1492. q_data->colorspace = pix->colorspace;
  1493. q_data->field = pix->field;
  1494. q_data->nplanes = pix->num_planes;
  1495. for (i = 0; i < pix->num_planes; i++) {
  1496. plane_fmt = &pix->plane_fmt[i];
  1497. q_data->bytesperline[i] = plane_fmt->bytesperline;
  1498. q_data->sizeimage[i] = plane_fmt->sizeimage;
  1499. }
  1500. q_data->c_rect.left = 0;
  1501. q_data->c_rect.top = 0;
  1502. q_data->c_rect.width = q_data->width;
  1503. q_data->c_rect.height = q_data->height;
  1504. if (q_data->field == V4L2_FIELD_ALTERNATE)
  1505. q_data->flags |= Q_DATA_INTERLACED_ALTERNATE;
  1506. else if (q_data->field == V4L2_FIELD_SEQ_TB)
  1507. q_data->flags |= Q_DATA_INTERLACED_SEQ_TB;
  1508. else
  1509. q_data->flags &= ~Q_IS_INTERLACED;
  1510. /* the crop height is halved for the case of SEQ_TB buffers */
  1511. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
  1512. q_data->c_rect.height /= 2;
  1513. vpe_dbg(ctx->dev, "Setting format for type %d, wxh: %dx%d, fmt: %d bpl_y %d",
  1514. f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
  1515. q_data->bytesperline[VPE_LUMA]);
  1516. if (q_data->nplanes == 2)
  1517. vpe_dbg(ctx->dev, " bpl_uv %d\n",
  1518. q_data->bytesperline[VPE_CHROMA]);
  1519. return 0;
  1520. }
  1521. static int vpe_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  1522. {
  1523. int ret;
  1524. struct vpe_ctx *ctx = file2ctx(file);
  1525. ret = vpe_try_fmt(file, priv, f);
  1526. if (ret)
  1527. return ret;
  1528. ret = __vpe_s_fmt(ctx, f);
  1529. if (ret)
  1530. return ret;
  1531. if (V4L2_TYPE_IS_OUTPUT(f->type))
  1532. set_src_registers(ctx);
  1533. else
  1534. set_dst_registers(ctx);
  1535. return set_srcdst_params(ctx);
  1536. }
  1537. static int __vpe_try_selection(struct vpe_ctx *ctx, struct v4l2_selection *s)
  1538. {
  1539. struct vpe_q_data *q_data;
  1540. int height;
  1541. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1542. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1543. return -EINVAL;
  1544. q_data = get_q_data(ctx, s->type);
  1545. if (!q_data)
  1546. return -EINVAL;
  1547. switch (s->target) {
  1548. case V4L2_SEL_TGT_COMPOSE:
  1549. /*
  1550. * COMPOSE target is only valid for capture buffer type, return
  1551. * error for output buffer type
  1552. */
  1553. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1554. return -EINVAL;
  1555. break;
  1556. case V4L2_SEL_TGT_CROP:
  1557. /*
  1558. * CROP target is only valid for output buffer type, return
  1559. * error for capture buffer type
  1560. */
  1561. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1562. return -EINVAL;
  1563. break;
  1564. /*
  1565. * bound and default crop/compose targets are invalid targets to
  1566. * try/set
  1567. */
  1568. default:
  1569. return -EINVAL;
  1570. }
  1571. /*
  1572. * For SEQ_TB buffers, crop height should be less than the height of
  1573. * the field height, not the buffer height
  1574. */
  1575. if (q_data->flags & Q_DATA_INTERLACED_SEQ_TB)
  1576. height = q_data->height / 2;
  1577. else
  1578. height = q_data->height;
  1579. if (s->r.top < 0 || s->r.left < 0) {
  1580. vpe_err(ctx->dev, "negative values for top and left\n");
  1581. s->r.top = s->r.left = 0;
  1582. }
  1583. v4l_bound_align_image(&s->r.width, MIN_W, q_data->width, 1,
  1584. &s->r.height, MIN_H, height, H_ALIGN, S_ALIGN);
  1585. /* adjust left/top if cropping rectangle is out of bounds */
  1586. if (s->r.left + s->r.width > q_data->width)
  1587. s->r.left = q_data->width - s->r.width;
  1588. if (s->r.top + s->r.height > q_data->height)
  1589. s->r.top = q_data->height - s->r.height;
  1590. return 0;
  1591. }
  1592. static int vpe_g_selection(struct file *file, void *fh,
  1593. struct v4l2_selection *s)
  1594. {
  1595. struct vpe_ctx *ctx = file2ctx(file);
  1596. struct vpe_q_data *q_data;
  1597. bool use_c_rect = false;
  1598. if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1599. (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
  1600. return -EINVAL;
  1601. q_data = get_q_data(ctx, s->type);
  1602. if (!q_data)
  1603. return -EINVAL;
  1604. switch (s->target) {
  1605. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1606. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1607. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1608. return -EINVAL;
  1609. break;
  1610. case V4L2_SEL_TGT_CROP_BOUNDS:
  1611. case V4L2_SEL_TGT_CROP_DEFAULT:
  1612. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1613. return -EINVAL;
  1614. break;
  1615. case V4L2_SEL_TGT_COMPOSE:
  1616. if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1617. return -EINVAL;
  1618. use_c_rect = true;
  1619. break;
  1620. case V4L2_SEL_TGT_CROP:
  1621. if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1622. return -EINVAL;
  1623. use_c_rect = true;
  1624. break;
  1625. default:
  1626. return -EINVAL;
  1627. }
  1628. if (use_c_rect) {
  1629. /*
  1630. * for CROP/COMPOSE target type, return c_rect params from the
  1631. * respective buffer type
  1632. */
  1633. s->r = q_data->c_rect;
  1634. } else {
  1635. /*
  1636. * for DEFAULT/BOUNDS target type, return width and height from
  1637. * S_FMT of the respective buffer type
  1638. */
  1639. s->r.left = 0;
  1640. s->r.top = 0;
  1641. s->r.width = q_data->width;
  1642. s->r.height = q_data->height;
  1643. }
  1644. return 0;
  1645. }
  1646. static int vpe_s_selection(struct file *file, void *fh,
  1647. struct v4l2_selection *s)
  1648. {
  1649. struct vpe_ctx *ctx = file2ctx(file);
  1650. struct vpe_q_data *q_data;
  1651. struct v4l2_selection sel = *s;
  1652. int ret;
  1653. ret = __vpe_try_selection(ctx, &sel);
  1654. if (ret)
  1655. return ret;
  1656. q_data = get_q_data(ctx, sel.type);
  1657. if (!q_data)
  1658. return -EINVAL;
  1659. if ((q_data->c_rect.left == sel.r.left) &&
  1660. (q_data->c_rect.top == sel.r.top) &&
  1661. (q_data->c_rect.width == sel.r.width) &&
  1662. (q_data->c_rect.height == sel.r.height)) {
  1663. vpe_dbg(ctx->dev,
  1664. "requested crop/compose values are already set\n");
  1665. return 0;
  1666. }
  1667. q_data->c_rect = sel.r;
  1668. return set_srcdst_params(ctx);
  1669. }
  1670. /*
  1671. * defines number of buffers/frames a context can process with VPE before
  1672. * switching to a different context. default value is 1 buffer per context
  1673. */
  1674. #define V4L2_CID_VPE_BUFS_PER_JOB (V4L2_CID_USER_TI_VPE_BASE + 0)
  1675. static int vpe_s_ctrl(struct v4l2_ctrl *ctrl)
  1676. {
  1677. struct vpe_ctx *ctx =
  1678. container_of(ctrl->handler, struct vpe_ctx, hdl);
  1679. switch (ctrl->id) {
  1680. case V4L2_CID_VPE_BUFS_PER_JOB:
  1681. ctx->bufs_per_job = ctrl->val;
  1682. break;
  1683. default:
  1684. vpe_err(ctx->dev, "Invalid control\n");
  1685. return -EINVAL;
  1686. }
  1687. return 0;
  1688. }
  1689. static const struct v4l2_ctrl_ops vpe_ctrl_ops = {
  1690. .s_ctrl = vpe_s_ctrl,
  1691. };
  1692. static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
  1693. .vidioc_querycap = vpe_querycap,
  1694. .vidioc_enum_fmt_vid_cap_mplane = vpe_enum_fmt,
  1695. .vidioc_g_fmt_vid_cap_mplane = vpe_g_fmt,
  1696. .vidioc_try_fmt_vid_cap_mplane = vpe_try_fmt,
  1697. .vidioc_s_fmt_vid_cap_mplane = vpe_s_fmt,
  1698. .vidioc_enum_fmt_vid_out_mplane = vpe_enum_fmt,
  1699. .vidioc_g_fmt_vid_out_mplane = vpe_g_fmt,
  1700. .vidioc_try_fmt_vid_out_mplane = vpe_try_fmt,
  1701. .vidioc_s_fmt_vid_out_mplane = vpe_s_fmt,
  1702. .vidioc_g_selection = vpe_g_selection,
  1703. .vidioc_s_selection = vpe_s_selection,
  1704. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1705. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1706. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1707. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1708. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1709. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1710. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1711. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1712. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1713. };
  1714. /*
  1715. * Queue operations
  1716. */
  1717. static int vpe_queue_setup(struct vb2_queue *vq,
  1718. unsigned int *nbuffers, unsigned int *nplanes,
  1719. unsigned int sizes[], struct device *alloc_devs[])
  1720. {
  1721. int i;
  1722. struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
  1723. struct vpe_q_data *q_data;
  1724. q_data = get_q_data(ctx, vq->type);
  1725. *nplanes = q_data->nplanes;
  1726. for (i = 0; i < *nplanes; i++)
  1727. sizes[i] = q_data->sizeimage[i];
  1728. vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
  1729. sizes[VPE_LUMA]);
  1730. if (q_data->nplanes == 2)
  1731. vpe_dbg(ctx->dev, " and %d\n", sizes[VPE_CHROMA]);
  1732. return 0;
  1733. }
  1734. static int vpe_buf_prepare(struct vb2_buffer *vb)
  1735. {
  1736. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1737. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1738. struct vpe_q_data *q_data;
  1739. int i, num_planes;
  1740. vpe_dbg(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1741. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1742. num_planes = q_data->nplanes;
  1743. if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1744. if (!(q_data->flags & Q_IS_INTERLACED)) {
  1745. vbuf->field = V4L2_FIELD_NONE;
  1746. } else {
  1747. if (vbuf->field != V4L2_FIELD_TOP &&
  1748. vbuf->field != V4L2_FIELD_BOTTOM &&
  1749. vbuf->field != V4L2_FIELD_SEQ_TB)
  1750. return -EINVAL;
  1751. }
  1752. }
  1753. for (i = 0; i < num_planes; i++) {
  1754. if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
  1755. vpe_err(ctx->dev,
  1756. "data will not fit into plane (%lu < %lu)\n",
  1757. vb2_plane_size(vb, i),
  1758. (long) q_data->sizeimage[i]);
  1759. return -EINVAL;
  1760. }
  1761. }
  1762. for (i = 0; i < num_planes; i++)
  1763. vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
  1764. return 0;
  1765. }
  1766. static void vpe_buf_queue(struct vb2_buffer *vb)
  1767. {
  1768. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1769. struct vpe_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1770. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1771. }
  1772. static int check_srcdst_sizes(struct vpe_ctx *ctx)
  1773. {
  1774. struct vpe_q_data *s_q_data = &ctx->q_data[Q_DATA_SRC];
  1775. struct vpe_q_data *d_q_data = &ctx->q_data[Q_DATA_DST];
  1776. unsigned int src_w = s_q_data->c_rect.width;
  1777. unsigned int src_h = s_q_data->c_rect.height;
  1778. unsigned int dst_w = d_q_data->c_rect.width;
  1779. unsigned int dst_h = d_q_data->c_rect.height;
  1780. if (src_w == dst_w && src_h == dst_h)
  1781. return 0;
  1782. if (src_h <= SC_MAX_PIXEL_HEIGHT &&
  1783. src_w <= SC_MAX_PIXEL_WIDTH &&
  1784. dst_h <= SC_MAX_PIXEL_HEIGHT &&
  1785. dst_w <= SC_MAX_PIXEL_WIDTH)
  1786. return 0;
  1787. return -1;
  1788. }
  1789. static void vpe_return_all_buffers(struct vpe_ctx *ctx, struct vb2_queue *q,
  1790. enum vb2_buffer_state state)
  1791. {
  1792. struct vb2_v4l2_buffer *vb;
  1793. unsigned long flags;
  1794. for (;;) {
  1795. if (V4L2_TYPE_IS_OUTPUT(q->type))
  1796. vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1797. else
  1798. vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1799. if (!vb)
  1800. break;
  1801. spin_lock_irqsave(&ctx->dev->lock, flags);
  1802. v4l2_m2m_buf_done(vb, state);
  1803. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1804. }
  1805. /*
  1806. * Cleanup the in-transit vb2 buffers that have been
  1807. * removed from their respective queue already but for
  1808. * which procecessing has not been completed yet.
  1809. */
  1810. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1811. spin_lock_irqsave(&ctx->dev->lock, flags);
  1812. if (ctx->src_vbs[2])
  1813. v4l2_m2m_buf_done(ctx->src_vbs[2], state);
  1814. if (ctx->src_vbs[1] && (ctx->src_vbs[1] != ctx->src_vbs[2]))
  1815. v4l2_m2m_buf_done(ctx->src_vbs[1], state);
  1816. if (ctx->src_vbs[0] &&
  1817. (ctx->src_vbs[0] != ctx->src_vbs[1]) &&
  1818. (ctx->src_vbs[0] != ctx->src_vbs[2]))
  1819. v4l2_m2m_buf_done(ctx->src_vbs[0], state);
  1820. ctx->src_vbs[2] = NULL;
  1821. ctx->src_vbs[1] = NULL;
  1822. ctx->src_vbs[0] = NULL;
  1823. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1824. } else {
  1825. if (ctx->dst_vb) {
  1826. spin_lock_irqsave(&ctx->dev->lock, flags);
  1827. v4l2_m2m_buf_done(ctx->dst_vb, state);
  1828. ctx->dst_vb = NULL;
  1829. spin_unlock_irqrestore(&ctx->dev->lock, flags);
  1830. }
  1831. }
  1832. }
  1833. static int vpe_start_streaming(struct vb2_queue *q, unsigned int count)
  1834. {
  1835. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1836. /* Check any of the size exceed maximum scaling sizes */
  1837. if (check_srcdst_sizes(ctx)) {
  1838. vpe_err(ctx->dev,
  1839. "Conversion setup failed, check source and destination parameters\n"
  1840. );
  1841. vpe_return_all_buffers(ctx, q, VB2_BUF_STATE_QUEUED);
  1842. return -EINVAL;
  1843. }
  1844. if (ctx->deinterlacing)
  1845. config_edi_input_mode(ctx, 0x0);
  1846. if (ctx->sequence != 0)
  1847. set_srcdst_params(ctx);
  1848. return 0;
  1849. }
  1850. static void vpe_stop_streaming(struct vb2_queue *q)
  1851. {
  1852. struct vpe_ctx *ctx = vb2_get_drv_priv(q);
  1853. vpe_dump_regs(ctx->dev);
  1854. vpdma_dump_regs(ctx->dev->vpdma);
  1855. vpe_return_all_buffers(ctx, q, VB2_BUF_STATE_ERROR);
  1856. }
  1857. static const struct vb2_ops vpe_qops = {
  1858. .queue_setup = vpe_queue_setup,
  1859. .buf_prepare = vpe_buf_prepare,
  1860. .buf_queue = vpe_buf_queue,
  1861. .wait_prepare = vb2_ops_wait_prepare,
  1862. .wait_finish = vb2_ops_wait_finish,
  1863. .start_streaming = vpe_start_streaming,
  1864. .stop_streaming = vpe_stop_streaming,
  1865. };
  1866. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1867. struct vb2_queue *dst_vq)
  1868. {
  1869. struct vpe_ctx *ctx = priv;
  1870. struct vpe_dev *dev = ctx->dev;
  1871. int ret;
  1872. memset(src_vq, 0, sizeof(*src_vq));
  1873. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1874. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1875. src_vq->drv_priv = ctx;
  1876. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1877. src_vq->ops = &vpe_qops;
  1878. src_vq->mem_ops = &vb2_dma_contig_memops;
  1879. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1880. src_vq->lock = &dev->dev_mutex;
  1881. src_vq->dev = dev->v4l2_dev.dev;
  1882. ret = vb2_queue_init(src_vq);
  1883. if (ret)
  1884. return ret;
  1885. memset(dst_vq, 0, sizeof(*dst_vq));
  1886. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1887. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1888. dst_vq->drv_priv = ctx;
  1889. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1890. dst_vq->ops = &vpe_qops;
  1891. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1892. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1893. dst_vq->lock = &dev->dev_mutex;
  1894. dst_vq->dev = dev->v4l2_dev.dev;
  1895. return vb2_queue_init(dst_vq);
  1896. }
  1897. static const struct v4l2_ctrl_config vpe_bufs_per_job = {
  1898. .ops = &vpe_ctrl_ops,
  1899. .id = V4L2_CID_VPE_BUFS_PER_JOB,
  1900. .name = "Buffers Per Transaction",
  1901. .type = V4L2_CTRL_TYPE_INTEGER,
  1902. .def = VPE_DEF_BUFS_PER_JOB,
  1903. .min = 1,
  1904. .max = VIDEO_MAX_FRAME,
  1905. .step = 1,
  1906. };
  1907. /*
  1908. * File operations
  1909. */
  1910. static int vpe_open(struct file *file)
  1911. {
  1912. struct vpe_dev *dev = video_drvdata(file);
  1913. struct vpe_q_data *s_q_data;
  1914. struct v4l2_ctrl_handler *hdl;
  1915. struct vpe_ctx *ctx;
  1916. int ret;
  1917. vpe_dbg(dev, "vpe_open\n");
  1918. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1919. if (!ctx)
  1920. return -ENOMEM;
  1921. ctx->dev = dev;
  1922. if (mutex_lock_interruptible(&dev->dev_mutex)) {
  1923. ret = -ERESTARTSYS;
  1924. goto free_ctx;
  1925. }
  1926. ret = vpdma_create_desc_list(&ctx->desc_list, VPE_DESC_LIST_SIZE,
  1927. VPDMA_LIST_TYPE_NORMAL);
  1928. if (ret != 0)
  1929. goto unlock;
  1930. ret = vpdma_alloc_desc_buf(&ctx->mmr_adb, sizeof(struct vpe_mmr_adb));
  1931. if (ret != 0)
  1932. goto free_desc_list;
  1933. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_h, SC_COEF_SRAM_SIZE);
  1934. if (ret != 0)
  1935. goto free_mmr_adb;
  1936. ret = vpdma_alloc_desc_buf(&ctx->sc_coeff_v, SC_COEF_SRAM_SIZE);
  1937. if (ret != 0)
  1938. goto free_sc_h;
  1939. init_adb_hdrs(ctx);
  1940. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1941. file->private_data = &ctx->fh;
  1942. hdl = &ctx->hdl;
  1943. v4l2_ctrl_handler_init(hdl, 1);
  1944. v4l2_ctrl_new_custom(hdl, &vpe_bufs_per_job, NULL);
  1945. if (hdl->error) {
  1946. ret = hdl->error;
  1947. goto exit_fh;
  1948. }
  1949. ctx->fh.ctrl_handler = hdl;
  1950. v4l2_ctrl_handler_setup(hdl);
  1951. s_q_data = &ctx->q_data[Q_DATA_SRC];
  1952. s_q_data->fmt = __find_format(V4L2_PIX_FMT_YUYV);
  1953. s_q_data->width = 1920;
  1954. s_q_data->height = 1080;
  1955. s_q_data->nplanes = 1;
  1956. s_q_data->bytesperline[VPE_LUMA] = (s_q_data->width *
  1957. s_q_data->fmt->vpdma_fmt[VPE_LUMA]->depth) >> 3;
  1958. s_q_data->sizeimage[VPE_LUMA] = (s_q_data->bytesperline[VPE_LUMA] *
  1959. s_q_data->height);
  1960. s_q_data->colorspace = V4L2_COLORSPACE_REC709;
  1961. s_q_data->field = V4L2_FIELD_NONE;
  1962. s_q_data->c_rect.left = 0;
  1963. s_q_data->c_rect.top = 0;
  1964. s_q_data->c_rect.width = s_q_data->width;
  1965. s_q_data->c_rect.height = s_q_data->height;
  1966. s_q_data->flags = 0;
  1967. ctx->q_data[Q_DATA_DST] = *s_q_data;
  1968. set_dei_shadow_registers(ctx);
  1969. set_src_registers(ctx);
  1970. set_dst_registers(ctx);
  1971. ret = set_srcdst_params(ctx);
  1972. if (ret)
  1973. goto exit_fh;
  1974. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1975. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1976. ret = PTR_ERR(ctx->fh.m2m_ctx);
  1977. goto exit_fh;
  1978. }
  1979. v4l2_fh_add(&ctx->fh);
  1980. /*
  1981. * for now, just report the creation of the first instance, we can later
  1982. * optimize the driver to enable or disable clocks when the first
  1983. * instance is created or the last instance released
  1984. */
  1985. if (atomic_inc_return(&dev->num_instances) == 1)
  1986. vpe_dbg(dev, "first instance created\n");
  1987. ctx->bufs_per_job = VPE_DEF_BUFS_PER_JOB;
  1988. ctx->load_mmrs = true;
  1989. vpe_dbg(dev, "created instance %p, m2m_ctx: %p\n",
  1990. ctx, ctx->fh.m2m_ctx);
  1991. mutex_unlock(&dev->dev_mutex);
  1992. return 0;
  1993. exit_fh:
  1994. v4l2_ctrl_handler_free(hdl);
  1995. v4l2_fh_exit(&ctx->fh);
  1996. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  1997. free_sc_h:
  1998. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  1999. free_mmr_adb:
  2000. vpdma_free_desc_buf(&ctx->mmr_adb);
  2001. free_desc_list:
  2002. vpdma_free_desc_list(&ctx->desc_list);
  2003. unlock:
  2004. mutex_unlock(&dev->dev_mutex);
  2005. free_ctx:
  2006. kfree(ctx);
  2007. return ret;
  2008. }
  2009. static int vpe_release(struct file *file)
  2010. {
  2011. struct vpe_dev *dev = video_drvdata(file);
  2012. struct vpe_ctx *ctx = file2ctx(file);
  2013. vpe_dbg(dev, "releasing instance %p\n", ctx);
  2014. mutex_lock(&dev->dev_mutex);
  2015. free_mv_buffers(ctx);
  2016. vpdma_unmap_desc_buf(dev->vpdma, &ctx->desc_list.buf);
  2017. vpdma_unmap_desc_buf(dev->vpdma, &ctx->mmr_adb);
  2018. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_h);
  2019. vpdma_unmap_desc_buf(dev->vpdma, &ctx->sc_coeff_v);
  2020. vpdma_free_desc_list(&ctx->desc_list);
  2021. vpdma_free_desc_buf(&ctx->mmr_adb);
  2022. vpdma_free_desc_buf(&ctx->sc_coeff_v);
  2023. vpdma_free_desc_buf(&ctx->sc_coeff_h);
  2024. v4l2_fh_del(&ctx->fh);
  2025. v4l2_fh_exit(&ctx->fh);
  2026. v4l2_ctrl_handler_free(&ctx->hdl);
  2027. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  2028. kfree(ctx);
  2029. /*
  2030. * for now, just report the release of the last instance, we can later
  2031. * optimize the driver to enable or disable clocks when the first
  2032. * instance is created or the last instance released
  2033. */
  2034. if (atomic_dec_return(&dev->num_instances) == 0)
  2035. vpe_dbg(dev, "last instance released\n");
  2036. mutex_unlock(&dev->dev_mutex);
  2037. return 0;
  2038. }
  2039. static const struct v4l2_file_operations vpe_fops = {
  2040. .owner = THIS_MODULE,
  2041. .open = vpe_open,
  2042. .release = vpe_release,
  2043. .poll = v4l2_m2m_fop_poll,
  2044. .unlocked_ioctl = video_ioctl2,
  2045. .mmap = v4l2_m2m_fop_mmap,
  2046. };
  2047. static const struct video_device vpe_videodev = {
  2048. .name = VPE_MODULE_NAME,
  2049. .fops = &vpe_fops,
  2050. .ioctl_ops = &vpe_ioctl_ops,
  2051. .minor = -1,
  2052. .release = video_device_release_empty,
  2053. .vfl_dir = VFL_DIR_M2M,
  2054. };
  2055. static const struct v4l2_m2m_ops m2m_ops = {
  2056. .device_run = device_run,
  2057. .job_ready = job_ready,
  2058. .job_abort = job_abort,
  2059. };
  2060. static int vpe_runtime_get(struct platform_device *pdev)
  2061. {
  2062. int r;
  2063. dev_dbg(&pdev->dev, "vpe_runtime_get\n");
  2064. r = pm_runtime_get_sync(&pdev->dev);
  2065. WARN_ON(r < 0);
  2066. return r < 0 ? r : 0;
  2067. }
  2068. static void vpe_runtime_put(struct platform_device *pdev)
  2069. {
  2070. int r;
  2071. dev_dbg(&pdev->dev, "vpe_runtime_put\n");
  2072. r = pm_runtime_put_sync(&pdev->dev);
  2073. WARN_ON(r < 0 && r != -ENOSYS);
  2074. }
  2075. static void vpe_fw_cb(struct platform_device *pdev)
  2076. {
  2077. struct vpe_dev *dev = platform_get_drvdata(pdev);
  2078. struct video_device *vfd;
  2079. int ret;
  2080. vfd = &dev->vfd;
  2081. *vfd = vpe_videodev;
  2082. vfd->lock = &dev->dev_mutex;
  2083. vfd->v4l2_dev = &dev->v4l2_dev;
  2084. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  2085. if (ret) {
  2086. vpe_err(dev, "Failed to register video device\n");
  2087. vpe_set_clock_enable(dev, 0);
  2088. vpe_runtime_put(pdev);
  2089. pm_runtime_disable(&pdev->dev);
  2090. v4l2_m2m_release(dev->m2m_dev);
  2091. v4l2_device_unregister(&dev->v4l2_dev);
  2092. return;
  2093. }
  2094. video_set_drvdata(vfd, dev);
  2095. dev_info(dev->v4l2_dev.dev, "Device registered as /dev/video%d\n",
  2096. vfd->num);
  2097. }
  2098. static int vpe_probe(struct platform_device *pdev)
  2099. {
  2100. struct vpe_dev *dev;
  2101. int ret, irq, func;
  2102. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  2103. if (!dev)
  2104. return -ENOMEM;
  2105. spin_lock_init(&dev->lock);
  2106. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  2107. if (ret)
  2108. return ret;
  2109. atomic_set(&dev->num_instances, 0);
  2110. mutex_init(&dev->dev_mutex);
  2111. dev->res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2112. "vpe_top");
  2113. /*
  2114. * HACK: we get resource info from device tree in the form of a list of
  2115. * VPE sub blocks, the driver currently uses only the base of vpe_top
  2116. * for register access, the driver should be changed later to access
  2117. * registers based on the sub block base addresses
  2118. */
  2119. dev->base = devm_ioremap(&pdev->dev, dev->res->start, SZ_32K);
  2120. if (!dev->base) {
  2121. ret = -ENOMEM;
  2122. goto v4l2_dev_unreg;
  2123. }
  2124. irq = platform_get_irq(pdev, 0);
  2125. ret = devm_request_irq(&pdev->dev, irq, vpe_irq, 0, VPE_MODULE_NAME,
  2126. dev);
  2127. if (ret)
  2128. goto v4l2_dev_unreg;
  2129. platform_set_drvdata(pdev, dev);
  2130. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  2131. if (IS_ERR(dev->m2m_dev)) {
  2132. vpe_err(dev, "Failed to init mem2mem device\n");
  2133. ret = PTR_ERR(dev->m2m_dev);
  2134. goto v4l2_dev_unreg;
  2135. }
  2136. pm_runtime_enable(&pdev->dev);
  2137. ret = vpe_runtime_get(pdev);
  2138. if (ret)
  2139. goto rel_m2m;
  2140. /* Perform clk enable followed by reset */
  2141. vpe_set_clock_enable(dev, 1);
  2142. vpe_top_reset(dev);
  2143. func = read_field_reg(dev, VPE_PID, VPE_PID_FUNC_MASK,
  2144. VPE_PID_FUNC_SHIFT);
  2145. vpe_dbg(dev, "VPE PID function %x\n", func);
  2146. vpe_top_vpdma_reset(dev);
  2147. dev->sc = sc_create(pdev, "sc");
  2148. if (IS_ERR(dev->sc)) {
  2149. ret = PTR_ERR(dev->sc);
  2150. goto runtime_put;
  2151. }
  2152. dev->csc = csc_create(pdev, "csc");
  2153. if (IS_ERR(dev->csc)) {
  2154. ret = PTR_ERR(dev->csc);
  2155. goto runtime_put;
  2156. }
  2157. dev->vpdma = &dev->vpdma_data;
  2158. ret = vpdma_create(pdev, dev->vpdma, vpe_fw_cb);
  2159. if (ret)
  2160. goto runtime_put;
  2161. return 0;
  2162. runtime_put:
  2163. vpe_runtime_put(pdev);
  2164. rel_m2m:
  2165. pm_runtime_disable(&pdev->dev);
  2166. v4l2_m2m_release(dev->m2m_dev);
  2167. v4l2_dev_unreg:
  2168. v4l2_device_unregister(&dev->v4l2_dev);
  2169. return ret;
  2170. }
  2171. static int vpe_remove(struct platform_device *pdev)
  2172. {
  2173. struct vpe_dev *dev = platform_get_drvdata(pdev);
  2174. v4l2_info(&dev->v4l2_dev, "Removing " VPE_MODULE_NAME);
  2175. v4l2_m2m_release(dev->m2m_dev);
  2176. video_unregister_device(&dev->vfd);
  2177. v4l2_device_unregister(&dev->v4l2_dev);
  2178. vpe_set_clock_enable(dev, 0);
  2179. vpe_runtime_put(pdev);
  2180. pm_runtime_disable(&pdev->dev);
  2181. return 0;
  2182. }
  2183. #if defined(CONFIG_OF)
  2184. static const struct of_device_id vpe_of_match[] = {
  2185. {
  2186. .compatible = "ti,vpe",
  2187. },
  2188. {},
  2189. };
  2190. MODULE_DEVICE_TABLE(of, vpe_of_match);
  2191. #endif
  2192. static struct platform_driver vpe_pdrv = {
  2193. .probe = vpe_probe,
  2194. .remove = vpe_remove,
  2195. .driver = {
  2196. .name = VPE_MODULE_NAME,
  2197. .of_match_table = of_match_ptr(vpe_of_match),
  2198. },
  2199. };
  2200. module_platform_driver(vpe_pdrv);
  2201. MODULE_DESCRIPTION("TI VPE driver");
  2202. MODULE_AUTHOR("Dale Farnsworth, <dale@farnsworth.org>");
  2203. MODULE_LICENSE("GPL");