0065-mt7628-pww.patch 5.1 KB

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