• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/iio/accel/
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/interrupt.h>
10#include <linux/irq.h>
11#include <linux/gpio.h>
12#include <linux/delay.h>
13#include <linux/mutex.h>
14#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/spi/spi.h>
17#include <linux/slab.h>
18#include <linux/sysfs.h>
19#include <linux/list.h>
20
21#include "../iio.h"
22#include "../sysfs.h"
23#include "accel.h"
24#include "../adc/adc.h"
25
26#include "adis16220.h"
27
28#define DRIVER_NAME		"adis16220"
29
30/**
31 * adis16220_spi_write_reg_8() - write single byte to a register
32 * @dev: device associated with child of actual device (iio_dev or iio_trig)
33 * @reg_address: the address of the register to be written
34 * @val: the value to write
35 **/
36static int adis16220_spi_write_reg_8(struct device *dev,
37		u8 reg_address,
38		u8 val)
39{
40	int ret;
41	struct iio_dev *indio_dev = dev_get_drvdata(dev);
42	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
43
44	mutex_lock(&st->buf_lock);
45	st->tx[0] = ADIS16220_WRITE_REG(reg_address);
46	st->tx[1] = val;
47
48	ret = spi_write(st->us, st->tx, 2);
49	mutex_unlock(&st->buf_lock);
50
51	return ret;
52}
53
54/**
55 * adis16220_spi_write_reg_16() - write 2 bytes to a pair of registers
56 * @dev: device associated with child of actual device (iio_dev or iio_trig)
57 * @reg_address: the address of the lower of the two registers. Second register
58 *               is assumed to have address one greater.
59 * @val: value to be written
60 **/
61static int adis16220_spi_write_reg_16(struct device *dev,
62		u8 lower_reg_address,
63		u16 value)
64{
65	int ret;
66	struct spi_message msg;
67	struct iio_dev *indio_dev = dev_get_drvdata(dev);
68	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
69	struct spi_transfer xfers[] = {
70		{
71			.tx_buf = st->tx,
72			.bits_per_word = 8,
73			.len = 2,
74			.cs_change = 1,
75			.delay_usecs = 35,
76		}, {
77			.tx_buf = st->tx + 2,
78			.bits_per_word = 8,
79			.len = 2,
80			.cs_change = 1,
81			.delay_usecs = 35,
82		},
83	};
84
85	mutex_lock(&st->buf_lock);
86	st->tx[0] = ADIS16220_WRITE_REG(lower_reg_address);
87	st->tx[1] = value & 0xFF;
88	st->tx[2] = ADIS16220_WRITE_REG(lower_reg_address + 1);
89	st->tx[3] = (value >> 8) & 0xFF;
90
91	spi_message_init(&msg);
92	spi_message_add_tail(&xfers[0], &msg);
93	spi_message_add_tail(&xfers[1], &msg);
94	ret = spi_sync(st->us, &msg);
95	mutex_unlock(&st->buf_lock);
96
97	return ret;
98}
99
100/**
101 * adis16220_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @dev: device associated with child of actual device (iio_dev or iio_trig)
103 * @reg_address: the address of the lower of the two registers. Second register
104 *               is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
106 **/
107static int adis16220_spi_read_reg_16(struct device *dev,
108		u8 lower_reg_address,
109		u16 *val)
110{
111	struct spi_message msg;
112	struct iio_dev *indio_dev = dev_get_drvdata(dev);
113	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
114	int ret;
115	struct spi_transfer xfers[] = {
116		{
117			.tx_buf = st->tx,
118			.bits_per_word = 8,
119			.len = 2,
120			.cs_change = 1,
121			.delay_usecs = 35,
122		}, {
123			.rx_buf = st->rx,
124			.bits_per_word = 8,
125			.len = 2,
126			.cs_change = 1,
127			.delay_usecs = 35,
128		},
129	};
130
131	mutex_lock(&st->buf_lock);
132	st->tx[0] = ADIS16220_READ_REG(lower_reg_address);
133	st->tx[1] = 0;
134
135	spi_message_init(&msg);
136	spi_message_add_tail(&xfers[0], &msg);
137	spi_message_add_tail(&xfers[1], &msg);
138	ret = spi_sync(st->us, &msg);
139	if (ret) {
140		dev_err(&st->us->dev,
141			"problem when reading 16 bit register 0x%02X",
142			lower_reg_address);
143		goto error_ret;
144	}
145	*val = (st->rx[0] << 8) | st->rx[1];
146
147error_ret:
148	mutex_unlock(&st->buf_lock);
149	return ret;
150}
151
152static ssize_t adis16220_spi_read_signed(struct device *dev,
153		struct device_attribute *attr,
154		char *buf,
155		unsigned bits)
156{
157	int ret;
158	s16 val = 0;
159	unsigned shift = 16 - bits;
160	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
161
162	ret = adis16220_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
163	if (ret)
164		return ret;
165
166	val = ((s16)(val << shift) >> shift);
167	return sprintf(buf, "%d\n", val);
168}
169
170static ssize_t adis16220_read_12bit_unsigned(struct device *dev,
171		struct device_attribute *attr,
172		char *buf)
173{
174	int ret;
175	u16 val = 0;
176	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
177
178	ret = adis16220_spi_read_reg_16(dev, this_attr->address, &val);
179	if (ret)
180		return ret;
181
182	return sprintf(buf, "%u\n", val & 0x0FFF);
183}
184
185static ssize_t adis16220_read_16bit(struct device *dev,
186		struct device_attribute *attr,
187		char *buf)
188{
189	struct iio_dev *indio_dev = dev_get_drvdata(dev);
190	ssize_t ret;
191
192	/* Take the iio_dev status lock */
193	mutex_lock(&indio_dev->mlock);
194	ret =  adis16220_spi_read_signed(dev, attr, buf, 16);
195	mutex_unlock(&indio_dev->mlock);
196
197	return ret;
198}
199
200static ssize_t adis16220_write_16bit(struct device *dev,
201		struct device_attribute *attr,
202		const char *buf,
203		size_t len)
204{
205	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
206	int ret;
207	long val;
208
209	ret = strict_strtol(buf, 10, &val);
210	if (ret)
211		goto error_ret;
212	ret = adis16220_spi_write_reg_16(dev, this_attr->address, val);
213
214error_ret:
215	return ret ? ret : len;
216}
217
218static int adis16220_capture(struct device *dev)
219{
220	int ret;
221	ret = adis16220_spi_write_reg_16(dev,
222			ADIS16220_GLOB_CMD,
223			0xBF08); /* initiates a manual data capture */
224	if (ret)
225		dev_err(dev, "problem beginning capture");
226
227	msleep(10); /* delay for capture to finish */
228
229	return ret;
230}
231
232static int adis16220_reset(struct device *dev)
233{
234	int ret;
235	ret = adis16220_spi_write_reg_8(dev,
236			ADIS16220_GLOB_CMD,
237			ADIS16220_GLOB_CMD_SW_RESET);
238	if (ret)
239		dev_err(dev, "problem resetting device");
240
241	return ret;
242}
243
244static ssize_t adis16220_write_reset(struct device *dev,
245		struct device_attribute *attr,
246		const char *buf, size_t len)
247{
248	if (len < 1)
249		return -1;
250	switch (buf[0]) {
251	case '1':
252	case 'y':
253	case 'Y':
254		return adis16220_reset(dev) == 0 ? len : -EIO;
255	}
256	return -1;
257}
258
259static ssize_t adis16220_write_capture(struct device *dev,
260		struct device_attribute *attr,
261		const char *buf, size_t len)
262{
263	if (len < 1)
264		return -1;
265	switch (buf[0]) {
266	case '1':
267	case 'y':
268	case 'Y':
269		return adis16220_capture(dev) == 0 ? len : -EIO;
270	}
271	return -1;
272}
273
274static int adis16220_check_status(struct device *dev)
275{
276	u16 status;
277	int ret;
278
279	ret = adis16220_spi_read_reg_16(dev, ADIS16220_DIAG_STAT, &status);
280
281	if (ret < 0) {
282		dev_err(dev, "Reading status failed\n");
283		goto error_ret;
284	}
285	ret = status & 0x7F;
286
287	if (status & ADIS16220_DIAG_STAT_VIOLATION)
288		dev_err(dev, "Capture period violation/interruption\n");
289	if (status & ADIS16220_DIAG_STAT_SPI_FAIL)
290		dev_err(dev, "SPI failure\n");
291	if (status & ADIS16220_DIAG_STAT_FLASH_UPT)
292		dev_err(dev, "Flash update failed\n");
293	if (status & ADIS16220_DIAG_STAT_POWER_HIGH)
294		dev_err(dev, "Power supply above 3.625V\n");
295	if (status & ADIS16220_DIAG_STAT_POWER_LOW)
296		dev_err(dev, "Power supply below 3.15V\n");
297
298error_ret:
299	return ret;
300}
301
302static int adis16220_self_test(struct device *dev)
303{
304	int ret;
305	ret = adis16220_spi_write_reg_16(dev,
306			ADIS16220_MSC_CTRL,
307			ADIS16220_MSC_CTRL_SELF_TEST_EN);
308	if (ret) {
309		dev_err(dev, "problem starting self test");
310		goto err_ret;
311	}
312
313	adis16220_check_status(dev);
314
315err_ret:
316	return ret;
317}
318
319static int adis16220_initial_setup(struct adis16220_state *st)
320{
321	int ret;
322	struct device *dev = &st->indio_dev->dev;
323
324	/* Do self test */
325	ret = adis16220_self_test(dev);
326	if (ret) {
327		dev_err(dev, "self test failure");
328		goto err_ret;
329	}
330
331	/* Read status register to check the result */
332	ret = adis16220_check_status(dev);
333	if (ret) {
334		adis16220_reset(dev);
335		dev_err(dev, "device not playing ball -> reset");
336		msleep(ADIS16220_STARTUP_DELAY);
337		ret = adis16220_check_status(dev);
338		if (ret) {
339			dev_err(dev, "giving up");
340			goto err_ret;
341		}
342	}
343
344	printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
345			st->us->chip_select, st->us->irq);
346
347err_ret:
348	return ret;
349}
350
351static ssize_t adis16220_capture_buffer_read(struct adis16220_state *st,
352					char *buf,
353					loff_t off,
354					size_t count,
355					int addr)
356{
357	struct spi_message msg;
358	struct spi_transfer xfers[] = {
359		{
360			.tx_buf = st->tx,
361			.bits_per_word = 8,
362			.len = 2,
363			.cs_change = 1,
364			.delay_usecs = 25,
365		}, {
366			.tx_buf = st->tx,
367			.rx_buf = st->rx,
368			.bits_per_word = 8,
369			.cs_change = 1,
370			.delay_usecs = 25,
371		},
372	};
373	int ret;
374	int i;
375
376	if (unlikely(!count))
377		return count;
378
379	if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
380		return -EINVAL;
381
382	if (off + count > ADIS16220_CAPTURE_SIZE)
383		count = ADIS16220_CAPTURE_SIZE - off;
384
385	/* write the begin position of capture buffer */
386	ret = adis16220_spi_write_reg_16(&st->indio_dev->dev,
387					ADIS16220_CAPT_PNTR,
388					off > 1);
389	if (ret)
390		return -EIO;
391
392	/* read count/2 values from capture buffer */
393	mutex_lock(&st->buf_lock);
394
395	for (i = 0; i < count; i += 2) {
396		st->tx[i] = ADIS16220_READ_REG(addr);
397		st->tx[i + 1] = 0;
398	}
399	xfers[1].len = count;
400
401	spi_message_init(&msg);
402	spi_message_add_tail(&xfers[0], &msg);
403	spi_message_add_tail(&xfers[1], &msg);
404	ret = spi_sync(st->us, &msg);
405	if (ret) {
406
407		mutex_unlock(&st->buf_lock);
408		return -EIO;
409	}
410
411	memcpy(buf, st->rx, count);
412
413	mutex_unlock(&st->buf_lock);
414	return count;
415}
416
417static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
418					struct bin_attribute *attr,
419					char *buf,
420					loff_t off,
421					size_t count)
422{
423	struct device *dev = container_of(kobj, struct device, kobj);
424	struct iio_dev *indio_dev = dev_get_drvdata(dev);
425	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
426
427	return adis16220_capture_buffer_read(st, buf,
428					off, count,
429					ADIS16220_CAPT_BUFA);
430}
431
432static struct bin_attribute accel_bin = {
433	.attr = {
434		.name = "accel_bin",
435		.mode = S_IRUGO,
436	},
437	.read = adis16220_accel_bin_read,
438	.size = ADIS16220_CAPTURE_SIZE,
439};
440
441static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
442				struct bin_attribute *attr,
443				char *buf, loff_t off,
444				size_t count)
445{
446	struct device *dev = container_of(kobj, struct device, kobj);
447	struct iio_dev *indio_dev = dev_get_drvdata(dev);
448	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
449
450	return adis16220_capture_buffer_read(st, buf,
451					off, count,
452					ADIS16220_CAPT_BUF1);
453}
454
455static struct bin_attribute adc1_bin = {
456	.attr = {
457		.name = "in0_bin",
458		.mode = S_IRUGO,
459	},
460	.read =  adis16220_adc1_bin_read,
461	.size = ADIS16220_CAPTURE_SIZE,
462};
463
464static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
465				struct bin_attribute *attr,
466				char *buf, loff_t off,
467				size_t count)
468{
469	struct device *dev = container_of(kobj, struct device, kobj);
470	struct iio_dev *indio_dev = dev_get_drvdata(dev);
471	struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
472
473	return adis16220_capture_buffer_read(st, buf,
474					off, count,
475					ADIS16220_CAPT_BUF2);
476}
477
478
479static struct bin_attribute adc2_bin = {
480	.attr = {
481		.name = "in1_bin",
482		.mode = S_IRUGO,
483	},
484	.read =  adis16220_adc2_bin_read,
485	.size = ADIS16220_CAPTURE_SIZE,
486};
487
488static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16220_read_12bit_unsigned,
489		ADIS16220_CAPT_SUPPLY);
490static IIO_CONST_ATTR(in_supply_scale, "0.0012207");
491static IIO_DEV_ATTR_ACCEL(adis16220_read_16bit, ADIS16220_CAPT_BUFA);
492static IIO_DEVICE_ATTR(accel_peak_raw, S_IRUGO, adis16220_read_16bit,
493		NULL, ADIS16220_CAPT_PEAKA);
494static IIO_DEV_ATTR_ACCEL_OFFSET(S_IWUSR | S_IRUGO,
495		adis16220_read_16bit,
496		adis16220_write_16bit,
497		ADIS16220_ACCL_NULL);
498static IIO_DEV_ATTR_TEMP_RAW(adis16220_read_12bit_unsigned);
499static IIO_CONST_ATTR(temp_offset, "25");
500static IIO_CONST_ATTR(temp_scale, "-0.47");
501
502static IIO_DEV_ATTR_IN_RAW(0, adis16220_read_16bit, ADIS16220_CAPT_BUF1);
503static IIO_DEV_ATTR_IN_RAW(1, adis16220_read_16bit, ADIS16220_CAPT_BUF2);
504
505static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
506		adis16220_write_reset, 0);
507
508#define IIO_DEV_ATTR_CAPTURE(_store)				\
509	IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
510
511static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
512
513#define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr)		\
514	IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
515
516static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
517		adis16220_read_16bit,
518		adis16220_write_16bit,
519		ADIS16220_CAPT_PNTR);
520
521static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("100200");
522
523static IIO_CONST_ATTR(name, "adis16220");
524
525static struct attribute *adis16220_attributes[] = {
526	&iio_dev_attr_in_supply_raw.dev_attr.attr,
527	&iio_const_attr_in_supply_scale.dev_attr.attr,
528	&iio_dev_attr_accel_raw.dev_attr.attr,
529	&iio_dev_attr_accel_offset.dev_attr.attr,
530	&iio_dev_attr_accel_peak_raw.dev_attr.attr,
531	&iio_dev_attr_temp_raw.dev_attr.attr,
532	&iio_dev_attr_in0_raw.dev_attr.attr,
533	&iio_dev_attr_in1_raw.dev_attr.attr,
534	&iio_const_attr_temp_offset.dev_attr.attr,
535	&iio_const_attr_temp_scale.dev_attr.attr,
536	&iio_const_attr_available_sampling_frequency.dev_attr.attr,
537	&iio_dev_attr_reset.dev_attr.attr,
538	&iio_dev_attr_capture.dev_attr.attr,
539	&iio_dev_attr_capture_count.dev_attr.attr,
540	&iio_const_attr_name.dev_attr.attr,
541	NULL
542};
543
544static const struct attribute_group adis16220_attribute_group = {
545	.attrs = adis16220_attributes,
546};
547
548static int __devinit adis16220_probe(struct spi_device *spi)
549{
550	int ret, regdone = 0;
551	struct adis16220_state *st = kzalloc(sizeof *st, GFP_KERNEL);
552	if (!st) {
553		ret =  -ENOMEM;
554		goto error_ret;
555	}
556	/* this is only used for removal purposes */
557	spi_set_drvdata(spi, st);
558
559	/* Allocate the comms buffers */
560	st->rx = kzalloc(sizeof(*st->rx)*ADIS16220_MAX_RX, GFP_KERNEL);
561	if (st->rx == NULL) {
562		ret = -ENOMEM;
563		goto error_free_st;
564	}
565	st->tx = kzalloc(sizeof(*st->tx)*ADIS16220_MAX_TX, GFP_KERNEL);
566	if (st->tx == NULL) {
567		ret = -ENOMEM;
568		goto error_free_rx;
569	}
570	st->us = spi;
571	mutex_init(&st->buf_lock);
572	/* setup the industrialio driver allocated elements */
573	st->indio_dev = iio_allocate_device();
574	if (st->indio_dev == NULL) {
575		ret = -ENOMEM;
576		goto error_free_tx;
577	}
578
579	st->indio_dev->dev.parent = &spi->dev;
580	st->indio_dev->attrs = &adis16220_attribute_group;
581	st->indio_dev->dev_data = (void *)(st);
582	st->indio_dev->driver_module = THIS_MODULE;
583	st->indio_dev->modes = INDIO_DIRECT_MODE;
584
585	ret = iio_device_register(st->indio_dev);
586	if (ret)
587		goto error_free_dev;
588	regdone = 1;
589
590	ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
591	if (ret)
592		goto error_free_dev;
593
594	ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
595	if (ret)
596		goto error_rm_accel_bin;
597
598	ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
599	if (ret)
600		goto error_rm_adc1_bin;
601
602	/* Get the device into a sane initial state */
603	ret = adis16220_initial_setup(st);
604	if (ret)
605		goto error_rm_adc2_bin;
606	return 0;
607
608error_rm_adc2_bin:
609	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
610error_rm_adc1_bin:
611	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
612error_rm_accel_bin:
613	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
614error_free_dev:
615	if (regdone)
616		iio_device_unregister(st->indio_dev);
617	else
618		iio_free_device(st->indio_dev);
619error_free_tx:
620	kfree(st->tx);
621error_free_rx:
622	kfree(st->rx);
623error_free_st:
624	kfree(st);
625error_ret:
626	return ret;
627}
628
629static int adis16220_remove(struct spi_device *spi)
630{
631	struct adis16220_state *st = spi_get_drvdata(spi);
632	struct iio_dev *indio_dev = st->indio_dev;
633
634	flush_scheduled_work();
635
636	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
637	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
638	sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
639	iio_device_unregister(indio_dev);
640	kfree(st->tx);
641	kfree(st->rx);
642	kfree(st);
643
644	return 0;
645}
646
647static struct spi_driver adis16220_driver = {
648	.driver = {
649		.name = "adis16220",
650		.owner = THIS_MODULE,
651	},
652	.probe = adis16220_probe,
653	.remove = __devexit_p(adis16220_remove),
654};
655
656static __init int adis16220_init(void)
657{
658	return spi_register_driver(&adis16220_driver);
659}
660module_init(adis16220_init);
661
662static __exit void adis16220_exit(void)
663{
664	spi_unregister_driver(&adis16220_driver);
665}
666module_exit(adis16220_exit);
667
668MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
669MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
670MODULE_LICENSE("GPL v2");
671