Path: blob/master/drivers/macintosh/windfarm_smu_sat.c
15109 views
/*1* Windfarm PowerMac thermal control. SMU "satellite" controller sensors.2*3* Copyright (C) 2005 Paul Mackerras, IBM Corp. <[email protected]>4*5* Released under the terms of the GNU GPL v2.6*/78#include <linux/types.h>9#include <linux/errno.h>10#include <linux/kernel.h>11#include <linux/slab.h>12#include <linux/init.h>13#include <linux/wait.h>14#include <linux/i2c.h>15#include <linux/mutex.h>16#include <asm/prom.h>17#include <asm/smu.h>18#include <asm/pmac_low_i2c.h>1920#include "windfarm.h"2122#define VERSION "0.2"2324#define DEBUG2526#ifdef DEBUG27#define DBG(args...) printk(args)28#else29#define DBG(args...) do { } while(0)30#endif3132/* If the cache is older than 800ms we'll refetch it */33#define MAX_AGE msecs_to_jiffies(800)3435struct wf_sat {36int nr;37atomic_t refcnt;38struct mutex mutex;39unsigned long last_read; /* jiffies when cache last updated */40u8 cache[16];41struct i2c_client *i2c;42struct device_node *node;43};4445static struct wf_sat *sats[2];4647struct wf_sat_sensor {48int index;49int index2; /* used for power sensors */50int shift;51struct wf_sat *sat;52struct wf_sensor sens;53};5455#define wf_to_sat(c) container_of(c, struct wf_sat_sensor, sens)5657struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,58unsigned int *size)59{60struct wf_sat *sat;61int err;62unsigned int i, len;63u8 *buf;64u8 data[4];6566/* TODO: Add the resulting partition to the device-tree */6768if (sat_id > 1 || (sat = sats[sat_id]) == NULL)69return NULL;7071err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);72if (err) {73printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);74return NULL;75}7677err = i2c_smbus_read_word_data(sat->i2c, 9);78if (err < 0) {79printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");80return NULL;81}82len = err;83if (len == 0) {84printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);85return NULL;86}8788len = le16_to_cpu(len);89len = (len + 3) & ~3;90buf = kmalloc(len, GFP_KERNEL);91if (buf == NULL)92return NULL;9394for (i = 0; i < len; i += 4) {95err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);96if (err < 0) {97printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",98err);99goto fail;100}101buf[i] = data[1];102buf[i+1] = data[0];103buf[i+2] = data[3];104buf[i+3] = data[2];105}106#ifdef DEBUG107DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);108for (i = 0; i < len; ++i)109DBG(" %x", buf[i]);110DBG("\n");111#endif112113if (size)114*size = len;115return (struct smu_sdbp_header *) buf;116117fail:118kfree(buf);119return NULL;120}121EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);122123/* refresh the cache */124static int wf_sat_read_cache(struct wf_sat *sat)125{126int err;127128err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);129if (err < 0)130return err;131sat->last_read = jiffies;132#ifdef LOTSA_DEBUG133{134int i;135DBG(KERN_DEBUG "wf_sat_get: data is");136for (i = 0; i < 16; ++i)137DBG(" %.2x", sat->cache[i]);138DBG("\n");139}140#endif141return 0;142}143144static int wf_sat_get(struct wf_sensor *sr, s32 *value)145{146struct wf_sat_sensor *sens = wf_to_sat(sr);147struct wf_sat *sat = sens->sat;148int i, err;149s32 val;150151if (sat->i2c == NULL)152return -ENODEV;153154mutex_lock(&sat->mutex);155if (time_after(jiffies, (sat->last_read + MAX_AGE))) {156err = wf_sat_read_cache(sat);157if (err)158goto fail;159}160161i = sens->index * 2;162val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;163if (sens->index2 >= 0) {164i = sens->index2 * 2;165/* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */166val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;167}168169*value = val;170err = 0;171172fail:173mutex_unlock(&sat->mutex);174return err;175}176177static void wf_sat_release(struct wf_sensor *sr)178{179struct wf_sat_sensor *sens = wf_to_sat(sr);180struct wf_sat *sat = sens->sat;181182if (atomic_dec_and_test(&sat->refcnt)) {183if (sat->nr >= 0)184sats[sat->nr] = NULL;185kfree(sat);186}187kfree(sens);188}189190static struct wf_sensor_ops wf_sat_ops = {191.get_value = wf_sat_get,192.release = wf_sat_release,193.owner = THIS_MODULE,194};195196static struct i2c_driver wf_sat_driver;197198static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)199{200struct i2c_board_info info;201struct i2c_client *client;202const u32 *reg;203u8 addr;204205reg = of_get_property(dev, "reg", NULL);206if (reg == NULL)207return;208addr = *reg;209DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);210211memset(&info, 0, sizeof(struct i2c_board_info));212info.addr = (addr >> 1) & 0x7f;213info.platform_data = dev;214strlcpy(info.type, "wf_sat", I2C_NAME_SIZE);215216client = i2c_new_device(adapter, &info);217if (client == NULL) {218printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");219return;220}221222/*223* Let i2c-core delete that device on driver removal.224* This is safe because i2c-core holds the core_lock mutex for us.225*/226list_add_tail(&client->detected, &wf_sat_driver.clients);227}228229static int wf_sat_probe(struct i2c_client *client,230const struct i2c_device_id *id)231{232struct device_node *dev = client->dev.platform_data;233struct wf_sat *sat;234struct wf_sat_sensor *sens;235const u32 *reg;236const char *loc, *type;237u8 chip, core;238struct device_node *child;239int shift, cpu, index;240char *name;241int vsens[2], isens[2];242243sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);244if (sat == NULL)245return -ENOMEM;246sat->nr = -1;247sat->node = of_node_get(dev);248atomic_set(&sat->refcnt, 0);249mutex_init(&sat->mutex);250sat->i2c = client;251i2c_set_clientdata(client, sat);252253vsens[0] = vsens[1] = -1;254isens[0] = isens[1] = -1;255child = NULL;256while ((child = of_get_next_child(dev, child)) != NULL) {257reg = of_get_property(child, "reg", NULL);258type = of_get_property(child, "device_type", NULL);259loc = of_get_property(child, "location", NULL);260if (reg == NULL || loc == NULL)261continue;262263/* the cooked sensors are between 0x30 and 0x37 */264if (*reg < 0x30 || *reg > 0x37)265continue;266index = *reg - 0x30;267268/* expect location to be CPU [AB][01] ... */269if (strncmp(loc, "CPU ", 4) != 0)270continue;271chip = loc[4] - 'A';272core = loc[5] - '0';273if (chip > 1 || core > 1) {274printk(KERN_ERR "wf_sat_create: don't understand "275"location %s for %s\n", loc, child->full_name);276continue;277}278cpu = 2 * chip + core;279if (sat->nr < 0)280sat->nr = chip;281else if (sat->nr != chip) {282printk(KERN_ERR "wf_sat_create: can't cope with "283"multiple CPU chips on one SAT (%s)\n", loc);284continue;285}286287if (strcmp(type, "voltage-sensor") == 0) {288name = "cpu-voltage";289shift = 4;290vsens[core] = index;291} else if (strcmp(type, "current-sensor") == 0) {292name = "cpu-current";293shift = 8;294isens[core] = index;295} else if (strcmp(type, "temp-sensor") == 0) {296name = "cpu-temp";297shift = 10;298} else299continue; /* hmmm shouldn't happen */300301/* the +16 is enough for "cpu-voltage-n" */302sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);303if (sens == NULL) {304printk(KERN_ERR "wf_sat_create: couldn't create "305"%s sensor %d (no memory)\n", name, cpu);306continue;307}308sens->index = index;309sens->index2 = -1;310sens->shift = shift;311sens->sat = sat;312atomic_inc(&sat->refcnt);313sens->sens.ops = &wf_sat_ops;314sens->sens.name = (char *) (sens + 1);315snprintf(sens->sens.name, 16, "%s-%d", name, cpu);316317if (wf_register_sensor(&sens->sens)) {318atomic_dec(&sat->refcnt);319kfree(sens);320}321}322323/* make the power sensors */324for (core = 0; core < 2; ++core) {325if (vsens[core] < 0 || isens[core] < 0)326continue;327cpu = 2 * sat->nr + core;328sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);329if (sens == NULL) {330printk(KERN_ERR "wf_sat_create: couldn't create power "331"sensor %d (no memory)\n", cpu);332continue;333}334sens->index = vsens[core];335sens->index2 = isens[core];336sens->shift = 0;337sens->sat = sat;338atomic_inc(&sat->refcnt);339sens->sens.ops = &wf_sat_ops;340sens->sens.name = (char *) (sens + 1);341snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);342343if (wf_register_sensor(&sens->sens)) {344atomic_dec(&sat->refcnt);345kfree(sens);346}347}348349if (sat->nr >= 0)350sats[sat->nr] = sat;351352return 0;353}354355static int wf_sat_attach(struct i2c_adapter *adapter)356{357struct device_node *busnode, *dev = NULL;358struct pmac_i2c_bus *bus;359360bus = pmac_i2c_adapter_to_bus(adapter);361if (bus == NULL)362return -ENODEV;363busnode = pmac_i2c_get_bus_node(bus);364365while ((dev = of_get_next_child(busnode, dev)) != NULL)366if (of_device_is_compatible(dev, "smu-sat"))367wf_sat_create(adapter, dev);368return 0;369}370371static int wf_sat_remove(struct i2c_client *client)372{373struct wf_sat *sat = i2c_get_clientdata(client);374375/* XXX TODO */376377sat->i2c = NULL;378return 0;379}380381static const struct i2c_device_id wf_sat_id[] = {382{ "wf_sat", 0 },383{ }384};385386static struct i2c_driver wf_sat_driver = {387.driver = {388.name = "wf_smu_sat",389},390.attach_adapter = wf_sat_attach,391.probe = wf_sat_probe,392.remove = wf_sat_remove,393.id_table = wf_sat_id,394};395396static int __init sat_sensors_init(void)397{398return i2c_add_driver(&wf_sat_driver);399}400401#if 0 /* uncomment when module_exit() below is uncommented */402static void __exit sat_sensors_exit(void)403{404i2c_del_driver(&wf_sat_driver);405}406#endif407408module_init(sat_sensors_init);409/*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */410411MODULE_AUTHOR("Paul Mackerras <[email protected]>");412MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");413MODULE_LICENSE("GPL");414415416