Lines Matching +full:vref +full:- +full:mv

1 // SPDX-License-Identifier: GPL-2.0-only
7 * specific-purpose and general purpose ADC converters and channels.
13 #include <linux/iio/adc/qcom-vadc-common.h>
27 * Qualcomm tree. Their kernel has two out-of-tree drivers for the ADC:
28 * drivers/misc/pmic8058-xoadc.c
29 * drivers/hwmon/pm8xxx-adc.c
126 * @PM8XXX_CHANNEL_INTERNAL: 625mV reference channel
127 * @PM8XXX_CHANNEL_125V: 1250mV reference channel
128 * @PM8XXX_CHANNEL_INTERNAL_2: 325mV reference channel
155 * struct xoadc_channel - encodes channel properties and defaults
162 * @prescale: the channels have hard-coded prescale ratios defined
185 * struct xoadc_variant - encodes the XOADC variant characteristics
230 * Taken from arch/arm/mach-msm/board-9615.c in the vendor tree:
247 * Taken from arch/arm/mach-msm/board-8930-pmic.c in the vendor tree:
272 * This was created by cross-referencing the vendor tree
273 * arch/arm/mach-msm/board-msm8x60.c msm_adc_channels_data[]
289 * 1-to-1 onto MPP5 thru 9, so naming them MPP5 thru MPP9 is
308 * The PM8921 has some pre-muxing on its channels, this comes from the vendor tree
309 * include/linux/mfd/pm8xxx/pm8xxx-adc.h
310 * board-flo-pmic.c (Nexus 7) and board-8064-pmic.c
371 * struct pm8xxx_chan_info - ADC channel information
388 * struct pm8xxx_xoadc - state container for the XOADC
392 * @vref: reference voltage regulator
395 * @chans: the channel information per-channel, configured by the device tree
406 struct regulator *vref; member
420 complete(&adc->complete); in pm8xxx_eoc_irq()
430 for (i = 0; i < adc->nchans; i++) { in pm8xxx_get_channel()
431 struct pm8xxx_chan_info *ch = &adc->chans[i]; in pm8xxx_get_channel()
432 if (ch->hwchan->amux_channel == chan) in pm8xxx_get_channel()
448 dev_dbg(adc->dev, "read channel \"%s\", amux %d, prescale/mux: %d, rsv %d\n", in pm8xxx_read_channel_rsv()
449 ch->name, ch->hwchan->amux_channel, ch->hwchan->pre_scale_mux, rsv); in pm8xxx_read_channel_rsv()
451 mutex_lock(&adc->lock); in pm8xxx_read_channel_rsv()
454 val = ch->hwchan->amux_channel << ADC_AMUX_SEL_SHIFT; in pm8xxx_read_channel_rsv()
455 val |= ch->hwchan->pre_scale_mux << ADC_AMUX_PREMUX_SHIFT; in pm8xxx_read_channel_rsv()
456 ret = regmap_write(adc->map, ADC_ARB_USRP_AMUX_CNTRL, val); in pm8xxx_read_channel_rsv()
463 if (adc->variant->broken_ratiometric && !force_ratiometric) { in pm8xxx_read_channel_rsv()
466 * reflected in the vendor tree drivers/misc/pmix8058-xoadc.c in pm8xxx_read_channel_rsv()
480 if (ch->hwchan->amux_channel == PM8XXX_CHANNEL_MUXOFF) in pm8xxx_read_channel_rsv()
486 rsvval = (ch->amux_ip_rsv << ADC_RSV_IP_SEL_SHIFT) | in pm8xxx_read_channel_rsv()
493 ret = regmap_update_bits(adc->map, in pm8xxx_read_channel_rsv()
500 ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM, in pm8xxx_read_channel_rsv()
506 ret = regmap_write(adc->map, ADC_ARB_USRP_DIG_PARAM, in pm8xxx_read_channel_rsv()
509 ch->decimation << ADC_DIG_PARAM_DEC_SHIFT); in pm8xxx_read_channel_rsv()
513 ret = regmap_write(adc->map, ADC_ARB_USRP_ANA_PARAM, in pm8xxx_read_channel_rsv()
519 ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, in pm8xxx_read_channel_rsv()
523 ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, in pm8xxx_read_channel_rsv()
530 reinit_completion(&adc->complete); in pm8xxx_read_channel_rsv()
531 ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, in pm8xxx_read_channel_rsv()
538 ret = wait_for_completion_timeout(&adc->complete, in pm8xxx_read_channel_rsv()
541 dev_err(adc->dev, "conversion timed out\n"); in pm8xxx_read_channel_rsv()
542 ret = -ETIMEDOUT; in pm8xxx_read_channel_rsv()
546 ret = regmap_read(adc->map, ADC_ARB_USRP_DATA0, &val); in pm8xxx_read_channel_rsv()
550 ret = regmap_read(adc->map, ADC_ARB_USRP_DATA1, &val); in pm8xxx_read_channel_rsv()
557 ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0); in pm8xxx_read_channel_rsv()
560 ret = regmap_write(adc->map, ADC_ARB_USRP_CNTRL, 0); in pm8xxx_read_channel_rsv()
565 mutex_unlock(&adc->lock); in pm8xxx_read_channel_rsv()
590 adc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV; in pm8xxx_calibrate_device()
591 adc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE; in pm8xxx_calibrate_device()
596 return -ENODEV; in pm8xxx_calibrate_device()
599 dev_err(adc->dev, "could not read 1.25V reference channel\n"); in pm8xxx_calibrate_device()
600 return -ENODEV; in pm8xxx_calibrate_device()
604 return -ENODEV; in pm8xxx_calibrate_device()
607 dev_err(adc->dev, "could not read 0.625V reference channel\n"); in pm8xxx_calibrate_device()
608 return -ENODEV; in pm8xxx_calibrate_device()
611 dev_err(adc->dev, "read same ADC code for 1.25V and 0.625V\n"); in pm8xxx_calibrate_device()
612 return -ENODEV; in pm8xxx_calibrate_device()
615 adc->graph[VADC_CALIB_ABSOLUTE].dy = read_1250v - read_0625v; in pm8xxx_calibrate_device()
616 adc->graph[VADC_CALIB_ABSOLUTE].gnd = read_0625v; in pm8xxx_calibrate_device()
618 dev_info(adc->dev, "absolute calibration dx = %d uV, dy = %d units\n", in pm8xxx_calibrate_device()
619 VADC_ABSOLUTE_RANGE_UV, adc->graph[VADC_CALIB_ABSOLUTE].dy); in pm8xxx_calibrate_device()
624 return -ENODEV; in pm8xxx_calibrate_device()
628 dev_err(adc->dev, "could not read MUXOFF reference channel\n"); in pm8xxx_calibrate_device()
629 return -ENODEV; in pm8xxx_calibrate_device()
634 dev_err(adc->dev, "could not read MUXOFF reference channel\n"); in pm8xxx_calibrate_device()
635 return -ENODEV; in pm8xxx_calibrate_device()
637 adc->graph[VADC_CALIB_RATIOMETRIC].dy = in pm8xxx_calibrate_device()
638 read_nomux_rsv5 - read_nomux_rsv4; in pm8xxx_calibrate_device()
639 adc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_nomux_rsv4; in pm8xxx_calibrate_device()
641 dev_info(adc->dev, "ratiometric calibration dx = %d, dy = %d units\n", in pm8xxx_calibrate_device()
643 adc->graph[VADC_CALIB_RATIOMETRIC].dy); in pm8xxx_calibrate_device()
659 ch = pm8xxx_get_channel(adc, chan->address); in pm8xxx_read_raw()
661 dev_err(adc->dev, "no such channel %lu\n", in pm8xxx_read_raw()
662 chan->address); in pm8xxx_read_raw()
663 return -EINVAL; in pm8xxx_read_raw()
669 ret = qcom_vadc_scale(ch->hwchan->scale_fn_type, in pm8xxx_read_raw()
670 &adc->graph[ch->calibration], in pm8xxx_read_raw()
671 &ch->hwchan->prescale, in pm8xxx_read_raw()
672 (ch->calibration == VADC_CALIB_ABSOLUTE), in pm8xxx_read_raw()
679 ch = pm8xxx_get_channel(adc, chan->address); in pm8xxx_read_raw()
681 dev_err(adc->dev, "no such channel %lu\n", in pm8xxx_read_raw()
682 chan->address); in pm8xxx_read_raw()
683 return -EINVAL; in pm8xxx_read_raw()
692 return -EINVAL; in pm8xxx_read_raw()
708 if (iiospec->nargs != 2) { in pm8xxx_fwnode_xlate()
709 dev_err(&indio_dev->dev, "wrong number of arguments for %pfwP need 2 got %d\n", in pm8xxx_fwnode_xlate()
710 iiospec->fwnode, in pm8xxx_fwnode_xlate()
711 iiospec->nargs); in pm8xxx_fwnode_xlate()
712 return -EINVAL; in pm8xxx_fwnode_xlate()
714 pre_scale_mux = (u8)iiospec->args[0]; in pm8xxx_fwnode_xlate()
715 amux_channel = (u8)iiospec->args[1]; in pm8xxx_fwnode_xlate()
716 dev_dbg(&indio_dev->dev, "pre scale/mux: %02x, amux: %02x\n", in pm8xxx_fwnode_xlate()
720 for (i = 0; i < adc->nchans; i++) in pm8xxx_fwnode_xlate()
721 if (adc->chans[i].hwchan->pre_scale_mux == pre_scale_mux && in pm8xxx_fwnode_xlate()
722 adc->chans[i].hwchan->amux_channel == amux_channel) in pm8xxx_fwnode_xlate()
725 return -EINVAL; in pm8xxx_fwnode_xlate()
760 while (hwchan->datasheet_name) { in pm8xxx_xoadc_parse_channel()
761 if (hwchan->pre_scale_mux == pre_scale_mux && in pm8xxx_xoadc_parse_channel()
762 hwchan->amux_channel == amux_channel) in pm8xxx_xoadc_parse_channel()
768 if (!hwchan->datasheet_name) { in pm8xxx_xoadc_parse_channel()
771 return -EINVAL; in pm8xxx_xoadc_parse_channel()
773 ch->name = name; in pm8xxx_xoadc_parse_channel()
774 ch->hwchan = hwchan; in pm8xxx_xoadc_parse_channel()
776 ch->calibration = VADC_CALIB_ABSOLUTE; in pm8xxx_xoadc_parse_channel()
778 ch->decimation = VADC_DEF_DECIMATION; in pm8xxx_xoadc_parse_channel()
781 ch->calibration = VADC_CALIB_RATIOMETRIC; in pm8xxx_xoadc_parse_channel()
784 return -EINVAL; in pm8xxx_xoadc_parse_channel()
788 return -EINVAL; in pm8xxx_xoadc_parse_channel()
801 ch->decimation = ret; in pm8xxx_xoadc_parse_channel()
804 iio_chan->channel = chid; in pm8xxx_xoadc_parse_channel()
805 iio_chan->address = hwchan->amux_channel; in pm8xxx_xoadc_parse_channel()
806 iio_chan->datasheet_name = hwchan->datasheet_name; in pm8xxx_xoadc_parse_channel()
807 iio_chan->type = hwchan->type; in pm8xxx_xoadc_parse_channel()
809 iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | in pm8xxx_xoadc_parse_channel()
811 iio_chan->indexed = 1; in pm8xxx_xoadc_parse_channel()
815 hwchan->pre_scale_mux, hwchan->amux_channel, ch->name, in pm8xxx_xoadc_parse_channel()
816 ch->amux_ip_rsv, ch->decimation, hwchan->prescale.numerator, in pm8xxx_xoadc_parse_channel()
817 hwchan->prescale.denominator, hwchan->scale_fn_type); in pm8xxx_xoadc_parse_channel()
829 adc->nchans = device_get_child_node_count(adc->dev); in pm8xxx_xoadc_parse_channels()
830 if (!adc->nchans) { in pm8xxx_xoadc_parse_channels()
831 dev_err(adc->dev, "no channel children\n"); in pm8xxx_xoadc_parse_channels()
832 return -ENODEV; in pm8xxx_xoadc_parse_channels()
834 dev_dbg(adc->dev, "found %d ADC channels\n", adc->nchans); in pm8xxx_xoadc_parse_channels()
836 adc->iio_chans = devm_kcalloc(adc->dev, adc->nchans, in pm8xxx_xoadc_parse_channels()
837 sizeof(*adc->iio_chans), GFP_KERNEL); in pm8xxx_xoadc_parse_channels()
838 if (!adc->iio_chans) in pm8xxx_xoadc_parse_channels()
839 return -ENOMEM; in pm8xxx_xoadc_parse_channels()
841 adc->chans = devm_kcalloc(adc->dev, adc->nchans, in pm8xxx_xoadc_parse_channels()
842 sizeof(*adc->chans), GFP_KERNEL); in pm8xxx_xoadc_parse_channels()
843 if (!adc->chans) in pm8xxx_xoadc_parse_channels()
844 return -ENOMEM; in pm8xxx_xoadc_parse_channels()
847 device_for_each_child_node(adc->dev, child) { in pm8xxx_xoadc_parse_channels()
848 ch = &adc->chans[i]; in pm8xxx_xoadc_parse_channels()
849 ret = pm8xxx_xoadc_parse_channel(adc->dev, child, in pm8xxx_xoadc_parse_channels()
850 adc->variant->channels, in pm8xxx_xoadc_parse_channels()
851 &adc->iio_chans[i], in pm8xxx_xoadc_parse_channels()
863 dev_err(adc->dev, "missing 1.25V reference channel\n"); in pm8xxx_xoadc_parse_channels()
864 return -ENODEV; in pm8xxx_xoadc_parse_channels()
868 dev_err(adc->dev, "missing 0.625V reference channel\n"); in pm8xxx_xoadc_parse_channels()
869 return -ENODEV; in pm8xxx_xoadc_parse_channels()
873 dev_err(adc->dev, "missing MUXOFF reference channel\n"); in pm8xxx_xoadc_parse_channels()
874 return -ENODEV; in pm8xxx_xoadc_parse_channels()
886 struct device *dev = &pdev->dev; in pm8xxx_xoadc_probe()
891 return -ENODEV; in pm8xxx_xoadc_probe()
895 return -ENOMEM; in pm8xxx_xoadc_probe()
899 adc->dev = dev; in pm8xxx_xoadc_probe()
900 adc->variant = variant; in pm8xxx_xoadc_probe()
901 init_completion(&adc->complete); in pm8xxx_xoadc_probe()
902 mutex_init(&adc->lock); in pm8xxx_xoadc_probe()
908 map = dev_get_regmap(dev->parent, NULL); in pm8xxx_xoadc_probe()
911 return -ENODEV; in pm8xxx_xoadc_probe()
913 adc->map = map; in pm8xxx_xoadc_probe()
916 adc->vref = devm_regulator_get(dev, "xoadc-ref"); in pm8xxx_xoadc_probe()
917 if (IS_ERR(adc->vref)) in pm8xxx_xoadc_probe()
918 return dev_err_probe(dev, PTR_ERR(adc->vref), in pm8xxx_xoadc_probe()
919 "failed to get XOADC VREF regulator\n"); in pm8xxx_xoadc_probe()
920 ret = regulator_enable(adc->vref); in pm8xxx_xoadc_probe()
922 dev_err(dev, "failed to enable XOADC VREF regulator\n"); in pm8xxx_xoadc_probe()
927 pm8xxx_eoc_irq, NULL, 0, variant->name, indio_dev); in pm8xxx_xoadc_probe()
933 indio_dev->name = variant->name; in pm8xxx_xoadc_probe()
934 indio_dev->modes = INDIO_DIRECT_MODE; in pm8xxx_xoadc_probe()
935 indio_dev->info = &pm8xxx_xoadc_info; in pm8xxx_xoadc_probe()
936 indio_dev->channels = adc->iio_chans; in pm8xxx_xoadc_probe()
937 indio_dev->num_channels = adc->nchans; in pm8xxx_xoadc_probe()
947 dev_info(dev, "%s XOADC driver enabled\n", variant->name); in pm8xxx_xoadc_probe()
954 regulator_disable(adc->vref); in pm8xxx_xoadc_probe()
966 regulator_disable(adc->vref); in pm8xxx_xoadc_remove()
970 .name = "PM8018-XOADC",
975 .name = "PM8038-XOADC",
980 .name = "PM8058-XOADC",
987 .name = "PM8921-XOADC",
994 .compatible = "qcom,pm8018-adc",
998 .compatible = "qcom,pm8038-adc",
1002 .compatible = "qcom,pm8058-adc",
1006 .compatible = "qcom,pm8921-adc",
1015 .name = "pm8xxx-adc",
1025 MODULE_ALIAS("platform:pm8xxx-xoadc");