Lines Matching +full:external +full:- +full:resistor
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2009-2013 Texas Instruments Inc.
13 * Based on twl4030-madc.c
61 * struct twl6030_chnl_calib - channel calibration
73 * struct twl6030_ideal_code - GPADC calibration parameters
94 * struct twl6030_gpadc_platform_data - platform specific data
113 * struct twl6030_gpadc_data - GPADC data
139 [0] = { /* ch 0, external, battery type, resistor value */
146 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
153 [2] = { /* ch 2, external, audio accessory/general purpose */
160 [3] = { /* ch 3, external, general purpose */
167 [4] = { /* ch 4, external, temperature measurement/general purpose */
174 [5] = { /* ch 5, external, general purpose */
181 [6] = { /* ch 6, external, general purpose */
202 [9] = { /* ch 9, internal, external charger input */
232 [0] = { /* ch 0, external, battery type, resistor value */
239 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
246 [2] = { /* ch 2, external, audio accessory/general purpose */
253 [3] = { /* ch 3, external, temperature with external diode/general
261 [4] = { /* ch 4, external, temperature measurement/general purpose */
268 [5] = { /* ch 5, external, general purpose */
275 [6] = { /* ch 6, external, general purpose */
296 [9] = { /* ch 9, internal, external charger input */
310 [11] = { /* ch 11, internal, VBUS DC-DC output current */
374 complete(&gpadc->irq_complete); in twl6030_gpadc_irq_handler()
427 const struct twl6030_ideal_code *ideal = pdata->ideal; in twl6030_channel_calibrated()
430 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels); in twl6030_channel_calibrated()
432 return pdata->ideal[i].code2; in twl6030_channel_calibrated()
438 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_gpadc_make_correction()
442 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_gpadc_make_correction()
443 corrected_code = ((raw_code * 1000) - in twl6030_gpadc_make_correction()
444 gpadc->twl6030_cal_tbl[i].offset_error) / in twl6030_gpadc_make_correction()
445 gpadc->twl6030_cal_tbl[i].gain_error; in twl6030_gpadc_make_correction()
453 u8 reg = gpadc->pdata->channel_to_reg(channel); in twl6030_gpadc_get_raw()
460 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg); in twl6030_gpadc_get_raw()
465 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code); in twl6030_gpadc_get_raw()
467 if (twl6030_channel_calibrated(gpadc->pdata, channel)) in twl6030_gpadc_get_raw()
478 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_gpadc_get_processed()
488 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_gpadc_get_processed()
490 gpadc->twl6030_cal_tbl[i].gain; in twl6030_gpadc_get_processed()
495 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code); in twl6030_gpadc_get_processed()
496 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value); in twl6030_gpadc_get_processed()
511 mutex_lock(&gpadc->lock); in twl6030_gpadc_read_raw()
513 ret = gpadc->pdata->start_conversion(chan->channel); in twl6030_gpadc_read_raw()
515 dev_err(gpadc->dev, "failed to start conversion\n"); in twl6030_gpadc_read_raw()
520 &gpadc->irq_complete, msecs_to_jiffies(5000)); in twl6030_gpadc_read_raw()
522 ret = -ETIMEDOUT; in twl6030_gpadc_read_raw()
525 ret = -EINTR; in twl6030_gpadc_read_raw()
531 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val); in twl6030_gpadc_read_raw()
532 ret = ret ? -EIO : IIO_VAL_INT; in twl6030_gpadc_read_raw()
536 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val); in twl6030_gpadc_read_raw()
537 ret = ret ? -EIO : IIO_VAL_INT; in twl6030_gpadc_read_raw()
544 mutex_unlock(&gpadc->lock); in twl6030_gpadc_read_raw()
557 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
558 * offset: b = d1 + (k - 1) * x1
564 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_calibrate_channel()
566 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_calibrate_channel()
569 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) / in twl6030_calibrate_channel()
570 (ideal[i].code2 - ideal[i].code1); in twl6030_calibrate_channel()
575 /* k - real curve gain */ in twl6030_calibrate_channel()
576 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1)); in twl6030_calibrate_channel()
578 /* b - offset of the real curve gain */ in twl6030_calibrate_channel()
579 b = (d1 * 1000) - (k - 1000) * x1; in twl6030_calibrate_channel()
581 gpadc->twl6030_cal_tbl[i].gain = gain; in twl6030_calibrate_channel()
582 gpadc->twl6030_cal_tbl[i].gain_error = k; in twl6030_calibrate_channel()
583 gpadc->twl6030_cal_tbl[i].offset_error = b; in twl6030_calibrate_channel()
585 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1); in twl6030_calibrate_channel()
586 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2); in twl6030_calibrate_channel()
587 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1); in twl6030_calibrate_channel()
588 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2); in twl6030_calibrate_channel()
589 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain); in twl6030_calibrate_channel()
590 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k); in twl6030_calibrate_channel()
591 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b); in twl6030_calibrate_channel()
598 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value in twl6030_gpadc_get_trim_offset()
625 dev_err(gpadc->dev, "calibration failed\n"); in twl6030_calibration()
690 val = -val; in twl6032_get_trim_value()
704 dev_err(gpadc->dev, "calibration failed\n"); in twl6032_calibration()
766 d1 = -d1; in twl6032_calibration()
774 d2 = -d2; in twl6032_calibration()
854 .compatible = "ti,twl6030-gpadc",
858 .compatible = "ti,twl6032-gpadc",
867 struct device *dev = &pdev->dev; in twl6030_gpadc_probe()
877 return -EINVAL; in twl6030_gpadc_probe()
879 pdata = match->data; in twl6030_gpadc_probe()
883 return -ENOMEM; in twl6030_gpadc_probe()
887 gpadc->twl6030_cal_tbl = devm_kcalloc(dev, in twl6030_gpadc_probe()
888 pdata->nchannels, in twl6030_gpadc_probe()
889 sizeof(*gpadc->twl6030_cal_tbl), in twl6030_gpadc_probe()
891 if (!gpadc->twl6030_cal_tbl) in twl6030_gpadc_probe()
892 return -ENOMEM; in twl6030_gpadc_probe()
894 gpadc->dev = dev; in twl6030_gpadc_probe()
895 gpadc->pdata = pdata; in twl6030_gpadc_probe()
898 mutex_init(&gpadc->lock); in twl6030_gpadc_probe()
899 init_completion(&gpadc->irq_complete); in twl6030_gpadc_probe()
901 ret = pdata->calibrate(gpadc); in twl6030_gpadc_probe()
903 dev_err(&pdev->dev, "failed to read calibration registers\n"); in twl6030_gpadc_probe()
917 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n"); in twl6030_gpadc_probe()
924 dev_err(&pdev->dev, "failed to enable GPADC module\n"); in twl6030_gpadc_probe()
928 indio_dev->name = DRIVER_NAME; in twl6030_gpadc_probe()
929 indio_dev->info = &twl6030_gpadc_iio_info; in twl6030_gpadc_probe()
930 indio_dev->modes = INDIO_DIRECT_MODE; in twl6030_gpadc_probe()
931 indio_dev->channels = pdata->iio_channels; in twl6030_gpadc_probe()
932 indio_dev->num_channels = pdata->nchannels; in twl6030_gpadc_probe()