exynos_drm_gsc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Eunchul Kim <chulspro.kim@samsung.com>
  5. * Jinyoung Jeon <jy0.jeon@samsung.com>
  6. * Sangmin Lee <lsmin.lee@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/component.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/of_device.h>
  21. #include <linux/regmap.h>
  22. #include <drm/drmP.h>
  23. #include <drm/exynos_drm.h>
  24. #include "regs-gsc.h"
  25. #include "exynos_drm_drv.h"
  26. #include "exynos_drm_iommu.h"
  27. #include "exynos_drm_ipp.h"
  28. /*
  29. * GSC stands for General SCaler and
  30. * supports image scaler/rotator and input/output DMA operations.
  31. * input DMA reads image data from the memory.
  32. * output DMA writes image data to memory.
  33. * GSC supports image rotation and image effect functions.
  34. */
  35. #define GSC_MAX_CLOCKS 8
  36. #define GSC_MAX_SRC 4
  37. #define GSC_MAX_DST 16
  38. #define GSC_RESET_TIMEOUT 50
  39. #define GSC_BUF_STOP 1
  40. #define GSC_BUF_START 2
  41. #define GSC_REG_SZ 16
  42. #define GSC_WIDTH_ITU_709 1280
  43. #define GSC_SC_UP_MAX_RATIO 65536
  44. #define GSC_SC_DOWN_RATIO_7_8 74898
  45. #define GSC_SC_DOWN_RATIO_6_8 87381
  46. #define GSC_SC_DOWN_RATIO_5_8 104857
  47. #define GSC_SC_DOWN_RATIO_4_8 131072
  48. #define GSC_SC_DOWN_RATIO_3_8 174762
  49. #define GSC_SC_DOWN_RATIO_2_8 262144
  50. #define GSC_CROP_MAX 8192
  51. #define GSC_CROP_MIN 32
  52. #define GSC_SCALE_MAX 4224
  53. #define GSC_SCALE_MIN 32
  54. #define GSC_COEF_RATIO 7
  55. #define GSC_COEF_PHASE 9
  56. #define GSC_COEF_ATTR 16
  57. #define GSC_COEF_H_8T 8
  58. #define GSC_COEF_V_4T 4
  59. #define GSC_COEF_DEPTH 3
  60. #define GSC_AUTOSUSPEND_DELAY 2000
  61. #define get_gsc_context(dev) platform_get_drvdata(to_platform_device(dev))
  62. #define gsc_read(offset) readl(ctx->regs + (offset))
  63. #define gsc_write(cfg, offset) writel(cfg, ctx->regs + (offset))
  64. /*
  65. * A structure of scaler.
  66. *
  67. * @range: narrow, wide.
  68. * @pre_shfactor: pre sclaer shift factor.
  69. * @pre_hratio: horizontal ratio of the prescaler.
  70. * @pre_vratio: vertical ratio of the prescaler.
  71. * @main_hratio: the main scaler's horizontal ratio.
  72. * @main_vratio: the main scaler's vertical ratio.
  73. */
  74. struct gsc_scaler {
  75. bool range;
  76. u32 pre_shfactor;
  77. u32 pre_hratio;
  78. u32 pre_vratio;
  79. unsigned long main_hratio;
  80. unsigned long main_vratio;
  81. };
  82. /*
  83. * A structure of gsc context.
  84. *
  85. * @regs_res: register resources.
  86. * @regs: memory mapped io registers.
  87. * @gsc_clk: gsc gate clock.
  88. * @sc: scaler infomations.
  89. * @id: gsc id.
  90. * @irq: irq number.
  91. * @rotation: supports rotation of src.
  92. */
  93. struct gsc_context {
  94. struct exynos_drm_ipp ipp;
  95. struct drm_device *drm_dev;
  96. struct device *dev;
  97. struct exynos_drm_ipp_task *task;
  98. struct exynos_drm_ipp_formats *formats;
  99. unsigned int num_formats;
  100. struct resource *regs_res;
  101. void __iomem *regs;
  102. const char **clk_names;
  103. struct clk *clocks[GSC_MAX_CLOCKS];
  104. int num_clocks;
  105. struct gsc_scaler sc;
  106. int id;
  107. int irq;
  108. bool rotation;
  109. };
  110. /**
  111. * struct gsc_driverdata - per device type driver data for init time.
  112. *
  113. * @limits: picture size limits array
  114. * @clk_names: names of clocks needed by this variant
  115. * @num_clocks: the number of clocks needed by this variant
  116. */
  117. struct gsc_driverdata {
  118. const struct drm_exynos_ipp_limit *limits;
  119. int num_limits;
  120. const char *clk_names[GSC_MAX_CLOCKS];
  121. int num_clocks;
  122. };
  123. /* 8-tap Filter Coefficient */
  124. static const int h_coef_8t[GSC_COEF_RATIO][GSC_COEF_ATTR][GSC_COEF_H_8T] = {
  125. { /* Ratio <= 65536 (~8:8) */
  126. { 0, 0, 0, 128, 0, 0, 0, 0 },
  127. { -1, 2, -6, 127, 7, -2, 1, 0 },
  128. { -1, 4, -12, 125, 16, -5, 1, 0 },
  129. { -1, 5, -15, 120, 25, -8, 2, 0 },
  130. { -1, 6, -18, 114, 35, -10, 3, -1 },
  131. { -1, 6, -20, 107, 46, -13, 4, -1 },
  132. { -2, 7, -21, 99, 57, -16, 5, -1 },
  133. { -1, 6, -20, 89, 68, -18, 5, -1 },
  134. { -1, 6, -20, 79, 79, -20, 6, -1 },
  135. { -1, 5, -18, 68, 89, -20, 6, -1 },
  136. { -1, 5, -16, 57, 99, -21, 7, -2 },
  137. { -1, 4, -13, 46, 107, -20, 6, -1 },
  138. { -1, 3, -10, 35, 114, -18, 6, -1 },
  139. { 0, 2, -8, 25, 120, -15, 5, -1 },
  140. { 0, 1, -5, 16, 125, -12, 4, -1 },
  141. { 0, 1, -2, 7, 127, -6, 2, -1 }
  142. }, { /* 65536 < Ratio <= 74898 (~8:7) */
  143. { 3, -8, 14, 111, 13, -8, 3, 0 },
  144. { 2, -6, 7, 112, 21, -10, 3, -1 },
  145. { 2, -4, 1, 110, 28, -12, 4, -1 },
  146. { 1, -2, -3, 106, 36, -13, 4, -1 },
  147. { 1, -1, -7, 103, 44, -15, 4, -1 },
  148. { 1, 1, -11, 97, 53, -16, 4, -1 },
  149. { 0, 2, -13, 91, 61, -16, 4, -1 },
  150. { 0, 3, -15, 85, 69, -17, 4, -1 },
  151. { 0, 3, -16, 77, 77, -16, 3, 0 },
  152. { -1, 4, -17, 69, 85, -15, 3, 0 },
  153. { -1, 4, -16, 61, 91, -13, 2, 0 },
  154. { -1, 4, -16, 53, 97, -11, 1, 1 },
  155. { -1, 4, -15, 44, 103, -7, -1, 1 },
  156. { -1, 4, -13, 36, 106, -3, -2, 1 },
  157. { -1, 4, -12, 28, 110, 1, -4, 2 },
  158. { -1, 3, -10, 21, 112, 7, -6, 2 }
  159. }, { /* 74898 < Ratio <= 87381 (~8:6) */
  160. { 2, -11, 25, 96, 25, -11, 2, 0 },
  161. { 2, -10, 19, 96, 31, -12, 2, 0 },
  162. { 2, -9, 14, 94, 37, -12, 2, 0 },
  163. { 2, -8, 10, 92, 43, -12, 1, 0 },
  164. { 2, -7, 5, 90, 49, -12, 1, 0 },
  165. { 2, -5, 1, 86, 55, -12, 0, 1 },
  166. { 2, -4, -2, 82, 61, -11, -1, 1 },
  167. { 1, -3, -5, 77, 67, -9, -1, 1 },
  168. { 1, -2, -7, 72, 72, -7, -2, 1 },
  169. { 1, -1, -9, 67, 77, -5, -3, 1 },
  170. { 1, -1, -11, 61, 82, -2, -4, 2 },
  171. { 1, 0, -12, 55, 86, 1, -5, 2 },
  172. { 0, 1, -12, 49, 90, 5, -7, 2 },
  173. { 0, 1, -12, 43, 92, 10, -8, 2 },
  174. { 0, 2, -12, 37, 94, 14, -9, 2 },
  175. { 0, 2, -12, 31, 96, 19, -10, 2 }
  176. }, { /* 87381 < Ratio <= 104857 (~8:5) */
  177. { -1, -8, 33, 80, 33, -8, -1, 0 },
  178. { -1, -8, 28, 80, 37, -7, -2, 1 },
  179. { 0, -8, 24, 79, 41, -7, -2, 1 },
  180. { 0, -8, 20, 78, 46, -6, -3, 1 },
  181. { 0, -8, 16, 76, 50, -4, -3, 1 },
  182. { 0, -7, 13, 74, 54, -3, -4, 1 },
  183. { 1, -7, 10, 71, 58, -1, -5, 1 },
  184. { 1, -6, 6, 68, 62, 1, -5, 1 },
  185. { 1, -6, 4, 65, 65, 4, -6, 1 },
  186. { 1, -5, 1, 62, 68, 6, -6, 1 },
  187. { 1, -5, -1, 58, 71, 10, -7, 1 },
  188. { 1, -4, -3, 54, 74, 13, -7, 0 },
  189. { 1, -3, -4, 50, 76, 16, -8, 0 },
  190. { 1, -3, -6, 46, 78, 20, -8, 0 },
  191. { 1, -2, -7, 41, 79, 24, -8, 0 },
  192. { 1, -2, -7, 37, 80, 28, -8, -1 }
  193. }, { /* 104857 < Ratio <= 131072 (~8:4) */
  194. { -3, 0, 35, 64, 35, 0, -3, 0 },
  195. { -3, -1, 32, 64, 38, 1, -3, 0 },
  196. { -2, -2, 29, 63, 41, 2, -3, 0 },
  197. { -2, -3, 27, 63, 43, 4, -4, 0 },
  198. { -2, -3, 24, 61, 46, 6, -4, 0 },
  199. { -2, -3, 21, 60, 49, 7, -4, 0 },
  200. { -1, -4, 19, 59, 51, 9, -4, -1 },
  201. { -1, -4, 16, 57, 53, 12, -4, -1 },
  202. { -1, -4, 14, 55, 55, 14, -4, -1 },
  203. { -1, -4, 12, 53, 57, 16, -4, -1 },
  204. { -1, -4, 9, 51, 59, 19, -4, -1 },
  205. { 0, -4, 7, 49, 60, 21, -3, -2 },
  206. { 0, -4, 6, 46, 61, 24, -3, -2 },
  207. { 0, -4, 4, 43, 63, 27, -3, -2 },
  208. { 0, -3, 2, 41, 63, 29, -2, -2 },
  209. { 0, -3, 1, 38, 64, 32, -1, -3 }
  210. }, { /* 131072 < Ratio <= 174762 (~8:3) */
  211. { -1, 8, 33, 48, 33, 8, -1, 0 },
  212. { -1, 7, 31, 49, 35, 9, -1, -1 },
  213. { -1, 6, 30, 49, 36, 10, -1, -1 },
  214. { -1, 5, 28, 48, 38, 12, -1, -1 },
  215. { -1, 4, 26, 48, 39, 13, 0, -1 },
  216. { -1, 3, 24, 47, 41, 15, 0, -1 },
  217. { -1, 2, 23, 47, 42, 16, 0, -1 },
  218. { -1, 2, 21, 45, 43, 18, 1, -1 },
  219. { -1, 1, 19, 45, 45, 19, 1, -1 },
  220. { -1, 1, 18, 43, 45, 21, 2, -1 },
  221. { -1, 0, 16, 42, 47, 23, 2, -1 },
  222. { -1, 0, 15, 41, 47, 24, 3, -1 },
  223. { -1, 0, 13, 39, 48, 26, 4, -1 },
  224. { -1, -1, 12, 38, 48, 28, 5, -1 },
  225. { -1, -1, 10, 36, 49, 30, 6, -1 },
  226. { -1, -1, 9, 35, 49, 31, 7, -1 }
  227. }, { /* 174762 < Ratio <= 262144 (~8:2) */
  228. { 2, 13, 30, 38, 30, 13, 2, 0 },
  229. { 2, 12, 29, 38, 30, 14, 3, 0 },
  230. { 2, 11, 28, 38, 31, 15, 3, 0 },
  231. { 2, 10, 26, 38, 32, 16, 4, 0 },
  232. { 1, 10, 26, 37, 33, 17, 4, 0 },
  233. { 1, 9, 24, 37, 34, 18, 5, 0 },
  234. { 1, 8, 24, 37, 34, 19, 5, 0 },
  235. { 1, 7, 22, 36, 35, 20, 6, 1 },
  236. { 1, 6, 21, 36, 36, 21, 6, 1 },
  237. { 1, 6, 20, 35, 36, 22, 7, 1 },
  238. { 0, 5, 19, 34, 37, 24, 8, 1 },
  239. { 0, 5, 18, 34, 37, 24, 9, 1 },
  240. { 0, 4, 17, 33, 37, 26, 10, 1 },
  241. { 0, 4, 16, 32, 38, 26, 10, 2 },
  242. { 0, 3, 15, 31, 38, 28, 11, 2 },
  243. { 0, 3, 14, 30, 38, 29, 12, 2 }
  244. }
  245. };
  246. /* 4-tap Filter Coefficient */
  247. static const int v_coef_4t[GSC_COEF_RATIO][GSC_COEF_ATTR][GSC_COEF_V_4T] = {
  248. { /* Ratio <= 65536 (~8:8) */
  249. { 0, 128, 0, 0 },
  250. { -4, 127, 5, 0 },
  251. { -6, 124, 11, -1 },
  252. { -8, 118, 19, -1 },
  253. { -8, 111, 27, -2 },
  254. { -8, 102, 37, -3 },
  255. { -8, 92, 48, -4 },
  256. { -7, 81, 59, -5 },
  257. { -6, 70, 70, -6 },
  258. { -5, 59, 81, -7 },
  259. { -4, 48, 92, -8 },
  260. { -3, 37, 102, -8 },
  261. { -2, 27, 111, -8 },
  262. { -1, 19, 118, -8 },
  263. { -1, 11, 124, -6 },
  264. { 0, 5, 127, -4 }
  265. }, { /* 65536 < Ratio <= 74898 (~8:7) */
  266. { 8, 112, 8, 0 },
  267. { 4, 111, 14, -1 },
  268. { 1, 109, 20, -2 },
  269. { -2, 105, 27, -2 },
  270. { -3, 100, 34, -3 },
  271. { -5, 93, 43, -3 },
  272. { -5, 86, 51, -4 },
  273. { -5, 77, 60, -4 },
  274. { -5, 69, 69, -5 },
  275. { -4, 60, 77, -5 },
  276. { -4, 51, 86, -5 },
  277. { -3, 43, 93, -5 },
  278. { -3, 34, 100, -3 },
  279. { -2, 27, 105, -2 },
  280. { -2, 20, 109, 1 },
  281. { -1, 14, 111, 4 }
  282. }, { /* 74898 < Ratio <= 87381 (~8:6) */
  283. { 16, 96, 16, 0 },
  284. { 12, 97, 21, -2 },
  285. { 8, 96, 26, -2 },
  286. { 5, 93, 32, -2 },
  287. { 2, 89, 39, -2 },
  288. { 0, 84, 46, -2 },
  289. { -1, 79, 53, -3 },
  290. { -2, 73, 59, -2 },
  291. { -2, 66, 66, -2 },
  292. { -2, 59, 73, -2 },
  293. { -3, 53, 79, -1 },
  294. { -2, 46, 84, 0 },
  295. { -2, 39, 89, 2 },
  296. { -2, 32, 93, 5 },
  297. { -2, 26, 96, 8 },
  298. { -2, 21, 97, 12 }
  299. }, { /* 87381 < Ratio <= 104857 (~8:5) */
  300. { 22, 84, 22, 0 },
  301. { 18, 85, 26, -1 },
  302. { 14, 84, 31, -1 },
  303. { 11, 82, 36, -1 },
  304. { 8, 79, 42, -1 },
  305. { 6, 76, 47, -1 },
  306. { 4, 72, 52, 0 },
  307. { 2, 68, 58, 0 },
  308. { 1, 63, 63, 1 },
  309. { 0, 58, 68, 2 },
  310. { 0, 52, 72, 4 },
  311. { -1, 47, 76, 6 },
  312. { -1, 42, 79, 8 },
  313. { -1, 36, 82, 11 },
  314. { -1, 31, 84, 14 },
  315. { -1, 26, 85, 18 }
  316. }, { /* 104857 < Ratio <= 131072 (~8:4) */
  317. { 26, 76, 26, 0 },
  318. { 22, 76, 30, 0 },
  319. { 19, 75, 34, 0 },
  320. { 16, 73, 38, 1 },
  321. { 13, 71, 43, 1 },
  322. { 10, 69, 47, 2 },
  323. { 8, 66, 51, 3 },
  324. { 6, 63, 55, 4 },
  325. { 5, 59, 59, 5 },
  326. { 4, 55, 63, 6 },
  327. { 3, 51, 66, 8 },
  328. { 2, 47, 69, 10 },
  329. { 1, 43, 71, 13 },
  330. { 1, 38, 73, 16 },
  331. { 0, 34, 75, 19 },
  332. { 0, 30, 76, 22 }
  333. }, { /* 131072 < Ratio <= 174762 (~8:3) */
  334. { 29, 70, 29, 0 },
  335. { 26, 68, 32, 2 },
  336. { 23, 67, 36, 2 },
  337. { 20, 66, 39, 3 },
  338. { 17, 65, 43, 3 },
  339. { 15, 63, 46, 4 },
  340. { 12, 61, 50, 5 },
  341. { 10, 58, 53, 7 },
  342. { 8, 56, 56, 8 },
  343. { 7, 53, 58, 10 },
  344. { 5, 50, 61, 12 },
  345. { 4, 46, 63, 15 },
  346. { 3, 43, 65, 17 },
  347. { 3, 39, 66, 20 },
  348. { 2, 36, 67, 23 },
  349. { 2, 32, 68, 26 }
  350. }, { /* 174762 < Ratio <= 262144 (~8:2) */
  351. { 32, 64, 32, 0 },
  352. { 28, 63, 34, 3 },
  353. { 25, 62, 37, 4 },
  354. { 22, 62, 40, 4 },
  355. { 19, 61, 43, 5 },
  356. { 17, 59, 46, 6 },
  357. { 15, 58, 48, 7 },
  358. { 13, 55, 51, 9 },
  359. { 11, 53, 53, 11 },
  360. { 9, 51, 55, 13 },
  361. { 7, 48, 58, 15 },
  362. { 6, 46, 59, 17 },
  363. { 5, 43, 61, 19 },
  364. { 4, 40, 62, 22 },
  365. { 4, 37, 62, 25 },
  366. { 3, 34, 63, 28 }
  367. }
  368. };
  369. static int gsc_sw_reset(struct gsc_context *ctx)
  370. {
  371. u32 cfg;
  372. int count = GSC_RESET_TIMEOUT;
  373. /* s/w reset */
  374. cfg = (GSC_SW_RESET_SRESET);
  375. gsc_write(cfg, GSC_SW_RESET);
  376. /* wait s/w reset complete */
  377. while (count--) {
  378. cfg = gsc_read(GSC_SW_RESET);
  379. if (!cfg)
  380. break;
  381. usleep_range(1000, 2000);
  382. }
  383. if (cfg) {
  384. DRM_ERROR("failed to reset gsc h/w.\n");
  385. return -EBUSY;
  386. }
  387. /* reset sequence */
  388. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  389. cfg |= (GSC_IN_BASE_ADDR_MASK |
  390. GSC_IN_BASE_ADDR_PINGPONG(0));
  391. gsc_write(cfg, GSC_IN_BASE_ADDR_Y_MASK);
  392. gsc_write(cfg, GSC_IN_BASE_ADDR_CB_MASK);
  393. gsc_write(cfg, GSC_IN_BASE_ADDR_CR_MASK);
  394. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  395. cfg |= (GSC_OUT_BASE_ADDR_MASK |
  396. GSC_OUT_BASE_ADDR_PINGPONG(0));
  397. gsc_write(cfg, GSC_OUT_BASE_ADDR_Y_MASK);
  398. gsc_write(cfg, GSC_OUT_BASE_ADDR_CB_MASK);
  399. gsc_write(cfg, GSC_OUT_BASE_ADDR_CR_MASK);
  400. return 0;
  401. }
  402. static void gsc_handle_irq(struct gsc_context *ctx, bool enable,
  403. bool overflow, bool done)
  404. {
  405. u32 cfg;
  406. DRM_DEBUG_KMS("enable[%d]overflow[%d]level[%d]\n",
  407. enable, overflow, done);
  408. cfg = gsc_read(GSC_IRQ);
  409. cfg |= (GSC_IRQ_OR_MASK | GSC_IRQ_FRMDONE_MASK);
  410. if (enable)
  411. cfg |= GSC_IRQ_ENABLE;
  412. else
  413. cfg &= ~GSC_IRQ_ENABLE;
  414. if (overflow)
  415. cfg &= ~GSC_IRQ_OR_MASK;
  416. else
  417. cfg |= GSC_IRQ_OR_MASK;
  418. if (done)
  419. cfg &= ~GSC_IRQ_FRMDONE_MASK;
  420. else
  421. cfg |= GSC_IRQ_FRMDONE_MASK;
  422. gsc_write(cfg, GSC_IRQ);
  423. }
  424. static void gsc_src_set_fmt(struct gsc_context *ctx, u32 fmt, bool tiled)
  425. {
  426. u32 cfg;
  427. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  428. cfg = gsc_read(GSC_IN_CON);
  429. cfg &= ~(GSC_IN_RGB_TYPE_MASK | GSC_IN_YUV422_1P_ORDER_MASK |
  430. GSC_IN_CHROMA_ORDER_MASK | GSC_IN_FORMAT_MASK |
  431. GSC_IN_TILE_TYPE_MASK | GSC_IN_TILE_MODE |
  432. GSC_IN_CHROM_STRIDE_SEL_MASK | GSC_IN_RB_SWAP_MASK);
  433. switch (fmt) {
  434. case DRM_FORMAT_RGB565:
  435. cfg |= GSC_IN_RGB565;
  436. break;
  437. case DRM_FORMAT_XRGB8888:
  438. case DRM_FORMAT_ARGB8888:
  439. cfg |= GSC_IN_XRGB8888;
  440. break;
  441. case DRM_FORMAT_BGRX8888:
  442. cfg |= (GSC_IN_XRGB8888 | GSC_IN_RB_SWAP);
  443. break;
  444. case DRM_FORMAT_YUYV:
  445. cfg |= (GSC_IN_YUV422_1P |
  446. GSC_IN_YUV422_1P_ORDER_LSB_Y |
  447. GSC_IN_CHROMA_ORDER_CBCR);
  448. break;
  449. case DRM_FORMAT_YVYU:
  450. cfg |= (GSC_IN_YUV422_1P |
  451. GSC_IN_YUV422_1P_ORDER_LSB_Y |
  452. GSC_IN_CHROMA_ORDER_CRCB);
  453. break;
  454. case DRM_FORMAT_UYVY:
  455. cfg |= (GSC_IN_YUV422_1P |
  456. GSC_IN_YUV422_1P_OEDER_LSB_C |
  457. GSC_IN_CHROMA_ORDER_CBCR);
  458. break;
  459. case DRM_FORMAT_VYUY:
  460. cfg |= (GSC_IN_YUV422_1P |
  461. GSC_IN_YUV422_1P_OEDER_LSB_C |
  462. GSC_IN_CHROMA_ORDER_CRCB);
  463. break;
  464. case DRM_FORMAT_NV21:
  465. cfg |= (GSC_IN_CHROMA_ORDER_CRCB | GSC_IN_YUV420_2P);
  466. break;
  467. case DRM_FORMAT_NV61:
  468. cfg |= (GSC_IN_CHROMA_ORDER_CRCB | GSC_IN_YUV422_2P);
  469. break;
  470. case DRM_FORMAT_YUV422:
  471. cfg |= GSC_IN_YUV422_3P;
  472. break;
  473. case DRM_FORMAT_YUV420:
  474. cfg |= (GSC_IN_CHROMA_ORDER_CBCR | GSC_IN_YUV420_3P);
  475. break;
  476. case DRM_FORMAT_YVU420:
  477. cfg |= (GSC_IN_CHROMA_ORDER_CRCB | GSC_IN_YUV420_3P);
  478. break;
  479. case DRM_FORMAT_NV12:
  480. cfg |= (GSC_IN_CHROMA_ORDER_CBCR | GSC_IN_YUV420_2P);
  481. break;
  482. case DRM_FORMAT_NV16:
  483. cfg |= (GSC_IN_CHROMA_ORDER_CBCR | GSC_IN_YUV422_2P);
  484. break;
  485. }
  486. if (tiled)
  487. cfg |= (GSC_IN_TILE_C_16x8 | GSC_IN_TILE_MODE);
  488. gsc_write(cfg, GSC_IN_CON);
  489. }
  490. static void gsc_src_set_transf(struct gsc_context *ctx, unsigned int rotation)
  491. {
  492. unsigned int degree = rotation & DRM_MODE_ROTATE_MASK;
  493. u32 cfg;
  494. cfg = gsc_read(GSC_IN_CON);
  495. cfg &= ~GSC_IN_ROT_MASK;
  496. switch (degree) {
  497. case DRM_MODE_ROTATE_0:
  498. if (rotation & DRM_MODE_REFLECT_X)
  499. cfg |= GSC_IN_ROT_XFLIP;
  500. if (rotation & DRM_MODE_REFLECT_Y)
  501. cfg |= GSC_IN_ROT_YFLIP;
  502. break;
  503. case DRM_MODE_ROTATE_90:
  504. cfg |= GSC_IN_ROT_90;
  505. if (rotation & DRM_MODE_REFLECT_X)
  506. cfg |= GSC_IN_ROT_XFLIP;
  507. if (rotation & DRM_MODE_REFLECT_Y)
  508. cfg |= GSC_IN_ROT_YFLIP;
  509. break;
  510. case DRM_MODE_ROTATE_180:
  511. cfg |= GSC_IN_ROT_180;
  512. if (rotation & DRM_MODE_REFLECT_X)
  513. cfg &= ~GSC_IN_ROT_XFLIP;
  514. if (rotation & DRM_MODE_REFLECT_Y)
  515. cfg &= ~GSC_IN_ROT_YFLIP;
  516. break;
  517. case DRM_MODE_ROTATE_270:
  518. cfg |= GSC_IN_ROT_270;
  519. if (rotation & DRM_MODE_REFLECT_X)
  520. cfg &= ~GSC_IN_ROT_XFLIP;
  521. if (rotation & DRM_MODE_REFLECT_Y)
  522. cfg &= ~GSC_IN_ROT_YFLIP;
  523. break;
  524. }
  525. gsc_write(cfg, GSC_IN_CON);
  526. ctx->rotation = (cfg & GSC_IN_ROT_90) ? 1 : 0;
  527. }
  528. static void gsc_src_set_size(struct gsc_context *ctx,
  529. struct exynos_drm_ipp_buffer *buf)
  530. {
  531. struct gsc_scaler *sc = &ctx->sc;
  532. u32 cfg;
  533. /* pixel offset */
  534. cfg = (GSC_SRCIMG_OFFSET_X(buf->rect.x) |
  535. GSC_SRCIMG_OFFSET_Y(buf->rect.y));
  536. gsc_write(cfg, GSC_SRCIMG_OFFSET);
  537. /* cropped size */
  538. cfg = (GSC_CROPPED_WIDTH(buf->rect.w) |
  539. GSC_CROPPED_HEIGHT(buf->rect.h));
  540. gsc_write(cfg, GSC_CROPPED_SIZE);
  541. /* original size */
  542. cfg = gsc_read(GSC_SRCIMG_SIZE);
  543. cfg &= ~(GSC_SRCIMG_HEIGHT_MASK |
  544. GSC_SRCIMG_WIDTH_MASK);
  545. cfg |= (GSC_SRCIMG_WIDTH(buf->buf.pitch[0] / buf->format->cpp[0]) |
  546. GSC_SRCIMG_HEIGHT(buf->buf.height));
  547. gsc_write(cfg, GSC_SRCIMG_SIZE);
  548. cfg = gsc_read(GSC_IN_CON);
  549. cfg &= ~GSC_IN_RGB_TYPE_MASK;
  550. if (buf->rect.w >= GSC_WIDTH_ITU_709)
  551. if (sc->range)
  552. cfg |= GSC_IN_RGB_HD_WIDE;
  553. else
  554. cfg |= GSC_IN_RGB_HD_NARROW;
  555. else
  556. if (sc->range)
  557. cfg |= GSC_IN_RGB_SD_WIDE;
  558. else
  559. cfg |= GSC_IN_RGB_SD_NARROW;
  560. gsc_write(cfg, GSC_IN_CON);
  561. }
  562. static void gsc_src_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  563. bool enqueue)
  564. {
  565. bool masked = !enqueue;
  566. u32 cfg;
  567. u32 mask = 0x00000001 << buf_id;
  568. /* mask register set */
  569. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  570. /* sequence id */
  571. cfg &= ~mask;
  572. cfg |= masked << buf_id;
  573. gsc_write(cfg, GSC_IN_BASE_ADDR_Y_MASK);
  574. gsc_write(cfg, GSC_IN_BASE_ADDR_CB_MASK);
  575. gsc_write(cfg, GSC_IN_BASE_ADDR_CR_MASK);
  576. }
  577. static void gsc_src_set_addr(struct gsc_context *ctx, u32 buf_id,
  578. struct exynos_drm_ipp_buffer *buf)
  579. {
  580. /* address register set */
  581. gsc_write(buf->dma_addr[0], GSC_IN_BASE_ADDR_Y(buf_id));
  582. gsc_write(buf->dma_addr[1], GSC_IN_BASE_ADDR_CB(buf_id));
  583. gsc_write(buf->dma_addr[2], GSC_IN_BASE_ADDR_CR(buf_id));
  584. gsc_src_set_buf_seq(ctx, buf_id, true);
  585. }
  586. static void gsc_dst_set_fmt(struct gsc_context *ctx, u32 fmt, bool tiled)
  587. {
  588. u32 cfg;
  589. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  590. cfg = gsc_read(GSC_OUT_CON);
  591. cfg &= ~(GSC_OUT_RGB_TYPE_MASK | GSC_OUT_YUV422_1P_ORDER_MASK |
  592. GSC_OUT_CHROMA_ORDER_MASK | GSC_OUT_FORMAT_MASK |
  593. GSC_OUT_CHROM_STRIDE_SEL_MASK | GSC_OUT_RB_SWAP_MASK |
  594. GSC_OUT_GLOBAL_ALPHA_MASK);
  595. switch (fmt) {
  596. case DRM_FORMAT_RGB565:
  597. cfg |= GSC_OUT_RGB565;
  598. break;
  599. case DRM_FORMAT_ARGB8888:
  600. case DRM_FORMAT_XRGB8888:
  601. cfg |= (GSC_OUT_XRGB8888 | GSC_OUT_GLOBAL_ALPHA(0xff));
  602. break;
  603. case DRM_FORMAT_BGRX8888:
  604. cfg |= (GSC_OUT_XRGB8888 | GSC_OUT_RB_SWAP);
  605. break;
  606. case DRM_FORMAT_YUYV:
  607. cfg |= (GSC_OUT_YUV422_1P |
  608. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  609. GSC_OUT_CHROMA_ORDER_CBCR);
  610. break;
  611. case DRM_FORMAT_YVYU:
  612. cfg |= (GSC_OUT_YUV422_1P |
  613. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  614. GSC_OUT_CHROMA_ORDER_CRCB);
  615. break;
  616. case DRM_FORMAT_UYVY:
  617. cfg |= (GSC_OUT_YUV422_1P |
  618. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  619. GSC_OUT_CHROMA_ORDER_CBCR);
  620. break;
  621. case DRM_FORMAT_VYUY:
  622. cfg |= (GSC_OUT_YUV422_1P |
  623. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  624. GSC_OUT_CHROMA_ORDER_CRCB);
  625. break;
  626. case DRM_FORMAT_NV21:
  627. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV420_2P);
  628. break;
  629. case DRM_FORMAT_NV61:
  630. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV422_2P);
  631. break;
  632. case DRM_FORMAT_YUV422:
  633. cfg |= GSC_OUT_YUV422_3P;
  634. break;
  635. case DRM_FORMAT_YUV420:
  636. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV420_3P);
  637. break;
  638. case DRM_FORMAT_YVU420:
  639. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV420_3P);
  640. break;
  641. case DRM_FORMAT_NV12:
  642. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV420_2P);
  643. break;
  644. case DRM_FORMAT_NV16:
  645. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV422_2P);
  646. break;
  647. }
  648. if (tiled)
  649. cfg |= (GSC_IN_TILE_C_16x8 | GSC_OUT_TILE_MODE);
  650. gsc_write(cfg, GSC_OUT_CON);
  651. }
  652. static int gsc_get_ratio_shift(u32 src, u32 dst, u32 *ratio)
  653. {
  654. DRM_DEBUG_KMS("src[%d]dst[%d]\n", src, dst);
  655. if (src >= dst * 8) {
  656. DRM_ERROR("failed to make ratio and shift.\n");
  657. return -EINVAL;
  658. } else if (src >= dst * 4)
  659. *ratio = 4;
  660. else if (src >= dst * 2)
  661. *ratio = 2;
  662. else
  663. *ratio = 1;
  664. return 0;
  665. }
  666. static void gsc_get_prescaler_shfactor(u32 hratio, u32 vratio, u32 *shfactor)
  667. {
  668. if (hratio == 4 && vratio == 4)
  669. *shfactor = 4;
  670. else if ((hratio == 4 && vratio == 2) ||
  671. (hratio == 2 && vratio == 4))
  672. *shfactor = 3;
  673. else if ((hratio == 4 && vratio == 1) ||
  674. (hratio == 1 && vratio == 4) ||
  675. (hratio == 2 && vratio == 2))
  676. *shfactor = 2;
  677. else if (hratio == 1 && vratio == 1)
  678. *shfactor = 0;
  679. else
  680. *shfactor = 1;
  681. }
  682. static int gsc_set_prescaler(struct gsc_context *ctx, struct gsc_scaler *sc,
  683. struct drm_exynos_ipp_task_rect *src,
  684. struct drm_exynos_ipp_task_rect *dst)
  685. {
  686. u32 cfg;
  687. u32 src_w, src_h, dst_w, dst_h;
  688. int ret = 0;
  689. src_w = src->w;
  690. src_h = src->h;
  691. if (ctx->rotation) {
  692. dst_w = dst->h;
  693. dst_h = dst->w;
  694. } else {
  695. dst_w = dst->w;
  696. dst_h = dst->h;
  697. }
  698. ret = gsc_get_ratio_shift(src_w, dst_w, &sc->pre_hratio);
  699. if (ret) {
  700. dev_err(ctx->dev, "failed to get ratio horizontal.\n");
  701. return ret;
  702. }
  703. ret = gsc_get_ratio_shift(src_h, dst_h, &sc->pre_vratio);
  704. if (ret) {
  705. dev_err(ctx->dev, "failed to get ratio vertical.\n");
  706. return ret;
  707. }
  708. DRM_DEBUG_KMS("pre_hratio[%d]pre_vratio[%d]\n",
  709. sc->pre_hratio, sc->pre_vratio);
  710. sc->main_hratio = (src_w << 16) / dst_w;
  711. sc->main_vratio = (src_h << 16) / dst_h;
  712. DRM_DEBUG_KMS("main_hratio[%ld]main_vratio[%ld]\n",
  713. sc->main_hratio, sc->main_vratio);
  714. gsc_get_prescaler_shfactor(sc->pre_hratio, sc->pre_vratio,
  715. &sc->pre_shfactor);
  716. DRM_DEBUG_KMS("pre_shfactor[%d]\n", sc->pre_shfactor);
  717. cfg = (GSC_PRESC_SHFACTOR(sc->pre_shfactor) |
  718. GSC_PRESC_H_RATIO(sc->pre_hratio) |
  719. GSC_PRESC_V_RATIO(sc->pre_vratio));
  720. gsc_write(cfg, GSC_PRE_SCALE_RATIO);
  721. return ret;
  722. }
  723. static void gsc_set_h_coef(struct gsc_context *ctx, unsigned long main_hratio)
  724. {
  725. int i, j, k, sc_ratio;
  726. if (main_hratio <= GSC_SC_UP_MAX_RATIO)
  727. sc_ratio = 0;
  728. else if (main_hratio <= GSC_SC_DOWN_RATIO_7_8)
  729. sc_ratio = 1;
  730. else if (main_hratio <= GSC_SC_DOWN_RATIO_6_8)
  731. sc_ratio = 2;
  732. else if (main_hratio <= GSC_SC_DOWN_RATIO_5_8)
  733. sc_ratio = 3;
  734. else if (main_hratio <= GSC_SC_DOWN_RATIO_4_8)
  735. sc_ratio = 4;
  736. else if (main_hratio <= GSC_SC_DOWN_RATIO_3_8)
  737. sc_ratio = 5;
  738. else
  739. sc_ratio = 6;
  740. for (i = 0; i < GSC_COEF_PHASE; i++)
  741. for (j = 0; j < GSC_COEF_H_8T; j++)
  742. for (k = 0; k < GSC_COEF_DEPTH; k++)
  743. gsc_write(h_coef_8t[sc_ratio][i][j],
  744. GSC_HCOEF(i, j, k));
  745. }
  746. static void gsc_set_v_coef(struct gsc_context *ctx, unsigned long main_vratio)
  747. {
  748. int i, j, k, sc_ratio;
  749. if (main_vratio <= GSC_SC_UP_MAX_RATIO)
  750. sc_ratio = 0;
  751. else if (main_vratio <= GSC_SC_DOWN_RATIO_7_8)
  752. sc_ratio = 1;
  753. else if (main_vratio <= GSC_SC_DOWN_RATIO_6_8)
  754. sc_ratio = 2;
  755. else if (main_vratio <= GSC_SC_DOWN_RATIO_5_8)
  756. sc_ratio = 3;
  757. else if (main_vratio <= GSC_SC_DOWN_RATIO_4_8)
  758. sc_ratio = 4;
  759. else if (main_vratio <= GSC_SC_DOWN_RATIO_3_8)
  760. sc_ratio = 5;
  761. else
  762. sc_ratio = 6;
  763. for (i = 0; i < GSC_COEF_PHASE; i++)
  764. for (j = 0; j < GSC_COEF_V_4T; j++)
  765. for (k = 0; k < GSC_COEF_DEPTH; k++)
  766. gsc_write(v_coef_4t[sc_ratio][i][j],
  767. GSC_VCOEF(i, j, k));
  768. }
  769. static void gsc_set_scaler(struct gsc_context *ctx, struct gsc_scaler *sc)
  770. {
  771. u32 cfg;
  772. DRM_DEBUG_KMS("main_hratio[%ld]main_vratio[%ld]\n",
  773. sc->main_hratio, sc->main_vratio);
  774. gsc_set_h_coef(ctx, sc->main_hratio);
  775. cfg = GSC_MAIN_H_RATIO_VALUE(sc->main_hratio);
  776. gsc_write(cfg, GSC_MAIN_H_RATIO);
  777. gsc_set_v_coef(ctx, sc->main_vratio);
  778. cfg = GSC_MAIN_V_RATIO_VALUE(sc->main_vratio);
  779. gsc_write(cfg, GSC_MAIN_V_RATIO);
  780. }
  781. static void gsc_dst_set_size(struct gsc_context *ctx,
  782. struct exynos_drm_ipp_buffer *buf)
  783. {
  784. struct gsc_scaler *sc = &ctx->sc;
  785. u32 cfg;
  786. /* pixel offset */
  787. cfg = (GSC_DSTIMG_OFFSET_X(buf->rect.x) |
  788. GSC_DSTIMG_OFFSET_Y(buf->rect.y));
  789. gsc_write(cfg, GSC_DSTIMG_OFFSET);
  790. /* scaled size */
  791. if (ctx->rotation)
  792. cfg = (GSC_SCALED_WIDTH(buf->rect.h) |
  793. GSC_SCALED_HEIGHT(buf->rect.w));
  794. else
  795. cfg = (GSC_SCALED_WIDTH(buf->rect.w) |
  796. GSC_SCALED_HEIGHT(buf->rect.h));
  797. gsc_write(cfg, GSC_SCALED_SIZE);
  798. /* original size */
  799. cfg = gsc_read(GSC_DSTIMG_SIZE);
  800. cfg &= ~(GSC_DSTIMG_HEIGHT_MASK | GSC_DSTIMG_WIDTH_MASK);
  801. cfg |= GSC_DSTIMG_WIDTH(buf->buf.pitch[0] / buf->format->cpp[0]) |
  802. GSC_DSTIMG_HEIGHT(buf->buf.height);
  803. gsc_write(cfg, GSC_DSTIMG_SIZE);
  804. cfg = gsc_read(GSC_OUT_CON);
  805. cfg &= ~GSC_OUT_RGB_TYPE_MASK;
  806. if (buf->rect.w >= GSC_WIDTH_ITU_709)
  807. if (sc->range)
  808. cfg |= GSC_OUT_RGB_HD_WIDE;
  809. else
  810. cfg |= GSC_OUT_RGB_HD_NARROW;
  811. else
  812. if (sc->range)
  813. cfg |= GSC_OUT_RGB_SD_WIDE;
  814. else
  815. cfg |= GSC_OUT_RGB_SD_NARROW;
  816. gsc_write(cfg, GSC_OUT_CON);
  817. }
  818. static int gsc_dst_get_buf_seq(struct gsc_context *ctx)
  819. {
  820. u32 cfg, i, buf_num = GSC_REG_SZ;
  821. u32 mask = 0x00000001;
  822. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  823. for (i = 0; i < GSC_REG_SZ; i++)
  824. if (cfg & (mask << i))
  825. buf_num--;
  826. DRM_DEBUG_KMS("buf_num[%d]\n", buf_num);
  827. return buf_num;
  828. }
  829. static void gsc_dst_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  830. bool enqueue)
  831. {
  832. bool masked = !enqueue;
  833. u32 cfg;
  834. u32 mask = 0x00000001 << buf_id;
  835. /* mask register set */
  836. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  837. /* sequence id */
  838. cfg &= ~mask;
  839. cfg |= masked << buf_id;
  840. gsc_write(cfg, GSC_OUT_BASE_ADDR_Y_MASK);
  841. gsc_write(cfg, GSC_OUT_BASE_ADDR_CB_MASK);
  842. gsc_write(cfg, GSC_OUT_BASE_ADDR_CR_MASK);
  843. /* interrupt enable */
  844. if (enqueue && gsc_dst_get_buf_seq(ctx) >= GSC_BUF_START)
  845. gsc_handle_irq(ctx, true, false, true);
  846. /* interrupt disable */
  847. if (!enqueue && gsc_dst_get_buf_seq(ctx) <= GSC_BUF_STOP)
  848. gsc_handle_irq(ctx, false, false, true);
  849. }
  850. static void gsc_dst_set_addr(struct gsc_context *ctx,
  851. u32 buf_id, struct exynos_drm_ipp_buffer *buf)
  852. {
  853. /* address register set */
  854. gsc_write(buf->dma_addr[0], GSC_OUT_BASE_ADDR_Y(buf_id));
  855. gsc_write(buf->dma_addr[1], GSC_OUT_BASE_ADDR_CB(buf_id));
  856. gsc_write(buf->dma_addr[2], GSC_OUT_BASE_ADDR_CR(buf_id));
  857. gsc_dst_set_buf_seq(ctx, buf_id, true);
  858. }
  859. static int gsc_get_src_buf_index(struct gsc_context *ctx)
  860. {
  861. u32 cfg, curr_index, i;
  862. u32 buf_id = GSC_MAX_SRC;
  863. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  864. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  865. curr_index = GSC_IN_CURR_GET_INDEX(cfg);
  866. for (i = curr_index; i < GSC_MAX_SRC; i++) {
  867. if (!((cfg >> i) & 0x1)) {
  868. buf_id = i;
  869. break;
  870. }
  871. }
  872. DRM_DEBUG_KMS("cfg[0x%x]curr_index[%d]buf_id[%d]\n", cfg,
  873. curr_index, buf_id);
  874. if (buf_id == GSC_MAX_SRC) {
  875. DRM_ERROR("failed to get in buffer index.\n");
  876. return -EINVAL;
  877. }
  878. gsc_src_set_buf_seq(ctx, buf_id, false);
  879. return buf_id;
  880. }
  881. static int gsc_get_dst_buf_index(struct gsc_context *ctx)
  882. {
  883. u32 cfg, curr_index, i;
  884. u32 buf_id = GSC_MAX_DST;
  885. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  886. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  887. curr_index = GSC_OUT_CURR_GET_INDEX(cfg);
  888. for (i = curr_index; i < GSC_MAX_DST; i++) {
  889. if (!((cfg >> i) & 0x1)) {
  890. buf_id = i;
  891. break;
  892. }
  893. }
  894. if (buf_id == GSC_MAX_DST) {
  895. DRM_ERROR("failed to get out buffer index.\n");
  896. return -EINVAL;
  897. }
  898. gsc_dst_set_buf_seq(ctx, buf_id, false);
  899. DRM_DEBUG_KMS("cfg[0x%x]curr_index[%d]buf_id[%d]\n", cfg,
  900. curr_index, buf_id);
  901. return buf_id;
  902. }
  903. static irqreturn_t gsc_irq_handler(int irq, void *dev_id)
  904. {
  905. struct gsc_context *ctx = dev_id;
  906. u32 status;
  907. int err = 0;
  908. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  909. status = gsc_read(GSC_IRQ);
  910. if (status & GSC_IRQ_STATUS_OR_IRQ) {
  911. dev_err(ctx->dev, "occurred overflow at %d, status 0x%x.\n",
  912. ctx->id, status);
  913. err = -EINVAL;
  914. }
  915. if (status & GSC_IRQ_STATUS_OR_FRM_DONE) {
  916. int src_buf_id, dst_buf_id;
  917. dev_dbg(ctx->dev, "occurred frame done at %d, status 0x%x.\n",
  918. ctx->id, status);
  919. src_buf_id = gsc_get_src_buf_index(ctx);
  920. dst_buf_id = gsc_get_dst_buf_index(ctx);
  921. DRM_DEBUG_KMS("buf_id_src[%d]buf_id_dst[%d]\n", src_buf_id,
  922. dst_buf_id);
  923. if (src_buf_id < 0 || dst_buf_id < 0)
  924. err = -EINVAL;
  925. }
  926. if (ctx->task) {
  927. struct exynos_drm_ipp_task *task = ctx->task;
  928. ctx->task = NULL;
  929. pm_runtime_mark_last_busy(ctx->dev);
  930. pm_runtime_put_autosuspend(ctx->dev);
  931. exynos_drm_ipp_task_done(task, err);
  932. }
  933. return IRQ_HANDLED;
  934. }
  935. static int gsc_reset(struct gsc_context *ctx)
  936. {
  937. struct gsc_scaler *sc = &ctx->sc;
  938. int ret;
  939. /* reset h/w block */
  940. ret = gsc_sw_reset(ctx);
  941. if (ret < 0) {
  942. dev_err(ctx->dev, "failed to reset hardware.\n");
  943. return ret;
  944. }
  945. /* scaler setting */
  946. memset(&ctx->sc, 0x0, sizeof(ctx->sc));
  947. sc->range = true;
  948. return 0;
  949. }
  950. static void gsc_start(struct gsc_context *ctx)
  951. {
  952. u32 cfg;
  953. gsc_handle_irq(ctx, true, false, true);
  954. /* enable one shot */
  955. cfg = gsc_read(GSC_ENABLE);
  956. cfg &= ~(GSC_ENABLE_ON_CLEAR_MASK |
  957. GSC_ENABLE_CLK_GATE_MODE_MASK);
  958. cfg |= GSC_ENABLE_ON_CLEAR_ONESHOT;
  959. gsc_write(cfg, GSC_ENABLE);
  960. /* src dma memory */
  961. cfg = gsc_read(GSC_IN_CON);
  962. cfg &= ~(GSC_IN_PATH_MASK | GSC_IN_LOCAL_SEL_MASK);
  963. cfg |= GSC_IN_PATH_MEMORY;
  964. gsc_write(cfg, GSC_IN_CON);
  965. /* dst dma memory */
  966. cfg = gsc_read(GSC_OUT_CON);
  967. cfg |= GSC_OUT_PATH_MEMORY;
  968. gsc_write(cfg, GSC_OUT_CON);
  969. gsc_set_scaler(ctx, &ctx->sc);
  970. cfg = gsc_read(GSC_ENABLE);
  971. cfg |= GSC_ENABLE_ON;
  972. gsc_write(cfg, GSC_ENABLE);
  973. }
  974. static int gsc_commit(struct exynos_drm_ipp *ipp,
  975. struct exynos_drm_ipp_task *task)
  976. {
  977. struct gsc_context *ctx = container_of(ipp, struct gsc_context, ipp);
  978. int ret;
  979. pm_runtime_get_sync(ctx->dev);
  980. ctx->task = task;
  981. ret = gsc_reset(ctx);
  982. if (ret) {
  983. pm_runtime_put_autosuspend(ctx->dev);
  984. ctx->task = NULL;
  985. return ret;
  986. }
  987. gsc_src_set_fmt(ctx, task->src.buf.fourcc, task->src.buf.modifier);
  988. gsc_src_set_transf(ctx, task->transform.rotation);
  989. gsc_src_set_size(ctx, &task->src);
  990. gsc_src_set_addr(ctx, 0, &task->src);
  991. gsc_dst_set_fmt(ctx, task->dst.buf.fourcc, task->dst.buf.modifier);
  992. gsc_dst_set_size(ctx, &task->dst);
  993. gsc_dst_set_addr(ctx, 0, &task->dst);
  994. gsc_set_prescaler(ctx, &ctx->sc, &task->src.rect, &task->dst.rect);
  995. gsc_start(ctx);
  996. return 0;
  997. }
  998. static void gsc_abort(struct exynos_drm_ipp *ipp,
  999. struct exynos_drm_ipp_task *task)
  1000. {
  1001. struct gsc_context *ctx =
  1002. container_of(ipp, struct gsc_context, ipp);
  1003. gsc_reset(ctx);
  1004. if (ctx->task) {
  1005. struct exynos_drm_ipp_task *task = ctx->task;
  1006. ctx->task = NULL;
  1007. pm_runtime_mark_last_busy(ctx->dev);
  1008. pm_runtime_put_autosuspend(ctx->dev);
  1009. exynos_drm_ipp_task_done(task, -EIO);
  1010. }
  1011. }
  1012. static struct exynos_drm_ipp_funcs ipp_funcs = {
  1013. .commit = gsc_commit,
  1014. .abort = gsc_abort,
  1015. };
  1016. static int gsc_bind(struct device *dev, struct device *master, void *data)
  1017. {
  1018. struct gsc_context *ctx = dev_get_drvdata(dev);
  1019. struct drm_device *drm_dev = data;
  1020. struct exynos_drm_ipp *ipp = &ctx->ipp;
  1021. ctx->drm_dev = drm_dev;
  1022. drm_iommu_attach_device(drm_dev, dev);
  1023. exynos_drm_ipp_register(drm_dev, ipp, &ipp_funcs,
  1024. DRM_EXYNOS_IPP_CAP_CROP | DRM_EXYNOS_IPP_CAP_ROTATE |
  1025. DRM_EXYNOS_IPP_CAP_SCALE | DRM_EXYNOS_IPP_CAP_CONVERT,
  1026. ctx->formats, ctx->num_formats, "gsc");
  1027. dev_info(dev, "The exynos gscaler has been probed successfully\n");
  1028. return 0;
  1029. }
  1030. static void gsc_unbind(struct device *dev, struct device *master,
  1031. void *data)
  1032. {
  1033. struct gsc_context *ctx = dev_get_drvdata(dev);
  1034. struct drm_device *drm_dev = data;
  1035. struct exynos_drm_ipp *ipp = &ctx->ipp;
  1036. exynos_drm_ipp_unregister(drm_dev, ipp);
  1037. drm_iommu_detach_device(drm_dev, dev);
  1038. }
  1039. static const struct component_ops gsc_component_ops = {
  1040. .bind = gsc_bind,
  1041. .unbind = gsc_unbind,
  1042. };
  1043. static const unsigned int gsc_formats[] = {
  1044. DRM_FORMAT_ARGB8888,
  1045. DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB565, DRM_FORMAT_BGRX8888,
  1046. DRM_FORMAT_NV12, DRM_FORMAT_NV16, DRM_FORMAT_NV21, DRM_FORMAT_NV61,
  1047. DRM_FORMAT_UYVY, DRM_FORMAT_VYUY, DRM_FORMAT_YUYV, DRM_FORMAT_YVYU,
  1048. DRM_FORMAT_YUV420, DRM_FORMAT_YVU420, DRM_FORMAT_YUV422,
  1049. };
  1050. static const unsigned int gsc_tiled_formats[] = {
  1051. DRM_FORMAT_NV12, DRM_FORMAT_NV21,
  1052. };
  1053. static int gsc_probe(struct platform_device *pdev)
  1054. {
  1055. struct device *dev = &pdev->dev;
  1056. struct gsc_driverdata *driver_data;
  1057. struct exynos_drm_ipp_formats *formats;
  1058. struct gsc_context *ctx;
  1059. struct resource *res;
  1060. int num_formats, ret, i, j;
  1061. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1062. if (!ctx)
  1063. return -ENOMEM;
  1064. driver_data = (struct gsc_driverdata *)of_device_get_match_data(dev);
  1065. ctx->dev = dev;
  1066. ctx->num_clocks = driver_data->num_clocks;
  1067. ctx->clk_names = driver_data->clk_names;
  1068. /* construct formats/limits array */
  1069. num_formats = ARRAY_SIZE(gsc_formats) + ARRAY_SIZE(gsc_tiled_formats);
  1070. formats = devm_kcalloc(dev, num_formats, sizeof(*formats), GFP_KERNEL);
  1071. if (!formats)
  1072. return -ENOMEM;
  1073. /* linear formats */
  1074. for (i = 0; i < ARRAY_SIZE(gsc_formats); i++) {
  1075. formats[i].fourcc = gsc_formats[i];
  1076. formats[i].type = DRM_EXYNOS_IPP_FORMAT_SOURCE |
  1077. DRM_EXYNOS_IPP_FORMAT_DESTINATION;
  1078. formats[i].limits = driver_data->limits;
  1079. formats[i].num_limits = driver_data->num_limits;
  1080. }
  1081. /* tiled formats */
  1082. for (j = i, i = 0; i < ARRAY_SIZE(gsc_tiled_formats); j++, i++) {
  1083. formats[j].fourcc = gsc_tiled_formats[i];
  1084. formats[j].modifier = DRM_FORMAT_MOD_SAMSUNG_16_16_TILE;
  1085. formats[j].type = DRM_EXYNOS_IPP_FORMAT_SOURCE |
  1086. DRM_EXYNOS_IPP_FORMAT_DESTINATION;
  1087. formats[j].limits = driver_data->limits;
  1088. formats[j].num_limits = driver_data->num_limits;
  1089. }
  1090. ctx->formats = formats;
  1091. ctx->num_formats = num_formats;
  1092. /* clock control */
  1093. for (i = 0; i < ctx->num_clocks; i++) {
  1094. ctx->clocks[i] = devm_clk_get(dev, ctx->clk_names[i]);
  1095. if (IS_ERR(ctx->clocks[i])) {
  1096. dev_err(dev, "failed to get clock: %s\n",
  1097. ctx->clk_names[i]);
  1098. return PTR_ERR(ctx->clocks[i]);
  1099. }
  1100. }
  1101. /* resource memory */
  1102. ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1103. ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
  1104. if (IS_ERR(ctx->regs))
  1105. return PTR_ERR(ctx->regs);
  1106. /* resource irq */
  1107. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1108. if (!res) {
  1109. dev_err(dev, "failed to request irq resource.\n");
  1110. return -ENOENT;
  1111. }
  1112. ctx->irq = res->start;
  1113. ret = devm_request_irq(dev, ctx->irq, gsc_irq_handler, 0,
  1114. dev_name(dev), ctx);
  1115. if (ret < 0) {
  1116. dev_err(dev, "failed to request irq.\n");
  1117. return ret;
  1118. }
  1119. /* context initailization */
  1120. ctx->id = pdev->id;
  1121. platform_set_drvdata(pdev, ctx);
  1122. pm_runtime_use_autosuspend(dev);
  1123. pm_runtime_set_autosuspend_delay(dev, GSC_AUTOSUSPEND_DELAY);
  1124. pm_runtime_enable(dev);
  1125. ret = component_add(dev, &gsc_component_ops);
  1126. if (ret)
  1127. goto err_pm_dis;
  1128. dev_info(dev, "drm gsc registered successfully.\n");
  1129. return 0;
  1130. err_pm_dis:
  1131. pm_runtime_dont_use_autosuspend(dev);
  1132. pm_runtime_disable(dev);
  1133. return ret;
  1134. }
  1135. static int gsc_remove(struct platform_device *pdev)
  1136. {
  1137. struct device *dev = &pdev->dev;
  1138. pm_runtime_dont_use_autosuspend(dev);
  1139. pm_runtime_disable(dev);
  1140. return 0;
  1141. }
  1142. static int __maybe_unused gsc_runtime_suspend(struct device *dev)
  1143. {
  1144. struct gsc_context *ctx = get_gsc_context(dev);
  1145. int i;
  1146. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1147. for (i = ctx->num_clocks - 1; i >= 0; i--)
  1148. clk_disable_unprepare(ctx->clocks[i]);
  1149. return 0;
  1150. }
  1151. static int __maybe_unused gsc_runtime_resume(struct device *dev)
  1152. {
  1153. struct gsc_context *ctx = get_gsc_context(dev);
  1154. int i, ret;
  1155. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1156. for (i = 0; i < ctx->num_clocks; i++) {
  1157. ret = clk_prepare_enable(ctx->clocks[i]);
  1158. if (ret) {
  1159. while (--i > 0)
  1160. clk_disable_unprepare(ctx->clocks[i]);
  1161. return ret;
  1162. }
  1163. }
  1164. return 0;
  1165. }
  1166. static const struct dev_pm_ops gsc_pm_ops = {
  1167. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1168. pm_runtime_force_resume)
  1169. SET_RUNTIME_PM_OPS(gsc_runtime_suspend, gsc_runtime_resume, NULL)
  1170. };
  1171. static const struct drm_exynos_ipp_limit gsc_5250_limits[] = {
  1172. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 4800, 8 }, .v = { 16, 3344, 8 }) },
  1173. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 2 }, .v = { 8, 3344, 2 }) },
  1174. { IPP_SIZE_LIMIT(ROTATED, .h = { 32, 2048 }, .v = { 16, 2048 }) },
  1175. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1176. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1177. };
  1178. static const struct drm_exynos_ipp_limit gsc_5420_limits[] = {
  1179. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 4800, 8 }, .v = { 16, 3344, 8 }) },
  1180. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 2 }, .v = { 8, 3344, 2 }) },
  1181. { IPP_SIZE_LIMIT(ROTATED, .h = { 16, 2016 }, .v = { 8, 2016 }) },
  1182. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1183. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1184. };
  1185. static const struct drm_exynos_ipp_limit gsc_5433_limits[] = {
  1186. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 8191, 16 }, .v = { 16, 8191, 2 }) },
  1187. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 1 }, .v = { 8, 3344, 1 }) },
  1188. { IPP_SIZE_LIMIT(ROTATED, .h = { 32, 2047 }, .v = { 8, 8191 }) },
  1189. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1190. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1191. };
  1192. static struct gsc_driverdata gsc_exynos5250_drvdata = {
  1193. .clk_names = {"gscl"},
  1194. .num_clocks = 1,
  1195. .limits = gsc_5250_limits,
  1196. .num_limits = ARRAY_SIZE(gsc_5250_limits),
  1197. };
  1198. static struct gsc_driverdata gsc_exynos5420_drvdata = {
  1199. .clk_names = {"gscl"},
  1200. .num_clocks = 1,
  1201. .limits = gsc_5420_limits,
  1202. .num_limits = ARRAY_SIZE(gsc_5420_limits),
  1203. };
  1204. static struct gsc_driverdata gsc_exynos5433_drvdata = {
  1205. .clk_names = {"pclk", "aclk", "aclk_xiu", "aclk_gsclbend"},
  1206. .num_clocks = 4,
  1207. .limits = gsc_5433_limits,
  1208. .num_limits = ARRAY_SIZE(gsc_5433_limits),
  1209. };
  1210. static const struct of_device_id exynos_drm_gsc_of_match[] = {
  1211. {
  1212. .compatible = "samsung,exynos5-gsc",
  1213. .data = &gsc_exynos5250_drvdata,
  1214. }, {
  1215. .compatible = "samsung,exynos5250-gsc",
  1216. .data = &gsc_exynos5250_drvdata,
  1217. }, {
  1218. .compatible = "samsung,exynos5420-gsc",
  1219. .data = &gsc_exynos5420_drvdata,
  1220. }, {
  1221. .compatible = "samsung,exynos5433-gsc",
  1222. .data = &gsc_exynos5433_drvdata,
  1223. }, {
  1224. },
  1225. };
  1226. MODULE_DEVICE_TABLE(of, exynos_drm_gsc_of_match);
  1227. struct platform_driver gsc_driver = {
  1228. .probe = gsc_probe,
  1229. .remove = gsc_remove,
  1230. .driver = {
  1231. .name = "exynos-drm-gsc",
  1232. .owner = THIS_MODULE,
  1233. .pm = &gsc_pm_ops,
  1234. .of_match_table = of_match_ptr(exynos_drm_gsc_of_match),
  1235. },
  1236. };