blob: c9bced55f82bc501288d90f8d5536b27e6e9a848 [file] [log] [blame]
Wolfram Sange9528052010-03-05 13:44:33 -08001/**
2 * drivers/gpio/max7301.c
3 *
4 * Copyright (C) 2006 Juergen Beisert, Pengutronix
5 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
6 * Copyright (C) 2009 Wolfram Sang, Pengutronix
7 *
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.
11 *
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
31 *
32 * The driver exports a standard gpiochip interface
33 */
34
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
42/*
43 * Pin configurations, see MAX7301 datasheet page 6
44 */
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
49
50#define PIN_NUMBER 28
51
52static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
53{
54 struct max7301 *ts = container_of(chip, struct max7301, chip);
55 u8 *config;
56 u8 offset_bits;
57 int ret;
58
59 /* First 4 pins are unused in the controller */
60 offset += 4;
61 offset_bits = (offset & 3) << 1;
62
63 config = &ts->port_config[offset >> 2];
64
65 mutex_lock(&ts->lock);
66
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);
71
72 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
73
74 mutex_unlock(&ts->lock);
75
76 return ret;
77}
78
79static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
80{
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);
87 }
88}
89
90static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
91 int value)
92{
93 struct max7301 *ts = container_of(chip, struct max7301, chip);
94 u8 *config;
95 u8 offset_bits;
96 int ret;
97
98 /* First 4 pins are unused in the controller */
99 offset += 4;
100 offset_bits = (offset & 3) << 1;
101
102 config = &ts->port_config[offset >> 2];
103
104 mutex_lock(&ts->lock);
105
106 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
107 | (PIN_CONFIG_OUT << offset_bits);
108
109 ret = __max7301_set(ts, offset, value);
110
111 if (!ret)
112 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
113
114 mutex_unlock(&ts->lock);
115
116 return ret;
117}
118
119static int max7301_get(struct gpio_chip *chip, unsigned offset)
120{
121 struct max7301 *ts = container_of(chip, struct max7301, chip);
122 int config, level = -EINVAL;
123
124 /* First 4 pins are unused in the controller */
125 offset += 4;
126
127 mutex_lock(&ts->lock);
128
129 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
130 & PIN_CONFIG_MASK;
131
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;
141 }
142 mutex_unlock(&ts->lock);
143
144 return level;
145}
146
147static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
148{
149 struct max7301 *ts = container_of(chip, struct max7301, chip);
150
151 /* First 4 pins are unused in the controller */
152 offset += 4;
153
154 mutex_lock(&ts->lock);
155
156 __max7301_set(ts, offset, value);
157
158 mutex_unlock(&ts->lock);
159}
160
161int __devinit __max730x_probe(struct max7301 *ts)
162{
163 struct device *dev = ts->dev;
164 struct max7301_platform_data *pdata;
165 int i, ret;
166
167 pdata = dev->platform_data;
168 if (!pdata || !pdata->base) {
169 dev_err(dev, "incorrect or missing platform data\n");
170 return -EINVAL;
171 }
172
173 mutex_init(&ts->lock);
174 dev_set_drvdata(dev, ts);
175
176 /* Power up the chip and disable IRQ output */
177 ts->write(dev, 0x04, 0x01);
178
179 ts->chip.label = dev->driver->name;
180
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;
185
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;
191
192 /*
193 * tristate all pins in hardware and cache the
194 * register values for later use.
195 */
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;
206 }
207 }
208
209 ret = gpiochip_add(&ts->chip);
210 if (ret)
211 goto exit_destroy;
212
213 return ret;
214
215exit_destroy:
216 dev_set_drvdata(dev, NULL);
217 mutex_destroy(&ts->lock);
218 return ret;
219}
220EXPORT_SYMBOL_GPL(__max730x_probe);
221
222int __devexit __max730x_remove(struct device *dev)
223{
224 struct max7301 *ts = dev_get_drvdata(dev);
225 int ret;
226
227 if (ts == NULL)
228 return -ENODEV;
229
230 dev_set_drvdata(dev, NULL);
231
232 /* Power down the chip and disable IRQ output */
233 ts->write(dev, 0x04, 0x00);
234
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);
241
242 return ret;
243}
244EXPORT_SYMBOL_GPL(__max730x_remove);