1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006 Juergen Beisert, Pengutronix
4 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
5 * Copyright (C) 2009 Wolfram Sang, Pengutronix
7 * The Maxim MAX7300/1 device is an I2C/SPI driven GPIO expander. There are
8 * 28 GPIOs. 8 of them can trigger an interrupt. See datasheet for more
11 * - DIN must be stable at the rising edge of clock.
13 * - always clock in 16 clocks at once
14 * - at DIN: D15 first, D0 last
15 * - D0..D7 = databyte, D8..D14 = commandbyte
16 * - D15 = low -> write command
18 * - always clock in 16 clocks at once
19 * - at DIN: D15 first, D0 last
20 * - D0..D7 = dummy, D8..D14 = register address
21 * - D15 = high -> read command
22 * - raise CS and assert it again
23 * - always clock in 16 clocks at once
24 * - at DOUT: D15 first, D0 last
25 * - D0..D7 contains the data from the first cycle
27 * The driver exports a standard gpiochip interface
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/platform_device.h>
33 #include <linux/mutex.h>
34 #include <linux/spi/max7301.h>
35 #include <linux/gpio/driver.h>
36 #include <linux/slab.h>
39 * Pin configurations, see MAX7301 datasheet page 6
41 #define PIN_CONFIG_MASK 0x03
42 #define PIN_CONFIG_IN_PULLUP 0x03
43 #define PIN_CONFIG_IN_WO_PULLUP 0x02
44 #define PIN_CONFIG_OUT 0x01
48 static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
50 struct max7301 *ts = container_of(chip, struct max7301, chip);
52 u8 offset_bits, pin_config;
55 /* First 4 pins are unused in the controller */
57 offset_bits = (offset & 3) << 1;
59 config = &ts->port_config[offset >> 2];
61 if (ts->input_pullup_active & BIT(offset))
62 pin_config = PIN_CONFIG_IN_PULLUP;
64 pin_config = PIN_CONFIG_IN_WO_PULLUP;
66 mutex_lock(&ts->lock);
68 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
69 | (pin_config << offset_bits);
71 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
73 mutex_unlock(&ts->lock);
78 static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
81 ts->out_level |= 1 << offset;
82 return ts->write(ts->dev, 0x20 + offset, 0x01);
84 ts->out_level &= ~(1 << offset);
85 return ts->write(ts->dev, 0x20 + offset, 0x00);
89 static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
92 struct max7301 *ts = container_of(chip, struct max7301, chip);
97 /* First 4 pins are unused in the controller */
99 offset_bits = (offset & 3) << 1;
101 config = &ts->port_config[offset >> 2];
103 mutex_lock(&ts->lock);
105 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
106 | (PIN_CONFIG_OUT << offset_bits);
108 ret = __max7301_set(ts, offset, value);
111 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
113 mutex_unlock(&ts->lock);
118 static int max7301_get(struct gpio_chip *chip, unsigned offset)
120 struct max7301 *ts = gpiochip_get_data(chip);
121 int config, level = -EINVAL;
123 /* First 4 pins are unused in the controller */
126 mutex_lock(&ts->lock);
128 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
133 /* Output: return cached level */
134 level = !!(ts->out_level & (1 << offset));
136 case PIN_CONFIG_IN_WO_PULLUP:
137 case PIN_CONFIG_IN_PULLUP:
138 /* Input: read out */
139 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
141 mutex_unlock(&ts->lock);
146 static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
148 struct max7301 *ts = gpiochip_get_data(chip);
150 /* First 4 pins are unused in the controller */
153 mutex_lock(&ts->lock);
155 __max7301_set(ts, offset, value);
157 mutex_unlock(&ts->lock);
160 int __max730x_probe(struct max7301 *ts)
162 struct device *dev = ts->dev;
163 struct max7301_platform_data *pdata;
166 pdata = dev_get_platdata(dev);
168 mutex_init(&ts->lock);
169 dev_set_drvdata(dev, ts);
171 /* Power up the chip and disable IRQ output */
172 ts->write(dev, 0x04, 0x01);
175 ts->input_pullup_active = pdata->input_pullup_active;
176 ts->chip.base = pdata->base;
180 ts->chip.label = dev->driver->name;
182 ts->chip.direction_input = max7301_direction_input;
183 ts->chip.get = max7301_get;
184 ts->chip.direction_output = max7301_direction_output;
185 ts->chip.set = max7301_set;
187 ts->chip.ngpio = PIN_NUMBER;
188 ts->chip.can_sleep = true;
189 ts->chip.parent = dev;
190 ts->chip.owner = THIS_MODULE;
193 * initialize pullups according to platform data and cache the
194 * register values for later use.
196 for (i = 1; i < 8; i++) {
199 * initialize port_config with "0xAA", which means
200 * input with internal pullup disabled. This is needed
201 * to avoid writing zeros (in the inner for loop),
202 * which is not allowed according to the datasheet.
204 ts->port_config[i] = 0xAA;
205 for (j = 0; j < 4; j++) {
206 int offset = (i - 1) * 4 + j;
207 ret = max7301_direction_input(&ts->chip, offset);
213 ret = gpiochip_add_data(&ts->chip, ts);
218 mutex_destroy(&ts->lock);
221 EXPORT_SYMBOL_GPL(__max730x_probe);
223 void __max730x_remove(struct device *dev)
225 struct max7301 *ts = dev_get_drvdata(dev);
227 /* Power down the chip and disable IRQ output */
228 ts->write(dev, 0x04, 0x00);
229 gpiochip_remove(&ts->chip);
230 mutex_destroy(&ts->lock);
232 EXPORT_SYMBOL_GPL(__max730x_remove);
234 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
235 MODULE_LICENSE("GPL v2");
236 MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");