Lines Matching full:vadc
21 #include <dt-bindings/iio/qcom,spmi-vadc.h>
23 #include "qcom-vadc-common.h"
25 /* VADC register and bit definitions */
76 * struct vadc_channel_prop - VADC channel property.
99 * struct vadc_priv - VADC private structure.
103 * @nchannels: number of VADC channels.
104 * @chan_props: array of VADC channel properties.
108 * @complete: VADC result notification after interrupt is received.
137 static int vadc_read(struct vadc_priv *vadc, u16 offset, u8 *data) in vadc_read() argument
139 return regmap_bulk_read(vadc->regmap, vadc->base + offset, data, 1); in vadc_read()
142 static int vadc_write(struct vadc_priv *vadc, u16 offset, u8 data) in vadc_write() argument
144 return regmap_write(vadc->regmap, vadc->base + offset, data); in vadc_write()
147 static int vadc_reset(struct vadc_priv *vadc) in vadc_reset() argument
152 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA); in vadc_reset()
156 ret = vadc_read(vadc, VADC_PERH_RESET_CTL3, &data); in vadc_reset()
160 ret = vadc_write(vadc, VADC_ACCESS, VADC_ACCESS_DATA); in vadc_reset()
166 return vadc_write(vadc, VADC_PERH_RESET_CTL3, data); in vadc_reset()
169 static int vadc_set_state(struct vadc_priv *vadc, bool state) in vadc_set_state() argument
171 return vadc_write(vadc, VADC_EN_CTL1, state ? VADC_EN_CTL1_SET : 0); in vadc_set_state()
174 static void vadc_show_status(struct vadc_priv *vadc) in vadc_show_status() argument
179 ret = vadc_read(vadc, VADC_MODE_CTL, &mode); in vadc_show_status()
183 ret = vadc_read(vadc, VADC_ADC_DIG_PARAM, &dig); in vadc_show_status()
187 ret = vadc_read(vadc, VADC_ADC_CH_SEL_CTL, &chan); in vadc_show_status()
191 ret = vadc_read(vadc, VADC_CONV_REQ, &req); in vadc_show_status()
195 ret = vadc_read(vadc, VADC_STATUS1, &sta1); in vadc_show_status()
199 ret = vadc_read(vadc, VADC_EN_CTL1, &en); in vadc_show_status()
203 dev_err(vadc->dev, in vadc_show_status()
208 static int vadc_configure(struct vadc_priv *vadc, in vadc_configure() argument
217 ret = vadc_write(vadc, VADC_MODE_CTL, mode_ctrl); in vadc_configure()
222 ret = vadc_write(vadc, VADC_ADC_CH_SEL_CTL, prop->channel); in vadc_configure()
228 ret = vadc_write(vadc, VADC_ADC_DIG_PARAM, decimation); in vadc_configure()
233 ret = vadc_write(vadc, VADC_HW_SETTLE_DELAY, prop->hw_settle_time); in vadc_configure()
237 ret = vadc_write(vadc, VADC_FAST_AVG_CTL, prop->avg_samples); in vadc_configure()
242 ret = vadc_write(vadc, VADC_FAST_AVG_EN, VADC_FAST_AVG_EN_SET); in vadc_configure()
244 ret = vadc_write(vadc, VADC_FAST_AVG_EN, 0); in vadc_configure()
249 static int vadc_poll_wait_eoc(struct vadc_priv *vadc, unsigned int interval_us) in vadc_poll_wait_eoc() argument
258 ret = vadc_read(vadc, VADC_STATUS1, &sta1); in vadc_poll_wait_eoc()
269 vadc_show_status(vadc); in vadc_poll_wait_eoc()
274 static int vadc_read_result(struct vadc_priv *vadc, u16 *data) in vadc_read_result() argument
278 ret = regmap_bulk_read(vadc->regmap, vadc->base + VADC_DATA, data, 2); in vadc_read_result()
287 static struct vadc_channel_prop *vadc_get_channel(struct vadc_priv *vadc, in vadc_get_channel() argument
292 for (i = 0; i < vadc->nchannels; i++) in vadc_get_channel()
293 if (vadc->chan_props[i].channel == num) in vadc_get_channel()
294 return &vadc->chan_props[i]; in vadc_get_channel()
296 dev_dbg(vadc->dev, "no such channel %02x\n", num); in vadc_get_channel()
301 static int vadc_do_conversion(struct vadc_priv *vadc, in vadc_do_conversion() argument
307 mutex_lock(&vadc->lock); in vadc_do_conversion()
309 ret = vadc_configure(vadc, prop); in vadc_do_conversion()
313 if (!vadc->poll_eoc) in vadc_do_conversion()
314 reinit_completion(&vadc->complete); in vadc_do_conversion()
316 ret = vadc_set_state(vadc, true); in vadc_do_conversion()
320 ret = vadc_write(vadc, VADC_CONV_REQ, VADC_CONV_REQ_SET); in vadc_do_conversion()
326 if (vadc->poll_eoc) { in vadc_do_conversion()
327 ret = vadc_poll_wait_eoc(vadc, timeout); in vadc_do_conversion()
329 ret = wait_for_completion_timeout(&vadc->complete, timeout); in vadc_do_conversion()
336 ret = vadc_poll_wait_eoc(vadc, VADC_CONV_TIME_MIN_US); in vadc_do_conversion()
341 ret = vadc_read_result(vadc, data); in vadc_do_conversion()
344 vadc_set_state(vadc, false); in vadc_do_conversion()
346 dev_err(vadc->dev, "conversion failed\n"); in vadc_do_conversion()
348 mutex_unlock(&vadc->lock); in vadc_do_conversion()
352 static int vadc_measure_ref_points(struct vadc_priv *vadc) in vadc_measure_ref_points() argument
358 vadc->graph[VADC_CALIB_RATIOMETRIC].dx = VADC_RATIOMETRIC_RANGE; in vadc_measure_ref_points()
359 vadc->graph[VADC_CALIB_ABSOLUTE].dx = VADC_ABSOLUTE_RANGE_UV; in vadc_measure_ref_points()
361 prop = vadc_get_channel(vadc, VADC_REF_1250MV); in vadc_measure_ref_points()
362 ret = vadc_do_conversion(vadc, prop, &read_1); in vadc_measure_ref_points()
367 prop = vadc_get_channel(vadc, VADC_SPARE1); in vadc_measure_ref_points()
369 prop = vadc_get_channel(vadc, VADC_REF_625MV); in vadc_measure_ref_points()
371 ret = vadc_do_conversion(vadc, prop, &read_2); in vadc_measure_ref_points()
380 vadc->graph[VADC_CALIB_ABSOLUTE].dy = read_1 - read_2; in vadc_measure_ref_points()
381 vadc->graph[VADC_CALIB_ABSOLUTE].gnd = read_2; in vadc_measure_ref_points()
384 prop = vadc_get_channel(vadc, VADC_VDD_VADC); in vadc_measure_ref_points()
385 ret = vadc_do_conversion(vadc, prop, &read_1); in vadc_measure_ref_points()
389 prop = vadc_get_channel(vadc, VADC_GND_REF); in vadc_measure_ref_points()
390 ret = vadc_do_conversion(vadc, prop, &read_2); in vadc_measure_ref_points()
399 vadc->graph[VADC_CALIB_RATIOMETRIC].dy = read_1 - read_2; in vadc_measure_ref_points()
400 vadc->graph[VADC_CALIB_RATIOMETRIC].gnd = read_2; in vadc_measure_ref_points()
403 dev_err(vadc->dev, "measure reference points failed\n"); in vadc_measure_ref_points()
448 struct vadc_priv *vadc = iio_priv(indio_dev); in vadc_read_raw() local
455 prop = &vadc->chan_props[chan->address]; in vadc_read_raw()
456 ret = vadc_do_conversion(vadc, prop, &adc_code); in vadc_read_raw()
461 &vadc->graph[prop->calibration], in vadc_read_raw()
470 prop = &vadc->chan_props[chan->address]; in vadc_read_raw()
471 ret = vadc_do_conversion(vadc, prop, &adc_code); in vadc_read_raw()
488 struct vadc_priv *vadc = iio_priv(indio_dev); in vadc_of_xlate() local
491 for (i = 0; i < vadc->nchannels; i++) in vadc_of_xlate()
492 if (vadc->iio_chans[i].channel == iiospec->args[0]) in vadc_of_xlate()
735 static int vadc_get_dt_data(struct vadc_priv *vadc, struct device_node *node) in vadc_get_dt_data() argument
744 vadc->nchannels = of_get_available_child_count(node); in vadc_get_dt_data()
745 if (!vadc->nchannels) in vadc_get_dt_data()
748 vadc->iio_chans = devm_kcalloc(vadc->dev, vadc->nchannels, in vadc_get_dt_data()
749 sizeof(*vadc->iio_chans), GFP_KERNEL); in vadc_get_dt_data()
750 if (!vadc->iio_chans) in vadc_get_dt_data()
753 vadc->chan_props = devm_kcalloc(vadc->dev, vadc->nchannels, in vadc_get_dt_data()
754 sizeof(*vadc->chan_props), GFP_KERNEL); in vadc_get_dt_data()
755 if (!vadc->chan_props) in vadc_get_dt_data()
758 iio_chan = vadc->iio_chans; in vadc_get_dt_data()
761 ret = vadc_get_dt_channel_data(vadc->dev, &prop, child); in vadc_get_dt_data()
768 vadc->chan_props[index] = prop; in vadc_get_dt_data()
783 if (!vadc_get_channel(vadc, VADC_REF_1250MV)) { in vadc_get_dt_data()
784 dev_err(vadc->dev, "Please define 1.25V channel\n"); in vadc_get_dt_data()
788 if (!vadc_get_channel(vadc, VADC_REF_625MV)) { in vadc_get_dt_data()
789 dev_err(vadc->dev, "Please define 0.625V channel\n"); in vadc_get_dt_data()
793 if (!vadc_get_channel(vadc, VADC_VDD_VADC)) { in vadc_get_dt_data()
794 dev_err(vadc->dev, "Please define VDD channel\n"); in vadc_get_dt_data()
798 if (!vadc_get_channel(vadc, VADC_GND_REF)) { in vadc_get_dt_data()
799 dev_err(vadc->dev, "Please define GND channel\n"); in vadc_get_dt_data()
808 struct vadc_priv *vadc = dev_id; in vadc_isr() local
810 complete(&vadc->complete); in vadc_isr()
815 static int vadc_check_revision(struct vadc_priv *vadc) in vadc_check_revision() argument
820 ret = vadc_read(vadc, VADC_PERPH_TYPE, &val); in vadc_check_revision()
825 dev_err(vadc->dev, "%d is not ADC\n", val); in vadc_check_revision()
829 ret = vadc_read(vadc, VADC_PERPH_SUBTYPE, &val); in vadc_check_revision()
834 dev_err(vadc->dev, "%d is not VADC\n", val); in vadc_check_revision()
838 ret = vadc_read(vadc, VADC_REVISION2, &val); in vadc_check_revision()
843 dev_err(vadc->dev, "revision %d not supported\n", val); in vadc_check_revision()
855 struct vadc_priv *vadc; in vadc_probe() local
868 indio_dev = devm_iio_device_alloc(dev, sizeof(*vadc)); in vadc_probe()
872 vadc = iio_priv(indio_dev); in vadc_probe()
873 vadc->regmap = regmap; in vadc_probe()
874 vadc->dev = dev; in vadc_probe()
875 vadc->base = reg; in vadc_probe()
876 vadc->are_ref_measured = false; in vadc_probe()
877 init_completion(&vadc->complete); in vadc_probe()
878 mutex_init(&vadc->lock); in vadc_probe()
880 ret = vadc_check_revision(vadc); in vadc_probe()
884 ret = vadc_get_dt_data(vadc, node); in vadc_probe()
892 vadc->poll_eoc = true; in vadc_probe()
895 "spmi-vadc", vadc); in vadc_probe()
900 ret = vadc_reset(vadc); in vadc_probe()
906 ret = vadc_measure_ref_points(vadc); in vadc_probe()
913 indio_dev->channels = vadc->iio_chans; in vadc_probe()
914 indio_dev->num_channels = vadc->nchannels; in vadc_probe()
920 { .compatible = "qcom,spmi-vadc" },
927 .name = "qcom-spmi-vadc",
934 MODULE_ALIAS("platform:qcom-spmi-vadc");