xref: /openbmc/linux/drivers/iio/adc/ti-ads1100.c (revision 7cf15f42)
154188054SMike Looijmans // SPDX-License-Identifier: GPL-2.0-only
254188054SMike Looijmans /*
354188054SMike Looijmans  * ADS1100 - Texas Instruments Analog-to-Digital Converter
454188054SMike Looijmans  *
554188054SMike Looijmans  * Copyright (c) 2023, Topic Embedded Products
654188054SMike Looijmans  *
754188054SMike Looijmans  * Datasheet: https://www.ti.com/lit/gpn/ads1100
854188054SMike Looijmans  * IIO driver for ADS1100 and ADS1000 ADC 16-bit I2C
954188054SMike Looijmans  */
1054188054SMike Looijmans 
1154188054SMike Looijmans #include <linux/bitfield.h>
1254188054SMike Looijmans #include <linux/bits.h>
1354188054SMike Looijmans #include <linux/delay.h>
1454188054SMike Looijmans #include <linux/module.h>
1554188054SMike Looijmans #include <linux/init.h>
1654188054SMike Looijmans #include <linux/i2c.h>
1754188054SMike Looijmans #include <linux/mutex.h>
1854188054SMike Looijmans #include <linux/property.h>
1954188054SMike Looijmans #include <linux/pm_runtime.h>
2054188054SMike Looijmans #include <linux/regulator/consumer.h>
2154188054SMike Looijmans #include <linux/units.h>
2254188054SMike Looijmans 
2354188054SMike Looijmans #include <linux/iio/iio.h>
2454188054SMike Looijmans #include <linux/iio/types.h>
2554188054SMike Looijmans 
2654188054SMike Looijmans /* The ADS1100 has a single byte config register */
2754188054SMike Looijmans 
2854188054SMike Looijmans /* Conversion in progress bit */
2954188054SMike Looijmans #define ADS1100_CFG_ST_BSY	BIT(7)
3054188054SMike Looijmans /* Single conversion bit */
3154188054SMike Looijmans #define ADS1100_CFG_SC		BIT(4)
3254188054SMike Looijmans /* Data rate */
3354188054SMike Looijmans #define ADS1100_DR_MASK		GENMASK(3, 2)
3454188054SMike Looijmans /* Gain */
3554188054SMike Looijmans #define ADS1100_PGA_MASK	GENMASK(1, 0)
3654188054SMike Looijmans 
3754188054SMike Looijmans #define ADS1100_CONTINUOUS	0
3854188054SMike Looijmans #define	ADS1100_SINGLESHOT	ADS1100_CFG_SC
3954188054SMike Looijmans 
4054188054SMike Looijmans #define ADS1100_SLEEP_DELAY_MS	2000
4154188054SMike Looijmans 
4254188054SMike Looijmans static const int ads1100_data_rate[] = { 128, 32, 16, 8 };
4354188054SMike Looijmans static const int ads1100_data_rate_bits[] = { 12, 14, 15, 16 };
4454188054SMike Looijmans 
4554188054SMike Looijmans struct ads1100_data {
4654188054SMike Looijmans 	struct i2c_client *client;
4754188054SMike Looijmans 	struct regulator *reg_vdd;
4854188054SMike Looijmans 	struct mutex lock;
4954188054SMike Looijmans 	int scale_avail[2 * 4]; /* 4 gain settings */
5054188054SMike Looijmans 	u8 config;
5154188054SMike Looijmans 	bool supports_data_rate; /* Only the ADS1100 can select the rate */
5254188054SMike Looijmans };
5354188054SMike Looijmans 
5454188054SMike Looijmans static const struct iio_chan_spec ads1100_channel = {
5554188054SMike Looijmans 	.type = IIO_VOLTAGE,
5654188054SMike Looijmans 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
5754188054SMike Looijmans 	.info_mask_shared_by_all =
5854188054SMike Looijmans 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
5954188054SMike Looijmans 	.info_mask_shared_by_all_available =
6054188054SMike Looijmans 	    BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
6154188054SMike Looijmans 	.scan_type = {
6254188054SMike Looijmans 		      .sign = 's',
6354188054SMike Looijmans 		      .realbits = 16,
6454188054SMike Looijmans 		      .storagebits = 16,
6554188054SMike Looijmans 		      .endianness = IIO_CPU,
6654188054SMike Looijmans 		       },
6754188054SMike Looijmans 	.datasheet_name = "AIN",
6854188054SMike Looijmans };
6954188054SMike Looijmans 
ads1100_set_config_bits(struct ads1100_data * data,u8 mask,u8 value)7054188054SMike Looijmans static int ads1100_set_config_bits(struct ads1100_data *data, u8 mask, u8 value)
7154188054SMike Looijmans {
7254188054SMike Looijmans 	int ret;
7354188054SMike Looijmans 	u8 config = (data->config & ~mask) | (value & mask);
7454188054SMike Looijmans 
7554188054SMike Looijmans 	if (data->config == config)
7654188054SMike Looijmans 		return 0;	/* Already done */
7754188054SMike Looijmans 
7854188054SMike Looijmans 	ret = i2c_master_send(data->client, &config, 1);
7954188054SMike Looijmans 	if (ret < 0)
8054188054SMike Looijmans 		return ret;
8154188054SMike Looijmans 
8254188054SMike Looijmans 	data->config = config;
8354188054SMike Looijmans 
8454188054SMike Looijmans 	return 0;
8554188054SMike Looijmans };
8654188054SMike Looijmans 
ads1100_data_bits(struct ads1100_data * data)8754188054SMike Looijmans static int ads1100_data_bits(struct ads1100_data *data)
8854188054SMike Looijmans {
8954188054SMike Looijmans 	return ads1100_data_rate_bits[FIELD_GET(ADS1100_DR_MASK, data->config)];
9054188054SMike Looijmans }
9154188054SMike Looijmans 
ads1100_get_adc_result(struct ads1100_data * data,int chan,int * val)9254188054SMike Looijmans static int ads1100_get_adc_result(struct ads1100_data *data, int chan, int *val)
9354188054SMike Looijmans {
9454188054SMike Looijmans 	int ret;
9554188054SMike Looijmans 	__be16 buffer;
9654188054SMike Looijmans 	s16 value;
9754188054SMike Looijmans 
9854188054SMike Looijmans 	if (chan != 0)
9954188054SMike Looijmans 		return -EINVAL;
10054188054SMike Looijmans 
10154188054SMike Looijmans 	ret = pm_runtime_resume_and_get(&data->client->dev);
10254188054SMike Looijmans 	if (ret < 0)
10354188054SMike Looijmans 		return ret;
10454188054SMike Looijmans 
10554188054SMike Looijmans 	ret = i2c_master_recv(data->client, (char *)&buffer, sizeof(buffer));
10654188054SMike Looijmans 
10754188054SMike Looijmans 	pm_runtime_mark_last_busy(&data->client->dev);
10854188054SMike Looijmans 	pm_runtime_put_autosuspend(&data->client->dev);
10954188054SMike Looijmans 
11054188054SMike Looijmans 	if (ret < 0) {
11154188054SMike Looijmans 		dev_err(&data->client->dev, "I2C read fail: %d\n", ret);
11254188054SMike Looijmans 		return ret;
11354188054SMike Looijmans 	}
11454188054SMike Looijmans 
11554188054SMike Looijmans 	/* Value is always 16-bit 2's complement */
11654188054SMike Looijmans 	value = be16_to_cpu(buffer);
11754188054SMike Looijmans 
11854188054SMike Looijmans 	/* Shift result to compensate for bit resolution vs. sample rate */
11954188054SMike Looijmans 	value <<= 16 - ads1100_data_bits(data);
12054188054SMike Looijmans 
12154188054SMike Looijmans 	*val = sign_extend32(value, 15);
12254188054SMike Looijmans 
12354188054SMike Looijmans 	return 0;
12454188054SMike Looijmans }
12554188054SMike Looijmans 
ads1100_set_scale(struct ads1100_data * data,int val,int val2)12654188054SMike Looijmans static int ads1100_set_scale(struct ads1100_data *data, int val, int val2)
12754188054SMike Looijmans {
12854188054SMike Looijmans 	int microvolts;
12954188054SMike Looijmans 	int gain;
13054188054SMike Looijmans 
13154188054SMike Looijmans 	/* With Vdd between 2.7 and 5V, the scale is always below 1 */
13254188054SMike Looijmans 	if (val)
13354188054SMike Looijmans 		return -EINVAL;
13454188054SMike Looijmans 
13554188054SMike Looijmans 	if (!val2)
13654188054SMike Looijmans 		return -EINVAL;
13754188054SMike Looijmans 
13854188054SMike Looijmans 	microvolts = regulator_get_voltage(data->reg_vdd);
13954188054SMike Looijmans 	/*
14054188054SMike Looijmans 	 * val2 is in 'micro' units, n = val2 / 1000000
14154188054SMike Looijmans 	 * result must be millivolts, d = microvolts / 1000
14254188054SMike Looijmans 	 * the full-scale value is d/n, corresponds to 2^15,
14354188054SMike Looijmans 	 * hence the gain = (d / n) >> 15, factoring out the 1000 and moving the
14454188054SMike Looijmans 	 * bitshift so everything fits in 32-bits yields this formula.
14554188054SMike Looijmans 	 */
14654188054SMike Looijmans 	gain = DIV_ROUND_CLOSEST(microvolts, BIT(15)) * MILLI / val2;
14754188054SMike Looijmans 	if (gain < BIT(0) || gain > BIT(3))
14854188054SMike Looijmans 		return -EINVAL;
14954188054SMike Looijmans 
15054188054SMike Looijmans 	ads1100_set_config_bits(data, ADS1100_PGA_MASK, ffs(gain) - 1);
15154188054SMike Looijmans 
15254188054SMike Looijmans 	return 0;
15354188054SMike Looijmans }
15454188054SMike Looijmans 
ads1100_set_data_rate(struct ads1100_data * data,int chan,int rate)15554188054SMike Looijmans static int ads1100_set_data_rate(struct ads1100_data *data, int chan, int rate)
15654188054SMike Looijmans {
15754188054SMike Looijmans 	unsigned int i;
15854188054SMike Looijmans 	unsigned int size;
15954188054SMike Looijmans 
16054188054SMike Looijmans 	size = data->supports_data_rate ? ARRAY_SIZE(ads1100_data_rate) : 1;
16154188054SMike Looijmans 	for (i = 0; i < size; i++) {
16254188054SMike Looijmans 		if (ads1100_data_rate[i] == rate)
16354188054SMike Looijmans 			return ads1100_set_config_bits(data, ADS1100_DR_MASK,
16454188054SMike Looijmans 						       FIELD_PREP(ADS1100_DR_MASK, i));
16554188054SMike Looijmans 	}
16654188054SMike Looijmans 
16754188054SMike Looijmans 	return -EINVAL;
16854188054SMike Looijmans }
16954188054SMike Looijmans 
ads1100_get_vdd_millivolts(struct ads1100_data * data)17054188054SMike Looijmans static int ads1100_get_vdd_millivolts(struct ads1100_data *data)
17154188054SMike Looijmans {
17254188054SMike Looijmans 	return regulator_get_voltage(data->reg_vdd) / (MICRO / MILLI);
17354188054SMike Looijmans }
17454188054SMike Looijmans 
ads1100_calc_scale_avail(struct ads1100_data * data)17554188054SMike Looijmans static void ads1100_calc_scale_avail(struct ads1100_data *data)
17654188054SMike Looijmans {
17754188054SMike Looijmans 	int millivolts = ads1100_get_vdd_millivolts(data);
17854188054SMike Looijmans 	unsigned int i;
17954188054SMike Looijmans 
18054188054SMike Looijmans 	for (i = 0; i < ARRAY_SIZE(data->scale_avail) / 2; i++) {
18154188054SMike Looijmans 		data->scale_avail[i * 2 + 0] = millivolts;
18254188054SMike Looijmans 		data->scale_avail[i * 2 + 1] = 15 + i;
18354188054SMike Looijmans 	}
18454188054SMike Looijmans }
18554188054SMike Looijmans 
ads1100_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)18654188054SMike Looijmans static int ads1100_read_avail(struct iio_dev *indio_dev,
18754188054SMike Looijmans 			      struct iio_chan_spec const *chan,
18854188054SMike Looijmans 			      const int **vals, int *type, int *length,
18954188054SMike Looijmans 			      long mask)
19054188054SMike Looijmans {
19154188054SMike Looijmans 	struct ads1100_data *data = iio_priv(indio_dev);
19254188054SMike Looijmans 
19354188054SMike Looijmans 	if (chan->type != IIO_VOLTAGE)
19454188054SMike Looijmans 		return -EINVAL;
19554188054SMike Looijmans 
19654188054SMike Looijmans 	switch (mask) {
19754188054SMike Looijmans 	case IIO_CHAN_INFO_SAMP_FREQ:
19854188054SMike Looijmans 		*type = IIO_VAL_INT;
19954188054SMike Looijmans 		*vals = ads1100_data_rate;
20054188054SMike Looijmans 		if (data->supports_data_rate)
20154188054SMike Looijmans 			*length = ARRAY_SIZE(ads1100_data_rate);
20254188054SMike Looijmans 		else
20354188054SMike Looijmans 			*length = 1;
20454188054SMike Looijmans 		return IIO_AVAIL_LIST;
20554188054SMike Looijmans 	case IIO_CHAN_INFO_SCALE:
20654188054SMike Looijmans 		*type = IIO_VAL_FRACTIONAL_LOG2;
20754188054SMike Looijmans 		*vals = data->scale_avail;
20854188054SMike Looijmans 		*length = ARRAY_SIZE(data->scale_avail);
20954188054SMike Looijmans 		return IIO_AVAIL_LIST;
21054188054SMike Looijmans 	default:
21154188054SMike Looijmans 		return -EINVAL;
21254188054SMike Looijmans 	}
21354188054SMike Looijmans }
21454188054SMike Looijmans 
ads1100_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)21554188054SMike Looijmans static int ads1100_read_raw(struct iio_dev *indio_dev,
21654188054SMike Looijmans 			    struct iio_chan_spec const *chan, int *val,
21754188054SMike Looijmans 			    int *val2, long mask)
21854188054SMike Looijmans {
21954188054SMike Looijmans 	int ret;
22054188054SMike Looijmans 	struct ads1100_data *data = iio_priv(indio_dev);
22154188054SMike Looijmans 
22254188054SMike Looijmans 	mutex_lock(&data->lock);
22354188054SMike Looijmans 	switch (mask) {
22454188054SMike Looijmans 	case IIO_CHAN_INFO_RAW:
22554188054SMike Looijmans 		ret = iio_device_claim_direct_mode(indio_dev);
22654188054SMike Looijmans 		if (ret)
22754188054SMike Looijmans 			break;
22854188054SMike Looijmans 
22954188054SMike Looijmans 		ret = ads1100_get_adc_result(data, chan->address, val);
23054188054SMike Looijmans 		if (ret >= 0)
23154188054SMike Looijmans 			ret = IIO_VAL_INT;
23254188054SMike Looijmans 		iio_device_release_direct_mode(indio_dev);
23354188054SMike Looijmans 		break;
23454188054SMike Looijmans 	case IIO_CHAN_INFO_SCALE:
23554188054SMike Looijmans 		/* full-scale is the supply voltage in millivolts */
23654188054SMike Looijmans 		*val = ads1100_get_vdd_millivolts(data);
23754188054SMike Looijmans 		*val2 = 15 + FIELD_GET(ADS1100_PGA_MASK, data->config);
23854188054SMike Looijmans 		ret = IIO_VAL_FRACTIONAL_LOG2;
23954188054SMike Looijmans 		break;
24054188054SMike Looijmans 	case IIO_CHAN_INFO_SAMP_FREQ:
24154188054SMike Looijmans 		*val = ads1100_data_rate[FIELD_GET(ADS1100_DR_MASK,
24254188054SMike Looijmans 						   data->config)];
24354188054SMike Looijmans 		ret = IIO_VAL_INT;
24454188054SMike Looijmans 		break;
24554188054SMike Looijmans 	default:
24654188054SMike Looijmans 		ret = -EINVAL;
24754188054SMike Looijmans 		break;
24854188054SMike Looijmans 	}
24954188054SMike Looijmans 	mutex_unlock(&data->lock);
25054188054SMike Looijmans 
25154188054SMike Looijmans 	return ret;
25254188054SMike Looijmans }
25354188054SMike Looijmans 
ads1100_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)25454188054SMike Looijmans static int ads1100_write_raw(struct iio_dev *indio_dev,
25554188054SMike Looijmans 			     struct iio_chan_spec const *chan, int val,
25654188054SMike Looijmans 			     int val2, long mask)
25754188054SMike Looijmans {
25854188054SMike Looijmans 	struct ads1100_data *data = iio_priv(indio_dev);
25954188054SMike Looijmans 	int ret;
26054188054SMike Looijmans 
26154188054SMike Looijmans 	mutex_lock(&data->lock);
26254188054SMike Looijmans 	switch (mask) {
26354188054SMike Looijmans 	case IIO_CHAN_INFO_SCALE:
26454188054SMike Looijmans 		ret = ads1100_set_scale(data, val, val2);
26554188054SMike Looijmans 		break;
26654188054SMike Looijmans 	case IIO_CHAN_INFO_SAMP_FREQ:
26754188054SMike Looijmans 		ret = ads1100_set_data_rate(data, chan->address, val);
26854188054SMike Looijmans 		break;
26954188054SMike Looijmans 	default:
27054188054SMike Looijmans 		ret = -EINVAL;
27154188054SMike Looijmans 		break;
27254188054SMike Looijmans 	}
27354188054SMike Looijmans 	mutex_unlock(&data->lock);
27454188054SMike Looijmans 
27554188054SMike Looijmans 	return ret;
27654188054SMike Looijmans }
27754188054SMike Looijmans 
27854188054SMike Looijmans static const struct iio_info ads1100_info = {
27954188054SMike Looijmans 	.read_avail = ads1100_read_avail,
28054188054SMike Looijmans 	.read_raw = ads1100_read_raw,
28154188054SMike Looijmans 	.write_raw = ads1100_write_raw,
28254188054SMike Looijmans };
28354188054SMike Looijmans 
ads1100_setup(struct ads1100_data * data)28454188054SMike Looijmans static int ads1100_setup(struct ads1100_data *data)
28554188054SMike Looijmans {
28654188054SMike Looijmans 	int ret;
28754188054SMike Looijmans 	u8 buffer[3];
28854188054SMike Looijmans 
28954188054SMike Looijmans 	/* Setup continuous sampling mode at 8sps */
29054188054SMike Looijmans 	buffer[0] = ADS1100_DR_MASK | ADS1100_CONTINUOUS;
29154188054SMike Looijmans 	ret = i2c_master_send(data->client, buffer, 1);
29254188054SMike Looijmans 	if (ret < 0)
29354188054SMike Looijmans 		return ret;
29454188054SMike Looijmans 
29554188054SMike Looijmans 	ret = i2c_master_recv(data->client, buffer, sizeof(buffer));
29654188054SMike Looijmans 	if (ret < 0)
29754188054SMike Looijmans 		return ret;
29854188054SMike Looijmans 
29954188054SMike Looijmans 	/* Config register returned in third byte, strip away the busy status */
30054188054SMike Looijmans 	data->config = buffer[2] & ~ADS1100_CFG_ST_BSY;
30154188054SMike Looijmans 
30254188054SMike Looijmans 	/* Detect the sample rate capability by checking the DR bits */
30354188054SMike Looijmans 	data->supports_data_rate = FIELD_GET(ADS1100_DR_MASK, buffer[2]) != 0;
30454188054SMike Looijmans 
30554188054SMike Looijmans 	return 0;
30654188054SMike Looijmans }
30754188054SMike Looijmans 
ads1100_reg_disable(void * reg)30854188054SMike Looijmans static void ads1100_reg_disable(void *reg)
30954188054SMike Looijmans {
31054188054SMike Looijmans 	regulator_disable(reg);
31154188054SMike Looijmans }
31254188054SMike Looijmans 
ads1100_disable_continuous(void * data)31354188054SMike Looijmans static void ads1100_disable_continuous(void *data)
31454188054SMike Looijmans {
31554188054SMike Looijmans 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
31654188054SMike Looijmans }
31754188054SMike Looijmans 
ads1100_probe(struct i2c_client * client)31854188054SMike Looijmans static int ads1100_probe(struct i2c_client *client)
31954188054SMike Looijmans {
32054188054SMike Looijmans 	struct iio_dev *indio_dev;
32154188054SMike Looijmans 	struct ads1100_data *data;
32254188054SMike Looijmans 	struct device *dev = &client->dev;
32354188054SMike Looijmans 	int ret;
32454188054SMike Looijmans 
32554188054SMike Looijmans 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
32654188054SMike Looijmans 	if (!indio_dev)
32754188054SMike Looijmans 		return -ENOMEM;
32854188054SMike Looijmans 
32954188054SMike Looijmans 	data = iio_priv(indio_dev);
33054188054SMike Looijmans 	dev_set_drvdata(dev, data);
33154188054SMike Looijmans 	data->client = client;
33254188054SMike Looijmans 	mutex_init(&data->lock);
33354188054SMike Looijmans 
33454188054SMike Looijmans 	indio_dev->name = "ads1100";
33554188054SMike Looijmans 	indio_dev->modes = INDIO_DIRECT_MODE;
33654188054SMike Looijmans 	indio_dev->channels = &ads1100_channel;
33754188054SMike Looijmans 	indio_dev->num_channels = 1;
33854188054SMike Looijmans 	indio_dev->info = &ads1100_info;
33954188054SMike Looijmans 
34054188054SMike Looijmans 	data->reg_vdd = devm_regulator_get(dev, "vdd");
34154188054SMike Looijmans 	if (IS_ERR(data->reg_vdd))
34254188054SMike Looijmans 		return dev_err_probe(dev, PTR_ERR(data->reg_vdd),
34354188054SMike Looijmans 				     "Failed to get vdd regulator\n");
34454188054SMike Looijmans 
34554188054SMike Looijmans 	ret = regulator_enable(data->reg_vdd);
34654188054SMike Looijmans 	if (ret < 0)
347c4351b64SDan Carpenter 		return dev_err_probe(dev, ret,
34854188054SMike Looijmans 				     "Failed to enable vdd regulator\n");
34954188054SMike Looijmans 
35054188054SMike Looijmans 	ret = devm_add_action_or_reset(dev, ads1100_reg_disable, data->reg_vdd);
35154188054SMike Looijmans 	if (ret)
35254188054SMike Looijmans 		return ret;
35354188054SMike Looijmans 
35454188054SMike Looijmans 	ret = ads1100_setup(data);
35554188054SMike Looijmans 	if (ret)
35654188054SMike Looijmans 		return dev_err_probe(dev, ret,
35754188054SMike Looijmans 				     "Failed to communicate with device\n");
35854188054SMike Looijmans 
35954188054SMike Looijmans 	ret = devm_add_action_or_reset(dev, ads1100_disable_continuous, data);
36054188054SMike Looijmans 	if (ret)
36154188054SMike Looijmans 		return ret;
36254188054SMike Looijmans 
36354188054SMike Looijmans 	ads1100_calc_scale_avail(data);
36454188054SMike Looijmans 
36554188054SMike Looijmans 	pm_runtime_set_autosuspend_delay(dev, ADS1100_SLEEP_DELAY_MS);
36654188054SMike Looijmans 	pm_runtime_use_autosuspend(dev);
36754188054SMike Looijmans 	pm_runtime_set_active(dev);
36854188054SMike Looijmans 	ret = devm_pm_runtime_enable(dev);
36954188054SMike Looijmans 	if (ret)
37054188054SMike Looijmans 		return dev_err_probe(dev, ret, "Failed to enable pm_runtime\n");
37154188054SMike Looijmans 
37254188054SMike Looijmans 	ret = devm_iio_device_register(dev, indio_dev);
37354188054SMike Looijmans 	if (ret)
37454188054SMike Looijmans 		return dev_err_probe(dev, ret,
37554188054SMike Looijmans 				     "Failed to register IIO device\n");
37654188054SMike Looijmans 
37754188054SMike Looijmans 	return 0;
37854188054SMike Looijmans }
37954188054SMike Looijmans 
ads1100_runtime_suspend(struct device * dev)38054188054SMike Looijmans static int ads1100_runtime_suspend(struct device *dev)
38154188054SMike Looijmans {
38254188054SMike Looijmans 	struct ads1100_data *data = dev_get_drvdata(dev);
38354188054SMike Looijmans 
38454188054SMike Looijmans 	ads1100_set_config_bits(data, ADS1100_CFG_SC, ADS1100_SINGLESHOT);
38554188054SMike Looijmans 	regulator_disable(data->reg_vdd);
38654188054SMike Looijmans 
38754188054SMike Looijmans 	return 0;
38854188054SMike Looijmans }
38954188054SMike Looijmans 
ads1100_runtime_resume(struct device * dev)39054188054SMike Looijmans static int ads1100_runtime_resume(struct device *dev)
39154188054SMike Looijmans {
39254188054SMike Looijmans 	struct ads1100_data *data = dev_get_drvdata(dev);
39354188054SMike Looijmans 	int ret;
39454188054SMike Looijmans 
39554188054SMike Looijmans 	ret = regulator_enable(data->reg_vdd);
39654188054SMike Looijmans 	if (ret) {
39754188054SMike Looijmans 		dev_err(&data->client->dev, "Failed to enable Vdd\n");
39854188054SMike Looijmans 		return ret;
39954188054SMike Looijmans 	}
40054188054SMike Looijmans 
40154188054SMike Looijmans 	/*
40254188054SMike Looijmans 	 * We'll always change the mode bit in the config register, so there is
40354188054SMike Looijmans 	 * no need here to "force" a write to the config register. If the device
40454188054SMike Looijmans 	 * has been power-cycled, we'll re-write its config register now.
40554188054SMike Looijmans 	 */
40654188054SMike Looijmans 	return ads1100_set_config_bits(data, ADS1100_CFG_SC,
40754188054SMike Looijmans 				       ADS1100_CONTINUOUS);
40854188054SMike Looijmans }
40954188054SMike Looijmans 
41054188054SMike Looijmans static DEFINE_RUNTIME_DEV_PM_OPS(ads1100_pm_ops,
41154188054SMike Looijmans 				 ads1100_runtime_suspend,
41254188054SMike Looijmans 				 ads1100_runtime_resume,
41354188054SMike Looijmans 				 NULL);
41454188054SMike Looijmans 
41554188054SMike Looijmans static const struct i2c_device_id ads1100_id[] = {
41654188054SMike Looijmans 	{ "ads1100" },
41754188054SMike Looijmans 	{ "ads1000" },
41854188054SMike Looijmans 	{ }
41954188054SMike Looijmans };
42054188054SMike Looijmans 
42154188054SMike Looijmans MODULE_DEVICE_TABLE(i2c, ads1100_id);
42254188054SMike Looijmans 
42354188054SMike Looijmans static const struct of_device_id ads1100_of_match[] = {
42454188054SMike Looijmans 	{.compatible = "ti,ads1100" },
42554188054SMike Looijmans 	{.compatible = "ti,ads1000" },
42654188054SMike Looijmans 	{ }
42754188054SMike Looijmans };
42854188054SMike Looijmans 
42954188054SMike Looijmans MODULE_DEVICE_TABLE(of, ads1100_of_match);
43054188054SMike Looijmans 
43154188054SMike Looijmans static struct i2c_driver ads1100_driver = {
43254188054SMike Looijmans 	.driver = {
43354188054SMike Looijmans 		   .name = "ads1100",
43454188054SMike Looijmans 		   .of_match_table = ads1100_of_match,
43554188054SMike Looijmans 		   .pm = pm_ptr(&ads1100_pm_ops),
43654188054SMike Looijmans 	},
437*7cf15f42SUwe Kleine-König 	.probe = ads1100_probe,
43854188054SMike Looijmans 	.id_table = ads1100_id,
43954188054SMike Looijmans };
44054188054SMike Looijmans 
44154188054SMike Looijmans module_i2c_driver(ads1100_driver);
44254188054SMike Looijmans 
44354188054SMike Looijmans MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
44454188054SMike Looijmans MODULE_DESCRIPTION("Texas Instruments ADS1100 ADC driver");
44554188054SMike Looijmans MODULE_LICENSE("GPL");
446