Lines Matching +full:fan +full:- +full:0
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. FCU fan control
30 #define DBG(args...) do { } while(0)
39 * If you define the below constant to 0, then it will be
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
52 #define FCU_FAN_RPM 0
80 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_release() local
82 kref_put(&fan->fcu_priv->ref, wf_fcu_release); in wf_fcu_fan_release()
83 kfree(fan); in wf_fcu_fan_release()
91 mutex_lock(&pv->lock); in wf_fcu_read_reg()
93 buf[0] = reg; in wf_fcu_read_reg()
94 tries = 0; in wf_fcu_read_reg()
96 nw = i2c_master_send(pv->i2c, buf, 1); in wf_fcu_read_reg()
97 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100) in wf_fcu_read_reg()
102 if (nw <= 0) { in wf_fcu_read_reg()
107 tries = 0; in wf_fcu_read_reg()
109 nr = i2c_master_recv(pv->i2c, buf, nb); in wf_fcu_read_reg()
110 if (nr > 0 || (nr < 0 && nr != -ENODEV) || tries >= 100) in wf_fcu_read_reg()
115 if (nr <= 0) in wf_fcu_read_reg()
118 mutex_unlock(&pv->lock); in wf_fcu_read_reg()
128 buf[0] = reg; in wf_fcu_write_reg()
131 tries = 0; in wf_fcu_write_reg()
133 nw = i2c_master_send(pv->i2c, buf, nb); in wf_fcu_write_reg()
134 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100) in wf_fcu_write_reg()
139 if (nw < 0) in wf_fcu_write_reg()
146 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_set_rpm() local
147 struct wf_fcu_priv *pv = fan->fcu_priv; in wf_fcu_fan_set_rpm()
148 int rc, shift = pv->rpm_shift; in wf_fcu_fan_set_rpm()
151 if (value < fan->min) in wf_fcu_fan_set_rpm()
152 value = fan->min; in wf_fcu_fan_set_rpm()
153 if (value > fan->max) in wf_fcu_fan_set_rpm()
154 value = fan->max; in wf_fcu_fan_set_rpm()
156 fan->target = value; in wf_fcu_fan_set_rpm()
158 buf[0] = value >> (8 - shift); in wf_fcu_fan_set_rpm()
160 rc = wf_fcu_write_reg(pv, 0x10 + (fan->id * 2), buf, 2); in wf_fcu_fan_set_rpm()
161 if (rc < 0) in wf_fcu_fan_set_rpm()
162 return -EIO; in wf_fcu_fan_set_rpm()
163 return 0; in wf_fcu_fan_set_rpm()
168 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_get_rpm() local
169 struct wf_fcu_priv *pv = fan->fcu_priv; in wf_fcu_fan_get_rpm()
170 int rc, reg_base, shift = pv->rpm_shift; in wf_fcu_fan_get_rpm()
175 rc = wf_fcu_read_reg(pv, 0xb, &failure, 1); in wf_fcu_fan_get_rpm()
177 return -EIO; in wf_fcu_fan_get_rpm()
178 if ((failure & (1 << fan->id)) != 0) in wf_fcu_fan_get_rpm()
179 return -EFAULT; in wf_fcu_fan_get_rpm()
180 rc = wf_fcu_read_reg(pv, 0xd, &active, 1); in wf_fcu_fan_get_rpm()
182 return -EIO; in wf_fcu_fan_get_rpm()
183 if ((active & (1 << fan->id)) == 0) in wf_fcu_fan_get_rpm()
184 return -ENXIO; in wf_fcu_fan_get_rpm()
188 reg_base = 0x11; in wf_fcu_fan_get_rpm()
190 reg_base = 0x10; in wf_fcu_fan_get_rpm()
192 rc = wf_fcu_read_reg(pv, reg_base + (fan->id * 2), buf, 2); in wf_fcu_fan_get_rpm()
194 return -EIO; in wf_fcu_fan_get_rpm()
196 *value = (buf[0] << (8 - shift)) | buf[1] >> shift; in wf_fcu_fan_get_rpm()
198 return 0; in wf_fcu_fan_get_rpm()
203 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_set_pwm() local
204 struct wf_fcu_priv *pv = fan->fcu_priv; in wf_fcu_fan_set_pwm()
208 if (value < fan->min) in wf_fcu_fan_set_pwm()
209 value = fan->min; in wf_fcu_fan_set_pwm()
210 if (value > fan->max) in wf_fcu_fan_set_pwm()
211 value = fan->max; in wf_fcu_fan_set_pwm()
213 fan->target = value; in wf_fcu_fan_set_pwm()
216 buf[0] = value; in wf_fcu_fan_set_pwm()
217 rc = wf_fcu_write_reg(pv, 0x30 + (fan->id * 2), buf, 1); in wf_fcu_fan_set_pwm()
218 if (rc < 0) in wf_fcu_fan_set_pwm()
219 return -EIO; in wf_fcu_fan_set_pwm()
220 return 0; in wf_fcu_fan_set_pwm()
225 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_get_pwm() local
226 struct wf_fcu_priv *pv = fan->fcu_priv; in wf_fcu_fan_get_pwm()
232 rc = wf_fcu_read_reg(pv, 0x2b, &failure, 1); in wf_fcu_fan_get_pwm()
234 return -EIO; in wf_fcu_fan_get_pwm()
235 if ((failure & (1 << fan->id)) != 0) in wf_fcu_fan_get_pwm()
236 return -EFAULT; in wf_fcu_fan_get_pwm()
237 rc = wf_fcu_read_reg(pv, 0x2d, &active, 1); in wf_fcu_fan_get_pwm()
239 return -EIO; in wf_fcu_fan_get_pwm()
240 if ((active & (1 << fan->id)) == 0) in wf_fcu_fan_get_pwm()
241 return -ENXIO; in wf_fcu_fan_get_pwm()
243 rc = wf_fcu_read_reg(pv, 0x30 + (fan->id * 2), buf, 1); in wf_fcu_fan_get_pwm()
245 return -EIO; in wf_fcu_fan_get_pwm()
247 *value = (((s32)buf[0]) * 1000) / 2559; in wf_fcu_fan_get_pwm()
249 return 0; in wf_fcu_fan_get_pwm()
254 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_min() local
256 return fan->min; in wf_fcu_fan_min()
261 struct wf_fcu_fan *fan = ct->priv; in wf_fcu_fan_max() local
263 return fan->max; in wf_fcu_fan_max()
284 static void wf_fcu_get_pump_minmax(struct wf_fcu_fan *fan) in wf_fcu_get_pump_minmax() argument
286 const struct mpu_data *mpu = wf_get_mpu(0); in wf_fcu_get_pump_minmax()
287 u16 pump_min = 0, pump_max = 0xffff; in wf_fcu_get_pump_minmax()
292 memcpy(&tmp, mpu->processor_part_num, 8); in wf_fcu_get_pump_minmax()
293 if (tmp[0] != 0xffff && tmp[1] != 0xffff) { in wf_fcu_get_pump_minmax()
294 pump_min = max(pump_min, tmp[0]); in wf_fcu_get_pump_minmax()
297 if (tmp[2] != 0xffff && tmp[3] != 0xffff) { in wf_fcu_get_pump_minmax()
307 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) { in wf_fcu_get_pump_minmax()
312 fan->min = pump_min; in wf_fcu_get_pump_minmax()
313 fan->max = pump_max; in wf_fcu_get_pump_minmax()
316 fan->ctrl.name, pump_min, pump_max); in wf_fcu_get_pump_minmax()
319 static void wf_fcu_get_rpmfan_minmax(struct wf_fcu_fan *fan) in wf_fcu_get_rpmfan_minmax() argument
321 struct wf_fcu_priv *pv = fan->fcu_priv; in wf_fcu_get_rpmfan_minmax()
322 const struct mpu_data *mpu0 = wf_get_mpu(0); in wf_fcu_get_rpmfan_minmax()
326 fan->min = 2400 >> pv->rpm_shift; in wf_fcu_get_rpmfan_minmax()
327 fan->max = 56000 >> pv->rpm_shift; in wf_fcu_get_rpmfan_minmax()
330 if (mpu0 && !strcmp(fan->ctrl.name, "cpu-front-fan-0")) { in wf_fcu_get_rpmfan_minmax()
331 fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan); in wf_fcu_get_rpmfan_minmax()
332 fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan); in wf_fcu_get_rpmfan_minmax()
335 if (mpu1 && !strcmp(fan->ctrl.name, "cpu-front-fan-1")) { in wf_fcu_get_rpmfan_minmax()
336 fan->min = max(fan->min, (s32)mpu1->rminn_intake_fan); in wf_fcu_get_rpmfan_minmax()
337 fan->max = min(fan->max, (s32)mpu1->rmaxn_intake_fan); in wf_fcu_get_rpmfan_minmax()
340 if (mpu0 && !strcmp(fan->ctrl.name, "cpu-rear-fan-0")) { in wf_fcu_get_rpmfan_minmax()
341 fan->min = max(fan->min, (s32)mpu0->rminn_exhaust_fan); in wf_fcu_get_rpmfan_minmax()
342 fan->max = min(fan->max, (s32)mpu0->rmaxn_exhaust_fan); in wf_fcu_get_rpmfan_minmax()
345 if (mpu1 && !strcmp(fan->ctrl.name, "cpu-rear-fan-1")) { in wf_fcu_get_rpmfan_minmax()
346 fan->min = max(fan->min, (s32)mpu1->rminn_exhaust_fan); in wf_fcu_get_rpmfan_minmax()
347 fan->max = min(fan->max, (s32)mpu1->rmaxn_exhaust_fan); in wf_fcu_get_rpmfan_minmax()
351 if (!strncmp(fan->ctrl.name, "cpu-fan", 7)) { in wf_fcu_get_rpmfan_minmax()
352 fan->min = max(fan->min, (s32)mpu0->rminn_intake_fan); in wf_fcu_get_rpmfan_minmax()
353 fan->max = min(fan->max, (s32)mpu0->rmaxn_intake_fan); in wf_fcu_get_rpmfan_minmax()
357 DBG("wf_fcu: fan min/max for %s set to: [%d..%d] RPM\n", in wf_fcu_get_rpmfan_minmax()
358 fan->ctrl.name, fan->min, fan->max); in wf_fcu_get_rpmfan_minmax()
364 struct wf_fcu_fan *fan; in wf_fcu_add_fan() local
366 fan = kzalloc(sizeof(*fan), GFP_KERNEL); in wf_fcu_add_fan()
367 if (!fan) in wf_fcu_add_fan()
369 fan->fcu_priv = pv; in wf_fcu_add_fan()
370 fan->id = id; in wf_fcu_add_fan()
371 fan->ctrl.name = name; in wf_fcu_add_fan()
372 fan->ctrl.priv = fan; in wf_fcu_add_fan()
378 if (!strncmp(name, "cpu-pump", strlen("cpu-pump"))) in wf_fcu_add_fan()
379 wf_fcu_get_pump_minmax(fan); in wf_fcu_add_fan()
381 wf_fcu_get_rpmfan_minmax(fan); in wf_fcu_add_fan()
382 fan->ctrl.type = WF_CONTROL_RPM_FAN; in wf_fcu_add_fan()
383 fan->ctrl.ops = &wf_fcu_fan_rpm_ops; in wf_fcu_add_fan()
385 fan->min = 10; in wf_fcu_add_fan()
386 fan->max = 100; in wf_fcu_add_fan()
387 fan->ctrl.type = WF_CONTROL_PWM_FAN; in wf_fcu_add_fan()
388 fan->ctrl.ops = &wf_fcu_fan_pwm_ops; in wf_fcu_add_fan()
391 if (wf_register_control(&fan->ctrl)) { in wf_fcu_add_fan()
392 pr_err("wf_fcu: Failed to register fan %s\n", name); in wf_fcu_add_fan()
393 kfree(fan); in wf_fcu_add_fan()
396 list_add(&fan->link, &pv->fan_list); in wf_fcu_add_fan()
397 kref_get(&pv->ref); in wf_fcu_add_fan()
402 /* Translation of device-tree location properties to in wf_fcu_lookup_fans()
403 * windfarm fan names in wf_fcu_lookup_fans()
406 const char *dt_name; /* Device-tree name */ in wf_fcu_lookup_fans()
409 { "BACKSIDE", "backside-fan", }, in wf_fcu_lookup_fans()
410 { "SYS CTRLR FAN", "backside-fan", }, in wf_fcu_lookup_fans()
411 { "DRIVE BAY", "drive-bay-fan", }, in wf_fcu_lookup_fans()
412 { "SLOT", "slots-fan", }, in wf_fcu_lookup_fans()
413 { "PCI FAN", "slots-fan", }, in wf_fcu_lookup_fans()
414 { "CPU A INTAKE", "cpu-front-fan-0", }, in wf_fcu_lookup_fans()
415 { "CPU A EXHAUST", "cpu-rear-fan-0", }, in wf_fcu_lookup_fans()
416 { "CPU B INTAKE", "cpu-front-fan-1", }, in wf_fcu_lookup_fans()
417 { "CPU B EXHAUST", "cpu-rear-fan-1", }, in wf_fcu_lookup_fans()
418 { "CPU A PUMP", "cpu-pump-0", }, in wf_fcu_lookup_fans()
419 { "CPU B PUMP", "cpu-pump-1", }, in wf_fcu_lookup_fans()
420 { "CPU A 1", "cpu-fan-a-0", }, in wf_fcu_lookup_fans()
421 { "CPU A 2", "cpu-fan-b-0", }, in wf_fcu_lookup_fans()
422 { "CPU A 3", "cpu-fan-c-0", }, in wf_fcu_lookup_fans()
423 { "CPU B 1", "cpu-fan-a-1", }, in wf_fcu_lookup_fans()
424 { "CPU B 2", "cpu-fan-b-1", }, in wf_fcu_lookup_fans()
425 { "CPU B 3", "cpu-fan-c-1", }, in wf_fcu_lookup_fans()
427 struct device_node *np, *fcu = pv->i2c->dev.of_node; in wf_fcu_lookup_fans()
430 DBG("Looking up FCU controls in device-tree...\n"); in wf_fcu_lookup_fans()
433 int id, type = -1; in wf_fcu_lookup_fans()
441 if (of_node_is_type(np, "fan-rpm-control") || in wf_fcu_lookup_fans()
442 of_node_is_type(np, "fan-rpm")) in wf_fcu_lookup_fans()
444 if (of_node_is_type(np, "fan-pwm-control") || in wf_fcu_lookup_fans()
445 of_node_is_type(np, "fan-pwm")) in wf_fcu_lookup_fans()
448 if (type == -1) in wf_fcu_lookup_fans()
456 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg); in wf_fcu_lookup_fans()
458 for (i = 0; i < ARRAY_SIZE(loc_trans); i++) { in wf_fcu_lookup_fans()
467 id = ((*reg) - 0x10) / 2; in wf_fcu_lookup_fans()
469 id = ((*reg) - 0x30) / 2; in wf_fcu_lookup_fans()
471 pr_warn("wf_fcu: Can't parse fan ID in device-tree for %pOF\n", np); in wf_fcu_lookup_fans()
486 wf_fcu_add_fan(pv, "backside-fan", FCU_FAN_PWM, 1); in wf_fcu_default_fans()
487 wf_fcu_add_fan(pv, "drive-bay-fan", FCU_FAN_RPM, 2); in wf_fcu_default_fans()
488 wf_fcu_add_fan(pv, "slots-fan", FCU_FAN_PWM, 2); in wf_fcu_default_fans()
489 wf_fcu_add_fan(pv, "cpu-front-fan-0", FCU_FAN_RPM, 3); in wf_fcu_default_fans()
490 wf_fcu_add_fan(pv, "cpu-rear-fan-0", FCU_FAN_RPM, 4); in wf_fcu_default_fans()
491 wf_fcu_add_fan(pv, "cpu-front-fan-1", FCU_FAN_RPM, 5); in wf_fcu_default_fans()
492 wf_fcu_add_fan(pv, "cpu-rear-fan-1", FCU_FAN_RPM, 6); in wf_fcu_default_fans()
497 unsigned char buf = 0xff; in wf_fcu_init_chip()
500 rc = wf_fcu_write_reg(pv, 0xe, &buf, 1); in wf_fcu_init_chip()
501 if (rc < 0) in wf_fcu_init_chip()
502 return -EIO; in wf_fcu_init_chip()
503 rc = wf_fcu_write_reg(pv, 0x2e, &buf, 1); in wf_fcu_init_chip()
504 if (rc < 0) in wf_fcu_init_chip()
505 return -EIO; in wf_fcu_init_chip()
506 rc = wf_fcu_read_reg(pv, 0, &buf, 1); in wf_fcu_init_chip()
507 if (rc < 0) in wf_fcu_init_chip()
508 return -EIO; in wf_fcu_init_chip()
509 pv->rpm_shift = (buf == 1) ? 2 : 3; in wf_fcu_init_chip()
511 pr_debug("wf_fcu: FCU Initialized, RPM fan shift is %d\n", in wf_fcu_init_chip()
512 pv->rpm_shift); in wf_fcu_init_chip()
514 return 0; in wf_fcu_init_chip()
523 return -ENOMEM; in wf_fcu_probe()
525 kref_init(&pv->ref); in wf_fcu_probe()
526 mutex_init(&pv->lock); in wf_fcu_probe()
527 INIT_LIST_HEAD(&pv->fan_list); in wf_fcu_probe()
528 pv->i2c = client; in wf_fcu_probe()
537 return -ENXIO; in wf_fcu_probe()
540 /* First lookup fans in the device-tree */ in wf_fcu_probe()
544 * Older machines don't have the device-tree entries in wf_fcu_probe()
547 if (list_empty(&pv->fan_list)) in wf_fcu_probe()
551 if (list_empty(&pv->fan_list)) { in wf_fcu_probe()
554 return -ENODEV; in wf_fcu_probe()
557 dev_set_drvdata(&client->dev, pv); in wf_fcu_probe()
559 return 0; in wf_fcu_probe()
564 struct wf_fcu_priv *pv = dev_get_drvdata(&client->dev); in wf_fcu_remove()
565 struct wf_fcu_fan *fan; in wf_fcu_remove() local
567 while (!list_empty(&pv->fan_list)) { in wf_fcu_remove()
568 fan = list_first_entry(&pv->fan_list, struct wf_fcu_fan, link); in wf_fcu_remove()
569 list_del(&fan->link); in wf_fcu_remove()
570 wf_unregister_control(&fan->ctrl); in wf_fcu_remove()
572 kref_put(&pv->ref, wf_fcu_release); in wf_fcu_remove()