Lines Matching +full:spmi +full:- +full:adc +full:- +full:rev2
1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/iio/adc/qcom-vadc-common.h>
23 #include <dt-bindings/iio/qcom,spmi-vadc.h>
104 * struct adc5_channel_prop - ADC channel property.
113 * @avg_samples: ability to provide single result from the ADC
133 * struct adc5_chip - ADC private structure.
134 * @regmap: SPMI ADC5 peripheral register map field.
135 * @dev: SPMI ADC5 device.
136 * @base: base address for the ADC peripheral.
137 * @nchannels: number of ADC channels.
138 * @chan_props: array of ADC channel properties.
141 * @complete: ADC result notification after interrupt is received.
142 * @lock: ADC lock for access to the peripheral.
158 static int adc5_read(struct adc5_chip *adc, u16 offset, u8 *data, int len) in adc5_read() argument
160 return regmap_bulk_read(adc->regmap, adc->base + offset, data, len); in adc5_read()
163 static int adc5_write(struct adc5_chip *adc, u16 offset, u8 *data, int len) in adc5_write() argument
165 return regmap_bulk_write(adc->regmap, adc->base + offset, data, len); in adc5_write()
168 static int adc5_masked_write(struct adc5_chip *adc, u16 offset, u8 mask, u8 val) in adc5_masked_write() argument
170 return regmap_update_bits(adc->regmap, adc->base + offset, mask, val); in adc5_masked_write()
173 static int adc5_read_voltage_data(struct adc5_chip *adc, u16 *data) in adc5_read_voltage_data() argument
178 ret = adc5_read(adc, ADC5_USR_DATA0, &rslt_lsb, sizeof(rslt_lsb)); in adc5_read_voltage_data()
182 ret = adc5_read(adc, ADC5_USR_DATA1, &rslt_msb, sizeof(rslt_lsb)); in adc5_read_voltage_data()
189 dev_err(adc->dev, "Invalid data:0x%x\n", *data); in adc5_read_voltage_data()
190 return -EINVAL; in adc5_read_voltage_data()
193 dev_dbg(adc->dev, "voltage raw code:0x%x\n", *data); in adc5_read_voltage_data()
198 static int adc5_poll_wait_eoc(struct adc5_chip *adc) in adc5_poll_wait_eoc() argument
205 ret = adc5_read(adc, ADC5_USR_STATUS1, &status1, in adc5_poll_wait_eoc()
217 return -ETIMEDOUT; in adc5_poll_wait_eoc()
220 static void adc5_update_dig_param(struct adc5_chip *adc, in adc5_update_dig_param() argument
225 *data |= (prop->cal_val << ADC5_USR_DIG_PARAM_CAL_VAL_SHIFT); in adc5_update_dig_param()
229 *data |= (prop->cal_method << ADC5_USR_DIG_PARAM_CAL_SEL_SHIFT); in adc5_update_dig_param()
233 *data |= (prop->decimation << ADC5_USR_DIG_PARAM_DEC_RATIO_SEL_SHIFT); in adc5_update_dig_param()
236 static int adc5_configure(struct adc5_chip *adc, in adc5_configure() argument
243 ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf)); in adc5_configure()
248 adc5_update_dig_param(adc, prop, &buf[0]); in adc5_configure()
252 buf[1] |= prop->avg_samples; in adc5_configure()
254 /* Select ADC channel */ in adc5_configure()
255 buf[2] = prop->channel; in adc5_configure()
259 buf[3] |= prop->hw_settle_time; in adc5_configure()
261 /* Select ADC enable */ in adc5_configure()
267 if (!adc->poll_eoc) in adc5_configure()
268 reinit_completion(&adc->complete); in adc5_configure()
270 return adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf)); in adc5_configure()
273 static int adc7_configure(struct adc5_chip *adc, in adc7_configure() argument
279 ret = adc5_masked_write(adc, ADC_APP_SID, ADC_APP_SID_MASK, prop->sid); in adc7_configure()
283 ret = adc5_read(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf)); in adc7_configure()
288 adc5_update_dig_param(adc, prop, &buf[0]); in adc7_configure()
292 buf[1] |= prop->avg_samples; in adc7_configure()
294 /* Select ADC channel */ in adc7_configure()
295 buf[2] = prop->channel; in adc7_configure()
299 buf[3] |= prop->hw_settle_time; in adc7_configure()
304 if (!adc->poll_eoc) in adc7_configure()
305 reinit_completion(&adc->complete); in adc7_configure()
307 ret = adc5_write(adc, ADC5_USR_DIG_PARAM, buf, sizeof(buf)); in adc7_configure()
311 return adc5_write(adc, ADC5_USR_CONV_REQ, &conv_req, 1); in adc7_configure()
314 static int adc5_do_conversion(struct adc5_chip *adc, in adc5_do_conversion() argument
321 mutex_lock(&adc->lock); in adc5_do_conversion()
323 ret = adc5_configure(adc, prop); in adc5_do_conversion()
325 dev_err(adc->dev, "ADC configure failed with %d\n", ret); in adc5_do_conversion()
329 if (adc->poll_eoc) { in adc5_do_conversion()
330 ret = adc5_poll_wait_eoc(adc); in adc5_do_conversion()
332 dev_err(adc->dev, "EOC bit not set\n"); in adc5_do_conversion()
336 ret = wait_for_completion_timeout(&adc->complete, in adc5_do_conversion()
339 dev_dbg(adc->dev, "Did not get completion timeout.\n"); in adc5_do_conversion()
340 ret = adc5_poll_wait_eoc(adc); in adc5_do_conversion()
342 dev_err(adc->dev, "EOC bit not set\n"); in adc5_do_conversion()
348 ret = adc5_read_voltage_data(adc, data_volt); in adc5_do_conversion()
350 mutex_unlock(&adc->lock); in adc5_do_conversion()
355 static int adc7_do_conversion(struct adc5_chip *adc, in adc7_do_conversion() argument
363 mutex_lock(&adc->lock); in adc7_do_conversion()
365 ret = adc7_configure(adc, prop); in adc7_do_conversion()
367 dev_err(adc->dev, "ADC configure failed with %d\n", ret); in adc7_do_conversion()
372 wait_for_completion_timeout(&adc->complete, ADC7_CONV_TIMEOUT); in adc7_do_conversion()
374 ret = adc5_read(adc, ADC5_USR_STATUS1, &status, 1); in adc7_do_conversion()
379 dev_err(adc->dev, "Unexpected conversion fault\n"); in adc7_do_conversion()
380 ret = -EIO; in adc7_do_conversion()
384 ret = adc5_read_voltage_data(adc, data_volt); in adc7_do_conversion()
387 mutex_unlock(&adc->lock); in adc7_do_conversion()
392 typedef int (*adc_do_conversion)(struct adc5_chip *adc,
399 struct adc5_chip *adc = dev_id; in adc5_isr() local
401 complete(&adc->complete); in adc5_isr()
409 struct adc5_chip *adc = iio_priv(indio_dev); in adc5_fwnode_xlate() local
412 for (i = 0; i < adc->nchannels; i++) in adc5_fwnode_xlate()
413 if (adc->chan_props[i].channel == iiospec->args[0]) in adc5_fwnode_xlate()
416 return -EINVAL; in adc5_fwnode_xlate()
422 struct adc5_chip *adc = iio_priv(indio_dev); in adc7_fwnode_xlate() local
425 for (i = 0; i < adc->nchannels; i++) { in adc7_fwnode_xlate()
426 v_channel = (adc->chan_props[i].sid << ADC_CHANNEL_OFFSET) | in adc7_fwnode_xlate()
427 adc->chan_props[i].channel; in adc7_fwnode_xlate()
428 if (v_channel == iiospec->args[0]) in adc7_fwnode_xlate()
432 return -EINVAL; in adc7_fwnode_xlate()
439 struct adc5_chip *adc = iio_priv(indio_dev); in adc_read_raw_common() local
444 prop = &adc->chan_props[chan->address]; in adc_read_raw_common()
448 ret = do_conv(adc, prop, chan, in adc_read_raw_common()
453 ret = qcom_adc5_hw_scale(prop->scale_fn_type, in adc_read_raw_common()
454 prop->prescale, in adc_read_raw_common()
455 adc->data, in adc_read_raw_common()
462 return -EINVAL; in adc_read_raw_common()
624 static int adc5_get_fw_channel_data(struct adc5_chip *adc, in adc5_get_fw_channel_data() argument
633 struct device *dev = adc->dev; in adc5_get_fw_channel_data()
645 if (adc->data->info == &adc7_info) { in adc5_get_fw_channel_data()
651 !data->adc_chans[chan].datasheet_name) { in adc5_get_fw_channel_data()
653 return -EINVAL; in adc5_get_fw_channel_data()
657 prop->channel = chan; in adc5_get_fw_channel_data()
658 prop->sid = sid; in adc5_get_fw_channel_data()
664 prop->datasheet_name = channel_name; in adc5_get_fw_channel_data()
668 ret = qcom_adc5_decimation_from_dt(value, data->decimation); in adc5_get_fw_channel_data()
674 prop->decimation = ret; in adc5_get_fw_channel_data()
676 prop->decimation = ADC5_DECIMATION_DEFAULT; in adc5_get_fw_channel_data()
679 ret = fwnode_property_read_u32_array(fwnode, "qcom,pre-scaling", varr, 2); in adc5_get_fw_channel_data()
683 dev_err(dev, "%02x invalid pre-scaling <%d %d>\n", in adc5_get_fw_channel_data()
687 prop->prescale = ret; in adc5_get_fw_channel_data()
689 prop->prescale = in adc5_get_fw_channel_data()
690 adc->data->adc_chans[prop->channel].prescale_index; in adc5_get_fw_channel_data()
693 ret = fwnode_property_read_u32(fwnode, "qcom,hw-settle-time", &value); in adc5_get_fw_channel_data()
697 ret = adc5_read(adc, ADC5_USR_REVISION1, dig_version, in adc5_get_fw_channel_data()
709 adc->data->info == &adc7_info) in adc5_get_fw_channel_data()
710 ret = qcom_adc5_hw_settle_time_from_dt(value, data->hw_settle_2); in adc5_get_fw_channel_data()
712 ret = qcom_adc5_hw_settle_time_from_dt(value, data->hw_settle_1); in adc5_get_fw_channel_data()
715 dev_err(dev, "%02x invalid hw-settle-time %d us\n", in adc5_get_fw_channel_data()
719 prop->hw_settle_time = ret; in adc5_get_fw_channel_data()
721 prop->hw_settle_time = VADC_DEF_HW_SETTLE_TIME; in adc5_get_fw_channel_data()
724 ret = fwnode_property_read_u32(fwnode, "qcom,avg-samples", &value); in adc5_get_fw_channel_data()
728 dev_err(dev, "%02x invalid avg-samples %d\n", in adc5_get_fw_channel_data()
732 prop->avg_samples = ret; in adc5_get_fw_channel_data()
734 prop->avg_samples = VADC_DEF_AVG_SAMPLES; in adc5_get_fw_channel_data()
738 prop->cal_method = ADC5_RATIOMETRIC_CAL; in adc5_get_fw_channel_data()
740 prop->cal_method = ADC5_ABSOLUTE_CAL; in adc5_get_fw_channel_data()
746 prop->cal_val = ADC5_TIMER_CAL; in adc5_get_fw_channel_data()
797 .compatible = "qcom,spmi-adc5",
801 .compatible = "qcom,spmi-adc7",
805 .compatible = "qcom,spmi-adc-rev2",
812 static int adc5_get_fw_data(struct adc5_chip *adc) in adc5_get_fw_data() argument
821 adc->nchannels = device_get_child_node_count(adc->dev); in adc5_get_fw_data()
822 if (!adc->nchannels) in adc5_get_fw_data()
823 return -EINVAL; in adc5_get_fw_data()
825 adc->iio_chans = devm_kcalloc(adc->dev, adc->nchannels, in adc5_get_fw_data()
826 sizeof(*adc->iio_chans), GFP_KERNEL); in adc5_get_fw_data()
827 if (!adc->iio_chans) in adc5_get_fw_data()
828 return -ENOMEM; in adc5_get_fw_data()
830 adc->chan_props = devm_kcalloc(adc->dev, adc->nchannels, in adc5_get_fw_data()
831 sizeof(*adc->chan_props), GFP_KERNEL); in adc5_get_fw_data()
832 if (!adc->chan_props) in adc5_get_fw_data()
833 return -ENOMEM; in adc5_get_fw_data()
835 chan_props = adc->chan_props; in adc5_get_fw_data()
836 iio_chan = adc->iio_chans; in adc5_get_fw_data()
837 adc->data = device_get_match_data(adc->dev); in adc5_get_fw_data()
838 if (!adc->data) in adc5_get_fw_data()
839 adc->data = &adc5_data_pmic; in adc5_get_fw_data()
841 device_for_each_child_node(adc->dev, child) { in adc5_get_fw_data()
842 ret = adc5_get_fw_channel_data(adc, &prop, child, adc->data); in adc5_get_fw_data()
849 adc->data->adc_chans[prop.channel].scale_fn_type; in adc5_get_fw_data()
851 adc_chan = &adc->data->adc_chans[prop.channel]; in adc5_get_fw_data()
853 iio_chan->channel = prop.channel; in adc5_get_fw_data()
854 iio_chan->datasheet_name = prop.datasheet_name; in adc5_get_fw_data()
855 iio_chan->extend_name = prop.datasheet_name; in adc5_get_fw_data()
856 iio_chan->info_mask_separate = adc_chan->info_mask; in adc5_get_fw_data()
857 iio_chan->type = adc_chan->type; in adc5_get_fw_data()
858 iio_chan->address = index; in adc5_get_fw_data()
869 struct device *dev = &pdev->dev; in adc5_probe()
871 struct adc5_chip *adc; in adc5_probe() local
876 regmap = dev_get_regmap(dev->parent, NULL); in adc5_probe()
878 return -ENODEV; in adc5_probe()
884 indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); in adc5_probe()
886 return -ENOMEM; in adc5_probe()
888 adc = iio_priv(indio_dev); in adc5_probe()
889 adc->regmap = regmap; in adc5_probe()
890 adc->dev = dev; in adc5_probe()
891 adc->base = reg; in adc5_probe()
893 init_completion(&adc->complete); in adc5_probe()
894 mutex_init(&adc->lock); in adc5_probe()
896 ret = adc5_get_fw_data(adc); in adc5_probe()
898 dev_err(dev, "adc get dt data failed\n"); in adc5_probe()
904 if (irq_eoc == -EPROBE_DEFER || irq_eoc == -EINVAL) in adc5_probe()
906 adc->poll_eoc = true; in adc5_probe()
909 "pm-adc5", adc); in adc5_probe()
914 indio_dev->name = pdev->name; in adc5_probe()
915 indio_dev->modes = INDIO_DIRECT_MODE; in adc5_probe()
916 indio_dev->info = adc->data->info; in adc5_probe()
917 indio_dev->channels = adc->iio_chans; in adc5_probe()
918 indio_dev->num_channels = adc->nchannels; in adc5_probe()
925 .name = "qcom-spmi-adc5",
932 MODULE_ALIAS("platform:qcom-spmi-adc5");
933 MODULE_DESCRIPTION("Qualcomm Technologies Inc. PMIC5 ADC driver");