Skip to content

Commit a1bbf82

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: twl-led: Implement .apply() callback
To eventually get rid of all legacy drivers convert this driver to the modern world implementing .apply(). This just pushed a variant of pwm_apply_legacy() into the driver that was slightly simplified because the driver doesn't provide a .set_polarity() callback. Signed-off-by: Uwe Kleine-König <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent c449a8c commit a1bbf82

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

drivers/pwm/pwm-twl-led.c

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,45 @@ static void twl4030_pwmled_disable(struct pwm_chip *chip,
137137
mutex_unlock(&twl->mutex);
138138
}
139139

140+
static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
141+
const struct pwm_state *state)
142+
{
143+
int ret;
144+
145+
if (state->polarity != PWM_POLARITY_NORMAL)
146+
return -EINVAL;
147+
148+
if (!state->enabled) {
149+
if (pwm->state.enabled)
150+
twl4030_pwmled_disable(chip, pwm);
151+
152+
return 0;
153+
}
154+
155+
/*
156+
* We cannot skip calling ->config even if state->period ==
157+
* pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
158+
* because we might have exited early in the last call to
159+
* pwm_apply_state because of !state->enabled and so the two values in
160+
* pwm->state might not be configured in hardware.
161+
*/
162+
ret = twl4030_pwmled_config(pwm->chip, pwm,
163+
state->duty_cycle, state->period);
164+
if (ret)
165+
return ret;
166+
167+
if (!pwm->state.enabled)
168+
ret = twl4030_pwmled_enable(chip, pwm);
169+
170+
return ret;
171+
}
172+
173+
174+
static const struct pwm_ops twl4030_pwmled_ops = {
175+
.apply = twl4030_pwmled_apply,
176+
.owner = THIS_MODULE,
177+
};
178+
140179
static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
141180
int duty_ns, int period_ns)
142181
{
@@ -206,6 +245,32 @@ static void twl6030_pwmled_disable(struct pwm_chip *chip,
206245
mutex_unlock(&twl->mutex);
207246
}
208247

248+
static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
249+
const struct pwm_state *state)
250+
{
251+
int err;
252+
253+
if (state->polarity != pwm->state.polarity)
254+
return -EINVAL;
255+
256+
if (!state->enabled) {
257+
if (pwm->state.enabled)
258+
twl6030_pwmled_disable(chip, pwm);
259+
260+
return 0;
261+
}
262+
263+
err = twl6030_pwmled_config(pwm->chip, pwm,
264+
state->duty_cycle, state->period);
265+
if (err)
266+
return err;
267+
268+
if (!pwm->state.enabled)
269+
err = twl6030_pwmled_enable(chip, pwm);
270+
271+
return err;
272+
}
273+
209274
static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
210275
{
211276
struct twl_pwmled_chip *twl = to_twl(chip);
@@ -257,17 +322,8 @@ static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
257322
mutex_unlock(&twl->mutex);
258323
}
259324

260-
static const struct pwm_ops twl4030_pwmled_ops = {
261-
.enable = twl4030_pwmled_enable,
262-
.disable = twl4030_pwmled_disable,
263-
.config = twl4030_pwmled_config,
264-
.owner = THIS_MODULE,
265-
};
266-
267325
static const struct pwm_ops twl6030_pwmled_ops = {
268-
.enable = twl6030_pwmled_enable,
269-
.disable = twl6030_pwmled_disable,
270-
.config = twl6030_pwmled_config,
326+
.apply = twl6030_pwmled_apply,
271327
.request = twl6030_pwmled_request,
272328
.free = twl6030_pwmled_free,
273329
.owner = THIS_MODULE,

0 commit comments

Comments
 (0)