pwm-sti.c 10 KB

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