Lines Matching +full:gain +full:- +full:milli
1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADS1100 - Texas Instruments Analog-to-Digital Converter
8 * IIO driver for ADS1100 and ADS1000 ADC 16-bit I2C
34 /* Gain */
49 int scale_avail[2 * 4]; /* 4 gain settings */
73 u8 config = (data->config & ~mask) | (value & mask); in ads1100_set_config_bits()
75 if (data->config == config) in ads1100_set_config_bits()
78 ret = i2c_master_send(data->client, &config, 1); in ads1100_set_config_bits()
82 data->config = config; in ads1100_set_config_bits()
89 return ads1100_data_rate_bits[FIELD_GET(ADS1100_DR_MASK, data->config)]; in ads1100_data_bits()
99 return -EINVAL; in ads1100_get_adc_result()
101 ret = pm_runtime_resume_and_get(&data->client->dev); in ads1100_get_adc_result()
105 ret = i2c_master_recv(data->client, (char *)&buffer, sizeof(buffer)); in ads1100_get_adc_result()
107 pm_runtime_mark_last_busy(&data->client->dev); in ads1100_get_adc_result()
108 pm_runtime_put_autosuspend(&data->client->dev); in ads1100_get_adc_result()
111 dev_err(&data->client->dev, "I2C read fail: %d\n", ret); in ads1100_get_adc_result()
115 /* Value is always 16-bit 2's complement */ in ads1100_get_adc_result()
119 value <<= 16 - ads1100_data_bits(data); in ads1100_get_adc_result()
129 int gain; in ads1100_set_scale() local
133 return -EINVAL; in ads1100_set_scale()
136 return -EINVAL; in ads1100_set_scale()
138 microvolts = regulator_get_voltage(data->reg_vdd); in ads1100_set_scale()
142 * the full-scale value is d/n, corresponds to 2^15, in ads1100_set_scale()
143 * hence the gain = (d / n) >> 15, factoring out the 1000 and moving the in ads1100_set_scale()
144 * bitshift so everything fits in 32-bits yields this formula. in ads1100_set_scale()
146 gain = DIV_ROUND_CLOSEST(microvolts, BIT(15)) * MILLI / val2; in ads1100_set_scale()
147 if (gain < BIT(0) || gain > BIT(3)) in ads1100_set_scale()
148 return -EINVAL; in ads1100_set_scale()
150 ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain) - 1); in ads1100_set_scale()
160 size = data->supports_data_rate ? ARRAY_SIZE(ads1100_data_rate) : 1; in ads1100_set_data_rate()
167 return -EINVAL; in ads1100_set_data_rate()
172 return regulator_get_voltage(data->reg_vdd) / (MICRO / MILLI); in ads1100_get_vdd_millivolts()
180 for (i = 0; i < ARRAY_SIZE(data->scale_avail) / 2; i++) { in ads1100_calc_scale_avail()
181 data->scale_avail[i * 2 + 0] = millivolts; in ads1100_calc_scale_avail()
182 data->scale_avail[i * 2 + 1] = 15 + i; in ads1100_calc_scale_avail()
193 if (chan->type != IIO_VOLTAGE) in ads1100_read_avail()
194 return -EINVAL; in ads1100_read_avail()
200 if (data->supports_data_rate) in ads1100_read_avail()
207 *vals = data->scale_avail; in ads1100_read_avail()
208 *length = ARRAY_SIZE(data->scale_avail); in ads1100_read_avail()
211 return -EINVAL; in ads1100_read_avail()
222 mutex_lock(&data->lock); in ads1100_read_raw()
229 ret = ads1100_get_adc_result(data, chan->address, val); in ads1100_read_raw()
235 /* full-scale is the supply voltage in millivolts */ in ads1100_read_raw()
237 *val2 = 15 + FIELD_GET(ADS1100_PGA_MASK, data->config); in ads1100_read_raw()
242 data->config)]; in ads1100_read_raw()
246 ret = -EINVAL; in ads1100_read_raw()
249 mutex_unlock(&data->lock); in ads1100_read_raw()
261 mutex_lock(&data->lock); in ads1100_write_raw()
267 ret = ads1100_set_data_rate(data, chan->address, val); in ads1100_write_raw()
270 ret = -EINVAL; in ads1100_write_raw()
273 mutex_unlock(&data->lock); in ads1100_write_raw()
291 ret = i2c_master_send(data->client, buffer, 1); in ads1100_setup()
295 ret = i2c_master_recv(data->client, buffer, sizeof(buffer)); in ads1100_setup()
300 data->config = buffer[2] & ~ADS1100_CFG_ST_BSY; in ads1100_setup()
303 data->supports_data_rate = FIELD_GET(ADS1100_DR_MASK, buffer[2]) != 0; in ads1100_setup()
322 struct device *dev = &client->dev; in ads1100_probe()
327 return -ENOMEM; in ads1100_probe()
331 data->client = client; in ads1100_probe()
332 mutex_init(&data->lock); in ads1100_probe()
334 indio_dev->name = "ads1100"; in ads1100_probe()
335 indio_dev->modes = INDIO_DIRECT_MODE; in ads1100_probe()
336 indio_dev->channels = &ads1100_channel; in ads1100_probe()
337 indio_dev->num_channels = 1; in ads1100_probe()
338 indio_dev->info = &ads1100_info; in ads1100_probe()
340 data->reg_vdd = devm_regulator_get(dev, "vdd"); in ads1100_probe()
341 if (IS_ERR(data->reg_vdd)) in ads1100_probe()
342 return dev_err_probe(dev, PTR_ERR(data->reg_vdd), in ads1100_probe()
345 ret = regulator_enable(data->reg_vdd); in ads1100_probe()
350 ret = devm_add_action_or_reset(dev, ads1100_reg_disable, data->reg_vdd); in ads1100_probe()
385 regulator_disable(data->reg_vdd); in ads1100_runtime_suspend()
395 ret = regulator_enable(data->reg_vdd); in ads1100_runtime_resume()
397 dev_err(&data->client->dev, "Failed to enable Vdd\n"); in ads1100_runtime_resume()
404 * has been power-cycled, we'll re-write its config register now. in ads1100_runtime_resume()