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