Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * kxsd9.c simple support for the Kionix KXSD9 3D |
| 3 | * accelerometer. |
| 4 | * |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 5 | * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 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 | * The i2c interface is very similar, so shouldn't be a problem once |
| 12 | * I have a suitable wire made up. |
| 13 | * |
| 14 | * TODO: Support the motion detector |
| 15 | * Uses register address incrementing so could have a |
| 16 | * heavily optimized ring buffer access function. |
| 17 | */ |
| 18 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/spi/spi.h> |
| 22 | #include <linux/sysfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 25 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 26 | #include <linux/iio/iio.h> |
| 27 | #include <linux/iio/sysfs.h> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 28 | |
| 29 | #define KXSD9_REG_X 0x00 |
| 30 | #define KXSD9_REG_Y 0x02 |
| 31 | #define KXSD9_REG_Z 0x04 |
| 32 | #define KXSD9_REG_AUX 0x06 |
| 33 | #define KXSD9_REG_RESET 0x0a |
| 34 | #define KXSD9_REG_CTRL_C 0x0c |
| 35 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 36 | #define KXSD9_FS_MASK 0x03 |
| 37 | |
| 38 | #define KXSD9_REG_CTRL_B 0x0d |
| 39 | #define KXSD9_REG_CTRL_A 0x0e |
| 40 | |
| 41 | #define KXSD9_READ(a) (0x80 | (a)) |
| 42 | #define KXSD9_WRITE(a) (a) |
| 43 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 44 | #define KXSD9_STATE_RX_SIZE 2 |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 45 | #define KXSD9_STATE_TX_SIZE 2 |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 46 | /** |
| 47 | * struct kxsd9_state - device related storage |
| 48 | * @buf_lock: protect the rx and tx buffers. |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 49 | * @us: spi device |
| 50 | * @rx: single rx buffer storage |
| 51 | * @tx: single tx buffer storage |
| 52 | **/ |
| 53 | struct kxsd9_state { |
| 54 | struct mutex buf_lock; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 55 | struct spi_device *us; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 56 | u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned; |
| 57 | u8 tx[KXSD9_STATE_TX_SIZE]; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 60 | #define KXSD9_SCALE_2G "0.011978" |
| 61 | #define KXSD9_SCALE_4G "0.023927" |
| 62 | #define KXSD9_SCALE_6G "0.035934" |
| 63 | #define KXSD9_SCALE_8G "0.047853" |
| 64 | |
| 65 | /* reverse order */ |
| 66 | static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 }; |
| 67 | |
| 68 | static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 69 | { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 70 | int ret, i; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 71 | struct kxsd9_state *st = iio_priv(indio_dev); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 72 | bool foundit = false; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 73 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 74 | for (i = 0; i < 4; i++) |
| 75 | if (micro == kxsd9_micro_scales[i]) { |
| 76 | foundit = true; |
| 77 | break; |
| 78 | } |
| 79 | if (!foundit) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 80 | return -EINVAL; |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 81 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 82 | mutex_lock(&st->buf_lock); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 83 | ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C)); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 84 | if (ret) |
| 85 | goto error_ret; |
| 86 | st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 87 | st->tx[1] = (ret & ~KXSD9_FS_MASK) | i; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 88 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 89 | ret = spi_write(st->us, st->tx, 2); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 90 | error_ret: |
| 91 | mutex_unlock(&st->buf_lock); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 92 | return ret; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 93 | } |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 94 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 95 | static int kxsd9_read(struct iio_dev *indio_dev, u8 address) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 96 | { |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 97 | int ret; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 98 | struct kxsd9_state *st = iio_priv(indio_dev); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 99 | struct spi_transfer xfers[] = { |
| 100 | { |
| 101 | .bits_per_word = 8, |
| 102 | .len = 1, |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 103 | .delay_usecs = 200, |
| 104 | .tx_buf = st->tx, |
| 105 | }, { |
| 106 | .bits_per_word = 8, |
| 107 | .len = 2, |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 108 | .rx_buf = st->rx, |
| 109 | }, |
| 110 | }; |
| 111 | |
| 112 | mutex_lock(&st->buf_lock); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 113 | st->tx[0] = KXSD9_READ(address); |
Lars-Peter Clausen | 14543a0 | 2013-01-09 17:31:00 +0000 | [diff] [blame] | 114 | ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers)); |
Frank Zago | 0ee005c | 2013-11-13 22:53:00 +0000 | [diff] [blame] | 115 | if (!ret) |
| 116 | ret = (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0); |
| 117 | mutex_unlock(&st->buf_lock); |
| 118 | return ret; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 121 | static IIO_CONST_ATTR(accel_scale_available, |
| 122 | KXSD9_SCALE_2G " " |
| 123 | KXSD9_SCALE_4G " " |
| 124 | KXSD9_SCALE_6G " " |
| 125 | KXSD9_SCALE_8G); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 126 | |
| 127 | static struct attribute *kxsd9_attributes[] = { |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 128 | &iio_const_attr_accel_scale_available.dev_attr.attr, |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 129 | NULL, |
| 130 | }; |
| 131 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 132 | static int kxsd9_write_raw(struct iio_dev *indio_dev, |
| 133 | struct iio_chan_spec const *chan, |
| 134 | int val, |
| 135 | int val2, |
| 136 | long mask) |
| 137 | { |
| 138 | int ret = -EINVAL; |
| 139 | |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 140 | if (mask == IIO_CHAN_INFO_SCALE) { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 141 | /* Check no integer component */ |
| 142 | if (val) |
| 143 | return -EINVAL; |
| 144 | ret = kxsd9_write_scale(indio_dev, val2); |
| 145 | } |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static int kxsd9_read_raw(struct iio_dev *indio_dev, |
| 151 | struct iio_chan_spec const *chan, |
| 152 | int *val, int *val2, long mask) |
| 153 | { |
| 154 | int ret = -EINVAL; |
| 155 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 156 | |
| 157 | switch (mask) { |
Jonathan Cameron | 31313fc | 2012-04-15 17:41:17 +0100 | [diff] [blame] | 158 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 159 | ret = kxsd9_read(indio_dev, chan->address); |
| 160 | if (ret < 0) |
| 161 | goto error_ret; |
| 162 | *val = ret; |
| 163 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 164 | case IIO_CHAN_INFO_SCALE: |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 165 | ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C)); |
| 166 | if (ret) |
| 167 | goto error_ret; |
| 168 | *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK]; |
| 169 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 170 | break; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 171 | } |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 172 | |
| 173 | error_ret: |
| 174 | return ret; |
| 175 | }; |
| 176 | #define KXSD9_ACCEL_CHAN(axis) \ |
| 177 | { \ |
| 178 | .type = IIO_ACCEL, \ |
| 179 | .modified = 1, \ |
| 180 | .channel2 = IIO_MOD_##axis, \ |
Jonathan Cameron | 2f6bb53 | 2013-02-27 19:03:36 +0000 | [diff] [blame] | 181 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 182 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 183 | .address = KXSD9_REG_##axis, \ |
| 184 | } |
| 185 | |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame] | 186 | static const struct iio_chan_spec kxsd9_channels[] = { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 187 | KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z), |
| 188 | { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 189 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 2f6bb53 | 2013-02-27 19:03:36 +0000 | [diff] [blame] | 190 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 191 | .indexed = 1, |
| 192 | .address = KXSD9_REG_AUX, |
| 193 | } |
| 194 | }; |
| 195 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 196 | static const struct attribute_group kxsd9_attribute_group = { |
| 197 | .attrs = kxsd9_attributes, |
| 198 | }; |
| 199 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 200 | static int kxsd9_power_up(struct kxsd9_state *st) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 201 | { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
anish kumar | 3fd47d4 | 2011-06-27 13:08:00 +0100 | [diff] [blame] | 204 | st->tx[0] = 0x0d; |
| 205 | st->tx[1] = 0x40; |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 206 | ret = spi_write(st->us, st->tx, 2); |
| 207 | if (ret) |
| 208 | return ret; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 209 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 210 | st->tx[0] = 0x0c; |
| 211 | st->tx[1] = 0x9b; |
| 212 | return spi_write(st->us, st->tx, 2); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 213 | }; |
| 214 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 215 | static const struct iio_info kxsd9_info = { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 216 | .read_raw = &kxsd9_read_raw, |
| 217 | .write_raw = &kxsd9_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 218 | .attrs = &kxsd9_attribute_group, |
| 219 | .driver_module = THIS_MODULE, |
| 220 | }; |
| 221 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 222 | static int kxsd9_probe(struct spi_device *spi) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 223 | { |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 224 | struct iio_dev *indio_dev; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 225 | struct kxsd9_state *st; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 226 | |
Sachin Kamat | 4ee3093 | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 227 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 228 | if (!indio_dev) |
| 229 | return -ENOMEM; |
| 230 | |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 231 | st = iio_priv(indio_dev); |
| 232 | spi_set_drvdata(spi, indio_dev); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 233 | |
| 234 | st->us = spi; |
| 235 | mutex_init(&st->buf_lock); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 236 | indio_dev->channels = kxsd9_channels; |
| 237 | indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels); |
| 238 | indio_dev->name = spi_get_device_id(spi)->name; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 239 | indio_dev->dev.parent = &spi->dev; |
| 240 | indio_dev->info = &kxsd9_info; |
| 241 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 242 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 243 | spi->mode = SPI_MODE_0; |
| 244 | spi_setup(spi); |
anish kumar | 3fd47d4 | 2011-06-27 13:08:00 +0100 | [diff] [blame] | 245 | kxsd9_power_up(st); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 246 | |
Sachin Kamat | dcf5272 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 247 | return iio_device_register(indio_dev); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 250 | static int kxsd9_remove(struct spi_device *spi) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 251 | { |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 252 | iio_device_unregister(spi_get_drvdata(spi)); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 257 | static const struct spi_device_id kxsd9_id[] = { |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 258 | {"kxsd9", 0}, |
| 259 | { }, |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 260 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 261 | MODULE_DEVICE_TABLE(spi, kxsd9_id); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 262 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 263 | static struct spi_driver kxsd9_driver = { |
| 264 | .driver = { |
| 265 | .name = "kxsd9", |
| 266 | .owner = THIS_MODULE, |
| 267 | }, |
| 268 | .probe = kxsd9_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 269 | .remove = kxsd9_remove, |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 270 | .id_table = kxsd9_id, |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 271 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 272 | module_spi_driver(kxsd9_driver); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 273 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 274 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 275 | MODULE_DESCRIPTION("Kionix KXSD9 SPI driver"); |
| 276 | MODULE_LICENSE("GPL v2"); |