Path: blob/master/drivers/macintosh/therm_adt746x.c
15109 views
/*1* Device driver for the i2c thermostat found on the iBook G4, Albook G42*3* Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt4*5* Documentation from 115254175ADT7467_pra.pdf and 3686221171167ADT7460_b.pdf6* http://www.onsemi.com/PowerSolutions/product.do?id=ADT74677* http://www.onsemi.com/PowerSolutions/product.do?id=ADT74608*9*/1011#include <linux/types.h>12#include <linux/module.h>13#include <linux/errno.h>14#include <linux/kernel.h>15#include <linux/delay.h>16#include <linux/sched.h>17#include <linux/i2c.h>18#include <linux/slab.h>19#include <linux/init.h>20#include <linux/spinlock.h>21#include <linux/wait.h>22#include <linux/suspend.h>23#include <linux/kthread.h>24#include <linux/moduleparam.h>25#include <linux/freezer.h>26#include <linux/of_platform.h>2728#include <asm/prom.h>29#include <asm/machdep.h>30#include <asm/io.h>31#include <asm/system.h>32#include <asm/sections.h>3334#undef DEBUG3536#define CONFIG_REG 0x4037#define MANUAL_MASK 0xe038#define AUTO_MASK 0x2039#define INVERT_MASK 0x104041static u8 TEMP_REG[3] = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */42static u8 LIMIT_REG[3] = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */43static u8 MANUAL_MODE[2] = {0x5c, 0x5d};44static u8 REM_CONTROL[2] = {0x00, 0x40};45static u8 FAN_SPEED[2] = {0x28, 0x2a};46static u8 FAN_SPD_SET[2] = {0x30, 0x31};4748static u8 default_limits_local[3] = {70, 50, 70}; /* local, sensor1, sensor2 */49static u8 default_limits_chip[3] = {80, 65, 80}; /* local, sensor1, sensor2 */50static const char *sensor_location[3];5152static int limit_adjust;53static int fan_speed = -1;54static int verbose;5556MODULE_AUTHOR("Colin Leroy <[email protected]>");57MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "58"Powerbook G4 Alu");59MODULE_LICENSE("GPL");6061module_param(limit_adjust, int, 0644);62MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "63"by N degrees.");6465module_param(fan_speed, int, 0644);66MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "67"(default 64)");6869module_param(verbose, bool, 0);70MODULE_PARM_DESC(verbose,"Verbose log operations "71"(default 0)");7273struct thermostat {74struct i2c_client *clt;75u8 temps[3];76u8 cached_temp[3];77u8 initial_limits[3];78u8 limits[3];79int last_speed[2];80int last_var[2];81int pwm_inv[2];82};8384static enum {ADT7460, ADT7467} therm_type;85static int therm_bus, therm_address;86static struct platform_device * of_dev;87static struct thermostat* thermostat;88static struct task_struct *thread_therm = NULL;8990static void write_both_fan_speed(struct thermostat *th, int speed);91static void write_fan_speed(struct thermostat *th, int speed, int fan);92static void thermostat_create_files(void);93static void thermostat_remove_files(void);9495static int96write_reg(struct thermostat* th, int reg, u8 data)97{98u8 tmp[2];99int rc;100101tmp[0] = reg;102tmp[1] = data;103rc = i2c_master_send(th->clt, (const char *)tmp, 2);104if (rc < 0)105return rc;106if (rc != 2)107return -ENODEV;108return 0;109}110111static int112read_reg(struct thermostat* th, int reg)113{114u8 reg_addr, data;115int rc;116117reg_addr = (u8)reg;118rc = i2c_master_send(th->clt, ®_addr, 1);119if (rc < 0)120return rc;121if (rc != 1)122return -ENODEV;123rc = i2c_master_recv(th->clt, (char *)&data, 1);124if (rc < 0)125return rc;126return data;127}128129static struct i2c_driver thermostat_driver;130131static int132attach_thermostat(struct i2c_adapter *adapter)133{134unsigned long bus_no;135struct i2c_board_info info;136struct i2c_client *client;137138if (strncmp(adapter->name, "uni-n", 5))139return -ENODEV;140bus_no = simple_strtoul(adapter->name + 6, NULL, 10);141if (bus_no != therm_bus)142return -ENODEV;143144memset(&info, 0, sizeof(struct i2c_board_info));145strlcpy(info.type, "therm_adt746x", I2C_NAME_SIZE);146info.addr = therm_address;147client = i2c_new_device(adapter, &info);148if (!client)149return -ENODEV;150151/*152* Let i2c-core delete that device on driver removal.153* This is safe because i2c-core holds the core_lock mutex for us.154*/155list_add_tail(&client->detected, &thermostat_driver.clients);156return 0;157}158159static int160remove_thermostat(struct i2c_client *client)161{162struct thermostat *th = i2c_get_clientdata(client);163int i;164165thermostat_remove_files();166167if (thread_therm != NULL) {168kthread_stop(thread_therm);169}170171printk(KERN_INFO "adt746x: Putting max temperatures back from "172"%d, %d, %d to %d, %d, %d\n",173th->limits[0], th->limits[1], th->limits[2],174th->initial_limits[0], th->initial_limits[1],175th->initial_limits[2]);176177for (i = 0; i < 3; i++)178write_reg(th, LIMIT_REG[i], th->initial_limits[i]);179180write_both_fan_speed(th, -1);181182thermostat = NULL;183184kfree(th);185186return 0;187}188189static int read_fan_speed(struct thermostat *th, u8 addr)190{191u8 tmp[2];192u16 res;193194/* should start with low byte */195tmp[1] = read_reg(th, addr);196tmp[0] = read_reg(th, addr + 1);197198res = tmp[1] + (tmp[0] << 8);199/* "a value of 0xffff means that the fan has stopped" */200return (res == 0xffff ? 0 : (90000*60)/res);201}202203static void write_both_fan_speed(struct thermostat *th, int speed)204{205write_fan_speed(th, speed, 0);206if (therm_type == ADT7460)207write_fan_speed(th, speed, 1);208}209210static void write_fan_speed(struct thermostat *th, int speed, int fan)211{212u8 manual;213214if (speed > 0xff)215speed = 0xff;216else if (speed < -1)217speed = 0;218219if (therm_type == ADT7467 && fan == 1)220return;221222if (th->last_speed[fan] != speed) {223if (verbose) {224if (speed == -1)225printk(KERN_DEBUG "adt746x: Setting speed to automatic "226"for %s fan.\n", sensor_location[fan+1]);227else228printk(KERN_DEBUG "adt746x: Setting speed to %d "229"for %s fan.\n", speed, sensor_location[fan+1]);230}231} else232return;233234if (speed >= 0) {235manual = read_reg(th, MANUAL_MODE[fan]);236manual &= ~INVERT_MASK;237write_reg(th, MANUAL_MODE[fan],238manual | MANUAL_MASK | th->pwm_inv[fan]);239write_reg(th, FAN_SPD_SET[fan], speed);240} else {241/* back to automatic */242if(therm_type == ADT7460) {243manual = read_reg(th,244MANUAL_MODE[fan]) & (~MANUAL_MASK);245manual &= ~INVERT_MASK;246manual |= th->pwm_inv[fan];247write_reg(th,248MANUAL_MODE[fan], manual|REM_CONTROL[fan]);249} else {250manual = read_reg(th, MANUAL_MODE[fan]);251manual &= ~INVERT_MASK;252manual |= th->pwm_inv[fan];253write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));254}255}256257th->last_speed[fan] = speed;258}259260static void read_sensors(struct thermostat *th)261{262int i = 0;263264for (i = 0; i < 3; i++)265th->temps[i] = read_reg(th, TEMP_REG[i]);266}267268#ifdef DEBUG269static void display_stats(struct thermostat *th)270{271if (th->temps[0] != th->cached_temp[0]272|| th->temps[1] != th->cached_temp[1]273|| th->temps[2] != th->cached_temp[2]) {274printk(KERN_INFO "adt746x: Temperature infos:"275" thermostats: %d,%d,%d;"276" limits: %d,%d,%d;"277" fan speed: %d RPM\n",278th->temps[0], th->temps[1], th->temps[2],279th->limits[0], th->limits[1], th->limits[2],280read_fan_speed(th, FAN_SPEED[0]));281}282th->cached_temp[0] = th->temps[0];283th->cached_temp[1] = th->temps[1];284th->cached_temp[2] = th->temps[2];285}286#endif287288static void update_fans_speed (struct thermostat *th)289{290int lastvar = 0; /* last variation, for iBook */291int i = 0;292293/* we don't care about local sensor, so we start at sensor 1 */294for (i = 1; i < 3; i++) {295int started = 0;296int fan_number = (therm_type == ADT7460 && i == 2);297int var = th->temps[i] - th->limits[i];298299if (var > -1) {300int step = (255 - fan_speed) / 7;301int new_speed = 0;302303/* hysteresis : change fan speed only if variation is304* more than two degrees */305if (abs(var - th->last_var[fan_number]) < 2)306continue;307308started = 1;309new_speed = fan_speed + ((var-1)*step);310311if (new_speed < fan_speed)312new_speed = fan_speed;313if (new_speed > 255)314new_speed = 255;315316if (verbose)317printk(KERN_DEBUG "adt746x: Setting fans speed to %d "318"(limit exceeded by %d on %s)\n",319new_speed, var,320sensor_location[fan_number+1]);321write_both_fan_speed(th, new_speed);322th->last_var[fan_number] = var;323} else if (var < -2) {324/* don't stop fan if sensor2 is cold and sensor1 is not325* so cold (lastvar >= -1) */326if (i == 2 && lastvar < -1) {327if (th->last_speed[fan_number] != 0)328if (verbose)329printk(KERN_DEBUG "adt746x: Stopping "330"fans.\n");331write_both_fan_speed(th, 0);332}333}334335lastvar = var;336337if (started)338return; /* we don't want to re-stop the fan339* if sensor1 is heating and sensor2 is not */340}341}342343static int monitor_task(void *arg)344{345struct thermostat* th = arg;346347set_freezable();348while(!kthread_should_stop()) {349try_to_freeze();350msleep_interruptible(2000);351352#ifndef DEBUG353if (fan_speed != -1)354read_sensors(th);355#else356read_sensors(th);357#endif358359if (fan_speed != -1)360update_fans_speed(th);361362#ifdef DEBUG363display_stats(th);364#endif365366}367368return 0;369}370371static void set_limit(struct thermostat *th, int i)372{373/* Set sensor1 limit higher to avoid powerdowns */374th->limits[i] = default_limits_chip[i] + limit_adjust;375write_reg(th, LIMIT_REG[i], th->limits[i]);376377/* set our limits to normal */378th->limits[i] = default_limits_local[i] + limit_adjust;379}380381static int probe_thermostat(struct i2c_client *client,382const struct i2c_device_id *id)383{384struct thermostat* th;385int rc;386int i;387388if (thermostat)389return 0;390391th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);392if (!th)393return -ENOMEM;394395i2c_set_clientdata(client, th);396th->clt = client;397398rc = read_reg(th, CONFIG_REG);399if (rc < 0) {400dev_err(&client->dev, "Thermostat failed to read config!\n");401kfree(th);402return -ENODEV;403}404405/* force manual control to start the fan quieter */406if (fan_speed == -1)407fan_speed = 64;408409if(therm_type == ADT7460) {410printk(KERN_INFO "adt746x: ADT7460 initializing\n");411/* The 7460 needs to be started explicitly */412write_reg(th, CONFIG_REG, 1);413} else414printk(KERN_INFO "adt746x: ADT7467 initializing\n");415416for (i = 0; i < 3; i++) {417th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);418set_limit(th, i);419}420421printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"422" to %d, %d, %d\n",423th->initial_limits[0], th->initial_limits[1],424th->initial_limits[2], th->limits[0], th->limits[1],425th->limits[2]);426427thermostat = th;428429/* record invert bit status because fw can corrupt it after suspend */430th->pwm_inv[0] = read_reg(th, MANUAL_MODE[0]) & INVERT_MASK;431th->pwm_inv[1] = read_reg(th, MANUAL_MODE[1]) & INVERT_MASK;432433/* be sure to really write fan speed the first time */434th->last_speed[0] = -2;435th->last_speed[1] = -2;436th->last_var[0] = -80;437th->last_var[1] = -80;438439if (fan_speed != -1) {440/* manual mode, stop fans */441write_both_fan_speed(th, 0);442} else {443/* automatic mode */444write_both_fan_speed(th, -1);445}446447thread_therm = kthread_run(monitor_task, th, "kfand");448449if (thread_therm == ERR_PTR(-ENOMEM)) {450printk(KERN_INFO "adt746x: Kthread creation failed\n");451thread_therm = NULL;452return -ENOMEM;453}454455thermostat_create_files();456457return 0;458}459460static const struct i2c_device_id therm_adt746x_id[] = {461{ "therm_adt746x", 0 },462{ }463};464465static struct i2c_driver thermostat_driver = {466.driver = {467.name = "therm_adt746x",468},469.attach_adapter = attach_thermostat,470.probe = probe_thermostat,471.remove = remove_thermostat,472.id_table = therm_adt746x_id,473};474475/*476* Now, unfortunately, sysfs doesn't give us a nice void * we could477* pass around to the attribute functions, so we don't really have478* choice but implement a bunch of them...479*480* FIXME, it does now...481*/482#define BUILD_SHOW_FUNC_INT(name, data) \483static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \484{ \485return sprintf(buf, "%d\n", data); \486}487488#define BUILD_SHOW_FUNC_STR(name, data) \489static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \490{ \491return sprintf(buf, "%s\n", data); \492}493494#define BUILD_SHOW_FUNC_FAN(name, data) \495static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \496{ \497return sprintf(buf, "%d (%d rpm)\n", \498thermostat->last_speed[data], \499read_fan_speed(thermostat, FAN_SPEED[data]) \500); \501}502503#define BUILD_STORE_FUNC_DEG(name, data) \504static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \505{ \506int val; \507int i; \508val = simple_strtol(buf, NULL, 10); \509printk(KERN_INFO "Adjusting limits by %d degrees\n", val); \510limit_adjust = val; \511for (i=0; i < 3; i++) \512set_limit(thermostat, i); \513return n; \514}515516#define BUILD_STORE_FUNC_INT(name, data) \517static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \518{ \519int val; \520val = simple_strtol(buf, NULL, 10); \521if (val < 0 || val > 255) \522return -EINVAL; \523printk(KERN_INFO "Setting specified fan speed to %d\n", val); \524data = val; \525return n; \526}527528BUILD_SHOW_FUNC_INT(sensor1_temperature, (read_reg(thermostat, TEMP_REG[1])))529BUILD_SHOW_FUNC_INT(sensor2_temperature, (read_reg(thermostat, TEMP_REG[2])))530BUILD_SHOW_FUNC_INT(sensor1_limit, thermostat->limits[1])531BUILD_SHOW_FUNC_INT(sensor2_limit, thermostat->limits[2])532BUILD_SHOW_FUNC_STR(sensor1_location, sensor_location[1])533BUILD_SHOW_FUNC_STR(sensor2_location, sensor_location[2])534535BUILD_SHOW_FUNC_INT(specified_fan_speed, fan_speed)536BUILD_SHOW_FUNC_FAN(sensor1_fan_speed, 0)537BUILD_SHOW_FUNC_FAN(sensor2_fan_speed, 1)538539BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)540BUILD_SHOW_FUNC_INT(limit_adjust, limit_adjust)541BUILD_STORE_FUNC_DEG(limit_adjust, thermostat)542543static DEVICE_ATTR(sensor1_temperature, S_IRUGO,544show_sensor1_temperature,NULL);545static DEVICE_ATTR(sensor2_temperature, S_IRUGO,546show_sensor2_temperature,NULL);547static DEVICE_ATTR(sensor1_limit, S_IRUGO,548show_sensor1_limit, NULL);549static DEVICE_ATTR(sensor2_limit, S_IRUGO,550show_sensor2_limit, NULL);551static DEVICE_ATTR(sensor1_location, S_IRUGO,552show_sensor1_location, NULL);553static DEVICE_ATTR(sensor2_location, S_IRUGO,554show_sensor2_location, NULL);555556static DEVICE_ATTR(specified_fan_speed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,557show_specified_fan_speed,store_specified_fan_speed);558559static DEVICE_ATTR(sensor1_fan_speed, S_IRUGO,560show_sensor1_fan_speed, NULL);561static DEVICE_ATTR(sensor2_fan_speed, S_IRUGO,562show_sensor2_fan_speed, NULL);563564static DEVICE_ATTR(limit_adjust, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,565show_limit_adjust, store_limit_adjust);566567568static int __init569thermostat_init(void)570{571struct device_node* np;572const u32 *prop;573int i = 0, offset = 0;574575np = of_find_node_by_name(NULL, "fan");576if (!np)577return -ENODEV;578if (of_device_is_compatible(np, "adt7460"))579therm_type = ADT7460;580else if (of_device_is_compatible(np, "adt7467"))581therm_type = ADT7467;582else {583of_node_put(np);584return -ENODEV;585}586587prop = of_get_property(np, "hwsensor-params-version", NULL);588printk(KERN_INFO "adt746x: version %d (%ssupported)\n", *prop,589(*prop == 1)?"":"un");590if (*prop != 1) {591of_node_put(np);592return -ENODEV;593}594595prop = of_get_property(np, "reg", NULL);596if (!prop) {597of_node_put(np);598return -ENODEV;599}600601/* look for bus either by path or using "reg" */602if (strstr(np->full_name, "/i2c-bus@") != NULL) {603const char *tmp_bus = (strstr(np->full_name, "/i2c-bus@") + 9);604therm_bus = tmp_bus[0]-'0';605} else {606therm_bus = ((*prop) >> 8) & 0x0f;607}608609therm_address = ((*prop) & 0xff) >> 1;610611printk(KERN_INFO "adt746x: Thermostat bus: %d, address: 0x%02x, "612"limit_adjust: %d, fan_speed: %d\n",613therm_bus, therm_address, limit_adjust, fan_speed);614615if (of_get_property(np, "hwsensor-location", NULL)) {616for (i = 0; i < 3; i++) {617sensor_location[i] = of_get_property(np,618"hwsensor-location", NULL) + offset;619620if (sensor_location[i] == NULL)621sensor_location[i] = "";622623printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);624offset += strlen(sensor_location[i]) + 1;625}626} else {627sensor_location[0] = "?";628sensor_location[1] = "?";629sensor_location[2] = "?";630}631632of_dev = of_platform_device_create(np, "temperatures", NULL);633of_node_put(np);634635if (of_dev == NULL) {636printk(KERN_ERR "Can't register temperatures device !\n");637return -ENODEV;638}639640#ifndef CONFIG_I2C_POWERMAC641request_module("i2c-powermac");642#endif643644return i2c_add_driver(&thermostat_driver);645}646647static void thermostat_create_files(void)648{649int err;650651err = device_create_file(&of_dev->dev, &dev_attr_sensor1_temperature);652err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_temperature);653err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_limit);654err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_limit);655err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_location);656err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_location);657err |= device_create_file(&of_dev->dev, &dev_attr_limit_adjust);658err |= device_create_file(&of_dev->dev, &dev_attr_specified_fan_speed);659err |= device_create_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);660if(therm_type == ADT7460)661err |= device_create_file(&of_dev->dev, &dev_attr_sensor2_fan_speed);662if (err)663printk(KERN_WARNING664"Failed to create temperature attribute file(s).\n");665}666667static void thermostat_remove_files(void)668{669if (of_dev) {670device_remove_file(&of_dev->dev, &dev_attr_sensor1_temperature);671device_remove_file(&of_dev->dev, &dev_attr_sensor2_temperature);672device_remove_file(&of_dev->dev, &dev_attr_sensor1_limit);673device_remove_file(&of_dev->dev, &dev_attr_sensor2_limit);674device_remove_file(&of_dev->dev, &dev_attr_sensor1_location);675device_remove_file(&of_dev->dev, &dev_attr_sensor2_location);676device_remove_file(&of_dev->dev, &dev_attr_limit_adjust);677device_remove_file(&of_dev->dev, &dev_attr_specified_fan_speed);678device_remove_file(&of_dev->dev, &dev_attr_sensor1_fan_speed);679680if(therm_type == ADT7460)681device_remove_file(&of_dev->dev,682&dev_attr_sensor2_fan_speed);683684}685}686687static void __exit688thermostat_exit(void)689{690i2c_del_driver(&thermostat_driver);691of_device_unregister(of_dev);692}693694module_init(thermostat_init);695module_exit(thermostat_exit);696697698