Lines Matching +full:pre +full:- +full:scaling
1 // SPDX-License-Identifier: GPL-2.0-only
7 * specific-purpose and general purpose ADC converters and channels.
24 #include "qcom-vadc-common.h"
28 * Qualcomm tree. Their kernel has two out-of-tree drivers for the ADC:
29 * drivers/misc/pmic8058-xoadc.c
30 * drivers/hwmon/pm8xxx-adc.c
139 * PM8058 AMUX premux scaling, two bits. This is done of the channel before
142 #define PM8058_AMUX_PRESCALE_0 0x0 /* No scaling on the signal */
143 #define PM8058_AMUX_PRESCALE_1 0x1 /* Unity scaling selected by the user */
156 * struct xoadc_channel - encodes channel properties and defaults
163 * @prescale: the channels have hard-coded prescale ratios defined
173 * selects the reference voltage for ratiometric scaling
186 * struct xoadc_variant - encodes the XOADC variant characteristics
189 * @broken_ratiometric: if the PMIC has broken ratiometric scaling (this
213 * _scale: scaling function type, this selects how the raw valued is mangled
229 * Taken from arch/arm/mach-msm/board-9615.c in the vendor tree:
246 * Taken from arch/arm/mach-msm/board-8930-pmic.c in the vendor tree:
271 * This was created by cross-referencing the vendor tree
272 * arch/arm/mach-msm/board-msm8x60.c msm_adc_channels_data[]
288 * 1-to-1 onto MPP5 thru 9, so naming them MPP5 thru MPP9 is
307 * The PM8921 has some pre-muxing on its channels, this comes from the vendor tree
308 * include/linux/mfd/pm8xxx/pm8xxx-adc.h
309 * board-flo-pmic.c (Nexus 7) and board-8064-pmic.c
325 /* FIXME: look into the scaling of this temperature */
330 /* Set scaling to 1/2 based on the name for these two */
370 * struct pm8xxx_chan_info - ADC channel information
372 * @hwchan: pointer to hardware channel information (muxing & scaling settings)
374 * @scale_fn_type: scaling function type
388 * struct pm8xxx_xoadc - state container for the XOADC
395 * @chans: the channel information per-channel, configured by the device tree
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->args_count != 2) { in pm8xxx_of_xlate()
709 dev_err(&indio_dev->dev, "wrong number of arguments for %pOFn need 2 got %d\n", in pm8xxx_of_xlate()
710 iiospec->np, in pm8xxx_of_xlate()
711 iiospec->args_count); in pm8xxx_of_xlate()
712 return -EINVAL; in pm8xxx_of_xlate()
714 pre_scale_mux = (u8)iiospec->args[0]; in pm8xxx_of_xlate()
715 amux_channel = (u8)iiospec->args[1]; in pm8xxx_of_xlate()
716 dev_dbg(&indio_dev->dev, "pre scale/mux: %02x, amux: %02x\n", in pm8xxx_of_xlate()
720 for (i = 0; i < adc->nchans; i++) in pm8xxx_of_xlate()
721 if (adc->chans[i].hwchan->pre_scale_mux == pre_scale_mux && in pm8xxx_of_xlate()
722 adc->chans[i].hwchan->amux_channel == amux_channel) in pm8xxx_of_xlate()
725 return -EINVAL; in pm8xxx_of_xlate()
739 const char *name = np->name; in pm8xxx_xoadc_parse_channel()
748 dev_err(dev, "invalid pre scale/mux number %s\n", name); in pm8xxx_xoadc_parse_channel()
760 while (hwchan && 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()
816 hwchan->pre_scale_mux, hwchan->amux_channel, ch->name, in pm8xxx_xoadc_parse_channel()
817 ch->amux_ip_rsv, ch->decimation, hwchan->prescale.num, in pm8xxx_xoadc_parse_channel()
818 hwchan->prescale.den, hwchan->scale_fn_type); in pm8xxx_xoadc_parse_channel()
831 adc->nchans = of_get_available_child_count(np); in pm8xxx_xoadc_parse_channels()
832 if (!adc->nchans) { in pm8xxx_xoadc_parse_channels()
833 dev_err(adc->dev, "no channel children\n"); in pm8xxx_xoadc_parse_channels()
834 return -ENODEV; in pm8xxx_xoadc_parse_channels()
836 dev_dbg(adc->dev, "found %d ADC channels\n", adc->nchans); in pm8xxx_xoadc_parse_channels()
838 adc->iio_chans = devm_kcalloc(adc->dev, adc->nchans, in pm8xxx_xoadc_parse_channels()
839 sizeof(*adc->iio_chans), GFP_KERNEL); in pm8xxx_xoadc_parse_channels()
840 if (!adc->iio_chans) in pm8xxx_xoadc_parse_channels()
841 return -ENOMEM; in pm8xxx_xoadc_parse_channels()
843 adc->chans = devm_kcalloc(adc->dev, adc->nchans, in pm8xxx_xoadc_parse_channels()
844 sizeof(*adc->chans), GFP_KERNEL); in pm8xxx_xoadc_parse_channels()
845 if (!adc->chans) in pm8xxx_xoadc_parse_channels()
846 return -ENOMEM; in pm8xxx_xoadc_parse_channels()
850 ch = &adc->chans[i]; in pm8xxx_xoadc_parse_channels()
851 ret = pm8xxx_xoadc_parse_channel(adc->dev, child, in pm8xxx_xoadc_parse_channels()
852 adc->variant->channels, in pm8xxx_xoadc_parse_channels()
853 &adc->iio_chans[i], in pm8xxx_xoadc_parse_channels()
865 dev_err(adc->dev, "missing 1.25V reference channel\n"); in pm8xxx_xoadc_parse_channels()
866 return -ENODEV; in pm8xxx_xoadc_parse_channels()
870 dev_err(adc->dev, "missing 0.625V reference channel\n"); in pm8xxx_xoadc_parse_channels()
871 return -ENODEV; in pm8xxx_xoadc_parse_channels()
875 dev_err(adc->dev, "missing MUXOFF reference channel\n"); in pm8xxx_xoadc_parse_channels()
876 return -ENODEV; in pm8xxx_xoadc_parse_channels()
887 struct device_node *np = pdev->dev.of_node; in pm8xxx_xoadc_probe()
889 struct device *dev = &pdev->dev; in pm8xxx_xoadc_probe()
894 return -ENODEV; in pm8xxx_xoadc_probe()
898 return -ENOMEM; in pm8xxx_xoadc_probe()
902 adc->dev = dev; in pm8xxx_xoadc_probe()
903 adc->variant = variant; in pm8xxx_xoadc_probe()
904 init_completion(&adc->complete); in pm8xxx_xoadc_probe()
905 mutex_init(&adc->lock); in pm8xxx_xoadc_probe()
911 map = dev_get_regmap(dev->parent, NULL); in pm8xxx_xoadc_probe()
914 return -ENXIO; in pm8xxx_xoadc_probe()
916 adc->map = map; in pm8xxx_xoadc_probe()
919 adc->vref = devm_regulator_get(dev, "xoadc-ref"); in pm8xxx_xoadc_probe()
920 if (IS_ERR(adc->vref)) { in pm8xxx_xoadc_probe()
922 return PTR_ERR(adc->vref); in pm8xxx_xoadc_probe()
924 ret = regulator_enable(adc->vref); in pm8xxx_xoadc_probe()
931 pm8xxx_eoc_irq, NULL, 0, variant->name, indio_dev); in pm8xxx_xoadc_probe()
937 indio_dev->name = variant->name; in pm8xxx_xoadc_probe()
938 indio_dev->modes = INDIO_DIRECT_MODE; in pm8xxx_xoadc_probe()
939 indio_dev->info = &pm8xxx_xoadc_info; in pm8xxx_xoadc_probe()
940 indio_dev->channels = adc->iio_chans; in pm8xxx_xoadc_probe()
941 indio_dev->num_channels = adc->nchans; in pm8xxx_xoadc_probe()
951 dev_info(dev, "%s XOADC driver enabled\n", variant->name); in pm8xxx_xoadc_probe()
958 regulator_disable(adc->vref); in pm8xxx_xoadc_probe()
970 regulator_disable(adc->vref); in pm8xxx_xoadc_remove()
976 .name = "PM8018-XOADC",
981 .name = "PM8038-XOADC",
986 .name = "PM8058-XOADC",
993 .name = "PM8921-XOADC",
1000 .compatible = "qcom,pm8018-adc",
1004 .compatible = "qcom,pm8038-adc",
1008 .compatible = "qcom,pm8058-adc",
1012 .compatible = "qcom,pm8921-adc",
1021 .name = "pm8xxx-adc",
1031 MODULE_ALIAS("platform:pm8xxx-xoadc");