adt7411.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
 *
 *  Copyright (C) 2008, 2010 Pengutronix
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
10
 *  TODO: SPI, use power-down mode for suspend?, interrupt handling?
11 12 13 14 15 16 17 18 19 20 21
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
22
#include <linux/slab.h>
23 24 25 26 27 28 29 30 31

#define ADT7411_REG_INT_TEMP_VDD_LSB		0x03
#define ADT7411_REG_EXT_TEMP_AIN14_LSB		0x04
#define ADT7411_REG_VDD_MSB			0x06
#define ADT7411_REG_INT_TEMP_MSB		0x07
#define ADT7411_REG_EXT_TEMP_AIN1_MSB		0x08

#define ADT7411_REG_CFG1			0x18
#define ADT7411_CFG1_START_MONITOR		(1 << 0)
32
#define ADT7411_CFG1_RESERVED_BIT1		(1 << 1)
33
#define ADT7411_CFG1_EXT_TDM			(1 << 2)
34
#define ADT7411_CFG1_RESERVED_BIT3		(1 << 3)
35 36 37 38 39 40

#define ADT7411_REG_CFG2			0x19
#define ADT7411_CFG2_DISABLE_AVG		(1 << 5)

#define ADT7411_REG_CFG3			0x1a
#define ADT7411_CFG3_ADC_CLK_225		(1 << 0)
41 42 43
#define ADT7411_CFG3_RESERVED_BIT1		(1 << 1)
#define ADT7411_CFG3_RESERVED_BIT2		(1 << 2)
#define ADT7411_CFG3_RESERVED_BIT3		(1 << 3)
44 45 46 47 48 49 50 51 52 53 54 55
#define ADT7411_CFG3_REF_VDD			(1 << 4)

#define ADT7411_REG_DEVICE_ID			0x4d
#define ADT7411_REG_MANUFACTURER_ID		0x4e

#define ADT7411_DEVICE_ID			0x2
#define ADT7411_MANUFACTURER_ID			0x41

static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };

struct adt7411_data {
	struct mutex device_lock;	/* for "atomic" device accesses */
W
Wolfram Sang 已提交
56
	struct mutex update_lock;
57 58
	unsigned long next_update;
	int vref_cached;
59
	struct i2c_client *client;
60
	bool use_ext_temp;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
};

/*
 * When reading a register containing (up to 4) lsb, all associated
 * msb-registers get locked by the hardware. After _one_ of those msb is read,
 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
 * is protected here with a mutex, too.
 */
static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
				u8 msb_reg, u8 lsb_shift)
{
	struct adt7411_data *data = i2c_get_clientdata(client);
	int val, tmp;

	mutex_lock(&data->device_lock);

	val = i2c_smbus_read_byte_data(client, lsb_reg);
	if (val < 0)
		goto exit_unlock;

	tmp = (val >> lsb_shift) & 3;
	val = i2c_smbus_read_byte_data(client, msb_reg);

	if (val >= 0)
		val = (val << 2) | tmp;

 exit_unlock:
	mutex_unlock(&data->device_lock);

	return val;
}

static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
				bool flag)
{
	struct adt7411_data *data = i2c_get_clientdata(client);
	int ret, val;

	mutex_lock(&data->device_lock);

	ret = i2c_smbus_read_byte_data(client, reg);
	if (ret < 0)
		goto exit_unlock;

	if (flag)
		val = ret | bit;
	else
		val = ret & ~bit;

	ret = i2c_smbus_write_byte_data(client, reg, val);

 exit_unlock:
	mutex_unlock(&data->device_lock);
	return ret;
}

static ssize_t adt7411_show_vdd(struct device *dev,
				struct device_attribute *attr, char *buf)
{
120 121
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
122 123 124 125 126 127 128 129 130
	int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
			ADT7411_REG_VDD_MSB, 2);

	return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
}

static ssize_t adt7411_show_temp(struct device *dev,
			struct device_attribute *attr, char *buf)
{
131
	int nr = to_sensor_dev_attr(attr)->index;
132 133
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
134 135 136 137 138 139 140 141 142 143 144
	int val;
	struct {
		u8 low;
		u8 high;
	} reg[2] = {
		{ ADT7411_REG_INT_TEMP_VDD_LSB, ADT7411_REG_INT_TEMP_MSB },
		{ ADT7411_REG_EXT_TEMP_AIN14_LSB,
		  ADT7411_REG_EXT_TEMP_AIN1_MSB },
	};

	val = adt7411_read_10_bit(client, reg[nr].low, reg[nr].high, 0);
145 146 147 148 149 150 151 152 153 154 155 156
	if (val < 0)
		return val;

	val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */

	return sprintf(buf, "%d\n", val * 250);
}

static ssize_t adt7411_show_input(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	int nr = to_sensor_dev_attr(attr)->index;
157 158
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
159 160 161
	int val;
	u8 lsb_reg, lsb_shift;

W
Wolfram Sang 已提交
162
	mutex_lock(&data->update_lock);
163 164 165
	if (time_after_eq(jiffies, data->next_update)) {
		val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
		if (val < 0)
W
Wolfram Sang 已提交
166
			goto exit_unlock;
167

W
Wolfram Sang 已提交
168
		if (val & ADT7411_CFG3_REF_VDD) {
169 170 171 172
			val = adt7411_read_10_bit(client,
					ADT7411_REG_INT_TEMP_VDD_LSB,
					ADT7411_REG_VDD_MSB, 2);
			if (val < 0)
W
Wolfram Sang 已提交
173
				goto exit_unlock;
174 175 176 177 178 179 180 181 182 183 184 185 186

			data->vref_cached = val * 7000 / 1024;
		} else {
			data->vref_cached = 2250;
		}

		data->next_update = jiffies + HZ;
	}

	lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
	lsb_shift = 2 * (nr & 0x03);
	val = adt7411_read_10_bit(client, lsb_reg,
			ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
W
Wolfram Sang 已提交
187 188
	if (val < 0)
		goto exit_unlock;
189

W
Wolfram Sang 已提交
190 191 192 193
	val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
 exit_unlock:
	mutex_unlock(&data->update_lock);
	return val;
194 195 196 197 198 199
}

static ssize_t adt7411_show_bit(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
200 201
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
202 203 204 205 206 207 208 209 210 211
	int ret = i2c_smbus_read_byte_data(client, attr2->index);

	return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
}

static ssize_t adt7411_set_bit(struct device *dev,
			       struct device_attribute *attr, const char *buf,
			       size_t count)
{
	struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
212 213
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
214 215 216
	int ret;
	unsigned long flag;

217
	ret = kstrtoul(buf, 0, &flag);
218 219 220 221 222 223
	if (ret || flag > 1)
		return -EINVAL;

	ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);

	/* force update */
W
Wolfram Sang 已提交
224
	mutex_lock(&data->update_lock);
225
	data->next_update = jiffies;
W
Wolfram Sang 已提交
226
	mutex_unlock(&data->update_lock);
227 228 229 230

	return ret < 0 ? ret : count;
}

231

232 233 234 235
#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
	SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
	adt7411_set_bit, __bit, __reg)

236 237
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL, 0);
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, adt7411_show_temp, NULL, 1);
238 239 240 241 242 243 244 245 246 247 248 249 250 251
static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);

static struct attribute *adt7411_attrs[] = {
252 253
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp2_input.dev_attr.attr,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	&dev_attr_in0_input.attr,
	&sensor_dev_attr_in1_input.dev_attr.attr,
	&sensor_dev_attr_in2_input.dev_attr.attr,
	&sensor_dev_attr_in3_input.dev_attr.attr,
	&sensor_dev_attr_in4_input.dev_attr.attr,
	&sensor_dev_attr_in5_input.dev_attr.attr,
	&sensor_dev_attr_in6_input.dev_attr.attr,
	&sensor_dev_attr_in7_input.dev_attr.attr,
	&sensor_dev_attr_in8_input.dev_attr.attr,
	&sensor_dev_attr_no_average.dev_attr.attr,
	&sensor_dev_attr_fast_sampling.dev_attr.attr,
	&sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
	NULL
};

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
static umode_t adt7411_attrs_visible(struct kobject *kobj,
				     struct attribute *attr, int index)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct adt7411_data *data = dev_get_drvdata(dev);
	bool visible = true;

	if (attr == &sensor_dev_attr_temp2_input.dev_attr.attr)
		visible = data->use_ext_temp;
	else if (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
		 attr == &sensor_dev_attr_in2_input.dev_attr.attr)
		visible = !data->use_ext_temp;

	return visible ? attr->mode : 0;
}

static const struct attribute_group adt7411_group = {
	.attrs = adt7411_attrs,
	.is_visible = adt7411_attrs_visible,
};
__ATTRIBUTE_GROUPS(adt7411);
290 291 292 293 294 295 296 297 298 299 300

static int adt7411_detect(struct i2c_client *client,
			  struct i2c_board_info *info)
{
	int val;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -ENODEV;

	val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
	if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
301 302 303
		dev_dbg(&client->dev,
			"Wrong manufacturer ID. Got %d, expected %d\n",
			val, ADT7411_MANUFACTURER_ID);
304 305 306 307 308
		return -ENODEV;
	}

	val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
	if (val < 0 || val != ADT7411_DEVICE_ID) {
309 310 311
		dev_dbg(&client->dev,
			"Wrong device ID. Got %d, expected %d\n",
			val, ADT7411_DEVICE_ID);
312 313 314 315 316 317 318 319
		return -ENODEV;
	}

	strlcpy(info->type, "adt7411", I2C_NAME_SIZE);

	return 0;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static int adt7411_init_device(struct adt7411_data *data)
{
	int ret;
	u8 val;

	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
	if (ret < 0)
		return ret;

	/*
	 * We must only write zero to bit 1 and bit 2 and only one to bit 3
	 * according to the datasheet.
	 */
	val = ret;
	val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
	val |= ADT7411_CFG3_RESERVED_BIT3;

	ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
	if (ret < 0)
		return ret;

345 346
	data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;

347 348 349 350 351 352 353 354 355 356 357 358 359 360
	/*
	 * We must only write zero to bit 1 and only one to bit 3 according to
	 * the datasheet.
	 */
	val = ret;
	val &= ~ADT7411_CFG1_RESERVED_BIT1;
	val |= ADT7411_CFG1_RESERVED_BIT3;

	/* enable monitoring */
	val |= ADT7411_CFG1_START_MONITOR;

	return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
}

B
Bill Pemberton 已提交
361
static int adt7411_probe(struct i2c_client *client,
362 363
				   const struct i2c_device_id *id)
{
364
	struct device *dev = &client->dev;
365
	struct adt7411_data *data;
366
	struct device *hwmon_dev;
367 368
	int ret;

369
	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
370 371 372 373
	if (!data)
		return -ENOMEM;

	i2c_set_clientdata(client, data);
374
	data->client = client;
375
	mutex_init(&data->device_lock);
W
Wolfram Sang 已提交
376
	mutex_init(&data->update_lock);
377

378
	ret = adt7411_init_device(data);
379
	if (ret < 0)
380
		return ret;
381 382 383 384

	/* force update on first occasion */
	data->next_update = jiffies;

385 386 387 388
	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
							   data,
							   adt7411_groups);
	return PTR_ERR_OR_ZERO(hwmon_dev);
389 390 391 392 393 394
}

static const struct i2c_device_id adt7411_id[] = {
	{ "adt7411", 0 },
	{ }
};
395
MODULE_DEVICE_TABLE(i2c, adt7411_id);
396 397 398 399 400 401 402 403 404 405 406 407

static struct i2c_driver adt7411_driver = {
	.driver		= {
		.name		= "adt7411",
	},
	.probe  = adt7411_probe,
	.id_table = adt7411_id,
	.detect = adt7411_detect,
	.address_list = normal_i2c,
	.class = I2C_CLASS_HWMON,
};

408
module_i2c_driver(adt7411_driver);
409 410 411 412 413

MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
	"Wolfram Sang <w.sang@pengutronix.de>");
MODULE_DESCRIPTION("ADT7411 driver");
MODULE_LICENSE("GPL v2");