mtk_disp_aal.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. /*
  2. * Copyright (c) 2019 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <drm/drmP.h>
  14. #include <linux/clk.h>
  15. #include <linux/component.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/soc/mediatek/mtk-cmdq.h>
  22. #include <linux/module.h>
  23. #include <linux/workqueue.h>
  24. //For 120Hz rotation issue
  25. #include <linux/time.h>
  26. #ifdef CONFIG_LEDS_MTK_DISP
  27. #define CONFIG_LEDS_BRIGHTNESS_CHANGED
  28. #include <mtk_leds_drv.h>
  29. #include <leds-mtk-disp.h>
  30. #elif defined CONFIG_LEDS_MTK_PWM
  31. #define CONFIG_LEDS_BRIGHTNESS_CHANGED
  32. #include <mtk_leds_drv.h>
  33. #include <leds-mtk-pwm.h>
  34. #else
  35. #define mt_leds_brightness_set(x, y) do { } while (0)
  36. #define MT65XX_LED_MODE_NONE (0)
  37. #define MT65XX_LED_MODE_CUST_LCM (4)
  38. #endif
  39. #include "mtk_drm_crtc.h"
  40. #include "mtk_drm_ddp_comp.h"
  41. #include "mtk_drm_drv.h"
  42. #include "mtk_drm_lowpower.h"
  43. #include "mtk_log.h"
  44. #include "mtk_dump.h"
  45. #include "mtk_disp_aal.h"
  46. #include "mtk_disp_color.h"
  47. #undef pr_fmt
  48. #define pr_fmt(fmt) "[disp_aal]" fmt
  49. // It's a work around for no comp assigned in functions.
  50. static struct mtk_ddp_comp *default_comp;
  51. //For 120Hz rotation issue
  52. struct timeval start, end;
  53. /* To enable debug log: */
  54. /* # echo aal_dbg:1 > /sys/kernel/debug/dispsys */
  55. int aal_dbg_en;
  56. static DECLARE_WAIT_QUEUE_HEAD(g_aal_hist_wq);
  57. static DEFINE_SPINLOCK(g_aal_clock_lock);
  58. static DEFINE_SPINLOCK(g_aal_hist_lock);
  59. static DEFINE_SPINLOCK(g_aal_irq_en_lock);
  60. static struct DISP_AAL_HIST g_aal_hist = {
  61. .serviceFlags = 0,
  62. .backlight = -1,
  63. .essStrengthIndex = ESS_LEVEL_BY_CUSTOM_LIB,
  64. .ess_enable = ESS_EN_BY_CUSTOM_LIB,
  65. .dre_enable = DRE_EN_BY_CUSTOM_LIB
  66. };
  67. static struct DISP_AAL_HIST g_aal_hist_db;
  68. //static ddp_module_notify g_ddp_notify;
  69. static atomic_t g_aal_hist_available = ATOMIC_INIT(0);
  70. static atomic_t g_aal_is_init_regs_valid = ATOMIC_INIT(0);
  71. static atomic_t g_aal_backlight_notified = ATOMIC_INIT(1023);
  72. static atomic_t g_aal_initialed = ATOMIC_INIT(0);
  73. static atomic_t g_aal_allowPartial = ATOMIC_INIT(0);
  74. static atomic_t g_aal_force_enable_irq = ATOMIC_INIT(0);
  75. static atomic_t g_led_mode = ATOMIC_INIT(MT65XX_LED_MODE_NONE);
  76. static atomic_t g_aal_force_relay = ATOMIC_INIT(0);
  77. static atomic_t g_aal_eof_irq = ATOMIC_INIT(0);
  78. static atomic_t g_aal_first_frame = ATOMIC_INIT(0);
  79. static struct workqueue_struct *aal_flip_wq;
  80. enum AAL_UPDATE_HIST {
  81. UPDATE_NONE = 0,
  82. UPDATE_SINGLE,
  83. UPDATE_MULTIPLE
  84. };
  85. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  86. /* #define DRE3_IN_DISP_AAL */
  87. /* HW specified */
  88. #define AAL_DRE_HIST_START (1152)
  89. #define AAL_DRE_HIST_END (4220)
  90. #define AAL_DRE_GAIN_START (4224)
  91. #define AAL_DRE_GAIN_END (6396)
  92. static DEFINE_SPINLOCK(g_aal_dre3_gain_lock);
  93. static atomic_t g_aal_force_hist_apb = ATOMIC_INIT(0);
  94. static atomic_t g_aal_dre_halt = ATOMIC_INIT(0);
  95. static atomic_t g_aal_dre_hw_init = ATOMIC_INIT(0);
  96. static struct DISP_DRE30_INIT g_aal_init_dre30;
  97. static struct DISP_DRE30_PARAM g_aal_gain;
  98. static struct DISP_DRE30_PARAM g_aal_gain_db;
  99. static struct DISP_DRE30_HIST g_aal_dre30_hist;
  100. static struct DISP_DRE30_HIST g_aal_dre30_hist_db;
  101. static atomic_t g_aal_change_to_dre30 = ATOMIC_INIT(0);
  102. static atomic_t g_aal_dre_config = ATOMIC_INIT(0);
  103. #define AAL_SRAM_SOF 1
  104. #define AAL_SRAM_EOF 0
  105. static u32 aal_sram_method = AAL_SRAM_SOF;
  106. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  107. static DECLARE_WAIT_QUEUE_HEAD(g_aal_size_wq);
  108. static bool g_aal_get_size_available;
  109. static struct DISP_AAL_DISPLAY_SIZE g_aal_size;
  110. static atomic_t g_aal_panel_type = ATOMIC_INIT(CONFIG_BY_CUSTOM_LIB);
  111. static int g_aal_ess_level = ESS_LEVEL_BY_CUSTOM_LIB;
  112. static int g_aal_dre_en = DRE_EN_BY_CUSTOM_LIB;
  113. static int g_aal_ess_en = ESS_EN_BY_CUSTOM_LIB;
  114. static int g_aal_ess_level_cmd_id;
  115. static int g_aal_dre_en_cmd_id;
  116. static int g_aal_ess_en_cmd_id;
  117. #define aal_min(a, b) (((a) < (b)) ? (a) : (b))
  118. enum AAL_IOCTL_CMD {
  119. INIT_REG = 0,
  120. SET_PARAM,
  121. EVENTCTL,
  122. FLIP_SRAM
  123. };
  124. struct dre3_node {
  125. struct device *dev;
  126. void __iomem *va;
  127. phys_addr_t pa;
  128. struct clk *clk;
  129. };
  130. struct mtk_disp_aal_data {
  131. bool support_shadow;
  132. int aal_dre_hist_start;
  133. int aal_dre_hist_end;
  134. int aal_dre_gain_start;
  135. int aal_dre_gain_end;
  136. int bitShift;
  137. };
  138. struct mtk_disp_aal {
  139. struct mtk_ddp_comp ddp_comp;
  140. struct drm_crtc *crtc;
  141. struct dre3_node dre3_hw;
  142. atomic_t dirty_frame_retrieved;
  143. atomic_t is_clock_on;
  144. const struct mtk_disp_aal_data *data;
  145. struct work_struct aal_flip_task;
  146. };
  147. static struct mtk_disp_aal *g_aal_data;
  148. static inline struct mtk_disp_aal *comp_to_aal(struct mtk_ddp_comp *comp)
  149. {
  150. return container_of(comp, struct mtk_disp_aal, ddp_comp);
  151. }
  152. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  153. static inline phys_addr_t mtk_aal_dre3_pa(struct mtk_ddp_comp *comp)
  154. {
  155. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  156. return (aal_data->dre3_hw.dev) ? aal_data->dre3_hw.pa : comp->regs_pa;
  157. }
  158. static inline void __iomem *mtk_aal_dre3_va(struct mtk_ddp_comp *comp)
  159. {
  160. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  161. return (aal_data->dre3_hw.dev) ? aal_data->dre3_hw.va : comp->regs;
  162. }
  163. static void mtk_aal_write_mask(void __iomem *address, u32 data, u32 mask)
  164. {
  165. u32 value = data;
  166. if (mask != ~0) {
  167. value = readl(address);
  168. value &= ~mask;
  169. data &= mask;
  170. value |= data;
  171. }
  172. writel(value, address);
  173. }
  174. #endif
  175. #define AALERR(fmt, arg...) pr_notice("[ERR]%s:" fmt, __func__, ##arg)
  176. static bool debug_flow_log;
  177. #define AALFLOW_LOG(fmt, arg...) do { \
  178. if (debug_flow_log) \
  179. pr_notice("[FLOW]%s:" fmt, __func__, ##arg); \
  180. } while (0)
  181. static bool debug_api_log;
  182. #define AALAPI_LOG(fmt, arg...) do { \
  183. if (debug_api_log) \
  184. pr_notice("[API]%s:" fmt, __func__, ##arg); \
  185. } while (0)
  186. static bool debug_write_cmdq_log;
  187. #define AALWC_LOG(fmt, arg...) do { \
  188. if (debug_write_cmdq_log) \
  189. pr_notice("[WC]%s:" fmt, __func__, ##arg); \
  190. } while (0)
  191. static bool debug_irq_log;
  192. #define AALIRQ_LOG(fmt, arg...) do { \
  193. if (debug_irq_log) \
  194. pr_notice("[IRQ]%s:" fmt, __func__, ##arg); \
  195. } while (0)
  196. /* config register which might have extra DRE3 aal hw */
  197. static inline s32 basic_cmdq_write(struct cmdq_pkt *handle,
  198. struct mtk_ddp_comp *comp, u32 offset, u32 value, u32 mask)
  199. {
  200. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  201. s32 result;
  202. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  203. phys_addr_t dre3_pa = mtk_aal_dre3_pa(comp);
  204. result = cmdq_pkt_write(handle, comp->cmdq_base,
  205. comp->regs_pa + offset, value, mask);
  206. if (result) {
  207. AALERR("write reg fail, offset:%#x\n", offset);
  208. return result;
  209. }
  210. AALWC_LOG("write 0x%03x with 0x%08x (0x%08x)\n",
  211. offset, value, mask);
  212. if (aal_data->dre3_hw.dev)
  213. result = cmdq_pkt_write(handle, comp->cmdq_base,
  214. dre3_pa + offset, value, mask);
  215. return result;
  216. #else
  217. return cmdq_pkt_write(handle, comp->cmdq_base,
  218. comp->regs_pa + offset, value, mask);
  219. #endif
  220. }
  221. static int disp_aal_get_cust_led(void)
  222. {
  223. struct device_node *led_node = NULL;
  224. int ret = 0;
  225. int led_mode;
  226. int pwm_config[5] = { 0 };
  227. led_node = of_find_compatible_node(NULL, NULL,
  228. "mediatek,lcd-backlight");
  229. if (!led_node) {
  230. ret = -1;
  231. pr_notice("Cannot find LED node from dts\n");
  232. } else {
  233. ret = of_property_read_u32(led_node, "led_mode", &led_mode);
  234. if (!ret)
  235. atomic_set(&g_led_mode, led_mode);
  236. else
  237. pr_notice("led dts can not get led mode data.\n");
  238. ret = of_property_read_u32_array(led_node,
  239. "pwm_config", pwm_config, ARRAY_SIZE(pwm_config));
  240. }
  241. if (ret)
  242. pr_notice("get pwm cust info fail\n");
  243. pr_notice("%s mode=%u\n", __func__, atomic_read(&g_led_mode));
  244. return ret;
  245. }
  246. #define LOG_INTERVAL_TH 200
  247. #define LOG_BUFFER_SIZE 4
  248. static char g_aal_log_buffer[256] = "";
  249. static int g_aal_log_index;
  250. struct timeval g_aal_log_prevtime = {0};
  251. bool disp_aal_is_support(void)
  252. {
  253. #ifdef CONFIG_MTK_AAL_SUPPORT
  254. return true;
  255. #else
  256. return false;
  257. #endif // CONFIG_MTK_AAL_SUPPORT
  258. }
  259. static void disp_aal_set_interrupt(struct mtk_ddp_comp *comp, int enable)
  260. {
  261. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  262. if (!disp_aal_is_support()) {
  263. AALIRQ_LOG("aal is not support\n");
  264. return;
  265. }
  266. if (enable && atomic_read(&g_aal_force_relay) != 1) {
  267. /* Enable output frame end interrupt */
  268. if (comp == NULL)
  269. writel(0x2, default_comp->regs + DISP_AAL_INTEN);
  270. else
  271. writel(0x2, comp->regs + DISP_AAL_INTEN);
  272. AALIRQ_LOG("interrupt enabled\n");
  273. } else if (!enable) {
  274. if (atomic_read(&aal_data->dirty_frame_retrieved) == 1) {
  275. if (comp == NULL)
  276. writel(0x0,
  277. default_comp->regs + DISP_AAL_INTEN);
  278. else
  279. writel(0x0, comp->regs + DISP_AAL_INTEN);
  280. AALIRQ_LOG("interrupt disabled");
  281. } //else {
  282. /* Dirty histogram was not retrieved. */
  283. /* Only if the dirty hist was retrieved, */
  284. /* interrupt can be disabled. */
  285. /* Continue interrupt until AALService can get */
  286. /* the latest histogram. */
  287. //}
  288. }
  289. }
  290. static unsigned long timevaldiff(struct timeval *starttime,
  291. struct timeval *finishtime)
  292. {
  293. unsigned long msec;
  294. msec = (finishtime->tv_sec-starttime->tv_sec)*1000;
  295. msec += (finishtime->tv_usec-starttime->tv_usec)/1000;
  296. return msec;
  297. }
  298. static void disp_aal_notify_backlight_log(int bl_1024)
  299. {
  300. struct timeval aal_time;
  301. unsigned long diff_mesc = 0;
  302. unsigned long tsec;
  303. unsigned long tusec;
  304. do_gettimeofday(&aal_time);
  305. tsec = (unsigned long)aal_time.tv_sec % 100;
  306. tusec = (unsigned long)aal_time.tv_usec / 1000;
  307. diff_mesc = timevaldiff(&g_aal_log_prevtime, &aal_time);
  308. if (!debug_api_log)
  309. return;
  310. pr_notice("time diff = %lu\n", diff_mesc);
  311. if (diff_mesc > LOG_INTERVAL_TH) {
  312. if (g_aal_log_index == 0) {
  313. pr_notice("%s: %d/1023\n", __func__, bl_1024);
  314. } else {
  315. sprintf(g_aal_log_buffer + strlen(g_aal_log_buffer),
  316. "%s, %d/1023 %03lu.%03lu", __func__,
  317. bl_1024, tsec, tusec);
  318. pr_notice("%s\n", g_aal_log_buffer);
  319. g_aal_log_index = 0;
  320. }
  321. } else {
  322. if (g_aal_log_index == 0) {
  323. sprintf(g_aal_log_buffer,
  324. "%s %d/1023 %03lu.%03lu", __func__,
  325. bl_1024, tsec, tusec);
  326. g_aal_log_index += 1;
  327. } else {
  328. sprintf(g_aal_log_buffer + strlen(g_aal_log_buffer),
  329. "%s, %d/1023 %03lu.%03lu", __func__,
  330. bl_1024, tsec, tusec);
  331. g_aal_log_index += 1;
  332. }
  333. if ((g_aal_log_index >= LOG_BUFFER_SIZE) || (bl_1024 == 0)) {
  334. pr_notice("%s\n", g_aal_log_buffer);
  335. g_aal_log_index = 0;
  336. }
  337. }
  338. memcpy(&g_aal_log_prevtime, &aal_time, sizeof(struct timeval));
  339. }
  340. void disp_aal_refresh_by_kernel(void)
  341. {
  342. unsigned long flags, clockflags;
  343. if (atomic_read(&g_aal_is_init_regs_valid) == 1) {
  344. spin_lock_irqsave(&g_aal_irq_en_lock, flags);
  345. atomic_set(&g_aal_force_enable_irq, 1);
  346. if (spin_trylock_irqsave(&g_aal_clock_lock, clockflags)) {
  347. if (atomic_read(&g_aal_data->is_clock_on) != 1)
  348. AALFLOW_LOG("clock is off\n");
  349. else
  350. disp_aal_set_interrupt(NULL, true);
  351. spin_unlock_irqrestore(&g_aal_clock_lock, clockflags);
  352. }
  353. spin_unlock_irqrestore(&g_aal_irq_en_lock, flags);
  354. /* Backlight or Kernel API latency should be smallest */
  355. mtk_crtc_check_trigger(default_comp->mtk_crtc, false, true);
  356. }
  357. }
  358. void disp_aal_notify_backlight_changed(int bl_1024)
  359. {
  360. unsigned long flags;
  361. int max_backlight = 0;
  362. unsigned int service_flags;
  363. AALAPI_LOG("%d/1023\n", bl_1024);
  364. disp_aal_notify_backlight_log(bl_1024);
  365. //disp_aal_exit_idle(__func__, 1);
  366. // FIXME
  367. //max_backlight = disp_pwm_get_max_backlight(DISP_PWM0);
  368. max_backlight = 1024;
  369. if (bl_1024 > max_backlight)
  370. bl_1024 = max_backlight;
  371. atomic_set(&g_aal_backlight_notified, bl_1024);
  372. service_flags = 0;
  373. if (bl_1024 == 0) {
  374. mt_leds_brightness_set("lcd-backlight", 0);
  375. /* set backlight = 0 may be not from AAL, */
  376. /* we have to let AALService can turn on backlight */
  377. /* on phone resumption */
  378. service_flags = AAL_SERVICE_FORCE_UPDATE;
  379. } else if (atomic_read(&g_aal_is_init_regs_valid) == 0 ||
  380. atomic_read(&g_aal_force_relay) == 1) {
  381. /* AAL Service is not running */
  382. mt_leds_brightness_set("lcd-backlight", bl_1024);
  383. }
  384. spin_lock_irqsave(&g_aal_hist_lock, flags);
  385. g_aal_hist.backlight = bl_1024;
  386. g_aal_hist.serviceFlags |= service_flags;
  387. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  388. // always notify aal service for LED changed
  389. mtk_drm_idlemgr_kick(__func__, &default_comp->mtk_crtc->base, 1);
  390. disp_aal_refresh_by_kernel();
  391. }
  392. #ifdef CONFIG_LEDS_BRIGHTNESS_CHANGED
  393. int led_brightness_changed_event(struct notifier_block *nb, unsigned long event,
  394. void *v)
  395. {
  396. int trans_level;
  397. struct led_conf_info *led_conf;
  398. led_conf = (struct led_conf_info *)v;
  399. switch (event) {
  400. case 1:
  401. trans_level = (
  402. (((1 << led_conf->trans_bits) - 1)
  403. * led_conf->cdev.brightness
  404. + ((led_conf->cdev.max_brightness) / 2))
  405. / (led_conf->cdev.max_brightness));
  406. disp_aal_notify_backlight_changed(trans_level);
  407. AALAPI_LOG("brightness changed: %d(%d)\n",
  408. trans_level, led_conf->cdev.brightness);
  409. break;
  410. case 2:
  411. disp_aal_notify_backlight_changed(0);
  412. break;
  413. case 3:
  414. trans_level = (
  415. (((1 << led_conf->trans_bits) - 1)
  416. * led_conf->max_level
  417. + ((led_conf->cdev.max_brightness) / 2))
  418. / (led_conf->cdev.max_brightness));
  419. AALAPI_LOG("set Maxbrightness %d(%d)\n",
  420. trans_level, led_conf->max_level);
  421. break;
  422. default:
  423. break;
  424. }
  425. return NOTIFY_DONE;
  426. }
  427. static struct notifier_block leds_init_notifier = {
  428. .notifier_call = led_brightness_changed_event,
  429. };
  430. #endif
  431. int mtk_drm_ioctl_aal_eventctl(struct drm_device *dev, void *data,
  432. struct drm_file *file_priv)
  433. {
  434. struct mtk_drm_private *private = dev->dev_private;
  435. struct mtk_ddp_comp *comp = private->ddp_comp[DDP_COMPONENT_AAL0];
  436. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  437. #ifdef CONFIG_MTK_DRE30_SUPPORT
  438. struct drm_crtc *crtc = private->crtc[0];
  439. #endif
  440. int ret = 0;
  441. unsigned long flags, clockflags;
  442. int *enabled = (int *)data;
  443. AALFLOW_LOG("%d\n", *enabled);
  444. spin_lock_irqsave(&g_aal_irq_en_lock, flags);
  445. if (atomic_read(&g_aal_force_enable_irq) == 1) {
  446. if (*enabled == 0)
  447. AALFLOW_LOG("force enable aal ieq 0 -> 1\n");
  448. *enabled = 1;
  449. }
  450. if (spin_trylock_irqsave(&g_aal_clock_lock, clockflags)) {
  451. if (atomic_read(&aal_data->is_clock_on) != 1) {
  452. AALFLOW_LOG("clock is off\n");
  453. ret = -EFAULT;
  454. } else
  455. disp_aal_set_interrupt(comp, *enabled);
  456. spin_unlock_irqrestore(&g_aal_clock_lock, clockflags);
  457. }
  458. spin_unlock_irqrestore(&g_aal_irq_en_lock, flags);
  459. if (*enabled) {
  460. #ifdef CONFIG_MTK_DRE30_SUPPORT
  461. mtk_crtc_user_cmd(crtc, comp, EVENTCTL, data);
  462. mtk_crtc_check_trigger(comp->mtk_crtc, true, true);
  463. #else
  464. mtk_crtc_check_trigger(comp->mtk_crtc, true, true);
  465. #endif
  466. }
  467. return ret;
  468. }
  469. static void mtk_crtc_user_cmd_work(struct work_struct *work_item)
  470. {
  471. mtk_crtc_user_cmd(g_aal_data->crtc, default_comp, FLIP_SRAM, NULL);
  472. }
  473. void disp_aal_flip_sram(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  474. const char *caller)
  475. {
  476. #ifdef CONFIG_MTK_DRE30_SUPPORT
  477. u32 hist_apb = 0, hist_int = 0, sram_cfg;
  478. phys_addr_t dre3_pa = mtk_aal_dre3_pa(comp);
  479. if (aal_sram_method != AAL_SRAM_SOF)
  480. return;
  481. if (atomic_read(&g_aal_dre_config) == 1) {
  482. AALFLOW_LOG("[SRAM] g_aal_dre_config not 0 in %s", caller);
  483. return;
  484. }
  485. atomic_set(&g_aal_dre_config, 1);
  486. if (atomic_cmpxchg(&g_aal_force_hist_apb, 0, 1) == 0) {
  487. hist_apb = 0;
  488. hist_int = 1;
  489. } else if (atomic_cmpxchg(&g_aal_force_hist_apb, 1, 0) == 1) {
  490. hist_apb = 1;
  491. hist_int = 0;
  492. } else {
  493. AALERR("[SRAM] Error when get hist_apb in %s", caller);
  494. }
  495. sram_cfg = (hist_int << 6)|(hist_apb << 5)|(1 << 4);
  496. AALFLOW_LOG("[SRAM] hist_apb(%d) hist_int(%d) 0x%08x in %s",
  497. hist_apb, hist_int, sram_cfg, caller);
  498. cmdq_pkt_write(handle, comp->cmdq_base,
  499. dre3_pa + DISP_AAL_SRAM_CFG, sram_cfg, (0x7 << 4));
  500. #endif
  501. }
  502. void disp_aal_first_flip_sram(struct mtk_ddp_comp *comp,
  503. struct cmdq_pkt *handle, const char *caller)
  504. {
  505. #ifdef CONFIG_MTK_DRE30_SUPPORT
  506. u32 hist_apb, hist_int, sram_cfg;
  507. phys_addr_t dre3_pa = mtk_aal_dre3_pa(comp);
  508. if (aal_sram_method != AAL_SRAM_SOF)
  509. return;
  510. if (atomic_cmpxchg(&g_aal_force_hist_apb, 0, 1) == 0) {
  511. hist_apb = 0;
  512. hist_int = 1;
  513. } else if (atomic_cmpxchg(&g_aal_force_hist_apb, 1, 0) == 1) {
  514. hist_apb = 1;
  515. hist_int = 0;
  516. } else {
  517. AALERR("[SRAM] Error when get hist_apb in %s", caller);
  518. }
  519. sram_cfg = (hist_int << 6)|(hist_apb << 5)|(1 << 4);
  520. AALFLOW_LOG("[SRAM] hist_apb(%d) hist_int(%d) 0x%08x in %s",
  521. hist_apb, hist_int, sram_cfg, caller);
  522. cmdq_pkt_write(handle, comp->cmdq_base,
  523. dre3_pa + DISP_AAL_SRAM_CFG, sram_cfg, (0x7 << 4));
  524. #endif
  525. }
  526. static void mtk_aal_init(struct mtk_ddp_comp *comp,
  527. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  528. {
  529. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  530. AALFLOW_LOG("+\n");
  531. if (disp_aal_is_support() == true &&
  532. atomic_read(&g_aal_force_relay) != 1) {
  533. AALFLOW_LOG("Enable AAL histogram\n");
  534. // Enable AAL histogram, engine
  535. cmdq_pkt_write(handle, comp->cmdq_base,
  536. comp->regs_pa + DISP_AAL_CFG, 0x3 << 1, (0x3 << 1));
  537. } else {
  538. AALFLOW_LOG("Disable AAL histogram\n");
  539. // Disable AAL histogram, engine
  540. cmdq_pkt_write(handle, comp->cmdq_base,
  541. comp->regs_pa + DISP_AAL_CFG, 0x0 << 1, (0x3 << 1));
  542. }
  543. /* get lcd-backlight mode from dts */
  544. if (atomic_read(&g_led_mode) == MT65XX_LED_MODE_NONE)
  545. disp_aal_get_cust_led();
  546. atomic_set(&g_aal_hist_available, 0);
  547. atomic_set(&g_aal_eof_irq, 0);
  548. atomic_set(&aal_data->dirty_frame_retrieved, 1);
  549. AALFLOW_LOG("led mode: %d-\n", atomic_read(&g_led_mode));
  550. }
  551. static bool debug_bypass_alg_mode;
  552. static void mtk_aal_config(struct mtk_ddp_comp *comp,
  553. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  554. {
  555. unsigned int val = 0;
  556. int width = cfg->w, height = cfg->h;
  557. AALFLOW_LOG("(w,h)=(%d,%d)+, %d\n",
  558. width, height, g_aal_get_size_available);
  559. if (g_aal_get_size_available == false) {
  560. g_aal_size.height = height;
  561. g_aal_size.width = width;
  562. g_aal_get_size_available = true;
  563. wake_up_interruptible(&g_aal_size_wq);
  564. AALFLOW_LOG("size available: (w,h)=(%d,%d)+\n", width, height);
  565. }
  566. val = (width << 16) | (height);
  567. cmdq_pkt_write(handle, comp->cmdq_base,
  568. comp->regs_pa + DISP_AAL_SIZE, val, ~0);
  569. cmdq_pkt_write(handle, comp->cmdq_base,
  570. comp->regs_pa + DISP_AAL_OUTPUT_SIZE, val, ~0);
  571. cmdq_pkt_write(handle, comp->cmdq_base,
  572. comp->regs_pa + DISP_AAL_OUTPUT_OFFSET,
  573. (0 << 16) | 0, ~0);
  574. if (atomic_read(&g_aal_force_relay) == 1) {
  575. // Set reply mode
  576. AALFLOW_LOG("g_aal_force_relay\n");
  577. cmdq_pkt_write(handle, comp->cmdq_base,
  578. comp->regs_pa + DISP_AAL_CFG, 1, 1);
  579. } else {
  580. // Disable reply mode
  581. cmdq_pkt_write(handle, comp->cmdq_base,
  582. comp->regs_pa + DISP_AAL_CFG, 0, 1);
  583. }
  584. mtk_aal_init(comp, cfg, handle);
  585. disp_aal_flip_sram(comp, handle, __func__);
  586. AALWC_LOG("AAL_CFG=0x%x\n",
  587. readl(comp->regs + DISP_AAL_CFG));
  588. }
  589. static void disp_aal_wait_hist(void)
  590. {
  591. int ret = 0;
  592. if (atomic_read(&g_aal_hist_available) == 0) {
  593. AALFLOW_LOG("wait_event_interruptible\n");
  594. ret = wait_event_interruptible(g_aal_hist_wq,
  595. (atomic_read(&g_aal_hist_available) == 1 &&
  596. atomic_read(&g_aal_eof_irq) == 1));
  597. AALFLOW_LOG("hist_available = 1, waken up, ret = %d", ret);
  598. } else
  599. AALFLOW_LOG("hist_available = 0");
  600. }
  601. static bool disp_aal_read_single_hist(struct mtk_ddp_comp *comp)
  602. {
  603. bool read_success = true;
  604. int i;
  605. if (atomic_read(&g_aal_eof_irq) == 0)
  606. return false;
  607. for (i = 0; i < AAL_HIST_BIN; i++) {
  608. g_aal_hist.maxHist[i] = readl(comp->regs +
  609. DISP_AAL_STATUS_00 + (i << 2));
  610. }
  611. for (i = 0; i < AAL_HIST_BIN; i++) {
  612. g_aal_hist.yHist[i] = readl(comp->regs +
  613. DISP_Y_HISTOGRAM_00 + (i << 2));
  614. }
  615. read_success = disp_color_reg_get(comp, DISP_COLOR_TWO_D_W1_RESULT,
  616. &g_aal_hist.colorHist);
  617. return read_success;
  618. }
  619. static int disp_aal_copy_hist_to_user(struct DISP_AAL_HIST *hist)
  620. {
  621. unsigned long flags;
  622. int ret = 0;
  623. if (hist == NULL) {
  624. AALERR("%s DstHist is NULL\n", __func__);
  625. return -1;
  626. }
  627. /* We assume only one thread will call this function */
  628. spin_lock_irqsave(&g_aal_hist_lock, flags);
  629. memcpy(&g_aal_hist_db, &g_aal_hist, sizeof(g_aal_hist));
  630. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  631. memcpy(&g_aal_dre30_hist_db, &g_aal_dre30_hist,
  632. sizeof(g_aal_dre30_hist));
  633. #endif
  634. g_aal_hist.panel_type = atomic_read(&g_aal_panel_type);
  635. g_aal_hist.essStrengthIndex = g_aal_ess_level;
  636. g_aal_hist.ess_enable = g_aal_ess_en;
  637. g_aal_hist.dre_enable = g_aal_dre_en;
  638. g_aal_hist.serviceFlags = 0;
  639. atomic_set(&g_aal_hist_available, 0);
  640. atomic_set(&g_aal_eof_irq, 0);
  641. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  642. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  643. g_aal_hist_db.dre30_hist = g_aal_init_dre30.dre30_hist_addr;
  644. #endif
  645. memcpy(hist, &g_aal_hist_db, sizeof(g_aal_hist_db));
  646. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  647. ret = copy_to_user(AAL_U32_PTR(g_aal_init_dre30.dre30_hist_addr),
  648. &g_aal_dre30_hist_db, sizeof(g_aal_dre30_hist_db));
  649. #endif
  650. AALFLOW_LOG("%s set g_aal_force_enable_irq to 0 +\n", __func__);
  651. atomic_set(&g_aal_force_enable_irq, 0);
  652. return ret;
  653. }
  654. void dump_hist(struct DISP_AAL_HIST *data)
  655. {
  656. int i = 0;
  657. pr_notice("maxHist:\n");
  658. for (i = 0; i < 3; i++) {
  659. pr_notice("%d %d %d %d %d %d %d %d %d %d",
  660. data->maxHist[i*10 + 0], data->maxHist[i*10 + 1],
  661. data->maxHist[i*10 + 2], data->maxHist[i*10 + 3],
  662. data->maxHist[i*10 + 4], data->maxHist[i*10 + 5],
  663. data->maxHist[i*10 + 6], data->maxHist[i*10 + 7],
  664. data->maxHist[i*10 + 9], data->maxHist[i*10 + 9]);
  665. }
  666. pr_notice("%d %d %d", data->maxHist[30], data->maxHist[31],
  667. data->maxHist[32]);
  668. pr_notice("yHist:\n");
  669. for (i = 0; i < 3; i++) {
  670. pr_notice("%d %d %d %d %d %d %d %d %d %d",
  671. data->yHist[i*10 + 0], data->yHist[i*10 + 1],
  672. data->yHist[i*10 + 2], data->yHist[i*10 + 3],
  673. data->yHist[i*10 + 4], data->yHist[i*10 + 5],
  674. data->yHist[i*10 + 6], data->yHist[i*10 + 7],
  675. data->yHist[i*10 + 9], data->yHist[i*10 + 9]);
  676. }
  677. pr_notice("%d %d %d", data->yHist[30], data->yHist[31],
  678. data->yHist[32]);
  679. pr_notice("serviceFlags:%u, backlight: %d, colorHist: %d\n",
  680. data->serviceFlags, data->backlight, data->colorHist);
  681. pr_notice("requestPartial:%d, panel_type: %u\n",
  682. data->requestPartial, data->panel_type);
  683. pr_notice("essStrengthIndex:%d, ess_enable: %d, dre_enable: %d\n",
  684. data->essStrengthIndex, data->ess_enable,
  685. data->dre_enable);
  686. }
  687. static bool debug_dump_aal_hist;
  688. int mtk_drm_ioctl_aal_get_hist(struct drm_device *dev, void *data,
  689. struct drm_file *file_priv)
  690. {
  691. disp_aal_wait_hist();
  692. if (disp_aal_copy_hist_to_user((struct DISP_AAL_HIST *) data) < 0)
  693. return -EFAULT;
  694. if (debug_dump_aal_hist)
  695. dump_hist(data);
  696. return 0;
  697. }
  698. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  699. static void disp_aal_dre3_config(struct mtk_ddp_comp *comp,
  700. struct cmdq_pkt *handle,
  701. const struct DISP_AAL_INITREG *init_regs)
  702. {
  703. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  704. phys_addr_t dre3_pa = mtk_aal_dre3_pa(comp);
  705. int dre_alg_mode = 1;
  706. AALFLOW_LOG("start, bitShift: %d\n", aal_data->data->bitShift);
  707. cmdq_pkt_write(handle, comp->cmdq_base,
  708. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_00,
  709. (g_aal_size.width - 1) << (aal_data->data->bitShift), ~0);
  710. cmdq_pkt_write(handle, comp->cmdq_base,
  711. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_01,
  712. (init_regs->dre_blk_y_num << 5) | init_regs->dre_blk_x_num,
  713. ~0);
  714. cmdq_pkt_write(handle, comp->cmdq_base,
  715. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_02,
  716. (init_regs->dre_blk_height << (aal_data->data->bitShift)) |
  717. init_regs->dre_blk_width, ~0);
  718. cmdq_pkt_write(handle, comp->cmdq_base,
  719. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_04,
  720. (init_regs->dre_flat_length_slope << 13) |
  721. init_regs->dre_flat_length_th, ~0);
  722. cmdq_pkt_write(handle, comp->cmdq_base,
  723. dre3_pa + DISP_AAL_DRE_CHROMA_HIST_00,
  724. (init_regs->dre_s_upper << 24) |
  725. (init_regs->dre_s_lower << 16) |
  726. (init_regs->dre_y_upper << 8) | init_regs->dre_y_lower, ~0);
  727. cmdq_pkt_write(handle, comp->cmdq_base,
  728. dre3_pa + DISP_AAL_DRE_CHROMA_HIST_01,
  729. (init_regs->dre_h_slope << 24) |
  730. (init_regs->dre_s_slope << 20) |
  731. (init_regs->dre_y_slope << 16) |
  732. (init_regs->dre_h_upper << 8) | init_regs->dre_h_lower, ~0);
  733. cmdq_pkt_write(handle, comp->cmdq_base,
  734. dre3_pa + DISP_AAL_DRE_ALPHA_BLEND_00,
  735. (init_regs->dre_y_alpha_shift_bit << 25) |
  736. (init_regs->dre_y_alpha_base << 16) |
  737. (init_regs->dre_x_alpha_shift_bit << 9) |
  738. init_regs->dre_x_alpha_base, ~0);
  739. cmdq_pkt_write(handle, comp->cmdq_base,
  740. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_05,
  741. init_regs->dre_blk_area, ~0);
  742. cmdq_pkt_write(handle, comp->cmdq_base,
  743. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_06,
  744. init_regs->dre_blk_area_min, ~0);
  745. cmdq_pkt_write(handle, comp->cmdq_base,
  746. dre3_pa + DISP_AAL_DRE_BLOCK_INFO_07,
  747. (g_aal_size.height - 1) << (aal_data->data->bitShift), ~0);
  748. cmdq_pkt_write(handle, comp->cmdq_base,
  749. dre3_pa + DISP_AAL_SRAM_CFG,
  750. init_regs->hist_bin_type, 0x1);
  751. #if defined(DRE3_IN_DISP_AAL)
  752. cmdq_pkt_write(handle, comp->cmdq_base,
  753. dre3_pa + DISP_AAL_DUAL_PIPE_INFO_00,
  754. (0 << 13) | 0, ~0);
  755. cmdq_pkt_write(handle, comp->cmdq_base,
  756. dre3_pa + DISP_AAL_DUAL_PIPE_INFO_01,
  757. ((init_regs->dre_blk_x_num-1) << 13) |
  758. (init_regs->dre_blk_width-1), ~0);
  759. #else
  760. cmdq_pkt_write(handle, comp->cmdq_base,
  761. dre3_pa + MDP_AAL_TILE_00,
  762. (0x1 << 21) | (0x1 << 20) |
  763. (init_regs->blk_num_x_end << 15) |
  764. (init_regs->blk_num_x_start << 10) |
  765. (init_regs->blk_num_y_end << 5) |
  766. init_regs->blk_num_y_start, ~0);
  767. cmdq_pkt_write(handle, comp->cmdq_base,
  768. dre3_pa + MDP_AAL_TILE_01,
  769. (init_regs->blk_cnt_x_end << (aal_data->data->bitShift)) |
  770. init_regs->blk_cnt_x_start, ~0);
  771. cmdq_pkt_write(handle, comp->cmdq_base,
  772. dre3_pa + MDP_AAL_TILE_02,
  773. (init_regs->blk_cnt_y_end << (aal_data->data->bitShift)) |
  774. init_regs->blk_cnt_y_start, ~0);
  775. #endif
  776. /* Change to Local DRE version */
  777. if (debug_bypass_alg_mode)
  778. dre_alg_mode = 0;
  779. cmdq_pkt_write(handle, comp->cmdq_base,
  780. dre3_pa + DISP_AAL_CFG_MAIN,
  781. dre_alg_mode << 4, 1 << 4);
  782. atomic_or(0x1, &g_aal_change_to_dre30);
  783. }
  784. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  785. #define CABC_GAINLMT(v0, v1, v2) (((v2) << 20) | ((v1) << 10) | (v0))
  786. static struct DISP_AAL_INITREG g_aal_init_regs;
  787. static int disp_aal_write_init_regs(struct mtk_ddp_comp *comp,
  788. struct cmdq_pkt *handle)
  789. {
  790. int ret = -EFAULT;
  791. if (atomic_read(&g_aal_is_init_regs_valid) == 1) {
  792. struct DISP_AAL_INITREG *init_regs = &g_aal_init_regs;
  793. int i, j = 0;
  794. int *gain;
  795. gain = init_regs->cabc_gainlmt;
  796. basic_cmdq_write(handle, comp, DISP_AAL_DRE_MAPPING_00,
  797. (init_regs->dre_map_bypass << 4), 1 << 4);
  798. for (i = 0; i <= 10; i++) {
  799. cmdq_pkt_write(handle, comp->cmdq_base,
  800. comp->regs_pa + DISP_AAL_CABC_GAINLMT_TBL(i),
  801. CABC_GAINLMT(gain[j], gain[j + 1], gain[j + 2]),
  802. ~0);
  803. j += 3;
  804. }
  805. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  806. disp_aal_dre3_config(comp, handle, init_regs);
  807. #endif
  808. AALFLOW_LOG("init done\n");
  809. ret = 0;
  810. }
  811. return ret;
  812. }
  813. #define PRINT_INIT_REG(x1) pr_notice("[INIT]%s=0x%x\n", #x1, data->x1)
  814. void dump_init_reg(struct DISP_AAL_INITREG *data)
  815. {
  816. PRINT_INIT_REG(dre_s_lower);
  817. PRINT_INIT_REG(dre_s_upper);
  818. PRINT_INIT_REG(dre_y_lower);
  819. PRINT_INIT_REG(dre_y_upper);
  820. PRINT_INIT_REG(dre_h_lower);
  821. PRINT_INIT_REG(dre_h_upper);
  822. PRINT_INIT_REG(dre_h_slope);
  823. PRINT_INIT_REG(dre_s_slope);
  824. PRINT_INIT_REG(dre_y_slope);
  825. PRINT_INIT_REG(dre_x_alpha_base);
  826. PRINT_INIT_REG(dre_x_alpha_shift_bit);
  827. PRINT_INIT_REG(dre_y_alpha_base);
  828. PRINT_INIT_REG(dre_y_alpha_shift_bit);
  829. PRINT_INIT_REG(dre_blk_x_num);
  830. PRINT_INIT_REG(dre_blk_y_num);
  831. PRINT_INIT_REG(dre_blk_height);
  832. PRINT_INIT_REG(dre_blk_width);
  833. PRINT_INIT_REG(dre_blk_area);
  834. PRINT_INIT_REG(dre_blk_area_min);
  835. PRINT_INIT_REG(hist_bin_type);
  836. PRINT_INIT_REG(dre_flat_length_slope);
  837. PRINT_INIT_REG(dre_flat_length_th);
  838. PRINT_INIT_REG(blk_num_x_start);
  839. PRINT_INIT_REG(blk_num_x_end);
  840. PRINT_INIT_REG(blk_cnt_x_start);
  841. PRINT_INIT_REG(blk_cnt_x_end);
  842. PRINT_INIT_REG(blk_num_y_start);
  843. PRINT_INIT_REG(blk_num_y_end);
  844. PRINT_INIT_REG(blk_cnt_y_start);
  845. PRINT_INIT_REG(blk_cnt_y_end);
  846. }
  847. static bool debug_dump_init_reg = true;
  848. static int disp_aal_set_init_reg(struct mtk_ddp_comp *comp,
  849. struct cmdq_pkt *handle, struct DISP_AAL_INITREG *user_regs)
  850. {
  851. int ret = -EFAULT;
  852. struct DISP_AAL_INITREG *init_regs;
  853. if (!disp_aal_is_support())
  854. return ret;
  855. init_regs = &g_aal_init_regs;
  856. memcpy(init_regs, user_regs, sizeof(*init_regs));
  857. if (debug_dump_init_reg)
  858. dump_init_reg(init_regs);
  859. atomic_set(&g_aal_is_init_regs_valid, 1);
  860. AALFLOW_LOG("Set init reg: %lu\n", sizeof(*init_regs));
  861. AALFLOW_LOG("init_reg.dre_map_bypass:%d\n", init_regs->dre_map_bypass);
  862. ret = disp_aal_write_init_regs(comp, handle);
  863. AALFLOW_LOG("ret = %d\n", ret);
  864. return ret;
  865. }
  866. int mtk_drm_ioctl_aal_init_reg(struct drm_device *dev, void *data,
  867. struct drm_file *file_priv)
  868. {
  869. struct mtk_drm_private *private = dev->dev_private;
  870. struct mtk_ddp_comp *comp = private->ddp_comp[DDP_COMPONENT_AAL0];
  871. struct drm_crtc *crtc = private->crtc[0];
  872. g_aal_data->crtc = crtc;
  873. return mtk_crtc_user_cmd(crtc, comp, INIT_REG, data);
  874. }
  875. static struct DISP_AAL_PARAM g_aal_param;
  876. #define DRE_REG_2(v0, off0, v1, off1) (((v1) << (off1)) | \
  877. ((v0) << (off0)))
  878. #define DRE_REG_3(v0, off0, v1, off1, v2, off2) \
  879. (((v2) << (off2)) | (v1 << (off1)) | ((v0) << (off0)))
  880. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  881. static int disp_aal_write_dre3_to_reg(struct mtk_ddp_comp *comp,
  882. struct cmdq_pkt *handle, const struct DISP_AAL_PARAM *param)
  883. {
  884. unsigned long flags;
  885. AALFLOW_LOG("\n");
  886. if (atomic_read(&g_aal_change_to_dre30) == 0x3) {
  887. if (copy_from_user(&g_aal_gain_db,
  888. AAL_U32_PTR(param->dre30_gain),
  889. sizeof(g_aal_gain_db)) == 0) {
  890. spin_lock_irqsave(&g_aal_dre3_gain_lock, flags);
  891. memcpy(&g_aal_gain, &g_aal_gain_db,
  892. sizeof(g_aal_gain));
  893. spin_unlock_irqrestore(&g_aal_dre3_gain_lock, flags);
  894. }
  895. }
  896. return 0;
  897. }
  898. #endif
  899. static int disp_aal_write_dre_to_reg(struct mtk_ddp_comp *comp,
  900. struct cmdq_pkt *handle, const struct DISP_AAL_PARAM *param)
  901. {
  902. const int *gain;
  903. gain = param->DREGainFltStatus;
  904. #if defined(CONFIG_MACH_MT6885) || defined(CONFIG_MACH_MT6873) \
  905. || defined(CONFIG_MACH_MT6893) || defined(CONFIG_MACH_MT6853) \
  906. || defined(CONFIG_MACH_MT6833)
  907. cmdq_pkt_write(handle, comp->cmdq_base,
  908. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(0),
  909. DRE_REG_2(gain[0], 0, gain[1], 14), ~0);
  910. cmdq_pkt_write(handle, comp->cmdq_base,
  911. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(1),
  912. DRE_REG_2(gain[2], 0, gain[3], 13), ~0);
  913. cmdq_pkt_write(handle, comp->cmdq_base,
  914. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(2),
  915. DRE_REG_2(gain[4], 0, gain[5], 12), ~0);
  916. cmdq_pkt_write(handle, comp->cmdq_base,
  917. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(3),
  918. DRE_REG_2(gain[6], 0, gain[7], 11), ~0);
  919. cmdq_pkt_write(handle, comp->cmdq_base,
  920. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(4),
  921. DRE_REG_2(gain[8], 0, gain[9], 11), ~0);
  922. cmdq_pkt_write(handle, comp->cmdq_base,
  923. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(5),
  924. DRE_REG_2(gain[10], 0, gain[11], 11), ~0);
  925. cmdq_pkt_write(handle, comp->cmdq_base,
  926. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(6),
  927. DRE_REG_3(gain[12], 0, gain[13], 11, gain[14], 22), ~0);
  928. cmdq_pkt_write(handle, comp->cmdq_base,
  929. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(7),
  930. DRE_REG_3(gain[15], 0, gain[16], 10, gain[17], 20), ~0);
  931. cmdq_pkt_write(handle, comp->cmdq_base,
  932. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(8),
  933. DRE_REG_3(gain[18], 0, gain[19], 10, gain[20], 20), ~0);
  934. cmdq_pkt_write(handle, comp->cmdq_base,
  935. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(9),
  936. DRE_REG_3(gain[21], 0, gain[22], 9, gain[23], 18), ~0);
  937. cmdq_pkt_write(handle, comp->cmdq_base,
  938. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(10),
  939. DRE_REG_3(gain[24], 0, gain[25], 9, gain[26], 18), ~0);
  940. /* Write dre curve to different register */
  941. cmdq_pkt_write(handle, comp->cmdq_base,
  942. comp->regs_pa + DISP_AAL_DRE_FLT_FORCE(11),
  943. DRE_REG_2(gain[27], 0, gain[28], 9), ~0);
  944. return 0;
  945. }
  946. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  947. #if defined(CONFIG_MTK_DRE30_SUPPORT) || !defined(NOT_SUPPORT_CABC_HW)
  948. static int disp_aal_write_cabc_to_reg(struct mtk_ddp_comp *comp,
  949. struct cmdq_pkt *handle, const struct DISP_AAL_PARAM *param)
  950. {
  951. int i;
  952. const int *gain;
  953. AALFLOW_LOG("\n");
  954. cmdq_pkt_write(handle, comp->cmdq_base,
  955. comp->regs_pa + DISP_AAL_CABC_00,
  956. 1 << 31, 1 << 31);
  957. cmdq_pkt_write(handle, comp->cmdq_base,
  958. comp->regs_pa + DISP_AAL_CABC_02,
  959. param->cabc_fltgain_force, 0x3ff);
  960. gain = param->cabc_gainlmt;
  961. for (i = 0; i <= 10; i++) {
  962. cmdq_pkt_write(handle, comp->cmdq_base,
  963. comp->regs_pa + DISP_AAL_CABC_GAINLMT_TBL(i),
  964. CABC_GAINLMT(gain[0], gain[1], gain[2]), ~0);
  965. gain += 3;
  966. }
  967. return 0;
  968. }
  969. #endif /* not define NOT_SUPPORT_CABC_HW */
  970. static int disp_aal_write_param_to_reg(struct mtk_ddp_comp *comp,
  971. struct cmdq_pkt *handle, const struct DISP_AAL_PARAM *param)
  972. {
  973. // From mt6885, on DRE3.5+ESS mode, ESS function was
  974. // controlled by DREGainFltStatus, not cabc_gainlmt, so need to
  975. // set DREGainFltStatus to hw whether DRE3.5 or 2.5
  976. disp_aal_write_dre_to_reg(comp, handle, param);
  977. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  978. disp_aal_write_dre3_to_reg(comp, handle, param);
  979. disp_aal_write_cabc_to_reg(comp, handle, param);
  980. #else
  981. #ifndef NOT_SUPPORT_CABC_HW
  982. disp_aal_write_cabc_to_reg(comp, handle, param);
  983. #endif
  984. #endif
  985. return 0;
  986. }
  987. void dump_param(const struct DISP_AAL_PARAM *param)
  988. {
  989. int i = 0;
  990. pr_notice("DREGainFltStatus: ");
  991. for (i = 0; i < 2; i++) {
  992. pr_notice("%d %d %d %d %d %d %d %d %d %d",
  993. param->DREGainFltStatus[i*10 + 0],
  994. param->DREGainFltStatus[i*10 + 1],
  995. param->DREGainFltStatus[i*10 + 2],
  996. param->DREGainFltStatus[i*10 + 3],
  997. param->DREGainFltStatus[i*10 + 4],
  998. param->DREGainFltStatus[i*10 + 5],
  999. param->DREGainFltStatus[i*10 + 6],
  1000. param->DREGainFltStatus[i*10 + 7],
  1001. param->DREGainFltStatus[i*10 + 8],
  1002. param->DREGainFltStatus[i*10 + 9]);
  1003. }
  1004. pr_notice("%d %d %d %d %d %d %d %d %d",
  1005. param->DREGainFltStatus[20], param->DREGainFltStatus[21],
  1006. param->DREGainFltStatus[22], param->DREGainFltStatus[23],
  1007. param->DREGainFltStatus[24], param->DREGainFltStatus[25],
  1008. param->DREGainFltStatus[26], param->DREGainFltStatus[27],
  1009. param->DREGainFltStatus[28]);
  1010. pr_notice("cabc_gainlmt: ");
  1011. for (i = 0; i < 3; i++) {
  1012. pr_notice("%d %d %d %d %d %d %d %d %d %d",
  1013. param->cabc_gainlmt[i*10 + 0],
  1014. param->cabc_gainlmt[i*10 + 1],
  1015. param->cabc_gainlmt[i*10 + 2],
  1016. param->cabc_gainlmt[i*10 + 3],
  1017. param->cabc_gainlmt[i*10 + 4],
  1018. param->cabc_gainlmt[i*10 + 5],
  1019. param->cabc_gainlmt[i*10 + 6],
  1020. param->cabc_gainlmt[i*10 + 7],
  1021. param->cabc_gainlmt[i*10 + 8],
  1022. param->cabc_gainlmt[i*10 + 9]);
  1023. }
  1024. pr_notice("%d %d %d",
  1025. param->cabc_gainlmt[30], param->cabc_gainlmt[31],
  1026. param->cabc_gainlmt[32]);
  1027. pr_notice("cabc_fltgain_force: %d, FinalBacklight: %d",
  1028. param->cabc_fltgain_force, param->FinalBacklight);
  1029. pr_notice("allowPartial: %d, refreshLatency: %d",
  1030. param->allowPartial, param->refreshLatency);
  1031. }
  1032. static bool debug_dump_input_param;
  1033. int disp_aal_set_param(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  1034. struct DISP_AAL_PARAM *param)
  1035. {
  1036. int ret = -EFAULT;
  1037. u64 time_use = 0;
  1038. if (debug_dump_input_param)
  1039. dump_param(&g_aal_param);
  1040. //For 120Hz rotation issue
  1041. do_gettimeofday(&end);
  1042. time_use = (end.tv_sec-start.tv_sec) * 1000000
  1043. + (end.tv_usec-start.tv_usec);
  1044. //pr_notice("set_param time_use is %lu us\n",time_use);
  1045. // tbd. to be fixd
  1046. if (time_use < 260) {
  1047. // Workaround for 120hz rotation,do not let
  1048. //aal command too fast,else it will merged with
  1049. //DISP commmand and caused trigger loop clear EOF
  1050. //before config loop.The DSI EOF has 100 us later then
  1051. //RDMA EOF,and the worst DISP config time is 153us,
  1052. //so if intervel less than 260 should delay
  1053. usleep_range(260-time_use, 270-time_use);
  1054. }
  1055. ret = disp_aal_write_param_to_reg(comp, handle, &g_aal_param);
  1056. disp_aal_flip_sram(comp, handle, __func__);
  1057. /* FIXME
  1058. * if (ret == 0)
  1059. * ret |= disp_pwm_set_backlight_cmdq(DISP_PWM0,
  1060. * backlight_value, cmdq);
  1061. */
  1062. // FIXME
  1063. return ret;
  1064. }
  1065. #define PRINT_AAL_REG(x1, x2, x3, x4) \
  1066. pr_notice("[2]0x%x=0x%x 0x%x=0x%x 0x%x=0x%x 0x%x=0x%x\n", \
  1067. x1, readl(comp->regs + x1), x2, readl(comp->regs + x2), \
  1068. x3, readl(comp->regs + x3), x4, readl(comp->regs + x4))
  1069. #define PRINT_AAL3_REG(x1, x2, x3, x4) \
  1070. pr_notice("[3]0x%x=0x%x 0x%x=0x%x 0x%x=0x%x 0x%x=0x%x\n", \
  1071. x1, readl(dre3_va + x1), x2, readl(dre3_va + x2), \
  1072. x3, readl(dre3_va + x3), x4, readl(dre3_va + x4))
  1073. bool dump_reg(struct mtk_ddp_comp *comp, bool locked)
  1074. {
  1075. unsigned long flags = 0;
  1076. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1077. bool dump_success = false;
  1078. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1079. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1080. #endif
  1081. if (locked || spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1082. if (atomic_read(&aal_data->is_clock_on)) {
  1083. PRINT_AAL_REG(0x0, 0x8, 0x10, 0x20);
  1084. PRINT_AAL_REG(0x30, 0xFC, 0x160, 0x200);
  1085. PRINT_AAL_REG(0x204, 0x20C, 0x3B4, 0x45C);
  1086. PRINT_AAL_REG(0x460, 0x464, 0x468, 0x4D8);
  1087. PRINT_AAL_REG(0x4DC, 0x500, 0x224, 0x504);
  1088. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1089. PRINT_AAL3_REG(0x0, 0x8, 0x10, 0x20);
  1090. PRINT_AAL3_REG(0x30, 0x34, 0x38, 0xC4);
  1091. PRINT_AAL3_REG(0xC8, 0xF4, 0xF8, 0x200);
  1092. PRINT_AAL3_REG(0x204, 0x45C, 0x460, 0x464);
  1093. PRINT_AAL3_REG(0x468, 0x46C, 0x470, 0x474);
  1094. PRINT_AAL3_REG(0x478, 0x480, 0x484, 0x488);
  1095. PRINT_AAL3_REG(0x48C, 0x490, 0x494, 0x498);
  1096. PRINT_AAL3_REG(0x49C, 0x4B4, 0x4B8, 0x4BC);
  1097. PRINT_AAL3_REG(0x4D4, 0x4EC, 0x4F0, 0x53C);
  1098. #endif
  1099. dump_success = true;
  1100. } else
  1101. AALIRQ_LOG("clock is not enabled\n");
  1102. if (!locked)
  1103. spin_unlock_irqrestore(&g_aal_clock_lock, flags);
  1104. } else
  1105. AALIRQ_LOG("clock lock is locked\n");
  1106. return dump_success;
  1107. }
  1108. static bool debug_skip_set_param;
  1109. int mtk_drm_ioctl_aal_set_param(struct drm_device *dev, void *data,
  1110. struct drm_file *file_priv)
  1111. {
  1112. int ret = 0;
  1113. struct mtk_drm_private *private = dev->dev_private;
  1114. struct mtk_ddp_comp *comp = private->ddp_comp[DDP_COMPONENT_AAL0];
  1115. struct drm_crtc *crtc = private->crtc[0];
  1116. int backlight_value = 0;
  1117. struct DISP_AAL_PARAM *param = (struct DISP_AAL_PARAM *) data;
  1118. bool delay_refresh = false;
  1119. if (debug_skip_set_param) {
  1120. pr_notice("skip_set_param for debug\n");
  1121. return ret;
  1122. }
  1123. /* Not need to protect g_aal_param, */
  1124. /* since only AALService can set AAL parameters. */
  1125. memcpy(&g_aal_param, param, sizeof(*param));
  1126. backlight_value = g_aal_param.FinalBacklight;
  1127. /* set cabc gain zero when detect backlight */
  1128. /* setting equal to zero */
  1129. if (backlight_value == 0)
  1130. g_aal_param.cabc_fltgain_force = 0;
  1131. ret = mtk_crtc_user_cmd(crtc, comp, SET_PARAM, data);
  1132. atomic_set(&g_aal_allowPartial, g_aal_param.allowPartial);
  1133. if (atomic_read(&g_aal_backlight_notified) == 0)
  1134. backlight_value = 0;
  1135. AALAPI_LOG("%d", backlight_value);
  1136. mt_leds_brightness_set("lcd-backlight", backlight_value);
  1137. AALFLOW_LOG("delay refresh: %d", g_aal_param.refreshLatency);
  1138. if (g_aal_param.refreshLatency == 33)
  1139. delay_refresh = true;
  1140. mtk_crtc_check_trigger(comp->mtk_crtc, delay_refresh, true);
  1141. return ret;
  1142. }
  1143. static int mtk_aal_user_cmd(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  1144. unsigned int cmd, void *data)
  1145. {
  1146. AALFLOW_LOG("cmd: %d\n", cmd);
  1147. switch (cmd) {
  1148. case INIT_REG:
  1149. if (disp_aal_set_init_reg(comp, handle,
  1150. (struct DISP_AAL_INITREG *) data) < 0) {
  1151. AALERR("INIT_REG: fail\n");
  1152. return -EFAULT;
  1153. }
  1154. break;
  1155. case SET_PARAM:
  1156. if (disp_aal_set_param(comp, handle,
  1157. (struct DISP_AAL_PARAM *) data) < 0) {
  1158. AALERR("SET_PARAM: fail\n");
  1159. return -EFAULT;
  1160. }
  1161. break;
  1162. case EVENTCTL:
  1163. disp_aal_flip_sram(comp, handle, __func__);
  1164. break;
  1165. case FLIP_SRAM:
  1166. disp_aal_first_flip_sram(comp, handle, __func__);
  1167. break;
  1168. default:
  1169. AALERR("error cmd: %d\n", cmd);
  1170. return -EINVAL;
  1171. }
  1172. return 0;
  1173. }
  1174. static DEFINE_SPINLOCK(g_aal_get_irq_lock);
  1175. static void disp_aal_clear_irq(struct mtk_ddp_comp *comp,
  1176. bool cleared)
  1177. {
  1178. unsigned int intsta;
  1179. unsigned long flags;
  1180. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1181. /* Check current irq status */
  1182. do {
  1183. intsta = readl(comp->regs + DISP_AAL_INTSTA);
  1184. if (spin_trylock_irqsave(&g_aal_get_irq_lock, flags)) {
  1185. writel(intsta & ~0x3, comp->regs + DISP_AAL_INTSTA);
  1186. spin_unlock_irqrestore(&g_aal_get_irq_lock, flags);
  1187. }
  1188. } while (0);
  1189. atomic_set(&aal_data->dirty_frame_retrieved, 1);
  1190. /*
  1191. * no need per-frame wakeup.
  1192. * We stop interrupt until next frame dirty.
  1193. */
  1194. if (cleared == true) {
  1195. if (spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1196. if (atomic_read(&aal_data->is_clock_on) != 1)
  1197. AALIRQ_LOG("clock is off\n");
  1198. else
  1199. disp_aal_set_interrupt(comp, false);
  1200. spin_unlock_irqrestore(&g_aal_clock_lock,
  1201. flags);
  1202. }
  1203. }
  1204. AALIRQ_LOG("AAL Module, process:(%d)\n", cleared);
  1205. }
  1206. static bool debug_skip_dre3_irq;
  1207. static bool debug_dump_reg_irq;
  1208. static int dump_blk_x = -1;
  1209. static int dump_blk_y = -1;
  1210. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1211. #define AAL_DRE_BLK_NUM (16)
  1212. #define AAL_BLK_MAX_ALLOWED_NUM (128)
  1213. #define AAL_DRE3_POINT_NUM (17)
  1214. #define AAL_DRE_GAIN_POINT16_START (512)
  1215. #define DRE_POLL_SLEEP_TIME_US (10)
  1216. #define DRE_MAX_POLL_TIME_US (1000)
  1217. static inline bool disp_aal_reg_poll(struct mtk_ddp_comp *comp,
  1218. unsigned long addr, unsigned int value, unsigned int mask)
  1219. {
  1220. bool return_value = false;
  1221. unsigned int reg_value = 0;
  1222. unsigned int polling_time = 0;
  1223. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1224. do {
  1225. reg_value = readl(dre3_va + addr);
  1226. if ((reg_value & mask) == value) {
  1227. return_value = true;
  1228. break;
  1229. }
  1230. udelay(DRE_POLL_SLEEP_TIME_US);
  1231. polling_time += DRE_POLL_SLEEP_TIME_US;
  1232. } while (polling_time < DRE_MAX_POLL_TIME_US);
  1233. return return_value;
  1234. }
  1235. static inline bool disp_aal_sram_write(struct mtk_ddp_comp *comp,
  1236. unsigned int addr, unsigned int value)
  1237. {
  1238. bool return_value = false;
  1239. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1240. do {
  1241. writel(addr, dre3_va + DISP_AAL_SRAM_RW_IF_0);
  1242. if (disp_aal_reg_poll(comp, DISP_AAL_SRAM_STATUS,
  1243. (0x1 << 16), (0x1 << 16)) != true)
  1244. break;
  1245. writel(value, dre3_va + DISP_AAL_SRAM_RW_IF_1);
  1246. return_value = true;
  1247. } while (0);
  1248. return return_value;
  1249. }
  1250. static inline bool disp_aal_sram_read(struct mtk_ddp_comp *comp,
  1251. unsigned int addr, unsigned int *value)
  1252. {
  1253. bool return_value = false;
  1254. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1255. do {
  1256. writel(addr, dre3_va + DISP_AAL_SRAM_RW_IF_2);
  1257. if (disp_aal_reg_poll(comp, DISP_AAL_SRAM_STATUS,
  1258. (0x1 << 17), (0x1 << 17)) != true)
  1259. break;
  1260. *value = readl(dre3_va + DISP_AAL_SRAM_RW_IF_3);
  1261. return_value = true;
  1262. } while (0);
  1263. return return_value;
  1264. }
  1265. static bool disp_aal_read_dre3(struct mtk_ddp_comp *comp,
  1266. const int dre_blk_x_num, const int dre_blk_y_num)
  1267. {
  1268. int hist_offset;
  1269. int arry_offset = 0;
  1270. unsigned int read_value;
  1271. int dump_start = -1;
  1272. u32 dump_table[6] = {0};
  1273. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1274. /* Read Global histogram for ESS */
  1275. if (disp_aal_read_single_hist(comp) != true)
  1276. return false;
  1277. AALIRQ_LOG("start\n");
  1278. if (dump_blk_x >= 0 && dump_blk_x < 16
  1279. && dump_blk_y >= 0 && dump_blk_y < 8)
  1280. dump_start = 6 * (dump_blk_x + dump_blk_y * 16);
  1281. /* Read Local histogram for DRE 3 */
  1282. for (hist_offset = aal_data->data->aal_dre_hist_start;
  1283. hist_offset <= aal_data->data->aal_dre_hist_end;
  1284. hist_offset += 4) {
  1285. if (disp_aal_sram_read(comp, hist_offset, &read_value) != true)
  1286. return false;
  1287. if (arry_offset >= AAL_DRE30_HIST_REGISTER_NUM)
  1288. return false;
  1289. if (dump_start >= 0 && arry_offset >= dump_start
  1290. && arry_offset < (dump_start + 6))
  1291. dump_table[arry_offset-dump_start] = read_value;
  1292. g_aal_dre30_hist.dre_hist[arry_offset++] = read_value;
  1293. }
  1294. if (dump_start >= 0)
  1295. pr_notice("[DRE3][HIST][%d-%d] %08x %08x %08x %08x %08x %08x\n",
  1296. dump_blk_x, dump_blk_y,
  1297. dump_table[0], dump_table[1], dump_table[2],
  1298. dump_table[3], dump_table[4], dump_table[5]);
  1299. return true;
  1300. }
  1301. static bool disp_aal_write_dre3(struct mtk_ddp_comp *comp)
  1302. {
  1303. int gain_offset;
  1304. int arry_offset = 0;
  1305. unsigned int write_value;
  1306. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1307. /* Write Local Gain Curve for DRE 3 */
  1308. AALIRQ_LOG("start\n");
  1309. for (gain_offset = aal_data->data->aal_dre_gain_start;
  1310. gain_offset <= aal_data->data->aal_dre_gain_end;
  1311. gain_offset += 4) {
  1312. if (arry_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1313. return false;
  1314. write_value = g_aal_gain.dre30_gain[arry_offset++];
  1315. if (!disp_aal_sram_write(comp, gain_offset, write_value))
  1316. return false;
  1317. }
  1318. return true;
  1319. }
  1320. static void disp_aal_update_dre3_sram(struct mtk_ddp_comp *comp,
  1321. bool check_sram)
  1322. {
  1323. bool result = false;
  1324. unsigned long flags;
  1325. int dre_blk_x_num, dre_blk_y_num;
  1326. unsigned int read_value;
  1327. int hist_apb = 0, hist_int = 0;
  1328. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1329. if (check_sram) {
  1330. read_value = readl(dre3_va + DISP_AAL_SRAM_CFG);
  1331. hist_apb = (read_value >> 5) & 0x1;
  1332. hist_int = (read_value >> 6) & 0x1;
  1333. AALIRQ_LOG("[SRAM] hist_apb(%d) hist_int(%d) 0x%08x in (SOF)",
  1334. hist_apb, hist_int, read_value);
  1335. if (hist_int != atomic_read(&g_aal_force_hist_apb))
  1336. AALIRQ_LOG("dre3: SRAM config %d != %d config?",
  1337. hist_int, atomic_read(&g_aal_force_hist_apb));
  1338. }
  1339. read_value = readl(dre3_va + DISP_AAL_DRE_BLOCK_INFO_01);
  1340. dre_blk_x_num = aal_min(AAL_DRE_BLK_NUM, read_value & 0x1F);
  1341. dre_blk_y_num = aal_min(AAL_BLK_MAX_ALLOWED_NUM/dre_blk_x_num,
  1342. (read_value >> 5) & 0x1F);
  1343. if (spin_trylock_irqsave(&g_aal_hist_lock, flags)) {
  1344. result = disp_aal_read_dre3(comp,
  1345. dre_blk_x_num, dre_blk_y_num);
  1346. if (result) {
  1347. g_aal_dre30_hist.dre_blk_x_num = dre_blk_x_num;
  1348. g_aal_dre30_hist.dre_blk_y_num = dre_blk_y_num;
  1349. atomic_set(&g_aal_hist_available, 1);
  1350. }
  1351. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  1352. if (result)
  1353. wake_up_interruptible(&g_aal_hist_wq);
  1354. else if (atomic_read(&g_aal_first_frame) == 0) {
  1355. atomic_set(&g_aal_first_frame, 1);
  1356. queue_work(aal_flip_wq, &g_aal_data->aal_flip_task);
  1357. }
  1358. }
  1359. if (spin_trylock_irqsave(&g_aal_dre3_gain_lock, flags)) {
  1360. /* Write DRE 3.0 gain */
  1361. disp_aal_write_dre3(comp);
  1362. spin_unlock_irqrestore(&g_aal_dre3_gain_lock, flags);
  1363. }
  1364. }
  1365. static void disp_aal_dre3_irq_handle(struct mtk_ddp_comp *comp)
  1366. {
  1367. int hist_apb = 0, hist_int = 0;
  1368. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1369. /* Only process AAL0 in single module state */
  1370. disp_aal_clear_irq(comp, false);
  1371. if (atomic_read(&g_aal_change_to_dre30) != 0x3)
  1372. return;
  1373. if (debug_dump_reg_irq)
  1374. debug_dump_reg_irq = !dump_reg(comp, true);
  1375. if (debug_skip_dre3_irq) {
  1376. pr_notice("skip dre3 irq for debug\n");
  1377. return;
  1378. }
  1379. if (aal_sram_method == AAL_SRAM_EOF &&
  1380. atomic_read(&g_aal_dre_halt) == 0) {
  1381. if (atomic_cmpxchg(&g_aal_force_hist_apb, 0, 1) == 0) {
  1382. hist_apb = 0;
  1383. hist_int = 1;
  1384. } else if (atomic_cmpxchg(&g_aal_force_hist_apb, 1, 0) == 1) {
  1385. hist_apb = 1;
  1386. hist_int = 0;
  1387. } else {
  1388. AALERR("Error when get hist_apb irq_handler\n");
  1389. return;
  1390. }
  1391. AALIRQ_LOG("[SRAM] hist_apb (%d) hist_int (%d) in (EOF)",
  1392. hist_apb, hist_int);
  1393. mtk_aal_write_mask(dre3_va + DISP_AAL_SRAM_CFG,
  1394. (hist_int << 6)|(hist_apb << 5)|(1 << 4), (0x7 << 4));
  1395. atomic_set(&g_aal_dre_halt, 1);
  1396. disp_aal_update_dre3_sram(comp, false);
  1397. atomic_set(&g_aal_dre_halt, 0);
  1398. } else if (aal_sram_method == AAL_SRAM_SOF) {
  1399. if (mtk_drm_is_idle(&(comp->mtk_crtc->base))) {
  1400. AALIRQ_LOG("[SRAM] when idle, operate SRAM in (EOF)");
  1401. disp_aal_update_dre3_sram(comp, false);
  1402. }
  1403. AALIRQ_LOG("[SRAM] clean dre_config in (EOF)");
  1404. atomic_set(&g_aal_dre_config, 0);
  1405. }
  1406. }
  1407. static void disp_aal_set_init_dre30(struct DISP_DRE30_INIT *user_regs)
  1408. {
  1409. struct DISP_DRE30_INIT *init_dre3;
  1410. init_dre3 = &g_aal_init_dre30;
  1411. memcpy(init_dre3, user_regs, sizeof(*init_dre3));
  1412. /* Modify DRE3.0 config flag */
  1413. atomic_or(0x2, &g_aal_change_to_dre30);
  1414. }
  1415. static void ddp_aal_dre3_write_curve_full(struct mtk_ddp_comp *comp)
  1416. {
  1417. void __iomem *dre3_va = mtk_aal_dre3_va(comp);
  1418. mtk_aal_write_mask(dre3_va + DISP_AAL_SRAM_CFG,
  1419. (1 << 6)|(0 << 5)|(1 << 4), (0x7 << 4));
  1420. disp_aal_write_dre3(comp);
  1421. mtk_aal_write_mask(dre3_va + DISP_AAL_SRAM_CFG,
  1422. (0 << 6)|(1 << 5)|(1 << 4), (0x7 << 4));
  1423. disp_aal_write_dre3(comp);
  1424. atomic_set(&g_aal_force_hist_apb, 0);
  1425. }
  1426. static bool write_block(const unsigned int *dre3_gain,
  1427. const int block_x, const int block_y, const int dre_blk_x_num)
  1428. {
  1429. bool return_value = false;
  1430. uint32_t block_offset = 4 * (block_y * dre_blk_x_num + block_x);
  1431. do {
  1432. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1433. break;
  1434. g_aal_gain.dre30_gain[block_offset++] =
  1435. ((dre3_gain[0] & 0xff) |
  1436. ((dre3_gain[1] & 0xff) << 8) |
  1437. ((dre3_gain[2] & 0xff) << 16) |
  1438. ((dre3_gain[3] & 0xff) << 24));
  1439. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1440. break;
  1441. g_aal_gain.dre30_gain[block_offset++] =
  1442. ((dre3_gain[4] & 0xff) |
  1443. ((dre3_gain[5] & 0xff) << 8) |
  1444. ((dre3_gain[6] & 0xff) << 16) |
  1445. ((dre3_gain[7] & 0xff) << 24));
  1446. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1447. break;
  1448. g_aal_gain.dre30_gain[block_offset++] =
  1449. ((dre3_gain[8] & 0xff) |
  1450. ((dre3_gain[9] & 0xff) << 8) |
  1451. ((dre3_gain[10] & 0xff) << 16) |
  1452. ((dre3_gain[11] & 0xff) << 24));
  1453. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1454. break;
  1455. g_aal_gain.dre30_gain[block_offset++] =
  1456. ((dre3_gain[12] & 0xff) |
  1457. ((dre3_gain[13] & 0xff) << 8) |
  1458. ((dre3_gain[14] & 0xff) << 16) |
  1459. ((dre3_gain[15] & 0xff) << 24));
  1460. return_value = true;
  1461. } while (0);
  1462. return return_value;
  1463. }
  1464. static bool write_curve16(const unsigned int *dre3_gain,
  1465. const int dre_blk_x_num, const int dre_blk_y_num)
  1466. {
  1467. int32_t blk_x, blk_y;
  1468. const int32_t blk_num_max = dre_blk_x_num * dre_blk_y_num;
  1469. unsigned int write_value = 0x0;
  1470. uint32_t bit_shift = 0;
  1471. uint32_t block_offset = AAL_DRE_GAIN_POINT16_START;
  1472. for (blk_y = 0; blk_y < dre_blk_y_num; blk_y++) {
  1473. for (blk_x = 0; blk_x < dre_blk_x_num; blk_x++) {
  1474. write_value |=
  1475. ((dre3_gain[16] & 0xff) << (8*bit_shift));
  1476. bit_shift++;
  1477. if (bit_shift >= 4) {
  1478. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1479. return false;
  1480. g_aal_gain.dre30_gain[block_offset++] =
  1481. write_value;
  1482. write_value = 0x0;
  1483. bit_shift = 0;
  1484. }
  1485. }
  1486. }
  1487. if ((blk_num_max>>2)<<2 != blk_num_max) {
  1488. /* configure last curve */
  1489. if (block_offset >= AAL_DRE30_GAIN_REGISTER_NUM)
  1490. return false;
  1491. g_aal_gain.dre30_gain[block_offset] = write_value;
  1492. }
  1493. return true;
  1494. }
  1495. static void disp_aal_dre3_init(struct mtk_ddp_comp *comp)
  1496. {
  1497. const int dre_blk_x_num = 8;
  1498. const int dre_blk_y_num = 16;
  1499. unsigned long flags;
  1500. int blk_x, blk_y, curve_point;
  1501. unsigned int dre3_gain[AAL_DRE3_POINT_NUM];
  1502. AALFLOW_LOG("start\n");
  1503. for (curve_point = 0; curve_point < AAL_DRE3_POINT_NUM;
  1504. curve_point++) {
  1505. /* assign initial gain curve */
  1506. dre3_gain[curve_point] = aal_min(255, 16 * curve_point);
  1507. }
  1508. spin_lock_irqsave(&g_aal_dre3_gain_lock, flags);
  1509. for (blk_y = 0; blk_y < dre_blk_y_num; blk_y++) {
  1510. for (blk_x = 0; blk_x < dre_blk_x_num; blk_x++) {
  1511. /* write each block dre curve */
  1512. write_block(dre3_gain, blk_x, blk_y, dre_blk_x_num);
  1513. }
  1514. }
  1515. /* write each block dre curve last point */
  1516. write_curve16(dre3_gain, dre_blk_x_num, dre_blk_y_num);
  1517. ddp_aal_dre3_write_curve_full(comp);
  1518. spin_unlock_irqrestore(&g_aal_dre3_gain_lock, flags);
  1519. }
  1520. #else
  1521. static void disp_aal_single_pipe_hist_update(struct mtk_ddp_comp *comp)
  1522. {
  1523. unsigned int intsta;
  1524. unsigned long flags;
  1525. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1526. bool read_success = false;
  1527. do {
  1528. intsta = readl(comp->regs + DISP_AAL_INTSTA);
  1529. AALIRQ_LOG("AAL Module, intsta: 0x%x\n", intsta);
  1530. /* Only process end of frame state */
  1531. if ((intsta & 0x2) == 0x0) {
  1532. AALERR("break\n");
  1533. break;
  1534. }
  1535. if (spin_trylock_irqsave(&g_aal_get_irq_lock, flags)) {
  1536. writel(intsta & ~0x3, comp->regs + DISP_AAL_INTSTA);
  1537. spin_unlock_irqrestore(&g_aal_get_irq_lock, flags);
  1538. }
  1539. /* Allow to disable interrupt */
  1540. AALIRQ_LOG("set dirty_frame_retrieved to 1\n");
  1541. atomic_set(&aal_data->dirty_frame_retrieved, 1);
  1542. if (spin_trylock_irqsave(&g_aal_hist_lock, flags)) {
  1543. read_success = disp_aal_read_single_hist(comp);
  1544. if (read_success == true)
  1545. atomic_set(&g_aal_hist_available, 1);
  1546. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  1547. AALIRQ_LOG("wake_up_interruptible g_aal_hist_wq:%d\n",
  1548. read_success);
  1549. if (read_success == true)
  1550. wake_up_interruptible(&g_aal_hist_wq);
  1551. } else {
  1552. /*
  1553. * Histogram was not be retrieved, but it's OK.
  1554. * Another interrupt will come until histogram available
  1555. * See: disp_aal_set_interrupt()
  1556. */
  1557. }
  1558. if (atomic_read(&g_aal_is_init_regs_valid) == 0) {
  1559. /*
  1560. * AAL service is not running, not need per-frame wakeup
  1561. * We stop interrupt until next frame dirty.
  1562. */
  1563. AALIRQ_LOG("set disp_aal_set_interrupt to 0\n");
  1564. if (spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1565. if (atomic_read(&aal_data->is_clock_on) != 1)
  1566. AALIRQ_LOG("clock is off\n");
  1567. else
  1568. disp_aal_set_interrupt(comp, false);
  1569. spin_unlock_irqrestore(&g_aal_clock_lock,
  1570. flags);
  1571. }
  1572. }
  1573. } while (0);
  1574. }
  1575. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  1576. int mtk_drm_ioctl_aal_init_dre30(struct drm_device *dev, void *data,
  1577. struct drm_file *file_priv)
  1578. {
  1579. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1580. AALFLOW_LOG("\n");
  1581. disp_aal_set_init_dre30((struct DISP_DRE30_INIT *) data);
  1582. #else
  1583. AALFLOW_LOG("DRE30 not support\n");
  1584. #endif
  1585. return 0;
  1586. }
  1587. static int disp_aal_wait_size(unsigned long timeout)
  1588. {
  1589. int ret = 0;
  1590. if (g_aal_get_size_available == false) {
  1591. ret = wait_event_interruptible(g_aal_size_wq,
  1592. g_aal_get_size_available == true);
  1593. pr_notice("size_available = 1, Waken up, ret = %d\n",
  1594. ret);
  1595. } else {
  1596. /* If g_aal_get_size_available is already set, */
  1597. /* means AALService was delayed */
  1598. pr_notice("size_available = 0\n");
  1599. }
  1600. return ret;
  1601. }
  1602. int mtk_drm_ioctl_aal_get_size(struct drm_device *dev, void *data,
  1603. struct drm_file *file_priv)
  1604. {
  1605. struct DISP_AAL_DISPLAY_SIZE *dst =
  1606. (struct DISP_AAL_DISPLAY_SIZE *)data;
  1607. AALFLOW_LOG("\n");
  1608. disp_aal_wait_size(60);
  1609. memcpy(dst, &g_aal_size, sizeof(g_aal_size));
  1610. return 0;
  1611. }
  1612. static void mtk_aal_start(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  1613. {
  1614. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1615. int dre_alg_mode = 0;
  1616. phys_addr_t dre3_pa = mtk_aal_dre3_pa(comp);
  1617. if (atomic_read(&g_aal_change_to_dre30) & 0x1)
  1618. dre_alg_mode = 1;
  1619. if (debug_bypass_alg_mode)
  1620. dre_alg_mode = 0;
  1621. cmdq_pkt_write(handle, comp->cmdq_base,
  1622. dre3_pa + DISP_AAL_CFG_MAIN,
  1623. dre_alg_mode << 4, 1 << 4);
  1624. #endif
  1625. AALFLOW_LOG("\n");
  1626. basic_cmdq_write(handle, comp, DISP_AAL_EN, 0x1, ~0);
  1627. }
  1628. static void mtk_aal_stop(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  1629. {
  1630. basic_cmdq_write(handle, comp, DISP_AAL_EN, 0x0, ~0);
  1631. }
  1632. static void mtk_aal_bypass(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  1633. {
  1634. #if 1
  1635. AALFLOW_LOG("\n");
  1636. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DISP_AAL_CFG,
  1637. 0x1, 0x1);
  1638. #else
  1639. AALFLOW_LOG("is ignored\n");
  1640. #endif
  1641. }
  1642. #define DRE_FLT_NUM (12)
  1643. #define CABC_GAINLMT_NUM (11)
  1644. struct aal_backup { /* structure for backup AAL register value */
  1645. unsigned int DRE_MAPPING;
  1646. unsigned int DRE_FLT_FORCE[DRE_FLT_NUM];
  1647. unsigned int CABC_00;
  1648. unsigned int CABC_02;
  1649. unsigned int CABC_GAINLMT[CABC_GAINLMT_NUM];
  1650. #if defined(DRE3_IN_DISP_AAL)
  1651. unsigned int DRE_BLOCK_INFO_00;
  1652. unsigned int DRE_BLOCK_INFO_01;
  1653. unsigned int DRE_BLOCK_INFO_02;
  1654. unsigned int DRE_BLOCK_INFO_04;
  1655. unsigned int DRE_BLOCK_INFO_05;
  1656. unsigned int DRE_BLOCK_INFO_06;
  1657. unsigned int DRE_BLOCK_INFO_07;
  1658. unsigned int DRE_CHROMA_HIST_00;
  1659. unsigned int DRE_CHROMA_HIST_01;
  1660. unsigned int DRE_ALPHA_BLEND_00;
  1661. unsigned int SRAM_CFG;
  1662. unsigned int DUAL_PIPE_INFO_00;
  1663. unsigned int DUAL_PIPE_INFO_01;
  1664. #endif
  1665. };
  1666. static struct aal_backup g_aal_backup;
  1667. static void ddp_aal_dre3_backup(struct mtk_ddp_comp *comp)
  1668. {
  1669. #if defined(DRE3_IN_DISP_AAL)
  1670. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1671. g_aal_backup.DRE_BLOCK_INFO_00 =
  1672. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_00);
  1673. g_aal_backup.DRE_BLOCK_INFO_01 =
  1674. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_01);
  1675. g_aal_backup.DRE_BLOCK_INFO_02 =
  1676. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_02);
  1677. g_aal_backup.DRE_BLOCK_INFO_04 =
  1678. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_04);
  1679. g_aal_backup.DRE_CHROMA_HIST_00 =
  1680. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_CHROMA_HIST_00);
  1681. g_aal_backup.DRE_CHROMA_HIST_01 =
  1682. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_CHROMA_HIST_01);
  1683. g_aal_backup.DRE_ALPHA_BLEND_00 =
  1684. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_ALPHA_BLEND_00);
  1685. g_aal_backup.DRE_BLOCK_INFO_05 =
  1686. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_05);
  1687. g_aal_backup.DRE_BLOCK_INFO_06 =
  1688. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_06);
  1689. g_aal_backup.DRE_BLOCK_INFO_07 =
  1690. readl(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_07);
  1691. g_aal_backup.SRAM_CFG =
  1692. readl(aal_data->dre3_hw.va + DISP_AAL_SRAM_CFG);
  1693. g_aal_backup.DUAL_PIPE_INFO_00 =
  1694. readl(aal_data->dre3_hw.va + DISP_AAL_DUAL_PIPE_INFO_00);
  1695. g_aal_backup.DUAL_PIPE_INFO_01 =
  1696. readl(aal_data->dre3_hw.va + DISP_AAL_DUAL_PIPE_INFO_01);
  1697. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  1698. }
  1699. static void ddp_aal_dre_backup(struct mtk_ddp_comp *comp)
  1700. {
  1701. int i;
  1702. g_aal_backup.DRE_MAPPING =
  1703. readl(comp->regs + DISP_AAL_DRE_MAPPING_00);
  1704. for (i = 0; i < DRE_FLT_NUM; i++)
  1705. g_aal_backup.DRE_FLT_FORCE[i] =
  1706. readl(comp->regs + DISP_AAL_DRE_FLT_FORCE(i));
  1707. }
  1708. static void ddp_aal_cabc_backup(struct mtk_ddp_comp *comp)
  1709. {
  1710. #if defined(CONFIG_MTK_DRE30_SUPPORT) || !defined(NOT_SUPPORT_CABC_HW)
  1711. int i;
  1712. g_aal_backup.CABC_00 = readl(comp->regs + DISP_AAL_CABC_00);
  1713. g_aal_backup.CABC_02 = readl(comp->regs + DISP_AAL_CABC_02);
  1714. for (i = 0; i < CABC_GAINLMT_NUM; i++)
  1715. g_aal_backup.CABC_GAINLMT[i] =
  1716. readl(comp->regs + DISP_AAL_CABC_GAINLMT_TBL(i));
  1717. #endif /* not define NOT_SUPPORT_CABC_HW */
  1718. }
  1719. static void ddp_aal_backup(struct mtk_ddp_comp *comp)
  1720. {
  1721. AALFLOW_LOG("\n");
  1722. ddp_aal_cabc_backup(comp);
  1723. ddp_aal_dre_backup(comp);
  1724. ddp_aal_dre3_backup(comp);
  1725. atomic_set(&g_aal_initialed, 1);
  1726. }
  1727. static void ddp_aal_dre3_restore(struct mtk_ddp_comp *comp)
  1728. {
  1729. #if defined(DRE3_IN_DISP_AAL)
  1730. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1731. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_00,
  1732. g_aal_backup.DRE_BLOCK_INFO_00 & (0x1FFF << 13), 0x1FFF << 13);
  1733. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_01,
  1734. g_aal_backup.DRE_BLOCK_INFO_01, ~0);
  1735. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_02,
  1736. g_aal_backup.DRE_BLOCK_INFO_02, ~0);
  1737. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_04,
  1738. g_aal_backup.DRE_BLOCK_INFO_04 & (0x3FF << 13), 0x3FF << 13);
  1739. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_CHROMA_HIST_00,
  1740. g_aal_backup.DRE_CHROMA_HIST_00, ~0);
  1741. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_CHROMA_HIST_01,
  1742. g_aal_backup.DRE_CHROMA_HIST_01 & 0xFFFF, 0xFFFF);
  1743. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_ALPHA_BLEND_00,
  1744. g_aal_backup.DRE_ALPHA_BLEND_00, ~0);
  1745. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_05,
  1746. g_aal_backup.DRE_BLOCK_INFO_05, ~0);
  1747. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_06,
  1748. g_aal_backup.DRE_BLOCK_INFO_06, ~0);
  1749. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DRE_BLOCK_INFO_07,
  1750. g_aal_backup.DRE_BLOCK_INFO_07, ~0);
  1751. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_SRAM_CFG,
  1752. g_aal_backup.SRAM_CFG, 0x1);
  1753. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DUAL_PIPE_INFO_00,
  1754. g_aal_backup.DUAL_PIPE_INFO_00, ~0);
  1755. mtk_aal_write_mask(aal_data->dre3_hw.va + DISP_AAL_DUAL_PIPE_INFO_01,
  1756. g_aal_backup.DUAL_PIPE_INFO_01, ~0);
  1757. #endif
  1758. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1759. unsigned long flags;
  1760. spin_lock_irqsave(&g_aal_dre3_gain_lock, flags);
  1761. ddp_aal_dre3_write_curve_full(comp);
  1762. spin_unlock_irqrestore(&g_aal_dre3_gain_lock, flags);
  1763. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  1764. }
  1765. static void ddp_aal_dre_restore(struct mtk_ddp_comp *comp)
  1766. {
  1767. int i;
  1768. writel(g_aal_backup.DRE_MAPPING,
  1769. comp->regs + DISP_AAL_DRE_MAPPING_00);
  1770. for (i = 0; i < DRE_FLT_NUM; i++)
  1771. writel(g_aal_backup.DRE_FLT_FORCE[i],
  1772. comp->regs + DISP_AAL_DRE_FLT_FORCE(i));
  1773. }
  1774. static void ddp_aal_cabc_restore(struct mtk_ddp_comp *comp)
  1775. {
  1776. #if defined(CONFIG_MTK_DRE30_SUPPORT) || !defined(NOT_SUPPORT_CABC_HW)
  1777. int i;
  1778. writel(g_aal_backup.CABC_00, comp->regs + DISP_AAL_CABC_00);
  1779. writel(g_aal_backup.CABC_02, comp->regs + DISP_AAL_CABC_02);
  1780. for (i = 0; i < CABC_GAINLMT_NUM; i++)
  1781. writel(g_aal_backup.CABC_GAINLMT[i],
  1782. comp->regs + DISP_AAL_CABC_GAINLMT_TBL(i));
  1783. #endif /* not define NOT_SUPPORT_CABC_HW */
  1784. }
  1785. static void ddp_aal_restore(struct mtk_ddp_comp *comp)
  1786. {
  1787. if (atomic_read(&g_aal_initialed) != 1)
  1788. return;
  1789. AALFLOW_LOG("\n");
  1790. ddp_aal_cabc_restore(comp);
  1791. ddp_aal_dre_restore(comp);
  1792. ddp_aal_dre3_restore(comp);
  1793. }
  1794. static bool debug_skip_first_br;
  1795. static void mtk_aal_prepare(struct mtk_ddp_comp *comp)
  1796. {
  1797. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1798. bool first_restore = (atomic_read(&aal_data->is_clock_on) == 0);
  1799. AALFLOW_LOG("\n");
  1800. mtk_ddp_comp_clk_prepare(comp);
  1801. atomic_set(&aal_data->is_clock_on, 1);
  1802. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1803. if (aal_data->dre3_hw.clk)
  1804. clk_prepare(aal_data->dre3_hw.clk);
  1805. #endif
  1806. if (!first_restore && !debug_skip_first_br)
  1807. return;
  1808. #if defined(CONFIG_DRM_MTK_SHADOW_REGISTER_SUPPORT)
  1809. if (aal_data->data->support_shadow) {
  1810. /* Enable shadow register and read shadow register */
  1811. mtk_ddp_write_mask_cpu(comp, 0x0,
  1812. DISP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  1813. } else {
  1814. /* Bypass shadow register and read shadow register */
  1815. mtk_ddp_write_mask_cpu(comp, AAL_BYPASS_SHADOW,
  1816. DISP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  1817. }
  1818. #else
  1819. #if defined(CONFIG_MACH_MT6873) || defined(CONFIG_MACH_MT6853) \
  1820. || defined(CONFIG_MACH_MT6833)
  1821. /* Bypass shadow register and read shadow register */
  1822. mtk_ddp_write_mask_cpu(comp, AAL_BYPASS_SHADOW,
  1823. DISP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  1824. #endif
  1825. #endif
  1826. ddp_aal_restore(comp);
  1827. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1828. if (atomic_cmpxchg(&g_aal_dre_hw_init, 0, 1) == 0)
  1829. disp_aal_dre3_init(comp);
  1830. #endif
  1831. }
  1832. static void mtk_aal_unprepare(struct mtk_ddp_comp *comp)
  1833. {
  1834. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1835. unsigned long flags;
  1836. bool first_backup = (atomic_read(&aal_data->is_clock_on) == 1);
  1837. AALFLOW_LOG("\n");
  1838. spin_lock_irqsave(&g_aal_clock_lock, flags);
  1839. atomic_set(&aal_data->is_clock_on, 0);
  1840. atomic_set(&g_aal_first_frame, 0);
  1841. spin_unlock_irqrestore(&g_aal_clock_lock, flags);
  1842. if (first_backup || debug_skip_first_br)
  1843. ddp_aal_backup(comp);
  1844. //disp_aal_clear_irq(comp, true);
  1845. mtk_ddp_comp_clk_unprepare(comp);
  1846. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1847. if (aal_data->dre3_hw.clk)
  1848. clk_unprepare(aal_data->dre3_hw.clk);
  1849. #endif
  1850. }
  1851. void mtk_aal_first_cfg(struct mtk_ddp_comp *comp,
  1852. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  1853. {
  1854. AALFLOW_LOG("\n");
  1855. mtk_aal_config(comp, cfg, handle);
  1856. }
  1857. int mtk_aal_io_cmd(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle,
  1858. enum mtk_ddp_io_cmd cmd, void *params)
  1859. {
  1860. unsigned long flags;
  1861. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1862. if (cmd == FRAME_DIRTY) {
  1863. AALFLOW_LOG("FRAME_DIRTY\n");
  1864. if (spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1865. if (atomic_read(&aal_data->is_clock_on) != 1)
  1866. AALIRQ_LOG("clock is off\n");
  1867. else
  1868. disp_aal_set_interrupt(comp, true);
  1869. spin_unlock_irqrestore(&g_aal_clock_lock, flags);
  1870. }
  1871. }
  1872. return 0;
  1873. }
  1874. static const struct mtk_ddp_comp_funcs mtk_disp_aal_funcs = {
  1875. .config = mtk_aal_config,
  1876. .first_cfg = mtk_aal_first_cfg,
  1877. .start = mtk_aal_start,
  1878. .stop = mtk_aal_stop,
  1879. .bypass = mtk_aal_bypass,
  1880. .user_cmd = mtk_aal_user_cmd,
  1881. .io_cmd = mtk_aal_io_cmd,
  1882. .prepare = mtk_aal_prepare,
  1883. .unprepare = mtk_aal_unprepare,
  1884. };
  1885. static int mtk_disp_aal_bind(struct device *dev, struct device *master,
  1886. void *data)
  1887. {
  1888. struct mtk_disp_aal *priv = dev_get_drvdata(dev);
  1889. struct drm_device *drm_dev = data;
  1890. int ret;
  1891. ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
  1892. if (ret < 0) {
  1893. dev_err(dev, "Failed to register component %s: %d\n",
  1894. dev->of_node->full_name, ret);
  1895. return ret;
  1896. }
  1897. return 0;
  1898. }
  1899. static void mtk_disp_aal_unbind(struct device *dev, struct device *master,
  1900. void *data)
  1901. {
  1902. struct mtk_disp_aal *priv = dev_get_drvdata(dev);
  1903. struct drm_device *drm_dev = data;
  1904. mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
  1905. }
  1906. static const struct component_ops mtk_disp_aal_component_ops = {
  1907. .bind = mtk_disp_aal_bind,
  1908. .unbind = mtk_disp_aal_unbind,
  1909. };
  1910. void mtk_aal_dump(struct mtk_ddp_comp *comp)
  1911. {
  1912. void __iomem *baddr = comp->regs;
  1913. DDPDUMP("== %s REGS ==\n", mtk_dump_comp_str(comp));
  1914. mtk_cust_dump_reg(baddr, 0x0, 0x20, 0x30, 0x4D8);
  1915. mtk_cust_dump_reg(baddr, 0x24, 0x28, 0x200, 0x10);
  1916. }
  1917. void disp_aal_on_end_of_frame(struct mtk_ddp_comp *comp)
  1918. {
  1919. //For 120Hz rotation issue
  1920. do_gettimeofday(&start);
  1921. atomic_set(&g_aal_eof_irq, 1);
  1922. if (atomic_read(&g_aal_force_relay) == 1) {
  1923. disp_aal_clear_irq(comp, true);
  1924. return;
  1925. }
  1926. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1927. disp_aal_dre3_irq_handle(comp);
  1928. #else
  1929. disp_aal_single_pipe_hist_update(comp);
  1930. #endif /* CONFIG_MTK_DRE30_SUPPORT */
  1931. }
  1932. void disp_aal_on_start_of_frame(void)
  1933. {
  1934. #ifdef CONFIG_MTK_DRE30_SUPPORT
  1935. unsigned long flags;
  1936. struct mtk_disp_aal *aal_data = comp_to_aal(default_comp);
  1937. if (atomic_read(&g_aal_force_relay) == 1)
  1938. return;
  1939. if (atomic_read(&g_aal_change_to_dre30) != 0x3)
  1940. return;
  1941. if (aal_sram_method != AAL_SRAM_SOF)
  1942. return;
  1943. AALIRQ_LOG("[SRAM] g_aal_dre_config(%d) in SOF",
  1944. atomic_read(&g_aal_dre_config));
  1945. if (spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1946. if (atomic_read(&aal_data->is_clock_on) != 1)
  1947. AALIRQ_LOG("clock is off\n");
  1948. else
  1949. disp_aal_update_dre3_sram(default_comp, true);
  1950. spin_unlock_irqrestore(&g_aal_clock_lock,
  1951. flags);
  1952. }
  1953. #endif
  1954. }
  1955. static irqreturn_t mtk_disp_aal_irq_handler(int irq, void *dev_id)
  1956. {
  1957. unsigned long flags;
  1958. irqreturn_t ret = IRQ_NONE;
  1959. struct mtk_disp_aal *priv = dev_id;
  1960. struct mtk_ddp_comp *comp = &priv->ddp_comp;
  1961. struct mtk_disp_aal *aal_data = comp_to_aal(comp);
  1962. if (spin_trylock_irqsave(&g_aal_clock_lock, flags)) {
  1963. if (atomic_read(&aal_data->is_clock_on) != 1)
  1964. AALIRQ_LOG("clock is off\n");
  1965. else {
  1966. disp_aal_on_end_of_frame(comp);
  1967. ret = IRQ_HANDLED;
  1968. }
  1969. spin_unlock_irqrestore(&g_aal_clock_lock, flags);
  1970. }
  1971. return ret;
  1972. }
  1973. static int mtk_disp_aal_probe(struct platform_device *pdev)
  1974. {
  1975. struct device *dev = &pdev->dev;
  1976. struct mtk_disp_aal *priv;
  1977. enum mtk_ddp_comp_id comp_id;
  1978. int ret, irq;
  1979. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  1980. struct device_node *dre3_dev_node;
  1981. struct platform_device *dre3_pdev;
  1982. struct resource dre3_res;
  1983. #endif
  1984. DDPINFO("%s+\n", __func__);
  1985. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1986. if (priv == NULL)
  1987. return -ENOMEM;
  1988. g_aal_data = priv;
  1989. atomic_set(&priv->dirty_frame_retrieved, 1);
  1990. atomic_set(&priv->is_clock_on, 0);
  1991. irq = platform_get_irq(pdev, 0);
  1992. if (irq < 0)
  1993. return irq;
  1994. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_AAL);
  1995. if ((int)comp_id < 0) {
  1996. AALERR("Failed to identify by alias: %d\n", comp_id);
  1997. return comp_id;
  1998. }
  1999. ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
  2000. &mtk_disp_aal_funcs);
  2001. if (ret) {
  2002. AALERR("Failed to initialize component: %d\n", ret);
  2003. return ret;
  2004. }
  2005. if (!default_comp)
  2006. default_comp = &priv->ddp_comp;
  2007. priv->data = of_device_get_match_data(dev);
  2008. platform_set_drvdata(pdev, priv);
  2009. ret = devm_request_irq(dev, irq, mtk_disp_aal_irq_handler,
  2010. IRQF_TRIGGER_NONE | IRQF_SHARED, dev_name(dev), priv);
  2011. if (ret)
  2012. dev_err(dev, "devm_request_irq fail: %d\n", ret);
  2013. pm_runtime_enable(dev);
  2014. #if defined(CONFIG_MTK_DRE30_SUPPORT)
  2015. do {
  2016. dre3_dev_node = of_parse_phandle(
  2017. pdev->dev.of_node, "aal_dre3", 0);
  2018. if (dre3_dev_node)
  2019. pr_notice("found dre3 aal node, it's another hw\n");
  2020. else
  2021. break;
  2022. dre3_pdev = of_find_device_by_node(dre3_dev_node);
  2023. if (dre3_pdev)
  2024. pr_notice("found dre3 aal device, it's another hw\n");
  2025. else
  2026. break;
  2027. of_node_put(dre3_dev_node);
  2028. priv->dre3_hw.dev = &dre3_pdev->dev;
  2029. priv->dre3_hw.va = of_iomap(dre3_pdev->dev.of_node, 0);
  2030. if (!priv->dre3_hw.va) {
  2031. pr_notice("cannot found allocate dre3 va!\n");
  2032. break;
  2033. }
  2034. ret = of_address_to_resource(
  2035. dre3_pdev->dev.of_node, 0, &dre3_res);
  2036. if (ret) {
  2037. pr_notice("cannot found allocate dre3 resource!\n");
  2038. break;
  2039. }
  2040. priv->dre3_hw.pa = dre3_res.start;
  2041. priv->dre3_hw.clk = of_clk_get_by_name(
  2042. dre3_dev_node, "DRE3_AAL0");
  2043. if (IS_ERR(priv->dre3_hw.clk)) {
  2044. pr_notice("fail @ dre3 clock. name:%s\n",
  2045. "DRE3_AAL0");
  2046. break;
  2047. }
  2048. pr_notice("dre3 dev:%p va:%p pa:%pa", priv->dre3_hw.dev,
  2049. priv->dre3_hw.va, &priv->dre3_hw.pa);
  2050. } while (0);
  2051. #endif
  2052. ret = component_add(dev, &mtk_disp_aal_component_ops);
  2053. if (ret) {
  2054. dev_err(dev, "Failed to add component: %d\n", ret);
  2055. pm_runtime_disable(dev);
  2056. }
  2057. #ifdef CONFIG_LEDS_BRIGHTNESS_CHANGED
  2058. mtk_leds_register_notifier(&leds_init_notifier);
  2059. #endif
  2060. aal_flip_wq = create_singlethread_workqueue("aal_flip_sram");
  2061. INIT_WORK(&g_aal_data->aal_flip_task, mtk_crtc_user_cmd_work);
  2062. AALFLOW_LOG("-\n");
  2063. return ret;
  2064. }
  2065. static int mtk_disp_aal_remove(struct platform_device *pdev)
  2066. {
  2067. struct mtk_disp_aal *priv = dev_get_drvdata(&pdev->dev);
  2068. component_del(&pdev->dev, &mtk_disp_aal_component_ops);
  2069. pm_runtime_disable(&pdev->dev);
  2070. if (priv->dre3_hw.dev)
  2071. pm_runtime_disable(priv->dre3_hw.dev);
  2072. #ifdef CONFIG_LEDS_BRIGHTNESS_CHANGED
  2073. mtk_leds_unregister_notifier(&leds_init_notifier);
  2074. #endif
  2075. return 0;
  2076. }
  2077. static const struct mtk_disp_aal_data mt6885_aal_driver_data = {
  2078. .support_shadow = false,
  2079. .aal_dre_hist_start = 1152,
  2080. .aal_dre_hist_end = 4220,
  2081. .aal_dre_gain_start = 4224,
  2082. .aal_dre_gain_end = 6396,
  2083. .bitShift = 13,
  2084. };
  2085. static const struct mtk_disp_aal_data mt6873_aal_driver_data = {
  2086. .support_shadow = false,
  2087. .aal_dre_hist_start = 1536,
  2088. .aal_dre_hist_end = 4604,
  2089. .aal_dre_gain_start = 4608,
  2090. .aal_dre_gain_end = 6780,
  2091. .bitShift = 16,
  2092. };
  2093. static const struct mtk_disp_aal_data mt6853_aal_driver_data = {
  2094. .support_shadow = false,
  2095. .aal_dre_hist_start = 1536,
  2096. .aal_dre_hist_end = 4604,
  2097. .aal_dre_gain_start = 4608,
  2098. .aal_dre_gain_end = 6780,
  2099. .bitShift = 16,
  2100. };
  2101. static const struct mtk_disp_aal_data mt6833_aal_driver_data = {
  2102. .support_shadow = false,
  2103. .aal_dre_hist_start = 1536,
  2104. .aal_dre_hist_end = 4604,
  2105. .aal_dre_gain_start = 4608,
  2106. .aal_dre_gain_end = 6780,
  2107. .bitShift = 16,
  2108. };
  2109. static const struct of_device_id mtk_disp_aal_driver_dt_match[] = {
  2110. { .compatible = "mediatek,mt6885-disp-aal",
  2111. .data = &mt6885_aal_driver_data},
  2112. { .compatible = "mediatek,mt6873-disp-aal",
  2113. .data = &mt6873_aal_driver_data},
  2114. { .compatible = "mediatek,mt6853-disp-aal",
  2115. .data = &mt6853_aal_driver_data},
  2116. { .compatible = "mediatek,mt6833-disp-aal",
  2117. .data = &mt6833_aal_driver_data},
  2118. {},
  2119. };
  2120. MODULE_DEVICE_TABLE(of, mtk_disp_aal_driver_dt_match);
  2121. struct platform_driver mtk_disp_aal_driver = {
  2122. .probe = mtk_disp_aal_probe,
  2123. .remove = mtk_disp_aal_remove,
  2124. .driver = {
  2125. .name = "mediatek-disp-aal",
  2126. .owner = THIS_MODULE,
  2127. .of_match_table = mtk_disp_aal_driver_dt_match,
  2128. },
  2129. };
  2130. /* Legacy AAL_SUPPORT_KERNEL_API */
  2131. void disp_aal_set_lcm_type(unsigned int panel_type)
  2132. {
  2133. unsigned long flags;
  2134. spin_lock_irqsave(&g_aal_hist_lock, flags);
  2135. atomic_set(&g_aal_panel_type, panel_type);
  2136. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  2137. AALAPI_LOG("panel_type = %d", panel_type);
  2138. }
  2139. #define AAL_CONTROL_CMD(ID, CONTROL) (ID << 16 | CONTROL)
  2140. void disp_aal_set_ess_level(int level)
  2141. {
  2142. unsigned long flags;
  2143. int level_command = 0;
  2144. spin_lock_irqsave(&g_aal_hist_lock, flags);
  2145. g_aal_ess_level_cmd_id += 1;
  2146. g_aal_ess_level_cmd_id = g_aal_ess_level_cmd_id % 64;
  2147. level_command = AAL_CONTROL_CMD(g_aal_ess_level_cmd_id, level);
  2148. g_aal_ess_level = level_command;
  2149. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  2150. disp_aal_refresh_by_kernel();
  2151. AALAPI_LOG("level = %d (cmd = 0x%x)", level, level_command);
  2152. }
  2153. void disp_aal_set_ess_en(int enable)
  2154. {
  2155. unsigned long flags;
  2156. int enable_command = 0;
  2157. int level_command = 0;
  2158. spin_lock_irqsave(&g_aal_hist_lock, flags);
  2159. g_aal_ess_en_cmd_id += 1;
  2160. g_aal_ess_en_cmd_id = g_aal_ess_en_cmd_id % 64;
  2161. enable_command = AAL_CONTROL_CMD(g_aal_ess_en_cmd_id, enable);
  2162. g_aal_ess_en = enable_command;
  2163. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  2164. disp_aal_refresh_by_kernel();
  2165. AALAPI_LOG("en = %d (cmd = 0x%x) level = 0x%08x (cmd = 0x%x)",
  2166. enable, enable_command, ESS_LEVEL_BY_CUSTOM_LIB, level_command);
  2167. }
  2168. void disp_aal_set_dre_en(int enable)
  2169. {
  2170. unsigned long flags;
  2171. int enable_command = 0;
  2172. spin_lock_irqsave(&g_aal_hist_lock, flags);
  2173. g_aal_dre_en_cmd_id += 1;
  2174. g_aal_dre_en_cmd_id = g_aal_dre_en_cmd_id % 64;
  2175. enable_command = AAL_CONTROL_CMD(g_aal_dre_en_cmd_id, enable);
  2176. g_aal_dre_en = enable_command;
  2177. spin_unlock_irqrestore(&g_aal_hist_lock, flags);
  2178. disp_aal_refresh_by_kernel();
  2179. AALAPI_LOG("en = %d (cmd = 0x%x)", enable, enable_command);
  2180. }
  2181. void disp_aal_debug(const char *opt)
  2182. {
  2183. pr_notice("[debug]: %s\n", opt);
  2184. if (strncmp(opt, "setparam:", 9) == 0) {
  2185. debug_skip_set_param = strncmp(opt + 9, "skip", 4) == 0;
  2186. pr_notice("[debug] skip_set_param=%d\n",
  2187. debug_skip_set_param);
  2188. } else if (strncmp(opt, "dre3irq:", 8) == 0) {
  2189. debug_skip_dre3_irq = strncmp(opt + 8, "skip", 4) == 0;
  2190. pr_notice("[debug] skip_dre3_irq=%d\n",
  2191. debug_skip_dre3_irq);
  2192. } else if (strncmp(opt, "dre3algmode:", 12) == 0) {
  2193. debug_bypass_alg_mode = strncmp(opt + 12, "bypass", 6) == 0;
  2194. pr_notice("[debug] bypass_alg_mode=%d\n",
  2195. debug_bypass_alg_mode);
  2196. } else if (strncmp(opt, "dumpregirq", 10) == 0) {
  2197. debug_dump_reg_irq = true;
  2198. pr_notice("[debug] debug_dump_reg_irq=%d\n",
  2199. debug_dump_reg_irq);
  2200. } else if (strncmp(opt, "dumpdre3hist:", 13) == 0) {
  2201. if (sscanf(opt + 13, "%d %d",
  2202. &dump_blk_x, &dump_blk_y) == 2)
  2203. pr_notice("[debug] dump_blk_x=%d dump_blk_y=%d\n",
  2204. dump_blk_x, dump_blk_y);
  2205. else
  2206. pr_notice("[debug] dump_blk parse fail\n");
  2207. } else if (strncmp(opt, "first_br:", 9) == 0) {
  2208. debug_skip_first_br = strncmp(opt + 9, "skip", 4) == 0;
  2209. pr_notice("[debug] skip_first_br=%d\n",
  2210. debug_skip_first_br);
  2211. } else if (strncmp(opt, "flow_log:", 9) == 0) {
  2212. debug_flow_log = strncmp(opt + 9, "1", 1) == 0;
  2213. pr_notice("[debug] debug_flow_log=%d\n",
  2214. debug_flow_log);
  2215. } else if (strncmp(opt, "api_log:", 8) == 0) {
  2216. debug_api_log = strncmp(opt + 8, "1", 1) == 0;
  2217. pr_notice("[debug] debug_api_log=%d\n",
  2218. debug_api_log);
  2219. } else if (strncmp(opt, "write_cmdq_log:", 15) == 0) {
  2220. debug_write_cmdq_log = strncmp(opt + 15, "1", 1) == 0;
  2221. pr_notice("[debug] debug_write_cmdq_log=%d\n",
  2222. debug_write_cmdq_log);
  2223. } else if (strncmp(opt, "irq_log:", 8) == 0) {
  2224. debug_irq_log = strncmp(opt + 8, "1", 1) == 0;
  2225. pr_notice("[debug] debug_irq_log=%d\n",
  2226. debug_irq_log);
  2227. } else if (strncmp(opt, "dump_aal_hist:", 14) == 0) {
  2228. debug_dump_aal_hist = strncmp(opt + 14, "1", 1) == 0;
  2229. pr_notice("[debug] debug_dump_aal_hist=%d\n",
  2230. debug_dump_aal_hist);
  2231. } else if (strncmp(opt, "dump_input_param:", 17) == 0) {
  2232. debug_dump_input_param = strncmp(opt + 17, "1", 1) == 0;
  2233. pr_notice("[debug] debug_dump_input_param=%d\n",
  2234. debug_dump_input_param);
  2235. } else if (strncmp(opt, "set_ess_level:", 14) == 0) {
  2236. int debug_ess_level;
  2237. if (sscanf(opt + 14, "%d", &debug_ess_level) == 1) {
  2238. pr_notice("[debug] ess_level=%d\n", debug_ess_level);
  2239. disp_aal_set_ess_level(debug_ess_level);
  2240. } else
  2241. pr_notice("[debug] set_ess_level failed\n");
  2242. } else if (strncmp(opt, "set_ess_en:", 11) == 0) {
  2243. bool debug_ess_en;
  2244. debug_ess_en = !strncmp(opt + 11, "1", 1);
  2245. pr_notice("[debug] debug_ess_en=%d\n", debug_ess_en);
  2246. disp_aal_set_ess_en(debug_ess_en);
  2247. } else if (strncmp(opt, "set_dre_en:", 11) == 0) {
  2248. bool debug_dre_en;
  2249. debug_dre_en = !strncmp(opt + 11, "1", 1);
  2250. pr_notice("[debug] debug_dre_en=%d\n", debug_dre_en);
  2251. disp_aal_set_dre_en(debug_dre_en);
  2252. #ifdef CONFIG_MTK_DRE30_SUPPORT
  2253. } else if (strncmp(opt, "aal_sram_method:", 16) == 0) {
  2254. bool aal_align_eof;
  2255. aal_align_eof = !strncmp(opt + 11, "0", 1);
  2256. aal_sram_method = aal_align_eof ? AAL_SRAM_EOF : AAL_SRAM_SOF;
  2257. pr_notice("[debug] aal_sram_method=%d\n", aal_sram_method);
  2258. #endif
  2259. } else if (strncmp(opt, "debugdump:", 10) == 0) {
  2260. pr_notice("[debug] skip_set_param=%d\n",
  2261. debug_skip_set_param);
  2262. pr_notice("[debug] skip_dre3_irq=%d\n",
  2263. debug_skip_dre3_irq);
  2264. pr_notice("[debug] bypass_alg_mode=%d\n",
  2265. debug_bypass_alg_mode);
  2266. pr_notice("[debug] debug_dump_reg_irq=%d\n",
  2267. debug_dump_reg_irq);
  2268. pr_notice("[debug] dump_blk_x=%d dump_blk_y=%d\n",
  2269. dump_blk_x, dump_blk_y);
  2270. pr_notice("[debug] skip_first_br=%d\n",
  2271. debug_skip_first_br);
  2272. pr_notice("[debug] debug_flow_log=%d\n",
  2273. debug_flow_log);
  2274. pr_notice("[debug] debug_api_log=%d\n",
  2275. debug_api_log);
  2276. pr_notice("[debug] debug_write_cmdq_log=%d\n",
  2277. debug_write_cmdq_log);
  2278. pr_notice("[debug] debug_irq_log=%d\n",
  2279. debug_irq_log);
  2280. pr_notice("[debug] debug_dump_aal_hist=%d\n",
  2281. debug_dump_aal_hist);
  2282. pr_notice("[debug] debug_dump_input_param=%d\n",
  2283. debug_dump_input_param);
  2284. pr_notice("[debug] debug_ess_level=%d\n", g_aal_ess_level);
  2285. pr_notice("[debug] debug_ess_en=%d\n", g_aal_ess_en);
  2286. pr_notice("[debug] debug_dre_en=%d\n", g_aal_dre_en);
  2287. }
  2288. }