linux/drivers/macintosh/windfarm_smu_sat.c
<<
>>
Prefs
   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>
  16#include <linux/mutex.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
  36struct wf_sat {
  37        int                     nr;
  38        atomic_t                refcnt;
  39        struct mutex            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
  46static struct wf_sat *sats[2];
  47
  48struct 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
  59static int wf_sat_attach(struct i2c_adapter *adapter);
  60static int wf_sat_detach(struct i2c_client *client);
  61
  62static 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
  70struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
  71                                                  unsigned int *size)
  72{
  73        struct wf_sat *sat;
  74        int err;
  75        unsigned int i, len;
  76        u8 *buf;
  77        u8 data[4];
  78
  79        /* TODO: Add the resulting partition to the device-tree */
  80
  81        if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
  82                return NULL;
  83
  84        err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8);
  85        if (err) {
  86                printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
  87                return NULL;
  88        }
  89
  90        err = i2c_smbus_read_word_data(&sat->i2c, 9);
  91        if (err < 0) {
  92                printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
  93                return NULL;
  94        }
  95        len = err;
  96        if (len == 0) {
  97                printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
  98                return NULL;
  99        }
 100
 101        len = le16_to_cpu(len);
 102        len = (len + 3) & ~3;
 103        buf = kmalloc(len, GFP_KERNEL);
 104        if (buf == NULL)
 105                return NULL;
 106
 107        for (i = 0; i < len; i += 4) {
 108                err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0xa, 4, data);
 109                if (err < 0) {
 110                        printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
 111                               err);
 112                        goto fail;
 113                }
 114                buf[i] = data[1];
 115                buf[i+1] = data[0];
 116                buf[i+2] = data[3];
 117                buf[i+3] = data[2];
 118        }
 119#ifdef DEBUG
 120        DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
 121        for (i = 0; i < len; ++i)
 122                DBG(" %x", buf[i]);
 123        DBG("\n");
 124#endif
 125
 126        if (size)
 127                *size = len;
 128        return (struct smu_sdbp_header *) buf;
 129
 130 fail:
 131        kfree(buf);
 132        return NULL;
 133}
 134EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
 135
 136/* refresh the cache */
 137static int wf_sat_read_cache(struct wf_sat *sat)
 138{
 139        int err;
 140
 141        err = i2c_smbus_read_i2c_block_data(&sat->i2c, 0x3f, 16, sat->cache);
 142        if (err < 0)
 143                return err;
 144        sat->last_read = jiffies;
 145#ifdef LOTSA_DEBUG
 146        {
 147                int i;
 148                DBG(KERN_DEBUG "wf_sat_get: data is");
 149                for (i = 0; i < 16; ++i)
 150                        DBG(" %.2x", sat->cache[i]);
 151                DBG("\n");
 152        }
 153#endif
 154        return 0;
 155}
 156
 157static int wf_sat_get(struct wf_sensor *sr, s32 *value)
 158{
 159        struct wf_sat_sensor *sens = wf_to_sat(sr);
 160        struct wf_sat *sat = sens->sat;
 161        int i, err;
 162        s32 val;
 163
 164        if (sat->i2c.adapter == NULL)
 165                return -ENODEV;
 166
 167        mutex_lock(&sat->mutex);
 168        if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
 169                err = wf_sat_read_cache(sat);
 170                if (err)
 171                        goto fail;
 172        }
 173
 174        i = sens->index * 2;
 175        val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
 176        if (sens->index2 >= 0) {
 177                i = sens->index2 * 2;
 178                /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
 179                val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
 180        }
 181
 182        *value = val;
 183        err = 0;
 184
 185 fail:
 186        mutex_unlock(&sat->mutex);
 187        return err;
 188}
 189
 190static void wf_sat_release(struct wf_sensor *sr)
 191{
 192        struct wf_sat_sensor *sens = wf_to_sat(sr);
 193        struct wf_sat *sat = sens->sat;
 194
 195        if (atomic_dec_and_test(&sat->refcnt)) {
 196                if (sat->i2c.adapter) {
 197                        i2c_detach_client(&sat->i2c);
 198                        sat->i2c.adapter = NULL;
 199                }
 200                if (sat->nr >= 0)
 201                        sats[sat->nr] = NULL;
 202                kfree(sat);
 203        }
 204        kfree(sens);
 205}
 206
 207static struct wf_sensor_ops wf_sat_ops = {
 208        .get_value      = wf_sat_get,
 209        .release        = wf_sat_release,
 210        .owner          = THIS_MODULE,
 211};
 212
 213static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
 214{
 215        struct wf_sat *sat;
 216        struct wf_sat_sensor *sens;
 217        const u32 *reg;
 218        const char *loc, *type;
 219        u8 addr, chip, core;
 220        struct device_node *child;
 221        int shift, cpu, index;
 222        char *name;
 223        int vsens[2], isens[2];
 224
 225        reg = of_get_property(dev, "reg", NULL);
 226        if (reg == NULL)
 227                return;
 228        addr = *reg;
 229        DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
 230
 231        sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
 232        if (sat == NULL)
 233                return;
 234        sat->nr = -1;
 235        sat->node = of_node_get(dev);
 236        atomic_set(&sat->refcnt, 0);
 237        mutex_init(&sat->mutex);
 238        sat->i2c.addr = (addr >> 1) & 0x7f;
 239        sat->i2c.adapter = adapter;
 240        sat->i2c.driver = &wf_sat_driver;
 241        strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1);
 242
 243        if (i2c_attach_client(&sat->i2c)) {
 244                printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
 245                goto fail;
 246        }
 247
 248        vsens[0] = vsens[1] = -1;
 249        isens[0] = isens[1] = -1;
 250        child = NULL;
 251        while ((child = of_get_next_child(dev, child)) != NULL) {
 252                reg = of_get_property(child, "reg", NULL);
 253                type = of_get_property(child, "device_type", NULL);
 254                loc = of_get_property(child, "location", NULL);
 255                if (reg == NULL || loc == NULL)
 256                        continue;
 257
 258                /* the cooked sensors are between 0x30 and 0x37 */
 259                if (*reg < 0x30 || *reg > 0x37)
 260                        continue;
 261                index = *reg - 0x30;
 262
 263                /* expect location to be CPU [AB][01] ... */
 264                if (strncmp(loc, "CPU ", 4) != 0)
 265                        continue;
 266                chip = loc[4] - 'A';
 267                core = loc[5] - '0';
 268                if (chip > 1 || core > 1) {
 269                        printk(KERN_ERR "wf_sat_create: don't understand "
 270                               "location %s for %s\n", loc, child->full_name);
 271                        continue;
 272                }
 273                cpu = 2 * chip + core;
 274                if (sat->nr < 0)
 275                        sat->nr = chip;
 276                else if (sat->nr != chip) {
 277                        printk(KERN_ERR "wf_sat_create: can't cope with "
 278                               "multiple CPU chips on one SAT (%s)\n", loc);
 279                        continue;
 280                }
 281
 282                if (strcmp(type, "voltage-sensor") == 0) {
 283                        name = "cpu-voltage";
 284                        shift = 4;
 285                        vsens[core] = index;
 286                } else if (strcmp(type, "current-sensor") == 0) {
 287                        name = "cpu-current";
 288                        shift = 8;
 289                        isens[core] = index;
 290                } else if (strcmp(type, "temp-sensor") == 0) {
 291                        name = "cpu-temp";
 292                        shift = 10;
 293                } else
 294                        continue;       /* hmmm shouldn't happen */
 295
 296                /* the +16 is enough for "cpu-voltage-n" */
 297                sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
 298                if (sens == NULL) {
 299                        printk(KERN_ERR "wf_sat_create: couldn't create "
 300                               "%s sensor %d (no memory)\n", name, cpu);
 301                        continue;
 302                }
 303                sens->index = index;
 304                sens->index2 = -1;
 305                sens->shift = shift;
 306                sens->sat = sat;
 307                atomic_inc(&sat->refcnt);
 308                sens->sens.ops = &wf_sat_ops;
 309                sens->sens.name = (char *) (sens + 1);
 310                snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
 311
 312                if (wf_register_sensor(&sens->sens)) {
 313                        atomic_dec(&sat->refcnt);
 314                        kfree(sens);
 315                }
 316        }
 317
 318        /* make the power sensors */
 319        for (core = 0; core < 2; ++core) {
 320                if (vsens[core] < 0 || isens[core] < 0)
 321                        continue;
 322                cpu = 2 * sat->nr + core;
 323                sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
 324                if (sens == NULL) {
 325                        printk(KERN_ERR "wf_sat_create: couldn't create power "
 326                               "sensor %d (no memory)\n", cpu);
 327                        continue;
 328                }
 329                sens->index = vsens[core];
 330                sens->index2 = isens[core];
 331                sens->shift = 0;
 332                sens->sat = sat;
 333                atomic_inc(&sat->refcnt);
 334                sens->sens.ops = &wf_sat_ops;
 335                sens->sens.name = (char *) (sens + 1);
 336                snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
 337
 338                if (wf_register_sensor(&sens->sens)) {
 339                        atomic_dec(&sat->refcnt);
 340                        kfree(sens);
 341                }
 342        }
 343
 344        if (sat->nr >= 0)
 345                sats[sat->nr] = sat;
 346
 347        return;
 348
 349 fail:
 350        kfree(sat);
 351}
 352
 353static int wf_sat_attach(struct i2c_adapter *adapter)
 354{
 355        struct device_node *busnode, *dev = NULL;
 356        struct pmac_i2c_bus *bus;
 357
 358        bus = pmac_i2c_adapter_to_bus(adapter);
 359        if (bus == NULL)
 360                return -ENODEV;
 361        busnode = pmac_i2c_get_bus_node(bus);
 362
 363        while ((dev = of_get_next_child(busnode, dev)) != NULL)
 364                if (of_device_is_compatible(dev, "smu-sat"))
 365                        wf_sat_create(adapter, dev);
 366        return 0;
 367}
 368
 369static int wf_sat_detach(struct i2c_client *client)
 370{
 371        struct wf_sat *sat = i2c_to_sat(client);
 372
 373        /* XXX TODO */
 374
 375        sat->i2c.adapter = NULL;
 376        return 0;
 377}
 378
 379static int __init sat_sensors_init(void)
 380{
 381        return i2c_add_driver(&wf_sat_driver);
 382}
 383
 384#if 0   /* uncomment when module_exit() below is uncommented */
 385static void __exit sat_sensors_exit(void)
 386{
 387        i2c_del_driver(&wf_sat_driver);
 388}
 389#endif
 390
 391module_init(sat_sensors_init);
 392/*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
 393
 394MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
 395MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
 396MODULE_LICENSE("GPL");
 397