Skip to contentSkip to conversation

gpio: max732x driver

PATCH In review David Brownell 2 participants 3 messages Jul 14, 2008 – Jul 22, 2008 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: max732x driver

From: Eric Miao <[email protected]>

This adds a driver supporting a family of I2C port expanders from Maxim, which includes the MAX7319 and MAX7320-7327 chips.

---
We're growing quite the collection of dedicated GPIO expander drivers! And the multi-function chip support is growing too. This applies after the patch adding support for max7301 chips.

 drivers/gpio/Kconfig        |   19 ++
 drivers/gpio/Makefile       |    1 
 drivers/gpio/max732x.c      |  385 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c/max732x.h |   19 ++
 4 files changed, 424 insertions(+)
drivers/gpio/Kconfig +19 -0
@@ -42,6 +42,25 @@ config GPIO_SYSFS
 
 comment "I2C GPIO expanders:"
 
+config GPIO_MAX732X
+	tristate "MAX7319, MAX7320-7327 I2C Port Expanders"
+	depends on I2C
+	help
+	  Say yes here to support the MAX7319, MAX7320-7327 series of I2C
+	  Port Expanders. Each IO port on these chips has a fixed role of
+	  Input (designated by 'I'), Push-Pull Output ('O'), or Open-Drain
+	  Input and Output (designed by 'P'). The combinations are listed
+	  below:
+
+	  8 bits:	max7319 (8I), max7320 (8O), max7321 (8P),
+		  	max7322 (4I4O), max7323 (4P4O)
+
+	  16 bits:	max7324 (8I8O), max7325 (8P8O),
+		  	max7326 (4I12O), max7327 (4P12O)
+
+	  Board setup code must specify the model to use, and the start
+	  number for these GPIOs.
+
 config GPIO_PCA953X
 	tristate "PCA953x, PCA955x, and MAX7310 I/O ports"
 	depends on I2C
drivers/gpio/Makefile +1 -0
@@ -5,6 +5,7 @@ ccflags-$(CONFIG_DEBUG_GPIO)	+= -DDEBUG
 obj-$(CONFIG_HAVE_GPIO_LIB)	+= gpiolib.o
 
 obj-$(CONFIG_GPIO_MAX7301)	+= max7301.o
+obj-$(CONFIG_GPIO_MAX732X)	+= max732x.o
 obj-$(CONFIG_GPIO_MCP23S08)	+= mcp23s08.o
 obj-$(CONFIG_GPIO_PCA953X)	+= pca953x.o
 obj-$(CONFIG_GPIO_PCF857X)	+= pcf857x.o
drivers/gpio/max732x.c new file +385 -0

385 lines not on this page · raw message

include/linux/i2c/max732x.h new file +19 -0
@@ -0,0 +1,19 @@
+#ifndef __LINUX_I2C_MAX732X_H
+#define __LINUX_I2C_MAX732X_H
+
+/* platform data for the MAX732x 8/16-bit I/O expander driver */
+
+struct max732x_platform_data {
+	/* number of the first GPIO */
+	unsigned	gpio_base;
+
+	void		*context;	/* param to setup/teardown */
+
+	int		(*setup)(struct i2c_client *client,
+				unsigned gpio, unsigned ngpio,
+				void *context);
+	int		(*teardown)(struct i2c_client *client,
+				unsigned gpio, unsigned ngpio,
+				void *context);
+};
+#endif /* __LINUX_I2C_MAX732X_H */
#1 lore raw
Quoted 19 lines · On Sun, 13 Jul 2008 19:18:06 -0700 David Brownell <[email protected]> wrote:

From: Eric Miao <[email protected]>

This adds a driver supporting a family of I2C port expanders from Maxim, which includes the MAX7319 and MAX7320-7327 chips.

---
We're growing quite the collection of dedicated GPIO expander drivers! And the multi-function chip support is growing too. This applies after the patch adding support for max7301 chips.

...

drivers/gpio/max732x.c new file +1 -0
+static int max732x_write(struct max732x_chip *chip, int group_a, uint8_t val)

drivers/gpio/max732x.c line 111

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+{
+	struct i2c_client *client;
+	int ret;
+
+	client = group_a ? chip->client_group_a : chip->client_group_b;
+	ret = i2c_smbus_write_byte(client, val);
+	if (ret < 0) {
+		dev_err(&client->dev, "failed writing\n");
+		return ret;
+	}

This..

Quoted 4 lines
drivers/gpio/max732x.c new file +4 -0
+	return 0;
+}
+
+static int max732x_read(struct max732x_chip *chip, int group_a, uint8_t *val)

drivers/gpio/max732x.c line 126

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+{
+	struct i2c_client *client;
+	int ret;
+
+	client = group_a ? chip->client_group_a : chip->client_group_b;
+	ret = i2c_smbus_read_byte(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "failed reading\n");
+		return ret;
+	}

and this demonstrate how weird it is that i2c_smbus_write_byte() and i2c_smbus_read_byte() return an s32' instead of plain old int'.

drivers/gpio/max732x.c line 134

Quoted 7 lines
drivers/gpio/max732x.c new file +7 -0
+	*val = (uint8_t)ret;
+	return 0;
+}
+
+static inline int is_group_a(struct max732x_chip *chip, unsigned off)
+{
+	return (1u << off) & chip->mask_group_a;

strange random unnecessary usage of "1u" in here

Quoted 6 lines
drivers/gpio/max732x.c new file +3 -0
+}
+
 
...
 
+static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)

drivers/gpio/max732x.c line 188

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+{
+	struct max732x_chip *chip;
+	unsigned int mask = 1u << off;
+
+	chip = container_of(gc, struct max732x_chip, gpio_chip);
+
+	if ((mask & chip->dir_input) == 0) {
+		dev_dbg(&chip->client->dev, "%s port %d is output only\n",
+			chip->client->name, off);
+		return -EACCES;

I don't think that EACCES is a suitable error code here. That's a security/permissions sort of thing. If userspace is requesting this driver to do something which the hardware cannot do then probably EINVAL is the appropriate return code.

Quoted 7 lines
drivers/gpio/max732x.c new file +7 -0
+	}
+
+	return 0;
+}
+
+static int max732x_gpio_direction_output(struct gpio_chip *gc,
+		unsigned off, int val)

drivers/gpio/max732x.c line 205

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+{
+	struct max732x_chip *chip;
+	unsigned int mask = 1u << off;
+
+	chip = container_of(gc, struct max732x_chip, gpio_chip);
+
+	if ((mask & chip->dir_output) == 0) {
+		dev_dbg(&chip->client->dev, "%s port %d is input only\n",
+			chip->client->name, off);
+		return -EACCES;

Ditto

Quoted 13 lines
drivers/gpio/max732x.c new file +10 -0
+	}
+
+	max732x_gpio_set_value(gc, off, val);
+	return 0;
+}
+
 
...
 
+static int __devinit max732x_probe(struct i2c_client *client,
+				   const struct i2c_device_id *id)
+{
+	struct max732x_platform_data *pdata;

drivers/gpio/max732x.c line 273

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+	struct max732x_chip *chip;
+	struct i2c_client *c;
+	uint16_t addr_a, addr_b;
+	int ret, nr_port;
+
+	pdata = client->dev.platform_data;
+	if (pdata == NULL)
+		return -ENODEV;
+
+	chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);

<goes back and checks that chip' is a struct max732x_chip*'>

I do find

	p = kmalloc(sizeof(*p), ...)

to be more conforting to read.

Quoted 12 lines
drivers/gpio/max732x.c new file +12 -0
+	if (chip == NULL)
+		return -ENOMEM;
+	chip->client = client;
+
+	nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
+
+	addr_a = (client->addr & 0x0f) | 0x60;
+	addr_b = (client->addr & 0x0f) | 0x50;
+
+	switch (client->addr & 0x70) {
+	case 0x60:
+		chip->client_group_a = client;

drivers/gpio/max732x.c line 295

Quoted 10 lines
drivers/gpio/max732x.c new file +10 -0
+		if (nr_port > 7) {
+			c = i2c_new_dummy(client->adapter, addr_b);
+			chip->client_group_b = chip->client_dummy = c;
+		}
+		break;
+	case 0x50:
+		chip->client_group_b = client;
+		if (nr_port > 7) {
+			c = i2c_new_dummy(client->adapter, addr_a);
+			chip->client_group_a = chip->client_dummy = c;

Multiple assignments get nasty comments from Linus when he's feeling perky.

Quoted 26 lines
drivers/gpio/max732x.c new file +25 -0
+		}
+		break;
+	default:
+		dev_err(&client->dev, "invalid I2C address specified %02x\n",
+				client->addr);
+		ret = -EINVAL;
+		goto out_failed;
+	}
+
+	mutex_init(&chip->lock);
+
+	max732x_read(chip, is_group_a(chip, 0), &chip->reg_out[0]);
+	if (nr_port > 7)
+		max732x_read(chip, is_group_a(chip, 8), &chip->reg_out[1]);
+
+	ret = gpiochip_add(&chip->gpio_chip);
+	if (ret)
+		goto out_failed;
+
+	if (pdata->setup) {
+		ret = pdata->setup(client, chip->gpio_chip.base,
+				chip->gpio_chip.ngpio, pdata->context);
+		if (ret < 0)
+			dev_warn(&client->dev, "setup failed, %d\n", ret);
+	}

drivers/gpio/max732x.c

Quoted 10 lines
drivers/gpio/max732x.c new file +8 -0
+
+	i2c_set_clientdata(client, chip);
+	return 0;
+
+out_failed:
+	kfree(chip);
+	return ret;
+}
 
...

But that's all very minor stuff. Nice-looking driver.

#2 lore raw
Quoted 9 lines · Andrew Morton
Quoted 4 lines

+     if ((mask & chip->dir_input) == 0) {
+             dev_dbg(&chip->client->dev, "%s port %d is output only\n", +                     chip->client->name, off);
+             return -EACCES;

I don't think that EACCES is a suitable error code here.  That's a security/permissions sort of thing.  If userspace is requesting this driver to do something which the hardware cannot do then probably EINVAL is the appropriate return code.

EINVAL is one of the most ambiguous return codes available. It's worth avoiding whenever there's *ANY* more meaningful code! In this case, the gpio_direction_input() call is good about returning that fault code only for bogus GPIO numbers.

EACCES is what you should get trying to open a write-only file for reading, which seems like a very direct analogy. That argument is just fine... it's the particular access request which must be denied, in this case because hardware itself disallows it rather than a bit in an inode.

- Dave