ads1015.c 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC
 * (C) Copyright 2010
 * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de>
 *
 * Based on the ads7828 driver by Steve Hardy.
 *
 * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/of.h>

#include <linux/i2c/ads1015.h>

/* ADS1015 registers */
enum {
	ADS1015_CONVERSION = 0,
	ADS1015_CONFIG = 1,
};

/* PGA fullscale voltages in mV */
static const unsigned int fullscale_table[8] = {
	6144, 4096, 2048, 1024, 512, 256, 256, 256 };

48 49 50 51
/* Data rates in samples per second */
static const unsigned int data_rate_table[8] = {
	128, 250, 490, 920, 1600, 2400, 3300, 3300 };

52
#define ADS1015_DEFAULT_CHANNELS 0xff
53 54
#define ADS1015_DEFAULT_PGA 2
#define ADS1015_DEFAULT_DATA_RATE 4
55 56 57 58

struct ads1015_data {
	struct device *hwmon_dev;
	struct mutex update_lock; /* mutex protect updates */
59
	struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
60 61
};

62
static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
63 64 65
{
	u16 config;
	struct ads1015_data *data = i2c_get_clientdata(client);
66 67 68
	unsigned int pga = data->channel_data[channel].pga;
	unsigned int data_rate = data->channel_data[channel].data_rate;
	unsigned int conversion_time_ms;
69 70 71 72
	int res;

	mutex_lock(&data->update_lock);

73
	/* get channel parameters */
74
	res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
75 76 77
	if (res < 0)
		goto err_unlock;
	config = res;
78
	conversion_time_ms = DIV_ROUND_UP(1000, data_rate_table[data_rate]);
79

80 81 82 83 84 85
	/* setup and start single conversion */
	config &= 0x001f;
	config |= (1 << 15) | (1 << 8);
	config |= (channel & 0x0007) << 12;
	config |= (pga & 0x0007) << 9;
	config |= (data_rate & 0x0007) << 5;
86

87
	res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
88 89
	if (res < 0)
		goto err_unlock;
90 91 92

	/* wait until conversion finished */
	msleep(conversion_time_ms);
93
	res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
94 95 96 97 98
	if (res < 0)
		goto err_unlock;
	config = res;
	if (!(config & (1 << 15))) {
		/* conversion not finished in time */
99 100 101 102
		res = -EIO;
		goto err_unlock;
	}

103
	res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
104 105 106 107 108 109

err_unlock:
	mutex_unlock(&data->update_lock);
	return res;
}

110 111 112 113 114 115 116 117 118 119
static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
			     s16 reg)
{
	struct ads1015_data *data = i2c_get_clientdata(client);
	unsigned int pga = data->channel_data[channel].pga;
	int fullscale = fullscale_table[pga];

	return DIV_ROUND_CLOSEST(reg * fullscale, 0x7ff0);
}

120 121 122 123 124 125 126
/* sysfs callback function */
static ssize_t show_in(struct device *dev, struct device_attribute *da,
	char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
	struct i2c_client *client = to_i2c_client(dev);
	int res;
127
	int index = attr->index;
128

129 130 131
	res = ads1015_read_adc(client, index);
	if (res < 0)
		return res;
132

133
	return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
134 135
}

136 137 138 139 140 141 142 143 144
static const struct sensor_device_attribute ads1015_in[] = {
	SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
	SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
	SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
	SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
	SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
	SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
	SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
	SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
145 146 147 148 149 150 151 152 153
};

/*
 * Driver interface
 */

static int ads1015_remove(struct i2c_client *client)
{
	struct ads1015_data *data = i2c_get_clientdata(client);
154 155
	int k;

156
	hwmon_device_unregister(data->hwmon_dev);
157
	for (k = 0; k < ADS1015_CHANNELS; ++k)
158
		device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
159 160 161 162
	return 0;
}

#ifdef CONFIG_OF
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static int ads1015_get_channels_config_of(struct i2c_client *client)
{
	struct ads1015_data *data = i2c_get_clientdata(client);
	struct device_node *node;

	if (!client->dev.of_node
	    || !of_get_next_child(client->dev.of_node, NULL))
		return -EINVAL;

	for_each_child_of_node(client->dev.of_node, node) {
		const __be32 *property;
		int len;
		unsigned int channel;
		unsigned int pga = ADS1015_DEFAULT_PGA;
		unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;

		property = of_get_property(node, "reg", &len);
		if (!property || len != sizeof(int)) {
			dev_err(&client->dev, "invalid reg on %s\n",
				node->full_name);
			continue;
		}

		channel = be32_to_cpup(property);
		if (channel > ADS1015_CHANNELS) {
			dev_err(&client->dev,
				"invalid channel index %d on %s\n",
				channel, node->full_name);
			continue;
		}

		property = of_get_property(node, "ti,gain", &len);
		if (property && len == sizeof(int)) {
			pga = be32_to_cpup(property);
			if (pga > 6) {
				dev_err(&client->dev,
					"invalid gain on %s\n",
					node->full_name);
			}
		}

		property = of_get_property(node, "ti,datarate", &len);
		if (property && len == sizeof(int)) {
			data_rate = be32_to_cpup(property);
			if (data_rate > 7) {
				dev_err(&client->dev,
					"invalid data_rate on %s\n",
					node->full_name);
			}
		}

		data->channel_data[channel].enabled = true;
		data->channel_data[channel].pga = pga;
		data->channel_data[channel].data_rate = data_rate;
	}

	return 0;
}
221 222
#endif

223 224 225 226 227 228
static void ads1015_get_channels_config(struct i2c_client *client)
{
	unsigned int k;
	struct ads1015_data *data = i2c_get_clientdata(client);
	struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);

229
	/* prefer platform data */
230 231 232 233 234
	if (pdata) {
		memcpy(data->channel_data, pdata->channel_data,
		       sizeof(data->channel_data));
		return;
	}
235 236

#ifdef CONFIG_OF
237 238
	if (!ads1015_get_channels_config_of(client))
		return;
239 240 241
#endif

	/* fallback on default configuration */
242 243 244 245 246
	for (k = 0; k < ADS1015_CHANNELS; ++k) {
		data->channel_data[k].enabled = true;
		data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
		data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
	}
247 248 249 250 251 252 253 254 255
}

static int ads1015_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct ads1015_data *data;
	int err;
	unsigned int k;

256 257 258 259
	data = devm_kzalloc(&client->dev, sizeof(struct ads1015_data),
			    GFP_KERNEL);
	if (!data)
		return -ENOMEM;
260 261 262 263 264

	i2c_set_clientdata(client, data);
	mutex_init(&data->update_lock);

	/* build sysfs attribute group */
265 266 267
	ads1015_get_channels_config(client);
	for (k = 0; k < ADS1015_CHANNELS; ++k) {
		if (!data->channel_data[k].enabled)
268
			continue;
269 270
		err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
		if (err)
271
			goto exit_remove;
272 273 274 275 276 277 278 279 280 281 282
	}

	data->hwmon_dev = hwmon_device_register(&client->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
		goto exit_remove;
	}

	return 0;

exit_remove:
283
	for (k = 0; k < ADS1015_CHANNELS; ++k)
284
		device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	return err;
}

static const struct i2c_device_id ads1015_id[] = {
	{ "ads1015", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, ads1015_id);

static struct i2c_driver ads1015_driver = {
	.driver = {
		.name = "ads1015",
	},
	.probe = ads1015_probe,
	.remove = ads1015_remove,
	.id_table = ads1015_id,
};

303
module_i2c_driver(ads1015_driver);
304 305 306 307

MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
MODULE_DESCRIPTION("ADS1015 driver");
MODULE_LICENSE("GPL");