150-pwm-add-sunxi-driver.patch 10 KB

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