1 /*
2 * lis3l02dq.c support STMicroelectronics LISD02DQ
3 * 3d 2g Linear Accelerometers via SPI
4 *
5 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
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 * Settings:
12 * 16 bit left justified mode used.
13 */
14
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/gpio.h>
18 #include <linux/of_gpio.h>
19 #include <linux/mutex.h>
20 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/spi/spi.h>
23 #include <linux/slab.h>
24 #include <linux/sysfs.h>
25 #include <linux/module.h>
26
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/buffer.h>
31
32 #include "lis3l02dq.h"
33
34 /* At the moment the spi framework doesn't allow global setting of cs_change.
35 * It's in the likely to be added comment at the top of spi.h.
36 * This means that use cannot be made of spi_write etc.
37 */
38 /* direct copy of the irq_default_primary_handler */
39 #ifndef CONFIG_IIO_BUFFER
lis3l02dq_nobuffer(int irq,void * private)40 static irqreturn_t lis3l02dq_nobuffer(int irq, void *private)
41 {
42 return IRQ_WAKE_THREAD;
43 }
44 #endif
45
46 /**
47 * lis3l02dq_spi_read_reg_8() - read single byte from a single register
48 * @indio_dev: iio_dev for this actual device
49 * @reg_address: the address of the register to be read
50 * @val: pass back the resulting value
51 **/
lis3l02dq_spi_read_reg_8(struct iio_dev * indio_dev,u8 reg_address,u8 * val)52 int lis3l02dq_spi_read_reg_8(struct iio_dev *indio_dev,
53 u8 reg_address, u8 *val)
54 {
55 struct lis3l02dq_state *st = iio_priv(indio_dev);
56 int ret;
57 struct spi_transfer xfer = {
58 .tx_buf = st->tx,
59 .rx_buf = st->rx,
60 .bits_per_word = 8,
61 .len = 2,
62 };
63
64 mutex_lock(&st->buf_lock);
65 st->tx[0] = LIS3L02DQ_READ_REG(reg_address);
66 st->tx[1] = 0;
67
68 ret = spi_sync_transfer(st->us, &xfer, 1);
69 *val = st->rx[1];
70 mutex_unlock(&st->buf_lock);
71
72 return ret;
73 }
74
75 /**
76 * lis3l02dq_spi_write_reg_8() - write single byte to a register
77 * @indio_dev: iio_dev for this device
78 * @reg_address: the address of the register to be written
79 * @val: the value to write
80 **/
lis3l02dq_spi_write_reg_8(struct iio_dev * indio_dev,u8 reg_address,u8 val)81 int lis3l02dq_spi_write_reg_8(struct iio_dev *indio_dev,
82 u8 reg_address,
83 u8 val)
84 {
85 int ret;
86 struct lis3l02dq_state *st = iio_priv(indio_dev);
87
88 mutex_lock(&st->buf_lock);
89 st->tx[0] = LIS3L02DQ_WRITE_REG(reg_address);
90 st->tx[1] = val;
91 ret = spi_write(st->us, st->tx, 2);
92 mutex_unlock(&st->buf_lock);
93
94 return ret;
95 }
96
97 /**
98 * lisl302dq_spi_write_reg_s16() - write 2 bytes to a pair of registers
99 * @indio_dev: iio_dev for this device
100 * @lower_reg_address: the address of the lower of the two registers.
101 * Second register is assumed to have address one greater.
102 * @value: value to be written
103 **/
lis3l02dq_spi_write_reg_s16(struct iio_dev * indio_dev,u8 lower_reg_address,s16 value)104 static int lis3l02dq_spi_write_reg_s16(struct iio_dev *indio_dev,
105 u8 lower_reg_address,
106 s16 value)
107 {
108 int ret;
109 struct lis3l02dq_state *st = iio_priv(indio_dev);
110 struct spi_transfer xfers[] = { {
111 .tx_buf = st->tx,
112 .bits_per_word = 8,
113 .len = 2,
114 .cs_change = 1,
115 }, {
116 .tx_buf = st->tx + 2,
117 .bits_per_word = 8,
118 .len = 2,
119 },
120 };
121
122 mutex_lock(&st->buf_lock);
123 st->tx[0] = LIS3L02DQ_WRITE_REG(lower_reg_address);
124 st->tx[1] = value & 0xFF;
125 st->tx[2] = LIS3L02DQ_WRITE_REG(lower_reg_address + 1);
126 st->tx[3] = (value >> 8) & 0xFF;
127
128 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
129 mutex_unlock(&st->buf_lock);
130
131 return ret;
132 }
133
lis3l02dq_read_reg_s16(struct iio_dev * indio_dev,u8 lower_reg_address,int * val)134 static int lis3l02dq_read_reg_s16(struct iio_dev *indio_dev,
135 u8 lower_reg_address,
136 int *val)
137 {
138 struct lis3l02dq_state *st = iio_priv(indio_dev);
139 int ret;
140 s16 tempval;
141 struct spi_transfer xfers[] = { {
142 .tx_buf = st->tx,
143 .rx_buf = st->rx,
144 .bits_per_word = 8,
145 .len = 2,
146 .cs_change = 1,
147 }, {
148 .tx_buf = st->tx + 2,
149 .rx_buf = st->rx + 2,
150 .bits_per_word = 8,
151 .len = 2,
152 },
153 };
154
155 mutex_lock(&st->buf_lock);
156 st->tx[0] = LIS3L02DQ_READ_REG(lower_reg_address);
157 st->tx[1] = 0;
158 st->tx[2] = LIS3L02DQ_READ_REG(lower_reg_address + 1);
159 st->tx[3] = 0;
160
161 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
162 if (ret) {
163 dev_err(&st->us->dev, "problem when reading 16 bit register");
164 goto error_ret;
165 }
166 tempval = (s16)(st->rx[1]) | ((s16)(st->rx[3]) << 8);
167
168 *val = tempval;
169 error_ret:
170 mutex_unlock(&st->buf_lock);
171 return ret;
172 }
173
174 enum lis3l02dq_rm_ind {
175 LIS3L02DQ_ACCEL,
176 LIS3L02DQ_GAIN,
177 LIS3L02DQ_BIAS,
178 };
179
180 static u8 lis3l02dq_axis_map[3][3] = {
181 [LIS3L02DQ_ACCEL] = { LIS3L02DQ_REG_OUT_X_L_ADDR,
182 LIS3L02DQ_REG_OUT_Y_L_ADDR,
183 LIS3L02DQ_REG_OUT_Z_L_ADDR },
184 [LIS3L02DQ_GAIN] = { LIS3L02DQ_REG_GAIN_X_ADDR,
185 LIS3L02DQ_REG_GAIN_Y_ADDR,
186 LIS3L02DQ_REG_GAIN_Z_ADDR },
187 [LIS3L02DQ_BIAS] = { LIS3L02DQ_REG_OFFSET_X_ADDR,
188 LIS3L02DQ_REG_OFFSET_Y_ADDR,
189 LIS3L02DQ_REG_OFFSET_Z_ADDR }
190 };
191
lis3l02dq_read_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)192 static int lis3l02dq_read_thresh(struct iio_dev *indio_dev,
193 const struct iio_chan_spec *chan,
194 enum iio_event_type type,
195 enum iio_event_direction dir,
196 enum iio_event_info info,
197 int *val, int *val2)
198 {
199 int ret;
200
201 ret = lis3l02dq_read_reg_s16(indio_dev, LIS3L02DQ_REG_THS_L_ADDR, val);
202 if (ret)
203 return ret;
204 return IIO_VAL_INT;
205 }
206
lis3l02dq_write_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)207 static int lis3l02dq_write_thresh(struct iio_dev *indio_dev,
208 const struct iio_chan_spec *chan,
209 enum iio_event_type type,
210 enum iio_event_direction dir,
211 enum iio_event_info info,
212 int val, int val2)
213 {
214 u16 value = val;
215
216 return lis3l02dq_spi_write_reg_s16(indio_dev,
217 LIS3L02DQ_REG_THS_L_ADDR,
218 value);
219 }
220
lis3l02dq_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)221 static int lis3l02dq_write_raw(struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan,
223 int val,
224 int val2,
225 long mask)
226 {
227 int ret = -EINVAL, reg;
228 u8 uval;
229 s8 sval;
230
231 switch (mask) {
232 case IIO_CHAN_INFO_CALIBBIAS:
233 if (val > 255 || val < -256)
234 return -EINVAL;
235 sval = val;
236 reg = lis3l02dq_axis_map[LIS3L02DQ_BIAS][chan->address];
237 ret = lis3l02dq_spi_write_reg_8(indio_dev, reg, sval);
238 break;
239 case IIO_CHAN_INFO_CALIBSCALE:
240 if (val & ~0xFF)
241 return -EINVAL;
242 uval = val;
243 reg = lis3l02dq_axis_map[LIS3L02DQ_GAIN][chan->address];
244 ret = lis3l02dq_spi_write_reg_8(indio_dev, reg, uval);
245 break;
246 }
247 return ret;
248 }
249
lis3l02dq_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)250 static int lis3l02dq_read_raw(struct iio_dev *indio_dev,
251 struct iio_chan_spec const *chan,
252 int *val,
253 int *val2,
254 long mask)
255 {
256 u8 utemp;
257 s8 stemp;
258 ssize_t ret = 0;
259 u8 reg;
260
261 switch (mask) {
262 case IIO_CHAN_INFO_RAW:
263 /* Take the iio_dev status lock */
264 mutex_lock(&indio_dev->mlock);
265 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
266 ret = -EBUSY;
267 } else {
268 reg = lis3l02dq_axis_map
269 [LIS3L02DQ_ACCEL][chan->address];
270 ret = lis3l02dq_read_reg_s16(indio_dev, reg, val);
271 }
272 mutex_unlock(&indio_dev->mlock);
273 if (ret < 0)
274 goto error_ret;
275 return IIO_VAL_INT;
276 case IIO_CHAN_INFO_SCALE:
277 *val = 0;
278 *val2 = 9580;
279 return IIO_VAL_INT_PLUS_MICRO;
280 case IIO_CHAN_INFO_CALIBSCALE:
281 reg = lis3l02dq_axis_map[LIS3L02DQ_GAIN][chan->address];
282 ret = lis3l02dq_spi_read_reg_8(indio_dev, reg, &utemp);
283 if (ret)
284 goto error_ret;
285 /* to match with what previous code does */
286 *val = utemp;
287 return IIO_VAL_INT;
288
289 case IIO_CHAN_INFO_CALIBBIAS:
290 reg = lis3l02dq_axis_map[LIS3L02DQ_BIAS][chan->address];
291 ret = lis3l02dq_spi_read_reg_8(indio_dev, reg, (u8 *)&stemp);
292 /* to match with what previous code does */
293 *val = stemp;
294 return IIO_VAL_INT;
295 }
296 error_ret:
297 return ret;
298 }
299
lis3l02dq_read_frequency(struct device * dev,struct device_attribute * attr,char * buf)300 static ssize_t lis3l02dq_read_frequency(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
303 {
304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
305 int ret, len = 0;
306 s8 t;
307
308 ret = lis3l02dq_spi_read_reg_8(indio_dev,
309 LIS3L02DQ_REG_CTRL_1_ADDR,
310 (u8 *)&t);
311 if (ret)
312 return ret;
313 t &= LIS3L02DQ_DEC_MASK;
314 switch (t) {
315 case LIS3L02DQ_REG_CTRL_1_DF_128:
316 len = sprintf(buf, "280\n");
317 break;
318 case LIS3L02DQ_REG_CTRL_1_DF_64:
319 len = sprintf(buf, "560\n");
320 break;
321 case LIS3L02DQ_REG_CTRL_1_DF_32:
322 len = sprintf(buf, "1120\n");
323 break;
324 case LIS3L02DQ_REG_CTRL_1_DF_8:
325 len = sprintf(buf, "4480\n");
326 break;
327 }
328 return len;
329 }
330
lis3l02dq_write_frequency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)331 static ssize_t lis3l02dq_write_frequency(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf,
334 size_t len)
335 {
336 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
337 unsigned long val;
338 int ret;
339 u8 t;
340
341 ret = kstrtoul(buf, 10, &val);
342 if (ret)
343 return ret;
344
345 mutex_lock(&indio_dev->mlock);
346 ret = lis3l02dq_spi_read_reg_8(indio_dev,
347 LIS3L02DQ_REG_CTRL_1_ADDR,
348 &t);
349 if (ret)
350 goto error_ret_mutex;
351 /* Wipe the bits clean */
352 t &= ~LIS3L02DQ_DEC_MASK;
353 switch (val) {
354 case 280:
355 t |= LIS3L02DQ_REG_CTRL_1_DF_128;
356 break;
357 case 560:
358 t |= LIS3L02DQ_REG_CTRL_1_DF_64;
359 break;
360 case 1120:
361 t |= LIS3L02DQ_REG_CTRL_1_DF_32;
362 break;
363 case 4480:
364 t |= LIS3L02DQ_REG_CTRL_1_DF_8;
365 break;
366 default:
367 ret = -EINVAL;
368 goto error_ret_mutex;
369 }
370
371 ret = lis3l02dq_spi_write_reg_8(indio_dev,
372 LIS3L02DQ_REG_CTRL_1_ADDR,
373 t);
374
375 error_ret_mutex:
376 mutex_unlock(&indio_dev->mlock);
377
378 return ret ? ret : len;
379 }
380
lis3l02dq_initial_setup(struct iio_dev * indio_dev)381 static int lis3l02dq_initial_setup(struct iio_dev *indio_dev)
382 {
383 struct lis3l02dq_state *st = iio_priv(indio_dev);
384 int ret;
385 u8 val, valtest;
386
387 st->us->mode = SPI_MODE_3;
388
389 spi_setup(st->us);
390
391 val = LIS3L02DQ_DEFAULT_CTRL1;
392 /* Write suitable defaults to ctrl1 */
393 ret = lis3l02dq_spi_write_reg_8(indio_dev,
394 LIS3L02DQ_REG_CTRL_1_ADDR,
395 val);
396 if (ret) {
397 dev_err(&st->us->dev, "problem with setup control register 1");
398 goto err_ret;
399 }
400 /* Repeat as sometimes doesn't work first time? */
401 ret = lis3l02dq_spi_write_reg_8(indio_dev,
402 LIS3L02DQ_REG_CTRL_1_ADDR,
403 val);
404 if (ret) {
405 dev_err(&st->us->dev, "problem with setup control register 1");
406 goto err_ret;
407 }
408
409 /* Read back to check this has worked acts as loose test of correct
410 * chip */
411 ret = lis3l02dq_spi_read_reg_8(indio_dev,
412 LIS3L02DQ_REG_CTRL_1_ADDR,
413 &valtest);
414 if (ret || (valtest != val)) {
415 dev_err(&indio_dev->dev,
416 "device not playing ball %d %d\n", valtest, val);
417 ret = -EINVAL;
418 goto err_ret;
419 }
420
421 val = LIS3L02DQ_DEFAULT_CTRL2;
422 ret = lis3l02dq_spi_write_reg_8(indio_dev,
423 LIS3L02DQ_REG_CTRL_2_ADDR,
424 val);
425 if (ret) {
426 dev_err(&st->us->dev, "problem with setup control register 2");
427 goto err_ret;
428 }
429
430 val = LIS3L02DQ_REG_WAKE_UP_CFG_LATCH_SRC;
431 ret = lis3l02dq_spi_write_reg_8(indio_dev,
432 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
433 val);
434 if (ret)
435 dev_err(&st->us->dev, "problem with interrupt cfg register");
436 err_ret:
437
438 return ret;
439 }
440
441 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
442 lis3l02dq_read_frequency,
443 lis3l02dq_write_frequency);
444
445 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("280 560 1120 4480");
446
lis3l02dq_event_handler(int irq,void * private)447 static irqreturn_t lis3l02dq_event_handler(int irq, void *private)
448 {
449 struct iio_dev *indio_dev = private;
450 u8 t;
451
452 s64 timestamp = iio_get_time_ns();
453
454 lis3l02dq_spi_read_reg_8(indio_dev,
455 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
456 &t);
457
458 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_HIGH)
459 iio_push_event(indio_dev,
460 IIO_MOD_EVENT_CODE(IIO_ACCEL,
461 0,
462 IIO_MOD_Z,
463 IIO_EV_TYPE_THRESH,
464 IIO_EV_DIR_RISING),
465 timestamp);
466
467 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_LOW)
468 iio_push_event(indio_dev,
469 IIO_MOD_EVENT_CODE(IIO_ACCEL,
470 0,
471 IIO_MOD_Z,
472 IIO_EV_TYPE_THRESH,
473 IIO_EV_DIR_FALLING),
474 timestamp);
475
476 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_HIGH)
477 iio_push_event(indio_dev,
478 IIO_MOD_EVENT_CODE(IIO_ACCEL,
479 0,
480 IIO_MOD_Y,
481 IIO_EV_TYPE_THRESH,
482 IIO_EV_DIR_RISING),
483 timestamp);
484
485 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_LOW)
486 iio_push_event(indio_dev,
487 IIO_MOD_EVENT_CODE(IIO_ACCEL,
488 0,
489 IIO_MOD_Y,
490 IIO_EV_TYPE_THRESH,
491 IIO_EV_DIR_FALLING),
492 timestamp);
493
494 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_HIGH)
495 iio_push_event(indio_dev,
496 IIO_MOD_EVENT_CODE(IIO_ACCEL,
497 0,
498 IIO_MOD_X,
499 IIO_EV_TYPE_THRESH,
500 IIO_EV_DIR_RISING),
501 timestamp);
502
503 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_LOW)
504 iio_push_event(indio_dev,
505 IIO_MOD_EVENT_CODE(IIO_ACCEL,
506 0,
507 IIO_MOD_X,
508 IIO_EV_TYPE_THRESH,
509 IIO_EV_DIR_FALLING),
510 timestamp);
511
512 /* Ack and allow for new interrupts */
513 lis3l02dq_spi_read_reg_8(indio_dev,
514 LIS3L02DQ_REG_WAKE_UP_ACK_ADDR,
515 &t);
516
517 return IRQ_HANDLED;
518 }
519
520 static const struct iio_event_spec lis3l02dq_event[] = {
521 {
522 .type = IIO_EV_TYPE_THRESH,
523 .dir = IIO_EV_DIR_RISING,
524 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
525 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
526 }, {
527 .type = IIO_EV_TYPE_THRESH,
528 .dir = IIO_EV_DIR_FALLING,
529 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
530 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
531 }
532 };
533
534 #define LIS3L02DQ_CHAN(index, mod) \
535 { \
536 .type = IIO_ACCEL, \
537 .modified = 1, \
538 .channel2 = mod, \
539 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
540 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
541 BIT(IIO_CHAN_INFO_CALIBBIAS), \
542 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
543 .address = index, \
544 .scan_index = index, \
545 .scan_type = { \
546 .sign = 's', \
547 .realbits = 12, \
548 .storagebits = 16, \
549 }, \
550 .event_spec = lis3l02dq_event, \
551 .num_event_specs = ARRAY_SIZE(lis3l02dq_event), \
552 }
553
554 static const struct iio_chan_spec lis3l02dq_channels[] = {
555 LIS3L02DQ_CHAN(0, IIO_MOD_X),
556 LIS3L02DQ_CHAN(1, IIO_MOD_Y),
557 LIS3L02DQ_CHAN(2, IIO_MOD_Z),
558 IIO_CHAN_SOFT_TIMESTAMP(3)
559 };
560
lis3l02dq_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)561 static int lis3l02dq_read_event_config(struct iio_dev *indio_dev,
562 const struct iio_chan_spec *chan,
563 enum iio_event_type type,
564 enum iio_event_direction dir)
565 {
566 u8 val;
567 int ret;
568 u8 mask = (1 << (chan->channel2*2 + (dir == IIO_EV_DIR_RISING)));
569
570 ret = lis3l02dq_spi_read_reg_8(indio_dev,
571 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
572 &val);
573 if (ret < 0)
574 return ret;
575
576 return !!(val & mask);
577 }
578
lis3l02dq_disable_all_events(struct iio_dev * indio_dev)579 int lis3l02dq_disable_all_events(struct iio_dev *indio_dev)
580 {
581 int ret;
582 u8 control, val;
583
584 ret = lis3l02dq_spi_read_reg_8(indio_dev,
585 LIS3L02DQ_REG_CTRL_2_ADDR,
586 &control);
587
588 control &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT;
589 ret = lis3l02dq_spi_write_reg_8(indio_dev,
590 LIS3L02DQ_REG_CTRL_2_ADDR,
591 control);
592 if (ret)
593 goto error_ret;
594 /* Also for consistency clear the mask */
595 ret = lis3l02dq_spi_read_reg_8(indio_dev,
596 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
597 &val);
598 if (ret)
599 goto error_ret;
600 val &= ~0x3f;
601
602 ret = lis3l02dq_spi_write_reg_8(indio_dev,
603 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
604 val);
605 if (ret)
606 goto error_ret;
607
608 ret = control;
609 error_ret:
610 return ret;
611 }
612
lis3l02dq_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)613 static int lis3l02dq_write_event_config(struct iio_dev *indio_dev,
614 const struct iio_chan_spec *chan,
615 enum iio_event_type type,
616 enum iio_event_direction dir,
617 int state)
618 {
619 int ret = 0;
620 u8 val, control;
621 u8 currentlyset;
622 bool changed = false;
623 u8 mask = (1 << (chan->channel2*2 + (dir == IIO_EV_DIR_RISING)));
624
625 mutex_lock(&indio_dev->mlock);
626 /* read current control */
627 ret = lis3l02dq_spi_read_reg_8(indio_dev,
628 LIS3L02DQ_REG_CTRL_2_ADDR,
629 &control);
630 if (ret)
631 goto error_ret;
632 ret = lis3l02dq_spi_read_reg_8(indio_dev,
633 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
634 &val);
635 if (ret < 0)
636 goto error_ret;
637 currentlyset = val & mask;
638
639 if (!currentlyset && state) {
640 changed = true;
641 val |= mask;
642 } else if (currentlyset && !state) {
643 changed = true;
644 val &= ~mask;
645 }
646
647 if (changed) {
648 ret = lis3l02dq_spi_write_reg_8(indio_dev,
649 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
650 val);
651 if (ret)
652 goto error_ret;
653 control = val & 0x3f ?
654 (control | LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT) :
655 (control & ~LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT);
656 ret = lis3l02dq_spi_write_reg_8(indio_dev,
657 LIS3L02DQ_REG_CTRL_2_ADDR,
658 control);
659 if (ret)
660 goto error_ret;
661 }
662
663 error_ret:
664 mutex_unlock(&indio_dev->mlock);
665 return ret;
666 }
667
668 static struct attribute *lis3l02dq_attributes[] = {
669 &iio_dev_attr_sampling_frequency.dev_attr.attr,
670 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
671 NULL
672 };
673
674 static const struct attribute_group lis3l02dq_attribute_group = {
675 .attrs = lis3l02dq_attributes,
676 };
677
678 static const struct iio_info lis3l02dq_info = {
679 .read_raw = &lis3l02dq_read_raw,
680 .write_raw = &lis3l02dq_write_raw,
681 .read_event_value = &lis3l02dq_read_thresh,
682 .write_event_value = &lis3l02dq_write_thresh,
683 .write_event_config = &lis3l02dq_write_event_config,
684 .read_event_config = &lis3l02dq_read_event_config,
685 .driver_module = THIS_MODULE,
686 .attrs = &lis3l02dq_attribute_group,
687 };
688
lis3l02dq_probe(struct spi_device * spi)689 static int lis3l02dq_probe(struct spi_device *spi)
690 {
691 int ret;
692 struct lis3l02dq_state *st;
693 struct iio_dev *indio_dev;
694
695 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
696 if (!indio_dev)
697 return -ENOMEM;
698 st = iio_priv(indio_dev);
699 /* this is only used for removal purposes */
700 spi_set_drvdata(spi, indio_dev);
701
702 st->us = spi;
703 st->gpio = of_get_gpio(spi->dev.of_node, 0);
704 mutex_init(&st->buf_lock);
705 indio_dev->name = spi->dev.driver->name;
706 indio_dev->dev.parent = &spi->dev;
707 indio_dev->info = &lis3l02dq_info;
708 indio_dev->channels = lis3l02dq_channels;
709 indio_dev->num_channels = ARRAY_SIZE(lis3l02dq_channels);
710
711 indio_dev->modes = INDIO_DIRECT_MODE;
712
713 ret = lis3l02dq_configure_buffer(indio_dev);
714 if (ret)
715 return ret;
716
717 if (spi->irq) {
718 ret = request_threaded_irq(st->us->irq,
719 &lis3l02dq_th,
720 &lis3l02dq_event_handler,
721 IRQF_TRIGGER_RISING,
722 "lis3l02dq",
723 indio_dev);
724 if (ret)
725 goto error_unreg_buffer_funcs;
726
727 ret = lis3l02dq_probe_trigger(indio_dev);
728 if (ret)
729 goto error_free_interrupt;
730 }
731
732 /* Get the device into a sane initial state */
733 ret = lis3l02dq_initial_setup(indio_dev);
734 if (ret)
735 goto error_remove_trigger;
736
737 ret = iio_device_register(indio_dev);
738 if (ret)
739 goto error_remove_trigger;
740
741 return 0;
742
743 error_remove_trigger:
744 if (spi->irq)
745 lis3l02dq_remove_trigger(indio_dev);
746 error_free_interrupt:
747 if (spi->irq)
748 free_irq(st->us->irq, indio_dev);
749 error_unreg_buffer_funcs:
750 lis3l02dq_unconfigure_buffer(indio_dev);
751 return ret;
752 }
753
754 /* Power down the device */
lis3l02dq_stop_device(struct iio_dev * indio_dev)755 static int lis3l02dq_stop_device(struct iio_dev *indio_dev)
756 {
757 int ret;
758 struct lis3l02dq_state *st = iio_priv(indio_dev);
759 u8 val = 0;
760
761 mutex_lock(&indio_dev->mlock);
762 ret = lis3l02dq_spi_write_reg_8(indio_dev,
763 LIS3L02DQ_REG_CTRL_1_ADDR,
764 val);
765 if (ret) {
766 dev_err(&st->us->dev, "problem with turning device off: ctrl1");
767 goto err_ret;
768 }
769
770 ret = lis3l02dq_spi_write_reg_8(indio_dev,
771 LIS3L02DQ_REG_CTRL_2_ADDR,
772 val);
773 if (ret)
774 dev_err(&st->us->dev, "problem with turning device off: ctrl2");
775 err_ret:
776 mutex_unlock(&indio_dev->mlock);
777 return ret;
778 }
779
780 /* fixme, confirm ordering in this function */
lis3l02dq_remove(struct spi_device * spi)781 static int lis3l02dq_remove(struct spi_device *spi)
782 {
783 struct iio_dev *indio_dev = spi_get_drvdata(spi);
784 struct lis3l02dq_state *st = iio_priv(indio_dev);
785
786 iio_device_unregister(indio_dev);
787
788 lis3l02dq_disable_all_events(indio_dev);
789 lis3l02dq_stop_device(indio_dev);
790
791 if (spi->irq)
792 free_irq(st->us->irq, indio_dev);
793
794 lis3l02dq_remove_trigger(indio_dev);
795 lis3l02dq_unconfigure_buffer(indio_dev);
796
797 return 0;
798 }
799
800 static struct spi_driver lis3l02dq_driver = {
801 .driver = {
802 .name = "lis3l02dq",
803 .owner = THIS_MODULE,
804 },
805 .probe = lis3l02dq_probe,
806 .remove = lis3l02dq_remove,
807 };
808 module_spi_driver(lis3l02dq_driver);
809
810 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
811 MODULE_DESCRIPTION("ST LIS3L02DQ Accelerometer SPI driver");
812 MODULE_LICENSE("GPL v2");
813 MODULE_ALIAS("spi:lis3l02dq");
814