Skip to content

Commit 28208c7

Browse files
jlabundythierryreding
authored andcommitted
pwm: iqs620a: Correct a stale state variable
If duty cycle is first set to a value that is sufficiently high to enable the output (e.g. 10000 ns) but then lowered to a value that is quantized to zero (e.g. 1000 ns), the output is disabled as the device cannot drive a constant zero (as expected). However if the device is later re-initialized due to watchdog bite, the output is re-enabled at the next-to-last duty cycle (10000 ns). This is because the iqs620_pwm->out_en flag unconditionally tracks state->enabled instead of what was actually written to the device. To solve this problem, use one state variable that encodes all 257 states of the output (duty_scale) with 0 representing tri-state, 1 representing the minimum available duty cycle and 256 representing 100% duty cycle. Signed-off-by: Jeff LaBundy <jeff@labundy.com> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
1 parent 72d6b24 commit 28208c7

1 file changed

Lines changed: 37 additions & 51 deletions

File tree

‎drivers/pwm/pwm-iqs620a.c‎

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,32 @@ struct iqs620_pwm_private {
3737
struct pwm_chip chip;
3838
struct notifier_block notifier;
3939
struct mutex lock;
40-
bool out_en;
41-
u8 duty_val;
40+
unsigned int duty_scale;
4241
};
4342

43+
static int iqs620_pwm_init(struct iqs620_pwm_private *iqs620_pwm,
44+
unsigned int duty_scale)
45+
{
46+
struct iqs62x_core *iqs62x = iqs620_pwm->iqs62x;
47+
int ret;
48+
49+
if (!duty_scale)
50+
return regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
51+
IQS620_PWR_SETTINGS_PWM_OUT, 0);
52+
53+
ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
54+
duty_scale - 1);
55+
if (ret)
56+
return ret;
57+
58+
return regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
59+
IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
60+
}
61+
4462
static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
4563
const struct pwm_state *state)
4664
{
4765
struct iqs620_pwm_private *iqs620_pwm;
48-
struct iqs62x_core *iqs62x;
4966
unsigned int duty_cycle;
5067
unsigned int duty_scale;
5168
int ret;
@@ -57,7 +74,6 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
5774
return -EINVAL;
5875

5976
iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
60-
iqs62x = iqs620_pwm->iqs62x;
6177

6278
/*
6379
* The duty cycle generated by the device is calculated as follows:
@@ -74,36 +90,15 @@ static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
7490
duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
7591
duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
7692

77-
mutex_lock(&iqs620_pwm->lock);
78-
79-
if (!state->enabled || !duty_scale) {
80-
ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
81-
IQS620_PWR_SETTINGS_PWM_OUT, 0);
82-
if (ret)
83-
goto err_mutex;
84-
}
85-
86-
if (duty_scale) {
87-
u8 duty_val = duty_scale - 1;
93+
if (!state->enabled)
94+
duty_scale = 0;
8895

89-
ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
90-
duty_val);
91-
if (ret)
92-
goto err_mutex;
93-
94-
iqs620_pwm->duty_val = duty_val;
95-
}
96-
97-
if (state->enabled && duty_scale) {
98-
ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
99-
IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
100-
if (ret)
101-
goto err_mutex;
102-
}
96+
mutex_lock(&iqs620_pwm->lock);
10397

104-
iqs620_pwm->out_en = state->enabled;
98+
ret = iqs620_pwm_init(iqs620_pwm, duty_scale);
99+
if (!ret)
100+
iqs620_pwm->duty_scale = duty_scale;
105101

106-
err_mutex:
107102
mutex_unlock(&iqs620_pwm->lock);
108103

109104
return ret;
@@ -121,12 +116,11 @@ static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
121116
/*
122117
* Since the device cannot generate a 0% duty cycle, requests to do so
123118
* cause subsequent calls to iqs620_pwm_get_state to report the output
124-
* as disabled with duty cycle equal to that which was in use prior to
125-
* the request. This is not ideal, but is the best compromise based on
119+
* as disabled. This is not ideal, but is the best compromise based on
126120
* the capabilities of the device.
127121
*/
128-
state->enabled = iqs620_pwm->out_en;
129-
state->duty_cycle = DIV_ROUND_UP((iqs620_pwm->duty_val + 1) *
122+
state->enabled = iqs620_pwm->duty_scale > 0;
123+
state->duty_cycle = DIV_ROUND_UP(iqs620_pwm->duty_scale *
130124
IQS620_PWM_PERIOD_NS, 256);
131125

132126
mutex_unlock(&iqs620_pwm->lock);
@@ -138,15 +132,13 @@ static int iqs620_pwm_notifier(struct notifier_block *notifier,
138132
unsigned long event_flags, void *context)
139133
{
140134
struct iqs620_pwm_private *iqs620_pwm;
141-
struct iqs62x_core *iqs62x;
142135
int ret;
143136

144137
if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
145138
return NOTIFY_DONE;
146139

147140
iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
148141
notifier);
149-
iqs62x = iqs620_pwm->iqs62x;
150142

151143
mutex_lock(&iqs620_pwm->lock);
152144

@@ -155,16 +147,8 @@ static int iqs620_pwm_notifier(struct notifier_block *notifier,
155147
* of a device reset, so nothing else is printed here unless there is
156148
* an additional failure.
157149
*/
158-
ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
159-
iqs620_pwm->duty_val);
160-
if (ret)
161-
goto err_mutex;
150+
ret = iqs620_pwm_init(iqs620_pwm, iqs620_pwm->duty_scale);
162151

163-
ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
164-
IQS620_PWR_SETTINGS_PWM_OUT,
165-
iqs620_pwm->out_en ? 0xff : 0);
166-
167-
err_mutex:
168152
mutex_unlock(&iqs620_pwm->lock);
169153

170154
if (ret) {
@@ -211,12 +195,14 @@ static int iqs620_pwm_probe(struct platform_device *pdev)
211195
ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
212196
if (ret)
213197
return ret;
214-
iqs620_pwm->out_en = val & IQS620_PWR_SETTINGS_PWM_OUT;
215198

216-
ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
217-
if (ret)
218-
return ret;
219-
iqs620_pwm->duty_val = val;
199+
if (val & IQS620_PWR_SETTINGS_PWM_OUT) {
200+
ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
201+
if (ret)
202+
return ret;
203+
204+
iqs620_pwm->duty_scale = val + 1;
205+
}
220206

221207
iqs620_pwm->chip.dev = &pdev->dev;
222208
iqs620_pwm->chip.ops = &iqs620_pwm_ops;

0 commit comments

Comments
 (0)