Commit | Line | Data |
---|---|---|
1c301fc5 JC |
1 | /* |
2 | * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives | |
3 | * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. | |
4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> | |
5 | * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> | |
6 | ||
7 | * Derived from the lm83 driver by Jean Delvare | |
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 version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/hwmon.h> | |
19 | #include <linux/hwmon-sysfs.h> | |
20 | #include <linux/err.h> | |
21 | ||
22 | /* Indexes for the sysfs hooks */ | |
23 | ||
24 | #define INPUT 0 | |
25 | #define MIN 1 | |
26 | #define MAX 2 | |
27 | #define CONTROL 3 | |
28 | #define OFFSET 3 | |
29 | #define AUTOMIN 4 | |
30 | #define THERM 5 | |
31 | #define HYSTERSIS 6 | |
32 | ||
33 | /* These are unique identifiers for the sysfs functions - unlike the | |
34 | numbers above, these are not also indexes into an array | |
35 | */ | |
36 | ||
37 | #define ALARM 9 | |
38 | #define FAULT 10 | |
39 | ||
40 | /* 7475 Common Registers */ | |
41 | ||
42 | #define REG_VOLTAGE_BASE 0x21 | |
43 | #define REG_TEMP_BASE 0x25 | |
44 | #define REG_TACH_BASE 0x28 | |
45 | #define REG_PWM_BASE 0x30 | |
46 | #define REG_PWM_MAX_BASE 0x38 | |
47 | ||
48 | #define REG_DEVID 0x3D | |
49 | #define REG_VENDID 0x3E | |
50 | ||
51 | #define REG_STATUS1 0x41 | |
52 | #define REG_STATUS2 0x42 | |
53 | ||
54 | #define REG_VOLTAGE_MIN_BASE 0x46 | |
55 | #define REG_VOLTAGE_MAX_BASE 0x47 | |
56 | ||
57 | #define REG_TEMP_MIN_BASE 0x4E | |
58 | #define REG_TEMP_MAX_BASE 0x4F | |
59 | ||
60 | #define REG_TACH_MIN_BASE 0x54 | |
61 | ||
62 | #define REG_PWM_CONFIG_BASE 0x5C | |
63 | ||
64 | #define REG_TEMP_TRANGE_BASE 0x5F | |
65 | ||
66 | #define REG_PWM_MIN_BASE 0x64 | |
67 | ||
68 | #define REG_TEMP_TMIN_BASE 0x67 | |
69 | #define REG_TEMP_THERM_BASE 0x6A | |
70 | ||
71 | #define REG_REMOTE1_HYSTERSIS 0x6D | |
72 | #define REG_REMOTE2_HYSTERSIS 0x6E | |
73 | ||
74 | #define REG_TEMP_OFFSET_BASE 0x70 | |
75 | ||
76 | #define REG_EXTEND1 0x76 | |
77 | #define REG_EXTEND2 0x77 | |
78 | #define REG_CONFIG5 0x7C | |
79 | ||
80 | #define CONFIG5_TWOSCOMP 0x01 | |
81 | #define CONFIG5_TEMPOFFSET 0x02 | |
82 | ||
83 | /* ADT7475 Settings */ | |
84 | ||
85 | #define ADT7475_VOLTAGE_COUNT 2 | |
86 | #define ADT7475_TEMP_COUNT 3 | |
87 | #define ADT7475_TACH_COUNT 4 | |
88 | #define ADT7475_PWM_COUNT 3 | |
89 | ||
90 | /* Macro to read the registers */ | |
91 | ||
92 | #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) | |
93 | ||
94 | /* Macros to easily index the registers */ | |
95 | ||
96 | #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) | |
97 | #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) | |
98 | ||
99 | #define PWM_REG(idx) (REG_PWM_BASE + (idx)) | |
100 | #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) | |
101 | #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) | |
102 | #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) | |
103 | ||
104 | #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) | |
105 | #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) | |
106 | #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) | |
107 | ||
108 | #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) | |
109 | #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) | |
110 | #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) | |
111 | #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) | |
112 | #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) | |
113 | #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) | |
114 | #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) | |
115 | ||
b180d050 | 116 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
1c301fc5 | 117 | |
b180d050 | 118 | I2C_CLIENT_INSMOD_2(adt7473, adt7475); |
1c301fc5 JC |
119 | |
120 | static const struct i2c_device_id adt7475_id[] = { | |
b180d050 | 121 | { "adt7473", adt7473 }, |
1c301fc5 JC |
122 | { "adt7475", adt7475 }, |
123 | { } | |
124 | }; | |
125 | MODULE_DEVICE_TABLE(i2c, adt7475_id); | |
126 | ||
127 | struct adt7475_data { | |
128 | struct device *hwmon_dev; | |
129 | struct mutex lock; | |
130 | ||
131 | unsigned long measure_updated; | |
132 | unsigned long limits_updated; | |
133 | char valid; | |
134 | ||
135 | u8 config5; | |
136 | u16 alarms; | |
137 | u16 voltage[3][3]; | |
138 | u16 temp[7][3]; | |
139 | u16 tach[2][4]; | |
140 | u8 pwm[4][3]; | |
141 | u8 range[3]; | |
142 | u8 pwmctl[3]; | |
143 | u8 pwmchan[3]; | |
144 | }; | |
145 | ||
146 | static struct i2c_driver adt7475_driver; | |
147 | static struct adt7475_data *adt7475_update_device(struct device *dev); | |
148 | static void adt7475_read_hystersis(struct i2c_client *client); | |
149 | static void adt7475_read_pwm(struct i2c_client *client, int index); | |
150 | ||
151 | /* Given a temp value, convert it to register value */ | |
152 | ||
153 | static inline u16 temp2reg(struct adt7475_data *data, long val) | |
154 | { | |
155 | u16 ret; | |
156 | ||
157 | if (!(data->config5 & CONFIG5_TWOSCOMP)) { | |
158 | val = SENSORS_LIMIT(val, -64000, 191000); | |
159 | ret = (val + 64500) / 1000; | |
160 | } else { | |
161 | val = SENSORS_LIMIT(val, -128000, 127000); | |
162 | if (val < -500) | |
163 | ret = (256500 + val) / 1000; | |
164 | else | |
165 | ret = (val + 500) / 1000; | |
166 | } | |
167 | ||
168 | return ret << 2; | |
169 | } | |
170 | ||
171 | /* Given a register value, convert it to a real temp value */ | |
172 | ||
173 | static inline int reg2temp(struct adt7475_data *data, u16 reg) | |
174 | { | |
175 | if (data->config5 & CONFIG5_TWOSCOMP) { | |
176 | if (reg >= 512) | |
177 | return (reg - 1024) * 250; | |
178 | else | |
179 | return reg * 250; | |
180 | } else | |
181 | return (reg - 256) * 250; | |
182 | } | |
183 | ||
184 | static inline int tach2rpm(u16 tach) | |
185 | { | |
186 | if (tach == 0 || tach == 0xFFFF) | |
187 | return 0; | |
188 | ||
189 | return (90000 * 60) / tach; | |
190 | } | |
191 | ||
192 | static inline u16 rpm2tach(unsigned long rpm) | |
193 | { | |
194 | if (rpm == 0) | |
195 | return 0; | |
196 | ||
197 | return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF); | |
198 | } | |
199 | ||
200 | static inline int reg2vcc(u16 reg) | |
201 | { | |
202 | return (4296 * reg) / 1000; | |
203 | } | |
204 | ||
205 | static inline int reg2vccp(u16 reg) | |
206 | { | |
207 | return (2929 * reg) / 1000; | |
208 | } | |
209 | ||
210 | static inline u16 vcc2reg(long vcc) | |
211 | { | |
212 | vcc = SENSORS_LIMIT(vcc, 0, 4396); | |
213 | return (vcc * 1000) / 4296; | |
214 | } | |
215 | ||
216 | static inline u16 vccp2reg(long vcc) | |
217 | { | |
218 | vcc = SENSORS_LIMIT(vcc, 0, 2998); | |
219 | return (vcc * 1000) / 2929; | |
220 | } | |
221 | ||
222 | static u16 adt7475_read_word(struct i2c_client *client, int reg) | |
223 | { | |
224 | u16 val; | |
225 | ||
226 | val = i2c_smbus_read_byte_data(client, reg); | |
227 | val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8); | |
228 | ||
229 | return val; | |
230 | } | |
231 | ||
232 | static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) | |
233 | { | |
234 | i2c_smbus_write_byte_data(client, reg + 1, val >> 8); | |
235 | i2c_smbus_write_byte_data(client, reg, val & 0xFF); | |
236 | } | |
237 | ||
238 | /* Find the nearest value in a table - used for pwm frequency and | |
239 | auto temp range */ | |
240 | static int find_nearest(long val, const int *array, int size) | |
241 | { | |
242 | int i; | |
243 | ||
244 | if (val < array[0]) | |
245 | return 0; | |
246 | ||
247 | if (val > array[size - 1]) | |
248 | return size - 1; | |
249 | ||
250 | for (i = 0; i < size - 1; i++) { | |
251 | int a, b; | |
252 | ||
253 | if (val > array[i + 1]) | |
254 | continue; | |
255 | ||
256 | a = val - array[i]; | |
257 | b = array[i + 1] - val; | |
258 | ||
259 | return (a <= b) ? i : i + 1; | |
260 | } | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, | |
266 | char *buf) | |
267 | { | |
268 | struct adt7475_data *data = adt7475_update_device(dev); | |
269 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
270 | unsigned short val; | |
271 | ||
272 | switch (sattr->nr) { | |
273 | case ALARM: | |
274 | return sprintf(buf, "%d\n", | |
275 | (data->alarms >> (sattr->index + 1)) & 1); | |
276 | default: | |
277 | val = data->voltage[sattr->nr][sattr->index]; | |
278 | return sprintf(buf, "%d\n", | |
279 | sattr->index == | |
280 | 0 ? reg2vccp(val) : reg2vcc(val)); | |
281 | } | |
282 | } | |
283 | ||
284 | static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, | |
285 | const char *buf, size_t count) | |
286 | { | |
287 | ||
288 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
289 | struct i2c_client *client = to_i2c_client(dev); | |
290 | struct adt7475_data *data = i2c_get_clientdata(client); | |
291 | unsigned char reg; | |
292 | long val; | |
293 | ||
294 | if (strict_strtol(buf, 10, &val)) | |
295 | return -EINVAL; | |
296 | ||
297 | mutex_lock(&data->lock); | |
298 | ||
299 | data->voltage[sattr->nr][sattr->index] = | |
300 | sattr->index ? vcc2reg(val) : vccp2reg(val); | |
301 | ||
302 | if (sattr->nr == MIN) | |
303 | reg = VOLTAGE_MIN_REG(sattr->index); | |
304 | else | |
305 | reg = VOLTAGE_MAX_REG(sattr->index); | |
306 | ||
307 | i2c_smbus_write_byte_data(client, reg, | |
308 | data->voltage[sattr->nr][sattr->index] >> 2); | |
309 | mutex_unlock(&data->lock); | |
310 | ||
311 | return count; | |
312 | } | |
313 | ||
314 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | |
315 | char *buf) | |
316 | { | |
317 | struct adt7475_data *data = adt7475_update_device(dev); | |
318 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
319 | int out; | |
320 | ||
321 | switch (sattr->nr) { | |
322 | case HYSTERSIS: | |
323 | mutex_lock(&data->lock); | |
324 | out = data->temp[sattr->nr][sattr->index]; | |
325 | if (sattr->index != 1) | |
326 | out = (out >> 4) & 0xF; | |
327 | else | |
328 | out = (out & 0xF); | |
329 | /* Show the value as an absolute number tied to | |
330 | * THERM */ | |
331 | out = reg2temp(data, data->temp[THERM][sattr->index]) - | |
332 | out * 1000; | |
333 | mutex_unlock(&data->lock); | |
334 | break; | |
335 | ||
336 | case OFFSET: | |
337 | /* Offset is always 2's complement, regardless of the | |
338 | * setting in CONFIG5 */ | |
339 | mutex_lock(&data->lock); | |
340 | out = (s8)data->temp[sattr->nr][sattr->index]; | |
341 | if (data->config5 & CONFIG5_TEMPOFFSET) | |
342 | out *= 1000; | |
343 | else | |
344 | out *= 500; | |
345 | mutex_unlock(&data->lock); | |
346 | break; | |
347 | ||
348 | case ALARM: | |
349 | out = (data->alarms >> (sattr->index + 4)) & 1; | |
350 | break; | |
351 | ||
352 | case FAULT: | |
353 | /* Note - only for remote1 and remote2 */ | |
cf312e07 | 354 | out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); |
1c301fc5 JC |
355 | break; |
356 | ||
357 | default: | |
358 | /* All other temp values are in the configured format */ | |
359 | out = reg2temp(data, data->temp[sattr->nr][sattr->index]); | |
360 | } | |
361 | ||
362 | return sprintf(buf, "%d\n", out); | |
363 | } | |
364 | ||
365 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, | |
366 | const char *buf, size_t count) | |
367 | { | |
368 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
369 | struct i2c_client *client = to_i2c_client(dev); | |
370 | struct adt7475_data *data = i2c_get_clientdata(client); | |
371 | unsigned char reg = 0; | |
372 | u8 out; | |
373 | int temp; | |
374 | long val; | |
375 | ||
376 | if (strict_strtol(buf, 10, &val)) | |
377 | return -EINVAL; | |
378 | ||
379 | mutex_lock(&data->lock); | |
380 | ||
381 | /* We need the config register in all cases for temp <-> reg conv. */ | |
382 | data->config5 = adt7475_read(REG_CONFIG5); | |
383 | ||
384 | switch (sattr->nr) { | |
385 | case OFFSET: | |
386 | if (data->config5 & CONFIG5_TEMPOFFSET) { | |
387 | val = SENSORS_LIMIT(val, -63000, 127000); | |
388 | out = data->temp[OFFSET][sattr->index] = val / 1000; | |
389 | } else { | |
390 | val = SENSORS_LIMIT(val, -63000, 64000); | |
391 | out = data->temp[OFFSET][sattr->index] = val / 500; | |
392 | } | |
393 | break; | |
394 | ||
395 | case HYSTERSIS: | |
396 | /* The value will be given as an absolute value, turn it | |
397 | into an offset based on THERM */ | |
398 | ||
399 | /* Read fresh THERM and HYSTERSIS values from the chip */ | |
400 | data->temp[THERM][sattr->index] = | |
401 | adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; | |
402 | adt7475_read_hystersis(client); | |
403 | ||
404 | temp = reg2temp(data, data->temp[THERM][sattr->index]); | |
405 | val = SENSORS_LIMIT(val, temp - 15000, temp); | |
406 | val = (temp - val) / 1000; | |
407 | ||
408 | if (sattr->index != 1) { | |
409 | data->temp[HYSTERSIS][sattr->index] &= 0xF0; | |
410 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; | |
411 | } else { | |
412 | data->temp[HYSTERSIS][sattr->index] &= 0x0F; | |
413 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); | |
414 | } | |
415 | ||
416 | out = data->temp[HYSTERSIS][sattr->index]; | |
417 | break; | |
418 | ||
419 | default: | |
420 | data->temp[sattr->nr][sattr->index] = temp2reg(data, val); | |
421 | ||
422 | /* We maintain an extra 2 digits of precision for simplicity | |
423 | * - shift those back off before writing the value */ | |
424 | out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); | |
425 | } | |
426 | ||
427 | switch (sattr->nr) { | |
428 | case MIN: | |
429 | reg = TEMP_MIN_REG(sattr->index); | |
430 | break; | |
431 | case MAX: | |
432 | reg = TEMP_MAX_REG(sattr->index); | |
433 | break; | |
434 | case OFFSET: | |
435 | reg = TEMP_OFFSET_REG(sattr->index); | |
436 | break; | |
437 | case AUTOMIN: | |
438 | reg = TEMP_TMIN_REG(sattr->index); | |
439 | break; | |
440 | case THERM: | |
441 | reg = TEMP_THERM_REG(sattr->index); | |
442 | break; | |
443 | case HYSTERSIS: | |
444 | if (sattr->index != 2) | |
445 | reg = REG_REMOTE1_HYSTERSIS; | |
446 | else | |
447 | reg = REG_REMOTE2_HYSTERSIS; | |
448 | ||
449 | break; | |
450 | } | |
451 | ||
452 | i2c_smbus_write_byte_data(client, reg, out); | |
453 | ||
454 | mutex_unlock(&data->lock); | |
455 | return count; | |
456 | } | |
457 | ||
458 | /* Table of autorange values - the user will write the value in millidegrees, | |
459 | and we'll convert it */ | |
460 | static const int autorange_table[] = { | |
461 | 2000, 2500, 3330, 4000, 5000, 6670, 8000, | |
462 | 10000, 13330, 16000, 20000, 26670, 32000, 40000, | |
463 | 53330, 80000 | |
464 | }; | |
465 | ||
466 | static ssize_t show_point2(struct device *dev, struct device_attribute *attr, | |
467 | char *buf) | |
468 | { | |
469 | struct adt7475_data *data = adt7475_update_device(dev); | |
470 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
471 | int out, val; | |
472 | ||
473 | mutex_lock(&data->lock); | |
474 | out = (data->range[sattr->index] >> 4) & 0x0F; | |
475 | val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); | |
476 | mutex_unlock(&data->lock); | |
477 | ||
478 | return sprintf(buf, "%d\n", val + autorange_table[out]); | |
479 | } | |
480 | ||
481 | static ssize_t set_point2(struct device *dev, struct device_attribute *attr, | |
482 | const char *buf, size_t count) | |
483 | { | |
484 | struct i2c_client *client = to_i2c_client(dev); | |
485 | struct adt7475_data *data = i2c_get_clientdata(client); | |
486 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
487 | int temp; | |
488 | long val; | |
489 | ||
490 | if (strict_strtol(buf, 10, &val)) | |
491 | return -EINVAL; | |
492 | ||
493 | mutex_lock(&data->lock); | |
494 | ||
495 | /* Get a fresh copy of the needed registers */ | |
496 | data->config5 = adt7475_read(REG_CONFIG5); | |
497 | data->temp[AUTOMIN][sattr->index] = | |
498 | adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; | |
499 | data->range[sattr->index] = | |
500 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); | |
501 | ||
502 | /* The user will write an absolute value, so subtract the start point | |
503 | to figure the range */ | |
504 | temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); | |
505 | val = SENSORS_LIMIT(val, temp + autorange_table[0], | |
506 | temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); | |
507 | val -= temp; | |
508 | ||
509 | /* Find the nearest table entry to what the user wrote */ | |
510 | val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table)); | |
511 | ||
512 | data->range[sattr->index] &= ~0xF0; | |
513 | data->range[sattr->index] |= val << 4; | |
514 | ||
515 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), | |
516 | data->range[sattr->index]); | |
517 | ||
518 | mutex_unlock(&data->lock); | |
519 | return count; | |
520 | } | |
521 | ||
522 | static ssize_t show_tach(struct device *dev, struct device_attribute *attr, | |
523 | char *buf) | |
524 | { | |
525 | struct adt7475_data *data = adt7475_update_device(dev); | |
526 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
527 | int out; | |
528 | ||
529 | if (sattr->nr == ALARM) | |
530 | out = (data->alarms >> (sattr->index + 10)) & 1; | |
531 | else | |
532 | out = tach2rpm(data->tach[sattr->nr][sattr->index]); | |
533 | ||
534 | return sprintf(buf, "%d\n", out); | |
535 | } | |
536 | ||
537 | static ssize_t set_tach(struct device *dev, struct device_attribute *attr, | |
538 | const char *buf, size_t count) | |
539 | { | |
540 | ||
541 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
542 | struct i2c_client *client = to_i2c_client(dev); | |
543 | struct adt7475_data *data = i2c_get_clientdata(client); | |
544 | unsigned long val; | |
545 | ||
546 | if (strict_strtoul(buf, 10, &val)) | |
547 | return -EINVAL; | |
548 | ||
549 | mutex_lock(&data->lock); | |
550 | ||
551 | data->tach[MIN][sattr->index] = rpm2tach(val); | |
552 | ||
553 | adt7475_write_word(client, TACH_MIN_REG(sattr->index), | |
554 | data->tach[MIN][sattr->index]); | |
555 | ||
556 | mutex_unlock(&data->lock); | |
557 | return count; | |
558 | } | |
559 | ||
560 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, | |
561 | char *buf) | |
562 | { | |
563 | struct adt7475_data *data = adt7475_update_device(dev); | |
564 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
565 | ||
566 | return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); | |
567 | } | |
568 | ||
569 | static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, | |
570 | char *buf) | |
571 | { | |
572 | struct adt7475_data *data = adt7475_update_device(dev); | |
573 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
574 | ||
575 | return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); | |
576 | } | |
577 | ||
578 | static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, | |
579 | char *buf) | |
580 | { | |
581 | struct adt7475_data *data = adt7475_update_device(dev); | |
582 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
583 | ||
584 | return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); | |
585 | } | |
586 | ||
587 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, | |
588 | const char *buf, size_t count) | |
589 | { | |
590 | ||
591 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
592 | struct i2c_client *client = to_i2c_client(dev); | |
593 | struct adt7475_data *data = i2c_get_clientdata(client); | |
594 | unsigned char reg = 0; | |
595 | long val; | |
596 | ||
597 | if (strict_strtol(buf, 10, &val)) | |
598 | return -EINVAL; | |
599 | ||
600 | mutex_lock(&data->lock); | |
601 | ||
602 | switch (sattr->nr) { | |
603 | case INPUT: | |
604 | /* Get a fresh value for CONTROL */ | |
605 | data->pwm[CONTROL][sattr->index] = | |
606 | adt7475_read(PWM_CONFIG_REG(sattr->index)); | |
607 | ||
608 | /* If we are not in manual mode, then we shouldn't allow | |
609 | * the user to set the pwm speed */ | |
610 | if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { | |
611 | mutex_unlock(&data->lock); | |
612 | return count; | |
613 | } | |
614 | ||
615 | reg = PWM_REG(sattr->index); | |
616 | break; | |
617 | ||
618 | case MIN: | |
619 | reg = PWM_MIN_REG(sattr->index); | |
620 | break; | |
621 | ||
622 | case MAX: | |
623 | reg = PWM_MAX_REG(sattr->index); | |
624 | break; | |
625 | } | |
626 | ||
627 | data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF); | |
628 | i2c_smbus_write_byte_data(client, reg, | |
629 | data->pwm[sattr->nr][sattr->index]); | |
630 | ||
631 | mutex_unlock(&data->lock); | |
632 | ||
633 | return count; | |
634 | } | |
635 | ||
636 | /* Called by set_pwmctrl and set_pwmchan */ | |
637 | ||
638 | static int hw_set_pwm(struct i2c_client *client, int index, | |
639 | unsigned int pwmctl, unsigned int pwmchan) | |
640 | { | |
641 | struct adt7475_data *data = i2c_get_clientdata(client); | |
642 | long val = 0; | |
643 | ||
644 | switch (pwmctl) { | |
645 | case 0: | |
646 | val = 0x03; /* Run at full speed */ | |
647 | break; | |
648 | case 1: | |
649 | val = 0x07; /* Manual mode */ | |
650 | break; | |
651 | case 2: | |
652 | switch (pwmchan) { | |
653 | case 1: | |
654 | /* Remote1 controls PWM */ | |
655 | val = 0x00; | |
656 | break; | |
657 | case 2: | |
658 | /* local controls PWM */ | |
659 | val = 0x01; | |
660 | break; | |
661 | case 4: | |
662 | /* remote2 controls PWM */ | |
663 | val = 0x02; | |
664 | break; | |
665 | case 6: | |
666 | /* local/remote2 control PWM */ | |
667 | val = 0x05; | |
668 | break; | |
669 | case 7: | |
670 | /* All three control PWM */ | |
671 | val = 0x06; | |
672 | break; | |
673 | default: | |
674 | return -EINVAL; | |
675 | } | |
676 | break; | |
677 | default: | |
678 | return -EINVAL; | |
679 | } | |
680 | ||
681 | data->pwmctl[index] = pwmctl; | |
682 | data->pwmchan[index] = pwmchan; | |
683 | ||
684 | data->pwm[CONTROL][index] &= ~0xE0; | |
685 | data->pwm[CONTROL][index] |= (val & 7) << 5; | |
686 | ||
687 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), | |
688 | data->pwm[CONTROL][index]); | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
693 | static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, | |
694 | const char *buf, size_t count) | |
695 | { | |
696 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
697 | struct i2c_client *client = to_i2c_client(dev); | |
698 | struct adt7475_data *data = i2c_get_clientdata(client); | |
699 | int r; | |
700 | long val; | |
701 | ||
702 | if (strict_strtol(buf, 10, &val)) | |
703 | return -EINVAL; | |
704 | ||
705 | mutex_lock(&data->lock); | |
706 | /* Read Modify Write PWM values */ | |
707 | adt7475_read_pwm(client, sattr->index); | |
708 | r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); | |
709 | if (r) | |
710 | count = r; | |
711 | mutex_unlock(&data->lock); | |
712 | ||
713 | return count; | |
714 | } | |
715 | ||
716 | static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, | |
717 | const char *buf, size_t count) | |
718 | { | |
719 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
720 | struct i2c_client *client = to_i2c_client(dev); | |
721 | struct adt7475_data *data = i2c_get_clientdata(client); | |
722 | int r; | |
723 | long val; | |
724 | ||
725 | if (strict_strtol(buf, 10, &val)) | |
726 | return -EINVAL; | |
727 | ||
728 | mutex_lock(&data->lock); | |
729 | /* Read Modify Write PWM values */ | |
730 | adt7475_read_pwm(client, sattr->index); | |
731 | r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); | |
732 | if (r) | |
733 | count = r; | |
734 | mutex_unlock(&data->lock); | |
735 | ||
736 | return count; | |
737 | } | |
738 | ||
739 | /* List of frequencies for the PWM */ | |
740 | static const int pwmfreq_table[] = { | |
741 | 11, 14, 22, 29, 35, 44, 58, 88 | |
742 | }; | |
743 | ||
744 | static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, | |
745 | char *buf) | |
746 | { | |
747 | struct adt7475_data *data = adt7475_update_device(dev); | |
748 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
749 | ||
750 | return sprintf(buf, "%d\n", | |
751 | pwmfreq_table[data->range[sattr->index] & 7]); | |
752 | } | |
753 | ||
754 | static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, | |
755 | const char *buf, size_t count) | |
756 | { | |
757 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
758 | struct i2c_client *client = to_i2c_client(dev); | |
759 | struct adt7475_data *data = i2c_get_clientdata(client); | |
760 | int out; | |
761 | long val; | |
762 | ||
763 | if (strict_strtol(buf, 10, &val)) | |
764 | return -EINVAL; | |
765 | ||
766 | out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); | |
767 | ||
768 | mutex_lock(&data->lock); | |
769 | ||
770 | data->range[sattr->index] = | |
771 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); | |
772 | data->range[sattr->index] &= ~7; | |
773 | data->range[sattr->index] |= out; | |
774 | ||
775 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), | |
776 | data->range[sattr->index]); | |
777 | ||
778 | mutex_unlock(&data->lock); | |
779 | return count; | |
780 | } | |
781 | ||
782 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 0); | |
783 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, | |
784 | set_voltage, MAX, 0); | |
785 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, | |
786 | set_voltage, MIN, 0); | |
787 | static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); | |
788 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 1); | |
789 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, | |
790 | set_voltage, MAX, 1); | |
791 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, | |
792 | set_voltage, MIN, 1); | |
793 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); | |
794 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); | |
795 | static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); | |
796 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); | |
797 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
798 | MAX, 0); | |
799 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
800 | MIN, 0); | |
801 | static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, | |
802 | set_temp, OFFSET, 0); | |
803 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, | |
804 | show_temp, set_temp, AUTOMIN, 0); | |
805 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, | |
806 | show_point2, set_point2, 0, 0); | |
807 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
808 | THERM, 0); | |
809 | static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, | |
810 | set_temp, HYSTERSIS, 0); | |
811 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); | |
812 | static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); | |
813 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
814 | MAX, 1); | |
815 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
816 | MIN, 1); | |
817 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, | |
818 | set_temp, OFFSET, 1); | |
819 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, | |
820 | show_temp, set_temp, AUTOMIN, 1); | |
821 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, | |
822 | show_point2, set_point2, 0, 1); | |
823 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
824 | THERM, 1); | |
825 | static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, | |
826 | set_temp, HYSTERSIS, 1); | |
827 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); | |
828 | static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); | |
829 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); | |
830 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
831 | MAX, 2); | |
832 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
833 | MIN, 2); | |
834 | static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, | |
835 | set_temp, OFFSET, 2); | |
836 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, | |
837 | show_temp, set_temp, AUTOMIN, 2); | |
838 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, | |
839 | show_point2, set_point2, 0, 2); | |
840 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, | |
841 | THERM, 2); | |
842 | static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, | |
843 | set_temp, HYSTERSIS, 2); | |
844 | static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); | |
845 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, | |
846 | MIN, 0); | |
847 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); | |
848 | static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); | |
849 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, | |
850 | MIN, 1); | |
851 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); | |
852 | static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); | |
853 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, | |
854 | MIN, 2); | |
855 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); | |
856 | static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); | |
857 | static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, | |
858 | MIN, 3); | |
859 | static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); | |
860 | static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, | |
861 | 0); | |
862 | static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, | |
863 | set_pwmfreq, INPUT, 0); | |
864 | static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, | |
865 | set_pwmctrl, INPUT, 0); | |
84d2a314 | 866 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, |
1c301fc5 JC |
867 | show_pwmchan, set_pwmchan, INPUT, 0); |
868 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
869 | set_pwm, MIN, 0); | |
870 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
871 | set_pwm, MAX, 0); | |
872 | static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, | |
873 | 1); | |
874 | static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, | |
875 | set_pwmfreq, INPUT, 1); | |
876 | static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, | |
877 | set_pwmctrl, INPUT, 1); | |
84d2a314 | 878 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, |
1c301fc5 JC |
879 | show_pwmchan, set_pwmchan, INPUT, 1); |
880 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
881 | set_pwm, MIN, 1); | |
882 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
883 | set_pwm, MAX, 1); | |
884 | static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, | |
885 | 2); | |
886 | static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, | |
887 | set_pwmfreq, INPUT, 2); | |
888 | static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, | |
889 | set_pwmctrl, INPUT, 2); | |
84d2a314 | 890 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, |
1c301fc5 JC |
891 | show_pwmchan, set_pwmchan, INPUT, 2); |
892 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
893 | set_pwm, MIN, 2); | |
894 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, | |
895 | set_pwm, MAX, 2); | |
896 | ||
897 | static struct attribute *adt7475_attrs[] = { | |
898 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
899 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
900 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
901 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
902 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
903 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
904 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
905 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
906 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
907 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | |
908 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | |
909 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
910 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
911 | &sensor_dev_attr_temp1_offset.dev_attr.attr, | |
912 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, | |
913 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, | |
914 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
915 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | |
916 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
917 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | |
918 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
919 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
920 | &sensor_dev_attr_temp2_offset.dev_attr.attr, | |
921 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, | |
922 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, | |
923 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
924 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | |
925 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
926 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
927 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
928 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
929 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
930 | &sensor_dev_attr_temp3_offset.dev_attr.attr, | |
931 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, | |
932 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, | |
933 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
934 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, | |
935 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
936 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
937 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | |
938 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
939 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
940 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
941 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
942 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
943 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
944 | &sensor_dev_attr_fan4_input.dev_attr.attr, | |
945 | &sensor_dev_attr_fan4_min.dev_attr.attr, | |
946 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, | |
947 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
948 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, | |
949 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, | |
84d2a314 | 950 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
1c301fc5 JC |
951 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
952 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, | |
953 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
954 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, | |
955 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | |
84d2a314 | 956 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
1c301fc5 JC |
957 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
958 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, | |
959 | &sensor_dev_attr_pwm3.dev_attr.attr, | |
960 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, | |
961 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, | |
84d2a314 | 962 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
1c301fc5 JC |
963 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
964 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, | |
965 | NULL, | |
966 | }; | |
967 | ||
968 | struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; | |
969 | ||
970 | static int adt7475_detect(struct i2c_client *client, int kind, | |
971 | struct i2c_board_info *info) | |
972 | { | |
973 | struct i2c_adapter *adapter = client->adapter; | |
b180d050 JD |
974 | int vendid, devid; |
975 | const char *name; | |
1c301fc5 JC |
976 | |
977 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
978 | return -ENODEV; | |
979 | ||
b180d050 JD |
980 | vendid = adt7475_read(REG_VENDID); |
981 | devid = adt7475_read(REG_DEVID); | |
982 | ||
983 | if (vendid == 0x41 && devid == 0x73) | |
984 | name = "adt7473"; | |
985 | else if (vendid == 0x41 && devid == 0x75 && client->addr == 0x2e) | |
986 | name = "adt7475"; | |
987 | else { | |
988 | dev_dbg(&adapter->dev, | |
989 | "Couldn't detect an ADT7473 or ADT7475 part at " | |
990 | "0x%02x\n", (unsigned int)client->addr); | |
52df6440 | 991 | return -ENODEV; |
1c301fc5 JC |
992 | } |
993 | ||
b180d050 | 994 | strlcpy(info->type, name, I2C_NAME_SIZE); |
1c301fc5 JC |
995 | |
996 | return 0; | |
997 | } | |
998 | ||
999 | static int adt7475_probe(struct i2c_client *client, | |
1000 | const struct i2c_device_id *id) | |
1001 | { | |
1002 | struct adt7475_data *data; | |
1003 | int i, ret = 0; | |
1004 | ||
1005 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
1006 | if (data == NULL) | |
1007 | return -ENOMEM; | |
1008 | ||
1009 | mutex_init(&data->lock); | |
1010 | i2c_set_clientdata(client, data); | |
1011 | ||
1012 | /* Call adt7475_read_pwm for all pwm's as this will reprogram any | |
1013 | pwm's which are disabled to manual mode with 0% duty cycle */ | |
1014 | for (i = 0; i < ADT7475_PWM_COUNT; i++) | |
1015 | adt7475_read_pwm(client, i); | |
1016 | ||
1017 | ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); | |
1018 | if (ret) | |
1019 | goto efree; | |
1020 | ||
1021 | data->hwmon_dev = hwmon_device_register(&client->dev); | |
1022 | if (IS_ERR(data->hwmon_dev)) { | |
1023 | ret = PTR_ERR(data->hwmon_dev); | |
1024 | goto eremove; | |
1025 | } | |
1026 | ||
1027 | return 0; | |
1028 | ||
1029 | eremove: | |
1030 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); | |
1031 | efree: | |
1032 | kfree(data); | |
1033 | return ret; | |
1034 | } | |
1035 | ||
1036 | static int adt7475_remove(struct i2c_client *client) | |
1037 | { | |
1038 | struct adt7475_data *data = i2c_get_clientdata(client); | |
1039 | ||
1040 | hwmon_device_unregister(data->hwmon_dev); | |
1041 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); | |
1042 | kfree(data); | |
1043 | ||
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static struct i2c_driver adt7475_driver = { | |
1048 | .class = I2C_CLASS_HWMON, | |
1049 | .driver = { | |
1050 | .name = "adt7475", | |
1051 | }, | |
1052 | .probe = adt7475_probe, | |
1053 | .remove = adt7475_remove, | |
1054 | .id_table = adt7475_id, | |
1055 | .detect = adt7475_detect, | |
1056 | .address_data = &addr_data, | |
1057 | }; | |
1058 | ||
1059 | static void adt7475_read_hystersis(struct i2c_client *client) | |
1060 | { | |
1061 | struct adt7475_data *data = i2c_get_clientdata(client); | |
1062 | ||
1063 | data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); | |
1064 | data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; | |
1065 | data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); | |
1066 | } | |
1067 | ||
1068 | static void adt7475_read_pwm(struct i2c_client *client, int index) | |
1069 | { | |
1070 | struct adt7475_data *data = i2c_get_clientdata(client); | |
1071 | unsigned int v; | |
1072 | ||
1073 | data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); | |
1074 | ||
1075 | /* Figure out the internal value for pwmctrl and pwmchan | |
1076 | based on the current settings */ | |
1077 | v = (data->pwm[CONTROL][index] >> 5) & 7; | |
1078 | ||
1079 | if (v == 3) | |
1080 | data->pwmctl[index] = 0; | |
1081 | else if (v == 7) | |
1082 | data->pwmctl[index] = 1; | |
1083 | else if (v == 4) { | |
1084 | /* The fan is disabled - we don't want to | |
1085 | support that, so change to manual mode and | |
1086 | set the duty cycle to 0 instead | |
1087 | */ | |
1088 | data->pwm[INPUT][index] = 0; | |
1089 | data->pwm[CONTROL][index] &= ~0xE0; | |
1090 | data->pwm[CONTROL][index] |= (7 << 5); | |
1091 | ||
1092 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), | |
1093 | data->pwm[INPUT][index]); | |
1094 | ||
1095 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), | |
1096 | data->pwm[CONTROL][index]); | |
1097 | ||
1098 | data->pwmctl[index] = 1; | |
1099 | } else { | |
1100 | data->pwmctl[index] = 2; | |
1101 | ||
1102 | switch (v) { | |
1103 | case 0: | |
1104 | data->pwmchan[index] = 1; | |
1105 | break; | |
1106 | case 1: | |
1107 | data->pwmchan[index] = 2; | |
1108 | break; | |
1109 | case 2: | |
1110 | data->pwmchan[index] = 4; | |
1111 | break; | |
1112 | case 5: | |
1113 | data->pwmchan[index] = 6; | |
1114 | break; | |
1115 | case 6: | |
1116 | data->pwmchan[index] = 7; | |
1117 | break; | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | static struct adt7475_data *adt7475_update_device(struct device *dev) | |
1123 | { | |
1124 | struct i2c_client *client = to_i2c_client(dev); | |
1125 | struct adt7475_data *data = i2c_get_clientdata(client); | |
1126 | u8 ext; | |
1127 | int i; | |
1128 | ||
1129 | mutex_lock(&data->lock); | |
1130 | ||
1131 | /* Measurement values update every 2 seconds */ | |
1132 | if (time_after(jiffies, data->measure_updated + HZ * 2) || | |
1133 | !data->valid) { | |
1134 | data->alarms = adt7475_read(REG_STATUS2) << 8; | |
1135 | data->alarms |= adt7475_read(REG_STATUS1); | |
1136 | ||
1137 | ext = adt7475_read(REG_EXTEND1); | |
1138 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) | |
1139 | data->voltage[INPUT][i] = | |
1140 | (adt7475_read(VOLTAGE_REG(i)) << 2) | | |
1141 | ((ext >> ((i + 1) * 2)) & 3); | |
1142 | ||
1143 | ext = adt7475_read(REG_EXTEND2); | |
1144 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) | |
1145 | data->temp[INPUT][i] = | |
1146 | (adt7475_read(TEMP_REG(i)) << 2) | | |
1147 | ((ext >> ((i + 1) * 2)) & 3); | |
1148 | ||
1149 | for (i = 0; i < ADT7475_TACH_COUNT; i++) | |
1150 | data->tach[INPUT][i] = | |
1151 | adt7475_read_word(client, TACH_REG(i)); | |
1152 | ||
1153 | /* Updated by hw when in auto mode */ | |
1154 | for (i = 0; i < ADT7475_PWM_COUNT; i++) | |
1155 | data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); | |
1156 | ||
1157 | data->measure_updated = jiffies; | |
1158 | } | |
1159 | ||
1160 | /* Limits and settings, should never change update every 60 seconds */ | |
56e35eee | 1161 | if (time_after(jiffies, data->limits_updated + HZ * 60) || |
1c301fc5 JC |
1162 | !data->valid) { |
1163 | data->config5 = adt7475_read(REG_CONFIG5); | |
1164 | ||
1165 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { | |
1166 | /* Adjust values so they match the input precision */ | |
1167 | data->voltage[MIN][i] = | |
1168 | adt7475_read(VOLTAGE_MIN_REG(i)) << 2; | |
1169 | data->voltage[MAX][i] = | |
1170 | adt7475_read(VOLTAGE_MAX_REG(i)) << 2; | |
1171 | } | |
1172 | ||
1173 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { | |
1174 | /* Adjust values so they match the input precision */ | |
1175 | data->temp[MIN][i] = | |
1176 | adt7475_read(TEMP_MIN_REG(i)) << 2; | |
1177 | data->temp[MAX][i] = | |
1178 | adt7475_read(TEMP_MAX_REG(i)) << 2; | |
1179 | data->temp[AUTOMIN][i] = | |
1180 | adt7475_read(TEMP_TMIN_REG(i)) << 2; | |
1181 | data->temp[THERM][i] = | |
1182 | adt7475_read(TEMP_THERM_REG(i)) << 2; | |
1183 | data->temp[OFFSET][i] = | |
1184 | adt7475_read(TEMP_OFFSET_REG(i)); | |
1185 | } | |
1186 | adt7475_read_hystersis(client); | |
1187 | ||
1188 | for (i = 0; i < ADT7475_TACH_COUNT; i++) | |
1189 | data->tach[MIN][i] = | |
1190 | adt7475_read_word(client, TACH_MIN_REG(i)); | |
1191 | ||
1192 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { | |
1193 | data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); | |
1194 | data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); | |
1195 | /* Set the channel and control information */ | |
1196 | adt7475_read_pwm(client, i); | |
1197 | } | |
1198 | ||
1199 | data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); | |
1200 | data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); | |
1201 | data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); | |
1202 | ||
1203 | data->limits_updated = jiffies; | |
1204 | data->valid = 1; | |
1205 | } | |
1206 | ||
1207 | mutex_unlock(&data->lock); | |
1208 | ||
1209 | return data; | |
1210 | } | |
1211 | ||
1212 | static int __init sensors_adt7475_init(void) | |
1213 | { | |
1214 | return i2c_add_driver(&adt7475_driver); | |
1215 | } | |
1216 | ||
1217 | static void __exit sensors_adt7475_exit(void) | |
1218 | { | |
1219 | i2c_del_driver(&adt7475_driver); | |
1220 | } | |
1221 | ||
1222 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); | |
1223 | MODULE_DESCRIPTION("adt7475 driver"); | |
1224 | MODULE_LICENSE("GPL"); | |
1225 | ||
1226 | module_init(sensors_adt7475_init); | |
1227 | module_exit(sensors_adt7475_exit); |