Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Windfarm PowerMac thermal control. SMU based controls |
| 3 | * |
| 4 | * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp. |
| 5 | * <benh@kernel.crashing.org> |
| 6 | * |
| 7 | * Released under the term of the GNU GPL v2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/wait.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 17 | #include <linux/completion.h> |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 18 | #include <asm/prom.h> |
| 19 | #include <asm/machdep.h> |
| 20 | #include <asm/io.h> |
| 21 | #include <asm/system.h> |
| 22 | #include <asm/sections.h> |
| 23 | #include <asm/smu.h> |
| 24 | |
| 25 | #include "windfarm.h" |
| 26 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 27 | #define VERSION "0.4" |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 28 | |
| 29 | #undef DEBUG |
| 30 | |
| 31 | #ifdef DEBUG |
| 32 | #define DBG(args...) printk(args) |
| 33 | #else |
| 34 | #define DBG(args...) do { } while(0) |
| 35 | #endif |
| 36 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 37 | static int smu_supports_new_fans_ops = 1; |
| 38 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 39 | /* |
| 40 | * SMU fans control object |
| 41 | */ |
| 42 | |
| 43 | static LIST_HEAD(smu_fans); |
| 44 | |
| 45 | struct smu_fan_control { |
| 46 | struct list_head link; |
| 47 | int fan_type; /* 0 = rpm, 1 = pwm */ |
| 48 | u32 reg; /* index in SMU */ |
| 49 | s32 value; /* current value */ |
| 50 | s32 min, max; /* min/max values */ |
| 51 | struct wf_control ctrl; |
| 52 | }; |
| 53 | #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl) |
| 54 | |
| 55 | static int smu_set_fan(int pwm, u8 id, u16 value) |
| 56 | { |
| 57 | struct smu_cmd cmd; |
| 58 | u8 buffer[16]; |
| 59 | DECLARE_COMPLETION(comp); |
| 60 | int rc; |
| 61 | |
| 62 | /* Fill SMU command structure */ |
| 63 | cmd.cmd = SMU_CMD_FAN_COMMAND; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 64 | |
| 65 | /* The SMU has an "old" and a "new" way of setting the fan speed |
| 66 | * Unfortunately, I found no reliable way to know which one works |
| 67 | * on a given machine model. After some investigations it appears |
| 68 | * that MacOS X just tries the new one, and if it fails fallbacks |
| 69 | * to the old ones ... Ugh. |
| 70 | */ |
| 71 | retry: |
| 72 | if (smu_supports_new_fans_ops) { |
| 73 | buffer[0] = 0x30; |
| 74 | buffer[1] = id; |
| 75 | *((u16 *)(&buffer[2])) = value; |
| 76 | cmd.data_len = 4; |
| 77 | } else { |
| 78 | if (id > 7) |
| 79 | return -EINVAL; |
| 80 | /* Fill argument buffer */ |
| 81 | memset(buffer, 0, 16); |
| 82 | buffer[0] = pwm ? 0x10 : 0x00; |
| 83 | buffer[1] = 0x01 << id; |
| 84 | *((u16 *)&buffer[2 + id * 2]) = value; |
| 85 | cmd.data_len = 14; |
| 86 | } |
| 87 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 88 | cmd.reply_len = 16; |
| 89 | cmd.data_buf = cmd.reply_buf = buffer; |
| 90 | cmd.status = 0; |
| 91 | cmd.done = smu_done_complete; |
| 92 | cmd.misc = ∁ |
| 93 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 94 | rc = smu_queue_cmd(&cmd); |
| 95 | if (rc) |
| 96 | return rc; |
| 97 | wait_for_completion(&comp); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 98 | |
| 99 | /* Handle fallback (see coment above) */ |
| 100 | if (cmd.status != 0 && smu_supports_new_fans_ops) { |
| 101 | printk(KERN_WARNING "windfarm: SMU failed new fan command " |
| 102 | "falling back to old method\n"); |
| 103 | smu_supports_new_fans_ops = 0; |
| 104 | goto retry; |
| 105 | } |
| 106 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 107 | return cmd.status; |
| 108 | } |
| 109 | |
| 110 | static void smu_fan_release(struct wf_control *ct) |
| 111 | { |
| 112 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 113 | |
| 114 | kfree(fct); |
| 115 | } |
| 116 | |
| 117 | static int smu_fan_set(struct wf_control *ct, s32 value) |
| 118 | { |
| 119 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 120 | |
| 121 | if (value < fct->min) |
| 122 | value = fct->min; |
| 123 | if (value > fct->max) |
| 124 | value = fct->max; |
| 125 | fct->value = value; |
| 126 | |
| 127 | return smu_set_fan(fct->fan_type, fct->reg, value); |
| 128 | } |
| 129 | |
| 130 | static int smu_fan_get(struct wf_control *ct, s32 *value) |
| 131 | { |
| 132 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 133 | *value = fct->value; /* todo: read from SMU */ |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static s32 smu_fan_min(struct wf_control *ct) |
| 138 | { |
| 139 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 140 | return fct->min; |
| 141 | } |
| 142 | |
| 143 | static s32 smu_fan_max(struct wf_control *ct) |
| 144 | { |
| 145 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 146 | return fct->max; |
| 147 | } |
| 148 | |
| 149 | static struct wf_control_ops smu_fan_ops = { |
| 150 | .set_value = smu_fan_set, |
| 151 | .get_value = smu_fan_get, |
| 152 | .get_min = smu_fan_min, |
| 153 | .get_max = smu_fan_max, |
| 154 | .release = smu_fan_release, |
| 155 | .owner = THIS_MODULE, |
| 156 | }; |
| 157 | |
| 158 | static struct smu_fan_control *smu_fan_create(struct device_node *node, |
| 159 | int pwm_fan) |
| 160 | { |
| 161 | struct smu_fan_control *fct; |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame^] | 162 | const s32 *v; |
| 163 | const u32 *reg; |
| 164 | const char *l; |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 165 | |
| 166 | fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL); |
| 167 | if (fct == NULL) |
| 168 | return NULL; |
| 169 | fct->ctrl.ops = &smu_fan_ops; |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame^] | 170 | l = get_property(node, "location", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 171 | if (l == NULL) |
| 172 | goto fail; |
| 173 | |
| 174 | fct->fan_type = pwm_fan; |
| 175 | fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN; |
| 176 | |
| 177 | /* 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 bit |
| 179 | * less consistent here between the iMac and the desktop models, but |
| 180 | * that is good enough for our needs for now at least. |
| 181 | * |
| 182 | * One problem though is that Apple seem to be inconsistent with case |
| 183 | * and the kernel doesn't have strcasecmp =P |
| 184 | */ |
| 185 | |
| 186 | fct->ctrl.name = NULL; |
| 187 | |
| 188 | /* Names used on desktop models */ |
| 189 | if (!strcmp(l, "Rear Fan 0") || !strcmp(l, "Rear Fan") || |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 190 | !strcmp(l, "Rear fan 0") || !strcmp(l, "Rear fan") || |
| 191 | !strcmp(l, "CPU A EXHAUST")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 192 | fct->ctrl.name = "cpu-rear-fan-0"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 193 | else if (!strcmp(l, "Rear Fan 1") || !strcmp(l, "Rear fan 1") || |
| 194 | !strcmp(l, "CPU B EXHAUST")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 195 | fct->ctrl.name = "cpu-rear-fan-1"; |
| 196 | else if (!strcmp(l, "Front Fan 0") || !strcmp(l, "Front Fan") || |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 197 | !strcmp(l, "Front fan 0") || !strcmp(l, "Front fan") || |
| 198 | !strcmp(l, "CPU A INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 199 | fct->ctrl.name = "cpu-front-fan-0"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 200 | else if (!strcmp(l, "Front Fan 1") || !strcmp(l, "Front fan 1") || |
| 201 | !strcmp(l, "CPU B INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 202 | fct->ctrl.name = "cpu-front-fan-1"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 203 | else if (!strcmp(l, "CPU A PUMP")) |
| 204 | fct->ctrl.name = "cpu-pump-0"; |
| 205 | else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") || |
| 206 | !strcmp(l, "EXPANSION SLOTS INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 207 | fct->ctrl.name = "slots-fan"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 208 | else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") || |
| 209 | !strcmp(l, "DRIVE BAY A INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 210 | fct->ctrl.name = "drive-bay-fan"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 211 | else if (!strcmp(l, "BACKSIDE")) |
| 212 | fct->ctrl.name = "backside-fan"; |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 213 | |
| 214 | /* Names used on iMac models */ |
| 215 | if (!strcmp(l, "System Fan") || !strcmp(l, "System fan")) |
| 216 | fct->ctrl.name = "system-fan"; |
| 217 | else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan")) |
| 218 | fct->ctrl.name = "cpu-fan"; |
| 219 | else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive")) |
| 220 | fct->ctrl.name = "drive-bay-fan"; |
| 221 | |
| 222 | /* Unrecognized fan, bail out */ |
| 223 | if (fct->ctrl.name == NULL) |
| 224 | goto fail; |
| 225 | |
| 226 | /* Get min & max values*/ |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame^] | 227 | v = get_property(node, "min-value", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 228 | if (v == NULL) |
| 229 | goto fail; |
| 230 | fct->min = *v; |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame^] | 231 | v = get_property(node, "max-value", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 232 | if (v == NULL) |
| 233 | goto fail; |
| 234 | fct->max = *v; |
| 235 | |
| 236 | /* Get "reg" value */ |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame^] | 237 | reg = get_property(node, "reg", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 238 | if (reg == NULL) |
| 239 | goto fail; |
| 240 | fct->reg = *reg; |
| 241 | |
| 242 | if (wf_register_control(&fct->ctrl)) |
| 243 | goto fail; |
| 244 | |
| 245 | return fct; |
| 246 | fail: |
| 247 | kfree(fct); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | static int __init smu_controls_init(void) |
| 253 | { |
| 254 | struct device_node *smu, *fans, *fan; |
| 255 | |
| 256 | if (!smu_present()) |
| 257 | return -ENODEV; |
| 258 | |
| 259 | smu = of_find_node_by_type(NULL, "smu"); |
| 260 | if (smu == NULL) |
| 261 | return -ENODEV; |
| 262 | |
| 263 | /* Look for RPM fans */ |
| 264 | for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 265 | if (!strcmp(fans->name, "rpm-fans") || |
| 266 | device_is_compatible(fans, "smu-rpm-fans")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 267 | break; |
| 268 | for (fan = NULL; |
| 269 | fans && (fan = of_get_next_child(fans, fan)) != NULL;) { |
| 270 | struct smu_fan_control *fct; |
| 271 | |
| 272 | fct = smu_fan_create(fan, 0); |
| 273 | if (fct == NULL) { |
| 274 | printk(KERN_WARNING "windfarm: Failed to create SMU " |
| 275 | "RPM fan %s\n", fan->name); |
| 276 | continue; |
| 277 | } |
| 278 | list_add(&fct->link, &smu_fans); |
| 279 | } |
| 280 | of_node_put(fans); |
| 281 | |
| 282 | |
| 283 | /* Look for PWM fans */ |
| 284 | for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;) |
| 285 | if (!strcmp(fans->name, "pwm-fans")) |
| 286 | break; |
| 287 | for (fan = NULL; |
| 288 | fans && (fan = of_get_next_child(fans, fan)) != NULL;) { |
| 289 | struct smu_fan_control *fct; |
| 290 | |
| 291 | fct = smu_fan_create(fan, 1); |
| 292 | if (fct == NULL) { |
| 293 | printk(KERN_WARNING "windfarm: Failed to create SMU " |
| 294 | "PWM fan %s\n", fan->name); |
| 295 | continue; |
| 296 | } |
| 297 | list_add(&fct->link, &smu_fans); |
| 298 | } |
| 299 | of_node_put(fans); |
| 300 | of_node_put(smu); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static void __exit smu_controls_exit(void) |
| 306 | { |
| 307 | struct smu_fan_control *fct; |
| 308 | |
| 309 | while (!list_empty(&smu_fans)) { |
| 310 | fct = list_entry(smu_fans.next, struct smu_fan_control, link); |
| 311 | list_del(&fct->link); |
| 312 | wf_unregister_control(&fct->ctrl); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
| 317 | module_init(smu_controls_init); |
| 318 | module_exit(smu_controls_exit); |
| 319 | |
| 320 | MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); |
| 321 | MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control"); |
| 322 | MODULE_LICENSE("GPL"); |
| 323 | |