GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / gpio / max730x.c
blob94ce773f95f8eebb7a54e09c7c6e2bb00b58128f
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, pin_config;
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 if (ts->input_pullup_active & BIT(offset))
67 pin_config = PIN_CONFIG_IN_PULLUP;
68 else
69 pin_config = PIN_CONFIG_IN_WO_PULLUP;
71 mutex_lock(&ts->lock);
73 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
74 | (pin_config << offset_bits);
76 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
78 mutex_unlock(&ts->lock);
80 return ret;
83 static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
85 if (value) {
86 ts->out_level |= 1 << offset;
87 return ts->write(ts->dev, 0x20 + offset, 0x01);
88 } else {
89 ts->out_level &= ~(1 << offset);
90 return ts->write(ts->dev, 0x20 + offset, 0x00);
94 static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
95 int value)
97 struct max7301 *ts = container_of(chip, struct max7301, chip);
98 u8 *config;
99 u8 offset_bits;
100 int ret;
102 /* First 4 pins are unused in the controller */
103 offset += 4;
104 offset_bits = (offset & 3) << 1;
106 config = &ts->port_config[offset >> 2];
108 mutex_lock(&ts->lock);
110 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
111 | (PIN_CONFIG_OUT << offset_bits);
113 ret = __max7301_set(ts, offset, value);
115 if (!ret)
116 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
118 mutex_unlock(&ts->lock);
120 return ret;
123 static int max7301_get(struct gpio_chip *chip, unsigned offset)
125 struct max7301 *ts = container_of(chip, struct max7301, chip);
126 int config, level = -EINVAL;
128 /* First 4 pins are unused in the controller */
129 offset += 4;
131 mutex_lock(&ts->lock);
133 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
134 & PIN_CONFIG_MASK;
136 switch (config) {
137 case PIN_CONFIG_OUT:
138 /* Output: return cached level */
139 level = !!(ts->out_level & (1 << offset));
140 break;
141 case PIN_CONFIG_IN_WO_PULLUP:
142 case PIN_CONFIG_IN_PULLUP:
143 /* Input: read out */
144 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
146 mutex_unlock(&ts->lock);
148 return level;
151 static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
153 struct max7301 *ts = container_of(chip, struct max7301, chip);
155 /* First 4 pins are unused in the controller */
156 offset += 4;
158 mutex_lock(&ts->lock);
160 __max7301_set(ts, offset, value);
162 mutex_unlock(&ts->lock);
165 int __devinit __max730x_probe(struct max7301 *ts)
167 struct device *dev = ts->dev;
168 struct max7301_platform_data *pdata;
169 int i, ret;
171 pdata = dev->platform_data;
172 if (!pdata || !pdata->base) {
173 dev_err(dev, "incorrect or missing platform data\n");
174 return -EINVAL;
177 mutex_init(&ts->lock);
178 dev_set_drvdata(dev, ts);
180 /* Power up the chip and disable IRQ output */
181 ts->write(dev, 0x04, 0x01);
183 ts->input_pullup_active = pdata->input_pullup_active;
184 ts->chip.label = dev->driver->name;
186 ts->chip.direction_input = max7301_direction_input;
187 ts->chip.get = max7301_get;
188 ts->chip.direction_output = max7301_direction_output;
189 ts->chip.set = max7301_set;
191 ts->chip.base = pdata->base;
192 ts->chip.ngpio = PIN_NUMBER;
193 ts->chip.can_sleep = 1;
194 ts->chip.dev = dev;
195 ts->chip.owner = THIS_MODULE;
198 * initialize pullups according to platform data and cache the
199 * register values for later use.
201 for (i = 1; i < 8; i++) {
202 int j;
204 * initialize port_config with "0xAA", which means
205 * input with internal pullup disabled. This is needed
206 * to avoid writing zeros (in the inner for loop),
207 * which is not allowed according to the datasheet.
209 ts->port_config[i] = 0xAA;
210 for (j = 0; j < 4; j++) {
211 int offset = (i - 1) * 4 + j;
212 ret = max7301_direction_input(&ts->chip, offset);
213 if (ret)
214 goto exit_destroy;
218 ret = gpiochip_add(&ts->chip);
219 if (ret)
220 goto exit_destroy;
222 return ret;
224 exit_destroy:
225 dev_set_drvdata(dev, NULL);
226 mutex_destroy(&ts->lock);
227 return ret;
229 EXPORT_SYMBOL_GPL(__max730x_probe);
231 int __devexit __max730x_remove(struct device *dev)
233 struct max7301 *ts = dev_get_drvdata(dev);
234 int ret;
236 if (ts == NULL)
237 return -ENODEV;
239 dev_set_drvdata(dev, NULL);
241 /* Power down the chip and disable IRQ output */
242 ts->write(dev, 0x04, 0x00);
244 ret = gpiochip_remove(&ts->chip);
245 if (!ret) {
246 mutex_destroy(&ts->lock);
247 kfree(ts);
248 } else
249 dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
251 return ret;
253 EXPORT_SYMBOL_GPL(__max730x_remove);
255 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
256 MODULE_LICENSE("GPL v2");
257 MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");