0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include <linux/module.h>
0031 #include <linux/init.h>
0032 #include <linux/platform_device.h>
0033 #include <linux/mutex.h>
0034 #include <linux/spi/max7301.h>
0035 #include <linux/gpio/driver.h>
0036 #include <linux/slab.h>
0037
0038
0039
0040
0041 #define PIN_CONFIG_MASK 0x03
0042 #define PIN_CONFIG_IN_PULLUP 0x03
0043 #define PIN_CONFIG_IN_WO_PULLUP 0x02
0044 #define PIN_CONFIG_OUT 0x01
0045
0046 #define PIN_NUMBER 28
0047
0048 static int max7301_direction_input(struct gpio_chip *chip, unsigned offset)
0049 {
0050 struct max7301 *ts = container_of(chip, struct max7301, chip);
0051 u8 *config;
0052 u8 offset_bits, pin_config;
0053 int ret;
0054
0055
0056 offset += 4;
0057 offset_bits = (offset & 3) << 1;
0058
0059 config = &ts->port_config[offset >> 2];
0060
0061 if (ts->input_pullup_active & BIT(offset))
0062 pin_config = PIN_CONFIG_IN_PULLUP;
0063 else
0064 pin_config = PIN_CONFIG_IN_WO_PULLUP;
0065
0066 mutex_lock(&ts->lock);
0067
0068 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
0069 | (pin_config << offset_bits);
0070
0071 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
0072
0073 mutex_unlock(&ts->lock);
0074
0075 return ret;
0076 }
0077
0078 static int __max7301_set(struct max7301 *ts, unsigned offset, int value)
0079 {
0080 if (value) {
0081 ts->out_level |= 1 << offset;
0082 return ts->write(ts->dev, 0x20 + offset, 0x01);
0083 } else {
0084 ts->out_level &= ~(1 << offset);
0085 return ts->write(ts->dev, 0x20 + offset, 0x00);
0086 }
0087 }
0088
0089 static int max7301_direction_output(struct gpio_chip *chip, unsigned offset,
0090 int value)
0091 {
0092 struct max7301 *ts = container_of(chip, struct max7301, chip);
0093 u8 *config;
0094 u8 offset_bits;
0095 int ret;
0096
0097
0098 offset += 4;
0099 offset_bits = (offset & 3) << 1;
0100
0101 config = &ts->port_config[offset >> 2];
0102
0103 mutex_lock(&ts->lock);
0104
0105 *config = (*config & ~(PIN_CONFIG_MASK << offset_bits))
0106 | (PIN_CONFIG_OUT << offset_bits);
0107
0108 ret = __max7301_set(ts, offset, value);
0109
0110 if (!ret)
0111 ret = ts->write(ts->dev, 0x08 + (offset >> 2), *config);
0112
0113 mutex_unlock(&ts->lock);
0114
0115 return ret;
0116 }
0117
0118 static int max7301_get(struct gpio_chip *chip, unsigned offset)
0119 {
0120 struct max7301 *ts = gpiochip_get_data(chip);
0121 int config, level = -EINVAL;
0122
0123
0124 offset += 4;
0125
0126 mutex_lock(&ts->lock);
0127
0128 config = (ts->port_config[offset >> 2] >> ((offset & 3) << 1))
0129 & PIN_CONFIG_MASK;
0130
0131 switch (config) {
0132 case PIN_CONFIG_OUT:
0133
0134 level = !!(ts->out_level & (1 << offset));
0135 break;
0136 case PIN_CONFIG_IN_WO_PULLUP:
0137 case PIN_CONFIG_IN_PULLUP:
0138
0139 level = ts->read(ts->dev, 0x20 + offset) & 0x01;
0140 }
0141 mutex_unlock(&ts->lock);
0142
0143 return level;
0144 }
0145
0146 static void max7301_set(struct gpio_chip *chip, unsigned offset, int value)
0147 {
0148 struct max7301 *ts = gpiochip_get_data(chip);
0149
0150
0151 offset += 4;
0152
0153 mutex_lock(&ts->lock);
0154
0155 __max7301_set(ts, offset, value);
0156
0157 mutex_unlock(&ts->lock);
0158 }
0159
0160 int __max730x_probe(struct max7301 *ts)
0161 {
0162 struct device *dev = ts->dev;
0163 struct max7301_platform_data *pdata;
0164 int i, ret;
0165
0166 pdata = dev_get_platdata(dev);
0167
0168 mutex_init(&ts->lock);
0169 dev_set_drvdata(dev, ts);
0170
0171
0172 ts->write(dev, 0x04, 0x01);
0173
0174 if (pdata) {
0175 ts->input_pullup_active = pdata->input_pullup_active;
0176 ts->chip.base = pdata->base;
0177 } else {
0178 ts->chip.base = -1;
0179 }
0180 ts->chip.label = dev->driver->name;
0181
0182 ts->chip.direction_input = max7301_direction_input;
0183 ts->chip.get = max7301_get;
0184 ts->chip.direction_output = max7301_direction_output;
0185 ts->chip.set = max7301_set;
0186
0187 ts->chip.ngpio = PIN_NUMBER;
0188 ts->chip.can_sleep = true;
0189 ts->chip.parent = dev;
0190 ts->chip.owner = THIS_MODULE;
0191
0192
0193
0194
0195
0196 for (i = 1; i < 8; i++) {
0197 int j;
0198
0199
0200
0201
0202
0203
0204 ts->port_config[i] = 0xAA;
0205 for (j = 0; j < 4; j++) {
0206 int offset = (i - 1) * 4 + j;
0207 ret = max7301_direction_input(&ts->chip, offset);
0208 if (ret)
0209 goto exit_destroy;
0210 }
0211 }
0212
0213 ret = gpiochip_add_data(&ts->chip, ts);
0214 if (!ret)
0215 return ret;
0216
0217 exit_destroy:
0218 mutex_destroy(&ts->lock);
0219 return ret;
0220 }
0221 EXPORT_SYMBOL_GPL(__max730x_probe);
0222
0223 void __max730x_remove(struct device *dev)
0224 {
0225 struct max7301 *ts = dev_get_drvdata(dev);
0226
0227
0228 ts->write(dev, 0x04, 0x00);
0229 gpiochip_remove(&ts->chip);
0230 mutex_destroy(&ts->lock);
0231 }
0232 EXPORT_SYMBOL_GPL(__max730x_remove);
0233
0234 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
0235 MODULE_LICENSE("GPL v2");
0236 MODULE_DESCRIPTION("MAX730x GPIO-Expanders, generic parts");