Lines Matching +full:int +full:- +full:vref +full:- +full:microvolt
1 // SPDX-License-Identifier: GPL-2.0-only
103 #define AD3552R_REG_ADDR_CH_DAC_16B(ch) (0x2C - (1 - ch) * 2)
108 #define AD3552R_REG_ADDR_CH_INPUT_16B(ch) (0x36 - (1 - ch) * 2)
112 #define AD3552R_REG_ADDR_CH_DAC_24B(ch) (0x3D - (1 - ch) * 3)
117 #define AD3552R_REG_ADDR_CH_INPUT_24B(ch) (0x4B - (1 - ch) * 3)
134 /* Internal source with Vref I/O floating */
136 /* Internal source with Vref I/O at 2.5V */
138 /* External source with Vref I/O as input */
156 /* Range from -5 V to 5 V. Requires Rfb2x connection */
158 /* Range from -10 V to 10 V. Requires Rfb4x connection */
166 [AD3552R_CH_OUTPUT_RANGE_NEG_5__5V] = {-5000, 5000},
167 [AD3552R_CH_OUTPUT_RANGE_NEG_10__10V] = {-10000, 10000}
179 /* Range from -2.5 V to 7.5 V. Requires Rfb2x connection */
181 /* Range from -5 V to 5 V. Requires Rfb2x connection */
190 [AD3542R_CH_OUTPUT_RANGE_NEG_2P5__7P5V] = {-2500, 7500},
191 [AD3542R_CH_OUTPUT_RANGE_NEG_5__5V] = {-5000, 5000}
214 /* - Direct register values */
215 /* From 0-3 */
218 * 0 -> Internal Vref, vref_io pin floating (default)
219 * 1 -> Internal Vref, vref_io driven by internal vref
220 * 2 or 3 -> External Vref
235 * Over-rider the range selector in order to manually set the output
249 /* Channel select. When set allow Input -> DAC and Mask -> DAC */
269 unsigned int num_hw_channels;
271 int num_ranges;
285 unsigned int num_ch;
303 /* 0 -> reg addr, 1->ch0 mask, 2->ch1 mask */
350 static int ad3552r_transfer(struct ad3552r_desc *dac, u8 addr, u32 len, in ad3552r_transfer()
359 return spi_write_then_read(dac->spi, buf, 1, data, len); in ad3552r_transfer()
362 return spi_write_then_read(dac->spi, buf, len + 1, NULL, 0); in ad3552r_transfer()
365 static int ad3552r_write_reg(struct ad3552r_desc *dac, u8 addr, u16 val) in ad3552r_write_reg()
383 static int ad3552r_read_reg(struct ad3552r_desc *dac, u8 addr, u16 *val) in ad3552r_read_reg()
385 int err; in ad3552r_read_reg()
408 static int ad3552r_update_reg_field(struct ad3552r_desc *dac, u8 addr, u16 mask, in ad3552r_update_reg_field()
411 int ret; in ad3552r_update_reg_field()
424 static int ad3552r_set_ch_value(struct ad3552r_desc *dac, in ad3552r_set_ch_value()
452 static int ad3552r_read_raw(struct iio_dev *indio_dev, in ad3552r_read_raw()
454 int *val, in ad3552r_read_raw()
455 int *val2, in ad3552r_read_raw()
460 int err; in ad3552r_read_raw()
461 u8 ch = chan->channel; in ad3552r_read_raw()
465 mutex_lock(&dac->lock); in ad3552r_read_raw()
468 mutex_unlock(&dac->lock); in ad3552r_read_raw()
474 mutex_lock(&dac->lock); in ad3552r_read_raw()
477 mutex_unlock(&dac->lock); in ad3552r_read_raw()
484 *val = dac->ch_data[ch].scale_int; in ad3552r_read_raw()
485 *val2 = dac->ch_data[ch].scale_dec; in ad3552r_read_raw()
488 *val = dac->ch_data[ch].offset_int; in ad3552r_read_raw()
489 *val2 = dac->ch_data[ch].offset_dec; in ad3552r_read_raw()
492 return -EINVAL; in ad3552r_read_raw()
496 static int ad3552r_write_raw(struct iio_dev *indio_dev, in ad3552r_write_raw()
498 int val, in ad3552r_write_raw()
499 int val2, in ad3552r_write_raw()
503 int err; in ad3552r_write_raw()
505 mutex_lock(&dac->lock); in ad3552r_write_raw()
509 AD3552R_REG_ADDR_CH_DAC_24B(chan->channel), in ad3552r_write_raw()
514 chan->channel, !val); in ad3552r_write_raw()
517 err = -EINVAL; in ad3552r_write_raw()
520 mutex_unlock(&dac->lock); in ad3552r_write_raw()
539 static int ad3552r_write_all_channels(struct ad3552r_desc *dac, u8 *data) in ad3552r_write_all_channels()
541 int err, len; in ad3552r_write_all_channels()
552 if (!dac->gpio_ldac) { in ad3552r_write_all_channels()
561 if (dac->gpio_ldac) in ad3552r_write_all_channels()
562 return ad3552r_trigger_hw_ldac(dac->gpio_ldac); in ad3552r_write_all_channels()
567 static int ad3552r_write_codes(struct ad3552r_desc *dac, u32 mask, u8 *data) in ad3552r_write_codes()
569 int err; in ad3552r_write_codes()
587 if (dac->gpio_ldac) in ad3552r_write_codes()
588 return ad3552r_trigger_hw_ldac(dac->gpio_ldac); in ad3552r_write_codes()
593 static irqreturn_t ad3552r_trigger_handler(int irq, void *p) in ad3552r_trigger_handler()
596 struct iio_dev *indio_dev = pf->indio_dev; in ad3552r_trigger_handler()
597 struct iio_buffer *buf = indio_dev->buffer; in ad3552r_trigger_handler()
601 int err; in ad3552r_trigger_handler()
608 mutex_lock(&dac->lock); in ad3552r_trigger_handler()
609 ad3552r_write_codes(dac, *indio_dev->active_scan_mask, buff); in ad3552r_trigger_handler()
610 mutex_unlock(&dac->lock); in ad3552r_trigger_handler()
612 iio_trigger_notify_done(indio_dev->trig); in ad3552r_trigger_handler()
617 static int ad3552r_check_scratch_pad(struct ad3552r_desc *dac) in ad3552r_check_scratch_pad()
622 int err; in ad3552r_check_scratch_pad()
633 return -ENODEV; in ad3552r_check_scratch_pad()
644 return -ENODEV; in ad3552r_check_scratch_pad()
654 static int ad3552r_read_reg_wrapper(struct reg_addr_pool *addr) in ad3552r_read_reg_wrapper()
656 int err; in ad3552r_read_reg_wrapper()
659 err = ad3552r_read_reg(addr->dac, addr->addr, &val); in ad3552r_read_reg_wrapper()
666 static int ad3552r_reset(struct ad3552r_desc *dac) in ad3552r_reset()
669 int ret; in ad3552r_reset()
670 int val; in ad3552r_reset()
672 dac->gpio_reset = devm_gpiod_get_optional(&dac->spi->dev, "reset", in ad3552r_reset()
674 if (IS_ERR(dac->gpio_reset)) in ad3552r_reset()
675 return dev_err_probe(&dac->spi->dev, PTR_ERR(dac->gpio_reset), in ad3552r_reset()
678 if (dac->gpio_reset) { in ad3552r_reset()
681 gpiod_set_value_cansleep(dac->gpio_reset, 1); in ad3552r_reset()
702 dev_err(&dac->spi->dev, "Error while resetting"); in ad3552r_reset()
713 dev_err(&dac->spi->dev, "Error while resetting"); in ad3552r_reset()
726 s64 vref, tmp, common, offset, gn, gp; in ad3552r_get_custom_range() local
730 * Vmax = 2.5 - [(GainP + Offset / 1024) * 2.5 * Rfb * 1.03] in ad3552r_get_custom_range()
733 vref = 2500; in ad3552r_get_custom_range()
735 common = 2575 * dac->ch_data[i].rfb; in ad3552r_get_custom_range()
736 offset = dac->ch_data[i].gain_offset; in ad3552r_get_custom_range()
738 gn = gains_scaling_table[dac->ch_data[i].n]; in ad3552r_get_custom_range()
741 *v_max = vref + tmp; in ad3552r_get_custom_range()
743 gp = gains_scaling_table[dac->ch_data[i].p]; in ad3552r_get_custom_range()
744 tmp = (1024 * gp - AD3552R_GAIN_SCALE * offset) * common; in ad3552r_get_custom_range()
746 *v_min = vref - tmp; in ad3552r_get_custom_range()
754 if (dac->ch_data[ch].range_override) { in ad3552r_calc_gain_and_offset()
758 idx = dac->ch_data[ch].range; in ad3552r_calc_gain_and_offset()
759 v_min = dac->model_data->ranges_table[idx][0]; in ad3552r_calc_gain_and_offset()
760 v_max = dac->model_data->ranges_table[idx][1]; in ad3552r_calc_gain_and_offset()
773 span = v_max - v_min; in ad3552r_calc_gain_and_offset()
774 dac->ch_data[ch].scale_int = div_s64_rem(span, 65536, &rem); in ad3552r_calc_gain_and_offset()
776 dac->ch_data[ch].scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000, in ad3552r_calc_gain_and_offset()
779 dac->ch_data[ch].offset_int = div_s64_rem(v_min * 65536, span, &rem); in ad3552r_calc_gain_and_offset()
781 dac->ch_data[ch].offset_dec = div_s64(tmp, span); in ad3552r_calc_gain_and_offset()
784 static int ad3552r_find_range(const struct ad3552r_model_data *model_data, in ad3552r_find_range()
787 int i; in ad3552r_find_range()
789 for (i = 0; i < model_data->num_ranges; i++) in ad3552r_find_range()
790 if (vals[0] == model_data->ranges_table[i][0] * 1000 && in ad3552r_find_range()
791 vals[1] == model_data->ranges_table[i][1] * 1000) in ad3552r_find_range()
794 return -EINVAL; in ad3552r_find_range()
797 static int ad3552r_configure_custom_gain(struct ad3552r_desc *dac, in ad3552r_configure_custom_gain()
801 struct device *dev = &dac->spi->dev; in ad3552r_configure_custom_gain()
803 int err; in ad3552r_configure_custom_gain()
809 "custom-output-range-config"); in ad3552r_configure_custom_gain()
811 return dev_err_probe(dev, -EINVAL, in ad3552r_configure_custom_gain()
812 "mandatory custom-output-range-config property missing\n"); in ad3552r_configure_custom_gain()
814 dac->ch_data[ch].range_override = 1; in ad3552r_configure_custom_gain()
817 err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-p", &val); in ad3552r_configure_custom_gain()
820 "mandatory adi,gain-scaling-p property missing\n"); in ad3552r_configure_custom_gain()
822 dac->ch_data[ch].p = val; in ad3552r_configure_custom_gain()
824 err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-n", &val); in ad3552r_configure_custom_gain()
827 "mandatory adi,gain-scaling-n property missing\n"); in ad3552r_configure_custom_gain()
829 dac->ch_data[ch].n = val; in ad3552r_configure_custom_gain()
831 err = fwnode_property_read_u32(gain_child, "adi,rfb-ohms", &val); in ad3552r_configure_custom_gain()
834 "mandatory adi,rfb-ohms property missing\n"); in ad3552r_configure_custom_gain()
835 dac->ch_data[ch].rfb = val; in ad3552r_configure_custom_gain()
837 err = fwnode_property_read_u32(gain_child, "adi,gain-offset", &val); in ad3552r_configure_custom_gain()
840 "mandatory adi,gain-offset property missing\n"); in ad3552r_configure_custom_gain()
841 dac->ch_data[ch].gain_offset = val; in ad3552r_configure_custom_gain()
860 static int ad3552r_configure_device(struct ad3552r_desc *dac) in ad3552r_configure_device()
862 struct device *dev = &dac->spi->dev; in ad3552r_configure_device()
863 int err, cnt = 0, voltage, delta = 100000; in ad3552r_configure_device()
866 dac->gpio_ldac = devm_gpiod_get_optional(dev, "ldac", GPIOD_OUT_HIGH); in ad3552r_configure_device()
867 if (IS_ERR(dac->gpio_ldac)) in ad3552r_configure_device()
868 return dev_err_probe(dev, PTR_ERR(dac->gpio_ldac), in ad3552r_configure_device()
871 voltage = devm_regulator_get_enable_read_voltage(dev, "vref"); in ad3552r_configure_device()
872 if (voltage < 0 && voltage != -ENODEV) in ad3552r_configure_device()
873 return dev_err_probe(dev, voltage, "Error getting vref voltage\n"); in ad3552r_configure_device()
875 if (voltage == -ENODEV) { in ad3552r_configure_device()
876 if (device_property_read_bool(dev, "adi,vref-out-en")) in ad3552r_configure_device()
881 if (voltage > 2500000 + delta || voltage < 2500000 - delta) { in ad3552r_configure_device()
882 dev_warn(dev, "vref-supply must be 2.5V"); in ad3552r_configure_device()
883 return -EINVAL; in ad3552r_configure_device()
895 err = device_property_read_u32(dev, "adi,sdo-drive-strength", &val); in ad3552r_configure_device()
898 dev_err(dev, "adi,sdo-drive-strength must be less than 4\n"); in ad3552r_configure_device()
899 return -EINVAL; in ad3552r_configure_device()
910 dac->num_ch = device_get_child_node_count(dev); in ad3552r_configure_device()
911 if (!dac->num_ch) { in ad3552r_configure_device()
913 return -ENODEV; in ad3552r_configure_device()
921 if (ch >= dac->model_data->num_hw_channels) in ad3552r_configure_device()
922 return dev_err_probe(dev, -EINVAL, in ad3552r_configure_device()
924 dac->model_data->num_hw_channels); in ad3552r_configure_device()
926 if (fwnode_property_present(child, "adi,output-range-microvolt")) { in ad3552r_configure_device()
928 "adi,output-range-microvolt", in ad3552r_configure_device()
933 "adi,output-range-microvolt property could not be parsed\n"); in ad3552r_configure_device()
935 err = ad3552r_find_range(dac->model_data, vals); in ad3552r_configure_device()
938 "Invalid adi,output-range-microvolt value\n"); in ad3552r_configure_device()
947 dac->ch_data[ch].range = val; in ad3552r_configure_device()
948 } else if (dac->model_data->requires_output_range) { in ad3552r_configure_device()
949 return dev_err_probe(dev, -EINVAL, in ad3552r_configure_device()
950 "adi,output-range-microvolt is required for %s\n", in ad3552r_configure_device()
951 dac->model_data->model_name); in ad3552r_configure_device()
959 dac->enabled_ch |= BIT(ch); in ad3552r_configure_device()
965 dac->channels[cnt] = AD3552R_CH_DAC(ch); in ad3552r_configure_device()
971 for_each_clear_bit(ch, &dac->enabled_ch, in ad3552r_configure_device()
972 dac->model_data->num_hw_channels) { in ad3552r_configure_device()
979 dac->num_ch = cnt; in ad3552r_configure_device()
984 static int ad3552r_init(struct ad3552r_desc *dac) in ad3552r_init()
986 int err; in ad3552r_init()
991 dev_err(&dac->spi->dev, "Reset failed\n"); in ad3552r_init()
997 dev_err(&dac->spi->dev, "Scratch pad test failed\n"); in ad3552r_init()
1003 dev_err(&dac->spi->dev, "Fail read PRODUCT_ID_L\n"); in ad3552r_init()
1010 dev_err(&dac->spi->dev, "Fail read PRODUCT_ID_H\n"); in ad3552r_init()
1015 if (id != dac->model_data->chip_id) { in ad3552r_init()
1016 dev_err(&dac->spi->dev, "Product id not matching\n"); in ad3552r_init()
1017 return -ENODEV; in ad3552r_init()
1023 static int ad3552r_probe(struct spi_device *spi) in ad3552r_probe()
1027 int err; in ad3552r_probe()
1029 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*dac)); in ad3552r_probe()
1031 return -ENOMEM; in ad3552r_probe()
1034 dac->spi = spi; in ad3552r_probe()
1035 dac->model_data = spi_get_device_match_data(spi); in ad3552r_probe()
1036 if (!dac->model_data) in ad3552r_probe()
1037 return -EINVAL; in ad3552r_probe()
1039 mutex_init(&dac->lock); in ad3552r_probe()
1046 indio_dev->name = dac->model_data->model_name; in ad3552r_probe()
1047 indio_dev->dev.parent = &spi->dev; in ad3552r_probe()
1048 indio_dev->info = &ad3552r_iio_info; in ad3552r_probe()
1049 indio_dev->num_channels = dac->num_ch; in ad3552r_probe()
1050 indio_dev->channels = dac->channels; in ad3552r_probe()
1051 indio_dev->modes = INDIO_DIRECT_MODE; in ad3552r_probe()
1053 err = devm_iio_triggered_buffer_setup_ext(&indio_dev->dev, indio_dev, NULL, in ad3552r_probe()
1061 return devm_iio_device_register(&spi->dev, indio_dev); in ad3552r_probe()