1 // SPDX-License-Identifier: GPL-2.0-only
3 * Windfarm PowerMac thermal control. LM87 sensor
5 * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/i2c.h>
17 #include <asm/machdep.h>
19 #include <asm/sections.h>
20 #include <asm/pmac_low_i2c.h>
29 #define DBG(args...) printk(args)
31 #define DBG(args...) do { } while(0)
34 struct wf_lm87_sensor {
35 struct i2c_client *i2c;
36 struct wf_sensor sens;
38 #define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens)
41 static int wf_lm87_read_reg(struct i2c_client *chip, int reg)
49 rc = i2c_master_send(chip, &buf, 1);
52 rc = i2c_master_recv(chip, &buf, 1);
57 DBG("wf_lm87: Error reading LM87, retrying...\n");
59 printk(KERN_ERR "wf_lm87: Error reading LM87 !\n");
66 static int wf_lm87_get(struct wf_sensor *sr, s32 *value)
68 struct wf_lm87_sensor *lm = sr->priv;
74 #define LM87_INT_TEMP 0x27
76 /* Read temperature register */
77 temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP);
85 static void wf_lm87_release(struct wf_sensor *sr)
87 struct wf_lm87_sensor *lm = wf_to_lm87(sr);
92 static const struct wf_sensor_ops wf_lm87_ops = {
93 .get_value = wf_lm87_get,
94 .release = wf_lm87_release,
98 static int wf_lm87_probe(struct i2c_client *client,
99 const struct i2c_device_id *id)
101 struct wf_lm87_sensor *lm;
102 const char *name = NULL, *loc;
103 struct device_node *np = NULL;
107 * The lm87 contains a whole pile of sensors, additionally,
108 * the Xserve G5 has several lm87's. However, for now we only
109 * care about the internal temperature sensor
111 for_each_child_of_node(client->dev.of_node, np) {
112 if (!of_node_name_eq(np, "int-temp"))
114 loc = of_get_property(np, "location", NULL);
117 if (strstr(loc, "DIMM"))
119 else if (strstr(loc, "Processors"))
120 name = "between-cpus-temp";
127 pr_warn("wf_lm87: Unsupported sensor %pOF\n",
128 client->dev.of_node);
132 lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
137 lm->sens.name = name;
138 lm->sens.ops = &wf_lm87_ops;
140 i2c_set_clientdata(client, lm);
142 rc = wf_register_sensor(&lm->sens);
148 static void wf_lm87_remove(struct i2c_client *client)
150 struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
152 /* Mark client detached */
156 wf_unregister_sensor(&lm->sens);
159 static const struct i2c_device_id wf_lm87_id[] = {
160 { "MAC,lm87cimt", 0 },
163 MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
165 static const struct of_device_id wf_lm87_of_id[] = {
166 { .compatible = "lm87cimt", },
169 MODULE_DEVICE_TABLE(of, wf_lm87_of_id);
171 static struct i2c_driver wf_lm87_driver = {
174 .of_match_table = wf_lm87_of_id,
176 .probe = wf_lm87_probe,
177 .remove = wf_lm87_remove,
178 .id_table = wf_lm87_id,
181 static int __init wf_lm87_sensor_init(void)
183 /* We only support this on the Xserve */
184 if (!of_machine_is_compatible("RackMac3,1"))
187 return i2c_add_driver(&wf_lm87_driver);
190 static void __exit wf_lm87_sensor_exit(void)
192 i2c_del_driver(&wf_lm87_driver);
196 module_init(wf_lm87_sensor_init);
197 module_exit(wf_lm87_sensor_exit);
199 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
200 MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
201 MODULE_LICENSE("GPL");