950-0884-pwm-Add-support-for-RP1-PWM.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. From 824f18efc8ad59e2783570ae2df83e2cd16b9f04 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <[email protected]>
  3. Date: Tue, 14 Feb 2023 14:03:54 +0000
  4. Subject: [PATCH] pwm: Add support for RP1 PWM
  5. Add a driver for the RP1 PWM block.
  6. Signed-off-by: Phil Elwell <[email protected]>
  7. ---
  8. .../devicetree/bindings/pwm/pwm-rp1.yaml | 38 ++++
  9. drivers/pwm/Kconfig | 9 +
  10. drivers/pwm/Makefile | 1 +
  11. drivers/pwm/pwm-rp1.c | 203 ++++++++++++++++++
  12. 4 files changed, 251 insertions(+)
  13. create mode 100644 Documentation/devicetree/bindings/pwm/pwm-rp1.yaml
  14. create mode 100644 drivers/pwm/pwm-rp1.c
  15. --- /dev/null
  16. +++ b/Documentation/devicetree/bindings/pwm/pwm-rp1.yaml
  17. @@ -0,0 +1,38 @@
  18. +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  19. +%YAML 1.2
  20. +---
  21. +$id: http://devicetree.org/schemas/pwm/pwm-rp1.yaml#
  22. +$schema: http://devicetree.org/meta-schemas/core.yaml#
  23. +
  24. +title: Raspberry Pi RP1 PWM controller
  25. +
  26. +maintainers:
  27. + - Naushir Patuck <[email protected]>
  28. +
  29. +properties:
  30. + compatible:
  31. + enum:
  32. + - raspberrypi,rp1-pwm
  33. +
  34. + reg:
  35. + maxItems: 1
  36. +
  37. + "#pwm-cells":
  38. + const: 3
  39. +
  40. +required:
  41. + - compatible
  42. + - reg
  43. + - clocks
  44. + - "#pwm-cells"
  45. +
  46. +additionalProperties: false
  47. +
  48. +examples:
  49. + - |
  50. + pwm0: pwm@98000 {
  51. + compatible = "raspberrypi,rp1-pwm";
  52. + reg = <0x0 0x98000 0x0 0x100>;
  53. + clocks = <&rp1_sys>;
  54. + #pwm-cells = <3>;
  55. + };
  56. --- a/drivers/pwm/Kconfig
  57. +++ b/drivers/pwm/Kconfig
  58. @@ -451,6 +451,15 @@ config PWM_RASPBERRYPI_POE
  59. Enable Raspberry Pi firmware controller PWM bus used to control the
  60. official RPI PoE hat
  61. +config PWM_RP1
  62. + tristate "RP1 PWM support"
  63. + depends on ARCH_BCM2835 || COMPILE_TEST
  64. + help
  65. + PWM framework driver for Raspberry Pi RP1 controller
  66. +
  67. + To compile this driver as a module, choose M here: the module
  68. + will be called pwm-rp1.
  69. +
  70. config PWM_RCAR
  71. tristate "Renesas R-Car PWM support"
  72. depends on ARCH_RENESAS || COMPILE_TEST
  73. --- a/drivers/pwm/Makefile
  74. +++ b/drivers/pwm/Makefile
  75. @@ -41,6 +41,7 @@ obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-om
  76. obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o
  77. obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
  78. obj-$(CONFIG_PWM_RASPBERRYPI_POE) += pwm-raspberrypi-poe.o
  79. +obj-$(CONFIG_PWM_RP1) += pwm-rp1.o
  80. obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
  81. obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
  82. obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
  83. --- /dev/null
  84. +++ b/drivers/pwm/pwm-rp1.c
  85. @@ -0,0 +1,203 @@
  86. +// SPDX-License-Identifier: GPL-2.0
  87. +/*
  88. + * pwm-rp1.c
  89. + *
  90. + * Raspberry Pi RP1 PWM.
  91. + *
  92. + * Copyright © 2023 Raspberry Pi Ltd.
  93. + *
  94. + * Author: Naushir Patuck ([email protected])
  95. + *
  96. + * Based on the pwm-bcm2835 driver by:
  97. + * Bart Tanghe <[email protected]>
  98. + */
  99. +
  100. +#include <linux/bitops.h>
  101. +#include <linux/clk.h>
  102. +#include <linux/err.h>
  103. +#include <linux/io.h>
  104. +#include <linux/module.h>
  105. +#include <linux/of.h>
  106. +#include <linux/platform_device.h>
  107. +#include <linux/pwm.h>
  108. +
  109. +#define PWM_GLOBAL_CTRL 0x000
  110. +#define PWM_CHANNEL_CTRL(x) (0x014 + ((x) * 16))
  111. +#define PWM_RANGE(x) (0x018 + ((x) * 16))
  112. +#define PWM_DUTY(x) (0x020 + ((x) * 16))
  113. +
  114. +/* 8:FIFO_POP_MASK + 0:Trailing edge M/S modulation */
  115. +#define PWM_CHANNEL_DEFAULT (BIT(8) + BIT(0))
  116. +#define PWM_CHANNEL_ENABLE(x) BIT(x)
  117. +#define PWM_POLARITY BIT(3)
  118. +#define SET_UPDATE BIT(31)
  119. +#define PWM_MODE_MASK GENMASK(1, 0)
  120. +
  121. +struct rp1_pwm {
  122. + struct pwm_chip chip;
  123. + struct device *dev;
  124. + void __iomem *base;
  125. + struct clk *clk;
  126. +};
  127. +
  128. +static inline struct rp1_pwm *to_rp1_pwm(struct pwm_chip *chip)
  129. +{
  130. + return container_of(chip, struct rp1_pwm, chip);
  131. +}
  132. +
  133. +static void rp1_pwm_apply_config(struct pwm_chip *chip, struct pwm_device *pwm)
  134. +{
  135. + struct rp1_pwm *pc = to_rp1_pwm(chip);
  136. + u32 value;
  137. +
  138. + value = readl(pc->base + PWM_GLOBAL_CTRL);
  139. + value |= SET_UPDATE;
  140. + writel(value, pc->base + PWM_GLOBAL_CTRL);
  141. +}
  142. +
  143. +static int rp1_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  144. +{
  145. + struct rp1_pwm *pc = to_rp1_pwm(chip);
  146. +
  147. + writel(PWM_CHANNEL_DEFAULT, pc->base + PWM_CHANNEL_CTRL(pwm->hwpwm));
  148. + return 0;
  149. +}
  150. +
  151. +static void rp1_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  152. +{
  153. + struct rp1_pwm *pc = to_rp1_pwm(chip);
  154. + u32 value;
  155. +
  156. + value = readl(pc->base + PWM_CHANNEL_CTRL(pwm->hwpwm));
  157. + value &= ~PWM_MODE_MASK;
  158. + writel(value, pc->base + PWM_CHANNEL_CTRL(pwm->hwpwm));
  159. + rp1_pwm_apply_config(chip, pwm);
  160. +}
  161. +
  162. +static int rp1_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  163. + const struct pwm_state *state)
  164. +{
  165. + struct rp1_pwm *pc = to_rp1_pwm(chip);
  166. + unsigned long clk_rate = clk_get_rate(pc->clk);
  167. + unsigned long clk_period;
  168. + u32 value;
  169. +
  170. + if (!clk_rate) {
  171. + dev_err(pc->dev, "failed to get clock rate\n");
  172. + return -EINVAL;
  173. + }
  174. +
  175. + /* set period */
  176. + clk_period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, clk_rate);
  177. +
  178. + writel(DIV_ROUND_CLOSEST(state->duty_cycle, clk_period),
  179. + pc->base + PWM_DUTY(pwm->hwpwm));
  180. +
  181. + /* set duty cycle */
  182. + writel(DIV_ROUND_CLOSEST(state->period, clk_period),
  183. + pc->base + PWM_RANGE(pwm->hwpwm));
  184. +
  185. + /* set polarity */
  186. + value = readl(pc->base + PWM_CHANNEL_CTRL(pwm->hwpwm));
  187. + if (state->polarity == PWM_POLARITY_NORMAL)
  188. + value &= ~PWM_POLARITY;
  189. + else
  190. + value |= PWM_POLARITY;
  191. + writel(value, pc->base + PWM_CHANNEL_CTRL(pwm->hwpwm));
  192. +
  193. + /* enable/disable */
  194. + value = readl(pc->base + PWM_GLOBAL_CTRL);
  195. + if (state->enabled)
  196. + value |= PWM_CHANNEL_ENABLE(pwm->hwpwm);
  197. + else
  198. + value &= ~PWM_CHANNEL_ENABLE(pwm->hwpwm);
  199. + writel(value, pc->base + PWM_GLOBAL_CTRL);
  200. +
  201. + rp1_pwm_apply_config(chip, pwm);
  202. +
  203. + return 0;
  204. +}
  205. +
  206. +static const struct pwm_ops rp1_pwm_ops = {
  207. + .request = rp1_pwm_request,
  208. + .free = rp1_pwm_free,
  209. + .apply = rp1_pwm_apply,
  210. + .owner = THIS_MODULE,
  211. +};
  212. +
  213. +static int rp1_pwm_probe(struct platform_device *pdev)
  214. +{
  215. + struct rp1_pwm *pc;
  216. + struct resource *res;
  217. + int ret;
  218. +
  219. + pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  220. + if (!pc)
  221. + return -ENOMEM;
  222. +
  223. + pc->dev = &pdev->dev;
  224. +
  225. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  226. + pc->base = devm_ioremap_resource(&pdev->dev, res);
  227. + if (IS_ERR(pc->base))
  228. + return PTR_ERR(pc->base);
  229. +
  230. + pc->clk = devm_clk_get(&pdev->dev, NULL);
  231. + if (IS_ERR(pc->clk))
  232. + return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
  233. + "clock not found\n");
  234. +
  235. + ret = clk_prepare_enable(pc->clk);
  236. + if (ret)
  237. + return ret;
  238. +
  239. + pc->chip.dev = &pdev->dev;
  240. + pc->chip.ops = &rp1_pwm_ops;
  241. + pc->chip.base = -1;
  242. + pc->chip.npwm = 4;
  243. + pc->chip.of_xlate = of_pwm_xlate_with_flags;
  244. + pc->chip.of_pwm_n_cells = 3;
  245. +
  246. + platform_set_drvdata(pdev, pc);
  247. +
  248. + ret = pwmchip_add(&pc->chip);
  249. + if (ret < 0)
  250. + goto add_fail;
  251. +
  252. + return 0;
  253. +
  254. +add_fail:
  255. + clk_disable_unprepare(pc->clk);
  256. + return ret;
  257. +}
  258. +
  259. +static int rp1_pwm_remove(struct platform_device *pdev)
  260. +{
  261. + struct rp1_pwm *pc = platform_get_drvdata(pdev);
  262. +
  263. + clk_disable_unprepare(pc->clk);
  264. +
  265. + pwmchip_remove(&pc->chip);
  266. +
  267. + return 0;
  268. +}
  269. +
  270. +static const struct of_device_id rp1_pwm_of_match[] = {
  271. + { .compatible = "raspberrypi,rp1-pwm" },
  272. + { /* sentinel */ }
  273. +};
  274. +MODULE_DEVICE_TABLE(of, rp1_pwm_of_match);
  275. +
  276. +static struct platform_driver rp1_pwm_driver = {
  277. + .driver = {
  278. + .name = "rpi-pwm",
  279. + .of_match_table = rp1_pwm_of_match,
  280. + },
  281. + .probe = rp1_pwm_probe,
  282. + .remove = rp1_pwm_remove,
  283. +};
  284. +module_platform_driver(rp1_pwm_driver);
  285. +
  286. +MODULE_AUTHOR("Naushir Patuck <[email protected]");
  287. +MODULE_DESCRIPTION("RP1 PWM driver");
  288. +MODULE_LICENSE("GPL");