pwm-atmel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Driver for Atmel Pulse Width Modulation Controller
  3. *
  4. * Copyright (C) 2013 Atmel Corporation
  5. * Bo Shen <voice.shen@atmel.com>
  6. *
  7. * Licensed under GPLv2.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. /* The following is global registers for PWM controller */
  21. #define PWM_ENA 0x04
  22. #define PWM_DIS 0x08
  23. #define PWM_SR 0x0C
  24. #define PWM_ISR 0x1C
  25. /* Bit field in SR */
  26. #define PWM_SR_ALL_CH_ON 0x0F
  27. /* The following register is PWM channel related registers */
  28. #define PWM_CH_REG_OFFSET 0x200
  29. #define PWM_CH_REG_SIZE 0x20
  30. #define PWM_CMR 0x0
  31. /* Bit field in CMR */
  32. #define PWM_CMR_CPOL (1 << 9)
  33. #define PWM_CMR_UPD_CDTY (1 << 10)
  34. #define PWM_CMR_CPRE_MSK 0xF
  35. /* The following registers for PWM v1 */
  36. #define PWMV1_CDTY 0x04
  37. #define PWMV1_CPRD 0x08
  38. #define PWMV1_CUPD 0x10
  39. /* The following registers for PWM v2 */
  40. #define PWMV2_CDTY 0x04
  41. #define PWMV2_CDTYUPD 0x08
  42. #define PWMV2_CPRD 0x0C
  43. #define PWMV2_CPRDUPD 0x10
  44. /*
  45. * Max value for duty and period
  46. *
  47. * Although the duty and period register is 32 bit,
  48. * however only the LSB 16 bits are significant.
  49. */
  50. #define PWM_MAX_DTY 0xFFFF
  51. #define PWM_MAX_PRD 0xFFFF
  52. #define PRD_MAX_PRES 10
  53. struct atmel_pwm_registers {
  54. u8 period;
  55. u8 period_upd;
  56. u8 duty;
  57. u8 duty_upd;
  58. };
  59. struct atmel_pwm_chip {
  60. struct pwm_chip chip;
  61. struct clk *clk;
  62. void __iomem *base;
  63. const struct atmel_pwm_registers *regs;
  64. unsigned int updated_pwms;
  65. /* ISR is cleared when read, ensure only one thread does that */
  66. struct mutex isr_lock;
  67. };
  68. static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
  69. {
  70. return container_of(chip, struct atmel_pwm_chip, chip);
  71. }
  72. static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
  73. unsigned long offset)
  74. {
  75. return readl_relaxed(chip->base + offset);
  76. }
  77. static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
  78. unsigned long offset, unsigned long val)
  79. {
  80. writel_relaxed(val, chip->base + offset);
  81. }
  82. static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
  83. unsigned int ch, unsigned long offset)
  84. {
  85. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  86. return readl_relaxed(chip->base + base + offset);
  87. }
  88. static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
  89. unsigned int ch, unsigned long offset,
  90. unsigned long val)
  91. {
  92. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  93. writel_relaxed(val, chip->base + base + offset);
  94. }
  95. static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
  96. const struct pwm_state *state,
  97. unsigned long *cprd, u32 *pres)
  98. {
  99. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  100. unsigned long long cycles = state->period;
  101. /* Calculate the period cycles and prescale value */
  102. cycles *= clk_get_rate(atmel_pwm->clk);
  103. do_div(cycles, NSEC_PER_SEC);
  104. for (*pres = 0; cycles > PWM_MAX_PRD; cycles >>= 1)
  105. (*pres)++;
  106. if (*pres > PRD_MAX_PRES) {
  107. dev_err(chip->dev, "pres exceeds the maximum value\n");
  108. return -EINVAL;
  109. }
  110. *cprd = cycles;
  111. return 0;
  112. }
  113. static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
  114. unsigned long cprd, unsigned long *cdty)
  115. {
  116. unsigned long long cycles = state->duty_cycle;
  117. cycles *= cprd;
  118. do_div(cycles, state->period);
  119. *cdty = cprd - cycles;
  120. }
  121. static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
  122. unsigned long cdty)
  123. {
  124. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  125. u32 val;
  126. if (atmel_pwm->regs->duty_upd ==
  127. atmel_pwm->regs->period_upd) {
  128. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  129. val &= ~PWM_CMR_UPD_CDTY;
  130. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  131. }
  132. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  133. atmel_pwm->regs->duty_upd, cdty);
  134. }
  135. static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
  136. struct pwm_device *pwm,
  137. unsigned long cprd, unsigned long cdty)
  138. {
  139. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  140. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  141. atmel_pwm->regs->duty, cdty);
  142. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
  143. atmel_pwm->regs->period, cprd);
  144. }
  145. static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
  146. bool disable_clk)
  147. {
  148. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  149. unsigned long timeout = jiffies + 2 * HZ;
  150. /*
  151. * Wait for at least a complete period to have passed before disabling a
  152. * channel to be sure that CDTY has been updated
  153. */
  154. mutex_lock(&atmel_pwm->isr_lock);
  155. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  156. while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
  157. time_before(jiffies, timeout)) {
  158. usleep_range(10, 100);
  159. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  160. }
  161. mutex_unlock(&atmel_pwm->isr_lock);
  162. atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
  163. /*
  164. * Wait for the PWM channel disable operation to be effective before
  165. * stopping the clock.
  166. */
  167. timeout = jiffies + 2 * HZ;
  168. while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
  169. time_before(jiffies, timeout))
  170. usleep_range(10, 100);
  171. if (disable_clk)
  172. clk_disable(atmel_pwm->clk);
  173. }
  174. static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  175. struct pwm_state *state)
  176. {
  177. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  178. struct pwm_state cstate;
  179. unsigned long cprd, cdty;
  180. u32 pres, val;
  181. int ret;
  182. pwm_get_state(pwm, &cstate);
  183. if (state->enabled) {
  184. if (cstate.enabled &&
  185. cstate.polarity == state->polarity &&
  186. cstate.period == state->period) {
  187. cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
  188. atmel_pwm->regs->period);
  189. atmel_pwm_calculate_cdty(state, cprd, &cdty);
  190. atmel_pwm_update_cdty(chip, pwm, cdty);
  191. return 0;
  192. }
  193. ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd,
  194. &pres);
  195. if (ret) {
  196. dev_err(chip->dev,
  197. "failed to calculate cprd and prescaler\n");
  198. return ret;
  199. }
  200. atmel_pwm_calculate_cdty(state, cprd, &cdty);
  201. if (cstate.enabled) {
  202. atmel_pwm_disable(chip, pwm, false);
  203. } else {
  204. ret = clk_enable(atmel_pwm->clk);
  205. if (ret) {
  206. dev_err(chip->dev, "failed to enable clock\n");
  207. return ret;
  208. }
  209. }
  210. /* It is necessary to preserve CPOL, inside CMR */
  211. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  212. val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
  213. if (state->polarity == PWM_POLARITY_NORMAL)
  214. val &= ~PWM_CMR_CPOL;
  215. else
  216. val |= PWM_CMR_CPOL;
  217. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  218. atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
  219. mutex_lock(&atmel_pwm->isr_lock);
  220. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  221. atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
  222. mutex_unlock(&atmel_pwm->isr_lock);
  223. atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
  224. } else if (cstate.enabled) {
  225. atmel_pwm_disable(chip, pwm, true);
  226. }
  227. return 0;
  228. }
  229. static const struct pwm_ops atmel_pwm_ops = {
  230. .apply = atmel_pwm_apply,
  231. .owner = THIS_MODULE,
  232. };
  233. static const struct atmel_pwm_registers atmel_pwm_regs_v1 = {
  234. .period = PWMV1_CPRD,
  235. .period_upd = PWMV1_CUPD,
  236. .duty = PWMV1_CDTY,
  237. .duty_upd = PWMV1_CUPD,
  238. };
  239. static const struct atmel_pwm_registers atmel_pwm_regs_v2 = {
  240. .period = PWMV2_CPRD,
  241. .period_upd = PWMV2_CPRDUPD,
  242. .duty = PWMV2_CDTY,
  243. .duty_upd = PWMV2_CDTYUPD,
  244. };
  245. static const struct platform_device_id atmel_pwm_devtypes[] = {
  246. {
  247. .name = "at91sam9rl-pwm",
  248. .driver_data = (kernel_ulong_t)&atmel_pwm_regs_v1,
  249. }, {
  250. .name = "sama5d3-pwm",
  251. .driver_data = (kernel_ulong_t)&atmel_pwm_regs_v2,
  252. }, {
  253. /* sentinel */
  254. },
  255. };
  256. MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes);
  257. static const struct of_device_id atmel_pwm_dt_ids[] = {
  258. {
  259. .compatible = "atmel,at91sam9rl-pwm",
  260. .data = &atmel_pwm_regs_v1,
  261. }, {
  262. .compatible = "atmel,sama5d3-pwm",
  263. .data = &atmel_pwm_regs_v2,
  264. }, {
  265. .compatible = "atmel,sama5d2-pwm",
  266. .data = &atmel_pwm_regs_v2,
  267. }, {
  268. /* sentinel */
  269. },
  270. };
  271. MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
  272. static inline const struct atmel_pwm_registers *
  273. atmel_pwm_get_driver_data(struct platform_device *pdev)
  274. {
  275. const struct platform_device_id *id;
  276. if (pdev->dev.of_node)
  277. return of_device_get_match_data(&pdev->dev);
  278. id = platform_get_device_id(pdev);
  279. return (struct atmel_pwm_registers *)id->driver_data;
  280. }
  281. static int atmel_pwm_probe(struct platform_device *pdev)
  282. {
  283. const struct atmel_pwm_registers *regs;
  284. struct atmel_pwm_chip *atmel_pwm;
  285. struct resource *res;
  286. int ret;
  287. regs = atmel_pwm_get_driver_data(pdev);
  288. if (!regs)
  289. return -ENODEV;
  290. atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
  291. if (!atmel_pwm)
  292. return -ENOMEM;
  293. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  294. atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
  295. if (IS_ERR(atmel_pwm->base))
  296. return PTR_ERR(atmel_pwm->base);
  297. atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
  298. if (IS_ERR(atmel_pwm->clk))
  299. return PTR_ERR(atmel_pwm->clk);
  300. ret = clk_prepare(atmel_pwm->clk);
  301. if (ret) {
  302. dev_err(&pdev->dev, "failed to prepare PWM clock\n");
  303. return ret;
  304. }
  305. atmel_pwm->chip.dev = &pdev->dev;
  306. atmel_pwm->chip.ops = &atmel_pwm_ops;
  307. if (pdev->dev.of_node) {
  308. atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  309. atmel_pwm->chip.of_pwm_n_cells = 3;
  310. }
  311. atmel_pwm->chip.base = -1;
  312. atmel_pwm->chip.npwm = 4;
  313. atmel_pwm->regs = regs;
  314. atmel_pwm->updated_pwms = 0;
  315. mutex_init(&atmel_pwm->isr_lock);
  316. ret = pwmchip_add(&atmel_pwm->chip);
  317. if (ret < 0) {
  318. dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
  319. goto unprepare_clk;
  320. }
  321. platform_set_drvdata(pdev, atmel_pwm);
  322. return ret;
  323. unprepare_clk:
  324. clk_unprepare(atmel_pwm->clk);
  325. return ret;
  326. }
  327. static int atmel_pwm_remove(struct platform_device *pdev)
  328. {
  329. struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
  330. clk_unprepare(atmel_pwm->clk);
  331. mutex_destroy(&atmel_pwm->isr_lock);
  332. return pwmchip_remove(&atmel_pwm->chip);
  333. }
  334. static struct platform_driver atmel_pwm_driver = {
  335. .driver = {
  336. .name = "atmel-pwm",
  337. .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
  338. },
  339. .id_table = atmel_pwm_devtypes,
  340. .probe = atmel_pwm_probe,
  341. .remove = atmel_pwm_remove,
  342. };
  343. module_platform_driver(atmel_pwm_driver);
  344. MODULE_ALIAS("platform:atmel-pwm");
  345. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  346. MODULE_DESCRIPTION("Atmel PWM driver");
  347. MODULE_LICENSE("GPL v2");