Path: blob/master/drivers/macintosh/windfarm_max6690_sensor.c
15109 views
/*1* Windfarm PowerMac thermal control. MAX6690 sensor.2*3* Copyright (C) 2005 Paul Mackerras, IBM Corp. <[email protected]>4*5* Use and redistribute under the terms of the GNU GPL v2.6*/7#include <linux/types.h>8#include <linux/errno.h>9#include <linux/kernel.h>10#include <linux/init.h>11#include <linux/slab.h>12#include <linux/i2c.h>13#include <asm/prom.h>14#include <asm/pmac_low_i2c.h>1516#include "windfarm.h"1718#define VERSION "0.2"1920/* This currently only exports the external temperature sensor,21since that's all the control loops need. */2223/* Some MAX6690 register numbers */24#define MAX6690_INTERNAL_TEMP 025#define MAX6690_EXTERNAL_TEMP 12627struct wf_6690_sensor {28struct i2c_client *i2c;29struct wf_sensor sens;30};3132#define wf_to_6690(x) container_of((x), struct wf_6690_sensor, sens)3334static int wf_max6690_get(struct wf_sensor *sr, s32 *value)35{36struct wf_6690_sensor *max = wf_to_6690(sr);37s32 data;3839if (max->i2c == NULL)40return -ENODEV;4142/* chip gets initialized by firmware */43data = i2c_smbus_read_byte_data(max->i2c, MAX6690_EXTERNAL_TEMP);44if (data < 0)45return data;46*value = data << 16;47return 0;48}4950static void wf_max6690_release(struct wf_sensor *sr)51{52struct wf_6690_sensor *max = wf_to_6690(sr);5354kfree(max);55}5657static struct wf_sensor_ops wf_max6690_ops = {58.get_value = wf_max6690_get,59.release = wf_max6690_release,60.owner = THIS_MODULE,61};6263static int wf_max6690_probe(struct i2c_client *client,64const struct i2c_device_id *id)65{66struct wf_6690_sensor *max;67int rc;6869max = kzalloc(sizeof(struct wf_6690_sensor), GFP_KERNEL);70if (max == NULL) {71printk(KERN_ERR "windfarm: Couldn't create MAX6690 sensor: "72"no memory\n");73return -ENOMEM;74}7576max->i2c = client;77max->sens.name = client->dev.platform_data;78max->sens.ops = &wf_max6690_ops;79i2c_set_clientdata(client, max);8081rc = wf_register_sensor(&max->sens);82if (rc) {83kfree(max);84}8586return rc;87}8889static struct i2c_driver wf_max6690_driver;9091static struct i2c_client *wf_max6690_create(struct i2c_adapter *adapter,92u8 addr, const char *loc)93{94struct i2c_board_info info;95struct i2c_client *client;96char *name;9798if (!strcmp(loc, "BACKSIDE"))99name = "backside-temp";100else if (!strcmp(loc, "NB Ambient"))101name = "north-bridge-temp";102else if (!strcmp(loc, "GPU Ambient"))103name = "gpu-temp";104else105goto fail;106107memset(&info, 0, sizeof(struct i2c_board_info));108info.addr = addr >> 1;109info.platform_data = name;110strlcpy(info.type, "wf_max6690", I2C_NAME_SIZE);111112client = i2c_new_device(adapter, &info);113if (client == NULL) {114printk(KERN_ERR "windfarm: failed to attach MAX6690 sensor\n");115goto fail;116}117118/*119* Let i2c-core delete that device on driver removal.120* This is safe because i2c-core holds the core_lock mutex for us.121*/122list_add_tail(&client->detected, &wf_max6690_driver.clients);123return client;124125fail:126return NULL;127}128129static int wf_max6690_attach(struct i2c_adapter *adapter)130{131struct device_node *busnode, *dev = NULL;132struct pmac_i2c_bus *bus;133const char *loc;134135bus = pmac_i2c_adapter_to_bus(adapter);136if (bus == NULL)137return -ENODEV;138busnode = pmac_i2c_get_bus_node(bus);139140while ((dev = of_get_next_child(busnode, dev)) != NULL) {141u8 addr;142143/* We must re-match the adapter in order to properly check144* the channel on multibus setups145*/146if (!pmac_i2c_match_adapter(dev, adapter))147continue;148if (!of_device_is_compatible(dev, "max6690"))149continue;150addr = pmac_i2c_get_dev_addr(dev);151loc = of_get_property(dev, "hwsensor-location", NULL);152if (loc == NULL || addr == 0)153continue;154printk("found max6690, loc=%s addr=0x%02x\n", loc, addr);155wf_max6690_create(adapter, addr, loc);156}157158return 0;159}160161static int wf_max6690_remove(struct i2c_client *client)162{163struct wf_6690_sensor *max = i2c_get_clientdata(client);164165max->i2c = NULL;166wf_unregister_sensor(&max->sens);167168return 0;169}170171static const struct i2c_device_id wf_max6690_id[] = {172{ "wf_max6690", 0 },173{ }174};175176static struct i2c_driver wf_max6690_driver = {177.driver = {178.name = "wf_max6690",179},180.attach_adapter = wf_max6690_attach,181.probe = wf_max6690_probe,182.remove = wf_max6690_remove,183.id_table = wf_max6690_id,184};185186static int __init wf_max6690_sensor_init(void)187{188/* Don't register on old machines that use therm_pm72 for now */189if (of_machine_is_compatible("PowerMac7,2") ||190of_machine_is_compatible("PowerMac7,3") ||191of_machine_is_compatible("RackMac3,1"))192return -ENODEV;193return i2c_add_driver(&wf_max6690_driver);194}195196static void __exit wf_max6690_sensor_exit(void)197{198i2c_del_driver(&wf_max6690_driver);199}200201module_init(wf_max6690_sensor_init);202module_exit(wf_max6690_sensor_exit);203204MODULE_AUTHOR("Paul Mackerras <[email protected]>");205MODULE_DESCRIPTION("MAX6690 sensor objects for PowerMac thermal control");206MODULE_LICENSE("GPL");207208209