xref: /OK3568_Linux_fs/kernel/drivers/macintosh/windfarm_lm87_sensor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Windfarm PowerMac thermal control. LM87 sensor
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2012 Benjamin Herrenschmidt, IBM Corp.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/wait.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <asm/prom.h>
17*4882a593Smuzhiyun #include <asm/machdep.h>
18*4882a593Smuzhiyun #include <asm/io.h>
19*4882a593Smuzhiyun #include <asm/sections.h>
20*4882a593Smuzhiyun #include <asm/pmac_low_i2c.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "windfarm.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define VERSION "1.0"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #undef DEBUG
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #ifdef DEBUG
29*4882a593Smuzhiyun #define DBG(args...)	printk(args)
30*4882a593Smuzhiyun #else
31*4882a593Smuzhiyun #define DBG(args...)	do { } while(0)
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun struct wf_lm87_sensor {
35*4882a593Smuzhiyun 	struct i2c_client	*i2c;
36*4882a593Smuzhiyun 	struct wf_sensor	sens;
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun #define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 
wf_lm87_read_reg(struct i2c_client * chip,int reg)41*4882a593Smuzhiyun static int wf_lm87_read_reg(struct i2c_client *chip, int reg)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	int rc, tries = 0;
44*4882a593Smuzhiyun 	u8 buf;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	for (;;) {
47*4882a593Smuzhiyun 		/* Set address */
48*4882a593Smuzhiyun 		buf = (u8)reg;
49*4882a593Smuzhiyun 		rc = i2c_master_send(chip, &buf, 1);
50*4882a593Smuzhiyun 		if (rc <= 0)
51*4882a593Smuzhiyun 			goto error;
52*4882a593Smuzhiyun 		rc = i2c_master_recv(chip, &buf, 1);
53*4882a593Smuzhiyun 		if (rc <= 0)
54*4882a593Smuzhiyun 			goto error;
55*4882a593Smuzhiyun 		return (int)buf;
56*4882a593Smuzhiyun 	error:
57*4882a593Smuzhiyun 		DBG("wf_lm87: Error reading LM87, retrying...\n");
58*4882a593Smuzhiyun 		if (++tries > 10) {
59*4882a593Smuzhiyun 			printk(KERN_ERR "wf_lm87: Error reading LM87 !\n");
60*4882a593Smuzhiyun 			return -EIO;
61*4882a593Smuzhiyun 		}
62*4882a593Smuzhiyun 		msleep(10);
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
wf_lm87_get(struct wf_sensor * sr,s32 * value)66*4882a593Smuzhiyun static int wf_lm87_get(struct wf_sensor *sr, s32 *value)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	struct wf_lm87_sensor *lm = sr->priv;
69*4882a593Smuzhiyun 	s32 temp;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (lm->i2c == NULL)
72*4882a593Smuzhiyun 		return -ENODEV;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #define LM87_INT_TEMP		0x27
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* Read temperature register */
77*4882a593Smuzhiyun 	temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP);
78*4882a593Smuzhiyun 	if (temp < 0)
79*4882a593Smuzhiyun 		return temp;
80*4882a593Smuzhiyun 	*value = temp << 16;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
wf_lm87_release(struct wf_sensor * sr)85*4882a593Smuzhiyun static void wf_lm87_release(struct wf_sensor *sr)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct wf_lm87_sensor *lm = wf_to_lm87(sr);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	kfree(lm);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static const struct wf_sensor_ops wf_lm87_ops = {
93*4882a593Smuzhiyun 	.get_value	= wf_lm87_get,
94*4882a593Smuzhiyun 	.release	= wf_lm87_release,
95*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
wf_lm87_probe(struct i2c_client * client,const struct i2c_device_id * id)98*4882a593Smuzhiyun static int wf_lm87_probe(struct i2c_client *client,
99*4882a593Smuzhiyun 			 const struct i2c_device_id *id)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	struct wf_lm87_sensor *lm;
102*4882a593Smuzhiyun 	const char *name = NULL, *loc;
103*4882a593Smuzhiyun 	struct device_node *np = NULL;
104*4882a593Smuzhiyun 	int rc;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * The lm87 contains a whole pile of sensors, additionally,
108*4882a593Smuzhiyun 	 * the Xserve G5 has several lm87's. However, for now we only
109*4882a593Smuzhiyun 	 * care about the internal temperature sensor
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	for_each_child_of_node(client->dev.of_node, np) {
112*4882a593Smuzhiyun 		if (!of_node_name_eq(np, "int-temp"))
113*4882a593Smuzhiyun 			continue;
114*4882a593Smuzhiyun 		loc = of_get_property(np, "location", NULL);
115*4882a593Smuzhiyun 		if (!loc)
116*4882a593Smuzhiyun 			continue;
117*4882a593Smuzhiyun 		if (strstr(loc, "DIMM"))
118*4882a593Smuzhiyun 			name = "dimms-temp";
119*4882a593Smuzhiyun 		else if (strstr(loc, "Processors"))
120*4882a593Smuzhiyun 			name = "between-cpus-temp";
121*4882a593Smuzhiyun 		if (name) {
122*4882a593Smuzhiyun 			of_node_put(np);
123*4882a593Smuzhiyun 			break;
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	if (!name) {
127*4882a593Smuzhiyun 		pr_warn("wf_lm87: Unsupported sensor %pOF\n",
128*4882a593Smuzhiyun 			client->dev.of_node);
129*4882a593Smuzhiyun 		return -ENODEV;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL);
133*4882a593Smuzhiyun 	if (lm == NULL)
134*4882a593Smuzhiyun 		return -ENODEV;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	lm->i2c = client;
137*4882a593Smuzhiyun 	lm->sens.name = name;
138*4882a593Smuzhiyun 	lm->sens.ops = &wf_lm87_ops;
139*4882a593Smuzhiyun 	lm->sens.priv = lm;
140*4882a593Smuzhiyun 	i2c_set_clientdata(client, lm);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	rc = wf_register_sensor(&lm->sens);
143*4882a593Smuzhiyun 	if (rc)
144*4882a593Smuzhiyun 		kfree(lm);
145*4882a593Smuzhiyun 	return rc;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
wf_lm87_remove(struct i2c_client * client)148*4882a593Smuzhiyun static int wf_lm87_remove(struct i2c_client *client)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	struct wf_lm87_sensor *lm = i2c_get_clientdata(client);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/* Mark client detached */
153*4882a593Smuzhiyun 	lm->i2c = NULL;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* release sensor */
156*4882a593Smuzhiyun 	wf_unregister_sensor(&lm->sens);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun static const struct i2c_device_id wf_lm87_id[] = {
162*4882a593Smuzhiyun 	{ "MAC,lm87cimt", 0 },
163*4882a593Smuzhiyun 	{ }
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, wf_lm87_id);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun static const struct of_device_id wf_lm87_of_id[] = {
168*4882a593Smuzhiyun 	{ .compatible = "lm87cimt", },
169*4882a593Smuzhiyun 	{ }
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, wf_lm87_of_id);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun static struct i2c_driver wf_lm87_driver = {
174*4882a593Smuzhiyun 	.driver = {
175*4882a593Smuzhiyun 		.name	= "wf_lm87",
176*4882a593Smuzhiyun 		.of_match_table = wf_lm87_of_id,
177*4882a593Smuzhiyun 	},
178*4882a593Smuzhiyun 	.probe		= wf_lm87_probe,
179*4882a593Smuzhiyun 	.remove		= wf_lm87_remove,
180*4882a593Smuzhiyun 	.id_table	= wf_lm87_id,
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
wf_lm87_sensor_init(void)183*4882a593Smuzhiyun static int __init wf_lm87_sensor_init(void)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	/* We only support this on the Xserve */
186*4882a593Smuzhiyun 	if (!of_machine_is_compatible("RackMac3,1"))
187*4882a593Smuzhiyun 		return -ENODEV;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return i2c_add_driver(&wf_lm87_driver);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
wf_lm87_sensor_exit(void)192*4882a593Smuzhiyun static void __exit wf_lm87_sensor_exit(void)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	i2c_del_driver(&wf_lm87_driver);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun module_init(wf_lm87_sensor_init);
199*4882a593Smuzhiyun module_exit(wf_lm87_sensor_exit);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
202*4882a593Smuzhiyun MODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control");
203*4882a593Smuzhiyun MODULE_LICENSE("GPL");
204*4882a593Smuzhiyun 
205