tmp421.c 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* tmp421.c
 *
 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
 * Preliminary support by:
 * Melvin Rook, Raymond Ng
 *
 * 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.
 */

/*
 * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
20
 * Supported models: TMP421, TMP422, TMP423, TMP441, TMP442
21 22 23 24 25 26 27 28 29 30 31
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
32
#include <linux/of_device.h>
33 34 35
#include <linux/sysfs.h>

/* Addresses to scan */
J
Jean Delvare 已提交
36 37
static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
					     I2C_CLIENT_END };
38

39
enum chips { tmp421, tmp422, tmp423, tmp441, tmp442 };
40 41

/* The TMP421 registers */
42
#define TMP421_STATUS_REG			0x08
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#define TMP421_CONFIG_REG_1			0x09
#define TMP421_CONVERSION_RATE_REG		0x0B
#define TMP421_MANUFACTURER_ID_REG		0xFE
#define TMP421_DEVICE_ID_REG			0xFF

static const u8 TMP421_TEMP_MSB[4]		= { 0x00, 0x01, 0x02, 0x03 };
static const u8 TMP421_TEMP_LSB[4]		= { 0x10, 0x11, 0x12, 0x13 };

/* Flags */
#define TMP421_CONFIG_SHUTDOWN			0x40
#define TMP421_CONFIG_RANGE			0x04

/* Manufacturer / Device ID's */
#define TMP421_MANUFACTURER_ID			0x55
#define TMP421_DEVICE_ID			0x21
#define TMP422_DEVICE_ID			0x22
#define TMP423_DEVICE_ID			0x23
60 61
#define TMP441_DEVICE_ID			0x41
#define TMP442_DEVICE_ID			0x42
62 63

static const struct i2c_device_id tmp421_id[] = {
64 65 66
	{ "tmp421", 2 },
	{ "tmp422", 3 },
	{ "tmp423", 4 },
67 68
	{ "tmp441", 2 },
	{ "tmp442", 3 },
69 70 71 72
	{ }
};
MODULE_DEVICE_TABLE(i2c, tmp421_id);

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static const struct of_device_id tmp421_of_match[] = {
	{
		.compatible = "ti,tmp421",
		.data = (void *)2
	},
	{
		.compatible = "ti,tmp422",
		.data = (void *)3
	},
	{
		.compatible = "ti,tmp423",
		.data = (void *)4
	},
	{
		.compatible = "ti,tmp441",
		.data = (void *)2
	},
	{
91
		.compatible = "ti,tmp442",
92 93 94 95 96 97
		.data = (void *)3
	},
	{ },
};
MODULE_DEVICE_TABLE(of, tmp421_of_match);

98
struct tmp421_data {
99
	struct i2c_client *client;
100
	struct mutex update_lock;
101 102 103 104
	u32 temp_config[5];
	struct hwmon_channel_info temp_info;
	const struct hwmon_channel_info *info[2];
	struct hwmon_chip_info chip;
105 106
	char valid;
	unsigned long last_updated;
107
	unsigned long channels;
108 109 110 111 112 113
	u8 config;
	s16 temp[4];
};

static int temp_from_s16(s16 reg)
{
114 115
	/* Mask out status bits */
	int temp = reg & ~0xf;
116 117 118 119 120 121

	return (temp * 1000 + 128) / 256;
}

static int temp_from_u16(u16 reg)
{
122 123
	/* Mask out status bits */
	int temp = reg & ~0xf;
124 125 126 127 128 129 130 131 132

	/* Add offset for extended temperature range. */
	temp -= 64 * 256;

	return (temp * 1000 + 128) / 256;
}

static struct tmp421_data *tmp421_update_device(struct device *dev)
{
133 134
	struct tmp421_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
135 136 137 138 139 140 141 142
	int i;

	mutex_lock(&data->update_lock);

	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
		data->config = i2c_smbus_read_byte_data(client,
			TMP421_CONFIG_REG_1);

143
		for (i = 0; i < data->channels; i++) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157
			data->temp[i] = i2c_smbus_read_byte_data(client,
				TMP421_TEMP_MSB[i]) << 8;
			data->temp[i] |= i2c_smbus_read_byte_data(client,
				TMP421_TEMP_LSB[i]);
		}
		data->last_updated = jiffies;
		data->valid = 1;
	}

	mutex_unlock(&data->update_lock);

	return data;
}

158 159
static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
160
{
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	struct tmp421_data *tmp421 = tmp421_update_device(dev);

	switch (attr) {
	case hwmon_temp_input:
		if (tmp421->config & TMP421_CONFIG_RANGE)
			*val = temp_from_u16(tmp421->temp[channel]);
		else
			*val = temp_from_s16(tmp421->temp[channel]);
		return 0;
	case hwmon_temp_fault:
		/*
		 * The OPEN bit signals a fault. This is bit 0 of the temperature
		 * register (low byte).
		 */
		*val = tmp421->temp[channel] & 0x01;
		return 0;
	default:
		return -EOPNOTSUPP;
	}
180 181 182

}

183 184
static umode_t tmp421_is_visible(const void *data, enum hwmon_sensor_types type,
				 u32 attr, int channel)
185
{
186 187 188 189 190 191 192 193 194 195
	switch (attr) {
	case hwmon_temp_fault:
		if (channel == 0)
			return 0;
		return S_IRUGO;
	case hwmon_temp_input:
		return S_IRUGO;
	default:
		return 0;
	}
196 197 198 199 200 201 202 203 204 205 206 207
}

static int tmp421_init_client(struct i2c_client *client)
{
	int config, config_orig;

	/* Set the conversion rate to 2 Hz */
	i2c_smbus_write_byte_data(client, TMP421_CONVERSION_RATE_REG, 0x05);

	/* Start conversions (disable shutdown if necessary) */
	config = i2c_smbus_read_byte_data(client, TMP421_CONFIG_REG_1);
	if (config < 0) {
208 209
		dev_err(&client->dev,
			"Could not read configuration register (%d)\n", config);
S
Sachin Kamat 已提交
210
		return config;
211 212 213 214 215 216 217 218 219 220 221 222 223
	}

	config_orig = config;
	config &= ~TMP421_CONFIG_SHUTDOWN;

	if (config != config_orig) {
		dev_info(&client->dev, "Enable monitoring chip\n");
		i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
	}

	return 0;
}

224
static int tmp421_detect(struct i2c_client *client,
225 226
			 struct i2c_board_info *info)
{
227
	enum chips kind;
228
	struct i2c_adapter *adapter = client->adapter;
229 230 231 232
	static const char * const names[] = {
		"TMP421", "TMP422", "TMP423",
		"TMP441", "TMP442"
	};
233
	int addr = client->addr;
234
	u8 reg;
235 236 237 238

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

239 240 241 242
	reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
	if (reg != TMP421_MANUFACTURER_ID)
		return -ENODEV;

243 244 245 246 247 248 249 250
	reg = i2c_smbus_read_byte_data(client, TMP421_CONVERSION_RATE_REG);
	if (reg & 0xf8)
		return -ENODEV;

	reg = i2c_smbus_read_byte_data(client, TMP421_STATUS_REG);
	if (reg & 0x7f)
		return -ENODEV;

251 252 253 254 255 256
	reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
	switch (reg) {
	case TMP421_DEVICE_ID:
		kind = tmp421;
		break;
	case TMP422_DEVICE_ID:
257 258
		if (addr == 0x2a)
			return -ENODEV;
259 260 261
		kind = tmp422;
		break;
	case TMP423_DEVICE_ID:
262 263
		if (addr != 0x4c && addr != 0x4d)
			return -ENODEV;
264 265
		kind = tmp423;
		break;
266 267 268 269 270 271 272 273
	case TMP441_DEVICE_ID:
		kind = tmp441;
		break;
	case TMP442_DEVICE_ID:
		if (addr != 0x4c && addr != 0x4d)
			return -ENODEV;
		kind = tmp442;
		break;
274 275
	default:
		return -ENODEV;
276
	}
277

J
Jean Delvare 已提交
278
	strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
279
	dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
J
Jean Delvare 已提交
280
		 names[kind], client->addr);
281 282 283 284

	return 0;
}

285 286 287 288 289
static const struct hwmon_ops tmp421_ops = {
	.is_visible = tmp421_is_visible,
	.read = tmp421_read,
};

290 291 292
static int tmp421_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
293 294
	struct device *dev = &client->dev;
	struct device *hwmon_dev;
295
	struct tmp421_data *data;
296
	int i, err;
297

298
	data = devm_kzalloc(dev, sizeof(struct tmp421_data), GFP_KERNEL);
299 300 301 302
	if (!data)
		return -ENOMEM;

	mutex_init(&data->update_lock);
303 304 305 306 307
	if (client->dev.of_node)
		data->channels = (unsigned long)
			of_device_get_match_data(&client->dev);
	else
		data->channels = id->driver_data;
308
	data->client = client;
309 310 311

	err = tmp421_init_client(client);
	if (err)
312
		return err;
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	for (i = 0; i < data->channels; i++)
		data->temp_config[i] = HWMON_T_INPUT | HWMON_T_FAULT;

	data->chip.ops = &tmp421_ops;
	data->chip.info = data->info;

	data->info[0] = &data->temp_info;

	data->temp_info.type = hwmon_temp;
	data->temp_info.config = data->temp_config;

	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
							 data,
							 &data->chip,
							 NULL);
329
	return PTR_ERR_OR_ZERO(hwmon_dev);
330 331 332 333 334 335
}

static struct i2c_driver tmp421_driver = {
	.class = I2C_CLASS_HWMON,
	.driver = {
		.name	= "tmp421",
336
		.of_match_table = of_match_ptr(tmp421_of_match),
337 338 339 340
	},
	.probe = tmp421_probe,
	.id_table = tmp421_id,
	.detect = tmp421_detect,
341
	.address_list = normal_i2c,
342 343
};

344
module_i2c_driver(tmp421_driver);
345 346

MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
347
MODULE_DESCRIPTION("Texas Instruments TMP421/422/423/441/442 temperature sensor driver");
348
MODULE_LICENSE("GPL");