2 * Intel Low Power Subsystem PWM controller driver
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>
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.
16 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/time.h>
25 #define PWM 0x00000000
26 #define PWM_ENABLE BIT(31)
27 #define PWM_SW_UPDATE BIT(30)
28 #define PWM_BASE_UNIT_SHIFT 8
29 #define PWM_ON_TIME_DIV_MASK 0x000000ff
31 /* Size of each PWM register space if multiple */
32 #define PWM_SIZE 0x400
36 struct pwm_lpss_chip {
39 const struct pwm_lpss_boardinfo *info;
40 u32 saved_ctrl[MAX_PWMS];
44 const struct pwm_lpss_boardinfo pwm_lpss_byt_info = {
49 EXPORT_SYMBOL_GPL(pwm_lpss_byt_info);
52 const struct pwm_lpss_boardinfo pwm_lpss_bsw_info = {
57 EXPORT_SYMBOL_GPL(pwm_lpss_bsw_info);
60 const struct pwm_lpss_boardinfo pwm_lpss_bxt_info = {
65 EXPORT_SYMBOL_GPL(pwm_lpss_bxt_info);
67 static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
69 return container_of(chip, struct pwm_lpss_chip, chip);
72 static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
74 struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
76 return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
79 static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
81 struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
83 writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
86 static void pwm_lpss_update(struct pwm_device *pwm)
88 pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_SW_UPDATE);
89 /* Give it some time to propagate */
93 static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
94 int duty_ns, int period_ns)
96 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
97 unsigned long long on_time_div;
98 unsigned long c = lpwm->info->clk_rate, base_unit_range;
99 unsigned long long base_unit, freq = NSEC_PER_SEC;
102 do_div(freq, period_ns);
106 * base_unit = round(base_unit_range * freq / c)
108 base_unit_range = BIT(lpwm->info->base_unit_bits);
109 freq *= base_unit_range;
111 base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
115 on_time_div = 255ULL * duty_ns;
116 do_div(on_time_div, period_ns);
117 on_time_div = 255ULL - on_time_div;
119 pm_runtime_get_sync(chip->dev);
121 ctrl = pwm_lpss_read(pwm);
122 ctrl &= ~PWM_ON_TIME_DIV_MASK;
123 ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
124 base_unit &= (base_unit_range - 1);
125 ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
127 pwm_lpss_write(pwm, ctrl);
130 * If the PWM is already enabled we need to notify the hardware
131 * about the change by setting PWM_SW_UPDATE.
133 if (pwm_is_enabled(pwm))
134 pwm_lpss_update(pwm);
136 pm_runtime_put(chip->dev);
141 static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
143 pm_runtime_get_sync(chip->dev);
146 * Hardware must first see PWM_SW_UPDATE before the PWM can be
149 pwm_lpss_update(pwm);
150 pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
154 static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
156 pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
157 pm_runtime_put(chip->dev);
160 static const struct pwm_ops pwm_lpss_ops = {
161 .config = pwm_lpss_config,
162 .enable = pwm_lpss_enable,
163 .disable = pwm_lpss_disable,
164 .owner = THIS_MODULE,
167 struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
168 const struct pwm_lpss_boardinfo *info)
170 struct pwm_lpss_chip *lpwm;
174 if (WARN_ON(info->npwm > MAX_PWMS))
175 return ERR_PTR(-ENODEV);
177 lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
179 return ERR_PTR(-ENOMEM);
181 lpwm->regs = devm_ioremap_resource(dev, r);
182 if (IS_ERR(lpwm->regs))
183 return ERR_CAST(lpwm->regs);
187 c = lpwm->info->clk_rate;
189 return ERR_PTR(-EINVAL);
191 lpwm->chip.dev = dev;
192 lpwm->chip.ops = &pwm_lpss_ops;
193 lpwm->chip.base = -1;
194 lpwm->chip.npwm = info->npwm;
196 ret = pwmchip_add(&lpwm->chip);
198 dev_err(dev, "failed to add PWM chip: %d\n", ret);
204 EXPORT_SYMBOL_GPL(pwm_lpss_probe);
206 int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
210 for (i = 0; i < lpwm->info->npwm; i++) {
211 if (pwm_is_enabled(&lpwm->chip.pwms[i]))
212 pm_runtime_put(lpwm->chip.dev);
214 return pwmchip_remove(&lpwm->chip);
216 EXPORT_SYMBOL_GPL(pwm_lpss_remove);
218 int pwm_lpss_suspend(struct device *dev)
220 struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
223 for (i = 0; i < lpwm->info->npwm; i++)
224 lpwm->saved_ctrl[i] = readl(lpwm->regs + i * PWM_SIZE + PWM);
228 EXPORT_SYMBOL_GPL(pwm_lpss_suspend);
230 int pwm_lpss_resume(struct device *dev)
232 struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
235 for (i = 0; i < lpwm->info->npwm; i++)
236 writel(lpwm->saved_ctrl[i], lpwm->regs + i * PWM_SIZE + PWM);
240 EXPORT_SYMBOL_GPL(pwm_lpss_resume);
242 MODULE_DESCRIPTION("PWM driver for Intel LPSS");
243 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
244 MODULE_LICENSE("GPL v2");