xref: /OK3568_Linux_fs/kernel/drivers/macintosh/windfarm_max6690_sensor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Windfarm PowerMac thermal control.  MAX6690 sensor.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005 Paul Mackerras, IBM Corp. <paulus@samba.org>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/errno.h>
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/i2c.h>
13*4882a593Smuzhiyun #include <asm/prom.h>
14*4882a593Smuzhiyun #include <asm/pmac_low_i2c.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "windfarm.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define VERSION "1.0"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* This currently only exports the external temperature sensor,
21*4882a593Smuzhiyun    since that's all the control loops need. */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Some MAX6690 register numbers */
24*4882a593Smuzhiyun #define MAX6690_INTERNAL_TEMP	0
25*4882a593Smuzhiyun #define MAX6690_EXTERNAL_TEMP	1
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct wf_6690_sensor {
28*4882a593Smuzhiyun 	struct i2c_client	*i2c;
29*4882a593Smuzhiyun 	struct wf_sensor	sens;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define wf_to_6690(x)	container_of((x), struct wf_6690_sensor, sens)
33*4882a593Smuzhiyun 
wf_max6690_get(struct wf_sensor * sr,s32 * value)34*4882a593Smuzhiyun static int wf_max6690_get(struct wf_sensor *sr, s32 *value)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct wf_6690_sensor *max = wf_to_6690(sr);
37*4882a593Smuzhiyun 	s32 data;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	if (max->i2c == NULL)
40*4882a593Smuzhiyun 		return -ENODEV;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	/* chip gets initialized by firmware */
43*4882a593Smuzhiyun 	data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);
44*4882a593Smuzhiyun 	if (data < 0)
45*4882a593Smuzhiyun 		return data;
46*4882a593Smuzhiyun 	*value = data << 16;
47*4882a593Smuzhiyun 	return 0;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
wf_max6690_release(struct wf_sensor * sr)50*4882a593Smuzhiyun static void wf_max6690_release(struct wf_sensor *sr)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct wf_6690_sensor *max = wf_to_6690(sr);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	kfree(max);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static const struct wf_sensor_ops wf_max6690_ops = {
58*4882a593Smuzhiyun 	.get_value	= wf_max6690_get,
59*4882a593Smuzhiyun 	.release	= wf_max6690_release,
60*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
wf_max6690_probe(struct i2c_client * client,const struct i2c_device_id * id)63*4882a593Smuzhiyun static int wf_max6690_probe(struct i2c_client *client,
64*4882a593Smuzhiyun 			    const struct i2c_device_id *id)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	const char *name, *loc;
67*4882a593Smuzhiyun 	struct wf_6690_sensor *max;
68*4882a593Smuzhiyun 	int rc;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
71*4882a593Smuzhiyun 	if (!loc) {
72*4882a593Smuzhiyun 		dev_warn(&client->dev, "Missing hwsensor-location property!\n");
73*4882a593Smuzhiyun 		return -ENXIO;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/*
77*4882a593Smuzhiyun 	 * We only expose the external temperature register for
78*4882a593Smuzhiyun 	 * now as this is all we need for our control loops
79*4882a593Smuzhiyun 	 */
80*4882a593Smuzhiyun 	if (!strcmp(loc, "BACKSIDE") || !strcmp(loc, "SYS CTRLR AMBIENT"))
81*4882a593Smuzhiyun 		name = "backside-temp";
82*4882a593Smuzhiyun 	else if (!strcmp(loc, "NB Ambient"))
83*4882a593Smuzhiyun 		name = "north-bridge-temp";
84*4882a593Smuzhiyun 	else if (!strcmp(loc, "GPU Ambient"))
85*4882a593Smuzhiyun 		name = "gpu-temp";
86*4882a593Smuzhiyun 	else
87*4882a593Smuzhiyun 		return -ENXIO;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);
90*4882a593Smuzhiyun 	if (max == NULL) {
91*4882a593Smuzhiyun 		printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "
92*4882a593Smuzhiyun 		       "no memory\n");
93*4882a593Smuzhiyun 		return -ENOMEM;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	max->i2c = client;
97*4882a593Smuzhiyun 	max->sens.name = name;
98*4882a593Smuzhiyun 	max->sens.ops = &wf_max6690_ops;
99*4882a593Smuzhiyun 	i2c_set_clientdata(client, max);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	rc = wf_register_sensor(&max->sens);
102*4882a593Smuzhiyun 	if (rc)
103*4882a593Smuzhiyun 		kfree(max);
104*4882a593Smuzhiyun 	return rc;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
wf_max6690_remove(struct i2c_client * client)107*4882a593Smuzhiyun static int wf_max6690_remove(struct i2c_client *client)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	struct wf_6690_sensor *max = i2c_get_clientdata(client);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	max->i2c = NULL;
112*4882a593Smuzhiyun 	wf_unregister_sensor(&max->sens);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static const struct i2c_device_id wf_max6690_id[] = {
118*4882a593Smuzhiyun 	{ "MAC,max6690", 0 },
119*4882a593Smuzhiyun 	{ }
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, wf_max6690_id);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static const struct of_device_id wf_max6690_of_id[] = {
124*4882a593Smuzhiyun 	{ .compatible = "max6690", },
125*4882a593Smuzhiyun 	{ }
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, wf_max6690_of_id);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static struct i2c_driver wf_max6690_driver = {
130*4882a593Smuzhiyun 	.driver = {
131*4882a593Smuzhiyun 		.name		= "wf_max6690",
132*4882a593Smuzhiyun 		.of_match_table = wf_max6690_of_id,
133*4882a593Smuzhiyun 	},
134*4882a593Smuzhiyun 	.probe		= wf_max6690_probe,
135*4882a593Smuzhiyun 	.remove		= wf_max6690_remove,
136*4882a593Smuzhiyun 	.id_table	= wf_max6690_id,
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun module_i2c_driver(wf_max6690_driver);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>");
142*4882a593Smuzhiyun MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");
143*4882a593Smuzhiyun MODULE_LICENSE("GPL");
144