Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 1 | /** |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 2 | * Copyright (C) 2006 Juergen Beisert, Pengutronix |
| 3 | * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix |
| 4 | * Copyright (C) 2009 Wolfram Sang, Pengutronix |
| 5 | * |
| 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. |
| 9 | * |
| 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 |
| 12 | * details |
| 13 | * Note: |
| 14 | * - DIN must be stable at the rising edge of clock. |
| 15 | * - when writing: |
| 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 |
| 20 | * - when reading |
| 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 |
| 29 | * |
| 30 | * The driver exports a standard gpiochip interface |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Pin configurations, see MAX7301 datasheet page 6 |
| 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 | |
| 51 | static int max7301_direction_input(struct gpio_chip *chip, unsigned offset) |
| 52 | { |
Linus Walleij | 5e45e01 | 2015-12-07 09:27:05 +0100 | [diff] [blame] | 53 | struct max7301 *ts = gpiochip_get_data(chip); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 54 | u8 *config; |
Marc Kleine-Budde | 4a22b8a | 2010-08-10 18:02:23 -0700 | [diff] [blame] | 55 | u8 offset_bits, pin_config; |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 56 | int ret; |
| 57 | |
| 58 | /* First 4 pins are unused in the controller */ |
| 59 | offset += 4; |
| 60 | offset_bits = (offset & 3) << 1; |
| 61 | |
| 62 | config = &ts->port_config[offset >> 2]; |
| 63 | |
Marc Kleine-Budde | 4a22b8a | 2010-08-10 18:02:23 -0700 | [diff] [blame] | 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 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 69 | mutex_lock(&ts->lock); |
| 70 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 71 | *config = (*config & ~(PIN_CONFIG_MASK << offset_bits)) |
Marc Kleine-Budde | 4a22b8a | 2010-08-10 18:02:23 -0700 | [diff] [blame] | 72 | | (pin_config << offset_bits); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 73 | |
| 74 | ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config); |
| 75 | |
| 76 | mutex_unlock(&ts->lock); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static 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 | |
| 92 | static int max7301_direction_output(struct gpio_chip *chip, unsigned offset, |
| 93 | int value) |
| 94 | { |
Linus Walleij | 5e45e01 | 2015-12-07 09:27:05 +0100 | [diff] [blame] | 95 | struct max7301 *ts = gpiochip_get_data(chip); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 96 | u8 *config; |
| 97 | u8 offset_bits; |
| 98 | int ret; |
| 99 | |
| 100 | /* First 4 pins are unused in the controller */ |
| 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 | |
| 121 | static int max7301_get(struct gpio_chip *chip, unsigned offset) |
| 122 | { |
Linus Walleij | 5e45e01 | 2015-12-07 09:27:05 +0100 | [diff] [blame] | 123 | struct max7301 *ts = gpiochip_get_data(chip); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 124 | int config, level = -EINVAL; |
| 125 | |
| 126 | /* First 4 pins are unused in the controller */ |
| 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 | /* Output: return cached level */ |
| 137 | level = !!(ts->out_level & (1 << offset)); |
| 138 | break; |
| 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; |
| 143 | } |
| 144 | mutex_unlock(&ts->lock); |
| 145 | |
| 146 | return level; |
| 147 | } |
| 148 | |
| 149 | static void max7301_set(struct gpio_chip *chip, unsigned offset, int value) |
| 150 | { |
Linus Walleij | 5e45e01 | 2015-12-07 09:27:05 +0100 | [diff] [blame] | 151 | struct max7301 *ts = gpiochip_get_data(chip); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 152 | |
| 153 | /* First 4 pins are unused in the controller */ |
| 154 | offset += 4; |
| 155 | |
| 156 | mutex_lock(&ts->lock); |
| 157 | |
| 158 | __max7301_set(ts, offset, value); |
| 159 | |
| 160 | mutex_unlock(&ts->lock); |
| 161 | } |
| 162 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 163 | int __max730x_probe(struct max7301 *ts) |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 164 | { |
| 165 | struct device *dev = ts->dev; |
| 166 | struct max7301_platform_data *pdata; |
| 167 | int i, ret; |
| 168 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 169 | pdata = dev_get_platdata(dev); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 170 | |
| 171 | mutex_init(&ts->lock); |
| 172 | dev_set_drvdata(dev, ts); |
| 173 | |
| 174 | /* Power up the chip and disable IRQ output */ |
| 175 | ts->write(dev, 0x04, 0x01); |
| 176 | |
Roland Stigge | 8754fcc | 2012-11-15 14:59:40 +0100 | [diff] [blame] | 177 | if (pdata) { |
| 178 | ts->input_pullup_active = pdata->input_pullup_active; |
| 179 | ts->chip.base = pdata->base; |
| 180 | } else { |
| 181 | ts->chip.base = -1; |
| 182 | } |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 183 | ts->chip.label = dev->driver->name; |
| 184 | |
| 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; |
| 189 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 190 | ts->chip.ngpio = PIN_NUMBER; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 191 | ts->chip.can_sleep = true; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 192 | ts->chip.parent = dev; |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 193 | ts->chip.owner = THIS_MODULE; |
| 194 | |
Christophe Leroy | 6f4deb1 | 2016-08-08 15:58:56 +0200 | [diff] [blame] | 195 | ret = gpiochip_add_data(&ts->chip, ts); |
| 196 | if (ret) |
| 197 | goto exit_destroy; |
| 198 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 199 | /* |
Marc Kleine-Budde | 4a22b8a | 2010-08-10 18:02:23 -0700 | [diff] [blame] | 200 | * initialize pullups according to platform data and cache the |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 201 | * register values for later use. |
| 202 | */ |
| 203 | for (i = 1; i < 8; i++) { |
| 204 | int j; |
Marc Kleine-Budde | 4a22b8a | 2010-08-10 18:02:23 -0700 | [diff] [blame] | 205 | /* |
| 206 | * initialize port_config with "0xAA", which means |
| 207 | * input with internal pullup disabled. This is needed |
| 208 | * to avoid writing zeros (in the inner for loop), |
| 209 | * which is not allowed according to the datasheet. |
| 210 | */ |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 211 | ts->port_config[i] = 0xAA; |
| 212 | for (j = 0; j < 4; j++) { |
| 213 | int offset = (i - 1) * 4 + j; |
| 214 | ret = max7301_direction_input(&ts->chip, offset); |
| 215 | if (ret) |
| 216 | goto exit_destroy; |
| 217 | } |
| 218 | } |
| 219 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 220 | return ret; |
| 221 | |
| 222 | exit_destroy: |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 223 | mutex_destroy(&ts->lock); |
| 224 | return ret; |
| 225 | } |
| 226 | EXPORT_SYMBOL_GPL(__max730x_probe); |
| 227 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 228 | int __max730x_remove(struct device *dev) |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 229 | { |
| 230 | struct max7301 *ts = dev_get_drvdata(dev); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 231 | |
| 232 | if (ts == NULL) |
| 233 | return -ENODEV; |
| 234 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 235 | /* Power down the chip and disable IRQ output */ |
| 236 | ts->write(dev, 0x04, 0x00); |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 237 | gpiochip_remove(&ts->chip); |
| 238 | mutex_destroy(&ts->lock); |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 239 | return 0; |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 240 | } |
| 241 | EXPORT_SYMBOL_GPL(__max730x_remove); |
Richard Röjfors | 06ca02b | 2010-03-23 13:35:20 -0700 | [diff] [blame] | 242 | |
| 243 | MODULE_AUTHOR("Juergen Beisert, Wolfram Sang"); |
| 244 | MODULE_LICENSE("GPL v2"); |
| 245 | MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts"); |