Linux 3.9-rc5
[deliverable/linux.git] / drivers / staging / iio / accel / adis16220_core.c
1 /*
2 * ADIS16220 Programmable Digital Vibration Sensor driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20
21 #include "adis16220.h"
22
23 static ssize_t adis16220_read_16bit(struct device *dev,
24 struct device_attribute *attr,
25 char *buf)
26 {
27 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
28 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
29 struct adis16220_state *st = iio_priv(indio_dev);
30 ssize_t ret;
31 s16 val = 0;
32
33 /* Take the iio_dev status lock */
34 mutex_lock(&indio_dev->mlock);
35 ret = adis_read_reg_16(&st->adis, this_attr->address,
36 (u16 *)&val);
37 mutex_unlock(&indio_dev->mlock);
38 if (ret)
39 return ret;
40 return sprintf(buf, "%d\n", val);
41 }
42
43 static ssize_t adis16220_write_16bit(struct device *dev,
44 struct device_attribute *attr,
45 const char *buf,
46 size_t len)
47 {
48 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
49 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
50 struct adis16220_state *st = iio_priv(indio_dev);
51 int ret;
52 u16 val;
53
54 ret = kstrtou16(buf, 10, &val);
55 if (ret)
56 goto error_ret;
57 ret = adis_write_reg_16(&st->adis, this_attr->address, val);
58
59 error_ret:
60 return ret ? ret : len;
61 }
62
63 static int adis16220_capture(struct iio_dev *indio_dev)
64 {
65 struct adis16220_state *st = iio_priv(indio_dev);
66 int ret;
67
68 /* initiates a manual data capture */
69 ret = adis_write_reg_16(&st->adis, ADIS16220_GLOB_CMD, 0xBF08);
70 if (ret)
71 dev_err(&indio_dev->dev, "problem beginning capture");
72
73 msleep(10); /* delay for capture to finish */
74
75 return ret;
76 }
77
78 static ssize_t adis16220_write_capture(struct device *dev,
79 struct device_attribute *attr,
80 const char *buf, size_t len)
81 {
82 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
83 bool val;
84 int ret;
85
86 ret = strtobool(buf, &val);
87 if (ret)
88 return ret;
89 if (!val)
90 return -EINVAL;
91 ret = adis16220_capture(indio_dev);
92 if (ret)
93 return ret;
94
95 return len;
96 }
97
98 static ssize_t adis16220_capture_buffer_read(struct iio_dev *indio_dev,
99 char *buf,
100 loff_t off,
101 size_t count,
102 int addr)
103 {
104 struct adis16220_state *st = iio_priv(indio_dev);
105 struct spi_message msg;
106 struct spi_transfer xfers[] = {
107 {
108 .tx_buf = st->tx,
109 .bits_per_word = 8,
110 .len = 2,
111 .cs_change = 1,
112 .delay_usecs = 25,
113 }, {
114 .tx_buf = st->tx,
115 .rx_buf = st->rx,
116 .bits_per_word = 8,
117 .cs_change = 1,
118 .delay_usecs = 25,
119 },
120 };
121 int ret;
122 int i;
123
124 if (unlikely(!count))
125 return count;
126
127 if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
128 return -EINVAL;
129
130 if (off + count > ADIS16220_CAPTURE_SIZE)
131 count = ADIS16220_CAPTURE_SIZE - off;
132
133 /* write the begin position of capture buffer */
134 ret = adis_write_reg_16(&st->adis,
135 ADIS16220_CAPT_PNTR,
136 off > 1);
137 if (ret)
138 return -EIO;
139
140 /* read count/2 values from capture buffer */
141 mutex_lock(&st->buf_lock);
142
143
144 for (i = 0; i < count; i += 2) {
145 st->tx[i] = ADIS_READ_REG(addr);
146 st->tx[i + 1] = 0;
147 }
148 xfers[1].len = count;
149
150 spi_message_init(&msg);
151 spi_message_add_tail(&xfers[0], &msg);
152 spi_message_add_tail(&xfers[1], &msg);
153 ret = spi_sync(st->adis.spi, &msg);
154 if (ret) {
155
156 mutex_unlock(&st->buf_lock);
157 return -EIO;
158 }
159
160 memcpy(buf, st->rx, count);
161
162 mutex_unlock(&st->buf_lock);
163 return count;
164 }
165
166 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
167 struct bin_attribute *attr,
168 char *buf,
169 loff_t off,
170 size_t count)
171 {
172 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
173
174 return adis16220_capture_buffer_read(indio_dev, buf,
175 off, count,
176 ADIS16220_CAPT_BUFA);
177 }
178
179 static struct bin_attribute accel_bin = {
180 .attr = {
181 .name = "accel_bin",
182 .mode = S_IRUGO,
183 },
184 .read = adis16220_accel_bin_read,
185 .size = ADIS16220_CAPTURE_SIZE,
186 };
187
188 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
189 struct bin_attribute *attr,
190 char *buf, loff_t off,
191 size_t count)
192 {
193 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
194
195 return adis16220_capture_buffer_read(indio_dev, buf,
196 off, count,
197 ADIS16220_CAPT_BUF1);
198 }
199
200 static struct bin_attribute adc1_bin = {
201 .attr = {
202 .name = "in0_bin",
203 .mode = S_IRUGO,
204 },
205 .read = adis16220_adc1_bin_read,
206 .size = ADIS16220_CAPTURE_SIZE,
207 };
208
209 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
210 struct bin_attribute *attr,
211 char *buf, loff_t off,
212 size_t count)
213 {
214 struct iio_dev *indio_dev = dev_to_iio_dev(kobj_to_dev(kobj));
215
216 return adis16220_capture_buffer_read(indio_dev, buf,
217 off, count,
218 ADIS16220_CAPT_BUF2);
219 }
220
221
222 static struct bin_attribute adc2_bin = {
223 .attr = {
224 .name = "in1_bin",
225 .mode = S_IRUGO,
226 },
227 .read = adis16220_adc2_bin_read,
228 .size = ADIS16220_CAPTURE_SIZE,
229 };
230
231 #define IIO_DEV_ATTR_CAPTURE(_store) \
232 IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
233
234 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
235
236 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr) \
237 IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
238
239 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
240 adis16220_read_16bit,
241 adis16220_write_16bit,
242 ADIS16220_CAPT_PNTR);
243
244 enum adis16220_channel {
245 in_supply, in_1, in_2, accel, temp
246 };
247
248 struct adis16220_address_spec {
249 u8 addr;
250 u8 bits;
251 bool sign;
252 };
253
254 /* Address / bits / signed */
255 static const struct adis16220_address_spec adis16220_addresses[][3] = {
256 [in_supply] = { { ADIS16220_CAPT_SUPPLY, 12, 0 }, },
257 [in_1] = { { ADIS16220_CAPT_BUF1, 16, 1 },
258 { ADIS16220_AIN1_NULL, 16, 1 },
259 { ADIS16220_CAPT_PEAK1, 16, 1 }, },
260 [in_2] = { { ADIS16220_CAPT_BUF2, 16, 1 },
261 { ADIS16220_AIN2_NULL, 16, 1 },
262 { ADIS16220_CAPT_PEAK2, 16, 1 }, },
263 [accel] = { { ADIS16220_CAPT_BUFA, 16, 1 },
264 { ADIS16220_ACCL_NULL, 16, 1 },
265 { ADIS16220_CAPT_PEAKA, 16, 1 }, },
266 [temp] = { { ADIS16220_CAPT_TEMP, 12, 0 }, }
267 };
268
269 static int adis16220_read_raw(struct iio_dev *indio_dev,
270 struct iio_chan_spec const *chan,
271 int *val, int *val2,
272 long mask)
273 {
274 struct adis16220_state *st = iio_priv(indio_dev);
275 const struct adis16220_address_spec *addr;
276 int ret = -EINVAL;
277 int addrind = 0;
278 u16 uval;
279 s16 sval;
280 u8 bits;
281
282 switch (mask) {
283 case IIO_CHAN_INFO_RAW:
284 addrind = 0;
285 break;
286 case IIO_CHAN_INFO_OFFSET:
287 if (chan->type == IIO_TEMP) {
288 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
289 return IIO_VAL_INT;
290 }
291 addrind = 1;
292 break;
293 case IIO_CHAN_INFO_PEAK:
294 addrind = 2;
295 break;
296 case IIO_CHAN_INFO_SCALE:
297 switch (chan->type) {
298 case IIO_TEMP:
299 *val = -470; /* -0.47 C */
300 *val2 = 0;
301 return IIO_VAL_INT_PLUS_MICRO;
302 case IIO_ACCEL:
303 *val2 = IIO_G_TO_M_S_2(19073); /* 19.073 g */
304 return IIO_VAL_INT_PLUS_MICRO;
305 case IIO_VOLTAGE:
306 if (chan->channel == 0) {
307 *val = 1;
308 *val2 = 220700; /* 1.2207 mV */
309 } else {
310 /* Should really be dependent on VDD */
311 *val2 = 305180; /* 305.18 uV */
312 }
313 return IIO_VAL_INT_PLUS_MICRO;
314 default:
315 return -EINVAL;
316 }
317 default:
318 return -EINVAL;
319 }
320 addr = &adis16220_addresses[chan->address][addrind];
321 if (addr->sign) {
322 ret = adis_read_reg_16(&st->adis, addr->addr, &sval);
323 if (ret)
324 return ret;
325 bits = addr->bits;
326 sval &= (1 << bits) - 1;
327 sval = (s16)(sval << (16 - bits)) >> (16 - bits);
328 *val = sval;
329 return IIO_VAL_INT;
330 } else {
331 ret = adis_read_reg_16(&st->adis, addr->addr, &uval);
332 if (ret)
333 return ret;
334 bits = addr->bits;
335 uval &= (1 << bits) - 1;
336 *val = uval;
337 return IIO_VAL_INT;
338 }
339 }
340
341 static const struct iio_chan_spec adis16220_channels[] = {
342 {
343 .type = IIO_VOLTAGE,
344 .indexed = 1,
345 .channel = 0,
346 .extend_name = "supply",
347 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
348 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
349 .address = in_supply,
350 }, {
351 .type = IIO_ACCEL,
352 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
353 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
354 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
355 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
356 .address = accel,
357 }, {
358 .type = IIO_TEMP,
359 .indexed = 1,
360 .channel = 0,
361 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
362 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
363 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
364 .address = temp,
365 }, {
366 .type = IIO_VOLTAGE,
367 .indexed = 1,
368 .channel = 1,
369 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
370 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
371 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
372 .address = in_1,
373 }, {
374 .type = IIO_VOLTAGE,
375 .indexed = 1,
376 .channel = 2,
377 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
378 .address = in_2,
379 }
380 };
381
382 static struct attribute *adis16220_attributes[] = {
383 &iio_dev_attr_capture.dev_attr.attr,
384 &iio_dev_attr_capture_count.dev_attr.attr,
385 NULL
386 };
387
388 static const struct attribute_group adis16220_attribute_group = {
389 .attrs = adis16220_attributes,
390 };
391
392 static const struct iio_info adis16220_info = {
393 .attrs = &adis16220_attribute_group,
394 .driver_module = THIS_MODULE,
395 .read_raw = &adis16220_read_raw,
396 };
397
398 static const char * const adis16220_status_error_msgs[] = {
399 [ADIS16220_DIAG_STAT_VIOLATION_BIT] = "Capture period violation/interruption",
400 [ADIS16220_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
401 [ADIS16220_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
402 [ADIS16220_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
403 [ADIS16220_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
404 };
405
406 static const struct adis_data adis16220_data = {
407 .read_delay = 35,
408 .write_delay = 35,
409 .msc_ctrl_reg = ADIS16220_MSC_CTRL,
410 .glob_cmd_reg = ADIS16220_GLOB_CMD,
411 .diag_stat_reg = ADIS16220_DIAG_STAT,
412
413 .self_test_mask = ADIS16220_MSC_CTRL_SELF_TEST_EN,
414 .startup_delay = ADIS16220_STARTUP_DELAY,
415
416 .status_error_msgs = adis16220_status_error_msgs,
417 .status_error_mask = BIT(ADIS16220_DIAG_STAT_VIOLATION_BIT) |
418 BIT(ADIS16220_DIAG_STAT_SPI_FAIL_BIT) |
419 BIT(ADIS16220_DIAG_STAT_FLASH_UPT_BIT) |
420 BIT(ADIS16220_DIAG_STAT_POWER_HIGH_BIT) |
421 BIT(ADIS16220_DIAG_STAT_POWER_LOW_BIT),
422 };
423
424 static int adis16220_probe(struct spi_device *spi)
425 {
426 int ret;
427 struct adis16220_state *st;
428 struct iio_dev *indio_dev;
429
430 /* setup the industrialio driver allocated elements */
431 indio_dev = iio_device_alloc(sizeof(*st));
432 if (indio_dev == NULL) {
433 ret = -ENOMEM;
434 goto error_ret;
435 }
436
437 st = iio_priv(indio_dev);
438 /* this is only used for removal purposes */
439 spi_set_drvdata(spi, indio_dev);
440
441 indio_dev->name = spi->dev.driver->name;
442 indio_dev->dev.parent = &spi->dev;
443 indio_dev->info = &adis16220_info;
444 indio_dev->modes = INDIO_DIRECT_MODE;
445 indio_dev->channels = adis16220_channels;
446 indio_dev->num_channels = ARRAY_SIZE(adis16220_channels);
447
448 ret = iio_device_register(indio_dev);
449 if (ret)
450 goto error_free_dev;
451
452 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &accel_bin);
453 if (ret)
454 goto error_unregister_dev;
455
456 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc1_bin);
457 if (ret)
458 goto error_rm_accel_bin;
459
460 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc2_bin);
461 if (ret)
462 goto error_rm_adc1_bin;
463
464 ret = adis_init(&st->adis, indio_dev, spi, &adis16220_data);
465 if (ret)
466 goto error_rm_adc2_bin;
467 /* Get the device into a sane initial state */
468 ret = adis_initial_startup(&st->adis);
469 if (ret)
470 goto error_rm_adc2_bin;
471 return 0;
472
473 error_rm_adc2_bin:
474 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
475 error_rm_adc1_bin:
476 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
477 error_rm_accel_bin:
478 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
479 error_unregister_dev:
480 iio_device_unregister(indio_dev);
481 error_free_dev:
482 iio_device_free(indio_dev);
483 error_ret:
484 return ret;
485 }
486
487 static int adis16220_remove(struct spi_device *spi)
488 {
489 struct iio_dev *indio_dev = spi_get_drvdata(spi);
490
491 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
492 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
493 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
494 iio_device_unregister(indio_dev);
495 iio_device_free(indio_dev);
496
497 return 0;
498 }
499
500 static struct spi_driver adis16220_driver = {
501 .driver = {
502 .name = "adis16220",
503 .owner = THIS_MODULE,
504 },
505 .probe = adis16220_probe,
506 .remove = adis16220_remove,
507 };
508 module_spi_driver(adis16220_driver);
509
510 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
511 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
512 MODULE_LICENSE("GPL v2");
513 MODULE_ALIAS("spi:adis16220");
This page took 0.066725 seconds and 5 git commands to generate.