blob: 4df994f72d9604071f936e72b1f3e52dce5633d6 [file] [log] [blame]
Mika Westerbergd16a5aa2014-03-20 22:04:23 +08001/*
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>
Alan Cox093e00b2014-04-18 19:17:40 +08009 * Author: Alan Cox <alan@linux.intel.com>
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080010 *
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
16#include <linux/acpi.h>
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080017#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>
Alan Cox093e00b2014-04-18 19:17:40 +080022#include <linux/pci.h>
23
24static int pci_drv, plat_drv; /* So we know which drivers registered */
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080025
26#define PWM 0x00000000
27#define PWM_ENABLE BIT(31)
28#define PWM_SW_UPDATE BIT(30)
29#define PWM_BASE_UNIT_SHIFT 8
30#define PWM_BASE_UNIT_MASK 0x00ffff00
31#define PWM_ON_TIME_DIV_MASK 0x000000ff
32#define PWM_DIVISION_CORRECTION 0x2
33#define PWM_LIMIT (0x8000 + PWM_DIVISION_CORRECTION)
34#define NSECS_PER_SEC 1000000000UL
35
36struct pwm_lpss_chip {
37 struct pwm_chip chip;
38 void __iomem *regs;
Alan Cox093e00b2014-04-18 19:17:40 +080039 unsigned long clk_rate;
40};
41
42struct pwm_lpss_boardinfo {
43 unsigned long clk_rate;
44};
45
46/* BayTrail */
47static const struct pwm_lpss_boardinfo byt_info = {
48 25000000
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080049};
50
51static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
52{
53 return container_of(chip, struct pwm_lpss_chip, chip);
54}
55
56static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
57 int duty_ns, int period_ns)
58{
59 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
60 u8 on_time_div;
61 unsigned long c;
62 unsigned long long base_unit, freq = NSECS_PER_SEC;
63 u32 ctrl;
64
65 do_div(freq, period_ns);
66
67 /* The equation is: base_unit = ((freq / c) * 65536) + correction */
68 base_unit = freq * 65536;
69
Alan Cox093e00b2014-04-18 19:17:40 +080070 c = lpwm->clk_rate;
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080071 if (!c)
72 return -EINVAL;
73
74 do_div(base_unit, c);
75 base_unit += PWM_DIVISION_CORRECTION;
76 if (base_unit > PWM_LIMIT)
77 return -EINVAL;
78
79 if (duty_ns <= 0)
80 duty_ns = 1;
81 on_time_div = 255 - (255 * duty_ns / period_ns);
82
83 ctrl = readl(lpwm->regs + PWM);
84 ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
85 ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
86 ctrl |= on_time_div;
87 /* request PWM to update on next cycle */
88 ctrl |= PWM_SW_UPDATE;
89 writel(ctrl, lpwm->regs + PWM);
90
91 return 0;
92}
93
94static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
95{
96 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
97 u32 ctrl;
Mika Westerbergd16a5aa2014-03-20 22:04:23 +080098
99 ctrl = readl(lpwm->regs + PWM);
100 writel(ctrl | PWM_ENABLE, lpwm->regs + PWM);
101
102 return 0;
103}
104
105static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
106{
107 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
108 u32 ctrl;
109
110 ctrl = readl(lpwm->regs + PWM);
111 writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800112}
113
114static const struct pwm_ops pwm_lpss_ops = {
115 .config = pwm_lpss_config,
116 .enable = pwm_lpss_enable,
117 .disable = pwm_lpss_disable,
118 .owner = THIS_MODULE,
119};
120
Alan Cox093e00b2014-04-18 19:17:40 +0800121static struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev,
122 struct resource *r,
Thierry Reding89c03392014-05-07 10:27:57 +0200123 const struct pwm_lpss_boardinfo *info)
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800124{
125 struct pwm_lpss_chip *lpwm;
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800126 int ret;
127
Alan Cox093e00b2014-04-18 19:17:40 +0800128 lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800129 if (!lpwm)
Alan Cox093e00b2014-04-18 19:17:40 +0800130 return ERR_PTR(-ENOMEM);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800131
Alan Cox093e00b2014-04-18 19:17:40 +0800132 lpwm->regs = devm_ioremap_resource(dev, r);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800133 if (IS_ERR(lpwm->regs))
Thierry Reding89c03392014-05-07 10:27:57 +0200134 return ERR_CAST(lpwm->regs);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800135
Heikki Krogerus65accd82014-05-09 11:35:21 +0300136 lpwm->clk_rate = info->clk_rate;
Alan Cox093e00b2014-04-18 19:17:40 +0800137 lpwm->chip.dev = dev;
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800138 lpwm->chip.ops = &pwm_lpss_ops;
139 lpwm->chip.base = -1;
140 lpwm->chip.npwm = 1;
141
142 ret = pwmchip_add(&lpwm->chip);
143 if (ret) {
Alan Cox093e00b2014-04-18 19:17:40 +0800144 dev_err(dev, "failed to add PWM chip: %d\n", ret);
145 return ERR_PTR(ret);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800146 }
147
Alan Cox093e00b2014-04-18 19:17:40 +0800148 return lpwm;
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800149}
150
Alan Cox093e00b2014-04-18 19:17:40 +0800151static int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800152{
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800153 u32 ctrl;
154
155 ctrl = readl(lpwm->regs + PWM);
156 writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
157
158 return pwmchip_remove(&lpwm->chip);
159}
160
Alan Cox093e00b2014-04-18 19:17:40 +0800161static int pwm_lpss_probe_pci(struct pci_dev *pdev,
162 const struct pci_device_id *id)
163{
164 const struct pwm_lpss_boardinfo *info;
165 struct pwm_lpss_chip *lpwm;
166 int err;
167
168 err = pci_enable_device(pdev);
169 if (err < 0)
170 return err;
171
172 info = (struct pwm_lpss_boardinfo *)id->driver_data;
173 lpwm = pwm_lpss_probe(&pdev->dev, &pdev->resource[0], info);
174 if (IS_ERR(lpwm))
175 return PTR_ERR(lpwm);
176
177 pci_set_drvdata(pdev, lpwm);
178 return 0;
179}
180
181static void pwm_lpss_remove_pci(struct pci_dev *pdev)
182{
183 struct pwm_lpss_chip *lpwm = pci_get_drvdata(pdev);
184
185 pwm_lpss_remove(lpwm);
186 pci_disable_device(pdev);
187}
188
189static struct pci_device_id pwm_lpss_pci_ids[] = {
190 { PCI_VDEVICE(INTEL, 0x0f08), (unsigned long)&byt_info},
191 { PCI_VDEVICE(INTEL, 0x0f09), (unsigned long)&byt_info},
192 { },
193};
194MODULE_DEVICE_TABLE(pci, pwm_lpss_pci_ids);
195
196static struct pci_driver pwm_lpss_driver_pci = {
197 .name = "pwm-lpss",
198 .id_table = pwm_lpss_pci_ids,
199 .probe = pwm_lpss_probe_pci,
200 .remove = pwm_lpss_remove_pci,
201};
202
203static int pwm_lpss_probe_platform(struct platform_device *pdev)
204{
Heikki Krogerus65accd82014-05-09 11:35:21 +0300205 const struct pwm_lpss_boardinfo *info;
206 const struct acpi_device_id *id;
Alan Cox093e00b2014-04-18 19:17:40 +0800207 struct pwm_lpss_chip *lpwm;
208 struct resource *r;
209
Heikki Krogerus65accd82014-05-09 11:35:21 +0300210 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
211 if (!id)
212 return -ENODEV;
213
Alan Cox093e00b2014-04-18 19:17:40 +0800214 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
215
Heikki Krogerus65accd82014-05-09 11:35:21 +0300216 info = (struct pwm_lpss_boardinfo *)id->driver_data;
217 lpwm = pwm_lpss_probe(&pdev->dev, r, info);
Alan Cox093e00b2014-04-18 19:17:40 +0800218 if (IS_ERR(lpwm))
219 return PTR_ERR(lpwm);
220
221 platform_set_drvdata(pdev, lpwm);
222 return 0;
223}
224
225static int pwm_lpss_remove_platform(struct platform_device *pdev)
226{
227 struct pwm_lpss_chip *lpwm = platform_get_drvdata(pdev);
228
229 return pwm_lpss_remove(lpwm);
230}
231
232static const struct acpi_device_id pwm_lpss_acpi_match[] = {
Heikki Krogerus65accd82014-05-09 11:35:21 +0300233 { "80860F09", (unsigned long)&byt_info },
Alan Cox093e00b2014-04-18 19:17:40 +0800234 { },
235};
236MODULE_DEVICE_TABLE(acpi, pwm_lpss_acpi_match);
237
238static struct platform_driver pwm_lpss_driver_platform = {
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800239 .driver = {
240 .name = "pwm-lpss",
241 .acpi_match_table = pwm_lpss_acpi_match,
242 },
Alan Cox093e00b2014-04-18 19:17:40 +0800243 .probe = pwm_lpss_probe_platform,
244 .remove = pwm_lpss_remove_platform,
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800245};
Alan Cox093e00b2014-04-18 19:17:40 +0800246
247static int __init pwm_init(void)
248{
249 pci_drv = pci_register_driver(&pwm_lpss_driver_pci);
250 plat_drv = platform_driver_register(&pwm_lpss_driver_platform);
251 if (pci_drv && plat_drv)
252 return pci_drv;
253
254 return 0;
255}
256module_init(pwm_init);
257
258static void __exit pwm_exit(void)
259{
260 if (!pci_drv)
261 pci_unregister_driver(&pwm_lpss_driver_pci);
262 if (!plat_drv)
263 platform_driver_unregister(&pwm_lpss_driver_platform);
264}
265module_exit(pwm_exit);
Mika Westerbergd16a5aa2014-03-20 22:04:23 +0800266
267MODULE_DESCRIPTION("PWM driver for Intel LPSS");
268MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
269MODULE_LICENSE("GPL v2");
270MODULE_ALIAS("platform:pwm-lpss");