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> |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 21 | #include <asm/sections.h> |
| 22 | #include <asm/smu.h> |
| 23 | |
| 24 | #include "windfarm.h" |
| 25 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 26 | #define VERSION "0.4" |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 27 | |
| 28 | #undef DEBUG |
| 29 | |
| 30 | #ifdef DEBUG |
| 31 | #define DBG(args...) printk(args) |
| 32 | #else |
| 33 | #define DBG(args...) do { } while(0) |
| 34 | #endif |
| 35 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 36 | static int smu_supports_new_fans_ops = 1; |
| 37 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 38 | /* |
| 39 | * SMU fans control object |
| 40 | */ |
| 41 | |
| 42 | static LIST_HEAD(smu_fans); |
| 43 | |
| 44 | struct smu_fan_control { |
| 45 | struct list_head link; |
| 46 | int fan_type; /* 0 = rpm, 1 = pwm */ |
| 47 | u32 reg; /* index in SMU */ |
| 48 | s32 value; /* current value */ |
| 49 | s32 min, max; /* min/max values */ |
| 50 | struct wf_control ctrl; |
| 51 | }; |
| 52 | #define to_smu_fan(c) container_of(c, struct smu_fan_control, ctrl) |
| 53 | |
| 54 | static int smu_set_fan(int pwm, u8 id, u16 value) |
| 55 | { |
| 56 | struct smu_cmd cmd; |
| 57 | u8 buffer[16]; |
Peter Zijlstra | 6e9a473 | 2006-09-30 23:28:10 -0700 | [diff] [blame] | 58 | DECLARE_COMPLETION_ONSTACK(comp); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 59 | int rc; |
| 60 | |
| 61 | /* Fill SMU command structure */ |
| 62 | cmd.cmd = SMU_CMD_FAN_COMMAND; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 63 | |
| 64 | /* The SMU has an "old" and a "new" way of setting the fan speed |
| 65 | * Unfortunately, I found no reliable way to know which one works |
| 66 | * on a given machine model. After some investigations it appears |
| 67 | * that MacOS X just tries the new one, and if it fails fallbacks |
| 68 | * to the old ones ... Ugh. |
| 69 | */ |
| 70 | retry: |
| 71 | if (smu_supports_new_fans_ops) { |
| 72 | buffer[0] = 0x30; |
| 73 | buffer[1] = id; |
| 74 | *((u16 *)(&buffer[2])) = value; |
| 75 | cmd.data_len = 4; |
| 76 | } else { |
| 77 | if (id > 7) |
| 78 | return -EINVAL; |
| 79 | /* Fill argument buffer */ |
| 80 | memset(buffer, 0, 16); |
| 81 | buffer[0] = pwm ? 0x10 : 0x00; |
| 82 | buffer[1] = 0x01 << id; |
| 83 | *((u16 *)&buffer[2 + id * 2]) = value; |
| 84 | cmd.data_len = 14; |
| 85 | } |
| 86 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 87 | cmd.reply_len = 16; |
| 88 | cmd.data_buf = cmd.reply_buf = buffer; |
| 89 | cmd.status = 0; |
| 90 | cmd.done = smu_done_complete; |
| 91 | cmd.misc = ∁ |
| 92 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 93 | rc = smu_queue_cmd(&cmd); |
| 94 | if (rc) |
| 95 | return rc; |
| 96 | wait_for_completion(&comp); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 97 | |
| 98 | /* Handle fallback (see coment above) */ |
| 99 | if (cmd.status != 0 && smu_supports_new_fans_ops) { |
| 100 | printk(KERN_WARNING "windfarm: SMU failed new fan command " |
| 101 | "falling back to old method\n"); |
| 102 | smu_supports_new_fans_ops = 0; |
| 103 | goto retry; |
| 104 | } |
| 105 | |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 106 | return cmd.status; |
| 107 | } |
| 108 | |
| 109 | static void smu_fan_release(struct wf_control *ct) |
| 110 | { |
| 111 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 112 | |
| 113 | kfree(fct); |
| 114 | } |
| 115 | |
| 116 | static int smu_fan_set(struct wf_control *ct, s32 value) |
| 117 | { |
| 118 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 119 | |
| 120 | if (value < fct->min) |
| 121 | value = fct->min; |
| 122 | if (value > fct->max) |
| 123 | value = fct->max; |
| 124 | fct->value = value; |
| 125 | |
| 126 | return smu_set_fan(fct->fan_type, fct->reg, value); |
| 127 | } |
| 128 | |
| 129 | static int smu_fan_get(struct wf_control *ct, s32 *value) |
| 130 | { |
| 131 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 132 | *value = fct->value; /* todo: read from SMU */ |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static s32 smu_fan_min(struct wf_control *ct) |
| 137 | { |
| 138 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 139 | return fct->min; |
| 140 | } |
| 141 | |
| 142 | static s32 smu_fan_max(struct wf_control *ct) |
| 143 | { |
| 144 | struct smu_fan_control *fct = to_smu_fan(ct); |
| 145 | return fct->max; |
| 146 | } |
| 147 | |
| 148 | static 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 | }; |
| 156 | |
| 157 | static struct smu_fan_control *smu_fan_create(struct device_node *node, |
| 158 | int pwm_fan) |
| 159 | { |
| 160 | struct smu_fan_control *fct; |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 161 | const s32 *v; |
| 162 | const u32 *reg; |
| 163 | const char *l; |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 164 | |
| 165 | fct = kmalloc(sizeof(struct smu_fan_control), GFP_KERNEL); |
| 166 | if (fct == NULL) |
| 167 | return NULL; |
| 168 | fct->ctrl.ops = &smu_fan_ops; |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 169 | l = of_get_property(node, "location", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 170 | if (l == NULL) |
| 171 | goto fail; |
| 172 | |
| 173 | fct->fan_type = pwm_fan; |
| 174 | fct->ctrl.type = pwm_fan ? WF_CONTROL_PWM_FAN : WF_CONTROL_RPM_FAN; |
Johannes Berg | b35c74d | 2010-02-20 16:43:02 +0100 | [diff] [blame] | 175 | sysfs_attr_init(&fct->ctrl.attr.attr); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 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"; |
Bolko Maass | 529586d | 2009-11-27 05:44:33 +0000 | [diff] [blame] | 205 | else if (!strcmp(l, "CPU B PUMP")) |
| 206 | fct->ctrl.name = "cpu-pump-1"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 207 | else if (!strcmp(l, "Slots Fan") || !strcmp(l, "Slots fan") || |
| 208 | !strcmp(l, "EXPANSION SLOTS INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 209 | fct->ctrl.name = "slots-fan"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 210 | else if (!strcmp(l, "Drive Bay") || !strcmp(l, "Drive bay") || |
| 211 | !strcmp(l, "DRIVE BAY A INTAKE")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 212 | fct->ctrl.name = "drive-bay-fan"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 213 | else if (!strcmp(l, "BACKSIDE")) |
| 214 | fct->ctrl.name = "backside-fan"; |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 215 | |
| 216 | /* Names used on iMac models */ |
| 217 | if (!strcmp(l, "System Fan") || !strcmp(l, "System fan")) |
| 218 | fct->ctrl.name = "system-fan"; |
| 219 | else if (!strcmp(l, "CPU Fan") || !strcmp(l, "CPU fan")) |
| 220 | fct->ctrl.name = "cpu-fan"; |
| 221 | else if (!strcmp(l, "Hard Drive") || !strcmp(l, "Hard drive")) |
| 222 | fct->ctrl.name = "drive-bay-fan"; |
Étienne Bersac | 80ff974 | 2008-04-29 15:39:55 +1000 | [diff] [blame] | 223 | else if (!strcmp(l, "HDD Fan")) /* seen on iMac G5 iSight */ |
| 224 | fct->ctrl.name = "hard-drive-fan"; |
| 225 | else if (!strcmp(l, "ODD Fan")) /* same */ |
| 226 | fct->ctrl.name = "optical-drive-fan"; |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 227 | |
| 228 | /* Unrecognized fan, bail out */ |
| 229 | if (fct->ctrl.name == NULL) |
| 230 | goto fail; |
| 231 | |
| 232 | /* Get min & max values*/ |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 233 | v = of_get_property(node, "min-value", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 234 | if (v == NULL) |
| 235 | goto fail; |
| 236 | fct->min = *v; |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 237 | v = of_get_property(node, "max-value", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 238 | if (v == NULL) |
| 239 | goto fail; |
| 240 | fct->max = *v; |
| 241 | |
| 242 | /* Get "reg" value */ |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 243 | reg = of_get_property(node, "reg", NULL); |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 244 | if (reg == NULL) |
| 245 | goto fail; |
| 246 | fct->reg = *reg; |
| 247 | |
| 248 | if (wf_register_control(&fct->ctrl)) |
| 249 | goto fail; |
| 250 | |
| 251 | return fct; |
| 252 | fail: |
| 253 | kfree(fct); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static int __init smu_controls_init(void) |
| 259 | { |
| 260 | struct device_node *smu, *fans, *fan; |
| 261 | |
| 262 | if (!smu_present()) |
| 263 | return -ENODEV; |
| 264 | |
| 265 | smu = of_find_node_by_type(NULL, "smu"); |
| 266 | if (smu == NULL) |
| 267 | return -ENODEV; |
| 268 | |
| 269 | /* Look for RPM fans */ |
| 270 | for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 271 | if (!strcmp(fans->name, "rpm-fans") || |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 272 | of_device_is_compatible(fans, "smu-rpm-fans")) |
Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 273 | break; |
| 274 | for (fan = NULL; |
| 275 | fans && (fan = of_get_next_child(fans, fan)) != NULL;) { |
| 276 | struct smu_fan_control *fct; |
| 277 | |
| 278 | fct = smu_fan_create(fan, 0); |
| 279 | if (fct == NULL) { |
| 280 | printk(KERN_WARNING "windfarm: Failed to create SMU " |
| 281 | "RPM fan %s\n", fan->name); |
| 282 | continue; |
| 283 | } |
| 284 | list_add(&fct->link, &smu_fans); |
| 285 | } |
| 286 | of_node_put(fans); |
| 287 | |
| 288 | |
| 289 | /* Look for PWM fans */ |
| 290 | for (fans = NULL; (fans = of_get_next_child(smu, fans)) != NULL;) |
| 291 | if (!strcmp(fans->name, "pwm-fans")) |
| 292 | break; |
| 293 | for (fan = NULL; |
| 294 | fans && (fan = of_get_next_child(fans, fan)) != NULL;) { |
| 295 | struct smu_fan_control *fct; |
| 296 | |
| 297 | fct = smu_fan_create(fan, 1); |
| 298 | if (fct == NULL) { |
| 299 | printk(KERN_WARNING "windfarm: Failed to create SMU " |
| 300 | "PWM fan %s\n", fan->name); |
| 301 | continue; |
| 302 | } |
| 303 | list_add(&fct->link, &smu_fans); |
| 304 | } |
| 305 | of_node_put(fans); |
| 306 | of_node_put(smu); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static void __exit smu_controls_exit(void) |
| 312 | { |
| 313 | struct smu_fan_control *fct; |
| 314 | |
| 315 | while (!list_empty(&smu_fans)) { |
| 316 | fct = list_entry(smu_fans.next, struct smu_fan_control, link); |
| 317 | list_del(&fct->link); |
| 318 | wf_unregister_control(&fct->ctrl); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | |
| 323 | module_init(smu_controls_init); |
| 324 | module_exit(smu_controls_exit); |
| 325 | |
| 326 | MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); |
| 327 | MODULE_DESCRIPTION("SMU control objects for PowerMacs thermal control"); |
| 328 | MODULE_LICENSE("GPL"); |
| 329 | |