Blame view

drivers/spi/spi-tle62x0.c 7.02 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
447aef1a1   Ben Dooks   SPI: tle620x powe...
2
  /*
ca632f556   Grant Likely   spi: reorganize d...
3
   * Support Infineon TLE62x0 driver chips
447aef1a1   Ben Dooks   SPI: tle620x powe...
4
5
6
   *
   * Copyright (c) 2007 Simtec Electronics
   *	Ben Dooks, <ben@simtec.co.uk>
447aef1a1   Ben Dooks   SPI: tle620x powe...
7
8
9
10
   */
  
  #include <linux/device.h>
  #include <linux/kernel.h>
d7614de42   Paul Gortmaker   spi: Add module.h...
11
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
12
  #include <linux/slab.h>
447aef1a1   Ben Dooks   SPI: tle620x powe...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
  #include <linux/spi/spi.h>
  #include <linux/spi/tle62x0.h>
  
  
  #define CMD_READ	0x00
  #define CMD_SET		0xff
  
  #define DIAG_NORMAL	0x03
  #define DIAG_OVERLOAD	0x02
  #define DIAG_OPEN	0x01
  #define DIAG_SHORTGND	0x00
  
  struct tle62x0_state {
  	struct spi_device	*us;
  	struct mutex		lock;
  	unsigned int		nr_gpio;
  	unsigned int		gpio_state;
  
  	unsigned char		tx_buff[4];
  	unsigned char		rx_buff[4];
  };
  
  static int to_gpio_num(struct device_attribute *attr);
  
  static inline int tle62x0_write(struct tle62x0_state *st)
  {
  	unsigned char *buff = st->tx_buff;
  	unsigned int gpio_state = st->gpio_state;
  
  	buff[0] = CMD_SET;
  
  	if (st->nr_gpio == 16) {
  		buff[1] = gpio_state >> 8;
  		buff[2] = gpio_state;
  	} else {
  		buff[1] = gpio_state;
  	}
cd4c42446   Andy Shevchenko   spi: tle62x0: dum...
51
52
  	dev_dbg(&st->us->dev, "buff %3ph
  ", buff);
447aef1a1   Ben Dooks   SPI: tle620x powe...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  
  	return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
  }
  
  static inline int tle62x0_read(struct tle62x0_state *st)
  {
  	unsigned char *txbuff = st->tx_buff;
  	struct spi_transfer xfer = {
  		.tx_buf		= txbuff,
  		.rx_buf		= st->rx_buff,
  		.len		= (st->nr_gpio * 2) / 8,
  	};
  	struct spi_message msg;
  
  	txbuff[0] = CMD_READ;
  	txbuff[1] = 0x00;
  	txbuff[2] = 0x00;
  	txbuff[3] = 0x00;
  
  	spi_message_init(&msg);
  	spi_message_add_tail(&xfer, &msg);
  
  	return spi_sync(st->us, &msg);
  }
  
  static unsigned char *decode_fault(unsigned int fault_code)
  {
  	fault_code &= 3;
  
  	switch (fault_code) {
  	case DIAG_NORMAL:
  		return "N";
  	case DIAG_OVERLOAD:
  		return "V";
  	case DIAG_OPEN:
  		return "O";
  	case DIAG_SHORTGND:
  		return "G";
  	}
  
  	return "?";
  }
  
  static ssize_t tle62x0_status_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct tle62x0_state *st = dev_get_drvdata(dev);
  	char *bp = buf;
  	unsigned char *buff = st->rx_buff;
  	unsigned long fault = 0;
  	int ptr;
  	int ret;
  
  	mutex_lock(&st->lock);
  	ret = tle62x0_read(st);
447aef1a1   Ben Dooks   SPI: tle620x powe...
108
109
  	dev_dbg(dev, "tle62x0_read() returned %d
  ", ret);
822bd5aa2   David Brownell   tle62x0 driver st...
110
111
112
113
  	if (ret < 0) {
  		mutex_unlock(&st->lock);
  		return ret;
  	}
447aef1a1   Ben Dooks   SPI: tle620x powe...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  
  	for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) {
  		fault <<= 8;
  		fault  |= ((unsigned long)buff[ptr]);
  
  		dev_dbg(dev, "byte %d is %02x
  ", ptr, buff[ptr]);
  	}
  
  	for (ptr = 0; ptr < st->nr_gpio; ptr++) {
  		bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2)));
  	}
  
  	*bp++ = '
  ';
  
  	mutex_unlock(&st->lock);
  	return bp - buf;
  }
  
  static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL);
  
  static ssize_t tle62x0_gpio_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct tle62x0_state *st = dev_get_drvdata(dev);
  	int gpio_num = to_gpio_num(attr);
  	int value;
  
  	mutex_lock(&st->lock);
  	value = (st->gpio_state >> gpio_num) & 1;
  	mutex_unlock(&st->lock);
  
  	return snprintf(buf, PAGE_SIZE, "%d", value);
  }
  
  static ssize_t tle62x0_gpio_store(struct device *dev,
  		struct device_attribute *attr,
  		const char *buf, size_t len)
  {
  	struct tle62x0_state *st = dev_get_drvdata(dev);
  	int gpio_num = to_gpio_num(attr);
  	unsigned long val;
  	char *endp;
  
  	val = simple_strtoul(buf, &endp, 0);
  	if (buf == endp)
  		return -EINVAL;
  
  	dev_dbg(dev, "setting gpio %d to %ld
  ", gpio_num, val);
  
  	mutex_lock(&st->lock);
  
  	if (val)
  		st->gpio_state |= 1 << gpio_num;
  	else
  		st->gpio_state &= ~(1 << gpio_num);
  
  	tle62x0_write(st);
  	mutex_unlock(&st->lock);
  
  	return len;
  }
  
  static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO,
  		tle62x0_gpio_show, tle62x0_gpio_store);
  
  static struct device_attribute *gpio_attrs[] = {
  	[0]		= &dev_attr_gpio1,
  	[1]		= &dev_attr_gpio2,
  	[2]		= &dev_attr_gpio3,
  	[3]		= &dev_attr_gpio4,
  	[4]		= &dev_attr_gpio5,
  	[5]		= &dev_attr_gpio6,
  	[6]		= &dev_attr_gpio7,
  	[7]		= &dev_attr_gpio8,
  	[8]		= &dev_attr_gpio9,
  	[9]		= &dev_attr_gpio10,
  	[10]		= &dev_attr_gpio11,
  	[11]		= &dev_attr_gpio12,
  	[12]		= &dev_attr_gpio13,
  	[13]		= &dev_attr_gpio14,
  	[14]		= &dev_attr_gpio15,
  	[15]		= &dev_attr_gpio16
  };
  
  static int to_gpio_num(struct device_attribute *attr)
  {
  	int ptr;
  
  	for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) {
  		if (gpio_attrs[ptr] == attr)
  			return ptr;
  	}
  
  	return -1;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
242
  static int tle62x0_probe(struct spi_device *spi)
447aef1a1   Ben Dooks   SPI: tle620x powe...
243
244
245
246
247
  {
  	struct tle62x0_state *st;
  	struct tle62x0_pdata *pdata;
  	int ptr;
  	int ret;
8074cf063   Jingoo Han   spi: use dev_get_...
248
  	pdata = dev_get_platdata(&spi->dev);
447aef1a1   Ben Dooks   SPI: tle620x powe...
249
250
251
252
253
254
255
  	if (pdata == NULL) {
  		dev_err(&spi->dev, "no device data specified
  ");
  		return -EINVAL;
  	}
  
  	st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL);
9f48e54bf   Jingoo Han   spi: tle62x0: rem...
256
  	if (st == NULL)
447aef1a1   Ben Dooks   SPI: tle620x powe...
257
  		return -ENOMEM;
447aef1a1   Ben Dooks   SPI: tle620x powe...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  	st->us = spi;
  	st->nr_gpio = pdata->gpio_count;
  	st->gpio_state = pdata->init_state;
  
  	mutex_init(&st->lock);
  
  	ret = device_create_file(&spi->dev, &dev_attr_status_show);
  	if (ret) {
  		dev_err(&spi->dev, "cannot create status attribute
  ");
  		goto err_status;
  	}
  
  	for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
  		ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
  		if (ret) {
  			dev_err(&spi->dev, "cannot create gpio attribute
  ");
  			goto err_gpios;
  		}
  	}
  
  	/* tle62x0_write(st); */
  	spi_set_drvdata(spi, st);
  	return 0;
  
   err_gpios:
80b403703   Axel Lin   spi/tle620x: add ...
286
  	while (--ptr >= 0)
447aef1a1   Ben Dooks   SPI: tle620x powe...
287
288
289
290
291
292
293
294
  		device_remove_file(&spi->dev, gpio_attrs[ptr]);
  
  	device_remove_file(&spi->dev, &dev_attr_status_show);
  
   err_status:
  	kfree(st);
  	return ret;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
295
  static int tle62x0_remove(struct spi_device *spi)
447aef1a1   Ben Dooks   SPI: tle620x powe...
296
297
298
299
300
301
  {
  	struct tle62x0_state *st = spi_get_drvdata(spi);
  	int ptr;
  
  	for (ptr = 0; ptr < st->nr_gpio; ptr++)
  		device_remove_file(&spi->dev, gpio_attrs[ptr]);
80b403703   Axel Lin   spi/tle620x: add ...
302
  	device_remove_file(&spi->dev, &dev_attr_status_show);
447aef1a1   Ben Dooks   SPI: tle620x powe...
303
304
305
306
307
308
309
  	kfree(st);
  	return 0;
  }
  
  static struct spi_driver tle62x0_driver = {
  	.driver = {
  		.name	= "tle62x0",
447aef1a1   Ben Dooks   SPI: tle620x powe...
310
311
  	},
  	.probe		= tle62x0_probe,
fd4a319bc   Grant Likely   spi: Remove HOTPL...
312
  	.remove		= tle62x0_remove,
447aef1a1   Ben Dooks   SPI: tle620x powe...
313
  };
38e271cde   Sachin Kamat   spi: spi-tle62x0:...
314
  module_spi_driver(tle62x0_driver);
447aef1a1   Ben Dooks   SPI: tle620x powe...
315
316
317
318
  
  MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  MODULE_DESCRIPTION("TLE62x0 SPI driver");
  MODULE_LICENSE("GPL v2");
e0626e384   Anton Vorontsov   spi: prefix modal...
319
  MODULE_ALIAS("spi:tle62x0");