pwm-lpss.c 6.6 KB

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