1 // SPDX-License-Identifier: GPL-2.0
3 * AD7280A Lithium Ion Battery Monitoring System
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/cleanup.h>
11 #include <linux/crc8.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/sysfs.h>
22 #include <linux/spi/spi.h>
24 #include <linux/iio/events.h>
25 #include <linux/iio/iio.h>
29 #define AD7280A_CELL_VOLTAGE_1_REG 0x0 /* D11 to D0, Read only */
30 #define AD7280A_CELL_VOLTAGE_2_REG 0x1 /* D11 to D0, Read only */
31 #define AD7280A_CELL_VOLTAGE_3_REG 0x2 /* D11 to D0, Read only */
32 #define AD7280A_CELL_VOLTAGE_4_REG 0x3 /* D11 to D0, Read only */
33 #define AD7280A_CELL_VOLTAGE_5_REG 0x4 /* D11 to D0, Read only */
34 #define AD7280A_CELL_VOLTAGE_6_REG 0x5 /* D11 to D0, Read only */
35 #define AD7280A_AUX_ADC_1_REG 0x6 /* D11 to D0, Read only */
36 #define AD7280A_AUX_ADC_2_REG 0x7 /* D11 to D0, Read only */
37 #define AD7280A_AUX_ADC_3_REG 0x8 /* D11 to D0, Read only */
38 #define AD7280A_AUX_ADC_4_REG 0x9 /* D11 to D0, Read only */
39 #define AD7280A_AUX_ADC_5_REG 0xA /* D11 to D0, Read only */
40 #define AD7280A_AUX_ADC_6_REG 0xB /* D11 to D0, Read only */
41 #define AD7280A_SELF_TEST_REG 0xC /* D11 to D0, Read only */
43 #define AD7280A_CTRL_HB_REG 0xD /* D15 to D8, Read/write */
44 #define AD7280A_CTRL_HB_CONV_INPUT_MSK GENMASK(7, 6)
45 #define AD7280A_CTRL_HB_CONV_INPUT_ALL 0
46 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_5 1
47 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL 2
48 #define AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST 3
49 #define AD7280A_CTRL_HB_CONV_RREAD_MSK GENMASK(5, 4)
50 #define AD7280A_CTRL_HB_CONV_RREAD_ALL 0
51 #define AD7280A_CTRL_HB_CONV_RREAD_6CELL_AUX1_3_5 1
52 #define AD7280A_CTRL_HB_CONV_RREAD_6CELL 2
53 #define AD7280A_CTRL_HB_CONV_RREAD_NO 3
54 #define AD7280A_CTRL_HB_CONV_START_MSK BIT(3)
55 #define AD7280A_CTRL_HB_CONV_START_CNVST 0
56 #define AD7280A_CTRL_HB_CONV_START_CS 1
57 #define AD7280A_CTRL_HB_CONV_AVG_MSK GENMASK(2, 1)
58 #define AD7280A_CTRL_HB_CONV_AVG_DIS 0
59 #define AD7280A_CTRL_HB_CONV_AVG_2 1
60 #define AD7280A_CTRL_HB_CONV_AVG_4 2
61 #define AD7280A_CTRL_HB_CONV_AVG_8 3
62 #define AD7280A_CTRL_HB_PWRDN_SW BIT(0)
64 #define AD7280A_CTRL_LB_REG 0xE /* D7 to D0, Read/write */
65 #define AD7280A_CTRL_LB_SWRST_MSK BIT(7)
66 #define AD7280A_CTRL_LB_ACQ_TIME_MSK GENMASK(6, 5)
67 #define AD7280A_CTRL_LB_ACQ_TIME_400ns 0
68 #define AD7280A_CTRL_LB_ACQ_TIME_800ns 1
69 #define AD7280A_CTRL_LB_ACQ_TIME_1200ns 2
70 #define AD7280A_CTRL_LB_ACQ_TIME_1600ns 3
71 #define AD7280A_CTRL_LB_MUST_SET BIT(4)
72 #define AD7280A_CTRL_LB_THERMISTOR_MSK BIT(3)
73 #define AD7280A_CTRL_LB_LOCK_DEV_ADDR_MSK BIT(2)
74 #define AD7280A_CTRL_LB_INC_DEV_ADDR_MSK BIT(1)
75 #define AD7280A_CTRL_LB_DAISY_CHAIN_RB_MSK BIT(0)
77 #define AD7280A_CELL_OVERVOLTAGE_REG 0xF /* D7 to D0, Read/write */
78 #define AD7280A_CELL_UNDERVOLTAGE_REG 0x10 /* D7 to D0, Read/write */
79 #define AD7280A_AUX_ADC_OVERVOLTAGE_REG 0x11 /* D7 to D0, Read/write */
80 #define AD7280A_AUX_ADC_UNDERVOLTAGE_REG 0x12 /* D7 to D0, Read/write */
82 #define AD7280A_ALERT_REG 0x13 /* D7 to D0, Read/write */
83 #define AD7280A_ALERT_REMOVE_MSK GENMASK(3, 0)
84 #define AD7280A_ALERT_REMOVE_AUX5 BIT(0)
85 #define AD7280A_ALERT_REMOVE_AUX3_AUX5 BIT(1)
86 #define AD7280A_ALERT_REMOVE_VIN5 BIT(2)
87 #define AD7280A_ALERT_REMOVE_VIN4_VIN5 BIT(3)
88 #define AD7280A_ALERT_GEN_STATIC_HIGH BIT(6)
89 #define AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN (BIT(7) | BIT(6))
91 #define AD7280A_CELL_BALANCE_REG 0x14 /* D7 to D0, Read/write */
92 #define AD7280A_CELL_BALANCE_CHAN_BITMAP_MSK GENMASK(7, 2)
93 #define AD7280A_CB1_TIMER_REG 0x15 /* D7 to D0, Read/write */
94 #define AD7280A_CB_TIMER_VAL_MSK GENMASK(7, 3)
95 #define AD7280A_CB2_TIMER_REG 0x16 /* D7 to D0, Read/write */
96 #define AD7280A_CB3_TIMER_REG 0x17 /* D7 to D0, Read/write */
97 #define AD7280A_CB4_TIMER_REG 0x18 /* D7 to D0, Read/write */
98 #define AD7280A_CB5_TIMER_REG 0x19 /* D7 to D0, Read/write */
99 #define AD7280A_CB6_TIMER_REG 0x1A /* D7 to D0, Read/write */
100 #define AD7280A_PD_TIMER_REG 0x1B /* D7 to D0, Read/write */
101 #define AD7280A_READ_REG 0x1C /* D7 to D0, Read/write */
102 #define AD7280A_READ_ADDR_MSK GENMASK(7, 2)
103 #define AD7280A_CNVST_CTRL_REG 0x1D /* D7 to D0, Read/write */
105 /* Transfer fields */
106 #define AD7280A_TRANS_WRITE_DEVADDR_MSK GENMASK(31, 27)
107 #define AD7280A_TRANS_WRITE_ADDR_MSK GENMASK(26, 21)
108 #define AD7280A_TRANS_WRITE_VAL_MSK GENMASK(20, 13)
109 #define AD7280A_TRANS_WRITE_ALL_MSK BIT(12)
110 #define AD7280A_TRANS_WRITE_CRC_MSK GENMASK(10, 3)
111 #define AD7280A_TRANS_WRITE_RES_PATTERN 0x2
113 /* Layouts differ for channel vs other registers */
114 #define AD7280A_TRANS_READ_DEVADDR_MSK GENMASK(31, 27)
115 #define AD7280A_TRANS_READ_CONV_CHANADDR_MSK GENMASK(26, 23)
116 #define AD7280A_TRANS_READ_CONV_DATA_MSK GENMASK(22, 11)
117 #define AD7280A_TRANS_READ_REG_REGADDR_MSK GENMASK(26, 21)
118 #define AD7280A_TRANS_READ_REG_DATA_MSK GENMASK(20, 13)
119 #define AD7280A_TRANS_READ_WRITE_ACK_MSK BIT(10)
120 #define AD7280A_TRANS_READ_CRC_MSK GENMASK(9, 2)
122 /* Magic value used to indicate this special case */
123 #define AD7280A_ALL_CELLS (0xAD << 16)
125 #define AD7280A_MAX_SPI_CLK_HZ 700000 /* < 1MHz */
126 #define AD7280A_MAX_CHAIN 8
127 #define AD7280A_CELLS_PER_DEV 6
128 #define AD7280A_BITS 12
129 #define AD7280A_NUM_CH (AD7280A_AUX_ADC_6_REG - \
130 AD7280A_CELL_VOLTAGE_1_REG + 1)
132 #define AD7280A_CALC_VOLTAGE_CHAN_NUM(d, c) (((d) * AD7280A_CELLS_PER_DEV) + \
134 #define AD7280A_CALC_TEMP_CHAN_NUM(d, c) (((d) * AD7280A_CELLS_PER_DEV) + \
135 (c) - AD7280A_CELLS_PER_DEV)
137 #define AD7280A_DEVADDR_MASTER 0
138 #define AD7280A_DEVADDR_ALL 0x1F
140 static const unsigned short ad7280a_n_avg
[4] = {1, 2, 4, 8};
141 static const unsigned short ad7280a_t_acq_ns
[4] = {470, 1030, 1510, 1945};
143 /* 5-bit device address is sent LSB first */
144 static unsigned int ad7280a_devaddr(unsigned int addr
)
146 return ((addr
& 0x1) << 4) |
147 ((addr
& 0x2) << 2) |
149 ((addr
& 0x8) >> 2) |
150 ((addr
& 0x10) >> 4);
154 * During a read a valid write is mandatory.
155 * So writing to the highest available address (Address 0x1F) and setting the
156 * address all parts bit to 0 is recommended.
157 * So the TXVAL is AD7280A_DEVADDR_ALL + CRC
159 #define AD7280A_READ_TXVAL 0xF800030A
164 * P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 => 0x2F
168 struct ad7280_state
{
169 struct spi_device
*spi
;
170 struct iio_chan_spec
*channels
;
171 unsigned int chain_last_alert_ignore
;
172 bool thermistor_term_en
;
175 int readback_delay_us
;
176 unsigned char crc_tab
[CRC8_TABLE_SIZE
];
177 u8 oversampling_ratio
;
179 unsigned char ctrl_lb
;
180 unsigned char cell_threshhigh
;
181 unsigned char cell_threshlow
;
182 unsigned char aux_threshhigh
;
183 unsigned char aux_threshlow
;
184 unsigned char cb_mask
[AD7280A_MAX_CHAIN
];
185 struct mutex lock
; /* protect sensor state */
187 __be32 tx
__aligned(IIO_DMA_MINALIGN
);
191 static unsigned char ad7280_calc_crc8(unsigned char *crc_tab
, unsigned int val
)
195 crc
= crc_tab
[val
>> 16 & 0xFF];
196 crc
= crc_tab
[crc
^ (val
>> 8 & 0xFF)];
198 return crc
^ (val
& 0xFF);
201 static int ad7280_check_crc(struct ad7280_state
*st
, unsigned int val
)
203 unsigned char crc
= ad7280_calc_crc8(st
->crc_tab
, val
>> 10);
205 if (crc
!= ((val
>> 2) & 0xFF))
212 * After initiating a conversion sequence we need to wait until the conversion
213 * is done. The delay is typically in the range of 15..30us however depending on
214 * the number of devices in the daisy chain, the number of averages taken,
215 * conversion delays and acquisition time options it may take up to 250us, in
216 * this case we better sleep instead of busy wait.
219 static void ad7280_delay(struct ad7280_state
*st
)
221 if (st
->readback_delay_us
< 50)
222 udelay(st
->readback_delay_us
);
224 usleep_range(250, 500);
227 static int __ad7280_read32(struct ad7280_state
*st
, unsigned int *val
)
230 struct spi_transfer t
= {
233 .len
= sizeof(st
->tx
),
236 st
->tx
= cpu_to_be32(AD7280A_READ_TXVAL
);
238 ret
= spi_sync_transfer(st
->spi
, &t
, 1);
242 *val
= be32_to_cpu(st
->rx
);
247 static int ad7280_write(struct ad7280_state
*st
, unsigned int devaddr
,
248 unsigned int addr
, bool all
, unsigned int val
)
250 unsigned int reg
= FIELD_PREP(AD7280A_TRANS_WRITE_DEVADDR_MSK
, devaddr
) |
251 FIELD_PREP(AD7280A_TRANS_WRITE_ADDR_MSK
, addr
) |
252 FIELD_PREP(AD7280A_TRANS_WRITE_VAL_MSK
, val
) |
253 FIELD_PREP(AD7280A_TRANS_WRITE_ALL_MSK
, all
);
255 reg
|= FIELD_PREP(AD7280A_TRANS_WRITE_CRC_MSK
,
256 ad7280_calc_crc8(st
->crc_tab
, reg
>> 11));
257 /* Reserved b010 pattern not included crc calc */
258 reg
|= AD7280A_TRANS_WRITE_RES_PATTERN
;
260 st
->tx
= cpu_to_be32(reg
);
262 return spi_write(st
->spi
, &st
->tx
, sizeof(st
->tx
));
265 static int ad7280_read_reg(struct ad7280_state
*st
, unsigned int devaddr
,
271 /* turns off the read operation on all parts */
272 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_HB_REG
, 1,
273 FIELD_PREP(AD7280A_CTRL_HB_CONV_INPUT_MSK
,
274 AD7280A_CTRL_HB_CONV_INPUT_ALL
) |
275 FIELD_PREP(AD7280A_CTRL_HB_CONV_RREAD_MSK
,
276 AD7280A_CTRL_HB_CONV_RREAD_NO
) |
277 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
,
278 st
->oversampling_ratio
));
282 /* turns on the read operation on the addressed part */
283 ret
= ad7280_write(st
, devaddr
, AD7280A_CTRL_HB_REG
, 0,
284 FIELD_PREP(AD7280A_CTRL_HB_CONV_INPUT_MSK
,
285 AD7280A_CTRL_HB_CONV_INPUT_ALL
) |
286 FIELD_PREP(AD7280A_CTRL_HB_CONV_RREAD_MSK
,
287 AD7280A_CTRL_HB_CONV_RREAD_ALL
) |
288 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
,
289 st
->oversampling_ratio
));
293 /* Set register address on the part to be read from */
294 ret
= ad7280_write(st
, devaddr
, AD7280A_READ_REG
, 0,
295 FIELD_PREP(AD7280A_READ_ADDR_MSK
, addr
));
299 ret
= __ad7280_read32(st
, &tmp
);
303 if (ad7280_check_crc(st
, tmp
))
306 if ((FIELD_GET(AD7280A_TRANS_READ_DEVADDR_MSK
, tmp
) != devaddr
) ||
307 (FIELD_GET(AD7280A_TRANS_READ_REG_REGADDR_MSK
, tmp
) != addr
))
310 return FIELD_GET(AD7280A_TRANS_READ_REG_DATA_MSK
, tmp
);
313 static int ad7280_read_channel(struct ad7280_state
*st
, unsigned int devaddr
,
319 ret
= ad7280_write(st
, devaddr
, AD7280A_READ_REG
, 0,
320 FIELD_PREP(AD7280A_READ_ADDR_MSK
, addr
));
324 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_HB_REG
, 1,
325 FIELD_PREP(AD7280A_CTRL_HB_CONV_INPUT_MSK
,
326 AD7280A_CTRL_HB_CONV_INPUT_ALL
) |
327 FIELD_PREP(AD7280A_CTRL_HB_CONV_RREAD_MSK
,
328 AD7280A_CTRL_HB_CONV_RREAD_NO
) |
329 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
,
330 st
->oversampling_ratio
));
334 ret
= ad7280_write(st
, devaddr
, AD7280A_CTRL_HB_REG
, 0,
335 FIELD_PREP(AD7280A_CTRL_HB_CONV_INPUT_MSK
,
336 AD7280A_CTRL_HB_CONV_INPUT_ALL
) |
337 FIELD_PREP(AD7280A_CTRL_HB_CONV_RREAD_MSK
,
338 AD7280A_CTRL_HB_CONV_RREAD_ALL
) |
339 FIELD_PREP(AD7280A_CTRL_HB_CONV_START_MSK
,
340 AD7280A_CTRL_HB_CONV_START_CS
) |
341 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
,
342 st
->oversampling_ratio
));
348 ret
= __ad7280_read32(st
, &tmp
);
352 if (ad7280_check_crc(st
, tmp
))
355 if ((FIELD_GET(AD7280A_TRANS_READ_DEVADDR_MSK
, tmp
) != devaddr
) ||
356 (FIELD_GET(AD7280A_TRANS_READ_CONV_CHANADDR_MSK
, tmp
) != addr
))
359 return FIELD_GET(AD7280A_TRANS_READ_CONV_DATA_MSK
, tmp
);
362 static int ad7280_read_all_channels(struct ad7280_state
*st
, unsigned int cnt
,
366 unsigned int tmp
, sum
= 0;
368 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_READ_REG
, 1,
369 AD7280A_CELL_VOLTAGE_1_REG
<< 2);
373 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_HB_REG
, 1,
374 FIELD_PREP(AD7280A_CTRL_HB_CONV_INPUT_MSK
,
375 AD7280A_CTRL_HB_CONV_INPUT_ALL
) |
376 FIELD_PREP(AD7280A_CTRL_HB_CONV_RREAD_MSK
,
377 AD7280A_CTRL_HB_CONV_RREAD_ALL
) |
378 FIELD_PREP(AD7280A_CTRL_HB_CONV_START_MSK
,
379 AD7280A_CTRL_HB_CONV_START_CS
) |
380 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
,
381 st
->oversampling_ratio
));
387 for (i
= 0; i
< cnt
; i
++) {
388 ret
= __ad7280_read32(st
, &tmp
);
392 if (ad7280_check_crc(st
, tmp
))
397 /* only sum cell voltages */
398 if (FIELD_GET(AD7280A_TRANS_READ_CONV_CHANADDR_MSK
, tmp
) <=
399 AD7280A_CELL_VOLTAGE_6_REG
)
400 sum
+= FIELD_GET(AD7280A_TRANS_READ_CONV_DATA_MSK
, tmp
);
406 static void ad7280_sw_power_down(void *data
)
408 struct ad7280_state
*st
= data
;
410 ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_HB_REG
, 1,
411 AD7280A_CTRL_HB_PWRDN_SW
|
412 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
, st
->oversampling_ratio
));
415 static int ad7280_chain_setup(struct ad7280_state
*st
)
420 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_LB_REG
, 1,
421 FIELD_PREP(AD7280A_CTRL_LB_DAISY_CHAIN_RB_MSK
, 1) |
422 FIELD_PREP(AD7280A_CTRL_LB_LOCK_DEV_ADDR_MSK
, 1) |
423 AD7280A_CTRL_LB_MUST_SET
|
424 FIELD_PREP(AD7280A_CTRL_LB_SWRST_MSK
, 1) |
429 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_LB_REG
, 1,
430 FIELD_PREP(AD7280A_CTRL_LB_DAISY_CHAIN_RB_MSK
, 1) |
431 FIELD_PREP(AD7280A_CTRL_LB_LOCK_DEV_ADDR_MSK
, 1) |
432 AD7280A_CTRL_LB_MUST_SET
|
433 FIELD_PREP(AD7280A_CTRL_LB_SWRST_MSK
, 0) |
436 goto error_power_down
;
438 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_READ_REG
, 1,
439 FIELD_PREP(AD7280A_READ_ADDR_MSK
, AD7280A_CTRL_LB_REG
));
441 goto error_power_down
;
443 for (n
= 0; n
<= AD7280A_MAX_CHAIN
; n
++) {
444 ret
= __ad7280_read32(st
, &val
);
446 goto error_power_down
;
451 if (ad7280_check_crc(st
, val
)) {
453 goto error_power_down
;
456 if (n
!= ad7280a_devaddr(FIELD_GET(AD7280A_TRANS_READ_DEVADDR_MSK
, val
))) {
458 goto error_power_down
;
464 ad7280_write(st
, AD7280A_DEVADDR_MASTER
, AD7280A_CTRL_HB_REG
, 1,
465 AD7280A_CTRL_HB_PWRDN_SW
|
466 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK
, st
->oversampling_ratio
));
471 static ssize_t
ad7280_show_balance_sw(struct iio_dev
*indio_dev
,
473 const struct iio_chan_spec
*chan
, char *buf
)
475 struct ad7280_state
*st
= iio_priv(indio_dev
);
477 return sysfs_emit(buf
, "%d\n",
478 !!(st
->cb_mask
[chan
->address
>> 8] &
479 BIT(chan
->address
& 0xFF)));
482 static ssize_t
ad7280_store_balance_sw(struct iio_dev
*indio_dev
,
484 const struct iio_chan_spec
*chan
,
485 const char *buf
, size_t len
)
487 struct ad7280_state
*st
= iio_priv(indio_dev
);
488 unsigned int devaddr
, ch
;
492 ret
= kstrtobool(buf
, &readin
);
496 devaddr
= chan
->address
>> 8;
497 ch
= chan
->address
& 0xFF;
499 mutex_lock(&st
->lock
);
501 st
->cb_mask
[devaddr
] |= BIT(ch
);
503 st
->cb_mask
[devaddr
] &= ~BIT(ch
);
505 ret
= ad7280_write(st
, devaddr
, AD7280A_CELL_BALANCE_REG
, 0,
506 FIELD_PREP(AD7280A_CELL_BALANCE_CHAN_BITMAP_MSK
,
507 st
->cb_mask
[devaddr
]));
508 mutex_unlock(&st
->lock
);
510 return ret
? ret
: len
;
513 static ssize_t
ad7280_show_balance_timer(struct iio_dev
*indio_dev
,
515 const struct iio_chan_spec
*chan
,
518 struct ad7280_state
*st
= iio_priv(indio_dev
);
522 mutex_lock(&st
->lock
);
523 ret
= ad7280_read_reg(st
, chan
->address
>> 8,
524 (chan
->address
& 0xFF) + AD7280A_CB1_TIMER_REG
);
525 mutex_unlock(&st
->lock
);
530 msecs
= FIELD_GET(AD7280A_CB_TIMER_VAL_MSK
, ret
) * 71500;
532 return sysfs_emit(buf
, "%u.%u\n", msecs
/ 1000, msecs
% 1000);
535 static ssize_t
ad7280_store_balance_timer(struct iio_dev
*indio_dev
,
537 const struct iio_chan_spec
*chan
,
538 const char *buf
, size_t len
)
540 struct ad7280_state
*st
= iio_priv(indio_dev
);
544 ret
= iio_str_to_fixpoint(buf
, 1000, &val
, &val2
);
548 val
= val
* 1000 + val2
;
554 mutex_lock(&st
->lock
);
555 ret
= ad7280_write(st
, chan
->address
>> 8,
556 (chan
->address
& 0xFF) + AD7280A_CB1_TIMER_REG
, 0,
557 FIELD_PREP(AD7280A_CB_TIMER_VAL_MSK
, val
));
558 mutex_unlock(&st
->lock
);
560 return ret
? ret
: len
;
563 static const struct iio_chan_spec_ext_info ad7280_cell_ext_info
[] = {
565 .name
= "balance_switch_en",
566 .read
= ad7280_show_balance_sw
,
567 .write
= ad7280_store_balance_sw
,
568 .shared
= IIO_SEPARATE
,
570 .name
= "balance_switch_timer",
571 .read
= ad7280_show_balance_timer
,
572 .write
= ad7280_store_balance_timer
,
573 .shared
= IIO_SEPARATE
,
578 static const struct iio_event_spec ad7280_events
[] = {
580 .type
= IIO_EV_TYPE_THRESH
,
581 .dir
= IIO_EV_DIR_RISING
,
582 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
584 .type
= IIO_EV_TYPE_THRESH
,
585 .dir
= IIO_EV_DIR_FALLING
,
586 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
590 static void ad7280_voltage_channel_init(struct iio_chan_spec
*chan
, int i
,
593 chan
->type
= IIO_VOLTAGE
;
594 chan
->differential
= 1;
596 chan
->channel2
= chan
->channel
+ 1;
598 chan
->event_spec
= ad7280_events
;
599 chan
->num_event_specs
= ARRAY_SIZE(ad7280_events
);
601 chan
->ext_info
= ad7280_cell_ext_info
;
604 static void ad7280_temp_channel_init(struct iio_chan_spec
*chan
, int i
,
607 chan
->type
= IIO_TEMP
;
610 chan
->event_spec
= ad7280_events
;
611 chan
->num_event_specs
= ARRAY_SIZE(ad7280_events
);
615 static void ad7280_common_fields_init(struct iio_chan_spec
*chan
, int addr
,
619 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
620 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
);
621 chan
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
);
622 chan
->address
= addr
;
623 chan
->scan_index
= cnt
;
624 chan
->scan_type
.sign
= 'u';
625 chan
->scan_type
.realbits
= 12;
626 chan
->scan_type
.storagebits
= 32;
629 static void ad7280_total_voltage_channel_init(struct iio_chan_spec
*chan
,
632 chan
->type
= IIO_VOLTAGE
;
633 chan
->differential
= 1;
635 chan
->channel2
= dev
* AD7280A_CELLS_PER_DEV
;
636 chan
->address
= AD7280A_ALL_CELLS
;
638 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
639 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
);
640 chan
->scan_index
= cnt
;
641 chan
->scan_type
.sign
= 'u';
642 chan
->scan_type
.realbits
= 32;
643 chan
->scan_type
.storagebits
= 32;
646 static void ad7280_init_dev_channels(struct ad7280_state
*st
, int dev
, int *cnt
,
650 struct iio_chan_spec
*chan
;
652 for (ch
= AD7280A_CELL_VOLTAGE_1_REG
; ch
<= AD7280A_AUX_ADC_6_REG
; ch
++) {
653 chan
= &st
->channels
[*cnt
];
655 if (ch
< AD7280A_AUX_ADC_1_REG
) {
656 i
= AD7280A_CALC_VOLTAGE_CHAN_NUM(dev
, ch
);
657 ad7280_voltage_channel_init(chan
, i
, irq_present
);
659 i
= AD7280A_CALC_TEMP_CHAN_NUM(dev
, ch
);
660 ad7280_temp_channel_init(chan
, i
, irq_present
);
663 addr
= ad7280a_devaddr(dev
) << 8 | ch
;
664 ad7280_common_fields_init(chan
, addr
, *cnt
);
670 static int ad7280_channel_init(struct ad7280_state
*st
, bool irq_present
)
674 st
->channels
= devm_kcalloc(&st
->spi
->dev
, (st
->slave_num
+ 1) * 12 + 1,
675 sizeof(*st
->channels
), GFP_KERNEL
);
679 for (dev
= 0; dev
<= st
->slave_num
; dev
++)
680 ad7280_init_dev_channels(st
, dev
, &cnt
, irq_present
);
682 ad7280_total_voltage_channel_init(&st
->channels
[cnt
], cnt
, dev
);
687 static int ad7280a_read_thresh(struct iio_dev
*indio_dev
,
688 const struct iio_chan_spec
*chan
,
689 enum iio_event_type type
,
690 enum iio_event_direction dir
,
691 enum iio_event_info info
, int *val
, int *val2
)
693 struct ad7280_state
*st
= iio_priv(indio_dev
);
695 switch (chan
->type
) {
698 case IIO_EV_DIR_RISING
:
699 *val
= 1000 + (st
->cell_threshhigh
* 1568L) / 100;
701 case IIO_EV_DIR_FALLING
:
702 *val
= 1000 + (st
->cell_threshlow
* 1568L) / 100;
710 case IIO_EV_DIR_RISING
:
711 *val
= ((st
->aux_threshhigh
) * 196L) / 10;
713 case IIO_EV_DIR_FALLING
:
714 *val
= (st
->aux_threshlow
* 196L) / 10;
725 static int ad7280a_write_thresh(struct iio_dev
*indio_dev
,
726 const struct iio_chan_spec
*chan
,
727 enum iio_event_type type
,
728 enum iio_event_direction dir
,
729 enum iio_event_info info
,
732 struct ad7280_state
*st
= iio_priv(indio_dev
);
740 mutex_lock(&st
->lock
);
741 switch (chan
->type
) {
743 value
= ((val
- 1000) * 100) / 1568; /* LSB 15.68mV */
744 value
= clamp(value
, 0L, 0xFFL
);
746 case IIO_EV_DIR_RISING
:
747 addr
= AD7280A_CELL_OVERVOLTAGE_REG
;
748 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, addr
,
752 st
->cell_threshhigh
= value
;
754 case IIO_EV_DIR_FALLING
:
755 addr
= AD7280A_CELL_UNDERVOLTAGE_REG
;
756 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, addr
,
760 st
->cell_threshlow
= value
;
768 value
= (val
* 10) / 196; /* LSB 19.6mV */
769 value
= clamp(value
, 0L, 0xFFL
);
771 case IIO_EV_DIR_RISING
:
772 addr
= AD7280A_AUX_ADC_OVERVOLTAGE_REG
;
773 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, addr
,
777 st
->aux_threshhigh
= value
;
779 case IIO_EV_DIR_FALLING
:
780 addr
= AD7280A_AUX_ADC_UNDERVOLTAGE_REG
;
781 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
, addr
,
785 st
->aux_threshlow
= value
;
798 mutex_unlock(&st
->lock
);
803 static irqreturn_t
ad7280_event_handler(int irq
, void *private)
805 struct iio_dev
*indio_dev
= private;
806 struct ad7280_state
*st
= iio_priv(indio_dev
);
809 unsigned int *channels
__free(kfree
) = kcalloc(st
->scan_cnt
, sizeof(*channels
),
814 ret
= ad7280_read_all_channels(st
, st
->scan_cnt
, channels
);
818 for (i
= 0; i
< st
->scan_cnt
; i
++) {
821 val
= FIELD_GET(AD7280A_TRANS_READ_CONV_DATA_MSK
, channels
[i
]);
822 if (FIELD_GET(AD7280A_TRANS_READ_CONV_CHANADDR_MSK
, channels
[i
]) <=
823 AD7280A_CELL_VOLTAGE_6_REG
) {
824 if (val
>= st
->cell_threshhigh
) {
825 u64 tmp
= IIO_EVENT_CODE(IIO_VOLTAGE
, 1, 0,
829 iio_push_event(indio_dev
, tmp
,
830 iio_get_time_ns(indio_dev
));
831 } else if (val
<= st
->cell_threshlow
) {
832 u64 tmp
= IIO_EVENT_CODE(IIO_VOLTAGE
, 1, 0,
836 iio_push_event(indio_dev
, tmp
,
837 iio_get_time_ns(indio_dev
));
840 if (val
>= st
->aux_threshhigh
) {
841 u64 tmp
= IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
844 iio_push_event(indio_dev
, tmp
,
845 iio_get_time_ns(indio_dev
));
846 } else if (val
<= st
->aux_threshlow
) {
847 u64 tmp
= IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
850 iio_push_event(indio_dev
, tmp
,
851 iio_get_time_ns(indio_dev
));
859 static void ad7280_update_delay(struct ad7280_state
*st
)
862 * Total Conversion Time = ((tACQ + tCONV) *
863 * (Number of Conversions per Part)) −
864 * tACQ + ((N - 1) * tDELAY)
866 * Readback Delay = Total Conversion Time + tWAIT
869 st
->readback_delay_us
=
870 ((ad7280a_t_acq_ns
[st
->acquisition_time
& 0x3] + 720) *
871 (AD7280A_NUM_CH
* ad7280a_n_avg
[st
->oversampling_ratio
& 0x3])) -
872 ad7280a_t_acq_ns
[st
->acquisition_time
& 0x3] + st
->slave_num
* 250;
874 /* Convert to usecs */
875 st
->readback_delay_us
= DIV_ROUND_UP(st
->readback_delay_us
, 1000);
876 st
->readback_delay_us
+= 5; /* Add tWAIT */
879 static int ad7280_read_raw(struct iio_dev
*indio_dev
,
880 struct iio_chan_spec
const *chan
,
885 struct ad7280_state
*st
= iio_priv(indio_dev
);
889 case IIO_CHAN_INFO_RAW
:
890 mutex_lock(&st
->lock
);
891 if (chan
->address
== AD7280A_ALL_CELLS
)
892 ret
= ad7280_read_all_channels(st
, st
->scan_cnt
, NULL
);
894 ret
= ad7280_read_channel(st
, chan
->address
>> 8,
895 chan
->address
& 0xFF);
896 mutex_unlock(&st
->lock
);
904 case IIO_CHAN_INFO_SCALE
:
905 if ((chan
->address
& 0xFF) <= AD7280A_CELL_VOLTAGE_6_REG
)
910 *val2
= AD7280A_BITS
;
911 return IIO_VAL_FRACTIONAL_LOG2
;
912 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
913 *val
= ad7280a_n_avg
[st
->oversampling_ratio
];
919 static int ad7280_write_raw(struct iio_dev
*indio_dev
,
920 struct iio_chan_spec
const *chan
,
921 int val
, int val2
, long mask
)
923 struct ad7280_state
*st
= iio_priv(indio_dev
);
927 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
930 for (i
= 0; i
< ARRAY_SIZE(ad7280a_n_avg
); i
++) {
931 if (val
== ad7280a_n_avg
[i
]) {
932 st
->oversampling_ratio
= i
;
933 ad7280_update_delay(st
);
943 static const struct iio_info ad7280_info
= {
944 .read_raw
= ad7280_read_raw
,
945 .write_raw
= ad7280_write_raw
,
946 .read_event_value
= &ad7280a_read_thresh
,
947 .write_event_value
= &ad7280a_write_thresh
,
950 static const struct iio_info ad7280_info_no_irq
= {
951 .read_raw
= ad7280_read_raw
,
952 .write_raw
= ad7280_write_raw
,
955 static int ad7280_probe(struct spi_device
*spi
)
957 struct device
*dev
= &spi
->dev
;
958 struct ad7280_state
*st
;
960 struct iio_dev
*indio_dev
;
962 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
966 st
= iio_priv(indio_dev
);
967 spi_set_drvdata(spi
, indio_dev
);
969 mutex_init(&st
->lock
);
971 st
->thermistor_term_en
=
972 device_property_read_bool(dev
, "adi,thermistor-termination");
974 if (device_property_present(dev
, "adi,acquisition-time-ns")) {
977 ret
= device_property_read_u32(dev
, "adi,acquisition-time-ns", &val
);
983 st
->acquisition_time
= AD7280A_CTRL_LB_ACQ_TIME_400ns
;
986 st
->acquisition_time
= AD7280A_CTRL_LB_ACQ_TIME_800ns
;
989 st
->acquisition_time
= AD7280A_CTRL_LB_ACQ_TIME_1200ns
;
992 st
->acquisition_time
= AD7280A_CTRL_LB_ACQ_TIME_1600ns
;
995 dev_err(dev
, "Firmware provided acquisition time is invalid\n");
999 st
->acquisition_time
= AD7280A_CTRL_LB_ACQ_TIME_400ns
;
1002 /* Alert masks are intended for when particular inputs are not wired up */
1003 if (device_property_present(dev
, "adi,voltage-alert-last-chan")) {
1006 ret
= device_property_read_u32(dev
, "adi,voltage-alert-last-chan", &val
);
1012 st
->chain_last_alert_ignore
|= AD7280A_ALERT_REMOVE_VIN4_VIN5
;
1015 st
->chain_last_alert_ignore
|= AD7280A_ALERT_REMOVE_VIN5
;
1021 "Firmware provided last voltage alert channel invalid\n");
1025 crc8_populate_msb(st
->crc_tab
, POLYNOM
);
1027 st
->spi
->max_speed_hz
= AD7280A_MAX_SPI_CLK_HZ
;
1028 st
->spi
->mode
= SPI_MODE_1
;
1031 st
->ctrl_lb
= FIELD_PREP(AD7280A_CTRL_LB_ACQ_TIME_MSK
, st
->acquisition_time
) |
1032 FIELD_PREP(AD7280A_CTRL_LB_THERMISTOR_MSK
, st
->thermistor_term_en
);
1033 st
->oversampling_ratio
= 0; /* No oversampling */
1035 ret
= ad7280_chain_setup(st
);
1039 st
->slave_num
= ret
;
1040 st
->scan_cnt
= (st
->slave_num
+ 1) * AD7280A_NUM_CH
;
1041 st
->cell_threshhigh
= 0xFF;
1042 st
->aux_threshhigh
= 0xFF;
1044 ret
= devm_add_action_or_reset(dev
, ad7280_sw_power_down
, st
);
1048 ad7280_update_delay(st
);
1050 indio_dev
->name
= spi_get_device_id(spi
)->name
;
1051 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1053 ret
= ad7280_channel_init(st
, spi
->irq
> 0);
1057 indio_dev
->num_channels
= ret
;
1058 indio_dev
->channels
= st
->channels
;
1060 ret
= ad7280_write(st
, AD7280A_DEVADDR_MASTER
,
1061 AD7280A_ALERT_REG
, 1,
1062 AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN
);
1066 ret
= ad7280_write(st
, ad7280a_devaddr(st
->slave_num
),
1067 AD7280A_ALERT_REG
, 0,
1068 AD7280A_ALERT_GEN_STATIC_HIGH
|
1069 FIELD_PREP(AD7280A_ALERT_REMOVE_MSK
,
1070 st
->chain_last_alert_ignore
));
1074 ret
= devm_request_threaded_irq(dev
, spi
->irq
,
1076 ad7280_event_handler
,
1077 IRQF_TRIGGER_FALLING
|
1084 indio_dev
->info
= &ad7280_info
;
1086 indio_dev
->info
= &ad7280_info_no_irq
;
1089 return devm_iio_device_register(dev
, indio_dev
);
1092 static const struct spi_device_id ad7280_id
[] = {
1096 MODULE_DEVICE_TABLE(spi
, ad7280_id
);
1098 static struct spi_driver ad7280_driver
= {
1102 .probe
= ad7280_probe
,
1103 .id_table
= ad7280_id
,
1105 module_spi_driver(ad7280_driver
);
1107 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
1108 MODULE_DESCRIPTION("Analog Devices AD7280A");
1109 MODULE_LICENSE("GPL v2");