Skip to content

Commit 54e626d

Browse files
ukleinekdtor
authored andcommitted
Input: max8997_haptic - optimize PWM configuration
Both pwm_config() and pwm_enable() are wrappers around pwm_apply_might_sleep(). Instead of calling this function twice only call it once without an intermediate step. Setup the PWM in max8997_haptic_enable() only where it was enabled historically. max8997_haptic_set_duty_cycle() is renamed accordingly to make it clear this function is only about the internal setup now. pwm_config() was called earlier back then, but that call has no effect on the hardware when the PWM is disabled, so delaying this configuration doesn't make a difference. As pwm_apply_might_sleep() is used now defining the whole state of the PWM, the call to pwm_apply_args() in .probe() can be dropped now, too. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com> Link: https://lore.kernel.org/r/20250630093718.2062359-2-u.kleine-koenig@baylibre.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent fc75e51 commit 54e626d

1 file changed

Lines changed: 47 additions & 49 deletions

File tree

drivers/input/misc/max8997_haptic.c

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,30 @@ struct max8997_haptic {
5353
unsigned int pattern_signal_period;
5454
};
5555

56-
static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
56+
static void max8997_haptic_set_internal_duty_cycle(struct max8997_haptic *chip)
5757
{
58-
int ret = 0;
58+
u8 duty_index = DIV_ROUND_UP(chip->level * 64, 100);
5959

60-
if (chip->mode == MAX8997_EXTERNAL_MODE) {
61-
unsigned int duty = chip->pwm_period * chip->level / 100;
62-
ret = pwm_config(chip->pwm, duty, chip->pwm_period);
63-
} else {
64-
u8 duty_index = 0;
65-
66-
duty_index = DIV_ROUND_UP(chip->level * 64, 100);
67-
68-
switch (chip->internal_mode_pattern) {
69-
case 0:
70-
max8997_write_reg(chip->client,
71-
MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
72-
break;
73-
case 1:
74-
max8997_write_reg(chip->client,
75-
MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
76-
break;
77-
case 2:
78-
max8997_write_reg(chip->client,
79-
MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
80-
break;
81-
case 3:
82-
max8997_write_reg(chip->client,
83-
MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
84-
break;
85-
default:
86-
break;
87-
}
60+
switch (chip->internal_mode_pattern) {
61+
case 0:
62+
max8997_write_reg(chip->client,
63+
MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
64+
break;
65+
case 1:
66+
max8997_write_reg(chip->client,
67+
MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
68+
break;
69+
case 2:
70+
max8997_write_reg(chip->client,
71+
MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
72+
break;
73+
case 3:
74+
max8997_write_reg(chip->client,
75+
MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
76+
break;
77+
default:
78+
break;
8879
}
89-
return ret;
9080
}
9181

9282
static void max8997_haptic_configure(struct max8997_haptic *chip)
@@ -155,11 +145,8 @@ static void max8997_haptic_enable(struct max8997_haptic *chip)
155145

156146
guard(mutex)(&chip->mutex);
157147

158-
error = max8997_haptic_set_duty_cycle(chip);
159-
if (error) {
160-
dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
161-
return;
162-
}
148+
if (chip->mode != MAX8997_EXTERNAL_MODE)
149+
max8997_haptic_set_internal_duty_cycle(chip);
163150

164151
if (!chip->enabled) {
165152
error = regulator_enable(chip->regulator);
@@ -168,16 +155,32 @@ static void max8997_haptic_enable(struct max8997_haptic *chip)
168155
return;
169156
}
170157
max8997_haptic_configure(chip);
171-
if (chip->mode == MAX8997_EXTERNAL_MODE) {
172-
error = pwm_enable(chip->pwm);
173-
if (error) {
174-
dev_err(chip->dev, "Failed to enable PWM\n");
175-
regulator_disable(chip->regulator);
176-
return;
177-
}
158+
}
159+
160+
/*
161+
* It would be more straight forward to configure the external PWM
162+
* earlier i.e. when the internal duty_cycle is setup in internal mode.
163+
* But historically this is done only after the regulator was enabled
164+
* and max8997_haptic_configure() set the enable bit in
165+
* MAX8997_HAPTIC_REG_CONF2. So better keep it this way.
166+
*/
167+
if (chip->mode == MAX8997_EXTERNAL_MODE) {
168+
struct pwm_state state;
169+
170+
pwm_init_state(chip->pwm, &state);
171+
state.period = chip->pwm_period;
172+
state.duty_cycle = chip->pwm_period * chip->level / 100;
173+
state.enabled = true;
174+
175+
error = pwm_apply_might_sleep(chip->pwm, &state);
176+
if (error) {
177+
dev_err(chip->dev, "Failed to enable PWM\n");
178+
regulator_disable(chip->regulator);
179+
return;
178180
}
179-
chip->enabled = true;
180181
}
182+
183+
chip->enabled = true;
181184
}
182185

183186
static void max8997_haptic_disable(struct max8997_haptic *chip)
@@ -282,11 +285,6 @@ static int max8997_haptic_probe(struct platform_device *pdev)
282285
goto err_free_mem;
283286
}
284287

285-
/*
286-
* FIXME: pwm_apply_args() should be removed when switching to
287-
* the atomic PWM API.
288-
*/
289-
pwm_apply_args(chip->pwm);
290288
break;
291289

292290
default:

0 commit comments

Comments
 (0)