blob: 709c13259f14e8cab35faee7237794be38c24744 [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
Jonathan Camerone435bc12009-08-18 18:06:23 +010046/**
47 * struct kxsd9_state - device related storage
48 * @buf_lock: protect the rx and tx buffers.
Jonathan Camerone435bc12009-08-18 18:06:23 +010049 * @us: spi device
50 * @rx: single rx buffer storage
51 * @tx: single tx buffer storage
52 **/
53struct kxsd9_state {
54 struct mutex buf_lock;
Jonathan Camerone435bc12009-08-18 18:06:23 +010055 struct spi_device *us;
Jonathan Cameroned0c0122011-06-27 13:17:56 +010056 u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned;
57 u8 tx[KXSD9_STATE_TX_SIZE];
Jonathan Camerone435bc12009-08-18 18:06:23 +010058};
59
Jonathan Camerond34dbee2011-08-12 17:47:52 +010060#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 */
66static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
67
68static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
Jonathan Camerone435bc12009-08-18 18:06:23 +010069{
Jonathan Camerond34dbee2011-08-12 17:47:52 +010070 int ret, i;
Jonathan Cameroned0c0122011-06-27 13:17:56 +010071 struct kxsd9_state *st = iio_priv(indio_dev);
Jonathan Camerond34dbee2011-08-12 17:47:52 +010072 bool foundit = false;
Jonathan Camerone435bc12009-08-18 18:06:23 +010073
Jonathan Camerond34dbee2011-08-12 17:47:52 +010074 for (i = 0; i < 4; i++)
75 if (micro == kxsd9_micro_scales[i]) {
76 foundit = true;
77 break;
78 }
79 if (!foundit)
Jonathan Camerone435bc12009-08-18 18:06:23 +010080 return -EINVAL;
Jonathan Cameronf3fb0012010-05-04 14:42:58 +010081
Jonathan Camerone435bc12009-08-18 18:06:23 +010082 mutex_lock(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +010083 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
Jonathan Camerone435bc12009-08-18 18:06:23 +010084 if (ret)
85 goto error_ret;
86 st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
Jonathan Camerond34dbee2011-08-12 17:47:52 +010087 st->tx[1] = (ret & ~KXSD9_FS_MASK) | i;
Jonathan Camerone435bc12009-08-18 18:06:23 +010088
Jonathan Camerond34dbee2011-08-12 17:47:52 +010089 ret = spi_write(st->us, st->tx, 2);
Jonathan Camerone435bc12009-08-18 18:06:23 +010090error_ret:
91 mutex_unlock(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +010092 return ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +010093}
Jonathan Cameronf3fb0012010-05-04 14:42:58 +010094
Jonathan Camerond34dbee2011-08-12 17:47:52 +010095static int kxsd9_read(struct iio_dev *indio_dev, u8 address)
Jonathan Camerone435bc12009-08-18 18:06:23 +010096{
Jonathan Camerone435bc12009-08-18 18:06:23 +010097 int ret;
Jonathan Cameroned0c0122011-06-27 13:17:56 +010098 struct kxsd9_state *st = iio_priv(indio_dev);
Jonathan Camerone435bc12009-08-18 18:06:23 +010099 struct spi_transfer xfers[] = {
100 {
101 .bits_per_word = 8,
102 .len = 1,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100103 .delay_usecs = 200,
104 .tx_buf = st->tx,
105 }, {
106 .bits_per_word = 8,
107 .len = 2,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100108 .rx_buf = st->rx,
109 },
110 };
111
112 mutex_lock(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100113 st->tx[0] = KXSD9_READ(address);
Lars-Peter Clausen14543a02013-01-09 17:31:00 +0000114 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
Jonathan Camerone435bc12009-08-18 18:06:23 +0100115 if (ret)
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100116 return ret;
117 return (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100118}
119
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100120static IIO_CONST_ATTR(accel_scale_available,
121 KXSD9_SCALE_2G " "
122 KXSD9_SCALE_4G " "
123 KXSD9_SCALE_6G " "
124 KXSD9_SCALE_8G);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100125
126static struct attribute *kxsd9_attributes[] = {
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100127 &iio_const_attr_accel_scale_available.dev_attr.attr,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100128 NULL,
129};
130
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100131static int kxsd9_write_raw(struct iio_dev *indio_dev,
132 struct iio_chan_spec const *chan,
133 int val,
134 int val2,
135 long mask)
136{
137 int ret = -EINVAL;
138
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100139 if (mask == IIO_CHAN_INFO_SCALE) {
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100140 /* Check no integer component */
141 if (val)
142 return -EINVAL;
143 ret = kxsd9_write_scale(indio_dev, val2);
144 }
145
146 return ret;
147}
148
149static int kxsd9_read_raw(struct iio_dev *indio_dev,
150 struct iio_chan_spec const *chan,
151 int *val, int *val2, long mask)
152{
153 int ret = -EINVAL;
154 struct kxsd9_state *st = iio_priv(indio_dev);
155
156 switch (mask) {
Jonathan Cameron31313fc2012-04-15 17:41:17 +0100157 case IIO_CHAN_INFO_RAW:
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100158 ret = kxsd9_read(indio_dev, chan->address);
159 if (ret < 0)
160 goto error_ret;
161 *val = ret;
162 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100163 case IIO_CHAN_INFO_SCALE:
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100164 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C));
165 if (ret)
166 goto error_ret;
167 *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK];
168 ret = IIO_VAL_INT_PLUS_MICRO;
169 break;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100170 }
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100171
172error_ret:
173 return ret;
174};
175#define KXSD9_ACCEL_CHAN(axis) \
176 { \
177 .type = IIO_ACCEL, \
178 .modified = 1, \
179 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron2f6bb532013-02-27 19:03:36 +0000180 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
181 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100182 .address = KXSD9_REG_##axis, \
183 }
184
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100185static const struct iio_chan_spec kxsd9_channels[] = {
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100186 KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
187 {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100188 .type = IIO_VOLTAGE,
Jonathan Cameron2f6bb532013-02-27 19:03:36 +0000189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100190 .indexed = 1,
191 .address = KXSD9_REG_AUX,
192 }
193};
194
Jonathan Camerone435bc12009-08-18 18:06:23 +0100195static const struct attribute_group kxsd9_attribute_group = {
196 .attrs = kxsd9_attributes,
197};
198
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500199static int kxsd9_power_up(struct kxsd9_state *st)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100200{
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100201 int ret;
202
anish kumar3fd47d42011-06-27 13:08:00 +0100203 st->tx[0] = 0x0d;
204 st->tx[1] = 0x40;
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100205 ret = spi_write(st->us, st->tx, 2);
206 if (ret)
207 return ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100208
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100209 st->tx[0] = 0x0c;
210 st->tx[1] = 0x9b;
211 return spi_write(st->us, st->tx, 2);
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
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500221static int kxsd9_probe(struct spi_device *spi)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100222{
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100223 struct iio_dev *indio_dev;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100224 struct kxsd9_state *st;
Jonathan Camerond526e512013-01-06 15:10:00 +0000225 int ret;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100226
Sachin Kamat4ee30932013-07-23 07:47:00 +0100227 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
228 if (!indio_dev)
229 return -ENOMEM;
230
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100231 st = iio_priv(indio_dev);
232 spi_set_drvdata(spi, indio_dev);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100233
234 st->us = spi;
235 mutex_init(&st->buf_lock);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100236 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 Cameroned0c0122011-06-27 13:17:56 +0100239 indio_dev->dev.parent = &spi->dev;
240 indio_dev->info = &kxsd9_info;
241 indio_dev->modes = INDIO_DIRECT_MODE;
242
Jonathan Camerone435bc12009-08-18 18:06:23 +0100243 spi->mode = SPI_MODE_0;
244 spi_setup(spi);
anish kumar3fd47d42011-06-27 13:08:00 +0100245 kxsd9_power_up(st);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100246
Jonathan Camerond526e512013-01-06 15:10:00 +0000247 ret = iio_device_register(indio_dev);
248 if (ret)
Sachin Kamat4ee30932013-07-23 07:47:00 +0100249 return ret;
Jonathan Camerond526e512013-01-06 15:10:00 +0000250
Jonathan Camerone435bc12009-08-18 18:06:23 +0100251 return 0;
Jonathan Camerone435bc12009-08-18 18:06:23 +0100252}
253
Bill Pemberton447d4f22012-11-19 13:26:37 -0500254static int kxsd9_remove(struct spi_device *spi)
Jonathan Camerone435bc12009-08-18 18:06:23 +0100255{
Jonathan Cameroned0c0122011-06-27 13:17:56 +0100256 iio_device_unregister(spi_get_drvdata(spi));
Jonathan Camerone435bc12009-08-18 18:06:23 +0100257
258 return 0;
259}
260
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100261static const struct spi_device_id kxsd9_id[] = {
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100262 {"kxsd9", 0},
263 { },
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100264};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100265MODULE_DEVICE_TABLE(spi, kxsd9_id);
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100266
Jonathan Camerone435bc12009-08-18 18:06:23 +0100267static struct spi_driver kxsd9_driver = {
268 .driver = {
269 .name = "kxsd9",
270 .owner = THIS_MODULE,
271 },
272 .probe = kxsd9_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500273 .remove = kxsd9_remove,
Jonathan Camerond34dbee2011-08-12 17:47:52 +0100274 .id_table = kxsd9_id,
Jonathan Camerone435bc12009-08-18 18:06:23 +0100275};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100276module_spi_driver(kxsd9_driver);
Jonathan Camerone435bc12009-08-18 18:06:23 +0100277
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100278MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone435bc12009-08-18 18:06:23 +0100279MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
280MODULE_LICENSE("GPL v2");