Input: force LED reset on resume
[safe/jmp/linux-2.6] / drivers / macintosh / windfarm_smu_sat.c
index 9a6c2cf..e20330a 100644 (file)
@@ -13,7 +13,7 @@
 #include <linux/init.h>
 #include <linux/wait.h>
 #include <linux/i2c.h>
-#include <asm/semaphore.h>
+#include <linux/mutex.h>
 #include <asm/prom.h>
 #include <asm/smu.h>
 #include <asm/pmac_low_i2c.h>
 struct wf_sat {
        int                     nr;
        atomic_t                refcnt;
-       struct semaphore        mutex;
+       struct mutex            mutex;
        unsigned long           last_read; /* jiffies when cache last updated */
        u8                      cache[16];
-       struct i2c_client       i2c;
+       struct i2c_client       *i2c;
        struct device_node      *node;
 };
 
@@ -54,38 +54,6 @@ struct wf_sat_sensor {
 };
 
 #define wf_to_sat(c)   container_of(c, struct wf_sat_sensor, sens)
-#define i2c_to_sat(c)  container_of(c, struct wf_sat, i2c)
-
-static int wf_sat_attach(struct i2c_adapter *adapter);
-static int wf_sat_detach(struct i2c_client *client);
-
-static struct i2c_driver wf_sat_driver = {
-       .driver = {
-               .name           = "wf_smu_sat",
-       },
-       .attach_adapter = wf_sat_attach,
-       .detach_client  = wf_sat_detach,
-};
-
-/*
- * XXX i2c_smbus_read_i2c_block_data doesn't pass the requested
- * length down to the low-level driver, so we use this, which
- * works well enough with the SMU i2c driver code...
- */
-static int sat_read_block(struct i2c_client *client, u8 command,
-                         u8 *values, int len)
-{
-       union i2c_smbus_data data;
-       int err;
-
-       data.block[0] = len;
-       err = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
-                            I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
-                            &data);
-       if (!err)
-               memcpy(values, data.block, len);
-       return err;
-}
 
 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
                                                  unsigned int *size)
@@ -101,17 +69,18 @@ struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
        if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
                return NULL;
 
-       err = i2c_smbus_write_word_data(&sat->i2c, 8, id << 8);
+       err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
        if (err) {
                printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
                return NULL;
        }
 
-       len = i2c_smbus_read_word_data(&sat->i2c, 9);
-       if (len < 0) {
+       err = i2c_smbus_read_word_data(sat->i2c, 9);
+       if (err < 0) {
                printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
                return NULL;
        }
+       len = err;
        if (len == 0) {
                printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
                return NULL;
@@ -124,8 +93,8 @@ struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
                return NULL;
 
        for (i = 0; i < len; i += 4) {
-               err = sat_read_block(&sat->i2c, 0xa, data, 4);
-               if (err) {
+               err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
+               if (err < 0) {
                        printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
                               err);
                        goto fail;
@@ -157,8 +126,8 @@ static int wf_sat_read_cache(struct wf_sat *sat)
 {
        int err;
 
-       err = sat_read_block(&sat->i2c, 0x3f, sat->cache, 16);
-       if (err)
+       err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
+       if (err < 0)
                return err;
        sat->last_read = jiffies;
 #ifdef LOTSA_DEBUG
@@ -180,10 +149,10 @@ static int wf_sat_get(struct wf_sensor *sr, s32 *value)
        int i, err;
        s32 val;
 
-       if (sat->i2c.adapter == NULL)
+       if (sat->i2c == NULL)
                return -ENODEV;
 
-       down(&sat->mutex);
+       mutex_lock(&sat->mutex);
        if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
                err = wf_sat_read_cache(sat);
                if (err)
@@ -202,7 +171,7 @@ static int wf_sat_get(struct wf_sensor *sr, s32 *value)
        err = 0;
 
  fail:
-       up(&sat->mutex);
+       mutex_unlock(&sat->mutex);
        return err;
 }
 
@@ -212,10 +181,6 @@ static void wf_sat_release(struct wf_sensor *sr)
        struct wf_sat *sat = sens->sat;
 
        if (atomic_dec_and_test(&sat->refcnt)) {
-               if (sat->i2c.adapter) {
-                       i2c_detach_client(&sat->i2c);
-                       sat->i2c.adapter = NULL;
-               }
                if (sat->nr >= 0)
                        sats[sat->nr] = NULL;
                kfree(sat);
@@ -229,40 +194,62 @@ static struct wf_sensor_ops wf_sat_ops = {
        .owner          = THIS_MODULE,
 };
 
+static struct i2c_driver wf_sat_driver;
+
 static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
 {
+       struct i2c_board_info info;
+       struct i2c_client *client;
+       const u32 *reg;
+       u8 addr;
+
+       reg = of_get_property(dev, "reg", NULL);
+       if (reg == NULL)
+               return;
+       addr = *reg;
+       DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
+
+       memset(&info, 0, sizeof(struct i2c_board_info));
+       info.addr = (addr >> 1) & 0x7f;
+       info.platform_data = dev;
+       strlcpy(info.type, "wf_sat", I2C_NAME_SIZE);
+
+       client = i2c_new_device(adapter, &info);
+       if (client == NULL) {
+               printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
+               return;
+       }
+
+       /*
+        * Let i2c-core delete that device on driver removal.
+        * This is safe because i2c-core holds the core_lock mutex for us.
+        */
+       list_add_tail(&client->detected, &wf_sat_driver.clients);
+}
+
+static int wf_sat_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
+{
+       struct device_node *dev = client->dev.platform_data;
        struct wf_sat *sat;
        struct wf_sat_sensor *sens;
        const u32 *reg;
        const char *loc, *type;
-       u8 addr, chip, core;
+       u8 chip, core;
        struct device_node *child;
        int shift, cpu, index;
        char *name;
        int vsens[2], isens[2];
 
-       reg = of_get_property(dev, "reg", NULL);
-       if (reg == NULL)
-               return;
-       addr = *reg;
-       DBG(KERN_DEBUG "wf_sat: creating sat at address %x\n", addr);
-
        sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
        if (sat == NULL)
-               return;
+               return -ENOMEM;
        sat->nr = -1;
        sat->node = of_node_get(dev);
        atomic_set(&sat->refcnt, 0);
-       init_MUTEX(&sat->mutex);
-       sat->i2c.addr = (addr >> 1) & 0x7f;
-       sat->i2c.adapter = adapter;
-       sat->i2c.driver = &wf_sat_driver;
-       strncpy(sat->i2c.name, "smu-sat", I2C_NAME_SIZE-1);
-
-       if (i2c_attach_client(&sat->i2c)) {
-               printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c\n");
-               goto fail;
-       }
+       mutex_init(&sat->mutex);
+       sat->i2c = client;
+       i2c_set_clientdata(client, sat);
 
        vsens[0] = vsens[1] = -1;
        isens[0] = isens[1] = -1;
@@ -363,10 +350,7 @@ static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
        if (sat->nr >= 0)
                sats[sat->nr] = sat;
 
-       return;
-
- fail:
-       kfree(sat);
+       return 0;
 }
 
 static int wf_sat_attach(struct i2c_adapter *adapter)
@@ -380,30 +364,48 @@ static int wf_sat_attach(struct i2c_adapter *adapter)
        busnode = pmac_i2c_get_bus_node(bus);
 
        while ((dev = of_get_next_child(busnode, dev)) != NULL)
-               if (device_is_compatible(dev, "smu-sat"))
+               if (of_device_is_compatible(dev, "smu-sat"))
                        wf_sat_create(adapter, dev);
        return 0;
 }
 
-static int wf_sat_detach(struct i2c_client *client)
+static int wf_sat_remove(struct i2c_client *client)
 {
-       struct wf_sat *sat = i2c_to_sat(client);
+       struct wf_sat *sat = i2c_get_clientdata(client);
 
        /* XXX TODO */
 
-       sat->i2c.adapter = NULL;
+       sat->i2c = NULL;
+       i2c_set_clientdata(client, NULL);
        return 0;
 }
 
+static const struct i2c_device_id wf_sat_id[] = {
+       { "wf_sat", 0 },
+       { }
+};
+
+static struct i2c_driver wf_sat_driver = {
+       .driver = {
+               .name           = "wf_smu_sat",
+       },
+       .attach_adapter = wf_sat_attach,
+       .probe          = wf_sat_probe,
+       .remove         = wf_sat_remove,
+       .id_table       = wf_sat_id,
+};
+
 static int __init sat_sensors_init(void)
 {
        return i2c_add_driver(&wf_sat_driver);
 }
 
+#if 0  /* uncomment when module_exit() below is uncommented */
 static void __exit sat_sensors_exit(void)
 {
        i2c_del_driver(&wf_sat_driver);
 }
+#endif
 
 module_init(sat_sensors_init);
 /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */