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
16 * - DIN must be stable at the rising edge of clock.
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
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
53 static int max7301_direction_input(struct gpio_chip
*chip
, unsigned offset
)
55 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
57 u8 offset_bits
, pin_config
;
60 /* First 4 pins are unused in the controller */
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
;
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
);
83 static int __max7301_set(struct max7301
*ts
, unsigned offset
, int value
)
86 ts
->out_level
|= 1 << offset
;
87 return ts
->write(ts
->dev
, 0x20 + offset
, 0x01);
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
,
97 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
102 /* First 4 pins are unused in the controller */
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
);
116 ret
= ts
->write(ts
->dev
, 0x08 + (offset
>> 2), *config
);
118 mutex_unlock(&ts
->lock
);
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 */
131 mutex_lock(&ts
->lock
);
133 config
= (ts
->port_config
[offset
>> 2] >> ((offset
& 3) << 1))
138 /* Output: return cached level */
139 level
= !!(ts
->out_level
& (1 << offset
));
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
);
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 */
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
;
171 pdata
= dev
->platform_data
;
172 if (!pdata
|| !pdata
->base
) {
173 dev_err(dev
, "incorrect or missing platform data\n");
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;
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
++) {
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
);
218 ret
= gpiochip_add(&ts
->chip
);
225 dev_set_drvdata(dev
, NULL
);
226 mutex_destroy(&ts
->lock
);
229 EXPORT_SYMBOL_GPL(__max730x_probe
);
231 int __devexit
__max730x_remove(struct device
*dev
)
233 struct max7301
*ts
= dev_get_drvdata(dev
);
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
);
246 mutex_destroy(&ts
->lock
);
249 dev_err(dev
, "Failed to remove GPIO controller: %d\n", 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");