exynos_drm_gsc.c 36 KB

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