pwm-sti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #define PWM_PRESCALE_LOW_MASK 0x0f
  27. #define PWM_PRESCALE_HIGH_MASK 0xf0
  28. /* Regfield IDs */
  29. enum {
  30. PWMCLK_PRESCALE_LOW,
  31. PWMCLK_PRESCALE_HIGH,
  32. PWM_EN,
  33. PWM_INT_EN,
  34. /* Keep last */
  35. MAX_REGFIELDS
  36. };
  37. struct sti_pwm_compat_data {
  38. const struct reg_field *reg_fields;
  39. unsigned int num_chan;
  40. unsigned int max_pwm_cnt;
  41. unsigned int max_prescale;
  42. };
  43. struct sti_pwm_chip {
  44. struct device *dev;
  45. struct clk *clk;
  46. unsigned long clk_rate;
  47. struct regmap *regmap;
  48. struct sti_pwm_compat_data *cdata;
  49. struct regmap_field *prescale_low;
  50. struct regmap_field *prescale_high;
  51. struct regmap_field *pwm_en;
  52. struct regmap_field *pwm_int_en;
  53. unsigned long *pwm_periods;
  54. struct pwm_chip chip;
  55. struct pwm_device *cur;
  56. void __iomem *mmio;
  57. };
  58. static const struct reg_field sti_pwm_regfields[MAX_REGFIELDS] = {
  59. [PWMCLK_PRESCALE_LOW] = REG_FIELD(STI_PWMCR, 0, 3),
  60. [PWMCLK_PRESCALE_HIGH] = REG_FIELD(STI_PWMCR, 11, 14),
  61. [PWM_EN] = REG_FIELD(STI_PWMCR, 9, 9),
  62. [PWM_INT_EN] = REG_FIELD(STI_INTEN, 0, 0),
  63. };
  64. static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)
  65. {
  66. return container_of(chip, struct sti_pwm_chip, chip);
  67. }
  68. /*
  69. * Calculate the period values supported by the PWM for the
  70. * current clock rate.
  71. */
  72. static void sti_pwm_calc_periods(struct sti_pwm_chip *pc)
  73. {
  74. struct sti_pwm_compat_data *cdata = pc->cdata;
  75. struct device *dev = pc->dev;
  76. unsigned long val;
  77. int i;
  78. /*
  79. * period_ns = (10^9 * (prescaler + 1) * (MAX_PWM_COUNT + 1)) / CLK_RATE
  80. */
  81. val = NSEC_PER_SEC / pc->clk_rate;
  82. val *= cdata->max_pwm_cnt + 1;
  83. dev_dbg(dev, "possible periods for clkrate[HZ]:%lu\n", pc->clk_rate);
  84. for (i = 0; i <= cdata->max_prescale; i++) {
  85. pc->pwm_periods[i] = val * (i + 1);
  86. dev_dbg(dev, "prescale:%d, period[ns]:%lu\n",
  87. i, pc->pwm_periods[i]);
  88. }
  89. }
  90. /* Calculate the number of PWM devices configured with a period. */
  91. static unsigned int sti_pwm_count_configured(struct pwm_chip *chip)
  92. {
  93. struct pwm_device *pwm;
  94. unsigned int ncfg = 0;
  95. unsigned int i;
  96. for (i = 0; i < chip->npwm; i++) {
  97. pwm = &chip->pwms[i];
  98. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  99. if (pwm_get_period(pwm))
  100. ncfg++;
  101. }
  102. }
  103. return ncfg;
  104. }
  105. static int sti_pwm_cmp_periods(const void *key, const void *elt)
  106. {
  107. unsigned long i = *(unsigned long *)key;
  108. unsigned long j = *(unsigned long *)elt;
  109. if (i < j)
  110. return -1;
  111. else
  112. return i == j ? 0 : 1;
  113. }
  114. /*
  115. * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles.
  116. * The only way to change the period (apart from changing the PWM input clock)
  117. * is to change the PWM clock prescaler.
  118. * The prescaler is of 8 bits, so 256 prescaler values and hence
  119. * 256 possible period values are supported (for a particular clock rate).
  120. * The requested period will be applied only if it matches one of these
  121. * 256 values.
  122. */
  123. static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  124. int duty_ns, int period_ns)
  125. {
  126. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  127. struct sti_pwm_compat_data *cdata = pc->cdata;
  128. struct pwm_device *cur = pc->cur;
  129. struct device *dev = pc->dev;
  130. unsigned int prescale = 0, pwmvalx;
  131. unsigned long *found;
  132. int ret;
  133. unsigned int ncfg;
  134. bool period_same = false;
  135. ncfg = sti_pwm_count_configured(chip);
  136. if (ncfg)
  137. period_same = (period_ns == pwm_get_period(cur));
  138. /* Allow configuration changes if one of the
  139. * following conditions satisfy.
  140. * 1. No channels have been configured.
  141. * 2. Only one channel has been configured and the new request
  142. * is for the same channel.
  143. * 3. Only one channel has been configured and the new request is
  144. * for a new channel and period of the new channel is same as
  145. * the current configured period.
  146. * 4. More than one channels are configured and period of the new
  147. * requestis the same as the current period.
  148. */
  149. if (!ncfg ||
  150. ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) ||
  151. ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) ||
  152. ((ncfg > 1) && period_same)) {
  153. /* Enable clock before writing to PWM registers. */
  154. ret = clk_enable(pc->clk);
  155. if (ret)
  156. return ret;
  157. if (!period_same) {
  158. /*
  159. * Search for matching period value.
  160. * The corresponding index is our prescale value.
  161. */
  162. found = bsearch(&period_ns, &pc->pwm_periods[0],
  163. cdata->max_prescale + 1,
  164. sizeof(unsigned long),
  165. sti_pwm_cmp_periods);
  166. if (!found) {
  167. dev_err(dev,
  168. "failed to find matching period\n");
  169. ret = -EINVAL;
  170. goto clk_dis;
  171. }
  172. prescale = found - &pc->pwm_periods[0];
  173. ret =
  174. regmap_field_write(pc->prescale_low,
  175. prescale & PWM_PRESCALE_LOW_MASK);
  176. if (ret)
  177. goto clk_dis;
  178. ret =
  179. regmap_field_write(pc->prescale_high,
  180. (prescale & PWM_PRESCALE_HIGH_MASK) >> 4);
  181. if (ret)
  182. goto clk_dis;
  183. }
  184. /*
  185. * When PWMVal == 0, PWM pulse = 1 local clock cycle.
  186. * When PWMVal == max_pwm_count,
  187. * PWM pulse = (max_pwm_count + 1) local cycles,
  188. * that is continuous pulse: signal never goes low.
  189. */
  190. pwmvalx = cdata->max_pwm_cnt * duty_ns / period_ns;
  191. ret = regmap_write(pc->regmap, STI_DS_REG(pwm->hwpwm), pwmvalx);
  192. if (ret)
  193. goto clk_dis;
  194. ret = regmap_field_write(pc->pwm_int_en, 0);
  195. pc->cur = pwm;
  196. dev_dbg(dev, "prescale:%u, period:%i, duty:%i, pwmvalx:%u\n",
  197. prescale, period_ns, duty_ns, pwmvalx);
  198. } else {
  199. return -EINVAL;
  200. }
  201. clk_dis:
  202. clk_disable(pc->clk);
  203. return ret;
  204. }
  205. static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  206. {
  207. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  208. struct device *dev = pc->dev;
  209. int ret;
  210. ret = clk_enable(pc->clk);
  211. if (ret)
  212. return ret;
  213. ret = regmap_field_write(pc->pwm_en, 1);
  214. if (ret)
  215. dev_err(dev, "%s,pwm_en write failed\n", __func__);
  216. return ret;
  217. }
  218. static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  219. {
  220. struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
  221. struct device *dev = pc->dev;
  222. unsigned int val;
  223. regmap_field_write(pc->pwm_en, 0);
  224. regmap_read(pc->regmap, STI_CNT, &val);
  225. dev_dbg(dev, "pwm counter :%u\n", val);
  226. clk_disable(pc->clk);
  227. }
  228. static const struct pwm_ops sti_pwm_ops = {
  229. .config = sti_pwm_config,
  230. .enable = sti_pwm_enable,
  231. .disable = sti_pwm_disable,
  232. .owner = THIS_MODULE,
  233. };
  234. static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
  235. {
  236. struct device *dev = pc->dev;
  237. const struct reg_field *reg_fields;
  238. struct device_node *np = dev->of_node;
  239. struct sti_pwm_compat_data *cdata = pc->cdata;
  240. u32 num_chan;
  241. of_property_read_u32(np, "st,pwm-num-chan", &num_chan);
  242. if (num_chan)
  243. cdata->num_chan = num_chan;
  244. reg_fields = cdata->reg_fields;
  245. pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap,
  246. reg_fields[PWMCLK_PRESCALE_LOW]);
  247. if (IS_ERR(pc->prescale_low))
  248. return PTR_ERR(pc->prescale_low);
  249. pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap,
  250. reg_fields[PWMCLK_PRESCALE_HIGH]);
  251. if (IS_ERR(pc->prescale_high))
  252. return PTR_ERR(pc->prescale_high);
  253. pc->pwm_en = devm_regmap_field_alloc(dev, pc->regmap,
  254. reg_fields[PWM_EN]);
  255. if (IS_ERR(pc->pwm_en))
  256. return PTR_ERR(pc->pwm_en);
  257. pc->pwm_int_en = devm_regmap_field_alloc(dev, pc->regmap,
  258. reg_fields[PWM_INT_EN]);
  259. if (IS_ERR(pc->pwm_int_en))
  260. return PTR_ERR(pc->pwm_int_en);
  261. return 0;
  262. }
  263. static const struct regmap_config sti_pwm_regmap_config = {
  264. .reg_bits = 32,
  265. .val_bits = 32,
  266. .reg_stride = 4,
  267. };
  268. static int sti_pwm_probe(struct platform_device *pdev)
  269. {
  270. struct device *dev = &pdev->dev;
  271. struct sti_pwm_compat_data *cdata;
  272. struct sti_pwm_chip *pc;
  273. struct resource *res;
  274. int ret;
  275. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  276. if (!pc)
  277. return -ENOMEM;
  278. cdata = devm_kzalloc(dev, sizeof(*cdata), GFP_KERNEL);
  279. if (!cdata)
  280. return -ENOMEM;
  281. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. pc->mmio = devm_ioremap_resource(dev, res);
  283. if (IS_ERR(pc->mmio))
  284. return PTR_ERR(pc->mmio);
  285. pc->regmap = devm_regmap_init_mmio(dev, pc->mmio,
  286. &sti_pwm_regmap_config);
  287. if (IS_ERR(pc->regmap))
  288. return PTR_ERR(pc->regmap);
  289. /*
  290. * Setup PWM data with default values: some values could be replaced
  291. * with specific ones provided from Device Tree.
  292. */
  293. cdata->reg_fields = &sti_pwm_regfields[0];
  294. cdata->max_prescale = 0xff;
  295. cdata->max_pwm_cnt = 255;
  296. cdata->num_chan = 1;
  297. pc->cdata = cdata;
  298. pc->dev = dev;
  299. ret = sti_pwm_probe_dt(pc);
  300. if (ret)
  301. return ret;
  302. pc->pwm_periods = devm_kzalloc(dev,
  303. sizeof(unsigned long) * (pc->cdata->max_prescale + 1),
  304. GFP_KERNEL);
  305. if (!pc->pwm_periods)
  306. return -ENOMEM;
  307. pc->clk = of_clk_get_by_name(dev->of_node, "pwm");
  308. if (IS_ERR(pc->clk)) {
  309. dev_err(dev, "failed to get PWM clock\n");
  310. return PTR_ERR(pc->clk);
  311. }
  312. pc->clk_rate = clk_get_rate(pc->clk);
  313. if (!pc->clk_rate) {
  314. dev_err(dev, "failed to get clock rate\n");
  315. return -EINVAL;
  316. }
  317. ret = clk_prepare(pc->clk);
  318. if (ret) {
  319. dev_err(dev, "failed to prepare clock\n");
  320. return ret;
  321. }
  322. sti_pwm_calc_periods(pc);
  323. pc->chip.dev = dev;
  324. pc->chip.ops = &sti_pwm_ops;
  325. pc->chip.base = -1;
  326. pc->chip.npwm = pc->cdata->num_chan;
  327. pc->chip.can_sleep = true;
  328. ret = pwmchip_add(&pc->chip);
  329. if (ret < 0) {
  330. clk_unprepare(pc->clk);
  331. return ret;
  332. }
  333. platform_set_drvdata(pdev, pc);
  334. return 0;
  335. }
  336. static int sti_pwm_remove(struct platform_device *pdev)
  337. {
  338. struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
  339. unsigned int i;
  340. for (i = 0; i < pc->cdata->num_chan; i++)
  341. pwm_disable(&pc->chip.pwms[i]);
  342. clk_unprepare(pc->clk);
  343. return pwmchip_remove(&pc->chip);
  344. }
  345. static const struct of_device_id sti_pwm_of_match[] = {
  346. { .compatible = "st,sti-pwm", },
  347. { /* sentinel */ }
  348. };
  349. MODULE_DEVICE_TABLE(of, sti_pwm_of_match);
  350. static struct platform_driver sti_pwm_driver = {
  351. .driver = {
  352. .name = "sti-pwm",
  353. .of_match_table = sti_pwm_of_match,
  354. },
  355. .probe = sti_pwm_probe,
  356. .remove = sti_pwm_remove,
  357. };
  358. module_platform_driver(sti_pwm_driver);
  359. MODULE_AUTHOR("Ajit Pal Singh <ajitpal.singh@st.com>");
  360. MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");
  361. MODULE_LICENSE("GPL");