diff options
author | Hans-Juergen Koch <hjk@linutronix.de> | 2007-05-08 11:22:00 -0400 |
---|---|---|
committer | Jean Delvare <khali@hyperion.delvare> | 2007-05-08 11:22:00 -0400 |
commit | d20620de0c3de622a9d6a841725bafaed6d1aec2 (patch) | |
tree | 5dbd1f0f04b44c0af6711b28a4fccc41b8bdaace /drivers/hwmon/max6650.c | |
parent | e84cfbcbe830c20af030fd7ba37edf8ed88fda5f (diff) |
hwmon: New max6650 driver
This driver supports the Maxim MAX6650 and MAX6651 fan speed
monitoring and control chips.
Signed-off-by: Hans J. Koch <hjk@linutronix.de>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/hwmon/max6650.c')
-rw-r--r-- | drivers/hwmon/max6650.c | 693 |
1 files changed, 693 insertions, 0 deletions
diff --git a/drivers/hwmon/max6650.c b/drivers/hwmon/max6650.c new file mode 100644 index 000000000000..8415664f33c2 --- /dev/null +++ b/drivers/hwmon/max6650.c | |||
@@ -0,0 +1,693 @@ | |||
1 | /* | ||
2 | * max6650.c - Part of lm_sensors, Linux kernel modules for hardware | ||
3 | * monitoring. | ||
4 | * | ||
5 | * (C) 2007 by Hans J. Koch <hjk@linutronix.de> | ||
6 | * | ||
7 | * based on code written by John Morris <john.morris@spirentcom.com> | ||
8 | * Copyright (c) 2003 Spirent Communications | ||
9 | * and Claus Gindhart <claus.gindhart@kontron.com> | ||
10 | * | ||
11 | * This module has only been tested with the MAX6650 chip. It should | ||
12 | * also work with the MAX6651. It does not distinguish max6650 and max6651 | ||
13 | * chips. | ||
14 | * | ||
15 | * Tha datasheet was last seen at: | ||
16 | * | ||
17 | * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf | ||
18 | * | ||
19 | * This program is free software; you can redistribute it and/or modify | ||
20 | * it under the terms of the GNU General Public License as published by | ||
21 | * the Free Software Foundation; either version 2 of the License, or | ||
22 | * (at your option) any later version. | ||
23 | * | ||
24 | * This program is distributed in the hope that it will be useful, | ||
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
27 | * GNU General Public License for more details. | ||
28 | * | ||
29 | * You should have received a copy of the GNU General Public License | ||
30 | * along with this program; if not, write to the Free Software | ||
31 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
32 | */ | ||
33 | |||
34 | #include <linux/module.h> | ||
35 | #include <linux/init.h> | ||
36 | #include <linux/slab.h> | ||
37 | #include <linux/jiffies.h> | ||
38 | #include <linux/i2c.h> | ||
39 | #include <linux/hwmon.h> | ||
40 | #include <linux/hwmon-sysfs.h> | ||
41 | #include <linux/err.h> | ||
42 | |||
43 | /* | ||
44 | * Addresses to scan. There are four disjoint possibilities, by pin config. | ||
45 | */ | ||
46 | |||
47 | static unsigned short normal_i2c[] = {0x1b, 0x1f, 0x48, 0x4b, I2C_CLIENT_END}; | ||
48 | |||
49 | /* | ||
50 | * Insmod parameters | ||
51 | */ | ||
52 | |||
53 | /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */ | ||
54 | static int fan_voltage; | ||
55 | /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */ | ||
56 | static int prescaler; | ||
57 | /* clock: The clock frequency of the chip the driver should assume */ | ||
58 | static int clock = 254000; | ||
59 | |||
60 | module_param(fan_voltage, int, S_IRUGO); | ||
61 | module_param(prescaler, int, S_IRUGO); | ||
62 | module_param(clock, int, S_IRUGO); | ||
63 | |||
64 | I2C_CLIENT_INSMOD_1(max6650); | ||
65 | |||
66 | /* | ||
67 | * MAX 6650/6651 registers | ||
68 | */ | ||
69 | |||
70 | #define MAX6650_REG_SPEED 0x00 | ||
71 | #define MAX6650_REG_CONFIG 0x02 | ||
72 | #define MAX6650_REG_GPIO_DEF 0x04 | ||
73 | #define MAX6650_REG_DAC 0x06 | ||
74 | #define MAX6650_REG_ALARM_EN 0x08 | ||
75 | #define MAX6650_REG_ALARM 0x0A | ||
76 | #define MAX6650_REG_TACH0 0x0C | ||
77 | #define MAX6650_REG_TACH1 0x0E | ||
78 | #define MAX6650_REG_TACH2 0x10 | ||
79 | #define MAX6650_REG_TACH3 0x12 | ||
80 | #define MAX6650_REG_GPIO_STAT 0x14 | ||
81 | #define MAX6650_REG_COUNT 0x16 | ||
82 | |||
83 | /* | ||
84 | * Config register bits | ||
85 | */ | ||
86 | |||
87 | #define MAX6650_CFG_V12 0x08 | ||
88 | #define MAX6650_CFG_PRESCALER_MASK 0x07 | ||
89 | #define MAX6650_CFG_PRESCALER_2 0x01 | ||
90 | #define MAX6650_CFG_PRESCALER_4 0x02 | ||
91 | #define MAX6650_CFG_PRESCALER_8 0x03 | ||
92 | #define MAX6650_CFG_PRESCALER_16 0x04 | ||
93 | #define MAX6650_CFG_MODE_MASK 0x30 | ||
94 | #define MAX6650_CFG_MODE_ON 0x00 | ||
95 | #define MAX6650_CFG_MODE_OFF 0x10 | ||
96 | #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20 | ||
97 | #define MAX6650_CFG_MODE_OPEN_LOOP 0x30 | ||
98 | #define MAX6650_COUNT_MASK 0x03 | ||
99 | |||
100 | /* Minimum and maximum values of the FAN-RPM */ | ||
101 | #define FAN_RPM_MIN 240 | ||
102 | #define FAN_RPM_MAX 30000 | ||
103 | |||
104 | #define DIV_FROM_REG(reg) (1 << (reg & 7)) | ||
105 | |||
106 | static int max6650_attach_adapter(struct i2c_adapter *adapter); | ||
107 | static int max6650_detect(struct i2c_adapter *adapter, int address, int kind); | ||
108 | static int max6650_init_client(struct i2c_client *client); | ||
109 | static int max6650_detach_client(struct i2c_client *client); | ||
110 | static struct max6650_data *max6650_update_device(struct device *dev); | ||
111 | |||
112 | /* | ||
113 | * Driver data (common to all clients) | ||
114 | */ | ||
115 | |||
116 | static struct i2c_driver max6650_driver = { | ||
117 | .driver = { | ||
118 | .name = "max6650", | ||
119 | }, | ||
120 | .attach_adapter = max6650_attach_adapter, | ||
121 | .detach_client = max6650_detach_client, | ||
122 | }; | ||
123 | |||
124 | /* | ||
125 | * Client data (each client gets its own) | ||
126 | */ | ||
127 | |||
128 | struct max6650_data | ||
129 | { | ||
130 | struct i2c_client client; | ||
131 | struct class_device *class_dev; | ||
132 | struct mutex update_lock; | ||
133 | char valid; /* zero until following fields are valid */ | ||
134 | unsigned long last_updated; /* in jiffies */ | ||
135 | |||
136 | /* register values */ | ||
137 | u8 speed; | ||
138 | u8 config; | ||
139 | u8 tach[4]; | ||
140 | u8 count; | ||
141 | u8 dac; | ||
142 | }; | ||
143 | |||
144 | static ssize_t get_fan(struct device *dev, struct device_attribute *devattr, | ||
145 | char *buf) | ||
146 | { | ||
147 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | ||
148 | struct max6650_data *data = max6650_update_device(dev); | ||
149 | int rpm; | ||
150 | |||
151 | /* | ||
152 | * Calculation details: | ||
153 | * | ||
154 | * Each tachometer counts over an interval given by the "count" | ||
155 | * register (0.25, 0.5, 1 or 2 seconds). This module assumes | ||
156 | * that the fans produce two pulses per revolution (this seems | ||
157 | * to be the most common). | ||
158 | */ | ||
159 | |||
160 | rpm = ((data->tach[attr->index] * 120) / DIV_FROM_REG(data->count)); | ||
161 | return sprintf(buf, "%d\n", rpm); | ||
162 | } | ||
163 | |||
164 | /* | ||
165 | * Set the fan speed to the specified RPM (or read back the RPM setting). | ||
166 | * This works in closed loop mode only. Use pwm1 for open loop speed setting. | ||
167 | * | ||
168 | * The MAX6650/1 will automatically control fan speed when in closed loop | ||
169 | * mode. | ||
170 | * | ||
171 | * Assumptions: | ||
172 | * | ||
173 | * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use | ||
174 | * the clock module parameter if you need to fine tune this. | ||
175 | * | ||
176 | * 2) The prescaler (low three bits of the config register) has already | ||
177 | * been set to an appropriate value. Use the prescaler module parameter | ||
178 | * if your BIOS doesn't initialize the chip properly. | ||
179 | * | ||
180 | * The relevant equations are given on pages 21 and 22 of the datasheet. | ||
181 | * | ||
182 | * From the datasheet, the relevant equation when in regulation is: | ||
183 | * | ||
184 | * [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE | ||
185 | * | ||
186 | * where: | ||
187 | * | ||
188 | * fCLK is the oscillator frequency (either the 254kHz internal | ||
189 | * oscillator or the externally applied clock) | ||
190 | * | ||
191 | * KTACH is the value in the speed register | ||
192 | * | ||
193 | * FanSpeed is the speed of the fan in rps | ||
194 | * | ||
195 | * KSCALE is the prescaler value (1, 2, 4, 8, or 16) | ||
196 | * | ||
197 | * When reading, we need to solve for FanSpeed. When writing, we need to | ||
198 | * solve for KTACH. | ||
199 | * | ||
200 | * Note: this tachometer is completely separate from the tachometers | ||
201 | * used to measure the fan speeds. Only one fan's speed (fan1) is | ||
202 | * controlled. | ||
203 | */ | ||
204 | |||
205 | static ssize_t get_target(struct device *dev, struct device_attribute *devattr, | ||
206 | char *buf) | ||
207 | { | ||
208 | struct max6650_data *data = max6650_update_device(dev); | ||
209 | int kscale, ktach, rpm; | ||
210 | |||
211 | /* | ||
212 | * Use the datasheet equation: | ||
213 | * | ||
214 | * FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)] | ||
215 | * | ||
216 | * then multiply by 60 to give rpm. | ||
217 | */ | ||
218 | |||
219 | kscale = DIV_FROM_REG(data->config); | ||
220 | ktach = data->speed; | ||
221 | rpm = 60 * kscale * clock / (256 * (ktach + 1)); | ||
222 | return sprintf(buf, "%d\n", rpm); | ||
223 | } | ||
224 | |||
225 | static ssize_t set_target(struct device *dev, struct device_attribute *devattr, | ||
226 | const char *buf, size_t count) | ||
227 | { | ||
228 | struct i2c_client *client = to_i2c_client(dev); | ||
229 | struct max6650_data *data = i2c_get_clientdata(client); | ||
230 | int rpm = simple_strtoul(buf, NULL, 10); | ||
231 | int kscale, ktach; | ||
232 | |||
233 | rpm = SENSORS_LIMIT(rpm, FAN_RPM_MIN, FAN_RPM_MAX); | ||
234 | |||
235 | /* | ||
236 | * Divide the required speed by 60 to get from rpm to rps, then | ||
237 | * use the datasheet equation: | ||
238 | * | ||
239 | * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1 | ||
240 | */ | ||
241 | |||
242 | mutex_lock(&data->update_lock); | ||
243 | |||
244 | kscale = DIV_FROM_REG(data->config); | ||
245 | ktach = ((clock * kscale) / (256 * rpm / 60)) - 1; | ||
246 | if (ktach < 0) | ||
247 | ktach = 0; | ||
248 | if (ktach > 255) | ||
249 | ktach = 255; | ||
250 | data->speed = ktach; | ||
251 | |||
252 | i2c_smbus_write_byte_data(client, MAX6650_REG_SPEED, data->speed); | ||
253 | |||
254 | mutex_unlock(&data->update_lock); | ||
255 | |||
256 | return count; | ||
257 | } | ||
258 | |||
259 | /* | ||
260 | * Get/set the fan speed in open loop mode using pwm1 sysfs file. | ||
261 | * Speed is given as a relative value from 0 to 255, where 255 is maximum | ||
262 | * speed. Note that this is done by writing directly to the chip's DAC, | ||
263 | * it won't change the closed loop speed set by fan1_target. | ||
264 | * Also note that due to rounding errors it is possible that you don't read | ||
265 | * back exactly the value you have set. | ||
266 | */ | ||
267 | |||
268 | static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr, | ||
269 | char *buf) | ||
270 | { | ||
271 | int pwm; | ||
272 | struct max6650_data *data = max6650_update_device(dev); | ||
273 | |||
274 | /* Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans. | ||
275 | Lower DAC values mean higher speeds. */ | ||
276 | if (data->config & MAX6650_CFG_V12) | ||
277 | pwm = 255 - (255 * (int)data->dac)/180; | ||
278 | else | ||
279 | pwm = 255 - (255 * (int)data->dac)/76; | ||
280 | |||
281 | if (pwm < 0) | ||
282 | pwm = 0; | ||
283 | |||
284 | return sprintf(buf, "%d\n", pwm); | ||
285 | } | ||
286 | |||
287 | static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, | ||
288 | const char *buf, size_t count) | ||
289 | { | ||
290 | struct i2c_client *client = to_i2c_client(dev); | ||
291 | struct max6650_data *data = i2c_get_clientdata(client); | ||
292 | int pwm = simple_strtoul(buf, NULL, 10); | ||
293 | |||
294 | pwm = SENSORS_LIMIT(pwm, 0, 255); | ||
295 | |||
296 | mutex_lock(&data->update_lock); | ||
297 | |||
298 | if (data->config & MAX6650_CFG_V12) | ||
299 | data->dac = 180 - (180 * pwm)/255; | ||
300 | else | ||
301 | data->dac = 76 - (76 * pwm)/255; | ||
302 | |||
303 | i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac); | ||
304 | |||
305 | mutex_unlock(&data->update_lock); | ||
306 | |||
307 | return count; | ||
308 | } | ||
309 | |||
310 | /* | ||
311 | * Get/Set controller mode: | ||
312 | * Possible values: | ||
313 | * 0 = Fan always on | ||
314 | * 1 = Open loop, Voltage is set according to speed, not regulated. | ||
315 | * 2 = Closed loop, RPM for all fans regulated by fan1 tachometer | ||
316 | */ | ||
317 | |||
318 | static ssize_t get_enable(struct device *dev, struct device_attribute *devattr, | ||
319 | char *buf) | ||
320 | { | ||
321 | struct max6650_data *data = max6650_update_device(dev); | ||
322 | int mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4; | ||
323 | int sysfs_modes[4] = {0, 1, 2, 1}; | ||
324 | |||
325 | return sprintf(buf, "%d\n", sysfs_modes[mode]); | ||
326 | } | ||
327 | |||
328 | static ssize_t set_enable(struct device *dev, struct device_attribute *devattr, | ||
329 | const char *buf, size_t count) | ||
330 | { | ||
331 | struct i2c_client *client = to_i2c_client(dev); | ||
332 | struct max6650_data *data = i2c_get_clientdata(client); | ||
333 | int mode = simple_strtoul(buf, NULL, 10); | ||
334 | int max6650_modes[3] = {0, 3, 2}; | ||
335 | |||
336 | if ((mode < 0)||(mode > 2)) { | ||
337 | dev_err(&client->dev, | ||
338 | "illegal value for pwm1_enable (%d)\n", mode); | ||
339 | return -EINVAL; | ||
340 | } | ||
341 | |||
342 | mutex_lock(&data->update_lock); | ||
343 | |||
344 | data->config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG); | ||
345 | data->config = (data->config & ~MAX6650_CFG_MODE_MASK) | ||
346 | | (max6650_modes[mode] << 4); | ||
347 | |||
348 | i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, data->config); | ||
349 | |||
350 | mutex_unlock(&data->update_lock); | ||
351 | |||
352 | return count; | ||
353 | } | ||
354 | |||
355 | /* | ||
356 | * Read/write functions for fan1_div sysfs file. The MAX6650 has no such | ||
357 | * divider. We handle this by converting between divider and counttime: | ||
358 | * | ||
359 | * (counttime == k) <==> (divider == 2^k), k = 0, 1, 2, or 3 | ||
360 | * | ||
361 | * Lower values of k allow to connect a faster fan without the risk of | ||
362 | * counter overflow. The price is lower resolution. You can also set counttime | ||
363 | * using the module parameter. Note that the module parameter "prescaler" also | ||
364 | * influences the behaviour. Unfortunately, there's no sysfs attribute | ||
365 | * defined for that. See the data sheet for details. | ||
366 | */ | ||
367 | |||
368 | static ssize_t get_div(struct device *dev, struct device_attribute *devattr, | ||
369 | char *buf) | ||
370 | { | ||
371 | struct max6650_data *data = max6650_update_device(dev); | ||
372 | |||
373 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->count)); | ||
374 | } | ||
375 | |||
376 | static ssize_t set_div(struct device *dev, struct device_attribute *devattr, | ||
377 | const char *buf, size_t count) | ||
378 | { | ||
379 | struct i2c_client *client = to_i2c_client(dev); | ||
380 | struct max6650_data *data = i2c_get_clientdata(client); | ||
381 | int div = simple_strtoul(buf, NULL, 10); | ||
382 | |||
383 | mutex_lock(&data->update_lock); | ||
384 | switch (div) { | ||
385 | case 1: | ||
386 | data->count = 0; | ||
387 | break; | ||
388 | case 2: | ||
389 | data->count = 1; | ||
390 | break; | ||
391 | case 4: | ||
392 | data->count = 2; | ||
393 | break; | ||
394 | case 8: | ||
395 | data->count = 3; | ||
396 | break; | ||
397 | default: | ||
398 | dev_err(&client->dev, | ||
399 | "illegal value for fan divider (%d)\n", div); | ||
400 | return -EINVAL; | ||
401 | } | ||
402 | |||
403 | i2c_smbus_write_byte_data(client, MAX6650_REG_COUNT, data->count); | ||
404 | mutex_unlock(&data->update_lock); | ||
405 | |||
406 | return count; | ||
407 | } | ||
408 | |||
409 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0); | ||
410 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1); | ||
411 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2); | ||
412 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3); | ||
413 | static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, get_target, set_target); | ||
414 | static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_div, set_div); | ||
415 | static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, get_enable, set_enable); | ||
416 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm); | ||
417 | |||
418 | |||
419 | static struct attribute *max6650_attrs[] = { | ||
420 | &sensor_dev_attr_fan1_input.dev_attr.attr, | ||
421 | &sensor_dev_attr_fan2_input.dev_attr.attr, | ||
422 | &sensor_dev_attr_fan3_input.dev_attr.attr, | ||
423 | &sensor_dev_attr_fan4_input.dev_attr.attr, | ||
424 | &dev_attr_fan1_target.attr, | ||
425 | &dev_attr_fan1_div.attr, | ||
426 | &dev_attr_pwm1_enable.attr, | ||
427 | &dev_attr_pwm1.attr, | ||
428 | NULL | ||
429 | }; | ||
430 | |||
431 | static struct attribute_group max6650_attr_grp = { | ||
432 | .attrs = max6650_attrs, | ||
433 | }; | ||
434 | |||
435 | /* | ||
436 | * Real code | ||
437 | */ | ||
438 | |||
439 | static int max6650_attach_adapter(struct i2c_adapter *adapter) | ||
440 | { | ||
441 | if (!(adapter->class & I2C_CLASS_HWMON)) { | ||
442 | dev_dbg(&adapter->dev, | ||
443 | "FATAL: max6650_attach_adapter class HWMON not set\n"); | ||
444 | return 0; | ||
445 | } | ||
446 | |||
447 | return i2c_probe(adapter, &addr_data, max6650_detect); | ||
448 | } | ||
449 | |||
450 | /* | ||
451 | * The following function does more than just detection. If detection | ||
452 | * succeeds, it also registers the new chip. | ||
453 | */ | ||
454 | |||
455 | static int max6650_detect(struct i2c_adapter *adapter, int address, int kind) | ||
456 | { | ||
457 | struct i2c_client *client; | ||
458 | struct max6650_data *data; | ||
459 | int err = -ENODEV; | ||
460 | |||
461 | dev_dbg(&adapter->dev, "max6650_detect called, kind = %d\n", kind); | ||
462 | |||
463 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | ||
464 | dev_dbg(&adapter->dev, "max6650: I2C bus doesn't support " | ||
465 | "byte read mode, skipping.\n"); | ||
466 | return 0; | ||
467 | } | ||
468 | |||
469 | if (!(data = kzalloc(sizeof(struct max6650_data), GFP_KERNEL))) { | ||
470 | dev_err(&adapter->dev, "max6650: out of memory.\n"); | ||
471 | return -ENOMEM; | ||
472 | } | ||
473 | |||
474 | client = &data->client; | ||
475 | i2c_set_clientdata(client, data); | ||
476 | client->addr = address; | ||
477 | client->adapter = adapter; | ||
478 | client->driver = &max6650_driver; | ||
479 | |||
480 | /* | ||
481 | * Now we do the remaining detection. A negative kind means that | ||
482 | * the driver was loaded with no force parameter (default), so we | ||
483 | * must both detect and identify the chip (actually there is only | ||
484 | * one possible kind of chip for now, max6650). A zero kind means that | ||
485 | * the driver was loaded with the force parameter, the detection | ||
486 | * step shall be skipped. A positive kind means that the driver | ||
487 | * was loaded with the force parameter and a given kind of chip is | ||
488 | * requested, so both the detection and the identification steps | ||
489 | * are skipped. | ||
490 | * | ||
491 | * Currently I can find no way to distinguish between a MAX6650 and | ||
492 | * a MAX6651. This driver has only been tried on the former. | ||
493 | */ | ||
494 | |||
495 | if ((kind < 0) && | ||
496 | ( (i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG) & 0xC0) | ||
497 | ||(i2c_smbus_read_byte_data(client, MAX6650_REG_GPIO_STAT) & 0xE0) | ||
498 | ||(i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN) & 0xE0) | ||
499 | ||(i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM) & 0xE0) | ||
500 | ||(i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT) & 0xFC))) { | ||
501 | dev_dbg(&adapter->dev, | ||
502 | "max6650: detection failed at 0x%02x.\n", address); | ||
503 | goto err_free; | ||
504 | } | ||
505 | |||
506 | dev_info(&adapter->dev, "max6650: chip found at 0x%02x.\n", address); | ||
507 | |||
508 | strlcpy(client->name, "max6650", I2C_NAME_SIZE); | ||
509 | mutex_init(&data->update_lock); | ||
510 | |||
511 | if ((err = i2c_attach_client(client))) { | ||
512 | dev_err(&adapter->dev, "max6650: failed to attach client.\n"); | ||
513 | goto err_free; | ||
514 | } | ||
515 | |||
516 | /* | ||
517 | * Initialize the max6650 chip | ||
518 | */ | ||
519 | if (max6650_init_client(client)) | ||
520 | goto err_detach; | ||
521 | |||
522 | err = sysfs_create_group(&client->dev.kobj, &max6650_attr_grp); | ||
523 | if (err) | ||
524 | goto err_detach; | ||
525 | |||
526 | data->class_dev = hwmon_device_register(&client->dev); | ||
527 | if (!IS_ERR(data->class_dev)) | ||
528 | return 0; | ||
529 | |||
530 | err = PTR_ERR(data->class_dev); | ||
531 | dev_err(&client->dev, "error registering hwmon device.\n"); | ||
532 | sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp); | ||
533 | err_detach: | ||
534 | i2c_detach_client(client); | ||
535 | err_free: | ||
536 | kfree(data); | ||
537 | return err; | ||
538 | } | ||
539 | |||
540 | static int max6650_detach_client(struct i2c_client *client) | ||
541 | { | ||
542 | struct max6650_data *data = i2c_get_clientdata(client); | ||
543 | int err; | ||
544 | |||
545 | sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp); | ||
546 | hwmon_device_unregister(data->class_dev); | ||
547 | err = i2c_detach_client(client); | ||
548 | if (!err) | ||
549 | kfree(data); | ||
550 | return err; | ||
551 | } | ||
552 | |||
553 | static int max6650_init_client(struct i2c_client *client) | ||
554 | { | ||
555 | struct max6650_data *data = i2c_get_clientdata(client); | ||
556 | int config; | ||
557 | int err = -EIO; | ||
558 | |||
559 | config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG); | ||
560 | |||
561 | if (config < 0) { | ||
562 | dev_err(&client->dev, "Error reading config, aborting.\n"); | ||
563 | return err; | ||
564 | } | ||
565 | |||
566 | switch (fan_voltage) { | ||
567 | case 0: | ||
568 | break; | ||
569 | case 5: | ||
570 | config &= ~MAX6650_CFG_V12; | ||
571 | break; | ||
572 | case 12: | ||
573 | config |= MAX6650_CFG_V12; | ||
574 | break; | ||
575 | default: | ||
576 | dev_err(&client->dev, | ||
577 | "illegal value for fan_voltage (%d)\n", | ||
578 | fan_voltage); | ||
579 | } | ||
580 | |||
581 | dev_info(&client->dev, "Fan voltage is set to %dV.\n", | ||
582 | (config & MAX6650_CFG_V12) ? 12 : 5); | ||
583 | |||
584 | switch (prescaler) { | ||
585 | case 0: | ||
586 | break; | ||
587 | case 1: | ||
588 | config &= ~MAX6650_CFG_PRESCALER_MASK; | ||
589 | break; | ||
590 | case 2: | ||
591 | config = (config & ~MAX6650_CFG_PRESCALER_MASK) | ||
592 | | MAX6650_CFG_PRESCALER_2; | ||
593 | break; | ||
594 | case 4: | ||
595 | config = (config & ~MAX6650_CFG_PRESCALER_MASK) | ||
596 | | MAX6650_CFG_PRESCALER_4; | ||
597 | break; | ||
598 | case 8: | ||
599 | config = (config & ~MAX6650_CFG_PRESCALER_MASK) | ||
600 | | MAX6650_CFG_PRESCALER_8; | ||
601 | break; | ||
602 | case 16: | ||
603 | config = (config & ~MAX6650_CFG_PRESCALER_MASK) | ||
604 | | MAX6650_CFG_PRESCALER_16; | ||
605 | break; | ||
606 | default: | ||
607 | dev_err(&client->dev, | ||
608 | "illegal value for prescaler (%d)\n", | ||
609 | prescaler); | ||
610 | } | ||
611 | |||
612 | dev_info(&client->dev, "Prescaler is set to %d.\n", | ||
613 | 1 << (config & MAX6650_CFG_PRESCALER_MASK)); | ||
614 | |||
615 | /* If mode is set to "full off", we change it to "open loop" and | ||
616 | * set DAC to 255, which has the same effect. We do this because | ||
617 | * there's no "full off" mode defined in hwmon specifcations. | ||
618 | */ | ||
619 | |||
620 | if ((config & MAX6650_CFG_MODE_MASK) == MAX6650_CFG_MODE_OFF) { | ||
621 | dev_dbg(&client->dev, "Change mode to open loop, full off.\n"); | ||
622 | config = (config & ~MAX6650_CFG_MODE_MASK) | ||
623 | | MAX6650_CFG_MODE_OPEN_LOOP; | ||
624 | if (i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, 255)) { | ||
625 | dev_err(&client->dev, "DAC write error, aborting.\n"); | ||
626 | return err; | ||
627 | } | ||
628 | } | ||
629 | |||
630 | if (i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, config)) { | ||
631 | dev_err(&client->dev, "Config write error, aborting.\n"); | ||
632 | return err; | ||
633 | } | ||
634 | |||
635 | data->config = config; | ||
636 | data->count = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT); | ||
637 | |||
638 | return 0; | ||
639 | } | ||
640 | |||
641 | static const u8 tach_reg[] = { | ||
642 | MAX6650_REG_TACH0, | ||
643 | MAX6650_REG_TACH1, | ||
644 | MAX6650_REG_TACH2, | ||
645 | MAX6650_REG_TACH3, | ||
646 | }; | ||
647 | |||
648 | static struct max6650_data *max6650_update_device(struct device *dev) | ||
649 | { | ||
650 | int i; | ||
651 | struct i2c_client *client = to_i2c_client(dev); | ||
652 | struct max6650_data *data = i2c_get_clientdata(client); | ||
653 | |||
654 | mutex_lock(&data->update_lock); | ||
655 | |||
656 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | ||
657 | data->speed = i2c_smbus_read_byte_data(client, | ||
658 | MAX6650_REG_SPEED); | ||
659 | data->config = i2c_smbus_read_byte_data(client, | ||
660 | MAX6650_REG_CONFIG); | ||
661 | for (i = 0; i < 4; i++) { | ||
662 | data->tach[i] = i2c_smbus_read_byte_data(client, | ||
663 | tach_reg[i]); | ||
664 | } | ||
665 | data->count = i2c_smbus_read_byte_data(client, | ||
666 | MAX6650_REG_COUNT); | ||
667 | data->dac = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC); | ||
668 | |||
669 | data->last_updated = jiffies; | ||
670 | data->valid = 1; | ||
671 | } | ||
672 | |||
673 | mutex_unlock(&data->update_lock); | ||
674 | |||
675 | return data; | ||
676 | } | ||
677 | |||
678 | static int __init sensors_max6650_init(void) | ||
679 | { | ||
680 | return i2c_add_driver(&max6650_driver); | ||
681 | } | ||
682 | |||
683 | static void __exit sensors_max6650_exit(void) | ||
684 | { | ||
685 | i2c_del_driver(&max6650_driver); | ||
686 | } | ||
687 | |||
688 | MODULE_AUTHOR("Hans J. Koch"); | ||
689 | MODULE_DESCRIPTION("MAX6650 sensor driver"); | ||
690 | MODULE_LICENSE("GPL"); | ||
691 | |||
692 | module_init(sensors_max6650_init); | ||
693 | module_exit(sensors_max6650_exit); | ||