0071-pwm-add-pwm-mediatek.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. From 6f5941c93bdf7649f392f1263b9068d360ceab4d Mon Sep 17 00:00:00 2001
  2. From: John Crispin <john@phrozen.org>
  3. Date: Fri, 6 May 2016 02:55:48 +0200
  4. Subject: [PATCH 071/102] pwm: add pwm-mediatek
  5. Signed-off-by: John Crispin <john@phrozen.org>
  6. ---
  7. arch/arm/boot/dts/mt7623-evb.dts | 17 +++
  8. arch/arm/boot/dts/mt7623.dtsi | 22 ++++
  9. drivers/pwm/Kconfig | 9 ++
  10. drivers/pwm/Makefile | 1 +
  11. drivers/pwm/pwm-mediatek.c | 230 ++++++++++++++++++++++++++++++++++++++
  12. 5 files changed, 279 insertions(+)
  13. create mode 100644 drivers/pwm/pwm-mediatek.c
  14. --- a/arch/arm/boot/dts/mt7623-evb.dts
  15. +++ b/arch/arm/boot/dts/mt7623-evb.dts
  16. @@ -341,6 +341,17 @@
  17. output-low;
  18. };
  19. };
  20. +
  21. + pwm_pins: pwm {
  22. + pins_pwm1 {
  23. + pinmux = <MT7623_PIN_204_PWM1_FUNC_PWM1>;
  24. + };
  25. +
  26. + pins_pwm2 {
  27. + pinmux = <MT7623_PIN_205_PWM2_FUNC_PWM2>;
  28. + };
  29. + };
  30. +
  31. };
  32. &nandc {
  33. @@ -419,3 +430,9 @@
  34. mediatek,reset-pin = <&pio 15 0>;
  35. status = "okay";
  36. };
  37. +
  38. +&pwm {
  39. + pinctrl-names = "default";
  40. + pinctrl-0 = <&pwm_pins>;
  41. + status = "okay";
  42. +};
  43. --- a/arch/arm/boot/dts/mt7623.dtsi
  44. +++ b/arch/arm/boot/dts/mt7623.dtsi
  45. @@ -324,6 +324,28 @@
  46. status = "disabled";
  47. };
  48. + pwm: pwm@11006000 {
  49. + compatible = "mediatek,mt7623-pwm";
  50. +
  51. + reg = <0 0x11006000 0 0x1000>;
  52. +
  53. + resets = <&pericfg MT2701_PERI_PWM_SW_RST>;
  54. + reset-names = "pwm";
  55. +
  56. + #pwm-cells = <2>;
  57. + clocks = <&topckgen CLK_TOP_PWM_SEL>,
  58. + <&pericfg CLK_PERI_PWM>,
  59. + <&pericfg CLK_PERI_PWM1>,
  60. + <&pericfg CLK_PERI_PWM2>,
  61. + <&pericfg CLK_PERI_PWM3>,
  62. + <&pericfg CLK_PERI_PWM4>,
  63. + <&pericfg CLK_PERI_PWM5>;
  64. + clock-names = "top", "main", "pwm1", "pwm2",
  65. + "pwm3", "pwm4", "pwm5";
  66. +
  67. + status = "disabled";
  68. + };
  69. +
  70. spi: spi@1100a000 {
  71. compatible = "mediatek,mt7623-spi", "mediatek,mt6589-spi";
  72. reg = <0 0x1100a000 0 0x1000>;
  73. --- a/drivers/pwm/Kconfig
  74. +++ b/drivers/pwm/Kconfig
  75. @@ -260,6 +260,15 @@ config PWM_MTK_DISP
  76. To compile this driver as a module, choose M here: the module
  77. will be called pwm-mtk-disp.
  78. +config PWM_MEDIATEK
  79. + tristate "MediaTek PWM support"
  80. + depends on ARCH_MEDIATEK || COMPILE_TEST
  81. + help
  82. + Generic PWM framework driver for Mediatek ARM SoC.
  83. +
  84. + To compile this driver as a module, choose M here: the module
  85. + will be called pwm-mxs.
  86. +
  87. config PWM_MXS
  88. tristate "Freescale MXS PWM support"
  89. depends on ARCH_MXS && OF
  90. --- a/drivers/pwm/Makefile
  91. +++ b/drivers/pwm/Makefile
  92. @@ -22,6 +22,7 @@ obj-$(CONFIG_PWM_LPC32XX) += pwm-lpc32xx
  93. obj-$(CONFIG_PWM_LPSS) += pwm-lpss.o
  94. obj-$(CONFIG_PWM_LPSS_PCI) += pwm-lpss-pci.o
  95. obj-$(CONFIG_PWM_LPSS_PLATFORM) += pwm-lpss-platform.o
  96. +obj-$(CONFIG_PWM_MEDIATEK) += pwm-mediatek.o
  97. obj-$(CONFIG_PWM_MTK_DISP) += pwm-mtk-disp.o
  98. obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
  99. obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o
  100. --- /dev/null
  101. +++ b/drivers/pwm/pwm-mediatek.c
  102. @@ -0,0 +1,230 @@
  103. +/*
  104. + * Mediatek Pulse Width Modulator driver
  105. + *
  106. + * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
  107. + *
  108. + * This file is licensed under the terms of the GNU General Public
  109. + * License version 2. This program is licensed "as is" without any
  110. + * warranty of any kind, whether express or implied.
  111. + */
  112. +
  113. +#include <linux/err.h>
  114. +#include <linux/io.h>
  115. +#include <linux/ioport.h>
  116. +#include <linux/kernel.h>
  117. +#include <linux/module.h>
  118. +#include <linux/clk.h>
  119. +#include <linux/of.h>
  120. +#include <linux/platform_device.h>
  121. +#include <linux/pwm.h>
  122. +#include <linux/slab.h>
  123. +#include <linux/types.h>
  124. +
  125. +#define NUM_PWM 5
  126. +
  127. +/* PWM registers and bits definitions */
  128. +#define PWMCON 0x00
  129. +#define PWMHDUR 0x04
  130. +#define PWMLDUR 0x08
  131. +#define PWMGDUR 0x0c
  132. +#define PWMWAVENUM 0x28
  133. +#define PWMDWIDTH 0x2c
  134. +#define PWMTHRES 0x30
  135. +
  136. +/**
  137. + * struct mtk_pwm_chip - struct representing pwm chip
  138. + *
  139. + * @mmio_base: base address of pwm chip
  140. + * @chip: linux pwm chip representation
  141. + */
  142. +struct mtk_pwm_chip {
  143. + void __iomem *mmio_base;
  144. + struct pwm_chip chip;
  145. + struct clk *clk_top;
  146. + struct clk *clk_main;
  147. + struct clk *clk_pwm[NUM_PWM];
  148. +};
  149. +
  150. +static inline struct mtk_pwm_chip *to_mtk_pwm_chip(struct pwm_chip *chip)
  151. +{
  152. + return container_of(chip, struct mtk_pwm_chip, chip);
  153. +}
  154. +
  155. +static inline u32 mtk_pwm_readl(struct mtk_pwm_chip *chip, unsigned int num,
  156. + unsigned long offset)
  157. +{
  158. + return ioread32(chip->mmio_base + 0x10 + (num * 0x40) + offset);
  159. +}
  160. +
  161. +static inline void mtk_pwm_writel(struct mtk_pwm_chip *chip,
  162. + unsigned int num, unsigned long offset,
  163. + unsigned long val)
  164. +{
  165. + iowrite32(val, chip->mmio_base + 0x10 + (num * 0x40) + offset);
  166. +}
  167. +
  168. +static int mtk_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  169. + int duty_ns, int period_ns)
  170. +{
  171. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  172. + u32 resolution = 100 / 4;
  173. + u32 clkdiv = 0;
  174. +
  175. + resolution = 1000000000 / (clk_get_rate(pc->clk_pwm[pwm->hwpwm]));
  176. +
  177. + while (period_ns / resolution > 8191) {
  178. + clkdiv++;
  179. + resolution *= 2;
  180. + }
  181. +
  182. + if (clkdiv > 7)
  183. + return -1;
  184. +
  185. + mtk_pwm_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | BIT(3) | clkdiv);
  186. + mtk_pwm_writel(pc, pwm->hwpwm, PWMDWIDTH, period_ns / resolution);
  187. + mtk_pwm_writel(pc, pwm->hwpwm, PWMTHRES, duty_ns / resolution);
  188. + return 0;
  189. +}
  190. +
  191. +static int mtk_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  192. +{
  193. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  194. + u32 val;
  195. + int ret;
  196. +
  197. + ret = clk_prepare(pc->clk_pwm[pwm->hwpwm]);
  198. + if (ret < 0)
  199. + return ret;
  200. +
  201. + val = ioread32(pc->mmio_base);
  202. + val |= BIT(pwm->hwpwm);
  203. + iowrite32(val, pc->mmio_base);
  204. +
  205. + return 0;
  206. +}
  207. +
  208. +static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  209. +{
  210. + struct mtk_pwm_chip *pc = to_mtk_pwm_chip(chip);
  211. + u32 val;
  212. +
  213. + val = ioread32(pc->mmio_base);
  214. + val &= ~BIT(pwm->hwpwm);
  215. + iowrite32(val, pc->mmio_base);
  216. + clk_unprepare(pc->clk_pwm[pwm->hwpwm]);
  217. +}
  218. +
  219. +static const struct pwm_ops mtk_pwm_ops = {
  220. + .config = mtk_pwm_config,
  221. + .enable = mtk_pwm_enable,
  222. + .disable = mtk_pwm_disable,
  223. + .owner = THIS_MODULE,
  224. +};
  225. +
  226. +static int mtk_pwm_probe(struct platform_device *pdev)
  227. +{
  228. + struct mtk_pwm_chip *pc;
  229. + struct resource *r;
  230. + int ret;
  231. +
  232. + pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  233. + if (!pc)
  234. + return -ENOMEM;
  235. +
  236. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. + pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  238. + if (IS_ERR(pc->mmio_base))
  239. + return PTR_ERR(pc->mmio_base);
  240. +
  241. + pc->clk_main = devm_clk_get(&pdev->dev, "main");
  242. + if (IS_ERR(pc->clk_main))
  243. + return PTR_ERR(pc->clk_main);
  244. +
  245. + pc->clk_top = devm_clk_get(&pdev->dev, "top");
  246. + if (IS_ERR(pc->clk_top))
  247. + return PTR_ERR(pc->clk_top);
  248. +
  249. + pc->clk_pwm[0] = devm_clk_get(&pdev->dev, "pwm1");
  250. + if (IS_ERR(pc->clk_pwm[0]))
  251. + return PTR_ERR(pc->clk_pwm[0]);
  252. +
  253. + pc->clk_pwm[1] = devm_clk_get(&pdev->dev, "pwm2");
  254. + if (IS_ERR(pc->clk_pwm[1]))
  255. + return PTR_ERR(pc->clk_pwm[1]);
  256. +
  257. + pc->clk_pwm[2] = devm_clk_get(&pdev->dev, "pwm3");
  258. + if (IS_ERR(pc->clk_pwm[2]))
  259. + return PTR_ERR(pc->clk_pwm[2]);
  260. +
  261. + pc->clk_pwm[3] = devm_clk_get(&pdev->dev, "pwm4");
  262. + if (IS_ERR(pc->clk_pwm[3]))
  263. + return PTR_ERR(pc->clk_pwm[3]);
  264. +
  265. + pc->clk_pwm[4] = devm_clk_get(&pdev->dev, "pwm5");
  266. + if (IS_ERR(pc->clk_pwm[4]))
  267. + return PTR_ERR(pc->clk_pwm[4]);
  268. +
  269. + ret = clk_prepare(pc->clk_top);
  270. + if (ret < 0)
  271. + return ret;
  272. +
  273. + ret = clk_prepare(pc->clk_main);
  274. + if (ret < 0)
  275. + goto disable_clk_top;
  276. +
  277. + platform_set_drvdata(pdev, pc);
  278. +
  279. + pc->chip.dev = &pdev->dev;
  280. + pc->chip.ops = &mtk_pwm_ops;
  281. + pc->chip.base = -1;
  282. + pc->chip.npwm = NUM_PWM;
  283. +
  284. + ret = pwmchip_add(&pc->chip);
  285. + if (ret < 0) {
  286. + dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  287. + goto disable_clk_main;
  288. + }
  289. +
  290. + return 0;
  291. +
  292. +disable_clk_main:
  293. + clk_unprepare(pc->clk_main);
  294. +disable_clk_top:
  295. + clk_unprepare(pc->clk_top);
  296. +
  297. + return ret;
  298. +}
  299. +
  300. +static int mtk_pwm_remove(struct platform_device *pdev)
  301. +{
  302. + struct mtk_pwm_chip *pc = platform_get_drvdata(pdev);
  303. + int i;
  304. +
  305. + for (i = 0; i < NUM_PWM; i++)
  306. + pwm_disable(&pc->chip.pwms[i]);
  307. +
  308. + return pwmchip_remove(&pc->chip);
  309. +}
  310. +
  311. +static const struct of_device_id mtk_pwm_of_match[] = {
  312. + { .compatible = "mediatek,mt7623-pwm" },
  313. + { }
  314. +};
  315. +
  316. +MODULE_DEVICE_TABLE(of, mtk_pwm_of_match);
  317. +
  318. +static struct platform_driver mtk_pwm_driver = {
  319. + .driver = {
  320. + .name = "mtk-pwm",
  321. + .owner = THIS_MODULE,
  322. + .of_match_table = mtk_pwm_of_match,
  323. + },
  324. + .probe = mtk_pwm_probe,
  325. + .remove = mtk_pwm_remove,
  326. +};
  327. +
  328. +module_platform_driver(mtk_pwm_driver);
  329. +
  330. +MODULE_LICENSE("GPL");
  331. +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  332. +MODULE_ALIAS("platform:mtk-pwm");