]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
86aa3e22 GR |
2 | * adm1026.c - Part of lm_sensors, Linux kernel modules for hardware |
3 | * monitoring | |
4 | * Copyright (C) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com> | |
5 | * Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com> | |
6 | * | |
7 | * Chip details at: | |
8 | * | |
9 | * <http://www.onsemi.com/PowerSolutions/product.do?id=ADM1026> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | */ | |
1da177e4 | 25 | |
1da177e4 LT |
26 | #include <linux/module.h> |
27 | #include <linux/init.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/jiffies.h> | |
30 | #include <linux/i2c.h> | |
943b0830 | 31 | #include <linux/hwmon.h> |
303760b4 JD |
32 | #include <linux/hwmon-sysfs.h> |
33 | #include <linux/hwmon-vid.h> | |
943b0830 | 34 | #include <linux/err.h> |
9a61bf63 | 35 | #include <linux/mutex.h> |
1da177e4 LT |
36 | |
37 | /* Addresses to scan */ | |
25e9c86d | 38 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
1da177e4 | 39 | |
cb01a231 JD |
40 | static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, |
41 | -1, -1, -1, -1, -1, -1, -1, -1 }; | |
1da177e4 LT |
42 | static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, |
43 | -1, -1, -1, -1, -1, -1, -1, -1 }; | |
44 | static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
45 | -1, -1, -1, -1, -1, -1, -1, -1 }; | |
46 | static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
47 | -1, -1, -1, -1, -1, -1, -1, -1 }; | |
48 | static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; | |
cb01a231 JD |
49 | module_param_array(gpio_input, int, NULL, 0); |
50 | MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs"); | |
51 | module_param_array(gpio_output, int, NULL, 0); | |
b55f3757 GR |
52 | MODULE_PARM_DESC(gpio_output, |
53 | "List of GPIO pins (0-16) to program as outputs"); | |
cb01a231 | 54 | module_param_array(gpio_inverted, int, NULL, 0); |
b55f3757 GR |
55 | MODULE_PARM_DESC(gpio_inverted, |
56 | "List of GPIO pins (0-16) to program as inverted"); | |
cb01a231 | 57 | module_param_array(gpio_normal, int, NULL, 0); |
b55f3757 GR |
58 | MODULE_PARM_DESC(gpio_normal, |
59 | "List of GPIO pins (0-16) to program as normal/non-inverted"); | |
cb01a231 JD |
60 | module_param_array(gpio_fan, int, NULL, 0); |
61 | MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs"); | |
1da177e4 LT |
62 | |
63 | /* Many ADM1026 constants specified below */ | |
64 | ||
65 | /* The ADM1026 registers */ | |
cb01a231 JD |
66 | #define ADM1026_REG_CONFIG1 0x00 |
67 | #define CFG1_MONITOR 0x01 | |
68 | #define CFG1_INT_ENABLE 0x02 | |
69 | #define CFG1_INT_CLEAR 0x04 | |
70 | #define CFG1_AIN8_9 0x08 | |
71 | #define CFG1_THERM_HOT 0x10 | |
72 | #define CFG1_DAC_AFC 0x20 | |
73 | #define CFG1_PWM_AFC 0x40 | |
74 | #define CFG1_RESET 0x80 | |
75 | ||
76 | #define ADM1026_REG_CONFIG2 0x01 | |
1da177e4 | 77 | /* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */ |
cb01a231 JD |
78 | |
79 | #define ADM1026_REG_CONFIG3 0x07 | |
80 | #define CFG3_GPIO16_ENABLE 0x01 | |
81 | #define CFG3_CI_CLEAR 0x02 | |
82 | #define CFG3_VREF_250 0x04 | |
83 | #define CFG3_GPIO16_DIR 0x40 | |
84 | #define CFG3_GPIO16_POL 0x80 | |
85 | ||
86 | #define ADM1026_REG_E2CONFIG 0x13 | |
87 | #define E2CFG_READ 0x01 | |
88 | #define E2CFG_WRITE 0x02 | |
89 | #define E2CFG_ERASE 0x04 | |
90 | #define E2CFG_ROM 0x08 | |
91 | #define E2CFG_CLK_EXT 0x80 | |
1da177e4 | 92 | |
86aa3e22 GR |
93 | /* |
94 | * There are 10 general analog inputs and 7 dedicated inputs | |
1da177e4 LT |
95 | * They are: |
96 | * 0 - 9 = AIN0 - AIN9 | |
97 | * 10 = Vbat | |
98 | * 11 = 3.3V Standby | |
99 | * 12 = 3.3V Main | |
100 | * 13 = +5V | |
101 | * 14 = Vccp (CPU core voltage) | |
102 | * 15 = +12V | |
103 | * 16 = -12V | |
104 | */ | |
105 | static u16 ADM1026_REG_IN[] = { | |
106 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, | |
107 | 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a, | |
108 | 0x2b, 0x2c, 0x2d, 0x2e, 0x2f | |
109 | }; | |
110 | static u16 ADM1026_REG_IN_MIN[] = { | |
111 | 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, | |
112 | 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a, | |
113 | 0x4b, 0x4c, 0x4d, 0x4e, 0x4f | |
114 | }; | |
115 | static u16 ADM1026_REG_IN_MAX[] = { | |
116 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, | |
117 | 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42, | |
118 | 0x43, 0x44, 0x45, 0x46, 0x47 | |
119 | }; | |
120 | ||
86aa3e22 GR |
121 | /* |
122 | * Temperatures are: | |
1da177e4 LT |
123 | * 0 - Internal |
124 | * 1 - External 1 | |
125 | * 2 - External 2 | |
126 | */ | |
127 | static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 }; | |
128 | static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 }; | |
129 | static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 }; | |
130 | static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 }; | |
131 | static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f }; | |
132 | static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f }; | |
133 | ||
cb01a231 JD |
134 | #define ADM1026_REG_FAN(nr) (0x38 + (nr)) |
135 | #define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr)) | |
136 | #define ADM1026_REG_FAN_DIV_0_3 0x02 | |
137 | #define ADM1026_REG_FAN_DIV_4_7 0x03 | |
1da177e4 | 138 | |
cb01a231 JD |
139 | #define ADM1026_REG_DAC 0x04 |
140 | #define ADM1026_REG_PWM 0x05 | |
1da177e4 | 141 | |
cb01a231 JD |
142 | #define ADM1026_REG_GPIO_CFG_0_3 0x08 |
143 | #define ADM1026_REG_GPIO_CFG_4_7 0x09 | |
144 | #define ADM1026_REG_GPIO_CFG_8_11 0x0a | |
145 | #define ADM1026_REG_GPIO_CFG_12_15 0x0b | |
1da177e4 | 146 | /* CFG_16 in REG_CFG3 */ |
cb01a231 JD |
147 | #define ADM1026_REG_GPIO_STATUS_0_7 0x24 |
148 | #define ADM1026_REG_GPIO_STATUS_8_15 0x25 | |
1da177e4 | 149 | /* STATUS_16 in REG_STATUS4 */ |
cb01a231 JD |
150 | #define ADM1026_REG_GPIO_MASK_0_7 0x1c |
151 | #define ADM1026_REG_GPIO_MASK_8_15 0x1d | |
1da177e4 LT |
152 | /* MASK_16 in REG_MASK4 */ |
153 | ||
cb01a231 JD |
154 | #define ADM1026_REG_COMPANY 0x16 |
155 | #define ADM1026_REG_VERSTEP 0x17 | |
1da177e4 | 156 | /* These are the recognized values for the above regs */ |
cb01a231 JD |
157 | #define ADM1026_COMPANY_ANALOG_DEV 0x41 |
158 | #define ADM1026_VERSTEP_GENERIC 0x40 | |
159 | #define ADM1026_VERSTEP_ADM1026 0x44 | |
1da177e4 | 160 | |
cb01a231 JD |
161 | #define ADM1026_REG_MASK1 0x18 |
162 | #define ADM1026_REG_MASK2 0x19 | |
163 | #define ADM1026_REG_MASK3 0x1a | |
164 | #define ADM1026_REG_MASK4 0x1b | |
1da177e4 | 165 | |
cb01a231 JD |
166 | #define ADM1026_REG_STATUS1 0x20 |
167 | #define ADM1026_REG_STATUS2 0x21 | |
168 | #define ADM1026_REG_STATUS3 0x22 | |
169 | #define ADM1026_REG_STATUS4 0x23 | |
1da177e4 LT |
170 | |
171 | #define ADM1026_FAN_ACTIVATION_TEMP_HYST -6 | |
cb01a231 JD |
172 | #define ADM1026_FAN_CONTROL_TEMP_RANGE 20 |
173 | #define ADM1026_PWM_MAX 255 | |
1da177e4 | 174 | |
86aa3e22 GR |
175 | /* |
176 | * Conversions. Rounding and limit checking is only done on the TO_REG | |
1da177e4 LT |
177 | * variants. Note that you should be a bit careful with which arguments |
178 | * these macros are called: arguments may be evaluated more than once. | |
179 | */ | |
180 | ||
86aa3e22 GR |
181 | /* |
182 | * IN are scaled according to built-in resistors. These are the | |
1da177e4 LT |
183 | * voltages corresponding to 3/4 of full scale (192 or 0xc0) |
184 | * NOTE: The -12V input needs an additional factor to account | |
185 | * for the Vref pullup resistor. | |
186 | * NEG12_OFFSET = SCALE * Vref / V-192 - Vref | |
187 | * = 13875 * 2.50 / 1.875 - 2500 | |
188 | * = 16000 | |
189 | * | |
190 | * The values in this table are based on Table II, page 15 of the | |
191 | * datasheet. | |
192 | */ | |
cb01a231 JD |
193 | static int adm1026_scaling[] = { /* .001 Volts */ |
194 | 2250, 2250, 2250, 2250, 2250, 2250, | |
195 | 1875, 1875, 1875, 1875, 3000, 3330, | |
1da177e4 LT |
196 | 3330, 4995, 2250, 12000, 13875 |
197 | }; | |
198 | #define NEG12_OFFSET 16000 | |
cb01a231 | 199 | #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from)) |
2a844c14 | 200 | #define INS_TO_REG(n, val) (clamp_val(SCALE(val, adm1026_scaling[n], 192),\ |
cb01a231 JD |
201 | 0, 255)) |
202 | #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n])) | |
1da177e4 | 203 | |
86aa3e22 GR |
204 | /* |
205 | * FAN speed is measured using 22.5kHz clock and counts for 2 pulses | |
1da177e4 LT |
206 | * and we assume a 2 pulse-per-rev fan tach signal |
207 | * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000 | |
208 | */ | |
cb01a231 | 209 | #define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \ |
2a844c14 | 210 | clamp_val(1350000 / ((val) * (div)), \ |
86aa3e22 GR |
211 | 1, 254)) |
212 | #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 0xff ? 0 : \ | |
213 | 1350000 / ((val) * (div))) | |
214 | #define DIV_FROM_REG(val) (1 << (val)) | |
cb01a231 | 215 | #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0) |
1da177e4 LT |
216 | |
217 | /* Temperature is reported in 1 degC increments */ | |
2a844c14 | 218 | #define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \ |
86aa3e22 | 219 | / 1000, -127, 127)) |
1da177e4 | 220 | #define TEMP_FROM_REG(val) ((val) * 1000) |
2a844c14 | 221 | #define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \ |
86aa3e22 | 222 | / 1000, -127, 127)) |
1da177e4 LT |
223 | #define OFFSET_FROM_REG(val) ((val) * 1000) |
224 | ||
2a844c14 | 225 | #define PWM_TO_REG(val) (clamp_val(val, 0, 255)) |
1da177e4 LT |
226 | #define PWM_FROM_REG(val) (val) |
227 | ||
228 | #define PWM_MIN_TO_REG(val) ((val) & 0xf0) | |
229 | #define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4)) | |
230 | ||
86aa3e22 GR |
231 | /* |
232 | * Analog output is a voltage, and scaled to millivolts. The datasheet | |
cb01a231 | 233 | * indicates that the DAC could be used to drive the fans, but in our |
1da177e4 LT |
234 | * example board (Arima HDAMA) it isn't connected to the fans at all. |
235 | */ | |
2a844c14 | 236 | #define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255)) |
86aa3e22 | 237 | #define DAC_FROM_REG(val) (((val) * 2500) / 255) |
1da177e4 | 238 | |
86aa3e22 GR |
239 | /* |
240 | * Chip sampling rates | |
1da177e4 LT |
241 | * |
242 | * Some sensors are not updated more frequently than once per second | |
243 | * so it doesn't make sense to read them more often than that. | |
244 | * We cache the results and return the saved data if the driver | |
245 | * is called again before a second has elapsed. | |
246 | * | |
247 | * Also, there is significant configuration data for this chip | |
248 | * So, we keep the config data up to date in the cache | |
249 | * when it is written and only sample it once every 5 *minutes* | |
250 | */ | |
cb01a231 JD |
251 | #define ADM1026_DATA_INTERVAL (1 * HZ) |
252 | #define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ) | |
1da177e4 | 253 | |
86aa3e22 GR |
254 | /* |
255 | * We allow for multiple chips in a single system. | |
1da177e4 LT |
256 | * |
257 | * For each registered ADM1026, we need to keep state information | |
258 | * at client->data. The adm1026_data structure is dynamically | |
86aa3e22 GR |
259 | * allocated, when a new client structure is allocated. |
260 | */ | |
1da177e4 LT |
261 | |
262 | struct pwm_data { | |
263 | u8 pwm; | |
264 | u8 enable; | |
265 | u8 auto_pwm_min; | |
266 | }; | |
267 | ||
268 | struct adm1026_data { | |
b591d3c1 AL |
269 | struct i2c_client *client; |
270 | const struct attribute_group *groups[3]; | |
1da177e4 | 271 | |
9a61bf63 | 272 | struct mutex update_lock; |
1da177e4 LT |
273 | int valid; /* !=0 if following fields are valid */ |
274 | unsigned long last_reading; /* In jiffies */ | |
275 | unsigned long last_config; /* In jiffies */ | |
276 | ||
cb01a231 JD |
277 | u8 in[17]; /* Register value */ |
278 | u8 in_max[17]; /* Register value */ | |
279 | u8 in_min[17]; /* Register value */ | |
280 | s8 temp[3]; /* Register value */ | |
281 | s8 temp_min[3]; /* Register value */ | |
282 | s8 temp_max[3]; /* Register value */ | |
283 | s8 temp_tmin[3]; /* Register value */ | |
284 | s8 temp_crit[3]; /* Register value */ | |
285 | s8 temp_offset[3]; /* Register value */ | |
286 | u8 fan[8]; /* Register value */ | |
287 | u8 fan_min[8]; /* Register value */ | |
288 | u8 fan_div[8]; /* Decoded value */ | |
289 | struct pwm_data pwm1; /* Pwm control values */ | |
cb01a231 | 290 | u8 vrm; /* VRM version */ |
1da177e4 | 291 | u8 analog_out; /* Register value (DAC) */ |
cb01a231 JD |
292 | long alarms; /* Register encoding, combined */ |
293 | long alarm_mask; /* Register encoding, combined */ | |
294 | long gpio; /* Register encoding, combined */ | |
295 | long gpio_mask; /* Register encoding, combined */ | |
296 | u8 gpio_config[17]; /* Decoded value */ | |
297 | u8 config1; /* Register value */ | |
298 | u8 config2; /* Register value */ | |
299 | u8 config3; /* Register value */ | |
1da177e4 LT |
300 | }; |
301 | ||
c49efcef | 302 | static int adm1026_read_value(struct i2c_client *client, u8 reg) |
1da177e4 LT |
303 | { |
304 | int res; | |
305 | ||
306 | if (reg < 0x80) { | |
307 | /* "RAM" locations */ | |
308 | res = i2c_smbus_read_byte_data(client, reg) & 0xff; | |
309 | } else { | |
310 | /* EEPROM, do nothing */ | |
311 | res = 0; | |
312 | } | |
313 | return res; | |
314 | } | |
315 | ||
c49efcef | 316 | static int adm1026_write_value(struct i2c_client *client, u8 reg, int value) |
1da177e4 LT |
317 | { |
318 | int res; | |
319 | ||
320 | if (reg < 0x80) { | |
321 | /* "RAM" locations */ | |
322 | res = i2c_smbus_write_byte_data(client, reg, value); | |
323 | } else { | |
324 | /* EEPROM, do nothing */ | |
325 | res = 0; | |
326 | } | |
327 | return res; | |
328 | } | |
329 | ||
1da177e4 LT |
330 | static struct adm1026_data *adm1026_update_device(struct device *dev) |
331 | { | |
b591d3c1 AL |
332 | struct adm1026_data *data = dev_get_drvdata(dev); |
333 | struct i2c_client *client = data->client; | |
1da177e4 LT |
334 | int i; |
335 | long value, alarms, gpio; | |
336 | ||
9a61bf63 | 337 | mutex_lock(&data->update_lock); |
1da177e4 | 338 | if (!data->valid |
86aa3e22 GR |
339 | || time_after(jiffies, |
340 | data->last_reading + ADM1026_DATA_INTERVAL)) { | |
1da177e4 | 341 | /* Things that change quickly */ |
cb01a231 | 342 | dev_dbg(&client->dev, "Reading sensor values\n"); |
86aa3e22 | 343 | for (i = 0; i <= 16; ++i) { |
1da177e4 LT |
344 | data->in[i] = |
345 | adm1026_read_value(client, ADM1026_REG_IN[i]); | |
346 | } | |
347 | ||
86aa3e22 | 348 | for (i = 0; i <= 7; ++i) { |
1da177e4 LT |
349 | data->fan[i] = |
350 | adm1026_read_value(client, ADM1026_REG_FAN(i)); | |
351 | } | |
352 | ||
86aa3e22 GR |
353 | for (i = 0; i <= 2; ++i) { |
354 | /* | |
355 | * NOTE: temp[] is s8 and we assume 2's complement | |
356 | * "conversion" in the assignment | |
357 | */ | |
1da177e4 LT |
358 | data->temp[i] = |
359 | adm1026_read_value(client, ADM1026_REG_TEMP[i]); | |
360 | } | |
361 | ||
cb01a231 | 362 | data->pwm1.pwm = adm1026_read_value(client, |
1da177e4 | 363 | ADM1026_REG_PWM); |
cb01a231 | 364 | data->analog_out = adm1026_read_value(client, |
1da177e4 LT |
365 | ADM1026_REG_DAC); |
366 | /* GPIO16 is MSbit of alarms, move it to gpio */ | |
367 | alarms = adm1026_read_value(client, ADM1026_REG_STATUS4); | |
cb01a231 | 368 | gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */ |
1da177e4 LT |
369 | alarms &= 0x7f; |
370 | alarms <<= 8; | |
371 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3); | |
372 | alarms <<= 8; | |
373 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2); | |
374 | alarms <<= 8; | |
375 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1); | |
376 | data->alarms = alarms; | |
377 | ||
378 | /* Read the GPIO values */ | |
cb01a231 | 379 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
380 | ADM1026_REG_GPIO_STATUS_8_15); |
381 | gpio <<= 8; | |
cb01a231 | 382 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
383 | ADM1026_REG_GPIO_STATUS_0_7); |
384 | data->gpio = gpio; | |
385 | ||
386 | data->last_reading = jiffies; | |
a0393713 | 387 | } /* last_reading */ |
1da177e4 LT |
388 | |
389 | if (!data->valid || | |
390 | time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) { | |
391 | /* Things that don't change often */ | |
392 | dev_dbg(&client->dev, "Reading config values\n"); | |
86aa3e22 | 393 | for (i = 0; i <= 16; ++i) { |
cb01a231 | 394 | data->in_min[i] = adm1026_read_value(client, |
1da177e4 | 395 | ADM1026_REG_IN_MIN[i]); |
cb01a231 | 396 | data->in_max[i] = adm1026_read_value(client, |
1da177e4 LT |
397 | ADM1026_REG_IN_MAX[i]); |
398 | } | |
399 | ||
400 | value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) | |
401 | | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) | |
402 | << 8); | |
86aa3e22 | 403 | for (i = 0; i <= 7; ++i) { |
cb01a231 | 404 | data->fan_min[i] = adm1026_read_value(client, |
1da177e4 LT |
405 | ADM1026_REG_FAN_MIN(i)); |
406 | data->fan_div[i] = DIV_FROM_REG(value & 0x03); | |
407 | value >>= 2; | |
408 | } | |
409 | ||
410 | for (i = 0; i <= 2; ++i) { | |
86aa3e22 GR |
411 | /* |
412 | * NOTE: temp_xxx[] are s8 and we assume 2's | |
1da177e4 LT |
413 | * complement "conversion" in the assignment |
414 | */ | |
cb01a231 | 415 | data->temp_min[i] = adm1026_read_value(client, |
1da177e4 | 416 | ADM1026_REG_TEMP_MIN[i]); |
cb01a231 | 417 | data->temp_max[i] = adm1026_read_value(client, |
1da177e4 | 418 | ADM1026_REG_TEMP_MAX[i]); |
cb01a231 | 419 | data->temp_tmin[i] = adm1026_read_value(client, |
1da177e4 | 420 | ADM1026_REG_TEMP_TMIN[i]); |
cb01a231 | 421 | data->temp_crit[i] = adm1026_read_value(client, |
1da177e4 | 422 | ADM1026_REG_TEMP_THERM[i]); |
cb01a231 | 423 | data->temp_offset[i] = adm1026_read_value(client, |
1da177e4 LT |
424 | ADM1026_REG_TEMP_OFFSET[i]); |
425 | } | |
426 | ||
427 | /* Read the STATUS/alarm masks */ | |
cb01a231 JD |
428 | alarms = adm1026_read_value(client, ADM1026_REG_MASK4); |
429 | gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */ | |
430 | alarms = (alarms & 0x7f) << 8; | |
1da177e4 LT |
431 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK3); |
432 | alarms <<= 8; | |
433 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK2); | |
434 | alarms <<= 8; | |
435 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK1); | |
436 | data->alarm_mask = alarms; | |
437 | ||
438 | /* Read the GPIO values */ | |
cb01a231 | 439 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
440 | ADM1026_REG_GPIO_MASK_8_15); |
441 | gpio <<= 8; | |
442 | gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7); | |
443 | data->gpio_mask = gpio; | |
444 | ||
445 | /* Read various values from CONFIG1 */ | |
cb01a231 | 446 | data->config1 = adm1026_read_value(client, |
1da177e4 LT |
447 | ADM1026_REG_CONFIG1); |
448 | if (data->config1 & CFG1_PWM_AFC) { | |
449 | data->pwm1.enable = 2; | |
cb01a231 | 450 | data->pwm1.auto_pwm_min = |
1da177e4 LT |
451 | PWM_MIN_FROM_REG(data->pwm1.pwm); |
452 | } | |
453 | /* Read the GPIO config */ | |
cb01a231 | 454 | data->config2 = adm1026_read_value(client, |
1da177e4 | 455 | ADM1026_REG_CONFIG2); |
cb01a231 | 456 | data->config3 = adm1026_read_value(client, |
1da177e4 LT |
457 | ADM1026_REG_CONFIG3); |
458 | data->gpio_config[16] = (data->config3 >> 6) & 0x03; | |
459 | ||
460 | value = 0; | |
86aa3e22 | 461 | for (i = 0; i <= 15; ++i) { |
1da177e4 LT |
462 | if ((i & 0x03) == 0) { |
463 | value = adm1026_read_value(client, | |
464 | ADM1026_REG_GPIO_CFG_0_3 + i/4); | |
465 | } | |
466 | data->gpio_config[i] = value & 0x03; | |
467 | value >>= 2; | |
468 | } | |
469 | ||
470 | data->last_config = jiffies; | |
a0393713 | 471 | } /* last_config */ |
1da177e4 | 472 | |
1da177e4 | 473 | data->valid = 1; |
9a61bf63 | 474 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
475 | return data; |
476 | } | |
477 | ||
050480f1 YI |
478 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
479 | char *buf) | |
1da177e4 | 480 | { |
050480f1 YI |
481 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
482 | int nr = sensor_attr->index; | |
1da177e4 | 483 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 484 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr])); |
1da177e4 | 485 | } |
050480f1 YI |
486 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
487 | char *buf) | |
1da177e4 | 488 | { |
050480f1 YI |
489 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
490 | int nr = sensor_attr->index; | |
cb01a231 JD |
491 | struct adm1026_data *data = adm1026_update_device(dev); |
492 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr])); | |
1da177e4 | 493 | } |
050480f1 YI |
494 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
495 | const char *buf, size_t count) | |
1da177e4 | 496 | { |
050480f1 YI |
497 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
498 | int nr = sensor_attr->index; | |
b591d3c1 AL |
499 | struct adm1026_data *data = dev_get_drvdata(dev); |
500 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
501 | long val; |
502 | int err; | |
503 | ||
504 | err = kstrtol(buf, 10, &val); | |
505 | if (err) | |
506 | return err; | |
1da177e4 | 507 | |
9a61bf63 | 508 | mutex_lock(&data->update_lock); |
1da177e4 LT |
509 | data->in_min[nr] = INS_TO_REG(nr, val); |
510 | adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]); | |
9a61bf63 | 511 | mutex_unlock(&data->update_lock); |
cb01a231 | 512 | return count; |
1da177e4 | 513 | } |
050480f1 YI |
514 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
515 | char *buf) | |
1da177e4 | 516 | { |
050480f1 YI |
517 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
518 | int nr = sensor_attr->index; | |
1da177e4 | 519 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 520 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr])); |
1da177e4 | 521 | } |
050480f1 YI |
522 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
523 | const char *buf, size_t count) | |
1da177e4 | 524 | { |
050480f1 YI |
525 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
526 | int nr = sensor_attr->index; | |
b591d3c1 AL |
527 | struct adm1026_data *data = dev_get_drvdata(dev); |
528 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
529 | long val; |
530 | int err; | |
531 | ||
532 | err = kstrtol(buf, 10, &val); | |
533 | if (err) | |
534 | return err; | |
1da177e4 | 535 | |
9a61bf63 | 536 | mutex_lock(&data->update_lock); |
1da177e4 LT |
537 | data->in_max[nr] = INS_TO_REG(nr, val); |
538 | adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]); | |
9a61bf63 | 539 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
540 | return count; |
541 | } | |
542 | ||
050480f1 YI |
543 | #define in_reg(offset) \ |
544 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \ | |
545 | NULL, offset); \ | |
546 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
547 | show_in_min, set_in_min, offset); \ | |
548 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
549 | show_in_max, set_in_max, offset); | |
1da177e4 LT |
550 | |
551 | ||
552 | in_reg(0); | |
553 | in_reg(1); | |
554 | in_reg(2); | |
555 | in_reg(3); | |
556 | in_reg(4); | |
557 | in_reg(5); | |
558 | in_reg(6); | |
559 | in_reg(7); | |
560 | in_reg(8); | |
561 | in_reg(9); | |
562 | in_reg(10); | |
563 | in_reg(11); | |
564 | in_reg(12); | |
565 | in_reg(13); | |
566 | in_reg(14); | |
567 | in_reg(15); | |
568 | ||
86aa3e22 GR |
569 | static ssize_t show_in16(struct device *dev, struct device_attribute *attr, |
570 | char *buf) | |
1da177e4 LT |
571 | { |
572 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 573 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) - |
1da177e4 LT |
574 | NEG12_OFFSET); |
575 | } | |
86aa3e22 GR |
576 | static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, |
577 | char *buf) | |
1da177e4 | 578 | { |
cb01a231 JD |
579 | struct adm1026_data *data = adm1026_update_device(dev); |
580 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16]) | |
1da177e4 LT |
581 | - NEG12_OFFSET); |
582 | } | |
86aa3e22 GR |
583 | static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, |
584 | const char *buf, size_t count) | |
1da177e4 | 585 | { |
b591d3c1 AL |
586 | struct adm1026_data *data = dev_get_drvdata(dev); |
587 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
588 | long val; |
589 | int err; | |
590 | ||
591 | err = kstrtol(buf, 10, &val); | |
592 | if (err) | |
593 | return err; | |
1da177e4 | 594 | |
9a61bf63 | 595 | mutex_lock(&data->update_lock); |
1da177e4 LT |
596 | data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET); |
597 | adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]); | |
9a61bf63 | 598 | mutex_unlock(&data->update_lock); |
cb01a231 | 599 | return count; |
1da177e4 | 600 | } |
86aa3e22 GR |
601 | static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, |
602 | char *buf) | |
1da177e4 LT |
603 | { |
604 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 605 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16]) |
1da177e4 LT |
606 | - NEG12_OFFSET); |
607 | } | |
86aa3e22 GR |
608 | static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, |
609 | const char *buf, size_t count) | |
1da177e4 | 610 | { |
b591d3c1 AL |
611 | struct adm1026_data *data = dev_get_drvdata(dev); |
612 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
613 | long val; |
614 | int err; | |
615 | ||
616 | err = kstrtol(buf, 10, &val); | |
617 | if (err) | |
618 | return err; | |
1da177e4 | 619 | |
9a61bf63 | 620 | mutex_lock(&data->update_lock); |
1da177e4 LT |
621 | data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET); |
622 | adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]); | |
9a61bf63 | 623 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
624 | return count; |
625 | } | |
626 | ||
050480f1 | 627 | static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16); |
86aa3e22 GR |
628 | static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, |
629 | set_in16_min, 16); | |
630 | static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, | |
631 | set_in16_max, 16); | |
1da177e4 LT |
632 | |
633 | ||
634 | /* Now add fan read/write functions */ | |
635 | ||
050480f1 YI |
636 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
637 | char *buf) | |
1da177e4 | 638 | { |
050480f1 YI |
639 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
640 | int nr = sensor_attr->index; | |
1da177e4 | 641 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 642 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
1da177e4 LT |
643 | data->fan_div[nr])); |
644 | } | |
050480f1 YI |
645 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
646 | char *buf) | |
1da177e4 | 647 | { |
050480f1 YI |
648 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
649 | int nr = sensor_attr->index; | |
1da177e4 | 650 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 651 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
1da177e4 LT |
652 | data->fan_div[nr])); |
653 | } | |
050480f1 YI |
654 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
655 | const char *buf, size_t count) | |
1da177e4 | 656 | { |
050480f1 YI |
657 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
658 | int nr = sensor_attr->index; | |
b591d3c1 AL |
659 | struct adm1026_data *data = dev_get_drvdata(dev); |
660 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
661 | long val; |
662 | int err; | |
663 | ||
664 | err = kstrtol(buf, 10, &val); | |
665 | if (err) | |
666 | return err; | |
1da177e4 | 667 | |
9a61bf63 | 668 | mutex_lock(&data->update_lock); |
1da177e4 LT |
669 | data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]); |
670 | adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr), | |
671 | data->fan_min[nr]); | |
9a61bf63 | 672 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
673 | return count; |
674 | } | |
675 | ||
cb01a231 JD |
676 | #define fan_offset(offset) \ |
677 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \ | |
678 | offset - 1); \ | |
679 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
050480f1 | 680 | show_fan_min, set_fan_min, offset - 1); |
1da177e4 LT |
681 | |
682 | fan_offset(1); | |
683 | fan_offset(2); | |
684 | fan_offset(3); | |
685 | fan_offset(4); | |
686 | fan_offset(5); | |
687 | fan_offset(6); | |
688 | fan_offset(7); | |
689 | fan_offset(8); | |
690 | ||
691 | /* Adjust fan_min to account for new fan divisor */ | |
692 | static void fixup_fan_min(struct device *dev, int fan, int old_div) | |
693 | { | |
b591d3c1 AL |
694 | struct adm1026_data *data = dev_get_drvdata(dev); |
695 | struct i2c_client *client = data->client; | |
cb01a231 JD |
696 | int new_min; |
697 | int new_div = data->fan_div[fan]; | |
1da177e4 LT |
698 | |
699 | /* 0 and 0xff are special. Don't adjust them */ | |
86aa3e22 | 700 | if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) |
1da177e4 | 701 | return; |
1da177e4 LT |
702 | |
703 | new_min = data->fan_min[fan] * old_div / new_div; | |
2a844c14 | 704 | new_min = clamp_val(new_min, 1, 254); |
1da177e4 LT |
705 | data->fan_min[fan] = new_min; |
706 | adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min); | |
707 | } | |
708 | ||
709 | /* Now add fan_div read/write functions */ | |
050480f1 YI |
710 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
711 | char *buf) | |
1da177e4 | 712 | { |
050480f1 YI |
713 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
714 | int nr = sensor_attr->index; | |
1da177e4 | 715 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 716 | return sprintf(buf, "%d\n", data->fan_div[nr]); |
1da177e4 | 717 | } |
050480f1 YI |
718 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
719 | const char *buf, size_t count) | |
1da177e4 | 720 | { |
050480f1 YI |
721 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
722 | int nr = sensor_attr->index; | |
b591d3c1 AL |
723 | struct adm1026_data *data = dev_get_drvdata(dev); |
724 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
725 | long val; |
726 | int orig_div, new_div; | |
727 | int err; | |
728 | ||
729 | err = kstrtol(buf, 10, &val); | |
730 | if (err) | |
731 | return err; | |
1da177e4 | 732 | |
cb01a231 | 733 | new_div = DIV_TO_REG(val); |
8b0f1840 | 734 | |
9a61bf63 | 735 | mutex_lock(&data->update_lock); |
1da177e4 LT |
736 | orig_div = data->fan_div[nr]; |
737 | data->fan_div[nr] = DIV_FROM_REG(new_div); | |
738 | ||
739 | if (nr < 4) { /* 0 <= nr < 4 */ | |
1da177e4 | 740 | adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3, |
52bc9802 GG |
741 | (DIV_TO_REG(data->fan_div[0]) << 0) | |
742 | (DIV_TO_REG(data->fan_div[1]) << 2) | | |
743 | (DIV_TO_REG(data->fan_div[2]) << 4) | | |
744 | (DIV_TO_REG(data->fan_div[3]) << 6)); | |
1da177e4 | 745 | } else { /* 3 < nr < 8 */ |
1da177e4 | 746 | adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7, |
52bc9802 GG |
747 | (DIV_TO_REG(data->fan_div[4]) << 0) | |
748 | (DIV_TO_REG(data->fan_div[5]) << 2) | | |
749 | (DIV_TO_REG(data->fan_div[6]) << 4) | | |
750 | (DIV_TO_REG(data->fan_div[7]) << 6)); | |
1da177e4 LT |
751 | } |
752 | ||
86aa3e22 | 753 | if (data->fan_div[nr] != orig_div) |
cb01a231 | 754 | fixup_fan_min(dev, nr, orig_div); |
86aa3e22 | 755 | |
9a61bf63 | 756 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
757 | return count; |
758 | } | |
759 | ||
cb01a231 JD |
760 | #define fan_offset_div(offset) \ |
761 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
050480f1 | 762 | show_fan_div, set_fan_div, offset - 1); |
1da177e4 LT |
763 | |
764 | fan_offset_div(1); | |
765 | fan_offset_div(2); | |
766 | fan_offset_div(3); | |
767 | fan_offset_div(4); | |
768 | fan_offset_div(5); | |
769 | fan_offset_div(6); | |
770 | fan_offset_div(7); | |
771 | fan_offset_div(8); | |
772 | ||
773 | /* Temps */ | |
050480f1 YI |
774 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
775 | char *buf) | |
1da177e4 | 776 | { |
050480f1 YI |
777 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
778 | int nr = sensor_attr->index; | |
1da177e4 | 779 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 780 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
1da177e4 | 781 | } |
050480f1 YI |
782 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
783 | char *buf) | |
1da177e4 | 784 | { |
050480f1 YI |
785 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
786 | int nr = sensor_attr->index; | |
1da177e4 | 787 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 788 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
1da177e4 | 789 | } |
050480f1 YI |
790 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
791 | const char *buf, size_t count) | |
1da177e4 | 792 | { |
050480f1 YI |
793 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
794 | int nr = sensor_attr->index; | |
b591d3c1 AL |
795 | struct adm1026_data *data = dev_get_drvdata(dev); |
796 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
797 | long val; |
798 | int err; | |
799 | ||
800 | err = kstrtol(buf, 10, &val); | |
801 | if (err) | |
802 | return err; | |
1da177e4 | 803 | |
9a61bf63 | 804 | mutex_lock(&data->update_lock); |
1da177e4 LT |
805 | data->temp_min[nr] = TEMP_TO_REG(val); |
806 | adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr], | |
807 | data->temp_min[nr]); | |
9a61bf63 | 808 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
809 | return count; |
810 | } | |
050480f1 YI |
811 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
812 | char *buf) | |
1da177e4 | 813 | { |
050480f1 YI |
814 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
815 | int nr = sensor_attr->index; | |
1da177e4 | 816 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 817 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
1da177e4 | 818 | } |
050480f1 YI |
819 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
820 | const char *buf, size_t count) | |
1da177e4 | 821 | { |
050480f1 YI |
822 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
823 | int nr = sensor_attr->index; | |
b591d3c1 AL |
824 | struct adm1026_data *data = dev_get_drvdata(dev); |
825 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
826 | long val; |
827 | int err; | |
828 | ||
829 | err = kstrtol(buf, 10, &val); | |
830 | if (err) | |
831 | return err; | |
1da177e4 | 832 | |
9a61bf63 | 833 | mutex_lock(&data->update_lock); |
1da177e4 LT |
834 | data->temp_max[nr] = TEMP_TO_REG(val); |
835 | adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr], | |
836 | data->temp_max[nr]); | |
9a61bf63 | 837 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
838 | return count; |
839 | } | |
050480f1 YI |
840 | |
841 | #define temp_reg(offset) \ | |
cb01a231 | 842 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \ |
050480f1 YI |
843 | NULL, offset - 1); \ |
844 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
845 | show_temp_min, set_temp_min, offset - 1); \ | |
846 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
847 | show_temp_max, set_temp_max, offset - 1); | |
1da177e4 LT |
848 | |
849 | ||
850 | temp_reg(1); | |
851 | temp_reg(2); | |
852 | temp_reg(3); | |
853 | ||
050480f1 YI |
854 | static ssize_t show_temp_offset(struct device *dev, |
855 | struct device_attribute *attr, char *buf) | |
1da177e4 | 856 | { |
050480f1 YI |
857 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
858 | int nr = sensor_attr->index; | |
1da177e4 | 859 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 860 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr])); |
1da177e4 | 861 | } |
050480f1 YI |
862 | static ssize_t set_temp_offset(struct device *dev, |
863 | struct device_attribute *attr, const char *buf, | |
864 | size_t count) | |
1da177e4 | 865 | { |
050480f1 YI |
866 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
867 | int nr = sensor_attr->index; | |
b591d3c1 AL |
868 | struct adm1026_data *data = dev_get_drvdata(dev); |
869 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
870 | long val; |
871 | int err; | |
872 | ||
873 | err = kstrtol(buf, 10, &val); | |
874 | if (err) | |
875 | return err; | |
1da177e4 | 876 | |
9a61bf63 | 877 | mutex_lock(&data->update_lock); |
1da177e4 LT |
878 | data->temp_offset[nr] = TEMP_TO_REG(val); |
879 | adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr], | |
880 | data->temp_offset[nr]); | |
9a61bf63 | 881 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
882 | return count; |
883 | } | |
884 | ||
86aa3e22 GR |
885 | #define temp_offset_reg(offset) \ |
886 | static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \ | |
050480f1 | 887 | show_temp_offset, set_temp_offset, offset - 1); |
1da177e4 LT |
888 | |
889 | temp_offset_reg(1); | |
890 | temp_offset_reg(2); | |
891 | temp_offset_reg(3); | |
892 | ||
050480f1 YI |
893 | static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev, |
894 | struct device_attribute *attr, char *buf) | |
1da177e4 | 895 | { |
050480f1 YI |
896 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
897 | int nr = sensor_attr->index; | |
1da177e4 | 898 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 899 | return sprintf(buf, "%d\n", TEMP_FROM_REG( |
1da177e4 LT |
900 | ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr])); |
901 | } | |
050480f1 YI |
902 | static ssize_t show_temp_auto_point2_temp(struct device *dev, |
903 | struct device_attribute *attr, char *buf) | |
1da177e4 | 904 | { |
050480f1 YI |
905 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
906 | int nr = sensor_attr->index; | |
1da177e4 | 907 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 908 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] + |
1da177e4 LT |
909 | ADM1026_FAN_CONTROL_TEMP_RANGE)); |
910 | } | |
050480f1 YI |
911 | static ssize_t show_temp_auto_point1_temp(struct device *dev, |
912 | struct device_attribute *attr, char *buf) | |
1da177e4 | 913 | { |
050480f1 YI |
914 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
915 | int nr = sensor_attr->index; | |
1da177e4 | 916 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 917 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr])); |
1da177e4 | 918 | } |
050480f1 YI |
919 | static ssize_t set_temp_auto_point1_temp(struct device *dev, |
920 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 921 | { |
050480f1 YI |
922 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
923 | int nr = sensor_attr->index; | |
b591d3c1 AL |
924 | struct adm1026_data *data = dev_get_drvdata(dev); |
925 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
926 | long val; |
927 | int err; | |
928 | ||
929 | err = kstrtol(buf, 10, &val); | |
930 | if (err) | |
931 | return err; | |
1da177e4 | 932 | |
9a61bf63 | 933 | mutex_lock(&data->update_lock); |
1da177e4 LT |
934 | data->temp_tmin[nr] = TEMP_TO_REG(val); |
935 | adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr], | |
936 | data->temp_tmin[nr]); | |
9a61bf63 | 937 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
938 | return count; |
939 | } | |
940 | ||
cb01a231 JD |
941 | #define temp_auto_point(offset) \ |
942 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \ | |
943 | S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \ | |
944 | set_temp_auto_point1_temp, offset - 1); \ | |
945 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\ | |
946 | show_temp_auto_point1_temp_hyst, NULL, offset - 1); \ | |
947 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \ | |
050480f1 | 948 | show_temp_auto_point2_temp, NULL, offset - 1); |
1da177e4 LT |
949 | |
950 | temp_auto_point(1); | |
951 | temp_auto_point(2); | |
952 | temp_auto_point(3); | |
953 | ||
050480f1 YI |
954 | static ssize_t show_temp_crit_enable(struct device *dev, |
955 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
956 | { |
957 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 958 | return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4); |
1da177e4 | 959 | } |
050480f1 YI |
960 | static ssize_t set_temp_crit_enable(struct device *dev, |
961 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 962 | { |
b591d3c1 AL |
963 | struct adm1026_data *data = dev_get_drvdata(dev); |
964 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
965 | unsigned long val; |
966 | int err; | |
967 | ||
968 | err = kstrtoul(buf, 10, &val); | |
969 | if (err) | |
970 | return err; | |
971 | ||
972 | if (val > 1) | |
973 | return -EINVAL; | |
974 | ||
975 | mutex_lock(&data->update_lock); | |
976 | data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4); | |
977 | adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1); | |
978 | mutex_unlock(&data->update_lock); | |
1da177e4 | 979 | |
1da177e4 LT |
980 | return count; |
981 | } | |
982 | ||
050480f1 YI |
983 | #define temp_crit_enable(offset) \ |
984 | static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \ | |
1da177e4 LT |
985 | show_temp_crit_enable, set_temp_crit_enable); |
986 | ||
050480f1 YI |
987 | temp_crit_enable(1); |
988 | temp_crit_enable(2); | |
989 | temp_crit_enable(3); | |
1da177e4 | 990 | |
050480f1 YI |
991 | static ssize_t show_temp_crit(struct device *dev, |
992 | struct device_attribute *attr, char *buf) | |
1da177e4 | 993 | { |
050480f1 YI |
994 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
995 | int nr = sensor_attr->index; | |
1da177e4 | 996 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 997 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr])); |
1da177e4 | 998 | } |
050480f1 YI |
999 | static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, |
1000 | const char *buf, size_t count) | |
1da177e4 | 1001 | { |
050480f1 YI |
1002 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
1003 | int nr = sensor_attr->index; | |
b591d3c1 AL |
1004 | struct adm1026_data *data = dev_get_drvdata(dev); |
1005 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
1006 | long val; |
1007 | int err; | |
1008 | ||
1009 | err = kstrtol(buf, 10, &val); | |
1010 | if (err) | |
1011 | return err; | |
1da177e4 | 1012 | |
9a61bf63 | 1013 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1014 | data->temp_crit[nr] = TEMP_TO_REG(val); |
1015 | adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr], | |
1016 | data->temp_crit[nr]); | |
9a61bf63 | 1017 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1018 | return count; |
1019 | } | |
1020 | ||
050480f1 YI |
1021 | #define temp_crit_reg(offset) \ |
1022 | static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \ | |
1023 | show_temp_crit, set_temp_crit, offset - 1); | |
1da177e4 LT |
1024 | |
1025 | temp_crit_reg(1); | |
1026 | temp_crit_reg(2); | |
1027 | temp_crit_reg(3); | |
1028 | ||
86aa3e22 GR |
1029 | static ssize_t show_analog_out_reg(struct device *dev, |
1030 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1031 | { |
1032 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1033 | return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out)); |
1da177e4 | 1034 | } |
86aa3e22 GR |
1035 | static ssize_t set_analog_out_reg(struct device *dev, |
1036 | struct device_attribute *attr, | |
1037 | const char *buf, size_t count) | |
1da177e4 | 1038 | { |
b591d3c1 AL |
1039 | struct adm1026_data *data = dev_get_drvdata(dev); |
1040 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
1041 | long val; |
1042 | int err; | |
1043 | ||
1044 | err = kstrtol(buf, 10, &val); | |
1045 | if (err) | |
1046 | return err; | |
1da177e4 | 1047 | |
9a61bf63 | 1048 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1049 | data->analog_out = DAC_TO_REG(val); |
1050 | adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out); | |
9a61bf63 | 1051 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1052 | return count; |
1053 | } | |
1054 | ||
cb01a231 | 1055 | static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg, |
1da177e4 LT |
1056 | set_analog_out_reg); |
1057 | ||
86aa3e22 GR |
1058 | static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, |
1059 | char *buf) | |
1da177e4 LT |
1060 | { |
1061 | struct adm1026_data *data = adm1026_update_device(dev); | |
a0cf354a JD |
1062 | int vid = (data->gpio >> 11) & 0x1f; |
1063 | ||
1064 | dev_dbg(dev, "Setting VID from GPIO11-15.\n"); | |
1065 | return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm)); | |
1da177e4 | 1066 | } |
86aa3e22 | 1067 | |
937df8df | 1068 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); |
1da177e4 | 1069 | |
86aa3e22 GR |
1070 | static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, |
1071 | char *buf) | |
1da177e4 | 1072 | { |
90d6619a | 1073 | struct adm1026_data *data = dev_get_drvdata(dev); |
cb01a231 | 1074 | return sprintf(buf, "%d\n", data->vrm); |
1da177e4 | 1075 | } |
86aa3e22 GR |
1076 | |
1077 | static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, | |
1078 | const char *buf, size_t count) | |
1da177e4 | 1079 | { |
f67fdabf | 1080 | struct adm1026_data *data = dev_get_drvdata(dev); |
86aa3e22 GR |
1081 | unsigned long val; |
1082 | int err; | |
1083 | ||
1084 | err = kstrtoul(buf, 10, &val); | |
1085 | if (err) | |
1086 | return err; | |
1da177e4 | 1087 | |
a4461647 AL |
1088 | if (val > 255) |
1089 | return -EINVAL; | |
1090 | ||
86aa3e22 | 1091 | data->vrm = val; |
1da177e4 LT |
1092 | return count; |
1093 | } | |
1094 | ||
1095 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); | |
1096 | ||
86aa3e22 GR |
1097 | static ssize_t show_alarms_reg(struct device *dev, |
1098 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1099 | { |
1100 | struct adm1026_data *data = adm1026_update_device(dev); | |
f67fdabf | 1101 | return sprintf(buf, "%ld\n", data->alarms); |
1da177e4 LT |
1102 | } |
1103 | ||
1104 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); | |
1105 | ||
a9273cb8 JD |
1106 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
1107 | char *buf) | |
1108 | { | |
1109 | struct adm1026_data *data = adm1026_update_device(dev); | |
1110 | int bitnr = to_sensor_dev_attr(attr)->index; | |
1111 | return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1); | |
1112 | } | |
1113 | ||
1114 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0); | |
1115 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1); | |
1116 | static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1); | |
1117 | static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2); | |
1118 | static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3); | |
1119 | static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4); | |
1120 | static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5); | |
1121 | static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6); | |
1122 | static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7); | |
1123 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8); | |
1124 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9); | |
1125 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10); | |
1126 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
1127 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12); | |
1128 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13); | |
1129 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14); | |
1130 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15); | |
1131 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16); | |
1132 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17); | |
1133 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18); | |
1134 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19); | |
1135 | static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20); | |
1136 | static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21); | |
1137 | static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22); | |
1138 | static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23); | |
1139 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24); | |
1140 | static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25); | |
1141 | static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26); | |
1142 | ||
86aa3e22 GR |
1143 | static ssize_t show_alarm_mask(struct device *dev, |
1144 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1145 | { |
1146 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1147 | return sprintf(buf, "%ld\n", data->alarm_mask); |
1da177e4 | 1148 | } |
86aa3e22 GR |
1149 | static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, |
1150 | const char *buf, size_t count) | |
1da177e4 | 1151 | { |
b591d3c1 AL |
1152 | struct adm1026_data *data = dev_get_drvdata(dev); |
1153 | struct i2c_client *client = data->client; | |
1da177e4 | 1154 | unsigned long mask; |
86aa3e22 GR |
1155 | long val; |
1156 | int err; | |
1157 | ||
1158 | err = kstrtol(buf, 10, &val); | |
1159 | if (err) | |
1160 | return err; | |
1da177e4 | 1161 | |
9a61bf63 | 1162 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1163 | data->alarm_mask = val & 0x7fffffff; |
1164 | mask = data->alarm_mask | |
1165 | | (data->gpio_mask & 0x10000 ? 0x80000000 : 0); | |
1166 | adm1026_write_value(client, ADM1026_REG_MASK1, | |
1167 | mask & 0xff); | |
1168 | mask >>= 8; | |
1169 | adm1026_write_value(client, ADM1026_REG_MASK2, | |
1170 | mask & 0xff); | |
1171 | mask >>= 8; | |
1172 | adm1026_write_value(client, ADM1026_REG_MASK3, | |
1173 | mask & 0xff); | |
1174 | mask >>= 8; | |
1175 | adm1026_write_value(client, ADM1026_REG_MASK4, | |
1176 | mask & 0xff); | |
9a61bf63 | 1177 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1178 | return count; |
1179 | } | |
1180 | ||
1181 | static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask, | |
1182 | set_alarm_mask); | |
1183 | ||
1184 | ||
86aa3e22 GR |
1185 | static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, |
1186 | char *buf) | |
1da177e4 LT |
1187 | { |
1188 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1189 | return sprintf(buf, "%ld\n", data->gpio); |
1da177e4 | 1190 | } |
86aa3e22 GR |
1191 | static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, |
1192 | const char *buf, size_t count) | |
1da177e4 | 1193 | { |
b591d3c1 AL |
1194 | struct adm1026_data *data = dev_get_drvdata(dev); |
1195 | struct i2c_client *client = data->client; | |
cb01a231 | 1196 | long gpio; |
86aa3e22 GR |
1197 | long val; |
1198 | int err; | |
1199 | ||
1200 | err = kstrtol(buf, 10, &val); | |
1201 | if (err) | |
1202 | return err; | |
1da177e4 | 1203 | |
9a61bf63 | 1204 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1205 | data->gpio = val & 0x1ffff; |
1206 | gpio = data->gpio; | |
cb01a231 | 1207 | adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff); |
1da177e4 | 1208 | gpio >>= 8; |
cb01a231 | 1209 | adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff); |
1da177e4 | 1210 | gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f); |
cb01a231 | 1211 | adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff); |
9a61bf63 | 1212 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1213 | return count; |
1214 | } | |
1215 | ||
1216 | static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio); | |
1217 | ||
86aa3e22 GR |
1218 | static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, |
1219 | char *buf) | |
1da177e4 LT |
1220 | { |
1221 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1222 | return sprintf(buf, "%ld\n", data->gpio_mask); |
1da177e4 | 1223 | } |
86aa3e22 GR |
1224 | static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, |
1225 | const char *buf, size_t count) | |
1da177e4 | 1226 | { |
b591d3c1 AL |
1227 | struct adm1026_data *data = dev_get_drvdata(dev); |
1228 | struct i2c_client *client = data->client; | |
cb01a231 | 1229 | long mask; |
86aa3e22 GR |
1230 | long val; |
1231 | int err; | |
1232 | ||
1233 | err = kstrtol(buf, 10, &val); | |
1234 | if (err) | |
1235 | return err; | |
1da177e4 | 1236 | |
9a61bf63 | 1237 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1238 | data->gpio_mask = val & 0x1ffff; |
1239 | mask = data->gpio_mask; | |
cb01a231 | 1240 | adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff); |
1da177e4 | 1241 | mask >>= 8; |
cb01a231 | 1242 | adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff); |
1da177e4 | 1243 | mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f); |
cb01a231 | 1244 | adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff); |
9a61bf63 | 1245 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1246 | return count; |
1247 | } | |
1248 | ||
1249 | static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask); | |
1250 | ||
86aa3e22 GR |
1251 | static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, |
1252 | char *buf) | |
1da177e4 LT |
1253 | { |
1254 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1255 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm)); |
1da177e4 | 1256 | } |
86aa3e22 GR |
1257 | |
1258 | static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, | |
1259 | const char *buf, size_t count) | |
1da177e4 | 1260 | { |
b591d3c1 AL |
1261 | struct adm1026_data *data = dev_get_drvdata(dev); |
1262 | struct i2c_client *client = data->client; | |
1da177e4 LT |
1263 | |
1264 | if (data->pwm1.enable == 1) { | |
86aa3e22 GR |
1265 | long val; |
1266 | int err; | |
1267 | ||
1268 | err = kstrtol(buf, 10, &val); | |
1269 | if (err) | |
1270 | return err; | |
1da177e4 | 1271 | |
9a61bf63 | 1272 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1273 | data->pwm1.pwm = PWM_TO_REG(val); |
1274 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
9a61bf63 | 1275 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1276 | } |
1277 | return count; | |
1278 | } | |
86aa3e22 GR |
1279 | |
1280 | static ssize_t show_auto_pwm_min(struct device *dev, | |
1281 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1282 | { |
1283 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1284 | return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min); |
1da177e4 | 1285 | } |
86aa3e22 GR |
1286 | |
1287 | static ssize_t set_auto_pwm_min(struct device *dev, | |
1288 | struct device_attribute *attr, const char *buf, | |
1289 | size_t count) | |
1da177e4 | 1290 | { |
b591d3c1 AL |
1291 | struct adm1026_data *data = dev_get_drvdata(dev); |
1292 | struct i2c_client *client = data->client; | |
86aa3e22 GR |
1293 | unsigned long val; |
1294 | int err; | |
1295 | ||
1296 | err = kstrtoul(buf, 10, &val); | |
1297 | if (err) | |
1298 | return err; | |
1da177e4 | 1299 | |
9a61bf63 | 1300 | mutex_lock(&data->update_lock); |
2a844c14 | 1301 | data->pwm1.auto_pwm_min = clamp_val(val, 0, 255); |
1da177e4 LT |
1302 | if (data->pwm1.enable == 2) { /* apply immediately */ |
1303 | data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) | | |
cb01a231 | 1304 | PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); |
1da177e4 LT |
1305 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); |
1306 | } | |
9a61bf63 | 1307 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1308 | return count; |
1309 | } | |
86aa3e22 GR |
1310 | |
1311 | static ssize_t show_auto_pwm_max(struct device *dev, | |
1312 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1313 | { |
cb01a231 | 1314 | return sprintf(buf, "%d\n", ADM1026_PWM_MAX); |
1da177e4 | 1315 | } |
86aa3e22 GR |
1316 | |
1317 | static ssize_t show_pwm_enable(struct device *dev, | |
1318 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1319 | { |
1320 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1321 | return sprintf(buf, "%d\n", data->pwm1.enable); |
1da177e4 | 1322 | } |
86aa3e22 GR |
1323 | |
1324 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, | |
1325 | const char *buf, size_t count) | |
1da177e4 | 1326 | { |
b591d3c1 AL |
1327 | struct adm1026_data *data = dev_get_drvdata(dev); |
1328 | struct i2c_client *client = data->client; | |
cb01a231 | 1329 | int old_enable; |
86aa3e22 GR |
1330 | unsigned long val; |
1331 | int err; | |
1da177e4 | 1332 | |
86aa3e22 GR |
1333 | err = kstrtoul(buf, 10, &val); |
1334 | if (err) | |
1335 | return err; | |
1336 | ||
1337 | if (val >= 3) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | mutex_lock(&data->update_lock); | |
1341 | old_enable = data->pwm1.enable; | |
1342 | data->pwm1.enable = val; | |
1343 | data->config1 = (data->config1 & ~CFG1_PWM_AFC) | |
1344 | | ((val == 2) ? CFG1_PWM_AFC : 0); | |
1345 | adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1); | |
1346 | if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */ | |
1347 | data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) | | |
1348 | PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); | |
1349 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
1350 | } else if (!((old_enable == 1) && (val == 1))) { | |
1351 | /* set pwm to safe value */ | |
1352 | data->pwm1.pwm = 255; | |
1353 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
1da177e4 | 1354 | } |
86aa3e22 GR |
1355 | mutex_unlock(&data->update_lock); |
1356 | ||
1da177e4 LT |
1357 | return count; |
1358 | } | |
1359 | ||
1360 | /* enable PWM fan control */ | |
cb01a231 JD |
1361 | static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); |
1362 | static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); | |
1363 | static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); | |
1364 | static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable, | |
1da177e4 | 1365 | set_pwm_enable); |
cb01a231 | 1366 | static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable, |
1da177e4 | 1367 | set_pwm_enable); |
cb01a231 | 1368 | static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable, |
1da177e4 | 1369 | set_pwm_enable); |
cb01a231 | 1370 | static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 | 1371 | show_auto_pwm_min, set_auto_pwm_min); |
cb01a231 | 1372 | static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 | 1373 | show_auto_pwm_min, set_auto_pwm_min); |
cb01a231 | 1374 | static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 LT |
1375 | show_auto_pwm_min, set_auto_pwm_min); |
1376 | ||
1377 | static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1378 | static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1379 | static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1380 | ||
681c6f7a MH |
1381 | static struct attribute *adm1026_attributes[] = { |
1382 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
1383 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
1384 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
a9273cb8 | 1385 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
681c6f7a MH |
1386 | &sensor_dev_attr_in1_input.dev_attr.attr, |
1387 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
1388 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
a9273cb8 | 1389 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
681c6f7a MH |
1390 | &sensor_dev_attr_in2_input.dev_attr.attr, |
1391 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
1392 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
a9273cb8 | 1393 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
681c6f7a MH |
1394 | &sensor_dev_attr_in3_input.dev_attr.attr, |
1395 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
1396 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
a9273cb8 | 1397 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
681c6f7a MH |
1398 | &sensor_dev_attr_in4_input.dev_attr.attr, |
1399 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
1400 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
a9273cb8 | 1401 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
681c6f7a MH |
1402 | &sensor_dev_attr_in5_input.dev_attr.attr, |
1403 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
1404 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
a9273cb8 | 1405 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
681c6f7a MH |
1406 | &sensor_dev_attr_in6_input.dev_attr.attr, |
1407 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
1408 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
a9273cb8 | 1409 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
681c6f7a MH |
1410 | &sensor_dev_attr_in7_input.dev_attr.attr, |
1411 | &sensor_dev_attr_in7_max.dev_attr.attr, | |
1412 | &sensor_dev_attr_in7_min.dev_attr.attr, | |
a9273cb8 | 1413 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
681c6f7a MH |
1414 | &sensor_dev_attr_in10_input.dev_attr.attr, |
1415 | &sensor_dev_attr_in10_max.dev_attr.attr, | |
1416 | &sensor_dev_attr_in10_min.dev_attr.attr, | |
a9273cb8 | 1417 | &sensor_dev_attr_in10_alarm.dev_attr.attr, |
681c6f7a MH |
1418 | &sensor_dev_attr_in11_input.dev_attr.attr, |
1419 | &sensor_dev_attr_in11_max.dev_attr.attr, | |
1420 | &sensor_dev_attr_in11_min.dev_attr.attr, | |
a9273cb8 | 1421 | &sensor_dev_attr_in11_alarm.dev_attr.attr, |
681c6f7a MH |
1422 | &sensor_dev_attr_in12_input.dev_attr.attr, |
1423 | &sensor_dev_attr_in12_max.dev_attr.attr, | |
1424 | &sensor_dev_attr_in12_min.dev_attr.attr, | |
a9273cb8 | 1425 | &sensor_dev_attr_in12_alarm.dev_attr.attr, |
681c6f7a MH |
1426 | &sensor_dev_attr_in13_input.dev_attr.attr, |
1427 | &sensor_dev_attr_in13_max.dev_attr.attr, | |
1428 | &sensor_dev_attr_in13_min.dev_attr.attr, | |
a9273cb8 | 1429 | &sensor_dev_attr_in13_alarm.dev_attr.attr, |
681c6f7a MH |
1430 | &sensor_dev_attr_in14_input.dev_attr.attr, |
1431 | &sensor_dev_attr_in14_max.dev_attr.attr, | |
1432 | &sensor_dev_attr_in14_min.dev_attr.attr, | |
a9273cb8 | 1433 | &sensor_dev_attr_in14_alarm.dev_attr.attr, |
681c6f7a MH |
1434 | &sensor_dev_attr_in15_input.dev_attr.attr, |
1435 | &sensor_dev_attr_in15_max.dev_attr.attr, | |
1436 | &sensor_dev_attr_in15_min.dev_attr.attr, | |
a9273cb8 | 1437 | &sensor_dev_attr_in15_alarm.dev_attr.attr, |
681c6f7a MH |
1438 | &sensor_dev_attr_in16_input.dev_attr.attr, |
1439 | &sensor_dev_attr_in16_max.dev_attr.attr, | |
1440 | &sensor_dev_attr_in16_min.dev_attr.attr, | |
a9273cb8 | 1441 | &sensor_dev_attr_in16_alarm.dev_attr.attr, |
681c6f7a MH |
1442 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
1443 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
1444 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
a9273cb8 | 1445 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
681c6f7a MH |
1446 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
1447 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
1448 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
a9273cb8 | 1449 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
681c6f7a MH |
1450 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
1451 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
1452 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
a9273cb8 | 1453 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
681c6f7a MH |
1454 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
1455 | &sensor_dev_attr_fan4_div.dev_attr.attr, | |
1456 | &sensor_dev_attr_fan4_min.dev_attr.attr, | |
a9273cb8 | 1457 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
681c6f7a MH |
1458 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
1459 | &sensor_dev_attr_fan5_div.dev_attr.attr, | |
1460 | &sensor_dev_attr_fan5_min.dev_attr.attr, | |
a9273cb8 | 1461 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, |
681c6f7a MH |
1462 | &sensor_dev_attr_fan6_input.dev_attr.attr, |
1463 | &sensor_dev_attr_fan6_div.dev_attr.attr, | |
1464 | &sensor_dev_attr_fan6_min.dev_attr.attr, | |
a9273cb8 | 1465 | &sensor_dev_attr_fan6_alarm.dev_attr.attr, |
681c6f7a MH |
1466 | &sensor_dev_attr_fan7_input.dev_attr.attr, |
1467 | &sensor_dev_attr_fan7_div.dev_attr.attr, | |
1468 | &sensor_dev_attr_fan7_min.dev_attr.attr, | |
a9273cb8 | 1469 | &sensor_dev_attr_fan7_alarm.dev_attr.attr, |
681c6f7a MH |
1470 | &sensor_dev_attr_fan8_input.dev_attr.attr, |
1471 | &sensor_dev_attr_fan8_div.dev_attr.attr, | |
1472 | &sensor_dev_attr_fan8_min.dev_attr.attr, | |
a9273cb8 | 1473 | &sensor_dev_attr_fan8_alarm.dev_attr.attr, |
681c6f7a MH |
1474 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
1475 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
1476 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
a9273cb8 | 1477 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
681c6f7a MH |
1478 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
1479 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
1480 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
a9273cb8 | 1481 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
681c6f7a MH |
1482 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
1483 | &sensor_dev_attr_temp2_offset.dev_attr.attr, | |
681c6f7a MH |
1484 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
1485 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, | |
681c6f7a MH |
1486 | &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr, |
1487 | &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr, | |
681c6f7a MH |
1488 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
1489 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, | |
681c6f7a MH |
1490 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
1491 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
681c6f7a MH |
1492 | &dev_attr_temp1_crit_enable.attr, |
1493 | &dev_attr_temp2_crit_enable.attr, | |
681c6f7a MH |
1494 | &dev_attr_cpu0_vid.attr, |
1495 | &dev_attr_vrm.attr, | |
1496 | &dev_attr_alarms.attr, | |
1497 | &dev_attr_alarm_mask.attr, | |
1498 | &dev_attr_gpio.attr, | |
1499 | &dev_attr_gpio_mask.attr, | |
1500 | &dev_attr_pwm1.attr, | |
1501 | &dev_attr_pwm2.attr, | |
1502 | &dev_attr_pwm3.attr, | |
1503 | &dev_attr_pwm1_enable.attr, | |
1504 | &dev_attr_pwm2_enable.attr, | |
1505 | &dev_attr_pwm3_enable.attr, | |
1506 | &dev_attr_temp1_auto_point1_pwm.attr, | |
1507 | &dev_attr_temp2_auto_point1_pwm.attr, | |
681c6f7a MH |
1508 | &dev_attr_temp1_auto_point2_pwm.attr, |
1509 | &dev_attr_temp2_auto_point2_pwm.attr, | |
681c6f7a MH |
1510 | &dev_attr_analog_out.attr, |
1511 | NULL | |
1512 | }; | |
1513 | ||
1514 | static const struct attribute_group adm1026_group = { | |
1515 | .attrs = adm1026_attributes, | |
1516 | }; | |
1517 | ||
5b34dbcd JD |
1518 | static struct attribute *adm1026_attributes_temp3[] = { |
1519 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
1520 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
1521 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
1522 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
1523 | &sensor_dev_attr_temp3_offset.dev_attr.attr, | |
1524 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, | |
1525 | &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr, | |
1526 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, | |
1527 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
1528 | &dev_attr_temp3_crit_enable.attr, | |
1529 | &dev_attr_temp3_auto_point1_pwm.attr, | |
1530 | &dev_attr_temp3_auto_point2_pwm.attr, | |
1d5f2c16 | 1531 | NULL |
5b34dbcd JD |
1532 | }; |
1533 | ||
1534 | static const struct attribute_group adm1026_group_temp3 = { | |
1535 | .attrs = adm1026_attributes_temp3, | |
1536 | }; | |
1537 | ||
1538 | static struct attribute *adm1026_attributes_in8_9[] = { | |
1539 | &sensor_dev_attr_in8_input.dev_attr.attr, | |
1540 | &sensor_dev_attr_in8_max.dev_attr.attr, | |
1541 | &sensor_dev_attr_in8_min.dev_attr.attr, | |
1542 | &sensor_dev_attr_in8_alarm.dev_attr.attr, | |
1543 | &sensor_dev_attr_in9_input.dev_attr.attr, | |
1544 | &sensor_dev_attr_in9_max.dev_attr.attr, | |
1545 | &sensor_dev_attr_in9_min.dev_attr.attr, | |
1546 | &sensor_dev_attr_in9_alarm.dev_attr.attr, | |
1d5f2c16 | 1547 | NULL |
5b34dbcd JD |
1548 | }; |
1549 | ||
1550 | static const struct attribute_group adm1026_group_in8_9 = { | |
1551 | .attrs = adm1026_attributes_in8_9, | |
1552 | }; | |
1553 | ||
57f7eb0b | 1554 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 1555 | static int adm1026_detect(struct i2c_client *client, |
57f7eb0b | 1556 | struct i2c_board_info *info) |
1da177e4 | 1557 | { |
57f7eb0b JD |
1558 | struct i2c_adapter *adapter = client->adapter; |
1559 | int address = client->addr; | |
1da177e4 | 1560 | int company, verstep; |
1da177e4 LT |
1561 | |
1562 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | |
1563 | /* We need to be able to do byte I/O */ | |
57f7eb0b | 1564 | return -ENODEV; |
a0393713 | 1565 | } |
1da177e4 | 1566 | |
1da177e4 LT |
1567 | /* Now, we do the remaining detection. */ |
1568 | ||
f67fdabf JD |
1569 | company = adm1026_read_value(client, ADM1026_REG_COMPANY); |
1570 | verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP); | |
1da177e4 | 1571 | |
b55f3757 GR |
1572 | dev_dbg(&adapter->dev, |
1573 | "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n", | |
f67fdabf | 1574 | i2c_adapter_id(client->adapter), client->addr, |
1da177e4 LT |
1575 | company, verstep); |
1576 | ||
52df6440 JD |
1577 | /* Determine the chip type. */ |
1578 | dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n", | |
1579 | i2c_adapter_id(adapter), address); | |
1580 | if (company == ADM1026_COMPANY_ANALOG_DEV | |
1581 | && verstep == ADM1026_VERSTEP_ADM1026) { | |
1582 | /* Analog Devices ADM1026 */ | |
1583 | } else if (company == ADM1026_COMPANY_ANALOG_DEV | |
1584 | && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) { | |
b55f3757 GR |
1585 | dev_err(&adapter->dev, |
1586 | "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n", | |
1587 | verstep); | |
52df6440 | 1588 | } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) { |
b55f3757 GR |
1589 | dev_err(&adapter->dev, |
1590 | "Found version/stepping 0x%02x. Assuming generic ADM1026.\n", | |
52df6440 JD |
1591 | verstep); |
1592 | } else { | |
1593 | dev_dbg(&adapter->dev, "Autodetection failed\n"); | |
1594 | /* Not an ADM1026... */ | |
1595 | return -ENODEV; | |
1da177e4 | 1596 | } |
52df6440 | 1597 | |
57f7eb0b | 1598 | strlcpy(info->type, "adm1026", I2C_NAME_SIZE); |
1da177e4 | 1599 | |
57f7eb0b JD |
1600 | return 0; |
1601 | } | |
1602 | ||
97df5566 AL |
1603 | static void adm1026_print_gpio(struct i2c_client *client) |
1604 | { | |
1605 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1606 | int i; | |
1607 | ||
1608 | dev_dbg(&client->dev, "GPIO config is:\n"); | |
1609 | for (i = 0; i <= 7; ++i) { | |
1610 | if (data->config2 & (1 << i)) { | |
1611 | dev_dbg(&client->dev, "\t%sGP%s%d\n", | |
1612 | data->gpio_config[i] & 0x02 ? "" : "!", | |
1613 | data->gpio_config[i] & 0x01 ? "OUT" : "IN", | |
1614 | i); | |
1615 | } else { | |
1616 | dev_dbg(&client->dev, "\tFAN%d\n", i); | |
1617 | } | |
1618 | } | |
1619 | for (i = 8; i <= 15; ++i) { | |
1620 | dev_dbg(&client->dev, "\t%sGP%s%d\n", | |
1621 | data->gpio_config[i] & 0x02 ? "" : "!", | |
1622 | data->gpio_config[i] & 0x01 ? "OUT" : "IN", | |
1623 | i); | |
1624 | } | |
1625 | if (data->config3 & CFG3_GPIO16_ENABLE) { | |
1626 | dev_dbg(&client->dev, "\t%sGP%s16\n", | |
1627 | data->gpio_config[16] & 0x02 ? "" : "!", | |
1628 | data->gpio_config[16] & 0x01 ? "OUT" : "IN"); | |
1629 | } else { | |
1630 | /* GPIO16 is THERM */ | |
1631 | dev_dbg(&client->dev, "\tTHERM\n"); | |
1632 | } | |
1633 | } | |
1634 | ||
1635 | static void adm1026_fixup_gpio(struct i2c_client *client) | |
1636 | { | |
1637 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1638 | int i; | |
1639 | int value; | |
1640 | ||
1641 | /* Make the changes requested. */ | |
1642 | /* | |
1643 | * We may need to unlock/stop monitoring or soft-reset the | |
1644 | * chip before we can make changes. This hasn't been | |
1645 | * tested much. FIXME | |
1646 | */ | |
1647 | ||
1648 | /* Make outputs */ | |
1649 | for (i = 0; i <= 16; ++i) { | |
1650 | if (gpio_output[i] >= 0 && gpio_output[i] <= 16) | |
1651 | data->gpio_config[gpio_output[i]] |= 0x01; | |
1652 | /* if GPIO0-7 is output, it isn't a FAN tach */ | |
1653 | if (gpio_output[i] >= 0 && gpio_output[i] <= 7) | |
1654 | data->config2 |= 1 << gpio_output[i]; | |
1655 | } | |
1656 | ||
1657 | /* Input overrides output */ | |
1658 | for (i = 0; i <= 16; ++i) { | |
1659 | if (gpio_input[i] >= 0 && gpio_input[i] <= 16) | |
1660 | data->gpio_config[gpio_input[i]] &= ~0x01; | |
1661 | /* if GPIO0-7 is input, it isn't a FAN tach */ | |
1662 | if (gpio_input[i] >= 0 && gpio_input[i] <= 7) | |
1663 | data->config2 |= 1 << gpio_input[i]; | |
1664 | } | |
1665 | ||
1666 | /* Inverted */ | |
1667 | for (i = 0; i <= 16; ++i) { | |
1668 | if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) | |
1669 | data->gpio_config[gpio_inverted[i]] &= ~0x02; | |
1670 | } | |
1671 | ||
1672 | /* Normal overrides inverted */ | |
1673 | for (i = 0; i <= 16; ++i) { | |
1674 | if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) | |
1675 | data->gpio_config[gpio_normal[i]] |= 0x02; | |
1676 | } | |
1677 | ||
1678 | /* Fan overrides input and output */ | |
1679 | for (i = 0; i <= 7; ++i) { | |
1680 | if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) | |
1681 | data->config2 &= ~(1 << gpio_fan[i]); | |
1682 | } | |
1683 | ||
1684 | /* Write new configs to registers */ | |
1685 | adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2); | |
1686 | data->config3 = (data->config3 & 0x3f) | |
1687 | | ((data->gpio_config[16] & 0x03) << 6); | |
1688 | adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3); | |
1689 | for (i = 15, value = 0; i >= 0; --i) { | |
1690 | value <<= 2; | |
1691 | value |= data->gpio_config[i] & 0x03; | |
1692 | if ((i & 0x03) == 0) { | |
1693 | adm1026_write_value(client, | |
1694 | ADM1026_REG_GPIO_CFG_0_3 + i/4, | |
1695 | value); | |
1696 | value = 0; | |
1697 | } | |
1698 | } | |
1699 | ||
1700 | /* Print the new config */ | |
1701 | adm1026_print_gpio(client); | |
1702 | } | |
1703 | ||
1704 | static void adm1026_init_client(struct i2c_client *client) | |
1705 | { | |
1706 | int value, i; | |
1707 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1708 | ||
1709 | dev_dbg(&client->dev, "Initializing device\n"); | |
1710 | /* Read chip config */ | |
1711 | data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1); | |
1712 | data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2); | |
1713 | data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3); | |
1714 | ||
1715 | /* Inform user of chip config */ | |
1716 | dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n", | |
1717 | data->config1); | |
1718 | if ((data->config1 & CFG1_MONITOR) == 0) { | |
1719 | dev_dbg(&client->dev, | |
1720 | "Monitoring not currently enabled.\n"); | |
1721 | } | |
1722 | if (data->config1 & CFG1_INT_ENABLE) { | |
1723 | dev_dbg(&client->dev, | |
1724 | "SMBALERT interrupts are enabled.\n"); | |
1725 | } | |
1726 | if (data->config1 & CFG1_AIN8_9) { | |
1727 | dev_dbg(&client->dev, | |
1728 | "in8 and in9 enabled. temp3 disabled.\n"); | |
1729 | } else { | |
1730 | dev_dbg(&client->dev, | |
1731 | "temp3 enabled. in8 and in9 disabled.\n"); | |
1732 | } | |
1733 | if (data->config1 & CFG1_THERM_HOT) { | |
1734 | dev_dbg(&client->dev, | |
1735 | "Automatic THERM, PWM, and temp limits enabled.\n"); | |
1736 | } | |
1737 | ||
1738 | if (data->config3 & CFG3_GPIO16_ENABLE) { | |
1739 | dev_dbg(&client->dev, | |
1740 | "GPIO16 enabled. THERM pin disabled.\n"); | |
1741 | } else { | |
1742 | dev_dbg(&client->dev, | |
1743 | "THERM pin enabled. GPIO16 disabled.\n"); | |
1744 | } | |
1745 | if (data->config3 & CFG3_VREF_250) | |
1746 | dev_dbg(&client->dev, "Vref is 2.50 Volts.\n"); | |
1747 | else | |
1748 | dev_dbg(&client->dev, "Vref is 1.82 Volts.\n"); | |
1749 | /* Read and pick apart the existing GPIO configuration */ | |
1750 | value = 0; | |
1751 | for (i = 0; i <= 15; ++i) { | |
1752 | if ((i & 0x03) == 0) { | |
1753 | value = adm1026_read_value(client, | |
1754 | ADM1026_REG_GPIO_CFG_0_3 + i / 4); | |
1755 | } | |
1756 | data->gpio_config[i] = value & 0x03; | |
1757 | value >>= 2; | |
1758 | } | |
1759 | data->gpio_config[16] = (data->config3 >> 6) & 0x03; | |
1760 | ||
1761 | /* ... and then print it */ | |
1762 | adm1026_print_gpio(client); | |
1763 | ||
1764 | /* | |
1765 | * If the user asks us to reprogram the GPIO config, then | |
1766 | * do it now. | |
1767 | */ | |
1768 | if (gpio_input[0] != -1 || gpio_output[0] != -1 | |
1769 | || gpio_inverted[0] != -1 || gpio_normal[0] != -1 | |
1770 | || gpio_fan[0] != -1) { | |
1771 | adm1026_fixup_gpio(client); | |
1772 | } | |
1773 | ||
1774 | /* | |
1775 | * WE INTENTIONALLY make no changes to the limits, | |
1776 | * offsets, pwms, fans and zones. If they were | |
1777 | * configured, we don't want to mess with them. | |
1778 | * If they weren't, the default is 100% PWM, no | |
1779 | * control and will suffice until 'sensors -s' | |
1780 | * can be run by the user. We DO set the default | |
1781 | * value for pwm1.auto_pwm_min to its maximum | |
1782 | * so that enabling automatic pwm fan control | |
1783 | * without first setting a value for pwm1.auto_pwm_min | |
1784 | * will not result in potentially dangerous fan speed decrease. | |
1785 | */ | |
1786 | data->pwm1.auto_pwm_min = 255; | |
1787 | /* Start monitoring */ | |
1788 | value = adm1026_read_value(client, ADM1026_REG_CONFIG1); | |
1789 | /* Set MONITOR, clear interrupt acknowledge and s/w reset */ | |
1790 | value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET); | |
1791 | dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value); | |
1792 | data->config1 = value; | |
1793 | adm1026_write_value(client, ADM1026_REG_CONFIG1, value); | |
1794 | ||
1795 | /* initialize fan_div[] to hardware defaults */ | |
1796 | value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) | | |
1797 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8); | |
1798 | for (i = 0; i <= 7; ++i) { | |
1799 | data->fan_div[i] = DIV_FROM_REG(value & 0x03); | |
1800 | value >>= 2; | |
1801 | } | |
1802 | } | |
1803 | ||
57f7eb0b JD |
1804 | static int adm1026_probe(struct i2c_client *client, |
1805 | const struct i2c_device_id *id) | |
1806 | { | |
b591d3c1 AL |
1807 | struct device *dev = &client->dev; |
1808 | struct device *hwmon_dev; | |
57f7eb0b | 1809 | struct adm1026_data *data; |
57f7eb0b | 1810 | |
b591d3c1 | 1811 | data = devm_kzalloc(dev, sizeof(struct adm1026_data), GFP_KERNEL); |
3421e212 GR |
1812 | if (!data) |
1813 | return -ENOMEM; | |
1da177e4 | 1814 | |
57f7eb0b | 1815 | i2c_set_clientdata(client, data); |
b591d3c1 | 1816 | data->client = client; |
9a61bf63 | 1817 | mutex_init(&data->update_lock); |
1da177e4 | 1818 | |
1da177e4 | 1819 | /* Set the VRM version */ |
303760b4 | 1820 | data->vrm = vid_which_vrm(); |
1da177e4 LT |
1821 | |
1822 | /* Initialize the ADM1026 chip */ | |
f67fdabf | 1823 | adm1026_init_client(client); |
1da177e4 | 1824 | |
b591d3c1 AL |
1825 | /* sysfs hooks */ |
1826 | data->groups[0] = &adm1026_group; | |
5b34dbcd | 1827 | if (data->config1 & CFG1_AIN8_9) |
b591d3c1 | 1828 | data->groups[1] = &adm1026_group_in8_9; |
5b34dbcd | 1829 | else |
b591d3c1 | 1830 | data->groups[1] = &adm1026_group_temp3; |
681c6f7a | 1831 | |
b591d3c1 AL |
1832 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
1833 | data, data->groups); | |
1834 | return PTR_ERR_OR_ZERO(hwmon_dev); | |
681c6f7a MH |
1835 | } |
1836 | ||
97df5566 AL |
1837 | static const struct i2c_device_id adm1026_id[] = { |
1838 | { "adm1026", 0 }, | |
1839 | { } | |
1840 | }; | |
1841 | MODULE_DEVICE_TABLE(i2c, adm1026_id); | |
1842 | ||
1843 | static struct i2c_driver adm1026_driver = { | |
1844 | .class = I2C_CLASS_HWMON, | |
1845 | .driver = { | |
1846 | .name = "adm1026", | |
1847 | }, | |
1848 | .probe = adm1026_probe, | |
97df5566 AL |
1849 | .id_table = adm1026_id, |
1850 | .detect = adm1026_detect, | |
1851 | .address_list = normal_i2c, | |
1852 | }; | |
1853 | ||
f0967eea | 1854 | module_i2c_driver(adm1026_driver); |
1da177e4 LT |
1855 | |
1856 | MODULE_LICENSE("GPL"); | |
1857 | MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, " | |
cb01a231 | 1858 | "Justin Thiessen <jthiessen@penguincomputing.com>"); |
1da177e4 | 1859 | MODULE_DESCRIPTION("ADM1026 driver"); |