0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/init.h>
0012 #include <linux/mutex.h>
0013 #include <linux/spi/spi.h>
0014 #include <linux/spi/mc33880.h>
0015 #include <linux/gpio/driver.h>
0016 #include <linux/slab.h>
0017 #include <linux/module.h>
0018
0019 #define DRIVER_NAME "mc33880"
0020
0021
0022
0023
0024 #define PIN_CONFIG_MASK 0x03
0025 #define PIN_CONFIG_IN_PULLUP 0x03
0026 #define PIN_CONFIG_IN_WO_PULLUP 0x02
0027 #define PIN_CONFIG_OUT 0x01
0028
0029 #define PIN_NUMBER 8
0030
0031
0032
0033
0034
0035
0036 struct mc33880 {
0037 struct mutex lock;
0038 u8 port_config;
0039 struct gpio_chip chip;
0040 struct spi_device *spi;
0041 };
0042
0043 static int mc33880_write_config(struct mc33880 *mc)
0044 {
0045 return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
0046 }
0047
0048
0049 static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
0050 {
0051 if (value)
0052 mc->port_config |= 1 << offset;
0053 else
0054 mc->port_config &= ~(1 << offset);
0055
0056 return mc33880_write_config(mc);
0057 }
0058
0059
0060 static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
0061 {
0062 struct mc33880 *mc = gpiochip_get_data(chip);
0063
0064 mutex_lock(&mc->lock);
0065
0066 __mc33880_set(mc, offset, value);
0067
0068 mutex_unlock(&mc->lock);
0069 }
0070
0071 static int mc33880_probe(struct spi_device *spi)
0072 {
0073 struct mc33880 *mc;
0074 struct mc33880_platform_data *pdata;
0075 int ret;
0076
0077 pdata = dev_get_platdata(&spi->dev);
0078 if (!pdata || !pdata->base) {
0079 dev_dbg(&spi->dev, "incorrect or missing platform data\n");
0080 return -EINVAL;
0081 }
0082
0083
0084
0085
0086 spi->bits_per_word = 8;
0087
0088 ret = spi_setup(spi);
0089 if (ret < 0)
0090 return ret;
0091
0092 mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL);
0093 if (!mc)
0094 return -ENOMEM;
0095
0096 mutex_init(&mc->lock);
0097
0098 spi_set_drvdata(spi, mc);
0099
0100 mc->spi = spi;
0101
0102 mc->chip.label = DRIVER_NAME,
0103 mc->chip.set = mc33880_set;
0104 mc->chip.base = pdata->base;
0105 mc->chip.ngpio = PIN_NUMBER;
0106 mc->chip.can_sleep = true;
0107 mc->chip.parent = &spi->dev;
0108 mc->chip.owner = THIS_MODULE;
0109
0110 mc->port_config = 0x00;
0111
0112
0113
0114
0115 ret = mc33880_write_config(mc);
0116 mc->port_config = 0x00;
0117 if (!ret)
0118 ret = mc33880_write_config(mc);
0119
0120 if (ret) {
0121 dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n",
0122 ret);
0123 goto exit_destroy;
0124 }
0125
0126 ret = gpiochip_add_data(&mc->chip, mc);
0127 if (ret)
0128 goto exit_destroy;
0129
0130 return ret;
0131
0132 exit_destroy:
0133 mutex_destroy(&mc->lock);
0134 return ret;
0135 }
0136
0137 static void mc33880_remove(struct spi_device *spi)
0138 {
0139 struct mc33880 *mc;
0140
0141 mc = spi_get_drvdata(spi);
0142
0143 gpiochip_remove(&mc->chip);
0144 mutex_destroy(&mc->lock);
0145 }
0146
0147 static struct spi_driver mc33880_driver = {
0148 .driver = {
0149 .name = DRIVER_NAME,
0150 },
0151 .probe = mc33880_probe,
0152 .remove = mc33880_remove,
0153 };
0154
0155 static int __init mc33880_init(void)
0156 {
0157 return spi_register_driver(&mc33880_driver);
0158 }
0159
0160
0161
0162 subsys_initcall(mc33880_init);
0163
0164 static void __exit mc33880_exit(void)
0165 {
0166 spi_unregister_driver(&mc33880_driver);
0167 }
0168 module_exit(mc33880_exit);
0169
0170 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
0171 MODULE_LICENSE("GPL v2");
0172