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