1 // SPDX-License-Identifier: GPL-2.0-only
2 /**
3 * BMA220 Digital triaxial acceleration sensor driver
4 *
5 * Copyright (c) 2016,2020 Intel Corporation.
6 */
7
8 #include <linux/bits.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/spi/spi.h>
13
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19
20 #define BMA220_REG_ID 0x00
21 #define BMA220_REG_ACCEL_X 0x02
22 #define BMA220_REG_ACCEL_Y 0x03
23 #define BMA220_REG_ACCEL_Z 0x04
24 #define BMA220_REG_RANGE 0x11
25 #define BMA220_REG_SUSPEND 0x18
26
27 #define BMA220_CHIP_ID 0xDD
28 #define BMA220_READ_MASK BIT(7)
29 #define BMA220_RANGE_MASK GENMASK(1, 0)
30 #define BMA220_DATA_SHIFT 2
31 #define BMA220_SUSPEND_SLEEP 0xFF
32 #define BMA220_SUSPEND_WAKE 0x00
33
34 #define BMA220_DEVICE_NAME "bma220"
35
36 #define BMA220_ACCEL_CHANNEL(index, reg, axis) { \
37 .type = IIO_ACCEL, \
38 .address = reg, \
39 .modified = 1, \
40 .channel2 = IIO_MOD_##axis, \
41 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
42 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
43 .scan_index = index, \
44 .scan_type = { \
45 .sign = 's', \
46 .realbits = 6, \
47 .storagebits = 8, \
48 .shift = BMA220_DATA_SHIFT, \
49 .endianness = IIO_CPU, \
50 }, \
51 }
52
53 enum bma220_axis {
54 AXIS_X,
55 AXIS_Y,
56 AXIS_Z,
57 };
58
59 static const int bma220_scale_table[][2] = {
60 {0, 623000}, {1, 248000}, {2, 491000}, {4, 983000},
61 };
62
63 struct bma220_data {
64 struct spi_device *spi_device;
65 struct mutex lock;
66 struct {
67 s8 chans[3];
68 /* Ensure timestamp is naturally aligned. */
69 s64 timestamp __aligned(8);
70 } scan;
71 u8 tx_buf[2] ____cacheline_aligned;
72 };
73
74 static const struct iio_chan_spec bma220_channels[] = {
75 BMA220_ACCEL_CHANNEL(0, BMA220_REG_ACCEL_X, X),
76 BMA220_ACCEL_CHANNEL(1, BMA220_REG_ACCEL_Y, Y),
77 BMA220_ACCEL_CHANNEL(2, BMA220_REG_ACCEL_Z, Z),
78 IIO_CHAN_SOFT_TIMESTAMP(3),
79 };
80
bma220_read_reg(struct spi_device * spi,u8 reg)81 static inline int bma220_read_reg(struct spi_device *spi, u8 reg)
82 {
83 return spi_w8r8(spi, reg | BMA220_READ_MASK);
84 }
85
86 static const unsigned long bma220_accel_scan_masks[] = {
87 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
88 0
89 };
90
bma220_trigger_handler(int irq,void * p)91 static irqreturn_t bma220_trigger_handler(int irq, void *p)
92 {
93 int ret;
94 struct iio_poll_func *pf = p;
95 struct iio_dev *indio_dev = pf->indio_dev;
96 struct bma220_data *data = iio_priv(indio_dev);
97 struct spi_device *spi = data->spi_device;
98
99 mutex_lock(&data->lock);
100 data->tx_buf[0] = BMA220_REG_ACCEL_X | BMA220_READ_MASK;
101 ret = spi_write_then_read(spi, data->tx_buf, 1, &data->scan.chans,
102 ARRAY_SIZE(bma220_channels) - 1);
103 if (ret < 0)
104 goto err;
105
106 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
107 pf->timestamp);
108 err:
109 mutex_unlock(&data->lock);
110 iio_trigger_notify_done(indio_dev->trig);
111
112 return IRQ_HANDLED;
113 }
114
bma220_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)115 static int bma220_read_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan,
117 int *val, int *val2, long mask)
118 {
119 int ret;
120 u8 range_idx;
121 struct bma220_data *data = iio_priv(indio_dev);
122
123 switch (mask) {
124 case IIO_CHAN_INFO_RAW:
125 ret = bma220_read_reg(data->spi_device, chan->address);
126 if (ret < 0)
127 return -EINVAL;
128 *val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5);
129 return IIO_VAL_INT;
130 case IIO_CHAN_INFO_SCALE:
131 ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE);
132 if (ret < 0)
133 return ret;
134 range_idx = ret & BMA220_RANGE_MASK;
135 *val = bma220_scale_table[range_idx][0];
136 *val2 = bma220_scale_table[range_idx][1];
137 return IIO_VAL_INT_PLUS_MICRO;
138 }
139
140 return -EINVAL;
141 }
142
bma220_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)143 static int bma220_write_raw(struct iio_dev *indio_dev,
144 struct iio_chan_spec const *chan,
145 int val, int val2, long mask)
146 {
147 int i;
148 int ret;
149 int index = -1;
150 struct bma220_data *data = iio_priv(indio_dev);
151
152 switch (mask) {
153 case IIO_CHAN_INFO_SCALE:
154 for (i = 0; i < ARRAY_SIZE(bma220_scale_table); i++)
155 if (val == bma220_scale_table[i][0] &&
156 val2 == bma220_scale_table[i][1]) {
157 index = i;
158 break;
159 }
160 if (index < 0)
161 return -EINVAL;
162
163 mutex_lock(&data->lock);
164 data->tx_buf[0] = BMA220_REG_RANGE;
165 data->tx_buf[1] = index;
166 ret = spi_write(data->spi_device, data->tx_buf,
167 sizeof(data->tx_buf));
168 if (ret < 0)
169 dev_err(&data->spi_device->dev,
170 "failed to set measurement range\n");
171 mutex_unlock(&data->lock);
172
173 return 0;
174 }
175
176 return -EINVAL;
177 }
178
bma220_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)179 static int bma220_read_avail(struct iio_dev *indio_dev,
180 struct iio_chan_spec const *chan,
181 const int **vals, int *type, int *length,
182 long mask)
183 {
184 switch (mask) {
185 case IIO_CHAN_INFO_SCALE:
186 *vals = (int *)bma220_scale_table;
187 *type = IIO_VAL_INT_PLUS_MICRO;
188 *length = ARRAY_SIZE(bma220_scale_table) * 2;
189 return IIO_AVAIL_LIST;
190 default:
191 return -EINVAL;
192 }
193 }
194
195 static const struct iio_info bma220_info = {
196 .read_raw = bma220_read_raw,
197 .write_raw = bma220_write_raw,
198 .read_avail = bma220_read_avail,
199 };
200
bma220_init(struct spi_device * spi)201 static int bma220_init(struct spi_device *spi)
202 {
203 int ret;
204
205 ret = bma220_read_reg(spi, BMA220_REG_ID);
206 if (ret != BMA220_CHIP_ID)
207 return -ENODEV;
208
209 /* Make sure the chip is powered on */
210 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
211 if (ret == BMA220_SUSPEND_WAKE)
212 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
213 if (ret < 0)
214 return ret;
215 if (ret == BMA220_SUSPEND_WAKE)
216 return -EBUSY;
217
218 return 0;
219 }
220
bma220_deinit(struct spi_device * spi)221 static int bma220_deinit(struct spi_device *spi)
222 {
223 int ret;
224
225 /* Make sure the chip is powered off */
226 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
227 if (ret == BMA220_SUSPEND_SLEEP)
228 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
229 if (ret < 0)
230 return ret;
231 if (ret == BMA220_SUSPEND_SLEEP)
232 return -EBUSY;
233
234 return 0;
235 }
236
bma220_probe(struct spi_device * spi)237 static int bma220_probe(struct spi_device *spi)
238 {
239 int ret;
240 struct iio_dev *indio_dev;
241 struct bma220_data *data;
242
243 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
244 if (!indio_dev) {
245 dev_err(&spi->dev, "iio allocation failed!\n");
246 return -ENOMEM;
247 }
248
249 data = iio_priv(indio_dev);
250 data->spi_device = spi;
251 spi_set_drvdata(spi, indio_dev);
252 mutex_init(&data->lock);
253
254 indio_dev->info = &bma220_info;
255 indio_dev->name = BMA220_DEVICE_NAME;
256 indio_dev->modes = INDIO_DIRECT_MODE;
257 indio_dev->channels = bma220_channels;
258 indio_dev->num_channels = ARRAY_SIZE(bma220_channels);
259 indio_dev->available_scan_masks = bma220_accel_scan_masks;
260
261 ret = bma220_init(data->spi_device);
262 if (ret)
263 return ret;
264
265 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
266 bma220_trigger_handler, NULL);
267 if (ret < 0) {
268 dev_err(&spi->dev, "iio triggered buffer setup failed\n");
269 goto err_suspend;
270 }
271
272 ret = iio_device_register(indio_dev);
273 if (ret < 0) {
274 dev_err(&spi->dev, "iio_device_register failed\n");
275 iio_triggered_buffer_cleanup(indio_dev);
276 goto err_suspend;
277 }
278
279 return 0;
280
281 err_suspend:
282 return bma220_deinit(spi);
283 }
284
bma220_remove(struct spi_device * spi)285 static int bma220_remove(struct spi_device *spi)
286 {
287 struct iio_dev *indio_dev = spi_get_drvdata(spi);
288
289 iio_device_unregister(indio_dev);
290 iio_triggered_buffer_cleanup(indio_dev);
291
292 return bma220_deinit(spi);
293 }
294
bma220_suspend(struct device * dev)295 static __maybe_unused int bma220_suspend(struct device *dev)
296 {
297 struct bma220_data *data = iio_priv(dev_get_drvdata(dev));
298
299 /* The chip can be suspended/woken up by a simple register read. */
300 return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
301 }
302
bma220_resume(struct device * dev)303 static __maybe_unused int bma220_resume(struct device *dev)
304 {
305 struct bma220_data *data = iio_priv(dev_get_drvdata(dev));
306
307 return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
308 }
309 static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume);
310
311 static const struct spi_device_id bma220_spi_id[] = {
312 {"bma220", 0},
313 {}
314 };
315
316 static const struct acpi_device_id bma220_acpi_id[] = {
317 {"BMA0220", 0},
318 {}
319 };
320 MODULE_DEVICE_TABLE(spi, bma220_spi_id);
321
322 static struct spi_driver bma220_driver = {
323 .driver = {
324 .name = "bma220_spi",
325 .pm = &bma220_pm_ops,
326 .acpi_match_table = bma220_acpi_id,
327 },
328 .probe = bma220_probe,
329 .remove = bma220_remove,
330 .id_table = bma220_spi_id,
331 };
332 module_spi_driver(bma220_driver);
333
334 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
335 MODULE_DESCRIPTION("BMA220 acceleration sensor driver");
336 MODULE_LICENSE("GPL v2");
337