Lines Matching +full:1 +full:st
46 #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_5 1
51 #define AD7280A_CTRL_HB_CONV_RREAD_6CELL_AUX1_3_5 1
56 #define AD7280A_CTRL_HB_CONV_START_CS 1
57 #define AD7280A_CTRL_HB_CONV_AVG_MSK GENMASK(2, 1)
59 #define AD7280A_CTRL_HB_CONV_AVG_2 1
68 #define AD7280A_CTRL_LB_ACQ_TIME_800ns 1
74 #define AD7280A_CTRL_LB_INC_DEV_ADDR_MSK BIT(1)
85 #define AD7280A_ALERT_REMOVE_AUX3_AUX5 BIT(1)
125 #define AD7280A_MAX_SPI_CLK_HZ 700000 /* < 1MHz */
130 AD7280A_CELL_VOLTAGE_1_REG + 1)
140 static const unsigned short ad7280a_n_avg[4] = {1, 2, 4, 8};
164 * P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 => 0x2F
201 static int ad7280_check_crc(struct ad7280_state *st, unsigned int val) in ad7280_check_crc() argument
203 unsigned char crc = ad7280_calc_crc8(st->crc_tab, val >> 10); in ad7280_check_crc()
219 static void ad7280_delay(struct ad7280_state *st) in ad7280_delay() argument
221 if (st->readback_delay_us < 50) in ad7280_delay()
222 udelay(st->readback_delay_us); in ad7280_delay()
227 static int __ad7280_read32(struct ad7280_state *st, unsigned int *val) in __ad7280_read32() argument
231 .tx_buf = &st->tx, in __ad7280_read32()
232 .rx_buf = &st->rx, in __ad7280_read32()
233 .len = sizeof(st->tx), in __ad7280_read32()
236 st->tx = cpu_to_be32(AD7280A_READ_TXVAL); in __ad7280_read32()
238 ret = spi_sync_transfer(st->spi, &t, 1); in __ad7280_read32()
242 *val = be32_to_cpu(st->rx); in __ad7280_read32()
247 static int ad7280_write(struct ad7280_state *st, unsigned int devaddr, in ad7280_write() argument
256 ad7280_calc_crc8(st->crc_tab, reg >> 11)); in ad7280_write()
260 st->tx = cpu_to_be32(reg); in ad7280_write()
262 return spi_write(st->spi, &st->tx, sizeof(st->tx)); in ad7280_write()
265 static int ad7280_read_reg(struct ad7280_state *st, unsigned int devaddr, in ad7280_read_reg() argument
272 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_HB_REG, 1, in ad7280_read_reg()
278 st->oversampling_ratio)); in ad7280_read_reg()
283 ret = ad7280_write(st, devaddr, AD7280A_CTRL_HB_REG, 0, in ad7280_read_reg()
289 st->oversampling_ratio)); in ad7280_read_reg()
294 ret = ad7280_write(st, devaddr, AD7280A_READ_REG, 0, in ad7280_read_reg()
299 ret = __ad7280_read32(st, &tmp); in ad7280_read_reg()
303 if (ad7280_check_crc(st, tmp)) in ad7280_read_reg()
313 static int ad7280_read_channel(struct ad7280_state *st, unsigned int devaddr, in ad7280_read_channel() argument
319 ret = ad7280_write(st, devaddr, AD7280A_READ_REG, 0, in ad7280_read_channel()
324 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_HB_REG, 1, in ad7280_read_channel()
330 st->oversampling_ratio)); in ad7280_read_channel()
334 ret = ad7280_write(st, devaddr, AD7280A_CTRL_HB_REG, 0, in ad7280_read_channel()
342 st->oversampling_ratio)); in ad7280_read_channel()
346 ad7280_delay(st); in ad7280_read_channel()
348 ret = __ad7280_read32(st, &tmp); in ad7280_read_channel()
352 if (ad7280_check_crc(st, tmp)) in ad7280_read_channel()
362 static int ad7280_read_all_channels(struct ad7280_state *st, unsigned int cnt, in ad7280_read_all_channels() argument
368 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_READ_REG, 1, in ad7280_read_all_channels()
373 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_HB_REG, 1, in ad7280_read_all_channels()
381 st->oversampling_ratio)); in ad7280_read_all_channels()
385 ad7280_delay(st); in ad7280_read_all_channels()
388 ret = __ad7280_read32(st, &tmp); in ad7280_read_all_channels()
392 if (ad7280_check_crc(st, tmp)) in ad7280_read_all_channels()
408 struct ad7280_state *st = data; in ad7280_sw_power_down() local
410 ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_HB_REG, 1, in ad7280_sw_power_down()
412 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK, st->oversampling_ratio)); in ad7280_sw_power_down()
415 static int ad7280_chain_setup(struct ad7280_state *st) in ad7280_chain_setup() argument
420 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_LB_REG, 1, in ad7280_chain_setup()
421 FIELD_PREP(AD7280A_CTRL_LB_DAISY_CHAIN_RB_MSK, 1) | in ad7280_chain_setup()
422 FIELD_PREP(AD7280A_CTRL_LB_LOCK_DEV_ADDR_MSK, 1) | in ad7280_chain_setup()
424 FIELD_PREP(AD7280A_CTRL_LB_SWRST_MSK, 1) | in ad7280_chain_setup()
425 st->ctrl_lb); in ad7280_chain_setup()
429 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_LB_REG, 1, in ad7280_chain_setup()
430 FIELD_PREP(AD7280A_CTRL_LB_DAISY_CHAIN_RB_MSK, 1) | in ad7280_chain_setup()
431 FIELD_PREP(AD7280A_CTRL_LB_LOCK_DEV_ADDR_MSK, 1) | in ad7280_chain_setup()
434 st->ctrl_lb); in ad7280_chain_setup()
438 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_READ_REG, 1, in ad7280_chain_setup()
444 ret = __ad7280_read32(st, &val); in ad7280_chain_setup()
449 return n - 1; in ad7280_chain_setup()
451 if (ad7280_check_crc(st, val)) { in ad7280_chain_setup()
464 ad7280_write(st, AD7280A_DEVADDR_MASTER, AD7280A_CTRL_HB_REG, 1, in ad7280_chain_setup()
466 FIELD_PREP(AD7280A_CTRL_HB_CONV_AVG_MSK, st->oversampling_ratio)); in ad7280_chain_setup()
475 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_show_balance_sw() local
478 !!(st->cb_mask[chan->address >> 8] & in ad7280_show_balance_sw()
487 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_store_balance_sw() local
499 mutex_lock(&st->lock); in ad7280_store_balance_sw()
501 st->cb_mask[devaddr] |= BIT(ch); in ad7280_store_balance_sw()
503 st->cb_mask[devaddr] &= ~BIT(ch); in ad7280_store_balance_sw()
505 ret = ad7280_write(st, devaddr, AD7280A_CELL_BALANCE_REG, 0, in ad7280_store_balance_sw()
507 st->cb_mask[devaddr])); in ad7280_store_balance_sw()
508 mutex_unlock(&st->lock); in ad7280_store_balance_sw()
518 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_show_balance_timer() local
522 mutex_lock(&st->lock); in ad7280_show_balance_timer()
523 ret = ad7280_read_reg(st, chan->address >> 8, in ad7280_show_balance_timer()
525 mutex_unlock(&st->lock); in ad7280_show_balance_timer()
540 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_store_balance_timer() local
554 mutex_lock(&st->lock); in ad7280_store_balance_timer()
555 ret = ad7280_write(st, chan->address >> 8, in ad7280_store_balance_timer()
558 mutex_unlock(&st->lock); in ad7280_store_balance_timer()
594 chan->differential = 1; in ad7280_voltage_channel_init()
596 chan->channel2 = chan->channel + 1; in ad7280_voltage_channel_init()
618 chan->indexed = 1; in ad7280_common_fields_init()
633 chan->differential = 1; in ad7280_total_voltage_channel_init()
637 chan->indexed = 1; in ad7280_total_voltage_channel_init()
646 static void ad7280_init_dev_channels(struct ad7280_state *st, int dev, int *cnt, in ad7280_init_dev_channels() argument
653 chan = &st->channels[*cnt]; in ad7280_init_dev_channels()
670 static int ad7280_channel_init(struct ad7280_state *st, bool irq_present) in ad7280_channel_init() argument
674 st->channels = devm_kcalloc(&st->spi->dev, (st->slave_num + 1) * 12 + 1, in ad7280_channel_init()
675 sizeof(*st->channels), GFP_KERNEL); in ad7280_channel_init()
676 if (!st->channels) in ad7280_channel_init()
679 for (dev = 0; dev <= st->slave_num; dev++) in ad7280_channel_init()
680 ad7280_init_dev_channels(st, dev, &cnt, irq_present); in ad7280_channel_init()
682 ad7280_total_voltage_channel_init(&st->channels[cnt], cnt, dev); in ad7280_channel_init()
684 return cnt + 1; in ad7280_channel_init()
693 struct ad7280_state *st = iio_priv(indio_dev); in ad7280a_read_thresh() local
699 *val = 1000 + (st->cell_threshhigh * 1568L) / 100; in ad7280a_read_thresh()
702 *val = 1000 + (st->cell_threshlow * 1568L) / 100; in ad7280a_read_thresh()
711 *val = ((st->aux_threshhigh) * 196L) / 10; in ad7280a_read_thresh()
714 *val = (st->aux_threshlow * 196L) / 10; in ad7280a_read_thresh()
732 struct ad7280_state *st = iio_priv(indio_dev); in ad7280a_write_thresh() local
740 mutex_lock(&st->lock); in ad7280a_write_thresh()
748 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr, in ad7280a_write_thresh()
749 1, value); in ad7280a_write_thresh()
752 st->cell_threshhigh = value; in ad7280a_write_thresh()
756 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr, in ad7280a_write_thresh()
757 1, value); in ad7280a_write_thresh()
760 st->cell_threshlow = value; in ad7280a_write_thresh()
773 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr, in ad7280a_write_thresh()
774 1, value); in ad7280a_write_thresh()
777 st->aux_threshhigh = value; in ad7280a_write_thresh()
781 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, addr, in ad7280a_write_thresh()
782 1, value); in ad7280a_write_thresh()
785 st->aux_threshlow = value; in ad7280a_write_thresh()
798 mutex_unlock(&st->lock); in ad7280a_write_thresh()
806 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_event_handler() local
809 unsigned int *channels __free(kfree) = kcalloc(st->scan_cnt, sizeof(*channels), in ad7280_event_handler()
814 ret = ad7280_read_all_channels(st, st->scan_cnt, channels); in ad7280_event_handler()
818 for (i = 0; i < st->scan_cnt; i++) { in ad7280_event_handler()
824 if (val >= st->cell_threshhigh) { in ad7280_event_handler()
825 u64 tmp = IIO_EVENT_CODE(IIO_VOLTAGE, 1, 0, in ad7280_event_handler()
831 } else if (val <= st->cell_threshlow) { in ad7280_event_handler()
832 u64 tmp = IIO_EVENT_CODE(IIO_VOLTAGE, 1, 0, in ad7280_event_handler()
840 if (val >= st->aux_threshhigh) { in ad7280_event_handler()
846 } else if (val <= st->aux_threshlow) { in ad7280_event_handler()
859 static void ad7280_update_delay(struct ad7280_state *st) in ad7280_update_delay() argument
864 * tACQ + ((N - 1) * tDELAY) in ad7280_update_delay()
869 st->readback_delay_us = in ad7280_update_delay()
870 ((ad7280a_t_acq_ns[st->acquisition_time & 0x3] + 720) * in ad7280_update_delay()
871 (AD7280A_NUM_CH * ad7280a_n_avg[st->oversampling_ratio & 0x3])) - in ad7280_update_delay()
872 ad7280a_t_acq_ns[st->acquisition_time & 0x3] + st->slave_num * 250; in ad7280_update_delay()
875 st->readback_delay_us = DIV_ROUND_UP(st->readback_delay_us, 1000); in ad7280_update_delay()
876 st->readback_delay_us += 5; /* Add tWAIT */ in ad7280_update_delay()
885 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_read_raw() local
890 mutex_lock(&st->lock); in ad7280_read_raw()
892 ret = ad7280_read_all_channels(st, st->scan_cnt, NULL); in ad7280_read_raw()
894 ret = ad7280_read_channel(st, chan->address >> 8, in ad7280_read_raw()
896 mutex_unlock(&st->lock); in ad7280_read_raw()
913 *val = ad7280a_n_avg[st->oversampling_ratio]; in ad7280_read_raw()
923 struct ad7280_state *st = iio_priv(indio_dev); in ad7280_write_raw() local
932 st->oversampling_ratio = i; in ad7280_write_raw()
933 ad7280_update_delay(st); in ad7280_write_raw()
958 struct ad7280_state *st; in ad7280_probe() local
962 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); in ad7280_probe()
966 st = iio_priv(indio_dev); in ad7280_probe()
968 st->spi = spi; in ad7280_probe()
969 mutex_init(&st->lock); in ad7280_probe()
971 st->thermistor_term_en = in ad7280_probe()
983 st->acquisition_time = AD7280A_CTRL_LB_ACQ_TIME_400ns; in ad7280_probe()
986 st->acquisition_time = AD7280A_CTRL_LB_ACQ_TIME_800ns; in ad7280_probe()
989 st->acquisition_time = AD7280A_CTRL_LB_ACQ_TIME_1200ns; in ad7280_probe()
992 st->acquisition_time = AD7280A_CTRL_LB_ACQ_TIME_1600ns; in ad7280_probe()
999 st->acquisition_time = AD7280A_CTRL_LB_ACQ_TIME_400ns; in ad7280_probe()
1012 st->chain_last_alert_ignore |= AD7280A_ALERT_REMOVE_VIN4_VIN5; in ad7280_probe()
1015 st->chain_last_alert_ignore |= AD7280A_ALERT_REMOVE_VIN5; in ad7280_probe()
1025 crc8_populate_msb(st->crc_tab, POLYNOM); in ad7280_probe()
1027 st->spi->max_speed_hz = AD7280A_MAX_SPI_CLK_HZ; in ad7280_probe()
1028 st->spi->mode = SPI_MODE_1; in ad7280_probe()
1029 spi_setup(st->spi); in ad7280_probe()
1031 st->ctrl_lb = FIELD_PREP(AD7280A_CTRL_LB_ACQ_TIME_MSK, st->acquisition_time) | in ad7280_probe()
1032 FIELD_PREP(AD7280A_CTRL_LB_THERMISTOR_MSK, st->thermistor_term_en); in ad7280_probe()
1033 st->oversampling_ratio = 0; /* No oversampling */ in ad7280_probe()
1035 ret = ad7280_chain_setup(st); in ad7280_probe()
1039 st->slave_num = ret; in ad7280_probe()
1040 st->scan_cnt = (st->slave_num + 1) * AD7280A_NUM_CH; in ad7280_probe()
1041 st->cell_threshhigh = 0xFF; in ad7280_probe()
1042 st->aux_threshhigh = 0xFF; in ad7280_probe()
1044 ret = devm_add_action_or_reset(dev, ad7280_sw_power_down, st); in ad7280_probe()
1048 ad7280_update_delay(st); in ad7280_probe()
1053 ret = ad7280_channel_init(st, spi->irq > 0); in ad7280_probe()
1058 indio_dev->channels = st->channels; in ad7280_probe()
1060 ret = ad7280_write(st, AD7280A_DEVADDR_MASTER, in ad7280_probe()
1061 AD7280A_ALERT_REG, 1, in ad7280_probe()
1066 ret = ad7280_write(st, ad7280a_devaddr(st->slave_num), in ad7280_probe()
1070 st->chain_last_alert_ignore)); in ad7280_probe()