Commit 54e626d0 authored by Uwe Kleine-König's avatar Uwe Kleine-König Committed by Dmitry Torokhov
Browse files

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: default avatarUwe 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: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent fc75e51e
Loading
Loading
Loading
Loading
+47 −49
Original line number Diff line number Diff line
@@ -53,17 +53,9 @@ struct max8997_haptic {
	unsigned int pattern_signal_period;
};

static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
static void max8997_haptic_set_internal_duty_cycle(struct max8997_haptic *chip)
{
	int ret = 0;

	if (chip->mode == MAX8997_EXTERNAL_MODE) {
		unsigned int duty = chip->pwm_period * chip->level / 100;
		ret = pwm_config(chip->pwm, duty, chip->pwm_period);
	} else {
		u8 duty_index = 0;

		duty_index = DIV_ROUND_UP(chip->level * 64, 100);
	u8 duty_index = DIV_ROUND_UP(chip->level * 64, 100);

	switch (chip->internal_mode_pattern) {
	case 0:
@@ -86,8 +78,6 @@ static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
		break;
	}
}
	return ret;
}

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

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

	error = max8997_haptic_set_duty_cycle(chip);
	if (error) {
		dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
		return;
	}
	if (chip->mode != MAX8997_EXTERNAL_MODE)
		max8997_haptic_set_internal_duty_cycle(chip);

	if (!chip->enabled) {
		error = regulator_enable(chip->regulator);
@@ -168,17 +155,33 @@ static void max8997_haptic_enable(struct max8997_haptic *chip)
			return;
		}
		max8997_haptic_configure(chip);
	}

	/*
	 * It would be more straight forward to configure the external PWM
	 * earlier i.e. when the internal duty_cycle is setup in internal mode.
	 * But historically this is done only after the regulator was enabled
	 * and max8997_haptic_configure() set the enable bit in
	 * MAX8997_HAPTIC_REG_CONF2. So better keep it this way.
	 */
	if (chip->mode == MAX8997_EXTERNAL_MODE) {
			error = pwm_enable(chip->pwm);
		struct pwm_state state;

		pwm_init_state(chip->pwm, &state);
		state.period = chip->pwm_period;
		state.duty_cycle = chip->pwm_period * chip->level / 100;
		state.enabled = true;

		error = pwm_apply_might_sleep(chip->pwm, &state);
		if (error) {
			dev_err(chip->dev, "Failed to enable PWM\n");
			regulator_disable(chip->regulator);
			return;
		}
	}

	chip->enabled = true;
}
}

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

		/*
		 * FIXME: pwm_apply_args() should be removed when switching to
		 * the atomic PWM API.
		 */
		pwm_apply_args(chip->pwm);
		break;

	default: