blob: 56905955352cac894b0c381bad597050be236438 [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
Ira Snyder49dc9ef2009-09-23 22:59:41 +020040#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010041#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
42#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
43#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jean Delvare6d6006b2007-12-02 23:33:57 +010045#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
47
48#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
49
Jean Delvare6d6006b2007-12-02 23:33:57 +010050#define ADM1031_REG_CONF1 0x00
51#define ADM1031_REG_CONF2 0x01
52#define ADM1031_REG_EXT_TEMP 0x06
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
55#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
56#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
57
58#define ADM1031_CONF2_PWM1_ENABLE 0x01
59#define ADM1031_CONF2_PWM2_ENABLE 0x02
60#define ADM1031_CONF2_TACH1_ENABLE 0x04
61#define ADM1031_CONF2_TACH2_ENABLE 0x08
62#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
63
64/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050065static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020068I2C_CLIENT_INSMOD_2(adm1030, adm1031);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70typedef u8 auto_chan_table_t[8][2];
71
72/* Each client has this additional data */
73struct adm1031_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070074 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010075 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int chip_type;
77 char valid; /* !=0 if following fields are valid */
78 unsigned long last_updated; /* In jiffies */
79 /* The chan_select_table contains the possible configurations for
80 * auto fan control.
81 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010082 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 u16 alarm;
84 u8 conf1;
85 u8 conf2;
86 u8 fan[2];
87 u8 fan_div[2];
88 u8 fan_min[2];
89 u8 pwm[2];
90 u8 old_pwm[2];
91 s8 temp[3];
92 u8 ext_temp[3];
93 u8 auto_temp[3];
94 u8 auto_temp_min[3];
95 u8 auto_temp_off[3];
96 u8 auto_temp_max[3];
Ira Snyder49dc9ef2009-09-23 22:59:41 +020097 s8 temp_offset[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 s8 temp_min[3];
99 s8 temp_max[3];
100 s8 temp_crit[3];
101};
102
Jean Delvareaf200f82008-07-16 19:30:09 +0200103static int adm1031_probe(struct i2c_client *client,
104 const struct i2c_device_id *id);
105static int adm1031_detect(struct i2c_client *client, int kind,
106 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static void adm1031_init_client(struct i2c_client *client);
Jean Delvareaf200f82008-07-16 19:30:09 +0200108static int adm1031_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct adm1031_data *adm1031_update_device(struct device *dev);
110
Jean Delvareaf200f82008-07-16 19:30:09 +0200111static const struct i2c_device_id adm1031_id[] = {
112 { "adm1030", adm1030 },
113 { "adm1031", adm1031 },
114 { }
115};
116MODULE_DEVICE_TABLE(i2c, adm1031_id);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* This is the driver that will be inserted */
119static struct i2c_driver adm1031_driver = {
Jean Delvareaf200f82008-07-16 19:30:09 +0200120 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100121 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100122 .name = "adm1031",
123 },
Jean Delvareaf200f82008-07-16 19:30:09 +0200124 .probe = adm1031_probe,
125 .remove = adm1031_remove,
126 .id_table = adm1031_id,
127 .detect = adm1031_detect,
128 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
132{
133 return i2c_smbus_read_byte_data(client, reg);
134}
135
136static inline int
137adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
138{
139 return i2c_smbus_write_byte_data(client, reg, value);
140}
141
142
143#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
144 ((val + 500) / 1000)))
145
146#define TEMP_FROM_REG(val) ((val) * 1000)
147
148#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
149
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200150#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
151#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
152 (val) | 0x70 : (val))
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
155
156static int FAN_TO_REG(int reg, int div)
157{
158 int tmp;
159 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
160 return tmp > 255 ? 255 : tmp;
161}
162
163#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
164
165#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
166#define PWM_FROM_REG(val) ((val) << 4)
167
168#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
169#define FAN_CHAN_TO_REG(val, reg) \
170 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
171
172#define AUTO_TEMP_MIN_TO_REG(val, reg) \
173 ((((val)/500) & 0xf8)|((reg) & 0x7))
174#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
175#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
176
177#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
178
179#define AUTO_TEMP_OFF_FROM_REG(reg) \
180 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
181
182#define AUTO_TEMP_MAX_FROM_REG(reg) \
183 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
184 AUTO_TEMP_MIN_FROM_REG(reg))
185
186static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
187{
188 int ret;
189 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
190
191 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
192 ret = ((reg & 0xf8) |
193 (range < 10000 ? 0 :
194 range < 20000 ? 1 :
195 range < 40000 ? 2 : range < 80000 ? 3 : 4));
196 return ret;
197}
198
199/* FAN auto control */
200#define GET_FAN_AUTO_BITFIELD(data, idx) \
201 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
202
Jean Delvare6d6006b2007-12-02 23:33:57 +0100203/* The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * control bitfields. the index in the table is the register value.
205 * MSb is the auto fan control enable bit, so the four first entries
206 * in the table disables auto fan control when both bitfields are zero.
207 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100208static const auto_chan_table_t auto_channel_select_table_adm1031 = {
209 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
210 { 2 /* 0b010 */ , 4 /* 0b100 */ },
211 { 2 /* 0b010 */ , 2 /* 0b010 */ },
212 { 4 /* 0b100 */ , 4 /* 0b100 */ },
213 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Jean Delvare6d6006b2007-12-02 23:33:57 +0100216static const auto_chan_table_t auto_channel_select_table_adm1030 = {
217 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
218 { 2 /* 0b10 */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 0xff /* invalid */ , 0 },
221 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224/* That function checks if a bitfield is valid and returns the other bitfield
225 * nearest match if no exact match where found.
226 */
227static int
228get_fan_auto_nearest(struct adm1031_data *data,
229 int chan, u8 val, u8 reg, u8 * new_reg)
230{
231 int i;
232 int first_match = -1, exact_match = -1;
233 u8 other_reg_val =
234 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
235
236 if (val == 0) {
237 *new_reg = 0;
238 return 0;
239 }
240
241 for (i = 0; i < 8; i++) {
242 if ((val == (*data->chan_select_table)[i][chan]) &&
243 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
244 other_reg_val)) {
245 /* We found an exact match */
246 exact_match = i;
247 break;
248 } else if (val == (*data->chan_select_table)[i][chan] &&
249 first_match == -1) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100250 /* Save the first match in case of an exact match has
251 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253 first_match = i;
254 }
255 }
256
257 if (exact_match >= 0) {
258 *new_reg = exact_match;
259 } else if (first_match >= 0) {
260 *new_reg = first_match;
261 } else {
262 return -EINVAL;
263 }
264 return 0;
265}
266
Jean Delvarec8010822007-12-02 23:39:38 +0100267static ssize_t show_fan_auto_channel(struct device *dev,
268 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Jean Delvarec8010822007-12-02 23:39:38 +0100270 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct adm1031_data *data = adm1031_update_device(dev);
272 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
273}
274
275static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100276set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct i2c_client *client = to_i2c_client(dev);
280 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100281 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int val = simple_strtol(buf, NULL, 10);
283 u8 reg;
284 int ret;
285 u8 old_fan_mode;
286
287 old_fan_mode = data->conf1;
288
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100289 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100292 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return ret;
294 }
Jean Delvare6d6006b2007-12-02 23:33:57 +0100295 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
296 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
298 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
Jean Delvare6d6006b2007-12-02 23:33:57 +0100299 /* Switch to Auto Fan Mode
300 * Save PWM registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * Set PWM registers to 33% Both */
302 data->old_pwm[0] = data->pwm[0];
303 data->old_pwm[1] = data->pwm[1];
304 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
305 } else {
306 /* Switch to Manual Mode */
307 data->pwm[0] = data->old_pwm[0];
308 data->pwm[1] = data->old_pwm[1];
309 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100310 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 data->pwm[0] | (data->pwm[1] << 4));
312 }
313 }
314 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
315 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100316 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return count;
318}
319
Jean Delvarec8010822007-12-02 23:39:38 +0100320static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
321 show_fan_auto_channel, set_fan_auto_channel, 0);
322static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
323 show_fan_auto_channel, set_fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/* Auto Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100326static ssize_t show_auto_temp_off(struct device *dev,
327 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Jean Delvarec8010822007-12-02 23:39:38 +0100329 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100331 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
333}
Jean Delvarec8010822007-12-02 23:39:38 +0100334static ssize_t show_auto_temp_min(struct device *dev,
335 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Jean Delvarec8010822007-12-02 23:39:38 +0100337 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct adm1031_data *data = adm1031_update_device(dev);
339 return sprintf(buf, "%d\n",
340 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
341}
342static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100343set_auto_temp_min(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct i2c_client *client = to_i2c_client(dev);
347 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100348 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int val = simple_strtol(buf, NULL, 10);
350
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100351 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
353 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
354 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100355 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return count;
357}
Jean Delvarec8010822007-12-02 23:39:38 +0100358static ssize_t show_auto_temp_max(struct device *dev,
359 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jean Delvarec8010822007-12-02 23:39:38 +0100361 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct adm1031_data *data = adm1031_update_device(dev);
363 return sprintf(buf, "%d\n",
364 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
365}
366static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100367set_auto_temp_max(struct device *dev, struct device_attribute *attr,
368 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct i2c_client *client = to_i2c_client(dev);
371 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100372 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int val = simple_strtol(buf, NULL, 10);
374
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100375 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
377 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
378 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return count;
381}
382
Jean Delvarec8010822007-12-02 23:39:38 +0100383#define auto_temp_reg(offset) \
384static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
385 show_auto_temp_off, NULL, offset - 1); \
386static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
387 show_auto_temp_min, set_auto_temp_min, offset - 1); \
388static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
389 show_auto_temp_max, set_auto_temp_max, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391auto_temp_reg(1);
392auto_temp_reg(2);
393auto_temp_reg(3);
394
395/* pwm */
Jean Delvarec8010822007-12-02 23:39:38 +0100396static ssize_t show_pwm(struct device *dev,
397 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jean Delvarec8010822007-12-02 23:39:38 +0100399 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct adm1031_data *data = adm1031_update_device(dev);
401 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
402}
Jean Delvarec8010822007-12-02 23:39:38 +0100403static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct i2c_client *client = to_i2c_client(dev);
407 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100408 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int val = simple_strtol(buf, NULL, 10);
410 int reg;
411
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100412 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100413 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 (((val>>4) & 0xf) != 5)) {
415 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVAL;
418 }
419 data->pwm[nr] = PWM_TO_REG(val);
420 reg = adm1031_read_value(client, ADM1031_REG_PWM);
421 adm1031_write_value(client, ADM1031_REG_PWM,
422 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
423 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100424 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return count;
426}
427
Jean Delvarec8010822007-12-02 23:39:38 +0100428static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
429static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
430static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
431 show_pwm, set_pwm, 0);
432static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
433 show_pwm, set_pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435/* Fans */
436
437/*
438 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600439 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * not supposed to run
441 */
442static int trust_fan_readings(struct adm1031_data *data, int chan)
443{
444 int res = 0;
445
446 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
447 switch (data->conf1 & 0x60) {
448 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
449 res = data->temp[chan+1] >=
450 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
451 break;
452 case 0x20: /* remote temp1 controls both fans */
453 res =
454 data->temp[1] >=
455 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
456 break;
457 case 0x40: /* remote temp2 controls both fans */
458 res =
459 data->temp[2] >=
460 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
461 break;
462 case 0x60: /* max controls both fans */
463 res =
464 data->temp[0] >=
465 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
466 || data->temp[1] >=
467 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100468 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 && data->temp[2] >=
470 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
471 break;
472 }
473 } else {
474 res = data->pwm[chan] > 0;
475 }
476 return res;
477}
478
479
Jean Delvarec8010822007-12-02 23:39:38 +0100480static ssize_t show_fan(struct device *dev,
481 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jean Delvarec8010822007-12-02 23:39:38 +0100483 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct adm1031_data *data = adm1031_update_device(dev);
485 int value;
486
487 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
488 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
489 return sprintf(buf, "%d\n", value);
490}
491
Jean Delvarec8010822007-12-02 23:39:38 +0100492static ssize_t show_fan_div(struct device *dev,
493 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jean Delvarec8010822007-12-02 23:39:38 +0100495 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct adm1031_data *data = adm1031_update_device(dev);
497 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
498}
Jean Delvarec8010822007-12-02 23:39:38 +0100499static ssize_t show_fan_min(struct device *dev,
500 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jean Delvarec8010822007-12-02 23:39:38 +0100502 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct adm1031_data *data = adm1031_update_device(dev);
504 return sprintf(buf, "%d\n",
505 FAN_FROM_REG(data->fan_min[nr],
506 FAN_DIV_FROM_REG(data->fan_div[nr])));
507}
Jean Delvarec8010822007-12-02 23:39:38 +0100508static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct i2c_client *client = to_i2c_client(dev);
512 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100513 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int val = simple_strtol(buf, NULL, 10);
515
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100516 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100518 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
520 } else {
521 data->fan_min[nr] = 0xff;
522 }
523 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100524 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return count;
526}
Jean Delvarec8010822007-12-02 23:39:38 +0100527static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
528 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct i2c_client *client = to_i2c_client(dev);
531 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100532 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int val = simple_strtol(buf, NULL, 10);
534 u8 tmp;
535 int old_div;
536 int new_min;
537
538 tmp = val == 8 ? 0xc0 :
539 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100540 val == 2 ? 0x40 :
541 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 0xff;
543 if (tmp == 0xff)
544 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100545
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100546 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100547 /* Get fresh readings */
548 data->fan_div[nr] = adm1031_read_value(client,
549 ADM1031_REG_FAN_DIV(nr));
550 data->fan_min[nr] = adm1031_read_value(client,
551 ADM1031_REG_FAN_MIN(nr));
552
553 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100555 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
556 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jean Delvare6d6006b2007-12-02 23:33:57 +0100559 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100561 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100563
564 /* Invalidate the cache: fan speed is no longer valid */
565 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100566 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return count;
568}
569
570#define fan_offset(offset) \
Jean Delvarec8010822007-12-02 23:39:38 +0100571static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
572 show_fan, NULL, offset - 1); \
573static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
574 show_fan_min, set_fan_min, offset - 1); \
575static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
576 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578fan_offset(1);
579fan_offset(2);
580
581
582/* Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100583static ssize_t show_temp(struct device *dev,
584 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jean Delvarec8010822007-12-02 23:39:38 +0100586 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct adm1031_data *data = adm1031_update_device(dev);
588 int ext;
589 ext = nr == 0 ?
590 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
591 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
592 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
593}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200594static ssize_t show_temp_offset(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
597 int nr = to_sensor_dev_attr(attr)->index;
598 struct adm1031_data *data = adm1031_update_device(dev);
599 return sprintf(buf, "%d\n",
600 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
601}
Jean Delvarec8010822007-12-02 23:39:38 +0100602static ssize_t show_temp_min(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_min[nr]));
608}
Jean Delvarec8010822007-12-02 23:39:38 +0100609static ssize_t show_temp_max(struct device *dev,
610 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Jean Delvarec8010822007-12-02 23:39:38 +0100612 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct adm1031_data *data = adm1031_update_device(dev);
614 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
615}
Jean Delvarec8010822007-12-02 23:39:38 +0100616static ssize_t show_temp_crit(struct device *dev,
617 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Jean Delvarec8010822007-12-02 23:39:38 +0100619 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct adm1031_data *data = adm1031_update_device(dev);
621 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
622}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200623static ssize_t set_temp_offset(struct device *dev,
624 struct device_attribute *attr, const char *buf,
625 size_t count)
626{
627 struct i2c_client *client = to_i2c_client(dev);
628 struct adm1031_data *data = i2c_get_clientdata(client);
629 int nr = to_sensor_dev_attr(attr)->index;
630 int val;
631
632 val = simple_strtol(buf, NULL, 10);
633 val = SENSORS_LIMIT(val, -15000, 15000);
634 mutex_lock(&data->update_lock);
635 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
636 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
637 data->temp_offset[nr]);
638 mutex_unlock(&data->update_lock);
639 return count;
640}
Jean Delvarec8010822007-12-02 23:39:38 +0100641static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
642 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct i2c_client *client = to_i2c_client(dev);
645 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100646 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int val;
648
649 val = simple_strtol(buf, NULL, 10);
650 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 data->temp_min[nr] = TEMP_TO_REG(val);
653 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
654 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100655 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return count;
657}
Jean Delvarec8010822007-12-02 23:39:38 +0100658static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct i2c_client *client = to_i2c_client(dev);
662 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100663 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int val;
665
666 val = simple_strtol(buf, NULL, 10);
667 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100668 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 data->temp_max[nr] = TEMP_TO_REG(val);
670 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
671 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100672 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return count;
674}
Jean Delvarec8010822007-12-02 23:39:38 +0100675static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
676 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct i2c_client *client = to_i2c_client(dev);
679 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100680 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int val;
682
683 val = simple_strtol(buf, NULL, 10);
684 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100685 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 data->temp_crit[nr] = TEMP_TO_REG(val);
687 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
688 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100689 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return count;
691}
692
Jean Delvarec8010822007-12-02 23:39:38 +0100693#define temp_reg(offset) \
694static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
695 show_temp, NULL, offset - 1); \
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200696static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
697 show_temp_offset, set_temp_offset, offset - 1); \
Jean Delvarec8010822007-12-02 23:39:38 +0100698static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
699 show_temp_min, set_temp_min, offset - 1); \
700static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
701 show_temp_max, set_temp_max, offset - 1); \
702static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
703 show_temp_crit, set_temp_crit, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705temp_reg(1);
706temp_reg(2);
707temp_reg(3);
708
709/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400710static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct adm1031_data *data = adm1031_update_device(dev);
713 return sprintf(buf, "%d\n", data->alarm);
714}
715
716static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
717
Jean Delvare050ab872007-12-02 23:42:24 +0100718static ssize_t show_alarm(struct device *dev,
719 struct device_attribute *attr, char *buf)
720{
721 int bitnr = to_sensor_dev_attr(attr)->index;
722 struct adm1031_data *data = adm1031_update_device(dev);
723 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
724}
725
726static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
727static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
728static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
729static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
730static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
731static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
732static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
733static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
734static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
735static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
736static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
737static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
738static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
739static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
740static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200742static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100743 &sensor_dev_attr_fan1_input.dev_attr.attr,
744 &sensor_dev_attr_fan1_div.dev_attr.attr,
745 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100746 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
747 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100748 &sensor_dev_attr_pwm1.dev_attr.attr,
749 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
750 &sensor_dev_attr_temp1_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200751 &sensor_dev_attr_temp1_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100752 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100753 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100754 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100755 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100756 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100757 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100758 &sensor_dev_attr_temp2_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200759 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100760 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100761 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100762 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100763 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100764 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100765 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
766 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200767
Jean Delvarec8010822007-12-02 23:39:38 +0100768 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
769 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
770 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200771
Jean Delvarec8010822007-12-02 23:39:38 +0100772 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
773 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
774 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200775
Jean Delvarec8010822007-12-02 23:39:38 +0100776 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200777
778 &dev_attr_alarms.attr,
779
780 NULL
781};
782
783static const struct attribute_group adm1031_group = {
784 .attrs = adm1031_attributes,
785};
786
787static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100788 &sensor_dev_attr_fan2_input.dev_attr.attr,
789 &sensor_dev_attr_fan2_div.dev_attr.attr,
790 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100791 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
792 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100793 &sensor_dev_attr_pwm2.dev_attr.attr,
794 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
795 &sensor_dev_attr_temp3_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200796 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100797 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100798 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100799 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100800 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100801 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100802 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
803 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100804 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
805 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
806 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
807 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200808 NULL
809};
810
811static const struct attribute_group adm1031_group_opt = {
812 .attrs = adm1031_attributes_opt,
813};
814
Jean Delvareaf200f82008-07-16 19:30:09 +0200815/* Return 0 if detection is successful, -ENODEV otherwise */
816static int adm1031_detect(struct i2c_client *client, int kind,
817 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Jean Delvareaf200f82008-07-16 19:30:09 +0200819 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 const char *name = "";
821
822 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareaf200f82008-07-16 19:30:09 +0200823 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (kind < 0) {
826 int id, co;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100827 id = i2c_smbus_read_byte_data(client, 0x3d);
828 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (!((id == 0x31 || id == 0x30) && co == 0x41))
Jean Delvareaf200f82008-07-16 19:30:09 +0200831 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 kind = (id == 0x30) ? adm1030 : adm1031;
833 }
834
835 if (kind <= 0)
836 kind = adm1031;
837
838 /* Given the detected chip type, set the chip name and the
839 * auto fan control helper table. */
840 if (kind == adm1030) {
841 name = "adm1030";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else if (kind == adm1031) {
843 name = "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Jean Delvareaf200f82008-07-16 19:30:09 +0200845 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jean Delvareaf200f82008-07-16 19:30:09 +0200847 return 0;
848}
849
850static int adm1031_probe(struct i2c_client *client,
851 const struct i2c_device_id *id)
852{
853 struct adm1031_data *data;
854 int err;
855
856 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
857 if (!data) {
858 err = -ENOMEM;
859 goto exit;
860 }
861
862 i2c_set_clientdata(client, data);
863 data->chip_type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100864 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jean Delvareaf200f82008-07-16 19:30:09 +0200866 if (data->chip_type == adm1030)
867 data->chan_select_table = &auto_channel_select_table_adm1030;
868 else
869 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100872 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Register sysfs hooks */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100875 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
Jean Delvareaf200f82008-07-16 19:30:09 +0200876 goto exit_free;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200877
Jean Delvareaf200f82008-07-16 19:30:09 +0200878 if (data->chip_type == adm1031) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100879 if ((err = sysfs_create_group(&client->dev.kobj,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200880 &adm1031_group_opt)))
881 goto exit_remove;
882 }
883
Jean Delvare6d6006b2007-12-02 23:33:57 +0100884 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700885 if (IS_ERR(data->hwmon_dev)) {
886 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200887 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
890 return 0;
891
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200892exit_remove:
Jean Delvare6d6006b2007-12-02 23:33:57 +0100893 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
894 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400896 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897exit:
898 return err;
899}
900
Jean Delvareaf200f82008-07-16 19:30:09 +0200901static int adm1031_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400903 struct adm1031_data *data = i2c_get_clientdata(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400904
Tony Jones1beeffe2007-08-20 13:46:20 -0700905 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200906 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
907 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400908 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910}
911
912static void adm1031_init_client(struct i2c_client *client)
913{
914 unsigned int read_val;
915 unsigned int mask;
916 struct adm1031_data *data = i2c_get_clientdata(client);
917
918 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
919 if (data->chip_type == adm1031) {
920 mask |= (ADM1031_CONF2_PWM2_ENABLE |
921 ADM1031_CONF2_TACH2_ENABLE);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* Initialize the ADM1031 chip (enables fan speed reading ) */
924 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
925 if ((read_val | mask) != read_val) {
926 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
927 }
928
929 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
930 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
931 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
932 ADM1031_CONF1_MONITOR_ENABLE);
933 }
934
935}
936
937static struct adm1031_data *adm1031_update_device(struct device *dev)
938{
939 struct i2c_client *client = to_i2c_client(dev);
940 struct adm1031_data *data = i2c_get_clientdata(client);
941 int chan;
942
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100943 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
946 || !data->valid) {
947
948 dev_dbg(&client->dev, "Starting adm1031 update\n");
949 for (chan = 0;
950 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
951 u8 oldh, newh;
952
953 oldh =
954 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
955 data->ext_temp[chan] =
956 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
957 newh =
958 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
959 if (newh != oldh) {
960 data->ext_temp[chan] =
961 adm1031_read_value(client,
962 ADM1031_REG_EXT_TEMP);
963#ifdef DEBUG
964 oldh =
965 adm1031_read_value(client,
966 ADM1031_REG_TEMP(chan));
967
968 /* oldh is actually newer */
969 if (newh != oldh)
970 dev_warn(&client->dev,
971 "Remote temperature may be "
972 "wrong.\n");
973#endif
974 }
975 data->temp[chan] = newh;
976
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200977 data->temp_offset[chan] =
978 adm1031_read_value(client,
979 ADM1031_REG_TEMP_OFFSET(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 data->temp_min[chan] =
981 adm1031_read_value(client,
982 ADM1031_REG_TEMP_MIN(chan));
983 data->temp_max[chan] =
984 adm1031_read_value(client,
985 ADM1031_REG_TEMP_MAX(chan));
986 data->temp_crit[chan] =
987 adm1031_read_value(client,
988 ADM1031_REG_TEMP_CRIT(chan));
989 data->auto_temp[chan] =
990 adm1031_read_value(client,
991 ADM1031_REG_AUTO_TEMP(chan));
992
993 }
994
995 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
996 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
997
998 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
999 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
1000 << 8);
1001 if (data->chip_type == adm1030) {
1002 data->alarm &= 0xc0ff;
1003 }
Jean Delvare6d6006b2007-12-02 23:33:57 +01001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
1006 data->fan_div[chan] =
1007 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
1008 data->fan_min[chan] =
1009 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
1010 data->fan[chan] =
1011 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
1012 data->pwm[chan] =
Jean Delvare6d6006b2007-12-02 23:33:57 +01001013 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 (4*chan));
1015 }
1016 data->last_updated = jiffies;
1017 data->valid = 1;
1018 }
1019
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001020 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return data;
1023}
1024
1025static int __init sensors_adm1031_init(void)
1026{
1027 return i2c_add_driver(&adm1031_driver);
1028}
1029
1030static void __exit sensors_adm1031_exit(void)
1031{
1032 i2c_del_driver(&adm1031_driver);
1033}
1034
1035MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1036MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1037MODULE_LICENSE("GPL");
1038
1039module_init(sensors_adm1031_init);
1040module_exit(sensors_adm1031_exit);