250-pwm-add-driver.patch 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. --- a/drivers/pwm/Kconfig
  2. +++ b/drivers/pwm/Kconfig
  3. @@ -187,6 +187,15 @@ config PWM_SPEAR
  4. To compile this driver as a module, choose M here: the module
  5. will be called pwm-spear.
  6. +config PWM_SUNXI
  7. + tristate "Allwinner PWM support"
  8. + depends on ARCH_SUNXI || COMPILE_TEST
  9. + help
  10. + Generic PWM framework driver for Allwinner SoCs.
  11. +
  12. + To compile this driver as a module, choose M here: the module
  13. + will be called pwm-sunxi.
  14. +
  15. config PWM_TEGRA
  16. tristate "NVIDIA Tegra PWM support"
  17. depends on ARCH_TEGRA
  18. --- a/drivers/pwm/Makefile
  19. +++ b/drivers/pwm/Makefile
  20. @@ -16,6 +16,7 @@ obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
  21. obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
  22. obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
  23. obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
  24. +obj-$(CONFIG_PWM_SUNXI) += pwm-sunxi.o
  25. obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
  26. obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
  27. obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o
  28. --- /dev/null
  29. +++ b/drivers/pwm/pwm-sunxi.c
  30. @@ -0,0 +1,338 @@
  31. +/*
  32. + * Driver for Allwinner Pulse Width Modulation Controller
  33. + *
  34. + * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
  35. + *
  36. + * Licensed under GPLv2.
  37. + */
  38. +
  39. +#include <linux/bitops.h>
  40. +#include <linux/clk.h>
  41. +#include <linux/err.h>
  42. +#include <linux/io.h>
  43. +#include <linux/of.h>
  44. +#include <linux/of_device.h>
  45. +#include <linux/platform_device.h>
  46. +#include <linux/pwm.h>
  47. +#include <linux/module.h>
  48. +#include <linux/mutex.h>
  49. +#include <linux/slab.h>
  50. +
  51. +#define PWM_CTRL_REG 0x0
  52. +
  53. +#define PWM_CH_PRD_BASE 0x4
  54. +#define PWM_CH_PRD_OFF 0x4
  55. +#define PWM_CH_PRD(x) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFF * (x))
  56. +
  57. +#define PWMCH_OFFSET 15
  58. +#define PWM_PRESCAL_MASK GENMASK(3, 0)
  59. +#define PWM_PRESCAL_OFF 0
  60. +#define PWM_EN BIT(4)
  61. +#define PWM_ACT_STATE BIT(5)
  62. +#define PWM_CLK_GATING BIT(6)
  63. +#define PWM_MODE BIT(7)
  64. +#define PWM_PULSE BIT(8)
  65. +#define PWM_BYPASS BIT(9)
  66. +
  67. +#define PWM_RDY_BASE 28
  68. +#define PWM_RDY_OFF 1
  69. +#define PWM_RDY(x) BIT(PWM_RDY_BASE + PWM_RDY_OFF * (x))
  70. +
  71. +#define PWM_PRD_ACT_MASK GENMASK(7, 0)
  72. +#define PWM_PRD(x) ((x - 1) << 16)
  73. +#define PWM_PRD_MASK GENMASK(7, 0)
  74. +
  75. +#define BIT_CH(bit, chan) (bit << (chan * PWMCH_OFFSET))
  76. +
  77. +u32 prescal_table[] = { 120, 180, 240, 360, 480, 0, 0, 0,
  78. + 12000, 24000, 36000, 48000, 72000,
  79. + 0, 0, 1 };
  80. +
  81. +struct sunxi_pwm_data {
  82. + bool has_rdy;
  83. +};
  84. +
  85. +struct sunxi_pwm_chip {
  86. + struct pwm_chip chip;
  87. + struct clk *clk;
  88. + void __iomem *base;
  89. + struct mutex ctrl_lock;
  90. + const struct sunxi_pwm_data *data;
  91. +};
  92. +
  93. +#define to_sunxi_pwm_chip(chip) container_of(chip, struct sunxi_pwm_chip, chip)
  94. +
  95. +static inline u32 sunxi_pwm_readl(struct sunxi_pwm_chip *chip,
  96. + unsigned long offset)
  97. +{
  98. + return readl(chip->base + offset);
  99. +}
  100. +
  101. +static inline void sunxi_pwm_writel(struct sunxi_pwm_chip *chip,
  102. + unsigned long offset, unsigned long val)
  103. +{
  104. + writel(val, chip->base + offset);
  105. +}
  106. +
  107. +static int sunxi_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  108. + int duty_ns, int period_ns)
  109. +{
  110. + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
  111. + u32 clk_rate, prd, dty;
  112. + u64 div;
  113. + u32 val, clk_gate;
  114. + int i, ret;
  115. +
  116. + clk_rate = clk_get_rate(sunxi_pwm->clk);
  117. +
  118. + /* First, test without any divider */
  119. + i = PWM_PRESCAL_MASK;
  120. + div = clk_rate * period_ns;
  121. + do_div(div, 1000000000);
  122. + if (div > PWM_PRD_MASK) {
  123. + /* Then go up from the first divider */
  124. + for (i = 0; i < PWM_PRESCAL_MASK; i++) {
  125. + if (!prescal_table[i])
  126. + continue;
  127. + div = clk_rate / prescal_table[i];
  128. + div = div * period_ns;
  129. + do_div(div, 1000000000);
  130. + if (div <= PWM_PRD_MASK)
  131. + break;
  132. + }
  133. + }
  134. +
  135. + if (div > PWM_PRD_MASK) {
  136. + dev_err(chip->dev, "prescaler exceeds the maximum value\n");
  137. + return -EINVAL;
  138. + }
  139. +
  140. + prd = div;
  141. + div *= duty_ns;
  142. + do_div(div, period_ns);
  143. + dty = div;
  144. +
  145. + ret = clk_prepare_enable(sunxi_pwm->clk);
  146. + if (ret) {
  147. + dev_err(chip->dev, "failed to enable PWM clock\n");
  148. + return ret;
  149. + }
  150. +
  151. + mutex_lock(&sunxi_pwm->ctrl_lock);
  152. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  153. +
  154. + if (sunxi_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) {
  155. + mutex_unlock(&sunxi_pwm->ctrl_lock);
  156. + clk_disable_unprepare(sunxi_pwm->clk);
  157. + return -EBUSY;
  158. + }
  159. +
  160. + clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  161. + if (clk_gate) {
  162. + val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  163. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  164. + }
  165. +
  166. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  167. + val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  168. + val |= i;
  169. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  170. +
  171. + sunxi_pwm_writel(sunxi_pwm, PWM_CH_PRD(pwm->hwpwm), dty | PWM_PRD(prd));
  172. +
  173. + if (clk_gate) {
  174. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  175. + val |= clk_gate;
  176. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  177. + }
  178. +
  179. + mutex_unlock(&sunxi_pwm->ctrl_lock);
  180. + clk_disable_unprepare(sunxi_pwm->clk);
  181. +
  182. + return 0;
  183. +}
  184. +
  185. +static int sunxi_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  186. + enum pwm_polarity polarity)
  187. +{
  188. + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
  189. + u32 val;
  190. + int ret;
  191. +
  192. + ret = clk_prepare_enable(sunxi_pwm->clk);
  193. + if (ret) {
  194. + dev_err(chip->dev, "failed to enable PWM clock\n");
  195. + return ret;
  196. + }
  197. +
  198. + mutex_lock(&sunxi_pwm->ctrl_lock);
  199. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  200. +
  201. + if (polarity != PWM_POLARITY_NORMAL)
  202. + val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  203. + else
  204. + val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  205. +
  206. +
  207. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  208. +
  209. + mutex_unlock(&sunxi_pwm->ctrl_lock);
  210. + clk_disable_unprepare(sunxi_pwm->clk);
  211. +
  212. + return 0;
  213. +}
  214. +
  215. +static int sunxi_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  216. +{
  217. + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
  218. + u32 val;
  219. + int ret;
  220. +
  221. + ret = clk_prepare_enable(sunxi_pwm->clk);
  222. + if (ret) {
  223. + dev_err(chip->dev, "failed to enable PWM clock\n");
  224. + return ret;
  225. + }
  226. +
  227. + mutex_lock(&sunxi_pwm->ctrl_lock);
  228. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  229. + val |= BIT_CH(PWM_EN, pwm->hwpwm);
  230. + val |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  231. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  232. + mutex_unlock(&sunxi_pwm->ctrl_lock);
  233. +
  234. + return 0;
  235. +}
  236. +
  237. +static void sunxi_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  238. +{
  239. + struct sunxi_pwm_chip *sunxi_pwm = to_sunxi_pwm_chip(chip);
  240. + u32 val;
  241. +
  242. + mutex_lock(&sunxi_pwm->ctrl_lock);
  243. + val = sunxi_pwm_readl(sunxi_pwm, PWM_CTRL_REG);
  244. + val &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  245. + val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  246. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG, val);
  247. + mutex_unlock(&sunxi_pwm->ctrl_lock);
  248. +
  249. + clk_disable_unprepare(sunxi_pwm->clk);
  250. +}
  251. +
  252. +static const struct pwm_ops sunxi_pwm_ops = {
  253. + .config = sunxi_pwm_config,
  254. + .set_polarity = sunxi_pwm_set_polarity,
  255. + .enable = sunxi_pwm_enable,
  256. + .disable = sunxi_pwm_disable,
  257. + .owner = THIS_MODULE,
  258. +};
  259. +
  260. +static const struct sunxi_pwm_data sunxi_pwm_data_a10 = {
  261. + .has_rdy = false,
  262. +};
  263. +
  264. +static const struct sunxi_pwm_data sunxi_pwm_data_a20 = {
  265. + .has_rdy = true,
  266. +};
  267. +
  268. +static const struct of_device_id sunxi_pwm_dt_ids[] = {
  269. + {
  270. + .compatible = "allwinner,sun4i-a10-pwm",
  271. + .data = &sunxi_pwm_data_a10,
  272. + }, {
  273. + .compatible = "allwinner,sun7i-a20-pwm",
  274. + .data = &sunxi_pwm_data_a20,
  275. + }, {
  276. + /* sentinel */
  277. + },
  278. +};
  279. +MODULE_DEVICE_TABLE(of, sunxi_pwm_dt_ids);
  280. +
  281. +static int sunxi_pwm_probe(struct platform_device *pdev)
  282. +{
  283. + struct sunxi_pwm_chip *sunxi_pwm;
  284. + struct resource *res;
  285. + int ret;
  286. +
  287. + const struct of_device_id *match;
  288. +
  289. + match = of_match_device(sunxi_pwm_dt_ids, &pdev->dev);
  290. + if (!match || !match->data)
  291. + return -ENODEV;
  292. +
  293. + sunxi_pwm = devm_kzalloc(&pdev->dev, sizeof(*sunxi_pwm), GFP_KERNEL);
  294. + if (!sunxi_pwm)
  295. + return -ENOMEM;
  296. +
  297. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  298. + sunxi_pwm->base = devm_ioremap_resource(&pdev->dev, res);
  299. + if (IS_ERR(sunxi_pwm->base))
  300. + return PTR_ERR(sunxi_pwm->base);
  301. +
  302. + sunxi_pwm->clk = devm_clk_get(&pdev->dev, NULL);
  303. + if (IS_ERR(sunxi_pwm->clk))
  304. + return PTR_ERR(sunxi_pwm->clk);
  305. +
  306. + sunxi_pwm->chip.dev = &pdev->dev;
  307. + sunxi_pwm->chip.ops = &sunxi_pwm_ops;
  308. +
  309. + sunxi_pwm->chip.base = -1;
  310. + sunxi_pwm->chip.npwm = 2;
  311. + sunxi_pwm->chip.can_sleep = true;
  312. + sunxi_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  313. + sunxi_pwm->chip.of_pwm_n_cells = 3;
  314. + sunxi_pwm->data = match->data;
  315. +
  316. + mutex_init(&sunxi_pwm->ctrl_lock);
  317. +
  318. + ret = clk_prepare_enable(sunxi_pwm->clk);
  319. + if (ret) {
  320. + dev_err(&pdev->dev, "failed to enable PWM clock\n");
  321. + goto error;
  322. + }
  323. +
  324. + /* By default, the polarity is inversed, set it to normal */
  325. + sunxi_pwm_writel(sunxi_pwm, PWM_CTRL_REG,
  326. + BIT_CH(PWM_ACT_STATE, 0) |
  327. + BIT_CH(PWM_ACT_STATE, 1));
  328. + clk_disable_unprepare(sunxi_pwm->clk);
  329. +
  330. + ret = pwmchip_add(&sunxi_pwm->chip);
  331. + if (ret < 0) {
  332. + dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
  333. + goto error;
  334. + }
  335. +
  336. + platform_set_drvdata(pdev, sunxi_pwm);
  337. +
  338. + return ret;
  339. +
  340. +error:
  341. + mutex_destroy(&sunxi_pwm->ctrl_lock);
  342. + clk_disable_unprepare(sunxi_pwm->clk);
  343. + return ret;
  344. +}
  345. +
  346. +static int sunxi_pwm_remove(struct platform_device *pdev)
  347. +{
  348. + struct sunxi_pwm_chip *sunxi_pwm = platform_get_drvdata(pdev);
  349. +
  350. + mutex_destroy(&sunxi_pwm->ctrl_lock);
  351. +
  352. + return pwmchip_remove(&sunxi_pwm->chip);
  353. +}
  354. +
  355. +static struct platform_driver sunxi_pwm_driver = {
  356. + .driver = {
  357. + .name = "sunxi-pwm",
  358. + .of_match_table = sunxi_pwm_dt_ids,
  359. + },
  360. + .probe = sunxi_pwm_probe,
  361. + .remove = sunxi_pwm_remove,
  362. +};
  363. +module_platform_driver(sunxi_pwm_driver);
  364. +
  365. +MODULE_ALIAS("platform:sunxi-pwm");
  366. +MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  367. +MODULE_DESCRIPTION("Allwinner PWM driver");
  368. +MODULE_LICENSE("GPL v2");