Commit | Line | Data |
---|---|---|
ab2b79d5 HG |
1 | /* tmp401.c |
2 | * | |
3 | * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com> | |
fce0758f AP |
4 | * Preliminary tmp411 support by: |
5 | * Gabriel Konat, Sander Leget, Wouter Willems | |
6 | * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de> | |
ab2b79d5 HG |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC. | |
25 | * | |
26 | * Note this IC is in some aspect similar to the LM90, but it has quite a | |
27 | * few differences too, for example the local temp has a higher resolution | |
28 | * and thus has 16 bits registers for its value and limit instead of 8 bits. | |
29 | */ | |
30 | ||
31 | #include <linux/module.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/jiffies.h> | |
35 | #include <linux/i2c.h> | |
36 | #include <linux/hwmon.h> | |
37 | #include <linux/hwmon-sysfs.h> | |
38 | #include <linux/err.h> | |
39 | #include <linux/mutex.h> | |
40 | #include <linux/sysfs.h> | |
41 | ||
42 | /* Addresses to scan */ | |
43 | static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; | |
44 | ||
e5e9f44c | 45 | enum chips { tmp401, tmp411 }; |
ab2b79d5 HG |
46 | |
47 | /* | |
48 | * The TMP401 registers, note some registers have different addresses for | |
49 | * reading and writing | |
50 | */ | |
51 | #define TMP401_STATUS 0x02 | |
52 | #define TMP401_CONFIG_READ 0x03 | |
53 | #define TMP401_CONFIG_WRITE 0x09 | |
54 | #define TMP401_CONVERSION_RATE_READ 0x04 | |
55 | #define TMP401_CONVERSION_RATE_WRITE 0x0A | |
56 | #define TMP401_TEMP_CRIT_HYST 0x21 | |
57 | #define TMP401_CONSECUTIVE_ALERT 0x22 | |
58 | #define TMP401_MANUFACTURER_ID_REG 0xFE | |
59 | #define TMP401_DEVICE_ID_REG 0xFF | |
fce0758f | 60 | #define TMP411_N_FACTOR_REG 0x18 |
ab2b79d5 HG |
61 | |
62 | static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 }; | |
63 | static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 }; | |
64 | static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 }; | |
65 | static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E }; | |
66 | static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 }; | |
67 | static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 }; | |
68 | static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D }; | |
69 | static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 }; | |
70 | /* These are called the THERM limit / hysteresis / mask in the datasheet */ | |
71 | static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 }; | |
72 | ||
fce0758f AP |
73 | static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 }; |
74 | static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 }; | |
75 | static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 }; | |
76 | static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 }; | |
77 | ||
ab2b79d5 HG |
78 | /* Flags */ |
79 | #define TMP401_CONFIG_RANGE 0x04 | |
80 | #define TMP401_CONFIG_SHUTDOWN 0x40 | |
81 | #define TMP401_STATUS_LOCAL_CRIT 0x01 | |
82 | #define TMP401_STATUS_REMOTE_CRIT 0x02 | |
83 | #define TMP401_STATUS_REMOTE_OPEN 0x04 | |
84 | #define TMP401_STATUS_REMOTE_LOW 0x08 | |
85 | #define TMP401_STATUS_REMOTE_HIGH 0x10 | |
86 | #define TMP401_STATUS_LOCAL_LOW 0x20 | |
87 | #define TMP401_STATUS_LOCAL_HIGH 0x40 | |
88 | ||
89 | /* Manufacturer / Device ID's */ | |
90 | #define TMP401_MANUFACTURER_ID 0x55 | |
91 | #define TMP401_DEVICE_ID 0x11 | |
fce0758f | 92 | #define TMP411_DEVICE_ID 0x12 |
ab2b79d5 HG |
93 | |
94 | /* | |
95 | * Functions declarations | |
96 | */ | |
97 | ||
98 | static int tmp401_probe(struct i2c_client *client, | |
99 | const struct i2c_device_id *id); | |
310ec792 | 100 | static int tmp401_detect(struct i2c_client *client, |
ab2b79d5 HG |
101 | struct i2c_board_info *info); |
102 | static int tmp401_remove(struct i2c_client *client); | |
103 | static struct tmp401_data *tmp401_update_device(struct device *dev); | |
104 | ||
105 | /* | |
106 | * Driver data (common to all clients) | |
107 | */ | |
108 | ||
109 | static const struct i2c_device_id tmp401_id[] = { | |
110 | { "tmp401", tmp401 }, | |
fce0758f | 111 | { "tmp411", tmp411 }, |
ab2b79d5 HG |
112 | { } |
113 | }; | |
114 | MODULE_DEVICE_TABLE(i2c, tmp401_id); | |
115 | ||
116 | static struct i2c_driver tmp401_driver = { | |
117 | .class = I2C_CLASS_HWMON, | |
118 | .driver = { | |
119 | .name = "tmp401", | |
120 | }, | |
121 | .probe = tmp401_probe, | |
122 | .remove = tmp401_remove, | |
123 | .id_table = tmp401_id, | |
124 | .detect = tmp401_detect, | |
c3813d6a | 125 | .address_list = normal_i2c, |
ab2b79d5 HG |
126 | }; |
127 | ||
128 | /* | |
129 | * Client data (each client gets its own) | |
130 | */ | |
131 | ||
132 | struct tmp401_data { | |
133 | struct device *hwmon_dev; | |
134 | struct mutex update_lock; | |
135 | char valid; /* zero until following fields are valid */ | |
136 | unsigned long last_updated; /* in jiffies */ | |
fce0758f | 137 | int kind; |
ab2b79d5 HG |
138 | |
139 | /* register values */ | |
140 | u8 status; | |
141 | u8 config; | |
142 | u16 temp[2]; | |
143 | u16 temp_low[2]; | |
144 | u16 temp_high[2]; | |
145 | u8 temp_crit[2]; | |
146 | u8 temp_crit_hyst; | |
fce0758f AP |
147 | u16 temp_lowest[2]; |
148 | u16 temp_highest[2]; | |
ab2b79d5 HG |
149 | }; |
150 | ||
151 | /* | |
152 | * Sysfs attr show / store functions | |
153 | */ | |
154 | ||
155 | static int tmp401_register_to_temp(u16 reg, u8 config) | |
156 | { | |
157 | int temp = reg; | |
158 | ||
159 | if (config & TMP401_CONFIG_RANGE) | |
160 | temp -= 64 * 256; | |
161 | ||
162 | return (temp * 625 + 80) / 160; | |
163 | } | |
164 | ||
165 | static u16 tmp401_temp_to_register(long temp, u8 config) | |
166 | { | |
167 | if (config & TMP401_CONFIG_RANGE) { | |
168 | temp = SENSORS_LIMIT(temp, -64000, 191000); | |
169 | temp += 64000; | |
170 | } else | |
171 | temp = SENSORS_LIMIT(temp, 0, 127000); | |
172 | ||
173 | return (temp * 160 + 312) / 625; | |
174 | } | |
175 | ||
176 | static int tmp401_crit_register_to_temp(u8 reg, u8 config) | |
177 | { | |
178 | int temp = reg; | |
179 | ||
180 | if (config & TMP401_CONFIG_RANGE) | |
181 | temp -= 64; | |
182 | ||
183 | return temp * 1000; | |
184 | } | |
185 | ||
186 | static u8 tmp401_crit_temp_to_register(long temp, u8 config) | |
187 | { | |
188 | if (config & TMP401_CONFIG_RANGE) { | |
189 | temp = SENSORS_LIMIT(temp, -64000, 191000); | |
190 | temp += 64000; | |
191 | } else | |
192 | temp = SENSORS_LIMIT(temp, 0, 127000); | |
193 | ||
194 | return (temp + 500) / 1000; | |
195 | } | |
196 | ||
197 | static ssize_t show_temp_value(struct device *dev, | |
198 | struct device_attribute *devattr, char *buf) | |
199 | { | |
200 | int index = to_sensor_dev_attr(devattr)->index; | |
201 | struct tmp401_data *data = tmp401_update_device(dev); | |
202 | ||
203 | return sprintf(buf, "%d\n", | |
204 | tmp401_register_to_temp(data->temp[index], data->config)); | |
205 | } | |
206 | ||
207 | static ssize_t show_temp_min(struct device *dev, | |
208 | struct device_attribute *devattr, char *buf) | |
209 | { | |
210 | int index = to_sensor_dev_attr(devattr)->index; | |
211 | struct tmp401_data *data = tmp401_update_device(dev); | |
212 | ||
213 | return sprintf(buf, "%d\n", | |
214 | tmp401_register_to_temp(data->temp_low[index], data->config)); | |
215 | } | |
216 | ||
217 | static ssize_t show_temp_max(struct device *dev, | |
218 | struct device_attribute *devattr, char *buf) | |
219 | { | |
220 | int index = to_sensor_dev_attr(devattr)->index; | |
221 | struct tmp401_data *data = tmp401_update_device(dev); | |
222 | ||
223 | return sprintf(buf, "%d\n", | |
224 | tmp401_register_to_temp(data->temp_high[index], data->config)); | |
225 | } | |
226 | ||
227 | static ssize_t show_temp_crit(struct device *dev, | |
228 | struct device_attribute *devattr, char *buf) | |
229 | { | |
230 | int index = to_sensor_dev_attr(devattr)->index; | |
231 | struct tmp401_data *data = tmp401_update_device(dev); | |
232 | ||
233 | return sprintf(buf, "%d\n", | |
234 | tmp401_crit_register_to_temp(data->temp_crit[index], | |
235 | data->config)); | |
236 | } | |
237 | ||
238 | static ssize_t show_temp_crit_hyst(struct device *dev, | |
239 | struct device_attribute *devattr, char *buf) | |
240 | { | |
241 | int temp, index = to_sensor_dev_attr(devattr)->index; | |
242 | struct tmp401_data *data = tmp401_update_device(dev); | |
243 | ||
244 | mutex_lock(&data->update_lock); | |
245 | temp = tmp401_crit_register_to_temp(data->temp_crit[index], | |
246 | data->config); | |
247 | temp -= data->temp_crit_hyst * 1000; | |
248 | mutex_unlock(&data->update_lock); | |
249 | ||
250 | return sprintf(buf, "%d\n", temp); | |
251 | } | |
252 | ||
fce0758f AP |
253 | static ssize_t show_temp_lowest(struct device *dev, |
254 | struct device_attribute *devattr, char *buf) | |
255 | { | |
256 | int index = to_sensor_dev_attr(devattr)->index; | |
257 | struct tmp401_data *data = tmp401_update_device(dev); | |
258 | ||
259 | return sprintf(buf, "%d\n", | |
260 | tmp401_register_to_temp(data->temp_lowest[index], | |
261 | data->config)); | |
262 | } | |
263 | ||
264 | static ssize_t show_temp_highest(struct device *dev, | |
265 | struct device_attribute *devattr, char *buf) | |
266 | { | |
267 | int index = to_sensor_dev_attr(devattr)->index; | |
268 | struct tmp401_data *data = tmp401_update_device(dev); | |
269 | ||
270 | return sprintf(buf, "%d\n", | |
271 | tmp401_register_to_temp(data->temp_highest[index], | |
272 | data->config)); | |
273 | } | |
274 | ||
ab2b79d5 HG |
275 | static ssize_t show_status(struct device *dev, |
276 | struct device_attribute *devattr, char *buf) | |
277 | { | |
278 | int mask = to_sensor_dev_attr(devattr)->index; | |
279 | struct tmp401_data *data = tmp401_update_device(dev); | |
280 | ||
281 | if (data->status & mask) | |
282 | return sprintf(buf, "1\n"); | |
283 | else | |
284 | return sprintf(buf, "0\n"); | |
285 | } | |
286 | ||
287 | static ssize_t store_temp_min(struct device *dev, struct device_attribute | |
288 | *devattr, const char *buf, size_t count) | |
289 | { | |
290 | int index = to_sensor_dev_attr(devattr)->index; | |
291 | struct tmp401_data *data = tmp401_update_device(dev); | |
292 | long val; | |
293 | u16 reg; | |
294 | ||
295 | if (strict_strtol(buf, 10, &val)) | |
296 | return -EINVAL; | |
297 | ||
298 | reg = tmp401_temp_to_register(val, data->config); | |
299 | ||
300 | mutex_lock(&data->update_lock); | |
301 | ||
302 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
303 | TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8); | |
304 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
305 | TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF); | |
306 | ||
307 | data->temp_low[index] = reg; | |
308 | ||
309 | mutex_unlock(&data->update_lock); | |
310 | ||
311 | return count; | |
312 | } | |
313 | ||
314 | static ssize_t store_temp_max(struct device *dev, struct device_attribute | |
315 | *devattr, const char *buf, size_t count) | |
316 | { | |
317 | int index = to_sensor_dev_attr(devattr)->index; | |
318 | struct tmp401_data *data = tmp401_update_device(dev); | |
319 | long val; | |
320 | u16 reg; | |
321 | ||
322 | if (strict_strtol(buf, 10, &val)) | |
323 | return -EINVAL; | |
324 | ||
325 | reg = tmp401_temp_to_register(val, data->config); | |
326 | ||
327 | mutex_lock(&data->update_lock); | |
328 | ||
329 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
330 | TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8); | |
331 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
332 | TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF); | |
333 | ||
334 | data->temp_high[index] = reg; | |
335 | ||
336 | mutex_unlock(&data->update_lock); | |
337 | ||
338 | return count; | |
339 | } | |
340 | ||
341 | static ssize_t store_temp_crit(struct device *dev, struct device_attribute | |
342 | *devattr, const char *buf, size_t count) | |
343 | { | |
344 | int index = to_sensor_dev_attr(devattr)->index; | |
345 | struct tmp401_data *data = tmp401_update_device(dev); | |
346 | long val; | |
347 | u8 reg; | |
348 | ||
349 | if (strict_strtol(buf, 10, &val)) | |
350 | return -EINVAL; | |
351 | ||
352 | reg = tmp401_crit_temp_to_register(val, data->config); | |
353 | ||
354 | mutex_lock(&data->update_lock); | |
355 | ||
356 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
357 | TMP401_TEMP_CRIT_LIMIT[index], reg); | |
358 | ||
359 | data->temp_crit[index] = reg; | |
360 | ||
361 | mutex_unlock(&data->update_lock); | |
362 | ||
363 | return count; | |
364 | } | |
365 | ||
366 | static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute | |
367 | *devattr, const char *buf, size_t count) | |
368 | { | |
369 | int temp, index = to_sensor_dev_attr(devattr)->index; | |
370 | struct tmp401_data *data = tmp401_update_device(dev); | |
371 | long val; | |
372 | u8 reg; | |
373 | ||
374 | if (strict_strtol(buf, 10, &val)) | |
375 | return -EINVAL; | |
376 | ||
377 | if (data->config & TMP401_CONFIG_RANGE) | |
378 | val = SENSORS_LIMIT(val, -64000, 191000); | |
379 | else | |
380 | val = SENSORS_LIMIT(val, 0, 127000); | |
381 | ||
382 | mutex_lock(&data->update_lock); | |
383 | temp = tmp401_crit_register_to_temp(data->temp_crit[index], | |
384 | data->config); | |
385 | val = SENSORS_LIMIT(val, temp - 255000, temp); | |
386 | reg = ((temp - val) + 500) / 1000; | |
387 | ||
388 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
389 | TMP401_TEMP_CRIT_HYST, reg); | |
390 | ||
391 | data->temp_crit_hyst = reg; | |
392 | ||
393 | mutex_unlock(&data->update_lock); | |
394 | ||
395 | return count; | |
396 | } | |
397 | ||
fce0758f AP |
398 | /* |
399 | * Resets the historical measurements of minimum and maximum temperatures. | |
400 | * This is done by writing any value to any of the minimum/maximum registers | |
401 | * (0x30-0x37). | |
402 | */ | |
403 | static ssize_t reset_temp_history(struct device *dev, | |
404 | struct device_attribute *devattr, const char *buf, size_t count) | |
405 | { | |
406 | long val; | |
407 | ||
408 | if (strict_strtol(buf, 10, &val)) | |
409 | return -EINVAL; | |
410 | ||
411 | if (val != 1) { | |
412 | dev_err(dev, "temp_reset_history value %ld not" | |
413 | " supported. Use 1 to reset the history!\n", val); | |
414 | return -EINVAL; | |
415 | } | |
416 | i2c_smbus_write_byte_data(to_i2c_client(dev), | |
417 | TMP411_TEMP_LOWEST_MSB[0], val); | |
418 | ||
419 | return count; | |
420 | } | |
421 | ||
ab2b79d5 HG |
422 | static struct sensor_device_attribute tmp401_attr[] = { |
423 | SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0), | |
424 | SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0), | |
425 | SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0), | |
426 | SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0), | |
427 | SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst, | |
428 | store_temp_crit_hyst, 0), | |
429 | SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL, | |
430 | TMP401_STATUS_LOCAL_LOW), | |
431 | SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL, | |
432 | TMP401_STATUS_LOCAL_HIGH), | |
433 | SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL, | |
434 | TMP401_STATUS_LOCAL_CRIT), | |
435 | SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1), | |
436 | SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1), | |
437 | SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1), | |
438 | SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1), | |
439 | SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1), | |
440 | SENSOR_ATTR(temp2_fault, 0444, show_status, NULL, | |
441 | TMP401_STATUS_REMOTE_OPEN), | |
442 | SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL, | |
443 | TMP401_STATUS_REMOTE_LOW), | |
444 | SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL, | |
445 | TMP401_STATUS_REMOTE_HIGH), | |
446 | SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL, | |
447 | TMP401_STATUS_REMOTE_CRIT), | |
448 | }; | |
449 | ||
fce0758f AP |
450 | /* |
451 | * Additional features of the TMP411 chip. | |
452 | * The TMP411 stores the minimum and maximum | |
453 | * temperature measured since power-on, chip-reset, or | |
454 | * minimum and maximum register reset for both the local | |
455 | * and remote channels. | |
456 | */ | |
457 | static struct sensor_device_attribute tmp411_attr[] = { | |
458 | SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0), | |
459 | SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0), | |
460 | SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1), | |
461 | SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1), | |
462 | SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0), | |
463 | }; | |
464 | ||
ab2b79d5 HG |
465 | /* |
466 | * Begin non sysfs callback code (aka Real code) | |
467 | */ | |
468 | ||
469 | static void tmp401_init_client(struct i2c_client *client) | |
470 | { | |
471 | int config, config_orig; | |
472 | ||
473 | /* Set the conversion rate to 2 Hz */ | |
474 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5); | |
475 | ||
476 | /* Start conversions (disable shutdown if necessary) */ | |
477 | config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); | |
478 | if (config < 0) { | |
479 | dev_warn(&client->dev, "Initialization failed!\n"); | |
480 | return; | |
481 | } | |
482 | ||
483 | config_orig = config; | |
484 | config &= ~TMP401_CONFIG_SHUTDOWN; | |
485 | ||
486 | if (config != config_orig) | |
487 | i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config); | |
488 | } | |
489 | ||
310ec792 | 490 | static int tmp401_detect(struct i2c_client *client, |
ab2b79d5 HG |
491 | struct i2c_board_info *info) |
492 | { | |
dbe73c8f | 493 | enum chips kind; |
ab2b79d5 | 494 | struct i2c_adapter *adapter = client->adapter; |
dbe73c8f | 495 | u8 reg; |
ab2b79d5 HG |
496 | |
497 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
498 | return -ENODEV; | |
499 | ||
500 | /* Detect and identify the chip */ | |
dbe73c8f JD |
501 | reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG); |
502 | if (reg != TMP401_MANUFACTURER_ID) | |
503 | return -ENODEV; | |
ab2b79d5 | 504 | |
dbe73c8f | 505 | reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); |
ab2b79d5 | 506 | |
dbe73c8f JD |
507 | switch (reg) { |
508 | case TMP401_DEVICE_ID: | |
509 | kind = tmp401; | |
510 | break; | |
511 | case TMP411_DEVICE_ID: | |
512 | kind = tmp411; | |
513 | break; | |
514 | default: | |
515 | return -ENODEV; | |
ab2b79d5 | 516 | } |
dbe73c8f JD |
517 | |
518 | reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); | |
519 | if (reg & 0x1b) | |
520 | return -ENODEV; | |
521 | ||
522 | reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ); | |
523 | /* Datasheet says: 0x1-0x6 */ | |
524 | if (reg > 15) | |
525 | return -ENODEV; | |
526 | ||
fce0758f | 527 | strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE); |
ab2b79d5 HG |
528 | |
529 | return 0; | |
530 | } | |
531 | ||
532 | static int tmp401_probe(struct i2c_client *client, | |
533 | const struct i2c_device_id *id) | |
534 | { | |
535 | int i, err = 0; | |
536 | struct tmp401_data *data; | |
fce0758f | 537 | const char *names[] = { "TMP401", "TMP411" }; |
ab2b79d5 HG |
538 | |
539 | data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL); | |
540 | if (!data) | |
541 | return -ENOMEM; | |
542 | ||
543 | i2c_set_clientdata(client, data); | |
544 | mutex_init(&data->update_lock); | |
fce0758f | 545 | data->kind = id->driver_data; |
ab2b79d5 HG |
546 | |
547 | /* Initialize the TMP401 chip */ | |
548 | tmp401_init_client(client); | |
549 | ||
550 | /* Register sysfs hooks */ | |
551 | for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) { | |
552 | err = device_create_file(&client->dev, | |
553 | &tmp401_attr[i].dev_attr); | |
554 | if (err) | |
555 | goto exit_remove; | |
556 | } | |
557 | ||
fce0758f AP |
558 | /* Register aditional tmp411 sysfs hooks */ |
559 | if (data->kind == tmp411) { | |
560 | for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) { | |
561 | err = device_create_file(&client->dev, | |
562 | &tmp411_attr[i].dev_attr); | |
563 | if (err) | |
564 | goto exit_remove; | |
565 | } | |
566 | } | |
567 | ||
ab2b79d5 HG |
568 | data->hwmon_dev = hwmon_device_register(&client->dev); |
569 | if (IS_ERR(data->hwmon_dev)) { | |
570 | err = PTR_ERR(data->hwmon_dev); | |
571 | data->hwmon_dev = NULL; | |
572 | goto exit_remove; | |
573 | } | |
574 | ||
fce0758f AP |
575 | dev_info(&client->dev, "Detected TI %s chip\n", |
576 | names[data->kind - 1]); | |
ab2b79d5 HG |
577 | |
578 | return 0; | |
579 | ||
580 | exit_remove: | |
581 | tmp401_remove(client); /* will also free data for us */ | |
582 | return err; | |
583 | } | |
584 | ||
585 | static int tmp401_remove(struct i2c_client *client) | |
586 | { | |
587 | struct tmp401_data *data = i2c_get_clientdata(client); | |
588 | int i; | |
589 | ||
590 | if (data->hwmon_dev) | |
591 | hwmon_device_unregister(data->hwmon_dev); | |
592 | ||
593 | for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) | |
594 | device_remove_file(&client->dev, &tmp401_attr[i].dev_attr); | |
595 | ||
fce0758f AP |
596 | if (data->kind == tmp411) { |
597 | for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) | |
598 | device_remove_file(&client->dev, | |
599 | &tmp411_attr[i].dev_attr); | |
600 | } | |
601 | ||
ab2b79d5 HG |
602 | kfree(data); |
603 | return 0; | |
604 | } | |
605 | ||
fce0758f AP |
606 | static struct tmp401_data *tmp401_update_device_reg16( |
607 | struct i2c_client *client, struct tmp401_data *data) | |
608 | { | |
609 | int i; | |
610 | ||
611 | for (i = 0; i < 2; i++) { | |
612 | /* | |
613 | * High byte must be read first immediately followed | |
614 | * by the low byte | |
615 | */ | |
616 | data->temp[i] = i2c_smbus_read_byte_data(client, | |
617 | TMP401_TEMP_MSB[i]) << 8; | |
618 | data->temp[i] |= i2c_smbus_read_byte_data(client, | |
619 | TMP401_TEMP_LSB[i]); | |
620 | data->temp_low[i] = i2c_smbus_read_byte_data(client, | |
621 | TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8; | |
622 | data->temp_low[i] |= i2c_smbus_read_byte_data(client, | |
623 | TMP401_TEMP_LOW_LIMIT_LSB[i]); | |
624 | data->temp_high[i] = i2c_smbus_read_byte_data(client, | |
625 | TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8; | |
626 | data->temp_high[i] |= i2c_smbus_read_byte_data(client, | |
627 | TMP401_TEMP_HIGH_LIMIT_LSB[i]); | |
628 | data->temp_crit[i] = i2c_smbus_read_byte_data(client, | |
629 | TMP401_TEMP_CRIT_LIMIT[i]); | |
630 | ||
631 | if (data->kind == tmp411) { | |
632 | data->temp_lowest[i] = i2c_smbus_read_byte_data(client, | |
633 | TMP411_TEMP_LOWEST_MSB[i]) << 8; | |
634 | data->temp_lowest[i] |= i2c_smbus_read_byte_data( | |
635 | client, TMP411_TEMP_LOWEST_LSB[i]); | |
636 | ||
637 | data->temp_highest[i] = i2c_smbus_read_byte_data( | |
638 | client, TMP411_TEMP_HIGHEST_MSB[i]) << 8; | |
639 | data->temp_highest[i] |= i2c_smbus_read_byte_data( | |
640 | client, TMP411_TEMP_HIGHEST_LSB[i]); | |
641 | } | |
642 | } | |
643 | return data; | |
644 | } | |
645 | ||
ab2b79d5 HG |
646 | static struct tmp401_data *tmp401_update_device(struct device *dev) |
647 | { | |
648 | struct i2c_client *client = to_i2c_client(dev); | |
649 | struct tmp401_data *data = i2c_get_clientdata(client); | |
ab2b79d5 HG |
650 | |
651 | mutex_lock(&data->update_lock); | |
652 | ||
653 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | |
654 | data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS); | |
655 | data->config = i2c_smbus_read_byte_data(client, | |
656 | TMP401_CONFIG_READ); | |
fce0758f | 657 | tmp401_update_device_reg16(client, data); |
ab2b79d5 HG |
658 | |
659 | data->temp_crit_hyst = i2c_smbus_read_byte_data(client, | |
660 | TMP401_TEMP_CRIT_HYST); | |
661 | ||
662 | data->last_updated = jiffies; | |
663 | data->valid = 1; | |
664 | } | |
665 | ||
666 | mutex_unlock(&data->update_lock); | |
667 | ||
668 | return data; | |
669 | } | |
670 | ||
671 | static int __init tmp401_init(void) | |
672 | { | |
673 | return i2c_add_driver(&tmp401_driver); | |
674 | } | |
675 | ||
676 | static void __exit tmp401_exit(void) | |
677 | { | |
678 | i2c_del_driver(&tmp401_driver); | |
679 | } | |
680 | ||
681 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); | |
682 | MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver"); | |
683 | MODULE_LICENSE("GPL"); | |
684 | ||
685 | module_init(tmp401_init); | |
686 | module_exit(tmp401_exit); |