10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
14 #include <linux/slab.h>
16 #include <linux/wait.h>
17 #include <linux/i2c.h>
19 #include <asm/machdep.h>
21 #include <asm/sections.h>
29 #define DBG(args...) printk(args)
31 #define DBG(args...) do { } while(0)
47 #define RPM_PID_USE_ACTUAL_SPEED 0
50 #define CPU_PUMP_OUTPUT_MAX 3200
51 #define CPU_PUMP_OUTPUT_MIN 1250
72 static void wf_fcu_release(
struct kref *
ref)
83 kref_put(&fan->
fcu_priv->ref, wf_fcu_release);
88 unsigned char *
buf,
int nb)
98 if (nw > 0 || (nw < 0 && nw != -
EIO) || tries >= 100)
104 pr_err(
"Failure writing address to FCU: %d", nw);
111 if (nr > 0 || (nr < 0 && nr != -
ENODEV) || tries >= 100)
117 pr_err(
"wf_fcu: Failure reading data from FCU: %d", nw);
123 static int wf_fcu_write_reg(
struct wf_fcu_priv *pv,
int reg,
124 const unsigned char *
ptr,
int nb)
127 unsigned char buf[16];
135 if (nw > 0 || (nw < 0 && nw != -
EIO) || tries >= 100)
141 pr_err(
"wf_fcu: Failure writing to FCU: %d", nw);
150 unsigned char buf[2];
152 if (value < fan->
min)
154 if (value > fan->
max)
159 buf[0] = value >> (8 - shift);
160 buf[1] = value << shift;
161 rc = wf_fcu_write_reg(pv, 0x10 + (fan->
id * 2), buf, 2);
167 static int wf_fcu_fan_get_rpm(
struct wf_control *ct,
s32 *value)
172 unsigned char failure;
174 unsigned char buf[2];
176 rc = wf_fcu_read_reg(pv, 0
xb, &failure, 1);
179 if ((failure & (1 << fan->
id)) != 0)
181 rc = wf_fcu_read_reg(pv, 0xd, &active, 1);
184 if ((active & (1 << fan->
id)) == 0)
188 #if RPM_PID_USE_ACTUAL_SPEED
193 rc = wf_fcu_read_reg(pv, reg_base + (fan->
id * 2), buf, 2);
197 *value = (buf[0] << (8 - shift)) | buf[1] >> shift;
202 static int wf_fcu_fan_set_pwm(
struct wf_control *ct,
s32 value)
206 unsigned char buf[2];
209 if (value < fan->min)
211 if (value > fan->
max)
216 value = (value * 2559) / 1000;
218 rc = wf_fcu_write_reg(pv, 0x30 + (fan->
id * 2), buf, 1);
224 static int wf_fcu_fan_get_pwm(
struct wf_control *ct,
s32 *value)
228 unsigned char failure;
230 unsigned char buf[2];
233 rc = wf_fcu_read_reg(pv, 0x2b, &failure, 1);
236 if ((failure & (1 << fan->
id)) != 0)
238 rc = wf_fcu_read_reg(pv, 0x2d, &active, 1);
241 if ((active & (1 << fan->
id)) == 0)
244 rc = wf_fcu_read_reg(pv, 0x30 + (fan->
id * 2), buf, 1);
248 *value = (((
s32)buf[0]) * 1000) / 2559;
268 .set_value = wf_fcu_fan_set_rpm,
269 .get_value = wf_fcu_fan_get_rpm,
270 .get_min = wf_fcu_fan_min,
271 .get_max = wf_fcu_fan_max,
272 .release = wf_fcu_fan_release,
277 .set_value = wf_fcu_fan_set_pwm,
278 .get_value = wf_fcu_fan_get_pwm,
279 .get_min = wf_fcu_fan_min,
280 .get_max = wf_fcu_fan_max,
281 .release = wf_fcu_fan_release,
287 const struct mpu_data *mpu = wf_get_mpu(0);
288 u16 pump_min = 0, pump_max = 0xffff;
294 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
295 pump_min =
max(pump_min, tmp[0]);
296 pump_max =
min(pump_max, tmp[1]);
298 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
299 pump_min =
max(pump_min, tmp[2]);
300 pump_max =
min(pump_max, tmp[3]);
308 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
316 DBG(
"wf_fcu: pump min/max for %s set to: [%d..%d] RPM\n",
317 fan->
ctrl.name, pump_min, pump_max);
323 const struct mpu_data *mpu0 = wf_get_mpu(0);
324 const struct mpu_data *mpu1 = wf_get_mpu(1);
331 if (mpu0 && !
strcmp(fan->
ctrl.name,
"cpu-front-fan-0")) {
336 if (mpu1 && !
strcmp(fan->
ctrl.name,
"cpu-front-fan-1")) {
341 if (mpu0 && !
strcmp(fan->
ctrl.name,
"cpu-rear-fan-0")) {
346 if (mpu1 && !
strcmp(fan->
ctrl.name,
"cpu-rear-fan-1")) {
358 DBG(
"wf_fcu: fan min/max for %s set to: [%d..%d] RPM\n",
381 wf_fcu_get_pump_minmax(fan);
383 wf_fcu_get_rpmfan_minmax(fan);
385 fan->
ctrl.ops = &wf_fcu_fan_rpm_ops;
390 fan->
ctrl.ops = &wf_fcu_fan_pwm_ops;
394 pr_err(
"wf_fcu: Failed to register fan %s\n", name);
407 static const struct {
411 {
"BACKSIDE",
"backside-fan", },
412 {
"SYS CTRLR FAN",
"backside-fan", },
413 {
"DRIVE BAY",
"drive-bay-fan", },
414 {
"SLOT",
"slots-fan", },
415 {
"PCI FAN",
"slots-fan", },
416 {
"CPU A INTAKE",
"cpu-front-fan-0", },
417 {
"CPU A EXHAUST",
"cpu-rear-fan-0", },
418 {
"CPU B INTAKE",
"cpu-front-fan-1", },
419 {
"CPU B EXHAUST",
"cpu-rear-fan-1", },
420 {
"CPU A PUMP",
"cpu-pump-0", },
421 {
"CPU B PUMP",
"cpu-pump-1", },
422 {
"CPU A 1",
"cpu-fan-a-0", },
423 {
"CPU A 2",
"cpu-fan-b-0", },
424 {
"CPU A 3",
"cpu-fan-c-0", },
425 {
"CPU B 1",
"cpu-fan-a-1", },
426 {
"CPU B 2",
"cpu-fan-b-1", },
427 {
"CPU B 3",
"cpu-fan-c-1", },
432 DBG(
"Looking up FCU controls in device-tree...\n");
440 DBG(
" control: %s, type: %s\n", np->
name, np->
type);
458 DBG(
" matching location: %s, reg: 0x%08x\n", loc, *reg);
461 if (
strncmp(loc, loc_trans[i].dt_name,
462 strlen(loc_trans[i].dt_name)))
464 name = loc_trans[
i].ct_name;
466 DBG(
" location match, name: %s\n", name);
469 id = ((*reg) - 0x10) / 2;
471 id = ((*reg) - 0x30) / 2;
474 "fan ID in device-tree for %s\n",
478 wf_fcu_add_fan(pv, name, type,
id);
490 wf_fcu_add_fan(pv,
"backside-fan",
FCU_FAN_PWM, 1);
491 wf_fcu_add_fan(pv,
"drive-bay-fan",
FCU_FAN_RPM, 2);
493 wf_fcu_add_fan(pv,
"cpu-front-fan-0",
FCU_FAN_RPM, 3);
494 wf_fcu_add_fan(pv,
"cpu-rear-fan-0",
FCU_FAN_RPM, 4);
495 wf_fcu_add_fan(pv,
"cpu-front-fan-1",
FCU_FAN_RPM, 5);
496 wf_fcu_add_fan(pv,
"cpu-rear-fan-1",
FCU_FAN_RPM, 6);
501 unsigned char buf = 0xff;
504 rc = wf_fcu_write_reg(pv, 0
xe, &buf, 1);
507 rc = wf_fcu_write_reg(pv, 0x2e, &buf, 1);
510 rc = wf_fcu_read_reg(pv, 0, &buf, 1);
515 pr_debug(
"wf_fcu: FCU Initialized, RPM fan shift is %d\n",
539 if (wf_fcu_init_chip(pv)) {
540 pr_err(
"wf_fcu: Initialization failed !\n");
546 wf_fcu_lookup_fans(pv);
553 wf_fcu_default_fans(pv);
557 pr_err(
"wf_fcu: Failed to find fans for your machine\n");
572 while (!list_empty(&pv->
fan_list)) {
577 kref_put(&pv->
ref, wf_fcu_release);
591 .probe = wf_fcu_probe,
592 .remove = wf_fcu_remove,
593 .id_table = wf_fcu_id,
596 static int __init wf_fcu_init(
void)
598 return i2c_add_driver(&wf_fcu_driver);
601 static void __exit wf_fcu_exit(
void)