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

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