2 * Copyright (C) 2006 Juergen Beisert, Pengutronix
3 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
4 * Copyright (C) 2009 Wolfram Sang, Pengutronix
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * The Maxim MAX7300/1 device is an I2C/SPI driven GPIO expander. There are
11 * 28 GPIOs. 8 of them can trigger an interrupt. See datasheet for more
14 * - DIN must be stable at the rising edge of clock.
16 * - always clock in 16 clocks at once
17 * - at DIN: D15 first, D0 last
18 * - D0..D7 = databyte, D8..D14 = commandbyte
19 * - D15 = low -> write command
21 * - always clock in 16 clocks at once
22 * - at DIN: D15 first, D0 last
23 * - D0..D7 = dummy, D8..D14 = register address
24 * - D15 = high -> read command
25 * - raise CS and assert it again
26 * - always clock in 16 clocks at once
27 * - at DOUT: D15 first, D0 last
28 * - D0..D7 contains the data from the first cycle
30 * The driver exports a standard gpiochip interface
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>
42 * Pin configurations, see MAX7301 datasheet page 6
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
51 static int max7301_direction_input(struct gpio_chip
*chip
, unsigned offset
)
53 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
55 u8 offset_bits
, pin_config
;
58 /* First 4 pins are unused in the controller */
60 offset_bits
= (offset
& 3) << 1;
62 config
= &ts
->port_config
[offset
>> 2];
64 if (ts
->input_pullup_active
& BIT(offset
))
65 pin_config
= PIN_CONFIG_IN_PULLUP
;
67 pin_config
= PIN_CONFIG_IN_WO_PULLUP
;
69 mutex_lock(&ts
->lock
);
71 *config
= (*config
& ~(PIN_CONFIG_MASK
<< offset_bits
))
72 | (pin_config
<< offset_bits
);
74 ret
= ts
->write(ts
->dev
, 0x08 + (offset
>> 2), *config
);
76 mutex_unlock(&ts
->lock
);
81 static int __max7301_set(struct max7301
*ts
, unsigned offset
, int value
)
84 ts
->out_level
|= 1 << offset
;
85 return ts
->write(ts
->dev
, 0x20 + offset
, 0x01);
87 ts
->out_level
&= ~(1 << offset
);
88 return ts
->write(ts
->dev
, 0x20 + offset
, 0x00);
92 static int max7301_direction_output(struct gpio_chip
*chip
, unsigned offset
,
95 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
100 /* First 4 pins are unused in the controller */
102 offset_bits
= (offset
& 3) << 1;
104 config
= &ts
->port_config
[offset
>> 2];
106 mutex_lock(&ts
->lock
);
108 *config
= (*config
& ~(PIN_CONFIG_MASK
<< offset_bits
))
109 | (PIN_CONFIG_OUT
<< offset_bits
);
111 ret
= __max7301_set(ts
, offset
, value
);
114 ret
= ts
->write(ts
->dev
, 0x08 + (offset
>> 2), *config
);
116 mutex_unlock(&ts
->lock
);
121 static int max7301_get(struct gpio_chip
*chip
, unsigned offset
)
123 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
124 int config
, level
= -EINVAL
;
126 /* First 4 pins are unused in the controller */
129 mutex_lock(&ts
->lock
);
131 config
= (ts
->port_config
[offset
>> 2] >> ((offset
& 3) << 1))
136 /* Output: return cached level */
137 level
= !!(ts
->out_level
& (1 << offset
));
139 case PIN_CONFIG_IN_WO_PULLUP
:
140 case PIN_CONFIG_IN_PULLUP
:
141 /* Input: read out */
142 level
= ts
->read(ts
->dev
, 0x20 + offset
) & 0x01;
144 mutex_unlock(&ts
->lock
);
149 static void max7301_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
151 struct max7301
*ts
= container_of(chip
, struct max7301
, chip
);
153 /* First 4 pins are unused in the controller */
156 mutex_lock(&ts
->lock
);
158 __max7301_set(ts
, offset
, value
);
160 mutex_unlock(&ts
->lock
);
163 int __max730x_probe(struct max7301
*ts
)
165 struct device
*dev
= ts
->dev
;
166 struct max7301_platform_data
*pdata
;
169 pdata
= dev
->platform_data
;
171 mutex_init(&ts
->lock
);
172 dev_set_drvdata(dev
, ts
);
174 /* Power up the chip and disable IRQ output */
175 ts
->write(dev
, 0x04, 0x01);
178 ts
->input_pullup_active
= pdata
->input_pullup_active
;
179 ts
->chip
.base
= pdata
->base
;
183 ts
->chip
.label
= dev
->driver
->name
;
185 ts
->chip
.direction_input
= max7301_direction_input
;
186 ts
->chip
.get
= max7301_get
;
187 ts
->chip
.direction_output
= max7301_direction_output
;
188 ts
->chip
.set
= max7301_set
;
190 ts
->chip
.ngpio
= PIN_NUMBER
;
191 ts
->chip
.can_sleep
= 1;
193 ts
->chip
.owner
= THIS_MODULE
;
196 * initialize pullups according to platform data and cache the
197 * register values for later use.
199 for (i
= 1; i
< 8; i
++) {
202 * initialize port_config with "0xAA", which means
203 * input with internal pullup disabled. This is needed
204 * to avoid writing zeros (in the inner for loop),
205 * which is not allowed according to the datasheet.
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
);
216 ret
= gpiochip_add(&ts
->chip
);
223 dev_set_drvdata(dev
, NULL
);
224 mutex_destroy(&ts
->lock
);
227 EXPORT_SYMBOL_GPL(__max730x_probe
);
229 int __max730x_remove(struct device
*dev
)
231 struct max7301
*ts
= dev_get_drvdata(dev
);
237 dev_set_drvdata(dev
, NULL
);
239 /* Power down the chip and disable IRQ output */
240 ts
->write(dev
, 0x04, 0x00);
242 ret
= gpiochip_remove(&ts
->chip
);
244 mutex_destroy(&ts
->lock
);
247 dev_err(dev
, "Failed to remove GPIO controller: %d\n", ret
);
251 EXPORT_SYMBOL_GPL(__max730x_remove
);
253 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
254 MODULE_LICENSE("GPL v2");
255 MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");