845-pwm-add-mediatek-support.patch 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. From fc8f96309c21c1bc3276427309cd7d361347d66e Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Mon, 7 Dec 2015 17:16:50 +0100
  4. Subject: [PATCH 52/53] pwm: add mediatek support
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. drivers/pwm/Kconfig | 9 +++
  8. drivers/pwm/Makefile | 1 +
  9. drivers/pwm/pwm-mediatek.c | 173 ++++++++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 183 insertions(+)
  11. create mode 100644 drivers/pwm/pwm-mediatek.c
  12. --- a/drivers/pwm/Kconfig
  13. +++ b/drivers/pwm/Kconfig
  14. @@ -393,6 +393,15 @@ config PWM_MEDIATEK
  15. To compile this driver as a module, choose M here: the module
  16. will be called pwm-mediatek.
  17. +config PWM_MEDIATEK_RAMIPS
  18. + tristate "Mediatek PWM support"
  19. + depends on RALINK && OF
  20. + help
  21. + Generic PWM framework driver for Mediatek ARM SoC.
  22. +
  23. + To compile this driver as a module, choose M here: the module
  24. + will be called pwm-mxs.
  25. +
  26. config PWM_MXS
  27. tristate "Freescale MXS PWM support"
  28. depends on ARCH_MXS || COMPILE_TEST
  29. --- a/drivers/pwm/Makefile
  30. +++ b/drivers/pwm/Makefile
  31. @@ -34,6 +34,7 @@ obj-$(CONFIG_PWM_LPSS_PCI) += pwm-lpss-p
  32. obj-$(CONFIG_PWM_LPSS_PLATFORM) += pwm-lpss-platform.o
  33. obj-$(CONFIG_PWM_MESON) += pwm-meson.o
  34. obj-$(CONFIG_PWM_MEDIATEK) += pwm-mediatek.o
  35. +obj-$(CONFIG_PWM_MEDIATEK_RAMIPS) += pwm-mediatek-ramips.o
  36. obj-$(CONFIG_PWM_MTK_DISP) += pwm-mtk-disp.o
  37. obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
  38. obj-$(CONFIG_PWM_NTXEC) += pwm-ntxec.o
  39. --- /dev/null
  40. +++ b/drivers/pwm/pwm-mediatek-ramips.c
  41. @@ -0,0 +1,197 @@
  42. +/*
  43. + * Mediatek Pulse Width Modulator driver
  44. + *
  45. + * Copyright (C) 2015 John Crispin <[email protected]>
  46. + *
  47. + * This file is licensed under the terms of the GNU General Public
  48. + * License version 2. This program is licensed "as is" without any
  49. + * warranty of any kind, whether express or implied.
  50. + */
  51. +
  52. +#include <linux/err.h>
  53. +#include <linux/io.h>
  54. +#include <linux/ioport.h>
  55. +#include <linux/kernel.h>
  56. +#include <linux/module.h>
  57. +#include <linux/of.h>
  58. +#include <linux/platform_device.h>
  59. +#include <linux/pwm.h>
  60. +#include <linux/slab.h>
  61. +#include <linux/types.h>
  62. +
  63. +#define NUM_PWM 4
  64. +
  65. +/* PWM registers and bits definitions */
  66. +#define PWMCON 0x00
  67. +#define PWMHDUR 0x04
  68. +#define PWMLDUR 0x08
  69. +#define PWMGDUR 0x0c
  70. +#define PWMWAVENUM 0x28
  71. +#define PWMDWIDTH 0x2c
  72. +#define PWMTHRES 0x30
  73. +
  74. +/**
  75. + * struct mtk_pwm_chip - struct representing pwm chip
  76. + *
  77. + * @mmio_base: base address of pwm chip
  78. + * @chip: linux pwm chip representation
  79. + */
  80. +struct mtk_pwm_chip {
  81. + void __iomem *mmio_base;
  82. + struct pwm_chip chip;
  83. +};
  84. +
  85. +static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
  86. +{
  87. + return container_of(chip, struct mtk_pwm_chip, chip);
  88. +}
  89. +
  90. +static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
  91. + unsigned long offset)
  92. +{
  93. + return ioread32(chip->mmio_base + 0x10 + (num * 0x40) + offset);
  94. +}
  95. +
  96. +static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
  97. + unsigned int num, unsigned long offset,
  98. + unsigned long val)
  99. +{
  100. + iowrite32(val, chip->mmio_base + 0x10 + (num * 0x40) + offset);
  101. +}
  102. +
  103. +static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  104. + int duty_ns, int period_ns)
  105. +{
  106. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  107. + u32 resolution = 100 / 4;
  108. + u32 clkdiv = 0;
  109. +
  110. + while (period_ns / resolution > 8191) {
  111. + clkdiv++;
  112. + resolution *= 2;
  113. + }
  114. +
  115. + if (clkdiv > 7)
  116. + return -1;
  117. +
  118. + mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | BIT(3) | clkdiv);
  119. + mtk_pwm_writel(pc, pwm->hwpwm, PWMDWIDTH, period_ns / resolution);
  120. + mtk_pwm_writel(pc, pwm->hwpwm, PWMTHRES, duty_ns / resolution);
  121. + return 0;
  122. +}
  123. +
  124. +static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  125. +{
  126. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  127. + u32 val;
  128. +
  129. + val = ioread32(pc->mmio_base);
  130. + val |= BIT(pwm->hwpwm);
  131. + iowrite32(val, pc->mmio_base);
  132. +
  133. + return 0;
  134. +}
  135. +
  136. +static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  137. +{
  138. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  139. + u32 val;
  140. +
  141. + val = ioread32(pc->mmio_base);
  142. + val &= ~BIT(pwm->hwpwm);
  143. + iowrite32(val, pc->mmio_base);
  144. +}
  145. +
  146. +static int mtk_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  147. + const struct pwm_state *state)
  148. +{
  149. + int err;
  150. + bool enabled = pwm->state.enabled;
  151. +
  152. + if (!state->enabled) {
  153. + if (enabled)
  154. + mtk_pwm_disable(chip, pwm);
  155. +
  156. + return 0;
  157. + }
  158. +
  159. + err = mtk_pwm_config(pwm->chip, pwm,
  160. + state->duty_cycle, state->period);
  161. + if (err)
  162. + return err;
  163. +
  164. + if (!enabled)
  165. + err = mtk_pwm_enable(chip, pwm);
  166. +
  167. + return err;
  168. +}
  169. +
  170. +static const struct pwm_ops mtk_pwm_ops = {
  171. + .apply = mtk_pwm_apply,
  172. + .owner = THIS_MODULE,
  173. +};
  174. +
  175. +static int mtk_pwm_probe(struct platform_device *pdev)
  176. +{
  177. + struct mtk_pwm_chip *pc;
  178. + struct resource *r;
  179. + int ret;
  180. +
  181. + pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  182. + if (!pc)
  183. + return -ENOMEM;
  184. +
  185. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  186. + pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  187. + if (IS_ERR(pc->mmio_base))
  188. + return PTR_ERR(pc->mmio_base);
  189. +
  190. + platform_set_drvdata(pdev, pc);
  191. +
  192. + pc->chip.dev = &pdev->dev;
  193. + pc->chip.ops = &mtk_pwm_ops;
  194. + pc->chip.base = -1;
  195. + pc->chip.npwm = NUM_PWM;
  196. +
  197. + ret = pwmchip_add(&pc->chip);
  198. + if (ret < 0)
  199. + dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  200. +
  201. + return ret;
  202. +}
  203. +
  204. +static int mtk_pwm_remove(struct platform_device *pdev)
  205. +{
  206. + struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
  207. + int i;
  208. +
  209. + for (i = 0; i < NUM_PWM; i++)
  210. + pwm_disable(&pc->chip.pwms[i]);
  211. +
  212. + pwmchip_remove(&pc->chip);
  213. +
  214. + return 0;
  215. +}
  216. +
  217. +static const struct of_device_id mtk_pwm_of_match[] = {
  218. + { .compatible = "mediatek,mt7628-pwm" },
  219. + { }
  220. +};
  221. +
  222. +MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
  223. +
  224. +static struct platform_driver mtk_pwm_driver = {
  225. + .driver = {
  226. + .name = "mtk-pwm",
  227. + .owner = THIS_MODULE,
  228. + .of_match_table = mtk_pwm_of_match,
  229. + },
  230. + .probe = mtk_pwm_probe,
  231. + .remove = mtk_pwm_remove,
  232. +};
  233. +
  234. +module_platform_driver(mtk_pwm_driver);
  235. +
  236. +MODULE_LICENSE("GPL");
  237. +MODULE_AUTHOR("John Crispin <[email protected]>");
  238. +MODULE_ALIAS("platform:mtk-pwm");