pwm-atmel.c 13 KB

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