windfarm_lm75_sensor.c 4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Windfarm PowerMac thermal control. LM75 sensor
 *
 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
 *                    <benh@kernel.crashing.org>
 *
 * Released under the term of the GNU GPL v2.
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/i2c.h>
#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/io.h>
#include <asm/sections.h>
22
#include <asm/pmac_low_i2c.h>
23 24 25

#include "windfarm.h"

26
#define VERSION "1.0"
27 28 29 30 31 32 33 34 35 36 37 38

#undef DEBUG

#ifdef DEBUG
#define DBG(args...)	printk(args)
#else
#define DBG(args...)	do { } while(0)
#endif

struct wf_lm75_sensor {
	int			ds1775 : 1;
	int			inited : 1;
39 40
	struct i2c_client	*i2c;
	struct wf_sensor	sens;
41 42 43 44 45 46 47 48
};
#define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)

static int wf_lm75_get(struct wf_sensor *sr, s32 *value)
{
	struct wf_lm75_sensor *lm = wf_to_lm75(sr);
	s32 data;

49
	if (lm->i2c == NULL)
50 51 52 53
		return -ENODEV;

	/* Init chip if necessary */
	if (!lm->inited) {
54
		u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);
55 56 57 58 59 60 61 62

		DBG("wf_lm75: Initializing %s, cfg was: %02x\n",
		    sr->name, cfg);

		/* clear shutdown bit, keep other settings as left by
		 * the firmware for now
		 */
		cfg_new = cfg & ~0x01;
63
		i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);
64 65 66 67 68 69 70
		lm->inited = 1;

		/* If we just powered it up, let's wait 200 ms */
		msleep(200);
	}

	/* Read temperature register */
71
	data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));
72 73 74 75 76 77 78 79 80 81 82 83 84
	data <<= 8;
	*value = data;

	return 0;
}

static void wf_lm75_release(struct wf_sensor *sr)
{
	struct wf_lm75_sensor *lm = wf_to_lm75(sr);

	kfree(lm);
}

85
static const struct wf_sensor_ops wf_lm75_ops = {
86 87 88 89 90
	.get_value	= wf_lm75_get,
	.release	= wf_lm75_release,
	.owner		= THIS_MODULE,
};

91 92
static int wf_lm75_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
93
{	
94
	struct wf_lm75_sensor *lm;
95 96
	int rc, ds1775 = id->driver_data;
	const char *name, *loc;
97 98

	DBG("wf_lm75: creating  %s device at address 0x%02x\n",
99 100 101 102 103 104 105
	    ds1775 ? "ds1775" : "lm75", client->addr);

	loc = of_get_property(client->dev.of_node, "hwsensor-location", NULL);
	if (!loc) {
		dev_warn(&client->dev, "Missing hwsensor-location property!\n");
		return -ENXIO;
	}
106 107 108 109 110 111

	/* Usual rant about sensor names not beeing very consistent in
	 * the device-tree, oh well ...
	 * Add more entries below as you deal with more setups
	 */
	if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))
112
		name = "hd-temp";
113
	else if (!strcmp(loc, "Incoming Air Temp"))
114
		name = "incoming-air-temp";
115
	else if (!strcmp(loc, "ODD Temp"))
116
		name = "optical-drive-temp";
117
	else if (!strcmp(loc, "HD Temp"))
118
		name = "hard-drive-temp";
119 120 121 122 123 124
	else if (!strcmp(loc, "PCI SLOTS"))
		name = "slots-temp";
	else if (!strcmp(loc, "CPU A INLET"))
		name = "cpu-inlet-temp-0";
	else if (!strcmp(loc, "CPU B INLET"))
		name = "cpu-inlet-temp-1";
125
	else
126 127
		return -ENXIO;
 	
128

129 130
	lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
	if (lm == NULL)
131
		return -ENODEV;
132

133 134 135
	lm->inited = 0;
	lm->ds1775 = ds1775;
	lm->i2c = client;
136
	lm->sens.name = name;
137 138
	lm->sens.ops = &wf_lm75_ops;
	i2c_set_clientdata(client, lm);
139

140 141 142 143
	rc = wf_register_sensor(&lm->sens);
	if (rc)
		kfree(lm);
	return rc;
144 145
}

146
static int wf_lm75_remove(struct i2c_client *client)
147
{
148
	struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
149 150 151 152

	DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);

	/* Mark client detached */
153
	lm->i2c = NULL;
154 155 156 157 158 159 160

	/* release sensor */
	wf_unregister_sensor(&lm->sens);

	return 0;
}

161
static const struct i2c_device_id wf_lm75_id[] = {
162 163
	{ "MAC,lm75", 0 },
	{ "MAC,ds1775", 1 },
164 165
	{ }
};
166
MODULE_DEVICE_TABLE(i2c, wf_lm75_id);
167 168 169 170 171 172 173 174 175 176

static struct i2c_driver wf_lm75_driver = {
	.driver = {
		.name	= "wf_lm75",
	},
	.probe		= wf_lm75_probe,
	.remove		= wf_lm75_remove,
	.id_table	= wf_lm75_id,
};

177
module_i2c_driver(wf_lm75_driver);
178 179 180 181 182

MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
MODULE_LICENSE("GPL");