1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/platform_device.h>
36#include <linux/mutex.h>
37#include <linux/spi/max7301.h>
38#include <linux/gpio.h>
39#include <linux/slab.h>
40
41
42
43
44#define PIN_CONFIG_MASK 0x03
45#define PIN_CONFIG_IN_PULLUP 0x03
46#define PIN_CONFIG_IN_WO_PULLUP 0x02
47#define PIN_CONFIG_OUT 0x01
48
49#define PIN_NUMBER 28
50
51static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
52{
53 struct max7301 *ts = container_of(chip, struct max7301, chip);
54 u8 *config;
55 u8 offset_bits, pin_config;
56 int ret;
57
58
59 offset += 4;
60 offset_bits = (offset & 3) << 1;
61
62 config = &ts->port_config[offset >> 2];
63
64 if (ts->input_pullup_active & BIT(offset))
65 pin_config = PIN_CONFIG_IN_PULLUP;
66 else
67 pin_config = PIN_CONFIG_IN_WO_PULLUP;
68
69 mutex_lock(&ts->lock);
70
71 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
72 | (pin_config << offset_bits);
73
74 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
75
76 mutex_unlock(&ts->lock);
77
78 return ret;
79}
80
81static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
82{
83 if (value) {
84 ts->out_level |= 1 << offset;
85 return ts->write(ts->dev, 0x20 + offset, 0x01);
86 } else {
87 ts->out_level &= ~(1 << offset);
88 return ts->write(ts->dev, 0x20 + offset, 0x00);
89 }
90}
91
92static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
93 int value)
94{
95 struct max7301 *ts = container_of(chip, struct max7301, chip);
96 u8 *config;
97 u8 offset_bits;
98 int ret;
99
100
101 offset += 4;
102 offset_bits = (offset & 3) << 1;
103
104 config = &ts->port_config[offset >> 2];
105
106 mutex_lock(&ts->lock);
107
108 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
109 | (PIN_CONFIG_OUT << offset_bits);
110
111 ret = __max7301_set(ts, offset, value);
112
113 if (!ret)
114 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
115
116 mutex_unlock(&ts->lock);
117
118 return ret;
119}
120
121static int max7301_get(struct gpio_chip *chip, unsigned offset)
122{
123 struct max7301 *ts = container_of(chip, struct max7301, chip);
124 int config, level = -EINVAL;
125
126
127 offset += 4;
128
129 mutex_lock(&ts->lock);
130
131 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
132 & PIN_CONFIG_MASK;
133
134 switch (config) {
135 case PIN_CONFIG_OUT:
136
137 level = !!(ts->out_level & (1 << offset));
138 break;
139 case PIN_CONFIG_IN_WO_PULLUP:
140 case PIN_CONFIG_IN_PULLUP:
141
142 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
143 }
144 mutex_unlock(&ts->lock);
145
146 return level;
147}
148
149static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
150{
151 struct max7301 *ts = container_of(chip, struct max7301, chip);
152
153
154 offset += 4;
155
156 mutex_lock(&ts->lock);
157
158 __max7301_set(ts, offset, value);
159
160 mutex_unlock(&ts->lock);
161}
162
163int __devinit __max730x_probe(struct max7301 *ts)
164{
165 struct device *dev = ts->dev;
166 struct max7301_platform_data *pdata;
167 int i, ret;
168
169 pdata = dev->platform_data;
170 if (!pdata || !pdata->base) {
171 dev_err(dev, "incorrect or missing platform data\n");
172 return -EINVAL;
173 }
174
175 mutex_init(&ts->lock);
176 dev_set_drvdata(dev, ts);
177
178
179 ts->write(dev, 0x04, 0x01);
180
181 ts->input_pullup_active = pdata->input_pullup_active;
182 ts->chip.label = dev->driver->name;
183
184 ts->chip.direction_input = max7301_direction_input;
185 ts->chip.get = max7301_get;
186 ts->chip.direction_output = max7301_direction_output;
187 ts->chip.set = max7301_set;
188
189 ts->chip.base = pdata->base;
190 ts->chip.ngpio = PIN_NUMBER;
191 ts->chip.can_sleep = 1;
192 ts->chip.dev = dev;
193 ts->chip.owner = THIS_MODULE;
194
195
196
197
198
199 for (i = 1; i < 8; i++) {
200 int j;
201
202
203
204
205
206
207 ts->port_config[i] = 0xAA;
208 for (j = 0; j < 4; j++) {
209 int offset = (i - 1) * 4 + j;
210 ret = max7301_direction_input(&ts->chip, offset);
211 if (ret)
212 goto exit_destroy;
213 }
214 }
215
216 ret = gpiochip_add(&ts->chip);
217 if (ret)
218 goto exit_destroy;
219
220 return ret;
221
222exit_destroy:
223 dev_set_drvdata(dev, NULL);
224 mutex_destroy(&ts->lock);
225 return ret;
226}
227EXPORT_SYMBOL_GPL(__max730x_probe);
228
229int __devexit __max730x_remove(struct device *dev)
230{
231 struct max7301 *ts = dev_get_drvdata(dev);
232 int ret;
233
234 if (ts == NULL)
235 return -ENODEV;
236
237 dev_set_drvdata(dev, NULL);
238
239
240 ts->write(dev, 0x04, 0x00);
241
242 ret = gpiochip_remove(&ts->chip);
243 if (!ret) {
244 mutex_destroy(&ts->lock);
245 kfree(ts);
246 } else
247 dev_err(dev, "Failed to remove GPIO controller: %d\n", ret);
248
249 return ret;
250}
251EXPORT_SYMBOL_GPL(__max730x_remove);
252
253MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
254MODULE_LICENSE("GPL v2");
255MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");
256