pwm-mtk-disp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * MediaTek display pulse-width-modulation controller driver.
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author: YH Huang <yh.huang@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/slab.h>
  24. #define DISP_PWM_EN 0x00
  25. #define PWM_CLKDIV_SHIFT 16
  26. #define PWM_CLKDIV_MAX 0x3ff
  27. #define PWM_CLKDIV_MASK (PWM_CLKDIV_MAX << PWM_CLKDIV_SHIFT)
  28. #define PWM_PERIOD_BIT_WIDTH 12
  29. #define PWM_PERIOD_MASK ((1 << PWM_PERIOD_BIT_WIDTH) - 1)
  30. #define PWM_HIGH_WIDTH_SHIFT 16
  31. #define PWM_HIGH_WIDTH_MASK (0x1fff << PWM_HIGH_WIDTH_SHIFT)
  32. struct mtk_pwm_data {
  33. u32 enable_mask;
  34. unsigned int con0;
  35. u32 con0_sel;
  36. unsigned int con1;
  37. bool has_commit;
  38. unsigned int commit;
  39. unsigned int commit_mask;
  40. unsigned int bls_debug;
  41. u32 bls_debug_mask;
  42. };
  43. struct mtk_disp_pwm {
  44. struct pwm_chip chip;
  45. const struct mtk_pwm_data *data;
  46. struct clk *clk_main;
  47. struct clk *clk_mm;
  48. void __iomem *base;
  49. };
  50. static inline struct mtk_disp_pwm *to_mtk_disp_pwm(struct pwm_chip *chip)
  51. {
  52. return container_of(chip, struct mtk_disp_pwm, chip);
  53. }
  54. static void mtk_disp_pwm_update_bits(struct mtk_disp_pwm *mdp, u32 offset,
  55. u32 mask, u32 data)
  56. {
  57. void __iomem *address = mdp->base + offset;
  58. u32 value;
  59. value = readl(address);
  60. value &= ~mask;
  61. value |= data;
  62. writel(value, address);
  63. }
  64. static int mtk_disp_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  65. int duty_ns, int period_ns)
  66. {
  67. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  68. u32 clk_div, period, high_width, value;
  69. u64 div, rate;
  70. int err;
  71. /*
  72. * Find period, high_width and clk_div to suit duty_ns and period_ns.
  73. * Calculate proper div value to keep period value in the bound.
  74. *
  75. * period_ns = 10^9 * (clk_div + 1) * (period + 1) / PWM_CLK_RATE
  76. * duty_ns = 10^9 * (clk_div + 1) * high_width / PWM_CLK_RATE
  77. *
  78. * period = (PWM_CLK_RATE * period_ns) / (10^9 * (clk_div + 1)) - 1
  79. * high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1))
  80. */
  81. rate = clk_get_rate(mdp->clk_main);
  82. clk_div = div_u64(rate * period_ns, NSEC_PER_SEC) >>
  83. PWM_PERIOD_BIT_WIDTH;
  84. if (clk_div > PWM_CLKDIV_MAX)
  85. return -EINVAL;
  86. div = NSEC_PER_SEC * (clk_div + 1);
  87. period = div64_u64(rate * period_ns, div);
  88. if (period > 0)
  89. period--;
  90. high_width = div64_u64(rate * duty_ns, div);
  91. value = period | (high_width << PWM_HIGH_WIDTH_SHIFT);
  92. err = clk_enable(mdp->clk_main);
  93. if (err < 0)
  94. return err;
  95. err = clk_enable(mdp->clk_mm);
  96. if (err < 0) {
  97. clk_disable(mdp->clk_main);
  98. return err;
  99. }
  100. mtk_disp_pwm_update_bits(mdp, mdp->data->con0,
  101. PWM_CLKDIV_MASK,
  102. clk_div << PWM_CLKDIV_SHIFT);
  103. mtk_disp_pwm_update_bits(mdp, mdp->data->con1,
  104. PWM_PERIOD_MASK | PWM_HIGH_WIDTH_MASK,
  105. value);
  106. if (mdp->data->has_commit) {
  107. mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
  108. mdp->data->commit_mask,
  109. mdp->data->commit_mask);
  110. mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
  111. mdp->data->commit_mask,
  112. 0x0);
  113. }
  114. clk_disable(mdp->clk_mm);
  115. clk_disable(mdp->clk_main);
  116. return 0;
  117. }
  118. static int mtk_disp_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  119. {
  120. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  121. int err;
  122. err = clk_enable(mdp->clk_main);
  123. if (err < 0)
  124. return err;
  125. err = clk_enable(mdp->clk_mm);
  126. if (err < 0) {
  127. clk_disable(mdp->clk_main);
  128. return err;
  129. }
  130. mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
  131. mdp->data->enable_mask);
  132. return 0;
  133. }
  134. static void mtk_disp_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  135. {
  136. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  137. mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
  138. 0x0);
  139. clk_disable(mdp->clk_mm);
  140. clk_disable(mdp->clk_main);
  141. }
  142. static const struct pwm_ops mtk_disp_pwm_ops = {
  143. .config = mtk_disp_pwm_config,
  144. .enable = mtk_disp_pwm_enable,
  145. .disable = mtk_disp_pwm_disable,
  146. .owner = THIS_MODULE,
  147. };
  148. static int mtk_disp_pwm_probe(struct platform_device *pdev)
  149. {
  150. struct mtk_disp_pwm *mdp;
  151. struct resource *r;
  152. int ret;
  153. mdp = devm_kzalloc(&pdev->dev, sizeof(*mdp), GFP_KERNEL);
  154. if (!mdp)
  155. return -ENOMEM;
  156. mdp->data = of_device_get_match_data(&pdev->dev);
  157. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. mdp->base = devm_ioremap_resource(&pdev->dev, r);
  159. if (IS_ERR(mdp->base))
  160. return PTR_ERR(mdp->base);
  161. mdp->clk_main = devm_clk_get(&pdev->dev, "main");
  162. if (IS_ERR(mdp->clk_main))
  163. return PTR_ERR(mdp->clk_main);
  164. mdp->clk_mm = devm_clk_get(&pdev->dev, "mm");
  165. if (IS_ERR(mdp->clk_mm))
  166. return PTR_ERR(mdp->clk_mm);
  167. ret = clk_prepare(mdp->clk_main);
  168. if (ret < 0)
  169. return ret;
  170. ret = clk_prepare(mdp->clk_mm);
  171. if (ret < 0)
  172. goto disable_clk_main;
  173. mdp->chip.dev = &pdev->dev;
  174. mdp->chip.ops = &mtk_disp_pwm_ops;
  175. mdp->chip.base = -1;
  176. mdp->chip.npwm = 1;
  177. ret = pwmchip_add(&mdp->chip);
  178. if (ret < 0) {
  179. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  180. goto disable_clk_mm;
  181. }
  182. platform_set_drvdata(pdev, mdp);
  183. /*
  184. * For MT2701, disable double buffer before writing register
  185. * and select manual mode and use PWM_PERIOD/PWM_HIGH_WIDTH.
  186. */
  187. if (!mdp->data->has_commit) {
  188. mtk_disp_pwm_update_bits(mdp, mdp->data->bls_debug,
  189. mdp->data->bls_debug_mask,
  190. mdp->data->bls_debug_mask);
  191. mtk_disp_pwm_update_bits(mdp, mdp->data->con0,
  192. mdp->data->con0_sel,
  193. mdp->data->con0_sel);
  194. }
  195. return 0;
  196. disable_clk_mm:
  197. clk_unprepare(mdp->clk_mm);
  198. disable_clk_main:
  199. clk_unprepare(mdp->clk_main);
  200. return ret;
  201. }
  202. static int mtk_disp_pwm_remove(struct platform_device *pdev)
  203. {
  204. struct mtk_disp_pwm *mdp = platform_get_drvdata(pdev);
  205. int ret;
  206. ret = pwmchip_remove(&mdp->chip);
  207. clk_unprepare(mdp->clk_mm);
  208. clk_unprepare(mdp->clk_main);
  209. return ret;
  210. }
  211. static const struct mtk_pwm_data mt2701_pwm_data = {
  212. .enable_mask = BIT(16),
  213. .con0 = 0xa8,
  214. .con0_sel = 0x2,
  215. .con1 = 0xac,
  216. .has_commit = false,
  217. .bls_debug = 0xb0,
  218. .bls_debug_mask = 0x3,
  219. };
  220. static const struct mtk_pwm_data mt8173_pwm_data = {
  221. .enable_mask = BIT(0),
  222. .con0 = 0x10,
  223. .con0_sel = 0x0,
  224. .con1 = 0x14,
  225. .has_commit = true,
  226. .commit = 0x8,
  227. .commit_mask = 0x1,
  228. };
  229. static const struct of_device_id mtk_disp_pwm_of_match[] = {
  230. { .compatible = "mediatek,mt2701-disp-pwm", .data = &mt2701_pwm_data},
  231. { .compatible = "mediatek,mt6595-disp-pwm", .data = &mt8173_pwm_data},
  232. { .compatible = "mediatek,mt8173-disp-pwm", .data = &mt8173_pwm_data},
  233. { }
  234. };
  235. MODULE_DEVICE_TABLE(of, mtk_disp_pwm_of_match);
  236. static struct platform_driver mtk_disp_pwm_driver = {
  237. .driver = {
  238. .name = "mediatek-disp-pwm",
  239. .of_match_table = mtk_disp_pwm_of_match,
  240. },
  241. .probe = mtk_disp_pwm_probe,
  242. .remove = mtk_disp_pwm_remove,
  243. };
  244. module_platform_driver(mtk_disp_pwm_driver);
  245. MODULE_AUTHOR("YH Huang <yh.huang@mediatek.com>");
  246. MODULE_DESCRIPTION("MediaTek SoC display PWM driver");
  247. MODULE_LICENSE("GPL v2");