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