Blame view

drivers/macintosh/windfarm_smu_sat.c 9.22 KB
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors.
   *
   * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
   *
   * Released under the terms of the GNU GPL v2.
   */
  
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/init.h>
  #include <linux/wait.h>
  #include <linux/i2c.h>
56783c5e4   Daniel Walker   [POWERPC] macinto...
16
  #include <linux/mutex.h>
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <asm/prom.h>
  #include <asm/smu.h>
  #include <asm/pmac_low_i2c.h>
  
  #include "windfarm.h"
  
  #define VERSION "0.2"
  
  #define DEBUG
  
  #ifdef DEBUG
  #define DBG(args...)	printk(args)
  #else
  #define DBG(args...)	do { } while(0)
  #endif
  
  /* If the cache is older than 800ms we'll refetch it */
  #define MAX_AGE		msecs_to_jiffies(800)
  
  struct wf_sat {
  	int			nr;
  	atomic_t		refcnt;
56783c5e4   Daniel Walker   [POWERPC] macinto...
39
  	struct mutex		mutex;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
40
41
  	unsigned long		last_read; /* jiffies when cache last updated */
  	u8			cache[16];
351ca3e31   Jean Delvare   windfarm: Convert...
42
  	struct i2c_client	*i2c;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  	struct device_node	*node;
  };
  
  static struct wf_sat *sats[2];
  
  struct wf_sat_sensor {
  	int		index;
  	int		index2;		/* used for power sensors */
  	int		shift;
  	struct wf_sat	*sat;
  	struct wf_sensor sens;
  };
  
  #define wf_to_sat(c)	container_of(c, struct wf_sat_sensor, sens)
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
57

ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
58
59
60
61
62
63
64
65
66
67
68
69
70
  struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
  						  unsigned int *size)
  {
  	struct wf_sat *sat;
  	int err;
  	unsigned int i, len;
  	u8 *buf;
  	u8 data[4];
  
  	/* TODO: Add the resulting partition to the device-tree */
  
  	if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
  		return NULL;
351ca3e31   Jean Delvare   windfarm: Convert...
71
  	err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
72
73
74
75
76
  	if (err) {
  		printk(KERN_ERR "smu_sat_get_sdb_part wr error %d
  ", err);
  		return NULL;
  	}
351ca3e31   Jean Delvare   windfarm: Convert...
77
  	err = i2c_smbus_read_word_data(sat->i2c, 9);
2fd091f3e   roel kluin   powerpc/macintosh...
78
  	if (err < 0) {
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
79
80
81
82
  		printk(KERN_ERR "smu_sat_get_sdb_part rd len error
  ");
  		return NULL;
  	}
2fd091f3e   roel kluin   powerpc/macintosh...
83
  	len = err;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
84
85
86
87
88
89
90
91
92
93
94
95
96
  	if (len == 0) {
  		printk(KERN_ERR "smu_sat_get_sdb_part no partition %x
  ", id);
  		return NULL;
  	}
  
  	len = le16_to_cpu(len);
  	len = (len + 3) & ~3;
  	buf = kmalloc(len, GFP_KERNEL);
  	if (buf == NULL)
  		return NULL;
  
  	for (i = 0; i < len; i += 4) {
351ca3e31   Jean Delvare   windfarm: Convert...
97
  		err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
4b2643d7d   Jean Delvare   i2c: Fix the i2c_...
98
  		if (err < 0) {
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  			printk(KERN_ERR "smu_sat_get_sdb_part rd err %d
  ",
  			       err);
  			goto fail;
  		}
  		buf[i] = data[1];
  		buf[i+1] = data[0];
  		buf[i+2] = data[3];
  		buf[i+3] = data[2];
  	}
  #ifdef DEBUG
  	DBG(KERN_DEBUG "sat %d partition %x:", sat_id, id);
  	for (i = 0; i < len; ++i)
  		DBG(" %x", buf[i]);
  	DBG("
  ");
  #endif
  
  	if (size)
  		*size = len;
  	return (struct smu_sdbp_header *) buf;
  
   fail:
  	kfree(buf);
  	return NULL;
  }
9127dd1aa   Johannes Berg   [PATCH] allow win...
125
  EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
126
127
128
129
130
  
  /* refresh the cache */
  static int wf_sat_read_cache(struct wf_sat *sat)
  {
  	int err;
351ca3e31   Jean Delvare   windfarm: Convert...
131
  	err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
4b2643d7d   Jean Delvare   i2c: Fix the i2c_...
132
  	if (err < 0)
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  		return err;
  	sat->last_read = jiffies;
  #ifdef LOTSA_DEBUG
  	{
  		int i;
  		DBG(KERN_DEBUG "wf_sat_get: data is");
  		for (i = 0; i < 16; ++i)
  			DBG(" %.2x", sat->cache[i]);
  		DBG("
  ");
  	}
  #endif
  	return 0;
  }
  
  static int wf_sat_get(struct wf_sensor *sr, s32 *value)
  {
  	struct wf_sat_sensor *sens = wf_to_sat(sr);
  	struct wf_sat *sat = sens->sat;
  	int i, err;
  	s32 val;
351ca3e31   Jean Delvare   windfarm: Convert...
154
  	if (sat->i2c == NULL)
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
155
  		return -ENODEV;
56783c5e4   Daniel Walker   [POWERPC] macinto...
156
  	mutex_lock(&sat->mutex);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  	if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
  		err = wf_sat_read_cache(sat);
  		if (err)
  			goto fail;
  	}
  
  	i = sens->index * 2;
  	val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
  	if (sens->index2 >= 0) {
  		i = sens->index2 * 2;
  		/* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
  		val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
  	}
  
  	*value = val;
  	err = 0;
  
   fail:
56783c5e4   Daniel Walker   [POWERPC] macinto...
175
  	mutex_unlock(&sat->mutex);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
176
177
178
179
180
181
182
183
184
  	return err;
  }
  
  static void wf_sat_release(struct wf_sensor *sr)
  {
  	struct wf_sat_sensor *sens = wf_to_sat(sr);
  	struct wf_sat *sat = sens->sat;
  
  	if (atomic_dec_and_test(&sat->refcnt)) {
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
185
186
187
188
189
190
191
192
193
194
195
196
  		if (sat->nr >= 0)
  			sats[sat->nr] = NULL;
  		kfree(sat);
  	}
  	kfree(sens);
  }
  
  static struct wf_sensor_ops wf_sat_ops = {
  	.get_value	= wf_sat_get,
  	.release	= wf_sat_release,
  	.owner		= THIS_MODULE,
  };
6f6b35e13   Jean Delvare   macintosh: Don't ...
197
  static struct i2c_driver wf_sat_driver;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
198
199
  static void wf_sat_create(struct i2c_adapter *adapter, struct device_node *dev)
  {
351ca3e31   Jean Delvare   windfarm: Convert...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  	struct i2c_board_info info;
  	struct i2c_client *client;
  	const u32 *reg;
  	u8 addr;
  
  	reg = of_get_property(dev, "reg", NULL);
  	if (reg == NULL)
  		return;
  	addr = *reg;
  	DBG(KERN_DEBUG "wf_sat: creating sat at address %x
  ", addr);
  
  	memset(&info, 0, sizeof(struct i2c_board_info));
  	info.addr = (addr >> 1) & 0x7f;
  	info.platform_data = dev;
  	strlcpy(info.type, "wf_sat", I2C_NAME_SIZE);
  
  	client = i2c_new_device(adapter, &info);
  	if (client == NULL) {
  		printk(KERN_ERR "windfarm: failed to attach smu-sat to i2c
  ");
  		return;
  	}
  
  	/*
  	 * Let i2c-core delete that device on driver removal.
  	 * This is safe because i2c-core holds the core_lock mutex for us.
  	 */
6f6b35e13   Jean Delvare   macintosh: Don't ...
228
  	list_add_tail(&client->detected, &wf_sat_driver.clients);
351ca3e31   Jean Delvare   windfarm: Convert...
229
230
231
232
233
234
  }
  
  static int wf_sat_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
  	struct device_node *dev = client->dev.platform_data;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
235
236
  	struct wf_sat *sat;
  	struct wf_sat_sensor *sens;
018a3d1db   Jeremy Kerr   [POWERPC] powerma...
237
238
  	const u32 *reg;
  	const char *loc, *type;
351ca3e31   Jean Delvare   windfarm: Convert...
239
  	u8 chip, core;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
240
241
242
243
  	struct device_node *child;
  	int shift, cpu, index;
  	char *name;
  	int vsens[2], isens[2];
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
244
245
  	sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
  	if (sat == NULL)
351ca3e31   Jean Delvare   windfarm: Convert...
246
  		return -ENOMEM;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
247
248
249
  	sat->nr = -1;
  	sat->node = of_node_get(dev);
  	atomic_set(&sat->refcnt, 0);
56783c5e4   Daniel Walker   [POWERPC] macinto...
250
  	mutex_init(&sat->mutex);
351ca3e31   Jean Delvare   windfarm: Convert...
251
252
  	sat->i2c = client;
  	i2c_set_clientdata(client, sat);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
253
254
255
256
257
  
  	vsens[0] = vsens[1] = -1;
  	isens[0] = isens[1] = -1;
  	child = NULL;
  	while ((child = of_get_next_child(dev, child)) != NULL) {
01b2726dd   Stephen Rothwell   [POWERPC] Rename ...
258
259
260
  		reg = of_get_property(child, "reg", NULL);
  		type = of_get_property(child, "device_type", NULL);
  		loc = of_get_property(child, "location", NULL);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  		if (reg == NULL || loc == NULL)
  			continue;
  
  		/* the cooked sensors are between 0x30 and 0x37 */
  		if (*reg < 0x30 || *reg > 0x37)
  			continue;
  		index = *reg - 0x30;
  
  		/* expect location to be CPU [AB][01] ... */
  		if (strncmp(loc, "CPU ", 4) != 0)
  			continue;
  		chip = loc[4] - 'A';
  		core = loc[5] - '0';
  		if (chip > 1 || core > 1) {
  			printk(KERN_ERR "wf_sat_create: don't understand "
  			       "location %s for %s
  ", loc, child->full_name);
  			continue;
  		}
  		cpu = 2 * chip + core;
  		if (sat->nr < 0)
  			sat->nr = chip;
  		else if (sat->nr != chip) {
  			printk(KERN_ERR "wf_sat_create: can't cope with "
  			       "multiple CPU chips on one SAT (%s)
  ", loc);
  			continue;
  		}
  
  		if (strcmp(type, "voltage-sensor") == 0) {
  			name = "cpu-voltage";
  			shift = 4;
  			vsens[core] = index;
  		} else if (strcmp(type, "current-sensor") == 0) {
  			name = "cpu-current";
  			shift = 8;
  			isens[core] = index;
  		} else if (strcmp(type, "temp-sensor") == 0) {
  			name = "cpu-temp";
  			shift = 10;
  		} else
  			continue;	/* hmmm shouldn't happen */
  
  		/* the +16 is enough for "cpu-voltage-n" */
  		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  		if (sens == NULL) {
  			printk(KERN_ERR "wf_sat_create: couldn't create "
  			       "%s sensor %d (no memory)
  ", name, cpu);
  			continue;
  		}
  		sens->index = index;
  		sens->index2 = -1;
  		sens->shift = shift;
  		sens->sat = sat;
  		atomic_inc(&sat->refcnt);
  		sens->sens.ops = &wf_sat_ops;
  		sens->sens.name = (char *) (sens + 1);
  		snprintf(sens->sens.name, 16, "%s-%d", name, cpu);
  
  		if (wf_register_sensor(&sens->sens)) {
  			atomic_dec(&sat->refcnt);
  			kfree(sens);
  		}
  	}
  
  	/* make the power sensors */
  	for (core = 0; core < 2; ++core) {
  		if (vsens[core] < 0 || isens[core] < 0)
  			continue;
  		cpu = 2 * sat->nr + core;
  		sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
  		if (sens == NULL) {
  			printk(KERN_ERR "wf_sat_create: couldn't create power "
  			       "sensor %d (no memory)
  ", cpu);
  			continue;
  		}
  		sens->index = vsens[core];
  		sens->index2 = isens[core];
  		sens->shift = 0;
  		sens->sat = sat;
  		atomic_inc(&sat->refcnt);
  		sens->sens.ops = &wf_sat_ops;
  		sens->sens.name = (char *) (sens + 1);
  		snprintf(sens->sens.name, 16, "cpu-power-%d", cpu);
  
  		if (wf_register_sensor(&sens->sens)) {
  			atomic_dec(&sat->refcnt);
  			kfree(sens);
  		}
  	}
  
  	if (sat->nr >= 0)
  		sats[sat->nr] = sat;
351ca3e31   Jean Delvare   windfarm: Convert...
356
  	return 0;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
357
358
359
360
361
362
363
364
365
366
367
368
369
  }
  
  static int wf_sat_attach(struct i2c_adapter *adapter)
  {
  	struct device_node *busnode, *dev = NULL;
  	struct pmac_i2c_bus *bus;
  
  	bus = pmac_i2c_adapter_to_bus(adapter);
  	if (bus == NULL)
  		return -ENODEV;
  	busnode = pmac_i2c_get_bus_node(bus);
  
  	while ((dev = of_get_next_child(busnode, dev)) != NULL)
55b61fec2   Stephen Rothwell   [POWERPC] Rename ...
370
  		if (of_device_is_compatible(dev, "smu-sat"))
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
371
372
373
  			wf_sat_create(adapter, dev);
  	return 0;
  }
351ca3e31   Jean Delvare   windfarm: Convert...
374
  static int wf_sat_remove(struct i2c_client *client)
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
375
  {
351ca3e31   Jean Delvare   windfarm: Convert...
376
  	struct wf_sat *sat = i2c_get_clientdata(client);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
377
378
  
  	/* XXX TODO */
351ca3e31   Jean Delvare   windfarm: Convert...
379
  	sat->i2c = NULL;
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
380
381
  	return 0;
  }
351ca3e31   Jean Delvare   windfarm: Convert...
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  static const struct i2c_device_id wf_sat_id[] = {
  	{ "wf_sat", 0 },
  	{ }
  };
  
  static struct i2c_driver wf_sat_driver = {
  	.driver = {
  		.name		= "wf_smu_sat",
  	},
  	.attach_adapter	= wf_sat_attach,
  	.probe		= wf_sat_probe,
  	.remove		= wf_sat_remove,
  	.id_table	= wf_sat_id,
  };
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
396
397
  static int __init sat_sensors_init(void)
  {
92e9a8507   Alexey Dobriyan   [PATCH] windfarm_...
398
  	return i2c_add_driver(&wf_sat_driver);
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
399
  }
a05afe914   Stephen Rothwell   [POWERPC] Comment...
400
  #if 0	/* uncomment when module_exit() below is uncommented */
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
401
402
403
404
  static void __exit sat_sensors_exit(void)
  {
  	i2c_del_driver(&wf_sat_driver);
  }
a05afe914   Stephen Rothwell   [POWERPC] Comment...
405
  #endif
ac171c466   Benjamin Herrenschmidt   [PATCH] powerpc: ...
406
407
408
409
410
411
412
  
  module_init(sat_sensors_init);
  /*module_exit(sat_sensors_exit); Uncomment when cleanup is implemented */
  
  MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
  MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
  MODULE_LICENSE("GPL");