1 // SPDX-License-Identifier: GPL-2.0
3 * AD7280A Lithium Ion Battery Monitoring System
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/crc8.h>
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>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
26 #define AD7280A_CELL_VOLTAGE_1 0x0 /* D11 to D0, Read only */
27 #define AD7280A_CELL_VOLTAGE_2 0x1 /* D11 to D0, Read only */
28 #define AD7280A_CELL_VOLTAGE_3 0x2 /* D11 to D0, Read only */
29 #define AD7280A_CELL_VOLTAGE_4 0x3 /* D11 to D0, Read only */
30 #define AD7280A_CELL_VOLTAGE_5 0x4 /* D11 to D0, Read only */
31 #define AD7280A_CELL_VOLTAGE_6 0x5 /* D11 to D0, Read only */
32 #define AD7280A_AUX_ADC_1 0x6 /* D11 to D0, Read only */
33 #define AD7280A_AUX_ADC_2 0x7 /* D11 to D0, Read only */
34 #define AD7280A_AUX_ADC_3 0x8 /* D11 to D0, Read only */
35 #define AD7280A_AUX_ADC_4 0x9 /* D11 to D0, Read only */
36 #define AD7280A_AUX_ADC_5 0xA /* D11 to D0, Read only */
37 #define AD7280A_AUX_ADC_6 0xB /* D11 to D0, Read only */
38 #define AD7280A_SELF_TEST 0xC /* D11 to D0, Read only */
39 #define AD7280A_CONTROL_HB 0xD /* D15 to D8, Read/write */
40 #define AD7280A_CONTROL_LB 0xE /* D7 to D0, Read/write */
41 #define AD7280A_CELL_OVERVOLTAGE 0xF /* D7 to D0, Read/write */
42 #define AD7280A_CELL_UNDERVOLTAGE 0x10 /* D7 to D0, Read/write */
43 #define AD7280A_AUX_ADC_OVERVOLTAGE 0x11 /* D7 to D0, Read/write */
44 #define AD7280A_AUX_ADC_UNDERVOLTAGE 0x12 /* D7 to D0, Read/write */
45 #define AD7280A_ALERT 0x13 /* D7 to D0, Read/write */
46 #define AD7280A_CELL_BALANCE 0x14 /* D7 to D0, Read/write */
47 #define AD7280A_CB1_TIMER 0x15 /* D7 to D0, Read/write */
48 #define AD7280A_CB2_TIMER 0x16 /* D7 to D0, Read/write */
49 #define AD7280A_CB3_TIMER 0x17 /* D7 to D0, Read/write */
50 #define AD7280A_CB4_TIMER 0x18 /* D7 to D0, Read/write */
51 #define AD7280A_CB5_TIMER 0x19 /* D7 to D0, Read/write */
52 #define AD7280A_CB6_TIMER 0x1A /* D7 to D0, Read/write */
53 #define AD7280A_PD_TIMER 0x1B /* D7 to D0, Read/write */
54 #define AD7280A_READ 0x1C /* D7 to D0, Read/write */
55 #define AD7280A_CNVST_CONTROL 0x1D /* D7 to D0, Read/write */
58 #define AD7280A_CTRL_HB_CONV_INPUT_ALL 0
59 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_4 BIT(6)
60 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL BIT(7)
61 #define AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST (BIT(7) | BIT(6))
62 #define AD7280A_CTRL_HB_CONV_RES_READ_ALL 0
63 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL_AUX1_3_4 BIT(4)
64 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL BIT(5)
65 #define AD7280A_CTRL_HB_CONV_RES_READ_NO (BIT(5) | BIT(4))
66 #define AD7280A_CTRL_HB_CONV_START_CNVST 0
67 #define AD7280A_CTRL_HB_CONV_START_CS BIT(3)
68 #define AD7280A_CTRL_HB_CONV_AVG_DIS 0
69 #define AD7280A_CTRL_HB_CONV_AVG_2 BIT(1)
70 #define AD7280A_CTRL_HB_CONV_AVG_4 BIT(2)
71 #define AD7280A_CTRL_HB_CONV_AVG_8 (BIT(2) | BIT(1))
72 #define AD7280A_CTRL_HB_CONV_AVG(x) ((x) << 1)
73 #define AD7280A_CTRL_HB_PWRDN_SW BIT(0)
75 #define AD7280A_CTRL_LB_SWRST BIT(7)
76 #define AD7280A_CTRL_LB_ACQ_TIME_400ns 0
77 #define AD7280A_CTRL_LB_ACQ_TIME_800ns BIT(5)
78 #define AD7280A_CTRL_LB_ACQ_TIME_1200ns BIT(6)
79 #define AD7280A_CTRL_LB_ACQ_TIME_1600ns (BIT(6) | BIT(5))
80 #define AD7280A_CTRL_LB_ACQ_TIME(x) ((x) << 5)
81 #define AD7280A_CTRL_LB_MUST_SET BIT(4)
82 #define AD7280A_CTRL_LB_THERMISTOR_EN BIT(3)
83 #define AD7280A_CTRL_LB_LOCK_DEV_ADDR BIT(2)
84 #define AD7280A_CTRL_LB_INC_DEV_ADDR BIT(1)
85 #define AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN BIT(0)
87 #define AD7280A_ALERT_GEN_STATIC_HIGH BIT(6)
88 #define AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN (BIT(7) | BIT(6))
90 #define AD7280A_ALL_CELLS (0xAD << 16)
92 #define AD7280A_MAX_SPI_CLK_HZ 700000 /* < 1MHz */
93 #define AD7280A_MAX_CHAIN 8
94 #define AD7280A_CELLS_PER_DEV 6
95 #define AD7280A_BITS 12
96 #define AD7280A_NUM_CH (AD7280A_AUX_ADC_6 - \
97 AD7280A_CELL_VOLTAGE_1 + 1)
99 #define AD7280A_CALC_VOLTAGE_CHAN_NUM(d, c) (((d) * AD7280A_CELLS_PER_DEV) + \
101 #define AD7280A_CALC_TEMP_CHAN_NUM(d, c) (((d) * AD7280A_CELLS_PER_DEV) + \
102 (c) - AD7280A_CELLS_PER_DEV)
104 #define AD7280A_DEVADDR_MASTER 0
105 #define AD7280A_DEVADDR_ALL 0x1F
106 /* 5-bit device address is sent LSB first */
107 static unsigned int ad7280a_devaddr(unsigned int addr)
109 return ((addr & 0x1) << 4) |
110 ((addr & 0x2) << 3) |
112 ((addr & 0x8) >> 3) |
113 ((addr & 0x10) >> 4);
116 /* During a read a valid write is mandatory.
117 * So writing to the highest available address (Address 0x1F)
118 * and setting the address all parts bit to 0 is recommended
119 * So the TXVAL is AD7280A_DEVADDR_ALL + CRC
121 #define AD7280A_READ_TXVAL 0xF800030A
126 * P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 => 0x2F
130 struct ad7280_state {
131 struct spi_device *spi;
132 struct iio_chan_spec *channels;
133 struct iio_dev_attr *iio_attr;
136 int readback_delay_us;
137 unsigned char crc_tab[CRC8_TABLE_SIZE];
138 unsigned char ctrl_hb;
139 unsigned char ctrl_lb;
140 unsigned char cell_threshhigh;
141 unsigned char cell_threshlow;
142 unsigned char aux_threshhigh;
143 unsigned char aux_threshlow;
144 unsigned char cb_mask[AD7280A_MAX_CHAIN];
145 struct mutex lock; /* protect sensor state */
147 __be32 buf[2] ____cacheline_aligned;
150 static unsigned char ad7280_calc_crc8(unsigned char *crc_tab, unsigned int val)
154 crc = crc_tab[val >> 16 & 0xFF];
155 crc = crc_tab[crc ^ (val >> 8 & 0xFF)];
157 return crc ^ (val & 0xFF);
160 static int ad7280_check_crc(struct ad7280_state *st, unsigned int val)
162 unsigned char crc = ad7280_calc_crc8(st->crc_tab, val >> 10);
164 if (crc != ((val >> 2) & 0xFF))
170 /* After initiating a conversion sequence we need to wait until the
171 * conversion is done. The delay is typically in the range of 15..30 us
172 * however depending an the number of devices in the daisy chain and the
173 * number of averages taken, conversion delays and acquisition time options
174 * it may take up to 250us, in this case we better sleep instead of busy
178 static void ad7280_delay(struct ad7280_state *st)
180 if (st->readback_delay_us < 50)
181 udelay(st->readback_delay_us);
183 usleep_range(250, 500);
186 static int __ad7280_read32(struct ad7280_state *st, unsigned int *val)
189 struct spi_transfer t = {
190 .tx_buf = &st->buf[0],
191 .rx_buf = &st->buf[1],
195 st->buf[0] = cpu_to_be32(AD7280A_READ_TXVAL);
197 ret = spi_sync_transfer(st->spi, &t, 1);
201 *val = be32_to_cpu(st->buf[1]);
206 static int ad7280_write(struct ad7280_state *st, unsigned int devaddr,
207 unsigned int addr, bool all, unsigned int val)
209 unsigned int reg = devaddr << 27 | addr << 21 |
210 (val & 0xFF) << 13 | all << 12;
212 reg |= ad7280_calc_crc8(st->crc_tab, reg >> 11) << 3 | 0x2;
213 st->buf[0] = cpu_to_be32(reg);
215 return spi_write(st->spi, &st->buf[0], 4);
218 static int ad7280_read(struct ad7280_state *st, unsigned int devaddr,
224 /* turns off the read operation on all parts */
225 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
226 AD7280A_CTRL_HB_CONV_INPUT_ALL |
227 AD7280A_CTRL_HB_CONV_RES_READ_NO |
232 /* turns on the read operation on the addressed part */
233 ret = ad7280_write(st, devaddr, AD7280A_CONTROL_HB, 0,
234 AD7280A_CTRL_HB_CONV_INPUT_ALL |
235 AD7280A_CTRL_HB_CONV_RES_READ_ALL |
240 /* Set register address on the part to be read from */
241 ret = ad7280_write(st, devaddr, AD7280A_READ, 0, addr << 2);
245 ret = __ad7280_read32(st, &tmp);
249 if (ad7280_check_crc(st, tmp))
252 if (((tmp >> 27) != devaddr) || (((tmp >> 21) & 0x3F) != addr))
255 return (tmp >> 13) & 0xFF;
258 static int ad7280_read_channel(struct ad7280_state *st, unsigned int devaddr,
264 ret = ad7280_write(st, devaddr, AD7280A_READ, 0, addr << 2);
268 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
269 AD7280A_CTRL_HB_CONV_INPUT_ALL |
270 AD7280A_CTRL_HB_CONV_RES_READ_NO |
275 ret = ad7280_write(st, devaddr, AD7280A_CONTROL_HB, 0,
276 AD7280A_CTRL_HB_CONV_INPUT_ALL |
277 AD7280A_CTRL_HB_CONV_RES_READ_ALL |
278 AD7280A_CTRL_HB_CONV_START_CS |
285 ret = __ad7280_read32(st, &tmp);
289 if (ad7280_check_crc(st, tmp))
292 if (((tmp >> 27) != devaddr) || (((tmp >> 23) & 0xF) != addr))
295 return (tmp >> 11) & 0xFFF;
298 static int ad7280_read_all_channels(struct ad7280_state *st, unsigned int cnt,
302 unsigned int tmp, sum = 0;
304 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_READ, 1,
305 AD7280A_CELL_VOLTAGE_1 << 2);
309 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
310 AD7280A_CTRL_HB_CONV_INPUT_ALL |
311 AD7280A_CTRL_HB_CONV_RES_READ_ALL |
312 AD7280A_CTRL_HB_CONV_START_CS |
319 for (i = 0; i < cnt; i++) {
320 ret = __ad7280_read32(st, &tmp);
324 if (ad7280_check_crc(st, tmp))
329 /* only sum cell voltages */
330 if (((tmp >> 23) & 0xF) <= AD7280A_CELL_VOLTAGE_6)
331 sum += ((tmp >> 11) & 0xFFF);
337 static void ad7280_sw_power_down(void *data)
339 struct ad7280_state *st = data;
341 ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
342 AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
345 static int ad7280_chain_setup(struct ad7280_state *st)
350 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_LB, 1,
351 AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN |
352 AD7280A_CTRL_LB_LOCK_DEV_ADDR |
353 AD7280A_CTRL_LB_MUST_SET |
354 AD7280A_CTRL_LB_SWRST |
359 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_LB, 1,
360 AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN |
361 AD7280A_CTRL_LB_LOCK_DEV_ADDR |
362 AD7280A_CTRL_LB_MUST_SET |
365 goto error_power_down;
367 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_READ, 1,
368 AD7280A_CONTROL_LB << 2);
370 goto error_power_down;
372 for (n = 0; n <= AD7280A_MAX_CHAIN; n++) {
373 ret = __ad7280_read32(st, &val);
375 goto error_power_down;
380 if (ad7280_check_crc(st, val)) {
382 goto error_power_down;
385 if (n != ad7280a_devaddr(val >> 27)) {
387 goto error_power_down;
393 ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CONTROL_HB, 1,
394 AD7280A_CTRL_HB_PWRDN_SW | st->ctrl_hb);
399 static ssize_t ad7280_show_balance_sw(struct device *dev,
400 struct device_attribute *attr,
403 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
404 struct ad7280_state *st = iio_priv(indio_dev);
405 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
407 return sprintf(buf, "%d\n",
408 !!(st->cb_mask[this_attr->address >> 8] &
409 (1 << ((this_attr->address & 0xFF) + 2))));
412 static ssize_t ad7280_store_balance_sw(struct device *dev,
413 struct device_attribute *attr,
417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
418 struct ad7280_state *st = iio_priv(indio_dev);
419 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
422 unsigned int devaddr, ch;
424 ret = strtobool(buf, &readin);
428 devaddr = this_attr->address >> 8;
429 ch = this_attr->address & 0xFF;
431 mutex_lock(&st->lock);
433 st->cb_mask[devaddr] |= 1 << (ch + 2);
435 st->cb_mask[devaddr] &= ~(1 << (ch + 2));
437 ret = ad7280_write(st, devaddr, AD7280A_CELL_BALANCE,
438 0, st->cb_mask[devaddr]);
439 mutex_unlock(&st->lock);
441 return ret ? ret : len;
444 static ssize_t ad7280_show_balance_timer(struct device *dev,
445 struct device_attribute *attr,
448 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
449 struct ad7280_state *st = iio_priv(indio_dev);
450 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
454 mutex_lock(&st->lock);
455 ret = ad7280_read(st, this_attr->address >> 8,
456 this_attr->address & 0xFF);
457 mutex_unlock(&st->lock);
462 msecs = (ret >> 3) * 71500;
464 return sprintf(buf, "%u\n", msecs);
467 static ssize_t ad7280_store_balance_timer(struct device *dev,
468 struct device_attribute *attr,
472 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
473 struct ad7280_state *st = iio_priv(indio_dev);
474 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
478 ret = kstrtoul(buf, 10, &val);
487 mutex_lock(&st->lock);
488 ret = ad7280_write(st, this_attr->address >> 8,
489 this_attr->address & 0xFF,
490 0, (val & 0x1F) << 3);
491 mutex_unlock(&st->lock);
493 return ret ? ret : len;
496 static struct attribute *ad7280_attributes[AD7280A_MAX_CHAIN *
497 AD7280A_CELLS_PER_DEV * 2 + 1];
499 static const struct attribute_group ad7280_attrs_group = {
500 .attrs = ad7280_attributes,
503 static void ad7280_voltage_channel_init(struct iio_chan_spec *chan, int i)
505 chan->type = IIO_VOLTAGE;
506 chan->differential = 1;
508 chan->channel2 = chan->channel + 1;
511 static void ad7280_temp_channel_init(struct iio_chan_spec *chan, int i)
513 chan->type = IIO_TEMP;
517 static void ad7280_common_fields_init(struct iio_chan_spec *chan, int addr,
521 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
522 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
523 chan->address = addr;
524 chan->scan_index = cnt;
525 chan->scan_type.sign = 'u';
526 chan->scan_type.realbits = 12;
527 chan->scan_type.storagebits = 32;
530 static void ad7280_total_voltage_channel_init(struct iio_chan_spec *chan,
533 chan->type = IIO_VOLTAGE;
534 chan->differential = 1;
536 chan->channel2 = dev * AD7280A_CELLS_PER_DEV;
537 chan->address = AD7280A_ALL_CELLS;
539 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
540 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
541 chan->scan_index = cnt;
542 chan->scan_type.sign = 'u';
543 chan->scan_type.realbits = 32;
544 chan->scan_type.storagebits = 32;
547 static void ad7280_timestamp_channel_init(struct iio_chan_spec *chan, int cnt)
549 chan->type = IIO_TIMESTAMP;
551 chan->scan_index = cnt;
552 chan->scan_type.sign = 's';
553 chan->scan_type.realbits = 64;
554 chan->scan_type.storagebits = 64;
557 static void ad7280_init_dev_channels(struct ad7280_state *st, int dev, int *cnt)
560 struct iio_chan_spec *chan;
562 for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_AUX_ADC_6; ch++) {
563 chan = &st->channels[*cnt];
565 if (ch < AD7280A_AUX_ADC_1) {
566 i = AD7280A_CALC_VOLTAGE_CHAN_NUM(dev, ch);
567 ad7280_voltage_channel_init(chan, i);
569 i = AD7280A_CALC_TEMP_CHAN_NUM(dev, ch);
570 ad7280_temp_channel_init(chan, i);
573 addr = ad7280a_devaddr(dev) << 8 | ch;
574 ad7280_common_fields_init(chan, addr, *cnt);
580 static int ad7280_channel_init(struct ad7280_state *st)
584 st->channels = devm_kcalloc(&st->spi->dev, (st->slave_num + 1) * 12 + 2,
585 sizeof(*st->channels), GFP_KERNEL);
589 for (dev = 0; dev <= st->slave_num; dev++)
590 ad7280_init_dev_channels(st, dev, &cnt);
592 ad7280_total_voltage_channel_init(&st->channels[cnt], cnt, dev);
594 ad7280_timestamp_channel_init(&st->channels[cnt], cnt);
599 static int ad7280_balance_switch_attr_init(struct iio_dev_attr *attr,
600 struct device *dev, int addr, int i)
602 attr->address = addr;
603 attr->dev_attr.attr.mode = 0644;
604 attr->dev_attr.show = ad7280_show_balance_sw;
605 attr->dev_attr.store = ad7280_store_balance_sw;
606 attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
607 "in%d-in%d_balance_switch_en",
609 if (!attr->dev_attr.attr.name)
615 static int ad7280_balance_timer_attr_init(struct iio_dev_attr *attr,
616 struct device *dev, int addr, int i)
618 attr->address = addr;
619 attr->dev_attr.attr.mode = 0644;
620 attr->dev_attr.show = ad7280_show_balance_timer;
621 attr->dev_attr.store = ad7280_store_balance_timer;
622 attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
623 "in%d-in%d_balance_timer",
625 if (!attr->dev_attr.attr.name)
631 static int ad7280_init_dev_attrs(struct ad7280_state *st, int dev, int *cnt)
633 int addr, ch, i, ret;
634 struct iio_dev_attr *iio_attr;
635 struct device *sdev = &st->spi->dev;
637 for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6; ch++) {
638 iio_attr = &st->iio_attr[*cnt];
639 addr = ad7280a_devaddr(dev) << 8 | ch;
640 i = dev * AD7280A_CELLS_PER_DEV + ch;
642 ret = ad7280_balance_switch_attr_init(iio_attr, sdev, addr, i);
646 ad7280_attributes[*cnt] = &iio_attr->dev_attr.attr;
649 iio_attr = &st->iio_attr[*cnt];
650 addr = ad7280a_devaddr(dev) << 8 | (AD7280A_CB1_TIMER + ch);
652 ret = ad7280_balance_timer_attr_init(iio_attr, sdev, addr, i);
656 ad7280_attributes[*cnt] = &iio_attr->dev_attr.attr;
660 ad7280_attributes[*cnt] = NULL;
665 static int ad7280_attr_init(struct ad7280_state *st)
667 int dev, cnt = 0, ret;
669 st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) *
670 (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
675 for (dev = 0; dev <= st->slave_num; dev++) {
676 ret = ad7280_init_dev_attrs(st, dev, &cnt);
684 static ssize_t ad7280_read_channel_config(struct device *dev,
685 struct device_attribute *attr,
688 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
689 struct ad7280_state *st = iio_priv(indio_dev);
690 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
693 switch (this_attr->address) {
694 case AD7280A_CELL_OVERVOLTAGE:
695 val = 1000 + (st->cell_threshhigh * 1568) / 100;
697 case AD7280A_CELL_UNDERVOLTAGE:
698 val = 1000 + (st->cell_threshlow * 1568) / 100;
700 case AD7280A_AUX_ADC_OVERVOLTAGE:
701 val = (st->aux_threshhigh * 196) / 10;
703 case AD7280A_AUX_ADC_UNDERVOLTAGE:
704 val = (st->aux_threshlow * 196) / 10;
710 return sprintf(buf, "%u\n", val);
713 static ssize_t ad7280_write_channel_config(struct device *dev,
714 struct device_attribute *attr,
718 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
719 struct ad7280_state *st = iio_priv(indio_dev);
720 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
725 ret = kstrtol(buf, 10, &val);
729 switch (this_attr->address) {
730 case AD7280A_CELL_OVERVOLTAGE:
731 case AD7280A_CELL_UNDERVOLTAGE:
732 val = ((val - 1000) * 100) / 1568; /* LSB 15.68mV */
734 case AD7280A_AUX_ADC_OVERVOLTAGE:
735 case AD7280A_AUX_ADC_UNDERVOLTAGE:
736 val = (val * 10) / 196; /* LSB 19.6mV */
742 val = clamp(val, 0L, 0xFFL);
744 mutex_lock(&st->lock);
745 switch (this_attr->address) {
746 case AD7280A_CELL_OVERVOLTAGE:
747 st->cell_threshhigh = val;
749 case AD7280A_CELL_UNDERVOLTAGE:
750 st->cell_threshlow = val;
752 case AD7280A_AUX_ADC_OVERVOLTAGE:
753 st->aux_threshhigh = val;
755 case AD7280A_AUX_ADC_UNDERVOLTAGE:
756 st->aux_threshlow = val;
760 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
761 this_attr->address, 1, val);
763 mutex_unlock(&st->lock);
765 return ret ? ret : len;
768 static irqreturn_t ad7280_event_handler(int irq, void *private)
770 struct iio_dev *indio_dev = private;
771 struct ad7280_state *st = iio_priv(indio_dev);
772 unsigned int *channels;
775 channels = kcalloc(st->scan_cnt, sizeof(*channels), GFP_KERNEL);
779 ret = ad7280_read_all_channels(st, st->scan_cnt, channels);
783 for (i = 0; i < st->scan_cnt; i++) {
784 if (((channels[i] >> 23) & 0xF) <= AD7280A_CELL_VOLTAGE_6) {
785 if (((channels[i] >> 11) & 0xFFF) >=
786 st->cell_threshhigh) {
787 u64 tmp = IIO_EVENT_CODE(IIO_VOLTAGE, 1, 0,
791 iio_push_event(indio_dev, tmp,
792 iio_get_time_ns(indio_dev));
793 } else if (((channels[i] >> 11) & 0xFFF) <=
794 st->cell_threshlow) {
795 u64 tmp = IIO_EVENT_CODE(IIO_VOLTAGE, 1, 0,
799 iio_push_event(indio_dev, tmp,
800 iio_get_time_ns(indio_dev));
803 if (((channels[i] >> 11) & 0xFFF) >=
804 st->aux_threshhigh) {
805 u64 tmp = IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
808 iio_push_event(indio_dev, tmp,
809 iio_get_time_ns(indio_dev));
810 } else if (((channels[i] >> 11) & 0xFFF) <=
812 u64 tmp = IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
815 iio_push_event(indio_dev, tmp,
816 iio_get_time_ns(indio_dev));
827 /* Note: No need to fix checkpatch warning that reads:
828 * CHECK: spaces preferred around that '-' (ctx:VxV)
829 * The function argument is stringified and doesn't need a fix
831 static IIO_DEVICE_ATTR_NAMED(in_thresh_low_value,
832 in_voltage-voltage_thresh_low_value,
834 ad7280_read_channel_config,
835 ad7280_write_channel_config,
836 AD7280A_CELL_UNDERVOLTAGE);
838 static IIO_DEVICE_ATTR_NAMED(in_thresh_high_value,
839 in_voltage-voltage_thresh_high_value,
841 ad7280_read_channel_config,
842 ad7280_write_channel_config,
843 AD7280A_CELL_OVERVOLTAGE);
845 static IIO_DEVICE_ATTR(in_temp_thresh_low_value,
847 ad7280_read_channel_config,
848 ad7280_write_channel_config,
849 AD7280A_AUX_ADC_UNDERVOLTAGE);
851 static IIO_DEVICE_ATTR(in_temp_thresh_high_value,
853 ad7280_read_channel_config,
854 ad7280_write_channel_config,
855 AD7280A_AUX_ADC_OVERVOLTAGE);
857 static struct attribute *ad7280_event_attributes[] = {
858 &iio_dev_attr_in_thresh_low_value.dev_attr.attr,
859 &iio_dev_attr_in_thresh_high_value.dev_attr.attr,
860 &iio_dev_attr_in_temp_thresh_low_value.dev_attr.attr,
861 &iio_dev_attr_in_temp_thresh_high_value.dev_attr.attr,
865 static const struct attribute_group ad7280_event_attrs_group = {
866 .attrs = ad7280_event_attributes,
869 static int ad7280_read_raw(struct iio_dev *indio_dev,
870 struct iio_chan_spec const *chan,
875 struct ad7280_state *st = iio_priv(indio_dev);
879 case IIO_CHAN_INFO_RAW:
880 mutex_lock(&st->lock);
881 if (chan->address == AD7280A_ALL_CELLS)
882 ret = ad7280_read_all_channels(st, st->scan_cnt, NULL);
884 ret = ad7280_read_channel(st, chan->address >> 8,
885 chan->address & 0xFF);
886 mutex_unlock(&st->lock);
894 case IIO_CHAN_INFO_SCALE:
895 if ((chan->address & 0xFF) <= AD7280A_CELL_VOLTAGE_6)
900 *val2 = AD7280A_BITS;
901 return IIO_VAL_FRACTIONAL_LOG2;
906 static const struct iio_info ad7280_info = {
907 .read_raw = ad7280_read_raw,
908 .event_attrs = &ad7280_event_attrs_group,
909 .attrs = &ad7280_attrs_group,
912 static const struct ad7280_platform_data ad7793_default_pdata = {
913 .acquisition_time = AD7280A_ACQ_TIME_400ns,
914 .conversion_averaging = AD7280A_CONV_AVG_DIS,
915 .thermistor_term_en = true,
918 static int ad7280_probe(struct spi_device *spi)
920 const struct ad7280_platform_data *pdata = dev_get_platdata(&spi->dev);
921 struct ad7280_state *st;
923 const unsigned short t_acq_ns[4] = {465, 1010, 1460, 1890};
924 const unsigned short n_avg[4] = {1, 2, 4, 8};
925 struct iio_dev *indio_dev;
927 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
931 st = iio_priv(indio_dev);
932 spi_set_drvdata(spi, indio_dev);
934 mutex_init(&st->lock);
937 pdata = &ad7793_default_pdata;
939 crc8_populate_msb(st->crc_tab, POLYNOM);
941 st->spi->max_speed_hz = AD7280A_MAX_SPI_CLK_HZ;
942 st->spi->mode = SPI_MODE_1;
945 st->ctrl_lb = AD7280A_CTRL_LB_ACQ_TIME(pdata->acquisition_time & 0x3);
946 st->ctrl_hb = AD7280A_CTRL_HB_CONV_AVG(pdata->conversion_averaging
947 & 0x3) | (pdata->thermistor_term_en ?
948 AD7280A_CTRL_LB_THERMISTOR_EN : 0);
950 ret = ad7280_chain_setup(st);
955 st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH;
956 st->cell_threshhigh = 0xFF;
957 st->aux_threshhigh = 0xFF;
959 ret = devm_add_action_or_reset(&spi->dev, ad7280_sw_power_down, st);
964 * Total Conversion Time = ((tACQ + tCONV) *
965 * (Number of Conversions per Part)) −
966 * tACQ + ((N - 1) * tDELAY)
968 * Readback Delay = Total Conversion Time + tWAIT
971 st->readback_delay_us =
972 ((t_acq_ns[pdata->acquisition_time & 0x3] + 695) *
973 (AD7280A_NUM_CH * n_avg[pdata->conversion_averaging & 0x3])) -
974 t_acq_ns[pdata->acquisition_time & 0x3] + st->slave_num * 250;
976 /* Convert to usecs */
977 st->readback_delay_us = DIV_ROUND_UP(st->readback_delay_us, 1000);
978 st->readback_delay_us += 5; /* Add tWAIT */
980 indio_dev->name = spi_get_device_id(spi)->name;
981 indio_dev->modes = INDIO_DIRECT_MODE;
983 ret = ad7280_channel_init(st);
987 indio_dev->num_channels = ret;
988 indio_dev->channels = st->channels;
989 indio_dev->info = &ad7280_info;
991 ret = ad7280_attr_init(st);
995 ret = devm_iio_device_register(&spi->dev, indio_dev);
1000 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER,
1002 AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN);
1006 ret = ad7280_write(st, ad7280a_devaddr(st->slave_num),
1008 AD7280A_ALERT_GEN_STATIC_HIGH |
1009 (pdata->chain_last_alert_ignore & 0xF));
1013 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
1015 ad7280_event_handler,
1016 IRQF_TRIGGER_FALLING |
1027 static const struct spi_device_id ad7280_id[] = {
1031 MODULE_DEVICE_TABLE(spi, ad7280_id);
1033 static struct spi_driver ad7280_driver = {
1037 .probe = ad7280_probe,
1038 .id_table = ad7280_id,
1040 module_spi_driver(ad7280_driver);
1043 MODULE_DESCRIPTION("Analog Devices AD7280A");
1044 MODULE_LICENSE("GPL v2");