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> |
| 14 | #include <linux/i2c-dev.h> |
| 15 | #include <asm/prom.h> |
| 16 | #include <asm/pmac_low_i2c.h> |
| 17 | |
| 18 | #include "windfarm.h" |
| 19 | |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 20 | #define VERSION "0.2" |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 21 | |
| 22 | /* This currently only exports the external temperature sensor, |
| 23 | since that's all the control loops need. */ |
| 24 | |
| 25 | /* Some MAX6690 register numbers */ |
| 26 | #define MAX6690_INTERNAL_TEMP 0 |
| 27 | #define MAX6690_EXTERNAL_TEMP 1 |
| 28 | |
| 29 | struct wf_6690_sensor { |
| 30 | struct i2c_client i2c; |
| 31 | struct wf_sensor sens; |
| 32 | }; |
| 33 | |
| 34 | #define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens) |
| 35 | #define i2c_to_6690(x) container_of((x), struct wf_6690_sensor, i2c) |
| 36 | |
| 37 | static int wf_max6690_attach(struct i2c_adapter *adapter); |
| 38 | static int wf_max6690_detach(struct i2c_client *client); |
| 39 | |
| 40 | static struct i2c_driver wf_max6690_driver = { |
| 41 | .driver = { |
| 42 | .name = "wf_max6690", |
| 43 | }, |
| 44 | .attach_adapter = wf_max6690_attach, |
| 45 | .detach_client = wf_max6690_detach, |
| 46 | }; |
| 47 | |
| 48 | static int wf_max6690_get(struct wf_sensor *sr, s32 *value) |
| 49 | { |
| 50 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 51 | s32 data; |
| 52 | |
| 53 | if (max->i2c.adapter == NULL) |
| 54 | return -ENODEV; |
| 55 | |
| 56 | /* chip gets initialized by firmware */ |
| 57 | data = i2c_smbus_read_byte_data(&max->i2c, MAX6690_EXTERNAL_TEMP); |
| 58 | if (data < 0) |
| 59 | return data; |
| 60 | *value = data << 16; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void wf_max6690_release(struct wf_sensor *sr) |
| 65 | { |
| 66 | struct wf_6690_sensor *max = wf_to_6690(sr); |
| 67 | |
| 68 | if (max->i2c.adapter) { |
| 69 | i2c_detach_client(&max->i2c); |
| 70 | max->i2c.adapter = NULL; |
| 71 | } |
| 72 | kfree(max); |
| 73 | } |
| 74 | |
| 75 | static struct wf_sensor_ops wf_max6690_ops = { |
| 76 | .get_value = wf_max6690_get, |
| 77 | .release = wf_max6690_release, |
| 78 | .owner = THIS_MODULE, |
| 79 | }; |
| 80 | |
| 81 | static void wf_max6690_create(struct i2c_adapter *adapter, u8 addr) |
| 82 | { |
| 83 | struct wf_6690_sensor *max; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 84 | char *name = "backside-temp"; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 85 | |
| 86 | max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL); |
| 87 | if (max == NULL) { |
| 88 | printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor %s: " |
| 89 | "no memory\n", name); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | max->sens.ops = &wf_max6690_ops; |
| 94 | max->sens.name = name; |
| 95 | max->i2c.addr = addr >> 1; |
| 96 | max->i2c.adapter = adapter; |
| 97 | max->i2c.driver = &wf_max6690_driver; |
| 98 | strncpy(max->i2c.name, name, I2C_NAME_SIZE-1); |
| 99 | |
| 100 | if (i2c_attach_client(&max->i2c)) { |
| 101 | printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n"); |
| 102 | goto fail; |
| 103 | } |
| 104 | |
| 105 | if (wf_register_sensor(&max->sens)) { |
| 106 | i2c_detach_client(&max->i2c); |
| 107 | goto fail; |
| 108 | } |
| 109 | |
| 110 | return; |
| 111 | |
| 112 | fail: |
| 113 | kfree(max); |
| 114 | } |
| 115 | |
| 116 | static int wf_max6690_attach(struct i2c_adapter *adapter) |
| 117 | { |
| 118 | struct device_node *busnode, *dev = NULL; |
| 119 | struct pmac_i2c_bus *bus; |
| 120 | const char *loc; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 121 | |
| 122 | bus = pmac_i2c_adapter_to_bus(adapter); |
| 123 | if (bus == NULL) |
| 124 | return -ENODEV; |
| 125 | busnode = pmac_i2c_get_bus_node(bus); |
| 126 | |
| 127 | while ((dev = of_get_next_child(busnode, dev)) != NULL) { |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 128 | u8 addr; |
| 129 | |
| 130 | /* We must re-match the adapter in order to properly check |
| 131 | * the channel on multibus setups |
| 132 | */ |
| 133 | if (!pmac_i2c_match_adapter(dev, adapter)) |
| 134 | continue; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 135 | if (!device_is_compatible(dev, "max6690")) |
| 136 | continue; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 137 | addr = pmac_i2c_get_dev_addr(dev); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 138 | loc = get_property(dev, "hwsensor-location", NULL); |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 139 | if (loc == NULL || addr == 0) |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 140 | continue; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 141 | printk("found max6690, loc=%s addr=0x%02x\n", loc, addr); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 142 | if (strcmp(loc, "BACKSIDE")) |
| 143 | continue; |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 144 | wf_max6690_create(adapter, addr); |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int wf_max6690_detach(struct i2c_client *client) |
| 151 | { |
| 152 | struct wf_6690_sensor *max = i2c_to_6690(client); |
| 153 | |
| 154 | max->i2c.adapter = NULL; |
| 155 | wf_unregister_sensor(&max->sens); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int __init wf_max6690_sensor_init(void) |
| 161 | { |
Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 162 | /* Don't register on old machines that use therm_pm72 for now */ |
| 163 | if (machine_is_compatible("PowerMac7,2") || |
| 164 | machine_is_compatible("PowerMac7,3") || |
| 165 | machine_is_compatible("RackMac3,1")) |
| 166 | return -ENODEV; |
Benjamin Herrenschmidt | ac171c4 | 2006-02-08 16:42:51 +1100 | [diff] [blame] | 167 | return i2c_add_driver(&wf_max6690_driver); |
| 168 | } |
| 169 | |
| 170 | static void __exit wf_max6690_sensor_exit(void) |
| 171 | { |
| 172 | i2c_del_driver(&wf_max6690_driver); |
| 173 | } |
| 174 | |
| 175 | module_init(wf_max6690_sensor_init); |
| 176 | module_exit(wf_max6690_sensor_exit); |
| 177 | |
| 178 | MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>"); |
| 179 | MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control"); |
| 180 | MODULE_LICENSE("GPL"); |