x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / hwmon / max1668.c
blob7ca889910262b61cb5a2ee08cab20bed54537dda
1 /*
2 * Copyright (c) 2011 David George <david.george@ska.ac.za>
4 * based on adm1021.c
5 * some credit to Christoph Scheurer, but largely a rewrite
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
32 /* Addresses to scan */
33 static const unsigned short max1668_addr_list[] = {
34 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
36 /* max1668 registers */
38 #define MAX1668_REG_TEMP(nr) (nr)
39 #define MAX1668_REG_STAT1 0x05
40 #define MAX1668_REG_STAT2 0x06
41 #define MAX1668_REG_MAN_ID 0xfe
42 #define MAX1668_REG_DEV_ID 0xff
44 /* limits */
46 /* write high limits */
47 #define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr))
48 /* write low limits */
49 #define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr))
50 /* read high limits */
51 #define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr))
52 /* read low limits */
53 #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr))
55 /* manufacturer and device ID Constants */
56 #define MAN_ID_MAXIM 0x4d
57 #define DEV_ID_MAX1668 0x3
58 #define DEV_ID_MAX1805 0x5
59 #define DEV_ID_MAX1989 0xb
61 /* read only mode module parameter */
62 static bool read_only;
63 module_param(read_only, bool, 0);
64 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
66 enum chips { max1668, max1805, max1989 };
68 struct max1668_data {
69 struct i2c_client *client;
70 const struct attribute_group *groups[3];
71 enum chips type;
73 struct mutex update_lock;
74 char valid; /* !=0 if following fields are valid */
75 unsigned long last_updated; /* In jiffies */
77 /* 1x local and 4x remote */
78 s8 temp_max[5];
79 s8 temp_min[5];
80 s8 temp[5];
81 u16 alarms;
84 static struct max1668_data *max1668_update_device(struct device *dev)
86 struct max1668_data *data = dev_get_drvdata(dev);
87 struct i2c_client *client = data->client;
88 struct max1668_data *ret = data;
89 s32 val;
90 int i;
92 mutex_lock(&data->update_lock);
94 if (data->valid && !time_after(jiffies,
95 data->last_updated + HZ + HZ / 2))
96 goto abort;
98 for (i = 0; i < 5; i++) {
99 val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i));
100 if (unlikely(val < 0)) {
101 ret = ERR_PTR(val);
102 goto abort;
104 data->temp[i] = (s8) val;
106 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i));
107 if (unlikely(val < 0)) {
108 ret = ERR_PTR(val);
109 goto abort;
111 data->temp_max[i] = (s8) val;
113 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i));
114 if (unlikely(val < 0)) {
115 ret = ERR_PTR(val);
116 goto abort;
118 data->temp_min[i] = (s8) val;
121 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1);
122 if (unlikely(val < 0)) {
123 ret = ERR_PTR(val);
124 goto abort;
126 data->alarms = val << 8;
128 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2);
129 if (unlikely(val < 0)) {
130 ret = ERR_PTR(val);
131 goto abort;
133 data->alarms |= val;
135 data->last_updated = jiffies;
136 data->valid = 1;
137 abort:
138 mutex_unlock(&data->update_lock);
140 return ret;
143 static ssize_t show_temp(struct device *dev,
144 struct device_attribute *devattr, char *buf)
146 int index = to_sensor_dev_attr(devattr)->index;
147 struct max1668_data *data = max1668_update_device(dev);
149 if (IS_ERR(data))
150 return PTR_ERR(data);
152 return sprintf(buf, "%d\n", data->temp[index] * 1000);
155 static ssize_t show_temp_max(struct device *dev,
156 struct device_attribute *devattr, char *buf)
158 int index = to_sensor_dev_attr(devattr)->index;
159 struct max1668_data *data = max1668_update_device(dev);
161 if (IS_ERR(data))
162 return PTR_ERR(data);
164 return sprintf(buf, "%d\n", data->temp_max[index] * 1000);
167 static ssize_t show_temp_min(struct device *dev,
168 struct device_attribute *devattr, char *buf)
170 int index = to_sensor_dev_attr(devattr)->index;
171 struct max1668_data *data = max1668_update_device(dev);
173 if (IS_ERR(data))
174 return PTR_ERR(data);
176 return sprintf(buf, "%d\n", data->temp_min[index] * 1000);
179 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
180 char *buf)
182 int index = to_sensor_dev_attr(attr)->index;
183 struct max1668_data *data = max1668_update_device(dev);
185 if (IS_ERR(data))
186 return PTR_ERR(data);
188 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
191 static ssize_t show_fault(struct device *dev,
192 struct device_attribute *devattr, char *buf)
194 int index = to_sensor_dev_attr(devattr)->index;
195 struct max1668_data *data = max1668_update_device(dev);
197 if (IS_ERR(data))
198 return PTR_ERR(data);
200 return sprintf(buf, "%u\n",
201 (data->alarms & (1 << 12)) && data->temp[index] == 127);
204 static ssize_t set_temp_max(struct device *dev,
205 struct device_attribute *devattr,
206 const char *buf, size_t count)
208 int index = to_sensor_dev_attr(devattr)->index;
209 struct max1668_data *data = dev_get_drvdata(dev);
210 struct i2c_client *client = data->client;
211 long temp;
212 int ret;
214 ret = kstrtol(buf, 10, &temp);
215 if (ret < 0)
216 return ret;
218 mutex_lock(&data->update_lock);
219 data->temp_max[index] = clamp_val(temp/1000, -128, 127);
220 ret = i2c_smbus_write_byte_data(client,
221 MAX1668_REG_LIMH_WR(index),
222 data->temp_max[index]);
223 if (ret < 0)
224 count = ret;
225 mutex_unlock(&data->update_lock);
227 return count;
230 static ssize_t set_temp_min(struct device *dev,
231 struct device_attribute *devattr,
232 const char *buf, size_t count)
234 int index = to_sensor_dev_attr(devattr)->index;
235 struct max1668_data *data = dev_get_drvdata(dev);
236 struct i2c_client *client = data->client;
237 long temp;
238 int ret;
240 ret = kstrtol(buf, 10, &temp);
241 if (ret < 0)
242 return ret;
244 mutex_lock(&data->update_lock);
245 data->temp_min[index] = clamp_val(temp/1000, -128, 127);
246 ret = i2c_smbus_write_byte_data(client,
247 MAX1668_REG_LIML_WR(index),
248 data->temp_min[index]);
249 if (ret < 0)
250 count = ret;
251 mutex_unlock(&data->update_lock);
253 return count;
256 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
257 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
258 set_temp_max, 0);
259 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
260 set_temp_min, 0);
261 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
262 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
263 set_temp_max, 1);
264 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
265 set_temp_min, 1);
266 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
267 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
268 set_temp_max, 2);
269 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
270 set_temp_min, 2);
271 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
272 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
273 set_temp_max, 3);
274 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
275 set_temp_min, 3);
276 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
277 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
278 set_temp_max, 4);
279 static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
280 set_temp_min, 4);
282 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
283 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
284 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
285 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
286 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
287 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
288 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
289 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
290 static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
291 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
293 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
294 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
295 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
296 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
298 /* Attributes common to MAX1668, MAX1989 and MAX1805 */
299 static struct attribute *max1668_attribute_common[] = {
300 &sensor_dev_attr_temp1_max.dev_attr.attr,
301 &sensor_dev_attr_temp1_min.dev_attr.attr,
302 &sensor_dev_attr_temp1_input.dev_attr.attr,
303 &sensor_dev_attr_temp2_max.dev_attr.attr,
304 &sensor_dev_attr_temp2_min.dev_attr.attr,
305 &sensor_dev_attr_temp2_input.dev_attr.attr,
306 &sensor_dev_attr_temp3_max.dev_attr.attr,
307 &sensor_dev_attr_temp3_min.dev_attr.attr,
308 &sensor_dev_attr_temp3_input.dev_attr.attr,
310 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
311 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
312 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
313 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
314 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
315 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
317 &sensor_dev_attr_temp2_fault.dev_attr.attr,
318 &sensor_dev_attr_temp3_fault.dev_attr.attr,
319 NULL
322 /* Attributes not present on MAX1805 */
323 static struct attribute *max1668_attribute_unique[] = {
324 &sensor_dev_attr_temp4_max.dev_attr.attr,
325 &sensor_dev_attr_temp4_min.dev_attr.attr,
326 &sensor_dev_attr_temp4_input.dev_attr.attr,
327 &sensor_dev_attr_temp5_max.dev_attr.attr,
328 &sensor_dev_attr_temp5_min.dev_attr.attr,
329 &sensor_dev_attr_temp5_input.dev_attr.attr,
331 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
332 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
333 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
334 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
336 &sensor_dev_attr_temp4_fault.dev_attr.attr,
337 &sensor_dev_attr_temp5_fault.dev_attr.attr,
338 NULL
341 static umode_t max1668_attribute_mode(struct kobject *kobj,
342 struct attribute *attr, int index)
344 umode_t ret = S_IRUGO;
345 if (read_only)
346 return ret;
347 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
348 attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
349 attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
350 attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
351 attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
352 attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
353 attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
354 attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
355 attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
356 attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
357 ret |= S_IWUSR;
358 return ret;
361 static const struct attribute_group max1668_group_common = {
362 .attrs = max1668_attribute_common,
363 .is_visible = max1668_attribute_mode
366 static const struct attribute_group max1668_group_unique = {
367 .attrs = max1668_attribute_unique,
368 .is_visible = max1668_attribute_mode
371 /* Return 0 if detection is successful, -ENODEV otherwise */
372 static int max1668_detect(struct i2c_client *client,
373 struct i2c_board_info *info)
375 struct i2c_adapter *adapter = client->adapter;
376 const char *type_name;
377 int man_id, dev_id;
379 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
380 return -ENODEV;
382 /* Check for unsupported part */
383 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
384 if (man_id != MAN_ID_MAXIM)
385 return -ENODEV;
387 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
388 if (dev_id < 0)
389 return -ENODEV;
391 type_name = NULL;
392 if (dev_id == DEV_ID_MAX1668)
393 type_name = "max1668";
394 else if (dev_id == DEV_ID_MAX1805)
395 type_name = "max1805";
396 else if (dev_id == DEV_ID_MAX1989)
397 type_name = "max1989";
399 if (!type_name)
400 return -ENODEV;
402 strlcpy(info->type, type_name, I2C_NAME_SIZE);
404 return 0;
407 static int max1668_probe(struct i2c_client *client,
408 const struct i2c_device_id *id)
410 struct i2c_adapter *adapter = client->adapter;
411 struct device *dev = &client->dev;
412 struct device *hwmon_dev;
413 struct max1668_data *data;
415 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
416 return -ENODEV;
418 data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL);
419 if (!data)
420 return -ENOMEM;
422 data->client = client;
423 data->type = id->driver_data;
424 mutex_init(&data->update_lock);
426 /* sysfs hooks */
427 data->groups[0] = &max1668_group_common;
428 if (data->type == max1668 || data->type == max1989)
429 data->groups[1] = &max1668_group_unique;
431 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
432 data, data->groups);
433 return PTR_ERR_OR_ZERO(hwmon_dev);
436 static const struct i2c_device_id max1668_id[] = {
437 { "max1668", max1668 },
438 { "max1805", max1805 },
439 { "max1989", max1989 },
442 MODULE_DEVICE_TABLE(i2c, max1668_id);
444 /* This is the driver that will be inserted */
445 static struct i2c_driver max1668_driver = {
446 .class = I2C_CLASS_HWMON,
447 .driver = {
448 .name = "max1668",
450 .probe = max1668_probe,
451 .id_table = max1668_id,
452 .detect = max1668_detect,
453 .address_list = max1668_addr_list,
456 module_i2c_driver(max1668_driver);
458 MODULE_AUTHOR("David George <david.george@ska.ac.za>");
459 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
460 MODULE_LICENSE("GPL");