Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6-block.git] / drivers / hwmon / adm1029.c
CommitLineData
cae2caae
CL
1/*
2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 *
962a75a2 4 * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
cae2caae 5 *
7c81c60f 6 * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
cae2caae
CL
7 *
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
25e9c86d
MH
42static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END
cae2caae
CL
44};
45
cae2caae
CL
46/*
47 * 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
08f50907 81#define DIV_FROM_REG(val) (1 << (((val) >> 6) - 1))
cae2caae
CL
82
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
cae2caae
CL
108/*
109 * Client data (each client gets its own)
110 */
111
112struct adm1029_data {
4fd5233f 113 struct i2c_client *client;
cae2caae
CL
114 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
b12e484e
AL
124/*
125 * function that update the status of the chips (temperature for example)
126 */
127static struct adm1029_data *adm1029_update_device(struct device *dev)
128{
4fd5233f
AL
129 struct adm1029_data *data = dev_get_drvdata(dev);
130 struct i2c_client *client = data->client;
b12e484e
AL
131
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
cae2caae
CL
168/*
169 * 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;
366716e6
CL
186 if (data->fan[attr->index] == 0
187 || (data->fan_div[attr->index] & 0xC0) == 0
cae2caae
CL
188 || 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);
366716e6 202 if ((data->fan_div[attr->index] & 0xC0) == 0)
cae2caae
CL
203 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{
4fd5233f
AL
210 struct adm1029_data *data = dev_get_drvdata(dev);
211 struct i2c_client *client = data->client;
cae2caae 212 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
cae2caae 213 u8 reg;
08f50907
FM
214 long val;
215 int ret = kstrtol(buf, 10, &val);
216 if (ret < 0)
217 return ret;
cae2caae
CL
218
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);
b55f3757
GR
237 dev_err(&client->dev,
238 "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
239 val);
cae2caae
CL
240 return -EINVAL;
241 }
242 /* Update the value */
243 reg = (reg & 0x3F) | (val << 6);
244
1035a9e3
AL
245 /* Update the cache */
246 data->fan_div[attr->index] = reg;
247
cae2caae
CL
248 /* 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/*
94b991d4
GR
257 * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
258 * S_IWUSR: Is Writable by User.
259 */
cae2caae
CL
260static 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
4fd5233f 283static struct attribute *adm1029_attrs[] = {
cae2caae
CL
284 &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
4fd5233f 302ATTRIBUTE_GROUPS(adm1029);
cae2caae
CL
303
304/*
305 * Real code
306 */
307
9c97fb4d 308/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 309static int adm1029_detect(struct i2c_client *client,
9c97fb4d 310 struct i2c_board_info *info)
cae2caae 311{
9c97fb4d 312 struct i2c_adapter *adapter = client->adapter;
52df6440 313 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
cae2caae 314
cae2caae 315 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
9c97fb4d 316 return -ENODEV;
cae2caae 317
94b991d4
GR
318 /*
319 * ADM1029 doesn't have CHIP ID, check just MAN ID
cae2caae
CL
320 * 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
52df6440
JD
325 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,
cae2caae 328 ADM1029_REG_TEMP_DEVICES_INSTALLED);
52df6440 329 nb_fan_support = i2c_smbus_read_byte_data(client,
cae2caae 330 ADM1029_REG_NB_FAN_SUPPORT);
52df6440
JD
331 /* 0x41 is Analog Devices */
332 if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
333 || nb_fan_support != 0x03)
334 return -ENODEV;
335
336 if ((chip_id & 0xF0) != 0x00) {
94b991d4
GR
337 /*
338 * There are no "official" CHIP ID, so actually
339 * we use Major/Minor revision for that
340 */
b55f3757
GR
341 pr_info("Unknown major revision %x, please let us know\n",
342 chip_id);
52df6440 343 return -ENODEV;
cae2caae 344 }
52df6440 345
9c97fb4d 346 strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
cae2caae 347
9c97fb4d
JD
348 return 0;
349}
350
b12e484e
AL
351static 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
9c97fb4d
JD
369static int adm1029_probe(struct i2c_client *client,
370 const struct i2c_device_id *id)
371{
4fd5233f 372 struct device *dev = &client->dev;
9c97fb4d 373 struct adm1029_data *data;
4fd5233f 374 struct device *hwmon_dev;
9c97fb4d 375
4fd5233f 376 data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
08940b8a
GR
377 if (!data)
378 return -ENOMEM;
cae2caae 379
4fd5233f 380 data->client = client;
cae2caae
CL
381 mutex_init(&data->update_lock);
382
cae2caae
CL
383 /*
384 * Initialize the ADM1029 chip
385 * Check config register
386 */
08940b8a
GR
387 if (adm1029_init_client(client) == 0)
388 return -ENODEV;
cae2caae 389
4fd5233f
AL
390 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
391 data,
392 adm1029_groups);
393 return PTR_ERR_OR_ZERO(hwmon_dev);
cae2caae
CL
394}
395
b12e484e
AL
396static const struct i2c_device_id adm1029_id[] = {
397 { "adm1029", 0 },
398 { }
399};
400MODULE_DEVICE_TABLE(i2c, adm1029_id);
cae2caae 401
b12e484e
AL
402static struct i2c_driver adm1029_driver = {
403 .class = I2C_CLASS_HWMON,
404 .driver = {
405 .name = "adm1029",
406 },
407 .probe = adm1029_probe,
b12e484e
AL
408 .id_table = adm1029_id,
409 .detect = adm1029_detect,
410 .address_list = normal_i2c,
411};
cae2caae 412
f0967eea 413module_i2c_driver(adm1029_driver);
cae2caae 414
962a75a2 415MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
cae2caae
CL
416MODULE_DESCRIPTION("adm1029 driver");
417MODULE_LICENSE("GPL v2");