pwm: lpss: Add range limit check for the base_unit register value
[linux-block.git] / drivers / pwm / pwm-lpss.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
d16a5aa9
MW
2/*
3 * Intel Low Power Subsystem PWM controller driver
4 *
5 * Copyright (C) 2014, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * Author: Chew Kean Ho <kean.ho.chew@intel.com>
8 * Author: Chang Rebecca Swee Fun <rebecca.swee.fun.chang@intel.com>
9 * Author: Chew Chiau Ee <chiau.ee.chew@intel.com>
093e00bb 10 * Author: Alan Cox <alan@linux.intel.com>
d16a5aa9
MW
11 */
12
37670676 13#include <linux/delay.h>
e0c86a3b 14#include <linux/io.h>
10d56a4c 15#include <linux/iopoll.h>
d16a5aa9
MW
16#include <linux/kernel.h>
17#include <linux/module.h>
f080be27 18#include <linux/pm_runtime.h>
883e4d07 19#include <linux/time.h>
093e00bb 20
c558e39e 21#include "pwm-lpss.h"
d16a5aa9
MW
22
23#define PWM 0x00000000
24#define PWM_ENABLE BIT(31)
25#define PWM_SW_UPDATE BIT(30)
26#define PWM_BASE_UNIT_SHIFT 8
d16a5aa9 27#define PWM_ON_TIME_DIV_MASK 0x000000ff
d16a5aa9 28
4e11f5ac
MW
29/* Size of each PWM register space if multiple */
30#define PWM_SIZE 0x400
31
d16a5aa9
MW
32static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
33{
34 return container_of(chip, struct pwm_lpss_chip, chip);
35}
36
4e11f5ac
MW
37static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
38{
39 struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
40
41 return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
42}
43
44static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
45{
46 struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
47
48 writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
49}
50
b997e3ed 51static int pwm_lpss_wait_for_update(struct pwm_device *pwm)
37670676 52{
10d56a4c
IK
53 struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
54 const void __iomem *addr = lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM;
55 const unsigned int ms = 500 * USEC_PER_MSEC;
56 u32 val;
57 int err;
58
b14e8cef 59 /*
10d56a4c
IK
60 * PWM Configuration register has SW_UPDATE bit that is set when a new
61 * configuration is written to the register. The bit is automatically
62 * cleared at the start of the next output cycle by the IP block.
63 *
64 * If one writes a new configuration to the register while it still has
65 * the bit enabled, PWM may freeze. That is, while one can still write
66 * to the register, it won't have an effect. Thus, we try to sleep long
67 * enough that the bit gets cleared and make sure the bit is not
68 * enabled while we update the configuration.
b14e8cef 69 */
10d56a4c
IK
70 err = readl_poll_timeout(addr, val, !(val & PWM_SW_UPDATE), 40, ms);
71 if (err)
72 dev_err(pwm->chip->dev, "PWM_SW_UPDATE was not cleared\n");
b14e8cef 73
10d56a4c
IK
74 return err;
75}
76
77static inline int pwm_lpss_is_updating(struct pwm_device *pwm)
78{
79 return (pwm_lpss_read(pwm) & PWM_SW_UPDATE) ? -EBUSY : 0;
37670676
MW
80}
81
b14e8cef
AS
82static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,
83 int duty_ns, int period_ns)
d16a5aa9 84{
ab248b60 85 unsigned long long on_time_div;
d9cd4a73 86 unsigned long c = lpwm->info->clk_rate, base_unit_range;
883e4d07 87 unsigned long long base_unit, freq = NSEC_PER_SEC;
2153bbc1 88 u32 orig_ctrl, ctrl;
d16a5aa9
MW
89
90 do_div(freq, period_ns);
91
883e4d07 92 /*
93 * The equation is:
e5ca4245 94 * base_unit = round(base_unit_range * freq / c)
883e4d07 95 */
181f4d2f 96 base_unit_range = BIT(lpwm->info->base_unit_bits);
e5ca4245 97 freq *= base_unit_range;
d16a5aa9 98
e5ca4245 99 base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
ef9f60da
HG
100 /* base_unit must not be 0 and we also want to avoid overflowing it */
101 base_unit = clamp_val(base_unit, 1, base_unit_range - 1);
d16a5aa9 102
ab248b60
MW
103 on_time_div = 255ULL * duty_ns;
104 do_div(on_time_div, period_ns);
105 on_time_div = 255ULL - on_time_div;
d16a5aa9 106
2153bbc1 107 orig_ctrl = ctrl = pwm_lpss_read(pwm);
883e4d07 108 ctrl &= ~PWM_ON_TIME_DIV_MASK;
181f4d2f 109 ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
883e4d07 110 ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
d16a5aa9 111 ctrl |= on_time_div;
2153bbc1
HG
112
113 if (orig_ctrl != ctrl) {
114 pwm_lpss_write(pwm, ctrl);
115 pwm_lpss_write(pwm, ctrl | PWM_SW_UPDATE);
116 }
d16a5aa9
MW
117}
118
b997e3ed
HG
119static inline void pwm_lpss_cond_enable(struct pwm_device *pwm, bool cond)
120{
121 if (cond)
122 pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
123}
124
b14e8cef 125static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,
71523d18 126 const struct pwm_state *state)
d16a5aa9 127{
b14e8cef 128 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
10d56a4c 129 int ret;
37670676 130
b14e8cef
AS
131 if (state->enabled) {
132 if (!pwm_is_enabled(pwm)) {
133 pm_runtime_get_sync(chip->dev);
10d56a4c
IK
134 ret = pwm_lpss_is_updating(pwm);
135 if (ret) {
136 pm_runtime_put(chip->dev);
137 return ret;
138 }
b14e8cef 139 pwm_lpss_prepare(lpwm, pwm, state->duty_cycle, state->period);
b997e3ed
HG
140 pwm_lpss_cond_enable(pwm, lpwm->info->bypass == false);
141 ret = pwm_lpss_wait_for_update(pwm);
10d56a4c
IK
142 if (ret) {
143 pm_runtime_put(chip->dev);
144 return ret;
145 }
b997e3ed 146 pwm_lpss_cond_enable(pwm, lpwm->info->bypass == true);
b14e8cef 147 } else {
10d56a4c
IK
148 ret = pwm_lpss_is_updating(pwm);
149 if (ret)
150 return ret;
b14e8cef 151 pwm_lpss_prepare(lpwm, pwm, state->duty_cycle, state->period);
b997e3ed 152 return pwm_lpss_wait_for_update(pwm);
b14e8cef
AS
153 }
154 } else if (pwm_is_enabled(pwm)) {
155 pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
156 pm_runtime_put(chip->dev);
157 }
d16a5aa9 158
b14e8cef 159 return 0;
d16a5aa9
MW
160}
161
280fec4c
HG
162static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
163 struct pwm_state *state)
164{
165 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
166 unsigned long base_unit_range;
167 unsigned long long base_unit, freq, on_time_div;
168 u32 ctrl;
169
01aa905d
HG
170 pm_runtime_get_sync(chip->dev);
171
280fec4c
HG
172 base_unit_range = BIT(lpwm->info->base_unit_bits);
173
174 ctrl = pwm_lpss_read(pwm);
175 on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
176 base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
177
178 freq = base_unit * lpwm->info->clk_rate;
179 do_div(freq, base_unit_range);
180 if (freq == 0)
181 state->period = NSEC_PER_SEC;
182 else
183 state->period = NSEC_PER_SEC / (unsigned long)freq;
184
185 on_time_div *= state->period;
186 do_div(on_time_div, 255);
187 state->duty_cycle = on_time_div;
188
189 state->polarity = PWM_POLARITY_NORMAL;
190 state->enabled = !!(ctrl & PWM_ENABLE);
191
01aa905d 192 pm_runtime_put(chip->dev);
280fec4c
HG
193}
194
d16a5aa9 195static const struct pwm_ops pwm_lpss_ops = {
b14e8cef 196 .apply = pwm_lpss_apply,
280fec4c 197 .get_state = pwm_lpss_get_state,
d16a5aa9
MW
198 .owner = THIS_MODULE,
199};
200
c558e39e
AS
201struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
202 const struct pwm_lpss_boardinfo *info)
d16a5aa9
MW
203{
204 struct pwm_lpss_chip *lpwm;
d9cd4a73 205 unsigned long c;
01aa905d
HG
206 int i, ret;
207 u32 ctrl;
d16a5aa9 208
1d375b58
HG
209 if (WARN_ON(info->npwm > MAX_PWMS))
210 return ERR_PTR(-ENODEV);
211
093e00bb 212 lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
d16a5aa9 213 if (!lpwm)
093e00bb 214 return ERR_PTR(-ENOMEM);
d16a5aa9 215
093e00bb 216 lpwm->regs = devm_ioremap_resource(dev, r);
d16a5aa9 217 if (IS_ERR(lpwm->regs))
89c0339e 218 return ERR_CAST(lpwm->regs);
093e00bb 219
883e4d07 220 lpwm->info = info;
d9cd4a73
AS
221
222 c = lpwm->info->clk_rate;
223 if (!c)
224 return ERR_PTR(-EINVAL);
225
093e00bb 226 lpwm->chip.dev = dev;
d16a5aa9
MW
227 lpwm->chip.ops = &pwm_lpss_ops;
228 lpwm->chip.base = -1;
4e11f5ac 229 lpwm->chip.npwm = info->npwm;
d16a5aa9
MW
230
231 ret = pwmchip_add(&lpwm->chip);
232 if (ret) {
093e00bb
AC
233 dev_err(dev, "failed to add PWM chip: %d\n", ret);
234 return ERR_PTR(ret);
d16a5aa9
MW
235 }
236
01aa905d
HG
237 for (i = 0; i < lpwm->info->npwm; i++) {
238 ctrl = pwm_lpss_read(&lpwm->chip.pwms[i]);
239 if (ctrl & PWM_ENABLE)
240 pm_runtime_get(dev);
241 }
242
093e00bb 243 return lpwm;
d16a5aa9 244}
c558e39e 245EXPORT_SYMBOL_GPL(pwm_lpss_probe);
d16a5aa9 246
c558e39e 247int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
d16a5aa9 248{
42885551
HG
249 int i;
250
251 for (i = 0; i < lpwm->info->npwm; i++) {
252 if (pwm_is_enabled(&lpwm->chip.pwms[i]))
253 pm_runtime_put(lpwm->chip.dev);
254 }
d16a5aa9
MW
255 return pwmchip_remove(&lpwm->chip);
256}
c558e39e 257EXPORT_SYMBOL_GPL(pwm_lpss_remove);
d16a5aa9 258
1d375b58
HG
259int pwm_lpss_suspend(struct device *dev)
260{
261 struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
262 int i;
263
264 for (i = 0; i < lpwm->info->npwm; i++)
265 lpwm->saved_ctrl[i] = readl(lpwm->regs + i * PWM_SIZE + PWM);
266
267 return 0;
268}
269EXPORT_SYMBOL_GPL(pwm_lpss_suspend);
270
271int pwm_lpss_resume(struct device *dev)
272{
273 struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
274 int i;
275
276 for (i = 0; i < lpwm->info->npwm; i++)
277 writel(lpwm->saved_ctrl[i], lpwm->regs + i * PWM_SIZE + PWM);
278
279 return 0;
280}
281EXPORT_SYMBOL_GPL(pwm_lpss_resume);
282
d16a5aa9
MW
283MODULE_DESCRIPTION("PWM driver for Intel LPSS");
284MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
285MODULE_LICENSE("GPL v2");