Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Windfarm PowerMac thermal control. SMU based controls
0004  *
0005  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
0006  *                    <benh@kernel.crashing.org>
0007  */
0008 
0009 #include <linux/types.h>
0010 #include <linux/errno.h>
0011 #include <linux/kernel.h>
0012 #include <linux/delay.h>
0013 #include <linux/slab.h>
0014 #include <linux/init.h>
0015 #include <linux/wait.h>
0016 #include <linux/completion.h>
0017 #include <linux/of.h>
0018 
0019 #include <asm/machdep.h>
0020 #include <asm/io.h>
0021 #include <asm/sections.h>
0022 #include <asm/smu.h>
0023 
0024 #include "windfarm.h"
0025 
0026 #define VERSION "0.4"
0027 
0028 #undef DEBUG
0029 
0030 #ifdef DEBUG
0031 #define DBG(args...)    printk(args)
0032 #else
0033 #define DBG(args...)    do { } while(0)
0034 #endif
0035 
0036 static int smu_supports_new_fans_ops = 1;
0037 
0038 /*
0039  * SMU fans control object
0040  */
0041 
0042 static LIST_HEAD(smu_fans);
0043 
0044 struct smu_fan_control {
0045     struct list_head    link;
0046     int             fan_type;   /* 0 = rpm, 1 = pwm */
0047     u32         reg;        /* index in SMU */
0048     s32         value;      /* current value */
0049     s32         min, max;   /* min/max values */
0050     struct wf_control   ctrl;
0051 };
0052 #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl)
0053 
0054 static int smu_set_fan(int pwm, u8 id, u16 value)
0055 {
0056     struct smu_cmd cmd;
0057     u8 buffer[16];
0058     DECLARE_COMPLETION_ONSTACK(comp);
0059     int rc;
0060 
0061     /* Fill SMU command structure */
0062     cmd.cmd = SMU_CMD_FAN_COMMAND;
0063 
0064     /* The SMU has an "old" and a "new" way of setting the fan speed
0065      * Unfortunately, I found no reliable way to know which one works
0066      * on a given machine model. After some investigations it appears
0067      * that MacOS X just tries the new one, and if it fails fallbacks
0068      * to the old ones ... Ugh.
0069      */
0070  retry:
0071     if (smu_supports_new_fans_ops) {
0072         buffer[0] = 0x30;
0073         buffer[1] = id;
0074         *((u16 *)(&buffer[2])) = value;
0075         cmd.data_len = 4;
0076     } else {
0077         if (id > 7)
0078             return -EINVAL;
0079         /* Fill argument buffer */
0080         memset(buffer, 0, 16);
0081         buffer[0] = pwm ? 0x10 : 0x00;
0082         buffer[1] = 0x01 << id;
0083         *((u16 *)&buffer[2 + id * 2]) = value;
0084         cmd.data_len = 14;
0085     }
0086 
0087     cmd.reply_len = 16;
0088     cmd.data_buf = cmd.reply_buf = buffer;
0089     cmd.status = 0;
0090     cmd.done = smu_done_complete;
0091     cmd.misc = &comp;
0092 
0093     rc = smu_queue_cmd(&cmd);
0094     if (rc)
0095         return rc;
0096     wait_for_completion(&comp);
0097 
0098     /* Handle fallback (see comment above) */
0099     if (cmd.status != 0 && smu_supports_new_fans_ops) {
0100         printk(KERN_WARNING "windfarm: SMU failed new fan command "
0101                "falling back to old method\n");
0102         smu_supports_new_fans_ops = 0;
0103         goto retry;
0104     }
0105 
0106     return cmd.status;
0107 }
0108 
0109 static void smu_fan_release(struct wf_control *ct)
0110 {
0111     struct smu_fan_control *fct = to_smu_fan(ct);
0112 
0113     kfree(fct);
0114 }
0115 
0116 static int smu_fan_set(struct wf_control *ct, s32 value)
0117 {
0118     struct smu_fan_control *fct = to_smu_fan(ct);
0119 
0120     if (value < fct->min)
0121         value = fct->min;
0122     if (value > fct->max)
0123         value = fct->max;
0124     fct->value = value;
0125 
0126     return smu_set_fan(fct->fan_type, fct->reg, value);
0127 }
0128 
0129 static int smu_fan_get(struct wf_control *ct, s32 *value)
0130 {
0131     struct smu_fan_control *fct = to_smu_fan(ct);
0132     *value = fct->value; /* todo: read from SMU */
0133     return 0;
0134 }
0135 
0136 static s32 smu_fan_min(struct wf_control *ct)
0137 {
0138     struct smu_fan_control *fct = to_smu_fan(ct);
0139     return fct->min;
0140 }
0141 
0142 static s32 smu_fan_max(struct wf_control *ct)
0143 {
0144     struct smu_fan_control *fct = to_smu_fan(ct);
0145     return fct->max;
0146 }
0147 
0148 static const struct wf_control_ops smu_fan_ops = {
0149     .set_value  = smu_fan_set,
0150     .get_value  = smu_fan_get,
0151     .get_min    = smu_fan_min,
0152     .get_max    = smu_fan_max,
0153     .release    = smu_fan_release,
0154     .owner      = THIS_MODULE,
0155 };
0156 
0157 static struct smu_fan_control *smu_fan_create(struct device_node *node,
0158                           int pwm_fan)
0159 {
0160     struct smu_fan_control *fct;
0161     const s32 *v;
0162     const u32 *reg;
0163     const char *l;
0164 
0165     fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL);
0166     if (fct == NULL)
0167         return NULL;
0168     fct->ctrl.ops = &smu_fan_ops;
0169     l = of_get_property(node, "location", NULL);
0170     if (l == NULL)
0171         goto fail;
0172 
0173     fct->fan_type = pwm_fan;
0174     fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN;
0175 
0176     /* We use the name & location here the same way we do for SMU sensors,
0177      * see the comment in windfarm_smu_sensors.c. The locations are a bit
0178      * less consistent here between the iMac and the desktop models, but
0179      * that is good enough for our needs for now at least.
0180      *
0181      * One problem though is that Apple seem to be inconsistent with case
0182      * and the kernel doesn't have strcasecmp =P
0183      */
0184 
0185     fct->ctrl.name = NULL;
0186 
0187     /* Names used on desktop models */
0188     if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") ||
0189         !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") ||
0190         !strcmp(l, "CPU A EXHAUST"))
0191         fct->ctrl.name = "cpu-rear-fan-0";
0192     else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") ||
0193          !strcmp(l, "CPU B EXHAUST"))
0194         fct->ctrl.name = "cpu-rear-fan-1";
0195     else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") ||
0196          !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") ||
0197          !strcmp(l, "CPU A INTAKE"))
0198         fct->ctrl.name = "cpu-front-fan-0";
0199     else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") ||
0200          !strcmp(l, "CPU B INTAKE"))
0201         fct->ctrl.name = "cpu-front-fan-1";
0202     else if (!strcmp(l, "CPU A PUMP"))
0203         fct->ctrl.name = "cpu-pump-0";
0204     else if (!strcmp(l, "CPU B PUMP"))
0205         fct->ctrl.name = "cpu-pump-1";
0206     else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") ||
0207          !strcmp(l, "EXPANSION SLOTS INTAKE"))
0208         fct->ctrl.name = "slots-fan";
0209     else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") ||
0210          !strcmp(l, "DRIVE BAY A INTAKE"))
0211         fct->ctrl.name = "drive-bay-fan";
0212     else if (!strcmp(l, "BACKSIDE"))
0213         fct->ctrl.name = "backside-fan";
0214 
0215     /* Names used on iMac models */
0216     if (!strcmp(l, "System Fan") || !strcmp(l, "System fan"))
0217         fct->ctrl.name = "system-fan";
0218     else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan"))
0219         fct->ctrl.name = "cpu-fan";
0220     else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive"))
0221         fct->ctrl.name = "drive-bay-fan";
0222     else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */
0223         fct->ctrl.name = "hard-drive-fan";
0224     else if (!strcmp(l, "ODD Fan")) /* same */
0225         fct->ctrl.name = "optical-drive-fan";
0226 
0227     /* Unrecognized fan, bail out */
0228     if (fct->ctrl.name == NULL)
0229         goto fail;
0230 
0231     /* Get min & max values*/
0232     v = of_get_property(node, "min-value", NULL);
0233     if (v == NULL)
0234         goto fail;
0235     fct->min = *v;
0236     v = of_get_property(node, "max-value", NULL);
0237     if (v == NULL)
0238         goto fail;
0239     fct->max = *v;
0240 
0241     /* Get "reg" value */
0242     reg = of_get_property(node, "reg", NULL);
0243     if (reg == NULL)
0244         goto fail;
0245     fct->reg = *reg;
0246 
0247     if (wf_register_control(&fct->ctrl))
0248         goto fail;
0249 
0250     return fct;
0251  fail:
0252     kfree(fct);
0253     return NULL;
0254 }
0255 
0256 
0257 static int __init smu_controls_init(void)
0258 {
0259     struct device_node *smu, *fans, *fan;
0260 
0261     if (!smu_present())
0262         return -ENODEV;
0263 
0264     smu = of_find_node_by_type(NULL, "smu");
0265     if (smu == NULL)
0266         return -ENODEV;
0267 
0268     /* Look for RPM fans */
0269     for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
0270         if (of_node_name_eq(fans, "rpm-fans") ||
0271             of_device_is_compatible(fans, "smu-rpm-fans"))
0272             break;
0273     for (fan = NULL;
0274          fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
0275         struct smu_fan_control *fct;
0276 
0277         fct = smu_fan_create(fan, 0);
0278         if (fct == NULL) {
0279             printk(KERN_WARNING "windfarm: Failed to create SMU "
0280                    "RPM fan %pOFn\n", fan);
0281             continue;
0282         }
0283         list_add(&fct->link, &smu_fans);
0284     }
0285     of_node_put(fans);
0286 
0287 
0288     /* Look for PWM fans */
0289     for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;)
0290         if (of_node_name_eq(fans, "pwm-fans"))
0291             break;
0292     for (fan = NULL;
0293          fans && (fan = of_get_next_child(fans, fan)) != NULL;) {
0294         struct smu_fan_control *fct;
0295 
0296         fct = smu_fan_create(fan, 1);
0297         if (fct == NULL) {
0298             printk(KERN_WARNING "windfarm: Failed to create SMU "
0299                    "PWM fan %pOFn\n", fan);
0300             continue;
0301         }
0302         list_add(&fct->link, &smu_fans);
0303     }
0304     of_node_put(fans);
0305     of_node_put(smu);
0306 
0307     return 0;
0308 }
0309 
0310 static void __exit smu_controls_exit(void)
0311 {
0312     struct smu_fan_control *fct;
0313 
0314     while (!list_empty(&smu_fans)) {
0315         fct = list_entry(smu_fans.next, struct smu_fan_control, link);
0316         list_del(&fct->link);
0317         wf_unregister_control(&fct->ctrl);
0318     }
0319 }
0320 
0321 
0322 module_init(smu_controls_init);
0323 module_exit(smu_controls_exit);
0324 
0325 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
0326 MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control");
0327 MODULE_LICENSE("GPL");
0328