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> | |
947e9271 | 33 | #include <linux/bitops.h> |
ab2b79d5 HG |
34 | #include <linux/slab.h> |
35 | #include <linux/jiffies.h> | |
36 | #include <linux/i2c.h> | |
37 | #include <linux/hwmon.h> | |
38 | #include <linux/hwmon-sysfs.h> | |
39 | #include <linux/err.h> | |
40 | #include <linux/mutex.h> | |
41 | #include <linux/sysfs.h> | |
42 | ||
43 | /* Addresses to scan */ | |
a1fac92b | 44 | static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
ab2b79d5 | 45 | |
a1fac92b | 46 | enum chips { tmp401, tmp411, tmp431 }; |
ab2b79d5 HG |
47 | |
48 | /* | |
49 | * The TMP401 registers, note some registers have different addresses for | |
50 | * reading and writing | |
51 | */ | |
52 | #define TMP401_STATUS 0x02 | |
53 | #define TMP401_CONFIG_READ 0x03 | |
54 | #define TMP401_CONFIG_WRITE 0x09 | |
55 | #define TMP401_CONVERSION_RATE_READ 0x04 | |
56 | #define TMP401_CONVERSION_RATE_WRITE 0x0A | |
57 | #define TMP401_TEMP_CRIT_HYST 0x21 | |
ab2b79d5 HG |
58 | #define TMP401_MANUFACTURER_ID_REG 0xFE |
59 | #define TMP401_DEVICE_ID_REG 0xFF | |
60 | ||
14f2a665 GR |
61 | static const u8 TMP401_TEMP_MSB_READ[6][2] = { |
62 | { 0x00, 0x01 }, /* temp */ | |
63 | { 0x06, 0x08 }, /* low limit */ | |
64 | { 0x05, 0x07 }, /* high limit */ | |
65 | { 0x20, 0x19 }, /* therm (crit) limit */ | |
66 | { 0x30, 0x34 }, /* lowest */ | |
67 | { 0x32, 0x36 }, /* highest */ | |
68 | }; | |
69 | ||
70 | static const u8 TMP401_TEMP_MSB_WRITE[6][2] = { | |
71 | { 0, 0 }, /* temp (unused) */ | |
72 | { 0x0C, 0x0E }, /* low limit */ | |
73 | { 0x0B, 0x0D }, /* high limit */ | |
74 | { 0x20, 0x19 }, /* therm (crit) limit */ | |
75 | { 0x30, 0x34 }, /* lowest */ | |
76 | { 0x32, 0x36 }, /* highest */ | |
77 | }; | |
78 | ||
79 | static const u8 TMP401_TEMP_LSB[6][2] = { | |
80 | { 0x15, 0x10 }, /* temp */ | |
81 | { 0x17, 0x14 }, /* low limit */ | |
82 | { 0x16, 0x13 }, /* high limit */ | |
83 | { 0, 0 }, /* therm (crit) limit (unused) */ | |
84 | { 0x31, 0x35 }, /* lowest */ | |
85 | { 0x33, 0x37 }, /* highest */ | |
86 | }; | |
fce0758f | 87 | |
ab2b79d5 | 88 | /* Flags */ |
947e9271 GR |
89 | #define TMP401_CONFIG_RANGE BIT(2) |
90 | #define TMP401_CONFIG_SHUTDOWN BIT(6) | |
91 | #define TMP401_STATUS_LOCAL_CRIT BIT(0) | |
92 | #define TMP401_STATUS_REMOTE_CRIT BIT(1) | |
93 | #define TMP401_STATUS_REMOTE_OPEN BIT(2) | |
94 | #define TMP401_STATUS_REMOTE_LOW BIT(3) | |
95 | #define TMP401_STATUS_REMOTE_HIGH BIT(4) | |
96 | #define TMP401_STATUS_LOCAL_LOW BIT(5) | |
97 | #define TMP401_STATUS_LOCAL_HIGH BIT(6) | |
ab2b79d5 HG |
98 | |
99 | /* Manufacturer / Device ID's */ | |
100 | #define TMP401_MANUFACTURER_ID 0x55 | |
101 | #define TMP401_DEVICE_ID 0x11 | |
4ce5b1fe GR |
102 | #define TMP411A_DEVICE_ID 0x12 |
103 | #define TMP411B_DEVICE_ID 0x13 | |
104 | #define TMP411C_DEVICE_ID 0x10 | |
a1fac92b | 105 | #define TMP431_DEVICE_ID 0x31 |
ab2b79d5 | 106 | |
ab2b79d5 HG |
107 | /* |
108 | * Driver data (common to all clients) | |
109 | */ | |
110 | ||
111 | static const struct i2c_device_id tmp401_id[] = { | |
112 | { "tmp401", tmp401 }, | |
fce0758f | 113 | { "tmp411", tmp411 }, |
a1fac92b | 114 | { "tmp431", tmp431 }, |
ab2b79d5 HG |
115 | { } |
116 | }; | |
117 | MODULE_DEVICE_TABLE(i2c, tmp401_id); | |
118 | ||
ab2b79d5 HG |
119 | /* |
120 | * Client data (each client gets its own) | |
121 | */ | |
122 | ||
123 | struct tmp401_data { | |
124 | struct device *hwmon_dev; | |
125 | struct mutex update_lock; | |
126 | char valid; /* zero until following fields are valid */ | |
127 | unsigned long last_updated; /* in jiffies */ | |
dc71afe5 | 128 | enum chips kind; |
ab2b79d5 | 129 | |
0846e30d GR |
130 | unsigned int update_interval; /* in milliseconds */ |
131 | ||
ab2b79d5 HG |
132 | /* register values */ |
133 | u8 status; | |
134 | u8 config; | |
14f2a665 | 135 | u16 temp[6][2]; |
ab2b79d5 HG |
136 | u8 temp_crit_hyst; |
137 | }; | |
138 | ||
139 | /* | |
140 | * Sysfs attr show / store functions | |
141 | */ | |
142 | ||
143 | static int tmp401_register_to_temp(u16 reg, u8 config) | |
144 | { | |
145 | int temp = reg; | |
146 | ||
147 | if (config & TMP401_CONFIG_RANGE) | |
148 | temp -= 64 * 256; | |
149 | ||
14f2a665 | 150 | return DIV_ROUND_CLOSEST(temp * 125, 32); |
ab2b79d5 HG |
151 | } |
152 | ||
14f2a665 | 153 | static u16 tmp401_temp_to_register(long temp, u8 config, int zbits) |
ab2b79d5 HG |
154 | { |
155 | if (config & TMP401_CONFIG_RANGE) { | |
2a844c14 | 156 | temp = clamp_val(temp, -64000, 191000); |
ab2b79d5 HG |
157 | temp += 64000; |
158 | } else | |
2a844c14 | 159 | temp = clamp_val(temp, 0, 127000); |
ab2b79d5 | 160 | |
14f2a665 | 161 | return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits; |
ab2b79d5 HG |
162 | } |
163 | ||
14f2a665 GR |
164 | static int tmp401_update_device_reg16(struct i2c_client *client, |
165 | struct tmp401_data *data) | |
ea63c2b9 | 166 | { |
14f2a665 GR |
167 | int i, j, val; |
168 | int num_regs = data->kind == tmp411 ? 6 : 4; | |
169 | ||
170 | for (i = 0; i < 2; i++) { /* local / rem1 */ | |
171 | for (j = 0; j < num_regs; j++) { /* temp / low / ... */ | |
172 | /* | |
173 | * High byte must be read first immediately followed | |
174 | * by the low byte | |
175 | */ | |
176 | val = i2c_smbus_read_byte_data(client, | |
177 | TMP401_TEMP_MSB_READ[j][i]); | |
178 | if (val < 0) | |
179 | return val; | |
180 | data->temp[j][i] = val << 8; | |
181 | if (j == 3) /* crit is msb only */ | |
182 | continue; | |
183 | val = i2c_smbus_read_byte_data(client, | |
184 | TMP401_TEMP_LSB[j][i]); | |
185 | if (val < 0) | |
186 | return val; | |
187 | data->temp[j][i] |= val; | |
ea63c2b9 AP |
188 | } |
189 | } | |
14f2a665 | 190 | return 0; |
ea63c2b9 AP |
191 | } |
192 | ||
193 | static struct tmp401_data *tmp401_update_device(struct device *dev) | |
194 | { | |
195 | struct i2c_client *client = to_i2c_client(dev); | |
196 | struct tmp401_data *data = i2c_get_clientdata(client); | |
14f2a665 GR |
197 | struct tmp401_data *ret = data; |
198 | int val; | |
0846e30d | 199 | unsigned long next_update; |
ea63c2b9 AP |
200 | |
201 | mutex_lock(&data->update_lock); | |
202 | ||
0846e30d GR |
203 | next_update = data->last_updated + |
204 | msecs_to_jiffies(data->update_interval) + 1; | |
205 | if (time_after(jiffies, next_update) || !data->valid) { | |
14f2a665 GR |
206 | val = i2c_smbus_read_byte_data(client, TMP401_STATUS); |
207 | if (val < 0) { | |
208 | ret = ERR_PTR(val); | |
209 | goto abort; | |
210 | } | |
211 | data->status = val; | |
212 | val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); | |
213 | if (val < 0) { | |
214 | ret = ERR_PTR(val); | |
215 | goto abort; | |
216 | } | |
217 | data->config = val; | |
218 | val = tmp401_update_device_reg16(client, data); | |
219 | if (val < 0) { | |
220 | ret = ERR_PTR(val); | |
221 | goto abort; | |
222 | } | |
223 | val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST); | |
224 | if (val < 0) { | |
225 | ret = ERR_PTR(val); | |
226 | goto abort; | |
227 | } | |
228 | data->temp_crit_hyst = val; | |
ea63c2b9 AP |
229 | |
230 | data->last_updated = jiffies; | |
231 | data->valid = 1; | |
232 | } | |
233 | ||
14f2a665 | 234 | abort: |
ea63c2b9 | 235 | mutex_unlock(&data->update_lock); |
14f2a665 | 236 | return ret; |
ab2b79d5 HG |
237 | } |
238 | ||
14f2a665 GR |
239 | static ssize_t show_temp(struct device *dev, |
240 | struct device_attribute *devattr, char *buf) | |
ab2b79d5 | 241 | { |
14f2a665 GR |
242 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
243 | int index = to_sensor_dev_attr_2(devattr)->index; | |
ab2b79d5 HG |
244 | struct tmp401_data *data = tmp401_update_device(dev); |
245 | ||
14f2a665 GR |
246 | if (IS_ERR(data)) |
247 | return PTR_ERR(data); | |
ab2b79d5 HG |
248 | |
249 | return sprintf(buf, "%d\n", | |
14f2a665 | 250 | tmp401_register_to_temp(data->temp[nr][index], data->config)); |
ab2b79d5 HG |
251 | } |
252 | ||
253 | static ssize_t show_temp_crit_hyst(struct device *dev, | |
254 | struct device_attribute *devattr, char *buf) | |
255 | { | |
256 | int temp, index = to_sensor_dev_attr(devattr)->index; | |
257 | struct tmp401_data *data = tmp401_update_device(dev); | |
258 | ||
14f2a665 GR |
259 | if (IS_ERR(data)) |
260 | return PTR_ERR(data); | |
261 | ||
ab2b79d5 | 262 | mutex_lock(&data->update_lock); |
14f2a665 | 263 | temp = tmp401_register_to_temp(data->temp[3][index], data->config); |
ab2b79d5 HG |
264 | temp -= data->temp_crit_hyst * 1000; |
265 | mutex_unlock(&data->update_lock); | |
266 | ||
267 | return sprintf(buf, "%d\n", temp); | |
268 | } | |
269 | ||
270 | static ssize_t show_status(struct device *dev, | |
271 | struct device_attribute *devattr, char *buf) | |
272 | { | |
273 | int mask = to_sensor_dev_attr(devattr)->index; | |
274 | struct tmp401_data *data = tmp401_update_device(dev); | |
275 | ||
14f2a665 GR |
276 | if (IS_ERR(data)) |
277 | return PTR_ERR(data); | |
ab2b79d5 | 278 | |
14f2a665 | 279 | return sprintf(buf, "%d\n", !!(data->status & mask)); |
ab2b79d5 HG |
280 | } |
281 | ||
14f2a665 GR |
282 | static ssize_t store_temp(struct device *dev, struct device_attribute *devattr, |
283 | const char *buf, size_t count) | |
ab2b79d5 | 284 | { |
14f2a665 GR |
285 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
286 | int index = to_sensor_dev_attr_2(devattr)->index; | |
287 | struct i2c_client *client = to_i2c_client(dev); | |
ab2b79d5 HG |
288 | struct tmp401_data *data = tmp401_update_device(dev); |
289 | long val; | |
290 | u16 reg; | |
291 | ||
14f2a665 GR |
292 | if (IS_ERR(data)) |
293 | return PTR_ERR(data); | |
ab2b79d5 | 294 | |
179c4fdb | 295 | if (kstrtol(buf, 10, &val)) |
ab2b79d5 HG |
296 | return -EINVAL; |
297 | ||
14f2a665 | 298 | reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4); |
ab2b79d5 HG |
299 | |
300 | mutex_lock(&data->update_lock); | |
301 | ||
14f2a665 GR |
302 | i2c_smbus_write_byte_data(client, |
303 | TMP401_TEMP_MSB_WRITE[nr][index], | |
304 | reg >> 8); | |
305 | if (nr != 3) { | |
306 | i2c_smbus_write_byte_data(client, | |
307 | TMP401_TEMP_LSB[nr][index], | |
308 | reg & 0xFF); | |
309 | } | |
310 | data->temp[nr][index] = reg; | |
ab2b79d5 HG |
311 | |
312 | mutex_unlock(&data->update_lock); | |
313 | ||
314 | return count; | |
315 | } | |
316 | ||
317 | static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute | |
318 | *devattr, const char *buf, size_t count) | |
319 | { | |
320 | int temp, index = to_sensor_dev_attr(devattr)->index; | |
321 | struct tmp401_data *data = tmp401_update_device(dev); | |
322 | long val; | |
323 | u8 reg; | |
324 | ||
14f2a665 GR |
325 | if (IS_ERR(data)) |
326 | return PTR_ERR(data); | |
327 | ||
179c4fdb | 328 | if (kstrtol(buf, 10, &val)) |
ab2b79d5 HG |
329 | return -EINVAL; |
330 | ||
331 | if (data->config & TMP401_CONFIG_RANGE) | |
2a844c14 | 332 | val = clamp_val(val, -64000, 191000); |
ab2b79d5 | 333 | else |
2a844c14 | 334 | val = clamp_val(val, 0, 127000); |
ab2b79d5 HG |
335 | |
336 | mutex_lock(&data->update_lock); | |
14f2a665 | 337 | temp = tmp401_register_to_temp(data->temp[3][index], data->config); |
2a844c14 | 338 | val = clamp_val(val, temp - 255000, temp); |
ab2b79d5 HG |
339 | reg = ((temp - val) + 500) / 1000; |
340 | ||
14f2a665 GR |
341 | i2c_smbus_write_byte_data(to_i2c_client(dev), TMP401_TEMP_CRIT_HYST, |
342 | reg); | |
ab2b79d5 HG |
343 | |
344 | data->temp_crit_hyst = reg; | |
345 | ||
346 | mutex_unlock(&data->update_lock); | |
347 | ||
348 | return count; | |
349 | } | |
350 | ||
fce0758f AP |
351 | /* |
352 | * Resets the historical measurements of minimum and maximum temperatures. | |
353 | * This is done by writing any value to any of the minimum/maximum registers | |
354 | * (0x30-0x37). | |
355 | */ | |
356 | static ssize_t reset_temp_history(struct device *dev, | |
357 | struct device_attribute *devattr, const char *buf, size_t count) | |
358 | { | |
8eb6d90f GR |
359 | struct i2c_client *client = to_i2c_client(dev); |
360 | struct tmp401_data *data = i2c_get_clientdata(client); | |
fce0758f AP |
361 | long val; |
362 | ||
179c4fdb | 363 | if (kstrtol(buf, 10, &val)) |
fce0758f AP |
364 | return -EINVAL; |
365 | ||
366 | if (val != 1) { | |
b55f3757 GR |
367 | dev_err(dev, |
368 | "temp_reset_history value %ld not supported. Use 1 to reset the history!\n", | |
369 | val); | |
fce0758f AP |
370 | return -EINVAL; |
371 | } | |
8eb6d90f GR |
372 | mutex_lock(&data->update_lock); |
373 | i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val); | |
374 | data->valid = 0; | |
375 | mutex_unlock(&data->update_lock); | |
fce0758f AP |
376 | |
377 | return count; | |
378 | } | |
379 | ||
0846e30d GR |
380 | static ssize_t show_update_interval(struct device *dev, |
381 | struct device_attribute *attr, char *buf) | |
382 | { | |
383 | struct i2c_client *client = to_i2c_client(dev); | |
384 | struct tmp401_data *data = i2c_get_clientdata(client); | |
385 | ||
386 | return sprintf(buf, "%u\n", data->update_interval); | |
387 | } | |
388 | ||
389 | static ssize_t set_update_interval(struct device *dev, | |
390 | struct device_attribute *attr, | |
391 | const char *buf, size_t count) | |
392 | { | |
393 | struct i2c_client *client = to_i2c_client(dev); | |
394 | struct tmp401_data *data = i2c_get_clientdata(client); | |
395 | unsigned long val; | |
396 | int err, rate; | |
397 | ||
398 | err = kstrtoul(buf, 10, &val); | |
399 | if (err) | |
400 | return err; | |
401 | ||
402 | /* | |
403 | * For valid rates, interval can be calculated as | |
404 | * interval = (1 << (7 - rate)) * 125; | |
405 | * Rounded rate is therefore | |
406 | * rate = 7 - __fls(interval * 4 / (125 * 3)); | |
407 | * Use clamp_val() to avoid overflows, and to ensure valid input | |
408 | * for __fls. | |
409 | */ | |
410 | val = clamp_val(val, 125, 16000); | |
411 | rate = 7 - __fls(val * 4 / (125 * 3)); | |
412 | mutex_lock(&data->update_lock); | |
413 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate); | |
414 | data->update_interval = (1 << (7 - rate)) * 125; | |
415 | mutex_unlock(&data->update_lock); | |
416 | ||
417 | return count; | |
418 | } | |
419 | ||
14f2a665 GR |
420 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0); |
421 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IWUSR | S_IRUGO, show_temp, | |
422 | store_temp, 1, 0); | |
423 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp, | |
424 | store_temp, 2, 0); | |
425 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IWUSR | S_IRUGO, show_temp, | |
426 | store_temp, 3, 0); | |
b4e665c7 GR |
427 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, |
428 | show_temp_crit_hyst, store_temp_crit_hyst, 0); | |
429 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL, | |
430 | TMP401_STATUS_LOCAL_LOW); | |
431 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL, | |
432 | TMP401_STATUS_LOCAL_HIGH); | |
433 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL, | |
434 | TMP401_STATUS_LOCAL_CRIT); | |
14f2a665 GR |
435 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1); |
436 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp, | |
437 | store_temp, 1, 1); | |
438 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp, | |
439 | store_temp, 2, 1); | |
440 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IWUSR | S_IRUGO, show_temp, | |
441 | store_temp, 3, 1); | |
b4e665c7 GR |
442 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, |
443 | NULL, 1); | |
444 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_status, NULL, | |
445 | TMP401_STATUS_REMOTE_OPEN); | |
446 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL, | |
447 | TMP401_STATUS_REMOTE_LOW); | |
448 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL, | |
449 | TMP401_STATUS_REMOTE_HIGH); | |
450 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL, | |
451 | TMP401_STATUS_REMOTE_CRIT); | |
452 | ||
0846e30d GR |
453 | static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval, |
454 | set_update_interval); | |
455 | ||
b4e665c7 GR |
456 | static struct attribute *tmp401_attributes[] = { |
457 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
458 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
459 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
460 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
461 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | |
462 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
463 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
464 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
465 | ||
466 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
467 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
468 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
469 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
470 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | |
471 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
472 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
473 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
474 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
475 | ||
0846e30d GR |
476 | &dev_attr_update_interval.attr, |
477 | ||
b4e665c7 GR |
478 | NULL |
479 | }; | |
480 | ||
481 | static const struct attribute_group tmp401_group = { | |
482 | .attrs = tmp401_attributes, | |
ab2b79d5 HG |
483 | }; |
484 | ||
fce0758f AP |
485 | /* |
486 | * Additional features of the TMP411 chip. | |
487 | * The TMP411 stores the minimum and maximum | |
488 | * temperature measured since power-on, chip-reset, or | |
489 | * minimum and maximum register reset for both the local | |
490 | * and remote channels. | |
491 | */ | |
14f2a665 GR |
492 | static SENSOR_DEVICE_ATTR_2(temp1_lowest, S_IRUGO, show_temp, NULL, 4, 0); |
493 | static SENSOR_DEVICE_ATTR_2(temp1_highest, S_IRUGO, show_temp, NULL, 5, 0); | |
494 | static SENSOR_DEVICE_ATTR_2(temp2_lowest, S_IRUGO, show_temp, NULL, 4, 1); | |
495 | static SENSOR_DEVICE_ATTR_2(temp2_highest, S_IRUGO, show_temp, NULL, 5, 1); | |
b4e665c7 GR |
496 | static SENSOR_DEVICE_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history, |
497 | 0); | |
498 | ||
499 | static struct attribute *tmp411_attributes[] = { | |
500 | &sensor_dev_attr_temp1_highest.dev_attr.attr, | |
501 | &sensor_dev_attr_temp1_lowest.dev_attr.attr, | |
502 | &sensor_dev_attr_temp2_highest.dev_attr.attr, | |
503 | &sensor_dev_attr_temp2_lowest.dev_attr.attr, | |
504 | &sensor_dev_attr_temp_reset_history.dev_attr.attr, | |
505 | NULL | |
506 | }; | |
507 | ||
508 | static const struct attribute_group tmp411_group = { | |
509 | .attrs = tmp411_attributes, | |
fce0758f AP |
510 | }; |
511 | ||
ab2b79d5 HG |
512 | /* |
513 | * Begin non sysfs callback code (aka Real code) | |
514 | */ | |
515 | ||
516 | static void tmp401_init_client(struct i2c_client *client) | |
517 | { | |
518 | int config, config_orig; | |
0846e30d | 519 | struct tmp401_data *data = i2c_get_clientdata(client); |
ab2b79d5 HG |
520 | |
521 | /* Set the conversion rate to 2 Hz */ | |
522 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5); | |
0846e30d | 523 | data->update_interval = 500; |
ab2b79d5 HG |
524 | |
525 | /* Start conversions (disable shutdown if necessary) */ | |
526 | config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); | |
527 | if (config < 0) { | |
528 | dev_warn(&client->dev, "Initialization failed!\n"); | |
529 | return; | |
530 | } | |
531 | ||
532 | config_orig = config; | |
533 | config &= ~TMP401_CONFIG_SHUTDOWN; | |
534 | ||
535 | if (config != config_orig) | |
536 | i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config); | |
537 | } | |
538 | ||
310ec792 | 539 | static int tmp401_detect(struct i2c_client *client, |
ab2b79d5 HG |
540 | struct i2c_board_info *info) |
541 | { | |
dbe73c8f | 542 | enum chips kind; |
ab2b79d5 | 543 | struct i2c_adapter *adapter = client->adapter; |
dbe73c8f | 544 | u8 reg; |
ab2b79d5 HG |
545 | |
546 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
547 | return -ENODEV; | |
548 | ||
549 | /* Detect and identify the chip */ | |
dbe73c8f JD |
550 | reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG); |
551 | if (reg != TMP401_MANUFACTURER_ID) | |
552 | return -ENODEV; | |
ab2b79d5 | 553 | |
dbe73c8f | 554 | reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); |
ab2b79d5 | 555 | |
dbe73c8f JD |
556 | switch (reg) { |
557 | case TMP401_DEVICE_ID: | |
a1fac92b GR |
558 | if (client->addr != 0x4c) |
559 | return -ENODEV; | |
dbe73c8f JD |
560 | kind = tmp401; |
561 | break; | |
4ce5b1fe GR |
562 | case TMP411A_DEVICE_ID: |
563 | if (client->addr != 0x4c) | |
564 | return -ENODEV; | |
565 | kind = tmp411; | |
566 | break; | |
567 | case TMP411B_DEVICE_ID: | |
568 | if (client->addr != 0x4d) | |
569 | return -ENODEV; | |
570 | kind = tmp411; | |
571 | break; | |
572 | case TMP411C_DEVICE_ID: | |
573 | if (client->addr != 0x4e) | |
574 | return -ENODEV; | |
dbe73c8f JD |
575 | kind = tmp411; |
576 | break; | |
a1fac92b GR |
577 | case TMP431_DEVICE_ID: |
578 | if (client->addr == 0x4e) | |
579 | return -ENODEV; | |
580 | kind = tmp431; | |
581 | break; | |
dbe73c8f JD |
582 | default: |
583 | return -ENODEV; | |
ab2b79d5 | 584 | } |
dbe73c8f JD |
585 | |
586 | reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); | |
587 | if (reg & 0x1b) | |
588 | return -ENODEV; | |
589 | ||
590 | reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ); | |
591 | /* Datasheet says: 0x1-0x6 */ | |
592 | if (reg > 15) | |
593 | return -ENODEV; | |
594 | ||
dc71afe5 | 595 | strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE); |
ab2b79d5 HG |
596 | |
597 | return 0; | |
598 | } | |
599 | ||
ea63c2b9 AP |
600 | static int tmp401_remove(struct i2c_client *client) |
601 | { | |
b4e665c7 | 602 | struct device *dev = &client->dev; |
ea63c2b9 | 603 | struct tmp401_data *data = i2c_get_clientdata(client); |
ea63c2b9 AP |
604 | |
605 | if (data->hwmon_dev) | |
606 | hwmon_device_unregister(data->hwmon_dev); | |
607 | ||
b4e665c7 | 608 | sysfs_remove_group(&dev->kobj, &tmp401_group); |
ea63c2b9 | 609 | |
b4e665c7 GR |
610 | if (data->kind == tmp411) |
611 | sysfs_remove_group(&dev->kobj, &tmp411_group); | |
ea63c2b9 | 612 | |
ea63c2b9 AP |
613 | return 0; |
614 | } | |
615 | ||
ab2b79d5 HG |
616 | static int tmp401_probe(struct i2c_client *client, |
617 | const struct i2c_device_id *id) | |
618 | { | |
b4e665c7 GR |
619 | struct device *dev = &client->dev; |
620 | int err; | |
ab2b79d5 | 621 | struct tmp401_data *data; |
a1fac92b | 622 | const char *names[] = { "TMP401", "TMP411", "TMP431" }; |
ab2b79d5 | 623 | |
b4e665c7 | 624 | data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL); |
ab2b79d5 HG |
625 | if (!data) |
626 | return -ENOMEM; | |
627 | ||
628 | i2c_set_clientdata(client, data); | |
629 | mutex_init(&data->update_lock); | |
fce0758f | 630 | data->kind = id->driver_data; |
ab2b79d5 HG |
631 | |
632 | /* Initialize the TMP401 chip */ | |
633 | tmp401_init_client(client); | |
634 | ||
635 | /* Register sysfs hooks */ | |
b4e665c7 GR |
636 | err = sysfs_create_group(&dev->kobj, &tmp401_group); |
637 | if (err) | |
638 | return err; | |
ab2b79d5 | 639 | |
a80581d0 | 640 | /* Register additional tmp411 sysfs hooks */ |
fce0758f | 641 | if (data->kind == tmp411) { |
b4e665c7 GR |
642 | err = sysfs_create_group(&dev->kobj, &tmp411_group); |
643 | if (err) | |
644 | goto exit_remove; | |
fce0758f AP |
645 | } |
646 | ||
b4e665c7 | 647 | data->hwmon_dev = hwmon_device_register(dev); |
ab2b79d5 HG |
648 | if (IS_ERR(data->hwmon_dev)) { |
649 | err = PTR_ERR(data->hwmon_dev); | |
650 | data->hwmon_dev = NULL; | |
651 | goto exit_remove; | |
652 | } | |
653 | ||
b4e665c7 | 654 | dev_info(dev, "Detected TI %s chip\n", names[data->kind]); |
ab2b79d5 HG |
655 | |
656 | return 0; | |
657 | ||
658 | exit_remove: | |
0df9fc7b | 659 | tmp401_remove(client); |
ab2b79d5 HG |
660 | return err; |
661 | } | |
662 | ||
ea63c2b9 AP |
663 | static struct i2c_driver tmp401_driver = { |
664 | .class = I2C_CLASS_HWMON, | |
665 | .driver = { | |
666 | .name = "tmp401", | |
667 | }, | |
668 | .probe = tmp401_probe, | |
669 | .remove = tmp401_remove, | |
670 | .id_table = tmp401_id, | |
671 | .detect = tmp401_detect, | |
672 | .address_list = normal_i2c, | |
673 | }; | |
ab2b79d5 | 674 | |
f0967eea | 675 | module_i2c_driver(tmp401_driver); |
ab2b79d5 HG |
676 | |
677 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); | |
678 | MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver"); | |
679 | MODULE_LICENSE("GPL"); |