pwm-atmel-hlcdc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_config(struct pwm_chip *c,
  47. struct pwm_device *pwm,
  48. int duty_ns, int period_ns)
  49. {
  50. struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  51. struct atmel_hlcdc *hlcdc = chip->hlcdc;
  52. struct clk *new_clk = hlcdc->slow_clk;
  53. u64 pwmcval = duty_ns * 256;
  54. unsigned long clk_freq;
  55. u64 clk_period_ns;
  56. u32 pwmcfg;
  57. int pres;
  58. if (!chip->errata || !chip->errata->slow_clk_erratum) {
  59. clk_freq = clk_get_rate(new_clk);
  60. if (!clk_freq)
  61. return -EINVAL;
  62. clk_period_ns = (u64)NSEC_PER_SEC * 256;
  63. do_div(clk_period_ns, clk_freq);
  64. }
  65. /* Errata: cannot use slow clk on some IP revisions */
  66. if ((chip->errata && chip->errata->slow_clk_erratum) ||
  67. clk_period_ns > period_ns) {
  68. new_clk = hlcdc->sys_clk;
  69. clk_freq = clk_get_rate(new_clk);
  70. if (!clk_freq)
  71. return -EINVAL;
  72. clk_period_ns = (u64)NSEC_PER_SEC * 256;
  73. do_div(clk_period_ns, clk_freq);
  74. }
  75. for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
  76. /* Errata: cannot divide by 1 on some IP revisions */
  77. if (!pres && chip->errata && chip->errata->div1_clk_erratum)
  78. continue;
  79. if ((clk_period_ns << pres) >= period_ns)
  80. break;
  81. }
  82. if (pres > ATMEL_HLCDC_PWMPS_MAX)
  83. return -EINVAL;
  84. pwmcfg = ATMEL_HLCDC_PWMPS(pres);
  85. if (new_clk != chip->cur_clk) {
  86. u32 gencfg = 0;
  87. int ret;
  88. ret = clk_prepare_enable(new_clk);
  89. if (ret)
  90. return ret;
  91. clk_disable_unprepare(chip->cur_clk);
  92. chip->cur_clk = new_clk;
  93. if (new_clk == hlcdc->sys_clk)
  94. gencfg = ATMEL_HLCDC_CLKPWMSEL;
  95. ret = regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(0),
  96. ATMEL_HLCDC_CLKPWMSEL, gencfg);
  97. if (ret)
  98. return ret;
  99. }
  100. do_div(pwmcval, period_ns);
  101. /*
  102. * The PWM duty cycle is configurable from 0/256 to 255/256 of the
  103. * period cycle. Hence we can't set a duty cycle occupying the
  104. * whole period cycle if we're asked to.
  105. * Set it to 255 if pwmcval is greater than 256.
  106. */
  107. if (pwmcval > 255)
  108. pwmcval = 255;
  109. pwmcfg |= ATMEL_HLCDC_PWMCVAL(pwmcval);
  110. return regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
  111. ATMEL_HLCDC_PWMCVAL_MASK |
  112. ATMEL_HLCDC_PWMPS_MASK,
  113. pwmcfg);
  114. }
  115. static int atmel_hlcdc_pwm_set_polarity(struct pwm_chip *c,
  116. struct pwm_device *pwm,
  117. enum pwm_polarity polarity)
  118. {
  119. struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  120. struct atmel_hlcdc *hlcdc = chip->hlcdc;
  121. u32 cfg = 0;
  122. if (polarity == PWM_POLARITY_NORMAL)
  123. cfg = ATMEL_HLCDC_PWMPOL;
  124. return regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
  125. ATMEL_HLCDC_PWMPOL, cfg);
  126. }
  127. static int atmel_hlcdc_pwm_enable(struct pwm_chip *c, struct pwm_device *pwm)
  128. {
  129. struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  130. struct atmel_hlcdc *hlcdc = chip->hlcdc;
  131. u32 status;
  132. int ret;
  133. ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PWM);
  134. if (ret)
  135. return ret;
  136. while (true) {
  137. ret = regmap_read(hlcdc->regmap, ATMEL_HLCDC_SR, &status);
  138. if (ret)
  139. return ret;
  140. if ((status & ATMEL_HLCDC_PWM) != 0)
  141. break;
  142. usleep_range(1, 10);
  143. }
  144. return 0;
  145. }
  146. static void atmel_hlcdc_pwm_disable(struct pwm_chip *c,
  147. struct pwm_device *pwm)
  148. {
  149. struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  150. struct atmel_hlcdc *hlcdc = chip->hlcdc;
  151. u32 status;
  152. int ret;
  153. ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PWM);
  154. if (ret)
  155. return;
  156. while (true) {
  157. ret = regmap_read(hlcdc->regmap, ATMEL_HLCDC_SR, &status);
  158. if (ret)
  159. return;
  160. if ((status & ATMEL_HLCDC_PWM) == 0)
  161. break;
  162. usleep_range(1, 10);
  163. }
  164. }
  165. static const struct pwm_ops atmel_hlcdc_pwm_ops = {
  166. .config = atmel_hlcdc_pwm_config,
  167. .set_polarity = atmel_hlcdc_pwm_set_polarity,
  168. .enable = atmel_hlcdc_pwm_enable,
  169. .disable = atmel_hlcdc_pwm_disable,
  170. .owner = THIS_MODULE,
  171. };
  172. static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_at91sam9x5_errata = {
  173. .slow_clk_erratum = true,
  174. };
  175. static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_sama5d3_errata = {
  176. .div1_clk_erratum = true,
  177. };
  178. static const struct of_device_id atmel_hlcdc_dt_ids[] = {
  179. {
  180. .compatible = "atmel,at91sam9n12-hlcdc",
  181. /* 9n12 has same errata as 9x5 HLCDC PWM */
  182. .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  183. },
  184. {
  185. .compatible = "atmel,at91sam9x5-hlcdc",
  186. .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  187. },
  188. {
  189. .compatible = "atmel,sama5d2-hlcdc",
  190. },
  191. {
  192. .compatible = "atmel,sama5d3-hlcdc",
  193. .data = &atmel_hlcdc_pwm_sama5d3_errata,
  194. },
  195. {
  196. .compatible = "atmel,sama5d4-hlcdc",
  197. .data = &atmel_hlcdc_pwm_sama5d3_errata,
  198. },
  199. { /* sentinel */ },
  200. };
  201. MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
  202. static int atmel_hlcdc_pwm_probe(struct platform_device *pdev)
  203. {
  204. const struct of_device_id *match;
  205. struct device *dev = &pdev->dev;
  206. struct atmel_hlcdc_pwm *chip;
  207. struct atmel_hlcdc *hlcdc;
  208. int ret;
  209. hlcdc = dev_get_drvdata(dev->parent);
  210. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  211. if (!chip)
  212. return -ENOMEM;
  213. ret = clk_prepare_enable(hlcdc->periph_clk);
  214. if (ret)
  215. return ret;
  216. match = of_match_node(atmel_hlcdc_dt_ids, dev->parent->of_node);
  217. if (match)
  218. chip->errata = match->data;
  219. chip->hlcdc = hlcdc;
  220. chip->chip.ops = &atmel_hlcdc_pwm_ops;
  221. chip->chip.dev = dev;
  222. chip->chip.base = -1;
  223. chip->chip.npwm = 1;
  224. chip->chip.of_xlate = of_pwm_xlate_with_flags;
  225. chip->chip.of_pwm_n_cells = 3;
  226. chip->chip.can_sleep = 1;
  227. ret = pwmchip_add_with_polarity(&chip->chip, PWM_POLARITY_INVERSED);
  228. if (ret) {
  229. clk_disable_unprepare(hlcdc->periph_clk);
  230. return ret;
  231. }
  232. platform_set_drvdata(pdev, chip);
  233. return 0;
  234. }
  235. static int atmel_hlcdc_pwm_remove(struct platform_device *pdev)
  236. {
  237. struct atmel_hlcdc_pwm *chip = platform_get_drvdata(pdev);
  238. int ret;
  239. ret = pwmchip_remove(&chip->chip);
  240. if (ret)
  241. return ret;
  242. clk_disable_unprepare(chip->hlcdc->periph_clk);
  243. return 0;
  244. }
  245. static const struct of_device_id atmel_hlcdc_pwm_dt_ids[] = {
  246. { .compatible = "atmel,hlcdc-pwm" },
  247. { /* sentinel */ },
  248. };
  249. static struct platform_driver atmel_hlcdc_pwm_driver = {
  250. .driver = {
  251. .name = "atmel-hlcdc-pwm",
  252. .of_match_table = atmel_hlcdc_pwm_dt_ids,
  253. },
  254. .probe = atmel_hlcdc_pwm_probe,
  255. .remove = atmel_hlcdc_pwm_remove,
  256. };
  257. module_platform_driver(atmel_hlcdc_pwm_driver);
  258. MODULE_ALIAS("platform:atmel-hlcdc-pwm");
  259. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  260. MODULE_DESCRIPTION("Atmel HLCDC PWM driver");
  261. MODULE_LICENSE("GPL v2");