Linux 4.19.133
[linux/fpc-iii.git] / drivers / macintosh / windfarm_lm87_sensor.c
blob8b57a1fa6ae33c3d5b00137b56bd7fb5a69a646e
1 /*
2 * Windfarm PowerMac thermal control. LM87 sensor
4 * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
6 * Released under the term of the GNU GPL v2.
8 */
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>
24 #include "windfarm.h"
26 #define VERSION "1.0"
28 #undef DEBUG
30 #ifdef DEBUG
31 #define DBG(args...) printk(args)
32 #else
33 #define DBG(args...) do { } while(0)
34 #endif
36 struct wf_lm87_sensor {
37 struct i2c_client *i2c;
38 struct wf_sensor sens;
40 #define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens)
43 static int wf_lm87_read_reg(struct i2c_client *chip, int reg)
45 int rc, tries = 0;
46 u8 buf;
48 for (;;) {
49 /* Set address */
50 buf = (u8)reg;
51 rc = i2c_master_send(chip, &buf, 1);
52 if (rc <= 0)
53 goto error;
54 rc = i2c_master_recv(chip, &buf, 1);
55 if (rc <= 0)
56 goto error;
57 return (int)buf;
58 error:
59 DBG("wf_lm87: Error reading LM87, retrying...\n");
60 if (++tries > 10) {
61 printk(KERN_ERR "wf_lm87: Error reading LM87 !\n");
62 return -EIO;
64 msleep(10);
68 static int wf_lm87_get(struct wf_sensor *sr, s32 *value)
70 struct wf_lm87_sensor *lm = sr->priv;
71 s32 temp;
73 if (lm->i2c == NULL)
74 return -ENODEV;
76 #define LM87_INT_TEMP 0x27
78 /* Read temperature register */
79 temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP);
80 if (temp < 0)
81 return temp;
82 *value = temp << 16;
84 return 0;
87 static void wf_lm87_release(struct wf_sensor *sr)
89 struct wf_lm87_sensor *lm = wf_to_lm87(sr);
91 kfree(lm);
94 static const struct wf_sensor_ops wf_lm87_ops = {
95 .get_value = wf_lm87_get,
96 .release = wf_lm87_release,
97 .owner = THIS_MODULE,
100 static int wf_lm87_probe(struct i2c_client *client,
101 const struct i2c_device_id *id)
103 struct wf_lm87_sensor *lm;
104 const char *name = NULL, *loc;
105 struct device_node *np = NULL;
106 int rc;
109 * The lm87 contains a whole pile of sensors, additionally,
110 * the Xserve G5 has several lm87's. However, for now we only
111 * care about the internal temperature sensor
113 while ((np = of_get_next_child(client->dev.of_node, np)) != NULL) {
114 if (strcmp(np->name, "int-temp"))
115 continue;
116 loc = of_get_property(np, "location", NULL);
117 if (!loc)
118 continue;
119 if (strstr(loc, "DIMM"))
120 name = "dimms-temp";
121 else if (strstr(loc, "Processors"))
122 name = "between-cpus-temp";
123 if (name) {
124 of_node_put(np);
125 break;
128 if (!name) {
129 pr_warning("wf_lm87: Unsupported sensor %pOF\n",
130 client->dev.of_node);
131 return -ENODEV;
134 lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
135 if (lm == NULL)
136 return -ENODEV;
138 lm->i2c = client;
139 lm->sens.name = name;
140 lm->sens.ops = &wf_lm87_ops;
141 lm->sens.priv = lm;
142 i2c_set_clientdata(client, lm);
144 rc = wf_register_sensor(&lm->sens);
145 if (rc)
146 kfree(lm);
147 return rc;
150 static int wf_lm87_remove(struct i2c_client *client)
152 struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
154 DBG("wf_lm87: i2c detatch called for %s\n", lm->sens.name);
156 /* Mark client detached */
157 lm->i2c = NULL;
159 /* release sensor */
160 wf_unregister_sensor(&lm->sens);
162 return 0;
165 static const struct i2c_device_id wf_lm87_id[] = {
166 { "MAC,lm87cimt", 0 },
169 MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
171 static const struct of_device_id wf_lm87_of_id[] = {
172 { .compatible = "lm87cimt", },
175 MODULE_DEVICE_TABLE(of, wf_lm87_of_id);
177 static struct i2c_driver wf_lm87_driver = {
178 .driver = {
179 .name = "wf_lm87",
180 .of_match_table = wf_lm87_of_id,
182 .probe = wf_lm87_probe,
183 .remove = wf_lm87_remove,
184 .id_table = wf_lm87_id,
187 static int __init wf_lm87_sensor_init(void)
189 /* We only support this on the Xserve */
190 if (!of_machine_is_compatible("RackMac3,1"))
191 return -ENODEV;
193 return i2c_add_driver(&wf_lm87_driver);
196 static void __exit wf_lm87_sensor_exit(void)
198 i2c_del_driver(&wf_lm87_driver);
202 module_init(wf_lm87_sensor_init);
203 module_exit(wf_lm87_sensor_exit);
205 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
206 MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
207 MODULE_LICENSE("GPL");