pwm-sti.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * PWM device driver for ST SoCs.
  3. * Author: Ajit Pal Singh <ajitpal.singh@st.com>
  4. *
  5. * Copyright (C) 2013-2014 STMicroelectronics (R&D) Limited
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/bsearch.h>
  13. #include <linux/clk.h>
  14. #include <linux/math64.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/time.h>
  23. #define STI_DS_REG(ch) (4 * (ch)) /* Channel's Duty Cycle register */
  24. #define STI_PWMCR 0x50 /* Control/Config register */
  25. #define STI_INTEN 0x54 /* Interrupt Enable/Disable register */
  26. /* Regfield IDs */
  27. enum {
  28. PWMCLK_PRESCALE,
  29. PWM_EN,
  30. PWM_INT_EN,
  31. /* Keep last */
  32. MAX_REGFIELDS
  33. };
  34. struct sti_pwm_compat_data {
  35. const struct reg_field *reg_fields;
  36. unsigned int num_chan;
  37. unsigned int max_pwm_cnt;
  38. unsigned int max_prescale;
  39. };
  40. struct sti_pwm_chip {
  41. struct device *dev;
  42. struct clk *clk;
  43. unsigned long clk_rate;
  44. struct regmap *regmap;
  45. struct sti_pwm_compat_data *cdata;
  46. struct regmap_field *prescale;
  47. struct regmap_field *pwm_en;
  48. struct regmap_field *pwm_int_en;
  49. unsigned long *pwm_periods;
  50. struct pwm_chip chip;
  51. void __iomem *mmio;
  52. };
  53. static const struct reg_field sti_pwm_regfields[MAX_REGFIELDS] = {
  54. [PWMCLK_PRESCALE] = REG_FIELD(STI_PWMCR, 0, 3),
  55. [PWM_EN] = REG_FIELD(STI_PWMCR, 9, 9),
  56. [PWM_INT_EN] = REG_FIELD(STI_INTEN, 0, 0),
  57. };
  58. static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)
  59. {
  60. return container_of(chip, struct sti_pwm_chip, chip);
  61. }
  62. /*
  63. * Calculate the period values supported by the PWM for the
  64. * current clock rate.
  65. */
  66. static void sti_pwm_calc_periods(struct sti_pwm_chip *pc)
  67. {
  68. struct sti_pwm_compat_data *cdata = pc->cdata;
  69. struct device *dev = pc->dev;
  70. unsigned long val;
  71. int i;
  72. /*
  73. * period_ns = (10^9 * (prescaler + 1) * (MAX_PWM_COUNT + 1)) / CLK_RATE
  74. */
  75. val = NSEC_PER_SEC / pc->clk_rate;
  76. val *= cdata->max_pwm_cnt + 1;
  77. dev_dbg(dev, "possible periods for clkrate[HZ]:%lu\n", pc->clk_rate);
  78. for (i = 0; i <= cdata->max_prescale; i++) {
  79. pc->pwm_periods[i] = val * (i + 1);
  80. dev_dbg(dev, "prescale:%d, period[ns]:%lu\n",
  81. i, pc->pwm_periods[i]);
  82. }
  83. }
  84. static int sti_pwm_cmp_periods(const void *key, const void *elt)
  85. {
  86. unsigned long i = *(unsigned long *)key;
  87. unsigned long j = *(unsigned long *)elt;
  88. if (i < j)
  89. return -1;
  90. else
  91. return i == j ? 0 : 1;
  92. }
  93. /*
  94. * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles.
  95. * The only way to change the period (apart from changing the PWM input clock)
  96. * is to change the PWM clock prescaler.
  97. * The prescaler is of 4 bits, so only 16 prescaler values and hence only
  98. * 16 possible period values are supported (for a particular clock rate).
  99. * The requested period will be applied only if it matches one of these
  100. * 16 values.
  101. */
  102. static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  103. int duty_ns, int period_ns)
  104. {
  105. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  106. struct sti_pwm_compat_data *cdata = pc->cdata;
  107. struct device *dev = pc->dev;
  108. unsigned int prescale, pwmvalx;
  109. unsigned long *found;
  110. int ret;
  111. /*
  112. * Search for matching period value. The corresponding index is our
  113. * prescale value
  114. */
  115. found = bsearch(&period_ns, &pc->pwm_periods[0],
  116. cdata->max_prescale + 1, sizeof(unsigned long),
  117. sti_pwm_cmp_periods);
  118. if (!found) {
  119. dev_err(dev, "failed to find matching period\n");
  120. return -EINVAL;
  121. }
  122. prescale = found - &pc->pwm_periods[0];
  123. /*
  124. * When PWMVal == 0, PWM pulse = 1 local clock cycle.
  125. * When PWMVal == max_pwm_count,
  126. * PWM pulse = (max_pwm_count + 1) local cycles,
  127. * that is continuous pulse: signal never goes low.
  128. */
  129. pwmvalx = cdata->max_pwm_cnt * duty_ns / period_ns;
  130. dev_dbg(dev, "prescale:%u, period:%i, duty:%i, pwmvalx:%u\n",
  131. prescale, period_ns, duty_ns, pwmvalx);
  132. /* Enable clock before writing to PWM registers */
  133. ret = clk_enable(pc->clk);
  134. if (ret)
  135. return ret;
  136. ret = regmap_field_write(pc->prescale, prescale);
  137. if (ret)
  138. goto clk_dis;
  139. ret = regmap_write(pc->regmap, STI_PWMVAL(pwm->hwpwm), pwmvalx);
  140. if (ret)
  141. goto clk_dis;
  142. ret = regmap_field_write(pc->pwm_int_en, 0);
  143. clk_dis:
  144. clk_disable(pc->clk);
  145. return ret;
  146. }
  147. static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  148. {
  149. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  150. struct device *dev = pc->dev;
  151. int ret;
  152. ret = clk_enable(pc->clk);
  153. if (ret)
  154. return ret;
  155. ret = regmap_field_write(pc->pwm_en, 1);
  156. if (ret)
  157. dev_err(dev, "%s,pwm_en write failed\n", __func__);
  158. return ret;
  159. }
  160. static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  161. {
  162. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  163. struct device *dev = pc->dev;
  164. unsigned int val;
  165. regmap_field_write(pc->pwm_en, 0);
  166. regmap_read(pc->regmap, STI_CNT, &val);
  167. dev_dbg(dev, "pwm counter :%u\n", val);
  168. clk_disable(pc->clk);
  169. }
  170. static const struct pwm_ops sti_pwm_ops = {
  171. .config = sti_pwm_config,
  172. .enable = sti_pwm_enable,
  173. .disable = sti_pwm_disable,
  174. .owner = THIS_MODULE,
  175. };
  176. static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
  177. {
  178. struct device *dev = pc->dev;
  179. const struct reg_field *reg_fields;
  180. struct device_node *np = dev->of_node;
  181. struct sti_pwm_compat_data *cdata = pc->cdata;
  182. u32 num_chan;
  183. of_property_read_u32(np, "st,pwm-num-chan", &num_chan);
  184. if (num_chan)
  185. cdata->num_chan = num_chan;
  186. reg_fields = cdata->reg_fields;
  187. pc->prescale = devm_regmap_field_alloc(dev, pc->regmap,
  188. reg_fields[PWMCLK_PRESCALE]);
  189. if (IS_ERR(pc->prescale))
  190. return PTR_ERR(pc->prescale);
  191. pc->pwm_en = devm_regmap_field_alloc(dev, pc->regmap,
  192. reg_fields[PWM_EN]);
  193. if (IS_ERR(pc->pwm_en))
  194. return PTR_ERR(pc->pwm_en);
  195. pc->pwm_int_en = devm_regmap_field_alloc(dev, pc->regmap,
  196. reg_fields[PWM_INT_EN]);
  197. if (IS_ERR(pc->pwm_int_en))
  198. return PTR_ERR(pc->pwm_int_en);
  199. return 0;
  200. }
  201. static const struct regmap_config sti_pwm_regmap_config = {
  202. .reg_bits = 32,
  203. .val_bits = 32,
  204. .reg_stride = 4,
  205. };
  206. static int sti_pwm_probe(struct platform_device *pdev)
  207. {
  208. struct device *dev = &pdev->dev;
  209. struct sti_pwm_compat_data *cdata;
  210. struct sti_pwm_chip *pc;
  211. struct resource *res;
  212. int ret;
  213. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  214. if (!pc)
  215. return -ENOMEM;
  216. cdata = devm_kzalloc(dev, sizeof(*cdata), GFP_KERNEL);
  217. if (!cdata)
  218. return -ENOMEM;
  219. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  220. pc->mmio = devm_ioremap_resource(dev, res);
  221. if (IS_ERR(pc->mmio))
  222. return PTR_ERR(pc->mmio);
  223. pc->regmap = devm_regmap_init_mmio(dev, pc->mmio,
  224. &sti_pwm_regmap_config);
  225. if (IS_ERR(pc->regmap))
  226. return PTR_ERR(pc->regmap);
  227. /*
  228. * Setup PWM data with default values: some values could be replaced
  229. * with specific ones provided from Device Tree.
  230. */
  231. cdata->reg_fields = &sti_pwm_regfields[0];
  232. cdata->max_prescale = 0xff;
  233. cdata->max_pwm_cnt = 255;
  234. cdata->num_chan = 1;
  235. pc->cdata = cdata;
  236. pc->dev = dev;
  237. ret = sti_pwm_probe_dt(pc);
  238. if (ret)
  239. return ret;
  240. pc->pwm_periods = devm_kzalloc(dev,
  241. sizeof(unsigned long) * (pc->cdata->max_prescale + 1),
  242. GFP_KERNEL);
  243. if (!pc->pwm_periods)
  244. return -ENOMEM;
  245. pc->clk = of_clk_get_by_name(dev->of_node, "pwm");
  246. if (IS_ERR(pc->clk)) {
  247. dev_err(dev, "failed to get PWM clock\n");
  248. return PTR_ERR(pc->clk);
  249. }
  250. pc->clk_rate = clk_get_rate(pc->clk);
  251. if (!pc->clk_rate) {
  252. dev_err(dev, "failed to get clock rate\n");
  253. return -EINVAL;
  254. }
  255. ret = clk_prepare(pc->clk);
  256. if (ret) {
  257. dev_err(dev, "failed to prepare clock\n");
  258. return ret;
  259. }
  260. sti_pwm_calc_periods(pc);
  261. pc->chip.dev = dev;
  262. pc->chip.ops = &sti_pwm_ops;
  263. pc->chip.base = -1;
  264. pc->chip.npwm = pc->cdata->num_chan;
  265. pc->chip.can_sleep = true;
  266. ret = pwmchip_add(&pc->chip);
  267. if (ret < 0) {
  268. clk_unprepare(pc->clk);
  269. return ret;
  270. }
  271. platform_set_drvdata(pdev, pc);
  272. return 0;
  273. }
  274. static int sti_pwm_remove(struct platform_device *pdev)
  275. {
  276. struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
  277. unsigned int i;
  278. for (i = 0; i < pc->cdata->num_chan; i++)
  279. pwm_disable(&pc->chip.pwms[i]);
  280. clk_unprepare(pc->clk);
  281. return pwmchip_remove(&pc->chip);
  282. }
  283. static const struct of_device_id sti_pwm_of_match[] = {
  284. { .compatible = "st,sti-pwm", },
  285. { /* sentinel */ }
  286. };
  287. MODULE_DEVICE_TABLE(of, sti_pwm_of_match);
  288. static struct platform_driver sti_pwm_driver = {
  289. .driver = {
  290. .name = "sti-pwm",
  291. .of_match_table = sti_pwm_of_match,
  292. },
  293. .probe = sti_pwm_probe,
  294. .remove = sti_pwm_remove,
  295. };
  296. module_platform_driver(sti_pwm_driver);
  297. MODULE_AUTHOR("Ajit Pal Singh <ajitpal.singh@st.com>");
  298. MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");
  299. MODULE_LICENSE("GPL");