pwm-lpss.c 6.4 KB

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