Blame view

drivers/hwmon/tmp421.c 8.5 KB
9410700b8   Andre Prendel   hwmon: Add driver...
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
  /* 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.
   *
   * 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.
   */
  
  /*
   * Driver for the Texas Instruments TMP421 SMBus temperature sensor IC.
   * Supported models: TMP421, TMP422, TMP423
   */
  
  #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>
  #include <linux/sysfs.h>
  
  /* Addresses to scan */
918ee91c0   Jean Delvare   hwmon: I2C addres...
39
40
  static const unsigned short normal_i2c[] = { 0x2a, 0x4c, 0x4d, 0x4e, 0x4f,
  					     I2C_CLIENT_END };
9410700b8   Andre Prendel   hwmon: Add driver...
41

e5e9f44c2   Jean Delvare   i2c: Drop I2C_CLI...
42
  enum chips { tmp421, tmp422, tmp423 };
9410700b8   Andre Prendel   hwmon: Add driver...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  
  /* The TMP421 registers */
  #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
  
  static const struct i2c_device_id tmp421_id[] = {
8d59582a8   Jean Delvare   hwmon: (tmp421) R...
64
65
66
  	{ "tmp421", 2 },
  	{ "tmp422", 3 },
  	{ "tmp423", 4 },
9410700b8   Andre Prendel   hwmon: Add driver...
67
68
69
70
71
72
73
74
75
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, tmp421_id);
  
  struct tmp421_data {
  	struct device *hwmon_dev;
  	struct mutex update_lock;
  	char valid;
  	unsigned long last_updated;
8d59582a8   Jean Delvare   hwmon: (tmp421) R...
76
  	int channels;
9410700b8   Andre Prendel   hwmon: Add driver...
77
78
79
80
81
82
  	u8 config;
  	s16 temp[4];
  };
  
  static int temp_from_s16(s16 reg)
  {
a44908d74   Jean Delvare   hwmon: (tmp421) F...
83
84
  	/* Mask out status bits */
  	int temp = reg & ~0xf;
9410700b8   Andre Prendel   hwmon: Add driver...
85
86
87
88
89
90
  
  	return (temp * 1000 + 128) / 256;
  }
  
  static int temp_from_u16(u16 reg)
  {
a44908d74   Jean Delvare   hwmon: (tmp421) F...
91
92
  	/* Mask out status bits */
  	int temp = reg & ~0xf;
9410700b8   Andre Prendel   hwmon: Add driver...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  
  	/* Add offset for extended temperature range. */
  	temp -= 64 * 256;
  
  	return (temp * 1000 + 128) / 256;
  }
  
  static struct tmp421_data *tmp421_update_device(struct device *dev)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct tmp421_data *data = i2c_get_clientdata(client);
  	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);
8d59582a8   Jean Delvare   hwmon: (tmp421) R...
111
  		for (i = 0; i < data->channels; i++) {
9410700b8   Andre Prendel   hwmon: Add driver...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  			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;
  }
  
  static ssize_t show_temp_value(struct device *dev,
  			       struct device_attribute *devattr, char *buf)
  {
  	int index = to_sensor_dev_attr(devattr)->index;
  	struct tmp421_data *data = tmp421_update_device(dev);
  	int temp;
  
  	mutex_lock(&data->update_lock);
  	if (data->config & TMP421_CONFIG_RANGE)
  		temp = temp_from_u16(data->temp[index]);
  	else
  		temp = temp_from_s16(data->temp[index]);
  	mutex_unlock(&data->update_lock);
  
  	return sprintf(buf, "%d
  ", temp);
  }
  
  static ssize_t show_fault(struct device *dev,
  			  struct device_attribute *devattr, char *buf)
  {
  	int index = to_sensor_dev_attr(devattr)->index;
  	struct tmp421_data *data = tmp421_update_device(dev);
  
  	/*
  	 * The OPEN bit signals a fault. This is bit 0 of the temperature
  	 * register (low byte).
  	 */
  	if (data->temp[index] & 0x01)
  		return sprintf(buf, "1
  ");
  	else
  		return sprintf(buf, "0
  ");
  }
587a1f165   Al Viro   switch ->is_visib...
161
  static umode_t tmp421_is_visible(struct kobject *kobj, struct attribute *a,
9410700b8   Andre Prendel   hwmon: Add driver...
162
163
164
165
166
167
168
169
170
  				int n)
  {
  	struct device *dev = container_of(kobj, struct device, kobj);
  	struct tmp421_data *data = dev_get_drvdata(dev);
  	struct device_attribute *devattr;
  	unsigned int index;
  
  	devattr = container_of(a, struct device_attribute, attr);
  	index = to_sensor_dev_attr(devattr)->index;
8d59582a8   Jean Delvare   hwmon: (tmp421) R...
171
  	if (index < data->channels)
9410700b8   Andre Prendel   hwmon: Add driver...
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
221
222
223
224
225
226
227
  		return a->mode;
  
  	return 0;
  }
  
  static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0);
  static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_value, NULL, 2);
  static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
  static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_value, NULL, 3);
  static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
  
  static struct attribute *tmp421_attr[] = {
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
  	&sensor_dev_attr_temp2_input.dev_attr.attr,
  	&sensor_dev_attr_temp2_fault.dev_attr.attr,
  	&sensor_dev_attr_temp3_input.dev_attr.attr,
  	&sensor_dev_attr_temp3_fault.dev_attr.attr,
  	&sensor_dev_attr_temp4_input.dev_attr.attr,
  	&sensor_dev_attr_temp4_fault.dev_attr.attr,
  	NULL
  };
  
  static const struct attribute_group tmp421_group = {
  	.attrs = tmp421_attr,
  	.is_visible = tmp421_is_visible,
  };
  
  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) {
  		dev_err(&client->dev, "Could not read configuration"
  			 " register (%d)
  ", config);
  		return -ENODEV;
  	}
  
  	config_orig = config;
  	config &= ~TMP421_CONFIG_SHUTDOWN;
  
  	if (config != config_orig) {
  		dev_info(&client->dev, "Enable monitoring chip
  ");
  		i2c_smbus_write_byte_data(client, TMP421_CONFIG_REG_1, config);
  	}
  
  	return 0;
  }
310ec7921   Jean Delvare   i2c: Drop the kin...
228
  static int tmp421_detect(struct i2c_client *client,
9410700b8   Andre Prendel   hwmon: Add driver...
229
230
  			 struct i2c_board_info *info)
  {
dbe73c8f4   Jean Delvare   hwmon: (tmp401/tm...
231
  	enum chips kind;
9410700b8   Andre Prendel   hwmon: Add driver...
232
233
  	struct i2c_adapter *adapter = client->adapter;
  	const char *names[] = { "TMP421", "TMP422", "TMP423" };
dbe73c8f4   Jean Delvare   hwmon: (tmp401/tm...
234
  	u8 reg;
9410700b8   Andre Prendel   hwmon: Add driver...
235
236
237
  
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;
dbe73c8f4   Jean Delvare   hwmon: (tmp401/tm...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  	reg = i2c_smbus_read_byte_data(client, TMP421_MANUFACTURER_ID_REG);
  	if (reg != TMP421_MANUFACTURER_ID)
  		return -ENODEV;
  
  	reg = i2c_smbus_read_byte_data(client, TMP421_DEVICE_ID_REG);
  	switch (reg) {
  	case TMP421_DEVICE_ID:
  		kind = tmp421;
  		break;
  	case TMP422_DEVICE_ID:
  		kind = tmp422;
  		break;
  	case TMP423_DEVICE_ID:
  		kind = tmp423;
  		break;
  	default:
  		return -ENODEV;
9410700b8   Andre Prendel   hwmon: Add driver...
255
  	}
dbe73c8f4   Jean Delvare   hwmon: (tmp401/tm...
256

dc71afe5a   Jean Delvare   hwmon: Fix off-by...
257
  	strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
9410700b8   Andre Prendel   hwmon: Add driver...
258
259
  	dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x
  ",
dc71afe5a   Jean Delvare   hwmon: Fix off-by...
260
  		 names[kind], client->addr);
9410700b8   Andre Prendel   hwmon: Add driver...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  
  	return 0;
  }
  
  static int tmp421_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
  	struct tmp421_data *data;
  	int err;
  
  	data = kzalloc(sizeof(struct tmp421_data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
  
  	i2c_set_clientdata(client, data);
  	mutex_init(&data->update_lock);
8d59582a8   Jean Delvare   hwmon: (tmp421) R...
277
  	data->channels = id->driver_data;
9410700b8   Andre Prendel   hwmon: Add driver...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  
  	err = tmp421_init_client(client);
  	if (err)
  		goto exit_free;
  
  	err = sysfs_create_group(&client->dev.kobj, &tmp421_group);
  	if (err)
  		goto exit_free;
  
  	data->hwmon_dev = hwmon_device_register(&client->dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
  		data->hwmon_dev = NULL;
  		goto exit_remove;
  	}
  	return 0;
  
  exit_remove:
  	sysfs_remove_group(&client->dev.kobj, &tmp421_group);
  
  exit_free:
9410700b8   Andre Prendel   hwmon: Add driver...
299
300
301
302
303
304
305
306
307
308
309
  	kfree(data);
  
  	return err;
  }
  
  static int tmp421_remove(struct i2c_client *client)
  {
  	struct tmp421_data *data = i2c_get_clientdata(client);
  
  	hwmon_device_unregister(data->hwmon_dev);
  	sysfs_remove_group(&client->dev.kobj, &tmp421_group);
9410700b8   Andre Prendel   hwmon: Add driver...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  	kfree(data);
  
  	return 0;
  }
  
  static struct i2c_driver tmp421_driver = {
  	.class = I2C_CLASS_HWMON,
  	.driver = {
  		.name	= "tmp421",
  	},
  	.probe = tmp421_probe,
  	.remove = tmp421_remove,
  	.id_table = tmp421_id,
  	.detect = tmp421_detect,
c3813d6af   Jean Delvare   i2c: Get rid of s...
324
  	.address_list = normal_i2c,
9410700b8   Andre Prendel   hwmon: Add driver...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  };
  
  static int __init tmp421_init(void)
  {
  	return i2c_add_driver(&tmp421_driver);
  }
  
  static void __exit tmp421_exit(void)
  {
  	i2c_del_driver(&tmp421_driver);
  }
  
  MODULE_AUTHOR("Andre Prendel <andre.prendel@gmx.de>");
  MODULE_DESCRIPTION("Texas Instruments TMP421/422/423 temperature sensor"
  		   " driver");
  MODULE_LICENSE("GPL");
  
  module_init(tmp421_init);
  module_exit(tmp421_exit);