1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. FCU fan control
5 * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/i2c.h>
18 #include <asm/machdep.h>
20 #include <asm/sections.h>
23 #include "windfarm_mpu.h"
28 #define DBG(args...) printk(args)
30 #define DBG(args...) do { } while(0)
34 * This option is "weird" :) Basically, if you define this to 1
35 * the control loop for the RPMs fans (not PWMs) will apply the
36 * correction factor obtained from the PID to the actual RPM
37 * speed read from the FCU.
39 * If you define the below constant to 0, then it will be
40 * applied to the setpoint RPM speed, that is basically the
41 * speed we proviously "asked" for.
43 * I'm using 0 for now which is what therm_pm72 used to do and
44 * what Darwin -apparently- does based on observed behaviour.
46 #define RPM_PID_USE_ACTUAL_SPEED 0
48 /* Default min/max for pumps */
49 #define CPU_PUMP_OUTPUT_MAX 3200
50 #define CPU_PUMP_OUTPUT_MIN 1250
57 struct i2c_client
*i2c
;
59 struct list_head fan_list
;
64 struct list_head link
;
67 struct wf_fcu_priv
*fcu_priv
;
68 struct wf_control ctrl
;
71 static void wf_fcu_release(struct kref
*ref
)
73 struct wf_fcu_priv
*pv
= container_of(ref
, struct wf_fcu_priv
, ref
);
78 static void wf_fcu_fan_release(struct wf_control
*ct
)
80 struct wf_fcu_fan
*fan
= ct
->priv
;
82 kref_put(&fan
->fcu_priv
->ref
, wf_fcu_release
);
86 static int wf_fcu_read_reg(struct wf_fcu_priv
*pv
, int reg
,
87 unsigned char *buf
, int nb
)
91 mutex_lock(&pv
->lock
);
96 nw
= i2c_master_send(pv
->i2c
, buf
, 1);
97 if (nw
> 0 || (nw
< 0 && nw
!= -EIO
) || tries
>= 100)
103 pr_err("Failure writing address to FCU: %d", nw
);
109 nr
= i2c_master_recv(pv
->i2c
, buf
, nb
);
110 if (nr
> 0 || (nr
< 0 && nr
!= -ENODEV
) || tries
>= 100)
116 pr_err("wf_fcu: Failure reading data from FCU: %d", nw
);
118 mutex_unlock(&pv
->lock
);
122 static int wf_fcu_write_reg(struct wf_fcu_priv
*pv
, int reg
,
123 const unsigned char *ptr
, int nb
)
126 unsigned char buf
[16];
129 memcpy(buf
+1, ptr
, nb
);
133 nw
= i2c_master_send(pv
->i2c
, buf
, nb
);
134 if (nw
> 0 || (nw
< 0 && nw
!= -EIO
) || tries
>= 100)
140 pr_err("wf_fcu: Failure writing to FCU: %d", nw
);
144 static int wf_fcu_fan_set_rpm(struct wf_control
*ct
, s32 value
)
146 struct wf_fcu_fan
*fan
= ct
->priv
;
147 struct wf_fcu_priv
*pv
= fan
->fcu_priv
;
148 int rc
, shift
= pv
->rpm_shift
;
149 unsigned char buf
[2];
151 if (value
< fan
->min
)
153 if (value
> fan
->max
)
158 buf
[0] = value
>> (8 - shift
);
159 buf
[1] = value
<< shift
;
160 rc
= wf_fcu_write_reg(pv
, 0x10 + (fan
->id
* 2), buf
, 2);
166 static int wf_fcu_fan_get_rpm(struct wf_control
*ct
, s32
*value
)
168 struct wf_fcu_fan
*fan
= ct
->priv
;
169 struct wf_fcu_priv
*pv
= fan
->fcu_priv
;
170 int rc
, reg_base
, shift
= pv
->rpm_shift
;
171 unsigned char failure
;
172 unsigned char active
;
173 unsigned char buf
[2];
175 rc
= wf_fcu_read_reg(pv
, 0xb, &failure
, 1);
178 if ((failure
& (1 << fan
->id
)) != 0)
180 rc
= wf_fcu_read_reg(pv
, 0xd, &active
, 1);
183 if ((active
& (1 << fan
->id
)) == 0)
186 /* Programmed value or real current speed */
187 #if RPM_PID_USE_ACTUAL_SPEED
192 rc
= wf_fcu_read_reg(pv
, reg_base
+ (fan
->id
* 2), buf
, 2);
196 *value
= (buf
[0] << (8 - shift
)) | buf
[1] >> shift
;
201 static int wf_fcu_fan_set_pwm(struct wf_control
*ct
, s32 value
)
203 struct wf_fcu_fan
*fan
= ct
->priv
;
204 struct wf_fcu_priv
*pv
= fan
->fcu_priv
;
205 unsigned char buf
[2];
208 if (value
< fan
->min
)
210 if (value
> fan
->max
)
215 value
= (value
* 2559) / 1000;
217 rc
= wf_fcu_write_reg(pv
, 0x30 + (fan
->id
* 2), buf
, 1);
223 static int wf_fcu_fan_get_pwm(struct wf_control
*ct
, s32
*value
)
225 struct wf_fcu_fan
*fan
= ct
->priv
;
226 struct wf_fcu_priv
*pv
= fan
->fcu_priv
;
227 unsigned char failure
;
228 unsigned char active
;
229 unsigned char buf
[2];
232 rc
= wf_fcu_read_reg(pv
, 0x2b, &failure
, 1);
235 if ((failure
& (1 << fan
->id
)) != 0)
237 rc
= wf_fcu_read_reg(pv
, 0x2d, &active
, 1);
240 if ((active
& (1 << fan
->id
)) == 0)
243 rc
= wf_fcu_read_reg(pv
, 0x30 + (fan
->id
* 2), buf
, 1);
247 *value
= (((s32
)buf
[0]) * 1000) / 2559;
252 static s32
wf_fcu_fan_min(struct wf_control
*ct
)
254 struct wf_fcu_fan
*fan
= ct
->priv
;
259 static s32
wf_fcu_fan_max(struct wf_control
*ct
)
261 struct wf_fcu_fan
*fan
= ct
->priv
;
266 static const struct wf_control_ops wf_fcu_fan_rpm_ops
= {
267 .set_value
= wf_fcu_fan_set_rpm
,
268 .get_value
= wf_fcu_fan_get_rpm
,
269 .get_min
= wf_fcu_fan_min
,
270 .get_max
= wf_fcu_fan_max
,
271 .release
= wf_fcu_fan_release
,
272 .owner
= THIS_MODULE
,
275 static const struct wf_control_ops wf_fcu_fan_pwm_ops
= {
276 .set_value
= wf_fcu_fan_set_pwm
,
277 .get_value
= wf_fcu_fan_get_pwm
,
278 .get_min
= wf_fcu_fan_min
,
279 .get_max
= wf_fcu_fan_max
,
280 .release
= wf_fcu_fan_release
,
281 .owner
= THIS_MODULE
,
284 static void wf_fcu_get_pump_minmax(struct wf_fcu_fan
*fan
)
286 const struct mpu_data
*mpu
= wf_get_mpu(0);
287 u16 pump_min
= 0, pump_max
= 0xffff;
290 /* Try to fetch pumps min/max infos from eeprom */
292 memcpy(&tmp
, mpu
->processor_part_num
, 8);
293 if (tmp
[0] != 0xffff && tmp
[1] != 0xffff) {
294 pump_min
= max(pump_min
, tmp
[0]);
295 pump_max
= min(pump_max
, tmp
[1]);
297 if (tmp
[2] != 0xffff && tmp
[3] != 0xffff) {
298 pump_min
= max(pump_min
, tmp
[2]);
299 pump_max
= min(pump_max
, tmp
[3]);
303 /* Double check the values, this _IS_ needed as the EEPROM on
304 * some dual 2.5Ghz G5s seem, at least, to have both min & max
305 * same to the same value ... (grrrr)
307 if (pump_min
== pump_max
|| pump_min
== 0 || pump_max
== 0xffff) {
308 pump_min
= CPU_PUMP_OUTPUT_MIN
;
309 pump_max
= CPU_PUMP_OUTPUT_MAX
;
315 DBG("wf_fcu: pump min/max for %s set to: [%d..%d] RPM\n",
316 fan
->ctrl
.name
, pump_min
, pump_max
);
319 static void wf_fcu_get_rpmfan_minmax(struct wf_fcu_fan
*fan
)
321 struct wf_fcu_priv
*pv
= fan
->fcu_priv
;
322 const struct mpu_data
*mpu0
= wf_get_mpu(0);
323 const struct mpu_data
*mpu1
= wf_get_mpu(1);
326 fan
->min
= 2400 >> pv
->rpm_shift
;
327 fan
->max
= 56000 >> pv
->rpm_shift
;
329 /* CPU fans have min/max in MPU */
330 if (mpu0
&& !strcmp(fan
->ctrl
.name
, "cpu-front-fan-0")) {
331 fan
->min
= max(fan
->min
, (s32
)mpu0
->rminn_intake_fan
);
332 fan
->max
= min(fan
->max
, (s32
)mpu0
->rmaxn_intake_fan
);
335 if (mpu1
&& !strcmp(fan
->ctrl
.name
, "cpu-front-fan-1")) {
336 fan
->min
= max(fan
->min
, (s32
)mpu1
->rminn_intake_fan
);
337 fan
->max
= min(fan
->max
, (s32
)mpu1
->rmaxn_intake_fan
);
340 if (mpu0
&& !strcmp(fan
->ctrl
.name
, "cpu-rear-fan-0")) {
341 fan
->min
= max(fan
->min
, (s32
)mpu0
->rminn_exhaust_fan
);
342 fan
->max
= min(fan
->max
, (s32
)mpu0
->rmaxn_exhaust_fan
);
345 if (mpu1
&& !strcmp(fan
->ctrl
.name
, "cpu-rear-fan-1")) {
346 fan
->min
= max(fan
->min
, (s32
)mpu1
->rminn_exhaust_fan
);
347 fan
->max
= min(fan
->max
, (s32
)mpu1
->rmaxn_exhaust_fan
);
350 /* Rackmac variants, we just use mpu0 intake */
351 if (!strncmp(fan
->ctrl
.name
, "cpu-fan", 7)) {
352 fan
->min
= max(fan
->min
, (s32
)mpu0
->rminn_intake_fan
);
353 fan
->max
= min(fan
->max
, (s32
)mpu0
->rmaxn_intake_fan
);
357 DBG("wf_fcu: fan min/max for %s set to: [%d..%d] RPM\n",
358 fan
->ctrl
.name
, fan
->min
, fan
->max
);
361 static void wf_fcu_add_fan(struct wf_fcu_priv
*pv
, const char *name
,
364 struct wf_fcu_fan
*fan
;
366 fan
= kzalloc(sizeof(*fan
), GFP_KERNEL
);
371 fan
->ctrl
.name
= name
;
372 fan
->ctrl
.priv
= fan
;
374 /* min/max is oddball but the code comes from
375 * therm_pm72 which seems to work so ...
377 if (type
== FCU_FAN_RPM
) {
378 if (!strncmp(name
, "cpu-pump", strlen("cpu-pump")))
379 wf_fcu_get_pump_minmax(fan
);
381 wf_fcu_get_rpmfan_minmax(fan
);
382 fan
->ctrl
.type
= WF_CONTROL_RPM_FAN
;
383 fan
->ctrl
.ops
= &wf_fcu_fan_rpm_ops
;
387 fan
->ctrl
.type
= WF_CONTROL_PWM_FAN
;
388 fan
->ctrl
.ops
= &wf_fcu_fan_pwm_ops
;
391 if (wf_register_control(&fan
->ctrl
)) {
392 pr_err("wf_fcu: Failed to register fan %s\n", name
);
396 list_add(&fan
->link
, &pv
->fan_list
);
400 static void wf_fcu_lookup_fans(struct wf_fcu_priv
*pv
)
402 /* Translation of device-tree location properties to
405 static const struct {
406 const char *dt_name
; /* Device-tree name */
407 const char *ct_name
; /* Control name */
409 { "BACKSIDE", "backside-fan", },
410 { "SYS CTRLR FAN", "backside-fan", },
411 { "DRIVE BAY", "drive-bay-fan", },
412 { "SLOT", "slots-fan", },
413 { "PCI FAN", "slots-fan", },
414 { "CPU A INTAKE", "cpu-front-fan-0", },
415 { "CPU A EXHAUST", "cpu-rear-fan-0", },
416 { "CPU B INTAKE", "cpu-front-fan-1", },
417 { "CPU B EXHAUST", "cpu-rear-fan-1", },
418 { "CPU A PUMP", "cpu-pump-0", },
419 { "CPU B PUMP", "cpu-pump-1", },
420 { "CPU A 1", "cpu-fan-a-0", },
421 { "CPU A 2", "cpu-fan-b-0", },
422 { "CPU A 3", "cpu-fan-c-0", },
423 { "CPU B 1", "cpu-fan-a-1", },
424 { "CPU B 2", "cpu-fan-b-1", },
425 { "CPU B 3", "cpu-fan-c-1", },
427 struct device_node
*np
, *fcu
= pv
->i2c
->dev
.of_node
;
430 DBG("Looking up FCU controls in device-tree...\n");
432 for_each_child_of_node(fcu
, np
) {
438 DBG(" control: %pOFn, type: %s\n", np
, of_node_get_device_type(np
));
440 /* Detect control type */
441 if (of_node_is_type(np
, "fan-rpm-control") ||
442 of_node_is_type(np
, "fan-rpm"))
444 if (of_node_is_type(np
, "fan-pwm-control") ||
445 of_node_is_type(np
, "fan-pwm"))
447 /* Only care about fans for now */
451 /* Lookup for a matching location */
452 loc
= of_get_property(np
, "location", NULL
);
453 reg
= of_get_property(np
, "reg", NULL
);
454 if (loc
== NULL
|| reg
== NULL
)
456 DBG(" matching location: %s, reg: 0x%08x\n", loc
, *reg
);
458 for (i
= 0; i
< ARRAY_SIZE(loc_trans
); i
++) {
459 if (strncmp(loc
, loc_trans
[i
].dt_name
,
460 strlen(loc_trans
[i
].dt_name
)))
462 name
= loc_trans
[i
].ct_name
;
464 DBG(" location match, name: %s\n", name
);
466 if (type
== FCU_FAN_RPM
)
467 id
= ((*reg
) - 0x10) / 2;
469 id
= ((*reg
) - 0x30) / 2;
471 pr_warn("wf_fcu: Can't parse fan ID in device-tree for %pOF\n", np
);
474 wf_fcu_add_fan(pv
, name
, type
, id
);
480 static void wf_fcu_default_fans(struct wf_fcu_priv
*pv
)
482 /* We only support the default fans for PowerMac7,2 */
483 if (!of_machine_is_compatible("PowerMac7,2"))
486 wf_fcu_add_fan(pv
, "backside-fan", FCU_FAN_PWM
, 1);
487 wf_fcu_add_fan(pv
, "drive-bay-fan", FCU_FAN_RPM
, 2);
488 wf_fcu_add_fan(pv
, "slots-fan", FCU_FAN_PWM
, 2);
489 wf_fcu_add_fan(pv
, "cpu-front-fan-0", FCU_FAN_RPM
, 3);
490 wf_fcu_add_fan(pv
, "cpu-rear-fan-0", FCU_FAN_RPM
, 4);
491 wf_fcu_add_fan(pv
, "cpu-front-fan-1", FCU_FAN_RPM
, 5);
492 wf_fcu_add_fan(pv
, "cpu-rear-fan-1", FCU_FAN_RPM
, 6);
495 static int wf_fcu_init_chip(struct wf_fcu_priv
*pv
)
497 unsigned char buf
= 0xff;
500 rc
= wf_fcu_write_reg(pv
, 0xe, &buf
, 1);
503 rc
= wf_fcu_write_reg(pv
, 0x2e, &buf
, 1);
506 rc
= wf_fcu_read_reg(pv
, 0, &buf
, 1);
509 pv
->rpm_shift
= (buf
== 1) ? 2 : 3;
511 pr_debug("wf_fcu: FCU Initialized, RPM fan shift is %d\n",
517 static int wf_fcu_probe(struct i2c_client
*client
,
518 const struct i2c_device_id
*id
)
520 struct wf_fcu_priv
*pv
;
522 pv
= kzalloc(sizeof(*pv
), GFP_KERNEL
);
527 mutex_init(&pv
->lock
);
528 INIT_LIST_HEAD(&pv
->fan_list
);
532 * First we must start the FCU which will query the
533 * shift value to apply to RPMs
535 if (wf_fcu_init_chip(pv
)) {
536 pr_err("wf_fcu: Initialization failed !\n");
541 /* First lookup fans in the device-tree */
542 wf_fcu_lookup_fans(pv
);
545 * Older machines don't have the device-tree entries
546 * we are looking for, just hard code the list
548 if (list_empty(&pv
->fan_list
))
549 wf_fcu_default_fans(pv
);
551 /* Still no fans ? FAIL */
552 if (list_empty(&pv
->fan_list
)) {
553 pr_err("wf_fcu: Failed to find fans for your machine\n");
558 dev_set_drvdata(&client
->dev
, pv
);
563 static int wf_fcu_remove(struct i2c_client
*client
)
565 struct wf_fcu_priv
*pv
= dev_get_drvdata(&client
->dev
);
566 struct wf_fcu_fan
*fan
;
568 while (!list_empty(&pv
->fan_list
)) {
569 fan
= list_first_entry(&pv
->fan_list
, struct wf_fcu_fan
, link
);
570 list_del(&fan
->link
);
571 wf_unregister_control(&fan
->ctrl
);
573 kref_put(&pv
->ref
, wf_fcu_release
);
577 static const struct i2c_device_id wf_fcu_id
[] = {
581 MODULE_DEVICE_TABLE(i2c
, wf_fcu_id
);
583 static const struct of_device_id wf_fcu_of_id
[] = {
584 { .compatible
= "fcu", },
587 MODULE_DEVICE_TABLE(of
, wf_fcu_of_id
);
589 static struct i2c_driver wf_fcu_driver
= {
592 .of_match_table
= wf_fcu_of_id
,
594 .probe
= wf_fcu_probe
,
595 .remove
= wf_fcu_remove
,
596 .id_table
= wf_fcu_id
,
599 module_i2c_driver(wf_fcu_driver
);
601 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
602 MODULE_DESCRIPTION("FCU control objects for PowerMacs thermal control");
603 MODULE_LICENSE("GPL");