Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / macintosh / windfarm_max6690_sensor.c
CommitLineData
ac171c46
BH
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>
ac171c46
BH
14#include <asm/prom.h>
15#include <asm/pmac_low_i2c.h>
16
17#include "windfarm.h"
18
b55fafc5 19#define VERSION "0.2"
ac171c46
BH
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
28struct wf_6690_sensor {
351ca3e3 29 struct i2c_client *i2c;
ac171c46
BH
30 struct wf_sensor sens;
31};
32
33#define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)
ac171c46
BH
34
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
351ca3e3 40 if (max->i2c == NULL)
ac171c46
BH
41 return -ENODEV;
42
43 /* chip gets initialized by firmware */
351ca3e3 44 data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
ac171c46
BH
45 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
ac171c46
BH
55 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
351ca3e3
JD
64static int wf_max6690_probe(struct i2c_client *client,
65 const struct i2c_device_id *id)
ac171c46
BH
66{
67 struct wf_6690_sensor *max;
351ca3e3 68 int rc;
ac171c46
BH
69
70 max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
71 if (max == NULL) {
351ca3e3
JD
72 printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
73 "no memory\n");
74 return -ENOMEM;
75 }
76
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);
ac171c46
BH
86 }
87
351ca3e3
JD
88 return rc;
89}
90
91static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter,
92 u8 addr, const char *loc)
93{
94 struct i2c_board_info info;
95 struct i2c_client *client;
96 char *name;
97
80ff974d
ÉB
98 if (!strcmp(loc, "BACKSIDE"))
99 name = "backside-temp";
100 else if (!strcmp(loc, "NB Ambient"))
101 name = "north-bridge-temp";
102 else if (!strcmp(loc, "GPU Ambient"))
103 name = "gpu-temp";
104 else
105 goto fail;
106
351ca3e3
JD
107 memset(&info, 0, sizeof(struct i2c_board_info));
108 info.addr = addr >> 1;
109 info.platform_data = name;
110 strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE);
ac171c46 111
351ca3e3
JD
112 client = i2c_new_device(adapter, &info);
113 if (client == NULL) {
ac171c46
BH
114 printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");
115 goto fail;
116 }
117
351ca3e3
JD
118 /*
119 * Let i2c-core delete that device on driver removal.
120 * This is safe because i2c-core holds the core_lock mutex for us.
121 */
122 list_add_tail(&client->detected, &client->driver->clients);
123 return client;
ac171c46
BH
124
125 fail:
351ca3e3 126 return NULL;
ac171c46
BH
127}
128
129static int wf_max6690_attach(struct i2c_adapter *adapter)
130{
131 struct device_node *busnode, *dev = NULL;
132 struct pmac_i2c_bus *bus;
133 const char *loc;
ac171c46
BH
134
135 bus = pmac_i2c_adapter_to_bus(adapter);
136 if (bus == NULL)
137 return -ENODEV;
138 busnode = pmac_i2c_get_bus_node(bus);
139
140 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
b55fafc5
BH
141 u8 addr;
142
143 /* We must re-match the adapter in order to properly check
144 * the channel on multibus setups
145 */
146 if (!pmac_i2c_match_adapter(dev, adapter))
147 continue;
55b61fec 148 if (!of_device_is_compatible(dev, "max6690"))
ac171c46 149 continue;
b55fafc5 150 addr = pmac_i2c_get_dev_addr(dev);
01b2726d 151 loc = of_get_property(dev, "hwsensor-location", NULL);
b55fafc5 152 if (loc == NULL || addr == 0)
ac171c46 153 continue;
b55fafc5 154 printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);
80ff974d 155 wf_max6690_create(adapter, addr, loc);
ac171c46
BH
156 }
157
158 return 0;
159}
160
351ca3e3 161static int wf_max6690_remove(struct i2c_client *client)
ac171c46 162{
351ca3e3 163 struct wf_6690_sensor *max = i2c_get_clientdata(client);
ac171c46 164
351ca3e3 165 max->i2c = NULL;
ac171c46
BH
166 wf_unregister_sensor(&max->sens);
167
168 return 0;
169}
170
351ca3e3
JD
171static const struct i2c_device_id wf_max6690_id[] = {
172 { "wf_max6690", 0 },
173 { }
174};
175
176static struct i2c_driver wf_max6690_driver = {
177 .driver = {
178 .name = "wf_max6690",
179 },
180 .attach_adapter = wf_max6690_attach,
181 .probe = wf_max6690_probe,
182 .remove = wf_max6690_remove,
183 .id_table = wf_max6690_id,
184};
185
ac171c46
BH
186static int __init wf_max6690_sensor_init(void)
187{
b55fafc5
BH
188 /* Don't register on old machines that use therm_pm72 for now */
189 if (machine_is_compatible("PowerMac7,2") ||
190 machine_is_compatible("PowerMac7,3") ||
191 machine_is_compatible("RackMac3,1"))
192 return -ENODEV;
ac171c46
BH
193 return i2c_add_driver(&wf_max6690_driver);
194}
195
196static void __exit wf_max6690_sensor_exit(void)
197{
198 i2c_del_driver(&wf_max6690_driver);
199}
200
201module_init(wf_max6690_sensor_init);
202module_exit(wf_max6690_sensor_exit);
203
204MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
205MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
206MODULE_LICENSE("GPL");