]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/iio/accel/kxsd9.c
Merge remote-tracking branches 'asoc/topic/sgtl5000', 'asoc/topic/simple', 'asoc...
[mirror_ubuntu-bionic-kernel.git] / drivers / iio / accel / kxsd9.c
CommitLineData
e435bc19
JC
1/*
2 * kxsd9.c simple support for the Kionix KXSD9 3D
3 * accelerometer.
4 *
0f8c9620 5 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
e435bc19
JC
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
e435bc19
JC
19#include <linux/device.h>
20#include <linux/kernel.h>
21#include <linux/spi/spi.h>
22#include <linux/sysfs.h>
5a0e3ad6 23#include <linux/slab.h>
99c97852 24#include <linux/module.h>
e435bc19 25
06458e27
JC
26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
e435bc19
JC
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
e435bc19
JC
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
e435bc19 44#define KXSD9_STATE_RX_SIZE 2
d34dbee8 45#define KXSD9_STATE_TX_SIZE 2
e435bc19
JC
46/**
47 * struct kxsd9_state - device related storage
48 * @buf_lock: protect the rx and tx buffers.
e435bc19
JC
49 * @us: spi device
50 * @rx: single rx buffer storage
51 * @tx: single tx buffer storage
52 **/
53struct kxsd9_state {
54 struct mutex buf_lock;
e435bc19 55 struct spi_device *us;
ed0c012b
JC
56 u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned;
57 u8 tx[KXSD9_STATE_TX_SIZE];
e435bc19
JC
58};
59
d34dbee8
JC
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"
e435bc19 64
d34dbee8
JC
65/* reverse order */
66static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
e435bc19 67
d34dbee8 68static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
e435bc19 69{
d34dbee8 70 int ret, i;
ed0c012b 71 struct kxsd9_state *st = iio_priv(indio_dev);
d34dbee8
JC
72 bool foundit = false;
73
74 for (i = 0; i < 4; i++)
75 if (micro == kxsd9_micro_scales[i]) {
76 foundit = true;
77 break;
78 }
79 if (!foundit)
e435bc19 80 return -EINVAL;
f3fb0011 81
e435bc19 82 mutex_lock(&st->buf_lock);
d34dbee8 83 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
0c1f91b9 84 if (ret < 0)
e435bc19
JC
85 goto error_ret;
86 st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
d34dbee8 87 st->tx[1] = (ret & ~KXSD9_FS_MASK) | i;
e435bc19 88
d34dbee8 89 ret = spi_write(st->us, st->tx, 2);
e435bc19
JC
90error_ret:
91 mutex_unlock(&st->buf_lock);
d34dbee8 92 return ret;
e435bc19 93}
f3fb0011 94
d34dbee8 95static int kxsd9_read(struct iio_dev *indio_dev, u8 address)
e435bc19 96{
e435bc19 97 int ret;
ed0c012b 98 struct kxsd9_state *st = iio_priv(indio_dev);
e435bc19
JC
99 struct spi_transfer xfers[] = {
100 {
101 .bits_per_word = 8,
102 .len = 1,
e435bc19
JC
103 .delay_usecs = 200,
104 .tx_buf = st->tx,
105 }, {
106 .bits_per_word = 8,
107 .len = 2,
e435bc19
JC
108 .rx_buf = st->rx,
109 },
110 };
111
112 mutex_lock(&st->buf_lock);
d34dbee8 113 st->tx[0] = KXSD9_READ(address);
14543a00 114 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
0ee005c7
FZ
115 if (!ret)
116 ret = (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
117 mutex_unlock(&st->buf_lock);
118 return ret;
e435bc19
JC
119}
120
f3fb0011
JC
121static IIO_CONST_ATTR(accel_scale_available,
122 KXSD9_SCALE_2G " "
123 KXSD9_SCALE_4G " "
124 KXSD9_SCALE_6G " "
125 KXSD9_SCALE_8G);
e435bc19
JC
126
127static struct attribute *kxsd9_attributes[] = {
f3fb0011 128 &iio_const_attr_accel_scale_available.dev_attr.attr,
e435bc19
JC
129 NULL,
130};
131
d34dbee8
JC
132static 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
c8a9f805 140 if (mask == IIO_CHAN_INFO_SCALE) {
d34dbee8
JC
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
150static 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) {
31313fc6 158 case IIO_CHAN_INFO_RAW:
d34dbee8
JC
159 ret = kxsd9_read(indio_dev, chan->address);
160 if (ret < 0)
161 goto error_ret;
162 *val = ret;
7ac61a06 163 ret = IIO_VAL_INT;
d34dbee8 164 break;
c8a9f805 165 case IIO_CHAN_INFO_SCALE:
d34dbee8 166 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
0c1f91b9 167 if (ret < 0)
d34dbee8 168 goto error_ret;
307fe9dd 169 *val = 0;
d34dbee8
JC
170 *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
171 ret = IIO_VAL_INT_PLUS_MICRO;
172 break;
73327b4c 173 }
d34dbee8
JC
174
175error_ret:
176 return ret;
177};
178#define KXSD9_ACCEL_CHAN(axis) \
179 { \
180 .type = IIO_ACCEL, \
181 .modified = 1, \
182 .channel2 = IIO_MOD_##axis, \
2f6bb534
JC
183 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
184 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
d34dbee8
JC
185 .address = KXSD9_REG_##axis, \
186 }
187
f4e4b955 188static const struct iio_chan_spec kxsd9_channels[] = {
d34dbee8
JC
189 KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
190 {
6835cb6b 191 .type = IIO_VOLTAGE,
2f6bb534 192 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
d34dbee8
JC
193 .indexed = 1,
194 .address = KXSD9_REG_AUX,
195 }
196};
197
e435bc19
JC
198static const struct attribute_group kxsd9_attribute_group = {
199 .attrs = kxsd9_attributes,
200};
201
4ae1c61f 202static int kxsd9_power_up(struct kxsd9_state *st)
e435bc19 203{
d34dbee8
JC
204 int ret;
205
3fd47d44 206 st->tx[0] = 0x0d;
207 st->tx[1] = 0x40;
d34dbee8
JC
208 ret = spi_write(st->us, st->tx, 2);
209 if (ret)
210 return ret;
e435bc19 211
d34dbee8
JC
212 st->tx[0] = 0x0c;
213 st->tx[1] = 0x9b;
214 return spi_write(st->us, st->tx, 2);
e435bc19
JC
215};
216
6fe8135f 217static const struct iio_info kxsd9_info = {
d34dbee8
JC
218 .read_raw = &kxsd9_read_raw,
219 .write_raw = &kxsd9_write_raw,
6fe8135f
JC
220 .attrs = &kxsd9_attribute_group,
221 .driver_module = THIS_MODULE,
222};
223
4ae1c61f 224static int kxsd9_probe(struct spi_device *spi)
e435bc19 225{
ed0c012b 226 struct iio_dev *indio_dev;
e435bc19 227 struct kxsd9_state *st;
e435bc19 228
4ee30933
SK
229 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
230 if (!indio_dev)
231 return -ENOMEM;
232
ed0c012b
JC
233 st = iio_priv(indio_dev);
234 spi_set_drvdata(spi, indio_dev);
e435bc19
JC
235
236 st->us = spi;
237 mutex_init(&st->buf_lock);
d34dbee8
JC
238 indio_dev->channels = kxsd9_channels;
239 indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
240 indio_dev->name = spi_get_device_id(spi)->name;
ed0c012b
JC
241 indio_dev->dev.parent = &spi->dev;
242 indio_dev->info = &kxsd9_info;
243 indio_dev->modes = INDIO_DIRECT_MODE;
244
e435bc19
JC
245 spi->mode = SPI_MODE_0;
246 spi_setup(spi);
3fd47d44 247 kxsd9_power_up(st);
e435bc19 248
dcf5272c 249 return iio_device_register(indio_dev);
e435bc19
JC
250}
251
447d4f29 252static int kxsd9_remove(struct spi_device *spi)
e435bc19 253{
ed0c012b 254 iio_device_unregister(spi_get_drvdata(spi));
e435bc19
JC
255
256 return 0;
257}
258
d34dbee8 259static const struct spi_device_id kxsd9_id[] = {
55e4390c
LPC
260 {"kxsd9", 0},
261 { },
d34dbee8 262};
55e4390c 263MODULE_DEVICE_TABLE(spi, kxsd9_id);
d34dbee8 264
e435bc19
JC
265static struct spi_driver kxsd9_driver = {
266 .driver = {
267 .name = "kxsd9",
e435bc19
JC
268 },
269 .probe = kxsd9_probe,
e543acf0 270 .remove = kxsd9_remove,
d34dbee8 271 .id_table = kxsd9_id,
e435bc19 272};
ae6ae6fe 273module_spi_driver(kxsd9_driver);
e435bc19 274
0f8c9620 275MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
e435bc19
JC
276MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
277MODULE_LICENSE("GPL v2");