Path: blob/master/drivers/macintosh/windfarm_lm75_sensor.c
15111 views
/*1* Windfarm PowerMac thermal control. LM75 sensor2*3* (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.4* <[email protected]>5*6* Released under the term of the GNU GPL v2.7*/89#include <linux/types.h>10#include <linux/errno.h>11#include <linux/kernel.h>12#include <linux/delay.h>13#include <linux/slab.h>14#include <linux/init.h>15#include <linux/wait.h>16#include <linux/i2c.h>17#include <asm/prom.h>18#include <asm/machdep.h>19#include <asm/io.h>20#include <asm/system.h>21#include <asm/sections.h>22#include <asm/pmac_low_i2c.h>2324#include "windfarm.h"2526#define VERSION "0.2"2728#undef DEBUG2930#ifdef DEBUG31#define DBG(args...) printk(args)32#else33#define DBG(args...) do { } while(0)34#endif3536struct wf_lm75_sensor {37int ds1775 : 1;38int inited : 1;39struct i2c_client *i2c;40struct wf_sensor sens;41};42#define wf_to_lm75(c) container_of(c, struct wf_lm75_sensor, sens)4344static int wf_lm75_get(struct wf_sensor *sr, s32 *value)45{46struct wf_lm75_sensor *lm = wf_to_lm75(sr);47s32 data;4849if (lm->i2c == NULL)50return -ENODEV;5152/* Init chip if necessary */53if (!lm->inited) {54u8 cfg_new, cfg = (u8)i2c_smbus_read_byte_data(lm->i2c, 1);5556DBG("wf_lm75: Initializing %s, cfg was: %02x\n",57sr->name, cfg);5859/* clear shutdown bit, keep other settings as left by60* the firmware for now61*/62cfg_new = cfg & ~0x01;63i2c_smbus_write_byte_data(lm->i2c, 1, cfg_new);64lm->inited = 1;6566/* If we just powered it up, let's wait 200 ms */67msleep(200);68}6970/* Read temperature register */71data = (s32)le16_to_cpu(i2c_smbus_read_word_data(lm->i2c, 0));72data <<= 8;73*value = data;7475return 0;76}7778static void wf_lm75_release(struct wf_sensor *sr)79{80struct wf_lm75_sensor *lm = wf_to_lm75(sr);8182kfree(lm);83}8485static struct wf_sensor_ops wf_lm75_ops = {86.get_value = wf_lm75_get,87.release = wf_lm75_release,88.owner = THIS_MODULE,89};9091static int wf_lm75_probe(struct i2c_client *client,92const struct i2c_device_id *id)93{94struct wf_lm75_sensor *lm;95int rc;9697lm = kzalloc(sizeof(struct wf_lm75_sensor), GFP_KERNEL);98if (lm == NULL)99return -ENODEV;100101lm->inited = 0;102lm->ds1775 = id->driver_data;103lm->i2c = client;104lm->sens.name = client->dev.platform_data;105lm->sens.ops = &wf_lm75_ops;106i2c_set_clientdata(client, lm);107108rc = wf_register_sensor(&lm->sens);109if (rc)110kfree(lm);111112return rc;113}114115static struct i2c_driver wf_lm75_driver;116117static struct i2c_client *wf_lm75_create(struct i2c_adapter *adapter,118u8 addr, int ds1775,119const char *loc)120{121struct i2c_board_info info;122struct i2c_client *client;123char *name;124125DBG("wf_lm75: creating %s device at address 0x%02x\n",126ds1775 ? "ds1775" : "lm75", addr);127128/* Usual rant about sensor names not beeing very consistent in129* the device-tree, oh well ...130* Add more entries below as you deal with more setups131*/132if (!strcmp(loc, "Hard drive") || !strcmp(loc, "DRIVE BAY"))133name = "hd-temp";134else if (!strcmp(loc, "Incoming Air Temp"))135name = "incoming-air-temp";136else if (!strcmp(loc, "ODD Temp"))137name = "optical-drive-temp";138else if (!strcmp(loc, "HD Temp"))139name = "hard-drive-temp";140else141goto fail;142143memset(&info, 0, sizeof(struct i2c_board_info));144info.addr = (addr >> 1) & 0x7f;145info.platform_data = name;146strlcpy(info.type, ds1775 ? "wf_ds1775" : "wf_lm75", I2C_NAME_SIZE);147148client = i2c_new_device(adapter, &info);149if (client == NULL) {150printk(KERN_ERR "windfarm: failed to attach %s %s to i2c\n",151ds1775 ? "ds1775" : "lm75", name);152goto fail;153}154155/*156* Let i2c-core delete that device on driver removal.157* This is safe because i2c-core holds the core_lock mutex for us.158*/159list_add_tail(&client->detected, &wf_lm75_driver.clients);160return client;161fail:162return NULL;163}164165static int wf_lm75_attach(struct i2c_adapter *adapter)166{167struct device_node *busnode, *dev;168struct pmac_i2c_bus *bus;169170DBG("wf_lm75: adapter %s detected\n", adapter->name);171172bus = pmac_i2c_adapter_to_bus(adapter);173if (bus == NULL)174return -ENODEV;175busnode = pmac_i2c_get_bus_node(bus);176177DBG("wf_lm75: bus found, looking for device...\n");178179/* Now look for lm75(s) in there */180for (dev = NULL;181(dev = of_get_next_child(busnode, dev)) != NULL;) {182const char *loc =183of_get_property(dev, "hwsensor-location", NULL);184u8 addr;185186/* We must re-match the adapter in order to properly check187* the channel on multibus setups188*/189if (!pmac_i2c_match_adapter(dev, adapter))190continue;191addr = pmac_i2c_get_dev_addr(dev);192if (loc == NULL || addr == 0)193continue;194/* real lm75 */195if (of_device_is_compatible(dev, "lm75"))196wf_lm75_create(adapter, addr, 0, loc);197/* ds1775 (compatible, better resolution */198else if (of_device_is_compatible(dev, "ds1775"))199wf_lm75_create(adapter, addr, 1, loc);200}201return 0;202}203204static int wf_lm75_remove(struct i2c_client *client)205{206struct wf_lm75_sensor *lm = i2c_get_clientdata(client);207208DBG("wf_lm75: i2c detatch called for %s\n", lm->sens.name);209210/* Mark client detached */211lm->i2c = NULL;212213/* release sensor */214wf_unregister_sensor(&lm->sens);215216return 0;217}218219static const struct i2c_device_id wf_lm75_id[] = {220{ "wf_lm75", 0 },221{ "wf_ds1775", 1 },222{ }223};224225static struct i2c_driver wf_lm75_driver = {226.driver = {227.name = "wf_lm75",228},229.attach_adapter = wf_lm75_attach,230.probe = wf_lm75_probe,231.remove = wf_lm75_remove,232.id_table = wf_lm75_id,233};234235static int __init wf_lm75_sensor_init(void)236{237/* Don't register on old machines that use therm_pm72 for now */238if (of_machine_is_compatible("PowerMac7,2") ||239of_machine_is_compatible("PowerMac7,3") ||240of_machine_is_compatible("RackMac3,1"))241return -ENODEV;242return i2c_add_driver(&wf_lm75_driver);243}244245static void __exit wf_lm75_sensor_exit(void)246{247i2c_del_driver(&wf_lm75_driver);248}249250251module_init(wf_lm75_sensor_init);252module_exit(wf_lm75_sensor_exit);253254MODULE_AUTHOR("Benjamin Herrenschmidt <[email protected]>");255MODULE_DESCRIPTION("LM75 sensor objects for PowerMacs thermal control");256MODULE_LICENSE("GPL");257258259260