pwm-atmel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Atmel Pulse Width Modulation Controller
  4. *
  5. * Copyright (C) 2013 Atmel Corporation
  6. * Bo Shen <voice.shen@atmel.com>
  7. *
  8. * Links to reference manuals for the supported PWM chips can be found in
  9. * Documentation/arch/arm/microchip.rst.
  10. *
  11. * Limitations:
  12. * - Periods start with the inactive level.
  13. * - Hardware has to be stopped in general to update settings.
  14. *
  15. * Software bugs/possible improvements:
  16. * - When atmel_pwm_apply() is called with state->enabled=false a change in
  17. * state->polarity isn't honored.
  18. * - Instead of sleeping to wait for a completed period, the interrupt
  19. * functionality could be used.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pwm.h>
  29. #include <linux/slab.h>
  30. /* The following is global registers for PWM controller */
  31. #define PWM_ENA 0x04
  32. #define PWM_DIS 0x08
  33. #define PWM_SR 0x0C
  34. #define PWM_ISR 0x1C
  35. /* Bit field in SR */
  36. #define PWM_SR_ALL_CH_MASK 0x0F
  37. /* The following register is PWM channel related registers */
  38. #define PWM_CH_REG_OFFSET 0x200
  39. #define PWM_CH_REG_SIZE 0x20
  40. #define PWM_CMR 0x0
  41. /* Bit field in CMR */
  42. #define PWM_CMR_CPOL (1 << 9)
  43. #define PWM_CMR_UPD_CDTY (1 << 10)
  44. #define PWM_CMR_CPRE_MSK 0xF
  45. /* The following registers for PWM v1 */
  46. #define PWMV1_CDTY 0x04
  47. #define PWMV1_CPRD 0x08
  48. #define PWMV1_CUPD 0x10
  49. /* The following registers for PWM v2 */
  50. #define PWMV2_CDTY 0x04
  51. #define PWMV2_CDTYUPD 0x08
  52. #define PWMV2_CPRD 0x0C
  53. #define PWMV2_CPRDUPD 0x10
  54. #define PWM_MAX_PRES 10
  55. struct atmel_pwm_registers {
  56. u8 period;
  57. u8 period_upd;
  58. u8 duty;
  59. u8 duty_upd;
  60. };
  61. struct atmel_pwm_config {
  62. u32 period_bits;
  63. };
  64. struct atmel_pwm_data {
  65. struct atmel_pwm_registers regs;
  66. struct atmel_pwm_config cfg;
  67. };
  68. struct atmel_pwm_chip {
  69. struct clk *clk;
  70. void __iomem *base;
  71. const struct atmel_pwm_data *data;
  72. /*
  73. * The hardware supports a mechanism to update a channel's duty cycle at
  74. * the end of the currently running period. When such an update is
  75. * pending we delay disabling the PWM until the new configuration is
  76. * active because otherwise pmw_config(duty_cycle=0); pwm_disable();
  77. * might not result in an inactive output.
  78. * This bitmask tracks for which channels an update is pending in
  79. * hardware.
  80. */
  81. u32 update_pending;
  82. };
  83. static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
  84. {
  85. return pwmchip_get_drvdata(chip);
  86. }
  87. static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
  88. unsigned long offset)
  89. {
  90. return readl_relaxed(chip->base + offset);
  91. }
  92. static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
  93. unsigned long offset, unsigned long val)
  94. {
  95. writel_relaxed(val, chip->base + offset);
  96. }
  97. static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
  98. unsigned int ch, unsigned long offset)
  99. {
  100. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  101. return atmel_pwm_readl(chip, base + offset);
  102. }
  103. static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
  104. unsigned int ch, unsigned long offset,
  105. unsigned long val)
  106. {
  107. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  108. atmel_pwm_writel(chip, base + offset, val);
  109. }
  110. static void atmel_pwm_update_pending(struct atmel_pwm_chip *chip)
  111. {
  112. /*
  113. * Each channel that has its bit in ISR set started a new period since
  114. * ISR was cleared and so there is no more update pending. Note that
  115. * reading ISR clears it, so this needs to handle all channels to not
  116. * loose information.
  117. */
  118. u32 isr = atmel_pwm_readl(chip, PWM_ISR);
  119. chip->update_pending &= ~isr;
  120. }
  121. static void atmel_pwm_set_pending(struct atmel_pwm_chip *chip, unsigned int ch)
  122. {
  123. /*
  124. * Clear pending flags in hardware because otherwise there might still
  125. * be a stale flag in ISR.
  126. */
  127. atmel_pwm_update_pending(chip);
  128. chip->update_pending |= (1 << ch);
  129. }
  130. static int atmel_pwm_test_pending(struct atmel_pwm_chip *chip, unsigned int ch)
  131. {
  132. int ret = 0;
  133. if (chip->update_pending & (1 << ch)) {
  134. atmel_pwm_update_pending(chip);
  135. if (chip->update_pending & (1 << ch))
  136. ret = 1;
  137. }
  138. return ret;
  139. }
  140. static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip *chip, unsigned int ch)
  141. {
  142. unsigned long timeout = jiffies + 2 * HZ;
  143. int ret;
  144. while ((ret = atmel_pwm_test_pending(chip, ch)) &&
  145. time_before(jiffies, timeout))
  146. usleep_range(10, 100);
  147. return ret ? -ETIMEDOUT : 0;
  148. }
  149. static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
  150. unsigned long clkrate,
  151. const struct pwm_state *state,
  152. unsigned long *cprd, u32 *pres)
  153. {
  154. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  155. unsigned long long cycles = state->period;
  156. int shift;
  157. /* Calculate the period cycles and prescale value */
  158. cycles *= clkrate;
  159. do_div(cycles, NSEC_PER_SEC);
  160. /*
  161. * The register for the period length is cfg.period_bits bits wide.
  162. * So for each bit the number of clock cycles is wider divide the input
  163. * clock frequency by two using pres and shift cprd accordingly.
  164. */
  165. shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
  166. if (shift > PWM_MAX_PRES) {
  167. dev_err(pwmchip_parent(chip), "pres exceeds the maximum value\n");
  168. return -EINVAL;
  169. } else if (shift > 0) {
  170. *pres = shift;
  171. cycles >>= *pres;
  172. } else {
  173. *pres = 0;
  174. }
  175. *cprd = cycles;
  176. return 0;
  177. }
  178. static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
  179. unsigned long clkrate, unsigned long cprd,
  180. u32 pres, unsigned long *cdty)
  181. {
  182. unsigned long long cycles = state->duty_cycle;
  183. cycles *= clkrate;
  184. do_div(cycles, NSEC_PER_SEC);
  185. cycles >>= pres;
  186. *cdty = cprd - cycles;
  187. }
  188. static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
  189. unsigned long cdty)
  190. {
  191. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  192. u32 val;
  193. if (atmel_pwm->data->regs.duty_upd ==
  194. atmel_pwm->data->regs.period_upd) {
  195. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  196. val &= ~PWM_CMR_UPD_CDTY;
  197. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  198. }
  199. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  200. atmel_pwm->data->regs.duty_upd, cdty);
  201. atmel_pwm_set_pending(atmel_pwm, pwm->hwpwm);
  202. }
  203. static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
  204. struct pwm_device *pwm,
  205. unsigned long cprd, unsigned long cdty)
  206. {
  207. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  208. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  209. atmel_pwm->data->regs.duty, cdty);
  210. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  211. atmel_pwm->data->regs.period, cprd);
  212. }
  213. static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
  214. bool disable_clk)
  215. {
  216. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  217. unsigned long timeout;
  218. atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
  219. atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
  220. /*
  221. * Wait for the PWM channel disable operation to be effective before
  222. * stopping the clock.
  223. */
  224. timeout = jiffies + 2 * HZ;
  225. while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
  226. time_before(jiffies, timeout))
  227. usleep_range(10, 100);
  228. if (disable_clk)
  229. clk_disable(atmel_pwm->clk);
  230. }
  231. static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  232. const struct pwm_state *state)
  233. {
  234. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  235. unsigned long cprd, cdty;
  236. u32 pres, val;
  237. int ret;
  238. if (state->enabled) {
  239. unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
  240. if (pwm->state.enabled &&
  241. pwm->state.polarity == state->polarity &&
  242. pwm->state.period == state->period) {
  243. u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  244. cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  245. atmel_pwm->data->regs.period);
  246. pres = cmr & PWM_CMR_CPRE_MSK;
  247. atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
  248. atmel_pwm_update_cdty(chip, pwm, cdty);
  249. return 0;
  250. }
  251. ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
  252. &pres);
  253. if (ret) {
  254. dev_err(pwmchip_parent(chip),
  255. "failed to calculate cprd and prescaler\n");
  256. return ret;
  257. }
  258. atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
  259. if (pwm->state.enabled) {
  260. atmel_pwm_disable(chip, pwm, false);
  261. } else {
  262. ret = clk_enable(atmel_pwm->clk);
  263. if (ret) {
  264. dev_err(pwmchip_parent(chip), "failed to enable clock\n");
  265. return ret;
  266. }
  267. }
  268. /* It is necessary to preserve CPOL, inside CMR */
  269. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  270. val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
  271. if (state->polarity == PWM_POLARITY_NORMAL)
  272. val &= ~PWM_CMR_CPOL;
  273. else
  274. val |= PWM_CMR_CPOL;
  275. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  276. atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
  277. atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
  278. } else if (pwm->state.enabled) {
  279. atmel_pwm_disable(chip, pwm, true);
  280. }
  281. return 0;
  282. }
  283. static int atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  284. struct pwm_state *state)
  285. {
  286. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  287. u32 sr, cmr;
  288. sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
  289. cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  290. if (sr & (1 << pwm->hwpwm)) {
  291. unsigned long rate = clk_get_rate(atmel_pwm->clk);
  292. u32 cdty, cprd, pres;
  293. u64 tmp;
  294. pres = cmr & PWM_CMR_CPRE_MSK;
  295. cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  296. atmel_pwm->data->regs.period);
  297. tmp = (u64)cprd * NSEC_PER_SEC;
  298. tmp <<= pres;
  299. state->period = DIV64_U64_ROUND_UP(tmp, rate);
  300. /* Wait for an updated duty_cycle queued in hardware */
  301. atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
  302. cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  303. atmel_pwm->data->regs.duty);
  304. tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
  305. tmp <<= pres;
  306. state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
  307. state->enabled = true;
  308. } else {
  309. state->enabled = false;
  310. }
  311. if (cmr & PWM_CMR_CPOL)
  312. state->polarity = PWM_POLARITY_INVERSED;
  313. else
  314. state->polarity = PWM_POLARITY_NORMAL;
  315. return 0;
  316. }
  317. static const struct pwm_ops atmel_pwm_ops = {
  318. .apply = atmel_pwm_apply,
  319. .get_state = atmel_pwm_get_state,
  320. };
  321. static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
  322. .regs = {
  323. .period = PWMV1_CPRD,
  324. .period_upd = PWMV1_CUPD,
  325. .duty = PWMV1_CDTY,
  326. .duty_upd = PWMV1_CUPD,
  327. },
  328. .cfg = {
  329. /* 16 bits to keep period and duty. */
  330. .period_bits = 16,
  331. },
  332. };
  333. static const struct atmel_pwm_data atmel_sama5_pwm_data = {
  334. .regs = {
  335. .period = PWMV2_CPRD,
  336. .period_upd = PWMV2_CPRDUPD,
  337. .duty = PWMV2_CDTY,
  338. .duty_upd = PWMV2_CDTYUPD,
  339. },
  340. .cfg = {
  341. /* 16 bits to keep period and duty. */
  342. .period_bits = 16,
  343. },
  344. };
  345. static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
  346. .regs = {
  347. .period = PWMV1_CPRD,
  348. .period_upd = PWMV1_CUPD,
  349. .duty = PWMV1_CDTY,
  350. .duty_upd = PWMV1_CUPD,
  351. },
  352. .cfg = {
  353. /* 32 bits to keep period and duty. */
  354. .period_bits = 32,
  355. },
  356. };
  357. static const struct of_device_id atmel_pwm_dt_ids[] = {
  358. {
  359. .compatible = "atmel,at91sam9rl-pwm",
  360. .data = &atmel_sam9rl_pwm_data,
  361. }, {
  362. .compatible = "atmel,sama5d3-pwm",
  363. .data = &atmel_sama5_pwm_data,
  364. }, {
  365. .compatible = "atmel,sama5d2-pwm",
  366. .data = &atmel_sama5_pwm_data,
  367. }, {
  368. .compatible = "microchip,sam9x60-pwm",
  369. .data = &mchp_sam9x60_pwm_data,
  370. }, {
  371. /* sentinel */
  372. },
  373. };
  374. MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
  375. static int atmel_pwm_enable_clk_if_on(struct pwm_chip *chip, bool on)
  376. {
  377. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  378. unsigned int i, cnt = 0;
  379. unsigned long sr;
  380. int ret = 0;
  381. sr = atmel_pwm_readl(atmel_pwm, PWM_SR) & PWM_SR_ALL_CH_MASK;
  382. if (!sr)
  383. return 0;
  384. cnt = bitmap_weight(&sr, chip->npwm);
  385. if (!on)
  386. goto disable_clk;
  387. for (i = 0; i < cnt; i++) {
  388. ret = clk_enable(atmel_pwm->clk);
  389. if (ret) {
  390. dev_err(pwmchip_parent(chip),
  391. "failed to enable clock for pwm %pe\n",
  392. ERR_PTR(ret));
  393. cnt = i;
  394. goto disable_clk;
  395. }
  396. }
  397. return 0;
  398. disable_clk:
  399. while (cnt--)
  400. clk_disable(atmel_pwm->clk);
  401. return ret;
  402. }
  403. static int atmel_pwm_probe(struct platform_device *pdev)
  404. {
  405. struct atmel_pwm_chip *atmel_pwm;
  406. struct pwm_chip *chip;
  407. int ret;
  408. chip = devm_pwmchip_alloc(&pdev->dev, 4, sizeof(*atmel_pwm));
  409. if (IS_ERR(chip))
  410. return PTR_ERR(chip);
  411. atmel_pwm = to_atmel_pwm_chip(chip);
  412. atmel_pwm->data = of_device_get_match_data(&pdev->dev);
  413. atmel_pwm->update_pending = 0;
  414. atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
  415. if (IS_ERR(atmel_pwm->base))
  416. return PTR_ERR(atmel_pwm->base);
  417. atmel_pwm->clk = devm_clk_get_prepared(&pdev->dev, NULL);
  418. if (IS_ERR(atmel_pwm->clk))
  419. return dev_err_probe(&pdev->dev, PTR_ERR(atmel_pwm->clk),
  420. "failed to get prepared PWM clock\n");
  421. chip->ops = &atmel_pwm_ops;
  422. ret = atmel_pwm_enable_clk_if_on(chip, true);
  423. if (ret < 0)
  424. return ret;
  425. ret = devm_pwmchip_add(&pdev->dev, chip);
  426. if (ret < 0) {
  427. dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  428. goto disable_clk;
  429. }
  430. return 0;
  431. disable_clk:
  432. atmel_pwm_enable_clk_if_on(chip, false);
  433. return ret;
  434. }
  435. static struct platform_driver atmel_pwm_driver = {
  436. .driver = {
  437. .name = "atmel-pwm",
  438. .of_match_table = atmel_pwm_dt_ids,
  439. },
  440. .probe = atmel_pwm_probe,
  441. };
  442. module_platform_driver(atmel_pwm_driver);
  443. MODULE_ALIAS("platform:atmel-pwm");
  444. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  445. MODULE_DESCRIPTION("Atmel PWM driver");
  446. MODULE_LICENSE("GPL v2");