pwm-atmel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. /* Protects .update_pending */
  83. spinlock_t lock;
  84. };
  85. static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
  86. {
  87. return pwmchip_get_drvdata(chip);
  88. }
  89. static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
  90. unsigned long offset)
  91. {
  92. return readl_relaxed(chip->base + offset);
  93. }
  94. static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
  95. unsigned long offset, unsigned long val)
  96. {
  97. writel_relaxed(val, chip->base + offset);
  98. }
  99. static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
  100. unsigned int ch, unsigned long offset)
  101. {
  102. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  103. return atmel_pwm_readl(chip, base + offset);
  104. }
  105. static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
  106. unsigned int ch, unsigned long offset,
  107. unsigned long val)
  108. {
  109. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  110. atmel_pwm_writel(chip, base + offset, val);
  111. }
  112. static void atmel_pwm_update_pending(struct atmel_pwm_chip *chip)
  113. {
  114. /*
  115. * Each channel that has its bit in ISR set started a new period since
  116. * ISR was cleared and so there is no more update pending. Note that
  117. * reading ISR clears it, so this needs to handle all channels to not
  118. * loose information.
  119. */
  120. u32 isr = atmel_pwm_readl(chip, PWM_ISR);
  121. chip->update_pending &= ~isr;
  122. }
  123. static void atmel_pwm_set_pending(struct atmel_pwm_chip *chip, unsigned int ch)
  124. {
  125. spin_lock(&chip->lock);
  126. /*
  127. * Clear pending flags in hardware because otherwise there might still
  128. * be a stale flag in ISR.
  129. */
  130. atmel_pwm_update_pending(chip);
  131. chip->update_pending |= (1 << ch);
  132. spin_unlock(&chip->lock);
  133. }
  134. static int atmel_pwm_test_pending(struct atmel_pwm_chip *chip, unsigned int ch)
  135. {
  136. int ret = 0;
  137. spin_lock(&chip->lock);
  138. if (chip->update_pending & (1 << ch)) {
  139. atmel_pwm_update_pending(chip);
  140. if (chip->update_pending & (1 << ch))
  141. ret = 1;
  142. }
  143. spin_unlock(&chip->lock);
  144. return ret;
  145. }
  146. static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip *chip, unsigned int ch)
  147. {
  148. unsigned long timeout = jiffies + 2 * HZ;
  149. int ret;
  150. while ((ret = atmel_pwm_test_pending(chip, ch)) &&
  151. time_before(jiffies, timeout))
  152. usleep_range(10, 100);
  153. return ret ? -ETIMEDOUT : 0;
  154. }
  155. static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
  156. unsigned long clkrate,
  157. const struct pwm_state *state,
  158. unsigned long *cprd, u32 *pres)
  159. {
  160. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  161. unsigned long long cycles = state->period;
  162. int shift;
  163. /* Calculate the period cycles and prescale value */
  164. cycles *= clkrate;
  165. do_div(cycles, NSEC_PER_SEC);
  166. /*
  167. * The register for the period length is cfg.period_bits bits wide.
  168. * So for each bit the number of clock cycles is wider divide the input
  169. * clock frequency by two using pres and shift cprd accordingly.
  170. */
  171. shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
  172. if (shift > PWM_MAX_PRES) {
  173. dev_err(pwmchip_parent(chip), "pres exceeds the maximum value\n");
  174. return -EINVAL;
  175. } else if (shift > 0) {
  176. *pres = shift;
  177. cycles >>= *pres;
  178. } else {
  179. *pres = 0;
  180. }
  181. *cprd = cycles;
  182. return 0;
  183. }
  184. static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
  185. unsigned long clkrate, unsigned long cprd,
  186. u32 pres, unsigned long *cdty)
  187. {
  188. unsigned long long cycles = state->duty_cycle;
  189. cycles *= clkrate;
  190. do_div(cycles, NSEC_PER_SEC);
  191. cycles >>= pres;
  192. *cdty = cprd - cycles;
  193. }
  194. static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
  195. unsigned long cdty)
  196. {
  197. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  198. u32 val;
  199. if (atmel_pwm->data->regs.duty_upd ==
  200. atmel_pwm->data->regs.period_upd) {
  201. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  202. val &= ~PWM_CMR_UPD_CDTY;
  203. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  204. }
  205. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  206. atmel_pwm->data->regs.duty_upd, cdty);
  207. atmel_pwm_set_pending(atmel_pwm, pwm->hwpwm);
  208. }
  209. static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
  210. struct pwm_device *pwm,
  211. unsigned long cprd, unsigned long cdty)
  212. {
  213. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  214. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  215. atmel_pwm->data->regs.duty, cdty);
  216. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  217. atmel_pwm->data->regs.period, cprd);
  218. }
  219. static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
  220. bool disable_clk)
  221. {
  222. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  223. unsigned long timeout;
  224. atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
  225. atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
  226. /*
  227. * Wait for the PWM channel disable operation to be effective before
  228. * stopping the clock.
  229. */
  230. timeout = jiffies + 2 * HZ;
  231. while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
  232. time_before(jiffies, timeout))
  233. usleep_range(10, 100);
  234. if (disable_clk)
  235. clk_disable(atmel_pwm->clk);
  236. }
  237. static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  238. const struct pwm_state *state)
  239. {
  240. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  241. unsigned long cprd, cdty;
  242. u32 pres, val;
  243. int ret;
  244. if (state->enabled) {
  245. unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
  246. if (pwm->state.enabled &&
  247. pwm->state.polarity == state->polarity &&
  248. pwm->state.period == state->period) {
  249. u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  250. cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  251. atmel_pwm->data->regs.period);
  252. pres = cmr & PWM_CMR_CPRE_MSK;
  253. atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
  254. atmel_pwm_update_cdty(chip, pwm, cdty);
  255. return 0;
  256. }
  257. ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
  258. &pres);
  259. if (ret) {
  260. dev_err(pwmchip_parent(chip),
  261. "failed to calculate cprd and prescaler\n");
  262. return ret;
  263. }
  264. atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
  265. if (pwm->state.enabled) {
  266. atmel_pwm_disable(chip, pwm, false);
  267. } else {
  268. ret = clk_enable(atmel_pwm->clk);
  269. if (ret) {
  270. dev_err(pwmchip_parent(chip), "failed to enable clock\n");
  271. return ret;
  272. }
  273. }
  274. /* It is necessary to preserve CPOL, inside CMR */
  275. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  276. val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
  277. if (state->polarity == PWM_POLARITY_NORMAL)
  278. val &= ~PWM_CMR_CPOL;
  279. else
  280. val |= PWM_CMR_CPOL;
  281. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  282. atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
  283. atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
  284. } else if (pwm->state.enabled) {
  285. atmel_pwm_disable(chip, pwm, true);
  286. }
  287. return 0;
  288. }
  289. static int atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  290. struct pwm_state *state)
  291. {
  292. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  293. u32 sr, cmr;
  294. sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
  295. cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  296. if (sr & (1 << pwm->hwpwm)) {
  297. unsigned long rate = clk_get_rate(atmel_pwm->clk);
  298. u32 cdty, cprd, pres;
  299. u64 tmp;
  300. pres = cmr & PWM_CMR_CPRE_MSK;
  301. cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  302. atmel_pwm->data->regs.period);
  303. tmp = (u64)cprd * NSEC_PER_SEC;
  304. tmp <<= pres;
  305. state->period = DIV64_U64_ROUND_UP(tmp, rate);
  306. /* Wait for an updated duty_cycle queued in hardware */
  307. atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
  308. cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  309. atmel_pwm->data->regs.duty);
  310. tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
  311. tmp <<= pres;
  312. state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
  313. state->enabled = true;
  314. } else {
  315. state->enabled = false;
  316. }
  317. if (cmr & PWM_CMR_CPOL)
  318. state->polarity = PWM_POLARITY_INVERSED;
  319. else
  320. state->polarity = PWM_POLARITY_NORMAL;
  321. return 0;
  322. }
  323. static const struct pwm_ops atmel_pwm_ops = {
  324. .apply = atmel_pwm_apply,
  325. .get_state = atmel_pwm_get_state,
  326. };
  327. static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
  328. .regs = {
  329. .period = PWMV1_CPRD,
  330. .period_upd = PWMV1_CUPD,
  331. .duty = PWMV1_CDTY,
  332. .duty_upd = PWMV1_CUPD,
  333. },
  334. .cfg = {
  335. /* 16 bits to keep period and duty. */
  336. .period_bits = 16,
  337. },
  338. };
  339. static const struct atmel_pwm_data atmel_sama5_pwm_data = {
  340. .regs = {
  341. .period = PWMV2_CPRD,
  342. .period_upd = PWMV2_CPRDUPD,
  343. .duty = PWMV2_CDTY,
  344. .duty_upd = PWMV2_CDTYUPD,
  345. },
  346. .cfg = {
  347. /* 16 bits to keep period and duty. */
  348. .period_bits = 16,
  349. },
  350. };
  351. static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
  352. .regs = {
  353. .period = PWMV1_CPRD,
  354. .period_upd = PWMV1_CUPD,
  355. .duty = PWMV1_CDTY,
  356. .duty_upd = PWMV1_CUPD,
  357. },
  358. .cfg = {
  359. /* 32 bits to keep period and duty. */
  360. .period_bits = 32,
  361. },
  362. };
  363. static const struct of_device_id atmel_pwm_dt_ids[] = {
  364. {
  365. .compatible = "atmel,at91sam9rl-pwm",
  366. .data = &atmel_sam9rl_pwm_data,
  367. }, {
  368. .compatible = "atmel,sama5d3-pwm",
  369. .data = &atmel_sama5_pwm_data,
  370. }, {
  371. .compatible = "atmel,sama5d2-pwm",
  372. .data = &atmel_sama5_pwm_data,
  373. }, {
  374. .compatible = "microchip,sam9x60-pwm",
  375. .data = &mchp_sam9x60_pwm_data,
  376. }, {
  377. /* sentinel */
  378. },
  379. };
  380. MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
  381. static int atmel_pwm_enable_clk_if_on(struct pwm_chip *chip, bool on)
  382. {
  383. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  384. unsigned int i, cnt = 0;
  385. unsigned long sr;
  386. int ret = 0;
  387. sr = atmel_pwm_readl(atmel_pwm, PWM_SR) & PWM_SR_ALL_CH_MASK;
  388. if (!sr)
  389. return 0;
  390. cnt = bitmap_weight(&sr, chip->npwm);
  391. if (!on)
  392. goto disable_clk;
  393. for (i = 0; i < cnt; i++) {
  394. ret = clk_enable(atmel_pwm->clk);
  395. if (ret) {
  396. dev_err(pwmchip_parent(chip),
  397. "failed to enable clock for pwm %pe\n",
  398. ERR_PTR(ret));
  399. cnt = i;
  400. goto disable_clk;
  401. }
  402. }
  403. return 0;
  404. disable_clk:
  405. while (cnt--)
  406. clk_disable(atmel_pwm->clk);
  407. return ret;
  408. }
  409. static int atmel_pwm_probe(struct platform_device *pdev)
  410. {
  411. struct atmel_pwm_chip *atmel_pwm;
  412. struct pwm_chip *chip;
  413. int ret;
  414. chip = devm_pwmchip_alloc(&pdev->dev, 4, sizeof(*atmel_pwm));
  415. if (IS_ERR(chip))
  416. return PTR_ERR(chip);
  417. atmel_pwm = to_atmel_pwm_chip(chip);
  418. atmel_pwm->data = of_device_get_match_data(&pdev->dev);
  419. atmel_pwm->update_pending = 0;
  420. spin_lock_init(&atmel_pwm->lock);
  421. atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
  422. if (IS_ERR(atmel_pwm->base))
  423. return PTR_ERR(atmel_pwm->base);
  424. atmel_pwm->clk = devm_clk_get_prepared(&pdev->dev, NULL);
  425. if (IS_ERR(atmel_pwm->clk))
  426. return dev_err_probe(&pdev->dev, PTR_ERR(atmel_pwm->clk),
  427. "failed to get prepared PWM clock\n");
  428. chip->ops = &atmel_pwm_ops;
  429. ret = atmel_pwm_enable_clk_if_on(chip, true);
  430. if (ret < 0)
  431. return ret;
  432. ret = devm_pwmchip_add(&pdev->dev, chip);
  433. if (ret < 0) {
  434. dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  435. goto disable_clk;
  436. }
  437. return 0;
  438. disable_clk:
  439. atmel_pwm_enable_clk_if_on(chip, false);
  440. return ret;
  441. }
  442. static struct platform_driver atmel_pwm_driver = {
  443. .driver = {
  444. .name = "atmel-pwm",
  445. .of_match_table = atmel_pwm_dt_ids,
  446. },
  447. .probe = atmel_pwm_probe,
  448. };
  449. module_platform_driver(atmel_pwm_driver);
  450. MODULE_ALIAS("platform:atmel-pwm");
  451. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  452. MODULE_DESCRIPTION("Atmel PWM driver");
  453. MODULE_LICENSE("GPL v2");