exynos_drm_gsc.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/pm_runtime.h>
  19. #include <plat/map-base.h>
  20. #include <drm/drmP.h>
  21. #include <drm/exynos_drm.h>
  22. #include "regs-gsc.h"
  23. #include "exynos_drm_ipp.h"
  24. #include "exynos_drm_gsc.h"
  25. /*
  26. * GSC stands for General SCaler and
  27. * supports image scaler/rotator and input/output DMA operations.
  28. * input DMA reads image data from the memory.
  29. * output DMA writes image data to memory.
  30. * GSC supports image rotation and image effect functions.
  31. *
  32. * M2M operation : supports crop/scale/rotation/csc so on.
  33. * Memory ----> GSC H/W ----> Memory.
  34. * Writeback operation : supports cloned screen with FIMD.
  35. * FIMD ----> GSC H/W ----> Memory.
  36. * Output operation : supports direct display using local path.
  37. * Memory ----> GSC H/W ----> FIMD, Mixer.
  38. */
  39. /*
  40. * TODO
  41. * 1. check suspend/resume api if needed.
  42. * 2. need to check use case platform_device_id.
  43. * 3. check src/dst size with, height.
  44. * 4. added check_prepare api for right register.
  45. * 5. need to add supported list in prop_list.
  46. * 6. check prescaler/scaler optimization.
  47. */
  48. #define GSC_MAX_DEVS 4
  49. #define GSC_MAX_SRC 4
  50. #define GSC_MAX_DST 16
  51. #define GSC_RESET_TIMEOUT 50
  52. #define GSC_BUF_STOP 1
  53. #define GSC_BUF_START 2
  54. #define GSC_REG_SZ 16
  55. #define GSC_WIDTH_ITU_709 1280
  56. #define GSC_SC_UP_MAX_RATIO 65536
  57. #define GSC_SC_DOWN_RATIO_7_8 74898
  58. #define GSC_SC_DOWN_RATIO_6_8 87381
  59. #define GSC_SC_DOWN_RATIO_5_8 104857
  60. #define GSC_SC_DOWN_RATIO_4_8 131072
  61. #define GSC_SC_DOWN_RATIO_3_8 174762
  62. #define GSC_SC_DOWN_RATIO_2_8 262144
  63. #define GSC_REFRESH_MIN 12
  64. #define GSC_REFRESH_MAX 60
  65. #define GSC_CROP_MAX 8192
  66. #define GSC_CROP_MIN 32
  67. #define GSC_SCALE_MAX 4224
  68. #define GSC_SCALE_MIN 32
  69. #define GSC_COEF_RATIO 7
  70. #define GSC_COEF_PHASE 9
  71. #define GSC_COEF_ATTR 16
  72. #define GSC_COEF_H_8T 8
  73. #define GSC_COEF_V_4T 4
  74. #define GSC_COEF_DEPTH 3
  75. #define get_gsc_context(dev) platform_get_drvdata(to_platform_device(dev))
  76. #define get_ctx_from_ippdrv(ippdrv) container_of(ippdrv,\
  77. struct gsc_context, ippdrv);
  78. #define gsc_read(offset) readl(ctx->regs + (offset))
  79. #define gsc_write(cfg, offset) writel(cfg, ctx->regs + (offset))
  80. /*
  81. * A structure of scaler.
  82. *
  83. * @range: narrow, wide.
  84. * @pre_shfactor: pre sclaer shift factor.
  85. * @pre_hratio: horizontal ratio of the prescaler.
  86. * @pre_vratio: vertical ratio of the prescaler.
  87. * @main_hratio: the main scaler's horizontal ratio.
  88. * @main_vratio: the main scaler's vertical ratio.
  89. */
  90. struct gsc_scaler {
  91. bool range;
  92. u32 pre_shfactor;
  93. u32 pre_hratio;
  94. u32 pre_vratio;
  95. unsigned long main_hratio;
  96. unsigned long main_vratio;
  97. };
  98. /*
  99. * A structure of scaler capability.
  100. *
  101. * find user manual 49.2 features.
  102. * @tile_w: tile mode or rotation width.
  103. * @tile_h: tile mode or rotation height.
  104. * @w: other cases width.
  105. * @h: other cases height.
  106. */
  107. struct gsc_capability {
  108. /* tile or rotation */
  109. u32 tile_w;
  110. u32 tile_h;
  111. /* other cases */
  112. u32 w;
  113. u32 h;
  114. };
  115. /*
  116. * A structure of gsc context.
  117. *
  118. * @ippdrv: prepare initialization using ippdrv.
  119. * @regs_res: register resources.
  120. * @regs: memory mapped io registers.
  121. * @lock: locking of operations.
  122. * @gsc_clk: gsc gate clock.
  123. * @sc: scaler infomations.
  124. * @id: gsc id.
  125. * @irq: irq number.
  126. * @rotation: supports rotation of src.
  127. * @suspended: qos operations.
  128. */
  129. struct gsc_context {
  130. struct exynos_drm_ippdrv ippdrv;
  131. struct resource *regs_res;
  132. void __iomem *regs;
  133. struct mutex lock;
  134. struct clk *gsc_clk;
  135. struct gsc_scaler sc;
  136. int id;
  137. int irq;
  138. bool rotation;
  139. bool suspended;
  140. };
  141. /* 8-tap Filter Coefficient */
  142. static const int h_coef_8t[GSC_COEF_RATIO][GSC_COEF_ATTR][GSC_COEF_H_8T] = {
  143. { /* Ratio <= 65536 (~8:8) */
  144. { 0, 0, 0, 128, 0, 0, 0, 0 },
  145. { -1, 2, -6, 127, 7, -2, 1, 0 },
  146. { -1, 4, -12, 125, 16, -5, 1, 0 },
  147. { -1, 5, -15, 120, 25, -8, 2, 0 },
  148. { -1, 6, -18, 114, 35, -10, 3, -1 },
  149. { -1, 6, -20, 107, 46, -13, 4, -1 },
  150. { -2, 7, -21, 99, 57, -16, 5, -1 },
  151. { -1, 6, -20, 89, 68, -18, 5, -1 },
  152. { -1, 6, -20, 79, 79, -20, 6, -1 },
  153. { -1, 5, -18, 68, 89, -20, 6, -1 },
  154. { -1, 5, -16, 57, 99, -21, 7, -2 },
  155. { -1, 4, -13, 46, 107, -20, 6, -1 },
  156. { -1, 3, -10, 35, 114, -18, 6, -1 },
  157. { 0, 2, -8, 25, 120, -15, 5, -1 },
  158. { 0, 1, -5, 16, 125, -12, 4, -1 },
  159. { 0, 1, -2, 7, 127, -6, 2, -1 }
  160. }, { /* 65536 < Ratio <= 74898 (~8:7) */
  161. { 3, -8, 14, 111, 13, -8, 3, 0 },
  162. { 2, -6, 7, 112, 21, -10, 3, -1 },
  163. { 2, -4, 1, 110, 28, -12, 4, -1 },
  164. { 1, -2, -3, 106, 36, -13, 4, -1 },
  165. { 1, -1, -7, 103, 44, -15, 4, -1 },
  166. { 1, 1, -11, 97, 53, -16, 4, -1 },
  167. { 0, 2, -13, 91, 61, -16, 4, -1 },
  168. { 0, 3, -15, 85, 69, -17, 4, -1 },
  169. { 0, 3, -16, 77, 77, -16, 3, 0 },
  170. { -1, 4, -17, 69, 85, -15, 3, 0 },
  171. { -1, 4, -16, 61, 91, -13, 2, 0 },
  172. { -1, 4, -16, 53, 97, -11, 1, 1 },
  173. { -1, 4, -15, 44, 103, -7, -1, 1 },
  174. { -1, 4, -13, 36, 106, -3, -2, 1 },
  175. { -1, 4, -12, 28, 110, 1, -4, 2 },
  176. { -1, 3, -10, 21, 112, 7, -6, 2 }
  177. }, { /* 74898 < Ratio <= 87381 (~8:6) */
  178. { 2, -11, 25, 96, 25, -11, 2, 0 },
  179. { 2, -10, 19, 96, 31, -12, 2, 0 },
  180. { 2, -9, 14, 94, 37, -12, 2, 0 },
  181. { 2, -8, 10, 92, 43, -12, 1, 0 },
  182. { 2, -7, 5, 90, 49, -12, 1, 0 },
  183. { 2, -5, 1, 86, 55, -12, 0, 1 },
  184. { 2, -4, -2, 82, 61, -11, -1, 1 },
  185. { 1, -3, -5, 77, 67, -9, -1, 1 },
  186. { 1, -2, -7, 72, 72, -7, -2, 1 },
  187. { 1, -1, -9, 67, 77, -5, -3, 1 },
  188. { 1, -1, -11, 61, 82, -2, -4, 2 },
  189. { 1, 0, -12, 55, 86, 1, -5, 2 },
  190. { 0, 1, -12, 49, 90, 5, -7, 2 },
  191. { 0, 1, -12, 43, 92, 10, -8, 2 },
  192. { 0, 2, -12, 37, 94, 14, -9, 2 },
  193. { 0, 2, -12, 31, 96, 19, -10, 2 }
  194. }, { /* 87381 < Ratio <= 104857 (~8:5) */
  195. { -1, -8, 33, 80, 33, -8, -1, 0 },
  196. { -1, -8, 28, 80, 37, -7, -2, 1 },
  197. { 0, -8, 24, 79, 41, -7, -2, 1 },
  198. { 0, -8, 20, 78, 46, -6, -3, 1 },
  199. { 0, -8, 16, 76, 50, -4, -3, 1 },
  200. { 0, -7, 13, 74, 54, -3, -4, 1 },
  201. { 1, -7, 10, 71, 58, -1, -5, 1 },
  202. { 1, -6, 6, 68, 62, 1, -5, 1 },
  203. { 1, -6, 4, 65, 65, 4, -6, 1 },
  204. { 1, -5, 1, 62, 68, 6, -6, 1 },
  205. { 1, -5, -1, 58, 71, 10, -7, 1 },
  206. { 1, -4, -3, 54, 74, 13, -7, 0 },
  207. { 1, -3, -4, 50, 76, 16, -8, 0 },
  208. { 1, -3, -6, 46, 78, 20, -8, 0 },
  209. { 1, -2, -7, 41, 79, 24, -8, 0 },
  210. { 1, -2, -7, 37, 80, 28, -8, -1 }
  211. }, { /* 104857 < Ratio <= 131072 (~8:4) */
  212. { -3, 0, 35, 64, 35, 0, -3, 0 },
  213. { -3, -1, 32, 64, 38, 1, -3, 0 },
  214. { -2, -2, 29, 63, 41, 2, -3, 0 },
  215. { -2, -3, 27, 63, 43, 4, -4, 0 },
  216. { -2, -3, 24, 61, 46, 6, -4, 0 },
  217. { -2, -3, 21, 60, 49, 7, -4, 0 },
  218. { -1, -4, 19, 59, 51, 9, -4, -1 },
  219. { -1, -4, 16, 57, 53, 12, -4, -1 },
  220. { -1, -4, 14, 55, 55, 14, -4, -1 },
  221. { -1, -4, 12, 53, 57, 16, -4, -1 },
  222. { -1, -4, 9, 51, 59, 19, -4, -1 },
  223. { 0, -4, 7, 49, 60, 21, -3, -2 },
  224. { 0, -4, 6, 46, 61, 24, -3, -2 },
  225. { 0, -4, 4, 43, 63, 27, -3, -2 },
  226. { 0, -3, 2, 41, 63, 29, -2, -2 },
  227. { 0, -3, 1, 38, 64, 32, -1, -3 }
  228. }, { /* 131072 < Ratio <= 174762 (~8:3) */
  229. { -1, 8, 33, 48, 33, 8, -1, 0 },
  230. { -1, 7, 31, 49, 35, 9, -1, -1 },
  231. { -1, 6, 30, 49, 36, 10, -1, -1 },
  232. { -1, 5, 28, 48, 38, 12, -1, -1 },
  233. { -1, 4, 26, 48, 39, 13, 0, -1 },
  234. { -1, 3, 24, 47, 41, 15, 0, -1 },
  235. { -1, 2, 23, 47, 42, 16, 0, -1 },
  236. { -1, 2, 21, 45, 43, 18, 1, -1 },
  237. { -1, 1, 19, 45, 45, 19, 1, -1 },
  238. { -1, 1, 18, 43, 45, 21, 2, -1 },
  239. { -1, 0, 16, 42, 47, 23, 2, -1 },
  240. { -1, 0, 15, 41, 47, 24, 3, -1 },
  241. { -1, 0, 13, 39, 48, 26, 4, -1 },
  242. { -1, -1, 12, 38, 48, 28, 5, -1 },
  243. { -1, -1, 10, 36, 49, 30, 6, -1 },
  244. { -1, -1, 9, 35, 49, 31, 7, -1 }
  245. }, { /* 174762 < Ratio <= 262144 (~8:2) */
  246. { 2, 13, 30, 38, 30, 13, 2, 0 },
  247. { 2, 12, 29, 38, 30, 14, 3, 0 },
  248. { 2, 11, 28, 38, 31, 15, 3, 0 },
  249. { 2, 10, 26, 38, 32, 16, 4, 0 },
  250. { 1, 10, 26, 37, 33, 17, 4, 0 },
  251. { 1, 9, 24, 37, 34, 18, 5, 0 },
  252. { 1, 8, 24, 37, 34, 19, 5, 0 },
  253. { 1, 7, 22, 36, 35, 20, 6, 1 },
  254. { 1, 6, 21, 36, 36, 21, 6, 1 },
  255. { 1, 6, 20, 35, 36, 22, 7, 1 },
  256. { 0, 5, 19, 34, 37, 24, 8, 1 },
  257. { 0, 5, 18, 34, 37, 24, 9, 1 },
  258. { 0, 4, 17, 33, 37, 26, 10, 1 },
  259. { 0, 4, 16, 32, 38, 26, 10, 2 },
  260. { 0, 3, 15, 31, 38, 28, 11, 2 },
  261. { 0, 3, 14, 30, 38, 29, 12, 2 }
  262. }
  263. };
  264. /* 4-tap Filter Coefficient */
  265. static const int v_coef_4t[GSC_COEF_RATIO][GSC_COEF_ATTR][GSC_COEF_V_4T] = {
  266. { /* Ratio <= 65536 (~8:8) */
  267. { 0, 128, 0, 0 },
  268. { -4, 127, 5, 0 },
  269. { -6, 124, 11, -1 },
  270. { -8, 118, 19, -1 },
  271. { -8, 111, 27, -2 },
  272. { -8, 102, 37, -3 },
  273. { -8, 92, 48, -4 },
  274. { -7, 81, 59, -5 },
  275. { -6, 70, 70, -6 },
  276. { -5, 59, 81, -7 },
  277. { -4, 48, 92, -8 },
  278. { -3, 37, 102, -8 },
  279. { -2, 27, 111, -8 },
  280. { -1, 19, 118, -8 },
  281. { -1, 11, 124, -6 },
  282. { 0, 5, 127, -4 }
  283. }, { /* 65536 < Ratio <= 74898 (~8:7) */
  284. { 8, 112, 8, 0 },
  285. { 4, 111, 14, -1 },
  286. { 1, 109, 20, -2 },
  287. { -2, 105, 27, -2 },
  288. { -3, 100, 34, -3 },
  289. { -5, 93, 43, -3 },
  290. { -5, 86, 51, -4 },
  291. { -5, 77, 60, -4 },
  292. { -5, 69, 69, -5 },
  293. { -4, 60, 77, -5 },
  294. { -4, 51, 86, -5 },
  295. { -3, 43, 93, -5 },
  296. { -3, 34, 100, -3 },
  297. { -2, 27, 105, -2 },
  298. { -2, 20, 109, 1 },
  299. { -1, 14, 111, 4 }
  300. }, { /* 74898 < Ratio <= 87381 (~8:6) */
  301. { 16, 96, 16, 0 },
  302. { 12, 97, 21, -2 },
  303. { 8, 96, 26, -2 },
  304. { 5, 93, 32, -2 },
  305. { 2, 89, 39, -2 },
  306. { 0, 84, 46, -2 },
  307. { -1, 79, 53, -3 },
  308. { -2, 73, 59, -2 },
  309. { -2, 66, 66, -2 },
  310. { -2, 59, 73, -2 },
  311. { -3, 53, 79, -1 },
  312. { -2, 46, 84, 0 },
  313. { -2, 39, 89, 2 },
  314. { -2, 32, 93, 5 },
  315. { -2, 26, 96, 8 },
  316. { -2, 21, 97, 12 }
  317. }, { /* 87381 < Ratio <= 104857 (~8:5) */
  318. { 22, 84, 22, 0 },
  319. { 18, 85, 26, -1 },
  320. { 14, 84, 31, -1 },
  321. { 11, 82, 36, -1 },
  322. { 8, 79, 42, -1 },
  323. { 6, 76, 47, -1 },
  324. { 4, 72, 52, 0 },
  325. { 2, 68, 58, 0 },
  326. { 1, 63, 63, 1 },
  327. { 0, 58, 68, 2 },
  328. { 0, 52, 72, 4 },
  329. { -1, 47, 76, 6 },
  330. { -1, 42, 79, 8 },
  331. { -1, 36, 82, 11 },
  332. { -1, 31, 84, 14 },
  333. { -1, 26, 85, 18 }
  334. }, { /* 104857 < Ratio <= 131072 (~8:4) */
  335. { 26, 76, 26, 0 },
  336. { 22, 76, 30, 0 },
  337. { 19, 75, 34, 0 },
  338. { 16, 73, 38, 1 },
  339. { 13, 71, 43, 1 },
  340. { 10, 69, 47, 2 },
  341. { 8, 66, 51, 3 },
  342. { 6, 63, 55, 4 },
  343. { 5, 59, 59, 5 },
  344. { 4, 55, 63, 6 },
  345. { 3, 51, 66, 8 },
  346. { 2, 47, 69, 10 },
  347. { 1, 43, 71, 13 },
  348. { 1, 38, 73, 16 },
  349. { 0, 34, 75, 19 },
  350. { 0, 30, 76, 22 }
  351. }, { /* 131072 < Ratio <= 174762 (~8:3) */
  352. { 29, 70, 29, 0 },
  353. { 26, 68, 32, 2 },
  354. { 23, 67, 36, 2 },
  355. { 20, 66, 39, 3 },
  356. { 17, 65, 43, 3 },
  357. { 15, 63, 46, 4 },
  358. { 12, 61, 50, 5 },
  359. { 10, 58, 53, 7 },
  360. { 8, 56, 56, 8 },
  361. { 7, 53, 58, 10 },
  362. { 5, 50, 61, 12 },
  363. { 4, 46, 63, 15 },
  364. { 3, 43, 65, 17 },
  365. { 3, 39, 66, 20 },
  366. { 2, 36, 67, 23 },
  367. { 2, 32, 68, 26 }
  368. }, { /* 174762 < Ratio <= 262144 (~8:2) */
  369. { 32, 64, 32, 0 },
  370. { 28, 63, 34, 3 },
  371. { 25, 62, 37, 4 },
  372. { 22, 62, 40, 4 },
  373. { 19, 61, 43, 5 },
  374. { 17, 59, 46, 6 },
  375. { 15, 58, 48, 7 },
  376. { 13, 55, 51, 9 },
  377. { 11, 53, 53, 11 },
  378. { 9, 51, 55, 13 },
  379. { 7, 48, 58, 15 },
  380. { 6, 46, 59, 17 },
  381. { 5, 43, 61, 19 },
  382. { 4, 40, 62, 22 },
  383. { 4, 37, 62, 25 },
  384. { 3, 34, 63, 28 }
  385. }
  386. };
  387. static int gsc_sw_reset(struct gsc_context *ctx)
  388. {
  389. u32 cfg;
  390. int count = GSC_RESET_TIMEOUT;
  391. DRM_DEBUG_KMS("%s\n", __func__);
  392. /* s/w reset */
  393. cfg = (GSC_SW_RESET_SRESET);
  394. gsc_write(cfg, GSC_SW_RESET);
  395. /* wait s/w reset complete */
  396. while (count--) {
  397. cfg = gsc_read(GSC_SW_RESET);
  398. if (!cfg)
  399. break;
  400. usleep_range(1000, 2000);
  401. }
  402. if (cfg) {
  403. DRM_ERROR("failed to reset gsc h/w.\n");
  404. return -EBUSY;
  405. }
  406. /* reset sequence */
  407. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  408. cfg |= (GSC_IN_BASE_ADDR_MASK |
  409. GSC_IN_BASE_ADDR_PINGPONG(0));
  410. gsc_write(cfg, GSC_IN_BASE_ADDR_Y_MASK);
  411. gsc_write(cfg, GSC_IN_BASE_ADDR_CB_MASK);
  412. gsc_write(cfg, GSC_IN_BASE_ADDR_CR_MASK);
  413. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  414. cfg |= (GSC_OUT_BASE_ADDR_MASK |
  415. GSC_OUT_BASE_ADDR_PINGPONG(0));
  416. gsc_write(cfg, GSC_OUT_BASE_ADDR_Y_MASK);
  417. gsc_write(cfg, GSC_OUT_BASE_ADDR_CB_MASK);
  418. gsc_write(cfg, GSC_OUT_BASE_ADDR_CR_MASK);
  419. return 0;
  420. }
  421. static void gsc_set_gscblk_fimd_wb(struct gsc_context *ctx, bool enable)
  422. {
  423. u32 gscblk_cfg;
  424. DRM_DEBUG_KMS("%s\n", __func__);
  425. gscblk_cfg = readl(SYSREG_GSCBLK_CFG1);
  426. if (enable)
  427. gscblk_cfg |= GSC_BLK_DISP1WB_DEST(ctx->id) |
  428. GSC_BLK_GSCL_WB_IN_SRC_SEL(ctx->id) |
  429. GSC_BLK_SW_RESET_WB_DEST(ctx->id);
  430. else
  431. gscblk_cfg |= GSC_BLK_PXLASYNC_LO_MASK_WB(ctx->id);
  432. writel(gscblk_cfg, SYSREG_GSCBLK_CFG1);
  433. }
  434. static void gsc_handle_irq(struct gsc_context *ctx, bool enable,
  435. bool overflow, bool done)
  436. {
  437. u32 cfg;
  438. DRM_DEBUG_KMS("%s:enable[%d]overflow[%d]level[%d]\n", __func__,
  439. enable, overflow, done);
  440. cfg = gsc_read(GSC_IRQ);
  441. cfg |= (GSC_IRQ_OR_MASK | GSC_IRQ_FRMDONE_MASK);
  442. if (enable)
  443. cfg |= GSC_IRQ_ENABLE;
  444. else
  445. cfg &= ~GSC_IRQ_ENABLE;
  446. if (overflow)
  447. cfg &= ~GSC_IRQ_OR_MASK;
  448. else
  449. cfg |= GSC_IRQ_OR_MASK;
  450. if (done)
  451. cfg &= ~GSC_IRQ_FRMDONE_MASK;
  452. else
  453. cfg |= GSC_IRQ_FRMDONE_MASK;
  454. gsc_write(cfg, GSC_IRQ);
  455. }
  456. static int gsc_src_set_fmt(struct device *dev, u32 fmt)
  457. {
  458. struct gsc_context *ctx = get_gsc_context(dev);
  459. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  460. u32 cfg;
  461. DRM_DEBUG_KMS("%s:fmt[0x%x]\n", __func__, fmt);
  462. cfg = gsc_read(GSC_IN_CON);
  463. cfg &= ~(GSC_IN_RGB_TYPE_MASK | GSC_IN_YUV422_1P_ORDER_MASK |
  464. GSC_IN_CHROMA_ORDER_MASK | GSC_IN_FORMAT_MASK |
  465. GSC_IN_TILE_TYPE_MASK | GSC_IN_TILE_MODE |
  466. GSC_IN_CHROM_STRIDE_SEL_MASK | GSC_IN_RB_SWAP_MASK);
  467. switch (fmt) {
  468. case DRM_FORMAT_RGB565:
  469. cfg |= GSC_IN_RGB565;
  470. break;
  471. case DRM_FORMAT_XRGB8888:
  472. cfg |= GSC_IN_XRGB8888;
  473. break;
  474. case DRM_FORMAT_BGRX8888:
  475. cfg |= (GSC_IN_XRGB8888 | GSC_IN_RB_SWAP);
  476. break;
  477. case DRM_FORMAT_YUYV:
  478. cfg |= (GSC_IN_YUV422_1P |
  479. GSC_IN_YUV422_1P_ORDER_LSB_Y |
  480. GSC_IN_CHROMA_ORDER_CBCR);
  481. break;
  482. case DRM_FORMAT_YVYU:
  483. cfg |= (GSC_IN_YUV422_1P |
  484. GSC_IN_YUV422_1P_ORDER_LSB_Y |
  485. GSC_IN_CHROMA_ORDER_CRCB);
  486. break;
  487. case DRM_FORMAT_UYVY:
  488. cfg |= (GSC_IN_YUV422_1P |
  489. GSC_IN_YUV422_1P_OEDER_LSB_C |
  490. GSC_IN_CHROMA_ORDER_CBCR);
  491. break;
  492. case DRM_FORMAT_VYUY:
  493. cfg |= (GSC_IN_YUV422_1P |
  494. GSC_IN_YUV422_1P_OEDER_LSB_C |
  495. GSC_IN_CHROMA_ORDER_CRCB);
  496. break;
  497. case DRM_FORMAT_NV21:
  498. case DRM_FORMAT_NV61:
  499. cfg |= (GSC_IN_CHROMA_ORDER_CRCB |
  500. GSC_IN_YUV420_2P);
  501. break;
  502. case DRM_FORMAT_YUV422:
  503. cfg |= GSC_IN_YUV422_3P;
  504. break;
  505. case DRM_FORMAT_YUV420:
  506. case DRM_FORMAT_YVU420:
  507. cfg |= GSC_IN_YUV420_3P;
  508. break;
  509. case DRM_FORMAT_NV12:
  510. case DRM_FORMAT_NV16:
  511. cfg |= (GSC_IN_CHROMA_ORDER_CBCR |
  512. GSC_IN_YUV420_2P);
  513. break;
  514. case DRM_FORMAT_NV12MT:
  515. cfg |= (GSC_IN_TILE_C_16x8 | GSC_IN_TILE_MODE);
  516. break;
  517. default:
  518. dev_err(ippdrv->dev, "inavlid target yuv order 0x%x.\n", fmt);
  519. return -EINVAL;
  520. }
  521. gsc_write(cfg, GSC_IN_CON);
  522. return 0;
  523. }
  524. static int gsc_src_set_transf(struct device *dev,
  525. enum drm_exynos_degree degree,
  526. enum drm_exynos_flip flip, bool *swap)
  527. {
  528. struct gsc_context *ctx = get_gsc_context(dev);
  529. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  530. u32 cfg;
  531. DRM_DEBUG_KMS("%s:degree[%d]flip[0x%x]\n", __func__,
  532. degree, flip);
  533. cfg = gsc_read(GSC_IN_CON);
  534. cfg &= ~GSC_IN_ROT_MASK;
  535. switch (degree) {
  536. case EXYNOS_DRM_DEGREE_0:
  537. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  538. cfg |= GSC_IN_ROT_XFLIP;
  539. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  540. cfg |= GSC_IN_ROT_YFLIP;
  541. break;
  542. case EXYNOS_DRM_DEGREE_90:
  543. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  544. cfg |= GSC_IN_ROT_90_XFLIP;
  545. else if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  546. cfg |= GSC_IN_ROT_90_YFLIP;
  547. else
  548. cfg |= GSC_IN_ROT_90;
  549. break;
  550. case EXYNOS_DRM_DEGREE_180:
  551. cfg |= GSC_IN_ROT_180;
  552. break;
  553. case EXYNOS_DRM_DEGREE_270:
  554. cfg |= GSC_IN_ROT_270;
  555. break;
  556. default:
  557. dev_err(ippdrv->dev, "inavlid degree value %d.\n", degree);
  558. return -EINVAL;
  559. }
  560. gsc_write(cfg, GSC_IN_CON);
  561. ctx->rotation = cfg &
  562. (GSC_IN_ROT_90 | GSC_IN_ROT_270) ? 1 : 0;
  563. *swap = ctx->rotation;
  564. return 0;
  565. }
  566. static int gsc_src_set_size(struct device *dev, int swap,
  567. struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
  568. {
  569. struct gsc_context *ctx = get_gsc_context(dev);
  570. struct drm_exynos_pos img_pos = *pos;
  571. struct gsc_scaler *sc = &ctx->sc;
  572. u32 cfg;
  573. DRM_DEBUG_KMS("%s:swap[%d]x[%d]y[%d]w[%d]h[%d]\n",
  574. __func__, swap, pos->x, pos->y, pos->w, pos->h);
  575. if (swap) {
  576. img_pos.w = pos->h;
  577. img_pos.h = pos->w;
  578. }
  579. /* pixel offset */
  580. cfg = (GSC_SRCIMG_OFFSET_X(img_pos.x) |
  581. GSC_SRCIMG_OFFSET_Y(img_pos.y));
  582. gsc_write(cfg, GSC_SRCIMG_OFFSET);
  583. /* cropped size */
  584. cfg = (GSC_CROPPED_WIDTH(img_pos.w) |
  585. GSC_CROPPED_HEIGHT(img_pos.h));
  586. gsc_write(cfg, GSC_CROPPED_SIZE);
  587. DRM_DEBUG_KMS("%s:hsize[%d]vsize[%d]\n",
  588. __func__, sz->hsize, sz->vsize);
  589. /* original size */
  590. cfg = gsc_read(GSC_SRCIMG_SIZE);
  591. cfg &= ~(GSC_SRCIMG_HEIGHT_MASK |
  592. GSC_SRCIMG_WIDTH_MASK);
  593. cfg |= (GSC_SRCIMG_WIDTH(sz->hsize) |
  594. GSC_SRCIMG_HEIGHT(sz->vsize));
  595. gsc_write(cfg, GSC_SRCIMG_SIZE);
  596. cfg = gsc_read(GSC_IN_CON);
  597. cfg &= ~GSC_IN_RGB_TYPE_MASK;
  598. DRM_DEBUG_KMS("%s:width[%d]range[%d]\n",
  599. __func__, pos->w, sc->range);
  600. if (pos->w >= GSC_WIDTH_ITU_709)
  601. if (sc->range)
  602. cfg |= GSC_IN_RGB_HD_WIDE;
  603. else
  604. cfg |= GSC_IN_RGB_HD_NARROW;
  605. else
  606. if (sc->range)
  607. cfg |= GSC_IN_RGB_SD_WIDE;
  608. else
  609. cfg |= GSC_IN_RGB_SD_NARROW;
  610. gsc_write(cfg, GSC_IN_CON);
  611. return 0;
  612. }
  613. static int gsc_src_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  614. enum drm_exynos_ipp_buf_type buf_type)
  615. {
  616. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  617. bool masked;
  618. u32 cfg;
  619. u32 mask = 0x00000001 << buf_id;
  620. DRM_DEBUG_KMS("%s:buf_id[%d]buf_type[%d]\n", __func__,
  621. buf_id, buf_type);
  622. /* mask register set */
  623. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  624. switch (buf_type) {
  625. case IPP_BUF_ENQUEUE:
  626. masked = false;
  627. break;
  628. case IPP_BUF_DEQUEUE:
  629. masked = true;
  630. break;
  631. default:
  632. dev_err(ippdrv->dev, "invalid buf ctrl parameter.\n");
  633. return -EINVAL;
  634. }
  635. /* sequence id */
  636. cfg &= ~mask;
  637. cfg |= masked << buf_id;
  638. gsc_write(cfg, GSC_IN_BASE_ADDR_Y_MASK);
  639. gsc_write(cfg, GSC_IN_BASE_ADDR_CB_MASK);
  640. gsc_write(cfg, GSC_IN_BASE_ADDR_CR_MASK);
  641. return 0;
  642. }
  643. static int gsc_src_set_addr(struct device *dev,
  644. struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
  645. enum drm_exynos_ipp_buf_type buf_type)
  646. {
  647. struct gsc_context *ctx = get_gsc_context(dev);
  648. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  649. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  650. struct drm_exynos_ipp_property *property;
  651. if (!c_node) {
  652. DRM_ERROR("failed to get c_node.\n");
  653. return -EFAULT;
  654. }
  655. property = &c_node->property;
  656. DRM_DEBUG_KMS("%s:prop_id[%d]buf_id[%d]buf_type[%d]\n", __func__,
  657. property->prop_id, buf_id, buf_type);
  658. if (buf_id > GSC_MAX_SRC) {
  659. dev_info(ippdrv->dev, "inavlid buf_id %d.\n", buf_id);
  660. return -EINVAL;
  661. }
  662. /* address register set */
  663. switch (buf_type) {
  664. case IPP_BUF_ENQUEUE:
  665. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_Y],
  666. GSC_IN_BASE_ADDR_Y(buf_id));
  667. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_CB],
  668. GSC_IN_BASE_ADDR_CB(buf_id));
  669. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_CR],
  670. GSC_IN_BASE_ADDR_CR(buf_id));
  671. break;
  672. case IPP_BUF_DEQUEUE:
  673. gsc_write(0x0, GSC_IN_BASE_ADDR_Y(buf_id));
  674. gsc_write(0x0, GSC_IN_BASE_ADDR_CB(buf_id));
  675. gsc_write(0x0, GSC_IN_BASE_ADDR_CR(buf_id));
  676. break;
  677. default:
  678. /* bypass */
  679. break;
  680. }
  681. return gsc_src_set_buf_seq(ctx, buf_id, buf_type);
  682. }
  683. static struct exynos_drm_ipp_ops gsc_src_ops = {
  684. .set_fmt = gsc_src_set_fmt,
  685. .set_transf = gsc_src_set_transf,
  686. .set_size = gsc_src_set_size,
  687. .set_addr = gsc_src_set_addr,
  688. };
  689. static int gsc_dst_set_fmt(struct device *dev, u32 fmt)
  690. {
  691. struct gsc_context *ctx = get_gsc_context(dev);
  692. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  693. u32 cfg;
  694. DRM_DEBUG_KMS("%s:fmt[0x%x]\n", __func__, fmt);
  695. cfg = gsc_read(GSC_OUT_CON);
  696. cfg &= ~(GSC_OUT_RGB_TYPE_MASK | GSC_OUT_YUV422_1P_ORDER_MASK |
  697. GSC_OUT_CHROMA_ORDER_MASK | GSC_OUT_FORMAT_MASK |
  698. GSC_OUT_CHROM_STRIDE_SEL_MASK | GSC_OUT_RB_SWAP_MASK |
  699. GSC_OUT_GLOBAL_ALPHA_MASK);
  700. switch (fmt) {
  701. case DRM_FORMAT_RGB565:
  702. cfg |= GSC_OUT_RGB565;
  703. break;
  704. case DRM_FORMAT_XRGB8888:
  705. cfg |= GSC_OUT_XRGB8888;
  706. break;
  707. case DRM_FORMAT_BGRX8888:
  708. cfg |= (GSC_OUT_XRGB8888 | GSC_OUT_RB_SWAP);
  709. break;
  710. case DRM_FORMAT_YUYV:
  711. cfg |= (GSC_OUT_YUV422_1P |
  712. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  713. GSC_OUT_CHROMA_ORDER_CBCR);
  714. break;
  715. case DRM_FORMAT_YVYU:
  716. cfg |= (GSC_OUT_YUV422_1P |
  717. GSC_OUT_YUV422_1P_ORDER_LSB_Y |
  718. GSC_OUT_CHROMA_ORDER_CRCB);
  719. break;
  720. case DRM_FORMAT_UYVY:
  721. cfg |= (GSC_OUT_YUV422_1P |
  722. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  723. GSC_OUT_CHROMA_ORDER_CBCR);
  724. break;
  725. case DRM_FORMAT_VYUY:
  726. cfg |= (GSC_OUT_YUV422_1P |
  727. GSC_OUT_YUV422_1P_OEDER_LSB_C |
  728. GSC_OUT_CHROMA_ORDER_CRCB);
  729. break;
  730. case DRM_FORMAT_NV21:
  731. case DRM_FORMAT_NV61:
  732. cfg |= (GSC_OUT_CHROMA_ORDER_CRCB | GSC_OUT_YUV420_2P);
  733. break;
  734. case DRM_FORMAT_YUV422:
  735. case DRM_FORMAT_YUV420:
  736. case DRM_FORMAT_YVU420:
  737. cfg |= GSC_OUT_YUV420_3P;
  738. break;
  739. case DRM_FORMAT_NV12:
  740. case DRM_FORMAT_NV16:
  741. cfg |= (GSC_OUT_CHROMA_ORDER_CBCR |
  742. GSC_OUT_YUV420_2P);
  743. break;
  744. case DRM_FORMAT_NV12MT:
  745. cfg |= (GSC_OUT_TILE_C_16x8 | GSC_OUT_TILE_MODE);
  746. break;
  747. default:
  748. dev_err(ippdrv->dev, "inavlid target yuv order 0x%x.\n", fmt);
  749. return -EINVAL;
  750. }
  751. gsc_write(cfg, GSC_OUT_CON);
  752. return 0;
  753. }
  754. static int gsc_dst_set_transf(struct device *dev,
  755. enum drm_exynos_degree degree,
  756. enum drm_exynos_flip flip, bool *swap)
  757. {
  758. struct gsc_context *ctx = get_gsc_context(dev);
  759. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  760. u32 cfg;
  761. DRM_DEBUG_KMS("%s:degree[%d]flip[0x%x]\n", __func__,
  762. degree, flip);
  763. cfg = gsc_read(GSC_IN_CON);
  764. cfg &= ~GSC_IN_ROT_MASK;
  765. switch (degree) {
  766. case EXYNOS_DRM_DEGREE_0:
  767. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  768. cfg |= GSC_IN_ROT_XFLIP;
  769. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  770. cfg |= GSC_IN_ROT_YFLIP;
  771. break;
  772. case EXYNOS_DRM_DEGREE_90:
  773. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  774. cfg |= GSC_IN_ROT_90_XFLIP;
  775. else if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  776. cfg |= GSC_IN_ROT_90_YFLIP;
  777. else
  778. cfg |= GSC_IN_ROT_90;
  779. break;
  780. case EXYNOS_DRM_DEGREE_180:
  781. cfg |= GSC_IN_ROT_180;
  782. break;
  783. case EXYNOS_DRM_DEGREE_270:
  784. cfg |= GSC_IN_ROT_270;
  785. break;
  786. default:
  787. dev_err(ippdrv->dev, "inavlid degree value %d.\n", degree);
  788. return -EINVAL;
  789. }
  790. gsc_write(cfg, GSC_IN_CON);
  791. ctx->rotation = cfg &
  792. (GSC_IN_ROT_90 | GSC_IN_ROT_270) ? 1 : 0;
  793. *swap = ctx->rotation;
  794. return 0;
  795. }
  796. static int gsc_get_ratio_shift(u32 src, u32 dst, u32 *ratio)
  797. {
  798. DRM_DEBUG_KMS("%s:src[%d]dst[%d]\n", __func__, src, dst);
  799. if (src >= dst * 8) {
  800. DRM_ERROR("failed to make ratio and shift.\n");
  801. return -EINVAL;
  802. } else if (src >= dst * 4)
  803. *ratio = 4;
  804. else if (src >= dst * 2)
  805. *ratio = 2;
  806. else
  807. *ratio = 1;
  808. return 0;
  809. }
  810. static void gsc_get_prescaler_shfactor(u32 hratio, u32 vratio, u32 *shfactor)
  811. {
  812. if (hratio == 4 && vratio == 4)
  813. *shfactor = 4;
  814. else if ((hratio == 4 && vratio == 2) ||
  815. (hratio == 2 && vratio == 4))
  816. *shfactor = 3;
  817. else if ((hratio == 4 && vratio == 1) ||
  818. (hratio == 1 && vratio == 4) ||
  819. (hratio == 2 && vratio == 2))
  820. *shfactor = 2;
  821. else if (hratio == 1 && vratio == 1)
  822. *shfactor = 0;
  823. else
  824. *shfactor = 1;
  825. }
  826. static int gsc_set_prescaler(struct gsc_context *ctx, struct gsc_scaler *sc,
  827. struct drm_exynos_pos *src, struct drm_exynos_pos *dst)
  828. {
  829. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  830. u32 cfg;
  831. u32 src_w, src_h, dst_w, dst_h;
  832. int ret = 0;
  833. src_w = src->w;
  834. src_h = src->h;
  835. if (ctx->rotation) {
  836. dst_w = dst->h;
  837. dst_h = dst->w;
  838. } else {
  839. dst_w = dst->w;
  840. dst_h = dst->h;
  841. }
  842. ret = gsc_get_ratio_shift(src_w, dst_w, &sc->pre_hratio);
  843. if (ret) {
  844. dev_err(ippdrv->dev, "failed to get ratio horizontal.\n");
  845. return ret;
  846. }
  847. ret = gsc_get_ratio_shift(src_h, dst_h, &sc->pre_vratio);
  848. if (ret) {
  849. dev_err(ippdrv->dev, "failed to get ratio vertical.\n");
  850. return ret;
  851. }
  852. DRM_DEBUG_KMS("%s:pre_hratio[%d]pre_vratio[%d]\n",
  853. __func__, sc->pre_hratio, sc->pre_vratio);
  854. sc->main_hratio = (src_w << 16) / dst_w;
  855. sc->main_vratio = (src_h << 16) / dst_h;
  856. DRM_DEBUG_KMS("%s:main_hratio[%ld]main_vratio[%ld]\n",
  857. __func__, sc->main_hratio, sc->main_vratio);
  858. gsc_get_prescaler_shfactor(sc->pre_hratio, sc->pre_vratio,
  859. &sc->pre_shfactor);
  860. DRM_DEBUG_KMS("%s:pre_shfactor[%d]\n", __func__,
  861. sc->pre_shfactor);
  862. cfg = (GSC_PRESC_SHFACTOR(sc->pre_shfactor) |
  863. GSC_PRESC_H_RATIO(sc->pre_hratio) |
  864. GSC_PRESC_V_RATIO(sc->pre_vratio));
  865. gsc_write(cfg, GSC_PRE_SCALE_RATIO);
  866. return ret;
  867. }
  868. static void gsc_set_h_coef(struct gsc_context *ctx, unsigned long main_hratio)
  869. {
  870. int i, j, k, sc_ratio;
  871. if (main_hratio <= GSC_SC_UP_MAX_RATIO)
  872. sc_ratio = 0;
  873. else if (main_hratio <= GSC_SC_DOWN_RATIO_7_8)
  874. sc_ratio = 1;
  875. else if (main_hratio <= GSC_SC_DOWN_RATIO_6_8)
  876. sc_ratio = 2;
  877. else if (main_hratio <= GSC_SC_DOWN_RATIO_5_8)
  878. sc_ratio = 3;
  879. else if (main_hratio <= GSC_SC_DOWN_RATIO_4_8)
  880. sc_ratio = 4;
  881. else if (main_hratio <= GSC_SC_DOWN_RATIO_3_8)
  882. sc_ratio = 5;
  883. else
  884. sc_ratio = 6;
  885. for (i = 0; i < GSC_COEF_PHASE; i++)
  886. for (j = 0; j < GSC_COEF_H_8T; j++)
  887. for (k = 0; k < GSC_COEF_DEPTH; k++)
  888. gsc_write(h_coef_8t[sc_ratio][i][j],
  889. GSC_HCOEF(i, j, k));
  890. }
  891. static void gsc_set_v_coef(struct gsc_context *ctx, unsigned long main_vratio)
  892. {
  893. int i, j, k, sc_ratio;
  894. if (main_vratio <= GSC_SC_UP_MAX_RATIO)
  895. sc_ratio = 0;
  896. else if (main_vratio <= GSC_SC_DOWN_RATIO_7_8)
  897. sc_ratio = 1;
  898. else if (main_vratio <= GSC_SC_DOWN_RATIO_6_8)
  899. sc_ratio = 2;
  900. else if (main_vratio <= GSC_SC_DOWN_RATIO_5_8)
  901. sc_ratio = 3;
  902. else if (main_vratio <= GSC_SC_DOWN_RATIO_4_8)
  903. sc_ratio = 4;
  904. else if (main_vratio <= GSC_SC_DOWN_RATIO_3_8)
  905. sc_ratio = 5;
  906. else
  907. sc_ratio = 6;
  908. for (i = 0; i < GSC_COEF_PHASE; i++)
  909. for (j = 0; j < GSC_COEF_V_4T; j++)
  910. for (k = 0; k < GSC_COEF_DEPTH; k++)
  911. gsc_write(v_coef_4t[sc_ratio][i][j],
  912. GSC_VCOEF(i, j, k));
  913. }
  914. static void gsc_set_scaler(struct gsc_context *ctx, struct gsc_scaler *sc)
  915. {
  916. u32 cfg;
  917. DRM_DEBUG_KMS("%s:main_hratio[%ld]main_vratio[%ld]\n",
  918. __func__, sc->main_hratio, sc->main_vratio);
  919. gsc_set_h_coef(ctx, sc->main_hratio);
  920. cfg = GSC_MAIN_H_RATIO_VALUE(sc->main_hratio);
  921. gsc_write(cfg, GSC_MAIN_H_RATIO);
  922. gsc_set_v_coef(ctx, sc->main_vratio);
  923. cfg = GSC_MAIN_V_RATIO_VALUE(sc->main_vratio);
  924. gsc_write(cfg, GSC_MAIN_V_RATIO);
  925. }
  926. static int gsc_dst_set_size(struct device *dev, int swap,
  927. struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
  928. {
  929. struct gsc_context *ctx = get_gsc_context(dev);
  930. struct drm_exynos_pos img_pos = *pos;
  931. struct gsc_scaler *sc = &ctx->sc;
  932. u32 cfg;
  933. DRM_DEBUG_KMS("%s:swap[%d]x[%d]y[%d]w[%d]h[%d]\n",
  934. __func__, swap, pos->x, pos->y, pos->w, pos->h);
  935. if (swap) {
  936. img_pos.w = pos->h;
  937. img_pos.h = pos->w;
  938. }
  939. /* pixel offset */
  940. cfg = (GSC_DSTIMG_OFFSET_X(pos->x) |
  941. GSC_DSTIMG_OFFSET_Y(pos->y));
  942. gsc_write(cfg, GSC_DSTIMG_OFFSET);
  943. /* scaled size */
  944. cfg = (GSC_SCALED_WIDTH(img_pos.w) | GSC_SCALED_HEIGHT(img_pos.h));
  945. gsc_write(cfg, GSC_SCALED_SIZE);
  946. DRM_DEBUG_KMS("%s:hsize[%d]vsize[%d]\n",
  947. __func__, sz->hsize, sz->vsize);
  948. /* original size */
  949. cfg = gsc_read(GSC_DSTIMG_SIZE);
  950. cfg &= ~(GSC_DSTIMG_HEIGHT_MASK |
  951. GSC_DSTIMG_WIDTH_MASK);
  952. cfg |= (GSC_DSTIMG_WIDTH(sz->hsize) |
  953. GSC_DSTIMG_HEIGHT(sz->vsize));
  954. gsc_write(cfg, GSC_DSTIMG_SIZE);
  955. cfg = gsc_read(GSC_OUT_CON);
  956. cfg &= ~GSC_OUT_RGB_TYPE_MASK;
  957. DRM_DEBUG_KMS("%s:width[%d]range[%d]\n",
  958. __func__, pos->w, sc->range);
  959. if (pos->w >= GSC_WIDTH_ITU_709)
  960. if (sc->range)
  961. cfg |= GSC_OUT_RGB_HD_WIDE;
  962. else
  963. cfg |= GSC_OUT_RGB_HD_NARROW;
  964. else
  965. if (sc->range)
  966. cfg |= GSC_OUT_RGB_SD_WIDE;
  967. else
  968. cfg |= GSC_OUT_RGB_SD_NARROW;
  969. gsc_write(cfg, GSC_OUT_CON);
  970. return 0;
  971. }
  972. static int gsc_dst_get_buf_seq(struct gsc_context *ctx)
  973. {
  974. u32 cfg, i, buf_num = GSC_REG_SZ;
  975. u32 mask = 0x00000001;
  976. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  977. for (i = 0; i < GSC_REG_SZ; i++)
  978. if (cfg & (mask << i))
  979. buf_num--;
  980. DRM_DEBUG_KMS("%s:buf_num[%d]\n", __func__, buf_num);
  981. return buf_num;
  982. }
  983. static int gsc_dst_set_buf_seq(struct gsc_context *ctx, u32 buf_id,
  984. enum drm_exynos_ipp_buf_type buf_type)
  985. {
  986. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  987. bool masked;
  988. u32 cfg;
  989. u32 mask = 0x00000001 << buf_id;
  990. int ret = 0;
  991. DRM_DEBUG_KMS("%s:buf_id[%d]buf_type[%d]\n", __func__,
  992. buf_id, buf_type);
  993. mutex_lock(&ctx->lock);
  994. /* mask register set */
  995. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  996. switch (buf_type) {
  997. case IPP_BUF_ENQUEUE:
  998. masked = false;
  999. break;
  1000. case IPP_BUF_DEQUEUE:
  1001. masked = true;
  1002. break;
  1003. default:
  1004. dev_err(ippdrv->dev, "invalid buf ctrl parameter.\n");
  1005. ret = -EINVAL;
  1006. goto err_unlock;
  1007. }
  1008. /* sequence id */
  1009. cfg &= ~mask;
  1010. cfg |= masked << buf_id;
  1011. gsc_write(cfg, GSC_OUT_BASE_ADDR_Y_MASK);
  1012. gsc_write(cfg, GSC_OUT_BASE_ADDR_CB_MASK);
  1013. gsc_write(cfg, GSC_OUT_BASE_ADDR_CR_MASK);
  1014. /* interrupt enable */
  1015. if (buf_type == IPP_BUF_ENQUEUE &&
  1016. gsc_dst_get_buf_seq(ctx) >= GSC_BUF_START)
  1017. gsc_handle_irq(ctx, true, false, true);
  1018. /* interrupt disable */
  1019. if (buf_type == IPP_BUF_DEQUEUE &&
  1020. gsc_dst_get_buf_seq(ctx) <= GSC_BUF_STOP)
  1021. gsc_handle_irq(ctx, false, false, true);
  1022. err_unlock:
  1023. mutex_unlock(&ctx->lock);
  1024. return ret;
  1025. }
  1026. static int gsc_dst_set_addr(struct device *dev,
  1027. struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
  1028. enum drm_exynos_ipp_buf_type buf_type)
  1029. {
  1030. struct gsc_context *ctx = get_gsc_context(dev);
  1031. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1032. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  1033. struct drm_exynos_ipp_property *property;
  1034. if (!c_node) {
  1035. DRM_ERROR("failed to get c_node.\n");
  1036. return -EFAULT;
  1037. }
  1038. property = &c_node->property;
  1039. DRM_DEBUG_KMS("%s:prop_id[%d]buf_id[%d]buf_type[%d]\n", __func__,
  1040. property->prop_id, buf_id, buf_type);
  1041. if (buf_id > GSC_MAX_DST) {
  1042. dev_info(ippdrv->dev, "inavlid buf_id %d.\n", buf_id);
  1043. return -EINVAL;
  1044. }
  1045. /* address register set */
  1046. switch (buf_type) {
  1047. case IPP_BUF_ENQUEUE:
  1048. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_Y],
  1049. GSC_OUT_BASE_ADDR_Y(buf_id));
  1050. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_CB],
  1051. GSC_OUT_BASE_ADDR_CB(buf_id));
  1052. gsc_write(buf_info->base[EXYNOS_DRM_PLANAR_CR],
  1053. GSC_OUT_BASE_ADDR_CR(buf_id));
  1054. break;
  1055. case IPP_BUF_DEQUEUE:
  1056. gsc_write(0x0, GSC_OUT_BASE_ADDR_Y(buf_id));
  1057. gsc_write(0x0, GSC_OUT_BASE_ADDR_CB(buf_id));
  1058. gsc_write(0x0, GSC_OUT_BASE_ADDR_CR(buf_id));
  1059. break;
  1060. default:
  1061. /* bypass */
  1062. break;
  1063. }
  1064. return gsc_dst_set_buf_seq(ctx, buf_id, buf_type);
  1065. }
  1066. static struct exynos_drm_ipp_ops gsc_dst_ops = {
  1067. .set_fmt = gsc_dst_set_fmt,
  1068. .set_transf = gsc_dst_set_transf,
  1069. .set_size = gsc_dst_set_size,
  1070. .set_addr = gsc_dst_set_addr,
  1071. };
  1072. static int gsc_clk_ctrl(struct gsc_context *ctx, bool enable)
  1073. {
  1074. DRM_DEBUG_KMS("%s:enable[%d]\n", __func__, enable);
  1075. if (enable) {
  1076. clk_enable(ctx->gsc_clk);
  1077. ctx->suspended = false;
  1078. } else {
  1079. clk_disable(ctx->gsc_clk);
  1080. ctx->suspended = true;
  1081. }
  1082. return 0;
  1083. }
  1084. static int gsc_get_src_buf_index(struct gsc_context *ctx)
  1085. {
  1086. u32 cfg, curr_index, i;
  1087. u32 buf_id = GSC_MAX_SRC;
  1088. int ret;
  1089. DRM_DEBUG_KMS("%s:gsc id[%d]\n", __func__, ctx->id);
  1090. cfg = gsc_read(GSC_IN_BASE_ADDR_Y_MASK);
  1091. curr_index = GSC_IN_CURR_GET_INDEX(cfg);
  1092. for (i = curr_index; i < GSC_MAX_SRC; i++) {
  1093. if (!((cfg >> i) & 0x1)) {
  1094. buf_id = i;
  1095. break;
  1096. }
  1097. }
  1098. if (buf_id == GSC_MAX_SRC) {
  1099. DRM_ERROR("failed to get in buffer index.\n");
  1100. return -EINVAL;
  1101. }
  1102. ret = gsc_src_set_buf_seq(ctx, buf_id, IPP_BUF_DEQUEUE);
  1103. if (ret < 0) {
  1104. DRM_ERROR("failed to dequeue.\n");
  1105. return ret;
  1106. }
  1107. DRM_DEBUG_KMS("%s:cfg[0x%x]curr_index[%d]buf_id[%d]\n", __func__, cfg,
  1108. curr_index, buf_id);
  1109. return buf_id;
  1110. }
  1111. static int gsc_get_dst_buf_index(struct gsc_context *ctx)
  1112. {
  1113. u32 cfg, curr_index, i;
  1114. u32 buf_id = GSC_MAX_DST;
  1115. int ret;
  1116. DRM_DEBUG_KMS("%s:gsc id[%d]\n", __func__, ctx->id);
  1117. cfg = gsc_read(GSC_OUT_BASE_ADDR_Y_MASK);
  1118. curr_index = GSC_OUT_CURR_GET_INDEX(cfg);
  1119. for (i = curr_index; i < GSC_MAX_DST; i++) {
  1120. if (!((cfg >> i) & 0x1)) {
  1121. buf_id = i;
  1122. break;
  1123. }
  1124. }
  1125. if (buf_id == GSC_MAX_DST) {
  1126. DRM_ERROR("failed to get out buffer index.\n");
  1127. return -EINVAL;
  1128. }
  1129. ret = gsc_dst_set_buf_seq(ctx, buf_id, IPP_BUF_DEQUEUE);
  1130. if (ret < 0) {
  1131. DRM_ERROR("failed to dequeue.\n");
  1132. return ret;
  1133. }
  1134. DRM_DEBUG_KMS("%s:cfg[0x%x]curr_index[%d]buf_id[%d]\n", __func__, cfg,
  1135. curr_index, buf_id);
  1136. return buf_id;
  1137. }
  1138. static irqreturn_t gsc_irq_handler(int irq, void *dev_id)
  1139. {
  1140. struct gsc_context *ctx = dev_id;
  1141. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1142. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  1143. struct drm_exynos_ipp_event_work *event_work =
  1144. c_node->event_work;
  1145. u32 status;
  1146. int buf_id[EXYNOS_DRM_OPS_MAX];
  1147. DRM_DEBUG_KMS("%s:gsc id[%d]\n", __func__, ctx->id);
  1148. status = gsc_read(GSC_IRQ);
  1149. if (status & GSC_IRQ_STATUS_OR_IRQ) {
  1150. dev_err(ippdrv->dev, "occured overflow at %d, status 0x%x.\n",
  1151. ctx->id, status);
  1152. return IRQ_NONE;
  1153. }
  1154. if (status & GSC_IRQ_STATUS_OR_FRM_DONE) {
  1155. dev_dbg(ippdrv->dev, "occured frame done at %d, status 0x%x.\n",
  1156. ctx->id, status);
  1157. buf_id[EXYNOS_DRM_OPS_SRC] = gsc_get_src_buf_index(ctx);
  1158. if (buf_id[EXYNOS_DRM_OPS_SRC] < 0)
  1159. return IRQ_HANDLED;
  1160. buf_id[EXYNOS_DRM_OPS_DST] = gsc_get_dst_buf_index(ctx);
  1161. if (buf_id[EXYNOS_DRM_OPS_DST] < 0)
  1162. return IRQ_HANDLED;
  1163. DRM_DEBUG_KMS("%s:buf_id_src[%d]buf_id_dst[%d]\n", __func__,
  1164. buf_id[EXYNOS_DRM_OPS_SRC], buf_id[EXYNOS_DRM_OPS_DST]);
  1165. event_work->ippdrv = ippdrv;
  1166. event_work->buf_id[EXYNOS_DRM_OPS_SRC] =
  1167. buf_id[EXYNOS_DRM_OPS_SRC];
  1168. event_work->buf_id[EXYNOS_DRM_OPS_DST] =
  1169. buf_id[EXYNOS_DRM_OPS_DST];
  1170. queue_work(ippdrv->event_workq,
  1171. (struct work_struct *)event_work);
  1172. }
  1173. return IRQ_HANDLED;
  1174. }
  1175. static int gsc_init_prop_list(struct exynos_drm_ippdrv *ippdrv)
  1176. {
  1177. struct drm_exynos_ipp_prop_list *prop_list;
  1178. DRM_DEBUG_KMS("%s\n", __func__);
  1179. prop_list = devm_kzalloc(ippdrv->dev, sizeof(*prop_list), GFP_KERNEL);
  1180. if (!prop_list) {
  1181. DRM_ERROR("failed to alloc property list.\n");
  1182. return -ENOMEM;
  1183. }
  1184. prop_list->version = 1;
  1185. prop_list->writeback = 1;
  1186. prop_list->refresh_min = GSC_REFRESH_MIN;
  1187. prop_list->refresh_max = GSC_REFRESH_MAX;
  1188. prop_list->flip = (1 << EXYNOS_DRM_FLIP_VERTICAL) |
  1189. (1 << EXYNOS_DRM_FLIP_HORIZONTAL);
  1190. prop_list->degree = (1 << EXYNOS_DRM_DEGREE_0) |
  1191. (1 << EXYNOS_DRM_DEGREE_90) |
  1192. (1 << EXYNOS_DRM_DEGREE_180) |
  1193. (1 << EXYNOS_DRM_DEGREE_270);
  1194. prop_list->csc = 1;
  1195. prop_list->crop = 1;
  1196. prop_list->crop_max.hsize = GSC_CROP_MAX;
  1197. prop_list->crop_max.vsize = GSC_CROP_MAX;
  1198. prop_list->crop_min.hsize = GSC_CROP_MIN;
  1199. prop_list->crop_min.vsize = GSC_CROP_MIN;
  1200. prop_list->scale = 1;
  1201. prop_list->scale_max.hsize = GSC_SCALE_MAX;
  1202. prop_list->scale_max.vsize = GSC_SCALE_MAX;
  1203. prop_list->scale_min.hsize = GSC_SCALE_MIN;
  1204. prop_list->scale_min.vsize = GSC_SCALE_MIN;
  1205. ippdrv->prop_list = prop_list;
  1206. return 0;
  1207. }
  1208. static inline bool gsc_check_drm_flip(enum drm_exynos_flip flip)
  1209. {
  1210. switch (flip) {
  1211. case EXYNOS_DRM_FLIP_NONE:
  1212. case EXYNOS_DRM_FLIP_VERTICAL:
  1213. case EXYNOS_DRM_FLIP_HORIZONTAL:
  1214. case EXYNOS_DRM_FLIP_BOTH:
  1215. return true;
  1216. default:
  1217. DRM_DEBUG_KMS("%s:invalid flip\n", __func__);
  1218. return false;
  1219. }
  1220. }
  1221. static int gsc_ippdrv_check_property(struct device *dev,
  1222. struct drm_exynos_ipp_property *property)
  1223. {
  1224. struct gsc_context *ctx = get_gsc_context(dev);
  1225. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1226. struct drm_exynos_ipp_prop_list *pp = ippdrv->prop_list;
  1227. struct drm_exynos_ipp_config *config;
  1228. struct drm_exynos_pos *pos;
  1229. struct drm_exynos_sz *sz;
  1230. bool swap;
  1231. int i;
  1232. DRM_DEBUG_KMS("%s\n", __func__);
  1233. for_each_ipp_ops(i) {
  1234. if ((i == EXYNOS_DRM_OPS_SRC) &&
  1235. (property->cmd == IPP_CMD_WB))
  1236. continue;
  1237. config = &property->config[i];
  1238. pos = &config->pos;
  1239. sz = &config->sz;
  1240. /* check for flip */
  1241. if (!gsc_check_drm_flip(config->flip)) {
  1242. DRM_ERROR("invalid flip.\n");
  1243. goto err_property;
  1244. }
  1245. /* check for degree */
  1246. switch (config->degree) {
  1247. case EXYNOS_DRM_DEGREE_90:
  1248. case EXYNOS_DRM_DEGREE_270:
  1249. swap = true;
  1250. break;
  1251. case EXYNOS_DRM_DEGREE_0:
  1252. case EXYNOS_DRM_DEGREE_180:
  1253. swap = false;
  1254. break;
  1255. default:
  1256. DRM_ERROR("invalid degree.\n");
  1257. goto err_property;
  1258. }
  1259. /* check for buffer bound */
  1260. if ((pos->x + pos->w > sz->hsize) ||
  1261. (pos->y + pos->h > sz->vsize)) {
  1262. DRM_ERROR("out of buf bound.\n");
  1263. goto err_property;
  1264. }
  1265. /* check for crop */
  1266. if ((i == EXYNOS_DRM_OPS_SRC) && (pp->crop)) {
  1267. if (swap) {
  1268. if ((pos->h < pp->crop_min.hsize) ||
  1269. (sz->vsize > pp->crop_max.hsize) ||
  1270. (pos->w < pp->crop_min.vsize) ||
  1271. (sz->hsize > pp->crop_max.vsize)) {
  1272. DRM_ERROR("out of crop size.\n");
  1273. goto err_property;
  1274. }
  1275. } else {
  1276. if ((pos->w < pp->crop_min.hsize) ||
  1277. (sz->hsize > pp->crop_max.hsize) ||
  1278. (pos->h < pp->crop_min.vsize) ||
  1279. (sz->vsize > pp->crop_max.vsize)) {
  1280. DRM_ERROR("out of crop size.\n");
  1281. goto err_property;
  1282. }
  1283. }
  1284. }
  1285. /* check for scale */
  1286. if ((i == EXYNOS_DRM_OPS_DST) && (pp->scale)) {
  1287. if (swap) {
  1288. if ((pos->h < pp->scale_min.hsize) ||
  1289. (sz->vsize > pp->scale_max.hsize) ||
  1290. (pos->w < pp->scale_min.vsize) ||
  1291. (sz->hsize > pp->scale_max.vsize)) {
  1292. DRM_ERROR("out of scale size.\n");
  1293. goto err_property;
  1294. }
  1295. } else {
  1296. if ((pos->w < pp->scale_min.hsize) ||
  1297. (sz->hsize > pp->scale_max.hsize) ||
  1298. (pos->h < pp->scale_min.vsize) ||
  1299. (sz->vsize > pp->scale_max.vsize)) {
  1300. DRM_ERROR("out of scale size.\n");
  1301. goto err_property;
  1302. }
  1303. }
  1304. }
  1305. }
  1306. return 0;
  1307. err_property:
  1308. for_each_ipp_ops(i) {
  1309. if ((i == EXYNOS_DRM_OPS_SRC) &&
  1310. (property->cmd == IPP_CMD_WB))
  1311. continue;
  1312. config = &property->config[i];
  1313. pos = &config->pos;
  1314. sz = &config->sz;
  1315. DRM_ERROR("[%s]f[%d]r[%d]pos[%d %d %d %d]sz[%d %d]\n",
  1316. i ? "dst" : "src", config->flip, config->degree,
  1317. pos->x, pos->y, pos->w, pos->h,
  1318. sz->hsize, sz->vsize);
  1319. }
  1320. return -EINVAL;
  1321. }
  1322. static int gsc_ippdrv_reset(struct device *dev)
  1323. {
  1324. struct gsc_context *ctx = get_gsc_context(dev);
  1325. struct gsc_scaler *sc = &ctx->sc;
  1326. int ret;
  1327. DRM_DEBUG_KMS("%s\n", __func__);
  1328. /* reset h/w block */
  1329. ret = gsc_sw_reset(ctx);
  1330. if (ret < 0) {
  1331. dev_err(dev, "failed to reset hardware.\n");
  1332. return ret;
  1333. }
  1334. /* scaler setting */
  1335. memset(&ctx->sc, 0x0, sizeof(ctx->sc));
  1336. sc->range = true;
  1337. return 0;
  1338. }
  1339. static int gsc_ippdrv_start(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  1340. {
  1341. struct gsc_context *ctx = get_gsc_context(dev);
  1342. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1343. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  1344. struct drm_exynos_ipp_property *property;
  1345. struct drm_exynos_ipp_config *config;
  1346. struct drm_exynos_pos img_pos[EXYNOS_DRM_OPS_MAX];
  1347. struct drm_exynos_ipp_set_wb set_wb;
  1348. u32 cfg;
  1349. int ret, i;
  1350. DRM_DEBUG_KMS("%s:cmd[%d]\n", __func__, cmd);
  1351. if (!c_node) {
  1352. DRM_ERROR("failed to get c_node.\n");
  1353. return -EINVAL;
  1354. }
  1355. property = &c_node->property;
  1356. gsc_handle_irq(ctx, true, false, true);
  1357. for_each_ipp_ops(i) {
  1358. config = &property->config[i];
  1359. img_pos[i] = config->pos;
  1360. }
  1361. switch (cmd) {
  1362. case IPP_CMD_M2M:
  1363. /* enable one shot */
  1364. cfg = gsc_read(GSC_ENABLE);
  1365. cfg &= ~(GSC_ENABLE_ON_CLEAR_MASK |
  1366. GSC_ENABLE_CLK_GATE_MODE_MASK);
  1367. cfg |= GSC_ENABLE_ON_CLEAR_ONESHOT;
  1368. gsc_write(cfg, GSC_ENABLE);
  1369. /* src dma memory */
  1370. cfg = gsc_read(GSC_IN_CON);
  1371. cfg &= ~(GSC_IN_PATH_MASK | GSC_IN_LOCAL_SEL_MASK);
  1372. cfg |= GSC_IN_PATH_MEMORY;
  1373. gsc_write(cfg, GSC_IN_CON);
  1374. /* dst dma memory */
  1375. cfg = gsc_read(GSC_OUT_CON);
  1376. cfg |= GSC_OUT_PATH_MEMORY;
  1377. gsc_write(cfg, GSC_OUT_CON);
  1378. break;
  1379. case IPP_CMD_WB:
  1380. set_wb.enable = 1;
  1381. set_wb.refresh = property->refresh_rate;
  1382. gsc_set_gscblk_fimd_wb(ctx, set_wb.enable);
  1383. exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
  1384. /* src local path */
  1385. cfg = gsc_read(GSC_IN_CON);
  1386. cfg &= ~(GSC_IN_PATH_MASK | GSC_IN_LOCAL_SEL_MASK);
  1387. cfg |= (GSC_IN_PATH_LOCAL | GSC_IN_LOCAL_FIMD_WB);
  1388. gsc_write(cfg, GSC_IN_CON);
  1389. /* dst dma memory */
  1390. cfg = gsc_read(GSC_OUT_CON);
  1391. cfg |= GSC_OUT_PATH_MEMORY;
  1392. gsc_write(cfg, GSC_OUT_CON);
  1393. break;
  1394. case IPP_CMD_OUTPUT:
  1395. /* src dma memory */
  1396. cfg = gsc_read(GSC_IN_CON);
  1397. cfg &= ~(GSC_IN_PATH_MASK | GSC_IN_LOCAL_SEL_MASK);
  1398. cfg |= GSC_IN_PATH_MEMORY;
  1399. gsc_write(cfg, GSC_IN_CON);
  1400. /* dst local path */
  1401. cfg = gsc_read(GSC_OUT_CON);
  1402. cfg |= GSC_OUT_PATH_MEMORY;
  1403. gsc_write(cfg, GSC_OUT_CON);
  1404. break;
  1405. default:
  1406. ret = -EINVAL;
  1407. dev_err(dev, "invalid operations.\n");
  1408. return ret;
  1409. }
  1410. ret = gsc_set_prescaler(ctx, &ctx->sc,
  1411. &img_pos[EXYNOS_DRM_OPS_SRC],
  1412. &img_pos[EXYNOS_DRM_OPS_DST]);
  1413. if (ret) {
  1414. dev_err(dev, "failed to set precalser.\n");
  1415. return ret;
  1416. }
  1417. gsc_set_scaler(ctx, &ctx->sc);
  1418. cfg = gsc_read(GSC_ENABLE);
  1419. cfg |= GSC_ENABLE_ON;
  1420. gsc_write(cfg, GSC_ENABLE);
  1421. return 0;
  1422. }
  1423. static void gsc_ippdrv_stop(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  1424. {
  1425. struct gsc_context *ctx = get_gsc_context(dev);
  1426. struct drm_exynos_ipp_set_wb set_wb = {0, 0};
  1427. u32 cfg;
  1428. DRM_DEBUG_KMS("%s:cmd[%d]\n", __func__, cmd);
  1429. switch (cmd) {
  1430. case IPP_CMD_M2M:
  1431. /* bypass */
  1432. break;
  1433. case IPP_CMD_WB:
  1434. gsc_set_gscblk_fimd_wb(ctx, set_wb.enable);
  1435. exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
  1436. break;
  1437. case IPP_CMD_OUTPUT:
  1438. default:
  1439. dev_err(dev, "invalid operations.\n");
  1440. break;
  1441. }
  1442. gsc_handle_irq(ctx, false, false, true);
  1443. /* reset sequence */
  1444. gsc_write(0xff, GSC_OUT_BASE_ADDR_Y_MASK);
  1445. gsc_write(0xff, GSC_OUT_BASE_ADDR_CB_MASK);
  1446. gsc_write(0xff, GSC_OUT_BASE_ADDR_CR_MASK);
  1447. cfg = gsc_read(GSC_ENABLE);
  1448. cfg &= ~GSC_ENABLE_ON;
  1449. gsc_write(cfg, GSC_ENABLE);
  1450. }
  1451. static int gsc_probe(struct platform_device *pdev)
  1452. {
  1453. struct device *dev = &pdev->dev;
  1454. struct gsc_context *ctx;
  1455. struct resource *res;
  1456. struct exynos_drm_ippdrv *ippdrv;
  1457. int ret;
  1458. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1459. if (!ctx)
  1460. return -ENOMEM;
  1461. /* clock control */
  1462. ctx->gsc_clk = devm_clk_get(dev, "gscl");
  1463. if (IS_ERR(ctx->gsc_clk)) {
  1464. dev_err(dev, "failed to get gsc clock.\n");
  1465. return PTR_ERR(ctx->gsc_clk);
  1466. }
  1467. /* resource memory */
  1468. ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1469. ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
  1470. if (IS_ERR(ctx->regs))
  1471. return PTR_ERR(ctx->regs);
  1472. /* resource irq */
  1473. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1474. if (!res) {
  1475. dev_err(dev, "failed to request irq resource.\n");
  1476. return -ENOENT;
  1477. }
  1478. ctx->irq = res->start;
  1479. ret = request_threaded_irq(ctx->irq, NULL, gsc_irq_handler,
  1480. IRQF_ONESHOT, "drm_gsc", ctx);
  1481. if (ret < 0) {
  1482. dev_err(dev, "failed to request irq.\n");
  1483. return ret;
  1484. }
  1485. /* context initailization */
  1486. ctx->id = pdev->id;
  1487. ippdrv = &ctx->ippdrv;
  1488. ippdrv->dev = dev;
  1489. ippdrv->ops[EXYNOS_DRM_OPS_SRC] = &gsc_src_ops;
  1490. ippdrv->ops[EXYNOS_DRM_OPS_DST] = &gsc_dst_ops;
  1491. ippdrv->check_property = gsc_ippdrv_check_property;
  1492. ippdrv->reset = gsc_ippdrv_reset;
  1493. ippdrv->start = gsc_ippdrv_start;
  1494. ippdrv->stop = gsc_ippdrv_stop;
  1495. ret = gsc_init_prop_list(ippdrv);
  1496. if (ret < 0) {
  1497. dev_err(dev, "failed to init property list.\n");
  1498. goto err_get_irq;
  1499. }
  1500. DRM_DEBUG_KMS("%s:id[%d]ippdrv[0x%x]\n", __func__, ctx->id,
  1501. (int)ippdrv);
  1502. mutex_init(&ctx->lock);
  1503. platform_set_drvdata(pdev, ctx);
  1504. pm_runtime_set_active(dev);
  1505. pm_runtime_enable(dev);
  1506. ret = exynos_drm_ippdrv_register(ippdrv);
  1507. if (ret < 0) {
  1508. dev_err(dev, "failed to register drm gsc device.\n");
  1509. goto err_ippdrv_register;
  1510. }
  1511. dev_info(&pdev->dev, "drm gsc registered successfully.\n");
  1512. return 0;
  1513. err_ippdrv_register:
  1514. devm_kfree(dev, ippdrv->prop_list);
  1515. pm_runtime_disable(dev);
  1516. err_get_irq:
  1517. free_irq(ctx->irq, ctx);
  1518. return ret;
  1519. }
  1520. static int gsc_remove(struct platform_device *pdev)
  1521. {
  1522. struct device *dev = &pdev->dev;
  1523. struct gsc_context *ctx = get_gsc_context(dev);
  1524. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1525. devm_kfree(dev, ippdrv->prop_list);
  1526. exynos_drm_ippdrv_unregister(ippdrv);
  1527. mutex_destroy(&ctx->lock);
  1528. pm_runtime_set_suspended(dev);
  1529. pm_runtime_disable(dev);
  1530. free_irq(ctx->irq, ctx);
  1531. return 0;
  1532. }
  1533. #ifdef CONFIG_PM_SLEEP
  1534. static int gsc_suspend(struct device *dev)
  1535. {
  1536. struct gsc_context *ctx = get_gsc_context(dev);
  1537. DRM_DEBUG_KMS("%s:id[%d]\n", __func__, ctx->id);
  1538. if (pm_runtime_suspended(dev))
  1539. return 0;
  1540. return gsc_clk_ctrl(ctx, false);
  1541. }
  1542. static int gsc_resume(struct device *dev)
  1543. {
  1544. struct gsc_context *ctx = get_gsc_context(dev);
  1545. DRM_DEBUG_KMS("%s:id[%d]\n", __func__, ctx->id);
  1546. if (!pm_runtime_suspended(dev))
  1547. return gsc_clk_ctrl(ctx, true);
  1548. return 0;
  1549. }
  1550. #endif
  1551. #ifdef CONFIG_PM_RUNTIME
  1552. static int gsc_runtime_suspend(struct device *dev)
  1553. {
  1554. struct gsc_context *ctx = get_gsc_context(dev);
  1555. DRM_DEBUG_KMS("%s:id[%d]\n", __func__, ctx->id);
  1556. return gsc_clk_ctrl(ctx, false);
  1557. }
  1558. static int gsc_runtime_resume(struct device *dev)
  1559. {
  1560. struct gsc_context *ctx = get_gsc_context(dev);
  1561. DRM_DEBUG_KMS("%s:id[%d]\n", __FILE__, ctx->id);
  1562. return gsc_clk_ctrl(ctx, true);
  1563. }
  1564. #endif
  1565. static const struct dev_pm_ops gsc_pm_ops = {
  1566. SET_SYSTEM_SLEEP_PM_OPS(gsc_suspend, gsc_resume)
  1567. SET_RUNTIME_PM_OPS(gsc_runtime_suspend, gsc_runtime_resume, NULL)
  1568. };
  1569. struct platform_driver gsc_driver = {
  1570. .probe = gsc_probe,
  1571. .remove = gsc_remove,
  1572. .driver = {
  1573. .name = "exynos-drm-gsc",
  1574. .owner = THIS_MODULE,
  1575. .pm = &gsc_pm_ops,
  1576. },
  1577. };