blob: fa6af51b300ddc6624dbc44eb4816221e3cf033b [file] [log] [blame]
Hans de Goedeab2b79d2009-06-15 18:39:46 +02001/* tmp401.c
2 *
3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
Andre Prendelfce07582009-06-15 18:39:47 +02004 * Preliminary tmp411 support by:
5 * Gabriel Konat, Sander Leget, Wouter Willems
6 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
Hans de Goedeab2b79d2009-06-15 18:39:46 +02007 *
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>
Guenter Roeck947e9272013-03-27 08:48:03 -070033#include <linux/bitops.h>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020034#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 */
Guenter Roecka1fac922013-03-15 12:55:08 -070044static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020045
Guenter Roecka1fac922013-03-15 12:55:08 -070046enum chips { tmp401, tmp411, tmp431 };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020047
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
Hans de Goedeab2b79d2009-06-15 18:39:46 +020058#define TMP401_MANUFACTURER_ID_REG 0xFE
59#define TMP401_DEVICE_ID_REG 0xFF
60
Guenter Roeck14f2a662013-03-27 21:23:10 -070061static 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};
Hans de Goedeab2b79d2009-06-15 18:39:46 +020069
Guenter Roeck14f2a662013-03-27 21:23:10 -070070static 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
79static 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};
Andre Prendelfce07582009-06-15 18:39:47 +020087
Hans de Goedeab2b79d2009-06-15 18:39:46 +020088/* Flags */
Guenter Roeck947e9272013-03-27 08:48:03 -070089#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)
Hans de Goedeab2b79d2009-06-15 18:39:46 +020098
99/* Manufacturer / Device ID's */
100#define TMP401_MANUFACTURER_ID 0x55
101#define TMP401_DEVICE_ID 0x11
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700102#define TMP411A_DEVICE_ID 0x12
103#define TMP411B_DEVICE_ID 0x13
104#define TMP411C_DEVICE_ID 0x10
Guenter Roecka1fac922013-03-15 12:55:08 -0700105#define TMP431_DEVICE_ID 0x31
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200106
107/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200108 * Driver data (common to all clients)
109 */
110
111static const struct i2c_device_id tmp401_id[] = {
112 { "tmp401", tmp401 },
Andre Prendelfce07582009-06-15 18:39:47 +0200113 { "tmp411", tmp411 },
Guenter Roecka1fac922013-03-15 12:55:08 -0700114 { "tmp431", tmp431 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200115 { }
116};
117MODULE_DEVICE_TABLE(i2c, tmp401_id);
118
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200119/*
120 * Client data (each client gets its own)
121 */
122
123struct 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 */
Jean Delvaredc71afe2010-03-05 22:17:26 +0100128 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200129
Guenter Roeck0846e302013-03-28 02:03:10 -0700130 unsigned int update_interval; /* in milliseconds */
131
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200132 /* register values */
133 u8 status;
134 u8 config;
Guenter Roeck14f2a662013-03-27 21:23:10 -0700135 u16 temp[6][2];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200136 u8 temp_crit_hyst;
137};
138
139/*
140 * Sysfs attr show / store functions
141 */
142
143static 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
Guenter Roeck14f2a662013-03-27 21:23:10 -0700150 return DIV_ROUND_CLOSEST(temp * 125, 32);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200151}
152
Guenter Roeck14f2a662013-03-27 21:23:10 -0700153static u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200154{
155 if (config & TMP401_CONFIG_RANGE) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800156 temp = clamp_val(temp, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200157 temp += 64000;
158 } else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800159 temp = clamp_val(temp, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200160
Guenter Roeck14f2a662013-03-27 21:23:10 -0700161 return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200162}
163
Guenter Roeck14f2a662013-03-27 21:23:10 -0700164static int tmp401_update_device_reg16(struct i2c_client *client,
165 struct tmp401_data *data)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200166{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700167 int i, j, val;
168 int num_regs = data->kind == tmp411 ? 6 : 4;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200169
Guenter Roeck14f2a662013-03-27 21:23:10 -0700170 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;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200188 }
189 }
Guenter Roeck14f2a662013-03-27 21:23:10 -0700190 return 0;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200191}
192
193static 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);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700197 struct tmp401_data *ret = data;
198 int val;
Guenter Roeck0846e302013-03-28 02:03:10 -0700199 unsigned long next_update;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200200
201 mutex_lock(&data->update_lock);
202
Guenter Roeck0846e302013-03-28 02:03:10 -0700203 next_update = data->last_updated +
204 msecs_to_jiffies(data->update_interval) + 1;
205 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck14f2a662013-03-27 21:23:10 -0700206 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;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200229
230 data->last_updated = jiffies;
231 data->valid = 1;
232 }
233
Guenter Roeck14f2a662013-03-27 21:23:10 -0700234abort:
Andre Prendelea63c2b2010-05-27 19:58:49 +0200235 mutex_unlock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700236 return ret;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200237}
238
Guenter Roeck14f2a662013-03-27 21:23:10 -0700239static ssize_t show_temp(struct device *dev,
240 struct device_attribute *devattr, char *buf)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200241{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700242 int nr = to_sensor_dev_attr_2(devattr)->nr;
243 int index = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200244 struct tmp401_data *data = tmp401_update_device(dev);
245
Guenter Roeck14f2a662013-03-27 21:23:10 -0700246 if (IS_ERR(data))
247 return PTR_ERR(data);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200248
249 return sprintf(buf, "%d\n",
Guenter Roeck14f2a662013-03-27 21:23:10 -0700250 tmp401_register_to_temp(data->temp[nr][index], data->config));
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200251}
252
253static 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
Guenter Roeck14f2a662013-03-27 21:23:10 -0700259 if (IS_ERR(data))
260 return PTR_ERR(data);
261
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200262 mutex_lock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700263 temp = tmp401_register_to_temp(data->temp[3][index], data->config);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200264 temp -= data->temp_crit_hyst * 1000;
265 mutex_unlock(&data->update_lock);
266
267 return sprintf(buf, "%d\n", temp);
268}
269
270static 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
Guenter Roeck14f2a662013-03-27 21:23:10 -0700276 if (IS_ERR(data))
277 return PTR_ERR(data);
278
279 return sprintf(buf, "%d\n", !!(data->status & mask));
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200280}
281
Guenter Roeck14f2a662013-03-27 21:23:10 -0700282static ssize_t store_temp(struct device *dev, struct device_attribute *devattr,
283 const char *buf, size_t count)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200284{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700285 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);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200288 struct tmp401_data *data = tmp401_update_device(dev);
289 long val;
290 u16 reg;
291
Guenter Roeck14f2a662013-03-27 21:23:10 -0700292 if (IS_ERR(data))
293 return PTR_ERR(data);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200294
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100295 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200296 return -EINVAL;
297
Guenter Roeck14f2a662013-03-27 21:23:10 -0700298 reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200299
300 mutex_lock(&data->update_lock);
301
Guenter Roeck14f2a662013-03-27 21:23:10 -0700302 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;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200311
312 mutex_unlock(&data->update_lock);
313
314 return count;
315}
316
317static 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
Guenter Roeck14f2a662013-03-27 21:23:10 -0700325 if (IS_ERR(data))
326 return PTR_ERR(data);
327
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100328 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200329 return -EINVAL;
330
331 if (data->config & TMP401_CONFIG_RANGE)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800332 val = clamp_val(val, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200333 else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800334 val = clamp_val(val, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200335
336 mutex_lock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700337 temp = tmp401_register_to_temp(data->temp[3][index], data->config);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800338 val = clamp_val(val, temp - 255000, temp);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200339 reg = ((temp - val) + 500) / 1000;
340
Guenter Roeck14f2a662013-03-27 21:23:10 -0700341 i2c_smbus_write_byte_data(to_i2c_client(dev), TMP401_TEMP_CRIT_HYST,
342 reg);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200343
344 data->temp_crit_hyst = reg;
345
346 mutex_unlock(&data->update_lock);
347
348 return count;
349}
350
Andre Prendelfce07582009-06-15 18:39:47 +0200351/*
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 */
356static ssize_t reset_temp_history(struct device *dev,
357 struct device_attribute *devattr, const char *buf, size_t count)
358{
Guenter Roeck8eb6d902013-04-14 04:39:46 -0700359 struct i2c_client *client = to_i2c_client(dev);
360 struct tmp401_data *data = i2c_get_clientdata(client);
Andre Prendelfce07582009-06-15 18:39:47 +0200361 long val;
362
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100363 if (kstrtol(buf, 10, &val))
Andre Prendelfce07582009-06-15 18:39:47 +0200364 return -EINVAL;
365
366 if (val != 1) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800367 dev_err(dev,
368 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
369 val);
Andre Prendelfce07582009-06-15 18:39:47 +0200370 return -EINVAL;
371 }
Guenter Roeck8eb6d902013-04-14 04:39:46 -0700372 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);
Andre Prendelfce07582009-06-15 18:39:47 +0200376
377 return count;
378}
379
Guenter Roeck0846e302013-03-28 02:03:10 -0700380static 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
389static 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
Guenter Roeck14f2a662013-03-27 21:23:10 -0700420static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
421static SENSOR_DEVICE_ATTR_2(temp1_min, S_IWUSR | S_IRUGO, show_temp,
422 store_temp, 1, 0);
423static SENSOR_DEVICE_ATTR_2(temp1_max, S_IWUSR | S_IRUGO, show_temp,
424 store_temp, 2, 0);
425static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
426 store_temp, 3, 0);
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700427static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
428 show_temp_crit_hyst, store_temp_crit_hyst, 0);
429static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
430 TMP401_STATUS_LOCAL_LOW);
431static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
432 TMP401_STATUS_LOCAL_HIGH);
433static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
434 TMP401_STATUS_LOCAL_CRIT);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700435static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
436static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp,
437 store_temp, 1, 1);
438static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp,
439 store_temp, 2, 1);
440static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IWUSR | S_IRUGO, show_temp,
441 store_temp, 3, 1);
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700442static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst,
443 NULL, 1);
444static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_status, NULL,
445 TMP401_STATUS_REMOTE_OPEN);
446static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
447 TMP401_STATUS_REMOTE_LOW);
448static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
449 TMP401_STATUS_REMOTE_HIGH);
450static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
451 TMP401_STATUS_REMOTE_CRIT);
452
Guenter Roeck0846e302013-03-28 02:03:10 -0700453static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
454 set_update_interval);
455
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700456static 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
Guenter Roeck0846e302013-03-28 02:03:10 -0700476 &dev_attr_update_interval.attr,
477
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700478 NULL
479};
480
481static const struct attribute_group tmp401_group = {
482 .attrs = tmp401_attributes,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200483};
484
485/*
Andre Prendelfce07582009-06-15 18:39:47 +0200486 * 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 */
Guenter Roeck14f2a662013-03-27 21:23:10 -0700492static SENSOR_DEVICE_ATTR_2(temp1_lowest, S_IRUGO, show_temp, NULL, 4, 0);
493static SENSOR_DEVICE_ATTR_2(temp1_highest, S_IRUGO, show_temp, NULL, 5, 0);
494static SENSOR_DEVICE_ATTR_2(temp2_lowest, S_IRUGO, show_temp, NULL, 4, 1);
495static SENSOR_DEVICE_ATTR_2(temp2_highest, S_IRUGO, show_temp, NULL, 5, 1);
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700496static SENSOR_DEVICE_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history,
497 0);
498
499static 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
508static const struct attribute_group tmp411_group = {
509 .attrs = tmp411_attributes,
Andre Prendelfce07582009-06-15 18:39:47 +0200510};
511
512/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200513 * Begin non sysfs callback code (aka Real code)
514 */
515
516static void tmp401_init_client(struct i2c_client *client)
517{
518 int config, config_orig;
Guenter Roeck0846e302013-03-28 02:03:10 -0700519 struct tmp401_data *data = i2c_get_clientdata(client);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200520
521 /* Set the conversion rate to 2 Hz */
522 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
Guenter Roeck0846e302013-03-28 02:03:10 -0700523 data->update_interval = 500;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200524
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
Jean Delvare310ec792009-12-14 21:17:23 +0100539static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200540 struct i2c_board_info *info)
541{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100542 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200543 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100544 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200545
546 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
547 return -ENODEV;
548
549 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100550 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
551 if (reg != TMP401_MANUFACTURER_ID)
552 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200553
Jean Delvaredbe73c82009-12-09 20:35:54 +0100554 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200555
Jean Delvaredbe73c82009-12-09 20:35:54 +0100556 switch (reg) {
557 case TMP401_DEVICE_ID:
Guenter Roecka1fac922013-03-15 12:55:08 -0700558 if (client->addr != 0x4c)
559 return -ENODEV;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100560 kind = tmp401;
561 break;
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700562 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;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100575 kind = tmp411;
576 break;
Guenter Roecka1fac922013-03-15 12:55:08 -0700577 case TMP431_DEVICE_ID:
578 if (client->addr == 0x4e)
579 return -ENODEV;
580 kind = tmp431;
581 break;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100582 default:
583 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200584 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100585
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
Jean Delvaredc71afe2010-03-05 22:17:26 +0100595 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200596
597 return 0;
598}
599
Andre Prendelea63c2b2010-05-27 19:58:49 +0200600static int tmp401_remove(struct i2c_client *client)
601{
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700602 struct device *dev = &client->dev;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200603 struct tmp401_data *data = i2c_get_clientdata(client);
Andre Prendelea63c2b2010-05-27 19:58:49 +0200604
605 if (data->hwmon_dev)
606 hwmon_device_unregister(data->hwmon_dev);
607
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700608 sysfs_remove_group(&dev->kobj, &tmp401_group);
Andre Prendelea63c2b2010-05-27 19:58:49 +0200609
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700610 if (data->kind == tmp411)
611 sysfs_remove_group(&dev->kobj, &tmp411_group);
Andre Prendelea63c2b2010-05-27 19:58:49 +0200612
Andre Prendelea63c2b2010-05-27 19:58:49 +0200613 return 0;
614}
615
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200616static int tmp401_probe(struct i2c_client *client,
617 const struct i2c_device_id *id)
618{
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700619 struct device *dev = &client->dev;
620 int err;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200621 struct tmp401_data *data;
Guenter Roecka1fac922013-03-15 12:55:08 -0700622 const char *names[] = { "TMP401", "TMP411", "TMP431" };
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200623
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700624 data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200625 if (!data)
626 return -ENOMEM;
627
628 i2c_set_clientdata(client, data);
629 mutex_init(&data->update_lock);
Andre Prendelfce07582009-06-15 18:39:47 +0200630 data->kind = id->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200631
632 /* Initialize the TMP401 chip */
633 tmp401_init_client(client);
634
635 /* Register sysfs hooks */
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700636 err = sysfs_create_group(&dev->kobj, &tmp401_group);
637 if (err)
638 return err;
639
640 /* Register additional tmp411 sysfs hooks */
641 if (data->kind == tmp411) {
642 err = sysfs_create_group(&dev->kobj, &tmp411_group);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200643 if (err)
644 goto exit_remove;
645 }
646
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700647 data->hwmon_dev = hwmon_device_register(dev);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200648 if (IS_ERR(data->hwmon_dev)) {
649 err = PTR_ERR(data->hwmon_dev);
650 data->hwmon_dev = NULL;
651 goto exit_remove;
652 }
653
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700654 dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200655
656 return 0;
657
658exit_remove:
Guenter Roeck0df9fc72012-06-02 11:35:53 -0700659 tmp401_remove(client);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200660 return err;
661}
662
Andre Prendelea63c2b2010-05-27 19:58:49 +0200663static 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};
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200674
Axel Linf0967ee2012-01-20 15:38:18 +0800675module_i2c_driver(tmp401_driver);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200676
677MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
678MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
679MODULE_LICENSE("GPL");