ASoC: ep93xx i2s audio driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / max730x.c
blob7696a5625d58c1944d8e15a72cf0f4768ff3f550
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>
41 #include <linux/slab.h>
44 * Pin configurations, see MAX7301 datasheet page 6
46 #define PIN_CONFIG_MASK 0x03
47 #define PIN_CONFIG_IN_PULLUP 0x03
48 #define PIN_CONFIG_IN_WO_PULLUP 0x02
49 #define PIN_CONFIG_OUT 0x01
51 #define PIN_NUMBER 28
53 static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
55 struct max7301 *ts = container_of(chip, struct max7301, chip);
56 u8 *config;
57 u8 offset_bits;
58 int ret;
60 /* First 4 pins are unused in the controller */
61 offset += 4;
62 offset_bits = (offset & 3) << 1;
64 config = &ts->port_config[offset >> 2];
66 mutex_lock(&ts->lock);
68 /* Standard GPIO API doesn't support pull-ups, has to be extended.
69 * Hard-coding no pollup for now. */
70 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
71 | (PIN_CONFIG_IN_WO_PULLUP << offset_bits);
73 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
75 mutex_unlock(&ts->lock);
77 return ret;
80 static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
82 if (value) {
83 ts->out_level |= 1 << offset;
84 return ts->write(ts->dev, 0x20 + offset, 0x01);
85 } else {
86 ts->out_level &= ~(1 << offset);
87 return ts->write(ts->dev, 0x20 + offset, 0x00);
91 static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
92 int value)
94 struct max7301 *ts = container_of(chip, struct max7301, chip);
95 u8 *config;
96 u8 offset_bits;
97 int ret;
99 /* First 4 pins are unused in the controller */
100 offset += 4;
101 offset_bits = (offset & 3) << 1;
103 config = &ts->port_config[offset >> 2];
105 mutex_lock(&ts->lock);
107 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
108 | (PIN_CONFIG_OUT << offset_bits);
110 ret = __max7301_set(ts, offset, value);
112 if (!ret)
113 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
115 mutex_unlock(&ts->lock);
117 return ret;
120 static int max7301_get(struct gpio_chip *chip, unsigned offset)
122 struct max7301 *ts = container_of(chip, struct max7301, chip);
123 int config, level = -EINVAL;
125 /* First 4 pins are unused in the controller */
126 offset += 4;
128 mutex_lock(&ts->lock);
130 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
131 & PIN_CONFIG_MASK;
133 switch (config) {
134 case PIN_CONFIG_OUT:
135 /* Output: return cached level */
136 level = !!(ts->out_level & (1 << offset));
137 break;
138 case PIN_CONFIG_IN_WO_PULLUP:
139 case PIN_CONFIG_IN_PULLUP:
140 /* Input: read out */
141 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
143 mutex_unlock(&ts->lock);
145 return level;
148 static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
150 struct max7301 *ts = container_of(chip, struct max7301, chip);
152 /* First 4 pins are unused in the controller */
153 offset += 4;
155 mutex_lock(&ts->lock);
157 __max7301_set(ts, offset, value);
159 mutex_unlock(&ts->lock);
162 int __devinit __max730x_probe(struct max7301 *ts)
164 struct device *dev = ts->dev;
165 struct max7301_platform_data *pdata;
166 int i, ret;
168 pdata = dev->platform_data;
169 if (!pdata || !pdata->base) {
170 dev_err(dev, "incorrect or missing platform data\n");
171 return -EINVAL;
174 mutex_init(&ts->lock);
175 dev_set_drvdata(dev, ts);
177 /* Power up the chip and disable IRQ output */
178 ts->write(dev, 0x04, 0x01);
180 ts->chip.label = dev->driver->name;
182 ts->chip.direction_input = max7301_direction_input;
183 ts->chip.get = max7301_get;
184 ts->chip.direction_output = max7301_direction_output;
185 ts->chip.set = max7301_set;
187 ts->chip.base = pdata->base;
188 ts->chip.ngpio = PIN_NUMBER;
189 ts->chip.can_sleep = 1;
190 ts->chip.dev = dev;
191 ts->chip.owner = THIS_MODULE;
194 * tristate all pins in hardware and cache the
195 * register values for later use.
197 for (i = 1; i < 8; i++) {
198 int j;
199 /* 0xAA means input with internal pullup disabled */
200 ts->write(dev, 0x08 + i, 0xAA);
201 ts->port_config[i] = 0xAA;
202 for (j = 0; j < 4; j++) {
203 int offset = (i - 1) * 4 + j;
204 ret = max7301_direction_input(&ts->chip, offset);
205 if (ret)
206 goto exit_destroy;
210 ret = gpiochip_add(&ts->chip);
211 if (ret)
212 goto exit_destroy;
214 return ret;
216 exit_destroy:
217 dev_set_drvdata(dev, NULL);
218 mutex_destroy(&ts->lock);
219 return ret;
221 EXPORT_SYMBOL_GPL(__max730x_probe);
223 int __devexit __max730x_remove(struct device *dev)
225 struct max7301 *ts = dev_get_drvdata(dev);
226 int ret;
228 if (ts == NULL)
229 return -ENODEV;
231 dev_set_drvdata(dev, NULL);
233 /* Power down the chip and disable IRQ output */
234 ts->write(dev, 0x04, 0x00);
236 ret = gpiochip_remove(&ts->chip);
237 if (!ret) {
238 mutex_destroy(&ts->lock);
239 kfree(ts);
240 } else
241 dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
243 return ret;
245 EXPORT_SYMBOL_GPL(__max730x_remove);
247 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
248 MODULE_LICENSE("GPL v2");
249 MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");