]>
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 { | |
1beeffe4 | 269 | struct device *hwmon_dev; |
1da177e4 | 270 | |
9a61bf63 | 271 | struct mutex update_lock; |
1da177e4 LT |
272 | int valid; /* !=0 if following fields are valid */ |
273 | unsigned long last_reading; /* In jiffies */ | |
274 | unsigned long last_config; /* In jiffies */ | |
275 | ||
cb01a231 JD |
276 | u8 in[17]; /* Register value */ |
277 | u8 in_max[17]; /* Register value */ | |
278 | u8 in_min[17]; /* Register value */ | |
279 | s8 temp[3]; /* Register value */ | |
280 | s8 temp_min[3]; /* Register value */ | |
281 | s8 temp_max[3]; /* Register value */ | |
282 | s8 temp_tmin[3]; /* Register value */ | |
283 | s8 temp_crit[3]; /* Register value */ | |
284 | s8 temp_offset[3]; /* Register value */ | |
285 | u8 fan[8]; /* Register value */ | |
286 | u8 fan_min[8]; /* Register value */ | |
287 | u8 fan_div[8]; /* Decoded value */ | |
288 | struct pwm_data pwm1; /* Pwm control values */ | |
cb01a231 | 289 | u8 vrm; /* VRM version */ |
1da177e4 | 290 | u8 analog_out; /* Register value (DAC) */ |
cb01a231 JD |
291 | long alarms; /* Register encoding, combined */ |
292 | long alarm_mask; /* Register encoding, combined */ | |
293 | long gpio; /* Register encoding, combined */ | |
294 | long gpio_mask; /* Register encoding, combined */ | |
295 | u8 gpio_config[17]; /* Decoded value */ | |
296 | u8 config1; /* Register value */ | |
297 | u8 config2; /* Register value */ | |
298 | u8 config3; /* Register value */ | |
1da177e4 LT |
299 | }; |
300 | ||
c49efcef | 301 | static int adm1026_read_value(struct i2c_client *client, u8 reg) |
1da177e4 LT |
302 | { |
303 | int res; | |
304 | ||
305 | if (reg < 0x80) { | |
306 | /* "RAM" locations */ | |
307 | res = i2c_smbus_read_byte_data(client, reg) & 0xff; | |
308 | } else { | |
309 | /* EEPROM, do nothing */ | |
310 | res = 0; | |
311 | } | |
312 | return res; | |
313 | } | |
314 | ||
c49efcef | 315 | static int adm1026_write_value(struct i2c_client *client, u8 reg, int value) |
1da177e4 LT |
316 | { |
317 | int res; | |
318 | ||
319 | if (reg < 0x80) { | |
320 | /* "RAM" locations */ | |
321 | res = i2c_smbus_write_byte_data(client, reg, value); | |
322 | } else { | |
323 | /* EEPROM, do nothing */ | |
324 | res = 0; | |
325 | } | |
326 | return res; | |
327 | } | |
328 | ||
1da177e4 LT |
329 | static struct adm1026_data *adm1026_update_device(struct device *dev) |
330 | { | |
331 | struct i2c_client *client = to_i2c_client(dev); | |
332 | struct adm1026_data *data = i2c_get_clientdata(client); | |
333 | int i; | |
334 | long value, alarms, gpio; | |
335 | ||
9a61bf63 | 336 | mutex_lock(&data->update_lock); |
1da177e4 | 337 | if (!data->valid |
86aa3e22 GR |
338 | || time_after(jiffies, |
339 | data->last_reading + ADM1026_DATA_INTERVAL)) { | |
1da177e4 | 340 | /* Things that change quickly */ |
cb01a231 | 341 | dev_dbg(&client->dev, "Reading sensor values\n"); |
86aa3e22 | 342 | for (i = 0; i <= 16; ++i) { |
1da177e4 LT |
343 | data->in[i] = |
344 | adm1026_read_value(client, ADM1026_REG_IN[i]); | |
345 | } | |
346 | ||
86aa3e22 | 347 | for (i = 0; i <= 7; ++i) { |
1da177e4 LT |
348 | data->fan[i] = |
349 | adm1026_read_value(client, ADM1026_REG_FAN(i)); | |
350 | } | |
351 | ||
86aa3e22 GR |
352 | for (i = 0; i <= 2; ++i) { |
353 | /* | |
354 | * NOTE: temp[] is s8 and we assume 2's complement | |
355 | * "conversion" in the assignment | |
356 | */ | |
1da177e4 LT |
357 | data->temp[i] = |
358 | adm1026_read_value(client, ADM1026_REG_TEMP[i]); | |
359 | } | |
360 | ||
cb01a231 | 361 | data->pwm1.pwm = adm1026_read_value(client, |
1da177e4 | 362 | ADM1026_REG_PWM); |
cb01a231 | 363 | data->analog_out = adm1026_read_value(client, |
1da177e4 LT |
364 | ADM1026_REG_DAC); |
365 | /* GPIO16 is MSbit of alarms, move it to gpio */ | |
366 | alarms = adm1026_read_value(client, ADM1026_REG_STATUS4); | |
cb01a231 | 367 | gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */ |
1da177e4 LT |
368 | alarms &= 0x7f; |
369 | alarms <<= 8; | |
370 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3); | |
371 | alarms <<= 8; | |
372 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2); | |
373 | alarms <<= 8; | |
374 | alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1); | |
375 | data->alarms = alarms; | |
376 | ||
377 | /* Read the GPIO values */ | |
cb01a231 | 378 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
379 | ADM1026_REG_GPIO_STATUS_8_15); |
380 | gpio <<= 8; | |
cb01a231 | 381 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
382 | ADM1026_REG_GPIO_STATUS_0_7); |
383 | data->gpio = gpio; | |
384 | ||
385 | data->last_reading = jiffies; | |
a0393713 | 386 | } /* last_reading */ |
1da177e4 LT |
387 | |
388 | if (!data->valid || | |
389 | time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) { | |
390 | /* Things that don't change often */ | |
391 | dev_dbg(&client->dev, "Reading config values\n"); | |
86aa3e22 | 392 | for (i = 0; i <= 16; ++i) { |
cb01a231 | 393 | data->in_min[i] = adm1026_read_value(client, |
1da177e4 | 394 | ADM1026_REG_IN_MIN[i]); |
cb01a231 | 395 | data->in_max[i] = adm1026_read_value(client, |
1da177e4 LT |
396 | ADM1026_REG_IN_MAX[i]); |
397 | } | |
398 | ||
399 | value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) | |
400 | | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) | |
401 | << 8); | |
86aa3e22 | 402 | for (i = 0; i <= 7; ++i) { |
cb01a231 | 403 | data->fan_min[i] = adm1026_read_value(client, |
1da177e4 LT |
404 | ADM1026_REG_FAN_MIN(i)); |
405 | data->fan_div[i] = DIV_FROM_REG(value & 0x03); | |
406 | value >>= 2; | |
407 | } | |
408 | ||
409 | for (i = 0; i <= 2; ++i) { | |
86aa3e22 GR |
410 | /* |
411 | * NOTE: temp_xxx[] are s8 and we assume 2's | |
1da177e4 LT |
412 | * complement "conversion" in the assignment |
413 | */ | |
cb01a231 | 414 | data->temp_min[i] = adm1026_read_value(client, |
1da177e4 | 415 | ADM1026_REG_TEMP_MIN[i]); |
cb01a231 | 416 | data->temp_max[i] = adm1026_read_value(client, |
1da177e4 | 417 | ADM1026_REG_TEMP_MAX[i]); |
cb01a231 | 418 | data->temp_tmin[i] = adm1026_read_value(client, |
1da177e4 | 419 | ADM1026_REG_TEMP_TMIN[i]); |
cb01a231 | 420 | data->temp_crit[i] = adm1026_read_value(client, |
1da177e4 | 421 | ADM1026_REG_TEMP_THERM[i]); |
cb01a231 | 422 | data->temp_offset[i] = adm1026_read_value(client, |
1da177e4 LT |
423 | ADM1026_REG_TEMP_OFFSET[i]); |
424 | } | |
425 | ||
426 | /* Read the STATUS/alarm masks */ | |
cb01a231 JD |
427 | alarms = adm1026_read_value(client, ADM1026_REG_MASK4); |
428 | gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */ | |
429 | alarms = (alarms & 0x7f) << 8; | |
1da177e4 LT |
430 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK3); |
431 | alarms <<= 8; | |
432 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK2); | |
433 | alarms <<= 8; | |
434 | alarms |= adm1026_read_value(client, ADM1026_REG_MASK1); | |
435 | data->alarm_mask = alarms; | |
436 | ||
437 | /* Read the GPIO values */ | |
cb01a231 | 438 | gpio |= adm1026_read_value(client, |
1da177e4 LT |
439 | ADM1026_REG_GPIO_MASK_8_15); |
440 | gpio <<= 8; | |
441 | gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7); | |
442 | data->gpio_mask = gpio; | |
443 | ||
444 | /* Read various values from CONFIG1 */ | |
cb01a231 | 445 | data->config1 = adm1026_read_value(client, |
1da177e4 LT |
446 | ADM1026_REG_CONFIG1); |
447 | if (data->config1 & CFG1_PWM_AFC) { | |
448 | data->pwm1.enable = 2; | |
cb01a231 | 449 | data->pwm1.auto_pwm_min = |
1da177e4 LT |
450 | PWM_MIN_FROM_REG(data->pwm1.pwm); |
451 | } | |
452 | /* Read the GPIO config */ | |
cb01a231 | 453 | data->config2 = adm1026_read_value(client, |
1da177e4 | 454 | ADM1026_REG_CONFIG2); |
cb01a231 | 455 | data->config3 = adm1026_read_value(client, |
1da177e4 LT |
456 | ADM1026_REG_CONFIG3); |
457 | data->gpio_config[16] = (data->config3 >> 6) & 0x03; | |
458 | ||
459 | value = 0; | |
86aa3e22 | 460 | for (i = 0; i <= 15; ++i) { |
1da177e4 LT |
461 | if ((i & 0x03) == 0) { |
462 | value = adm1026_read_value(client, | |
463 | ADM1026_REG_GPIO_CFG_0_3 + i/4); | |
464 | } | |
465 | data->gpio_config[i] = value & 0x03; | |
466 | value >>= 2; | |
467 | } | |
468 | ||
469 | data->last_config = jiffies; | |
a0393713 | 470 | } /* last_config */ |
1da177e4 | 471 | |
1da177e4 | 472 | data->valid = 1; |
9a61bf63 | 473 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
474 | return data; |
475 | } | |
476 | ||
050480f1 YI |
477 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
478 | char *buf) | |
1da177e4 | 479 | { |
050480f1 YI |
480 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
481 | int nr = sensor_attr->index; | |
1da177e4 | 482 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 483 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr])); |
1da177e4 | 484 | } |
050480f1 YI |
485 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
486 | char *buf) | |
1da177e4 | 487 | { |
050480f1 YI |
488 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
489 | int nr = sensor_attr->index; | |
cb01a231 JD |
490 | struct adm1026_data *data = adm1026_update_device(dev); |
491 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr])); | |
1da177e4 | 492 | } |
050480f1 YI |
493 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
494 | const char *buf, size_t count) | |
1da177e4 | 495 | { |
050480f1 YI |
496 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
497 | int nr = sensor_attr->index; | |
1da177e4 LT |
498 | struct i2c_client *client = to_i2c_client(dev); |
499 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
500 | long val; |
501 | int err; | |
502 | ||
503 | err = kstrtol(buf, 10, &val); | |
504 | if (err) | |
505 | return err; | |
1da177e4 | 506 | |
9a61bf63 | 507 | mutex_lock(&data->update_lock); |
1da177e4 LT |
508 | data->in_min[nr] = INS_TO_REG(nr, val); |
509 | adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]); | |
9a61bf63 | 510 | mutex_unlock(&data->update_lock); |
cb01a231 | 511 | return count; |
1da177e4 | 512 | } |
050480f1 YI |
513 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
514 | char *buf) | |
1da177e4 | 515 | { |
050480f1 YI |
516 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
517 | int nr = sensor_attr->index; | |
1da177e4 | 518 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 519 | return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr])); |
1da177e4 | 520 | } |
050480f1 YI |
521 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
522 | const char *buf, size_t count) | |
1da177e4 | 523 | { |
050480f1 YI |
524 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
525 | int nr = sensor_attr->index; | |
1da177e4 LT |
526 | struct i2c_client *client = to_i2c_client(dev); |
527 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
528 | long val; |
529 | int err; | |
530 | ||
531 | err = kstrtol(buf, 10, &val); | |
532 | if (err) | |
533 | return err; | |
1da177e4 | 534 | |
9a61bf63 | 535 | mutex_lock(&data->update_lock); |
1da177e4 LT |
536 | data->in_max[nr] = INS_TO_REG(nr, val); |
537 | adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]); | |
9a61bf63 | 538 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
539 | return count; |
540 | } | |
541 | ||
050480f1 YI |
542 | #define in_reg(offset) \ |
543 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \ | |
544 | NULL, offset); \ | |
545 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
546 | show_in_min, set_in_min, offset); \ | |
547 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
548 | show_in_max, set_in_max, offset); | |
1da177e4 LT |
549 | |
550 | ||
551 | in_reg(0); | |
552 | in_reg(1); | |
553 | in_reg(2); | |
554 | in_reg(3); | |
555 | in_reg(4); | |
556 | in_reg(5); | |
557 | in_reg(6); | |
558 | in_reg(7); | |
559 | in_reg(8); | |
560 | in_reg(9); | |
561 | in_reg(10); | |
562 | in_reg(11); | |
563 | in_reg(12); | |
564 | in_reg(13); | |
565 | in_reg(14); | |
566 | in_reg(15); | |
567 | ||
86aa3e22 GR |
568 | static ssize_t show_in16(struct device *dev, struct device_attribute *attr, |
569 | char *buf) | |
1da177e4 LT |
570 | { |
571 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 572 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) - |
1da177e4 LT |
573 | NEG12_OFFSET); |
574 | } | |
86aa3e22 GR |
575 | static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, |
576 | char *buf) | |
1da177e4 | 577 | { |
cb01a231 JD |
578 | struct adm1026_data *data = adm1026_update_device(dev); |
579 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16]) | |
1da177e4 LT |
580 | - NEG12_OFFSET); |
581 | } | |
86aa3e22 GR |
582 | static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, |
583 | const char *buf, size_t count) | |
1da177e4 LT |
584 | { |
585 | struct i2c_client *client = to_i2c_client(dev); | |
586 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
587 | long val; |
588 | int err; | |
589 | ||
590 | err = kstrtol(buf, 10, &val); | |
591 | if (err) | |
592 | return err; | |
1da177e4 | 593 | |
9a61bf63 | 594 | mutex_lock(&data->update_lock); |
1da177e4 LT |
595 | data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET); |
596 | adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]); | |
9a61bf63 | 597 | mutex_unlock(&data->update_lock); |
cb01a231 | 598 | return count; |
1da177e4 | 599 | } |
86aa3e22 GR |
600 | static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, |
601 | char *buf) | |
1da177e4 LT |
602 | { |
603 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 604 | return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16]) |
1da177e4 LT |
605 | - NEG12_OFFSET); |
606 | } | |
86aa3e22 GR |
607 | static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, |
608 | const char *buf, size_t count) | |
1da177e4 LT |
609 | { |
610 | struct i2c_client *client = to_i2c_client(dev); | |
611 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
612 | long val; |
613 | int err; | |
614 | ||
615 | err = kstrtol(buf, 10, &val); | |
616 | if (err) | |
617 | return err; | |
1da177e4 | 618 | |
9a61bf63 | 619 | mutex_lock(&data->update_lock); |
1da177e4 LT |
620 | data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET); |
621 | adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]); | |
9a61bf63 | 622 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
623 | return count; |
624 | } | |
625 | ||
050480f1 | 626 | static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16); |
86aa3e22 GR |
627 | static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, |
628 | set_in16_min, 16); | |
629 | static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, | |
630 | set_in16_max, 16); | |
1da177e4 LT |
631 | |
632 | ||
633 | /* Now add fan read/write functions */ | |
634 | ||
050480f1 YI |
635 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
636 | char *buf) | |
1da177e4 | 637 | { |
050480f1 YI |
638 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
639 | int nr = sensor_attr->index; | |
1da177e4 | 640 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 641 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
1da177e4 LT |
642 | data->fan_div[nr])); |
643 | } | |
050480f1 YI |
644 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
645 | char *buf) | |
1da177e4 | 646 | { |
050480f1 YI |
647 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
648 | int nr = sensor_attr->index; | |
1da177e4 | 649 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 650 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
1da177e4 LT |
651 | data->fan_div[nr])); |
652 | } | |
050480f1 YI |
653 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
654 | const char *buf, size_t count) | |
1da177e4 | 655 | { |
050480f1 YI |
656 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
657 | int nr = sensor_attr->index; | |
1da177e4 LT |
658 | struct i2c_client *client = to_i2c_client(dev); |
659 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
660 | long val; |
661 | int err; | |
662 | ||
663 | err = kstrtol(buf, 10, &val); | |
664 | if (err) | |
665 | return err; | |
1da177e4 | 666 | |
9a61bf63 | 667 | mutex_lock(&data->update_lock); |
1da177e4 LT |
668 | data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]); |
669 | adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr), | |
670 | data->fan_min[nr]); | |
9a61bf63 | 671 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
672 | return count; |
673 | } | |
674 | ||
cb01a231 JD |
675 | #define fan_offset(offset) \ |
676 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \ | |
677 | offset - 1); \ | |
678 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
050480f1 | 679 | show_fan_min, set_fan_min, offset - 1); |
1da177e4 LT |
680 | |
681 | fan_offset(1); | |
682 | fan_offset(2); | |
683 | fan_offset(3); | |
684 | fan_offset(4); | |
685 | fan_offset(5); | |
686 | fan_offset(6); | |
687 | fan_offset(7); | |
688 | fan_offset(8); | |
689 | ||
690 | /* Adjust fan_min to account for new fan divisor */ | |
691 | static void fixup_fan_min(struct device *dev, int fan, int old_div) | |
692 | { | |
693 | struct i2c_client *client = to_i2c_client(dev); | |
694 | struct adm1026_data *data = i2c_get_clientdata(client); | |
cb01a231 JD |
695 | int new_min; |
696 | int new_div = data->fan_div[fan]; | |
1da177e4 LT |
697 | |
698 | /* 0 and 0xff are special. Don't adjust them */ | |
86aa3e22 | 699 | if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) |
1da177e4 | 700 | return; |
1da177e4 LT |
701 | |
702 | new_min = data->fan_min[fan] * old_div / new_div; | |
2a844c14 | 703 | new_min = clamp_val(new_min, 1, 254); |
1da177e4 LT |
704 | data->fan_min[fan] = new_min; |
705 | adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min); | |
706 | } | |
707 | ||
708 | /* Now add fan_div read/write functions */ | |
050480f1 YI |
709 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
710 | char *buf) | |
1da177e4 | 711 | { |
050480f1 YI |
712 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
713 | int nr = sensor_attr->index; | |
1da177e4 | 714 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 715 | return sprintf(buf, "%d\n", data->fan_div[nr]); |
1da177e4 | 716 | } |
050480f1 YI |
717 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
718 | const char *buf, size_t count) | |
1da177e4 | 719 | { |
050480f1 YI |
720 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
721 | int nr = sensor_attr->index; | |
1da177e4 LT |
722 | struct i2c_client *client = to_i2c_client(dev); |
723 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
724 | long val; |
725 | int orig_div, new_div; | |
726 | int err; | |
727 | ||
728 | err = kstrtol(buf, 10, &val); | |
729 | if (err) | |
730 | return err; | |
1da177e4 | 731 | |
cb01a231 | 732 | new_div = DIV_TO_REG(val); |
8b0f1840 | 733 | |
9a61bf63 | 734 | mutex_lock(&data->update_lock); |
1da177e4 LT |
735 | orig_div = data->fan_div[nr]; |
736 | data->fan_div[nr] = DIV_FROM_REG(new_div); | |
737 | ||
738 | if (nr < 4) { /* 0 <= nr < 4 */ | |
1da177e4 | 739 | adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3, |
52bc9802 GG |
740 | (DIV_TO_REG(data->fan_div[0]) << 0) | |
741 | (DIV_TO_REG(data->fan_div[1]) << 2) | | |
742 | (DIV_TO_REG(data->fan_div[2]) << 4) | | |
743 | (DIV_TO_REG(data->fan_div[3]) << 6)); | |
1da177e4 | 744 | } else { /* 3 < nr < 8 */ |
1da177e4 | 745 | adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7, |
52bc9802 GG |
746 | (DIV_TO_REG(data->fan_div[4]) << 0) | |
747 | (DIV_TO_REG(data->fan_div[5]) << 2) | | |
748 | (DIV_TO_REG(data->fan_div[6]) << 4) | | |
749 | (DIV_TO_REG(data->fan_div[7]) << 6)); | |
1da177e4 LT |
750 | } |
751 | ||
86aa3e22 | 752 | if (data->fan_div[nr] != orig_div) |
cb01a231 | 753 | fixup_fan_min(dev, nr, orig_div); |
86aa3e22 | 754 | |
9a61bf63 | 755 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
756 | return count; |
757 | } | |
758 | ||
cb01a231 JD |
759 | #define fan_offset_div(offset) \ |
760 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
050480f1 | 761 | show_fan_div, set_fan_div, offset - 1); |
1da177e4 LT |
762 | |
763 | fan_offset_div(1); | |
764 | fan_offset_div(2); | |
765 | fan_offset_div(3); | |
766 | fan_offset_div(4); | |
767 | fan_offset_div(5); | |
768 | fan_offset_div(6); | |
769 | fan_offset_div(7); | |
770 | fan_offset_div(8); | |
771 | ||
772 | /* Temps */ | |
050480f1 YI |
773 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
774 | char *buf) | |
1da177e4 | 775 | { |
050480f1 YI |
776 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
777 | int nr = sensor_attr->index; | |
1da177e4 | 778 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 779 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
1da177e4 | 780 | } |
050480f1 YI |
781 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
782 | char *buf) | |
1da177e4 | 783 | { |
050480f1 YI |
784 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
785 | int nr = sensor_attr->index; | |
1da177e4 | 786 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 787 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
1da177e4 | 788 | } |
050480f1 YI |
789 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
790 | const char *buf, size_t count) | |
1da177e4 | 791 | { |
050480f1 YI |
792 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
793 | int nr = sensor_attr->index; | |
1da177e4 LT |
794 | struct i2c_client *client = to_i2c_client(dev); |
795 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
796 | long val; |
797 | int err; | |
798 | ||
799 | err = kstrtol(buf, 10, &val); | |
800 | if (err) | |
801 | return err; | |
1da177e4 | 802 | |
9a61bf63 | 803 | mutex_lock(&data->update_lock); |
1da177e4 LT |
804 | data->temp_min[nr] = TEMP_TO_REG(val); |
805 | adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr], | |
806 | data->temp_min[nr]); | |
9a61bf63 | 807 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
808 | return count; |
809 | } | |
050480f1 YI |
810 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
811 | char *buf) | |
1da177e4 | 812 | { |
050480f1 YI |
813 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
814 | int nr = sensor_attr->index; | |
1da177e4 | 815 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 816 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
1da177e4 | 817 | } |
050480f1 YI |
818 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
819 | const char *buf, size_t count) | |
1da177e4 | 820 | { |
050480f1 YI |
821 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
822 | int nr = sensor_attr->index; | |
1da177e4 LT |
823 | struct i2c_client *client = to_i2c_client(dev); |
824 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
825 | long val; |
826 | int err; | |
827 | ||
828 | err = kstrtol(buf, 10, &val); | |
829 | if (err) | |
830 | return err; | |
1da177e4 | 831 | |
9a61bf63 | 832 | mutex_lock(&data->update_lock); |
1da177e4 LT |
833 | data->temp_max[nr] = TEMP_TO_REG(val); |
834 | adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr], | |
835 | data->temp_max[nr]); | |
9a61bf63 | 836 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
837 | return count; |
838 | } | |
050480f1 YI |
839 | |
840 | #define temp_reg(offset) \ | |
cb01a231 | 841 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \ |
050480f1 YI |
842 | NULL, offset - 1); \ |
843 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
844 | show_temp_min, set_temp_min, offset - 1); \ | |
845 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
846 | show_temp_max, set_temp_max, offset - 1); | |
1da177e4 LT |
847 | |
848 | ||
849 | temp_reg(1); | |
850 | temp_reg(2); | |
851 | temp_reg(3); | |
852 | ||
050480f1 YI |
853 | static ssize_t show_temp_offset(struct device *dev, |
854 | struct device_attribute *attr, char *buf) | |
1da177e4 | 855 | { |
050480f1 YI |
856 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
857 | int nr = sensor_attr->index; | |
1da177e4 | 858 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 859 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr])); |
1da177e4 | 860 | } |
050480f1 YI |
861 | static ssize_t set_temp_offset(struct device *dev, |
862 | struct device_attribute *attr, const char *buf, | |
863 | size_t count) | |
1da177e4 | 864 | { |
050480f1 YI |
865 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
866 | int nr = sensor_attr->index; | |
1da177e4 LT |
867 | struct i2c_client *client = to_i2c_client(dev); |
868 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
869 | long val; |
870 | int err; | |
871 | ||
872 | err = kstrtol(buf, 10, &val); | |
873 | if (err) | |
874 | return err; | |
1da177e4 | 875 | |
9a61bf63 | 876 | mutex_lock(&data->update_lock); |
1da177e4 LT |
877 | data->temp_offset[nr] = TEMP_TO_REG(val); |
878 | adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr], | |
879 | data->temp_offset[nr]); | |
9a61bf63 | 880 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
881 | return count; |
882 | } | |
883 | ||
86aa3e22 GR |
884 | #define temp_offset_reg(offset) \ |
885 | static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \ | |
050480f1 | 886 | show_temp_offset, set_temp_offset, offset - 1); |
1da177e4 LT |
887 | |
888 | temp_offset_reg(1); | |
889 | temp_offset_reg(2); | |
890 | temp_offset_reg(3); | |
891 | ||
050480f1 YI |
892 | static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev, |
893 | struct device_attribute *attr, char *buf) | |
1da177e4 | 894 | { |
050480f1 YI |
895 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
896 | int nr = sensor_attr->index; | |
1da177e4 | 897 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 898 | return sprintf(buf, "%d\n", TEMP_FROM_REG( |
1da177e4 LT |
899 | ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr])); |
900 | } | |
050480f1 YI |
901 | static ssize_t show_temp_auto_point2_temp(struct device *dev, |
902 | struct device_attribute *attr, char *buf) | |
1da177e4 | 903 | { |
050480f1 YI |
904 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
905 | int nr = sensor_attr->index; | |
1da177e4 | 906 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 907 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] + |
1da177e4 LT |
908 | ADM1026_FAN_CONTROL_TEMP_RANGE)); |
909 | } | |
050480f1 YI |
910 | static ssize_t show_temp_auto_point1_temp(struct device *dev, |
911 | struct device_attribute *attr, char *buf) | |
1da177e4 | 912 | { |
050480f1 YI |
913 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
914 | int nr = sensor_attr->index; | |
1da177e4 | 915 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 916 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr])); |
1da177e4 | 917 | } |
050480f1 YI |
918 | static ssize_t set_temp_auto_point1_temp(struct device *dev, |
919 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 920 | { |
050480f1 YI |
921 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
922 | int nr = sensor_attr->index; | |
1da177e4 LT |
923 | struct i2c_client *client = to_i2c_client(dev); |
924 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
925 | long val; |
926 | int err; | |
927 | ||
928 | err = kstrtol(buf, 10, &val); | |
929 | if (err) | |
930 | return err; | |
1da177e4 | 931 | |
9a61bf63 | 932 | mutex_lock(&data->update_lock); |
1da177e4 LT |
933 | data->temp_tmin[nr] = TEMP_TO_REG(val); |
934 | adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr], | |
935 | data->temp_tmin[nr]); | |
9a61bf63 | 936 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
937 | return count; |
938 | } | |
939 | ||
cb01a231 JD |
940 | #define temp_auto_point(offset) \ |
941 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \ | |
942 | S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \ | |
943 | set_temp_auto_point1_temp, offset - 1); \ | |
944 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\ | |
945 | show_temp_auto_point1_temp_hyst, NULL, offset - 1); \ | |
946 | static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \ | |
050480f1 | 947 | show_temp_auto_point2_temp, NULL, offset - 1); |
1da177e4 LT |
948 | |
949 | temp_auto_point(1); | |
950 | temp_auto_point(2); | |
951 | temp_auto_point(3); | |
952 | ||
050480f1 YI |
953 | static ssize_t show_temp_crit_enable(struct device *dev, |
954 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
955 | { |
956 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 957 | return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4); |
1da177e4 | 958 | } |
050480f1 YI |
959 | static ssize_t set_temp_crit_enable(struct device *dev, |
960 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 LT |
961 | { |
962 | struct i2c_client *client = to_i2c_client(dev); | |
963 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
964 | unsigned long val; |
965 | int err; | |
966 | ||
967 | err = kstrtoul(buf, 10, &val); | |
968 | if (err) | |
969 | return err; | |
970 | ||
971 | if (val > 1) | |
972 | return -EINVAL; | |
973 | ||
974 | mutex_lock(&data->update_lock); | |
975 | data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4); | |
976 | adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1); | |
977 | mutex_unlock(&data->update_lock); | |
1da177e4 | 978 | |
1da177e4 LT |
979 | return count; |
980 | } | |
981 | ||
050480f1 YI |
982 | #define temp_crit_enable(offset) \ |
983 | static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \ | |
1da177e4 LT |
984 | show_temp_crit_enable, set_temp_crit_enable); |
985 | ||
050480f1 YI |
986 | temp_crit_enable(1); |
987 | temp_crit_enable(2); | |
988 | temp_crit_enable(3); | |
1da177e4 | 989 | |
050480f1 YI |
990 | static ssize_t show_temp_crit(struct device *dev, |
991 | struct device_attribute *attr, char *buf) | |
1da177e4 | 992 | { |
050480f1 YI |
993 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
994 | int nr = sensor_attr->index; | |
1da177e4 | 995 | struct adm1026_data *data = adm1026_update_device(dev); |
cb01a231 | 996 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr])); |
1da177e4 | 997 | } |
050480f1 YI |
998 | static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, |
999 | const char *buf, size_t count) | |
1da177e4 | 1000 | { |
050480f1 YI |
1001 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
1002 | int nr = sensor_attr->index; | |
1da177e4 LT |
1003 | struct i2c_client *client = to_i2c_client(dev); |
1004 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
1005 | long val; |
1006 | int err; | |
1007 | ||
1008 | err = kstrtol(buf, 10, &val); | |
1009 | if (err) | |
1010 | return err; | |
1da177e4 | 1011 | |
9a61bf63 | 1012 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1013 | data->temp_crit[nr] = TEMP_TO_REG(val); |
1014 | adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr], | |
1015 | data->temp_crit[nr]); | |
9a61bf63 | 1016 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1017 | return count; |
1018 | } | |
1019 | ||
050480f1 YI |
1020 | #define temp_crit_reg(offset) \ |
1021 | static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \ | |
1022 | show_temp_crit, set_temp_crit, offset - 1); | |
1da177e4 LT |
1023 | |
1024 | temp_crit_reg(1); | |
1025 | temp_crit_reg(2); | |
1026 | temp_crit_reg(3); | |
1027 | ||
86aa3e22 GR |
1028 | static ssize_t show_analog_out_reg(struct device *dev, |
1029 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1030 | { |
1031 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1032 | return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out)); |
1da177e4 | 1033 | } |
86aa3e22 GR |
1034 | static ssize_t set_analog_out_reg(struct device *dev, |
1035 | struct device_attribute *attr, | |
1036 | const char *buf, size_t count) | |
1da177e4 LT |
1037 | { |
1038 | struct i2c_client *client = to_i2c_client(dev); | |
1039 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
1040 | long val; |
1041 | int err; | |
1042 | ||
1043 | err = kstrtol(buf, 10, &val); | |
1044 | if (err) | |
1045 | return err; | |
1da177e4 | 1046 | |
9a61bf63 | 1047 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1048 | data->analog_out = DAC_TO_REG(val); |
1049 | adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out); | |
9a61bf63 | 1050 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1051 | return count; |
1052 | } | |
1053 | ||
cb01a231 | 1054 | static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg, |
1da177e4 LT |
1055 | set_analog_out_reg); |
1056 | ||
86aa3e22 GR |
1057 | static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, |
1058 | char *buf) | |
1da177e4 LT |
1059 | { |
1060 | struct adm1026_data *data = adm1026_update_device(dev); | |
a0cf354a JD |
1061 | int vid = (data->gpio >> 11) & 0x1f; |
1062 | ||
1063 | dev_dbg(dev, "Setting VID from GPIO11-15.\n"); | |
1064 | return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm)); | |
1da177e4 | 1065 | } |
86aa3e22 | 1066 | |
937df8df | 1067 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); |
1da177e4 | 1068 | |
86aa3e22 GR |
1069 | static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, |
1070 | char *buf) | |
1da177e4 | 1071 | { |
90d6619a | 1072 | struct adm1026_data *data = dev_get_drvdata(dev); |
cb01a231 | 1073 | return sprintf(buf, "%d\n", data->vrm); |
1da177e4 | 1074 | } |
86aa3e22 GR |
1075 | |
1076 | static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, | |
1077 | const char *buf, size_t count) | |
1da177e4 | 1078 | { |
f67fdabf | 1079 | struct adm1026_data *data = dev_get_drvdata(dev); |
86aa3e22 GR |
1080 | unsigned long val; |
1081 | int err; | |
1082 | ||
1083 | err = kstrtoul(buf, 10, &val); | |
1084 | if (err) | |
1085 | return err; | |
1da177e4 | 1086 | |
86aa3e22 | 1087 | data->vrm = val; |
1da177e4 LT |
1088 | return count; |
1089 | } | |
1090 | ||
1091 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); | |
1092 | ||
86aa3e22 GR |
1093 | static ssize_t show_alarms_reg(struct device *dev, |
1094 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1095 | { |
1096 | struct adm1026_data *data = adm1026_update_device(dev); | |
f67fdabf | 1097 | return sprintf(buf, "%ld\n", data->alarms); |
1da177e4 LT |
1098 | } |
1099 | ||
1100 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL); | |
1101 | ||
a9273cb8 JD |
1102 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
1103 | char *buf) | |
1104 | { | |
1105 | struct adm1026_data *data = adm1026_update_device(dev); | |
1106 | int bitnr = to_sensor_dev_attr(attr)->index; | |
1107 | return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1); | |
1108 | } | |
1109 | ||
1110 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0); | |
1111 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1); | |
1112 | static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1); | |
1113 | static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2); | |
1114 | static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3); | |
1115 | static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4); | |
1116 | static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5); | |
1117 | static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6); | |
1118 | static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7); | |
1119 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8); | |
1120 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9); | |
1121 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10); | |
1122 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
1123 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12); | |
1124 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13); | |
1125 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14); | |
1126 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15); | |
1127 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16); | |
1128 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17); | |
1129 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18); | |
1130 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19); | |
1131 | static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20); | |
1132 | static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21); | |
1133 | static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22); | |
1134 | static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23); | |
1135 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24); | |
1136 | static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25); | |
1137 | static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26); | |
1138 | ||
86aa3e22 GR |
1139 | static ssize_t show_alarm_mask(struct device *dev, |
1140 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1141 | { |
1142 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1143 | return sprintf(buf, "%ld\n", data->alarm_mask); |
1da177e4 | 1144 | } |
86aa3e22 GR |
1145 | static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, |
1146 | const char *buf, size_t count) | |
1da177e4 LT |
1147 | { |
1148 | struct i2c_client *client = to_i2c_client(dev); | |
1149 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1da177e4 | 1150 | unsigned long mask; |
86aa3e22 GR |
1151 | long val; |
1152 | int err; | |
1153 | ||
1154 | err = kstrtol(buf, 10, &val); | |
1155 | if (err) | |
1156 | return err; | |
1da177e4 | 1157 | |
9a61bf63 | 1158 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1159 | data->alarm_mask = val & 0x7fffffff; |
1160 | mask = data->alarm_mask | |
1161 | | (data->gpio_mask & 0x10000 ? 0x80000000 : 0); | |
1162 | adm1026_write_value(client, ADM1026_REG_MASK1, | |
1163 | mask & 0xff); | |
1164 | mask >>= 8; | |
1165 | adm1026_write_value(client, ADM1026_REG_MASK2, | |
1166 | mask & 0xff); | |
1167 | mask >>= 8; | |
1168 | adm1026_write_value(client, ADM1026_REG_MASK3, | |
1169 | mask & 0xff); | |
1170 | mask >>= 8; | |
1171 | adm1026_write_value(client, ADM1026_REG_MASK4, | |
1172 | mask & 0xff); | |
9a61bf63 | 1173 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1174 | return count; |
1175 | } | |
1176 | ||
1177 | static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask, | |
1178 | set_alarm_mask); | |
1179 | ||
1180 | ||
86aa3e22 GR |
1181 | static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, |
1182 | char *buf) | |
1da177e4 LT |
1183 | { |
1184 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1185 | return sprintf(buf, "%ld\n", data->gpio); |
1da177e4 | 1186 | } |
86aa3e22 GR |
1187 | static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, |
1188 | const char *buf, size_t count) | |
1da177e4 LT |
1189 | { |
1190 | struct i2c_client *client = to_i2c_client(dev); | |
1191 | struct adm1026_data *data = i2c_get_clientdata(client); | |
cb01a231 | 1192 | long gpio; |
86aa3e22 GR |
1193 | long val; |
1194 | int err; | |
1195 | ||
1196 | err = kstrtol(buf, 10, &val); | |
1197 | if (err) | |
1198 | return err; | |
1da177e4 | 1199 | |
9a61bf63 | 1200 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1201 | data->gpio = val & 0x1ffff; |
1202 | gpio = data->gpio; | |
cb01a231 | 1203 | adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff); |
1da177e4 | 1204 | gpio >>= 8; |
cb01a231 | 1205 | adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff); |
1da177e4 | 1206 | gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f); |
cb01a231 | 1207 | adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff); |
9a61bf63 | 1208 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1209 | return count; |
1210 | } | |
1211 | ||
1212 | static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio); | |
1213 | ||
86aa3e22 GR |
1214 | static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, |
1215 | char *buf) | |
1da177e4 LT |
1216 | { |
1217 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1218 | return sprintf(buf, "%ld\n", data->gpio_mask); |
1da177e4 | 1219 | } |
86aa3e22 GR |
1220 | static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, |
1221 | const char *buf, size_t count) | |
1da177e4 LT |
1222 | { |
1223 | struct i2c_client *client = to_i2c_client(dev); | |
1224 | struct adm1026_data *data = i2c_get_clientdata(client); | |
cb01a231 | 1225 | long mask; |
86aa3e22 GR |
1226 | long val; |
1227 | int err; | |
1228 | ||
1229 | err = kstrtol(buf, 10, &val); | |
1230 | if (err) | |
1231 | return err; | |
1da177e4 | 1232 | |
9a61bf63 | 1233 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1234 | data->gpio_mask = val & 0x1ffff; |
1235 | mask = data->gpio_mask; | |
cb01a231 | 1236 | adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff); |
1da177e4 | 1237 | mask >>= 8; |
cb01a231 | 1238 | adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff); |
1da177e4 | 1239 | mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f); |
cb01a231 | 1240 | adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff); |
9a61bf63 | 1241 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1242 | return count; |
1243 | } | |
1244 | ||
1245 | static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask); | |
1246 | ||
86aa3e22 GR |
1247 | static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, |
1248 | char *buf) | |
1da177e4 LT |
1249 | { |
1250 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1251 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm)); |
1da177e4 | 1252 | } |
86aa3e22 GR |
1253 | |
1254 | static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, | |
1255 | const char *buf, size_t count) | |
1da177e4 LT |
1256 | { |
1257 | struct i2c_client *client = to_i2c_client(dev); | |
1258 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1259 | ||
1260 | if (data->pwm1.enable == 1) { | |
86aa3e22 GR |
1261 | long val; |
1262 | int err; | |
1263 | ||
1264 | err = kstrtol(buf, 10, &val); | |
1265 | if (err) | |
1266 | return err; | |
1da177e4 | 1267 | |
9a61bf63 | 1268 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1269 | data->pwm1.pwm = PWM_TO_REG(val); |
1270 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
9a61bf63 | 1271 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1272 | } |
1273 | return count; | |
1274 | } | |
86aa3e22 GR |
1275 | |
1276 | static ssize_t show_auto_pwm_min(struct device *dev, | |
1277 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1278 | { |
1279 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1280 | return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min); |
1da177e4 | 1281 | } |
86aa3e22 GR |
1282 | |
1283 | static ssize_t set_auto_pwm_min(struct device *dev, | |
1284 | struct device_attribute *attr, const char *buf, | |
1285 | size_t count) | |
1da177e4 LT |
1286 | { |
1287 | struct i2c_client *client = to_i2c_client(dev); | |
1288 | struct adm1026_data *data = i2c_get_clientdata(client); | |
86aa3e22 GR |
1289 | unsigned long val; |
1290 | int err; | |
1291 | ||
1292 | err = kstrtoul(buf, 10, &val); | |
1293 | if (err) | |
1294 | return err; | |
1da177e4 | 1295 | |
9a61bf63 | 1296 | mutex_lock(&data->update_lock); |
2a844c14 | 1297 | data->pwm1.auto_pwm_min = clamp_val(val, 0, 255); |
1da177e4 LT |
1298 | if (data->pwm1.enable == 2) { /* apply immediately */ |
1299 | data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) | | |
cb01a231 | 1300 | PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); |
1da177e4 LT |
1301 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); |
1302 | } | |
9a61bf63 | 1303 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1304 | return count; |
1305 | } | |
86aa3e22 GR |
1306 | |
1307 | static ssize_t show_auto_pwm_max(struct device *dev, | |
1308 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1309 | { |
cb01a231 | 1310 | return sprintf(buf, "%d\n", ADM1026_PWM_MAX); |
1da177e4 | 1311 | } |
86aa3e22 GR |
1312 | |
1313 | static ssize_t show_pwm_enable(struct device *dev, | |
1314 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
1315 | { |
1316 | struct adm1026_data *data = adm1026_update_device(dev); | |
cb01a231 | 1317 | return sprintf(buf, "%d\n", data->pwm1.enable); |
1da177e4 | 1318 | } |
86aa3e22 GR |
1319 | |
1320 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, | |
1321 | const char *buf, size_t count) | |
1da177e4 LT |
1322 | { |
1323 | struct i2c_client *client = to_i2c_client(dev); | |
1324 | struct adm1026_data *data = i2c_get_clientdata(client); | |
cb01a231 | 1325 | int old_enable; |
86aa3e22 GR |
1326 | unsigned long val; |
1327 | int err; | |
1da177e4 | 1328 | |
86aa3e22 GR |
1329 | err = kstrtoul(buf, 10, &val); |
1330 | if (err) | |
1331 | return err; | |
1332 | ||
1333 | if (val >= 3) | |
1334 | return -EINVAL; | |
1335 | ||
1336 | mutex_lock(&data->update_lock); | |
1337 | old_enable = data->pwm1.enable; | |
1338 | data->pwm1.enable = val; | |
1339 | data->config1 = (data->config1 & ~CFG1_PWM_AFC) | |
1340 | | ((val == 2) ? CFG1_PWM_AFC : 0); | |
1341 | adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1); | |
1342 | if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */ | |
1343 | data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) | | |
1344 | PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); | |
1345 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
1346 | } else if (!((old_enable == 1) && (val == 1))) { | |
1347 | /* set pwm to safe value */ | |
1348 | data->pwm1.pwm = 255; | |
1349 | adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm); | |
1da177e4 | 1350 | } |
86aa3e22 GR |
1351 | mutex_unlock(&data->update_lock); |
1352 | ||
1da177e4 LT |
1353 | return count; |
1354 | } | |
1355 | ||
1356 | /* enable PWM fan control */ | |
cb01a231 JD |
1357 | static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); |
1358 | static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); | |
1359 | static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); | |
1360 | static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable, | |
1da177e4 | 1361 | set_pwm_enable); |
cb01a231 | 1362 | static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable, |
1da177e4 | 1363 | set_pwm_enable); |
cb01a231 | 1364 | static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable, |
1da177e4 | 1365 | set_pwm_enable); |
cb01a231 | 1366 | static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 | 1367 | show_auto_pwm_min, set_auto_pwm_min); |
cb01a231 | 1368 | static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 | 1369 | show_auto_pwm_min, set_auto_pwm_min); |
cb01a231 | 1370 | static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR, |
1da177e4 LT |
1371 | show_auto_pwm_min, set_auto_pwm_min); |
1372 | ||
1373 | static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1374 | static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1375 | static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL); | |
1376 | ||
681c6f7a MH |
1377 | static struct attribute *adm1026_attributes[] = { |
1378 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
1379 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
1380 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
a9273cb8 | 1381 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
681c6f7a MH |
1382 | &sensor_dev_attr_in1_input.dev_attr.attr, |
1383 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
1384 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
a9273cb8 | 1385 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
681c6f7a MH |
1386 | &sensor_dev_attr_in2_input.dev_attr.attr, |
1387 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
1388 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
a9273cb8 | 1389 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
681c6f7a MH |
1390 | &sensor_dev_attr_in3_input.dev_attr.attr, |
1391 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
1392 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
a9273cb8 | 1393 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
681c6f7a MH |
1394 | &sensor_dev_attr_in4_input.dev_attr.attr, |
1395 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
1396 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
a9273cb8 | 1397 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
681c6f7a MH |
1398 | &sensor_dev_attr_in5_input.dev_attr.attr, |
1399 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
1400 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
a9273cb8 | 1401 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
681c6f7a MH |
1402 | &sensor_dev_attr_in6_input.dev_attr.attr, |
1403 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
1404 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
a9273cb8 | 1405 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
681c6f7a MH |
1406 | &sensor_dev_attr_in7_input.dev_attr.attr, |
1407 | &sensor_dev_attr_in7_max.dev_attr.attr, | |
1408 | &sensor_dev_attr_in7_min.dev_attr.attr, | |
a9273cb8 | 1409 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
681c6f7a MH |
1410 | &sensor_dev_attr_in10_input.dev_attr.attr, |
1411 | &sensor_dev_attr_in10_max.dev_attr.attr, | |
1412 | &sensor_dev_attr_in10_min.dev_attr.attr, | |
a9273cb8 | 1413 | &sensor_dev_attr_in10_alarm.dev_attr.attr, |
681c6f7a MH |
1414 | &sensor_dev_attr_in11_input.dev_attr.attr, |
1415 | &sensor_dev_attr_in11_max.dev_attr.attr, | |
1416 | &sensor_dev_attr_in11_min.dev_attr.attr, | |
a9273cb8 | 1417 | &sensor_dev_attr_in11_alarm.dev_attr.attr, |
681c6f7a MH |
1418 | &sensor_dev_attr_in12_input.dev_attr.attr, |
1419 | &sensor_dev_attr_in12_max.dev_attr.attr, | |
1420 | &sensor_dev_attr_in12_min.dev_attr.attr, | |
a9273cb8 | 1421 | &sensor_dev_attr_in12_alarm.dev_attr.attr, |
681c6f7a MH |
1422 | &sensor_dev_attr_in13_input.dev_attr.attr, |
1423 | &sensor_dev_attr_in13_max.dev_attr.attr, | |
1424 | &sensor_dev_attr_in13_min.dev_attr.attr, | |
a9273cb8 | 1425 | &sensor_dev_attr_in13_alarm.dev_attr.attr, |
681c6f7a MH |
1426 | &sensor_dev_attr_in14_input.dev_attr.attr, |
1427 | &sensor_dev_attr_in14_max.dev_attr.attr, | |
1428 | &sensor_dev_attr_in14_min.dev_attr.attr, | |
a9273cb8 | 1429 | &sensor_dev_attr_in14_alarm.dev_attr.attr, |
681c6f7a MH |
1430 | &sensor_dev_attr_in15_input.dev_attr.attr, |
1431 | &sensor_dev_attr_in15_max.dev_attr.attr, | |
1432 | &sensor_dev_attr_in15_min.dev_attr.attr, | |
a9273cb8 | 1433 | &sensor_dev_attr_in15_alarm.dev_attr.attr, |
681c6f7a MH |
1434 | &sensor_dev_attr_in16_input.dev_attr.attr, |
1435 | &sensor_dev_attr_in16_max.dev_attr.attr, | |
1436 | &sensor_dev_attr_in16_min.dev_attr.attr, | |
a9273cb8 | 1437 | &sensor_dev_attr_in16_alarm.dev_attr.attr, |
681c6f7a MH |
1438 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
1439 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
1440 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
a9273cb8 | 1441 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
681c6f7a MH |
1442 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
1443 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
1444 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
a9273cb8 | 1445 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
681c6f7a MH |
1446 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
1447 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
1448 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
a9273cb8 | 1449 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
681c6f7a MH |
1450 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
1451 | &sensor_dev_attr_fan4_div.dev_attr.attr, | |
1452 | &sensor_dev_attr_fan4_min.dev_attr.attr, | |
a9273cb8 | 1453 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
681c6f7a MH |
1454 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
1455 | &sensor_dev_attr_fan5_div.dev_attr.attr, | |
1456 | &sensor_dev_attr_fan5_min.dev_attr.attr, | |
a9273cb8 | 1457 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, |
681c6f7a MH |
1458 | &sensor_dev_attr_fan6_input.dev_attr.attr, |
1459 | &sensor_dev_attr_fan6_div.dev_attr.attr, | |
1460 | &sensor_dev_attr_fan6_min.dev_attr.attr, | |
a9273cb8 | 1461 | &sensor_dev_attr_fan6_alarm.dev_attr.attr, |
681c6f7a MH |
1462 | &sensor_dev_attr_fan7_input.dev_attr.attr, |
1463 | &sensor_dev_attr_fan7_div.dev_attr.attr, | |
1464 | &sensor_dev_attr_fan7_min.dev_attr.attr, | |
a9273cb8 | 1465 | &sensor_dev_attr_fan7_alarm.dev_attr.attr, |
681c6f7a MH |
1466 | &sensor_dev_attr_fan8_input.dev_attr.attr, |
1467 | &sensor_dev_attr_fan8_div.dev_attr.attr, | |
1468 | &sensor_dev_attr_fan8_min.dev_attr.attr, | |
a9273cb8 | 1469 | &sensor_dev_attr_fan8_alarm.dev_attr.attr, |
681c6f7a MH |
1470 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
1471 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
1472 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
a9273cb8 | 1473 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
681c6f7a MH |
1474 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
1475 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
1476 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
a9273cb8 | 1477 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
681c6f7a MH |
1478 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
1479 | &sensor_dev_attr_temp2_offset.dev_attr.attr, | |
681c6f7a MH |
1480 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
1481 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, | |
681c6f7a MH |
1482 | &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr, |
1483 | &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr, | |
681c6f7a MH |
1484 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
1485 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, | |
681c6f7a MH |
1486 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
1487 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
681c6f7a MH |
1488 | &dev_attr_temp1_crit_enable.attr, |
1489 | &dev_attr_temp2_crit_enable.attr, | |
681c6f7a MH |
1490 | &dev_attr_cpu0_vid.attr, |
1491 | &dev_attr_vrm.attr, | |
1492 | &dev_attr_alarms.attr, | |
1493 | &dev_attr_alarm_mask.attr, | |
1494 | &dev_attr_gpio.attr, | |
1495 | &dev_attr_gpio_mask.attr, | |
1496 | &dev_attr_pwm1.attr, | |
1497 | &dev_attr_pwm2.attr, | |
1498 | &dev_attr_pwm3.attr, | |
1499 | &dev_attr_pwm1_enable.attr, | |
1500 | &dev_attr_pwm2_enable.attr, | |
1501 | &dev_attr_pwm3_enable.attr, | |
1502 | &dev_attr_temp1_auto_point1_pwm.attr, | |
1503 | &dev_attr_temp2_auto_point1_pwm.attr, | |
681c6f7a MH |
1504 | &dev_attr_temp1_auto_point2_pwm.attr, |
1505 | &dev_attr_temp2_auto_point2_pwm.attr, | |
681c6f7a MH |
1506 | &dev_attr_analog_out.attr, |
1507 | NULL | |
1508 | }; | |
1509 | ||
1510 | static const struct attribute_group adm1026_group = { | |
1511 | .attrs = adm1026_attributes, | |
1512 | }; | |
1513 | ||
5b34dbcd JD |
1514 | static struct attribute *adm1026_attributes_temp3[] = { |
1515 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
1516 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
1517 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
1518 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
1519 | &sensor_dev_attr_temp3_offset.dev_attr.attr, | |
1520 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, | |
1521 | &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr, | |
1522 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, | |
1523 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
1524 | &dev_attr_temp3_crit_enable.attr, | |
1525 | &dev_attr_temp3_auto_point1_pwm.attr, | |
1526 | &dev_attr_temp3_auto_point2_pwm.attr, | |
1d5f2c16 | 1527 | NULL |
5b34dbcd JD |
1528 | }; |
1529 | ||
1530 | static const struct attribute_group adm1026_group_temp3 = { | |
1531 | .attrs = adm1026_attributes_temp3, | |
1532 | }; | |
1533 | ||
1534 | static struct attribute *adm1026_attributes_in8_9[] = { | |
1535 | &sensor_dev_attr_in8_input.dev_attr.attr, | |
1536 | &sensor_dev_attr_in8_max.dev_attr.attr, | |
1537 | &sensor_dev_attr_in8_min.dev_attr.attr, | |
1538 | &sensor_dev_attr_in8_alarm.dev_attr.attr, | |
1539 | &sensor_dev_attr_in9_input.dev_attr.attr, | |
1540 | &sensor_dev_attr_in9_max.dev_attr.attr, | |
1541 | &sensor_dev_attr_in9_min.dev_attr.attr, | |
1542 | &sensor_dev_attr_in9_alarm.dev_attr.attr, | |
1d5f2c16 | 1543 | NULL |
5b34dbcd JD |
1544 | }; |
1545 | ||
1546 | static const struct attribute_group adm1026_group_in8_9 = { | |
1547 | .attrs = adm1026_attributes_in8_9, | |
1548 | }; | |
1549 | ||
57f7eb0b | 1550 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 1551 | static int adm1026_detect(struct i2c_client *client, |
57f7eb0b | 1552 | struct i2c_board_info *info) |
1da177e4 | 1553 | { |
57f7eb0b JD |
1554 | struct i2c_adapter *adapter = client->adapter; |
1555 | int address = client->addr; | |
1da177e4 | 1556 | int company, verstep; |
1da177e4 LT |
1557 | |
1558 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | |
1559 | /* We need to be able to do byte I/O */ | |
57f7eb0b | 1560 | return -ENODEV; |
a0393713 | 1561 | } |
1da177e4 | 1562 | |
1da177e4 LT |
1563 | /* Now, we do the remaining detection. */ |
1564 | ||
f67fdabf JD |
1565 | company = adm1026_read_value(client, ADM1026_REG_COMPANY); |
1566 | verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP); | |
1da177e4 | 1567 | |
b55f3757 GR |
1568 | dev_dbg(&adapter->dev, |
1569 | "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n", | |
f67fdabf | 1570 | i2c_adapter_id(client->adapter), client->addr, |
1da177e4 LT |
1571 | company, verstep); |
1572 | ||
52df6440 JD |
1573 | /* Determine the chip type. */ |
1574 | dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n", | |
1575 | i2c_adapter_id(adapter), address); | |
1576 | if (company == ADM1026_COMPANY_ANALOG_DEV | |
1577 | && verstep == ADM1026_VERSTEP_ADM1026) { | |
1578 | /* Analog Devices ADM1026 */ | |
1579 | } else if (company == ADM1026_COMPANY_ANALOG_DEV | |
1580 | && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) { | |
b55f3757 GR |
1581 | dev_err(&adapter->dev, |
1582 | "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n", | |
1583 | verstep); | |
52df6440 | 1584 | } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) { |
b55f3757 GR |
1585 | dev_err(&adapter->dev, |
1586 | "Found version/stepping 0x%02x. Assuming generic ADM1026.\n", | |
52df6440 JD |
1587 | verstep); |
1588 | } else { | |
1589 | dev_dbg(&adapter->dev, "Autodetection failed\n"); | |
1590 | /* Not an ADM1026... */ | |
1591 | return -ENODEV; | |
1da177e4 | 1592 | } |
52df6440 | 1593 | |
57f7eb0b | 1594 | strlcpy(info->type, "adm1026", I2C_NAME_SIZE); |
1da177e4 | 1595 | |
57f7eb0b JD |
1596 | return 0; |
1597 | } | |
1598 | ||
97df5566 AL |
1599 | static void adm1026_print_gpio(struct i2c_client *client) |
1600 | { | |
1601 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1602 | int i; | |
1603 | ||
1604 | dev_dbg(&client->dev, "GPIO config is:\n"); | |
1605 | for (i = 0; i <= 7; ++i) { | |
1606 | if (data->config2 & (1 << i)) { | |
1607 | dev_dbg(&client->dev, "\t%sGP%s%d\n", | |
1608 | data->gpio_config[i] & 0x02 ? "" : "!", | |
1609 | data->gpio_config[i] & 0x01 ? "OUT" : "IN", | |
1610 | i); | |
1611 | } else { | |
1612 | dev_dbg(&client->dev, "\tFAN%d\n", i); | |
1613 | } | |
1614 | } | |
1615 | for (i = 8; i <= 15; ++i) { | |
1616 | dev_dbg(&client->dev, "\t%sGP%s%d\n", | |
1617 | data->gpio_config[i] & 0x02 ? "" : "!", | |
1618 | data->gpio_config[i] & 0x01 ? "OUT" : "IN", | |
1619 | i); | |
1620 | } | |
1621 | if (data->config3 & CFG3_GPIO16_ENABLE) { | |
1622 | dev_dbg(&client->dev, "\t%sGP%s16\n", | |
1623 | data->gpio_config[16] & 0x02 ? "" : "!", | |
1624 | data->gpio_config[16] & 0x01 ? "OUT" : "IN"); | |
1625 | } else { | |
1626 | /* GPIO16 is THERM */ | |
1627 | dev_dbg(&client->dev, "\tTHERM\n"); | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | static void adm1026_fixup_gpio(struct i2c_client *client) | |
1632 | { | |
1633 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1634 | int i; | |
1635 | int value; | |
1636 | ||
1637 | /* Make the changes requested. */ | |
1638 | /* | |
1639 | * We may need to unlock/stop monitoring or soft-reset the | |
1640 | * chip before we can make changes. This hasn't been | |
1641 | * tested much. FIXME | |
1642 | */ | |
1643 | ||
1644 | /* Make outputs */ | |
1645 | for (i = 0; i <= 16; ++i) { | |
1646 | if (gpio_output[i] >= 0 && gpio_output[i] <= 16) | |
1647 | data->gpio_config[gpio_output[i]] |= 0x01; | |
1648 | /* if GPIO0-7 is output, it isn't a FAN tach */ | |
1649 | if (gpio_output[i] >= 0 && gpio_output[i] <= 7) | |
1650 | data->config2 |= 1 << gpio_output[i]; | |
1651 | } | |
1652 | ||
1653 | /* Input overrides output */ | |
1654 | for (i = 0; i <= 16; ++i) { | |
1655 | if (gpio_input[i] >= 0 && gpio_input[i] <= 16) | |
1656 | data->gpio_config[gpio_input[i]] &= ~0x01; | |
1657 | /* if GPIO0-7 is input, it isn't a FAN tach */ | |
1658 | if (gpio_input[i] >= 0 && gpio_input[i] <= 7) | |
1659 | data->config2 |= 1 << gpio_input[i]; | |
1660 | } | |
1661 | ||
1662 | /* Inverted */ | |
1663 | for (i = 0; i <= 16; ++i) { | |
1664 | if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) | |
1665 | data->gpio_config[gpio_inverted[i]] &= ~0x02; | |
1666 | } | |
1667 | ||
1668 | /* Normal overrides inverted */ | |
1669 | for (i = 0; i <= 16; ++i) { | |
1670 | if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) | |
1671 | data->gpio_config[gpio_normal[i]] |= 0x02; | |
1672 | } | |
1673 | ||
1674 | /* Fan overrides input and output */ | |
1675 | for (i = 0; i <= 7; ++i) { | |
1676 | if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) | |
1677 | data->config2 &= ~(1 << gpio_fan[i]); | |
1678 | } | |
1679 | ||
1680 | /* Write new configs to registers */ | |
1681 | adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2); | |
1682 | data->config3 = (data->config3 & 0x3f) | |
1683 | | ((data->gpio_config[16] & 0x03) << 6); | |
1684 | adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3); | |
1685 | for (i = 15, value = 0; i >= 0; --i) { | |
1686 | value <<= 2; | |
1687 | value |= data->gpio_config[i] & 0x03; | |
1688 | if ((i & 0x03) == 0) { | |
1689 | adm1026_write_value(client, | |
1690 | ADM1026_REG_GPIO_CFG_0_3 + i/4, | |
1691 | value); | |
1692 | value = 0; | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | /* Print the new config */ | |
1697 | adm1026_print_gpio(client); | |
1698 | } | |
1699 | ||
1700 | static void adm1026_init_client(struct i2c_client *client) | |
1701 | { | |
1702 | int value, i; | |
1703 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1704 | ||
1705 | dev_dbg(&client->dev, "Initializing device\n"); | |
1706 | /* Read chip config */ | |
1707 | data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1); | |
1708 | data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2); | |
1709 | data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3); | |
1710 | ||
1711 | /* Inform user of chip config */ | |
1712 | dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n", | |
1713 | data->config1); | |
1714 | if ((data->config1 & CFG1_MONITOR) == 0) { | |
1715 | dev_dbg(&client->dev, | |
1716 | "Monitoring not currently enabled.\n"); | |
1717 | } | |
1718 | if (data->config1 & CFG1_INT_ENABLE) { | |
1719 | dev_dbg(&client->dev, | |
1720 | "SMBALERT interrupts are enabled.\n"); | |
1721 | } | |
1722 | if (data->config1 & CFG1_AIN8_9) { | |
1723 | dev_dbg(&client->dev, | |
1724 | "in8 and in9 enabled. temp3 disabled.\n"); | |
1725 | } else { | |
1726 | dev_dbg(&client->dev, | |
1727 | "temp3 enabled. in8 and in9 disabled.\n"); | |
1728 | } | |
1729 | if (data->config1 & CFG1_THERM_HOT) { | |
1730 | dev_dbg(&client->dev, | |
1731 | "Automatic THERM, PWM, and temp limits enabled.\n"); | |
1732 | } | |
1733 | ||
1734 | if (data->config3 & CFG3_GPIO16_ENABLE) { | |
1735 | dev_dbg(&client->dev, | |
1736 | "GPIO16 enabled. THERM pin disabled.\n"); | |
1737 | } else { | |
1738 | dev_dbg(&client->dev, | |
1739 | "THERM pin enabled. GPIO16 disabled.\n"); | |
1740 | } | |
1741 | if (data->config3 & CFG3_VREF_250) | |
1742 | dev_dbg(&client->dev, "Vref is 2.50 Volts.\n"); | |
1743 | else | |
1744 | dev_dbg(&client->dev, "Vref is 1.82 Volts.\n"); | |
1745 | /* Read and pick apart the existing GPIO configuration */ | |
1746 | value = 0; | |
1747 | for (i = 0; i <= 15; ++i) { | |
1748 | if ((i & 0x03) == 0) { | |
1749 | value = adm1026_read_value(client, | |
1750 | ADM1026_REG_GPIO_CFG_0_3 + i / 4); | |
1751 | } | |
1752 | data->gpio_config[i] = value & 0x03; | |
1753 | value >>= 2; | |
1754 | } | |
1755 | data->gpio_config[16] = (data->config3 >> 6) & 0x03; | |
1756 | ||
1757 | /* ... and then print it */ | |
1758 | adm1026_print_gpio(client); | |
1759 | ||
1760 | /* | |
1761 | * If the user asks us to reprogram the GPIO config, then | |
1762 | * do it now. | |
1763 | */ | |
1764 | if (gpio_input[0] != -1 || gpio_output[0] != -1 | |
1765 | || gpio_inverted[0] != -1 || gpio_normal[0] != -1 | |
1766 | || gpio_fan[0] != -1) { | |
1767 | adm1026_fixup_gpio(client); | |
1768 | } | |
1769 | ||
1770 | /* | |
1771 | * WE INTENTIONALLY make no changes to the limits, | |
1772 | * offsets, pwms, fans and zones. If they were | |
1773 | * configured, we don't want to mess with them. | |
1774 | * If they weren't, the default is 100% PWM, no | |
1775 | * control and will suffice until 'sensors -s' | |
1776 | * can be run by the user. We DO set the default | |
1777 | * value for pwm1.auto_pwm_min to its maximum | |
1778 | * so that enabling automatic pwm fan control | |
1779 | * without first setting a value for pwm1.auto_pwm_min | |
1780 | * will not result in potentially dangerous fan speed decrease. | |
1781 | */ | |
1782 | data->pwm1.auto_pwm_min = 255; | |
1783 | /* Start monitoring */ | |
1784 | value = adm1026_read_value(client, ADM1026_REG_CONFIG1); | |
1785 | /* Set MONITOR, clear interrupt acknowledge and s/w reset */ | |
1786 | value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET); | |
1787 | dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value); | |
1788 | data->config1 = value; | |
1789 | adm1026_write_value(client, ADM1026_REG_CONFIG1, value); | |
1790 | ||
1791 | /* initialize fan_div[] to hardware defaults */ | |
1792 | value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) | | |
1793 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8); | |
1794 | for (i = 0; i <= 7; ++i) { | |
1795 | data->fan_div[i] = DIV_FROM_REG(value & 0x03); | |
1796 | value >>= 2; | |
1797 | } | |
1798 | } | |
1799 | ||
57f7eb0b JD |
1800 | static int adm1026_probe(struct i2c_client *client, |
1801 | const struct i2c_device_id *id) | |
1802 | { | |
1803 | struct adm1026_data *data; | |
1804 | int err; | |
1805 | ||
3421e212 GR |
1806 | data = devm_kzalloc(&client->dev, sizeof(struct adm1026_data), |
1807 | GFP_KERNEL); | |
1808 | if (!data) | |
1809 | return -ENOMEM; | |
1da177e4 | 1810 | |
57f7eb0b | 1811 | i2c_set_clientdata(client, data); |
9a61bf63 | 1812 | mutex_init(&data->update_lock); |
1da177e4 | 1813 | |
1da177e4 | 1814 | /* Set the VRM version */ |
303760b4 | 1815 | data->vrm = vid_which_vrm(); |
1da177e4 LT |
1816 | |
1817 | /* Initialize the ADM1026 chip */ | |
f67fdabf | 1818 | adm1026_init_client(client); |
1da177e4 LT |
1819 | |
1820 | /* Register sysfs hooks */ | |
86aa3e22 GR |
1821 | err = sysfs_create_group(&client->dev.kobj, &adm1026_group); |
1822 | if (err) | |
3421e212 | 1823 | return err; |
5b34dbcd JD |
1824 | if (data->config1 & CFG1_AIN8_9) |
1825 | err = sysfs_create_group(&client->dev.kobj, | |
1826 | &adm1026_group_in8_9); | |
1827 | else | |
1828 | err = sysfs_create_group(&client->dev.kobj, | |
1829 | &adm1026_group_temp3); | |
1830 | if (err) | |
1831 | goto exitremove; | |
681c6f7a | 1832 | |
f67fdabf | 1833 | data->hwmon_dev = hwmon_device_register(&client->dev); |
1beeffe4 TJ |
1834 | if (IS_ERR(data->hwmon_dev)) { |
1835 | err = PTR_ERR(data->hwmon_dev); | |
681c6f7a | 1836 | goto exitremove; |
943b0830 MH |
1837 | } |
1838 | ||
1da177e4 LT |
1839 | return 0; |
1840 | ||
1841 | /* Error out and cleanup code */ | |
681c6f7a | 1842 | exitremove: |
f67fdabf | 1843 | sysfs_remove_group(&client->dev.kobj, &adm1026_group); |
5b34dbcd JD |
1844 | if (data->config1 & CFG1_AIN8_9) |
1845 | sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9); | |
1846 | else | |
1847 | sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3); | |
1da177e4 LT |
1848 | return err; |
1849 | } | |
681c6f7a | 1850 | |
57f7eb0b | 1851 | static int adm1026_remove(struct i2c_client *client) |
681c6f7a MH |
1852 | { |
1853 | struct adm1026_data *data = i2c_get_clientdata(client); | |
1beeffe4 | 1854 | hwmon_device_unregister(data->hwmon_dev); |
681c6f7a | 1855 | sysfs_remove_group(&client->dev.kobj, &adm1026_group); |
5b34dbcd JD |
1856 | if (data->config1 & CFG1_AIN8_9) |
1857 | sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9); | |
1858 | else | |
1859 | sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3); | |
681c6f7a MH |
1860 | return 0; |
1861 | } | |
1862 | ||
97df5566 AL |
1863 | static const struct i2c_device_id adm1026_id[] = { |
1864 | { "adm1026", 0 }, | |
1865 | { } | |
1866 | }; | |
1867 | MODULE_DEVICE_TABLE(i2c, adm1026_id); | |
1868 | ||
1869 | static struct i2c_driver adm1026_driver = { | |
1870 | .class = I2C_CLASS_HWMON, | |
1871 | .driver = { | |
1872 | .name = "adm1026", | |
1873 | }, | |
1874 | .probe = adm1026_probe, | |
1875 | .remove = adm1026_remove, | |
1876 | .id_table = adm1026_id, | |
1877 | .detect = adm1026_detect, | |
1878 | .address_list = normal_i2c, | |
1879 | }; | |
1880 | ||
f0967eea | 1881 | module_i2c_driver(adm1026_driver); |
1da177e4 LT |
1882 | |
1883 | MODULE_LICENSE("GPL"); | |
1884 | MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, " | |
cb01a231 | 1885 | "Justin Thiessen <jthiessen@penguincomputing.com>"); |
1da177e4 | 1886 | MODULE_DESCRIPTION("ADM1026 driver"); |