mtk_disp_gamma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/soc/mediatek/mtk-cmdq.h>
  21. #include "mtk_drm_crtc.h"
  22. #include "mtk_drm_ddp_comp.h"
  23. #include "mtk_drm_drv.h"
  24. #include "mtk_log.h"
  25. #include "mtk_disp_gamma.h"
  26. #include "mtk_dump.h"
  27. #define DISP_GAMMA_EN 0x0000
  28. #define DISP_GAMMA_CFG 0x0020
  29. #define DISP_GAMMA_SIZE 0x0030
  30. #define DISP_GAMMA_LUT 0x0700
  31. #define LUT_10BIT_MASK 0x03ff
  32. #define GAMMA_EN BIT(0)
  33. #define GAMMA_LUT_EN BIT(1)
  34. #define GAMMA_RELAYMODE BIT(0)
  35. static unsigned int g_gamma_relay_value[DISP_GAMMA_TOTAL];
  36. #define index_of_gamma(module) ((module == DDP_COMPONENT_GAMMA0) ? 0 : 1)
  37. static struct DISP_GAMMA_LUT_T *g_disp_gamma_lut[DISP_GAMMA_TOTAL] = { NULL };
  38. static DEFINE_MUTEX(g_gamma_global_lock);
  39. static atomic_t g_gamma_is_clock_on[DISP_GAMMA_TOTAL] = { ATOMIC_INIT(0),
  40. ATOMIC_INIT(0)};
  41. static DEFINE_SPINLOCK(g_gamma_clock_lock);
  42. /* TODO */
  43. /* static ddp_module_notify g_gamma_ddp_notify; */
  44. enum GAMMA_IOCTL_CMD {
  45. SET_GAMMALUT = 0,
  46. };
  47. struct mtk_disp_gamma_data {
  48. bool support_shadow;
  49. };
  50. struct mtk_disp_gamma {
  51. struct mtk_ddp_comp ddp_comp;
  52. struct drm_crtc *crtc;
  53. const struct mtk_disp_gamma_data *data;
  54. };
  55. static void mtk_gamma_init(struct mtk_ddp_comp *comp,
  56. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  57. {
  58. cmdq_pkt_write(handle, comp->cmdq_base,
  59. comp->regs_pa + DISP_GAMMA_SIZE,
  60. (cfg->w << 16) | cfg->h, ~0);
  61. cmdq_pkt_write(handle, comp->cmdq_base,
  62. comp->regs_pa + DISP_GAMMA_EN, GAMMA_EN, ~0);
  63. }
  64. static void mtk_gamma_config(struct mtk_ddp_comp *comp,
  65. struct mtk_ddp_config *cfg,
  66. struct cmdq_pkt *handle)
  67. {
  68. /* TODO: only call init function if frame dirty */
  69. mtk_gamma_init(comp, cfg, handle);
  70. //cmdq_pkt_write(handle, comp->cmdq_base,
  71. // comp->regs_pa + DISP_GAMMA_SIZE,
  72. // (cfg->w << 16) | cfg->h, ~0);
  73. //cmdq_pkt_write(handle, comp->cmdq_base,
  74. // comp->regs_pa + DISP_GAMMA_CFG,
  75. // GAMMA_RELAYMODE, BIT(0));
  76. }
  77. static int mtk_gamma_write_lut_reg(struct mtk_ddp_comp *comp,
  78. struct cmdq_pkt *handle, int lock)
  79. {
  80. struct DISP_GAMMA_LUT_T *gamma_lut;
  81. int i;
  82. int ret = 0;
  83. int id = index_of_gamma(comp->id);
  84. if (lock)
  85. mutex_lock(&g_gamma_global_lock);
  86. gamma_lut = g_disp_gamma_lut[id];
  87. if (gamma_lut == NULL) {
  88. DDPINFO("%s: table [%d] not initialized\n", __func__, id);
  89. ret = -EFAULT;
  90. goto gamma_write_lut_unlock;
  91. }
  92. for (i = 0; i < DISP_GAMMA_LUT_SIZE; i++) {
  93. cmdq_pkt_write(handle, comp->cmdq_base,
  94. (comp->regs_pa + DISP_GAMMA_LUT + i * 4),
  95. gamma_lut->lut[i], ~0);
  96. if ((i & 0x3f) == 0) {
  97. DDPINFO("[0x%08lx](%d) = 0x%x\n",
  98. (long)(comp->regs_pa + DISP_GAMMA_LUT + i * 4),
  99. i, gamma_lut->lut[i]);
  100. }
  101. }
  102. i--;
  103. DDPINFO("[0x%08lx](%d) = 0x%x\n",
  104. (long)(comp->regs_pa + DISP_GAMMA_LUT + i * 4), i, gamma_lut->lut[i]);
  105. if ((int)(gamma_lut->lut[0] & 0x3FF) -
  106. (int)(gamma_lut->lut[510] & 0x3FF) > 0) {
  107. cmdq_pkt_write(handle, comp->cmdq_base,
  108. comp->regs_pa + DISP_GAMMA_CFG, 0x1 << 2, 0x4);
  109. DDPINFO("decreasing LUT\n");
  110. } else {
  111. cmdq_pkt_write(handle, comp->cmdq_base,
  112. comp->regs_pa + DISP_GAMMA_CFG, 0x0 << 2, 0x4);
  113. DDPINFO("Incremental LUT\n");
  114. }
  115. cmdq_pkt_write(handle, comp->cmdq_base,
  116. comp->regs_pa + DISP_GAMMA_CFG,
  117. 0x2 | g_gamma_relay_value[id], 0x3);
  118. gamma_write_lut_unlock:
  119. if (lock)
  120. mutex_unlock(&g_gamma_global_lock);
  121. return ret;
  122. }
  123. static int mtk_gamma_set_lut(struct mtk_ddp_comp *comp,
  124. struct cmdq_pkt *handle, struct DISP_GAMMA_LUT_T *user_gamma_lut)
  125. {
  126. /* TODO: use CPU to write register */
  127. int ret = 0;
  128. int id;
  129. struct DISP_GAMMA_LUT_T *gamma_lut, *old_lut;
  130. DDPINFO("%s\n", __func__);
  131. gamma_lut = kmalloc(sizeof(struct DISP_GAMMA_LUT_T),
  132. GFP_KERNEL);
  133. if (gamma_lut == NULL) {
  134. DDPPR_ERR("%s: no memory\n", __func__);
  135. return -EFAULT;
  136. }
  137. if (user_gamma_lut == NULL) {
  138. ret = -EFAULT;
  139. kfree(gamma_lut);
  140. } else {
  141. memcpy(gamma_lut, user_gamma_lut,
  142. sizeof(struct DISP_GAMMA_LUT_T));
  143. id = index_of_gamma(comp->id);
  144. if (id >= 0 && id < DISP_GAMMA_TOTAL) {
  145. mutex_lock(&g_gamma_global_lock);
  146. old_lut = g_disp_gamma_lut[id];
  147. g_disp_gamma_lut[id] = gamma_lut;
  148. DDPINFO("%s: Set module(%d) lut\n", __func__, comp->id);
  149. ret = mtk_gamma_write_lut_reg(comp, handle, 0);
  150. mutex_unlock(&g_gamma_global_lock);
  151. if (old_lut != NULL)
  152. kfree(old_lut);
  153. if (comp->mtk_crtc != NULL)
  154. mtk_crtc_check_trigger(comp->mtk_crtc, false,
  155. false);
  156. } else {
  157. DDPPR_ERR("%s: invalid ID = %d\n", __func__, comp->id);
  158. ret = -EFAULT;
  159. }
  160. }
  161. return ret;
  162. }
  163. int mtk_drm_ioctl_set_gammalut(struct drm_device *dev, void *data,
  164. struct drm_file *file_priv)
  165. {
  166. struct mtk_drm_private *private = dev->dev_private;
  167. struct mtk_ddp_comp *comp = private->ddp_comp[DDP_COMPONENT_GAMMA0];
  168. struct drm_crtc *crtc = private->crtc[0];
  169. return mtk_crtc_user_cmd(crtc, comp, SET_GAMMALUT, data);
  170. }
  171. static void mtk_gamma_start(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  172. {
  173. DDPINFO("%s\n", __func__);
  174. cmdq_pkt_write(handle, comp->cmdq_base,
  175. comp->regs_pa + DISP_GAMMA_EN, GAMMA_EN, ~0);
  176. mtk_gamma_write_lut_reg(comp, handle, 0);
  177. }
  178. static void mtk_gamma_stop(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  179. {
  180. cmdq_pkt_write(handle, comp->cmdq_base,
  181. comp->regs_pa + DISP_GAMMA_EN, 0x0, ~0);
  182. }
  183. static void mtk_gamma_bypass(struct mtk_ddp_comp *comp, struct cmdq_pkt *handle)
  184. {
  185. DDPINFO("%s\n", __func__);
  186. cmdq_pkt_write(handle, comp->cmdq_base,
  187. comp->regs_pa + DISP_GAMMA_CFG, 0x1, 0x1);
  188. g_gamma_relay_value[index_of_gamma(comp->id)] = 0x1;
  189. }
  190. static void mtk_gamma_set(struct mtk_ddp_comp *comp,
  191. struct drm_crtc_state *state, struct cmdq_pkt *handle)
  192. {
  193. unsigned int i;
  194. struct drm_color_lut *lut;
  195. u32 word = 0;
  196. u32 word_first = 0;
  197. u32 word_last = 0;
  198. DDPINFO("%s\n", __func__);
  199. if (state->gamma_lut) {
  200. cmdq_pkt_write(handle, comp->cmdq_base,
  201. comp->regs_pa + DISP_GAMMA_CFG,
  202. 1<<GAMMA_LUT_EN, 1<<GAMMA_LUT_EN);
  203. lut = (struct drm_color_lut *)state->gamma_lut->data;
  204. for (i = 0; i < MTK_LUT_SIZE; i++) {
  205. word = GAMMA_ENTRY(lut[i].red >> 6,
  206. lut[i].green >> 6, lut[i].blue >> 6);
  207. cmdq_pkt_write(handle, comp->cmdq_base,
  208. comp->regs_pa
  209. + (DISP_GAMMA_LUT + i * 4),
  210. word, ~0);
  211. // first & last word for
  212. // decreasing/incremental LUT
  213. if (i == 0)
  214. word_first = word;
  215. else if (i == MTK_LUT_SIZE - 1)
  216. word_last = word;
  217. }
  218. }
  219. if ((word_first - word_last) > 0) {
  220. cmdq_pkt_write(handle, comp->cmdq_base,
  221. comp->regs_pa + DISP_GAMMA_CFG, 0x1 << 2, 0x4);
  222. DDPINFO("decreasing LUT\n");
  223. } else {
  224. cmdq_pkt_write(handle, comp->cmdq_base,
  225. comp->regs_pa + DISP_GAMMA_CFG, 0x0 << 2, 0x4);
  226. DDPINFO("Incremental LUT\n");
  227. }
  228. }
  229. static int mtk_gamma_user_cmd(struct mtk_ddp_comp *comp,
  230. struct cmdq_pkt *handle, unsigned int cmd, void *data)
  231. {
  232. DDPINFO("%s: cmd: %d\n", __func__, cmd);
  233. switch (cmd) {
  234. case SET_GAMMALUT:
  235. {
  236. struct DISP_GAMMA_LUT_T *config = data;
  237. if (mtk_gamma_set_lut(comp, handle, config) < 0) {
  238. DDPPR_ERR("%s: failed\n", __func__);
  239. return -EFAULT;
  240. }
  241. }
  242. break;
  243. default:
  244. DDPPR_ERR("%s: error cmd: %d\n", __func__, cmd);
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. static void mtk_gamma_prepare(struct mtk_ddp_comp *comp)
  250. {
  251. mtk_ddp_comp_clk_prepare(comp);
  252. atomic_set(&g_gamma_is_clock_on[index_of_gamma(comp->id)], 1);
  253. }
  254. static void mtk_gamma_unprepare(struct mtk_ddp_comp *comp)
  255. {
  256. unsigned long flags;
  257. DDPINFO("%s @ %d......... spin_trylock_irqsave ++ ",
  258. __func__, __LINE__);
  259. spin_lock_irqsave(&g_gamma_clock_lock, flags);
  260. DDPINFO("%s @ %d......... spin_trylock_irqsave -- ",
  261. __func__, __LINE__);
  262. atomic_set(&g_gamma_is_clock_on[index_of_gamma(comp->id)], 0);
  263. spin_unlock_irqrestore(&g_gamma_clock_lock, flags);
  264. DDPINFO("%s @ %d......... spin_unlock_irqrestore ",
  265. __func__, __LINE__);
  266. mtk_ddp_comp_clk_unprepare(comp);
  267. }
  268. static const struct mtk_ddp_comp_funcs mtk_disp_gamma_funcs = {
  269. .gamma_set = mtk_gamma_set,
  270. .config = mtk_gamma_config,
  271. .start = mtk_gamma_start,
  272. .stop = mtk_gamma_stop,
  273. .bypass = mtk_gamma_bypass,
  274. .user_cmd = mtk_gamma_user_cmd,
  275. .prepare = mtk_gamma_prepare,
  276. .unprepare = mtk_gamma_unprepare,
  277. };
  278. static int mtk_disp_gamma_bind(struct device *dev, struct device *master,
  279. void *data)
  280. {
  281. struct mtk_disp_gamma *priv = dev_get_drvdata(dev);
  282. struct drm_device *drm_dev = data;
  283. int ret;
  284. DDPINFO("%s\n", __func__);
  285. ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
  286. if (ret < 0) {
  287. dev_err(dev, "Failed to register component %s: %d\n",
  288. dev->of_node->full_name, ret);
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. static void mtk_disp_gamma_unbind(struct device *dev, struct device *master,
  294. void *data)
  295. {
  296. struct mtk_disp_gamma *priv = dev_get_drvdata(dev);
  297. struct drm_device *drm_dev = data;
  298. mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
  299. }
  300. static const struct component_ops mtk_disp_gamma_component_ops = {
  301. .bind = mtk_disp_gamma_bind, .unbind = mtk_disp_gamma_unbind,
  302. };
  303. void mtk_gamma_dump(struct mtk_ddp_comp *comp)
  304. {
  305. void __iomem *baddr = comp->regs;
  306. DDPDUMP("== %s REGS ==\n", mtk_dump_comp_str(comp));
  307. mtk_cust_dump_reg(baddr, 0x0, 0x20, 0x24, 0x28);
  308. }
  309. static int mtk_disp_gamma_probe(struct platform_device *pdev)
  310. {
  311. struct device *dev = &pdev->dev;
  312. struct mtk_disp_gamma *priv;
  313. enum mtk_ddp_comp_id comp_id;
  314. int ret;
  315. DDPINFO("%s+\n", __func__);
  316. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  317. if (priv == NULL)
  318. return -ENOMEM;
  319. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_GAMMA);
  320. if ((int)comp_id < 0) {
  321. DDPPR_ERR("Failed to identify by alias: %d\n", comp_id);
  322. return comp_id;
  323. }
  324. ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
  325. &mtk_disp_gamma_funcs);
  326. if (ret != 0) {
  327. DDPPR_ERR("Failed to initialize component: %d\n", ret);
  328. return ret;
  329. }
  330. platform_set_drvdata(pdev, priv);
  331. pm_runtime_enable(dev);
  332. ret = component_add(dev, &mtk_disp_gamma_component_ops);
  333. if (ret != 0) {
  334. dev_err(dev, "Failed to add component: %d\n", ret);
  335. pm_runtime_disable(dev);
  336. }
  337. DDPINFO("%s-\n", __func__);
  338. return ret;
  339. }
  340. static int mtk_disp_gamma_remove(struct platform_device *pdev)
  341. {
  342. component_del(&pdev->dev, &mtk_disp_gamma_component_ops);
  343. pm_runtime_disable(&pdev->dev);
  344. return 0;
  345. }
  346. static const struct mtk_disp_gamma_data mt6779_gamma_driver_data = {
  347. .support_shadow = false,
  348. };
  349. static const struct mtk_disp_gamma_data mt6885_gamma_driver_data = {
  350. .support_shadow = false,
  351. };
  352. static const struct mtk_disp_gamma_data mt6873_gamma_driver_data = {
  353. .support_shadow = false,
  354. };
  355. static const struct mtk_disp_gamma_data mt6853_gamma_driver_data = {
  356. .support_shadow = false,
  357. };
  358. static const struct mtk_disp_gamma_data mt6833_gamma_driver_data = {
  359. .support_shadow = false,
  360. };
  361. static const struct of_device_id mtk_disp_gamma_driver_dt_match[] = {
  362. { .compatible = "mediatek,mt6779-disp-gamma",
  363. .data = &mt6779_gamma_driver_data},
  364. { .compatible = "mediatek,mt6885-disp-gamma",
  365. .data = &mt6885_gamma_driver_data},
  366. { .compatible = "mediatek,mt6873-disp-gamma",
  367. .data = &mt6873_gamma_driver_data},
  368. { .compatible = "mediatek,mt6853-disp-gamma",
  369. .data = &mt6853_gamma_driver_data},
  370. { .compatible = "mediatek,mt6833-disp-gamma",
  371. .data = &mt6833_gamma_driver_data},
  372. {},
  373. };
  374. MODULE_DEVICE_TABLE(of, mtk_disp_gamma_driver_dt_match);
  375. struct platform_driver mtk_disp_gamma_driver = {
  376. .probe = mtk_disp_gamma_probe,
  377. .remove = mtk_disp_gamma_remove,
  378. .driver = {
  379. .name = "mediatek-disp-gamma",
  380. .owner = THIS_MODULE,
  381. .of_match_table = mtk_disp_gamma_driver_dt_match,
  382. },
  383. };