Path: blob/master/drivers/macintosh/windfarm_smu_controls.c
15112 views
/*1* Windfarm PowerMac thermal control. SMU based controls2*3* (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.4* <[email protected]>5*6* Released under the term of the GNU GPL v2.7*/89#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/completion.h>17#include <asm/prom.h>18#include <asm/machdep.h>19#include <asm/io.h>20#include <asm/system.h>21#include <asm/sections.h>22#include <asm/smu.h>2324#include "windfarm.h"2526#define VERSION "0.4"2728#undef DEBUG2930#ifdef DEBUG31#define DBG(args...) printk(args)32#else33#define DBG(args...) do { } while(0)34#endif3536static int smu_supports_new_fans_ops = 1;3738/*39* SMU fans control object40*/4142static LIST_HEAD(smu_fans);4344struct smu_fan_control {45struct list_head link;46int fan_type; /* 0 = rpm, 1 = pwm */47u32 reg; /* index in SMU */48s32 value; /* current value */49s32 min, max; /* min/max values */50struct wf_control ctrl;51};52#define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)5354static int smu_set_fan(int pwm, u8 id, u16 value)55{56struct smu_cmd cmd;57u8 buffer[16];58DECLARE_COMPLETION_ONSTACK(comp);59int rc;6061/* Fill SMU command structure */62cmd.cmd = SMU_CMD_FAN_COMMAND;6364/* The SMU has an "old" and a "new" way of setting the fan speed65* Unfortunately, I found no reliable way to know which one works66* on a given machine model. After some investigations it appears67* that MacOS X just tries the new one, and if it fails fallbacks68* to the old ones ... Ugh.69*/70retry:71if (smu_supports_new_fans_ops) {72buffer[0] = 0x30;73buffer[1] = id;74*((u16 *)(&buffer[2])) = value;75cmd.data_len = 4;76} else {77if (id > 7)78return -EINVAL;79/* Fill argument buffer */80memset(buffer, 0, 16);81buffer[0] = pwm ? 0x10 : 0x00;82buffer[1] = 0x01 << id;83*((u16 *)&buffer[2 + id * 2]) = value;84cmd.data_len = 14;85}8687cmd.reply_len = 16;88cmd.data_buf = cmd.reply_buf = buffer;89cmd.status = 0;90cmd.done = smu_done_complete;91cmd.misc = ∁9293rc = smu_queue_cmd(&cmd);94if (rc)95return rc;96wait_for_completion(&comp);9798/* Handle fallback (see coment above) */99if (cmd.status != 0 && smu_supports_new_fans_ops) {100printk(KERN_WARNING "windfarm: SMU failed new fan command "101"falling back to old method\n");102smu_supports_new_fans_ops = 0;103goto retry;104}105106return cmd.status;107}108109static void smu_fan_release(struct wf_control *ct)110{111struct smu_fan_control *fct = to_smu_fan(ct);112113kfree(fct);114}115116static int smu_fan_set(struct wf_control *ct, s32 value)117{118struct smu_fan_control *fct = to_smu_fan(ct);119120if (value < fct->min)121value = fct->min;122if (value > fct->max)123value = fct->max;124fct->value = value;125126return smu_set_fan(fct->fan_type, fct->reg, value);127}128129static int smu_fan_get(struct wf_control *ct, s32 *value)130{131struct smu_fan_control *fct = to_smu_fan(ct);132*value = fct->value; /* todo: read from SMU */133return 0;134}135136static s32 smu_fan_min(struct wf_control *ct)137{138struct smu_fan_control *fct = to_smu_fan(ct);139return fct->min;140}141142static s32 smu_fan_max(struct wf_control *ct)143{144struct smu_fan_control *fct = to_smu_fan(ct);145return fct->max;146}147148static struct wf_control_ops smu_fan_ops = {149.set_value = smu_fan_set,150.get_value = smu_fan_get,151.get_min = smu_fan_min,152.get_max = smu_fan_max,153.release = smu_fan_release,154.owner = THIS_MODULE,155};156157static struct smu_fan_control *smu_fan_create(struct device_node *node,158int pwm_fan)159{160struct smu_fan_control *fct;161const s32 *v;162const u32 *reg;163const char *l;164165fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);166if (fct == NULL)167return NULL;168fct->ctrl.ops = &smu_fan_ops;169l = of_get_property(node, "location", NULL);170if (l == NULL)171goto fail;172173fct->fan_type = pwm_fan;174fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;175sysfs_attr_init(&fct->ctrl.attr.attr);176177/* We use the name & location here the same way we do for SMU sensors,178* see the comment in windfarm_smu_sensors.c. The locations are a bit179* less consistent here between the iMac and the desktop models, but180* that is good enough for our needs for now at least.181*182* One problem though is that Apple seem to be inconsistent with case183* and the kernel doesn't have strcasecmp =P184*/185186fct->ctrl.name = NULL;187188/* Names used on desktop models */189if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||190!strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") ||191!strcmp(l, "CPU A EXHAUST"))192fct->ctrl.name = "cpu-rear-fan-0";193else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") ||194!strcmp(l, "CPU B EXHAUST"))195fct->ctrl.name = "cpu-rear-fan-1";196else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||197!strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") ||198!strcmp(l, "CPU A INTAKE"))199fct->ctrl.name = "cpu-front-fan-0";200else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") ||201!strcmp(l, "CPU B INTAKE"))202fct->ctrl.name = "cpu-front-fan-1";203else if (!strcmp(l, "CPU A PUMP"))204fct->ctrl.name = "cpu-pump-0";205else if (!strcmp(l, "CPU B PUMP"))206fct->ctrl.name = "cpu-pump-1";207else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") ||208!strcmp(l, "EXPANSION SLOTS INTAKE"))209fct->ctrl.name = "slots-fan";210else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") ||211!strcmp(l, "DRIVE BAY A INTAKE"))212fct->ctrl.name = "drive-bay-fan";213else if (!strcmp(l, "BACKSIDE"))214fct->ctrl.name = "backside-fan";215216/* Names used on iMac models */217if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))218fct->ctrl.name = "system-fan";219else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))220fct->ctrl.name = "cpu-fan";221else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))222fct->ctrl.name = "drive-bay-fan";223else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */224fct->ctrl.name = "hard-drive-fan";225else if (!strcmp(l, "ODD Fan")) /* same */226fct->ctrl.name = "optical-drive-fan";227228/* Unrecognized fan, bail out */229if (fct->ctrl.name == NULL)230goto fail;231232/* Get min & max values*/233v = of_get_property(node, "min-value", NULL);234if (v == NULL)235goto fail;236fct->min = *v;237v = of_get_property(node, "max-value", NULL);238if (v == NULL)239goto fail;240fct->max = *v;241242/* Get "reg" value */243reg = of_get_property(node, "reg", NULL);244if (reg == NULL)245goto fail;246fct->reg = *reg;247248if (wf_register_control(&fct->ctrl))249goto fail;250251return fct;252fail:253kfree(fct);254return NULL;255}256257258static int __init smu_controls_init(void)259{260struct device_node *smu, *fans, *fan;261262if (!smu_present())263return -ENODEV;264265smu = of_find_node_by_type(NULL, "smu");266if (smu == NULL)267return -ENODEV;268269/* Look for RPM fans */270for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)271if (!strcmp(fans->name, "rpm-fans") ||272of_device_is_compatible(fans, "smu-rpm-fans"))273break;274for (fan = NULL;275fans && (fan = of_get_next_child(fans, fan)) != NULL;) {276struct smu_fan_control *fct;277278fct = smu_fan_create(fan, 0);279if (fct == NULL) {280printk(KERN_WARNING "windfarm: Failed to create SMU "281"RPM fan %s\n", fan->name);282continue;283}284list_add(&fct->link, &smu_fans);285}286of_node_put(fans);287288289/* Look for PWM fans */290for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)291if (!strcmp(fans->name, "pwm-fans"))292break;293for (fan = NULL;294fans && (fan = of_get_next_child(fans, fan)) != NULL;) {295struct smu_fan_control *fct;296297fct = smu_fan_create(fan, 1);298if (fct == NULL) {299printk(KERN_WARNING "windfarm: Failed to create SMU "300"PWM fan %s\n", fan->name);301continue;302}303list_add(&fct->link, &smu_fans);304}305of_node_put(fans);306of_node_put(smu);307308return 0;309}310311static void __exit smu_controls_exit(void)312{313struct smu_fan_control *fct;314315while (!list_empty(&smu_fans)) {316fct = list_entry(smu_fans.next, struct smu_fan_control, link);317list_del(&fct->link);318wf_unregister_control(&fct->ctrl);319}320}321322323module_init(smu_controls_init);324module_exit(smu_controls_exit);325326MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");327MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");328MODULE_LICENSE("GPL");329330331332