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