📄 asb100.c
字号:
/* asb100.c - Part of lm_sensors, Linux kernel modules for hardware monitoring Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com> (derived from w83781d.c) Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>, Philip Edelbrock <phil@netroedge.com>, and Mark Studebaker <mdsxyz123@yahoo.com> 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.*//* This driver supports the hardware sensor chips: Asus ASB100 and ASB100-A "BACH". ASB100-A supports pwm1, while plain ASB100 does not. There is no known way for the driver to tell which one is there. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA asb100 7 3 1 4 0x31 0x0694 yes no*/#include <linux/config.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/ioport.h>#include <linux/types.h>#include <linux/i2c.h>#include <linux/i2c-sensor.h>#include <linux/i2c-vid.h>#include <linux/init.h>#include <asm/errno.h>#include <asm/io.h>#include "lm75.h"/* HISTORY: 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6*/#define ASB100_VERSION "1.0.0"/* I2C addresses to scan */static unsigned short normal_i2c[] = { I2C_CLIENT_END };static unsigned short normal_i2c_range[] = { 0x28, 0x2f, I2C_CLIENT_END };/* ISA addresses to scan (none) */static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };/* default VRM to 9.0 instead of 8.2 */#define ASB100_DEFAULT_VRM 90/* Insmod parameters */SENSORS_INSMOD_1(asb100);I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: " "{bus, clientaddr, subclientaddr1, subclientaddr2}");/* Voltage IN registers 0-6 */#define ASB100_REG_IN(nr) (0x20 + (nr))#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))/* FAN IN registers 1-3 */#define ASB100_REG_FAN(nr) (0x28 + (nr))#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))/* TEMPERATURE registers 1-4 */static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])#define ASB100_REG_TEMP2_CONFIG 0x0152#define ASB100_REG_TEMP3_CONFIG 0x0252#define ASB100_REG_CONFIG 0x40#define ASB100_REG_ALARM1 0x41#define ASB100_REG_ALARM2 0x42#define ASB100_REG_SMIM1 0x43#define ASB100_REG_SMIM2 0x44#define ASB100_REG_VID_FANDIV 0x47#define ASB100_REG_I2C_ADDR 0x48#define ASB100_REG_CHIPID 0x49#define ASB100_REG_I2C_SUBADDR 0x4a#define ASB100_REG_PIN 0x4b#define ASB100_REG_IRQ 0x4c#define ASB100_REG_BANK 0x4e#define ASB100_REG_CHIPMAN 0x4f#define ASB100_REG_WCHIPID 0x58/* bit 7 -> enable, bits 0-3 -> duty cycle */#define ASB100_REG_PWM1 0x59/* CONVERSIONS Rounding and limit checking is only done on the TO_REG variants. *//* These constants are a guess, consistent w/ w83781d */#define ASB100_IN_MIN ( 0)#define ASB100_IN_MAX (4080)/* IN: 1/1000 V (0V to 4.08V) REG: 16mV/bit */static u8 IN_TO_REG(unsigned val){ unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX); return (nval + 8) / 16;}static unsigned IN_FROM_REG(u8 reg){ return reg * 16;}static u8 FAN_TO_REG(long rpm, int div){ if (rpm == -1) return 0; if (rpm == 0) return 255; rpm = SENSORS_LIMIT(rpm, 1, 1000000); return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);}static int FAN_FROM_REG(u8 val, int div){ return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);}/* These constants are a guess, consistent w/ w83781d */#define ASB100_TEMP_MIN (-128000)#define ASB100_TEMP_MAX ( 127000)/* TEMP: 0.001C/bit (-128C to +127C) REG: 1C/bit, two's complement */static u8 TEMP_TO_REG(int temp){ int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX); ntemp += (ntemp<0 ? -500 : 500); return (u8)(ntemp / 1000);}static int TEMP_FROM_REG(u8 reg){ return (s8)reg * 1000;}/* PWM: 0 - 255 per sensors documentation REG: (6.25% duty cycle per bit) */static u8 ASB100_PWM_TO_REG(int pwm){ pwm = SENSORS_LIMIT(pwm, 0, 255); return (u8)(pwm / 16);}static int ASB100_PWM_FROM_REG(u8 reg){ return reg * 16;}#define ALARMS_FROM_REG(val) (val)#define DIV_FROM_REG(val) (1 << (val))/* FAN DIV: 1, 2, 4, or 8 (defaults to 2) REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */static u8 DIV_TO_REG(long val){ return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;}/* For each registered client, we need to keep some data in memory. That data is pointed to by client->data. The structure itself is dynamically allocated, at the same time the client itself is allocated. */struct asb100_data { struct i2c_client client; struct semaphore lock; enum chips type; struct semaphore update_lock; unsigned long last_updated; /* In jiffies */ /* array of 2 pointers to subclients */ struct i2c_client *lm75[2]; char valid; /* !=0 if following fields are valid */ u8 in[7]; /* Register value */ u8 in_max[7]; /* Register value */ u8 in_min[7]; /* Register value */ u8 fan[3]; /* Register value */ u8 fan_min[3]; /* Register value */ u16 temp[4]; /* Register value (0 and 3 are u8 only) */ u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */ u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */ u8 fan_div[3]; /* Register encoding, right justified */ u8 pwm; /* Register encoding */ u8 vid; /* Register encoding, combined */ u32 alarms; /* Register encoding, combined */ u8 vrm;};static int asb100_read_value(struct i2c_client *client, u16 reg);static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);static int asb100_attach_adapter(struct i2c_adapter *adapter);static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);static int asb100_detach_client(struct i2c_client *client);static struct asb100_data *asb100_update_device(struct device *dev);static void asb100_init_client(struct i2c_client *client);static struct i2c_driver asb100_driver = { .owner = THIS_MODULE, .name = "asb100", .id = I2C_DRIVERID_ASB100, .flags = I2C_DF_NOTIFY, .attach_adapter = asb100_attach_adapter, .detach_client = asb100_detach_client,};/* 7 Voltages */#define show_in_reg(reg) \static ssize_t show_##reg (struct device *dev, char *buf, int nr) \{ \ struct asb100_data *data = asb100_update_device(dev); \ return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \}show_in_reg(in)show_in_reg(in_min)show_in_reg(in_max)#define set_in_reg(REG, reg) \static ssize_t set_in_##reg(struct device *dev, const char *buf, \ size_t count, int nr) \{ \ struct i2c_client *client = to_i2c_client(dev); \ struct asb100_data *data = i2c_get_clientdata(client); \ unsigned long val = simple_strtoul(buf, NULL, 10); \ data->in_##reg[nr] = IN_TO_REG(val); \ asb100_write_value(client, ASB100_REG_IN_##REG(nr), \ data->in_##reg[nr]); \ return count; \}set_in_reg(MIN, min)set_in_reg(MAX, max)#define sysfs_in(offset) \static ssize_t \ show_in##offset (struct device *dev, char *buf) \{ \ return show_in(dev, buf, 0x##offset); \} \static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ show_in##offset, NULL); \static ssize_t \ show_in##offset##_min (struct device *dev, char *buf) \{ \ return show_in_min(dev, buf, 0x##offset); \} \static ssize_t \ show_in##offset##_max (struct device *dev, char *buf) \{ \ return show_in_max(dev, buf, 0x##offset); \} \static ssize_t set_in##offset##_min (struct device *dev, \ const char *buf, size_t count) \{ \ return set_in_min(dev, buf, count, 0x##offset); \} \static ssize_t set_in##offset##_max (struct device *dev, \ const char *buf, size_t count) \{ \ return set_in_max(dev, buf, count, 0x##offset); \} \static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ show_in##offset##_min, set_in##offset##_min); \static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ show_in##offset##_max, set_in##offset##_max);sysfs_in(0);sysfs_in(1);sysfs_in(2);sysfs_in(3);sysfs_in(4);sysfs_in(5);sysfs_in(6);#define device_create_file_in(client, offset) do { \ device_create_file(&client->dev, &dev_attr_in##offset##_input); \ device_create_file(&client->dev, &dev_attr_in##offset##_min); \ device_create_file(&client->dev, &dev_attr_in##offset##_max); \} while (0)/* 3 Fans */static ssize_t show_fan(struct device *dev, char *buf, int nr){ struct asb100_data *data = asb100_update_device(dev); return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr])));}static ssize_t show_fan_min(struct device *dev, char *buf, int nr){ struct asb100_data *data = asb100_update_device(dev); return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));}static ssize_t show_fan_div(struct device *dev, char *buf, int nr){ struct asb100_data *data = asb100_update_device(dev); return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));}static ssize_t set_fan_min(struct device *dev, const char *buf, size_t count, int nr){ struct i2c_client *client = to_i2c_client(dev); struct asb100_data *data = i2c_get_clientdata(client); u32 val = simple_strtoul(buf, NULL, 10); data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); return count;}/* Note: we save and restore the fan minimum here, because its value is determined in part by the fan divisor. This follows the principle of least suprise; the user doesn't expect the fan minimum to change just because the divisor changed. */static ssize_t set_fan_div(struct device *dev, const char *buf, size_t count, int nr){ struct i2c_client *client = to_i2c_client(dev); struct asb100_data *data = i2c_get_clientdata(client); unsigned long min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); unsigned long val = simple_strtoul(buf, NULL, 10); int reg; data->fan_div[nr] = DIV_TO_REG(val); switch(nr) { case 0: /* fan 1 */ reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); reg = (reg & 0xcf) | (data->fan_div[0] << 4); asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); break; case 1: /* fan 2 */ reg = asb100_read_value(client, ASB100_REG_VID_FANDIV); reg = (reg & 0x3f) | (data->fan_div[1] << 6); asb100_write_value(client, ASB100_REG_VID_FANDIV, reg); break; case 2: /* fan 3 */ reg = asb100_read_value(client, ASB100_REG_PIN); reg = (reg & 0x3f) | (data->fan_div[2] << 6); asb100_write_value(client, ASB100_REG_PIN, reg); break; } data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]); return count;}#define sysfs_fan(offset) \static ssize_t show_fan##offset(struct device *dev, char *buf) \{ \ return show_fan(dev, buf, offset - 1); \} \static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \{ \ return show_fan_min(dev, buf, offset - 1); \} \static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \{ \ return show_fan_div(dev, buf, offset - 1); \} \static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \ size_t count) \{ \ return set_fan_min(dev, buf, count, offset - 1); \} \static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \ size_t count) \{ \ return set_fan_div(dev, buf, count, offset - 1); \} \static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ show_fan##offset, NULL); \static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ show_fan##offset##_min, set_fan##offset##_min); \static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ show_fan##offset##_div, set_fan##offset##_div);sysfs_fan(1);sysfs_fan(2);sysfs_fan(3);#define device_create_file_fan(client, offset) do { \ device_create_file(&client->dev, &dev_attr_fan##offset##_input); \ device_create_file(&client->dev, &dev_attr_fan##offset##_min); \ device_create_file(&client->dev, &dev_attr_fan##offset##_div); \} while (0)/* 4 Temp. Sensors */static int sprintf_temp_from_reg(u16 reg, char *buf, int nr){ int ret = 0; switch (nr) { case 1: case 2: ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg)); break; case 0: case 3: default: ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg)); break; } return ret;} #define show_temp_reg(reg) \static ssize_t show_##reg(struct device *dev, char *buf, int nr) \{ \ struct asb100_data *data = asb100_update_device(dev); \ return sprintf_temp_from_reg(data->reg[nr], buf, nr); \}show_temp_reg(temp);show_temp_reg(temp_max);show_temp_reg(temp_hyst);#define set_temp_reg(REG, reg) \static ssize_t set_##reg(struct device *dev, const char *buf, \ size_t count, int nr) \{ \ struct i2c_client *client = to_i2c_client(dev); \ struct asb100_data *data = i2c_get_clientdata(client); \ unsigned long val = simple_strtoul(buf, NULL, 10); \ switch (nr) { \ case 1: case 2: \ data->reg[nr] = LM75_TEMP_TO_REG(val); \ break; \ case 0: case 3: default: \ data->reg[nr] = TEMP_TO_REG(val); \ break; \ } \ asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \ data->reg[nr]); \ return count; \}set_temp_reg(MAX, temp_max);set_temp_reg(HYST, temp_hyst);#define sysfs_temp(num) \static ssize_t show_temp##num(struct device *dev, char *buf) \{ \ return show_temp(dev, buf, num-1); \} \static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \static ssize_t show_temp_max##num(struct device *dev, char *buf) \{ \ return show_temp_max(dev, buf, num-1); \} \static ssize_t set_temp_max##num(struct device *dev, const char *buf, \ size_t count) \{ \ return set_temp_max(dev, buf, count, num-1); \} \static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \ show_temp_max##num, set_temp_max##num); \static ssize_t show_temp_hyst##num(struct device *dev, char *buf) \{ \ return show_temp_hyst(dev, buf, num-1); \} \static ssize_t set_temp_hyst##num(struct device *dev, const char *buf, \ size_t count) \{ \ return set_temp_hyst(dev, buf, count, num-1); \} \static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \ show_temp_hyst##num, set_temp_hyst##num);sysfs_temp(1);sysfs_temp(2);sysfs_temp(3);sysfs_temp(4);/* VID */#define device_create_file_temp(client, num) do { \ device_create_file(&client->dev, &dev_attr_temp##num##_input); \ device_create_file(&client->dev, &dev_attr_temp##num##_max); \ device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \} while (0)static ssize_t show_vid(struct device *dev, char *buf){ struct asb100_data *data = asb100_update_device(dev); return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));}static DEVICE_ATTR(in0_ref, S_IRUGO, show_vid, NULL);#define device_create_file_vid(client) \device_create_file(&client->dev, &dev_attr_in0_ref)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -