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