GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / macintosh / windfarm_smu_sat.c
1 /*
2  * Windfarm PowerMac thermal control.  SMU "satellite" controller sensors.
3  *
4  * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
5  *
6  * Released under the terms of the GNU GPL v2.
7  */
8
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
17 #include <asm/prom.h>
18 #include <asm/smu.h>
19 #include <asm/pmac_low_i2c.h>
20
21 #include "windfarm.h"
22
23 #define VERSION "1.0"
24
25 /* If the cache is older than 800ms we'll refetch it */
26 #define MAX_AGE         msecs_to_jiffies(800)
27
28 struct wf_sat {
29         struct kref             ref;
30         int                     nr;
31         struct mutex            mutex;
32         unsigned long           last_read; /* jiffies when cache last updated */
33         u8                      cache[16];
34         struct list_head        sensors;
35         struct i2c_client       *i2c;
36         struct device_node      *node;
37 };
38
39 static struct wf_sat *sats[2];
40
41 struct wf_sat_sensor {
42         struct list_head        link;
43         int                     index;
44         int                     index2;         /* used for power sensors */
45         int                     shift;
46         struct wf_sat           *sat;
47         struct wf_sensor        sens;
48 };
49
50 #define wf_to_sat(c)    container_of(c, struct wf_sat_sensor, sens)
51
52 struct smu_sdbp_header *smu_sat_get_sdb_partition(unsigned int sat_id, int id,
53                                                   unsigned int *size)
54 {
55         struct wf_sat *sat;
56         int err;
57         unsigned int i, len;
58         u8 *buf;
59         u8 data[4];
60
61         /* TODO: Add the resulting partition to the device-tree */
62
63         if (sat_id > 1 || (sat = sats[sat_id]) == NULL)
64                 return NULL;
65
66         err = i2c_smbus_write_word_data(sat->i2c, 8, id << 8);
67         if (err) {
68                 printk(KERN_ERR "smu_sat_get_sdb_part wr error %d\n", err);
69                 return NULL;
70         }
71
72         err = i2c_smbus_read_word_data(sat->i2c, 9);
73         if (err < 0) {
74                 printk(KERN_ERR "smu_sat_get_sdb_part rd len error\n");
75                 return NULL;
76         }
77         len = err;
78         if (len == 0) {
79                 printk(KERN_ERR "smu_sat_get_sdb_part no partition %x\n", id);
80                 return NULL;
81         }
82
83         len = le16_to_cpu(len);
84         len = (len + 3) & ~3;
85         buf = kmalloc(len, GFP_KERNEL);
86         if (buf == NULL)
87                 return NULL;
88
89         for (i = 0; i < len; i += 4) {
90                 err = i2c_smbus_read_i2c_block_data(sat->i2c, 0xa, 4, data);
91                 if (err < 0) {
92                         printk(KERN_ERR "smu_sat_get_sdb_part rd err %d\n",
93                                err);
94                         goto fail;
95                 }
96                 buf[i] = data[1];
97                 buf[i+1] = data[0];
98                 buf[i+2] = data[3];
99                 buf[i+3] = data[2];
100         }
101
102         printk(KERN_DEBUG "sat %d partition %x:", sat_id, id);
103         print_hex_dump(KERN_DEBUG, "  ", DUMP_PREFIX_OFFSET,
104                        16, 1, buf, len, false);
105         if (size)
106                 *size = len;
107         return (struct smu_sdbp_header *) buf;
108
109  fail:
110         kfree(buf);
111         return NULL;
112 }
113 EXPORT_SYMBOL_GPL(smu_sat_get_sdb_partition);
114
115 /* refresh the cache */
116 static int wf_sat_read_cache(struct wf_sat *sat)
117 {
118         int err;
119
120         err = i2c_smbus_read_i2c_block_data(sat->i2c, 0x3f, 16, sat->cache);
121         if (err < 0)
122                 return err;
123         sat->last_read = jiffies;
124
125 #ifdef LOTSA_DEBUG
126         {
127                 int i;
128                 printk(KERN_DEBUG "wf_sat_get: data is");
129                 print_hex_dump(KERN_DEBUG, "  ", DUMP_PREFIX_OFFSET,
130                                16, 1, sat->cache, 16, false);
131         }
132 #endif
133         return 0;
134 }
135
136 static int wf_sat_sensor_get(struct wf_sensor *sr, s32 *value)
137 {
138         struct wf_sat_sensor *sens = wf_to_sat(sr);
139         struct wf_sat *sat = sens->sat;
140         int i, err;
141         s32 val;
142
143         if (sat->i2c == NULL)
144                 return -ENODEV;
145
146         mutex_lock(&sat->mutex);
147         if (time_after(jiffies, (sat->last_read + MAX_AGE))) {
148                 err = wf_sat_read_cache(sat);
149                 if (err)
150                         goto fail;
151         }
152
153         i = sens->index * 2;
154         val = ((sat->cache[i] << 8) + sat->cache[i+1]) << sens->shift;
155         if (sens->index2 >= 0) {
156                 i = sens->index2 * 2;
157                 /* 4.12 * 8.8 -> 12.20; shift right 4 to get 16.16 */
158                 val = (val * ((sat->cache[i] << 8) + sat->cache[i+1])) >> 4;
159         }
160
161         *value = val;
162         err = 0;
163
164  fail:
165         mutex_unlock(&sat->mutex);
166         return err;
167 }
168
169 static void wf_sat_release(struct kref *ref)
170 {
171         struct wf_sat *sat = container_of(ref, struct wf_sat, ref);
172
173         if (sat->nr >= 0)
174                 sats[sat->nr] = NULL;
175         of_node_put(sat->node);
176         kfree(sat);
177 }
178
179 static void wf_sat_sensor_release(struct wf_sensor *sr)
180 {
181         struct wf_sat_sensor *sens = wf_to_sat(sr);
182         struct wf_sat *sat = sens->sat;
183
184         kfree(sens);
185         kref_put(&sat->ref, wf_sat_release);
186 }
187
188 static const struct wf_sensor_ops wf_sat_ops = {
189         .get_value      = wf_sat_sensor_get,
190         .release        = wf_sat_sensor_release,
191         .owner          = THIS_MODULE,
192 };
193
194 static int wf_sat_probe(struct i2c_client *client,
195                         const struct i2c_device_id *id)
196 {
197         struct device_node *dev = client->dev.of_node;
198         struct wf_sat *sat;
199         struct wf_sat_sensor *sens;
200         const u32 *reg;
201         const char *loc, *type;
202         u8 chip, core;
203         struct device_node *child;
204         int shift, cpu, index;
205         char *name;
206         int vsens[2], isens[2];
207
208         sat = kzalloc(sizeof(struct wf_sat), GFP_KERNEL);
209         if (sat == NULL)
210                 return -ENOMEM;
211         sat->nr = -1;
212         sat->node = of_node_get(dev);
213         kref_init(&sat->ref);
214         mutex_init(&sat->mutex);
215         sat->i2c = client;
216         INIT_LIST_HEAD(&sat->sensors);
217         i2c_set_clientdata(client, sat);
218
219         vsens[0] = vsens[1] = -1;
220         isens[0] = isens[1] = -1;
221         child = NULL;
222         while ((child = of_get_next_child(dev, child)) != NULL) {
223                 reg = of_get_property(child, "reg", NULL);
224                 type = of_get_property(child, "device_type", NULL);
225                 loc = of_get_property(child, "location", NULL);
226                 if (reg == NULL || loc == NULL)
227                         continue;
228
229                 /* the cooked sensors are between 0x30 and 0x37 */
230                 if (*reg < 0x30 || *reg > 0x37)
231                         continue;
232                 index = *reg - 0x30;
233
234                 /* expect location to be CPU [AB][01] ... */
235                 if (strncmp(loc, "CPU ", 4) != 0)
236                         continue;
237                 chip = loc[4] - 'A';
238                 core = loc[5] - '0';
239                 if (chip > 1 || core > 1) {
240                         printk(KERN_ERR "wf_sat_create: don't understand "
241                                "location %s for %pOF\n", loc, child);
242                         continue;
243                 }
244                 cpu = 2 * chip + core;
245                 if (sat->nr < 0)
246                         sat->nr = chip;
247                 else if (sat->nr != chip) {
248                         printk(KERN_ERR "wf_sat_create: can't cope with "
249                                "multiple CPU chips on one SAT (%s)\n", loc);
250                         continue;
251                 }
252
253                 if (strcmp(type, "voltage-sensor") == 0) {
254                         name = "cpu-voltage";
255                         shift = 4;
256                         vsens[core] = index;
257                 } else if (strcmp(type, "current-sensor") == 0) {
258                         name = "cpu-current";
259                         shift = 8;
260                         isens[core] = index;
261                 } else if (strcmp(type, "temp-sensor") == 0) {
262                         name = "cpu-temp";
263                         shift = 10;
264                 } else
265                         continue;       /* hmmm shouldn't happen */
266
267                 /* the +16 is enough for "cpu-voltage-n" */
268                 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
269                 if (sens == NULL) {
270                         printk(KERN_ERR "wf_sat_create: couldn't create "
271                                "%s sensor %d (no memory)\n", name, cpu);
272                         continue;
273                 }
274                 sens->index = index;
275                 sens->index2 = -1;
276                 sens->shift = shift;
277                 sens->sat = sat;
278                 sens->sens.ops = &wf_sat_ops;
279                 sens->sens.name = (char *) (sens + 1);
280                 snprintf((char *)sens->sens.name, 16, "%s-%d", name, cpu);
281
282                 if (wf_register_sensor(&sens->sens))
283                         kfree(sens);
284                 else {
285                         list_add(&sens->link, &sat->sensors);
286                         kref_get(&sat->ref);
287                 }
288         }
289
290         /* make the power sensors */
291         for (core = 0; core < 2; ++core) {
292                 if (vsens[core] < 0 || isens[core] < 0)
293                         continue;
294                 cpu = 2 * sat->nr + core;
295                 sens = kzalloc(sizeof(struct wf_sat_sensor) + 16, GFP_KERNEL);
296                 if (sens == NULL) {
297                         printk(KERN_ERR "wf_sat_create: couldn't create power "
298                                "sensor %d (no memory)\n", cpu);
299                         continue;
300                 }
301                 sens->index = vsens[core];
302                 sens->index2 = isens[core];
303                 sens->shift = 0;
304                 sens->sat = sat;
305                 sens->sens.ops = &wf_sat_ops;
306                 sens->sens.name = (char *) (sens + 1);
307                 snprintf((char *)sens->sens.name, 16, "cpu-power-%d", cpu);
308
309                 if (wf_register_sensor(&sens->sens))
310                         kfree(sens);
311                 else {
312                         list_add(&sens->link, &sat->sensors);
313                         kref_get(&sat->ref);
314                 }
315         }
316
317         if (sat->nr >= 0)
318                 sats[sat->nr] = sat;
319
320         return 0;
321 }
322
323 static int wf_sat_remove(struct i2c_client *client)
324 {
325         struct wf_sat *sat = i2c_get_clientdata(client);
326         struct wf_sat_sensor *sens;
327
328         /* release sensors */
329         while(!list_empty(&sat->sensors)) {
330                 sens = list_first_entry(&sat->sensors,
331                                         struct wf_sat_sensor, link);
332                 list_del(&sens->link);
333                 wf_unregister_sensor(&sens->sens);
334         }
335         sat->i2c = NULL;
336         kref_put(&sat->ref, wf_sat_release);
337
338         return 0;
339 }
340
341 static const struct i2c_device_id wf_sat_id[] = {
342         { "MAC,smu-sat", 0 },
343         { }
344 };
345 MODULE_DEVICE_TABLE(i2c, wf_sat_id);
346
347 static const struct of_device_id wf_sat_of_id[] = {
348         { .compatible = "smu-sat", },
349         { }
350 };
351 MODULE_DEVICE_TABLE(of, wf_sat_of_id);
352
353 static struct i2c_driver wf_sat_driver = {
354         .driver = {
355                 .name           = "wf_smu_sat",
356                 .of_match_table = wf_sat_of_id,
357         },
358         .probe          = wf_sat_probe,
359         .remove         = wf_sat_remove,
360         .id_table       = wf_sat_id,
361 };
362
363 module_i2c_driver(wf_sat_driver);
364
365 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
366 MODULE_DESCRIPTION("SMU satellite sensors for PowerMac thermal control");
367 MODULE_LICENSE("GPL");