diff mbox series

[2/2] hwmon: (adt7475) Add support for configuring initial PWM duty cycle

Message ID 20240508170544.263059-3-chris.packham@alliedtelesis.co.nz (mailing list archive)
State Superseded
Headers show
Series hwmon: (adt7475) duty cycle configuration | expand

Commit Message

Chris Packham May 8, 2024, 5:05 p.m. UTC
By default the PWM duty cycle in hardware is 100%. On some systems this
can cause unwanted fan noise. Add the ability to take an initial PWM
duty cycle and frequency via device properties.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
 drivers/hwmon/adt7475.c | 56 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

Comments

Chris Packham May 8, 2024, 5:49 p.m. UTC | #1
On 9/05/24 05:05, Chris Packham wrote:
> By default the PWM duty cycle in hardware is 100%. On some systems this
> can cause unwanted fan noise. Add the ability to take an initial PWM
> duty cycle and frequency via device properties.
>
> Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
> ---
>   drivers/hwmon/adt7475.c | 56 +++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 56 insertions(+)
>
> diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
> index 4224ffb30483..b0c7c1a95897 100644
> --- a/drivers/hwmon/adt7475.c
> +++ b/drivers/hwmon/adt7475.c
> @@ -1662,6 +1662,54 @@ static int adt7475_set_pwm_polarity(struct i2c_client *client)
>   	return 0;
>   }
>   
> +static int adt7475_set_pwm_initial_freq(struct i2c_client *client)
> +{
> +	int ret, out, i;
> +	u32 freqs[ADT7475_PWM_COUNT];
> +	int data;
> +
> +	ret = device_property_read_u32_array(&client->dev,
> +					     "pwm-initial-frequency", freqs,
> +					     ARRAY_SIZE(freqs));
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
> +		out = find_closest(freqs[i], pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
> +		data = adt7475_read(TEMP_TRANGE_REG(i));
> +		if (data < 0)
> +			return data;
> +		data &= ~0xf;
> +		data |= out;
> +
> +		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(i), data);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int adt7475_set_pwm_initial_duty(struct i2c_client *client)
> +{
> +	int ret, i;
> +	u32 dutys[ADT7475_PWM_COUNT];
> +
> +	ret = device_property_read_u32_array(&client->dev,
> +					     "adi,pwm-initial-duty-cycle", dutys,
> +					     ARRAY_SIZE(dutys));
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
> +		ret = i2c_smbus_write_byte_data(client, PWM_MAX_REG(i), dutys[i] & 0xff);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> +
>   static int adt7475_probe(struct i2c_client *client)
>   {
>   	enum chips chip;
> @@ -1778,6 +1826,14 @@ static int adt7475_probe(struct i2c_client *client)
>   	if (ret && ret != -EINVAL)
>   		dev_warn(&client->dev, "Error configuring pwm polarity\n");
>   
> +	ret = adt7475_set_pwm_initial_freq(client);
> +	if (ret)
> +		return ret;

Whoops I meant to send the version that handles -EINVAL. v2 will handle 
errors the same way as pwm_polarity above. I'll wait for a bit before 
sending that out.

> +
> +	ret = adt7475_set_pwm_initial_duty(client);
> +	if (ret)
> +		return ret;
> +
>   	/* Start monitoring */
>   	switch (chip) {
>   	case adt7475:
diff mbox series

Patch

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 4224ffb30483..b0c7c1a95897 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -1662,6 +1662,54 @@  static int adt7475_set_pwm_polarity(struct i2c_client *client)
 	return 0;
 }
 
+static int adt7475_set_pwm_initial_freq(struct i2c_client *client)
+{
+	int ret, out, i;
+	u32 freqs[ADT7475_PWM_COUNT];
+	int data;
+
+	ret = device_property_read_u32_array(&client->dev,
+					     "pwm-initial-frequency", freqs,
+					     ARRAY_SIZE(freqs));
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
+		out = find_closest(freqs[i], pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
+		data = adt7475_read(TEMP_TRANGE_REG(i));
+		if (data < 0)
+			return data;
+		data &= ~0xf;
+		data |= out;
+
+		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(i), data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int adt7475_set_pwm_initial_duty(struct i2c_client *client)
+{
+	int ret, i;
+	u32 dutys[ADT7475_PWM_COUNT];
+
+	ret = device_property_read_u32_array(&client->dev,
+					     "adi,pwm-initial-duty-cycle", dutys,
+					     ARRAY_SIZE(dutys));
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
+		ret = i2c_smbus_write_byte_data(client, PWM_MAX_REG(i), dutys[i] & 0xff);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int adt7475_probe(struct i2c_client *client)
 {
 	enum chips chip;
@@ -1778,6 +1826,14 @@  static int adt7475_probe(struct i2c_client *client)
 	if (ret && ret != -EINVAL)
 		dev_warn(&client->dev, "Error configuring pwm polarity\n");
 
+	ret = adt7475_set_pwm_initial_freq(client);
+	if (ret)
+		return ret;
+
+	ret = adt7475_set_pwm_initial_duty(client);
+	if (ret)
+		return ret;
+
 	/* Start monitoring */
 	switch (chip) {
 	case adt7475: