pwm-atmel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_chip {
  54. struct pwm_chip chip;
  55. struct clk *clk;
  56. void __iomem *base;
  57. unsigned int updated_pwms;
  58. /* ISR is cleared when read, ensure only one thread does that */
  59. struct mutex isr_lock;
  60. void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
  61. unsigned long dty, unsigned long prd);
  62. };
  63. static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
  64. {
  65. return container_of(chip, struct atmel_pwm_chip, chip);
  66. }
  67. static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
  68. unsigned long offset)
  69. {
  70. return readl_relaxed(chip->base + offset);
  71. }
  72. static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
  73. unsigned long offset, unsigned long val)
  74. {
  75. writel_relaxed(val, chip->base + offset);
  76. }
  77. static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
  78. unsigned int ch, unsigned long offset)
  79. {
  80. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  81. return readl_relaxed(chip->base + base + offset);
  82. }
  83. static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
  84. unsigned int ch, unsigned long offset,
  85. unsigned long val)
  86. {
  87. unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
  88. writel_relaxed(val, chip->base + base + offset);
  89. }
  90. static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  91. int duty_ns, int period_ns)
  92. {
  93. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  94. unsigned long prd, dty;
  95. unsigned long long div;
  96. unsigned int pres = 0;
  97. u32 val;
  98. int ret;
  99. if (pwm_is_enabled(pwm) && (period_ns != pwm_get_period(pwm))) {
  100. dev_err(chip->dev, "cannot change PWM period while enabled\n");
  101. return -EBUSY;
  102. }
  103. /* Calculate the period cycles and prescale value */
  104. div = (unsigned long long)clk_get_rate(atmel_pwm->clk) * period_ns;
  105. do_div(div, NSEC_PER_SEC);
  106. while (div > PWM_MAX_PRD) {
  107. div >>= 1;
  108. pres++;
  109. }
  110. if (pres > PRD_MAX_PRES) {
  111. dev_err(chip->dev, "pres exceeds the maximum value\n");
  112. return -EINVAL;
  113. }
  114. /* Calculate the duty cycles */
  115. prd = div;
  116. div *= duty_ns;
  117. do_div(div, period_ns);
  118. dty = prd - div;
  119. ret = clk_enable(atmel_pwm->clk);
  120. if (ret) {
  121. dev_err(chip->dev, "failed to enable PWM clock\n");
  122. return ret;
  123. }
  124. /* It is necessary to preserve CPOL, inside CMR */
  125. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  126. val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
  127. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  128. atmel_pwm->config(chip, pwm, dty, prd);
  129. mutex_lock(&atmel_pwm->isr_lock);
  130. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  131. atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
  132. mutex_unlock(&atmel_pwm->isr_lock);
  133. clk_disable(atmel_pwm->clk);
  134. return ret;
  135. }
  136. static void atmel_pwm_config_v1(struct pwm_chip *chip, struct pwm_device *pwm,
  137. unsigned long dty, unsigned long prd)
  138. {
  139. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  140. unsigned int val;
  141. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CUPD, dty);
  142. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  143. val &= ~PWM_CMR_UPD_CDTY;
  144. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  145. /*
  146. * If the PWM channel is enabled, only update CDTY by using the update
  147. * register, it needs to set bit 10 of CMR to 0
  148. */
  149. if (pwm_is_enabled(pwm))
  150. return;
  151. /*
  152. * If the PWM channel is disabled, write value to duty and period
  153. * registers directly.
  154. */
  155. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CDTY, dty);
  156. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CPRD, prd);
  157. }
  158. static void atmel_pwm_config_v2(struct pwm_chip *chip, struct pwm_device *pwm,
  159. unsigned long dty, unsigned long prd)
  160. {
  161. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  162. if (pwm_is_enabled(pwm)) {
  163. /*
  164. * If the PWM channel is enabled, using the duty update register
  165. * to update the value.
  166. */
  167. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CDTYUPD, dty);
  168. } else {
  169. /*
  170. * If the PWM channel is disabled, write value to duty and
  171. * period registers directly.
  172. */
  173. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CDTY, dty);
  174. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CPRD, prd);
  175. }
  176. }
  177. static int atmel_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  178. enum pwm_polarity polarity)
  179. {
  180. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  181. u32 val;
  182. int ret;
  183. val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
  184. if (polarity == PWM_POLARITY_NORMAL)
  185. val &= ~PWM_CMR_CPOL;
  186. else
  187. val |= PWM_CMR_CPOL;
  188. ret = clk_enable(atmel_pwm->clk);
  189. if (ret) {
  190. dev_err(chip->dev, "failed to enable PWM clock\n");
  191. return ret;
  192. }
  193. atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
  194. clk_disable(atmel_pwm->clk);
  195. return 0;
  196. }
  197. static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  198. {
  199. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  200. int ret;
  201. ret = clk_enable(atmel_pwm->clk);
  202. if (ret) {
  203. dev_err(chip->dev, "failed to enable PWM clock\n");
  204. return ret;
  205. }
  206. atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
  207. return 0;
  208. }
  209. static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  210. {
  211. struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
  212. unsigned long timeout = jiffies + 2 * HZ;
  213. /*
  214. * Wait for at least a complete period to have passed before disabling a
  215. * channel to be sure that CDTY has been updated
  216. */
  217. mutex_lock(&atmel_pwm->isr_lock);
  218. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  219. while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
  220. time_before(jiffies, timeout)) {
  221. usleep_range(10, 100);
  222. atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
  223. }
  224. mutex_unlock(&atmel_pwm->isr_lock);
  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. clk_disable(atmel_pwm->clk);
  235. }
  236. static const struct pwm_ops atmel_pwm_ops = {
  237. .config = atmel_pwm_config,
  238. .set_polarity = atmel_pwm_set_polarity,
  239. .enable = atmel_pwm_enable,
  240. .disable = atmel_pwm_disable,
  241. .owner = THIS_MODULE,
  242. };
  243. struct atmel_pwm_data {
  244. void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
  245. unsigned long dty, unsigned long prd);
  246. };
  247. static const struct atmel_pwm_data atmel_pwm_data_v1 = {
  248. .config = atmel_pwm_config_v1,
  249. };
  250. static const struct atmel_pwm_data atmel_pwm_data_v2 = {
  251. .config = atmel_pwm_config_v2,
  252. };
  253. static const struct platform_device_id atmel_pwm_devtypes[] = {
  254. {
  255. .name = "at91sam9rl-pwm",
  256. .driver_data = (kernel_ulong_t)&atmel_pwm_data_v1,
  257. }, {
  258. .name = "sama5d3-pwm",
  259. .driver_data = (kernel_ulong_t)&atmel_pwm_data_v2,
  260. }, {
  261. /* sentinel */
  262. },
  263. };
  264. MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes);
  265. static const struct of_device_id atmel_pwm_dt_ids[] = {
  266. {
  267. .compatible = "atmel,at91sam9rl-pwm",
  268. .data = &atmel_pwm_data_v1,
  269. }, {
  270. .compatible = "atmel,sama5d3-pwm",
  271. .data = &atmel_pwm_data_v2,
  272. }, {
  273. /* sentinel */
  274. },
  275. };
  276. MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
  277. static inline const struct atmel_pwm_data *
  278. atmel_pwm_get_driver_data(struct platform_device *pdev)
  279. {
  280. const struct platform_device_id *id;
  281. if (pdev->dev.of_node)
  282. return of_device_get_match_data(&pdev->dev);
  283. id = platform_get_device_id(pdev);
  284. return (struct atmel_pwm_data *)id->driver_data;
  285. }
  286. static int atmel_pwm_probe(struct platform_device *pdev)
  287. {
  288. const struct atmel_pwm_data *data;
  289. struct atmel_pwm_chip *atmel_pwm;
  290. struct resource *res;
  291. int ret;
  292. data = atmel_pwm_get_driver_data(pdev);
  293. if (!data)
  294. return -ENODEV;
  295. atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
  296. if (!atmel_pwm)
  297. return -ENOMEM;
  298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  299. atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
  300. if (IS_ERR(atmel_pwm->base))
  301. return PTR_ERR(atmel_pwm->base);
  302. atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
  303. if (IS_ERR(atmel_pwm->clk))
  304. return PTR_ERR(atmel_pwm->clk);
  305. ret = clk_prepare(atmel_pwm->clk);
  306. if (ret) {
  307. dev_err(&pdev->dev, "failed to prepare PWM clock\n");
  308. return ret;
  309. }
  310. atmel_pwm->chip.dev = &pdev->dev;
  311. atmel_pwm->chip.ops = &atmel_pwm_ops;
  312. if (pdev->dev.of_node) {
  313. atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  314. atmel_pwm->chip.of_pwm_n_cells = 3;
  315. }
  316. atmel_pwm->chip.base = -1;
  317. atmel_pwm->chip.npwm = 4;
  318. atmel_pwm->chip.can_sleep = true;
  319. atmel_pwm->config = data->config;
  320. atmel_pwm->updated_pwms = 0;
  321. mutex_init(&atmel_pwm->isr_lock);
  322. ret = pwmchip_add(&atmel_pwm->chip);
  323. if (ret < 0) {
  324. dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
  325. goto unprepare_clk;
  326. }
  327. platform_set_drvdata(pdev, atmel_pwm);
  328. return ret;
  329. unprepare_clk:
  330. clk_unprepare(atmel_pwm->clk);
  331. return ret;
  332. }
  333. static int atmel_pwm_remove(struct platform_device *pdev)
  334. {
  335. struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
  336. clk_unprepare(atmel_pwm->clk);
  337. mutex_destroy(&atmel_pwm->isr_lock);
  338. return pwmchip_remove(&atmel_pwm->chip);
  339. }
  340. static struct platform_driver atmel_pwm_driver = {
  341. .driver = {
  342. .name = "atmel-pwm",
  343. .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
  344. },
  345. .id_table = atmel_pwm_devtypes,
  346. .probe = atmel_pwm_probe,
  347. .remove = atmel_pwm_remove,
  348. };
  349. module_platform_driver(atmel_pwm_driver);
  350. MODULE_ALIAS("platform:atmel-pwm");
  351. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  352. MODULE_DESCRIPTION("Atmel PWM driver");
  353. MODULE_LICENSE("GPL v2");