| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors. | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org> | 
|  | 5 | * | 
|  | 6 | * Released under the terms of the GNU GPL v2. | 
|  | 7 | */ | 
|  | 8 |  | 
|  | 9 | #include <linux/types.h> | 
|  | 10 | #include <linux/errno.h> | 
|  | 11 | #include <linux/kernel.h> | 
|  | 12 | #include <linux/slab.h> | 
|  | 13 | #include <linux/init.h> | 
|  | 14 | #include <linux/wait.h> | 
|  | 15 | #include <linux/i2c.h> | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 16 | #include <asm/semaphore.h> | 
|  | 17 | #include <asm/prom.h> | 
|  | 18 | #include <asm/smu.h> | 
|  | 19 | #include <asm/pmac_low_i2c.h> | 
|  | 20 |  | 
|  | 21 | #include "windfarm.h" | 
|  | 22 |  | 
|  | 23 | #define VERSION "0.2" | 
|  | 24 |  | 
|  | 25 | #define DEBUG | 
|  | 26 |  | 
|  | 27 | #ifdef DEBUG | 
|  | 28 | #define DBG(args...)	printk(args) | 
|  | 29 | #else | 
|  | 30 | #define DBG(args...)	do { } while(0) | 
|  | 31 | #endif | 
|  | 32 |  | 
|  | 33 | /* If the cache is older than 800ms we'll refetch it */ | 
|  | 34 | #define MAX_AGE		msecs_to_jiffies(800) | 
|  | 35 |  | 
|  | 36 | struct wf_sat { | 
|  | 37 | int			nr; | 
|  | 38 | atomic_t		refcnt; | 
|  | 39 | struct semaphore	mutex; | 
|  | 40 | unsigned long		last_read; /* jiffies when cache last updated */ | 
|  | 41 | u8			cache[16]; | 
|  | 42 | struct i2c_client	i2c; | 
|  | 43 | struct device_node	*node; | 
|  | 44 | }; | 
|  | 45 |  | 
|  | 46 | static struct wf_sat *sats[2]; | 
|  | 47 |  | 
|  | 48 | struct wf_sat_sensor { | 
|  | 49 | int		index; | 
|  | 50 | int		index2;		/* used for power sensors */ | 
|  | 51 | int		shift; | 
|  | 52 | struct wf_sat	*sat; | 
|  | 53 | struct wf_sensor sens; | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | #define wf_to_sat(c)	container_of(c, struct wf_sat_sensor, sens) | 
|  | 57 | #define i2c_to_sat(c)	container_of(c, struct wf_sat, i2c) | 
|  | 58 |  | 
|  | 59 | static int wf_sat_attach(struct i2c_adapter *adapter); | 
|  | 60 | static int wf_sat_detach(struct i2c_client *client); | 
|  | 61 |  | 
|  | 62 | static struct i2c_driver wf_sat_driver = { | 
|  | 63 | .driver = { | 
|  | 64 | .name		= "wf_smu_sat", | 
|  | 65 | }, | 
|  | 66 | .attach_adapter	= wf_sat_attach, | 
|  | 67 | .detach_client	= wf_sat_detach, | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | /* | 
|  | 71 | * XXX i2c_smbus_read_i2c_block_data doesn't pass the requested | 
|  | 72 | * length down to the low-level driver, so we use this, which | 
|  | 73 | * works well enough with the SMU i2c driver code... | 
|  | 74 | */ | 
|  | 75 | static int sat_read_block(struct i2c_client *client, u8 command, | 
|  | 76 | u8 *values, int len) | 
|  | 77 | { | 
|  | 78 | union i2c_smbus_data data; | 
|  | 79 | int err; | 
|  | 80 |  | 
|  | 81 | data.block[0] = len; | 
|  | 82 | err = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | 
|  | 83 | I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA, | 
|  | 84 | &data); | 
|  | 85 | if (!err) | 
|  | 86 | memcpy(values, data.block, len); | 
|  | 87 | return err; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id, | 
|  | 91 | unsigned int *size) | 
|  | 92 | { | 
|  | 93 | struct wf_sat *sat; | 
|  | 94 | int err; | 
|  | 95 | unsigned int i, len; | 
|  | 96 | u8 *buf; | 
|  | 97 | u8 data[4]; | 
|  | 98 |  | 
|  | 99 | /* TODO: Add the resulting partition to the device-tree */ | 
|  | 100 |  | 
|  | 101 | if (sat_id > 1 || (sat = sats[sat_id]) == NULL) | 
|  | 102 | return NULL; | 
|  | 103 |  | 
|  | 104 | err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8); | 
|  | 105 | if (err) { | 
|  | 106 | printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err); | 
|  | 107 | return NULL; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | len = i2c_smbus_read_word_data(&sat->i2c, 9); | 
|  | 111 | if (len < 0) { | 
|  | 112 | printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n"); | 
|  | 113 | return NULL; | 
|  | 114 | } | 
|  | 115 | if (len == 0) { | 
|  | 116 | printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id); | 
|  | 117 | return NULL; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | len = le16_to_cpu(len); | 
|  | 121 | len = (len + 3) & ~3; | 
|  | 122 | buf = kmalloc(len, GFP_KERNEL); | 
|  | 123 | if (buf == NULL) | 
|  | 124 | return NULL; | 
|  | 125 |  | 
|  | 126 | for (i = 0; i < len; i += 4) { | 
|  | 127 | err = sat_read_block(&sat->i2c, 0xa, data, 4); | 
|  | 128 | if (err) { | 
|  | 129 | printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n", | 
|  | 130 | err); | 
|  | 131 | goto fail; | 
|  | 132 | } | 
|  | 133 | buf[i] = data[1]; | 
|  | 134 | buf[i+1] = data[0]; | 
|  | 135 | buf[i+2] = data[3]; | 
|  | 136 | buf[i+3] = data[2]; | 
|  | 137 | } | 
|  | 138 | #ifdef DEBUG | 
|  | 139 | DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id); | 
|  | 140 | for (i = 0; i < len; ++i) | 
|  | 141 | DBG(" %x", buf[i]); | 
|  | 142 | DBG("\n"); | 
|  | 143 | #endif | 
|  | 144 |  | 
|  | 145 | if (size) | 
|  | 146 | *size = len; | 
|  | 147 | return (struct smu_sdbp_header *) buf; | 
|  | 148 |  | 
|  | 149 | fail: | 
|  | 150 | kfree(buf); | 
|  | 151 | return NULL; | 
|  | 152 | } | 
| Johannes Berg | 9127dd1 | 2006-02-17 13:52:54 -0800 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition); | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 154 |  | 
|  | 155 | /* refresh the cache */ | 
|  | 156 | static int wf_sat_read_cache(struct wf_sat *sat) | 
|  | 157 | { | 
|  | 158 | int err; | 
|  | 159 |  | 
|  | 160 | err = sat_read_block(&sat->i2c, 0x3f, sat->cache, 16); | 
|  | 161 | if (err) | 
|  | 162 | return err; | 
|  | 163 | sat->last_read = jiffies; | 
|  | 164 | #ifdef LOTSA_DEBUG | 
|  | 165 | { | 
|  | 166 | int i; | 
|  | 167 | DBG(KERN_DEBUG "wf_sat_get: data is"); | 
|  | 168 | for (i = 0; i < 16; ++i) | 
|  | 169 | DBG(" %.2x", sat->cache[i]); | 
|  | 170 | DBG("\n"); | 
|  | 171 | } | 
|  | 172 | #endif | 
|  | 173 | return 0; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | static int wf_sat_get(struct wf_sensor *sr, s32 *value) | 
|  | 177 | { | 
|  | 178 | struct wf_sat_sensor *sens = wf_to_sat(sr); | 
|  | 179 | struct wf_sat *sat = sens->sat; | 
|  | 180 | int i, err; | 
|  | 181 | s32 val; | 
|  | 182 |  | 
|  | 183 | if (sat->i2c.adapter == NULL) | 
|  | 184 | return -ENODEV; | 
|  | 185 |  | 
|  | 186 | down(&sat->mutex); | 
|  | 187 | if (time_after(jiffies, (sat->last_read + MAX_AGE))) { | 
|  | 188 | err = wf_sat_read_cache(sat); | 
|  | 189 | if (err) | 
|  | 190 | goto fail; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | i = sens->index * 2; | 
|  | 194 | val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift; | 
|  | 195 | if (sens->index2 >= 0) { | 
|  | 196 | i = sens->index2 * 2; | 
|  | 197 | /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */ | 
|  | 198 | val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | *value = val; | 
|  | 202 | err = 0; | 
|  | 203 |  | 
|  | 204 | fail: | 
|  | 205 | up(&sat->mutex); | 
|  | 206 | return err; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | static void wf_sat_release(struct wf_sensor *sr) | 
|  | 210 | { | 
|  | 211 | struct wf_sat_sensor *sens = wf_to_sat(sr); | 
|  | 212 | struct wf_sat *sat = sens->sat; | 
|  | 213 |  | 
|  | 214 | if (atomic_dec_and_test(&sat->refcnt)) { | 
|  | 215 | if (sat->i2c.adapter) { | 
|  | 216 | i2c_detach_client(&sat->i2c); | 
|  | 217 | sat->i2c.adapter = NULL; | 
|  | 218 | } | 
|  | 219 | if (sat->nr >= 0) | 
|  | 220 | sats[sat->nr] = NULL; | 
|  | 221 | kfree(sat); | 
|  | 222 | } | 
|  | 223 | kfree(sens); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | static struct wf_sensor_ops wf_sat_ops = { | 
|  | 227 | .get_value	= wf_sat_get, | 
|  | 228 | .release	= wf_sat_release, | 
|  | 229 | .owner		= THIS_MODULE, | 
|  | 230 | }; | 
|  | 231 |  | 
|  | 232 | static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev) | 
|  | 233 | { | 
|  | 234 | struct wf_sat *sat; | 
|  | 235 | struct wf_sat_sensor *sens; | 
| Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 236 | const u32 *reg; | 
|  | 237 | const char *loc, *type; | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 238 | u8 addr, chip, core; | 
|  | 239 | struct device_node *child; | 
|  | 240 | int shift, cpu, index; | 
|  | 241 | char *name; | 
|  | 242 | int vsens[2], isens[2]; | 
|  | 243 |  | 
| Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 244 | reg = get_property(dev, "reg", NULL); | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 245 | if (reg == NULL) | 
|  | 246 | return; | 
|  | 247 | addr = *reg; | 
|  | 248 | DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr); | 
|  | 249 |  | 
|  | 250 | sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL); | 
|  | 251 | if (sat == NULL) | 
|  | 252 | return; | 
|  | 253 | sat->nr = -1; | 
|  | 254 | sat->node = of_node_get(dev); | 
|  | 255 | atomic_set(&sat->refcnt, 0); | 
|  | 256 | init_MUTEX(&sat->mutex); | 
|  | 257 | sat->i2c.addr = (addr >> 1) & 0x7f; | 
|  | 258 | sat->i2c.adapter = adapter; | 
|  | 259 | sat->i2c.driver = &wf_sat_driver; | 
|  | 260 | strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1); | 
|  | 261 |  | 
|  | 262 | if (i2c_attach_client(&sat->i2c)) { | 
|  | 263 | printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n"); | 
|  | 264 | goto fail; | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | vsens[0] = vsens[1] = -1; | 
|  | 268 | isens[0] = isens[1] = -1; | 
|  | 269 | child = NULL; | 
|  | 270 | while ((child = of_get_next_child(dev, child)) != NULL) { | 
| Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 271 | reg = get_property(child, "reg", NULL); | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 272 | type = get_property(child, "device_type", NULL); | 
|  | 273 | loc = get_property(child, "location", NULL); | 
|  | 274 | if (reg == NULL || loc == NULL) | 
|  | 275 | continue; | 
|  | 276 |  | 
|  | 277 | /* the cooked sensors are between 0x30 and 0x37 */ | 
|  | 278 | if (*reg < 0x30 || *reg > 0x37) | 
|  | 279 | continue; | 
|  | 280 | index = *reg - 0x30; | 
|  | 281 |  | 
|  | 282 | /* expect location to be CPU [AB][01] ... */ | 
|  | 283 | if (strncmp(loc, "CPU ", 4) != 0) | 
|  | 284 | continue; | 
|  | 285 | chip = loc[4] - 'A'; | 
|  | 286 | core = loc[5] - '0'; | 
|  | 287 | if (chip > 1 || core > 1) { | 
|  | 288 | printk(KERN_ERR "wf_sat_create: don't understand " | 
|  | 289 | "location %s for %s\n", loc, child->full_name); | 
|  | 290 | continue; | 
|  | 291 | } | 
|  | 292 | cpu = 2 * chip + core; | 
|  | 293 | if (sat->nr < 0) | 
|  | 294 | sat->nr = chip; | 
|  | 295 | else if (sat->nr != chip) { | 
|  | 296 | printk(KERN_ERR "wf_sat_create: can't cope with " | 
|  | 297 | "multiple CPU chips on one SAT (%s)\n", loc); | 
|  | 298 | continue; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | if (strcmp(type, "voltage-sensor") == 0) { | 
|  | 302 | name = "cpu-voltage"; | 
|  | 303 | shift = 4; | 
|  | 304 | vsens[core] = index; | 
|  | 305 | } else if (strcmp(type, "current-sensor") == 0) { | 
|  | 306 | name = "cpu-current"; | 
|  | 307 | shift = 8; | 
|  | 308 | isens[core] = index; | 
|  | 309 | } else if (strcmp(type, "temp-sensor") == 0) { | 
|  | 310 | name = "cpu-temp"; | 
|  | 311 | shift = 10; | 
|  | 312 | } else | 
|  | 313 | continue;	/* hmmm shouldn't happen */ | 
|  | 314 |  | 
|  | 315 | /* the +16 is enough for "cpu-voltage-n" */ | 
|  | 316 | sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL); | 
|  | 317 | if (sens == NULL) { | 
|  | 318 | printk(KERN_ERR "wf_sat_create: couldn't create " | 
|  | 319 | "%s sensor %d (no memory)\n", name, cpu); | 
|  | 320 | continue; | 
|  | 321 | } | 
|  | 322 | sens->index = index; | 
|  | 323 | sens->index2 = -1; | 
|  | 324 | sens->shift = shift; | 
|  | 325 | sens->sat = sat; | 
|  | 326 | atomic_inc(&sat->refcnt); | 
|  | 327 | sens->sens.ops = &wf_sat_ops; | 
|  | 328 | sens->sens.name = (char *) (sens + 1); | 
|  | 329 | snprintf(sens->sens.name, 16, "%s-%d", name, cpu); | 
|  | 330 |  | 
|  | 331 | if (wf_register_sensor(&sens->sens)) { | 
|  | 332 | atomic_dec(&sat->refcnt); | 
|  | 333 | kfree(sens); | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | /* make the power sensors */ | 
|  | 338 | for (core = 0; core < 2; ++core) { | 
|  | 339 | if (vsens[core] < 0 || isens[core] < 0) | 
|  | 340 | continue; | 
|  | 341 | cpu = 2 * sat->nr + core; | 
|  | 342 | sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL); | 
|  | 343 | if (sens == NULL) { | 
|  | 344 | printk(KERN_ERR "wf_sat_create: couldn't create power " | 
|  | 345 | "sensor %d (no memory)\n", cpu); | 
|  | 346 | continue; | 
|  | 347 | } | 
|  | 348 | sens->index = vsens[core]; | 
|  | 349 | sens->index2 = isens[core]; | 
|  | 350 | sens->shift = 0; | 
|  | 351 | sens->sat = sat; | 
|  | 352 | atomic_inc(&sat->refcnt); | 
|  | 353 | sens->sens.ops = &wf_sat_ops; | 
|  | 354 | sens->sens.name = (char *) (sens + 1); | 
|  | 355 | snprintf(sens->sens.name, 16, "cpu-power-%d", cpu); | 
|  | 356 |  | 
|  | 357 | if (wf_register_sensor(&sens->sens)) { | 
|  | 358 | atomic_dec(&sat->refcnt); | 
|  | 359 | kfree(sens); | 
|  | 360 | } | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | if (sat->nr >= 0) | 
|  | 364 | sats[sat->nr] = sat; | 
|  | 365 |  | 
|  | 366 | return; | 
|  | 367 |  | 
|  | 368 | fail: | 
|  | 369 | kfree(sat); | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | static int wf_sat_attach(struct i2c_adapter *adapter) | 
|  | 373 | { | 
|  | 374 | struct device_node *busnode, *dev = NULL; | 
|  | 375 | struct pmac_i2c_bus *bus; | 
|  | 376 |  | 
|  | 377 | bus = pmac_i2c_adapter_to_bus(adapter); | 
|  | 378 | if (bus == NULL) | 
|  | 379 | return -ENODEV; | 
|  | 380 | busnode = pmac_i2c_get_bus_node(bus); | 
|  | 381 |  | 
|  | 382 | while ((dev = of_get_next_child(busnode, dev)) != NULL) | 
|  | 383 | if (device_is_compatible(dev, "smu-sat")) | 
|  | 384 | wf_sat_create(adapter, dev); | 
|  | 385 | return 0; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | static int wf_sat_detach(struct i2c_client *client) | 
|  | 389 | { | 
|  | 390 | struct wf_sat *sat = i2c_to_sat(client); | 
|  | 391 |  | 
|  | 392 | /* XXX TODO */ | 
|  | 393 |  | 
|  | 394 | sat->i2c.adapter = NULL; | 
|  | 395 | return 0; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | static int __init sat_sensors_init(void) | 
|  | 399 | { | 
| Alexey Dobriyan | 92e9a85 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 400 | return i2c_add_driver(&wf_sat_driver); | 
| Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
|  | 403 | static void __exit sat_sensors_exit(void) | 
|  | 404 | { | 
|  | 405 | i2c_del_driver(&wf_sat_driver); | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | module_init(sat_sensors_init); | 
|  | 409 | /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */ | 
|  | 410 |  | 
|  | 411 | MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>"); | 
|  | 412 | MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control"); | 
|  | 413 | MODULE_LICENSE("GPL"); |