exynos_drm_gsc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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)
  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. gsc_write(cfg, GSC_IN_CON);
  487. }
  488. static void gsc_src_set_transf(struct gsc_context *ctx, unsigned int rotation)
  489. {
  490. unsigned int degree = rotation & DRM_MODE_ROTATE_MASK;
  491. u32 cfg;
  492. cfg = gsc_read(GSC_IN_CON);
  493. cfg &= ~GSC_IN_ROT_MASK;
  494. switch (degree) {
  495. case DRM_MODE_ROTATE_0:
  496. if (rotation & DRM_MODE_REFLECT_X)
  497. cfg |= GSC_IN_ROT_XFLIP;
  498. if (rotation & DRM_MODE_REFLECT_Y)
  499. cfg |= GSC_IN_ROT_YFLIP;
  500. break;
  501. case DRM_MODE_ROTATE_90:
  502. cfg |= GSC_IN_ROT_90;
  503. if (rotation & DRM_MODE_REFLECT_X)
  504. cfg |= GSC_IN_ROT_XFLIP;
  505. if (rotation & DRM_MODE_REFLECT_Y)
  506. cfg |= GSC_IN_ROT_YFLIP;
  507. break;
  508. case DRM_MODE_ROTATE_180:
  509. cfg |= GSC_IN_ROT_180;
  510. if (rotation & DRM_MODE_REFLECT_X)
  511. cfg &= ~GSC_IN_ROT_XFLIP;
  512. if (rotation & DRM_MODE_REFLECT_Y)
  513. cfg &= ~GSC_IN_ROT_YFLIP;
  514. break;
  515. case DRM_MODE_ROTATE_270:
  516. cfg |= GSC_IN_ROT_270;
  517. if (rotation & DRM_MODE_REFLECT_X)
  518. cfg &= ~GSC_IN_ROT_XFLIP;
  519. if (rotation & DRM_MODE_REFLECT_Y)
  520. cfg &= ~GSC_IN_ROT_YFLIP;
  521. break;
  522. }
  523. gsc_write(cfg, GSC_IN_CON);
  524. ctx->rotation = (cfg & GSC_IN_ROT_90) ? 1 : 0;
  525. }
  526. static void gsc_src_set_size(struct gsc_context *ctx,
  527. struct exynos_drm_ipp_buffer *buf)
  528. {
  529. struct gsc_scaler *sc = &ctx->sc;
  530. u32 cfg;
  531. /* pixel offset */
  532. cfg = (GSC_SRCIMG_OFFSET_X(buf->rect.x) |
  533. GSC_SRCIMG_OFFSET_Y(buf->rect.y));
  534. gsc_write(cfg, GSC_SRCIMG_OFFSET);
  535. /* cropped size */
  536. cfg = (GSC_CROPPED_WIDTH(buf->rect.w) |
  537. GSC_CROPPED_HEIGHT(buf->rect.h));
  538. gsc_write(cfg, GSC_CROPPED_SIZE);
  539. /* original size */
  540. cfg = gsc_read(GSC_SRCIMG_SIZE);
  541. cfg &= ~(GSC_SRCIMG_HEIGHT_MASK |
  542. GSC_SRCIMG_WIDTH_MASK);
  543. cfg |= (GSC_SRCIMG_WIDTH(buf->buf.pitch[0] / buf->format->cpp[0]) |
  544. GSC_SRCIMG_HEIGHT(buf->buf.height));
  545. gsc_write(cfg, GSC_SRCIMG_SIZE);
  546. cfg = gsc_read(GSC_IN_CON);
  547. cfg &= ~GSC_IN_RGB_TYPE_MASK;
  548. if (buf->rect.w >= GSC_WIDTH_ITU_709)
  549. if (sc->range)
  550. cfg |= GSC_IN_RGB_HD_WIDE;
  551. else
  552. cfg |= GSC_IN_RGB_HD_NARROW;
  553. else
  554. if (sc->range)
  555. cfg |= GSC_IN_RGB_SD_WIDE;
  556. else
  557. cfg |= GSC_IN_RGB_SD_NARROW;
  558. gsc_write(cfg, GSC_IN_CON);
  559. }
  560. static void gsc_src_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  561. bool enqueue)
  562. {
  563. bool masked = !enqueue;
  564. u32 cfg;
  565. u32 mask = 0x00000001 << buf_id;
  566. /* mask register set */
  567. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  568. /* sequence id */
  569. cfg &= ~mask;
  570. cfg |= masked << buf_id;
  571. gsc_write(cfg, GSC_IN_BASE_ADDR_Y_MASK);
  572. gsc_write(cfg, GSC_IN_BASE_ADDR_CB_MASK);
  573. gsc_write(cfg, GSC_IN_BASE_ADDR_CR_MASK);
  574. }
  575. static void gsc_src_set_addr(struct gsc_context *ctx, u32 buf_id,
  576. struct exynos_drm_ipp_buffer *buf)
  577. {
  578. /* address register set */
  579. gsc_write(buf->dma_addr[0], GSC_IN_BASE_ADDR_Y(buf_id));
  580. gsc_write(buf->dma_addr[1], GSC_IN_BASE_ADDR_CB(buf_id));
  581. gsc_write(buf->dma_addr[2], GSC_IN_BASE_ADDR_CR(buf_id));
  582. gsc_src_set_buf_seq(ctx, buf_id, true);
  583. }
  584. static void gsc_dst_set_fmt(struct gsc_context *ctx, u32 fmt)
  585. {
  586. u32 cfg;
  587. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  588. cfg = gsc_read(GSC_OUT_CON);
  589. cfg &= ~(GSC_OUT_RGB_TYPE_MASK | GSC_OUT_YUV422_1P_ORDER_MASK |
  590. GSC_OUT_CHROMA_ORDER_MASK | GSC_OUT_FORMAT_MASK |
  591. GSC_OUT_CHROM_STRIDE_SEL_MASK | GSC_OUT_RB_SWAP_MASK |
  592. GSC_OUT_GLOBAL_ALPHA_MASK);
  593. switch (fmt) {
  594. case DRM_FORMAT_RGB565:
  595. cfg |= GSC_OUT_RGB565;
  596. break;
  597. case DRM_FORMAT_ARGB8888:
  598. case DRM_FORMAT_XRGB8888:
  599. cfg |= (GSC_OUT_XRGB8888 | GSC_OUT_GLOBAL_ALPHA(0xff));
  600. break;
  601. case DRM_FORMAT_BGRX8888:
  602. cfg |= (GSC_OUT_XRGB8888 | GSC_OUT_RB_SWAP);
  603. break;
  604. case DRM_FORMAT_YUYV:
  605. cfg |= (GSC_OUT_YUV422_1P |
  606. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  607. GSC_OUT_CHROMA_ORDER_CBCR);
  608. break;
  609. case DRM_FORMAT_YVYU:
  610. cfg |= (GSC_OUT_YUV422_1P |
  611. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  612. GSC_OUT_CHROMA_ORDER_CRCB);
  613. break;
  614. case DRM_FORMAT_UYVY:
  615. cfg |= (GSC_OUT_YUV422_1P |
  616. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  617. GSC_OUT_CHROMA_ORDER_CBCR);
  618. break;
  619. case DRM_FORMAT_VYUY:
  620. cfg |= (GSC_OUT_YUV422_1P |
  621. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  622. GSC_OUT_CHROMA_ORDER_CRCB);
  623. break;
  624. case DRM_FORMAT_NV21:
  625. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV420_2P);
  626. break;
  627. case DRM_FORMAT_NV61:
  628. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV422_2P);
  629. break;
  630. case DRM_FORMAT_YUV422:
  631. cfg |= GSC_OUT_YUV422_3P;
  632. break;
  633. case DRM_FORMAT_YUV420:
  634. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV420_3P);
  635. break;
  636. case DRM_FORMAT_YVU420:
  637. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV420_3P);
  638. break;
  639. case DRM_FORMAT_NV12:
  640. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV420_2P);
  641. break;
  642. case DRM_FORMAT_NV16:
  643. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR | GSC_OUT_YUV422_2P);
  644. break;
  645. }
  646. gsc_write(cfg, GSC_OUT_CON);
  647. }
  648. static int gsc_get_ratio_shift(u32 src, u32 dst, u32 *ratio)
  649. {
  650. DRM_DEBUG_KMS("src[%d]dst[%d]\n", src, dst);
  651. if (src >= dst * 8) {
  652. DRM_ERROR("failed to make ratio and shift.\n");
  653. return -EINVAL;
  654. } else if (src >= dst * 4)
  655. *ratio = 4;
  656. else if (src >= dst * 2)
  657. *ratio = 2;
  658. else
  659. *ratio = 1;
  660. return 0;
  661. }
  662. static void gsc_get_prescaler_shfactor(u32 hratio, u32 vratio, u32 *shfactor)
  663. {
  664. if (hratio == 4 && vratio == 4)
  665. *shfactor = 4;
  666. else if ((hratio == 4 && vratio == 2) ||
  667. (hratio == 2 && vratio == 4))
  668. *shfactor = 3;
  669. else if ((hratio == 4 && vratio == 1) ||
  670. (hratio == 1 && vratio == 4) ||
  671. (hratio == 2 && vratio == 2))
  672. *shfactor = 2;
  673. else if (hratio == 1 && vratio == 1)
  674. *shfactor = 0;
  675. else
  676. *shfactor = 1;
  677. }
  678. static int gsc_set_prescaler(struct gsc_context *ctx, struct gsc_scaler *sc,
  679. struct drm_exynos_ipp_task_rect *src,
  680. struct drm_exynos_ipp_task_rect *dst)
  681. {
  682. u32 cfg;
  683. u32 src_w, src_h, dst_w, dst_h;
  684. int ret = 0;
  685. src_w = src->w;
  686. src_h = src->h;
  687. if (ctx->rotation) {
  688. dst_w = dst->h;
  689. dst_h = dst->w;
  690. } else {
  691. dst_w = dst->w;
  692. dst_h = dst->h;
  693. }
  694. ret = gsc_get_ratio_shift(src_w, dst_w, &sc->pre_hratio);
  695. if (ret) {
  696. dev_err(ctx->dev, "failed to get ratio horizontal.\n");
  697. return ret;
  698. }
  699. ret = gsc_get_ratio_shift(src_h, dst_h, &sc->pre_vratio);
  700. if (ret) {
  701. dev_err(ctx->dev, "failed to get ratio vertical.\n");
  702. return ret;
  703. }
  704. DRM_DEBUG_KMS("pre_hratio[%d]pre_vratio[%d]\n",
  705. sc->pre_hratio, sc->pre_vratio);
  706. sc->main_hratio = (src_w << 16) / dst_w;
  707. sc->main_vratio = (src_h << 16) / dst_h;
  708. DRM_DEBUG_KMS("main_hratio[%ld]main_vratio[%ld]\n",
  709. sc->main_hratio, sc->main_vratio);
  710. gsc_get_prescaler_shfactor(sc->pre_hratio, sc->pre_vratio,
  711. &sc->pre_shfactor);
  712. DRM_DEBUG_KMS("pre_shfactor[%d]\n", sc->pre_shfactor);
  713. cfg = (GSC_PRESC_SHFACTOR(sc->pre_shfactor) |
  714. GSC_PRESC_H_RATIO(sc->pre_hratio) |
  715. GSC_PRESC_V_RATIO(sc->pre_vratio));
  716. gsc_write(cfg, GSC_PRE_SCALE_RATIO);
  717. return ret;
  718. }
  719. static void gsc_set_h_coef(struct gsc_context *ctx, unsigned long main_hratio)
  720. {
  721. int i, j, k, sc_ratio;
  722. if (main_hratio <= GSC_SC_UP_MAX_RATIO)
  723. sc_ratio = 0;
  724. else if (main_hratio <= GSC_SC_DOWN_RATIO_7_8)
  725. sc_ratio = 1;
  726. else if (main_hratio <= GSC_SC_DOWN_RATIO_6_8)
  727. sc_ratio = 2;
  728. else if (main_hratio <= GSC_SC_DOWN_RATIO_5_8)
  729. sc_ratio = 3;
  730. else if (main_hratio <= GSC_SC_DOWN_RATIO_4_8)
  731. sc_ratio = 4;
  732. else if (main_hratio <= GSC_SC_DOWN_RATIO_3_8)
  733. sc_ratio = 5;
  734. else
  735. sc_ratio = 6;
  736. for (i = 0; i < GSC_COEF_PHASE; i++)
  737. for (j = 0; j < GSC_COEF_H_8T; j++)
  738. for (k = 0; k < GSC_COEF_DEPTH; k++)
  739. gsc_write(h_coef_8t[sc_ratio][i][j],
  740. GSC_HCOEF(i, j, k));
  741. }
  742. static void gsc_set_v_coef(struct gsc_context *ctx, unsigned long main_vratio)
  743. {
  744. int i, j, k, sc_ratio;
  745. if (main_vratio <= GSC_SC_UP_MAX_RATIO)
  746. sc_ratio = 0;
  747. else if (main_vratio <= GSC_SC_DOWN_RATIO_7_8)
  748. sc_ratio = 1;
  749. else if (main_vratio <= GSC_SC_DOWN_RATIO_6_8)
  750. sc_ratio = 2;
  751. else if (main_vratio <= GSC_SC_DOWN_RATIO_5_8)
  752. sc_ratio = 3;
  753. else if (main_vratio <= GSC_SC_DOWN_RATIO_4_8)
  754. sc_ratio = 4;
  755. else if (main_vratio <= GSC_SC_DOWN_RATIO_3_8)
  756. sc_ratio = 5;
  757. else
  758. sc_ratio = 6;
  759. for (i = 0; i < GSC_COEF_PHASE; i++)
  760. for (j = 0; j < GSC_COEF_V_4T; j++)
  761. for (k = 0; k < GSC_COEF_DEPTH; k++)
  762. gsc_write(v_coef_4t[sc_ratio][i][j],
  763. GSC_VCOEF(i, j, k));
  764. }
  765. static void gsc_set_scaler(struct gsc_context *ctx, struct gsc_scaler *sc)
  766. {
  767. u32 cfg;
  768. DRM_DEBUG_KMS("main_hratio[%ld]main_vratio[%ld]\n",
  769. sc->main_hratio, sc->main_vratio);
  770. gsc_set_h_coef(ctx, sc->main_hratio);
  771. cfg = GSC_MAIN_H_RATIO_VALUE(sc->main_hratio);
  772. gsc_write(cfg, GSC_MAIN_H_RATIO);
  773. gsc_set_v_coef(ctx, sc->main_vratio);
  774. cfg = GSC_MAIN_V_RATIO_VALUE(sc->main_vratio);
  775. gsc_write(cfg, GSC_MAIN_V_RATIO);
  776. }
  777. static void gsc_dst_set_size(struct gsc_context *ctx,
  778. struct exynos_drm_ipp_buffer *buf)
  779. {
  780. struct gsc_scaler *sc = &ctx->sc;
  781. u32 cfg;
  782. /* pixel offset */
  783. cfg = (GSC_DSTIMG_OFFSET_X(buf->rect.x) |
  784. GSC_DSTIMG_OFFSET_Y(buf->rect.y));
  785. gsc_write(cfg, GSC_DSTIMG_OFFSET);
  786. /* scaled size */
  787. if (ctx->rotation)
  788. cfg = (GSC_SCALED_WIDTH(buf->rect.h) |
  789. GSC_SCALED_HEIGHT(buf->rect.w));
  790. else
  791. cfg = (GSC_SCALED_WIDTH(buf->rect.w) |
  792. GSC_SCALED_HEIGHT(buf->rect.h));
  793. gsc_write(cfg, GSC_SCALED_SIZE);
  794. /* original size */
  795. cfg = gsc_read(GSC_DSTIMG_SIZE);
  796. cfg &= ~(GSC_DSTIMG_HEIGHT_MASK | GSC_DSTIMG_WIDTH_MASK);
  797. cfg |= GSC_DSTIMG_WIDTH(buf->buf.pitch[0] / buf->format->cpp[0]) |
  798. GSC_DSTIMG_HEIGHT(buf->buf.height);
  799. gsc_write(cfg, GSC_DSTIMG_SIZE);
  800. cfg = gsc_read(GSC_OUT_CON);
  801. cfg &= ~GSC_OUT_RGB_TYPE_MASK;
  802. if (buf->rect.w >= GSC_WIDTH_ITU_709)
  803. if (sc->range)
  804. cfg |= GSC_OUT_RGB_HD_WIDE;
  805. else
  806. cfg |= GSC_OUT_RGB_HD_NARROW;
  807. else
  808. if (sc->range)
  809. cfg |= GSC_OUT_RGB_SD_WIDE;
  810. else
  811. cfg |= GSC_OUT_RGB_SD_NARROW;
  812. gsc_write(cfg, GSC_OUT_CON);
  813. }
  814. static int gsc_dst_get_buf_seq(struct gsc_context *ctx)
  815. {
  816. u32 cfg, i, buf_num = GSC_REG_SZ;
  817. u32 mask = 0x00000001;
  818. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  819. for (i = 0; i < GSC_REG_SZ; i++)
  820. if (cfg & (mask << i))
  821. buf_num--;
  822. DRM_DEBUG_KMS("buf_num[%d]\n", buf_num);
  823. return buf_num;
  824. }
  825. static void gsc_dst_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  826. bool enqueue)
  827. {
  828. bool masked = !enqueue;
  829. u32 cfg;
  830. u32 mask = 0x00000001 << buf_id;
  831. /* mask register set */
  832. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  833. /* sequence id */
  834. cfg &= ~mask;
  835. cfg |= masked << buf_id;
  836. gsc_write(cfg, GSC_OUT_BASE_ADDR_Y_MASK);
  837. gsc_write(cfg, GSC_OUT_BASE_ADDR_CB_MASK);
  838. gsc_write(cfg, GSC_OUT_BASE_ADDR_CR_MASK);
  839. /* interrupt enable */
  840. if (enqueue && gsc_dst_get_buf_seq(ctx) >= GSC_BUF_START)
  841. gsc_handle_irq(ctx, true, false, true);
  842. /* interrupt disable */
  843. if (!enqueue && gsc_dst_get_buf_seq(ctx) <= GSC_BUF_STOP)
  844. gsc_handle_irq(ctx, false, false, true);
  845. }
  846. static void gsc_dst_set_addr(struct gsc_context *ctx,
  847. u32 buf_id, struct exynos_drm_ipp_buffer *buf)
  848. {
  849. /* address register set */
  850. gsc_write(buf->dma_addr[0], GSC_OUT_BASE_ADDR_Y(buf_id));
  851. gsc_write(buf->dma_addr[1], GSC_OUT_BASE_ADDR_CB(buf_id));
  852. gsc_write(buf->dma_addr[2], GSC_OUT_BASE_ADDR_CR(buf_id));
  853. gsc_dst_set_buf_seq(ctx, buf_id, true);
  854. }
  855. static int gsc_get_src_buf_index(struct gsc_context *ctx)
  856. {
  857. u32 cfg, curr_index, i;
  858. u32 buf_id = GSC_MAX_SRC;
  859. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  860. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  861. curr_index = GSC_IN_CURR_GET_INDEX(cfg);
  862. for (i = curr_index; i < GSC_MAX_SRC; i++) {
  863. if (!((cfg >> i) & 0x1)) {
  864. buf_id = i;
  865. break;
  866. }
  867. }
  868. DRM_DEBUG_KMS("cfg[0x%x]curr_index[%d]buf_id[%d]\n", cfg,
  869. curr_index, buf_id);
  870. if (buf_id == GSC_MAX_SRC) {
  871. DRM_ERROR("failed to get in buffer index.\n");
  872. return -EINVAL;
  873. }
  874. gsc_src_set_buf_seq(ctx, buf_id, false);
  875. return buf_id;
  876. }
  877. static int gsc_get_dst_buf_index(struct gsc_context *ctx)
  878. {
  879. u32 cfg, curr_index, i;
  880. u32 buf_id = GSC_MAX_DST;
  881. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  882. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  883. curr_index = GSC_OUT_CURR_GET_INDEX(cfg);
  884. for (i = curr_index; i < GSC_MAX_DST; i++) {
  885. if (!((cfg >> i) & 0x1)) {
  886. buf_id = i;
  887. break;
  888. }
  889. }
  890. if (buf_id == GSC_MAX_DST) {
  891. DRM_ERROR("failed to get out buffer index.\n");
  892. return -EINVAL;
  893. }
  894. gsc_dst_set_buf_seq(ctx, buf_id, false);
  895. DRM_DEBUG_KMS("cfg[0x%x]curr_index[%d]buf_id[%d]\n", cfg,
  896. curr_index, buf_id);
  897. return buf_id;
  898. }
  899. static irqreturn_t gsc_irq_handler(int irq, void *dev_id)
  900. {
  901. struct gsc_context *ctx = dev_id;
  902. u32 status;
  903. int err = 0;
  904. DRM_DEBUG_KMS("gsc id[%d]\n", ctx->id);
  905. status = gsc_read(GSC_IRQ);
  906. if (status & GSC_IRQ_STATUS_OR_IRQ) {
  907. dev_err(ctx->dev, "occurred overflow at %d, status 0x%x.\n",
  908. ctx->id, status);
  909. err = -EINVAL;
  910. }
  911. if (status & GSC_IRQ_STATUS_OR_FRM_DONE) {
  912. int src_buf_id, dst_buf_id;
  913. dev_dbg(ctx->dev, "occurred frame done at %d, status 0x%x.\n",
  914. ctx->id, status);
  915. src_buf_id = gsc_get_src_buf_index(ctx);
  916. dst_buf_id = gsc_get_dst_buf_index(ctx);
  917. DRM_DEBUG_KMS("buf_id_src[%d]buf_id_dst[%d]\n", src_buf_id,
  918. dst_buf_id);
  919. if (src_buf_id < 0 || dst_buf_id < 0)
  920. err = -EINVAL;
  921. }
  922. if (ctx->task) {
  923. struct exynos_drm_ipp_task *task = ctx->task;
  924. ctx->task = NULL;
  925. pm_runtime_mark_last_busy(ctx->dev);
  926. pm_runtime_put_autosuspend(ctx->dev);
  927. exynos_drm_ipp_task_done(task, err);
  928. }
  929. return IRQ_HANDLED;
  930. }
  931. static int gsc_reset(struct gsc_context *ctx)
  932. {
  933. struct gsc_scaler *sc = &ctx->sc;
  934. int ret;
  935. /* reset h/w block */
  936. ret = gsc_sw_reset(ctx);
  937. if (ret < 0) {
  938. dev_err(ctx->dev, "failed to reset hardware.\n");
  939. return ret;
  940. }
  941. /* scaler setting */
  942. memset(&ctx->sc, 0x0, sizeof(ctx->sc));
  943. sc->range = true;
  944. return 0;
  945. }
  946. static void gsc_start(struct gsc_context *ctx)
  947. {
  948. u32 cfg;
  949. gsc_handle_irq(ctx, true, false, true);
  950. /* enable one shot */
  951. cfg = gsc_read(GSC_ENABLE);
  952. cfg &= ~(GSC_ENABLE_ON_CLEAR_MASK |
  953. GSC_ENABLE_CLK_GATE_MODE_MASK);
  954. cfg |= GSC_ENABLE_ON_CLEAR_ONESHOT;
  955. gsc_write(cfg, GSC_ENABLE);
  956. /* src dma memory */
  957. cfg = gsc_read(GSC_IN_CON);
  958. cfg &= ~(GSC_IN_PATH_MASK | GSC_IN_LOCAL_SEL_MASK);
  959. cfg |= GSC_IN_PATH_MEMORY;
  960. gsc_write(cfg, GSC_IN_CON);
  961. /* dst dma memory */
  962. cfg = gsc_read(GSC_OUT_CON);
  963. cfg |= GSC_OUT_PATH_MEMORY;
  964. gsc_write(cfg, GSC_OUT_CON);
  965. gsc_set_scaler(ctx, &ctx->sc);
  966. cfg = gsc_read(GSC_ENABLE);
  967. cfg |= GSC_ENABLE_ON;
  968. gsc_write(cfg, GSC_ENABLE);
  969. }
  970. static int gsc_commit(struct exynos_drm_ipp *ipp,
  971. struct exynos_drm_ipp_task *task)
  972. {
  973. struct gsc_context *ctx = container_of(ipp, struct gsc_context, ipp);
  974. int ret;
  975. pm_runtime_get_sync(ctx->dev);
  976. ctx->task = task;
  977. ret = gsc_reset(ctx);
  978. if (ret) {
  979. pm_runtime_put_autosuspend(ctx->dev);
  980. ctx->task = NULL;
  981. return ret;
  982. }
  983. gsc_src_set_fmt(ctx, task->src.buf.fourcc);
  984. gsc_src_set_transf(ctx, task->transform.rotation);
  985. gsc_src_set_size(ctx, &task->src);
  986. gsc_src_set_addr(ctx, 0, &task->src);
  987. gsc_dst_set_fmt(ctx, task->dst.buf.fourcc);
  988. gsc_dst_set_size(ctx, &task->dst);
  989. gsc_dst_set_addr(ctx, 0, &task->dst);
  990. gsc_set_prescaler(ctx, &ctx->sc, &task->src.rect, &task->dst.rect);
  991. gsc_start(ctx);
  992. return 0;
  993. }
  994. static void gsc_abort(struct exynos_drm_ipp *ipp,
  995. struct exynos_drm_ipp_task *task)
  996. {
  997. struct gsc_context *ctx =
  998. container_of(ipp, struct gsc_context, ipp);
  999. gsc_reset(ctx);
  1000. if (ctx->task) {
  1001. struct exynos_drm_ipp_task *task = ctx->task;
  1002. ctx->task = NULL;
  1003. pm_runtime_mark_last_busy(ctx->dev);
  1004. pm_runtime_put_autosuspend(ctx->dev);
  1005. exynos_drm_ipp_task_done(task, -EIO);
  1006. }
  1007. }
  1008. static struct exynos_drm_ipp_funcs ipp_funcs = {
  1009. .commit = gsc_commit,
  1010. .abort = gsc_abort,
  1011. };
  1012. static int gsc_bind(struct device *dev, struct device *master, void *data)
  1013. {
  1014. struct gsc_context *ctx = dev_get_drvdata(dev);
  1015. struct drm_device *drm_dev = data;
  1016. struct exynos_drm_ipp *ipp = &ctx->ipp;
  1017. ctx->drm_dev = drm_dev;
  1018. drm_iommu_attach_device(drm_dev, dev);
  1019. exynos_drm_ipp_register(drm_dev, ipp, &ipp_funcs,
  1020. DRM_EXYNOS_IPP_CAP_CROP | DRM_EXYNOS_IPP_CAP_ROTATE |
  1021. DRM_EXYNOS_IPP_CAP_SCALE | DRM_EXYNOS_IPP_CAP_CONVERT,
  1022. ctx->formats, ctx->num_formats, "gsc");
  1023. dev_info(dev, "The exynos gscaler has been probed successfully\n");
  1024. return 0;
  1025. }
  1026. static void gsc_unbind(struct device *dev, struct device *master,
  1027. void *data)
  1028. {
  1029. struct gsc_context *ctx = dev_get_drvdata(dev);
  1030. struct drm_device *drm_dev = data;
  1031. struct exynos_drm_ipp *ipp = &ctx->ipp;
  1032. exynos_drm_ipp_unregister(drm_dev, ipp);
  1033. drm_iommu_detach_device(drm_dev, dev);
  1034. }
  1035. static const struct component_ops gsc_component_ops = {
  1036. .bind = gsc_bind,
  1037. .unbind = gsc_unbind,
  1038. };
  1039. static const unsigned int gsc_formats[] = {
  1040. DRM_FORMAT_ARGB8888,
  1041. DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB565, DRM_FORMAT_BGRX8888,
  1042. DRM_FORMAT_NV12, DRM_FORMAT_NV16, DRM_FORMAT_NV21, DRM_FORMAT_NV61,
  1043. DRM_FORMAT_UYVY, DRM_FORMAT_VYUY, DRM_FORMAT_YUYV, DRM_FORMAT_YVYU,
  1044. DRM_FORMAT_YUV420, DRM_FORMAT_YVU420, DRM_FORMAT_YUV422,
  1045. };
  1046. static int gsc_probe(struct platform_device *pdev)
  1047. {
  1048. struct device *dev = &pdev->dev;
  1049. struct gsc_driverdata *driver_data;
  1050. struct exynos_drm_ipp_formats *formats;
  1051. struct gsc_context *ctx;
  1052. struct resource *res;
  1053. int ret, i;
  1054. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1055. if (!ctx)
  1056. return -ENOMEM;
  1057. formats = devm_kcalloc(dev,
  1058. ARRAY_SIZE(gsc_formats), sizeof(*formats),
  1059. GFP_KERNEL);
  1060. if (!formats)
  1061. return -ENOMEM;
  1062. driver_data = (struct gsc_driverdata *)of_device_get_match_data(dev);
  1063. ctx->dev = dev;
  1064. ctx->num_clocks = driver_data->num_clocks;
  1065. ctx->clk_names = driver_data->clk_names;
  1066. for (i = 0; i < ARRAY_SIZE(gsc_formats); i++) {
  1067. formats[i].fourcc = gsc_formats[i];
  1068. formats[i].type = DRM_EXYNOS_IPP_FORMAT_SOURCE |
  1069. DRM_EXYNOS_IPP_FORMAT_DESTINATION;
  1070. formats[i].limits = driver_data->limits;
  1071. formats[i].num_limits = driver_data->num_limits;
  1072. }
  1073. ctx->formats = formats;
  1074. ctx->num_formats = ARRAY_SIZE(gsc_formats);
  1075. /* clock control */
  1076. for (i = 0; i < ctx->num_clocks; i++) {
  1077. ctx->clocks[i] = devm_clk_get(dev, ctx->clk_names[i]);
  1078. if (IS_ERR(ctx->clocks[i])) {
  1079. dev_err(dev, "failed to get clock: %s\n",
  1080. ctx->clk_names[i]);
  1081. return PTR_ERR(ctx->clocks[i]);
  1082. }
  1083. }
  1084. /* resource memory */
  1085. ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1086. ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
  1087. if (IS_ERR(ctx->regs))
  1088. return PTR_ERR(ctx->regs);
  1089. /* resource irq */
  1090. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1091. if (!res) {
  1092. dev_err(dev, "failed to request irq resource.\n");
  1093. return -ENOENT;
  1094. }
  1095. ctx->irq = res->start;
  1096. ret = devm_request_irq(dev, ctx->irq, gsc_irq_handler, 0,
  1097. dev_name(dev), ctx);
  1098. if (ret < 0) {
  1099. dev_err(dev, "failed to request irq.\n");
  1100. return ret;
  1101. }
  1102. /* context initailization */
  1103. ctx->id = pdev->id;
  1104. platform_set_drvdata(pdev, ctx);
  1105. pm_runtime_use_autosuspend(dev);
  1106. pm_runtime_set_autosuspend_delay(dev, GSC_AUTOSUSPEND_DELAY);
  1107. pm_runtime_enable(dev);
  1108. ret = component_add(dev, &gsc_component_ops);
  1109. if (ret)
  1110. goto err_pm_dis;
  1111. dev_info(dev, "drm gsc registered successfully.\n");
  1112. return 0;
  1113. err_pm_dis:
  1114. pm_runtime_dont_use_autosuspend(dev);
  1115. pm_runtime_disable(dev);
  1116. return ret;
  1117. }
  1118. static int gsc_remove(struct platform_device *pdev)
  1119. {
  1120. struct device *dev = &pdev->dev;
  1121. component_del(dev, &gsc_component_ops);
  1122. pm_runtime_dont_use_autosuspend(dev);
  1123. pm_runtime_disable(dev);
  1124. return 0;
  1125. }
  1126. static int __maybe_unused gsc_runtime_suspend(struct device *dev)
  1127. {
  1128. struct gsc_context *ctx = get_gsc_context(dev);
  1129. int i;
  1130. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1131. for (i = ctx->num_clocks - 1; i >= 0; i--)
  1132. clk_disable_unprepare(ctx->clocks[i]);
  1133. return 0;
  1134. }
  1135. static int __maybe_unused gsc_runtime_resume(struct device *dev)
  1136. {
  1137. struct gsc_context *ctx = get_gsc_context(dev);
  1138. int i, ret;
  1139. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1140. for (i = 0; i < ctx->num_clocks; i++) {
  1141. ret = clk_prepare_enable(ctx->clocks[i]);
  1142. if (ret) {
  1143. while (--i > 0)
  1144. clk_disable_unprepare(ctx->clocks[i]);
  1145. return ret;
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. static const struct dev_pm_ops gsc_pm_ops = {
  1151. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1152. pm_runtime_force_resume)
  1153. SET_RUNTIME_PM_OPS(gsc_runtime_suspend, gsc_runtime_resume, NULL)
  1154. };
  1155. static const struct drm_exynos_ipp_limit gsc_5250_limits[] = {
  1156. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 4800, 8 }, .v = { 16, 3344, 8 }) },
  1157. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 2 }, .v = { 8, 3344, 2 }) },
  1158. { IPP_SIZE_LIMIT(ROTATED, .h = { 32, 2048 }, .v = { 16, 2048 }) },
  1159. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1160. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1161. };
  1162. static const struct drm_exynos_ipp_limit gsc_5420_limits[] = {
  1163. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 4800, 8 }, .v = { 16, 3344, 8 }) },
  1164. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 2 }, .v = { 8, 3344, 2 }) },
  1165. { IPP_SIZE_LIMIT(ROTATED, .h = { 16, 2016 }, .v = { 8, 2016 }) },
  1166. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1167. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1168. };
  1169. static const struct drm_exynos_ipp_limit gsc_5433_limits[] = {
  1170. { IPP_SIZE_LIMIT(BUFFER, .h = { 32, 8191, 16 }, .v = { 16, 8191, 2 }) },
  1171. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4800, 1 }, .v = { 8, 3344, 1 }) },
  1172. { IPP_SIZE_LIMIT(ROTATED, .h = { 32, 2047 }, .v = { 8, 8191 }) },
  1173. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 16, (1 << 16) * 8 },
  1174. .v = { (1 << 16) / 16, (1 << 16) * 8 }) },
  1175. };
  1176. static struct gsc_driverdata gsc_exynos5250_drvdata = {
  1177. .clk_names = {"gscl"},
  1178. .num_clocks = 1,
  1179. .limits = gsc_5250_limits,
  1180. .num_limits = ARRAY_SIZE(gsc_5250_limits),
  1181. };
  1182. static struct gsc_driverdata gsc_exynos5420_drvdata = {
  1183. .clk_names = {"gscl"},
  1184. .num_clocks = 1,
  1185. .limits = gsc_5420_limits,
  1186. .num_limits = ARRAY_SIZE(gsc_5420_limits),
  1187. };
  1188. static struct gsc_driverdata gsc_exynos5433_drvdata = {
  1189. .clk_names = {"pclk", "aclk", "aclk_xiu", "aclk_gsclbend"},
  1190. .num_clocks = 4,
  1191. .limits = gsc_5433_limits,
  1192. .num_limits = ARRAY_SIZE(gsc_5433_limits),
  1193. };
  1194. static const struct of_device_id exynos_drm_gsc_of_match[] = {
  1195. {
  1196. .compatible = "samsung,exynos5-gsc",
  1197. .data = &gsc_exynos5250_drvdata,
  1198. }, {
  1199. .compatible = "samsung,exynos5250-gsc",
  1200. .data = &gsc_exynos5250_drvdata,
  1201. }, {
  1202. .compatible = "samsung,exynos5420-gsc",
  1203. .data = &gsc_exynos5420_drvdata,
  1204. }, {
  1205. .compatible = "samsung,exynos5433-gsc",
  1206. .data = &gsc_exynos5433_drvdata,
  1207. }, {
  1208. },
  1209. };
  1210. MODULE_DEVICE_TABLE(of, exynos_drm_gsc_of_match);
  1211. struct platform_driver gsc_driver = {
  1212. .probe = gsc_probe,
  1213. .remove = gsc_remove,
  1214. .driver = {
  1215. .name = "exynos-drm-gsc",
  1216. .owner = THIS_MODULE,
  1217. .pm = &gsc_pm_ops,
  1218. .of_match_table = of_match_ptr(exynos_drm_gsc_of_match),
  1219. },
  1220. };