Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 1 | /* |
| 2 | * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90 |
| 3 | * |
| 4 | * Copyright (c) 2010-2010 Analog Devices Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/sysfs.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 18 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/sysfs.h> |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 21 | |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 22 | struct ad2s90_state { |
| 23 | struct mutex lock; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 24 | struct spi_device *sdev; |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 25 | u8 rx[2] ____cacheline_aligned; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 26 | }; |
| 27 | |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 28 | static int ad2s90_read_raw(struct iio_dev *indio_dev, |
| 29 | struct iio_chan_spec const *chan, |
| 30 | int *val, |
| 31 | int *val2, |
| 32 | long m) |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 33 | { |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 34 | int ret; |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 35 | struct ad2s90_state *st = iio_priv(indio_dev); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 36 | |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 37 | mutex_lock(&st->lock); |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 38 | ret = spi_read(st->sdev, st->rx, 2); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 39 | if (ret) |
| 40 | goto error_ret; |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 41 | *val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4); |
| 42 | |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 43 | error_ret: |
| 44 | mutex_unlock(&st->lock); |
| 45 | |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 46 | return IIO_VAL_INT; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 49 | static const struct iio_info ad2s90_info = { |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 50 | .read_raw = &ad2s90_read_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 51 | .driver_module = THIS_MODULE, |
| 52 | }; |
| 53 | |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 54 | static const struct iio_chan_spec ad2s90_chan = { |
| 55 | .type = IIO_ANGL, |
| 56 | .indexed = 1, |
| 57 | .channel = 0, |
Jonathan Cameron | 3589ba9 | 2013-02-27 19:41:51 +0000 | [diff] [blame] | 58 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 61 | static int ad2s90_probe(struct spi_device *spi) |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 62 | { |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 63 | struct iio_dev *indio_dev; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 64 | struct ad2s90_state *st; |
| 65 | int ret = 0; |
| 66 | |
Sachin Kamat | 6bba798 | 2013-09-11 10:55:00 +0100 | [diff] [blame] | 67 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 68 | if (!indio_dev) |
| 69 | return -ENOMEM; |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 70 | st = iio_priv(indio_dev); |
| 71 | spi_set_drvdata(spi, indio_dev); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 72 | |
| 73 | mutex_init(&st->lock); |
| 74 | st->sdev = spi; |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 75 | indio_dev->dev.parent = &spi->dev; |
| 76 | indio_dev->info = &ad2s90_info; |
| 77 | indio_dev->modes = INDIO_DIRECT_MODE; |
Jonathan Cameron | 199d847 | 2011-10-05 15:27:58 +0100 | [diff] [blame] | 78 | indio_dev->channels = &ad2s90_chan; |
| 79 | indio_dev->num_channels = 1; |
Jonathan Cameron | 19147f5 | 2011-10-05 15:27:57 +0100 | [diff] [blame] | 80 | indio_dev->name = spi_get_device_id(spi)->name; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 81 | |
Jonathan Cameron | 72a86cc | 2011-10-05 15:27:56 +0100 | [diff] [blame] | 82 | ret = iio_device_register(indio_dev); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 83 | if (ret) |
Sachin Kamat | 6bba798 | 2013-09-11 10:55:00 +0100 | [diff] [blame] | 84 | return ret; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 85 | |
| 86 | /* need 600ns between CS and the first falling edge of SCLK */ |
| 87 | spi->max_speed_hz = 830000; |
| 88 | spi->mode = SPI_MODE_3; |
| 89 | spi_setup(spi); |
| 90 | |
| 91 | return 0; |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 94 | static int ad2s90_remove(struct spi_device *spi) |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 95 | { |
Jonathan Cameron | 58f08b0 | 2011-06-27 13:07:53 +0100 | [diff] [blame] | 96 | iio_device_unregister(spi_get_drvdata(spi)); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Jonathan Cameron | 19147f5 | 2011-10-05 15:27:57 +0100 | [diff] [blame] | 101 | static const struct spi_device_id ad2s90_id[] = { |
| 102 | { "ad2s90" }, |
| 103 | {} |
| 104 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 105 | MODULE_DEVICE_TABLE(spi, ad2s90_id); |
Jonathan Cameron | 19147f5 | 2011-10-05 15:27:57 +0100 | [diff] [blame] | 106 | |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 107 | static struct spi_driver ad2s90_driver = { |
| 108 | .driver = { |
Jonathan Cameron | 19147f5 | 2011-10-05 15:27:57 +0100 | [diff] [blame] | 109 | .name = "ad2s90", |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 110 | .owner = THIS_MODULE, |
| 111 | }, |
| 112 | .probe = ad2s90_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 113 | .remove = ad2s90_remove, |
Jonathan Cameron | 19147f5 | 2011-10-05 15:27:57 +0100 | [diff] [blame] | 114 | .id_table = ad2s90_id, |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 115 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 116 | module_spi_driver(ad2s90_driver); |
Graf Yang | f46d9f1 | 2010-10-27 21:44:19 -0400 | [diff] [blame] | 117 | |
| 118 | MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>"); |
| 119 | MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver"); |
| 120 | MODULE_LICENSE("GPL v2"); |