From 899ca67043eb844239bab7b082323bd7f53cc9a2 Mon Sep 17 00:00:00 2001 From: Philip Edelbrock Date: Sun, 10 Jan 1999 08:03:43 +0000 Subject: [PATCH] (Phil) Added a driver for the Analog Devices ADM1021. The ADM1021 is basically an expanded LM75. It can have an external temp probe (in addition to the internal one), customizable conversion rate (in # of times per second, depending on power consumption requirements), and actually has a device-id register for easier/more-certain detecting! The only strange property is the low-limit. The low-limit doesn't act as a Hystersis value like the LM75. I.e., if the temp falls below it, it triggers an alarm instead of clearing it. Strange, ey? This makes using the chip for a thermostat control (like for a fan) much harder. But it *could* be used to turn on a heater to prevent your computer from freezing. :') Here's what my output looks like (without a remote sensor. temp's are the same format as the LM75 [high-limit, low-limit, value]): [root@penn adm1021-i2c-0-18]# ls -l total 0 -r--r--r-- 1 root root 0 Jan 9 23:42 die_code -rw-r--r-- 1 root root 0 Jan 9 23:42 remote_temp -r--r--r-- 1 root root 0 Jan 9 23:42 status -rw-r--r-- 1 root root 0 Jan 9 23:42 temp [root@penn adm1021-i2c-0-18]# cat * 3 60 20 0 76 60 20 24 PS- all temp values in the ADM1021 (limits and readings) are signed (2's comp) bytes. So, all readings go from -128 to 127 C (actually, the docs say it can only reliable sense down to -65), and only in whole degree increments. git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@153 7894878c-1315-0410-8ee3-d5d059ff63e0 --- kernel/Module.mk | 2 +- kernel/chips/adm1021.c | 460 +++++++++++++++++++++++++++++++++++++++ kernel/include/sensors.h | 6 + src/Module.mk | 2 +- src/adm1021.c | 460 +++++++++++++++++++++++++++++++++++++++ src/sensors.h | 6 + 6 files changed, 934 insertions(+), 2 deletions(-) create mode 100644 kernel/chips/adm1021.c create mode 100644 src/adm1021.c diff --git a/kernel/Module.mk b/kernel/Module.mk index afb57a60..f4ad9367 100644 --- a/kernel/Module.mk +++ b/kernel/Module.mk @@ -27,7 +27,7 @@ SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o $(MODULE_DIR)/isa.o \ $(MODULE_DIR)/i2c-proc.o $(MODULE_DIR)/lm75.o \ $(MODULE_DIR)/i2c-proc.o $(MODULE_DIR)/gl518sm.o \ $(MODULE_DIR)/eeprom.o $(MODULE_DIR)/w83781d.o \ - $(MODULE_DIR)/lm80.o + $(MODULE_DIR)/lm80.o $(MODULE_DIR)/adm1021.o SRCHEADERFILES := $(MODULE_DIR)/sensors.h $(MODULE_DIR)/isa.h \ $(MODULE_DIR)/smbus.h diff --git a/kernel/chips/adm1021.c b/kernel/chips/adm1021.c new file mode 100644 index 00000000..f16c2d29 --- /dev/null +++ b/kernel/chips/adm1021.c @@ -0,0 +1,460 @@ +/* + adm1021.c - Part of lm_sensors, Linux kernel modules for hardware + monitoring + Copyright (c) 1998 Frodo Looijaard and + Philip Edelbrock + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include "smbus.h" +#include "sensors.h" +#include "i2c.h" +#include "version.h" + +/* adm1021 constants specified below */ + +/* The adm1021 registers */ +/* Read-only */ +#define adm1021_REG_TEMP 0x00 +#define adm1021_REG_REMOTE_TEMP 0x01 +#define adm1021_REG_STATUS 0x02 +#define adm1021_REG_DEVICE_ID 0x0FE /* should always read 0x41 */ +#define adm1021_REG_DIE_CODE 0x0FF +/* These use different addresses for reading/writing */ +#define adm1021_REG_CONFIG_R 0x03 +#define adm1021_REG_CONFIG_W 0x09 +#define adm1021_REG_CONV_RATE_R 0x04 +#define adm1021_REG_CONV_RATE_W 0x0A +#define adm1021_REG_TOS_R 0x05 +#define adm1021_REG_TOS_W 0x0B +#define adm1021_REG_REMOTE_TOS_R 0x07 +#define adm1021_REG_REMOTE_TOS_W 0x0D +#define adm1021_REG_THYST_R 0x06 +#define adm1021_REG_THYST_W 0x0C +#define adm1021_REG_REMOTE_THYST_R 0x08 +#define adm1021_REG_REMOTE_THYST_W 0x0E +/* write-only */ +#define adm1021_REG_ONESHOT 0x0F + + +/* Conversions note: 1021 uses normal integer signed-byte format*/ +#define TEMP_FROM_REG(val) (val > 127 ? val-256 : val) +#define TEMP_TO_REG(val) (val < 0 ? val+256 : val) + +/* Initial values */ + +/* Note: Eventhough I left the low and high limits named os and hyst, +they don't quite work like a thermostat the way the LM75 does. I.e., +a lower temp than THYST actuall triggers an alarm instead of +clearing it. Weird, ey? --Phil */ +#define adm1021_INIT_TOS 60 +#define adm1021_INIT_THYST 20 +#define adm1021_INIT_REMOTE_TOS 60 +#define adm1021_INIT_REMOTE_THYST 20 + +/* Each client has this additional data */ +struct adm1021_data { + int sysctl_id; + + struct semaphore update_lock; + char valid; /* !=0 if following fields are valid */ + unsigned long last_updated; /* In jiffies */ + + u8 temp,temp_os,temp_hyst; /* Register values */ + u8 remote_temp,remote_temp_os,remote_temp_hyst,status,die_code; +}; + +#ifdef MODULE +extern int init_module(void); +extern int cleanup_module(void); +#endif /* MODULE */ + +static int adm1021_init(void); +static int adm1021_cleanup(void); +static int adm1021_attach_adapter(struct i2c_adapter *adapter); +static int adm1021_detach_client(struct i2c_client *client); +static int adm1021_command(struct i2c_client *client, unsigned int cmd, + void *arg); +static void adm1021_inc_use (struct i2c_client *client); +static void adm1021_dec_use (struct i2c_client *client); +static u16 swap_bytes(u16 val); +static int adm1021_read_value(struct i2c_client *client, u8 reg); +static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value); +static void adm1021_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_remote_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_status(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_die_code(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_update_client(struct i2c_client *client); + + +/* This is the driver that will be inserted */ +static struct i2c_driver adm1021_driver = { + /* name */ "adm1021 sensor chip driver", + /* id */ I2C_DRIVERID_ADM1021, + /* flags */ DF_NOTIFY, + /* attach_adapter */ &adm1021_attach_adapter, + /* detach_client */ &adm1021_detach_client, + /* command */ &adm1021_command, + /* inc_use */ &adm1021_inc_use, + /* dec_use */ &adm1021_dec_use +}; + +/* These files are created for each detected adm1021. This is just a template; + though at first sight, you might think we could use a statically + allocated list, we need some way to get back to the parent - which + is done through one of the 'extra' fields which are initialized + when a new copy is allocated. */ +static ctl_table adm1021_dir_table_template[] = { + { ADM1021_SYSCTL_TEMP, "temp", NULL, 0, 0644, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_temp }, + {ADM1021_SYSCTL_REMOTE_TEMP, "remote_temp", NULL, 0, 0644, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_remote_temp }, + {ADM1021_SYSCTL_DIE_CODE, "die_code", NULL, 0, 0444, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_die_code }, + {ADM1021_SYSCTL_STATUS, "status", NULL, 0, 0444, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_status }, + { 0 } +}; + +/* Used by init/cleanup */ +static int adm1021_initialized = 0; + +/* I choose here for semi-static allocation. Complete dynamic + allocation could also be used; the code needed for this would probably + take more memory than the datastructure takes now. */ +#define MAX_adm1021_NR 9 +static struct i2c_client *adm1021_list[MAX_adm1021_NR]; + + +int adm1021_attach_adapter(struct i2c_adapter *adapter) +{ + int address,err,i; + struct i2c_client *new_client; + struct adm1021_data *data; + + err = 0; + + /* OK, this is no detection. I know. It will do for now, though. */ + + /* Set err only if a global error would make registering other clients + impossible too (like out-of-memory). */ + for (address = 0x18; (! err) && (address <= 0x54); address ++) { + /* Legal addresses: 0x18,19,20, 29,30,31, 52,53,54 + (Notice the two jumps) */ + if (address==0x21) { address=0x28; continue;} + if (address==0x32) { address=0x51; continue;} + /* Later on, we will keep a list of registered addresses for each + adapter, and check whether they are used here */ + + /* Verify device is 1021 by checked special DEVICE_ID + register (I wish all SMBus chips had this..) */ + if (smbus_read_byte_data(adapter,address,adm1021_REG_DEVICE_ID) != 0x41){ + continue; + } else { + printk("dm1021.o: Device found and passed detection.\n"); + } + /* continue; */ + + /* Allocate space for a new client structure */ + if (! (new_client = kmalloc(sizeof(struct i2c_client) + + sizeof(struct adm1021_data), + GFP_KERNEL))) { + err = -ENOMEM; + continue; + } + + /* Find a place in our global list */ + for (i = 0; i < MAX_adm1021_NR; i++) + if (! adm1021_list[i]) + break; + if (i == MAX_adm1021_NR) { + err = -ENOMEM; + printk("adm1021.o: No empty slots left, recompile and heighten " + "MAX_adm1021_NR!\n"); + goto ERROR1; + } + adm1021_list[i] = new_client; + + /* Fill the new client structure with data */ + data = (struct adm1021_data *) (new_client + 1); + new_client->data = data; + new_client->id = i; + new_client->addr = address; + new_client->adapter = adapter; + new_client->driver = &adm1021_driver; + strcpy(new_client->name,"adm1021 chip"); + data->valid = 0; + data->update_lock = MUTEX; + + /* Tell i2c-core a new client has arrived */ + if ((err = i2c_attach_client(new_client))) + goto ERROR2; + + /* Register a new directory entry with module sensors */ + if ((err = sensors_register_entry(new_client,"adm1021", + adm1021_dir_table_template)) < 0) + goto ERROR3; + data->sysctl_id = err; + err = 0; + + /* Initialize the adm1021 chip */ + adm1021_write_value(new_client,adm1021_REG_TOS_W, + TEMP_TO_REG(adm1021_INIT_TOS)); + adm1021_write_value(new_client,adm1021_REG_THYST_W, + TEMP_TO_REG(adm1021_INIT_THYST)); + adm1021_write_value(new_client,adm1021_REG_REMOTE_TOS_W, + TEMP_TO_REG(adm1021_INIT_REMOTE_TOS)); + adm1021_write_value(new_client,adm1021_REG_REMOTE_THYST_W, + TEMP_TO_REG(adm1021_INIT_REMOTE_THYST)); + adm1021_write_value(new_client,adm1021_REG_CONFIG_W,0); /* Enable ADC and disable suspend mode */ + /* Set Conversion rate to 1/sec (this can be tinkered with) */ + adm1021_write_value(new_client,adm1021_REG_CONV_RATE_W,0x04); + + continue; +/* OK, this is not exactly good programming practice, usually. But it is + very code-efficient in this case. */ + +ERROR3: + i2c_detach_client(new_client); +ERROR2: + adm1021_list[i] = NULL; +ERROR1: + kfree(new_client); + } + return err; +} + +int adm1021_detach_client(struct i2c_client *client) +{ + int err,i; + for (i = 0; i < MAX_adm1021_NR; i++) + if (client == adm1021_list[i]) + break; + if ((i == MAX_adm1021_NR)) { + printk("adm1021.o: Client to detach not found.\n"); + return -ENOENT; + } + + sensors_deregister_entry(((struct adm1021_data *)(client->data))->sysctl_id); + + if ((err = i2c_detach_client(client))) { + printk("adm1021.o: Client deregistration failed, client not detached.\n"); + return err; + } + + adm1021_list[i] = NULL; + kfree(client); + return 0; +} + + +/* No commands defined yet */ +int adm1021_command(struct i2c_client *client, unsigned int cmd, void *arg) +{ + return 0; +} + +void adm1021_inc_use (struct i2c_client *client) +{ +#ifdef MODULE + MOD_INC_USE_COUNT; +#endif +} + +void adm1021_dec_use (struct i2c_client *client) +{ +#ifdef MODULE + MOD_DEC_USE_COUNT; +#endif +} + +u16 swap_bytes(u16 val) +{ + return (val >> 8) | (val << 8); +} + +/* All registers are byte-sized */ +int adm1021_read_value(struct i2c_client *client, u8 reg) +{ + return smbus_read_byte_data(client->adapter,client->addr,reg); +} + +int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value) +{ + return smbus_write_byte_data(client->adapter,client->addr,reg,value); +} + +void adm1021_update_client(struct i2c_client *client) +{ + struct adm1021_data *data = client->data; + + down(&data->update_lock); + + if ((jiffies - data->last_updated > HZ+HZ/2 ) || + (jiffies < data->last_updated) || ! data->valid) { + +#ifdef DEBUG + printk("Starting adm1021 update\n"); +#endif + + data->temp = adm1021_read_value(client,adm1021_REG_TEMP); + data->temp_os = adm1021_read_value(client,adm1021_REG_TOS_R); + data->temp_hyst = adm1021_read_value(client,adm1021_REG_THYST_R); + data->remote_temp = adm1021_read_value(client,adm1021_REG_REMOTE_TEMP); + data->remote_temp_os = adm1021_read_value(client,adm1021_REG_REMOTE_TOS_R); + data->remote_temp_hyst = adm1021_read_value(client,adm1021_REG_REMOTE_THYST_R); + data->die_code = adm1021_read_value(client,adm1021_REG_DIE_CODE); + data->status = adm1021_read_value(client,adm1021_REG_STATUS); + data->last_updated = jiffies; + data->valid = 1; + } + + up(&data->update_lock); +} + + +void adm1021_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = TEMP_FROM_REG(data->temp_os); + results[1] = TEMP_FROM_REG(data->temp_hyst); + results[2] = TEMP_FROM_REG(data->temp); + *nrels_mag = 3; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + if (*nrels_mag >= 1) { + data->temp_os = TEMP_TO_REG(results[0]); + adm1021_write_value(client,adm1021_REG_TOS_W,data->temp_os); + } + if (*nrels_mag >= 2) { + data->temp_hyst = TEMP_TO_REG(results[1]); + adm1021_write_value(client,adm1021_REG_THYST_W,data->temp_hyst); + } + } +} + +void adm1021_remote_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = TEMP_FROM_REG(data->remote_temp_os); + results[1] = TEMP_FROM_REG(data->remote_temp_hyst); + results[2] = TEMP_FROM_REG(data->remote_temp); + *nrels_mag = 3; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + if (*nrels_mag >= 1) { + data->remote_temp_os = TEMP_TO_REG(results[0]); + adm1021_write_value(client,adm1021_REG_REMOTE_TOS_W,data->remote_temp_os); + } + if (*nrels_mag >= 2) { + data->remote_temp_hyst = TEMP_TO_REG(results[1]); + adm1021_write_value(client,adm1021_REG_REMOTE_THYST_W,data->remote_temp_hyst); + } + } +} + +void adm1021_die_code(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = data->die_code; + *nrels_mag = 1; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + /* Can't write to it */ + } +} + +void adm1021_status(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = data->status; + *nrels_mag = 1; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + /* Can't write to it */ + } +} + +int adm1021_init(void) +{ + int res; + + printk("adm1021.o version %s (%s)\n",LM_VERSION,LM_DATE); + adm1021_initialized = 0; + if ((res = i2c_add_driver(&adm1021_driver))) { + printk("adm1021.o: Driver registration failed, module not inserted.\n"); + adm1021_cleanup(); + return res; + } + adm1021_initialized ++; + return 0; +} + +int adm1021_cleanup(void) +{ + int res; + + if (adm1021_initialized >= 1) { + if ((res = i2c_del_driver(&adm1021_driver))) { + printk("adm1021.o: Driver deregistration failed, module not removed.\n"); + return res; + } + adm1021_initialized --; + } + + return 0; +} + + +#ifdef MODULE + +MODULE_AUTHOR("Frodo Looijaard and Philip Edelbrock "); +MODULE_DESCRIPTION("adm1021 driver"); + +int init_module(void) +{ + return adm1021_init(); +} + +int cleanup_module(void) +{ + return adm1021_cleanup(); +} + +#endif /* MODULE */ + diff --git a/kernel/include/sensors.h b/kernel/include/sensors.h index 575a29db..32cbd963 100644 --- a/kernel/include/sensors.h +++ b/kernel/include/sensors.h @@ -88,6 +88,7 @@ extern void sensors_deregister_entry(int id); #define I2C_DRIVERID_EEPROM 1005 #define I2C_DRIVERID_W83781D 1006 #define I2C_DRIVERID_LM80 1007 +#define I2C_DRIVERID_ADM1021 1008 /* Sysctl IDs */ #ifdef DEV_HWMON @@ -168,6 +169,11 @@ struct sensors_chips_data { #define LM75_SYSCTL_TEMP 1200 /* Degrees Celcius * 10 */ +#define ADM1021_SYSCTL_TEMP 1200 +#define ADM1021_SYSCTL_REMOTE_TEMP 1201 +#define ADM1021_SYSCTL_DIE_CODE 1202 +#define ADM1021_SYSCTL_STATUS 1203 + #define GL518_SYSCTL_VDD 1000 /* Volts * 100 */ #define GL518_SYSCTL_VIN1 1001 #define GL518_SYSCTL_VIN2 1002 diff --git a/src/Module.mk b/src/Module.mk index afb57a60..f4ad9367 100644 --- a/src/Module.mk +++ b/src/Module.mk @@ -27,7 +27,7 @@ SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o $(MODULE_DIR)/isa.o \ $(MODULE_DIR)/i2c-proc.o $(MODULE_DIR)/lm75.o \ $(MODULE_DIR)/i2c-proc.o $(MODULE_DIR)/gl518sm.o \ $(MODULE_DIR)/eeprom.o $(MODULE_DIR)/w83781d.o \ - $(MODULE_DIR)/lm80.o + $(MODULE_DIR)/lm80.o $(MODULE_DIR)/adm1021.o SRCHEADERFILES := $(MODULE_DIR)/sensors.h $(MODULE_DIR)/isa.h \ $(MODULE_DIR)/smbus.h diff --git a/src/adm1021.c b/src/adm1021.c new file mode 100644 index 00000000..f16c2d29 --- /dev/null +++ b/src/adm1021.c @@ -0,0 +1,460 @@ +/* + adm1021.c - Part of lm_sensors, Linux kernel modules for hardware + monitoring + Copyright (c) 1998 Frodo Looijaard and + Philip Edelbrock + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include "smbus.h" +#include "sensors.h" +#include "i2c.h" +#include "version.h" + +/* adm1021 constants specified below */ + +/* The adm1021 registers */ +/* Read-only */ +#define adm1021_REG_TEMP 0x00 +#define adm1021_REG_REMOTE_TEMP 0x01 +#define adm1021_REG_STATUS 0x02 +#define adm1021_REG_DEVICE_ID 0x0FE /* should always read 0x41 */ +#define adm1021_REG_DIE_CODE 0x0FF +/* These use different addresses for reading/writing */ +#define adm1021_REG_CONFIG_R 0x03 +#define adm1021_REG_CONFIG_W 0x09 +#define adm1021_REG_CONV_RATE_R 0x04 +#define adm1021_REG_CONV_RATE_W 0x0A +#define adm1021_REG_TOS_R 0x05 +#define adm1021_REG_TOS_W 0x0B +#define adm1021_REG_REMOTE_TOS_R 0x07 +#define adm1021_REG_REMOTE_TOS_W 0x0D +#define adm1021_REG_THYST_R 0x06 +#define adm1021_REG_THYST_W 0x0C +#define adm1021_REG_REMOTE_THYST_R 0x08 +#define adm1021_REG_REMOTE_THYST_W 0x0E +/* write-only */ +#define adm1021_REG_ONESHOT 0x0F + + +/* Conversions note: 1021 uses normal integer signed-byte format*/ +#define TEMP_FROM_REG(val) (val > 127 ? val-256 : val) +#define TEMP_TO_REG(val) (val < 0 ? val+256 : val) + +/* Initial values */ + +/* Note: Eventhough I left the low and high limits named os and hyst, +they don't quite work like a thermostat the way the LM75 does. I.e., +a lower temp than THYST actuall triggers an alarm instead of +clearing it. Weird, ey? --Phil */ +#define adm1021_INIT_TOS 60 +#define adm1021_INIT_THYST 20 +#define adm1021_INIT_REMOTE_TOS 60 +#define adm1021_INIT_REMOTE_THYST 20 + +/* Each client has this additional data */ +struct adm1021_data { + int sysctl_id; + + struct semaphore update_lock; + char valid; /* !=0 if following fields are valid */ + unsigned long last_updated; /* In jiffies */ + + u8 temp,temp_os,temp_hyst; /* Register values */ + u8 remote_temp,remote_temp_os,remote_temp_hyst,status,die_code; +}; + +#ifdef MODULE +extern int init_module(void); +extern int cleanup_module(void); +#endif /* MODULE */ + +static int adm1021_init(void); +static int adm1021_cleanup(void); +static int adm1021_attach_adapter(struct i2c_adapter *adapter); +static int adm1021_detach_client(struct i2c_client *client); +static int adm1021_command(struct i2c_client *client, unsigned int cmd, + void *arg); +static void adm1021_inc_use (struct i2c_client *client); +static void adm1021_dec_use (struct i2c_client *client); +static u16 swap_bytes(u16 val); +static int adm1021_read_value(struct i2c_client *client, u8 reg); +static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value); +static void adm1021_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_remote_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_status(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_die_code(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results); +static void adm1021_update_client(struct i2c_client *client); + + +/* This is the driver that will be inserted */ +static struct i2c_driver adm1021_driver = { + /* name */ "adm1021 sensor chip driver", + /* id */ I2C_DRIVERID_ADM1021, + /* flags */ DF_NOTIFY, + /* attach_adapter */ &adm1021_attach_adapter, + /* detach_client */ &adm1021_detach_client, + /* command */ &adm1021_command, + /* inc_use */ &adm1021_inc_use, + /* dec_use */ &adm1021_dec_use +}; + +/* These files are created for each detected adm1021. This is just a template; + though at first sight, you might think we could use a statically + allocated list, we need some way to get back to the parent - which + is done through one of the 'extra' fields which are initialized + when a new copy is allocated. */ +static ctl_table adm1021_dir_table_template[] = { + { ADM1021_SYSCTL_TEMP, "temp", NULL, 0, 0644, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_temp }, + {ADM1021_SYSCTL_REMOTE_TEMP, "remote_temp", NULL, 0, 0644, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_remote_temp }, + {ADM1021_SYSCTL_DIE_CODE, "die_code", NULL, 0, 0444, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_die_code }, + {ADM1021_SYSCTL_STATUS, "status", NULL, 0, 0444, NULL, &sensors_proc_real, + &sensors_sysctl_real, NULL, &adm1021_status }, + { 0 } +}; + +/* Used by init/cleanup */ +static int adm1021_initialized = 0; + +/* I choose here for semi-static allocation. Complete dynamic + allocation could also be used; the code needed for this would probably + take more memory than the datastructure takes now. */ +#define MAX_adm1021_NR 9 +static struct i2c_client *adm1021_list[MAX_adm1021_NR]; + + +int adm1021_attach_adapter(struct i2c_adapter *adapter) +{ + int address,err,i; + struct i2c_client *new_client; + struct adm1021_data *data; + + err = 0; + + /* OK, this is no detection. I know. It will do for now, though. */ + + /* Set err only if a global error would make registering other clients + impossible too (like out-of-memory). */ + for (address = 0x18; (! err) && (address <= 0x54); address ++) { + /* Legal addresses: 0x18,19,20, 29,30,31, 52,53,54 + (Notice the two jumps) */ + if (address==0x21) { address=0x28; continue;} + if (address==0x32) { address=0x51; continue;} + /* Later on, we will keep a list of registered addresses for each + adapter, and check whether they are used here */ + + /* Verify device is 1021 by checked special DEVICE_ID + register (I wish all SMBus chips had this..) */ + if (smbus_read_byte_data(adapter,address,adm1021_REG_DEVICE_ID) != 0x41){ + continue; + } else { + printk("dm1021.o: Device found and passed detection.\n"); + } + /* continue; */ + + /* Allocate space for a new client structure */ + if (! (new_client = kmalloc(sizeof(struct i2c_client) + + sizeof(struct adm1021_data), + GFP_KERNEL))) { + err = -ENOMEM; + continue; + } + + /* Find a place in our global list */ + for (i = 0; i < MAX_adm1021_NR; i++) + if (! adm1021_list[i]) + break; + if (i == MAX_adm1021_NR) { + err = -ENOMEM; + printk("adm1021.o: No empty slots left, recompile and heighten " + "MAX_adm1021_NR!\n"); + goto ERROR1; + } + adm1021_list[i] = new_client; + + /* Fill the new client structure with data */ + data = (struct adm1021_data *) (new_client + 1); + new_client->data = data; + new_client->id = i; + new_client->addr = address; + new_client->adapter = adapter; + new_client->driver = &adm1021_driver; + strcpy(new_client->name,"adm1021 chip"); + data->valid = 0; + data->update_lock = MUTEX; + + /* Tell i2c-core a new client has arrived */ + if ((err = i2c_attach_client(new_client))) + goto ERROR2; + + /* Register a new directory entry with module sensors */ + if ((err = sensors_register_entry(new_client,"adm1021", + adm1021_dir_table_template)) < 0) + goto ERROR3; + data->sysctl_id = err; + err = 0; + + /* Initialize the adm1021 chip */ + adm1021_write_value(new_client,adm1021_REG_TOS_W, + TEMP_TO_REG(adm1021_INIT_TOS)); + adm1021_write_value(new_client,adm1021_REG_THYST_W, + TEMP_TO_REG(adm1021_INIT_THYST)); + adm1021_write_value(new_client,adm1021_REG_REMOTE_TOS_W, + TEMP_TO_REG(adm1021_INIT_REMOTE_TOS)); + adm1021_write_value(new_client,adm1021_REG_REMOTE_THYST_W, + TEMP_TO_REG(adm1021_INIT_REMOTE_THYST)); + adm1021_write_value(new_client,adm1021_REG_CONFIG_W,0); /* Enable ADC and disable suspend mode */ + /* Set Conversion rate to 1/sec (this can be tinkered with) */ + adm1021_write_value(new_client,adm1021_REG_CONV_RATE_W,0x04); + + continue; +/* OK, this is not exactly good programming practice, usually. But it is + very code-efficient in this case. */ + +ERROR3: + i2c_detach_client(new_client); +ERROR2: + adm1021_list[i] = NULL; +ERROR1: + kfree(new_client); + } + return err; +} + +int adm1021_detach_client(struct i2c_client *client) +{ + int err,i; + for (i = 0; i < MAX_adm1021_NR; i++) + if (client == adm1021_list[i]) + break; + if ((i == MAX_adm1021_NR)) { + printk("adm1021.o: Client to detach not found.\n"); + return -ENOENT; + } + + sensors_deregister_entry(((struct adm1021_data *)(client->data))->sysctl_id); + + if ((err = i2c_detach_client(client))) { + printk("adm1021.o: Client deregistration failed, client not detached.\n"); + return err; + } + + adm1021_list[i] = NULL; + kfree(client); + return 0; +} + + +/* No commands defined yet */ +int adm1021_command(struct i2c_client *client, unsigned int cmd, void *arg) +{ + return 0; +} + +void adm1021_inc_use (struct i2c_client *client) +{ +#ifdef MODULE + MOD_INC_USE_COUNT; +#endif +} + +void adm1021_dec_use (struct i2c_client *client) +{ +#ifdef MODULE + MOD_DEC_USE_COUNT; +#endif +} + +u16 swap_bytes(u16 val) +{ + return (val >> 8) | (val << 8); +} + +/* All registers are byte-sized */ +int adm1021_read_value(struct i2c_client *client, u8 reg) +{ + return smbus_read_byte_data(client->adapter,client->addr,reg); +} + +int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value) +{ + return smbus_write_byte_data(client->adapter,client->addr,reg,value); +} + +void adm1021_update_client(struct i2c_client *client) +{ + struct adm1021_data *data = client->data; + + down(&data->update_lock); + + if ((jiffies - data->last_updated > HZ+HZ/2 ) || + (jiffies < data->last_updated) || ! data->valid) { + +#ifdef DEBUG + printk("Starting adm1021 update\n"); +#endif + + data->temp = adm1021_read_value(client,adm1021_REG_TEMP); + data->temp_os = adm1021_read_value(client,adm1021_REG_TOS_R); + data->temp_hyst = adm1021_read_value(client,adm1021_REG_THYST_R); + data->remote_temp = adm1021_read_value(client,adm1021_REG_REMOTE_TEMP); + data->remote_temp_os = adm1021_read_value(client,adm1021_REG_REMOTE_TOS_R); + data->remote_temp_hyst = adm1021_read_value(client,adm1021_REG_REMOTE_THYST_R); + data->die_code = adm1021_read_value(client,adm1021_REG_DIE_CODE); + data->status = adm1021_read_value(client,adm1021_REG_STATUS); + data->last_updated = jiffies; + data->valid = 1; + } + + up(&data->update_lock); +} + + +void adm1021_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = TEMP_FROM_REG(data->temp_os); + results[1] = TEMP_FROM_REG(data->temp_hyst); + results[2] = TEMP_FROM_REG(data->temp); + *nrels_mag = 3; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + if (*nrels_mag >= 1) { + data->temp_os = TEMP_TO_REG(results[0]); + adm1021_write_value(client,adm1021_REG_TOS_W,data->temp_os); + } + if (*nrels_mag >= 2) { + data->temp_hyst = TEMP_TO_REG(results[1]); + adm1021_write_value(client,adm1021_REG_THYST_W,data->temp_hyst); + } + } +} + +void adm1021_remote_temp(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = TEMP_FROM_REG(data->remote_temp_os); + results[1] = TEMP_FROM_REG(data->remote_temp_hyst); + results[2] = TEMP_FROM_REG(data->remote_temp); + *nrels_mag = 3; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + if (*nrels_mag >= 1) { + data->remote_temp_os = TEMP_TO_REG(results[0]); + adm1021_write_value(client,adm1021_REG_REMOTE_TOS_W,data->remote_temp_os); + } + if (*nrels_mag >= 2) { + data->remote_temp_hyst = TEMP_TO_REG(results[1]); + adm1021_write_value(client,adm1021_REG_REMOTE_THYST_W,data->remote_temp_hyst); + } + } +} + +void adm1021_die_code(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = data->die_code; + *nrels_mag = 1; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + /* Can't write to it */ + } +} + +void adm1021_status(struct i2c_client *client, int operation, int ctl_name, + int *nrels_mag, long *results) +{ + struct adm1021_data *data = client->data; + if (operation == SENSORS_PROC_REAL_INFO) + *nrels_mag = 0; + else if (operation == SENSORS_PROC_REAL_READ) { + adm1021_update_client(client); + results[0] = data->status; + *nrels_mag = 1; + } else if (operation == SENSORS_PROC_REAL_WRITE) { + /* Can't write to it */ + } +} + +int adm1021_init(void) +{ + int res; + + printk("adm1021.o version %s (%s)\n",LM_VERSION,LM_DATE); + adm1021_initialized = 0; + if ((res = i2c_add_driver(&adm1021_driver))) { + printk("adm1021.o: Driver registration failed, module not inserted.\n"); + adm1021_cleanup(); + return res; + } + adm1021_initialized ++; + return 0; +} + +int adm1021_cleanup(void) +{ + int res; + + if (adm1021_initialized >= 1) { + if ((res = i2c_del_driver(&adm1021_driver))) { + printk("adm1021.o: Driver deregistration failed, module not removed.\n"); + return res; + } + adm1021_initialized --; + } + + return 0; +} + + +#ifdef MODULE + +MODULE_AUTHOR("Frodo Looijaard and Philip Edelbrock "); +MODULE_DESCRIPTION("adm1021 driver"); + +int init_module(void) +{ + return adm1021_init(); +} + +int cleanup_module(void) +{ + return adm1021_cleanup(); +} + +#endif /* MODULE */ + diff --git a/src/sensors.h b/src/sensors.h index 575a29db..32cbd963 100644 --- a/src/sensors.h +++ b/src/sensors.h @@ -88,6 +88,7 @@ extern void sensors_deregister_entry(int id); #define I2C_DRIVERID_EEPROM 1005 #define I2C_DRIVERID_W83781D 1006 #define I2C_DRIVERID_LM80 1007 +#define I2C_DRIVERID_ADM1021 1008 /* Sysctl IDs */ #ifdef DEV_HWMON @@ -168,6 +169,11 @@ struct sensors_chips_data { #define LM75_SYSCTL_TEMP 1200 /* Degrees Celcius * 10 */ +#define ADM1021_SYSCTL_TEMP 1200 +#define ADM1021_SYSCTL_REMOTE_TEMP 1201 +#define ADM1021_SYSCTL_DIE_CODE 1202 +#define ADM1021_SYSCTL_STATUS 1203 + #define GL518_SYSCTL_VDD 1000 /* Volts * 100 */ #define GL518_SYSCTL_VIN1 1001 #define GL518_SYSCTL_VIN2 1002