GNU Linux-libre 4.9.282-gnu1
[releases.git] / drivers / iio / accel / stk8ba50.c
1 /**
2  * Sensortek STK8BA50 3-Axis Accelerometer
3  *
4  * Copyright (c) 2015, 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  * STK8BA50 7-bit I2C address: 0x18.
11  */
12
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24
25 #define STK8BA50_REG_XOUT                       0x02
26 #define STK8BA50_REG_YOUT                       0x04
27 #define STK8BA50_REG_ZOUT                       0x06
28 #define STK8BA50_REG_RANGE                      0x0F
29 #define STK8BA50_REG_BWSEL                      0x10
30 #define STK8BA50_REG_POWMODE                    0x11
31 #define STK8BA50_REG_SWRST                      0x14
32 #define STK8BA50_REG_INTEN2                     0x17
33 #define STK8BA50_REG_INTMAP2                    0x1A
34
35 #define STK8BA50_MODE_NORMAL                    0
36 #define STK8BA50_MODE_SUSPEND                   1
37 #define STK8BA50_MODE_POWERBIT                  BIT(7)
38 #define STK8BA50_DATA_SHIFT                     6
39 #define STK8BA50_RESET_CMD                      0xB6
40 #define STK8BA50_SR_1792HZ_IDX                  7
41 #define STK8BA50_DREADY_INT_MASK                0x10
42 #define STK8BA50_DREADY_INT_MAP                 0x81
43 #define STK8BA50_ALL_CHANNEL_MASK               7
44 #define STK8BA50_ALL_CHANNEL_SIZE               6
45
46 #define STK8BA50_DRIVER_NAME                    "stk8ba50"
47 #define STK8BA50_IRQ_NAME                       "stk8ba50_event"
48
49 #define STK8BA50_SCALE_AVAIL                    "0.0384 0.0767 0.1534 0.3069"
50
51 /*
52  * The accelerometer has four measurement ranges:
53  * +/-2g; +/-4g; +/-8g; +/-16g
54  *
55  * Acceleration values are 10-bit, 2's complement.
56  * Scales are calculated as following:
57  *
58  * scale1 = (2 + 2) * 9.81 / (2^10 - 1)   = 0.0384
59  * scale2 = (4 + 4) * 9.81 / (2^10 - 1)   = 0.0767
60  * etc.
61  *
62  * Scales are stored in this format:
63  * { <register value>, <scale value> }
64  *
65  * Locally, the range is stored as a table index.
66  */
67 static const struct {
68         u8 reg_val;
69         u32 scale_val;
70 } stk8ba50_scale_table[] = {
71         {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
72 };
73
74 /* Sample rates are stored as { <register value>, <Hz value> } */
75 static const struct {
76         u8 reg_val;
77         u16 samp_freq;
78 } stk8ba50_samp_freq_table[] = {
79         {0x08, 14},  {0x09, 25},  {0x0A, 56},  {0x0B, 112},
80         {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
81 };
82
83 /* Used to map scan mask bits to their corresponding channel register. */
84 static const int stk8ba50_channel_table[] = {
85         STK8BA50_REG_XOUT,
86         STK8BA50_REG_YOUT,
87         STK8BA50_REG_ZOUT
88 };
89
90 struct stk8ba50_data {
91         struct i2c_client *client;
92         struct mutex lock;
93         int range;
94         u8 sample_rate_idx;
95         struct iio_trigger *dready_trig;
96         bool dready_trigger_on;
97         /* Ensure timestamp is naturally aligned */
98         struct {
99                 s16 chans[3];
100                 s64 timetamp __aligned(8);
101         } scan;
102 };
103
104 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) {                      \
105         .type = IIO_ACCEL,                                              \
106         .address = reg,                                                 \
107         .modified = 1,                                                  \
108         .channel2 = IIO_MOD_##axis,                                     \
109         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
110         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),           \
111                                     BIT(IIO_CHAN_INFO_SAMP_FREQ),       \
112         .scan_index = index,                                            \
113         .scan_type = {                                                  \
114                 .sign = 's',                                            \
115                 .realbits = 10,                                         \
116                 .storagebits = 16,                                      \
117                 .shift = STK8BA50_DATA_SHIFT,                           \
118                 .endianness = IIO_CPU,                                  \
119         },                                                              \
120 }
121
122 static const struct iio_chan_spec stk8ba50_channels[] = {
123         STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X),
124         STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y),
125         STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z),
126         IIO_CHAN_SOFT_TIMESTAMP(3),
127 };
128
129 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
130
131 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
132
133 static struct attribute *stk8ba50_attributes[] = {
134         &iio_const_attr_in_accel_scale_available.dev_attr.attr,
135         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
136         NULL,
137 };
138
139 static const struct attribute_group stk8ba50_attribute_group = {
140         .attrs = stk8ba50_attributes
141 };
142
143 static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg)
144 {
145         int ret;
146         struct i2c_client *client = data->client;
147
148         ret = i2c_smbus_read_word_data(client, reg);
149         if (ret < 0) {
150                 dev_err(&client->dev, "register read failed\n");
151                 return ret;
152         }
153
154         return ret;
155 }
156
157 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig,
158                                                bool state)
159 {
160         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
161         struct stk8ba50_data *data = iio_priv(indio_dev);
162         int ret;
163
164         if (state)
165                 ret = i2c_smbus_write_byte_data(data->client,
166                         STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
167         else
168                 ret = i2c_smbus_write_byte_data(data->client,
169                         STK8BA50_REG_INTEN2, 0x00);
170
171         if (ret < 0)
172                 dev_err(&data->client->dev, "failed to set trigger state\n");
173         else
174                 data->dready_trigger_on = state;
175
176         return ret;
177 }
178
179 static const struct iio_trigger_ops stk8ba50_trigger_ops = {
180         .set_trigger_state = stk8ba50_data_rdy_trigger_set_state,
181         .owner = THIS_MODULE,
182 };
183
184 static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode)
185 {
186         int ret;
187         u8 masked_reg;
188         struct i2c_client *client = data->client;
189
190         ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE);
191         if (ret < 0)
192                 goto exit_err;
193
194         if (mode)
195                 masked_reg = ret | STK8BA50_MODE_POWERBIT;
196         else
197                 masked_reg = ret & (~STK8BA50_MODE_POWERBIT);
198
199         ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE,
200                                         masked_reg);
201         if (ret < 0)
202                 goto exit_err;
203
204         return ret;
205
206 exit_err:
207         dev_err(&client->dev, "failed to change sensor mode\n");
208         return ret;
209 }
210
211 static int stk8ba50_read_raw(struct iio_dev *indio_dev,
212                              struct iio_chan_spec const *chan,
213                              int *val, int *val2, long mask)
214 {
215         struct stk8ba50_data *data = iio_priv(indio_dev);
216         int ret;
217
218         switch (mask) {
219         case IIO_CHAN_INFO_RAW:
220                 if (iio_buffer_enabled(indio_dev))
221                         return -EBUSY;
222                 mutex_lock(&data->lock);
223                 ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
224                 if (ret < 0) {
225                         mutex_unlock(&data->lock);
226                         return -EINVAL;
227                 }
228                 ret = stk8ba50_read_accel(data, chan->address);
229                 if (ret < 0) {
230                         stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
231                         mutex_unlock(&data->lock);
232                         return -EINVAL;
233                 }
234                 *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9);
235                 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
236                 mutex_unlock(&data->lock);
237                 return IIO_VAL_INT;
238         case IIO_CHAN_INFO_SCALE:
239                 *val = 0;
240                 *val2 = stk8ba50_scale_table[data->range].scale_val;
241                 return IIO_VAL_INT_PLUS_MICRO;
242         case IIO_CHAN_INFO_SAMP_FREQ:
243                 *val = stk8ba50_samp_freq_table
244                                 [data->sample_rate_idx].samp_freq;
245                 *val2 = 0;
246                 return IIO_VAL_INT;
247         }
248
249         return -EINVAL;
250 }
251
252 static int stk8ba50_write_raw(struct iio_dev *indio_dev,
253                               struct iio_chan_spec const *chan,
254                               int val, int val2, long mask)
255 {
256         int ret;
257         int i;
258         int index = -1;
259         struct stk8ba50_data *data = iio_priv(indio_dev);
260
261         switch (mask) {
262         case IIO_CHAN_INFO_SCALE:
263                 if (val != 0)
264                         return -EINVAL;
265
266                 for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
267                         if (val2 == stk8ba50_scale_table[i].scale_val) {
268                                 index = i;
269                                 break;
270                         }
271                 if (index < 0)
272                         return -EINVAL;
273
274                 ret = i2c_smbus_write_byte_data(data->client,
275                                 STK8BA50_REG_RANGE,
276                                 stk8ba50_scale_table[index].reg_val);
277                 if (ret < 0)
278                         dev_err(&data->client->dev,
279                                         "failed to set measurement range\n");
280                 else
281                         data->range = index;
282
283                 return ret;
284         case IIO_CHAN_INFO_SAMP_FREQ:
285                 for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
286                         if (val == stk8ba50_samp_freq_table[i].samp_freq) {
287                                 index = i;
288                                 break;
289                         }
290                 if (index < 0)
291                         return -EINVAL;
292
293                 ret = i2c_smbus_write_byte_data(data->client,
294                                 STK8BA50_REG_BWSEL,
295                                 stk8ba50_samp_freq_table[index].reg_val);
296                 if (ret < 0)
297                         dev_err(&data->client->dev,
298                                         "failed to set sampling rate\n");
299                 else
300                         data->sample_rate_idx = index;
301
302                 return ret;
303         }
304
305         return -EINVAL;
306 }
307
308 static const struct iio_info stk8ba50_info = {
309         .driver_module          = THIS_MODULE,
310         .read_raw               = stk8ba50_read_raw,
311         .write_raw              = stk8ba50_write_raw,
312         .attrs                  = &stk8ba50_attribute_group,
313 };
314
315 static irqreturn_t stk8ba50_trigger_handler(int irq, void *p)
316 {
317         struct iio_poll_func *pf = p;
318         struct iio_dev *indio_dev = pf->indio_dev;
319         struct stk8ba50_data *data = iio_priv(indio_dev);
320         int bit, ret, i = 0;
321
322         mutex_lock(&data->lock);
323         /*
324          * Do a bulk read if all channels are requested,
325          * from 0x02 (XOUT1) to 0x07 (ZOUT2)
326          */
327         if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) {
328                 ret = i2c_smbus_read_i2c_block_data(data->client,
329                                                     STK8BA50_REG_XOUT,
330                                                     STK8BA50_ALL_CHANNEL_SIZE,
331                                                     (u8 *)data->scan.chans);
332                 if (ret < STK8BA50_ALL_CHANNEL_SIZE) {
333                         dev_err(&data->client->dev, "register read failed\n");
334                         goto err;
335                 }
336         } else {
337                 for_each_set_bit(bit, indio_dev->active_scan_mask,
338                                  indio_dev->masklength) {
339                         ret = stk8ba50_read_accel(data,
340                                                   stk8ba50_channel_table[bit]);
341                         if (ret < 0)
342                                 goto err;
343
344                         data->scan.chans[i++] = ret;
345                 }
346         }
347         iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
348                                            pf->timestamp);
349 err:
350         mutex_unlock(&data->lock);
351         iio_trigger_notify_done(indio_dev->trig);
352
353         return IRQ_HANDLED;
354 }
355
356 static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private)
357 {
358         struct iio_dev *indio_dev = private;
359         struct stk8ba50_data *data = iio_priv(indio_dev);
360
361         if (data->dready_trigger_on)
362                 iio_trigger_poll(data->dready_trig);
363
364         return IRQ_HANDLED;
365 }
366
367 static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev)
368 {
369         struct stk8ba50_data *data = iio_priv(indio_dev);
370
371         return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
372 }
373
374 static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev)
375 {
376         struct stk8ba50_data *data = iio_priv(indio_dev);
377
378         return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
379 }
380
381 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = {
382         .preenable   = stk8ba50_buffer_preenable,
383         .postenable  = iio_triggered_buffer_postenable,
384         .predisable  = iio_triggered_buffer_predisable,
385         .postdisable = stk8ba50_buffer_postdisable,
386 };
387
388 static int stk8ba50_probe(struct i2c_client *client,
389                           const struct i2c_device_id *id)
390 {
391         int ret;
392         struct iio_dev *indio_dev;
393         struct stk8ba50_data *data;
394
395         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
396         if (!indio_dev) {
397                 dev_err(&client->dev, "iio allocation failed!\n");
398                 return -ENOMEM;
399         }
400
401         data = iio_priv(indio_dev);
402         data->client = client;
403         i2c_set_clientdata(client, indio_dev);
404         mutex_init(&data->lock);
405
406         indio_dev->dev.parent = &client->dev;
407         indio_dev->info = &stk8ba50_info;
408         indio_dev->name = STK8BA50_DRIVER_NAME;
409         indio_dev->modes = INDIO_DIRECT_MODE;
410         indio_dev->channels = stk8ba50_channels;
411         indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels);
412
413         /* Reset all registers on startup */
414         ret = i2c_smbus_write_byte_data(client,
415                         STK8BA50_REG_SWRST, STK8BA50_RESET_CMD);
416         if (ret < 0) {
417                 dev_err(&client->dev, "failed to reset sensor\n");
418                 goto err_power_off;
419         }
420
421         /* The default range is +/-2g */
422         data->range = 0;
423
424         /* The default sampling rate is 1792 Hz (maximum) */
425         data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
426
427         /* Set up interrupts */
428         ret = i2c_smbus_write_byte_data(client,
429                         STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
430         if (ret < 0) {
431                 dev_err(&client->dev, "failed to set up interrupts\n");
432                 goto err_power_off;
433         }
434         ret = i2c_smbus_write_byte_data(client,
435                         STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP);
436         if (ret < 0) {
437                 dev_err(&client->dev, "failed to set up interrupts\n");
438                 goto err_power_off;
439         }
440
441         if (client->irq > 0) {
442                 ret = devm_request_threaded_irq(&client->dev, client->irq,
443                                                 stk8ba50_data_rdy_trig_poll,
444                                                 NULL,
445                                                 IRQF_TRIGGER_RISING |
446                                                 IRQF_ONESHOT,
447                                                 STK8BA50_IRQ_NAME,
448                                                 indio_dev);
449                 if (ret < 0) {
450                         dev_err(&client->dev, "request irq %d failed\n",
451                                 client->irq);
452                         goto err_power_off;
453                 }
454
455                 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
456                                                            "%s-dev%d",
457                                                            indio_dev->name,
458                                                            indio_dev->id);
459                 if (!data->dready_trig) {
460                         ret = -ENOMEM;
461                         goto err_power_off;
462                 }
463
464                 data->dready_trig->dev.parent = &client->dev;
465                 data->dready_trig->ops = &stk8ba50_trigger_ops;
466                 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
467                 ret = iio_trigger_register(data->dready_trig);
468                 if (ret) {
469                         dev_err(&client->dev, "iio trigger register failed\n");
470                         goto err_power_off;
471                 }
472         }
473
474         ret = iio_triggered_buffer_setup(indio_dev,
475                                          iio_pollfunc_store_time,
476                                          stk8ba50_trigger_handler,
477                                          &stk8ba50_buffer_setup_ops);
478         if (ret < 0) {
479                 dev_err(&client->dev, "iio triggered buffer setup failed\n");
480                 goto err_trigger_unregister;
481         }
482
483         ret = iio_device_register(indio_dev);
484         if (ret < 0) {
485                 dev_err(&client->dev, "device_register failed\n");
486                 goto err_buffer_cleanup;
487         }
488
489         return ret;
490
491 err_buffer_cleanup:
492         iio_triggered_buffer_cleanup(indio_dev);
493 err_trigger_unregister:
494         if (data->dready_trig)
495                 iio_trigger_unregister(data->dready_trig);
496 err_power_off:
497         stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
498         return ret;
499 }
500
501 static int stk8ba50_remove(struct i2c_client *client)
502 {
503         struct iio_dev *indio_dev = i2c_get_clientdata(client);
504         struct stk8ba50_data *data = iio_priv(indio_dev);
505
506         iio_device_unregister(indio_dev);
507         iio_triggered_buffer_cleanup(indio_dev);
508
509         if (data->dready_trig)
510                 iio_trigger_unregister(data->dready_trig);
511
512         return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
513 }
514
515 #ifdef CONFIG_PM_SLEEP
516 static int stk8ba50_suspend(struct device *dev)
517 {
518         struct stk8ba50_data *data;
519
520         data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
521
522         return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
523 }
524
525 static int stk8ba50_resume(struct device *dev)
526 {
527         struct stk8ba50_data *data;
528
529         data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
530
531         return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
532 }
533
534 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume);
535
536 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
537 #else
538 #define STK8BA50_PM_OPS NULL
539 #endif
540
541 static const struct i2c_device_id stk8ba50_i2c_id[] = {
542         {"stk8ba50", 0},
543         {}
544 };
545 MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id);
546
547 static const struct acpi_device_id stk8ba50_acpi_id[] = {
548         {"STK8BA50", 0},
549         {}
550 };
551
552 MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id);
553
554 static struct i2c_driver stk8ba50_driver = {
555         .driver = {
556                 .name = "stk8ba50",
557                 .pm = STK8BA50_PM_OPS,
558                 .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id),
559         },
560         .probe =            stk8ba50_probe,
561         .remove =           stk8ba50_remove,
562         .id_table =         stk8ba50_i2c_id,
563 };
564
565 module_i2c_driver(stk8ba50_driver);
566
567 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
568 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
569 MODULE_LICENSE("GPL v2");