Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Windfarm PowerMac thermal control. MAX6690 sensor. |
| 3 | * |
| 4 | * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org> |
| 5 | * |
| 6 | * Use and redistribute under the terms of the GNU GPL v2. |
| 7 | */ |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/i2c.h> |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 14 | #include <asm/prom.h> |
| 15 | #include <asm/pmac_low_i2c.h> |
| 16 | |
| 17 | #include "windfarm.h" |
| 18 | |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 19 | #define VERSION "0.2" |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 20 | |
| 21 | /* This currently only exports the external temperature sensor, |
| 22 | since that's all the control loops need. */ |
| 23 | |
| 24 | /* Some MAX6690 register numbers */ |
| 25 | #define MAX6690_INTERNAL_TEMP 0 |
| 26 | #define MAX6690_EXTERNAL_TEMP 1 |
| 27 | |
| 28 | struct wf_6690_sensor { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 29 | struct i2c_client *i2c; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 30 | struct wf_sensor sens; |
| 31 | }; |
| 32 | |
| 33 | #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 34 | |
| 35 | static int wf_max6690_get(struct wf_sensor *sr, s32 *value) |
| 36 | { |
| 37 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 38 | s32 data; |
| 39 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 40 | if (max->i2c == NULL) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 41 | return -ENODEV; |
| 42 | |
| 43 | /* chip gets initialized by firmware */ |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 44 | data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 45 | if (data < 0) |
| 46 | return data; |
| 47 | *value = data << 16; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static void wf_max6690_release(struct wf_sensor *sr) |
| 52 | { |
| 53 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 54 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 55 | kfree(max); |
| 56 | } |
| 57 | |
| 58 | static struct wf_sensor_ops wf_max6690_ops = { |
| 59 | .get_value = wf_max6690_get, |
| 60 | .release = wf_max6690_release, |
| 61 | .owner = THIS_MODULE, |
| 62 | }; |
| 63 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 64 | static int wf_max6690_probe(struct i2c_client *client, |
| 65 | const struct i2c_device_id *id) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 66 | { |
| 67 | struct wf_6690_sensor *max; |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 68 | int rc; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 69 | |
| 70 | max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL); |
| 71 | if (max == NULL) { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 72 | printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: " |
| 73 | "no memory\n"); |
| 74 | return -ENOMEM; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 75 | } |
| 76 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 77 | max->i2c = client; |
| 78 | max->sens.name = client->dev.platform_data; |
| 79 | max->sens.ops = &wf_max6690_ops; |
| 80 | i2c_set_clientdata(client, max); |
| 81 | |
| 82 | rc = wf_register_sensor(&max->sens); |
| 83 | if (rc) { |
| 84 | i2c_set_clientdata(client, NULL); |
| 85 | kfree(max); |
| 86 | } |
| 87 | |
| 88 | return rc; |
| 89 | } |
| 90 | |
Jean Delvare | 6f6b35e | 2009-10-04 22:53:46 +0200 | [diff] [blame] | 91 | static struct i2c_driver wf_max6690_driver; |
| 92 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 93 | static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter, |
| 94 | u8 addr, const char *loc) |
| 95 | { |
| 96 | struct i2c_board_info info; |
| 97 | struct i2c_client *client; |
| 98 | char *name; |
| 99 | |
Étienne Bersac | 80ff974 | 2008-04-29 15:39:55 +1000 | [diff] [blame] | 100 | if (!strcmp(loc, "BACKSIDE")) |
| 101 | name = "backside-temp"; |
| 102 | else if (!strcmp(loc, "NB Ambient")) |
| 103 | name = "north-bridge-temp"; |
| 104 | else if (!strcmp(loc, "GPU Ambient")) |
| 105 | name = "gpu-temp"; |
| 106 | else |
| 107 | goto fail; |
| 108 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 109 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 110 | info.addr = addr >> 1; |
| 111 | info.platform_data = name; |
| 112 | strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 113 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 114 | client = i2c_new_device(adapter, &info); |
| 115 | if (client == NULL) { |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 116 | printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n"); |
| 117 | goto fail; |
| 118 | } |
| 119 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Let i2c-core delete that device on driver removal. |
| 122 | * This is safe because i2c-core holds the core_lock mutex for us. |
| 123 | */ |
Jean Delvare | 6f6b35e | 2009-10-04 22:53:46 +0200 | [diff] [blame] | 124 | list_add_tail(&client->detected, &wf_max6690_driver.clients); |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 125 | return client; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 126 | |
| 127 | fail: |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 128 | return NULL; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int wf_max6690_attach(struct i2c_adapter *adapter) |
| 132 | { |
| 133 | struct device_node *busnode, *dev = NULL; |
| 134 | struct pmac_i2c_bus *bus; |
| 135 | const char *loc; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 136 | |
| 137 | bus = pmac_i2c_adapter_to_bus(adapter); |
| 138 | if (bus == NULL) |
| 139 | return -ENODEV; |
| 140 | busnode = pmac_i2c_get_bus_node(bus); |
| 141 | |
| 142 | while ((dev = of_get_next_child(busnode, dev)) != NULL) { |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 143 | u8 addr; |
| 144 | |
| 145 | /* We must re-match the adapter in order to properly check |
| 146 | * the channel on multibus setups |
| 147 | */ |
| 148 | if (!pmac_i2c_match_adapter(dev, adapter)) |
| 149 | continue; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 150 | if (!of_device_is_compatible(dev, "max6690")) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 151 | continue; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 152 | addr = pmac_i2c_get_dev_addr(dev); |
Stephen Rothwell | 01b2726 | 2007-04-27 13:41:15 +1000 | [diff] [blame] | 153 | loc = of_get_property(dev, "hwsensor-location", NULL); |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 154 | if (loc == NULL || addr == 0) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 155 | continue; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 156 | printk("found max6690, loc=%s addr=0x%02x\n", loc, addr); |
Étienne Bersac | 80ff974 | 2008-04-29 15:39:55 +1000 | [diff] [blame] | 157 | wf_max6690_create(adapter, addr, loc); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 163 | static int wf_max6690_remove(struct i2c_client *client) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 164 | { |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 165 | struct wf_6690_sensor *max = i2c_get_clientdata(client); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 166 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 167 | max->i2c = NULL; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 168 | wf_unregister_sensor(&max->sens); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Jean Delvare | 351ca3e | 2009-06-15 18:01:51 +0200 | [diff] [blame] | 173 | static const struct i2c_device_id wf_max6690_id[] = { |
| 174 | { "wf_max6690", 0 }, |
| 175 | { } |
| 176 | }; |
| 177 | |
| 178 | static struct i2c_driver wf_max6690_driver = { |
| 179 | .driver = { |
| 180 | .name = "wf_max6690", |
| 181 | }, |
| 182 | .attach_adapter = wf_max6690_attach, |
| 183 | .probe = wf_max6690_probe, |
| 184 | .remove = wf_max6690_remove, |
| 185 | .id_table = wf_max6690_id, |
| 186 | }; |
| 187 | |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 188 | static int __init wf_max6690_sensor_init(void) |
| 189 | { |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 190 | /* Don't register on old machines that use therm_pm72 for now */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 191 | if (of_machine_is_compatible("PowerMac7,2") || |
| 192 | of_machine_is_compatible("PowerMac7,3") || |
| 193 | of_machine_is_compatible("RackMac3,1")) |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 194 | return -ENODEV; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 195 | return i2c_add_driver(&wf_max6690_driver); |
| 196 | } |
| 197 | |
| 198 | static void __exit wf_max6690_sensor_exit(void) |
| 199 | { |
| 200 | i2c_del_driver(&wf_max6690_driver); |
| 201 | } |
| 202 | |
| 203 | module_init(wf_max6690_sensor_init); |
| 204 | module_exit(wf_max6690_sensor_exit); |
| 205 | |
| 206 | MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>"); |
| 207 | MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control"); |
| 208 | MODULE_LICENSE("GPL"); |