1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/init.h>
24#include <linux/mutex.h>
25#include <linux/spi/spi.h>
26#include <linux/spi/mc33880.h>
27#include <linux/gpio.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30
31#define DRIVER_NAME "mc33880"
32
33
34
35
36#define PIN_CONFIG_MASK 0x03
37#define PIN_CONFIG_IN_PULLUP 0x03
38#define PIN_CONFIG_IN_WO_PULLUP 0x02
39#define PIN_CONFIG_OUT 0x01
40
41#define PIN_NUMBER 8
42
43
44
45
46
47
48struct mc33880 {
49 struct mutex lock;
50 u8 port_config;
51 struct gpio_chip chip;
52 struct spi_device *spi;
53};
54
55static int mc33880_write_config(struct mc33880 *mc)
56{
57 return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
58}
59
60
61static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
62{
63 if (value)
64 mc->port_config |= 1 << offset;
65 else
66 mc->port_config &= ~(1 << offset);
67
68 return mc33880_write_config(mc);
69}
70
71
72static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
73{
74 struct mc33880 *mc = gpiochip_get_data(chip);
75
76 mutex_lock(&mc->lock);
77
78 __mc33880_set(mc, offset, value);
79
80 mutex_unlock(&mc->lock);
81}
82
83static int mc33880_probe(struct spi_device *spi)
84{
85 struct mc33880 *mc;
86 struct mc33880_platform_data *pdata;
87 int ret;
88
89 pdata = dev_get_platdata(&spi->dev);
90 if (!pdata || !pdata->base) {
91 dev_dbg(&spi->dev, "incorrect or missing platform data\n");
92 return -EINVAL;
93 }
94
95
96
97
98 spi->bits_per_word = 8;
99
100 ret = spi_setup(spi);
101 if (ret < 0)
102 return ret;
103
104 mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL);
105 if (!mc)
106 return -ENOMEM;
107
108 mutex_init(&mc->lock);
109
110 spi_set_drvdata(spi, mc);
111
112 mc->spi = spi;
113
114 mc->chip.label = DRIVER_NAME,
115 mc->chip.set = mc33880_set;
116 mc->chip.base = pdata->base;
117 mc->chip.ngpio = PIN_NUMBER;
118 mc->chip.can_sleep = true;
119 mc->chip.parent = &spi->dev;
120 mc->chip.owner = THIS_MODULE;
121
122 mc->port_config = 0x00;
123
124
125
126
127 ret = mc33880_write_config(mc);
128 mc->port_config = 0x00;
129 if (!ret)
130 ret = mc33880_write_config(mc);
131
132 if (ret) {
133 dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n",
134 ret);
135 goto exit_destroy;
136 }
137
138 ret = gpiochip_add_data(&mc->chip, mc);
139 if (ret)
140 goto exit_destroy;
141
142 return ret;
143
144exit_destroy:
145 mutex_destroy(&mc->lock);
146 return ret;
147}
148
149static int mc33880_remove(struct spi_device *spi)
150{
151 struct mc33880 *mc;
152
153 mc = spi_get_drvdata(spi);
154 if (!mc)
155 return -ENODEV;
156
157 gpiochip_remove(&mc->chip);
158 mutex_destroy(&mc->lock);
159
160 return 0;
161}
162
163static struct spi_driver mc33880_driver = {
164 .driver = {
165 .name = DRIVER_NAME,
166 },
167 .probe = mc33880_probe,
168 .remove = mc33880_remove,
169};
170
171static int __init mc33880_init(void)
172{
173 return spi_register_driver(&mc33880_driver);
174}
175
176
177
178subsys_initcall(mc33880_init);
179
180static void __exit mc33880_exit(void)
181{
182 spi_unregister_driver(&mc33880_driver);
183}
184module_exit(mc33880_exit);
185
186MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
187MODULE_LICENSE("GPL v2");
188
189