2 * AD7280A Lithium Ion Battery Monitoring System
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
24 #define AD7280A_CELL_VOLTAGE_1 0x0 /* D11 to D0, Read only */
25 #define AD7280A_CELL_VOLTAGE_2 0x1 /* D11 to D0, Read only */
26 #define AD7280A_CELL_VOLTAGE_3 0x2 /* D11 to D0, Read only */
27 #define AD7280A_CELL_VOLTAGE_4 0x3 /* D11 to D0, Read only */
28 #define AD7280A_CELL_VOLTAGE_5 0x4 /* D11 to D0, Read only */
29 #define AD7280A_CELL_VOLTAGE_6 0x5 /* D11 to D0, Read only */
30 #define AD7280A_AUX_ADC_1 0x6 /* D11 to D0, Read only */
31 #define AD7280A_AUX_ADC_2 0x7 /* D11 to D0, Read only */
32 #define AD7280A_AUX_ADC_3 0x8 /* D11 to D0, Read only */
33 #define AD7280A_AUX_ADC_4 0x9 /* D11 to D0, Read only */
34 #define AD7280A_AUX_ADC_5 0xA /* D11 to D0, Read only */
35 #define AD7280A_AUX_ADC_6 0xB /* D11 to D0, Read only */
36 #define AD7280A_SELF_TEST 0xC /* D11 to D0, Read only */
37 #define AD7280A_CONTROL_HB 0xD /* D15 to D8, Read/write */
38 #define AD7280A_CONTROL_LB 0xE /* D7 to D0, Read/write */
39 #define AD7280A_CELL_OVERVOLTAGE 0xF /* D7 to D0, Read/write */
40 #define AD7280A_CELL_UNDERVOLTAGE 0x10 /* D7 to D0, Read/write */
41 #define AD7280A_AUX_ADC_OVERVOLTAGE 0x11 /* D7 to D0, Read/write */
42 #define AD7280A_AUX_ADC_UNDERVOLTAGE 0x12 /* D7 to D0, Read/write */
43 #define AD7280A_ALERT 0x13 /* D7 to D0, Read/write */
44 #define AD7280A_CELL_BALANCE 0x14 /* D7 to D0, Read/write */
45 #define AD7280A_CB1_TIMER 0x15 /* D7 to D0, Read/write */
46 #define AD7280A_CB2_TIMER 0x16 /* D7 to D0, Read/write */
47 #define AD7280A_CB3_TIMER 0x17 /* D7 to D0, Read/write */
48 #define AD7280A_CB4_TIMER 0x18 /* D7 to D0, Read/write */
49 #define AD7280A_CB5_TIMER 0x19 /* D7 to D0, Read/write */
50 #define AD7280A_CB6_TIMER 0x1A /* D7 to D0, Read/write */
51 #define AD7280A_PD_TIMER 0x1B /* D7 to D0, Read/write */
52 #define AD7280A_READ 0x1C /* D7 to D0, Read/write */
53 #define AD7280A_CNVST_CONTROL 0x1D /* D7 to D0, Read/write */
56 #define AD7280A_CTRL_HB_CONV_INPUT_ALL (0 << 6)
57 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_4 (1 << 6)
58 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL (2 << 6)
59 #define AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST (3 << 6)
60 #define AD7280A_CTRL_HB_CONV_RES_READ_ALL (0 << 4)
61 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL_AUX1_3_4 (1 << 4)
62 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL (2 << 4)
63 #define AD7280A_CTRL_HB_CONV_RES_READ_NO (3 << 4)
64 #define AD7280A_CTRL_HB_CONV_START_CNVST (0 << 3)
65 #define AD7280A_CTRL_HB_CONV_START_CS (1 << 3)
66 #define AD7280A_CTRL_HB_CONV_AVG_DIS (0 << 1)
67 #define AD7280A_CTRL_HB_CONV_AVG_2 (1 << 1)
68 #define AD7280A_CTRL_HB_CONV_AVG_4 (2 << 1)
69 #define AD7280A_CTRL_HB_CONV_AVG_8 (3 << 1)
70 #define AD7280A_CTRL_HB_CONV_AVG(x) ((x) << 1)
71 #define AD7280A_CTRL_HB_PWRDN_SW (1 << 0)
73 #define AD7280A_CTRL_LB_SWRST (1 << 7)
74 #define AD7280A_CTRL_LB_ACQ_TIME_400ns (0 << 5)
75 #define AD7280A_CTRL_LB_ACQ_TIME_800ns (1 << 5)
76 #define AD7280A_CTRL_LB_ACQ_TIME_1200ns (2 << 5)
77 #define AD7280A_CTRL_LB_ACQ_TIME_1600ns (3 << 5)
78 #define AD7280A_CTRL_LB_ACQ_TIME(x) ((x) << 5)
79 #define AD7280A_CTRL_LB_MUST_SET (1 << 4)
80 #define AD7280A_CTRL_LB_THERMISTOR_EN (1 << 3)
81 #define AD7280A_CTRL_LB_LOCK_DEV_ADDR (1 << 2)
82 #define AD7280A_CTRL_LB_INC_DEV_ADDR (1 << 1)
83 #define AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN (1 << 0)
85 #define AD7280A_ALERT_GEN_STATIC_HIGH (1 << 6)
86 #define AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN (3 << 6)
88 #define AD7280A_ALL_CELLS (0xAD << 16)
90 #define AD7280A_MAX_SPI_CLK_Hz 700000 /* < 1MHz */
91 #define AD7280A_MAX_CHAIN 8
92 #define AD7280A_CELLS_PER_DEV 6
93 #define AD7280A_BITS 12
94 #define AD7280A_NUM_CH (AD7280A_AUX_ADC_6 - \
95 AD7280A_CELL_VOLTAGE_1 + 1)
97 #define AD7280A_DEVADDR_MASTER 0
98 #define AD7280A_DEVADDR_ALL 0x1F
99 /* 5-bit device address is sent LSB first */
100 #define AD7280A_DEVADDR(addr) (((addr & 0x1) << 4) | ((addr & 0x2) << 3) | \
101 (addr & 0x4) | ((addr & 0x8) >> 3) | \
102 ((addr & 0x10) >> 4))
104 /* During a read a valid write is mandatory.
105 * So writing to the highest available address (Address 0x1F)
106 * and setting the address all parts bit to 0 is recommended
107 * So the TXVAL is AD7280A_DEVADDR_ALL + CRC
109 #define AD7280A_READ_TXVAL 0xF800030A
114 * P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 => 0x2F
117 #define POLYNOM_ORDER 8
118 #define HIGHBIT 1 << (POLYNOM_ORDER - 1);
120 struct ad7280_state
{
121 struct spi_device
*spi
;
122 struct iio_chan_spec
*channels
;
123 struct iio_dev_attr
*iio_attr
;
126 int readback_delay_us
;
127 unsigned char crc_tab
[256];
128 unsigned char ctrl_hb
;
129 unsigned char ctrl_lb
;
130 unsigned char cell_threshhigh
;
131 unsigned char cell_threshlow
;
132 unsigned char aux_threshhigh
;
133 unsigned char aux_threshlow
;
134 unsigned char cb_mask
[AD7280A_MAX_CHAIN
];
137 static void ad7280_crc8_build_table(unsigned char *crc_tab
)
139 unsigned char bit
, crc
;
142 for (cnt
= 0; cnt
< 256; cnt
++) {
144 for (i
= 0; i
< 8; i
++) {
154 static unsigned char ad7280_calc_crc8(unsigned char *crc_tab
, unsigned val
)
158 crc
= crc_tab
[val
>> 16 & 0xFF];
159 crc
= crc_tab
[crc
^ (val
>> 8 & 0xFF)];
161 return crc
^ (val
& 0xFF);
164 static int ad7280_check_crc(struct ad7280_state
*st
, unsigned val
)
166 unsigned char crc
= ad7280_calc_crc8(st
->crc_tab
, val
>> 10);
168 if (crc
!= ((val
>> 2) & 0xFF))
174 /* After initiating a conversion sequence we need to wait until the
175 * conversion is done. The delay is typically in the range of 15..30 us
176 * however depending an the number of devices in the daisy chain and the
177 * number of averages taken, conversion delays and acquisition time options
178 * it may take up to 250us, in this case we better sleep instead of busy
182 static void ad7280_delay(struct ad7280_state
*st
)
184 if (st
->readback_delay_us
< 50)
185 udelay(st
->readback_delay_us
);
190 static int __ad7280_read32(struct spi_device
*spi
, unsigned *val
)
192 unsigned rx_buf
, tx_buf
= cpu_to_be32(AD7280A_READ_TXVAL
);
195 struct spi_transfer t
= {
200 struct spi_message m
;
202 spi_message_init(&m
);
203 spi_message_add_tail(&t
, &m
);
205 ret
= spi_sync(spi
, &m
);
209 *val
= be32_to_cpu(rx_buf
);
214 static int ad7280_write(struct ad7280_state
*st
, unsigned devaddr
,
215 unsigned addr
, bool all
, unsigned val
)
217 unsigned reg
= (devaddr
<< 27 | addr
<< 21 |
218 (val
& 0xFF) << 13 | all
<< 12);
220 reg
|= ad7280_calc_crc8(st
->crc_tab
, reg
>> 11) << 3 | 0x2;
221 reg
= cpu_to_be32(reg
);
223 return spi_write(st
->spi
, ®
, 4);
226 static int ad7280_read(struct ad7280_state
*st
, unsigned devaddr
,
232 /* turns off the read operation on all parts */
233 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_HB
, 1,
234 AD7280A_CTRL_HB_CONV_INPUT_ALL
|
235 AD7280A_CTRL_HB_CONV_RES_READ_NO
|
240 /* turns on the read operation on the addressed part */
241 ret
= ad7280_write(st
, devaddr
, AD7280A_CONTROL_HB
, 0,
242 AD7280A_CTRL_HB_CONV_INPUT_ALL
|
243 AD7280A_CTRL_HB_CONV_RES_READ_ALL
|
248 /* Set register address on the part to be read from */
249 ret
= ad7280_write(st
, devaddr
, AD7280A_READ
, 0, addr
<< 2);
253 __ad7280_read32(st
->spi
, &tmp
);
255 if (ad7280_check_crc(st
, tmp
))
258 if (((tmp
>> 27) != devaddr
) || (((tmp
>> 21) & 0x3F) != addr
))
261 return (tmp
>> 13) & 0xFF;
264 static int ad7280_read_channel(struct ad7280_state
*st
, unsigned devaddr
,
270 ret
= ad7280_write(st
, devaddr
, AD7280A_READ
, 0, addr
<< 2);
274 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_HB
, 1,
275 AD7280A_CTRL_HB_CONV_INPUT_ALL
|
276 AD7280A_CTRL_HB_CONV_RES_READ_NO
|
281 ret
= ad7280_write(st
, devaddr
, AD7280A_CONTROL_HB
, 0,
282 AD7280A_CTRL_HB_CONV_INPUT_ALL
|
283 AD7280A_CTRL_HB_CONV_RES_READ_ALL
|
284 AD7280A_CTRL_HB_CONV_START_CS
|
291 __ad7280_read32(st
->spi
, &tmp
);
293 if (ad7280_check_crc(st
, tmp
))
296 if (((tmp
>> 27) != devaddr
) || (((tmp
>> 23) & 0xF) != addr
))
299 return (tmp
>> 11) & 0xFFF;
302 static int ad7280_read_all_channels(struct ad7280_state
*st
, unsigned cnt
,
306 unsigned tmp
, sum
= 0;
308 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_READ
, 1,
309 AD7280A_CELL_VOLTAGE_1
<< 2);
313 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_HB
, 1,
314 AD7280A_CTRL_HB_CONV_INPUT_ALL
|
315 AD7280A_CTRL_HB_CONV_RES_READ_ALL
|
316 AD7280A_CTRL_HB_CONV_START_CS
|
323 for (i
= 0; i
< cnt
; i
++) {
324 __ad7280_read32(st
->spi
, &tmp
);
326 if (ad7280_check_crc(st
, tmp
))
331 /* only sum cell voltages */
332 if (((tmp
>> 23) & 0xF) <= AD7280A_CELL_VOLTAGE_6
)
333 sum
+= ((tmp
>> 11) & 0xFFF);
339 static int ad7280_chain_setup(struct ad7280_state
*st
)
344 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_LB
, 1,
345 AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN
|
346 AD7280A_CTRL_LB_LOCK_DEV_ADDR
|
347 AD7280A_CTRL_LB_MUST_SET
|
348 AD7280A_CTRL_LB_SWRST
|
353 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_LB
, 1,
354 AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN
|
355 AD7280A_CTRL_LB_LOCK_DEV_ADDR
|
356 AD7280A_CTRL_LB_MUST_SET
|
361 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_READ
, 1,
362 AD7280A_CONTROL_LB
<< 2);
366 for (n
= 0; n
<= AD7280A_MAX_CHAIN
; n
++) {
367 __ad7280_read32(st
->spi
, &val
);
371 if (ad7280_check_crc(st
, val
))
374 if (n
!= AD7280A_DEVADDR(val
>> 27))
381 static ssize_t
ad7280_show_balance_sw(struct device
*dev
,
382 struct device_attribute
*attr
,
385 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
386 struct ad7280_state
*st
= iio_priv(dev_info
);
387 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
389 return sprintf(buf
, "%d\n",
390 !!(st
->cb_mask
[this_attr
->address
>> 8] &
391 (1 << ((this_attr
->address
& 0xFF) + 2))));
394 static ssize_t
ad7280_store_balance_sw(struct device
*dev
,
395 struct device_attribute
*attr
,
399 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
400 struct ad7280_state
*st
= iio_priv(dev_info
);
401 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
404 unsigned devaddr
, ch
;
406 ret
= strtobool(buf
, &readin
);
410 devaddr
= this_attr
->address
>> 8;
411 ch
= this_attr
->address
& 0xFF;
413 mutex_lock(&dev_info
->mlock
);
415 st
->cb_mask
[devaddr
] |= 1 << (ch
+ 2);
417 st
->cb_mask
[devaddr
] &= ~(1 << (ch
+ 2));
419 ret
= ad7280_write(st
, devaddr
, AD7280A_CELL_BALANCE
,
420 0, st
->cb_mask
[devaddr
]);
421 mutex_unlock(&dev_info
->mlock
);
423 return ret
? ret
: len
;
426 static ssize_t
ad7280_show_balance_timer(struct device
*dev
,
427 struct device_attribute
*attr
,
430 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
431 struct ad7280_state
*st
= iio_priv(dev_info
);
432 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
436 mutex_lock(&dev_info
->mlock
);
437 ret
= ad7280_read(st
, this_attr
->address
>> 8,
438 this_attr
->address
& 0xFF);
439 mutex_unlock(&dev_info
->mlock
);
444 msecs
= (ret
>> 3) * 71500;
446 return sprintf(buf
, "%d\n", msecs
);
449 static ssize_t
ad7280_store_balance_timer(struct device
*dev
,
450 struct device_attribute
*attr
,
454 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
455 struct ad7280_state
*st
= iio_priv(dev_info
);
456 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
460 ret
= strict_strtoul(buf
, 10, &val
);
469 mutex_lock(&dev_info
->mlock
);
470 ret
= ad7280_write(st
, this_attr
->address
>> 8,
471 this_attr
->address
& 0xFF,
472 0, (val
& 0x1F) << 3);
473 mutex_unlock(&dev_info
->mlock
);
475 return ret
? ret
: len
;
478 static struct attribute
*ad7280_attributes
[AD7280A_MAX_CHAIN
*
479 AD7280A_CELLS_PER_DEV
* 2 + 1];
481 static struct attribute_group ad7280_attrs_group
= {
482 .attrs
= ad7280_attributes
,
485 static int ad7280_channel_init(struct ad7280_state
*st
)
489 st
->channels
= kzalloc(sizeof(*st
->channels
) *
490 ((st
->slave_num
+ 1) * 12 + 2), GFP_KERNEL
);
491 if (st
->channels
== NULL
)
494 for (dev
= 0, cnt
= 0; dev
<= st
->slave_num
; dev
++)
495 for (ch
= AD7280A_CELL_VOLTAGE_1
; ch
<= AD7280A_AUX_ADC_6
; ch
++,
497 if (ch
< AD7280A_AUX_ADC_1
) {
498 st
->channels
[cnt
].type
= IIO_IN_DIFF
;
499 st
->channels
[cnt
].channel
= (dev
* 6) + ch
;
500 st
->channels
[cnt
].channel2
=
501 st
->channels
[cnt
].channel
+ 1;
503 st
->channels
[cnt
].type
= IIO_TEMP
;
504 st
->channels
[cnt
].channel
= (dev
* 6) + ch
- 6;
506 st
->channels
[cnt
].indexed
= 1;
507 st
->channels
[cnt
].info_mask
=
508 (1 << IIO_CHAN_INFO_SCALE_SHARED
);
509 st
->channels
[cnt
].address
=
510 AD7280A_DEVADDR(dev
) << 8 | ch
;
511 st
->channels
[cnt
].scan_index
= cnt
;
512 st
->channels
[cnt
].scan_type
.sign
= 'u';
513 st
->channels
[cnt
].scan_type
.realbits
= 12;
514 st
->channels
[cnt
].scan_type
.storagebits
= 32;
515 st
->channels
[cnt
].scan_type
.shift
= 0;
518 st
->channels
[cnt
].type
= IIO_IN_DIFF
;
519 st
->channels
[cnt
].channel
= 0;
520 st
->channels
[cnt
].channel2
= dev
* 6;
521 st
->channels
[cnt
].address
= AD7280A_ALL_CELLS
;
522 st
->channels
[cnt
].indexed
= 1;
523 st
->channels
[cnt
].info_mask
= (1 << IIO_CHAN_INFO_SCALE_SHARED
);
524 st
->channels
[cnt
].scan_index
= cnt
;
525 st
->channels
[cnt
].scan_type
.sign
= 'u';
526 st
->channels
[cnt
].scan_type
.realbits
= 32;
527 st
->channels
[cnt
].scan_type
.storagebits
= 32;
528 st
->channels
[cnt
].scan_type
.shift
= 0;
530 st
->channels
[cnt
].type
= IIO_TIMESTAMP
;
531 st
->channels
[cnt
].channel
= -1;
532 st
->channels
[cnt
].scan_index
= cnt
;
533 st
->channels
[cnt
].scan_type
.sign
= 's';
534 st
->channels
[cnt
].scan_type
.realbits
= 64;
535 st
->channels
[cnt
].scan_type
.storagebits
= 64;
536 st
->channels
[cnt
].scan_type
.shift
= 0;
541 static int ad7280_attr_init(struct ad7280_state
*st
)
545 st
->iio_attr
= kzalloc(sizeof(*st
->iio_attr
) * (st
->slave_num
+ 1) *
546 AD7280A_CELLS_PER_DEV
* 2, GFP_KERNEL
);
547 if (st
->iio_attr
== NULL
)
550 for (dev
= 0, cnt
= 0; dev
<= st
->slave_num
; dev
++)
551 for (ch
= AD7280A_CELL_VOLTAGE_1
; ch
<= AD7280A_CELL_VOLTAGE_6
;
553 st
->iio_attr
[cnt
].address
=
554 AD7280A_DEVADDR(dev
) << 8 | ch
;
555 st
->iio_attr
[cnt
].dev_attr
.attr
.mode
=
557 st
->iio_attr
[cnt
].dev_attr
.show
=
558 ad7280_show_balance_sw
;
559 st
->iio_attr
[cnt
].dev_attr
.store
=
560 ad7280_store_balance_sw
;
561 st
->iio_attr
[cnt
].dev_attr
.attr
.name
=
562 kasprintf(GFP_KERNEL
,
563 "in%d-in%d_balance_switch_en",
564 (dev
* AD7280A_CELLS_PER_DEV
) + ch
,
565 (dev
* AD7280A_CELLS_PER_DEV
) + ch
+ 1);
566 ad7280_attributes
[cnt
] =
567 &st
->iio_attr
[cnt
].dev_attr
.attr
;
569 st
->iio_attr
[cnt
].address
=
570 AD7280A_DEVADDR(dev
) << 8 |
571 (AD7280A_CB1_TIMER
+ ch
);
572 st
->iio_attr
[cnt
].dev_attr
.attr
.mode
=
574 st
->iio_attr
[cnt
].dev_attr
.show
=
575 ad7280_show_balance_timer
;
576 st
->iio_attr
[cnt
].dev_attr
.store
=
577 ad7280_store_balance_timer
;
578 st
->iio_attr
[cnt
].dev_attr
.attr
.name
=
579 kasprintf(GFP_KERNEL
, "in%d-in%d_balance_timer",
580 (dev
* AD7280A_CELLS_PER_DEV
) + ch
,
581 (dev
* AD7280A_CELLS_PER_DEV
) + ch
+ 1);
582 ad7280_attributes
[cnt
] =
583 &st
->iio_attr
[cnt
].dev_attr
.attr
;
586 ad7280_attributes
[cnt
] = NULL
;
591 static ssize_t
ad7280_read_channel_config(struct device
*dev
,
592 struct device_attribute
*attr
,
595 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
596 struct ad7280_state
*st
= iio_priv(dev_info
);
597 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
600 switch (this_attr
->address
) {
601 case AD7280A_CELL_OVERVOLTAGE
:
602 val
= 1000 + (st
->cell_threshhigh
* 1568) / 100;
604 case AD7280A_CELL_UNDERVOLTAGE
:
605 val
= 1000 + (st
->cell_threshlow
* 1568) / 100;
607 case AD7280A_AUX_ADC_OVERVOLTAGE
:
608 val
= (st
->aux_threshhigh
* 196) / 10;
610 case AD7280A_AUX_ADC_UNDERVOLTAGE
:
611 val
= (st
->aux_threshlow
* 196) / 10;
617 return sprintf(buf
, "%d\n", val
);
620 static ssize_t
ad7280_write_channel_config(struct device
*dev
,
621 struct device_attribute
*attr
,
625 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
626 struct ad7280_state
*st
= iio_priv(dev_info
);
627 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
632 ret
= strict_strtol(buf
, 10, &val
);
636 switch (this_attr
->address
) {
637 case AD7280A_CELL_OVERVOLTAGE
:
638 case AD7280A_CELL_UNDERVOLTAGE
:
639 val
= ((val
- 1000) * 100) / 1568; /* LSB 15.68mV */
641 case AD7280A_AUX_ADC_OVERVOLTAGE
:
642 case AD7280A_AUX_ADC_UNDERVOLTAGE
:
643 val
= (val
* 10) / 196; /* LSB 19.6mV */
649 val
= clamp(val
, 0L, 0xFFL
);
651 mutex_lock(&dev_info
->mlock
);
652 switch (this_attr
->address
) {
653 case AD7280A_CELL_OVERVOLTAGE
:
654 st
->cell_threshhigh
= val
;
656 case AD7280A_CELL_UNDERVOLTAGE
:
657 st
->cell_threshlow
= val
;
659 case AD7280A_AUX_ADC_OVERVOLTAGE
:
660 st
->aux_threshhigh
= val
;
662 case AD7280A_AUX_ADC_UNDERVOLTAGE
:
663 st
->aux_threshlow
= val
;
667 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
,
668 this_attr
->address
, 1, val
);
670 mutex_unlock(&dev_info
->mlock
);
672 return ret
? ret
: len
;
675 static irqreturn_t
ad7280_event_handler(int irq
, void *private)
677 struct iio_dev
*dev_info
= private;
678 struct ad7280_state
*st
= iio_priv(dev_info
);
682 channels
= kzalloc(sizeof(*channels
) * st
->scan_cnt
, GFP_KERNEL
);
683 if (channels
== NULL
)
686 ret
= ad7280_read_all_channels(st
, st
->scan_cnt
, channels
);
690 for (i
= 0; i
< st
->scan_cnt
; i
++) {
691 if (((channels
[i
] >> 23) & 0xF) <= AD7280A_CELL_VOLTAGE_6
) {
692 if (((channels
[i
] >> 11) & 0xFFF) >=
694 iio_push_event(dev_info
, 0,
695 IIO_UNMOD_EVENT_CODE(IIO_IN_DIFF
,
700 else if (((channels
[i
] >> 11) & 0xFFF) <=
702 iio_push_event(dev_info
, 0,
703 IIO_UNMOD_EVENT_CODE(IIO_IN_DIFF
,
709 if (((channels
[i
] >> 11) & 0xFFF) >= st
->aux_threshhigh
)
710 iio_push_event(dev_info
, 0,
711 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
716 else if (((channels
[i
] >> 11) & 0xFFF) <=
718 iio_push_event(dev_info
, 0,
719 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
732 static IIO_DEVICE_ATTR_NAMED(in_thresh_low_value
,
733 in
-in_thresh_low_value
,
735 ad7280_read_channel_config
,
736 ad7280_write_channel_config
,
737 AD7280A_CELL_UNDERVOLTAGE
);
739 static IIO_DEVICE_ATTR_NAMED(in_thresh_high_value
,
740 in
-in_thresh_high_value
,
742 ad7280_read_channel_config
,
743 ad7280_write_channel_config
,
744 AD7280A_CELL_OVERVOLTAGE
);
746 static IIO_DEVICE_ATTR(temp_thresh_low_value
,
748 ad7280_read_channel_config
,
749 ad7280_write_channel_config
,
750 AD7280A_AUX_ADC_UNDERVOLTAGE
);
752 static IIO_DEVICE_ATTR(temp_thresh_high_value
,
754 ad7280_read_channel_config
,
755 ad7280_write_channel_config
,
756 AD7280A_AUX_ADC_OVERVOLTAGE
);
759 static struct attribute
*ad7280_event_attributes
[] = {
760 &iio_dev_attr_in_thresh_low_value
.dev_attr
.attr
,
761 &iio_dev_attr_in_thresh_high_value
.dev_attr
.attr
,
762 &iio_dev_attr_temp_thresh_low_value
.dev_attr
.attr
,
763 &iio_dev_attr_temp_thresh_high_value
.dev_attr
.attr
,
767 static struct attribute_group ad7280_event_attrs_group
= {
768 .attrs
= ad7280_event_attributes
,
771 static int ad7280_read_raw(struct iio_dev
*dev_info
,
772 struct iio_chan_spec
const *chan
,
777 struct ad7280_state
*st
= iio_priv(dev_info
);
778 unsigned int scale_uv
;
783 mutex_lock(&dev_info
->mlock
);
784 if (chan
->address
== AD7280A_ALL_CELLS
)
785 ret
= ad7280_read_all_channels(st
, st
->scan_cnt
, NULL
);
787 ret
= ad7280_read_channel(st
, chan
->address
>> 8,
788 chan
->address
& 0xFF);
789 mutex_unlock(&dev_info
->mlock
);
797 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
798 if ((chan
->address
& 0xFF) <= AD7280A_CELL_VOLTAGE_6
)
799 scale_uv
= (4000 * 1000) >> AD7280A_BITS
;
801 scale_uv
= (5000 * 1000) >> AD7280A_BITS
;
803 *val
= scale_uv
/ 1000;
804 *val2
= (scale_uv
% 1000) * 1000;
805 return IIO_VAL_INT_PLUS_MICRO
;
810 static const struct iio_info ad7280_info
= {
811 .read_raw
= &ad7280_read_raw
,
812 .num_interrupt_lines
= 1,
813 .event_attrs
= &ad7280_event_attrs_group
,
814 .attrs
= &ad7280_attrs_group
,
815 .driver_module
= THIS_MODULE
,
818 static const struct ad7280_platform_data ad7793_default_pdata
= {
819 .acquisition_time
= AD7280A_ACQ_TIME_400ns
,
820 .conversion_averaging
= AD7280A_CONV_AVG_DIS
,
821 .thermistor_term_en
= true,
824 static int __devinit
ad7280_probe(struct spi_device
*spi
)
826 const struct ad7280_platform_data
*pdata
= spi
->dev
.platform_data
;
827 struct ad7280_state
*st
;
828 int ret
, regdone
= 0;
829 const unsigned short tACQ_ns
[4] = {465, 1010, 1460, 1890};
830 const unsigned short nAVG
[4] = {1, 2, 4, 8};
831 struct iio_dev
*indio_dev
= iio_allocate_device(sizeof(*st
));
833 if (indio_dev
== NULL
)
836 st
= iio_priv(indio_dev
);
837 spi_set_drvdata(spi
, indio_dev
);
841 pdata
= &ad7793_default_pdata
;
843 ad7280_crc8_build_table(st
->crc_tab
);
845 st
->spi
->max_speed_hz
= AD7280A_MAX_SPI_CLK_Hz
;
846 st
->spi
->mode
= SPI_MODE_1
;
849 st
->ctrl_lb
= AD7280A_CTRL_LB_ACQ_TIME(pdata
->acquisition_time
& 0x3);
850 st
->ctrl_hb
= AD7280A_CTRL_HB_CONV_AVG(pdata
->conversion_averaging
851 & 0x3) | (pdata
->thermistor_term_en
?
852 AD7280A_CTRL_LB_THERMISTOR_EN
: 0);
854 ret
= ad7280_chain_setup(st
);
856 goto error_free_device
;
859 st
->scan_cnt
= (st
->slave_num
+ 1) * AD7280A_NUM_CH
;
860 st
->cell_threshhigh
= 0xFF;
861 st
->aux_threshhigh
= 0xFF;
864 * Total Conversion Time = ((tACQ + tCONV) *
865 * (Number of Conversions per Part)) −
866 * tACQ + ((N - 1) * tDELAY)
868 * Readback Delay = Total Conversion Time + tWAIT
871 st
->readback_delay_us
=
872 ((tACQ_ns
[pdata
->acquisition_time
& 0x3] + 695) *
873 (AD7280A_NUM_CH
* nAVG
[pdata
->conversion_averaging
& 0x3]))
874 - tACQ_ns
[pdata
->acquisition_time
& 0x3] +
877 /* Convert to usecs */
878 st
->readback_delay_us
= DIV_ROUND_UP(st
->readback_delay_us
, 1000);
879 st
->readback_delay_us
+= 5; /* Add tWAIT */
881 indio_dev
->name
= spi_get_device_id(spi
)->name
;
882 indio_dev
->dev
.parent
= &spi
->dev
;
883 indio_dev
->modes
= INDIO_DIRECT_MODE
;
885 ret
= ad7280_channel_init(st
);
887 goto error_free_device
;
889 indio_dev
->num_channels
= ret
;
890 indio_dev
->channels
= st
->channels
;
891 indio_dev
->info
= &ad7280_info
;
893 ret
= ad7280_attr_init(st
);
895 goto error_free_channels
;
897 ret
= iio_device_register(indio_dev
);
899 goto error_free_attr
;
903 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
,
905 AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN
);
907 goto error_free_attr
;
909 ret
= ad7280_write(st
, AD7280A_DEVADDR(st
->slave_num
),
911 AD7280A_ALERT_GEN_STATIC_HIGH
|
912 (pdata
->chain_last_alert_ignore
& 0xF));
914 goto error_free_attr
;
916 ret
= request_threaded_irq(spi
->irq
,
918 ad7280_event_handler
,
919 IRQF_TRIGGER_FALLING
|
924 goto error_free_attr
;
937 iio_device_unregister(indio_dev
);
939 iio_free_device(indio_dev
);
944 static int __devexit
ad7280_remove(struct spi_device
*spi
)
946 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
947 struct ad7280_state
*st
= iio_priv(indio_dev
);
950 free_irq(spi
->irq
, indio_dev
);
952 ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CONTROL_HB
, 1,
953 AD7280A_CTRL_HB_PWRDN_SW
| st
->ctrl_hb
);
957 iio_device_unregister(indio_dev
);
962 static const struct spi_device_id ad7280_id
[] = {
967 static struct spi_driver ad7280_driver
= {
970 .bus
= &spi_bus_type
,
971 .owner
= THIS_MODULE
,
973 .probe
= ad7280_probe
,
974 .remove
= __devexit_p(ad7280_remove
),
975 .id_table
= ad7280_id
,
978 static int __init
ad7280_init(void)
980 return spi_register_driver(&ad7280_driver
);
982 module_init(ad7280_init
);
984 static void __exit
ad7280_exit(void)
986 spi_unregister_driver(&ad7280_driver
);
988 module_exit(ad7280_exit
);
990 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
991 MODULE_DESCRIPTION("Analog Devices AD7280A");
992 MODULE_LICENSE("GPL v2");