]>
Commit | Line | Data |
---|---|---|
84f1e442 RV |
1 | /* |
2 | * f75375s.c - driver for the Fintek F75375/SP and F75373 | |
3 | * hardware monitoring features | |
4 | * Copyright (C) 2006-2007 Riku Voipio <riku.voipio@movial.fi> | |
5 | * | |
6 | * Datasheets available at: | |
7 | * | |
8 | * f75375: | |
9 | * http://www.fintek.com.tw/files/productfiles/2005111152950.pdf | |
10 | * | |
11 | * f75373: | |
12 | * http://www.fintek.com.tw/files/productfiles/2005111153128.pdf | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 | * | |
28 | */ | |
29 | ||
30 | #include <linux/module.h> | |
31 | #include <linux/jiffies.h> | |
32 | #include <linux/hwmon.h> | |
33 | #include <linux/hwmon-sysfs.h> | |
34 | #include <linux/i2c.h> | |
35 | #include <linux/err.h> | |
36 | #include <linux/mutex.h> | |
ff312d07 | 37 | #include <linux/f75375s.h> |
84f1e442 RV |
38 | |
39 | /* Addresses to scan */ | |
25e9c86d | 40 | static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END }; |
84f1e442 RV |
41 | |
42 | /* Insmod parameters */ | |
43 | I2C_CLIENT_INSMOD_2(f75373, f75375); | |
44 | ||
45 | /* Fintek F75375 registers */ | |
46 | #define F75375_REG_CONFIG0 0x0 | |
47 | #define F75375_REG_CONFIG1 0x1 | |
48 | #define F75375_REG_CONFIG2 0x2 | |
49 | #define F75375_REG_CONFIG3 0x3 | |
50 | #define F75375_REG_ADDR 0x4 | |
51 | #define F75375_REG_INTR 0x31 | |
52 | #define F75375_CHIP_ID 0x5A | |
53 | #define F75375_REG_VERSION 0x5C | |
54 | #define F75375_REG_VENDOR 0x5D | |
55 | #define F75375_REG_FAN_TIMER 0x60 | |
56 | ||
57 | #define F75375_REG_VOLT(nr) (0x10 + (nr)) | |
58 | #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2) | |
59 | #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2) | |
60 | ||
61 | #define F75375_REG_TEMP(nr) (0x14 + (nr)) | |
62 | #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2) | |
63 | #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2) | |
64 | ||
65 | #define F75375_REG_FAN(nr) (0x16 + (nr) * 2) | |
66 | #define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2) | |
67 | #define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10) | |
68 | #define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10) | |
69 | #define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10) | |
70 | ||
71 | #define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10) | |
72 | #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step)) | |
73 | #define F75375_REG_FAN_B_SPEED(nr, step) \ | |
74 | ((0xA5 + (nr) * 0x10) + (step) * 2) | |
75 | ||
76 | #define F75375_REG_PWM1_RAISE_DUTY 0x69 | |
77 | #define F75375_REG_PWM2_RAISE_DUTY 0x6A | |
78 | #define F75375_REG_PWM1_DROP_DUTY 0x6B | |
79 | #define F75375_REG_PWM2_DROP_DUTY 0x6C | |
80 | ||
81 | #define FAN_CTRL_LINEAR(nr) (4 + nr) | |
82 | #define FAN_CTRL_MODE(nr) (5 + ((nr) * 2)) | |
83 | ||
84 | /* | |
85 | * Data structures and manipulation thereof | |
86 | */ | |
87 | ||
88 | struct f75375_data { | |
89 | unsigned short addr; | |
620c142d | 90 | struct i2c_client *client; |
1beeffe4 | 91 | struct device *hwmon_dev; |
84f1e442 RV |
92 | |
93 | const char *name; | |
94 | int kind; | |
95 | struct mutex update_lock; /* protect register access */ | |
96 | char valid; | |
97 | unsigned long last_updated; /* In jiffies */ | |
98 | unsigned long last_limits; /* In jiffies */ | |
99 | ||
100 | /* Register values */ | |
101 | u8 in[4]; | |
102 | u8 in_max[4]; | |
103 | u8 in_min[4]; | |
104 | u16 fan[2]; | |
105 | u16 fan_min[2]; | |
106 | u16 fan_full[2]; | |
107 | u16 fan_exp[2]; | |
108 | u8 fan_timer; | |
109 | u8 pwm[2]; | |
110 | u8 pwm_mode[2]; | |
111 | u8 pwm_enable[2]; | |
112 | s8 temp[2]; | |
113 | s8 temp_high[2]; | |
114 | s8 temp_max_hyst[2]; | |
115 | }; | |
116 | ||
117 | static int f75375_attach_adapter(struct i2c_adapter *adapter); | |
118 | static int f75375_detect(struct i2c_adapter *adapter, int address, int kind); | |
119 | static int f75375_detach_client(struct i2c_client *client); | |
d2653e92 JD |
120 | static int f75375_probe(struct i2c_client *client, |
121 | const struct i2c_device_id *id); | |
620c142d | 122 | static int f75375_remove(struct i2c_client *client); |
84f1e442 | 123 | |
620c142d | 124 | static struct i2c_driver f75375_legacy_driver = { |
84f1e442 | 125 | .driver = { |
620c142d | 126 | .name = "f75375_legacy", |
84f1e442 RV |
127 | }, |
128 | .attach_adapter = f75375_attach_adapter, | |
129 | .detach_client = f75375_detach_client, | |
130 | }; | |
131 | ||
620c142d RV |
132 | static struct i2c_driver f75375_driver = { |
133 | .driver = { | |
134 | .name = "f75375", | |
135 | }, | |
136 | .probe = f75375_probe, | |
137 | .remove = f75375_remove, | |
138 | }; | |
139 | ||
84f1e442 RV |
140 | static inline int f75375_read8(struct i2c_client *client, u8 reg) |
141 | { | |
142 | return i2c_smbus_read_byte_data(client, reg); | |
143 | } | |
144 | ||
145 | /* in most cases, should be called while holding update_lock */ | |
146 | static inline u16 f75375_read16(struct i2c_client *client, u8 reg) | |
147 | { | |
148 | return ((i2c_smbus_read_byte_data(client, reg) << 8) | |
149 | | i2c_smbus_read_byte_data(client, reg + 1)); | |
150 | } | |
151 | ||
152 | static inline void f75375_write8(struct i2c_client *client, u8 reg, | |
153 | u8 value) | |
154 | { | |
155 | i2c_smbus_write_byte_data(client, reg, value); | |
156 | } | |
157 | ||
158 | static inline void f75375_write16(struct i2c_client *client, u8 reg, | |
159 | u16 value) | |
160 | { | |
161 | int err = i2c_smbus_write_byte_data(client, reg, (value << 8)); | |
162 | if (err) | |
163 | return; | |
164 | i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF)); | |
165 | } | |
166 | ||
167 | static struct f75375_data *f75375_update_device(struct device *dev) | |
168 | { | |
169 | struct i2c_client *client = to_i2c_client(dev); | |
170 | struct f75375_data *data = i2c_get_clientdata(client); | |
171 | int nr; | |
172 | ||
173 | mutex_lock(&data->update_lock); | |
174 | ||
175 | /* Limit registers cache is refreshed after 60 seconds */ | |
176 | if (time_after(jiffies, data->last_limits + 60 * HZ) | |
177 | || !data->valid) { | |
178 | for (nr = 0; nr < 2; nr++) { | |
179 | data->temp_high[nr] = | |
180 | f75375_read8(client, F75375_REG_TEMP_HIGH(nr)); | |
181 | data->temp_max_hyst[nr] = | |
182 | f75375_read8(client, F75375_REG_TEMP_HYST(nr)); | |
183 | data->fan_full[nr] = | |
184 | f75375_read16(client, F75375_REG_FAN_FULL(nr)); | |
185 | data->fan_min[nr] = | |
186 | f75375_read16(client, F75375_REG_FAN_MIN(nr)); | |
187 | data->fan_exp[nr] = | |
188 | f75375_read16(client, F75375_REG_FAN_EXP(nr)); | |
189 | data->pwm[nr] = f75375_read8(client, | |
190 | F75375_REG_FAN_PWM_DUTY(nr)); | |
191 | ||
192 | } | |
193 | for (nr = 0; nr < 4; nr++) { | |
194 | data->in_max[nr] = | |
195 | f75375_read8(client, F75375_REG_VOLT_HIGH(nr)); | |
196 | data->in_min[nr] = | |
197 | f75375_read8(client, F75375_REG_VOLT_LOW(nr)); | |
198 | } | |
199 | data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER); | |
200 | data->last_limits = jiffies; | |
201 | } | |
202 | ||
203 | /* Measurement registers cache is refreshed after 2 second */ | |
204 | if (time_after(jiffies, data->last_updated + 2 * HZ) | |
205 | || !data->valid) { | |
206 | for (nr = 0; nr < 2; nr++) { | |
207 | data->temp[nr] = | |
208 | f75375_read8(client, F75375_REG_TEMP(nr)); | |
209 | data->fan[nr] = | |
210 | f75375_read16(client, F75375_REG_FAN(nr)); | |
211 | } | |
212 | for (nr = 0; nr < 4; nr++) | |
213 | data->in[nr] = | |
214 | f75375_read8(client, F75375_REG_VOLT(nr)); | |
215 | ||
216 | data->last_updated = jiffies; | |
217 | data->valid = 1; | |
218 | } | |
219 | ||
220 | mutex_unlock(&data->update_lock); | |
221 | return data; | |
222 | } | |
223 | ||
224 | static inline u16 rpm_from_reg(u16 reg) | |
225 | { | |
226 | if (reg == 0 || reg == 0xffff) | |
227 | return 0; | |
228 | return (1500000 / reg); | |
229 | } | |
230 | ||
231 | static inline u16 rpm_to_reg(int rpm) | |
232 | { | |
233 | if (rpm < 367 || rpm > 0xffff) | |
234 | return 0xffff; | |
235 | return (1500000 / rpm); | |
236 | } | |
237 | ||
238 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, | |
239 | const char *buf, size_t count) | |
240 | { | |
241 | int nr = to_sensor_dev_attr(attr)->index; | |
242 | struct i2c_client *client = to_i2c_client(dev); | |
243 | struct f75375_data *data = i2c_get_clientdata(client); | |
244 | int val = simple_strtoul(buf, NULL, 10); | |
245 | ||
246 | mutex_lock(&data->update_lock); | |
247 | data->fan_min[nr] = rpm_to_reg(val); | |
248 | f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]); | |
249 | mutex_unlock(&data->update_lock); | |
250 | return count; | |
251 | } | |
252 | ||
253 | static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr, | |
254 | const char *buf, size_t count) | |
255 | { | |
256 | int nr = to_sensor_dev_attr(attr)->index; | |
257 | struct i2c_client *client = to_i2c_client(dev); | |
258 | struct f75375_data *data = i2c_get_clientdata(client); | |
259 | int val = simple_strtoul(buf, NULL, 10); | |
260 | ||
261 | mutex_lock(&data->update_lock); | |
262 | data->fan_exp[nr] = rpm_to_reg(val); | |
263 | f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]); | |
264 | mutex_unlock(&data->update_lock); | |
265 | return count; | |
266 | } | |
267 | ||
268 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, | |
269 | const char *buf, size_t count) | |
270 | { | |
271 | int nr = to_sensor_dev_attr(attr)->index; | |
272 | struct i2c_client *client = to_i2c_client(dev); | |
273 | struct f75375_data *data = i2c_get_clientdata(client); | |
274 | int val = simple_strtoul(buf, NULL, 10); | |
275 | ||
276 | mutex_lock(&data->update_lock); | |
277 | data->pwm[nr] = SENSORS_LIMIT(val, 0, 255); | |
278 | f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]); | |
279 | mutex_unlock(&data->update_lock); | |
280 | return count; | |
281 | } | |
282 | ||
283 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute | |
284 | *attr, char *buf) | |
285 | { | |
286 | int nr = to_sensor_dev_attr(attr)->index; | |
287 | struct f75375_data *data = f75375_update_device(dev); | |
288 | return sprintf(buf, "%d\n", data->pwm_enable[nr]); | |
289 | } | |
290 | ||
ff312d07 | 291 | static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val) |
84f1e442 | 292 | { |
84f1e442 | 293 | struct f75375_data *data = i2c_get_clientdata(client); |
84f1e442 RV |
294 | u8 fanmode; |
295 | ||
296 | if (val < 0 || val > 4) | |
297 | return -EINVAL; | |
298 | ||
84f1e442 RV |
299 | fanmode = f75375_read8(client, F75375_REG_FAN_TIMER); |
300 | fanmode = ~(3 << FAN_CTRL_MODE(nr)); | |
301 | ||
302 | switch (val) { | |
303 | case 0: /* Full speed */ | |
304 | fanmode |= (3 << FAN_CTRL_MODE(nr)); | |
305 | data->pwm[nr] = 255; | |
306 | f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), | |
307 | data->pwm[nr]); | |
308 | break; | |
309 | case 1: /* PWM */ | |
310 | fanmode |= (3 << FAN_CTRL_MODE(nr)); | |
311 | break; | |
312 | case 2: /* AUTOMATIC*/ | |
313 | fanmode |= (2 << FAN_CTRL_MODE(nr)); | |
314 | break; | |
315 | case 3: /* fan speed */ | |
316 | break; | |
317 | } | |
318 | f75375_write8(client, F75375_REG_FAN_TIMER, fanmode); | |
319 | data->pwm_enable[nr] = val; | |
ff312d07 RV |
320 | return 0; |
321 | } | |
322 | ||
323 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, | |
324 | const char *buf, size_t count) | |
325 | { | |
326 | int nr = to_sensor_dev_attr(attr)->index; | |
327 | struct i2c_client *client = to_i2c_client(dev); | |
328 | struct f75375_data *data = i2c_get_clientdata(client); | |
329 | int val = simple_strtoul(buf, NULL, 10); | |
330 | int err = 0; | |
331 | ||
332 | mutex_lock(&data->update_lock); | |
333 | err = set_pwm_enable_direct(client, nr, val); | |
84f1e442 | 334 | mutex_unlock(&data->update_lock); |
ff312d07 | 335 | return err ? err : count; |
84f1e442 RV |
336 | } |
337 | ||
338 | static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr, | |
339 | const char *buf, size_t count) | |
340 | { | |
341 | int nr = to_sensor_dev_attr(attr)->index; | |
342 | struct i2c_client *client = to_i2c_client(dev); | |
343 | struct f75375_data *data = i2c_get_clientdata(client); | |
344 | int val = simple_strtoul(buf, NULL, 10); | |
345 | u8 conf = 0; | |
346 | ||
76e83bef | 347 | if (!(val == 0 || val == 1)) |
84f1e442 RV |
348 | return -EINVAL; |
349 | ||
350 | mutex_lock(&data->update_lock); | |
351 | conf = f75375_read8(client, F75375_REG_CONFIG1); | |
352 | conf = ~(1 << FAN_CTRL_LINEAR(nr)); | |
353 | ||
354 | if (val == 0) | |
355 | conf |= (1 << FAN_CTRL_LINEAR(nr)) ; | |
356 | ||
357 | f75375_write8(client, F75375_REG_CONFIG1, conf); | |
358 | data->pwm_mode[nr] = val; | |
359 | mutex_unlock(&data->update_lock); | |
360 | return count; | |
361 | } | |
362 | ||
363 | static ssize_t show_pwm(struct device *dev, struct device_attribute | |
364 | *attr, char *buf) | |
365 | { | |
366 | int nr = to_sensor_dev_attr(attr)->index; | |
367 | struct f75375_data *data = f75375_update_device(dev); | |
368 | return sprintf(buf, "%d\n", data->pwm[nr]); | |
369 | } | |
370 | ||
371 | static ssize_t show_pwm_mode(struct device *dev, struct device_attribute | |
372 | *attr, char *buf) | |
373 | { | |
374 | int nr = to_sensor_dev_attr(attr)->index; | |
375 | struct f75375_data *data = f75375_update_device(dev); | |
376 | return sprintf(buf, "%d\n", data->pwm_mode[nr]); | |
377 | } | |
378 | ||
379 | #define VOLT_FROM_REG(val) ((val) * 8) | |
380 | #define VOLT_TO_REG(val) ((val) / 8) | |
381 | ||
382 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, | |
383 | char *buf) | |
384 | { | |
385 | int nr = to_sensor_dev_attr(attr)->index; | |
386 | struct f75375_data *data = f75375_update_device(dev); | |
387 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr])); | |
388 | } | |
389 | ||
390 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, | |
391 | char *buf) | |
392 | { | |
393 | int nr = to_sensor_dev_attr(attr)->index; | |
394 | struct f75375_data *data = f75375_update_device(dev); | |
395 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr])); | |
396 | } | |
397 | ||
398 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, | |
399 | char *buf) | |
400 | { | |
401 | int nr = to_sensor_dev_attr(attr)->index; | |
402 | struct f75375_data *data = f75375_update_device(dev); | |
403 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr])); | |
404 | } | |
405 | ||
406 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, | |
407 | const char *buf, size_t count) | |
408 | { | |
409 | int nr = to_sensor_dev_attr(attr)->index; | |
410 | struct i2c_client *client = to_i2c_client(dev); | |
411 | struct f75375_data *data = i2c_get_clientdata(client); | |
412 | int val = simple_strtoul(buf, NULL, 10); | |
413 | val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff); | |
414 | mutex_lock(&data->update_lock); | |
415 | data->in_max[nr] = val; | |
416 | f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]); | |
417 | mutex_unlock(&data->update_lock); | |
418 | return count; | |
419 | } | |
420 | ||
421 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, | |
422 | const char *buf, size_t count) | |
423 | { | |
424 | int nr = to_sensor_dev_attr(attr)->index; | |
425 | struct i2c_client *client = to_i2c_client(dev); | |
426 | struct f75375_data *data = i2c_get_clientdata(client); | |
427 | int val = simple_strtoul(buf, NULL, 10); | |
428 | val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff); | |
429 | mutex_lock(&data->update_lock); | |
430 | data->in_min[nr] = val; | |
431 | f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]); | |
432 | mutex_unlock(&data->update_lock); | |
433 | return count; | |
434 | } | |
435 | #define TEMP_FROM_REG(val) ((val) * 1000) | |
436 | #define TEMP_TO_REG(val) ((val) / 1000) | |
437 | ||
438 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | |
439 | char *buf) | |
440 | { | |
441 | int nr = to_sensor_dev_attr(attr)->index; | |
442 | struct f75375_data *data = f75375_update_device(dev); | |
443 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); | |
444 | } | |
445 | ||
446 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | |
447 | char *buf) | |
448 | { | |
449 | int nr = to_sensor_dev_attr(attr)->index; | |
450 | struct f75375_data *data = f75375_update_device(dev); | |
451 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])); | |
452 | } | |
453 | ||
454 | static ssize_t show_temp_max_hyst(struct device *dev, | |
455 | struct device_attribute *attr, char *buf) | |
456 | { | |
457 | int nr = to_sensor_dev_attr(attr)->index; | |
458 | struct f75375_data *data = f75375_update_device(dev); | |
459 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr])); | |
460 | } | |
461 | ||
462 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | |
463 | const char *buf, size_t count) | |
464 | { | |
465 | int nr = to_sensor_dev_attr(attr)->index; | |
466 | struct i2c_client *client = to_i2c_client(dev); | |
467 | struct f75375_data *data = i2c_get_clientdata(client); | |
468 | int val = simple_strtol(buf, NULL, 10); | |
469 | val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127); | |
470 | mutex_lock(&data->update_lock); | |
471 | data->temp_high[nr] = val; | |
472 | f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]); | |
473 | mutex_unlock(&data->update_lock); | |
474 | return count; | |
475 | } | |
476 | ||
477 | static ssize_t set_temp_max_hyst(struct device *dev, | |
478 | struct device_attribute *attr, const char *buf, size_t count) | |
479 | { | |
480 | int nr = to_sensor_dev_attr(attr)->index; | |
481 | struct i2c_client *client = to_i2c_client(dev); | |
482 | struct f75375_data *data = i2c_get_clientdata(client); | |
483 | int val = simple_strtol(buf, NULL, 10); | |
484 | val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127); | |
485 | mutex_lock(&data->update_lock); | |
486 | data->temp_max_hyst[nr] = val; | |
487 | f75375_write8(client, F75375_REG_TEMP_HYST(nr), | |
488 | data->temp_max_hyst[nr]); | |
489 | mutex_unlock(&data->update_lock); | |
490 | return count; | |
491 | } | |
492 | ||
493 | #define show_fan(thing) \ | |
494 | static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \ | |
495 | char *buf)\ | |
496 | {\ | |
497 | int nr = to_sensor_dev_attr(attr)->index;\ | |
498 | struct f75375_data *data = f75375_update_device(dev); \ | |
499 | return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \ | |
500 | } | |
501 | ||
502 | show_fan(fan); | |
503 | show_fan(fan_min); | |
504 | show_fan(fan_full); | |
505 | show_fan(fan_exp); | |
506 | ||
507 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0); | |
508 | static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR, | |
509 | show_in_max, set_in_max, 0); | |
510 | static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR, | |
511 | show_in_min, set_in_min, 0); | |
512 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1); | |
513 | static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR, | |
514 | show_in_max, set_in_max, 1); | |
515 | static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR, | |
516 | show_in_min, set_in_min, 1); | |
517 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2); | |
518 | static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR, | |
519 | show_in_max, set_in_max, 2); | |
520 | static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR, | |
521 | show_in_min, set_in_min, 2); | |
522 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3); | |
523 | static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR, | |
524 | show_in_max, set_in_max, 3); | |
525 | static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR, | |
526 | show_in_min, set_in_min, 3); | |
527 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | |
528 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR, | |
529 | show_temp_max_hyst, set_temp_max_hyst, 0); | |
530 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR, | |
531 | show_temp_max, set_temp_max, 0); | |
532 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | |
533 | static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR, | |
534 | show_temp_max_hyst, set_temp_max_hyst, 1); | |
535 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR, | |
536 | show_temp_max, set_temp_max, 1); | |
537 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); | |
538 | static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0); | |
539 | static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR, | |
540 | show_fan_min, set_fan_min, 0); | |
541 | static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR, | |
542 | show_fan_exp, set_fan_exp, 0); | |
543 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); | |
544 | static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1); | |
545 | static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR, | |
546 | show_fan_min, set_fan_min, 1); | |
547 | static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR, | |
548 | show_fan_exp, set_fan_exp, 1); | |
549 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, | |
550 | show_pwm, set_pwm, 0); | |
551 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, | |
552 | show_pwm_enable, set_pwm_enable, 0); | |
76e83bef | 553 | static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, |
84f1e442 RV |
554 | show_pwm_mode, set_pwm_mode, 0); |
555 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, | |
556 | show_pwm, set_pwm, 1); | |
557 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, | |
558 | show_pwm_enable, set_pwm_enable, 1); | |
76e83bef | 559 | static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, |
84f1e442 RV |
560 | show_pwm_mode, set_pwm_mode, 1); |
561 | ||
562 | static struct attribute *f75375_attributes[] = { | |
563 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
564 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
565 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | |
566 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
567 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
568 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, | |
569 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
570 | &sensor_dev_attr_fan1_full.dev_attr.attr, | |
571 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
572 | &sensor_dev_attr_fan1_exp.dev_attr.attr, | |
573 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
574 | &sensor_dev_attr_fan2_full.dev_attr.attr, | |
575 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
576 | &sensor_dev_attr_fan2_exp.dev_attr.attr, | |
577 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
578 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, | |
579 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, | |
580 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
581 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | |
582 | &sensor_dev_attr_pwm2_mode.dev_attr.attr, | |
583 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
584 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
585 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
586 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
587 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
588 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
589 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
590 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
591 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
592 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
593 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
594 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
595 | NULL | |
596 | }; | |
597 | ||
598 | static const struct attribute_group f75375_group = { | |
599 | .attrs = f75375_attributes, | |
600 | }; | |
601 | ||
602 | static int f75375_detach_client(struct i2c_client *client) | |
603 | { | |
84f1e442 RV |
604 | int err; |
605 | ||
620c142d | 606 | f75375_remove(client); |
84f1e442 RV |
607 | err = i2c_detach_client(client); |
608 | if (err) { | |
609 | dev_err(&client->dev, | |
610 | "Client deregistration failed, " | |
611 | "client not detached.\n"); | |
612 | return err; | |
613 | } | |
620c142d RV |
614 | kfree(client); |
615 | return 0; | |
616 | } | |
617 | ||
ff312d07 RV |
618 | static void f75375_init(struct i2c_client *client, struct f75375_data *data, |
619 | struct f75375s_platform_data *f75375s_pdata) | |
620 | { | |
621 | int nr; | |
622 | set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]); | |
623 | set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]); | |
624 | for (nr = 0; nr < 2; nr++) { | |
625 | data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255); | |
626 | f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), | |
627 | data->pwm[nr]); | |
628 | } | |
629 | ||
630 | } | |
631 | ||
d2653e92 JD |
632 | static int f75375_probe(struct i2c_client *client, |
633 | const struct i2c_device_id *id) | |
620c142d RV |
634 | { |
635 | struct f75375_data *data = i2c_get_clientdata(client); | |
ff312d07 | 636 | struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data; |
620c142d RV |
637 | int err; |
638 | ||
639 | if (!i2c_check_functionality(client->adapter, | |
640 | I2C_FUNC_SMBUS_BYTE_DATA)) | |
641 | return -EIO; | |
642 | if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL))) | |
643 | return -ENOMEM; | |
644 | ||
645 | i2c_set_clientdata(client, data); | |
646 | data->client = client; | |
647 | mutex_init(&data->update_lock); | |
648 | ||
649 | if (strcmp(client->name, "f75375") == 0) | |
650 | data->kind = f75375; | |
651 | else if (strcmp(client->name, "f75373") == 0) | |
652 | data->kind = f75373; | |
653 | else { | |
654 | dev_err(&client->dev, "Unsupported device: %s\n", client->name); | |
655 | return -ENODEV; | |
656 | } | |
657 | ||
658 | if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group))) | |
659 | goto exit_free; | |
660 | ||
76e83bef RV |
661 | if (data->kind == f75375) { |
662 | err = sysfs_chmod_file(&client->dev.kobj, | |
663 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, | |
664 | S_IRUGO | S_IWUSR); | |
665 | if (err) | |
666 | goto exit_remove; | |
667 | err = sysfs_chmod_file(&client->dev.kobj, | |
668 | &sensor_dev_attr_pwm2_mode.dev_attr.attr, | |
669 | S_IRUGO | S_IWUSR); | |
670 | if (err) | |
671 | goto exit_remove; | |
672 | } | |
673 | ||
620c142d RV |
674 | data->hwmon_dev = hwmon_device_register(&client->dev); |
675 | if (IS_ERR(data->hwmon_dev)) { | |
676 | err = PTR_ERR(data->hwmon_dev); | |
677 | goto exit_remove; | |
678 | } | |
679 | ||
ff312d07 RV |
680 | if (f75375s_pdata != NULL) |
681 | f75375_init(client, data, f75375s_pdata); | |
682 | ||
620c142d RV |
683 | return 0; |
684 | ||
685 | exit_remove: | |
686 | sysfs_remove_group(&client->dev.kobj, &f75375_group); | |
687 | exit_free: | |
688 | kfree(data); | |
689 | i2c_set_clientdata(client, NULL); | |
690 | return err; | |
691 | } | |
692 | ||
693 | static int f75375_remove(struct i2c_client *client) | |
694 | { | |
695 | struct f75375_data *data = i2c_get_clientdata(client); | |
696 | hwmon_device_unregister(data->hwmon_dev); | |
697 | sysfs_remove_group(&client->dev.kobj, &f75375_group); | |
84f1e442 | 698 | kfree(data); |
620c142d | 699 | i2c_set_clientdata(client, NULL); |
84f1e442 RV |
700 | return 0; |
701 | } | |
702 | ||
703 | static int f75375_attach_adapter(struct i2c_adapter *adapter) | |
704 | { | |
705 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
706 | return 0; | |
707 | return i2c_probe(adapter, &addr_data, f75375_detect); | |
708 | } | |
709 | ||
710 | /* This function is called by i2c_probe */ | |
711 | static int f75375_detect(struct i2c_adapter *adapter, int address, int kind) | |
712 | { | |
713 | struct i2c_client *client; | |
84f1e442 RV |
714 | u8 version = 0; |
715 | int err = 0; | |
716 | const char *name = ""; | |
717 | ||
620c142d | 718 | if (!(client = kzalloc(sizeof(*client), GFP_KERNEL))) { |
84f1e442 RV |
719 | err = -ENOMEM; |
720 | goto exit; | |
721 | } | |
84f1e442 RV |
722 | client->addr = address; |
723 | client->adapter = adapter; | |
620c142d | 724 | client->driver = &f75375_legacy_driver; |
84f1e442 RV |
725 | |
726 | if (kind < 0) { | |
727 | u16 vendid = f75375_read16(client, F75375_REG_VENDOR); | |
728 | u16 chipid = f75375_read16(client, F75375_CHIP_ID); | |
729 | version = f75375_read8(client, F75375_REG_VERSION); | |
730 | if (chipid == 0x0306 && vendid == 0x1934) { | |
731 | kind = f75375; | |
732 | } else if (chipid == 0x0204 && vendid == 0x1934) { | |
733 | kind = f75373; | |
734 | } else { | |
735 | dev_err(&adapter->dev, | |
736 | "failed,%02X,%02X,%02X\n", | |
737 | chipid, version, vendid); | |
738 | goto exit_free; | |
739 | } | |
740 | } | |
741 | ||
742 | if (kind == f75375) { | |
743 | name = "f75375"; | |
744 | } else if (kind == f75373) { | |
745 | name = "f75373"; | |
746 | } | |
84f1e442 RV |
747 | dev_info(&adapter->dev, "found %s version: %02X\n", name, version); |
748 | strlcpy(client->name, name, I2C_NAME_SIZE); | |
620c142d | 749 | |
84f1e442 RV |
750 | if ((err = i2c_attach_client(client))) |
751 | goto exit_free; | |
752 | ||
d2653e92 | 753 | if ((err = f75375_probe(client, NULL)) < 0) |
84f1e442 RV |
754 | goto exit_detach; |
755 | ||
84f1e442 RV |
756 | return 0; |
757 | ||
84f1e442 RV |
758 | exit_detach: |
759 | i2c_detach_client(client); | |
760 | exit_free: | |
620c142d | 761 | kfree(client); |
84f1e442 RV |
762 | exit: |
763 | return err; | |
764 | } | |
765 | ||
766 | static int __init sensors_f75375_init(void) | |
767 | { | |
620c142d RV |
768 | int status; |
769 | status = i2c_add_driver(&f75375_driver); | |
770 | if (status) | |
771 | return status; | |
772 | ||
773 | status = i2c_add_driver(&f75375_legacy_driver); | |
774 | if (status) | |
775 | i2c_del_driver(&f75375_driver); | |
776 | ||
777 | return status; | |
84f1e442 RV |
778 | } |
779 | ||
780 | static void __exit sensors_f75375_exit(void) | |
781 | { | |
620c142d | 782 | i2c_del_driver(&f75375_legacy_driver); |
84f1e442 RV |
783 | i2c_del_driver(&f75375_driver); |
784 | } | |
785 | ||
786 | MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>"); | |
787 | MODULE_LICENSE("GPL"); | |
788 | MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver"); | |
789 | ||
790 | module_init(sensors_f75375_init); | |
791 | module_exit(sensors_f75375_exit); |