blob: 789441830cd8fb8a5bc3c19ffbcd639ef2935a47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
7 Reworked by Jean Delvare <khali@linux-fr.org>
Jean Delvare6d6006b2007-12-02 23:33:57 +01008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 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*/
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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvarec8010822007-12-02 23:39:38 +010030#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Following macros takes channel parameter starting from 0 to 2 */
35#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010036#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ADM1031_REG_PWM (0x22)
38#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
39
Jean Delvare6d6006b2007-12-02 23:33:57 +010040#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
41#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
42#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvare6d6006b2007-12-02 23:33:57 +010044#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
46
47#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
48
Jean Delvare6d6006b2007-12-02 23:33:57 +010049#define ADM1031_REG_CONF1 0x00
50#define ADM1031_REG_CONF2 0x01
51#define ADM1031_REG_EXT_TEMP 0x06
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
54#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
55#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
56
57#define ADM1031_CONF2_PWM1_ENABLE 0x01
58#define ADM1031_CONF2_PWM2_ENABLE 0x02
59#define ADM1031_CONF2_TACH1_ENABLE 0x04
60#define ADM1031_CONF2_TACH2_ENABLE 0x08
61#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
62
63/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050064static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020067I2C_CLIENT_INSMOD_2(adm1030, adm1031);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69typedef u8 auto_chan_table_t[8][2];
70
71/* Each client has this additional data */
72struct adm1031_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070073 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010074 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int chip_type;
76 char valid; /* !=0 if following fields are valid */
77 unsigned long last_updated; /* In jiffies */
78 /* The chan_select_table contains the possible configurations for
79 * auto fan control.
80 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010081 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 u16 alarm;
83 u8 conf1;
84 u8 conf2;
85 u8 fan[2];
86 u8 fan_div[2];
87 u8 fan_min[2];
88 u8 pwm[2];
89 u8 old_pwm[2];
90 s8 temp[3];
91 u8 ext_temp[3];
92 u8 auto_temp[3];
93 u8 auto_temp_min[3];
94 u8 auto_temp_off[3];
95 u8 auto_temp_max[3];
96 s8 temp_min[3];
97 s8 temp_max[3];
98 s8 temp_crit[3];
99};
100
Jean Delvareaf200f82008-07-16 19:30:09 +0200101static int adm1031_probe(struct i2c_client *client,
102 const struct i2c_device_id *id);
103static int adm1031_detect(struct i2c_client *client, int kind,
104 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void adm1031_init_client(struct i2c_client *client);
Jean Delvareaf200f82008-07-16 19:30:09 +0200106static int adm1031_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct adm1031_data *adm1031_update_device(struct device *dev);
108
Jean Delvareaf200f82008-07-16 19:30:09 +0200109static const struct i2c_device_id adm1031_id[] = {
110 { "adm1030", adm1030 },
111 { "adm1031", adm1031 },
112 { }
113};
114MODULE_DEVICE_TABLE(i2c, adm1031_id);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* This is the driver that will be inserted */
117static struct i2c_driver adm1031_driver = {
Jean Delvareaf200f82008-07-16 19:30:09 +0200118 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100119 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100120 .name = "adm1031",
121 },
Jean Delvareaf200f82008-07-16 19:30:09 +0200122 .probe = adm1031_probe,
123 .remove = adm1031_remove,
124 .id_table = adm1031_id,
125 .detect = adm1031_detect,
126 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
130{
131 return i2c_smbus_read_byte_data(client, reg);
132}
133
134static inline int
135adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
136{
137 return i2c_smbus_write_byte_data(client, reg, value);
138}
139
140
141#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
142 ((val + 500) / 1000)))
143
144#define TEMP_FROM_REG(val) ((val) * 1000)
145
146#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
147
148#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
149
150static int FAN_TO_REG(int reg, int div)
151{
152 int tmp;
153 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
154 return tmp > 255 ? 255 : tmp;
155}
156
157#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
158
159#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
160#define PWM_FROM_REG(val) ((val) << 4)
161
162#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
163#define FAN_CHAN_TO_REG(val, reg) \
164 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
165
166#define AUTO_TEMP_MIN_TO_REG(val, reg) \
167 ((((val)/500) & 0xf8)|((reg) & 0x7))
168#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
169#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
170
171#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
172
173#define AUTO_TEMP_OFF_FROM_REG(reg) \
174 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
175
176#define AUTO_TEMP_MAX_FROM_REG(reg) \
177 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
178 AUTO_TEMP_MIN_FROM_REG(reg))
179
180static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
181{
182 int ret;
183 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
184
185 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
186 ret = ((reg & 0xf8) |
187 (range < 10000 ? 0 :
188 range < 20000 ? 1 :
189 range < 40000 ? 2 : range < 80000 ? 3 : 4));
190 return ret;
191}
192
193/* FAN auto control */
194#define GET_FAN_AUTO_BITFIELD(data, idx) \
195 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
196
Jean Delvare6d6006b2007-12-02 23:33:57 +0100197/* The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * control bitfields. the index in the table is the register value.
199 * MSb is the auto fan control enable bit, so the four first entries
200 * in the table disables auto fan control when both bitfields are zero.
201 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100202static const auto_chan_table_t auto_channel_select_table_adm1031 = {
203 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
204 { 2 /* 0b010 */ , 4 /* 0b100 */ },
205 { 2 /* 0b010 */ , 2 /* 0b010 */ },
206 { 4 /* 0b100 */ , 4 /* 0b100 */ },
207 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Jean Delvare6d6006b2007-12-02 23:33:57 +0100210static const auto_chan_table_t auto_channel_select_table_adm1030 = {
211 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
212 { 2 /* 0b10 */ , 0 },
213 { 0xff /* invalid */ , 0 },
214 { 0xff /* invalid */ , 0 },
215 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218/* That function checks if a bitfield is valid and returns the other bitfield
219 * nearest match if no exact match where found.
220 */
221static int
222get_fan_auto_nearest(struct adm1031_data *data,
223 int chan, u8 val, u8 reg, u8 * new_reg)
224{
225 int i;
226 int first_match = -1, exact_match = -1;
227 u8 other_reg_val =
228 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
229
230 if (val == 0) {
231 *new_reg = 0;
232 return 0;
233 }
234
235 for (i = 0; i < 8; i++) {
236 if ((val == (*data->chan_select_table)[i][chan]) &&
237 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
238 other_reg_val)) {
239 /* We found an exact match */
240 exact_match = i;
241 break;
242 } else if (val == (*data->chan_select_table)[i][chan] &&
243 first_match == -1) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100244 /* Save the first match in case of an exact match has
245 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
247 first_match = i;
248 }
249 }
250
251 if (exact_match >= 0) {
252 *new_reg = exact_match;
253 } else if (first_match >= 0) {
254 *new_reg = first_match;
255 } else {
256 return -EINVAL;
257 }
258 return 0;
259}
260
Jean Delvarec8010822007-12-02 23:39:38 +0100261static ssize_t show_fan_auto_channel(struct device *dev,
262 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jean Delvarec8010822007-12-02 23:39:38 +0100264 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct adm1031_data *data = adm1031_update_device(dev);
266 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
267}
268
269static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100270set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct i2c_client *client = to_i2c_client(dev);
274 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100275 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int val = simple_strtol(buf, NULL, 10);
277 u8 reg;
278 int ret;
279 u8 old_fan_mode;
280
281 old_fan_mode = data->conf1;
282
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100283 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100286 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return ret;
288 }
Jean Delvare6d6006b2007-12-02 23:33:57 +0100289 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
290 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
292 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
Jean Delvare6d6006b2007-12-02 23:33:57 +0100293 /* Switch to Auto Fan Mode
294 * Save PWM registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * Set PWM registers to 33% Both */
296 data->old_pwm[0] = data->pwm[0];
297 data->old_pwm[1] = data->pwm[1];
298 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
299 } else {
300 /* Switch to Manual Mode */
301 data->pwm[0] = data->old_pwm[0];
302 data->pwm[1] = data->old_pwm[1];
303 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100304 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 data->pwm[0] | (data->pwm[1] << 4));
306 }
307 }
308 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
309 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100310 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return count;
312}
313
Jean Delvarec8010822007-12-02 23:39:38 +0100314static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
315 show_fan_auto_channel, set_fan_auto_channel, 0);
316static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
317 show_fan_auto_channel, set_fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/* Auto Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100320static ssize_t show_auto_temp_off(struct device *dev,
321 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Jean Delvarec8010822007-12-02 23:39:38 +0100323 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100325 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
327}
Jean Delvarec8010822007-12-02 23:39:38 +0100328static ssize_t show_auto_temp_min(struct device *dev,
329 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Jean Delvarec8010822007-12-02 23:39:38 +0100331 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct adm1031_data *data = adm1031_update_device(dev);
333 return sprintf(buf, "%d\n",
334 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
335}
336static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100337set_auto_temp_min(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct i2c_client *client = to_i2c_client(dev);
341 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100342 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int val = simple_strtol(buf, NULL, 10);
344
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100345 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
347 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
348 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100349 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return count;
351}
Jean Delvarec8010822007-12-02 23:39:38 +0100352static ssize_t show_auto_temp_max(struct device *dev,
353 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jean Delvarec8010822007-12-02 23:39:38 +0100355 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct adm1031_data *data = adm1031_update_device(dev);
357 return sprintf(buf, "%d\n",
358 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
359}
360static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100361set_auto_temp_max(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct i2c_client *client = to_i2c_client(dev);
365 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100366 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int val = simple_strtol(buf, NULL, 10);
368
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100369 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
371 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
372 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100373 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return count;
375}
376
Jean Delvarec8010822007-12-02 23:39:38 +0100377#define auto_temp_reg(offset) \
378static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
379 show_auto_temp_off, NULL, offset - 1); \
380static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
381 show_auto_temp_min, set_auto_temp_min, offset - 1); \
382static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
383 show_auto_temp_max, set_auto_temp_max, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385auto_temp_reg(1);
386auto_temp_reg(2);
387auto_temp_reg(3);
388
389/* pwm */
Jean Delvarec8010822007-12-02 23:39:38 +0100390static ssize_t show_pwm(struct device *dev,
391 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jean Delvarec8010822007-12-02 23:39:38 +0100393 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct adm1031_data *data = adm1031_update_device(dev);
395 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
396}
Jean Delvarec8010822007-12-02 23:39:38 +0100397static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
398 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct i2c_client *client = to_i2c_client(dev);
401 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100402 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int val = simple_strtol(buf, NULL, 10);
404 int reg;
405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100407 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 (((val>>4) & 0xf) != 5)) {
409 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100410 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -EINVAL;
412 }
413 data->pwm[nr] = PWM_TO_REG(val);
414 reg = adm1031_read_value(client, ADM1031_REG_PWM);
415 adm1031_write_value(client, ADM1031_REG_PWM,
416 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
417 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100418 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return count;
420}
421
Jean Delvarec8010822007-12-02 23:39:38 +0100422static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
423static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
424static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
425 show_pwm, set_pwm, 0);
426static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
427 show_pwm, set_pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/* Fans */
430
431/*
432 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600433 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * not supposed to run
435 */
436static int trust_fan_readings(struct adm1031_data *data, int chan)
437{
438 int res = 0;
439
440 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
441 switch (data->conf1 & 0x60) {
442 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
443 res = data->temp[chan+1] >=
444 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
445 break;
446 case 0x20: /* remote temp1 controls both fans */
447 res =
448 data->temp[1] >=
449 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
450 break;
451 case 0x40: /* remote temp2 controls both fans */
452 res =
453 data->temp[2] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
455 break;
456 case 0x60: /* max controls both fans */
457 res =
458 data->temp[0] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
460 || data->temp[1] >=
461 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100462 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 && data->temp[2] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
465 break;
466 }
467 } else {
468 res = data->pwm[chan] > 0;
469 }
470 return res;
471}
472
473
Jean Delvarec8010822007-12-02 23:39:38 +0100474static ssize_t show_fan(struct device *dev,
475 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jean Delvarec8010822007-12-02 23:39:38 +0100477 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct adm1031_data *data = adm1031_update_device(dev);
479 int value;
480
481 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
482 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
483 return sprintf(buf, "%d\n", value);
484}
485
Jean Delvarec8010822007-12-02 23:39:38 +0100486static ssize_t show_fan_div(struct device *dev,
487 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jean Delvarec8010822007-12-02 23:39:38 +0100489 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct adm1031_data *data = adm1031_update_device(dev);
491 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
492}
Jean Delvarec8010822007-12-02 23:39:38 +0100493static ssize_t show_fan_min(struct device *dev,
494 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jean Delvarec8010822007-12-02 23:39:38 +0100496 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct adm1031_data *data = adm1031_update_device(dev);
498 return sprintf(buf, "%d\n",
499 FAN_FROM_REG(data->fan_min[nr],
500 FAN_DIV_FROM_REG(data->fan_div[nr])));
501}
Jean Delvarec8010822007-12-02 23:39:38 +0100502static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct i2c_client *client = to_i2c_client(dev);
506 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100507 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int val = simple_strtol(buf, NULL, 10);
509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100512 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
514 } else {
515 data->fan_min[nr] = 0xff;
516 }
517 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100518 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return count;
520}
Jean Delvarec8010822007-12-02 23:39:38 +0100521static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 struct i2c_client *client = to_i2c_client(dev);
525 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100526 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int val = simple_strtol(buf, NULL, 10);
528 u8 tmp;
529 int old_div;
530 int new_min;
531
532 tmp = val == 8 ? 0xc0 :
533 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100534 val == 2 ? 0x40 :
535 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 0xff;
537 if (tmp == 0xff)
538 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100539
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100540 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100541 /* Get fresh readings */
542 data->fan_div[nr] = adm1031_read_value(client,
543 ADM1031_REG_FAN_DIV(nr));
544 data->fan_min[nr] = adm1031_read_value(client,
545 ADM1031_REG_FAN_MIN(nr));
546
547 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100549 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
550 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jean Delvare6d6006b2007-12-02 23:33:57 +0100553 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100555 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100557
558 /* Invalidate the cache: fan speed is no longer valid */
559 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100560 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return count;
562}
563
564#define fan_offset(offset) \
Jean Delvarec8010822007-12-02 23:39:38 +0100565static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
566 show_fan, NULL, offset - 1); \
567static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
568 show_fan_min, set_fan_min, offset - 1); \
569static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
570 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572fan_offset(1);
573fan_offset(2);
574
575
576/* Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100577static ssize_t show_temp(struct device *dev,
578 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Jean Delvarec8010822007-12-02 23:39:38 +0100580 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct adm1031_data *data = adm1031_update_device(dev);
582 int ext;
583 ext = nr == 0 ?
584 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
585 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
586 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
587}
Jean Delvarec8010822007-12-02 23:39:38 +0100588static ssize_t show_temp_min(struct device *dev,
589 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Jean Delvarec8010822007-12-02 23:39:38 +0100591 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct adm1031_data *data = adm1031_update_device(dev);
593 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
594}
Jean Delvarec8010822007-12-02 23:39:38 +0100595static ssize_t show_temp_max(struct device *dev,
596 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jean Delvarec8010822007-12-02 23:39:38 +0100598 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct adm1031_data *data = adm1031_update_device(dev);
600 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
601}
Jean Delvarec8010822007-12-02 23:39:38 +0100602static ssize_t show_temp_crit(struct device *dev,
603 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jean Delvarec8010822007-12-02 23:39:38 +0100605 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct adm1031_data *data = adm1031_update_device(dev);
607 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
608}
Jean Delvarec8010822007-12-02 23:39:38 +0100609static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct i2c_client *client = to_i2c_client(dev);
613 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100614 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int val;
616
617 val = simple_strtol(buf, NULL, 10);
618 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 data->temp_min[nr] = TEMP_TO_REG(val);
621 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
622 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100623 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return count;
625}
Jean Delvarec8010822007-12-02 23:39:38 +0100626static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct i2c_client *client = to_i2c_client(dev);
630 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100631 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int val;
633
634 val = simple_strtol(buf, NULL, 10);
635 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100636 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 data->temp_max[nr] = TEMP_TO_REG(val);
638 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
639 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100640 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return count;
642}
Jean Delvarec8010822007-12-02 23:39:38 +0100643static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct i2c_client *client = to_i2c_client(dev);
647 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100648 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int val;
650
651 val = simple_strtol(buf, NULL, 10);
652 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 data->temp_crit[nr] = TEMP_TO_REG(val);
655 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
656 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100657 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return count;
659}
660
Jean Delvarec8010822007-12-02 23:39:38 +0100661#define temp_reg(offset) \
662static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
663 show_temp, NULL, offset - 1); \
664static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
665 show_temp_min, set_temp_min, offset - 1); \
666static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
667 show_temp_max, set_temp_max, offset - 1); \
668static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
669 show_temp_crit, set_temp_crit, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671temp_reg(1);
672temp_reg(2);
673temp_reg(3);
674
675/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400676static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct adm1031_data *data = adm1031_update_device(dev);
679 return sprintf(buf, "%d\n", data->alarm);
680}
681
682static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
683
Jean Delvare050ab872007-12-02 23:42:24 +0100684static ssize_t show_alarm(struct device *dev,
685 struct device_attribute *attr, char *buf)
686{
687 int bitnr = to_sensor_dev_attr(attr)->index;
688 struct adm1031_data *data = adm1031_update_device(dev);
689 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
690}
691
692static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
693static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
694static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
695static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
696static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
697static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
698static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
699static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
700static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
701static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
702static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
703static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
704static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
705static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
706static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200708static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100709 &sensor_dev_attr_fan1_input.dev_attr.attr,
710 &sensor_dev_attr_fan1_div.dev_attr.attr,
711 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100712 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
713 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100714 &sensor_dev_attr_pwm1.dev_attr.attr,
715 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
716 &sensor_dev_attr_temp1_input.dev_attr.attr,
717 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100718 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100719 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100720 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100721 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100722 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100723 &sensor_dev_attr_temp2_input.dev_attr.attr,
724 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100725 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100726 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100727 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100728 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100729 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
730 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200731
Jean Delvarec8010822007-12-02 23:39:38 +0100732 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
733 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
734 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200735
Jean Delvarec8010822007-12-02 23:39:38 +0100736 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
737 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
738 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200739
Jean Delvarec8010822007-12-02 23:39:38 +0100740 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200741
742 &dev_attr_alarms.attr,
743
744 NULL
745};
746
747static const struct attribute_group adm1031_group = {
748 .attrs = adm1031_attributes,
749};
750
751static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100752 &sensor_dev_attr_fan2_input.dev_attr.attr,
753 &sensor_dev_attr_fan2_div.dev_attr.attr,
754 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100755 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
756 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100757 &sensor_dev_attr_pwm2.dev_attr.attr,
758 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
759 &sensor_dev_attr_temp3_input.dev_attr.attr,
760 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100761 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100762 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100763 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100764 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100765 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
766 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100767 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
768 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
769 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
770 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200771 NULL
772};
773
774static const struct attribute_group adm1031_group_opt = {
775 .attrs = adm1031_attributes_opt,
776};
777
Jean Delvareaf200f82008-07-16 19:30:09 +0200778/* Return 0 if detection is successful, -ENODEV otherwise */
779static int adm1031_detect(struct i2c_client *client, int kind,
780 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Jean Delvareaf200f82008-07-16 19:30:09 +0200782 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 const char *name = "";
784
785 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareaf200f82008-07-16 19:30:09 +0200786 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (kind < 0) {
789 int id, co;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100790 id = i2c_smbus_read_byte_data(client, 0x3d);
791 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!((id == 0x31 || id == 0x30) && co == 0x41))
Jean Delvareaf200f82008-07-16 19:30:09 +0200794 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 kind = (id == 0x30) ? adm1030 : adm1031;
796 }
797
798 if (kind <= 0)
799 kind = adm1031;
800
801 /* Given the detected chip type, set the chip name and the
802 * auto fan control helper table. */
803 if (kind == adm1030) {
804 name = "adm1030";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else if (kind == adm1031) {
806 name = "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Jean Delvareaf200f82008-07-16 19:30:09 +0200808 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Jean Delvareaf200f82008-07-16 19:30:09 +0200810 return 0;
811}
812
813static int adm1031_probe(struct i2c_client *client,
814 const struct i2c_device_id *id)
815{
816 struct adm1031_data *data;
817 int err;
818
819 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
820 if (!data) {
821 err = -ENOMEM;
822 goto exit;
823 }
824
825 i2c_set_clientdata(client, data);
826 data->chip_type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jean Delvareaf200f82008-07-16 19:30:09 +0200829 if (data->chip_type == adm1030)
830 data->chan_select_table = &auto_channel_select_table_adm1030;
831 else
832 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100835 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* Register sysfs hooks */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100838 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
Jean Delvareaf200f82008-07-16 19:30:09 +0200839 goto exit_free;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200840
Jean Delvareaf200f82008-07-16 19:30:09 +0200841 if (data->chip_type == adm1031) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100842 if ((err = sysfs_create_group(&client->dev.kobj,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200843 &adm1031_group_opt)))
844 goto exit_remove;
845 }
846
Jean Delvare6d6006b2007-12-02 23:33:57 +0100847 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700848 if (IS_ERR(data->hwmon_dev)) {
849 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200850 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 return 0;
854
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200855exit_remove:
Jean Delvare6d6006b2007-12-02 23:33:57 +0100856 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
857 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400859 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860exit:
861 return err;
862}
863
Jean Delvareaf200f82008-07-16 19:30:09 +0200864static int adm1031_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400866 struct adm1031_data *data = i2c_get_clientdata(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400867
Tony Jones1beeffe2007-08-20 13:46:20 -0700868 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200869 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
870 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400871 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873}
874
875static void adm1031_init_client(struct i2c_client *client)
876{
877 unsigned int read_val;
878 unsigned int mask;
879 struct adm1031_data *data = i2c_get_clientdata(client);
880
881 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
882 if (data->chip_type == adm1031) {
883 mask |= (ADM1031_CONF2_PWM2_ENABLE |
884 ADM1031_CONF2_TACH2_ENABLE);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* Initialize the ADM1031 chip (enables fan speed reading ) */
887 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
888 if ((read_val | mask) != read_val) {
889 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
890 }
891
892 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
893 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
894 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
895 ADM1031_CONF1_MONITOR_ENABLE);
896 }
897
898}
899
900static struct adm1031_data *adm1031_update_device(struct device *dev)
901{
902 struct i2c_client *client = to_i2c_client(dev);
903 struct adm1031_data *data = i2c_get_clientdata(client);
904 int chan;
905
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100906 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
909 || !data->valid) {
910
911 dev_dbg(&client->dev, "Starting adm1031 update\n");
912 for (chan = 0;
913 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
914 u8 oldh, newh;
915
916 oldh =
917 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
918 data->ext_temp[chan] =
919 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
920 newh =
921 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
922 if (newh != oldh) {
923 data->ext_temp[chan] =
924 adm1031_read_value(client,
925 ADM1031_REG_EXT_TEMP);
926#ifdef DEBUG
927 oldh =
928 adm1031_read_value(client,
929 ADM1031_REG_TEMP(chan));
930
931 /* oldh is actually newer */
932 if (newh != oldh)
933 dev_warn(&client->dev,
934 "Remote temperature may be "
935 "wrong.\n");
936#endif
937 }
938 data->temp[chan] = newh;
939
940 data->temp_min[chan] =
941 adm1031_read_value(client,
942 ADM1031_REG_TEMP_MIN(chan));
943 data->temp_max[chan] =
944 adm1031_read_value(client,
945 ADM1031_REG_TEMP_MAX(chan));
946 data->temp_crit[chan] =
947 adm1031_read_value(client,
948 ADM1031_REG_TEMP_CRIT(chan));
949 data->auto_temp[chan] =
950 adm1031_read_value(client,
951 ADM1031_REG_AUTO_TEMP(chan));
952
953 }
954
955 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
956 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
957
958 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
959 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
960 << 8);
961 if (data->chip_type == adm1030) {
962 data->alarm &= 0xc0ff;
963 }
Jean Delvare6d6006b2007-12-02 23:33:57 +0100964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
966 data->fan_div[chan] =
967 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
968 data->fan_min[chan] =
969 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
970 data->fan[chan] =
971 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
972 data->pwm[chan] =
Jean Delvare6d6006b2007-12-02 23:33:57 +0100973 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 (4*chan));
975 }
976 data->last_updated = jiffies;
977 data->valid = 1;
978 }
979
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100980 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 return data;
983}
984
985static int __init sensors_adm1031_init(void)
986{
987 return i2c_add_driver(&adm1031_driver);
988}
989
990static void __exit sensors_adm1031_exit(void)
991{
992 i2c_del_driver(&adm1031_driver);
993}
994
995MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
996MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
997MODULE_LICENSE("GPL");
998
999module_init(sensors_adm1031_init);
1000module_exit(sensors_adm1031_exit);