Lines Matching refs:iadc
117 static int iadc_read(struct iadc_chip *iadc, u16 offset, u8 *data) in iadc_read() argument
122 ret = regmap_read(iadc->regmap, iadc->base + offset, &val); in iadc_read()
130 static int iadc_write(struct iadc_chip *iadc, u16 offset, u8 data) in iadc_write() argument
132 return regmap_write(iadc->regmap, iadc->base + offset, data); in iadc_write()
135 static int iadc_reset(struct iadc_chip *iadc) in iadc_reset() argument
140 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA); in iadc_reset()
144 ret = iadc_read(iadc, IADC_PERH_RESET_CTL3, &data); in iadc_reset()
148 ret = iadc_write(iadc, IADC_SEC_ACCESS, IADC_SEC_ACCESS_DATA); in iadc_reset()
154 return iadc_write(iadc, IADC_PERH_RESET_CTL3, data); in iadc_reset()
157 static int iadc_set_state(struct iadc_chip *iadc, bool state) in iadc_set_state() argument
159 return iadc_write(iadc, IADC_EN_CTL1, state ? IADC_EN_CTL1_SET : 0); in iadc_set_state()
162 static void iadc_status_show(struct iadc_chip *iadc) in iadc_status_show() argument
167 ret = iadc_read(iadc, IADC_MODE_CTL, &mode); in iadc_status_show()
171 ret = iadc_read(iadc, IADC_DIG_PARAM, &dig); in iadc_status_show()
175 ret = iadc_read(iadc, IADC_CH_SEL_CTL, &chan); in iadc_status_show()
179 ret = iadc_read(iadc, IADC_CONV_REQ, &req); in iadc_status_show()
183 ret = iadc_read(iadc, IADC_STATUS1, &sta1); in iadc_status_show()
187 ret = iadc_read(iadc, IADC_EN_CTL1, &en); in iadc_status_show()
191 dev_err(iadc->dev, in iadc_status_show()
196 static int iadc_configure(struct iadc_chip *iadc, int channel) in iadc_configure() argument
203 ret = iadc_write(iadc, IADC_MODE_CTL, mode); in iadc_configure()
208 ret = iadc_write(iadc, IADC_CH_SEL_CTL, channel); in iadc_configure()
214 ret = iadc_write(iadc, IADC_DIG_PARAM, decim); in iadc_configure()
219 ret = iadc_write(iadc, IADC_HW_SETTLE_DELAY, IADC_DEF_HW_SETTLE_TIME); in iadc_configure()
223 ret = iadc_write(iadc, IADC_FAST_AVG_CTL, IADC_DEF_AVG_SAMPLES); in iadc_configure()
228 ret = iadc_write(iadc, IADC_FAST_AVG_EN, IADC_FAST_AVG_EN_SET); in iadc_configure()
230 ret = iadc_write(iadc, IADC_FAST_AVG_EN, 0); in iadc_configure()
235 if (!iadc->poll_eoc) in iadc_configure()
236 reinit_completion(&iadc->complete); in iadc_configure()
238 ret = iadc_set_state(iadc, true); in iadc_configure()
243 return iadc_write(iadc, IADC_CONV_REQ, IADC_CONV_REQ_SET); in iadc_configure()
246 static int iadc_poll_wait_eoc(struct iadc_chip *iadc, unsigned int interval_us) in iadc_poll_wait_eoc() argument
255 ret = iadc_read(iadc, IADC_STATUS1, &sta1); in iadc_poll_wait_eoc()
266 iadc_status_show(iadc); in iadc_poll_wait_eoc()
271 static int iadc_read_result(struct iadc_chip *iadc, u16 *data) in iadc_read_result() argument
273 return regmap_bulk_read(iadc->regmap, iadc->base + IADC_DATA, data, 2); in iadc_read_result()
276 static int iadc_do_conversion(struct iadc_chip *iadc, int chan, u16 *data) in iadc_do_conversion() argument
281 ret = iadc_configure(iadc, chan); in iadc_do_conversion()
287 if (iadc->poll_eoc) { in iadc_do_conversion()
288 ret = iadc_poll_wait_eoc(iadc, wait); in iadc_do_conversion()
290 ret = wait_for_completion_timeout(&iadc->complete, in iadc_do_conversion()
296 ret = iadc_poll_wait_eoc(iadc, IADC_CONV_TIME_MIN_US); in iadc_do_conversion()
300 ret = iadc_read_result(iadc, data); in iadc_do_conversion()
302 iadc_set_state(iadc, false); in iadc_do_conversion()
304 dev_err(iadc->dev, "conversion failed\n"); in iadc_do_conversion()
313 struct iadc_chip *iadc = iio_priv(indio_dev); in iadc_read_raw() local
320 mutex_lock(&iadc->lock); in iadc_read_raw()
321 ret = iadc_do_conversion(iadc, chan->channel, &adc_raw); in iadc_read_raw()
322 mutex_unlock(&iadc->lock); in iadc_read_raw()
326 vsense_raw = adc_raw - iadc->offset[chan->channel]; in iadc_read_raw()
329 vsense_uv /= (s32)iadc->gain - iadc->offset[chan->channel]; in iadc_read_raw()
331 isense_ua = vsense_uv / iadc->rsense[chan->channel]; in iadc_read_raw()
333 dev_dbg(iadc->dev, "off %d gain %d adc %d %duV I %duA\n", in iadc_read_raw()
334 iadc->offset[chan->channel], iadc->gain, in iadc_read_raw()
354 struct iadc_chip *iadc = dev_id; in iadc_isr() local
356 complete(&iadc->complete); in iadc_isr()
361 static int iadc_update_offset(struct iadc_chip *iadc) in iadc_update_offset() argument
365 ret = iadc_do_conversion(iadc, IADC_GAIN_17P857MV, &iadc->gain); in iadc_update_offset()
369 ret = iadc_do_conversion(iadc, IADC_INT_OFFSET_CSP2_CSN2, in iadc_update_offset()
370 &iadc->offset[IADC_INT_RSENSE]); in iadc_update_offset()
374 if (iadc->gain == iadc->offset[IADC_INT_RSENSE]) { in iadc_update_offset()
375 dev_err(iadc->dev, "error: internal offset == gain %d\n", in iadc_update_offset()
376 iadc->gain); in iadc_update_offset()
380 ret = iadc_do_conversion(iadc, IADC_EXT_OFFSET_CSP_CSN, in iadc_update_offset()
381 &iadc->offset[IADC_EXT_RSENSE]); in iadc_update_offset()
385 if (iadc->gain == iadc->offset[IADC_EXT_RSENSE]) { in iadc_update_offset()
386 dev_err(iadc->dev, "error: external offset == gain %d\n", in iadc_update_offset()
387 iadc->gain); in iadc_update_offset()
394 static int iadc_version_check(struct iadc_chip *iadc) in iadc_version_check() argument
399 ret = iadc_read(iadc, IADC_PERPH_TYPE, &val); in iadc_version_check()
404 dev_err(iadc->dev, "%d is not ADC\n", val); in iadc_version_check()
408 ret = iadc_read(iadc, IADC_PERPH_SUBTYPE, &val); in iadc_version_check()
413 dev_err(iadc->dev, "%d is not IADC\n", val); in iadc_version_check()
417 ret = iadc_read(iadc, IADC_REVISION2, &val); in iadc_version_check()
422 dev_err(iadc->dev, "revision %d not supported\n", val); in iadc_version_check()
429 static int iadc_rsense_read(struct iadc_chip *iadc, struct device_node *node) in iadc_rsense_read() argument
435 &iadc->rsense[IADC_EXT_RSENSE]); in iadc_rsense_read()
437 iadc->rsense[IADC_EXT_RSENSE] = IADC_INT_RSENSE_IDEAL_VALUE; in iadc_rsense_read()
439 if (!iadc->rsense[IADC_EXT_RSENSE]) { in iadc_rsense_read()
440 dev_err(iadc->dev, "external resistor can't be zero Ohms"); in iadc_rsense_read()
444 ret = iadc_read(iadc, IADC_NOMINAL_RSENSE, &deviation); in iadc_rsense_read()
461 iadc->rsense[IADC_INT_RSENSE] = int_sense; in iadc_rsense_read()
489 struct iadc_chip *iadc; in iadc_probe() local
493 indio_dev = devm_iio_device_alloc(dev, sizeof(*iadc)); in iadc_probe()
497 iadc = iio_priv(indio_dev); in iadc_probe()
498 iadc->dev = dev; in iadc_probe()
500 iadc->regmap = dev_get_regmap(dev->parent, NULL); in iadc_probe()
501 if (!iadc->regmap) in iadc_probe()
504 init_completion(&iadc->complete); in iadc_probe()
505 mutex_init(&iadc->lock); in iadc_probe()
511 iadc->base = res; in iadc_probe()
513 ret = iadc_version_check(iadc); in iadc_probe()
517 ret = iadc_rsense_read(iadc, node); in iadc_probe()
521 dev_dbg(iadc->dev, "sense resistors %d and %d micro Ohm\n", in iadc_probe()
522 iadc->rsense[IADC_INT_RSENSE], in iadc_probe()
523 iadc->rsense[IADC_EXT_RSENSE]); in iadc_probe()
530 iadc->poll_eoc = true; in iadc_probe()
532 ret = iadc_reset(iadc); in iadc_probe()
538 if (!iadc->poll_eoc) { in iadc_probe()
540 "spmi-iadc", iadc); in iadc_probe()
546 device_init_wakeup(iadc->dev, 1); in iadc_probe()
549 ret = iadc_update_offset(iadc); in iadc_probe()