2005-11-07 13:08:17 +08:00
|
|
|
/*
|
|
|
|
* 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/system.h>
|
|
|
|
#include <asm/sections.h>
|
2006-01-07 08:35:26 +08:00
|
|
|
#include <asm/pmac_low_i2c.h>
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
#include "windfarm.h"
|
|
|
|
|
2006-03-03 14:03:21 +08:00
|
|
|
#define VERSION "0.2"
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
#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;
|
2009-06-16 00:01:51 +08:00
|
|
|
struct i2c_client *i2c;
|
2005-11-07 13:08:17 +08:00
|
|
|
struct wf_sensor sens;
|
|
|
|
};
|
|
|
|
#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;
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
if (lm->i2c == NULL)
|
2005-11-07 13:08:17 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
/* Init chip if necessary */
|
|
|
|
if (!lm->inited) {
|
2009-06-16 00:01:51 +08:00
|
|
|
u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
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;
|
2009-06-16 00:01:51 +08:00
|
|
|
i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);
|
2005-11-07 13:08:17 +08:00
|
|
|
lm->inited = 1;
|
|
|
|
|
|
|
|
/* If we just powered it up, let's wait 200 ms */
|
|
|
|
msleep(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read temperature register */
|
2009-06-16 00:01:51 +08:00
|
|
|
data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));
|
2005-11-07 13:08:17 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wf_sensor_ops wf_lm75_ops = {
|
|
|
|
.get_value = wf_lm75_get,
|
|
|
|
.release = wf_lm75_release,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
static int wf_lm75_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
2005-11-07 13:08:17 +08:00
|
|
|
{
|
|
|
|
struct wf_lm75_sensor *lm;
|
2006-03-03 14:03:21 +08:00
|
|
|
int rc;
|
2005-11-07 13:08:17 +08:00
|
|
|
|
some kmalloc/memset ->kzalloc (tree wide)
Transform some calls to kmalloc/memset to a single kzalloc (or kcalloc).
Here is a short excerpt of the semantic patch performing
this transformation:
@@
type T2;
expression x;
identifier f,fld;
expression E;
expression E1,E2;
expression e1,e2,e3,y;
statement S;
@@
x =
- kmalloc
+ kzalloc
(E1,E2)
... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
- memset((T2)x,0,E1);
@@
expression E1,E2,E3;
@@
- kzalloc(E1 * E2,E3)
+ kcalloc(E1,E2,E3)
[akpm@linux-foundation.org: get kcalloc args the right way around]
Signed-off-by: Yoann Padioleau <padator@wanadoo.fr>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Acked-by: Russell King <rmk@arm.linux.org.uk>
Cc: Bryan Wu <bryan.wu@analog.com>
Acked-by: Jiri Slaby <jirislaby@gmail.com>
Cc: Dave Airlie <airlied@linux.ie>
Acked-by: Roland Dreier <rolandd@cisco.com>
Cc: Jiri Kosina <jkosina@suse.cz>
Acked-by: Dmitry Torokhov <dtor@mail.ru>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Acked-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Acked-by: Pierre Ossman <drzeus-list@drzeus.cx>
Cc: Jeff Garzik <jeff@garzik.org>
Cc: "David S. Miller" <davem@davemloft.net>
Acked-by: Greg KH <greg@kroah.com>
Cc: James Bottomley <James.Bottomley@steeleye.com>
Cc: "Antonino A. Daplas" <adaplas@pol.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-19 16:49:03 +08:00
|
|
|
lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);
|
2005-11-07 13:08:17 +08:00
|
|
|
if (lm == NULL)
|
2009-06-16 00:01:51 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
lm->inited = 0;
|
|
|
|
lm->ds1775 = id->driver_data;
|
|
|
|
lm->i2c = client;
|
|
|
|
lm->sens.name = client->dev.platform_data;
|
|
|
|
lm->sens.ops = &wf_lm75_ops;
|
|
|
|
i2c_set_clientdata(client, lm);
|
|
|
|
|
|
|
|
rc = wf_register_sensor(&lm->sens);
|
|
|
|
if (rc) {
|
|
|
|
i2c_set_clientdata(client, NULL);
|
|
|
|
kfree(lm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-10-05 04:53:46 +08:00
|
|
|
static struct i2c_driver wf_lm75_driver;
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
static struct i2c_client *wf_lm75_create(struct i2c_adapter *adapter,
|
|
|
|
u8 addr, int ds1775,
|
|
|
|
const char *loc)
|
|
|
|
{
|
|
|
|
struct i2c_board_info info;
|
|
|
|
struct i2c_client *client;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
DBG("wf_lm75: creating %s device at address 0x%02x\n",
|
|
|
|
ds1775 ? "ds1775" : "lm75", addr);
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
/* 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"))
|
2009-06-16 00:01:51 +08:00
|
|
|
name = "hd-temp";
|
2008-04-29 13:39:55 +08:00
|
|
|
else if (!strcmp(loc, "Incoming Air Temp"))
|
2009-06-16 00:01:51 +08:00
|
|
|
name = "incoming-air-temp";
|
2008-04-29 13:39:55 +08:00
|
|
|
else if (!strcmp(loc, "ODD Temp"))
|
2009-06-16 00:01:51 +08:00
|
|
|
name = "optical-drive-temp";
|
2008-04-29 13:39:55 +08:00
|
|
|
else if (!strcmp(loc, "HD Temp"))
|
2009-06-16 00:01:51 +08:00
|
|
|
name = "hard-drive-temp";
|
2005-11-07 13:08:17 +08:00
|
|
|
else
|
|
|
|
goto fail;
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
memset(&info, 0, sizeof(struct i2c_board_info));
|
|
|
|
info.addr = (addr >> 1) & 0x7f;
|
|
|
|
info.platform_data = name;
|
|
|
|
strlcpy(info.type, ds1775 ? "wf_ds1775" : "wf_lm75", I2C_NAME_SIZE);
|
2005-11-07 13:08:17 +08:00
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
client = i2c_new_device(adapter, &info);
|
|
|
|
if (client == NULL) {
|
|
|
|
printk(KERN_ERR "windfarm: failed to attach %s %s to i2c\n",
|
|
|
|
ds1775 ? "ds1775" : "lm75", name);
|
2005-11-07 13:08:17 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
/*
|
|
|
|
* Let i2c-core delete that device on driver removal.
|
|
|
|
* This is safe because i2c-core holds the core_lock mutex for us.
|
|
|
|
*/
|
2009-10-05 04:53:46 +08:00
|
|
|
list_add_tail(&client->detected, &wf_lm75_driver.clients);
|
2009-06-16 00:01:51 +08:00
|
|
|
return client;
|
2005-11-07 13:08:17 +08:00
|
|
|
fail:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wf_lm75_attach(struct i2c_adapter *adapter)
|
|
|
|
{
|
2006-01-07 08:35:26 +08:00
|
|
|
struct device_node *busnode, *dev;
|
|
|
|
struct pmac_i2c_bus *bus;
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
DBG("wf_lm75: adapter %s detected\n", adapter->name);
|
|
|
|
|
2006-01-07 08:35:26 +08:00
|
|
|
bus = pmac_i2c_adapter_to_bus(adapter);
|
|
|
|
if (bus == NULL)
|
|
|
|
return -ENODEV;
|
|
|
|
busnode = pmac_i2c_get_bus_node(bus);
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
DBG("wf_lm75: bus found, looking for device...\n");
|
|
|
|
|
|
|
|
/* Now look for lm75(s) in there */
|
|
|
|
for (dev = NULL;
|
2006-01-07 08:35:26 +08:00
|
|
|
(dev = of_get_next_child(busnode, dev)) != NULL;) {
|
2005-11-07 13:08:17 +08:00
|
|
|
const char *loc =
|
2007-04-27 11:41:15 +08:00
|
|
|
of_get_property(dev, "hwsensor-location", NULL);
|
2006-03-03 14:03:21 +08:00
|
|
|
u8 addr;
|
|
|
|
|
|
|
|
/* We must re-match the adapter in order to properly check
|
|
|
|
* the channel on multibus setups
|
|
|
|
*/
|
|
|
|
if (!pmac_i2c_match_adapter(dev, adapter))
|
|
|
|
continue;
|
|
|
|
addr = pmac_i2c_get_dev_addr(dev);
|
|
|
|
if (loc == NULL || addr == 0)
|
2005-11-07 13:08:17 +08:00
|
|
|
continue;
|
|
|
|
/* real lm75 */
|
2007-05-03 15:26:52 +08:00
|
|
|
if (of_device_is_compatible(dev, "lm75"))
|
2006-03-03 14:03:21 +08:00
|
|
|
wf_lm75_create(adapter, addr, 0, loc);
|
2005-11-07 13:08:17 +08:00
|
|
|
/* ds1775 (compatible, better resolution */
|
2007-05-03 15:26:52 +08:00
|
|
|
else if (of_device_is_compatible(dev, "ds1775"))
|
2006-03-03 14:03:21 +08:00
|
|
|
wf_lm75_create(adapter, addr, 1, loc);
|
2005-11-07 13:08:17 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
static int wf_lm75_remove(struct i2c_client *client)
|
2005-11-07 13:08:17 +08:00
|
|
|
{
|
2009-06-16 00:01:51 +08:00
|
|
|
struct wf_lm75_sensor *lm = i2c_get_clientdata(client);
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);
|
|
|
|
|
|
|
|
/* Mark client detached */
|
2009-06-16 00:01:51 +08:00
|
|
|
lm->i2c = NULL;
|
2005-11-07 13:08:17 +08:00
|
|
|
|
|
|
|
/* release sensor */
|
|
|
|
wf_unregister_sensor(&lm->sens);
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
i2c_set_clientdata(client, NULL);
|
2005-11-07 13:08:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-16 00:01:51 +08:00
|
|
|
static const struct i2c_device_id wf_lm75_id[] = {
|
|
|
|
{ "wf_lm75", 0 },
|
|
|
|
{ "wf_ds1775", 1 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct i2c_driver wf_lm75_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "wf_lm75",
|
|
|
|
},
|
|
|
|
.attach_adapter = wf_lm75_attach,
|
|
|
|
.probe = wf_lm75_probe,
|
|
|
|
.remove = wf_lm75_remove,
|
|
|
|
.id_table = wf_lm75_id,
|
|
|
|
};
|
|
|
|
|
2005-11-07 13:08:17 +08:00
|
|
|
static int __init wf_lm75_sensor_init(void)
|
|
|
|
{
|
2006-03-03 14:03:21 +08:00
|
|
|
/* Don't register on old machines that use therm_pm72 for now */
|
|
|
|
if (machine_is_compatible("PowerMac7,2") ||
|
|
|
|
machine_is_compatible("PowerMac7,3") ||
|
|
|
|
machine_is_compatible("RackMac3,1"))
|
|
|
|
return -ENODEV;
|
2006-01-06 16:11:29 +08:00
|
|
|
return i2c_add_driver(&wf_lm75_driver);
|
2005-11-07 13:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit wf_lm75_sensor_exit(void)
|
|
|
|
{
|
|
|
|
i2c_del_driver(&wf_lm75_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module_init(wf_lm75_sensor_init);
|
|
|
|
module_exit(wf_lm75_sensor_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
|
|
|
|
MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|