gpio: add driver for MAX7300 I2C GPIO extender
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / max730x.c
blobc9bced55f82bc501288d90f8d5536b27e6e9a848
1 /**
2 * drivers/gpio/max7301.c
4 * Copyright (C) 2006 Juergen Beisert, Pengutronix
5 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
6 * Copyright (C) 2009 Wolfram Sang, Pengutronix
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * The Maxim MAX7300/1 device is an I2C/SPI driven GPIO expander. There are
13 * 28 GPIOs. 8 of them can trigger an interrupt. See datasheet for more
14 * details
15 * Note:
16 * - DIN must be stable at the rising edge of clock.
17 * - when writing:
18 * - always clock in 16 clocks at once
19 * - at DIN: D15 first, D0 last
20 * - D0..D7 = databyte, D8..D14 = commandbyte
21 * - D15 = low -> write command
22 * - when reading
23 * - always clock in 16 clocks at once
24 * - at DIN: D15 first, D0 last
25 * - D0..D7 = dummy, D8..D14 = register address
26 * - D15 = high -> read command
27 * - raise CS and assert it again
28 * - always clock in 16 clocks at once
29 * - at DOUT: D15 first, D0 last
30 * - D0..D7 contains the data from the first cycle
32 * The driver exports a standard gpiochip interface
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/platform_device.h>
38 #include <linux/mutex.h>
39 #include <linux/spi/max7301.h>
40 #include <linux/gpio.h>
43 * Pin configurations, see MAX7301 datasheet page 6
45 #define PIN_CONFIG_MASK 0x03
46 #define PIN_CONFIG_IN_PULLUP 0x03
47 #define PIN_CONFIG_IN_WO_PULLUP 0x02
48 #define PIN_CONFIG_OUT 0x01
50 #define PIN_NUMBER 28
52 static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
54 struct max7301 *ts = container_of(chip, struct max7301, chip);
55 u8 *config;
56 u8 offset_bits;
57 int ret;
59 /* First 4 pins are unused in the controller */
60 offset += 4;
61 offset_bits = (offset & 3) << 1;
63 config = &ts->port_config[offset >> 2];
65 mutex_lock(&ts->lock);
67 /* Standard GPIO API doesn't support pull-ups, has to be extended.
68 * Hard-coding no pollup for now. */
69 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
70 | (PIN_CONFIG_IN_WO_PULLUP << offset_bits);
72 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
74 mutex_unlock(&ts->lock);
76 return ret;
79 static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
81 if (value) {
82 ts->out_level |= 1 << offset;
83 return ts->write(ts->dev, 0x20 + offset, 0x01);
84 } else {
85 ts->out_level &= ~(1 << offset);
86 return ts->write(ts->dev, 0x20 + offset, 0x00);
90 static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
91 int value)
93 struct max7301 *ts = container_of(chip, struct max7301, chip);
94 u8 *config;
95 u8 offset_bits;
96 int ret;
98 /* First 4 pins are unused in the controller */
99 offset += 4;
100 offset_bits = (offset & 3) << 1;
102 config = &ts->port_config[offset >> 2];
104 mutex_lock(&ts->lock);
106 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
107 | (PIN_CONFIG_OUT << offset_bits);
109 ret = __max7301_set(ts, offset, value);
111 if (!ret)
112 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
114 mutex_unlock(&ts->lock);
116 return ret;
119 static int max7301_get(struct gpio_chip *chip, unsigned offset)
121 struct max7301 *ts = container_of(chip, struct max7301, chip);
122 int config, level = -EINVAL;
124 /* First 4 pins are unused in the controller */
125 offset += 4;
127 mutex_lock(&ts->lock);
129 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
130 & PIN_CONFIG_MASK;
132 switch (config) {
133 case PIN_CONFIG_OUT:
134 /* Output: return cached level */
135 level = !!(ts->out_level & (1 << offset));
136 break;
137 case PIN_CONFIG_IN_WO_PULLUP:
138 case PIN_CONFIG_IN_PULLUP:
139 /* Input: read out */
140 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
142 mutex_unlock(&ts->lock);
144 return level;
147 static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
149 struct max7301 *ts = container_of(chip, struct max7301, chip);
151 /* First 4 pins are unused in the controller */
152 offset += 4;
154 mutex_lock(&ts->lock);
156 __max7301_set(ts, offset, value);
158 mutex_unlock(&ts->lock);
161 int __devinit __max730x_probe(struct max7301 *ts)
163 struct device *dev = ts->dev;
164 struct max7301_platform_data *pdata;
165 int i, ret;
167 pdata = dev->platform_data;
168 if (!pdata || !pdata->base) {
169 dev_err(dev, "incorrect or missing platform data\n");
170 return -EINVAL;
173 mutex_init(&ts->lock);
174 dev_set_drvdata(dev, ts);
176 /* Power up the chip and disable IRQ output */
177 ts->write(dev, 0x04, 0x01);
179 ts->chip.label = dev->driver->name;
181 ts->chip.direction_input = max7301_direction_input;
182 ts->chip.get = max7301_get;
183 ts->chip.direction_output = max7301_direction_output;
184 ts->chip.set = max7301_set;
186 ts->chip.base = pdata->base;
187 ts->chip.ngpio = PIN_NUMBER;
188 ts->chip.can_sleep = 1;
189 ts->chip.dev = dev;
190 ts->chip.owner = THIS_MODULE;
193 * tristate all pins in hardware and cache the
194 * register values for later use.
196 for (i = 1; i < 8; i++) {
197 int j;
198 /* 0xAA means input with internal pullup disabled */
199 ts->write(dev, 0x08 + i, 0xAA);
200 ts->port_config[i] = 0xAA;
201 for (j = 0; j < 4; j++) {
202 int offset = (i - 1) * 4 + j;
203 ret = max7301_direction_input(&ts->chip, offset);
204 if (ret)
205 goto exit_destroy;
209 ret = gpiochip_add(&ts->chip);
210 if (ret)
211 goto exit_destroy;
213 return ret;
215 exit_destroy:
216 dev_set_drvdata(dev, NULL);
217 mutex_destroy(&ts->lock);
218 return ret;
220 EXPORT_SYMBOL_GPL(__max730x_probe);
222 int __devexit __max730x_remove(struct device *dev)
224 struct max7301 *ts = dev_get_drvdata(dev);
225 int ret;
227 if (ts == NULL)
228 return -ENODEV;
230 dev_set_drvdata(dev, NULL);
232 /* Power down the chip and disable IRQ output */
233 ts->write(dev, 0x04, 0x00);
235 ret = gpiochip_remove(&ts->chip);
236 if (!ret) {
237 mutex_destroy(&ts->lock);
238 kfree(ts);
239 } else
240 dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
242 return ret;
244 EXPORT_SYMBOL_GPL(__max730x_remove);