blob: 1f9e9a867f342108d645b29aa78c9e874cbf81b5 [file] [log] [blame]
Jonathan Camerone435bc12009-08-18 18:06:23 +01001/*
2 * kxsd9.c simple support for the Kionix KXSD9 3D
3 * accelerometer.
4 *
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01005 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
Jonathan Camerone435bc12009-08-18 18:06:23 +01006 *
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 Camerone435bc12009-08-18 18:06:23 +010019#include <linux/device.h>
20#include <linux/kernel.h>
21#include <linux/spi/spi.h>
22#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040024#include <linux/module.h>
Jonathan Camerone435bc12009-08-18 18:06:23 +010025
Jonathan Cameron06458e22012-04-25 15:54:58 +010026#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
Jonathan Camerone435bc12009-08-18 18:06:23 +010028
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 Camerone435bc12009-08-18 18:06:23 +010036#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 Camerone435bc12009-08-18 18:06:23 +010044#define KXSD9_STATE_RX_SIZE 2
Jonathan Camerond34dbee2011-08-12 17:47:52 +010045#define KXSD9_STATE_TX_SIZE 2
Linus Walleij9f907972016-09-01 11:44:36 +020046
47struct kxsd9_transport;
48
Jonathan Camerone435bc12009-08-18 18:06:23 +010049/**
Linus Walleij9f907972016-09-01 11:44:36 +020050 * struct kxsd9_transport - transport adapter for SPI or I2C
51 * @trdev: transport device such as SPI or I2C
52 * @write1(): function to write a byte to the device
53 * @write2(): function to write two consecutive bytes to the device
54 * @readval(): function to read a 16bit value from the device
55 * @rx: cache aligned read buffer
56 * @tx: cache aligned write buffer
57 */
58struct kxsd9_transport {
59 void *trdev;
60 int (*write1) (struct kxsd9_transport *tr, u8 byte);
61 int (*write2) (struct kxsd9_transport *tr, u8 b1, u8 b2);
62 int (*readval) (struct kxsd9_transport *tr, u8 address);
Jonathan Cameroned0c0122011-06-27 13:17:56 +010063 u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned;
64 u8 tx[KXSD9_STATE_TX_SIZE];
Jonathan Camerone435bc12009-08-18 18:06:23 +010065};
66
Linus Walleij9f907972016-09-01 11:44:36 +020067/**
68 * struct kxsd9_state - device related storage
69 * @transport: transport for the KXSD9
70 * @buf_lock: protect the rx and tx buffers.
71 * @us: spi device
72 **/
73struct kxsd9_state {
74 struct kxsd9_transport *transport;
75 struct mutex buf_lock;
76};
77
Jonathan Camerond34dbee2011-08-12 17:47:52 +010078#define KXSD9_SCALE_2G "0.011978"
79#define KXSD9_SCALE_4G "0.023927"
80#define KXSD9_SCALE_6G "0.035934"
81#define KXSD9_SCALE_8G "0.047853"
82
83/* reverse order */
84static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
85
86static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
Jonathan Camerone435bc12009-08-18 18:06:23 +010087{
Jonathan Camerond34dbee2011-08-12 17:47:52 +010088 int ret, i;
Jonathan Cameroned0c0122011-06-27 13:17:56 +010089 struct kxsd9_state *st = iio_priv(indio_dev);
Jonathan Camerond34dbee2011-08-12 17:47:52 +010090 bool foundit = false;
Jonathan Camerone435bc12009-08-18 18:06:23 +010091
Jonathan Camerond34dbee2011-08-12 17:47:52 +010092 for (i = 0; i < 4; i++)
93 if (micro == kxsd9_micro_scales[i]) {
94 foundit = true;
95 break;
96 }
97 if (!foundit)
Jonathan Camerone435bc12009-08-18 18:06:23 +010098 return -EINVAL;
Jonathan Cameronf3fb0012010-05-04 14:42:58 +010099
Jonathan Camerone435bc12009-08-18 18:06:23 +0100100 mutex_lock(&st->buf_lock);
Linus Walleij9f907972016-09-01 11:44:36 +0200101 ret = st->transport->write1(st->transport, KXSD9_READ(KXSD9_REG_CTRL_C));
102 if (ret)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100103 goto error_ret;
Linus Walleij9f907972016-09-01 11:44:36 +0200104 ret = st->transport->write2(st->transport,
105 KXSD9_WRITE(KXSD9_REG_CTRL_C),
106 (ret & ~KXSD9_FS_MASK) | i);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100107error_ret:
108 mutex_unlock(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100109 return ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100110}
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100111
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100112static int kxsd9_read(struct iio_dev *indio_dev, u8 address)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100113{
Jonathan Camerone435bc12009-08-18 18:06:23 +0100114 int ret;
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100115 struct kxsd9_state *st = iio_priv(indio_dev);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100116
117 mutex_lock(&st->buf_lock);
Linus Walleij9f907972016-09-01 11:44:36 +0200118 ret = st->transport->readval(st->transport, KXSD9_READ(address));
Frank Zago0ee005c2013-11-13 22:53:00 +0000119 mutex_unlock(&st->buf_lock);
120 return ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100121}
122
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100123static IIO_CONST_ATTR(accel_scale_available,
124 KXSD9_SCALE_2G " "
125 KXSD9_SCALE_4G " "
126 KXSD9_SCALE_6G " "
127 KXSD9_SCALE_8G);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100128
129static struct attribute *kxsd9_attributes[] = {
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100130 &iio_const_attr_accel_scale_available.dev_attr.attr,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100131 NULL,
132};
133
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100134static int kxsd9_write_raw(struct iio_dev *indio_dev,
135 struct iio_chan_spec const *chan,
136 int val,
137 int val2,
138 long mask)
139{
140 int ret = -EINVAL;
141
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100142 if (mask == IIO_CHAN_INFO_SCALE) {
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100143 /* Check no integer component */
144 if (val)
145 return -EINVAL;
146 ret = kxsd9_write_scale(indio_dev, val2);
147 }
148
149 return ret;
150}
151
152static int kxsd9_read_raw(struct iio_dev *indio_dev,
153 struct iio_chan_spec const *chan,
154 int *val, int *val2, long mask)
155{
156 int ret = -EINVAL;
157 struct kxsd9_state *st = iio_priv(indio_dev);
158
159 switch (mask) {
Jonathan Cameron31313fc2012-04-15 17:41:17 +0100160 case IIO_CHAN_INFO_RAW:
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100161 ret = kxsd9_read(indio_dev, chan->address);
162 if (ret < 0)
163 goto error_ret;
164 *val = ret;
Linus Walleij7ac61a02016-08-16 15:33:28 +0200165 ret = IIO_VAL_INT;
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100166 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100167 case IIO_CHAN_INFO_SCALE:
Linus Walleij9f907972016-09-01 11:44:36 +0200168 ret = st->transport->write1(st->transport, KXSD9_READ(KXSD9_REG_CTRL_C));
169 if (ret)
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100170 goto error_ret;
Linus Walleij307fe9d2016-09-01 11:44:35 +0200171 *val = 0;
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100172 *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
173 ret = IIO_VAL_INT_PLUS_MICRO;
174 break;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100175 }
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100176
177error_ret:
178 return ret;
179};
180#define KXSD9_ACCEL_CHAN(axis) \
181 { \
182 .type = IIO_ACCEL, \
183 .modified = 1, \
184 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron2f6bb532013-02-27 19:03:36 +0000185 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
186 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100187 .address = KXSD9_REG_##axis, \
188 }
189
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100190static const struct iio_chan_spec kxsd9_channels[] = {
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100191 KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
192 {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100193 .type = IIO_VOLTAGE,
Jonathan Cameron2f6bb532013-02-27 19:03:36 +0000194 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100195 .indexed = 1,
196 .address = KXSD9_REG_AUX,
197 }
198};
199
Jonathan Camerone435bc12009-08-18 18:06:23 +0100200static const struct attribute_group kxsd9_attribute_group = {
201 .attrs = kxsd9_attributes,
202};
203
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500204static int kxsd9_power_up(struct kxsd9_state *st)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100205{
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100206 int ret;
207
Linus Walleij9f907972016-09-01 11:44:36 +0200208 ret = st->transport->write2(st->transport, 0x0d, 0x40);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100209 if (ret)
210 return ret;
Linus Walleij9f907972016-09-01 11:44:36 +0200211 return st->transport->write2(st->transport, 0x0c, 0x9b);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100212};
213
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100214static const struct iio_info kxsd9_info = {
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100215 .read_raw = &kxsd9_read_raw,
216 .write_raw = &kxsd9_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100217 .attrs = &kxsd9_attribute_group,
218 .driver_module = THIS_MODULE,
219};
220
Linus Walleij9f907972016-09-01 11:44:36 +0200221static int kxsd9_common_probe(struct device *parent,
222 struct kxsd9_transport *transport,
Linus Walleij154021a2016-09-01 11:44:37 +0200223 const char *name)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100224{
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100225 struct iio_dev *indio_dev;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100226 struct kxsd9_state *st;
Linus Walleij9f907972016-09-01 11:44:36 +0200227 int ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100228
Linus Walleij9f907972016-09-01 11:44:36 +0200229 indio_dev = devm_iio_device_alloc(parent, sizeof(*st));
Sachin Kamat4ee30932013-07-23 07:47:00 +0100230 if (!indio_dev)
231 return -ENOMEM;
232
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100233 st = iio_priv(indio_dev);
Linus Walleij9f907972016-09-01 11:44:36 +0200234 st->transport = transport;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100235
Jonathan Camerone435bc12009-08-18 18:06:23 +0100236 mutex_init(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100237 indio_dev->channels = kxsd9_channels;
238 indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
Linus Walleij9f907972016-09-01 11:44:36 +0200239 indio_dev->name = name;
240 indio_dev->dev.parent = parent;
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100241 indio_dev->info = &kxsd9_info;
242 indio_dev->modes = INDIO_DIRECT_MODE;
243
anish kumar3fd47d42011-06-27 13:08:00 +0100244 kxsd9_power_up(st);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100245
Linus Walleij9f907972016-09-01 11:44:36 +0200246 ret = iio_device_register(indio_dev);
247 if (ret)
248 return ret;
249
Linus Walleij154021a2016-09-01 11:44:37 +0200250 dev_set_drvdata(parent, indio_dev);
251
252 return 0;
253}
254
255static int kxsd9_common_remove(struct device *parent)
256{
257 struct iio_dev *indio_dev = dev_get_drvdata(parent);
258
259 iio_device_unregister(indio_dev);
260
Linus Walleij9f907972016-09-01 11:44:36 +0200261 return 0;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100262}
263
Linus Walleij9f907972016-09-01 11:44:36 +0200264static int kxsd9_spi_write1(struct kxsd9_transport *tr, u8 byte)
265{
266 struct spi_device *spi = tr->trdev;
267
268 return spi_w8r8(spi, byte);
269}
270
271static int kxsd9_spi_write2(struct kxsd9_transport *tr, u8 b1, u8 b2)
272{
273 struct spi_device *spi = tr->trdev;
274
275 tr->tx[0] = b1;
276 tr->tx[1] = b2;
277 return spi_write(spi, tr->tx, 2);
278}
279
280static int kxsd9_spi_readval(struct kxsd9_transport *tr, u8 address)
281{
282 struct spi_device *spi = tr->trdev;
283 struct spi_transfer xfers[] = {
284 {
285 .bits_per_word = 8,
286 .len = 1,
287 .delay_usecs = 200,
288 .tx_buf = tr->tx,
289 }, {
290 .bits_per_word = 8,
291 .len = 2,
292 .rx_buf = tr->rx,
293 },
294 };
295 int ret;
296
297 tr->tx[0] = address;
298 ret = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers));
299 if (!ret)
300 ret = (((u16)(tr->rx[0])) << 8) | (tr->rx[1] & 0xF0);
301 return ret;
302}
303
304static int kxsd9_spi_probe(struct spi_device *spi)
305{
306 struct kxsd9_transport *transport;
Linus Walleij9f907972016-09-01 11:44:36 +0200307 int ret;
308
309 transport = devm_kzalloc(&spi->dev, sizeof(*transport), GFP_KERNEL);
310 if (!transport)
311 return -ENOMEM;
312
313 transport->trdev = spi;
314 transport->write1 = kxsd9_spi_write1;
315 transport->write2 = kxsd9_spi_write2;
316 transport->readval = kxsd9_spi_readval;
317 spi->mode = SPI_MODE_0;
318 spi_setup(spi);
319
320 ret = kxsd9_common_probe(&spi->dev,
321 transport,
Linus Walleij154021a2016-09-01 11:44:37 +0200322 spi_get_device_id(spi)->name);
Linus Walleij9f907972016-09-01 11:44:36 +0200323 if (ret)
324 return ret;
325
Linus Walleij9f907972016-09-01 11:44:36 +0200326 return 0;
327}
328
329static int kxsd9_spi_remove(struct spi_device *spi)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100330{
Linus Walleij154021a2016-09-01 11:44:37 +0200331 return kxsd9_common_remove(&spi->dev);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100332}
333
Linus Walleij9f907972016-09-01 11:44:36 +0200334static const struct spi_device_id kxsd9_spi_id[] = {
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100335 {"kxsd9", 0},
336 { },
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100337};
Linus Walleij9f907972016-09-01 11:44:36 +0200338MODULE_DEVICE_TABLE(spi, kxsd9_spi_id);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100339
Linus Walleij9f907972016-09-01 11:44:36 +0200340static struct spi_driver kxsd9_spi_driver = {
Jonathan Camerone435bc12009-08-18 18:06:23 +0100341 .driver = {
342 .name = "kxsd9",
Jonathan Camerone435bc12009-08-18 18:06:23 +0100343 },
Linus Walleij9f907972016-09-01 11:44:36 +0200344 .probe = kxsd9_spi_probe,
345 .remove = kxsd9_spi_remove,
346 .id_table = kxsd9_spi_id,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100347};
Linus Walleij9f907972016-09-01 11:44:36 +0200348module_spi_driver(kxsd9_spi_driver);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100349
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100350MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone435bc12009-08-18 18:06:23 +0100351MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
352MODULE_LICENSE("GPL v2");