blob: 8c5cdb56025862b7e6e8381d49321005e17db141 [file] [log] [blame]
Corentin Labbecae2caa2007-02-14 21:15:04 +01001/*
2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 *
LABBE Corentin962a75a2014-05-09 14:04:01 +02004 * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
Corentin Labbecae2caa2007-02-14 21:15:04 +01005 *
Jean Delvare7c81c602014-01-29 20:40:08 +01006 * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
Corentin Labbecae2caa2007-02-14 21:15:04 +01007 *
8 * Give only processor, motherboard temperatures and fan tachs
9 * Very rare chip please let me know if you use it
10 *
11 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
12 *
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation version 2 of the License
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/hwmon.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37
38/*
39 * Addresses to scan
40 */
41
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050042static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END
Corentin Labbecae2caa2007-02-14 21:15:04 +010044};
45
46/*
Corentin Labbecae2caa2007-02-14 21:15:04 +010047 * The ADM1029 registers
48 * Manufacturer ID is 0x41 for Analog Devices
49 */
50
51#define ADM1029_REG_MAN_ID 0x0D
52#define ADM1029_REG_CHIP_ID 0x0E
53#define ADM1029_REG_CONFIG 0x01
54#define ADM1029_REG_NB_FAN_SUPPORT 0x02
55
56#define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06
57
58#define ADM1029_REG_LOCAL_TEMP 0xA0
59#define ADM1029_REG_REMOTE1_TEMP 0xA1
60#define ADM1029_REG_REMOTE2_TEMP 0xA2
61
62#define ADM1029_REG_LOCAL_TEMP_HIGH 0x90
63#define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91
64#define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92
65
66#define ADM1029_REG_LOCAL_TEMP_LOW 0x98
67#define ADM1029_REG_REMOTE1_TEMP_LOW 0x99
68#define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A
69
70#define ADM1029_REG_FAN1 0x70
71#define ADM1029_REG_FAN2 0x71
72
73#define ADM1029_REG_FAN1_MIN 0x78
74#define ADM1029_REG_FAN2_MIN 0x79
75
76#define ADM1029_REG_FAN1_CONFIG 0x68
77#define ADM1029_REG_FAN2_CONFIG 0x69
78
79#define TEMP_FROM_REG(val) ((val) * 1000)
80
Frans Meulenbroeks08f50902012-01-08 19:34:18 +010081#define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
Corentin Labbecae2caa2007-02-14 21:15:04 +010082
83/* Registers to be checked by adm1029_update_device() */
84static const u8 ADM1029_REG_TEMP[] = {
85 ADM1029_REG_LOCAL_TEMP,
86 ADM1029_REG_REMOTE1_TEMP,
87 ADM1029_REG_REMOTE2_TEMP,
88 ADM1029_REG_LOCAL_TEMP_HIGH,
89 ADM1029_REG_REMOTE1_TEMP_HIGH,
90 ADM1029_REG_REMOTE2_TEMP_HIGH,
91 ADM1029_REG_LOCAL_TEMP_LOW,
92 ADM1029_REG_REMOTE1_TEMP_LOW,
93 ADM1029_REG_REMOTE2_TEMP_LOW,
94};
95
96static const u8 ADM1029_REG_FAN[] = {
97 ADM1029_REG_FAN1,
98 ADM1029_REG_FAN2,
99 ADM1029_REG_FAN1_MIN,
100 ADM1029_REG_FAN2_MIN,
101};
102
103static const u8 ADM1029_REG_FAN_DIV[] = {
104 ADM1029_REG_FAN1_CONFIG,
105 ADM1029_REG_FAN2_CONFIG,
106};
107
108/*
Corentin Labbecae2caa2007-02-14 21:15:04 +0100109 * Client data (each client gets its own)
110 */
111
112struct adm1029_data {
Axel Lin4fd52332014-06-29 09:42:17 +0800113 struct i2c_client *client;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100114 struct mutex update_lock;
115 char valid; /* zero until following fields are valid */
116 unsigned long last_updated; /* in jiffies */
117
118 /* registers values, signed for temperature, unsigned for other stuff */
119 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
120 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
121 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
122};
123
124/*
Axel Linb12e4842014-06-29 09:41:08 +0800125 * function that update the status of the chips (temperature for example)
126 */
127static struct adm1029_data *adm1029_update_device(struct device *dev)
128{
Axel Lin4fd52332014-06-29 09:42:17 +0800129 struct adm1029_data *data = dev_get_drvdata(dev);
130 struct i2c_client *client = data->client;
Axel Linb12e4842014-06-29 09:41:08 +0800131
132 mutex_lock(&data->update_lock);
133 /*
134 * Use the "cache" Luke, don't recheck values
135 * if there are already checked not a long time later
136 */
137 if (time_after(jiffies, data->last_updated + HZ * 2)
138 || !data->valid) {
139 int nr;
140
141 dev_dbg(&client->dev, "Updating adm1029 data\n");
142
143 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
144 data->temp[nr] =
145 i2c_smbus_read_byte_data(client,
146 ADM1029_REG_TEMP[nr]);
147 }
148 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
149 data->fan[nr] =
150 i2c_smbus_read_byte_data(client,
151 ADM1029_REG_FAN[nr]);
152 }
153 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
154 data->fan_div[nr] =
155 i2c_smbus_read_byte_data(client,
156 ADM1029_REG_FAN_DIV[nr]);
157 }
158
159 data->last_updated = jiffies;
160 data->valid = 1;
161 }
162
163 mutex_unlock(&data->update_lock);
164
165 return data;
166}
167
168/*
Corentin Labbecae2caa2007-02-14 21:15:04 +0100169 * Sysfs stuff
170 */
171
172static ssize_t
173show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
174{
175 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
176 struct adm1029_data *data = adm1029_update_device(dev);
177 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
178}
179
180static ssize_t
181show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
182{
183 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184 struct adm1029_data *data = adm1029_update_device(dev);
185 u16 val;
Corentin Labbe366716e2008-10-17 17:51:20 +0200186 if (data->fan[attr->index] == 0
187 || (data->fan_div[attr->index] & 0xC0) == 0
Corentin Labbecae2caa2007-02-14 21:15:04 +0100188 || data->fan[attr->index] == 255) {
189 return sprintf(buf, "0\n");
190 }
191
192 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
193 / data->fan[attr->index];
194 return sprintf(buf, "%d\n", val);
195}
196
197static ssize_t
198show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
199{
200 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
201 struct adm1029_data *data = adm1029_update_device(dev);
Corentin Labbe366716e2008-10-17 17:51:20 +0200202 if ((data->fan_div[attr->index] & 0xC0) == 0)
Corentin Labbecae2caa2007-02-14 21:15:04 +0100203 return sprintf(buf, "0\n");
204 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
205}
206
207static ssize_t set_fan_div(struct device *dev,
208 struct device_attribute *devattr, const char *buf, size_t count)
209{
Axel Lin4fd52332014-06-29 09:42:17 +0800210 struct adm1029_data *data = dev_get_drvdata(dev);
211 struct i2c_client *client = data->client;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100212 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100213 u8 reg;
Frans Meulenbroeks08f50902012-01-08 19:34:18 +0100214 long val;
215 int ret = kstrtol(buf, 10, &val);
216 if (ret < 0)
217 return ret;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100218
219 mutex_lock(&data->update_lock);
220
221 /*Read actual config */
222 reg = i2c_smbus_read_byte_data(client,
223 ADM1029_REG_FAN_DIV[attr->index]);
224
225 switch (val) {
226 case 1:
227 val = 1;
228 break;
229 case 2:
230 val = 2;
231 break;
232 case 4:
233 val = 3;
234 break;
235 default:
236 mutex_unlock(&data->update_lock);
Guenter Roeckb55f3752013-01-10 10:01:24 -0800237 dev_err(&client->dev,
238 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
239 val);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100240 return -EINVAL;
241 }
242 /* Update the value */
243 reg = (reg & 0x3F) | (val << 6);
244
Axel Lin1035a9e2014-07-02 08:29:55 +0800245 /* Update the cache */
246 data->fan_div[attr->index] = reg;
247
Corentin Labbecae2caa2007-02-14 21:15:04 +0100248 /* Write value */
249 i2c_smbus_write_byte_data(client,
250 ADM1029_REG_FAN_DIV[attr->index], reg);
251 mutex_unlock(&data->update_lock);
252
253 return count;
254}
255
256/*
Guenter Roeck94b991d2012-01-19 11:02:14 -0800257 * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
258 * S_IWUSR: Is Writable by User.
259 */
Corentin Labbecae2caa2007-02-14 21:15:04 +0100260static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
261static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
262static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
263
264static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
265static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
266static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
267
268static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
269static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
270static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
271
272static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
273static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
274
275static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
276static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
277
278static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
279 show_fan_div, set_fan_div, 0);
280static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
281 show_fan_div, set_fan_div, 1);
282
Axel Lin4fd52332014-06-29 09:42:17 +0800283static struct attribute *adm1029_attrs[] = {
Corentin Labbecae2caa2007-02-14 21:15:04 +0100284 &sensor_dev_attr_temp1_input.dev_attr.attr,
285 &sensor_dev_attr_temp1_min.dev_attr.attr,
286 &sensor_dev_attr_temp1_max.dev_attr.attr,
287 &sensor_dev_attr_temp2_input.dev_attr.attr,
288 &sensor_dev_attr_temp2_min.dev_attr.attr,
289 &sensor_dev_attr_temp2_max.dev_attr.attr,
290 &sensor_dev_attr_temp3_input.dev_attr.attr,
291 &sensor_dev_attr_temp3_min.dev_attr.attr,
292 &sensor_dev_attr_temp3_max.dev_attr.attr,
293 &sensor_dev_attr_fan1_input.dev_attr.attr,
294 &sensor_dev_attr_fan2_input.dev_attr.attr,
295 &sensor_dev_attr_fan1_min.dev_attr.attr,
296 &sensor_dev_attr_fan2_min.dev_attr.attr,
297 &sensor_dev_attr_fan1_div.dev_attr.attr,
298 &sensor_dev_attr_fan2_div.dev_attr.attr,
299 NULL
300};
301
Axel Lin4fd52332014-06-29 09:42:17 +0800302ATTRIBUTE_GROUPS(adm1029);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100303
304/*
305 * Real code
306 */
307
Jean Delvare9c97fb42008-07-16 19:30:09 +0200308/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100309static int adm1029_detect(struct i2c_client *client,
Jean Delvare9c97fb42008-07-16 19:30:09 +0200310 struct i2c_board_info *info)
Corentin Labbecae2caa2007-02-14 21:15:04 +0100311{
Jean Delvare9c97fb42008-07-16 19:30:09 +0200312 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100313 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100314
Corentin Labbecae2caa2007-02-14 21:15:04 +0100315 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9c97fb42008-07-16 19:30:09 +0200316 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100317
Guenter Roeck94b991d2012-01-19 11:02:14 -0800318 /*
319 * ADM1029 doesn't have CHIP ID, check just MAN ID
Corentin Labbecae2caa2007-02-14 21:15:04 +0100320 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
321 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
322 * documented
323 */
324
Jean Delvare52df6442009-12-09 20:35:57 +0100325 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
326 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
327 temp_devices_installed = i2c_smbus_read_byte_data(client,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100328 ADM1029_REG_TEMP_DEVICES_INSTALLED);
Jean Delvare52df6442009-12-09 20:35:57 +0100329 nb_fan_support = i2c_smbus_read_byte_data(client,
Corentin Labbecae2caa2007-02-14 21:15:04 +0100330 ADM1029_REG_NB_FAN_SUPPORT);
Jean Delvare52df6442009-12-09 20:35:57 +0100331 /* 0x41 is Analog Devices */
332 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
333 || nb_fan_support != 0x03)
334 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100335
Jean Delvare52df6442009-12-09 20:35:57 +0100336 if ((chip_id & 0xF0) != 0x00) {
Guenter Roeck94b991d2012-01-19 11:02:14 -0800337 /*
338 * There are no "official" CHIP ID, so actually
339 * we use Major/Minor revision for that
340 */
Guenter Roeckb55f3752013-01-10 10:01:24 -0800341 pr_info("Unknown major revision %x, please let us know\n",
342 chip_id);
Jean Delvare52df6442009-12-09 20:35:57 +0100343 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100344 }
Jean Delvare52df6442009-12-09 20:35:57 +0100345
Jean Delvare9c97fb42008-07-16 19:30:09 +0200346 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100347
Jean Delvare9c97fb42008-07-16 19:30:09 +0200348 return 0;
349}
350
Axel Linb12e4842014-06-29 09:41:08 +0800351static int adm1029_init_client(struct i2c_client *client)
352{
353 u8 config;
354
355 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
356 if ((config & 0x10) == 0) {
357 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
358 config | 0x10);
359 }
360 /* recheck config */
361 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
362 if ((config & 0x10) == 0) {
363 dev_err(&client->dev, "Initialization failed!\n");
364 return 0;
365 }
366 return 1;
367}
368
Jean Delvare9c97fb42008-07-16 19:30:09 +0200369static int adm1029_probe(struct i2c_client *client,
370 const struct i2c_device_id *id)
371{
Axel Lin4fd52332014-06-29 09:42:17 +0800372 struct device *dev = &client->dev;
Jean Delvare9c97fb42008-07-16 19:30:09 +0200373 struct adm1029_data *data;
Axel Lin4fd52332014-06-29 09:42:17 +0800374 struct device *hwmon_dev;
Jean Delvare9c97fb42008-07-16 19:30:09 +0200375
Axel Lin4fd52332014-06-29 09:42:17 +0800376 data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
Guenter Roeck08940b82012-06-02 09:57:59 -0700377 if (!data)
378 return -ENOMEM;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100379
Axel Lin4fd52332014-06-29 09:42:17 +0800380 data->client = client;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100381 mutex_init(&data->update_lock);
382
Corentin Labbecae2caa2007-02-14 21:15:04 +0100383 /*
384 * Initialize the ADM1029 chip
385 * Check config register
386 */
Guenter Roeck08940b82012-06-02 09:57:59 -0700387 if (adm1029_init_client(client) == 0)
388 return -ENODEV;
Corentin Labbecae2caa2007-02-14 21:15:04 +0100389
Axel Lin4fd52332014-06-29 09:42:17 +0800390 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
391 data,
392 adm1029_groups);
393 return PTR_ERR_OR_ZERO(hwmon_dev);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100394}
395
Axel Linb12e4842014-06-29 09:41:08 +0800396static const struct i2c_device_id adm1029_id[] = {
397 { "adm1029", 0 },
398 { }
399};
400MODULE_DEVICE_TABLE(i2c, adm1029_id);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100401
Axel Linb12e4842014-06-29 09:41:08 +0800402static struct i2c_driver adm1029_driver = {
403 .class = I2C_CLASS_HWMON,
404 .driver = {
405 .name = "adm1029",
406 },
407 .probe = adm1029_probe,
Axel Linb12e4842014-06-29 09:41:08 +0800408 .id_table = adm1029_id,
409 .detect = adm1029_detect,
410 .address_list = normal_i2c,
411};
Corentin Labbecae2caa2007-02-14 21:15:04 +0100412
Axel Linf0967ee2012-01-20 15:38:18 +0800413module_i2c_driver(adm1029_driver);
Corentin Labbecae2caa2007-02-14 21:15:04 +0100414
LABBE Corentin962a75a2014-05-09 14:04:01 +0200415MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
Corentin Labbecae2caa2007-02-14 21:15:04 +0100416MODULE_DESCRIPTION("adm1029 driver");
417MODULE_LICENSE("GPL v2");