diff mbox series

[v4,5/5] hwmon: (adt7475) Add support for inverting pwm output

Message ID 20200221041631.10960-6-chris.packham@alliedtelesis.co.nz
State New
Headers show
Series None | expand

Commit Message

Chris Packham Feb. 21, 2020, 4:16 a.m. UTC
Add a "invert-pwm" device-tree property to allow hardware designs to use
inverted logic on the PWM output. Preserve the invert PWM output bit if
the property is not found to allow for bootloaders/bios which may have
configured this earlier.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---

Notes:
    Changes in v4:
    - use vendor prefix for new property
    
    Changes in v3:
    - New

 drivers/hwmon/adt7475.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
diff mbox series

Patch

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 3649b18359dc..13267181dde6 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -1509,6 +1509,41 @@  static int load_attenuators(const struct i2c_client *client, int chip,
 	return 0;
 }
 
+static int adt7475_set_pwm_polarity(struct i2c_client *client)
+{
+	char *prop;
+	int ret, i;
+	u8 val;
+
+	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
+		ret = adt7475_read(PWM_CONFIG_REG(i));
+		if (ret < 0)
+			return ret;
+		val = ret;
+		switch (i) {
+		case 0:
+			prop = "adi,invert-pwm1";
+			break;
+		case 1:
+			prop = "adi,invert-pwm2";
+			break;
+		case 2:
+			prop = "adi,invert-pwm3";
+			break;
+		}
+
+		ret = set_property_bit(client, prop, &val, 4);
+		if (ret)
+			continue;
+
+		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(i), val);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int adt7475_probe(struct i2c_client *client,
 			 const struct i2c_device_id *id)
 {
@@ -1617,6 +1652,10 @@  static int adt7475_probe(struct i2c_client *client,
 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
 		adt7475_read_pwm(client, i);
 
+	ret = adt7475_set_pwm_polarity(client);
+	if (ret)
+		dev_err(&client->dev, "Error configuring pwm polarity");
+
 	/* Start monitoring */
 	switch (chip) {
 	case adt7475: