blob: 940e73d1cf09920e0967c76ef0e22cd70c6e2103 [file] [log] [blame]
Ben Dooks447aef12007-07-17 04:04:10 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Support Infineon TLE62x0 driver chips
Ben Dooks447aef12007-07-17 04:04:10 -07003 *
4 * Copyright (c) 2007 Simtec Electronics
5 * Ben Dooks, <ben@simtec.co.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/device.h>
13#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Ben Dooks447aef12007-07-17 04:04:10 -070015
16#include <linux/spi/spi.h>
17#include <linux/spi/tle62x0.h>
18
19
20#define CMD_READ 0x00
21#define CMD_SET 0xff
22
23#define DIAG_NORMAL 0x03
24#define DIAG_OVERLOAD 0x02
25#define DIAG_OPEN 0x01
26#define DIAG_SHORTGND 0x00
27
28struct tle62x0_state {
29 struct spi_device *us;
30 struct mutex lock;
31 unsigned int nr_gpio;
32 unsigned int gpio_state;
33
34 unsigned char tx_buff[4];
35 unsigned char rx_buff[4];
36};
37
38static int to_gpio_num(struct device_attribute *attr);
39
40static inline int tle62x0_write(struct tle62x0_state *st)
41{
42 unsigned char *buff = st->tx_buff;
43 unsigned int gpio_state = st->gpio_state;
44
45 buff[0] = CMD_SET;
46
47 if (st->nr_gpio == 16) {
48 buff[1] = gpio_state >> 8;
49 buff[2] = gpio_state;
50 } else {
51 buff[1] = gpio_state;
52 }
53
54 dev_dbg(&st->us->dev, "buff %02x,%02x,%02x\n",
55 buff[0], buff[1], buff[2]);
56
57 return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
58}
59
60static inline int tle62x0_read(struct tle62x0_state *st)
61{
62 unsigned char *txbuff = st->tx_buff;
63 struct spi_transfer xfer = {
64 .tx_buf = txbuff,
65 .rx_buf = st->rx_buff,
66 .len = (st->nr_gpio * 2) / 8,
67 };
68 struct spi_message msg;
69
70 txbuff[0] = CMD_READ;
71 txbuff[1] = 0x00;
72 txbuff[2] = 0x00;
73 txbuff[3] = 0x00;
74
75 spi_message_init(&msg);
76 spi_message_add_tail(&xfer, &msg);
77
78 return spi_sync(st->us, &msg);
79}
80
81static unsigned char *decode_fault(unsigned int fault_code)
82{
83 fault_code &= 3;
84
85 switch (fault_code) {
86 case DIAG_NORMAL:
87 return "N";
88 case DIAG_OVERLOAD:
89 return "V";
90 case DIAG_OPEN:
91 return "O";
92 case DIAG_SHORTGND:
93 return "G";
94 }
95
96 return "?";
97}
98
99static ssize_t tle62x0_status_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
101{
102 struct tle62x0_state *st = dev_get_drvdata(dev);
103 char *bp = buf;
104 unsigned char *buff = st->rx_buff;
105 unsigned long fault = 0;
106 int ptr;
107 int ret;
108
109 mutex_lock(&st->lock);
110 ret = tle62x0_read(st);
Ben Dooks447aef12007-07-17 04:04:10 -0700111 dev_dbg(dev, "tle62x0_read() returned %d\n", ret);
David Brownell822bd5a2007-11-14 17:00:04 -0800112 if (ret < 0) {
113 mutex_unlock(&st->lock);
114 return ret;
115 }
Ben Dooks447aef12007-07-17 04:04:10 -0700116
117 for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) {
118 fault <<= 8;
119 fault |= ((unsigned long)buff[ptr]);
120
121 dev_dbg(dev, "byte %d is %02x\n", ptr, buff[ptr]);
122 }
123
124 for (ptr = 0; ptr < st->nr_gpio; ptr++) {
125 bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2)));
126 }
127
128 *bp++ = '\n';
129
130 mutex_unlock(&st->lock);
131 return bp - buf;
132}
133
134static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL);
135
136static ssize_t tle62x0_gpio_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 struct tle62x0_state *st = dev_get_drvdata(dev);
140 int gpio_num = to_gpio_num(attr);
141 int value;
142
143 mutex_lock(&st->lock);
144 value = (st->gpio_state >> gpio_num) & 1;
145 mutex_unlock(&st->lock);
146
147 return snprintf(buf, PAGE_SIZE, "%d", value);
148}
149
150static ssize_t tle62x0_gpio_store(struct device *dev,
151 struct device_attribute *attr,
152 const char *buf, size_t len)
153{
154 struct tle62x0_state *st = dev_get_drvdata(dev);
155 int gpio_num = to_gpio_num(attr);
156 unsigned long val;
157 char *endp;
158
159 val = simple_strtoul(buf, &endp, 0);
160 if (buf == endp)
161 return -EINVAL;
162
163 dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val);
164
165 mutex_lock(&st->lock);
166
167 if (val)
168 st->gpio_state |= 1 << gpio_num;
169 else
170 st->gpio_state &= ~(1 << gpio_num);
171
172 tle62x0_write(st);
173 mutex_unlock(&st->lock);
174
175 return len;
176}
177
178static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO,
179 tle62x0_gpio_show, tle62x0_gpio_store);
180static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO,
181 tle62x0_gpio_show, tle62x0_gpio_store);
182static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO,
183 tle62x0_gpio_show, tle62x0_gpio_store);
184static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO,
185 tle62x0_gpio_show, tle62x0_gpio_store);
186static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO,
187 tle62x0_gpio_show, tle62x0_gpio_store);
188static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO,
189 tle62x0_gpio_show, tle62x0_gpio_store);
190static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO,
191 tle62x0_gpio_show, tle62x0_gpio_store);
192static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO,
193 tle62x0_gpio_show, tle62x0_gpio_store);
194static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO,
195 tle62x0_gpio_show, tle62x0_gpio_store);
196static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO,
197 tle62x0_gpio_show, tle62x0_gpio_store);
198static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO,
199 tle62x0_gpio_show, tle62x0_gpio_store);
200static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO,
201 tle62x0_gpio_show, tle62x0_gpio_store);
202static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO,
203 tle62x0_gpio_show, tle62x0_gpio_store);
204static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO,
205 tle62x0_gpio_show, tle62x0_gpio_store);
206static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO,
207 tle62x0_gpio_show, tle62x0_gpio_store);
208static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO,
209 tle62x0_gpio_show, tle62x0_gpio_store);
210
211static struct device_attribute *gpio_attrs[] = {
212 [0] = &dev_attr_gpio1,
213 [1] = &dev_attr_gpio2,
214 [2] = &dev_attr_gpio3,
215 [3] = &dev_attr_gpio4,
216 [4] = &dev_attr_gpio5,
217 [5] = &dev_attr_gpio6,
218 [6] = &dev_attr_gpio7,
219 [7] = &dev_attr_gpio8,
220 [8] = &dev_attr_gpio9,
221 [9] = &dev_attr_gpio10,
222 [10] = &dev_attr_gpio11,
223 [11] = &dev_attr_gpio12,
224 [12] = &dev_attr_gpio13,
225 [13] = &dev_attr_gpio14,
226 [14] = &dev_attr_gpio15,
227 [15] = &dev_attr_gpio16
228};
229
230static int to_gpio_num(struct device_attribute *attr)
231{
232 int ptr;
233
234 for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) {
235 if (gpio_attrs[ptr] == attr)
236 return ptr;
237 }
238
239 return -1;
240}
241
242static int __devinit tle62x0_probe(struct spi_device *spi)
243{
244 struct tle62x0_state *st;
245 struct tle62x0_pdata *pdata;
246 int ptr;
247 int ret;
248
249 pdata = spi->dev.platform_data;
250 if (pdata == NULL) {
251 dev_err(&spi->dev, "no device data specified\n");
252 return -EINVAL;
253 }
254
255 st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL);
256 if (st == NULL) {
257 dev_err(&spi->dev, "no memory for device state\n");
258 return -ENOMEM;
259 }
260
261 st->us = spi;
262 st->nr_gpio = pdata->gpio_count;
263 st->gpio_state = pdata->init_state;
264
265 mutex_init(&st->lock);
266
267 ret = device_create_file(&spi->dev, &dev_attr_status_show);
268 if (ret) {
269 dev_err(&spi->dev, "cannot create status attribute\n");
270 goto err_status;
271 }
272
273 for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
274 ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
275 if (ret) {
276 dev_err(&spi->dev, "cannot create gpio attribute\n");
277 goto err_gpios;
278 }
279 }
280
281 /* tle62x0_write(st); */
282 spi_set_drvdata(spi, st);
283 return 0;
284
285 err_gpios:
Axel Lin80b40372011-05-11 20:39:05 +0800286 while (--ptr >= 0)
Ben Dooks447aef12007-07-17 04:04:10 -0700287 device_remove_file(&spi->dev, gpio_attrs[ptr]);
288
289 device_remove_file(&spi->dev, &dev_attr_status_show);
290
291 err_status:
292 kfree(st);
293 return ret;
294}
295
296static int __devexit tle62x0_remove(struct spi_device *spi)
297{
298 struct tle62x0_state *st = spi_get_drvdata(spi);
299 int ptr;
300
301 for (ptr = 0; ptr < st->nr_gpio; ptr++)
302 device_remove_file(&spi->dev, gpio_attrs[ptr]);
303
Axel Lin80b40372011-05-11 20:39:05 +0800304 device_remove_file(&spi->dev, &dev_attr_status_show);
Ben Dooks447aef12007-07-17 04:04:10 -0700305 kfree(st);
306 return 0;
307}
308
309static struct spi_driver tle62x0_driver = {
310 .driver = {
311 .name = "tle62x0",
312 .owner = THIS_MODULE,
313 },
314 .probe = tle62x0_probe,
315 .remove = __devexit_p(tle62x0_remove),
316};
317
318static __init int tle62x0_init(void)
319{
320 return spi_register_driver(&tle62x0_driver);
321}
322
323static __exit void tle62x0_exit(void)
324{
325 spi_unregister_driver(&tle62x0_driver);
326}
327
328module_init(tle62x0_init);
329module_exit(tle62x0_exit);
330
331MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
332MODULE_DESCRIPTION("TLE62x0 SPI driver");
333MODULE_LICENSE("GPL v2");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700334MODULE_ALIAS("spi:tle62x0");