blob: 1644b92e7cc47ae8f8507d777cd1ecbe64612484 [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
Jean Delvaree5e9f442009-12-14 21:17:27 +010067enum chips { adm1030, adm1031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69typedef u8 auto_chan_table_t[8][2];
70
71/* Each client has this additional data */
72struct adm1031_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070073 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010074 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int chip_type;
76 char valid; /* !=0 if following fields are valid */
77 unsigned long last_updated; /* In jiffies */
78 /* The chan_select_table contains the possible configurations for
79 * auto fan control.
80 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010081 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 u16 alarm;
83 u8 conf1;
84 u8 conf2;
85 u8 fan[2];
86 u8 fan_div[2];
87 u8 fan_min[2];
88 u8 pwm[2];
89 u8 old_pwm[2];
90 s8 temp[3];
91 u8 ext_temp[3];
92 u8 auto_temp[3];
93 u8 auto_temp_min[3];
94 u8 auto_temp_off[3];
95 u8 auto_temp_max[3];
Ira Snyder49dc9ef2009-09-23 22:59:41 +020096 s8 temp_offset[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 s8 temp_min[3];
98 s8 temp_max[3];
99 s8 temp_crit[3];
100};
101
Jean Delvareaf200f82008-07-16 19:30:09 +0200102static int adm1031_probe(struct i2c_client *client,
103 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100104static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200105 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void adm1031_init_client(struct i2c_client *client);
Jean Delvareaf200f82008-07-16 19:30:09 +0200107static int adm1031_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct adm1031_data *adm1031_update_device(struct device *dev);
109
Jean Delvareaf200f82008-07-16 19:30:09 +0200110static const struct i2c_device_id adm1031_id[] = {
111 { "adm1030", adm1030 },
112 { "adm1031", adm1031 },
113 { }
114};
115MODULE_DEVICE_TABLE(i2c, adm1031_id);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* This is the driver that will be inserted */
118static struct i2c_driver adm1031_driver = {
Jean Delvareaf200f82008-07-16 19:30:09 +0200119 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100120 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100121 .name = "adm1031",
122 },
Jean Delvareaf200f82008-07-16 19:30:09 +0200123 .probe = adm1031_probe,
124 .remove = adm1031_remove,
125 .id_table = adm1031_id,
126 .detect = adm1031_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100127 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
131{
132 return i2c_smbus_read_byte_data(client, reg);
133}
134
135static inline int
136adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
137{
138 return i2c_smbus_write_byte_data(client, reg, value);
139}
140
141
142#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
143 ((val + 500) / 1000)))
144
145#define TEMP_FROM_REG(val) ((val) * 1000)
146
147#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
148
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200149#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
150#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
151 (val) | 0x70 : (val))
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
154
155static int FAN_TO_REG(int reg, int div)
156{
157 int tmp;
158 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
159 return tmp > 255 ? 255 : tmp;
160}
161
162#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
163
164#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
165#define PWM_FROM_REG(val) ((val) << 4)
166
167#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
168#define FAN_CHAN_TO_REG(val, reg) \
169 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
170
171#define AUTO_TEMP_MIN_TO_REG(val, reg) \
172 ((((val)/500) & 0xf8)|((reg) & 0x7))
173#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
174#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
175
176#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
177
178#define AUTO_TEMP_OFF_FROM_REG(reg) \
179 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
180
181#define AUTO_TEMP_MAX_FROM_REG(reg) \
182 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
183 AUTO_TEMP_MIN_FROM_REG(reg))
184
185static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
186{
187 int ret;
188 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
189
190 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
191 ret = ((reg & 0xf8) |
192 (range < 10000 ? 0 :
193 range < 20000 ? 1 :
194 range < 40000 ? 2 : range < 80000 ? 3 : 4));
195 return ret;
196}
197
198/* FAN auto control */
199#define GET_FAN_AUTO_BITFIELD(data, idx) \
200 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
201
Jean Delvare6d6006b2007-12-02 23:33:57 +0100202/* The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * control bitfields. the index in the table is the register value.
204 * MSb is the auto fan control enable bit, so the four first entries
205 * in the table disables auto fan control when both bitfields are zero.
206 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100207static const auto_chan_table_t auto_channel_select_table_adm1031 = {
208 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
209 { 2 /* 0b010 */ , 4 /* 0b100 */ },
210 { 2 /* 0b010 */ , 2 /* 0b010 */ },
211 { 4 /* 0b100 */ , 4 /* 0b100 */ },
212 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Jean Delvare6d6006b2007-12-02 23:33:57 +0100215static const auto_chan_table_t auto_channel_select_table_adm1030 = {
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
217 { 2 /* 0b10 */ , 0 },
218 { 0xff /* invalid */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
223/* That function checks if a bitfield is valid and returns the other bitfield
224 * nearest match if no exact match where found.
225 */
226static int
227get_fan_auto_nearest(struct adm1031_data *data,
228 int chan, u8 val, u8 reg, u8 * new_reg)
229{
230 int i;
231 int first_match = -1, exact_match = -1;
232 u8 other_reg_val =
233 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
234
235 if (val == 0) {
236 *new_reg = 0;
237 return 0;
238 }
239
240 for (i = 0; i < 8; i++) {
241 if ((val == (*data->chan_select_table)[i][chan]) &&
242 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
243 other_reg_val)) {
244 /* We found an exact match */
245 exact_match = i;
246 break;
247 } else if (val == (*data->chan_select_table)[i][chan] &&
248 first_match == -1) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100249 /* Save the first match in case of an exact match has
250 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252 first_match = i;
253 }
254 }
255
256 if (exact_match >= 0) {
257 *new_reg = exact_match;
258 } else if (first_match >= 0) {
259 *new_reg = first_match;
260 } else {
261 return -EINVAL;
262 }
263 return 0;
264}
265
Jean Delvarec8010822007-12-02 23:39:38 +0100266static ssize_t show_fan_auto_channel(struct device *dev,
267 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Jean Delvarec8010822007-12-02 23:39:38 +0100269 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct adm1031_data *data = adm1031_update_device(dev);
271 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
272}
273
274static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100275set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct i2c_client *client = to_i2c_client(dev);
279 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100280 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int val = simple_strtol(buf, NULL, 10);
282 u8 reg;
283 int ret;
284 u8 old_fan_mode;
285
286 old_fan_mode = data->conf1;
287
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100288 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100291 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return ret;
293 }
Jean Delvare6d6006b2007-12-02 23:33:57 +0100294 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
295 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
297 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
Jean Delvare6d6006b2007-12-02 23:33:57 +0100298 /* Switch to Auto Fan Mode
299 * Save PWM registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * Set PWM registers to 33% Both */
301 data->old_pwm[0] = data->pwm[0];
302 data->old_pwm[1] = data->pwm[1];
303 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
304 } else {
305 /* Switch to Manual Mode */
306 data->pwm[0] = data->old_pwm[0];
307 data->pwm[1] = data->old_pwm[1];
308 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100309 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 data->pwm[0] | (data->pwm[1] << 4));
311 }
312 }
313 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
314 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return count;
317}
318
Jean Delvarec8010822007-12-02 23:39:38 +0100319static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
320 show_fan_auto_channel, set_fan_auto_channel, 0);
321static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
322 show_fan_auto_channel, set_fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/* Auto Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100325static ssize_t show_auto_temp_off(struct device *dev,
326 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Jean Delvarec8010822007-12-02 23:39:38 +0100328 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100330 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
332}
Jean Delvarec8010822007-12-02 23:39:38 +0100333static ssize_t show_auto_temp_min(struct device *dev,
334 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jean Delvarec8010822007-12-02 23:39:38 +0100336 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct adm1031_data *data = adm1031_update_device(dev);
338 return sprintf(buf, "%d\n",
339 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
340}
341static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100342set_auto_temp_min(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct i2c_client *client = to_i2c_client(dev);
346 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100347 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int val = simple_strtol(buf, NULL, 10);
349
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
352 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
353 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100354 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return count;
356}
Jean Delvarec8010822007-12-02 23:39:38 +0100357static ssize_t show_auto_temp_max(struct device *dev,
358 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jean Delvarec8010822007-12-02 23:39:38 +0100360 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct adm1031_data *data = adm1031_update_device(dev);
362 return sprintf(buf, "%d\n",
363 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
364}
365static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100366set_auto_temp_max(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct i2c_client *client = to_i2c_client(dev);
370 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100371 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int val = simple_strtol(buf, NULL, 10);
373
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100374 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
376 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
377 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100378 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return count;
380}
381
Jean Delvarec8010822007-12-02 23:39:38 +0100382#define auto_temp_reg(offset) \
383static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
384 show_auto_temp_off, NULL, offset - 1); \
385static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
386 show_auto_temp_min, set_auto_temp_min, offset - 1); \
387static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
388 show_auto_temp_max, set_auto_temp_max, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390auto_temp_reg(1);
391auto_temp_reg(2);
392auto_temp_reg(3);
393
394/* pwm */
Jean Delvarec8010822007-12-02 23:39:38 +0100395static ssize_t show_pwm(struct device *dev,
396 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Jean Delvarec8010822007-12-02 23:39:38 +0100398 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct adm1031_data *data = adm1031_update_device(dev);
400 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
401}
Jean Delvarec8010822007-12-02 23:39:38 +0100402static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct i2c_client *client = to_i2c_client(dev);
406 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100407 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int val = simple_strtol(buf, NULL, 10);
409 int reg;
410
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100412 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 (((val>>4) & 0xf) != 5)) {
414 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100415 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EINVAL;
417 }
418 data->pwm[nr] = PWM_TO_REG(val);
419 reg = adm1031_read_value(client, ADM1031_REG_PWM);
420 adm1031_write_value(client, ADM1031_REG_PWM,
421 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
422 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100423 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return count;
425}
426
Jean Delvarec8010822007-12-02 23:39:38 +0100427static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
428static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
429static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
430 show_pwm, set_pwm, 0);
431static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
432 show_pwm, set_pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434/* Fans */
435
436/*
437 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600438 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * not supposed to run
440 */
441static int trust_fan_readings(struct adm1031_data *data, int chan)
442{
443 int res = 0;
444
445 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
446 switch (data->conf1 & 0x60) {
447 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
448 res = data->temp[chan+1] >=
449 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
450 break;
451 case 0x20: /* remote temp1 controls both fans */
452 res =
453 data->temp[1] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
455 break;
456 case 0x40: /* remote temp2 controls both fans */
457 res =
458 data->temp[2] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
460 break;
461 case 0x60: /* max controls both fans */
462 res =
463 data->temp[0] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
465 || data->temp[1] >=
466 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100467 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 && data->temp[2] >=
469 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
470 break;
471 }
472 } else {
473 res = data->pwm[chan] > 0;
474 }
475 return res;
476}
477
478
Jean Delvarec8010822007-12-02 23:39:38 +0100479static ssize_t show_fan(struct device *dev,
480 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jean Delvarec8010822007-12-02 23:39:38 +0100482 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct adm1031_data *data = adm1031_update_device(dev);
484 int value;
485
486 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
487 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
488 return sprintf(buf, "%d\n", value);
489}
490
Jean Delvarec8010822007-12-02 23:39:38 +0100491static ssize_t show_fan_div(struct device *dev,
492 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jean Delvarec8010822007-12-02 23:39:38 +0100494 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct adm1031_data *data = adm1031_update_device(dev);
496 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
497}
Jean Delvarec8010822007-12-02 23:39:38 +0100498static ssize_t show_fan_min(struct device *dev,
499 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jean Delvarec8010822007-12-02 23:39:38 +0100501 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct adm1031_data *data = adm1031_update_device(dev);
503 return sprintf(buf, "%d\n",
504 FAN_FROM_REG(data->fan_min[nr],
505 FAN_DIV_FROM_REG(data->fan_div[nr])));
506}
Jean Delvarec8010822007-12-02 23:39:38 +0100507static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
508 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct i2c_client *client = to_i2c_client(dev);
511 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100512 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int val = simple_strtol(buf, NULL, 10);
514
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100515 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100517 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
519 } else {
520 data->fan_min[nr] = 0xff;
521 }
522 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100523 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return count;
525}
Jean Delvarec8010822007-12-02 23:39:38 +0100526static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct i2c_client *client = to_i2c_client(dev);
530 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100531 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int val = simple_strtol(buf, NULL, 10);
533 u8 tmp;
534 int old_div;
535 int new_min;
536
537 tmp = val == 8 ? 0xc0 :
538 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100539 val == 2 ? 0x40 :
540 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 0xff;
542 if (tmp == 0xff)
543 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100544
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100545 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100546 /* Get fresh readings */
547 data->fan_div[nr] = adm1031_read_value(client,
548 ADM1031_REG_FAN_DIV(nr));
549 data->fan_min[nr] = adm1031_read_value(client,
550 ADM1031_REG_FAN_MIN(nr));
551
552 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100554 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
555 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jean Delvare6d6006b2007-12-02 23:33:57 +0100558 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100560 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100562
563 /* Invalidate the cache: fan speed is no longer valid */
564 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100565 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return count;
567}
568
569#define fan_offset(offset) \
Jean Delvarec8010822007-12-02 23:39:38 +0100570static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
571 show_fan, NULL, offset - 1); \
572static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
573 show_fan_min, set_fan_min, offset - 1); \
574static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
575 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577fan_offset(1);
578fan_offset(2);
579
580
581/* Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100582static ssize_t show_temp(struct device *dev,
583 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jean Delvarec8010822007-12-02 23:39:38 +0100585 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct adm1031_data *data = adm1031_update_device(dev);
587 int ext;
588 ext = nr == 0 ?
589 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
590 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
591 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
592}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200593static ssize_t show_temp_offset(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 int nr = to_sensor_dev_attr(attr)->index;
597 struct adm1031_data *data = adm1031_update_device(dev);
598 return sprintf(buf, "%d\n",
599 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
600}
Jean Delvarec8010822007-12-02 23:39:38 +0100601static ssize_t show_temp_min(struct device *dev,
602 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Jean Delvarec8010822007-12-02 23:39:38 +0100604 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct adm1031_data *data = adm1031_update_device(dev);
606 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
607}
Jean Delvarec8010822007-12-02 23:39:38 +0100608static ssize_t show_temp_max(struct device *dev,
609 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jean Delvarec8010822007-12-02 23:39:38 +0100611 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct adm1031_data *data = adm1031_update_device(dev);
613 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
614}
Jean Delvarec8010822007-12-02 23:39:38 +0100615static ssize_t show_temp_crit(struct device *dev,
616 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jean Delvarec8010822007-12-02 23:39:38 +0100618 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct adm1031_data *data = adm1031_update_device(dev);
620 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
621}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200622static ssize_t set_temp_offset(struct device *dev,
623 struct device_attribute *attr, const char *buf,
624 size_t count)
625{
626 struct i2c_client *client = to_i2c_client(dev);
627 struct adm1031_data *data = i2c_get_clientdata(client);
628 int nr = to_sensor_dev_attr(attr)->index;
629 int val;
630
631 val = simple_strtol(buf, NULL, 10);
632 val = SENSORS_LIMIT(val, -15000, 15000);
633 mutex_lock(&data->update_lock);
634 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
635 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
636 data->temp_offset[nr]);
637 mutex_unlock(&data->update_lock);
638 return count;
639}
Jean Delvarec8010822007-12-02 23:39:38 +0100640static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct i2c_client *client = to_i2c_client(dev);
644 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100645 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int val;
647
648 val = simple_strtol(buf, NULL, 10);
649 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 data->temp_min[nr] = TEMP_TO_REG(val);
652 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
653 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100654 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return count;
656}
Jean Delvarec8010822007-12-02 23:39:38 +0100657static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct i2c_client *client = to_i2c_client(dev);
661 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100662 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int val;
664
665 val = simple_strtol(buf, NULL, 10);
666 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100667 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 data->temp_max[nr] = TEMP_TO_REG(val);
669 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
670 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return count;
673}
Jean Delvarec8010822007-12-02 23:39:38 +0100674static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
675 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct i2c_client *client = to_i2c_client(dev);
678 struct adm1031_data *data = i2c_get_clientdata(client);
Jean Delvarec8010822007-12-02 23:39:38 +0100679 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int val;
681
682 val = simple_strtol(buf, NULL, 10);
683 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100684 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 data->temp_crit[nr] = TEMP_TO_REG(val);
686 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
687 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100688 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return count;
690}
691
Jean Delvarec8010822007-12-02 23:39:38 +0100692#define temp_reg(offset) \
693static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
694 show_temp, NULL, offset - 1); \
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200695static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
696 show_temp_offset, set_temp_offset, offset - 1); \
Jean Delvarec8010822007-12-02 23:39:38 +0100697static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
698 show_temp_min, set_temp_min, offset - 1); \
699static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
700 show_temp_max, set_temp_max, offset - 1); \
701static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
702 show_temp_crit, set_temp_crit, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704temp_reg(1);
705temp_reg(2);
706temp_reg(3);
707
708/* Alarms */
Yani Ioannou30f74292005-05-17 06:41:35 -0400709static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct adm1031_data *data = adm1031_update_device(dev);
712 return sprintf(buf, "%d\n", data->alarm);
713}
714
715static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
716
Jean Delvare050ab872007-12-02 23:42:24 +0100717static ssize_t show_alarm(struct device *dev,
718 struct device_attribute *attr, char *buf)
719{
720 int bitnr = to_sensor_dev_attr(attr)->index;
721 struct adm1031_data *data = adm1031_update_device(dev);
722 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
723}
724
725static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
726static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
727static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
728static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
729static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
730static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
731static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
732static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
733static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
734static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
735static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
736static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
737static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
738static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
739static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200741static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100742 &sensor_dev_attr_fan1_input.dev_attr.attr,
743 &sensor_dev_attr_fan1_div.dev_attr.attr,
744 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100745 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
746 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100747 &sensor_dev_attr_pwm1.dev_attr.attr,
748 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
749 &sensor_dev_attr_temp1_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200750 &sensor_dev_attr_temp1_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100751 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100752 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100753 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100754 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100755 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100756 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100757 &sensor_dev_attr_temp2_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200758 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100759 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100760 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100761 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100762 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100763 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100764 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
765 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200766
Jean Delvarec8010822007-12-02 23:39:38 +0100767 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
768 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
769 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200770
Jean Delvarec8010822007-12-02 23:39:38 +0100771 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
772 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
773 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200774
Jean Delvarec8010822007-12-02 23:39:38 +0100775 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200776
777 &dev_attr_alarms.attr,
778
779 NULL
780};
781
782static const struct attribute_group adm1031_group = {
783 .attrs = adm1031_attributes,
784};
785
786static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100787 &sensor_dev_attr_fan2_input.dev_attr.attr,
788 &sensor_dev_attr_fan2_div.dev_attr.attr,
789 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100790 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
791 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100792 &sensor_dev_attr_pwm2.dev_attr.attr,
793 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
794 &sensor_dev_attr_temp3_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200795 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100796 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100797 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100798 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100799 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100800 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100801 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
802 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100803 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
804 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
805 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
806 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200807 NULL
808};
809
810static const struct attribute_group adm1031_group_opt = {
811 .attrs = adm1031_attributes_opt,
812};
813
Jean Delvareaf200f82008-07-16 19:30:09 +0200814/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100815static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200816 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Jean Delvareaf200f82008-07-16 19:30:09 +0200818 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100819 const char *name;
820 int id, co;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
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
Jean Delvare52df6442009-12-09 20:35:57 +0100825 id = i2c_smbus_read_byte_data(client, 0x3d);
826 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jean Delvare52df6442009-12-09 20:35:57 +0100828 if (!((id == 0x31 || id == 0x30) && co == 0x41))
829 return -ENODEV;
830 name = (id == 0x30) ? "adm1030" : "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jean Delvareaf200f82008-07-16 19:30:09 +0200832 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jean Delvareaf200f82008-07-16 19:30:09 +0200834 return 0;
835}
836
837static int adm1031_probe(struct i2c_client *client,
838 const struct i2c_device_id *id)
839{
840 struct adm1031_data *data;
841 int err;
842
843 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
844 if (!data) {
845 err = -ENOMEM;
846 goto exit;
847 }
848
849 i2c_set_clientdata(client, data);
850 data->chip_type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100851 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jean Delvareaf200f82008-07-16 19:30:09 +0200853 if (data->chip_type == adm1030)
854 data->chan_select_table = &auto_channel_select_table_adm1030;
855 else
856 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100859 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* Register sysfs hooks */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100862 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
Jean Delvareaf200f82008-07-16 19:30:09 +0200863 goto exit_free;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200864
Jean Delvareaf200f82008-07-16 19:30:09 +0200865 if (data->chip_type == adm1031) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100866 if ((err = sysfs_create_group(&client->dev.kobj,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200867 &adm1031_group_opt)))
868 goto exit_remove;
869 }
870
Jean Delvare6d6006b2007-12-02 23:33:57 +0100871 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700872 if (IS_ERR(data->hwmon_dev)) {
873 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200874 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877 return 0;
878
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200879exit_remove:
Jean Delvare6d6006b2007-12-02 23:33:57 +0100880 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
881 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400883 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884exit:
885 return err;
886}
887
Jean Delvareaf200f82008-07-16 19:30:09 +0200888static int adm1031_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400890 struct adm1031_data *data = i2c_get_clientdata(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400891
Tony Jones1beeffe2007-08-20 13:46:20 -0700892 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200893 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
894 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400895 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
897}
898
899static void adm1031_init_client(struct i2c_client *client)
900{
901 unsigned int read_val;
902 unsigned int mask;
903 struct adm1031_data *data = i2c_get_clientdata(client);
904
905 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
906 if (data->chip_type == adm1031) {
907 mask |= (ADM1031_CONF2_PWM2_ENABLE |
908 ADM1031_CONF2_TACH2_ENABLE);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Initialize the ADM1031 chip (enables fan speed reading ) */
911 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
912 if ((read_val | mask) != read_val) {
913 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
914 }
915
916 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
917 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
918 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
919 ADM1031_CONF1_MONITOR_ENABLE);
920 }
921
922}
923
924static struct adm1031_data *adm1031_update_device(struct device *dev)
925{
926 struct i2c_client *client = to_i2c_client(dev);
927 struct adm1031_data *data = i2c_get_clientdata(client);
928 int chan;
929
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100930 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
933 || !data->valid) {
934
935 dev_dbg(&client->dev, "Starting adm1031 update\n");
936 for (chan = 0;
937 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
938 u8 oldh, newh;
939
940 oldh =
941 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
942 data->ext_temp[chan] =
943 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
944 newh =
945 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
946 if (newh != oldh) {
947 data->ext_temp[chan] =
948 adm1031_read_value(client,
949 ADM1031_REG_EXT_TEMP);
950#ifdef DEBUG
951 oldh =
952 adm1031_read_value(client,
953 ADM1031_REG_TEMP(chan));
954
955 /* oldh is actually newer */
956 if (newh != oldh)
957 dev_warn(&client->dev,
958 "Remote temperature may be "
959 "wrong.\n");
960#endif
961 }
962 data->temp[chan] = newh;
963
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200964 data->temp_offset[chan] =
965 adm1031_read_value(client,
966 ADM1031_REG_TEMP_OFFSET(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 data->temp_min[chan] =
968 adm1031_read_value(client,
969 ADM1031_REG_TEMP_MIN(chan));
970 data->temp_max[chan] =
971 adm1031_read_value(client,
972 ADM1031_REG_TEMP_MAX(chan));
973 data->temp_crit[chan] =
974 adm1031_read_value(client,
975 ADM1031_REG_TEMP_CRIT(chan));
976 data->auto_temp[chan] =
977 adm1031_read_value(client,
978 ADM1031_REG_AUTO_TEMP(chan));
979
980 }
981
982 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
983 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
984
985 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
986 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
987 << 8);
988 if (data->chip_type == adm1030) {
989 data->alarm &= 0xc0ff;
990 }
Jean Delvare6d6006b2007-12-02 23:33:57 +0100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
993 data->fan_div[chan] =
994 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
995 data->fan_min[chan] =
996 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
997 data->fan[chan] =
998 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
999 data->pwm[chan] =
Jean Delvare6d6006b2007-12-02 23:33:57 +01001000 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 (4*chan));
1002 }
1003 data->last_updated = jiffies;
1004 data->valid = 1;
1005 }
1006
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001007 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 return data;
1010}
1011
1012static int __init sensors_adm1031_init(void)
1013{
1014 return i2c_add_driver(&adm1031_driver);
1015}
1016
1017static void __exit sensors_adm1031_exit(void)
1018{
1019 i2c_del_driver(&adm1031_driver);
1020}
1021
1022MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1023MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1024MODULE_LICENSE("GPL");
1025
1026module_init(sensors_adm1031_init);
1027module_exit(sensors_adm1031_exit);