]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hwmon/adm1031.c
Merge tag 'nfs-for-4.13-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[mirror_ubuntu-artful-kernel.git] / drivers / hwmon / adm1031.c
CommitLineData
1da177e4 1/*
fbb6670d
GR
2 * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Based on lm75.c and lm85.c
5 * Supports adm1030 / adm1031
6 * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7c81c60f 7 * Reworked by Jean Delvare <jdelvare@suse.de>
fbb6670d
GR
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
1da177e4
LT
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
943b0830 29#include <linux/hwmon.h>
c801082d 30#include <linux/hwmon-sysfs.h>
943b0830 31#include <linux/err.h>
9a61bf63 32#include <linux/mutex.h>
1da177e4
LT
33
34/* Following macros takes channel parameter starting from 0 to 2 */
35#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
6d6006b8 36#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
1da177e4
LT
37#define ADM1031_REG_PWM (0x22)
38#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
87c33daa 39#define ADM1031_REG_FAN_FILTER (0x23)
1da177e4 40
49dc9efe 41#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
6d6006b8
JD
42#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
1da177e4 45
6d6006b8 46#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
1da177e4
LT
47#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48
49#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50
6d6006b8
JD
51#define ADM1031_REG_CONF1 0x00
52#define ADM1031_REG_CONF2 0x01
53#define ADM1031_REG_EXT_TEMP 0x06
1da177e4
LT
54
55#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
56#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
57#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
58
59#define ADM1031_CONF2_PWM1_ENABLE 0x01
60#define ADM1031_CONF2_PWM2_ENABLE 0x02
61#define ADM1031_CONF2_TACH1_ENABLE 0x04
62#define ADM1031_CONF2_TACH2_ENABLE 0x08
63#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64
87c33daa
JD
65#define ADM1031_UPDATE_RATE_MASK 0x1c
66#define ADM1031_UPDATE_RATE_SHIFT 2
67
1da177e4 68/* Addresses to scan */
25e9c86d 69static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1da177e4 70
e5e9f44c 71enum chips { adm1030, adm1031 };
1da177e4
LT
72
73typedef u8 auto_chan_table_t[8][2];
74
75/* Each client has this additional data */
76struct adm1031_data {
b060f3c4
AL
77 struct i2c_client *client;
78 const struct attribute_group *groups[3];
9a61bf63 79 struct mutex update_lock;
1da177e4
LT
80 int chip_type;
81 char valid; /* !=0 if following fields are valid */
82 unsigned long last_updated; /* In jiffies */
a51b9944 83 unsigned int update_interval; /* In milliseconds */
fbb6670d
GR
84 /*
85 * The chan_select_table contains the possible configurations for
1da177e4
LT
86 * auto fan control.
87 */
6d6006b8 88 const auto_chan_table_t *chan_select_table;
1da177e4
LT
89 u16 alarm;
90 u8 conf1;
91 u8 conf2;
92 u8 fan[2];
93 u8 fan_div[2];
94 u8 fan_min[2];
95 u8 pwm[2];
96 u8 old_pwm[2];
97 s8 temp[3];
98 u8 ext_temp[3];
99 u8 auto_temp[3];
100 u8 auto_temp_min[3];
101 u8 auto_temp_off[3];
102 u8 auto_temp_max[3];
49dc9efe 103 s8 temp_offset[3];
1da177e4
LT
104 s8 temp_min[3];
105 s8 temp_max[3];
106 s8 temp_crit[3];
107};
108
1da177e4
LT
109static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
110{
111 return i2c_smbus_read_byte_data(client, reg);
112}
113
114static inline int
115adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
116{
117 return i2c_smbus_write_byte_data(client, reg, value);
118}
119
278ee1c8
AL
120static struct adm1031_data *adm1031_update_device(struct device *dev)
121{
b060f3c4
AL
122 struct adm1031_data *data = dev_get_drvdata(dev);
123 struct i2c_client *client = data->client;
278ee1c8
AL
124 unsigned long next_update;
125 int chan;
126
127 mutex_lock(&data->update_lock);
128
129 next_update = data->last_updated
130 + msecs_to_jiffies(data->update_interval);
131 if (time_after(jiffies, next_update) || !data->valid) {
132
133 dev_dbg(&client->dev, "Starting adm1031 update\n");
134 for (chan = 0;
135 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
136 u8 oldh, newh;
137
138 oldh =
139 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
140 data->ext_temp[chan] =
141 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
142 newh =
143 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
144 if (newh != oldh) {
145 data->ext_temp[chan] =
146 adm1031_read_value(client,
147 ADM1031_REG_EXT_TEMP);
148#ifdef DEBUG
149 oldh =
150 adm1031_read_value(client,
151 ADM1031_REG_TEMP(chan));
152
153 /* oldh is actually newer */
154 if (newh != oldh)
155 dev_warn(&client->dev,
156 "Remote temperature may be wrong.\n");
157#endif
158 }
159 data->temp[chan] = newh;
160
161 data->temp_offset[chan] =
162 adm1031_read_value(client,
163 ADM1031_REG_TEMP_OFFSET(chan));
164 data->temp_min[chan] =
165 adm1031_read_value(client,
166 ADM1031_REG_TEMP_MIN(chan));
167 data->temp_max[chan] =
168 adm1031_read_value(client,
169 ADM1031_REG_TEMP_MAX(chan));
170 data->temp_crit[chan] =
171 adm1031_read_value(client,
172 ADM1031_REG_TEMP_CRIT(chan));
173 data->auto_temp[chan] =
174 adm1031_read_value(client,
175 ADM1031_REG_AUTO_TEMP(chan));
176
177 }
178
179 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
180 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
181
182 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
183 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
184 if (data->chip_type == adm1030)
185 data->alarm &= 0xc0ff;
186
187 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
188 chan++) {
189 data->fan_div[chan] =
190 adm1031_read_value(client,
191 ADM1031_REG_FAN_DIV(chan));
192 data->fan_min[chan] =
193 adm1031_read_value(client,
194 ADM1031_REG_FAN_MIN(chan));
195 data->fan[chan] =
196 adm1031_read_value(client,
197 ADM1031_REG_FAN_SPEED(chan));
198 data->pwm[chan] =
199 (adm1031_read_value(client,
200 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
201 }
202 data->last_updated = jiffies;
203 data->valid = 1;
204 }
205
206 mutex_unlock(&data->update_lock);
207
208 return data;
209}
1da177e4
LT
210
211#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
212 ((val + 500) / 1000)))
213
214#define TEMP_FROM_REG(val) ((val) * 1000)
215
216#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
217
49dc9efe
IS
218#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
219#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
220 (val) | 0x70 : (val))
221
1c720093
GR
222#define FAN_FROM_REG(reg, div) ((reg) ? \
223 (11250 * 60) / ((reg) * (div)) : 0)
1da177e4
LT
224
225static int FAN_TO_REG(int reg, int div)
226{
227 int tmp;
2a844c14 228 tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
1da177e4
LT
229 return tmp > 255 ? 255 : tmp;
230}
231
232#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
233
2a844c14 234#define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
1da177e4
LT
235#define PWM_FROM_REG(val) ((val) << 4)
236
237#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
238#define FAN_CHAN_TO_REG(val, reg) \
239 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
240
241#define AUTO_TEMP_MIN_TO_REG(val, reg) \
1c720093
GR
242 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
243#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
1da177e4
LT
244#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
245
246#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
247
248#define AUTO_TEMP_OFF_FROM_REG(reg) \
249 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
250
251#define AUTO_TEMP_MAX_FROM_REG(reg) \
252 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
253 AUTO_TEMP_MIN_FROM_REG(reg))
254
255static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
256{
257 int ret;
258 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
259
260 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
261 ret = ((reg & 0xf8) |
262 (range < 10000 ? 0 :
263 range < 20000 ? 1 :
264 range < 40000 ? 2 : range < 80000 ? 3 : 4));
265 return ret;
266}
267
268/* FAN auto control */
269#define GET_FAN_AUTO_BITFIELD(data, idx) \
1c720093 270 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
1da177e4 271
fbb6670d
GR
272/*
273 * The tables below contains the possible values for the auto fan
1da177e4
LT
274 * control bitfields. the index in the table is the register value.
275 * MSb is the auto fan control enable bit, so the four first entries
276 * in the table disables auto fan control when both bitfields are zero.
277 */
6d6006b8
JD
278static const auto_chan_table_t auto_channel_select_table_adm1031 = {
279 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
280 { 2 /* 0b010 */ , 4 /* 0b100 */ },
281 { 2 /* 0b010 */ , 2 /* 0b010 */ },
282 { 4 /* 0b100 */ , 4 /* 0b100 */ },
283 { 7 /* 0b111 */ , 7 /* 0b111 */ },
1da177e4
LT
284};
285
6d6006b8
JD
286static const auto_chan_table_t auto_channel_select_table_adm1030 = {
287 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
288 { 2 /* 0b10 */ , 0 },
289 { 0xff /* invalid */ , 0 },
290 { 0xff /* invalid */ , 0 },
291 { 3 /* 0b11 */ , 0 },
1da177e4
LT
292};
293
fbb6670d
GR
294/*
295 * That function checks if a bitfield is valid and returns the other bitfield
1da177e4
LT
296 * nearest match if no exact match where found.
297 */
298static int
ce15a81d 299get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
1da177e4
LT
300{
301 int i;
302 int first_match = -1, exact_match = -1;
303 u8 other_reg_val =
304 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
305
ce15a81d 306 if (val == 0)
1da177e4 307 return 0;
1da177e4
LT
308
309 for (i = 0; i < 8; i++) {
310 if ((val == (*data->chan_select_table)[i][chan]) &&
311 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
312 other_reg_val)) {
313 /* We found an exact match */
314 exact_match = i;
315 break;
316 } else if (val == (*data->chan_select_table)[i][chan] &&
317 first_match == -1) {
fbb6670d
GR
318 /*
319 * Save the first match in case of an exact match has
6d6006b8 320 * not been found
1da177e4
LT
321 */
322 first_match = i;
323 }
324 }
325
1c720093 326 if (exact_match >= 0)
ce15a81d 327 return exact_match;
1c720093 328 else if (first_match >= 0)
ce15a81d 329 return first_match;
1c720093 330
ce15a81d 331 return -EINVAL;
1da177e4
LT
332}
333
c801082d
JD
334static ssize_t show_fan_auto_channel(struct device *dev,
335 struct device_attribute *attr, char *buf)
1da177e4 336{
c801082d 337 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
338 struct adm1031_data *data = adm1031_update_device(dev);
339 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
340}
341
342static ssize_t
c801082d
JD
343set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
1da177e4 345{
b060f3c4
AL
346 struct adm1031_data *data = dev_get_drvdata(dev);
347 struct i2c_client *client = data->client;
c801082d 348 int nr = to_sensor_dev_attr(attr)->index;
1c720093 349 long val;
1da177e4
LT
350 u8 reg;
351 int ret;
352 u8 old_fan_mode;
353
1c720093
GR
354 ret = kstrtol(buf, 10, &val);
355 if (ret)
356 return ret;
357
1da177e4
LT
358 old_fan_mode = data->conf1;
359
9a61bf63 360 mutex_lock(&data->update_lock);
6d6006b8 361
ce15a81d
GR
362 ret = get_fan_auto_nearest(data, nr, val, data->conf1);
363 if (ret < 0) {
9a61bf63 364 mutex_unlock(&data->update_lock);
1da177e4
LT
365 return ret;
366 }
ce15a81d 367 reg = ret;
6d6006b8
JD
368 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
369 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
1da177e4 370 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
1c720093 371 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
fbb6670d
GR
372 /*
373 * Switch to Auto Fan Mode
6d6006b8 374 * Save PWM registers
fbb6670d
GR
375 * Set PWM registers to 33% Both
376 */
1da177e4
LT
377 data->old_pwm[0] = data->pwm[0];
378 data->old_pwm[1] = data->pwm[1];
379 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
380 } else {
381 /* Switch to Manual Mode */
382 data->pwm[0] = data->old_pwm[0];
383 data->pwm[1] = data->old_pwm[1];
384 /* Restore PWM registers */
6d6006b8 385 adm1031_write_value(client, ADM1031_REG_PWM,
1da177e4
LT
386 data->pwm[0] | (data->pwm[1] << 4));
387 }
388 }
389 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
390 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
9a61bf63 391 mutex_unlock(&data->update_lock);
1da177e4
LT
392 return count;
393}
394
c801082d
JD
395static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
396 show_fan_auto_channel, set_fan_auto_channel, 0);
397static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
398 show_fan_auto_channel, set_fan_auto_channel, 1);
1da177e4
LT
399
400/* Auto Temps */
c801082d
JD
401static ssize_t show_auto_temp_off(struct device *dev,
402 struct device_attribute *attr, char *buf)
1da177e4 403{
c801082d 404 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 405 struct adm1031_data *data = adm1031_update_device(dev);
6d6006b8 406 return sprintf(buf, "%d\n",
1da177e4
LT
407 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
408}
c801082d
JD
409static ssize_t show_auto_temp_min(struct device *dev,
410 struct device_attribute *attr, char *buf)
1da177e4 411{
c801082d 412 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
413 struct adm1031_data *data = adm1031_update_device(dev);
414 return sprintf(buf, "%d\n",
415 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
416}
417static ssize_t
c801082d
JD
418set_auto_temp_min(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t count)
1da177e4 420{
b060f3c4
AL
421 struct adm1031_data *data = dev_get_drvdata(dev);
422 struct i2c_client *client = data->client;
c801082d 423 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
424 long val;
425 int ret;
426
427 ret = kstrtol(buf, 10, &val);
428 if (ret)
429 return ret;
1da177e4 430
145e74a4 431 val = clamp_val(val, 0, 127000);
9a61bf63 432 mutex_lock(&data->update_lock);
1da177e4
LT
433 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
434 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
435 data->auto_temp[nr]);
9a61bf63 436 mutex_unlock(&data->update_lock);
1da177e4
LT
437 return count;
438}
c801082d
JD
439static ssize_t show_auto_temp_max(struct device *dev,
440 struct device_attribute *attr, char *buf)
1da177e4 441{
c801082d 442 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
443 struct adm1031_data *data = adm1031_update_device(dev);
444 return sprintf(buf, "%d\n",
445 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
446}
447static ssize_t
c801082d
JD
448set_auto_temp_max(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
1da177e4 450{
b060f3c4
AL
451 struct adm1031_data *data = dev_get_drvdata(dev);
452 struct i2c_client *client = data->client;
c801082d 453 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
454 long val;
455 int ret;
456
457 ret = kstrtol(buf, 10, &val);
458 if (ret)
459 return ret;
1da177e4 460
145e74a4 461 val = clamp_val(val, 0, 127000);
9a61bf63 462 mutex_lock(&data->update_lock);
1c720093
GR
463 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
464 data->pwm[nr]);
1da177e4
LT
465 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
466 data->temp_max[nr]);
9a61bf63 467 mutex_unlock(&data->update_lock);
1da177e4
LT
468 return count;
469}
470
c801082d
JD
471#define auto_temp_reg(offset) \
472static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
473 show_auto_temp_off, NULL, offset - 1); \
474static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
475 show_auto_temp_min, set_auto_temp_min, offset - 1); \
476static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
477 show_auto_temp_max, set_auto_temp_max, offset - 1)
1da177e4
LT
478
479auto_temp_reg(1);
480auto_temp_reg(2);
481auto_temp_reg(3);
482
483/* pwm */
c801082d
JD
484static ssize_t show_pwm(struct device *dev,
485 struct device_attribute *attr, char *buf)
1da177e4 486{
c801082d 487 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
488 struct adm1031_data *data = adm1031_update_device(dev);
489 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
490}
c801082d
JD
491static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count)
1da177e4 493{
b060f3c4
AL
494 struct adm1031_data *data = dev_get_drvdata(dev);
495 struct i2c_client *client = data->client;
c801082d 496 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
497 long val;
498 int ret, reg;
499
500 ret = kstrtol(buf, 10, &val);
501 if (ret)
502 return ret;
1da177e4 503
9a61bf63 504 mutex_lock(&data->update_lock);
6d6006b8 505 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
1da177e4
LT
506 (((val>>4) & 0xf) != 5)) {
507 /* In automatic mode, the only PWM accepted is 33% */
9a61bf63 508 mutex_unlock(&data->update_lock);
1da177e4
LT
509 return -EINVAL;
510 }
511 data->pwm[nr] = PWM_TO_REG(val);
512 reg = adm1031_read_value(client, ADM1031_REG_PWM);
513 adm1031_write_value(client, ADM1031_REG_PWM,
514 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
515 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
9a61bf63 516 mutex_unlock(&data->update_lock);
1da177e4
LT
517 return count;
518}
519
c801082d
JD
520static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
521static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
522static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
523 show_pwm, set_pwm, 0);
524static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
525 show_pwm, set_pwm, 1);
1da177e4
LT
526
527/* Fans */
528
529/*
530 * That function checks the cases where the fan reading is not
44bbe87e 531 * relevant. It is used to provide 0 as fan reading when the fan is
1da177e4
LT
532 * not supposed to run
533 */
534static int trust_fan_readings(struct adm1031_data *data, int chan)
535{
536 int res = 0;
537
538 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
539 switch (data->conf1 & 0x60) {
1c720093
GR
540 case 0x00:
541 /*
542 * remote temp1 controls fan1,
543 * remote temp2 controls fan2
544 */
1da177e4 545 res = data->temp[chan+1] >=
1c720093 546 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
1da177e4
LT
547 break;
548 case 0x20: /* remote temp1 controls both fans */
549 res =
550 data->temp[1] >=
551 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
552 break;
553 case 0x40: /* remote temp2 controls both fans */
554 res =
555 data->temp[2] >=
556 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
557 break;
558 case 0x60: /* max controls both fans */
559 res =
560 data->temp[0] >=
561 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
562 || data->temp[1] >=
563 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
6d6006b8 564 || (data->chip_type == adm1031
1da177e4
LT
565 && data->temp[2] >=
566 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
567 break;
568 }
569 } else {
570 res = data->pwm[chan] > 0;
571 }
572 return res;
573}
574
575
c801082d
JD
576static ssize_t show_fan(struct device *dev,
577 struct device_attribute *attr, char *buf)
1da177e4 578{
c801082d 579 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
580 struct adm1031_data *data = adm1031_update_device(dev);
581 int value;
582
583 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
584 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
585 return sprintf(buf, "%d\n", value);
586}
587
c801082d
JD
588static ssize_t show_fan_div(struct device *dev,
589 struct device_attribute *attr, char *buf)
1da177e4 590{
c801082d 591 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
592 struct adm1031_data *data = adm1031_update_device(dev);
593 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
594}
c801082d
JD
595static ssize_t show_fan_min(struct device *dev,
596 struct device_attribute *attr, char *buf)
1da177e4 597{
c801082d 598 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
599 struct adm1031_data *data = adm1031_update_device(dev);
600 return sprintf(buf, "%d\n",
601 FAN_FROM_REG(data->fan_min[nr],
602 FAN_DIV_FROM_REG(data->fan_div[nr])));
603}
c801082d
JD
604static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
605 const char *buf, size_t count)
1da177e4 606{
b060f3c4
AL
607 struct adm1031_data *data = dev_get_drvdata(dev);
608 struct i2c_client *client = data->client;
c801082d 609 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
610 long val;
611 int ret;
612
613 ret = kstrtol(buf, 10, &val);
614 if (ret)
615 return ret;
1da177e4 616
9a61bf63 617 mutex_lock(&data->update_lock);
1da177e4 618 if (val) {
6d6006b8 619 data->fan_min[nr] =
1da177e4
LT
620 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
621 } else {
622 data->fan_min[nr] = 0xff;
623 }
624 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 625 mutex_unlock(&data->update_lock);
1da177e4
LT
626 return count;
627}
c801082d
JD
628static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
629 const char *buf, size_t count)
1da177e4 630{
b060f3c4
AL
631 struct adm1031_data *data = dev_get_drvdata(dev);
632 struct i2c_client *client = data->client;
c801082d 633 int nr = to_sensor_dev_attr(attr)->index;
1c720093 634 long val;
1da177e4
LT
635 u8 tmp;
636 int old_div;
637 int new_min;
1c720093
GR
638 int ret;
639
640 ret = kstrtol(buf, 10, &val);
641 if (ret)
642 return ret;
1da177e4
LT
643
644 tmp = val == 8 ? 0xc0 :
645 val == 4 ? 0x80 :
6d6006b8
JD
646 val == 2 ? 0x40 :
647 val == 1 ? 0x00 :
1da177e4
LT
648 0xff;
649 if (tmp == 0xff)
650 return -EINVAL;
6d6006b8 651
9a61bf63 652 mutex_lock(&data->update_lock);
38a1f0e9
JD
653 /* Get fresh readings */
654 data->fan_div[nr] = adm1031_read_value(client,
655 ADM1031_REG_FAN_DIV(nr));
656 data->fan_min[nr] = adm1031_read_value(client,
657 ADM1031_REG_FAN_MIN(nr));
658
659 /* Write the new clock divider and fan min */
1da177e4 660 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
6d6006b8
JD
661 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
662 new_min = data->fan_min[nr] * old_div / val;
1da177e4 663 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
1da177e4 664
6d6006b8 665 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
1da177e4 666 data->fan_div[nr]);
6d6006b8 667 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
1da177e4 668 data->fan_min[nr]);
38a1f0e9
JD
669
670 /* Invalidate the cache: fan speed is no longer valid */
671 data->valid = 0;
9a61bf63 672 mutex_unlock(&data->update_lock);
1da177e4
LT
673 return count;
674}
675
676#define fan_offset(offset) \
c801082d
JD
677static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
678 show_fan, NULL, offset - 1); \
679static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
680 show_fan_min, set_fan_min, offset - 1); \
681static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
682 show_fan_div, set_fan_div, offset - 1)
1da177e4
LT
683
684fan_offset(1);
685fan_offset(2);
686
687
688/* Temps */
c801082d
JD
689static ssize_t show_temp(struct device *dev,
690 struct device_attribute *attr, char *buf)
1da177e4 691{
c801082d 692 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
693 struct adm1031_data *data = adm1031_update_device(dev);
694 int ext;
695 ext = nr == 0 ?
696 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
697 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
698 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
699}
49dc9efe
IS
700static ssize_t show_temp_offset(struct device *dev,
701 struct device_attribute *attr, char *buf)
702{
703 int nr = to_sensor_dev_attr(attr)->index;
704 struct adm1031_data *data = adm1031_update_device(dev);
705 return sprintf(buf, "%d\n",
706 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
707}
c801082d
JD
708static ssize_t show_temp_min(struct device *dev,
709 struct device_attribute *attr, char *buf)
1da177e4 710{
c801082d 711 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
712 struct adm1031_data *data = adm1031_update_device(dev);
713 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
714}
c801082d
JD
715static ssize_t show_temp_max(struct device *dev,
716 struct device_attribute *attr, char *buf)
1da177e4 717{
c801082d 718 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
719 struct adm1031_data *data = adm1031_update_device(dev);
720 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
721}
c801082d
JD
722static ssize_t show_temp_crit(struct device *dev,
723 struct device_attribute *attr, char *buf)
1da177e4 724{
c801082d 725 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
726 struct adm1031_data *data = adm1031_update_device(dev);
727 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
728}
49dc9efe
IS
729static ssize_t set_temp_offset(struct device *dev,
730 struct device_attribute *attr, const char *buf,
731 size_t count)
732{
b060f3c4
AL
733 struct adm1031_data *data = dev_get_drvdata(dev);
734 struct i2c_client *client = data->client;
49dc9efe 735 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
736 long val;
737 int ret;
738
739 ret = kstrtol(buf, 10, &val);
740 if (ret)
741 return ret;
49dc9efe 742
2a844c14 743 val = clamp_val(val, -15000, 15000);
49dc9efe
IS
744 mutex_lock(&data->update_lock);
745 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
746 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
747 data->temp_offset[nr]);
748 mutex_unlock(&data->update_lock);
749 return count;
750}
c801082d
JD
751static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
752 const char *buf, size_t count)
1da177e4 753{
b060f3c4
AL
754 struct adm1031_data *data = dev_get_drvdata(dev);
755 struct i2c_client *client = data->client;
c801082d 756 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
757 long val;
758 int ret;
759
760 ret = kstrtol(buf, 10, &val);
761 if (ret)
762 return ret;
1da177e4 763
145e74a4 764 val = clamp_val(val, -55000, 127000);
9a61bf63 765 mutex_lock(&data->update_lock);
1da177e4
LT
766 data->temp_min[nr] = TEMP_TO_REG(val);
767 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
768 data->temp_min[nr]);
9a61bf63 769 mutex_unlock(&data->update_lock);
1da177e4
LT
770 return count;
771}
c801082d
JD
772static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
1da177e4 774{
b060f3c4
AL
775 struct adm1031_data *data = dev_get_drvdata(dev);
776 struct i2c_client *client = data->client;
c801082d 777 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
778 long val;
779 int ret;
780
781 ret = kstrtol(buf, 10, &val);
782 if (ret)
783 return ret;
1da177e4 784
145e74a4 785 val = clamp_val(val, -55000, 127000);
9a61bf63 786 mutex_lock(&data->update_lock);
1da177e4
LT
787 data->temp_max[nr] = TEMP_TO_REG(val);
788 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
789 data->temp_max[nr]);
9a61bf63 790 mutex_unlock(&data->update_lock);
1da177e4
LT
791 return count;
792}
c801082d
JD
793static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
1da177e4 795{
b060f3c4
AL
796 struct adm1031_data *data = dev_get_drvdata(dev);
797 struct i2c_client *client = data->client;
c801082d 798 int nr = to_sensor_dev_attr(attr)->index;
1c720093
GR
799 long val;
800 int ret;
801
802 ret = kstrtol(buf, 10, &val);
803 if (ret)
804 return ret;
1da177e4 805
145e74a4 806 val = clamp_val(val, -55000, 127000);
9a61bf63 807 mutex_lock(&data->update_lock);
1da177e4
LT
808 data->temp_crit[nr] = TEMP_TO_REG(val);
809 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
810 data->temp_crit[nr]);
9a61bf63 811 mutex_unlock(&data->update_lock);
1da177e4
LT
812 return count;
813}
814
c801082d
JD
815#define temp_reg(offset) \
816static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
817 show_temp, NULL, offset - 1); \
49dc9efe
IS
818static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
819 show_temp_offset, set_temp_offset, offset - 1); \
c801082d
JD
820static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
821 show_temp_min, set_temp_min, offset - 1); \
822static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
823 show_temp_max, set_temp_max, offset - 1); \
824static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
825 show_temp_crit, set_temp_crit, offset - 1)
1da177e4
LT
826
827temp_reg(1);
828temp_reg(2);
829temp_reg(3);
830
831/* Alarms */
bfb6b173 832static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
1c720093 833 char *buf)
1da177e4
LT
834{
835 struct adm1031_data *data = adm1031_update_device(dev);
836 return sprintf(buf, "%d\n", data->alarm);
837}
838
bfb6b173 839static DEVICE_ATTR_RO(alarms);
1da177e4 840
050ab878
JD
841static ssize_t show_alarm(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 int bitnr = to_sensor_dev_attr(attr)->index;
845 struct adm1031_data *data = adm1031_update_device(dev);
846 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
847}
848
849static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
850static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
851static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
852static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
853static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
854static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
855static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
856static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
857static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
858static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
859static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
860static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
861static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
862static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
863static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
1da177e4 864
a51b9944
GR
865/* Update Interval */
866static const unsigned int update_intervals[] = {
87c33daa
JD
867 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
868};
869
bfb6b173 870static ssize_t update_interval_show(struct device *dev,
a51b9944 871 struct device_attribute *attr, char *buf)
87c33daa 872{
b060f3c4 873 struct adm1031_data *data = dev_get_drvdata(dev);
87c33daa 874
a51b9944 875 return sprintf(buf, "%u\n", data->update_interval);
87c33daa
JD
876}
877
bfb6b173
JL
878static ssize_t update_interval_store(struct device *dev,
879 struct device_attribute *attr,
880 const char *buf, size_t count)
87c33daa 881{
b060f3c4
AL
882 struct adm1031_data *data = dev_get_drvdata(dev);
883 struct i2c_client *client = data->client;
87c33daa
JD
884 unsigned long val;
885 int i, err;
886 u8 reg;
887
179c4fdb 888 err = kstrtoul(buf, 10, &val);
87c33daa
JD
889 if (err)
890 return err;
891
a51b9944
GR
892 /*
893 * Find the nearest update interval from the table.
894 * Use it to determine the matching update rate.
895 */
896 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
897 if (val >= update_intervals[i])
87c33daa
JD
898 break;
899 }
a51b9944 900 /* if not found, we point to the last entry (lowest update interval) */
87c33daa
JD
901
902 /* set the new update rate while preserving other settings */
903 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
904 reg &= ~ADM1031_UPDATE_RATE_MASK;
905 reg |= i << ADM1031_UPDATE_RATE_SHIFT;
906 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
907
908 mutex_lock(&data->update_lock);
a51b9944 909 data->update_interval = update_intervals[i];
87c33daa
JD
910 mutex_unlock(&data->update_lock);
911
912 return count;
913}
914
bfb6b173 915static DEVICE_ATTR_RW(update_interval);
87c33daa 916
681c6f7a 917static struct attribute *adm1031_attributes[] = {
c801082d
JD
918 &sensor_dev_attr_fan1_input.dev_attr.attr,
919 &sensor_dev_attr_fan1_div.dev_attr.attr,
920 &sensor_dev_attr_fan1_min.dev_attr.attr,
050ab878
JD
921 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
922 &sensor_dev_attr_fan1_fault.dev_attr.attr,
c801082d
JD
923 &sensor_dev_attr_pwm1.dev_attr.attr,
924 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
925 &sensor_dev_attr_temp1_input.dev_attr.attr,
49dc9efe 926 &sensor_dev_attr_temp1_offset.dev_attr.attr,
c801082d 927 &sensor_dev_attr_temp1_min.dev_attr.attr,
050ab878 928 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
c801082d 929 &sensor_dev_attr_temp1_max.dev_attr.attr,
050ab878 930 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
c801082d 931 &sensor_dev_attr_temp1_crit.dev_attr.attr,
050ab878 932 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
c801082d 933 &sensor_dev_attr_temp2_input.dev_attr.attr,
49dc9efe 934 &sensor_dev_attr_temp2_offset.dev_attr.attr,
c801082d 935 &sensor_dev_attr_temp2_min.dev_attr.attr,
050ab878 936 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
c801082d 937 &sensor_dev_attr_temp2_max.dev_attr.attr,
050ab878 938 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
c801082d 939 &sensor_dev_attr_temp2_crit.dev_attr.attr,
050ab878
JD
940 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
941 &sensor_dev_attr_temp2_fault.dev_attr.attr,
c801082d
JD
942
943 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
944 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
945 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
946
947 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
948 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
949 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
950
951 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
681c6f7a 952
a51b9944 953 &dev_attr_update_interval.attr,
681c6f7a
MH
954 &dev_attr_alarms.attr,
955
956 NULL
957};
958
959static const struct attribute_group adm1031_group = {
960 .attrs = adm1031_attributes,
961};
962
963static struct attribute *adm1031_attributes_opt[] = {
c801082d
JD
964 &sensor_dev_attr_fan2_input.dev_attr.attr,
965 &sensor_dev_attr_fan2_div.dev_attr.attr,
966 &sensor_dev_attr_fan2_min.dev_attr.attr,
050ab878
JD
967 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
968 &sensor_dev_attr_fan2_fault.dev_attr.attr,
c801082d
JD
969 &sensor_dev_attr_pwm2.dev_attr.attr,
970 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
971 &sensor_dev_attr_temp3_input.dev_attr.attr,
49dc9efe 972 &sensor_dev_attr_temp3_offset.dev_attr.attr,
c801082d 973 &sensor_dev_attr_temp3_min.dev_attr.attr,
050ab878 974 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
c801082d 975 &sensor_dev_attr_temp3_max.dev_attr.attr,
050ab878 976 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
c801082d 977 &sensor_dev_attr_temp3_crit.dev_attr.attr,
050ab878
JD
978 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
979 &sensor_dev_attr_temp3_fault.dev_attr.attr,
c801082d
JD
980 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
981 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
982 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
983 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
681c6f7a
MH
984 NULL
985};
986
987static const struct attribute_group adm1031_group_opt = {
988 .attrs = adm1031_attributes_opt,
989};
990
af200f88 991/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 992static int adm1031_detect(struct i2c_client *client,
af200f88 993 struct i2c_board_info *info)
1da177e4 994{
af200f88 995 struct i2c_adapter *adapter = client->adapter;
52df6440
JD
996 const char *name;
997 int id, co;
1da177e4
LT
998
999 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
af200f88 1000 return -ENODEV;
1da177e4 1001
52df6440
JD
1002 id = i2c_smbus_read_byte_data(client, 0x3d);
1003 co = i2c_smbus_read_byte_data(client, 0x3e);
1da177e4 1004
52df6440
JD
1005 if (!((id == 0x31 || id == 0x30) && co == 0x41))
1006 return -ENODEV;
1007 name = (id == 0x30) ? "adm1030" : "adm1031";
1da177e4 1008
af200f88 1009 strlcpy(info->type, name, I2C_NAME_SIZE);
1da177e4 1010
af200f88
JD
1011 return 0;
1012}
1013
278ee1c8
AL
1014static void adm1031_init_client(struct i2c_client *client)
1015{
1016 unsigned int read_val;
1017 unsigned int mask;
1018 int i;
1019 struct adm1031_data *data = i2c_get_clientdata(client);
1020
1021 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
1022 if (data->chip_type == adm1031) {
1023 mask |= (ADM1031_CONF2_PWM2_ENABLE |
1024 ADM1031_CONF2_TACH2_ENABLE);
1025 }
1026 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1027 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1028 if ((read_val | mask) != read_val)
1029 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
1030
1031 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
1032 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1033 adm1031_write_value(client, ADM1031_REG_CONF1,
1034 read_val | ADM1031_CONF1_MONITOR_ENABLE);
1035 }
1036
1037 /* Read the chip's update rate */
1038 mask = ADM1031_UPDATE_RATE_MASK;
1039 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
1040 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
1041 /* Save it as update interval */
1042 data->update_interval = update_intervals[i];
1043}
1044
af200f88
JD
1045static int adm1031_probe(struct i2c_client *client,
1046 const struct i2c_device_id *id)
1047{
b060f3c4
AL
1048 struct device *dev = &client->dev;
1049 struct device *hwmon_dev;
af200f88 1050 struct adm1031_data *data;
af200f88 1051
b060f3c4 1052 data = devm_kzalloc(dev, sizeof(struct adm1031_data), GFP_KERNEL);
dc2fd663
GR
1053 if (!data)
1054 return -ENOMEM;
af200f88
JD
1055
1056 i2c_set_clientdata(client, data);
b060f3c4 1057 data->client = client;
af200f88 1058 data->chip_type = id->driver_data;
9a61bf63 1059 mutex_init(&data->update_lock);
1da177e4 1060
af200f88
JD
1061 if (data->chip_type == adm1030)
1062 data->chan_select_table = &auto_channel_select_table_adm1030;
1063 else
1064 data->chan_select_table = &auto_channel_select_table_adm1031;
1da177e4
LT
1065
1066 /* Initialize the ADM1031 chip */
6d6006b8 1067 adm1031_init_client(client);
1da177e4 1068
b060f3c4
AL
1069 /* sysfs hooks */
1070 data->groups[0] = &adm1031_group;
1071 if (data->chip_type == adm1031)
1072 data->groups[1] = &adm1031_group_opt;
1da177e4 1073
b060f3c4
AL
1074 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1075 data, data->groups);
1076 return PTR_ERR_OR_ZERO(hwmon_dev);
1da177e4
LT
1077}
1078
278ee1c8
AL
1079static const struct i2c_device_id adm1031_id[] = {
1080 { "adm1030", adm1030 },
1081 { "adm1031", adm1031 },
1082 { }
1083};
1084MODULE_DEVICE_TABLE(i2c, adm1031_id);
1da177e4 1085
278ee1c8
AL
1086static struct i2c_driver adm1031_driver = {
1087 .class = I2C_CLASS_HWMON,
1088 .driver = {
1089 .name = "adm1031",
1090 },
1091 .probe = adm1031_probe,
278ee1c8
AL
1092 .id_table = adm1031_id,
1093 .detect = adm1031_detect,
1094 .address_list = normal_i2c,
1095};
1da177e4 1096
f0967eea 1097module_i2c_driver(adm1031_driver);
1da177e4
LT
1098
1099MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1100MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1101MODULE_LICENSE("GPL");