440-add-h6-pwm.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  2. index 581d23287333..487899d4cc3f 100644
  3. --- a/drivers/pwm/pwm-sun4i.c
  4. +++ b/drivers/pwm/pwm-sun4i.c
  5. @@ -16,6 +16,7 @@
  6. #include <linux/of_device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/pwm.h>
  9. +#include <linux/reset.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/time.h>
  13. @@ -78,6 +79,7 @@ struct sun4i_pwm_data {
  14. struct sun4i_pwm_chip {
  15. struct pwm_chip chip;
  16. struct clk *clk;
  17. + struct reset_control *rst;
  18. void __iomem *base;
  19. spinlock_t ctrl_lock;
  20. const struct sun4i_pwm_data *data;
  21. @@ -364,6 +366,22 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  22. if (IS_ERR(pwm->clk))
  23. return PTR_ERR(pwm->clk);
  24. + pwm->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  25. + if (IS_ERR(pwm->rst)) {
  26. + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
  27. + dev_err(&pdev->dev, "get reset failed %pe\n",
  28. + pwm->rst);
  29. + return PTR_ERR(pwm->rst);
  30. + }
  31. +
  32. + /* Deassert reset */
  33. + ret = reset_control_deassert(pwm->rst);
  34. + if (ret) {
  35. + dev_err(&pdev->dev, "cannot deassert reset control: %pe\n",
  36. + ERR_PTR(ret));
  37. + return ret;
  38. + }
  39. +
  40. pwm->chip.dev = &pdev->dev;
  41. pwm->chip.ops = &sun4i_pwm_ops;
  42. pwm->chip.base = -1;
  43. @@ -376,19 +394,31 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  44. ret = pwmchip_add(&pwm->chip);
  45. if (ret < 0) {
  46. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  47. - return ret;
  48. + goto err_pwm_add;
  49. }
  50. platform_set_drvdata(pdev, pwm);
  51. return 0;
  52. +
  53. +err_pwm_add:
  54. + reset_control_assert(pwm->rst);
  55. +
  56. + return ret;
  57. }
  58. static int sun4i_pwm_remove(struct platform_device *pdev)
  59. {
  60. struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
  61. + int ret;
  62. +
  63. + ret = pwmchip_remove(&pwm->chip);
  64. + if (ret)
  65. + return ret;
  66. +
  67. + reset_control_assert(pwm->rst);
  68. - return pwmchip_remove(&pwm->chip);
  69. + return 0;
  70. }
  71. static struct platform_driver sun4i_pwm_driver = {
  72. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  73. index 487899d4cc3f..80026167044b 100644
  74. --- a/drivers/pwm/pwm-sun4i.c
  75. +++ b/drivers/pwm/pwm-sun4i.c
  76. @@ -362,9 +362,34 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  77. if (IS_ERR(pwm->base))
  78. return PTR_ERR(pwm->base);
  79. - pwm->clk = devm_clk_get(&pdev->dev, NULL);
  80. - if (IS_ERR(pwm->clk))
  81. + /*
  82. + * All hardware variants need a source clock that is divided and
  83. + * then feeds the counter that defines the output wave form. In the
  84. + * device tree this clock is either unnamed or called "mod".
  85. + * Some variants (e.g. H6) need another clock to access the
  86. + * hardware registers; this is called "bus".
  87. + * So we request "mod" first (and ignore the corner case that a
  88. + * parent provides a "mod" clock while the right one would be the
  89. + * unnamed one of the PWM device) and if this is not found we fall
  90. + * back to the first clock of the PWM.
  91. + */
  92. + pwm->clk = devm_clk_get_optional(&pdev->dev, "mod");
  93. + if (IS_ERR(pwm->clk)) {
  94. + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
  95. + dev_err(&pdev->dev, "get mod clock failed %pe\n",
  96. + pwm->clk);
  97. return PTR_ERR(pwm->clk);
  98. + }
  99. +
  100. + if (!pwm->clk) {
  101. + pwm->clk = devm_clk_get(&pdev->dev, NULL);
  102. + if (IS_ERR(pwm->clk)) {
  103. + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
  104. + dev_err(&pdev->dev, "get unnamed clock failed %pe\n",
  105. + pwm->clk);
  106. + return PTR_ERR(pwm->clk);
  107. + }
  108. + }
  109. pwm->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  110. if (IS_ERR(pwm->rst)) {
  111. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  112. index 80026167044b..a6727dd89e28 100644
  113. --- a/drivers/pwm/pwm-sun4i.c
  114. +++ b/drivers/pwm/pwm-sun4i.c
  115. @@ -78,6 +78,7 @@ struct sun4i_pwm_data {
  116. struct sun4i_pwm_chip {
  117. struct pwm_chip chip;
  118. + struct clk *bus_clk;
  119. struct clk *clk;
  120. struct reset_control *rst;
  121. void __iomem *base;
  122. @@ -391,6 +392,14 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  123. }
  124. }
  125. + pwm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
  126. + if (IS_ERR(pwm->bus_clk)) {
  127. + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
  128. + dev_err(&pdev->dev, "get bus clock failed %pe\n",
  129. + pwm->bus_clk);
  130. + return PTR_ERR(pwm->bus_clk);
  131. + }
  132. +
  133. pwm->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  134. if (IS_ERR(pwm->rst)) {
  135. if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
  136. @@ -407,6 +416,17 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  137. return ret;
  138. }
  139. + /*
  140. + * We're keeping the bus clock on for the sake of simplicity.
  141. + * Actually it only needs to be on for hardware register accesses.
  142. + */
  143. + ret = clk_prepare_enable(pwm->bus_clk);
  144. + if (ret) {
  145. + dev_err(&pdev->dev, "cannot prepare and enable bus_clk %pe\n",
  146. + ERR_PTR(ret));
  147. + goto err_bus;
  148. + }
  149. +
  150. pwm->chip.dev = &pdev->dev;
  151. pwm->chip.ops = &sun4i_pwm_ops;
  152. pwm->chip.base = -1;
  153. @@ -427,6 +447,8 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
  154. return 0;
  155. err_pwm_add:
  156. + clk_disable_unprepare(pwm->bus_clk);
  157. +err_bus:
  158. reset_control_assert(pwm->rst);
  159. return ret;
  160. @@ -441,6 +463,7 @@ static int sun4i_pwm_remove(struct platform_device *pdev)
  161. if (ret)
  162. return ret;
  163. + clk_disable_unprepare(pwm->bus_clk);
  164. reset_control_assert(pwm->rst);
  165. return 0;
  166. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  167. index a6727dd89e28..e369b5a398f4 100644
  168. --- a/drivers/pwm/pwm-sun4i.c
  169. +++ b/drivers/pwm/pwm-sun4i.c
  170. @@ -202,9 +202,9 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  171. {
  172. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  173. struct pwm_state cstate;
  174. - u32 ctrl;
  175. + u32 ctrl, duty, period, val;
  176. int ret;
  177. - unsigned int delay_us;
  178. + unsigned int delay_us, prescaler;
  179. unsigned long now;
  180. pwm_get_state(pwm, &cstate);
  181. @@ -220,43 +220,37 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  182. spin_lock(&sun4i_pwm->ctrl_lock);
  183. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  184. - if ((cstate.period != state->period) ||
  185. - (cstate.duty_cycle != state->duty_cycle)) {
  186. - u32 period, duty, val;
  187. - unsigned int prescaler;
  188. -
  189. - ret = sun4i_pwm_calculate(sun4i_pwm, state,
  190. - &duty, &period, &prescaler);
  191. - if (ret) {
  192. - dev_err(chip->dev, "period exceeds the maximum value\n");
  193. - spin_unlock(&sun4i_pwm->ctrl_lock);
  194. - if (!cstate.enabled)
  195. - clk_disable_unprepare(sun4i_pwm->clk);
  196. - return ret;
  197. - }
  198. -
  199. - if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  200. - /* Prescaler changed, the clock has to be gated */
  201. - ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  202. - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  203. + ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler);
  204. + if (ret) {
  205. + dev_err(chip->dev, "period exceeds the maximum value\n");
  206. + spin_unlock(&sun4i_pwm->ctrl_lock);
  207. + if (!cstate.enabled)
  208. + clk_disable_unprepare(sun4i_pwm->clk);
  209. + return ret;
  210. + }
  211. - ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  212. - ctrl |= BIT_CH(prescaler, pwm->hwpwm);
  213. - }
  214. + if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  215. + /* Prescaler changed, the clock has to be gated */
  216. + ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  217. + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  218. - val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
  219. - sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  220. - sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
  221. - usecs_to_jiffies(cstate.period / 1000 + 1);
  222. - sun4i_pwm->needs_delay[pwm->hwpwm] = true;
  223. + ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  224. + ctrl |= BIT_CH(prescaler, pwm->hwpwm);
  225. }
  226. + val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
  227. + sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  228. + sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
  229. + usecs_to_jiffies(cstate.period / 1000 + 1);
  230. + sun4i_pwm->needs_delay[pwm->hwpwm] = true;
  231. +
  232. if (state->polarity != PWM_POLARITY_NORMAL)
  233. ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  234. else
  235. ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  236. ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  237. +
  238. if (state->enabled) {
  239. ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
  240. } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
  241. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  242. index e369b5a398f4..07bf7be6074b 100644
  243. --- a/drivers/pwm/pwm-sun4i.c
  244. +++ b/drivers/pwm/pwm-sun4i.c
  245. @@ -3,6 +3,10 @@
  246. * Driver for Allwinner sun4i Pulse Width Modulation Controller
  247. *
  248. * Copyright (C) 2014 Alexandre Belloni <[email protected]>
  249. + *
  250. + * Limitations:
  251. + * - When outputing the source clock directly, the PWM logic will be bypassed
  252. + * and the currently running period is not guaranteed to be completed
  253. */
  254. #include <linux/bitops.h>
  255. @@ -73,6 +77,7 @@ static const u32 prescaler_table[] = {
  256. struct sun4i_pwm_data {
  257. bool has_prescaler_bypass;
  258. + bool has_direct_mod_clk_output;
  259. unsigned int npwm;
  260. };
  261. @@ -118,6 +123,20 @@ static void sun4i_pwm_get_state(struct pwm_chip *chip,
  262. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  263. + /*
  264. + * PWM chapter in H6 manual has a diagram which explains that if bypass
  265. + * bit is set, no other setting has any meaning. Even more, experiment
  266. + * proved that also enable bit is ignored in this case.
  267. + */
  268. + if ((val & BIT_CH(PWM_BYPASS, pwm->hwpwm)) &&
  269. + sun4i_pwm->data->has_direct_mod_clk_output) {
  270. + state->period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, clk_rate);
  271. + state->duty_cycle = DIV_ROUND_UP_ULL(state->period, 2);
  272. + state->polarity = PWM_POLARITY_NORMAL;
  273. + state->enabled = true;
  274. + return;
  275. + }
  276. +
  277. if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) &&
  278. sun4i_pwm->data->has_prescaler_bypass)
  279. prescaler = 1;
  280. @@ -149,13 +168,24 @@ static void sun4i_pwm_get_state(struct pwm_chip *chip,
  281. static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
  282. const struct pwm_state *state,
  283. - u32 *dty, u32 *prd, unsigned int *prsclr)
  284. + u32 *dty, u32 *prd, unsigned int *prsclr,
  285. + bool *bypass)
  286. {
  287. u64 clk_rate, div = 0;
  288. unsigned int pval, prescaler = 0;
  289. clk_rate = clk_get_rate(sun4i_pwm->clk);
  290. + *bypass = sun4i_pwm->data->has_direct_mod_clk_output &&
  291. + state->enabled &&
  292. + (state->period * clk_rate >= NSEC_PER_SEC) &&
  293. + (state->period * clk_rate < 2 * NSEC_PER_SEC) &&
  294. + (state->duty_cycle * clk_rate * 2 >= NSEC_PER_SEC);
  295. +
  296. + /* Skip calculation of other parameters if we bypass them */
  297. + if (*bypass)
  298. + return 0;
  299. +
  300. if (sun4i_pwm->data->has_prescaler_bypass) {
  301. /* First, test without any prescaler when available */
  302. prescaler = PWM_PRESCAL_MASK;
  303. @@ -206,6 +236,7 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  304. int ret;
  305. unsigned int delay_us, prescaler;
  306. unsigned long now;
  307. + bool bypass;
  308. pwm_get_state(pwm, &cstate);
  309. @@ -220,7 +251,8 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  310. spin_lock(&sun4i_pwm->ctrl_lock);
  311. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  312. - ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler);
  313. + ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
  314. + &bypass);
  315. if (ret) {
  316. dev_err(chip->dev, "period exceeds the maximum value\n");
  317. spin_unlock(&sun4i_pwm->ctrl_lock);
  318. @@ -229,6 +261,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  319. return ret;
  320. }
  321. + if (sun4i_pwm->data->has_direct_mod_clk_output) {
  322. + if (bypass) {
  323. + ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
  324. + /* We can skip other parameter */
  325. + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  326. + spin_unlock(&sun4i_pwm->ctrl_lock);
  327. + return 0;
  328. + } else {
  329. + ctrl &= ~BIT_CH(PWM_BYPASS, pwm->hwpwm);
  330. + }
  331. + }
  332. +
  333. if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  334. /* Prescaler changed, the clock has to be gated */
  335. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  336. diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
  337. index 07bf7be6074b..c394878c7e5d 100644
  338. --- a/drivers/pwm/pwm-sun4i.c
  339. +++ b/drivers/pwm/pwm-sun4i.c
  340. @@ -360,6 +360,12 @@ static const struct sun4i_pwm_data sun4i_pwm_single_bypass = {
  341. .npwm = 1,
  342. };
  343. +static const struct sun4i_pwm_data sun50i_h6_pwm_data = {
  344. + .has_prescaler_bypass = true,
  345. + .has_direct_mod_clk_output = true,
  346. + .npwm = 2,
  347. +};
  348. +
  349. static const struct of_device_id sun4i_pwm_dt_ids[] = {
  350. {
  351. .compatible = "allwinner,sun4i-a10-pwm",
  352. @@ -376,6 +382,9 @@ static const struct of_device_id sun4i_pwm_dt_ids[] = {
  353. }, {
  354. .compatible = "allwinner,sun8i-h3-pwm",
  355. .data = &sun4i_pwm_single_bypass,
  356. + }, {
  357. + .compatible = "allwinner,sun50i-h6-pwm",
  358. + .data = &sun50i_h6_pwm_data,
  359. }, {
  360. /* sentinel */
  361. },