Skip to contentSkip to conversation

GPIO output support for LM93 kernel driver

PATCH In review Olivier Hochreutiner 2 participants 3 messages Jan 12, 2009 – Jan 13, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 GPIO output support for LM93 kernel driver

Hi there,

I have been using the LM93 kernel driver and I noticed that 'gpio' file in sysfs is read-only and that the GPIO pins of the LM93 chip can be used as input only.
I attached a patch that allows to also use GPIO pins as output, by simply writing to 'gpio' in sysfs. The patch is against the current git revision of the mainstream kernel.
I did not handle the case where the GPIO pins are used as inputs after being used as output (in which case, according to the datasheet, 0s should be written to the GPO register, and thus 1s should be written to the 'gpio' sysfs file). Also, if some pins are used as inputs and others as outputs, then, when writing to the 'gpio' file in sysfs, the pins used as inputs should be set to 1 (so that the corresponding bits in the GPO register are set to 0).

Comments are welcome.

Best regards,

Olivier Hochreutiner

---

hwmon/lm93.c +33 -5
@@ -23,6 +23,9 @@
     Modified for mainline integration by Hans J. Koch <[email protected]>
         Copyright (c) 2007 Hans J. Koch, Linutronix GmbH
 
+    Added GPIO output support by Olivier Hochreutiner <[email protected]>
+        Copyright (c) 2009 Olivier Hochreutiner
+
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
@@ -108,6 +111,9 @@
 #define LM93_REG_PWM_MIN_HYST_34	0xc4
 #define LM93_REG_PWM_MIN_HYST(nr)	(0xc3 + (nr)/2)
 
+/* GPIO output state */
+#define LM93_REG_GPO			0xc5
+
 /* prochot_override & prochot_interval */
 #define LM93_REG_PROCHOT_OVERRIDE	0xc6
 #define LM93_REG_PROCHOT_INTERVAL	0xc7
@@ -265,8 +271,8 @@
 	/* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
 	u8 vccp_limits[2];
 
-	/* GPIO input state (register format, i.e. inverted) */
-	u8 gpi;
+	/* GPIO input/output state (register format, i.e. inverted) */
+	u8 gpio;
 
 	/* #PROCHOT override (register format) */
 	u8 prochot_override;
@@ -696,6 +702,13 @@
 	return ~reg & 0xff;
 }
 
+/* GPIO: 0-255, GPIO0 is LSB
+ * REG: inverted */
+static unsigned LM93_GPO_TO_REG(u8 data)
+{
+	return ~data & 0xff;
+}
+
 /* alarm bitmask definitions
    The LM93 has nearly 64 bits of error status... I've pared that down to
    what I think is a useful subset in order to fit it into 32 bits.
@@ -918,7 +931,7 @@
 				LM93_REG_VCCP_LIMIT_OFF(i));
 
 	/* GPIO input state */
-	data->gpi = lm93_read_byte(client, LM93_REG_GPI);
+	data->gpio = lm93_read_byte(client, LM93_REG_GPI);
 
 	/* #PROCHOT override state */
 	data->prochot_override = lm93_read_byte(client,
@@ -2281,10 +2294,25 @@
 				char *buf)
 {
 	struct lm93_data *data = lm93_update_device(dev);
-	return sprintf(buf,"%d\n",LM93_GPI_FROM_REG(data->gpi));
+	return sprintf(buf,"%d\n",LM93_GPI_FROM_REG(data->gpio));
+}
+
+static ssize_t store_gpio(struct device *dev,
+					struct device_attribute *attr,
+					const char *buf, size_t count)
+{
+	struct i2c_client *client = to_i2c_client(dev);
+	struct lm93_data *data = i2c_get_clientdata(client);
+	long val = simple_strtol(buf, NULL, 10);
+
+	mutex_lock(&data->update_lock);
+	data->gpio = LM93_GPO_TO_REG(val);
+	lm93_write_byte(client, LM93_REG_GPO, data->gpio);
+	mutex_unlock(&data->update_lock);
+	return count;
 }
 
-static DEVICE_ATTR(gpio, S_IRUGO, show_gpio, NULL);
+static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, store_gpio);
 
 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
 				char *buf)

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw
Quoted 16 lines · Olivier Hochreutiner

Hi there,

I have been using the LM93 kernel driver and I noticed that 'gpio' file in sysfs is read-only and that the GPIO pins of the LM93 chip can be used as input only.
I attached a patch that allows to also use GPIO pins as output, by simply writing to 'gpio' in sysfs. The patch is against the current git revision of the mainstream kernel.
I did not handle the case where the GPIO pins are used as inputs after being used as output (in which case, according to the datasheet, 0s should be written to the GPO register, and thus 1s should be written to the 'gpio' sysfs file). Also, if some pins are used as inputs and others as outputs, then, when writing to the 'gpio' file in sysfs, the pins used as inputs should be set to 1 (so that the corresponding bits in the GPO register are set to 0).

Quoted 1 line

Comments are welcome.

IMO gpiolib would be better and more generic solution instead of creating some custom sysfs entry...

Quoted 11 lines

Best regards,

Olivier Hochreutiner

------------------------------------------------------------------------

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#2 lore raw
In reply to Olivier Hochreutiner
Quoted 14 lines
Quoted 14 lines

I have been using the LM93 kernel driver and I noticed that 'gpio' file in sysfs is read-only and that the GPIO pins of the LM93 chip can be used as input only.
I attached a patch that allows to also use GPIO pins as output, by simply writing to 'gpio' in sysfs. The patch is against the current git revision of the mainstream kernel.
I did not handle the case where the GPIO pins are used as inputs after being used as output (in which case, according to the datasheet, 0s should be written to the GPO register, and thus 1s should be written to the 'gpio' sysfs file). Also, if some pins are used as inputs and others as outputs, then, when writing to the 'gpio' file in sysfs, the pins used as inputs should be set to 1 (so that the corresponding bits in the GPO register are set to 0).

Quoted 4 lines
Quoted 1 line

Comments are welcome.

IMO gpiolib would be better and more generic solution instead of creating some custom sysfs entry...

The sysfs entry already exists (and is present the mainstream kernel), so that's why I preferred to use it. I don't think the sysfs entry will be removed anyway, or will it ?

Olivier

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors