blob: 8204113268f471db6021ffa7915be03272a0c40c [file] [log] [blame]
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +11001/*
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 Herrenschmidtac171c42006-02-08 16:42:51 +110014#include <asm/prom.h>
15#include <asm/pmac_low_i2c.h>
16
17#include "windfarm.h"
18
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +110019#define VERSION "0.2"
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110020
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
28struct wf_6690_sensor {
Jean Delvare351ca3e2009-06-15 18:01:51 +020029 struct i2c_client *i2c;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110030 struct wf_sensor sens;
31};
32
33#define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110034
35static 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 Delvare351ca3e2009-06-15 18:01:51 +020040 if (max->i2c == NULL)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110041 return -ENODEV;
42
43 /* chip gets initialized by firmware */
Jean Delvare351ca3e2009-06-15 18:01:51 +020044 data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110045 if (data < 0)
46 return data;
47 *value = data << 16;
48 return 0;
49}
50
51static void wf_max6690_release(struct wf_sensor *sr)
52{
53 struct wf_6690_sensor *max = wf_to_6690(sr);
54
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110055 kfree(max);
56}
57
58static 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 Delvare351ca3e2009-06-15 18:01:51 +020064static int wf_max6690_probe(struct i2c_client *client,
65 const struct i2c_device_id *id)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110066{
67 struct wf_6690_sensor *max;
Jean Delvare351ca3e2009-06-15 18:01:51 +020068 int rc;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110069
70 max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
71 if (max == NULL) {
Jean Delvare351ca3e2009-06-15 18:01:51 +020072 printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
73 "no memory\n");
74 return -ENOMEM;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +110075 }
76
Jean Delvare351ca3e2009-06-15 18:01:51 +020077 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) {
Jean Delvare351ca3e2009-06-15 18:01:51 +020084 kfree(max);
85 }
86
87 return rc;
88}
89
Jean Delvare6f6b35e2009-10-04 22:53:46 +020090static struct i2c_driver wf_max6690_driver;
91
Jean Delvare351ca3e2009-06-15 18:01:51 +020092static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter,
93 u8 addr, const char *loc)
94{
95 struct i2c_board_info info;
96 struct i2c_client *client;
97 char *name;
98
Étienne Bersac80ff9742008-04-29 15:39:55 +100099 if (!strcmp(loc, "BACKSIDE"))
100 name = "backside-temp";
101 else if (!strcmp(loc, "NB Ambient"))
102 name = "north-bridge-temp";
103 else if (!strcmp(loc, "GPU Ambient"))
104 name = "gpu-temp";
105 else
106 goto fail;
107
Jean Delvare351ca3e2009-06-15 18:01:51 +0200108 memset(&info, 0, sizeof(struct i2c_board_info));
109 info.addr = addr >> 1;
110 info.platform_data = name;
111 strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100112
Jean Delvare351ca3e2009-06-15 18:01:51 +0200113 client = i2c_new_device(adapter, &info);
114 if (client == NULL) {
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100115 printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
116 goto fail;
117 }
118
Jean Delvare351ca3e2009-06-15 18:01:51 +0200119 /*
120 * Let i2c-core delete that device on driver removal.
121 * This is safe because i2c-core holds the core_lock mutex for us.
122 */
Jean Delvare6f6b35e2009-10-04 22:53:46 +0200123 list_add_tail(&client->detected, &wf_max6690_driver.clients);
Jean Delvare351ca3e2009-06-15 18:01:51 +0200124 return client;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100125
126 fail:
Jean Delvare351ca3e2009-06-15 18:01:51 +0200127 return NULL;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100128}
129
130static int wf_max6690_attach(struct i2c_adapter *adapter)
131{
132 struct device_node *busnode, *dev = NULL;
133 struct pmac_i2c_bus *bus;
134 const char *loc;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100135
136 bus = pmac_i2c_adapter_to_bus(adapter);
137 if (bus == NULL)
138 return -ENODEV;
139 busnode = pmac_i2c_get_bus_node(bus);
140
141 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100142 u8 addr;
143
144 /* We must re-match the adapter in order to properly check
145 * the channel on multibus setups
146 */
147 if (!pmac_i2c_match_adapter(dev, adapter))
148 continue;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000149 if (!of_device_is_compatible(dev, "max6690"))
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100150 continue;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100151 addr = pmac_i2c_get_dev_addr(dev);
Stephen Rothwell01b27262007-04-27 13:41:15 +1000152 loc = of_get_property(dev, "hwsensor-location", NULL);
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100153 if (loc == NULL || addr == 0)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100154 continue;
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100155 printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
Étienne Bersac80ff9742008-04-29 15:39:55 +1000156 wf_max6690_create(adapter, addr, loc);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100157 }
158
159 return 0;
160}
161
Jean Delvare351ca3e2009-06-15 18:01:51 +0200162static int wf_max6690_remove(struct i2c_client *client)
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100163{
Jean Delvare351ca3e2009-06-15 18:01:51 +0200164 struct wf_6690_sensor *max = i2c_get_clientdata(client);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100165
Jean Delvare351ca3e2009-06-15 18:01:51 +0200166 max->i2c = NULL;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100167 wf_unregister_sensor(&max->sens);
168
169 return 0;
170}
171
Jean Delvare351ca3e2009-06-15 18:01:51 +0200172static const struct i2c_device_id wf_max6690_id[] = {
173 { "wf_max6690", 0 },
174 { }
175};
176
177static struct i2c_driver wf_max6690_driver = {
178 .driver = {
179 .name = "wf_max6690",
180 },
181 .attach_adapter = wf_max6690_attach,
182 .probe = wf_max6690_probe,
183 .remove = wf_max6690_remove,
184 .id_table = wf_max6690_id,
185};
186
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100187static int __init wf_max6690_sensor_init(void)
188{
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100189 /* Don't register on old machines that use therm_pm72 for now */
Grant Likely71a157e2010-02-01 21:34:14 -0700190 if (of_machine_is_compatible("PowerMac7,2") ||
191 of_machine_is_compatible("PowerMac7,3") ||
192 of_machine_is_compatible("RackMac3,1"))
Benjamin Herrenschmidtb55fafc2006-03-03 17:03:21 +1100193 return -ENODEV;
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100194 return i2c_add_driver(&wf_max6690_driver);
195}
196
197static void __exit wf_max6690_sensor_exit(void)
198{
199 i2c_del_driver(&wf_max6690_driver);
200}
201
202module_init(wf_max6690_sensor_init);
203module_exit(wf_max6690_sensor_exit);
204
205MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
206MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
207MODULE_LICENSE("GPL");