pwm-atmel-hlcdc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2014 Free Electrons
  3. * Copyright (C) 2014 Atmel
  4. *
  5. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/mfd/atmel-hlcdc.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pwm.h>
  25. #include <linux/regmap.h>
  26. #define ATMEL_HLCDC_PWMCVAL_MASK GENMASK(15, 8)
  27. #define ATMEL_HLCDC_PWMCVAL(x) (((x) << 8) & ATMEL_HLCDC_PWMCVAL_MASK)
  28. #define ATMEL_HLCDC_PWMPOL BIT(4)
  29. #define ATMEL_HLCDC_PWMPS_MASK GENMASK(2, 0)
  30. #define ATMEL_HLCDC_PWMPS_MAX 0x6
  31. #define ATMEL_HLCDC_PWMPS(x) ((x) & ATMEL_HLCDC_PWMPS_MASK)
  32. struct atmel_hlcdc_pwm_errata {
  33. bool slow_clk_erratum;
  34. bool div1_clk_erratum;
  35. };
  36. struct atmel_hlcdc_pwm {
  37. struct pwm_chip chip;
  38. struct atmel_hlcdc *hlcdc;
  39. struct clk *cur_clk;
  40. const struct atmel_hlcdc_pwm_errata *errata;
  41. };
  42. static inline struct atmel_hlcdc_pwm *to_atmel_hlcdc_pwm(struct pwm_chip *chip)
  43. {
  44. return container_of(chip, struct atmel_hlcdc_pwm, chip);
  45. }
  46. static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
  47. struct pwm_state *state)
  48. {
  49. struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  50. struct atmel_hlcdc *hlcdc = chip->hlcdc;
  51. unsigned int status;
  52. int ret;
  53. if (state->enabled) {
  54. struct clk *new_clk = hlcdc->slow_clk;
  55. u64 pwmcval = state->duty_cycle * 256;
  56. unsigned long clk_freq;
  57. u64 clk_period_ns;
  58. u32 pwmcfg;
  59. int pres;
  60. if (!chip->errata || !chip->errata->slow_clk_erratum) {
  61. clk_freq = clk_get_rate(new_clk);
  62. if (!clk_freq)
  63. return -EINVAL;
  64. clk_period_ns = (u64)NSEC_PER_SEC * 256;
  65. do_div(clk_period_ns, clk_freq);
  66. }
  67. /* Errata: cannot use slow clk on some IP revisions */
  68. if ((chip->errata && chip->errata->slow_clk_erratum) ||
  69. clk_period_ns > state->period) {
  70. new_clk = hlcdc->sys_clk;
  71. clk_freq = clk_get_rate(new_clk);
  72. if (!clk_freq)
  73. return -EINVAL;
  74. clk_period_ns = (u64)NSEC_PER_SEC * 256;
  75. do_div(clk_period_ns, clk_freq);
  76. }
  77. for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
  78. /* Errata: cannot divide by 1 on some IP revisions */
  79. if (!pres && chip->errata &&
  80. chip->errata->div1_clk_erratum)
  81. continue;
  82. if ((clk_period_ns << pres) >= state->period)
  83. break;
  84. }
  85. if (pres > ATMEL_HLCDC_PWMPS_MAX)
  86. return -EINVAL;
  87. pwmcfg = ATMEL_HLCDC_PWMPS(pres);
  88. if (new_clk != chip->cur_clk) {
  89. u32 gencfg = 0;
  90. int ret;
  91. ret = clk_prepare_enable(new_clk);
  92. if (ret)
  93. return ret;
  94. clk_disable_unprepare(chip->cur_clk);
  95. chip->cur_clk = new_clk;
  96. if (new_clk == hlcdc->sys_clk)
  97. gencfg = ATMEL_HLCDC_CLKPWMSEL;
  98. ret = regmap_update_bits(hlcdc->regmap,
  99. ATMEL_HLCDC_CFG(0),
  100. ATMEL_HLCDC_CLKPWMSEL,
  101. gencfg);
  102. if (ret)
  103. return ret;
  104. }
  105. do_div(pwmcval, state->period);
  106. /*
  107. * The PWM duty cycle is configurable from 0/256 to 255/256 of
  108. * the period cycle. Hence we can't set a duty cycle occupying
  109. * the whole period cycle if we're asked to.
  110. * Set it to 255 if pwmcval is greater than 256.
  111. */
  112. if (pwmcval > 255)
  113. pwmcval = 255;
  114. pwmcfg |= ATMEL_HLCDC_PWMCVAL(pwmcval);
  115. if (state->polarity == PWM_POLARITY_NORMAL)
  116. pwmcfg |= ATMEL_HLCDC_PWMPOL;
  117. ret = regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
  118. ATMEL_HLCDC_PWMCVAL_MASK |
  119. ATMEL_HLCDC_PWMPS_MASK |
  120. ATMEL_HLCDC_PWMPOL,
  121. pwmcfg);
  122. if (ret)
  123. return ret;
  124. ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
  125. ATMEL_HLCDC_PWM);
  126. if (ret)
  127. return ret;
  128. ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
  129. status,
  130. status & ATMEL_HLCDC_PWM,
  131. 10, 0);
  132. if (ret)
  133. return ret;
  134. } else {
  135. ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
  136. ATMEL_HLCDC_PWM);
  137. if (ret)
  138. return ret;
  139. ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
  140. status,
  141. !(status & ATMEL_HLCDC_PWM),
  142. 10, 0);
  143. if (ret)
  144. return ret;
  145. clk_disable_unprepare(chip->cur_clk);
  146. chip->cur_clk = NULL;
  147. }
  148. return 0;
  149. }
  150. static const struct pwm_ops atmel_hlcdc_pwm_ops = {
  151. .apply = atmel_hlcdc_pwm_apply,
  152. .owner = THIS_MODULE,
  153. };
  154. static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_at91sam9x5_errata = {
  155. .slow_clk_erratum = true,
  156. };
  157. static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_sama5d3_errata = {
  158. .div1_clk_erratum = true,
  159. };
  160. #ifdef CONFIG_PM_SLEEP
  161. static int atmel_hlcdc_pwm_suspend(struct device *dev)
  162. {
  163. struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
  164. /* Keep the periph clock enabled if the PWM is still running. */
  165. if (pwm_is_enabled(&chip->chip.pwms[0]))
  166. clk_disable_unprepare(chip->hlcdc->periph_clk);
  167. return 0;
  168. }
  169. static int atmel_hlcdc_pwm_resume(struct device *dev)
  170. {
  171. struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
  172. struct pwm_state state;
  173. int ret;
  174. pwm_get_state(&chip->chip.pwms[0], &state);
  175. /* Re-enable the periph clock it was stopped during suspend. */
  176. if (!state.enabled) {
  177. ret = clk_prepare_enable(chip->hlcdc->periph_clk);
  178. if (ret)
  179. return ret;
  180. }
  181. return atmel_hlcdc_pwm_apply(&chip->chip, &chip->chip.pwms[0], &state);
  182. }
  183. #endif
  184. static SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops,
  185. atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume);
  186. static const struct of_device_id atmel_hlcdc_dt_ids[] = {
  187. {
  188. .compatible = "atmel,at91sam9n12-hlcdc",
  189. /* 9n12 has same errata as 9x5 HLCDC PWM */
  190. .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  191. },
  192. {
  193. .compatible = "atmel,at91sam9x5-hlcdc",
  194. .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  195. },
  196. {
  197. .compatible = "atmel,sama5d2-hlcdc",
  198. },
  199. {
  200. .compatible = "atmel,sama5d3-hlcdc",
  201. .data = &atmel_hlcdc_pwm_sama5d3_errata,
  202. },
  203. {
  204. .compatible = "atmel,sama5d4-hlcdc",
  205. .data = &atmel_hlcdc_pwm_sama5d3_errata,
  206. },
  207. { /* sentinel */ },
  208. };
  209. MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
  210. static int atmel_hlcdc_pwm_probe(struct platform_device *pdev)
  211. {
  212. const struct of_device_id *match;
  213. struct device *dev = &pdev->dev;
  214. struct atmel_hlcdc_pwm *chip;
  215. struct atmel_hlcdc *hlcdc;
  216. int ret;
  217. hlcdc = dev_get_drvdata(dev->parent);
  218. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  219. if (!chip)
  220. return -ENOMEM;
  221. ret = clk_prepare_enable(hlcdc->periph_clk);
  222. if (ret)
  223. return ret;
  224. match = of_match_node(atmel_hlcdc_dt_ids, dev->parent->of_node);
  225. if (match)
  226. chip->errata = match->data;
  227. chip->hlcdc = hlcdc;
  228. chip->chip.ops = &atmel_hlcdc_pwm_ops;
  229. chip->chip.dev = dev;
  230. chip->chip.base = -1;
  231. chip->chip.npwm = 1;
  232. chip->chip.of_xlate = of_pwm_xlate_with_flags;
  233. chip->chip.of_pwm_n_cells = 3;
  234. ret = pwmchip_add_with_polarity(&chip->chip, PWM_POLARITY_INVERSED);
  235. if (ret) {
  236. clk_disable_unprepare(hlcdc->periph_clk);
  237. return ret;
  238. }
  239. platform_set_drvdata(pdev, chip);
  240. return 0;
  241. }
  242. static int atmel_hlcdc_pwm_remove(struct platform_device *pdev)
  243. {
  244. struct atmel_hlcdc_pwm *chip = platform_get_drvdata(pdev);
  245. int ret;
  246. ret = pwmchip_remove(&chip->chip);
  247. if (ret)
  248. return ret;
  249. clk_disable_unprepare(chip->hlcdc->periph_clk);
  250. return 0;
  251. }
  252. static const struct of_device_id atmel_hlcdc_pwm_dt_ids[] = {
  253. { .compatible = "atmel,hlcdc-pwm" },
  254. { /* sentinel */ },
  255. };
  256. static struct platform_driver atmel_hlcdc_pwm_driver = {
  257. .driver = {
  258. .name = "atmel-hlcdc-pwm",
  259. .of_match_table = atmel_hlcdc_pwm_dt_ids,
  260. .pm = &atmel_hlcdc_pwm_pm_ops,
  261. },
  262. .probe = atmel_hlcdc_pwm_probe,
  263. .remove = atmel_hlcdc_pwm_remove,
  264. };
  265. module_platform_driver(atmel_hlcdc_pwm_driver);
  266. MODULE_ALIAS("platform:atmel-hlcdc-pwm");
  267. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  268. MODULE_DESCRIPTION("Atmel HLCDC PWM driver");
  269. MODULE_LICENSE("GPL v2");