⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lm80.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * lm80.c - From lm_sensors, Linux kernel modules for hardware * monitoring * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl> * and Philip Edelbrock <phil@netroedge.com> * * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org> * * 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 <linux/config.h>#include <linux/module.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/i2c.h>#include <linux/i2c-sensor.h>/* Addresses to scan */static unsigned short normal_i2c[] = { I2C_CLIENT_END };static unsigned short normal_i2c_range[] = { 0x28, 0x2f, I2C_CLIENT_END };static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };/* Insmod parameters */SENSORS_INSMOD_1(lm80);/* Many LM80 constants specified below *//* The LM80 registers */#define LM80_REG_IN_MAX(nr)		(0x2a + (nr) * 2)#define LM80_REG_IN_MIN(nr)		(0x2b + (nr) * 2)#define LM80_REG_IN(nr)			(0x20 + (nr))#define LM80_REG_FAN1			0x28#define LM80_REG_FAN2			0x29#define LM80_REG_FAN_MIN(nr)		(0x3b + (nr))#define LM80_REG_TEMP			0x27#define LM80_REG_TEMP_HOT_MAX		0x38#define LM80_REG_TEMP_HOT_HYST		0x39#define LM80_REG_TEMP_OS_MAX		0x3a#define LM80_REG_TEMP_OS_HYST		0x3b#define LM80_REG_CONFIG			0x00#define LM80_REG_ALARM1			0x01#define LM80_REG_ALARM2			0x02#define LM80_REG_MASK1			0x03#define LM80_REG_MASK2			0x04#define LM80_REG_FANDIV			0x05#define LM80_REG_RES			0x06/* Conversions. Rounding and limit checking is only done on the TO_REG   variants. Note that you should be a bit careful with which arguments   these macros are called: arguments may be evaluated more than once.   Fixing this is just not worth it. */#define IN_TO_REG(val)		(SENSORS_LIMIT(((val)+5)/10,0,255))#define IN_FROM_REG(val)	((val)*10)static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div){	if (rpm == 0)		return 255;	rpm = SENSORS_LIMIT(rpm, 1, 1000000);	return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);}#define FAN_FROM_REG(val,div)	((val)==0?-1:\				(val)==255?0:1350000/((div)*(val)))static inline long TEMP_FROM_REG(u16 temp){	long res;	temp >>= 4;	if (temp < 0x0800)		res = 625 * (long) temp;	else		res = ((long) temp - 0x01000) * 625;	return res / 10;}#define TEMP_LIMIT_FROM_REG(val)	(((val)>0x80?(val)-0x100:(val))*1000)#define TEMP_LIMIT_TO_REG(val)		SENSORS_LIMIT((val)<0?\					((val)-500)/1000:((val)+500)/1000,0,255)#define ALARMS_FROM_REG(val)		(val)#define DIV_FROM_REG(val)		(1 << (val))#define DIV_TO_REG(val)			((val)==8?3:(val)==4?2:(val)==1?0:1)/* * Client data (each client gets its own) */struct lm80_data {	struct i2c_client client;	struct semaphore update_lock;	char valid;		/* !=0 if following fields are valid */	unsigned long last_updated;	/* In jiffies */	u8 in[7];		/* Register value */	u8 in_max[7];		/* Register value */	u8 in_min[7];		/* Register value */	u8 fan[2];		/* Register value */	u8 fan_min[2];		/* Register value */	u8 fan_div[2];		/* Register encoding, shifted right */	u16 temp;		/* Register values, shifted right */	u8 temp_hot_max;	/* Register value */	u8 temp_hot_hyst;	/* Register value */	u8 temp_os_max;		/* Register value */	u8 temp_os_hyst;	/* Register value */	u16 alarms;		/* Register encoding, combined */};/*  * Functions declaration */static int lm80_attach_adapter(struct i2c_adapter *adapter);static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);static void lm80_init_client(struct i2c_client *client);static int lm80_detach_client(struct i2c_client *client);static struct lm80_data *lm80_update_device(struct device *dev);static int lm80_read_value(struct i2c_client *client, u8 reg);static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);/* * Internal variables */static int lm80_id = 0;/* * Driver data (common to all clients) */static struct i2c_driver lm80_driver = {	.owner		= THIS_MODULE,	.name		= "lm80",	.id		= I2C_DRIVERID_LM80,	.flags		= I2C_DF_NOTIFY,	.attach_adapter	= lm80_attach_adapter,	.detach_client	= lm80_detach_client,};/* * Sysfs stuff */#define show_in(suffix, value) \static ssize_t show_in_##suffix(struct device *dev, char *buf) \{ \	struct lm80_data *data = lm80_update_device(dev); \	return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \}show_in(min0, in_min[0]);show_in(min1, in_min[1]);show_in(min2, in_min[2]);show_in(min3, in_min[3]);show_in(min4, in_min[4]);show_in(min5, in_min[5]);show_in(min6, in_min[6]);show_in(max0, in_max[0]);show_in(max1, in_max[1]);show_in(max2, in_max[2]);show_in(max3, in_max[3]);show_in(max4, in_max[4]);show_in(max5, in_max[5]);show_in(max6, in_max[6]);show_in(input0, in[0]);show_in(input1, in[1]);show_in(input2, in[2]);show_in(input3, in[3]);show_in(input4, in[4]);show_in(input5, in[5]);show_in(input6, in[6]);#define set_in(suffix, value, reg) \static ssize_t set_in_##suffix(struct device *dev, const char *buf, \	size_t count) \{ \	struct i2c_client *client = to_i2c_client(dev); \	struct lm80_data *data = i2c_get_clientdata(client); \	long val = simple_strtol(buf, NULL, 10); \	data->value = IN_TO_REG(val); \	lm80_write_value(client, reg, data->value); \	return count; \}set_in(min0, in_min[0], LM80_REG_IN_MIN(0));set_in(min1, in_min[1], LM80_REG_IN_MIN(1));set_in(min2, in_min[2], LM80_REG_IN_MIN(2));set_in(min3, in_min[3], LM80_REG_IN_MIN(3));set_in(min4, in_min[4], LM80_REG_IN_MIN(4));set_in(min5, in_min[5], LM80_REG_IN_MIN(5));set_in(min6, in_min[6], LM80_REG_IN_MIN(6));set_in(max0, in_max[0], LM80_REG_IN_MAX(0));set_in(max1, in_max[1], LM80_REG_IN_MAX(1));set_in(max2, in_max[2], LM80_REG_IN_MAX(2));set_in(max3, in_max[3], LM80_REG_IN_MAX(3));set_in(max4, in_max[4], LM80_REG_IN_MAX(4));set_in(max5, in_max[5], LM80_REG_IN_MAX(5));set_in(max6, in_max[6], LM80_REG_IN_MAX(6));#define show_fan(suffix, value, div) \static ssize_t show_fan_##suffix(struct device *dev, char *buf) \{ \	struct lm80_data *data = lm80_update_device(dev); \	return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \		       DIV_FROM_REG(data->div))); \}show_fan(min1, fan_min[0], fan_div[0]);show_fan(min2, fan_min[1], fan_div[1]);show_fan(input1, fan[0], fan_div[0]);show_fan(input2, fan[1], fan_div[1]);#define show_fan_div(suffix, value) \static ssize_t show_fan_div##suffix(struct device *dev, char *buf) \{ \	struct lm80_data *data = lm80_update_device(dev); \	return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \}show_fan_div(1, fan_div[0]);show_fan_div(2, fan_div[1]);#define set_fan(suffix, value, reg, div) \static ssize_t set_fan_##suffix(struct device *dev, const char *buf, \	size_t count) \{ \	struct i2c_client *client = to_i2c_client(dev); \	struct lm80_data *data = i2c_get_clientdata(client); \	long val = simple_strtoul(buf, NULL, 10); \	data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \	lm80_write_value(client, reg, data->value); \	return count; \}set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);/* 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 lm80_data *data = i2c_get_clientdata(client);	unsigned long min;	u8 reg;	/* Save fan_min */	min = FAN_FROM_REG(data->fan_min[nr],			   DIV_FROM_REG(data->fan_div[nr]));	data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));	reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))	    | (data->fan_div[nr] << (2 * (nr + 1)));	lm80_write_value(client, LM80_REG_FANDIV, reg);	/* Restore fan_min */	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));	lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);	return count;}#define set_fan_div(number) \static ssize_t set_fan_div##number(struct device *dev, const char *buf, \	size_t count) \{ \	return set_fan_div(dev, buf, count, number - 1); \}set_fan_div(1);set_fan_div(2);static ssize_t show_temp_input1(struct device *dev, char *buf){	struct lm80_data *data = lm80_update_device(dev);	return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -