blob: 44e1fd7f3d81f652ce70da1ba6d4da3890414c47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeckfbb66702012-01-19 11:02:14 -08002 * 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>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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))
Jean Delvare87c33da2010-05-27 19:58:46 +020039#define ADM1031_REG_FAN_FILTER (0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ira Snyder49dc9ef2009-09-23 22:59:41 +020041#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010042#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jean Delvare6d6006b2007-12-02 23:33:57 +010046#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48
49#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50
Jean Delvare6d6006b2007-12-02 23:33:57 +010051#define ADM1031_REG_CONF1 0x00
52#define ADM1031_REG_CONF2 0x01
53#define ADM1031_REG_EXT_TEMP 0x06
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
56#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
57#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
58
59#define ADM1031_CONF2_PWM1_ENABLE 0x01
60#define ADM1031_CONF2_PWM2_ENABLE 0x02
61#define ADM1031_CONF2_TACH1_ENABLE 0x04
62#define ADM1031_CONF2_TACH2_ENABLE 0x08
63#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64
Jean Delvare87c33da2010-05-27 19:58:46 +020065#define ADM1031_UPDATE_RATE_MASK 0x1c
66#define ADM1031_UPDATE_RATE_SHIFT 2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050069static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jean Delvaree5e9f442009-12-14 21:17:27 +010071enum chips { adm1030, adm1031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73typedef u8 auto_chan_table_t[8][2];
74
75/* Each client has this additional data */
76struct adm1031_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070077 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010078 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int chip_type;
80 char valid; /* !=0 if following fields are valid */
81 unsigned long last_updated; /* In jiffies */
Guenter Roecka51b9942010-09-17 17:24:14 +020082 unsigned int update_interval; /* In milliseconds */
Guenter Roeckfbb66702012-01-19 11:02:14 -080083 /*
84 * The chan_select_table contains the possible configurations for
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * auto fan control.
86 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010087 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 u16 alarm;
89 u8 conf1;
90 u8 conf2;
91 u8 fan[2];
92 u8 fan_div[2];
93 u8 fan_min[2];
94 u8 pwm[2];
95 u8 old_pwm[2];
96 s8 temp[3];
97 u8 ext_temp[3];
98 u8 auto_temp[3];
99 u8 auto_temp_min[3];
100 u8 auto_temp_off[3];
101 u8 auto_temp_max[3];
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200102 s8 temp_offset[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 s8 temp_min[3];
104 s8 temp_max[3];
105 s8 temp_crit[3];
106};
107
Jean Delvareaf200f82008-07-16 19:30:09 +0200108static int adm1031_probe(struct i2c_client *client,
109 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100110static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200111 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void adm1031_init_client(struct i2c_client *client);
Jean Delvareaf200f82008-07-16 19:30:09 +0200113static int adm1031_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static struct adm1031_data *adm1031_update_device(struct device *dev);
115
Jean Delvareaf200f82008-07-16 19:30:09 +0200116static const struct i2c_device_id adm1031_id[] = {
117 { "adm1030", adm1030 },
118 { "adm1031", adm1031 },
119 { }
120};
121MODULE_DEVICE_TABLE(i2c, adm1031_id);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* This is the driver that will be inserted */
124static struct i2c_driver adm1031_driver = {
Jean Delvareaf200f82008-07-16 19:30:09 +0200125 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100126 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100127 .name = "adm1031",
128 },
Jean Delvareaf200f82008-07-16 19:30:09 +0200129 .probe = adm1031_probe,
130 .remove = adm1031_remove,
131 .id_table = adm1031_id,
132 .detect = adm1031_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100133 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
137{
138 return i2c_smbus_read_byte_data(client, reg);
139}
140
141static inline int
142adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
143{
144 return i2c_smbus_write_byte_data(client, reg, value);
145}
146
147
148#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
149 ((val + 500) / 1000)))
150
151#define TEMP_FROM_REG(val) ((val) * 1000)
152
153#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
154
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200155#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
156#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
157 (val) | 0x70 : (val))
158
Guenter Roeck1c720092012-01-16 22:51:48 +0100159#define FAN_FROM_REG(reg, div) ((reg) ? \
160 (11250 * 60) / ((reg) * (div)) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static int FAN_TO_REG(int reg, int div)
163{
164 int tmp;
165 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
166 return tmp > 255 ? 255 : tmp;
167}
168
169#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
170
171#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
172#define PWM_FROM_REG(val) ((val) << 4)
173
174#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
175#define FAN_CHAN_TO_REG(val, reg) \
176 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
177
178#define AUTO_TEMP_MIN_TO_REG(val, reg) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100179 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
180#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
182
183#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
184
185#define AUTO_TEMP_OFF_FROM_REG(reg) \
186 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
187
188#define AUTO_TEMP_MAX_FROM_REG(reg) \
189 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
190 AUTO_TEMP_MIN_FROM_REG(reg))
191
192static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
193{
194 int ret;
195 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
196
197 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
198 ret = ((reg & 0xf8) |
199 (range < 10000 ? 0 :
200 range < 20000 ? 1 :
201 range < 40000 ? 2 : range < 80000 ? 3 : 4));
202 return ret;
203}
204
205/* FAN auto control */
206#define GET_FAN_AUTO_BITFIELD(data, idx) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100207 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Guenter Roeckfbb66702012-01-19 11:02:14 -0800209/*
210 * The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * control bitfields. the index in the table is the register value.
212 * MSb is the auto fan control enable bit, so the four first entries
213 * in the table disables auto fan control when both bitfields are zero.
214 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100215static const auto_chan_table_t auto_channel_select_table_adm1031 = {
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
217 { 2 /* 0b010 */ , 4 /* 0b100 */ },
218 { 2 /* 0b010 */ , 2 /* 0b010 */ },
219 { 4 /* 0b100 */ , 4 /* 0b100 */ },
220 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Jean Delvare6d6006b2007-12-02 23:33:57 +0100223static const auto_chan_table_t auto_channel_select_table_adm1030 = {
224 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
225 { 2 /* 0b10 */ , 0 },
226 { 0xff /* invalid */ , 0 },
227 { 0xff /* invalid */ , 0 },
228 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Guenter Roeckfbb66702012-01-19 11:02:14 -0800231/*
232 * That function checks if a bitfield is valid and returns the other bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * nearest match if no exact match where found.
234 */
235static int
Guenter Roeckce15a812012-03-24 08:51:05 -0700236get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int i;
239 int first_match = -1, exact_match = -1;
240 u8 other_reg_val =
241 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
242
Guenter Roeckce15a812012-03-24 08:51:05 -0700243 if (val == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 for (i = 0; i < 8; i++) {
247 if ((val == (*data->chan_select_table)[i][chan]) &&
248 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
249 other_reg_val)) {
250 /* We found an exact match */
251 exact_match = i;
252 break;
253 } else if (val == (*data->chan_select_table)[i][chan] &&
254 first_match == -1) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800255 /*
256 * Save the first match in case of an exact match has
Jean Delvare6d6006b2007-12-02 23:33:57 +0100257 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
259 first_match = i;
260 }
261 }
262
Guenter Roeck1c720092012-01-16 22:51:48 +0100263 if (exact_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700264 return exact_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100265 else if (first_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700266 return first_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100267
Guenter Roeckce15a812012-03-24 08:51:05 -0700268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Jean Delvarec8010822007-12-02 23:39:38 +0100271static ssize_t show_fan_auto_channel(struct device *dev,
272 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Jean Delvarec8010822007-12-02 23:39:38 +0100274 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct adm1031_data *data = adm1031_update_device(dev);
276 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
277}
278
279static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100280set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
281 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct i2c_client *client = to_i2c_client(dev);
284 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100285 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100286 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u8 reg;
288 int ret;
289 u8 old_fan_mode;
290
Guenter Roeck1c720092012-01-16 22:51:48 +0100291 ret = kstrtol(buf, 10, &val);
292 if (ret)
293 return ret;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 old_fan_mode = data->conf1;
296
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100297 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100298
Guenter Roeckce15a812012-03-24 08:51:05 -0700299 ret = get_fan_auto_nearest(data, nr, val, data->conf1);
300 if (ret < 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100301 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return ret;
303 }
Guenter Roeckce15a812012-03-24 08:51:05 -0700304 reg = ret;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100305 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
306 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100308 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800309 /*
310 * Switch to Auto Fan Mode
Jean Delvare6d6006b2007-12-02 23:33:57 +0100311 * Save PWM registers
Guenter Roeckfbb66702012-01-19 11:02:14 -0800312 * Set PWM registers to 33% Both
313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 data->old_pwm[0] = data->pwm[0];
315 data->old_pwm[1] = data->pwm[1];
316 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
317 } else {
318 /* Switch to Manual Mode */
319 data->pwm[0] = data->old_pwm[0];
320 data->pwm[1] = data->old_pwm[1];
321 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100322 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 data->pwm[0] | (data->pwm[1] << 4));
324 }
325 }
326 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
327 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100328 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return count;
330}
331
Jean Delvarec8010822007-12-02 23:39:38 +0100332static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
333 show_fan_auto_channel, set_fan_auto_channel, 0);
334static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
335 show_fan_auto_channel, set_fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/* Auto Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100338static ssize_t show_auto_temp_off(struct device *dev,
339 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Jean Delvarec8010822007-12-02 23:39:38 +0100341 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100343 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
345}
Jean Delvarec8010822007-12-02 23:39:38 +0100346static ssize_t show_auto_temp_min(struct device *dev,
347 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Jean Delvarec8010822007-12-02 23:39:38 +0100349 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct adm1031_data *data = adm1031_update_device(dev);
351 return sprintf(buf, "%d\n",
352 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
353}
354static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100355set_auto_temp_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100360 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100361 long val;
362 int ret;
363
364 ret = kstrtol(buf, 10, &val);
365 if (ret)
366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
370 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
371 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100372 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return count;
374}
Jean Delvarec8010822007-12-02 23:39:38 +0100375static ssize_t show_auto_temp_max(struct device *dev,
376 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jean Delvarec8010822007-12-02 23:39:38 +0100378 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct adm1031_data *data = adm1031_update_device(dev);
380 return sprintf(buf, "%d\n",
381 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
382}
383static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100384set_auto_temp_max(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100389 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100390 long val;
391 int ret;
392
393 ret = kstrtol(buf, 10, &val);
394 if (ret)
395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100397 mutex_lock(&data->update_lock);
Guenter Roeck1c720092012-01-16 22:51:48 +0100398 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
399 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
401 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return count;
404}
405
Jean Delvarec8010822007-12-02 23:39:38 +0100406#define auto_temp_reg(offset) \
407static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
408 show_auto_temp_off, NULL, offset - 1); \
409static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
410 show_auto_temp_min, set_auto_temp_min, offset - 1); \
411static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_auto_temp_max, set_auto_temp_max, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414auto_temp_reg(1);
415auto_temp_reg(2);
416auto_temp_reg(3);
417
418/* pwm */
Jean Delvarec8010822007-12-02 23:39:38 +0100419static ssize_t show_pwm(struct device *dev,
420 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jean Delvarec8010822007-12-02 23:39:38 +0100422 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct adm1031_data *data = adm1031_update_device(dev);
424 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
425}
Jean Delvarec8010822007-12-02 23:39:38 +0100426static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
427 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct i2c_client *client = to_i2c_client(dev);
430 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100431 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100432 long val;
433 int ret, reg;
434
435 ret = kstrtol(buf, 10, &val);
436 if (ret)
437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100439 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100440 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 (((val>>4) & 0xf) != 5)) {
442 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EINVAL;
445 }
446 data->pwm[nr] = PWM_TO_REG(val);
447 reg = adm1031_read_value(client, ADM1031_REG_PWM);
448 adm1031_write_value(client, ADM1031_REG_PWM,
449 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
450 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return count;
453}
454
Jean Delvarec8010822007-12-02 23:39:38 +0100455static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
456static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
457static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
458 show_pwm, set_pwm, 0);
459static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
460 show_pwm, set_pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462/* Fans */
463
464/*
465 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600466 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * not supposed to run
468 */
469static int trust_fan_readings(struct adm1031_data *data, int chan)
470{
471 int res = 0;
472
473 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
474 switch (data->conf1 & 0x60) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100475 case 0x00:
476 /*
477 * remote temp1 controls fan1,
478 * remote temp2 controls fan2
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 res = data->temp[chan+1] >=
Guenter Roeck1c720092012-01-16 22:51:48 +0100481 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case 0x20: /* remote temp1 controls both fans */
484 res =
485 data->temp[1] >=
486 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
487 break;
488 case 0x40: /* remote temp2 controls both fans */
489 res =
490 data->temp[2] >=
491 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
492 break;
493 case 0x60: /* max controls both fans */
494 res =
495 data->temp[0] >=
496 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
497 || data->temp[1] >=
498 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100499 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 && data->temp[2] >=
501 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
502 break;
503 }
504 } else {
505 res = data->pwm[chan] > 0;
506 }
507 return res;
508}
509
510
Jean Delvarec8010822007-12-02 23:39:38 +0100511static ssize_t show_fan(struct device *dev,
512 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Jean Delvarec8010822007-12-02 23:39:38 +0100514 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct adm1031_data *data = adm1031_update_device(dev);
516 int value;
517
518 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
519 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
520 return sprintf(buf, "%d\n", value);
521}
522
Jean Delvarec8010822007-12-02 23:39:38 +0100523static ssize_t show_fan_div(struct device *dev,
524 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Jean Delvarec8010822007-12-02 23:39:38 +0100526 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct adm1031_data *data = adm1031_update_device(dev);
528 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
529}
Jean Delvarec8010822007-12-02 23:39:38 +0100530static ssize_t show_fan_min(struct device *dev,
531 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jean Delvarec8010822007-12-02 23:39:38 +0100533 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct adm1031_data *data = adm1031_update_device(dev);
535 return sprintf(buf, "%d\n",
536 FAN_FROM_REG(data->fan_min[nr],
537 FAN_DIV_FROM_REG(data->fan_div[nr])));
538}
Jean Delvarec8010822007-12-02 23:39:38 +0100539static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct i2c_client *client = to_i2c_client(dev);
543 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100544 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100545 long val;
546 int ret;
547
548 ret = kstrtol(buf, 10, &val);
549 if (ret)
550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100552 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100554 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
556 } else {
557 data->fan_min[nr] = 0xff;
558 }
559 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100560 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return count;
562}
Jean Delvarec8010822007-12-02 23:39:38 +0100563static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct i2c_client *client = to_i2c_client(dev);
567 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100568 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100569 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 u8 tmp;
571 int old_div;
572 int new_min;
Guenter Roeck1c720092012-01-16 22:51:48 +0100573 int ret;
574
575 ret = kstrtol(buf, 10, &val);
576 if (ret)
577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 tmp = val == 8 ? 0xc0 :
580 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100581 val == 2 ? 0x40 :
582 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 0xff;
584 if (tmp == 0xff)
585 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100586
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100588 /* Get fresh readings */
589 data->fan_div[nr] = adm1031_read_value(client,
590 ADM1031_REG_FAN_DIV(nr));
591 data->fan_min[nr] = adm1031_read_value(client,
592 ADM1031_REG_FAN_MIN(nr));
593
594 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100596 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
597 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jean Delvare6d6006b2007-12-02 23:33:57 +0100600 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100602 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100604
605 /* Invalidate the cache: fan speed is no longer valid */
606 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100607 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return count;
609}
610
611#define fan_offset(offset) \
Jean Delvarec8010822007-12-02 23:39:38 +0100612static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
613 show_fan, NULL, offset - 1); \
614static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
615 show_fan_min, set_fan_min, offset - 1); \
616static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
617 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619fan_offset(1);
620fan_offset(2);
621
622
623/* Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100624static ssize_t show_temp(struct device *dev,
625 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Jean Delvarec8010822007-12-02 23:39:38 +0100627 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct adm1031_data *data = adm1031_update_device(dev);
629 int ext;
630 ext = nr == 0 ?
631 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
632 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
633 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
634}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200635static ssize_t show_temp_offset(struct device *dev,
636 struct device_attribute *attr, char *buf)
637{
638 int nr = to_sensor_dev_attr(attr)->index;
639 struct adm1031_data *data = adm1031_update_device(dev);
640 return sprintf(buf, "%d\n",
641 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
642}
Jean Delvarec8010822007-12-02 23:39:38 +0100643static ssize_t show_temp_min(struct device *dev,
644 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Jean Delvarec8010822007-12-02 23:39:38 +0100646 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct adm1031_data *data = adm1031_update_device(dev);
648 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
649}
Jean Delvarec8010822007-12-02 23:39:38 +0100650static ssize_t show_temp_max(struct device *dev,
651 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jean Delvarec8010822007-12-02 23:39:38 +0100653 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct adm1031_data *data = adm1031_update_device(dev);
655 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
656}
Jean Delvarec8010822007-12-02 23:39:38 +0100657static ssize_t show_temp_crit(struct device *dev,
658 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvarec8010822007-12-02 23:39:38 +0100660 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct adm1031_data *data = adm1031_update_device(dev);
662 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
663}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200664static ssize_t set_temp_offset(struct device *dev,
665 struct device_attribute *attr, const char *buf,
666 size_t count)
667{
668 struct i2c_client *client = to_i2c_client(dev);
669 struct adm1031_data *data = i2c_get_clientdata(client);
670 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100671 long val;
672 int ret;
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200673
Guenter Roeck1c720092012-01-16 22:51:48 +0100674 ret = kstrtol(buf, 10, &val);
675 if (ret)
676 return ret;
677
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200678 val = SENSORS_LIMIT(val, -15000, 15000);
679 mutex_lock(&data->update_lock);
680 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
681 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
682 data->temp_offset[nr]);
683 mutex_unlock(&data->update_lock);
684 return count;
685}
Jean Delvarec8010822007-12-02 23:39:38 +0100686static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct i2c_client *client = to_i2c_client(dev);
690 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100691 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100692 long val;
693 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Guenter Roeck1c720092012-01-16 22:51:48 +0100695 ret = kstrtol(buf, 10, &val);
696 if (ret)
697 return ret;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100700 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 data->temp_min[nr] = TEMP_TO_REG(val);
702 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
703 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100704 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return count;
706}
Jean Delvarec8010822007-12-02 23:39:38 +0100707static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100712 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100713 long val;
714 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Guenter Roeck1c720092012-01-16 22:51:48 +0100716 ret = kstrtol(buf, 10, &val);
717 if (ret)
718 return ret;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 data->temp_max[nr] = TEMP_TO_REG(val);
723 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
724 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100725 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return count;
727}
Jean Delvarec8010822007-12-02 23:39:38 +0100728static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
729 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct i2c_client *client = to_i2c_client(dev);
732 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100733 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100734 long val;
735 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Guenter Roeck1c720092012-01-16 22:51:48 +0100737 ret = kstrtol(buf, 10, &val);
738 if (ret)
739 return ret;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100742 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 data->temp_crit[nr] = TEMP_TO_REG(val);
744 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
745 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100746 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return count;
748}
749
Jean Delvarec8010822007-12-02 23:39:38 +0100750#define temp_reg(offset) \
751static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
752 show_temp, NULL, offset - 1); \
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200753static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
754 show_temp_offset, set_temp_offset, offset - 1); \
Jean Delvarec8010822007-12-02 23:39:38 +0100755static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
756 show_temp_min, set_temp_min, offset - 1); \
757static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
758 show_temp_max, set_temp_max, offset - 1); \
759static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
760 show_temp_crit, set_temp_crit, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762temp_reg(1);
763temp_reg(2);
764temp_reg(3);
765
766/* Alarms */
Guenter Roeck1c720092012-01-16 22:51:48 +0100767static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
768 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct adm1031_data *data = adm1031_update_device(dev);
771 return sprintf(buf, "%d\n", data->alarm);
772}
773
774static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
775
Jean Delvare050ab872007-12-02 23:42:24 +0100776static ssize_t show_alarm(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 int bitnr = to_sensor_dev_attr(attr)->index;
780 struct adm1031_data *data = adm1031_update_device(dev);
781 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
782}
783
784static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
785static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
786static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
787static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
788static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
789static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
790static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
791static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
792static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
793static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
794static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
795static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
796static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
797static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
798static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Guenter Roecka51b9942010-09-17 17:24:14 +0200800/* Update Interval */
801static const unsigned int update_intervals[] = {
Jean Delvare87c33da2010-05-27 19:58:46 +0200802 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
803};
804
Guenter Roecka51b9942010-09-17 17:24:14 +0200805static ssize_t show_update_interval(struct device *dev,
806 struct device_attribute *attr, char *buf)
Jean Delvare87c33da2010-05-27 19:58:46 +0200807{
808 struct i2c_client *client = to_i2c_client(dev);
809 struct adm1031_data *data = i2c_get_clientdata(client);
810
Guenter Roecka51b9942010-09-17 17:24:14 +0200811 return sprintf(buf, "%u\n", data->update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200812}
813
Guenter Roecka51b9942010-09-17 17:24:14 +0200814static ssize_t set_update_interval(struct device *dev,
815 struct device_attribute *attr,
816 const char *buf, size_t count)
Jean Delvare87c33da2010-05-27 19:58:46 +0200817{
818 struct i2c_client *client = to_i2c_client(dev);
819 struct adm1031_data *data = i2c_get_clientdata(client);
820 unsigned long val;
821 int i, err;
822 u8 reg;
823
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100824 err = kstrtoul(buf, 10, &val);
Jean Delvare87c33da2010-05-27 19:58:46 +0200825 if (err)
826 return err;
827
Guenter Roecka51b9942010-09-17 17:24:14 +0200828 /*
829 * Find the nearest update interval from the table.
830 * Use it to determine the matching update rate.
831 */
832 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
833 if (val >= update_intervals[i])
Jean Delvare87c33da2010-05-27 19:58:46 +0200834 break;
835 }
Guenter Roecka51b9942010-09-17 17:24:14 +0200836 /* if not found, we point to the last entry (lowest update interval) */
Jean Delvare87c33da2010-05-27 19:58:46 +0200837
838 /* set the new update rate while preserving other settings */
839 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
840 reg &= ~ADM1031_UPDATE_RATE_MASK;
841 reg |= i << ADM1031_UPDATE_RATE_SHIFT;
842 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
843
844 mutex_lock(&data->update_lock);
Guenter Roecka51b9942010-09-17 17:24:14 +0200845 data->update_interval = update_intervals[i];
Jean Delvare87c33da2010-05-27 19:58:46 +0200846 mutex_unlock(&data->update_lock);
847
848 return count;
849}
850
Guenter Roecka51b9942010-09-17 17:24:14 +0200851static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
852 set_update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200853
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200854static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100855 &sensor_dev_attr_fan1_input.dev_attr.attr,
856 &sensor_dev_attr_fan1_div.dev_attr.attr,
857 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100858 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
859 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100860 &sensor_dev_attr_pwm1.dev_attr.attr,
861 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
862 &sensor_dev_attr_temp1_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200863 &sensor_dev_attr_temp1_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100864 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100865 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100866 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100867 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100868 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100869 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100870 &sensor_dev_attr_temp2_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200871 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100872 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100873 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100874 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100875 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100876 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100877 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
878 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200879
Jean Delvarec8010822007-12-02 23:39:38 +0100880 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
881 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
882 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200883
Jean Delvarec8010822007-12-02 23:39:38 +0100884 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
885 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
886 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200887
Jean Delvarec8010822007-12-02 23:39:38 +0100888 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200889
Guenter Roecka51b9942010-09-17 17:24:14 +0200890 &dev_attr_update_interval.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200891 &dev_attr_alarms.attr,
892
893 NULL
894};
895
896static const struct attribute_group adm1031_group = {
897 .attrs = adm1031_attributes,
898};
899
900static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100901 &sensor_dev_attr_fan2_input.dev_attr.attr,
902 &sensor_dev_attr_fan2_div.dev_attr.attr,
903 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100904 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
905 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100906 &sensor_dev_attr_pwm2.dev_attr.attr,
907 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
908 &sensor_dev_attr_temp3_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200909 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100910 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100911 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100912 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100913 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100914 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100915 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
916 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100917 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
918 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
919 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
920 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200921 NULL
922};
923
924static const struct attribute_group adm1031_group_opt = {
925 .attrs = adm1031_attributes_opt,
926};
927
Jean Delvareaf200f82008-07-16 19:30:09 +0200928/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100929static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200930 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Jean Delvareaf200f82008-07-16 19:30:09 +0200932 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100933 const char *name;
934 int id, co;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareaf200f82008-07-16 19:30:09 +0200937 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jean Delvare52df6442009-12-09 20:35:57 +0100939 id = i2c_smbus_read_byte_data(client, 0x3d);
940 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jean Delvare52df6442009-12-09 20:35:57 +0100942 if (!((id == 0x31 || id == 0x30) && co == 0x41))
943 return -ENODEV;
944 name = (id == 0x30) ? "adm1030" : "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jean Delvareaf200f82008-07-16 19:30:09 +0200946 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jean Delvareaf200f82008-07-16 19:30:09 +0200948 return 0;
949}
950
951static int adm1031_probe(struct i2c_client *client,
952 const struct i2c_device_id *id)
953{
954 struct adm1031_data *data;
955 int err;
956
957 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
958 if (!data) {
959 err = -ENOMEM;
960 goto exit;
961 }
962
963 i2c_set_clientdata(client, data);
964 data->chip_type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100965 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jean Delvareaf200f82008-07-16 19:30:09 +0200967 if (data->chip_type == adm1030)
968 data->chan_select_table = &auto_channel_select_table_adm1030;
969 else
970 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100973 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* Register sysfs hooks */
Guenter Roeck1c720092012-01-16 22:51:48 +0100976 err = sysfs_create_group(&client->dev.kobj, &adm1031_group);
977 if (err)
Jean Delvareaf200f82008-07-16 19:30:09 +0200978 goto exit_free;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200979
Jean Delvareaf200f82008-07-16 19:30:09 +0200980 if (data->chip_type == adm1031) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100981 err = sysfs_create_group(&client->dev.kobj, &adm1031_group_opt);
982 if (err)
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200983 goto exit_remove;
984 }
985
Jean Delvare6d6006b2007-12-02 23:33:57 +0100986 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700987 if (IS_ERR(data->hwmon_dev)) {
988 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200989 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 return 0;
993
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200994exit_remove:
Jean Delvare6d6006b2007-12-02 23:33:57 +0100995 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
996 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400998 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999exit:
1000 return err;
1001}
1002
Jean Delvareaf200f82008-07-16 19:30:09 +02001003static int adm1031_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001005 struct adm1031_data *data = i2c_get_clientdata(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001006
Tony Jones1beeffe2007-08-20 13:46:20 -07001007 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001008 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
1009 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001010 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
1012}
1013
1014static void adm1031_init_client(struct i2c_client *client)
1015{
1016 unsigned int read_val;
1017 unsigned int mask;
Jean Delvare87c33da2010-05-27 19:58:46 +02001018 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct adm1031_data *data = i2c_get_clientdata(client);
1020
1021 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
1022 if (data->chip_type == adm1031) {
1023 mask |= (ADM1031_CONF2_PWM2_ENABLE |
1024 ADM1031_CONF2_TACH2_ENABLE);
Jean Delvare6d6006b2007-12-02 23:33:57 +01001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1027 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
Guenter Roeck1c720092012-01-16 22:51:48 +01001028 if ((read_val | mask) != read_val)
1029 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
1032 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
Guenter Roeck1c720092012-01-16 22:51:48 +01001033 adm1031_write_value(client, ADM1031_REG_CONF1,
1034 read_val | ADM1031_CONF1_MONITOR_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Jean Delvare87c33da2010-05-27 19:58:46 +02001037 /* Read the chip's update rate */
1038 mask = ADM1031_UPDATE_RATE_MASK;
1039 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
1040 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
Guenter Roecka51b9942010-09-17 17:24:14 +02001041 /* Save it as update interval */
1042 data->update_interval = update_intervals[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static struct adm1031_data *adm1031_update_device(struct device *dev)
1046{
1047 struct i2c_client *client = to_i2c_client(dev);
1048 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvare87c33da2010-05-27 19:58:46 +02001049 unsigned long next_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int chan;
1051
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001052 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Guenter Roecka51b9942010-09-17 17:24:14 +02001054 next_update = data->last_updated
1055 + msecs_to_jiffies(data->update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +02001056 if (time_after(jiffies, next_update) || !data->valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 dev_dbg(&client->dev, "Starting adm1031 update\n");
1059 for (chan = 0;
1060 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
1061 u8 oldh, newh;
1062
1063 oldh =
1064 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
1065 data->ext_temp[chan] =
1066 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
1067 newh =
1068 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
1069 if (newh != oldh) {
1070 data->ext_temp[chan] =
1071 adm1031_read_value(client,
1072 ADM1031_REG_EXT_TEMP);
1073#ifdef DEBUG
1074 oldh =
1075 adm1031_read_value(client,
1076 ADM1031_REG_TEMP(chan));
1077
1078 /* oldh is actually newer */
1079 if (newh != oldh)
1080 dev_warn(&client->dev,
Guenter Roeck1c720092012-01-16 22:51:48 +01001081 "Remote temperature may be wrong.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#endif
1083 }
1084 data->temp[chan] = newh;
1085
Ira Snyder49dc9ef2009-09-23 22:59:41 +02001086 data->temp_offset[chan] =
1087 adm1031_read_value(client,
1088 ADM1031_REG_TEMP_OFFSET(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 data->temp_min[chan] =
1090 adm1031_read_value(client,
1091 ADM1031_REG_TEMP_MIN(chan));
1092 data->temp_max[chan] =
1093 adm1031_read_value(client,
1094 ADM1031_REG_TEMP_MAX(chan));
1095 data->temp_crit[chan] =
1096 adm1031_read_value(client,
1097 ADM1031_REG_TEMP_CRIT(chan));
1098 data->auto_temp[chan] =
1099 adm1031_read_value(client,
1100 ADM1031_REG_AUTO_TEMP(chan));
1101
1102 }
1103
1104 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
1105 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
1106
1107 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
Guenter Roeck1c720092012-01-16 22:51:48 +01001108 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
1109 if (data->chip_type == adm1030)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 data->alarm &= 0xc0ff;
Jean Delvare6d6006b2007-12-02 23:33:57 +01001111
Guenter Roeck1c720092012-01-16 22:51:48 +01001112 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
1113 chan++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 data->fan_div[chan] =
Guenter Roeck1c720092012-01-16 22:51:48 +01001115 adm1031_read_value(client,
1116 ADM1031_REG_FAN_DIV(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 data->fan_min[chan] =
Guenter Roeck1c720092012-01-16 22:51:48 +01001118 adm1031_read_value(client,
1119 ADM1031_REG_FAN_MIN(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 data->fan[chan] =
Guenter Roeck1c720092012-01-16 22:51:48 +01001121 adm1031_read_value(client,
1122 ADM1031_REG_FAN_SPEED(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 data->pwm[chan] =
Guenter Roeck1c720092012-01-16 22:51:48 +01001124 (adm1031_read_value(client,
1125 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 data->last_updated = jiffies;
1128 data->valid = 1;
1129 }
1130
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001131 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 return data;
1134}
1135
Axel Linf0967ee2012-01-20 15:38:18 +08001136module_i2c_driver(adm1031_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1139MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1140MODULE_LICENSE("GPL");