Lines Matching +full:9 +full:- +full:channel

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
86 * struct twl6030_ideal_code - GPADC calibration parameters
90 * @channel: channel number
97 int channel; member
107 * struct twl6030_gpadc_platform_data - platform specific data
112 * @channel_to_reg: pointer to ADC function to convert channel to
120 int (*start_conversion)(int channel);
121 u8 (*channel_to_reg)(int channel);
126 * struct twl6030_gpadc_data - GPADC data
131 * channel with gain error and offset
153 .channel = 0,
160 .channel = 1,
167 .channel = 2,
174 .channel = 3,
181 .channel = 4,
188 .channel = 5,
195 .channel = 6,
202 .channel = 7,
209 .channel = 8,
215 [9] = { /* ch 9, internal, external charger input */
216 .channel = 9,
223 .channel = 10,
230 .channel = 11,
235 .channel = 14,
246 .channel = 0,
253 .channel = 1,
260 .channel = 2,
268 .channel = 3,
275 .channel = 4,
282 .channel = 5,
289 .channel = 6,
296 .channel = 7,
303 .channel = 8,
309 [9] = { /* ch 9, internal, external charger input */
310 .channel = 9,
317 .channel = 10,
323 [11] = { /* ch 11, internal, VBUS DC-DC output current */
324 .channel = 11,
333 .channel = 14,
342 .channel = 17,
345 .channel = 18,
387 complete(&gpadc->irq_complete); in twl6030_gpadc_irq_handler()
392 static int twl6030_start_conversion(int channel) in twl6030_start_conversion() argument
398 static int twl6032_start_conversion(int channel) in twl6032_start_conversion() argument
402 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel); in twl6032_start_conversion()
410 static u8 twl6030_channel_to_reg(int channel) in twl6030_channel_to_reg() argument
412 return TWL6030_GPADC_GPCH0_LSB + 2 * channel; in twl6030_channel_to_reg()
415 static u8 twl6032_channel_to_reg(int channel) in twl6032_channel_to_reg() argument
418 * for any prior chosen channel, when the conversion is ready in twl6032_channel_to_reg()
426 int channel, int size) in twl6030_gpadc_lookup() argument
431 if (ideal[i].channel == channel) in twl6030_gpadc_lookup()
438 *pdata, int channel) in twl6030_channel_calibrated() argument
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()
449 int channel, int raw_code) in twl6030_gpadc_make_correction() argument
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()
464 int channel, int *res) in twl6030_gpadc_get_raw() argument
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()
481 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code); in twl6030_gpadc_get_raw()
489 int channel, int *val) in twl6030_gpadc_get_processed() argument
491 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal; in twl6030_gpadc_get_processed()
497 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code); 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()
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()
569 * channel.
570 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
571 * offset: b = d1 + (k - 1) * x1
574 int channel, int d1, int d2) in twl6030_calibrate_channel() argument
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()
638 dev_err(gpadc->dev, "calibration failed\n"); in twl6030_calibration()
669 case 9: in twl6030_calibration()
671 d2 = trim_regs[9]; in twl6030_calibration()
703 val = -val; in twl6032_get_trim_value()
717 dev_err(gpadc->dev, "calibration failed\n"); in twl6032_calibration()
752 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7, in twl6032_calibration()
755 case 9: in twl6032_calibration()
779 d1 = -d1; in twl6032_calibration()
787 d2 = -d2; in twl6032_calibration()
804 .channel = chn, \
819 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
867 .compatible = "ti,twl6030-gpadc",
871 .compatible = "ti,twl6032-gpadc",
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()