pwm-lpss.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Intel Low Power Subsystem PWM controller driver
  3. *
  4. * Copyright (C) 2014, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. * Author: Chew Kean Ho <kean.ho.chew@intel.com>
  7. * Author: Chang Rebecca Swee Fun <rebecca.swee.fun.chang@intel.com>
  8. * Author: Chew Chiau Ee <chiau.ee.chew@intel.com>
  9. * Author: Alan Cox <alan@linux.intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/acpi.h>
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pwm.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pci.h>
  23. static int pci_drv, plat_drv; /* So we know which drivers registered */
  24. #define PWM 0x00000000
  25. #define PWM_ENABLE BIT(31)
  26. #define PWM_SW_UPDATE BIT(30)
  27. #define PWM_BASE_UNIT_SHIFT 8
  28. #define PWM_BASE_UNIT_MASK 0x00ffff00
  29. #define PWM_ON_TIME_DIV_MASK 0x000000ff
  30. #define PWM_DIVISION_CORRECTION 0x2
  31. #define PWM_LIMIT (0x8000 + PWM_DIVISION_CORRECTION)
  32. #define NSECS_PER_SEC 1000000000UL
  33. struct pwm_lpss_chip {
  34. struct pwm_chip chip;
  35. void __iomem *regs;
  36. struct clk *clk;
  37. unsigned long clk_rate;
  38. };
  39. struct pwm_lpss_boardinfo {
  40. unsigned long clk_rate;
  41. };
  42. /* BayTrail */
  43. static const struct pwm_lpss_boardinfo byt_info = {
  44. 25000000
  45. };
  46. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  47. {
  48. return container_of(chip, struct pwm_lpss_chip, chip);
  49. }
  50. static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
  51. int duty_ns, int period_ns)
  52. {
  53. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  54. u8 on_time_div;
  55. unsigned long c;
  56. unsigned long long base_unit, freq = NSECS_PER_SEC;
  57. u32 ctrl;
  58. do_div(freq, period_ns);
  59. /* The equation is: base_unit = ((freq / c) * 65536) + correction */
  60. base_unit = freq * 65536;
  61. c = lpwm->clk_rate;
  62. if (!c)
  63. return -EINVAL;
  64. do_div(base_unit, c);
  65. base_unit += PWM_DIVISION_CORRECTION;
  66. if (base_unit > PWM_LIMIT)
  67. return -EINVAL;
  68. if (duty_ns <= 0)
  69. duty_ns = 1;
  70. on_time_div = 255 - (255 * duty_ns / period_ns);
  71. ctrl = readl(lpwm->regs + PWM);
  72. ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
  73. ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
  74. ctrl |= on_time_div;
  75. /* request PWM to update on next cycle */
  76. ctrl |= PWM_SW_UPDATE;
  77. writel(ctrl, lpwm->regs + PWM);
  78. return 0;
  79. }
  80. static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  81. {
  82. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  83. u32 ctrl;
  84. int ret;
  85. ret = clk_prepare_enable(lpwm->clk);
  86. if (ret)
  87. return ret;
  88. ctrl = readl(lpwm->regs + PWM);
  89. writel(ctrl | PWM_ENABLE, lpwm->regs + PWM);
  90. return 0;
  91. }
  92. static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  93. {
  94. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  95. u32 ctrl;
  96. ctrl = readl(lpwm->regs + PWM);
  97. writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
  98. clk_disable_unprepare(lpwm->clk);
  99. }
  100. static const struct pwm_ops pwm_lpss_ops = {
  101. .config = pwm_lpss_config,
  102. .enable = pwm_lpss_enable,
  103. .disable = pwm_lpss_disable,
  104. .owner = THIS_MODULE,
  105. };
  106. static struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev,
  107. struct resource *r,
  108. const struct pwm_lpss_boardinfo *info)
  109. {
  110. struct pwm_lpss_chip *lpwm;
  111. int ret;
  112. lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
  113. if (!lpwm)
  114. return ERR_PTR(-ENOMEM);
  115. lpwm->regs = devm_ioremap_resource(dev, r);
  116. if (IS_ERR(lpwm->regs))
  117. return ERR_CAST(lpwm->regs);
  118. if (info) {
  119. lpwm->clk_rate = info->clk_rate;
  120. } else {
  121. lpwm->clk = devm_clk_get(dev, NULL);
  122. if (IS_ERR(lpwm->clk)) {
  123. dev_err(dev, "failed to get PWM clock\n");
  124. return ERR_CAST(lpwm->clk);
  125. }
  126. lpwm->clk_rate = clk_get_rate(lpwm->clk);
  127. }
  128. lpwm->chip.dev = dev;
  129. lpwm->chip.ops = &pwm_lpss_ops;
  130. lpwm->chip.base = -1;
  131. lpwm->chip.npwm = 1;
  132. ret = pwmchip_add(&lpwm->chip);
  133. if (ret) {
  134. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  135. return ERR_PTR(ret);
  136. }
  137. return lpwm;
  138. }
  139. static int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
  140. {
  141. u32 ctrl;
  142. ctrl = readl(lpwm->regs + PWM);
  143. writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
  144. return pwmchip_remove(&lpwm->chip);
  145. }
  146. static int pwm_lpss_probe_pci(struct pci_dev *pdev,
  147. const struct pci_device_id *id)
  148. {
  149. const struct pwm_lpss_boardinfo *info;
  150. struct pwm_lpss_chip *lpwm;
  151. int err;
  152. err = pci_enable_device(pdev);
  153. if (err < 0)
  154. return err;
  155. info = (struct pwm_lpss_boardinfo *)id->driver_data;
  156. lpwm = pwm_lpss_probe(&pdev->dev, &pdev->resource[0], info);
  157. if (IS_ERR(lpwm))
  158. return PTR_ERR(lpwm);
  159. pci_set_drvdata(pdev, lpwm);
  160. return 0;
  161. }
  162. static void pwm_lpss_remove_pci(struct pci_dev *pdev)
  163. {
  164. struct pwm_lpss_chip *lpwm = pci_get_drvdata(pdev);
  165. pwm_lpss_remove(lpwm);
  166. pci_disable_device(pdev);
  167. }
  168. static struct pci_device_id pwm_lpss_pci_ids[] = {
  169. { PCI_VDEVICE(INTEL, 0x0f08), (unsigned long)&byt_info},
  170. { PCI_VDEVICE(INTEL, 0x0f09), (unsigned long)&byt_info},
  171. { },
  172. };
  173. MODULE_DEVICE_TABLE(pci, pwm_lpss_pci_ids);
  174. static struct pci_driver pwm_lpss_driver_pci = {
  175. .name = "pwm-lpss",
  176. .id_table = pwm_lpss_pci_ids,
  177. .probe = pwm_lpss_probe_pci,
  178. .remove = pwm_lpss_remove_pci,
  179. };
  180. static int pwm_lpss_probe_platform(struct platform_device *pdev)
  181. {
  182. struct pwm_lpss_chip *lpwm;
  183. struct resource *r;
  184. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. lpwm = pwm_lpss_probe(&pdev->dev, r, NULL);
  186. if (IS_ERR(lpwm))
  187. return PTR_ERR(lpwm);
  188. platform_set_drvdata(pdev, lpwm);
  189. return 0;
  190. }
  191. static int pwm_lpss_remove_platform(struct platform_device *pdev)
  192. {
  193. struct pwm_lpss_chip *lpwm = platform_get_drvdata(pdev);
  194. return pwm_lpss_remove(lpwm);
  195. }
  196. static const struct acpi_device_id pwm_lpss_acpi_match[] = {
  197. { "80860F09", 0 },
  198. { },
  199. };
  200. MODULE_DEVICE_TABLE(acpi, pwm_lpss_acpi_match);
  201. static struct platform_driver pwm_lpss_driver_platform = {
  202. .driver = {
  203. .name = "pwm-lpss",
  204. .acpi_match_table = pwm_lpss_acpi_match,
  205. },
  206. .probe = pwm_lpss_probe_platform,
  207. .remove = pwm_lpss_remove_platform,
  208. };
  209. static int __init pwm_init(void)
  210. {
  211. pci_drv = pci_register_driver(&pwm_lpss_driver_pci);
  212. plat_drv = platform_driver_register(&pwm_lpss_driver_platform);
  213. if (pci_drv && plat_drv)
  214. return pci_drv;
  215. return 0;
  216. }
  217. module_init(pwm_init);
  218. static void __exit pwm_exit(void)
  219. {
  220. if (!pci_drv)
  221. pci_unregister_driver(&pwm_lpss_driver_pci);
  222. if (!plat_drv)
  223. platform_driver_unregister(&pwm_lpss_driver_platform);
  224. }
  225. module_exit(pwm_exit);
  226. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  227. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  228. MODULE_LICENSE("GPL v2");
  229. MODULE_ALIAS("platform:pwm-lpss");