Lines Matching +full:end +full:- +full:of +full:- +full:conversion
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2009-2013 Texas Instruments Inc.
13 * Based on twl4030-madc.c
74 * struct twl6030_chnl_calib - channel calibration
77 * @offset_error: offset of the real curve
86 * struct twl6030_ideal_code - GPADC calibration parameters
88 * to the and of the measurable input range
92 * @code2: ideal code for at the end of the range
94 * @volt2: voltage input at the end(high voltage)
107 * struct twl6030_gpadc_platform_data - platform specific data
108 * @nchannels: number of GPADC channels
111 * @start_conversion: pointer to ADC start conversion function
113 * register address for reading conversion result
126 * struct twl6030_gpadc_data - GPADC data
129 * @irq_complete: completion to signal end of conversion
323 [11] = { /* ch 11, internal, VBUS DC-DC output current */
387 complete(&gpadc->irq_complete); in twl6030_gpadc_irq_handler()
418 * for any prior chosen channel, when the conversion is ready in twl6032_channel_to_reg()
440 const struct twl6030_ideal_code *ideal = pdata->ideal; in twl6030_channel_calibrated()
443 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels); in twl6030_channel_calibrated()
445 return pdata->ideal[i].code2; in twl6030_channel_calibrated()
451 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_gpadc_make_correction()
455 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_gpadc_make_correction()
456 corrected_code = ((raw_code * 1000) - in twl6030_gpadc_make_correction()
457 gpadc->twl6030_cal_tbl[i].offset_error) / in twl6030_gpadc_make_correction()
458 gpadc->twl6030_cal_tbl[i].gain_error; in twl6030_gpadc_make_correction()
466 u8 reg = gpadc->pdata->channel_to_reg(channel); in twl6030_gpadc_get_raw()
473 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg); in twl6030_gpadc_get_raw()
478 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code); in twl6030_gpadc_get_raw()
480 if (twl6030_channel_calibrated(gpadc->pdata, channel)) in twl6030_gpadc_get_raw()
491 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_gpadc_get_processed()
501 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_gpadc_get_processed()
503 gpadc->twl6030_cal_tbl[i].gain; in twl6030_gpadc_get_processed()
508 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code); in twl6030_gpadc_get_processed()
509 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value); in twl6030_gpadc_get_processed()
524 mutex_lock(&gpadc->lock); in twl6030_gpadc_read_raw()
526 ret = gpadc->pdata->start_conversion(chan->channel); in twl6030_gpadc_read_raw()
528 dev_err(gpadc->dev, "failed to start conversion\n"); in twl6030_gpadc_read_raw()
531 /* wait for conversion to complete */ in twl6030_gpadc_read_raw()
533 &gpadc->irq_complete, msecs_to_jiffies(5000)); in twl6030_gpadc_read_raw()
535 ret = -ETIMEDOUT; in twl6030_gpadc_read_raw()
538 ret = -EINTR; in twl6030_gpadc_read_raw()
544 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val); in twl6030_gpadc_read_raw()
545 ret = ret ? -EIO : IIO_VAL_INT; in twl6030_gpadc_read_raw()
549 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val); in twl6030_gpadc_read_raw()
550 ret = ret ? -EIO : IIO_VAL_INT; in twl6030_gpadc_read_raw()
557 mutex_unlock(&gpadc->lock); in twl6030_gpadc_read_raw()
568 * The goal is to find offset and gain of the real curve for each calibrated
570 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
571 * offset: b = d1 + (k - 1) * x1
577 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_calibrate_channel()
579 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels); in twl6030_calibrate_channel()
582 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) / in twl6030_calibrate_channel()
583 (ideal[i].code2 - ideal[i].code1); in twl6030_calibrate_channel()
588 /* k - real curve gain */ in twl6030_calibrate_channel()
589 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1)); in twl6030_calibrate_channel()
591 /* b - offset of the real curve gain */ in twl6030_calibrate_channel()
592 b = (d1 * 1000) - (k - 1000) * x1; in twl6030_calibrate_channel()
594 gpadc->twl6030_cal_tbl[i].gain = gain; in twl6030_calibrate_channel()
595 gpadc->twl6030_cal_tbl[i].gain_error = k; in twl6030_calibrate_channel()
596 gpadc->twl6030_cal_tbl[i].offset_error = b; in twl6030_calibrate_channel()
598 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1); in twl6030_calibrate_channel()
599 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2); in twl6030_calibrate_channel()
600 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1); in twl6030_calibrate_channel()
601 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2); in twl6030_calibrate_channel()
602 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain); in twl6030_calibrate_channel()
603 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k); in twl6030_calibrate_channel()
604 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b); in twl6030_calibrate_channel()
611 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value in twl6030_gpadc_get_trim_offset()
613 * is absolute value, the correct conversion results are in twl6030_gpadc_get_trim_offset()
638 dev_err(gpadc->dev, "calibration failed\n"); in twl6030_calibration()
703 val = -val; in twl6032_get_trim_value()
717 dev_err(gpadc->dev, "calibration failed\n"); in twl6032_calibration()
779 d1 = -d1; in twl6032_calibration()
787 d2 = -d2; in twl6032_calibration()
867 .compatible = "ti,twl6030-gpadc",
871 .compatible = "ti,twl6032-gpadc",
874 { /* end */ }
876 MODULE_DEVICE_TABLE(of, of_twl6030_match_tbl);
880 struct device *dev = &pdev->dev; in twl6030_gpadc_probe()
887 pdata = device_get_match_data(&pdev->dev); in twl6030_gpadc_probe()
889 return -EINVAL; in twl6030_gpadc_probe()
893 return -ENOMEM; in twl6030_gpadc_probe()
897 gpadc->twl6030_cal_tbl = devm_kcalloc(dev, in twl6030_gpadc_probe()
898 pdata->nchannels, in twl6030_gpadc_probe()
899 sizeof(*gpadc->twl6030_cal_tbl), in twl6030_gpadc_probe()
901 if (!gpadc->twl6030_cal_tbl) in twl6030_gpadc_probe()
902 return -ENOMEM; in twl6030_gpadc_probe()
904 gpadc->dev = dev; in twl6030_gpadc_probe()
905 gpadc->pdata = pdata; in twl6030_gpadc_probe()
908 mutex_init(&gpadc->lock); in twl6030_gpadc_probe()
909 init_completion(&gpadc->irq_complete); in twl6030_gpadc_probe()
911 ret = pdata->calibrate(gpadc); in twl6030_gpadc_probe()
960 indio_dev->name = DRIVER_NAME; in twl6030_gpadc_probe()
961 indio_dev->info = &twl6030_gpadc_iio_info; in twl6030_gpadc_probe()
962 indio_dev->modes = INDIO_DIRECT_MODE; in twl6030_gpadc_probe()
963 indio_dev->channels = pdata->iio_channels; in twl6030_gpadc_probe()
964 indio_dev->num_channels = pdata->nchannels; in twl6030_gpadc_probe()