9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
17 #include <linux/module.h>
26 #define AD7280A_CELL_VOLTAGE_1 0x0
27 #define AD7280A_CELL_VOLTAGE_2 0x1
28 #define AD7280A_CELL_VOLTAGE_3 0x2
29 #define AD7280A_CELL_VOLTAGE_4 0x3
30 #define AD7280A_CELL_VOLTAGE_5 0x4
31 #define AD7280A_CELL_VOLTAGE_6 0x5
32 #define AD7280A_AUX_ADC_1 0x6
33 #define AD7280A_AUX_ADC_2 0x7
34 #define AD7280A_AUX_ADC_3 0x8
35 #define AD7280A_AUX_ADC_4 0x9
36 #define AD7280A_AUX_ADC_5 0xA
37 #define AD7280A_AUX_ADC_6 0xB
38 #define AD7280A_SELF_TEST 0xC
39 #define AD7280A_CONTROL_HB 0xD
40 #define AD7280A_CONTROL_LB 0xE
41 #define AD7280A_CELL_OVERVOLTAGE 0xF
42 #define AD7280A_CELL_UNDERVOLTAGE 0x10
43 #define AD7280A_AUX_ADC_OVERVOLTAGE 0x11
44 #define AD7280A_AUX_ADC_UNDERVOLTAGE 0x12
45 #define AD7280A_ALERT 0x13
46 #define AD7280A_CELL_BALANCE 0x14
47 #define AD7280A_CB1_TIMER 0x15
48 #define AD7280A_CB2_TIMER 0x16
49 #define AD7280A_CB3_TIMER 0x17
50 #define AD7280A_CB4_TIMER 0x18
51 #define AD7280A_CB5_TIMER 0x19
52 #define AD7280A_CB6_TIMER 0x1A
53 #define AD7280A_PD_TIMER 0x1B
54 #define AD7280A_READ 0x1C
55 #define AD7280A_CNVST_CONTROL 0x1D
58 #define AD7280A_CTRL_HB_CONV_INPUT_ALL (0 << 6)
59 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_4 (1 << 6)
60 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL (2 << 6)
61 #define AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST (3 << 6)
62 #define AD7280A_CTRL_HB_CONV_RES_READ_ALL (0 << 4)
63 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL_AUX1_3_4 (1 << 4)
64 #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL (2 << 4)
65 #define AD7280A_CTRL_HB_CONV_RES_READ_NO (3 << 4)
66 #define AD7280A_CTRL_HB_CONV_START_CNVST (0 << 3)
67 #define AD7280A_CTRL_HB_CONV_START_CS (1 << 3)
68 #define AD7280A_CTRL_HB_CONV_AVG_DIS (0 << 1)
69 #define AD7280A_CTRL_HB_CONV_AVG_2 (1 << 1)
70 #define AD7280A_CTRL_HB_CONV_AVG_4 (2 << 1)
71 #define AD7280A_CTRL_HB_CONV_AVG_8 (3 << 1)
72 #define AD7280A_CTRL_HB_CONV_AVG(x) ((x) << 1)
73 #define AD7280A_CTRL_HB_PWRDN_SW (1 << 0)
75 #define AD7280A_CTRL_LB_SWRST (1 << 7)
76 #define AD7280A_CTRL_LB_ACQ_TIME_400ns (0 << 5)
77 #define AD7280A_CTRL_LB_ACQ_TIME_800ns (1 << 5)
78 #define AD7280A_CTRL_LB_ACQ_TIME_1200ns (2 << 5)
79 #define AD7280A_CTRL_LB_ACQ_TIME_1600ns (3 << 5)
80 #define AD7280A_CTRL_LB_ACQ_TIME(x) ((x) << 5)
81 #define AD7280A_CTRL_LB_MUST_SET (1 << 4)
82 #define AD7280A_CTRL_LB_THERMISTOR_EN (1 << 3)
83 #define AD7280A_CTRL_LB_LOCK_DEV_ADDR (1 << 2)
84 #define AD7280A_CTRL_LB_INC_DEV_ADDR (1 << 1)
85 #define AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN (1 << 0)
87 #define AD7280A_ALERT_GEN_STATIC_HIGH (1 << 6)
88 #define AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN (3 << 6)
90 #define AD7280A_ALL_CELLS (0xAD << 16)
92 #define AD7280A_MAX_SPI_CLK_Hz 700000
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_DEVADDR_MASTER 0
100 #define AD7280A_DEVADDR_ALL 0x1F
102 #define AD7280A_DEVADDR(addr) (((addr & 0x1) << 4) | ((addr & 0x2) << 3) | \
103 (addr & 0x4) | ((addr & 0x8) >> 3) | \
104 ((addr & 0x10) >> 4))
111 #define AD7280A_READ_TXVAL 0xF800030A
119 #define POLYNOM_ORDER 8
120 #define HIGHBIT 1 << (POLYNOM_ORDER - 1);
139 static void ad7280_crc8_build_table(
unsigned char *crc_tab)
144 for (cnt = 0; cnt < 256; cnt++) {
146 for (i = 0; i < 8; i++) {
156 static unsigned char ad7280_calc_crc8(
unsigned char *crc_tab,
unsigned val)
160 crc = crc_tab[val >> 16 & 0xFF];
161 crc = crc_tab[crc ^ (val >> 8 & 0xFF)];
163 return crc ^ (val & 0xFF);
168 unsigned char crc = ad7280_calc_crc8(st->
crc_tab, val >> 10);
170 if (crc != ((val >> 2) & 0xFF))
192 static int __ad7280_read32(
struct spi_device *
spi,
unsigned *val)
204 spi_message_init(&
m);
205 spi_message_add_tail(&t, &
m);
216 static int ad7280_write(
struct ad7280_state *st,
unsigned devaddr,
217 unsigned addr,
bool all,
unsigned val)
219 unsigned reg = (devaddr << 27 | addr << 21 |
220 (val & 0xFF) << 13 | all << 12);
222 reg |= ad7280_calc_crc8(st->
crc_tab, reg >> 11) << 3 | 0x2;
225 return spi_write(st->
spi, ®, 4);
228 static int ad7280_read(
struct ad7280_state *st,
unsigned devaddr,
251 ret = ad7280_write(st, devaddr,
AD7280A_READ, 0, addr << 2);
255 __ad7280_read32(st->
spi, &tmp);
257 if (ad7280_check_crc(st, tmp))
260 if (((tmp >> 27) != devaddr) || (((tmp >> 21) & 0x3F) != addr))
263 return (tmp >> 13) & 0xFF;
266 static int ad7280_read_channel(
struct ad7280_state *st,
unsigned devaddr,
272 ret = ad7280_write(st, devaddr,
AD7280A_READ, 0, addr << 2);
293 __ad7280_read32(st->
spi, &tmp);
295 if (ad7280_check_crc(st, tmp))
298 if (((tmp >> 27) != devaddr) || (((tmp >> 23) & 0xF) != addr))
301 return (tmp >> 11) & 0xFFF;
304 static int ad7280_read_all_channels(
struct ad7280_state *st,
unsigned cnt,
325 for (i = 0; i <
cnt; i++) {
326 __ad7280_read32(st->
spi, &tmp);
328 if (ad7280_check_crc(st, tmp))
335 sum += ((tmp >> 11) & 0xFFF);
369 __ad7280_read32(st->
spi, &val);
373 if (ad7280_check_crc(st, val))
387 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
393 (1 << ((this_attr->
address & 0xFF) + 2))));
401 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
406 unsigned devaddr, ch;
412 devaddr = this_attr->
address >> 8;
413 ch = this_attr->
address & 0xFF;
417 st->
cb_mask[devaddr] |= 1 << (ch + 2);
419 st->
cb_mask[devaddr] &= ~(1 << (ch + 2));
425 return ret ? ret : len;
432 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
439 ret = ad7280_read(st, this_attr->
address >> 8,
446 msecs = (ret >> 3) * 71500;
448 return sprintf(buf,
"%d\n", msecs);
451 static ssize_t ad7280_store_balance_timer(
struct device *dev,
456 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
462 ret = kstrtoul(buf, 10, &val);
472 ret = ad7280_write(st, this_attr->
address >> 8,
474 0, (val & 0x1F) << 3);
477 return ret ? ret : len;
484 .attrs = ad7280_attributes,
496 for (dev = 0, cnt = 0; dev <= st->
slave_num; dev++)
557 for (dev = 0, cnt = 0; dev <= st->
slave_num; dev++)
565 ad7280_show_balance_sw;
567 ad7280_store_balance_sw;
570 "in%d-in%d_balance_switch_en",
572 (dev * AD7280A_CELLS_PER_DEV) + ch + 1);
573 ad7280_attributes[
cnt] =
582 ad7280_show_balance_timer;
584 ad7280_store_balance_timer;
587 (dev * AD7280A_CELLS_PER_DEV) + ch,
588 (dev * AD7280A_CELLS_PER_DEV) + ch + 1);
589 ad7280_attributes[
cnt] =
598 static ssize_t ad7280_read_channel_config(
struct device *dev,
602 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
624 return sprintf(buf,
"%d\n", val);
627 static ssize_t ad7280_write_channel_config(
struct device *dev,
632 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
646 val = ((val - 1000) * 100) / 1568;
650 val = (val * 10) / 196;
656 val =
clamp(val, 0L, 0xFFL);
679 return ret ? ret : len;
682 static irqreturn_t ad7280_event_handler(
int irq,
void *
private)
684 struct iio_dev *indio_dev =
private;
690 if (channels ==
NULL)
693 ret = ad7280_read_all_channels(st, st->
scan_cnt, channels);
697 for (i = 0; i < st->
scan_cnt; i++) {
699 if (((channels[i] >> 11) & 0xFFF) >=
709 else if (((channels[i] >> 11) & 0xFFF) <=
727 else if (((channels[i] >> 11) & 0xFFF) <=
745 in_voltage-voltage_thresh_low_value,
747 ad7280_read_channel_config,
748 ad7280_write_channel_config,
752 in_voltage-voltage_thresh_high_value,
754 ad7280_read_channel_config,
755 ad7280_write_channel_config,
760 ad7280_read_channel_config,
761 ad7280_write_channel_config,
766 ad7280_read_channel_config,
767 ad7280_write_channel_config,
771 static struct attribute *ad7280_event_attributes[] = {
772 &iio_dev_attr_in_thresh_low_value.dev_attr.attr,
773 &iio_dev_attr_in_thresh_high_value.dev_attr.attr,
774 &iio_dev_attr_in_temp_thresh_low_value.dev_attr.attr,
775 &iio_dev_attr_in_temp_thresh_high_value.dev_attr.attr,
780 .attrs = ad7280_event_attributes,
783 static int ad7280_read_raw(
struct iio_dev *indio_dev,
790 unsigned int scale_uv;
799 ret = ad7280_read_channel(st, chan->
address >> 8,
815 *val = scale_uv / 1000;
816 *val2 = (scale_uv % 1000) * 1000;
822 static const struct iio_info ad7280_info = {
823 .read_raw = &ad7280_read_raw,
824 .event_attrs = &ad7280_event_attrs_group,
825 .
attrs = &ad7280_attrs_group,
832 .thermistor_term_en =
true,
840 const unsigned short tACQ_ns[4] = {465, 1010, 1460, 1890};
841 const unsigned short nAVG[4] = {1, 2, 4, 8};
844 if (indio_dev ==
NULL)
847 st = iio_priv(indio_dev);
848 spi_set_drvdata(spi, indio_dev);
852 pdata = &ad7793_default_pdata;
854 ad7280_crc8_build_table(st->
crc_tab);
865 ret = ad7280_chain_setup(st);
867 goto error_free_device;
893 indio_dev->
dev.parent = &spi->
dev;
896 ret = ad7280_channel_init(st);
898 goto error_free_device;
902 indio_dev->
info = &ad7280_info;
904 ret = ad7280_attr_init(st);
906 goto error_free_channels;
910 goto error_free_attr;
917 goto error_unregister;
924 goto error_unregister;
928 ad7280_event_handler,
934 goto error_unregister;
955 struct iio_dev *indio_dev = spi_get_drvdata(spi);
983 .probe = ad7280_probe,
985 .id_table = ad7280_id,