1 /*
2  * Windfarm PowerMac thermal control. LM75 sensor
3  *
4  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5  *                    <benh@kernel.crashing.org>
6  *
7  * Released under the term of the GNU GPL v2.
8  */
9 
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
17 #include <linux/i2c.h>
18 #include <asm/prom.h>
19 #include <asm/machdep.h>
20 #include <asm/io.h>
21 #include <asm/sections.h>
22 #include <asm/pmac_low_i2c.h>
23 
24 #include "windfarm.h"
25 
26 #define VERSION "0.2"
27 
28 #undef DEBUG
29 
30 #ifdef DEBUG
31 #define DBG(args...)	printk(args)
32 #else
33 #define DBG(args...)	do { } while(0)
34 #endif
35 
36 struct wf_lm75_sensor {
37 	int			ds1775 : 1;
38 	int			inited : 1;
39 	struct 	i2c_client	*i2c;
40 	struct 	wf_sensor	sens;
41 };
42 #define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)
43 
44 static int wf_lm75_get(struct wf_sensor *sr, s32 *value)
45 {
46 	struct wf_lm75_sensor *lm = wf_to_lm75(sr);
47 	s32 data;
48 
49 	if (lm->i2c == NULL)
50 		return -ENODEV;
51 
52 	/* Init chip if necessary */
53 	if (!lm->inited) {
54 		u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);
55 
56 		DBG("wf_lm75: Initializing %s, cfg was: %02x\n",
57 		    sr->name, cfg);
58 
59 		/* clear shutdown bit, keep other settings as left by
60 		 * the firmware for now
61 		 */
62 		cfg_new = cfg & ~0x01;
63 		i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);
64 		lm->inited = 1;
65 
66 		/* If we just powered it up, let's wait 200 ms */
67 		msleep(200);
68 	}
69 
70 	/* Read temperature register */
71 	data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));
72 	data <<= 8;
73 	*value = data;
74 
75 	return 0;
76 }
77 
78 static void wf_lm75_release(struct wf_sensor *sr)
79 {
80 	struct wf_lm75_sensor *lm = wf_to_lm75(sr);
81 
82 	kfree(lm);
83 }
84 
85 static struct wf_sensor_ops wf_lm75_ops = {
86 	.get_value	= wf_lm75_get,
87 	.release	= wf_lm75_release,
88 	.owner		= THIS_MODULE,
89 };
90 
91 static int wf_lm75_probe(struct i2c_client *client,
92 			 const struct i2c_device_id *id)
93 {
94 	struct wf_lm75_sensor *lm;
95 	int rc;
96 
97 	lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
98 	if (lm == NULL)
99 		return -ENODEV;
100 
101 	lm->inited = 0;
102 	lm->ds1775 = id->driver_data;
103 	lm->i2c = client;
104 	lm->sens.name = client->dev.platform_data;
105 	lm->sens.ops = &wf_lm75_ops;
106 	i2c_set_clientdata(client, lm);
107 
108 	rc = wf_register_sensor(&lm->sens);
109 	if (rc)
110 		kfree(lm);
111 
112 	return rc;
113 }
114 
115 static struct i2c_driver wf_lm75_driver;
116 
117 static struct i2c_client *wf_lm75_create(struct i2c_adapter *adapter,
118 					     u8 addr, int ds1775,
119 					     const char *loc)
120 {
121 	struct i2c_board_info info;
122 	struct i2c_client *client;
123 	char *name;
124 
125 	DBG("wf_lm75: creating  %s device at address 0x%02x\n",
126 	    ds1775 ? "ds1775" : "lm75", addr);
127 
128 	/* Usual rant about sensor names not beeing very consistent in
129 	 * the device-tree, oh well ...
130 	 * Add more entries below as you deal with more setups
131 	 */
132 	if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))
133 		name = "hd-temp";
134 	else if (!strcmp(loc, "Incoming Air Temp"))
135 		name = "incoming-air-temp";
136 	else if (!strcmp(loc, "ODD Temp"))
137 		name = "optical-drive-temp";
138 	else if (!strcmp(loc, "HD Temp"))
139 		name = "hard-drive-temp";
140 	else
141 		goto fail;
142 
143 	memset(&info, 0, sizeof(struct i2c_board_info));
144 	info.addr = (addr >> 1) & 0x7f;
145 	info.platform_data = name;
146 	strlcpy(info.type, ds1775 ? "wf_ds1775" : "wf_lm75", I2C_NAME_SIZE);
147 
148 	client = i2c_new_device(adapter, &info);
149 	if (client == NULL) {
150 		printk(KERN_ERR "windfarm: failed to attach %s %s to i2c\n",
151 		       ds1775 ? "ds1775" : "lm75", name);
152 		goto fail;
153 	}
154 
155 	/*
156 	 * Let i2c-core delete that device on driver removal.
157 	 * This is safe because i2c-core holds the core_lock mutex for us.
158 	 */
159 	list_add_tail(&client->detected, &wf_lm75_driver.clients);
160 	return client;
161  fail:
162 	return NULL;
163 }
164 
165 static int wf_lm75_attach(struct i2c_adapter *adapter)
166 {
167 	struct device_node *busnode, *dev;
168 	struct pmac_i2c_bus *bus;
169 
170 	DBG("wf_lm75: adapter %s detected\n", adapter->name);
171 
172 	bus = pmac_i2c_adapter_to_bus(adapter);
173 	if (bus == NULL)
174 		return -ENODEV;
175 	busnode = pmac_i2c_get_bus_node(bus);
176 
177 	DBG("wf_lm75: bus found, looking for device...\n");
178 
179 	/* Now look for lm75(s) in there */
180 	for (dev = NULL;
181 	     (dev = of_get_next_child(busnode, dev)) != NULL;) {
182 		const char *loc =
183 			of_get_property(dev, "hwsensor-location", NULL);
184 		u8 addr;
185 
186 		/* We must re-match the adapter in order to properly check
187 		 * the channel on multibus setups
188 		 */
189 		if (!pmac_i2c_match_adapter(dev, adapter))
190 			continue;
191 		addr = pmac_i2c_get_dev_addr(dev);
192 		if (loc == NULL || addr == 0)
193 			continue;
194 		/* real lm75 */
195 		if (of_device_is_compatible(dev, "lm75"))
196 			wf_lm75_create(adapter, addr, 0, loc);
197 		/* ds1775 (compatible, better resolution */
198 		else if (of_device_is_compatible(dev, "ds1775"))
199 			wf_lm75_create(adapter, addr, 1, loc);
200 	}
201 	return 0;
202 }
203 
204 static int wf_lm75_remove(struct i2c_client *client)
205 {
206 	struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
207 
208 	DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
209 
210 	/* Mark client detached */
211 	lm->i2c = NULL;
212 
213 	/* release sensor */
214 	wf_unregister_sensor(&lm->sens);
215 
216 	return 0;
217 }
218 
219 static const struct i2c_device_id wf_lm75_id[] = {
220 	{ "wf_lm75", 0 },
221 	{ "wf_ds1775", 1 },
222 	{ }
223 };
224 
225 static struct i2c_driver wf_lm75_driver = {
226 	.driver = {
227 		.name	= "wf_lm75",
228 	},
229 	.attach_adapter	= wf_lm75_attach,
230 	.probe		= wf_lm75_probe,
231 	.remove		= wf_lm75_remove,
232 	.id_table	= wf_lm75_id,
233 };
234 
235 static int __init wf_lm75_sensor_init(void)
236 {
237 	/* Don't register on old machines that use therm_pm72 for now */
238 	if (of_machine_is_compatible("PowerMac7,2") ||
239 	    of_machine_is_compatible("PowerMac7,3") ||
240 	    of_machine_is_compatible("RackMac3,1"))
241 		return -ENODEV;
242 	return i2c_add_driver(&wf_lm75_driver);
243 }
244 
245 static void __exit wf_lm75_sensor_exit(void)
246 {
247 	i2c_del_driver(&wf_lm75_driver);
248 }
249 
250 
251 module_init(wf_lm75_sensor_init);
252 module_exit(wf_lm75_sensor_exit);
253 
254 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
255 MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
256 MODULE_LICENSE("GPL");
257 
258