| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 1 | /* | 
|  | 2 | * adm9240.c	Part of lm_sensors, Linux kernel modules for hardware | 
|  | 3 | * 		monitoring | 
|  | 4 | * | 
|  | 5 | * Copyright (C) 1999	Frodo Looijaard <frodol@dds.nl> | 
|  | 6 | *			Philip Edelbrock <phil@netroedge.com> | 
|  | 7 | * Copyright (C) 2003	Michiel Rook <michiel@grendelproject.nl> | 
| Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 8 | * Copyright (C) 2005	Grant Coady <gcoady.lk@gmail.com> with valuable | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 9 | * 				guidance from Jean Delvare | 
|  | 10 | * | 
|  | 11 | * Driver supports	Analog Devices		ADM9240 | 
|  | 12 | *			Dallas Semiconductor	DS1780 | 
|  | 13 | *			National Semiconductor	LM81 | 
|  | 14 | * | 
|  | 15 | * ADM9240 is the reference, DS1780 and LM81 are register compatibles | 
|  | 16 | * | 
|  | 17 | * Voltage	Six inputs are scaled by chip, VID also reported | 
|  | 18 | * Temperature	Chip temperature to 0.5'C, maximum and max_hysteris | 
|  | 19 | * Fans		2 fans, low speed alarm, automatic fan clock divider | 
|  | 20 | * Alarms	16-bit map of active alarms | 
|  | 21 | * Analog Out	0..1250 mV output | 
|  | 22 | * | 
|  | 23 | * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear' | 
|  | 24 | * | 
|  | 25 | * Test hardware: Intel SE440BX-2 desktop motherboard --Grant | 
|  | 26 | * | 
|  | 27 | * LM81 extended temp reading not implemented | 
|  | 28 | * | 
|  | 29 | * This program is free software; you can redistribute it and/or modify | 
|  | 30 | * it under the terms of the GNU General Public License as published by | 
|  | 31 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 32 | * (at your option) any later version. | 
|  | 33 | * | 
|  | 34 | * This program is distributed in the hope that it will be useful, | 
|  | 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 37 | * GNU General Public License for more details. | 
|  | 38 | * | 
|  | 39 | * You should have received a copy of the GNU General Public License | 
|  | 40 | * along with this program; if not, write to the Free Software | 
|  | 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 42 | */ | 
|  | 43 |  | 
|  | 44 | #include <linux/init.h> | 
|  | 45 | #include <linux/module.h> | 
|  | 46 | #include <linux/slab.h> | 
|  | 47 | #include <linux/i2c.h> | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 48 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 49 | #include <linux/hwmon.h> | 
| Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 50 | #include <linux/hwmon-vid.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 51 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 52 | #include <linux/mutex.h> | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 53 |  | 
|  | 54 | /* Addresses to scan */ | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 55 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 56 | I2C_CLIENT_END }; | 
|  | 57 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 58 | /* Insmod parameters */ | 
| Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 59 | I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 60 |  | 
|  | 61 | /* ADM9240 registers */ | 
|  | 62 | #define ADM9240_REG_MAN_ID		0x3e | 
|  | 63 | #define ADM9240_REG_DIE_REV		0x3f | 
|  | 64 | #define ADM9240_REG_CONFIG		0x40 | 
|  | 65 |  | 
|  | 66 | #define ADM9240_REG_IN(nr)		(0x20 + (nr))   /* 0..5 */ | 
|  | 67 | #define ADM9240_REG_IN_MAX(nr)		(0x2b + (nr) * 2) | 
|  | 68 | #define ADM9240_REG_IN_MIN(nr)		(0x2c + (nr) * 2) | 
|  | 69 | #define ADM9240_REG_FAN(nr)		(0x28 + (nr))   /* 0..1 */ | 
|  | 70 | #define ADM9240_REG_FAN_MIN(nr)		(0x3b + (nr)) | 
|  | 71 | #define ADM9240_REG_INT(nr)		(0x41 + (nr)) | 
|  | 72 | #define ADM9240_REG_INT_MASK(nr)	(0x43 + (nr)) | 
|  | 73 | #define ADM9240_REG_TEMP		0x27 | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 74 | #define ADM9240_REG_TEMP_MAX(nr)	(0x39 + (nr)) /* 0, 1 = high, hyst */ | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 75 | #define ADM9240_REG_ANALOG_OUT		0x19 | 
|  | 76 | #define ADM9240_REG_CHASSIS_CLEAR	0x46 | 
|  | 77 | #define ADM9240_REG_VID_FAN_DIV		0x47 | 
|  | 78 | #define ADM9240_REG_I2C_ADDR		0x48 | 
|  | 79 | #define ADM9240_REG_VID4		0x49 | 
|  | 80 | #define ADM9240_REG_TEMP_CONF		0x4b | 
|  | 81 |  | 
|  | 82 | /* generalised scaling with integer rounding */ | 
|  | 83 | static inline int SCALE(long val, int mul, int div) | 
|  | 84 | { | 
|  | 85 | if (val < 0) | 
|  | 86 | return (val * mul - div / 2) / div; | 
|  | 87 | else | 
|  | 88 | return (val * mul + div / 2) / div; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | /* adm9240 internally scales voltage measurements */ | 
|  | 92 | static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; | 
|  | 93 |  | 
|  | 94 | static inline unsigned int IN_FROM_REG(u8 reg, int n) | 
|  | 95 | { | 
|  | 96 | return SCALE(reg, nom_mv[n], 192); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | static inline u8 IN_TO_REG(unsigned long val, int n) | 
|  | 100 | { | 
|  | 101 | return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | /* temperature range: -40..125, 127 disables temperature alarm */ | 
|  | 105 | static inline s8 TEMP_TO_REG(long val) | 
|  | 106 | { | 
|  | 107 | return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | /* two fans, each with low fan speed limit */ | 
|  | 111 | static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) | 
|  | 112 | { | 
|  | 113 | if (!reg) /* error */ | 
|  | 114 | return -1; | 
|  | 115 |  | 
|  | 116 | if (reg == 255) | 
|  | 117 | return 0; | 
|  | 118 |  | 
|  | 119 | return SCALE(1350000, 1, reg * div); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | /* analog out 0..1250mV */ | 
|  | 123 | static inline u8 AOUT_TO_REG(unsigned long val) | 
|  | 124 | { | 
|  | 125 | return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | static inline unsigned int AOUT_FROM_REG(u8 reg) | 
|  | 129 | { | 
|  | 130 | return SCALE(reg, 1250, 255); | 
|  | 131 | } | 
|  | 132 |  | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 133 | static int adm9240_probe(struct i2c_client *client, | 
|  | 134 | const struct i2c_device_id *id); | 
|  | 135 | static int adm9240_detect(struct i2c_client *client, int kind, | 
|  | 136 | struct i2c_board_info *info); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 137 | static void adm9240_init_client(struct i2c_client *client); | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 138 | static int adm9240_remove(struct i2c_client *client); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 139 | static struct adm9240_data *adm9240_update_device(struct device *dev); | 
|  | 140 |  | 
|  | 141 | /* driver data */ | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 142 | static const struct i2c_device_id adm9240_id[] = { | 
|  | 143 | { "adm9240", adm9240 }, | 
|  | 144 | { "ds1780", ds1780 }, | 
|  | 145 | { "lm81", lm81 }, | 
|  | 146 | { } | 
|  | 147 | }; | 
|  | 148 | MODULE_DEVICE_TABLE(i2c, adm9240_id); | 
|  | 149 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 150 | static struct i2c_driver adm9240_driver = { | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 151 | .class		= I2C_CLASS_HWMON, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 152 | .driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 153 | .name	= "adm9240", | 
|  | 154 | }, | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 155 | .probe		= adm9240_probe, | 
|  | 156 | .remove		= adm9240_remove, | 
|  | 157 | .id_table	= adm9240_id, | 
|  | 158 | .detect		= adm9240_detect, | 
|  | 159 | .address_data	= &addr_data, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 160 | }; | 
|  | 161 |  | 
|  | 162 | /* per client data */ | 
|  | 163 | struct adm9240_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 164 | struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 165 | struct mutex update_lock; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 166 | char valid; | 
|  | 167 | unsigned long last_updated_measure; | 
|  | 168 | unsigned long last_updated_config; | 
|  | 169 |  | 
|  | 170 | u8 in[6];		/* ro	in0_input */ | 
|  | 171 | u8 in_max[6];		/* rw	in0_max */ | 
|  | 172 | u8 in_min[6];		/* rw	in0_min */ | 
|  | 173 | u8 fan[2];		/* ro	fan1_input */ | 
|  | 174 | u8 fan_min[2];		/* rw	fan1_min */ | 
|  | 175 | u8 fan_div[2];		/* rw	fan1_div, read-only accessor */ | 
|  | 176 | s16 temp;		/* ro	temp1_input, 9-bit sign-extended */ | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 177 | s8 temp_max[2];		/* rw	0 -> temp_max, 1 -> temp_max_hyst */ | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 178 | u16 alarms;		/* ro	alarms */ | 
| Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 179 | u8 aout;		/* rw	aout_output */ | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 180 | u8 vid;			/* ro	vid */ | 
|  | 181 | u8 vrm;			/* --	vrm set on startup, no accessor */ | 
|  | 182 | }; | 
|  | 183 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 184 | /*** sysfs accessors ***/ | 
|  | 185 |  | 
|  | 186 | /* temperature */ | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 187 | static ssize_t show_temp(struct device *dev, struct device_attribute *dummy, | 
|  | 188 | char *buf) | 
|  | 189 | { | 
|  | 190 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 191 | return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */ | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 194 | static ssize_t show_max(struct device *dev, struct device_attribute *devattr, | 
|  | 195 | char *buf) | 
|  | 196 | { | 
|  | 197 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 198 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 199 | return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000); | 
|  | 200 | } | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 201 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 202 | static ssize_t set_max(struct device *dev, struct device_attribute *devattr, | 
|  | 203 | const char *buf, size_t count) | 
|  | 204 | { | 
|  | 205 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 206 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 207 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 208 | long val = simple_strtol(buf, NULL, 10); | 
|  | 209 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 210 | mutex_lock(&data->update_lock); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 211 | data->temp_max[attr->index] = TEMP_TO_REG(val); | 
|  | 212 | i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index), | 
|  | 213 | data->temp_max[attr->index]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 214 | mutex_unlock(&data->update_lock); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 215 | return count; | 
|  | 216 | } | 
|  | 217 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 218 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 219 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, | 
|  | 220 | show_max, set_max, 0); | 
|  | 221 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, | 
|  | 222 | show_max, set_max, 1); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 223 |  | 
|  | 224 | /* voltage */ | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 225 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, | 
|  | 226 | char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 227 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 228 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 229 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 230 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], | 
|  | 231 | attr->index)); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 234 | static ssize_t show_in_min(struct device *dev, | 
|  | 235 | struct device_attribute *devattr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 236 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 237 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 238 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 239 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], | 
|  | 240 | attr->index)); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 243 | static ssize_t show_in_max(struct device *dev, | 
|  | 244 | struct device_attribute *devattr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 245 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 246 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 247 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 248 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], | 
|  | 249 | attr->index)); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 252 | static ssize_t set_in_min(struct device *dev, | 
|  | 253 | struct device_attribute *devattr, | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 254 | const char *buf, size_t count) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 255 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 256 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 257 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 258 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 259 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 260 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 261 | mutex_lock(&data->update_lock); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 262 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); | 
|  | 263 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index), | 
|  | 264 | data->in_min[attr->index]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 265 | mutex_unlock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 266 | return count; | 
|  | 267 | } | 
|  | 268 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 269 | static ssize_t set_in_max(struct device *dev, | 
|  | 270 | struct device_attribute *devattr, | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 271 | const char *buf, size_t count) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 272 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 273 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 274 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 275 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 276 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 277 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 278 | mutex_lock(&data->update_lock); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 279 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); | 
|  | 280 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index), | 
|  | 281 | data->in_max[attr->index]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 282 | mutex_unlock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 283 | return count; | 
|  | 284 | } | 
|  | 285 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 286 | #define vin(nr)							\ | 
|  | 287 | static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, 		\ | 
|  | 288 | show_in, NULL, nr);				\ | 
|  | 289 | static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR,	\ | 
|  | 290 | show_in_min, set_in_min, nr);			\ | 
|  | 291 | static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR,	\ | 
|  | 292 | show_in_max, set_in_max, nr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 293 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 294 | vin(0); | 
|  | 295 | vin(1); | 
|  | 296 | vin(2); | 
|  | 297 | vin(3); | 
|  | 298 | vin(4); | 
|  | 299 | vin(5); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 300 |  | 
|  | 301 | /* fans */ | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 302 | static ssize_t show_fan(struct device *dev, | 
|  | 303 | struct device_attribute *devattr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 304 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 305 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 306 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 307 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], | 
|  | 308 | 1 << data->fan_div[attr->index])); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 311 | static ssize_t show_fan_min(struct device *dev, | 
|  | 312 | struct device_attribute *devattr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 313 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 314 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 315 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 316 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], | 
|  | 317 | 1 << data->fan_div[attr->index])); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 320 | static ssize_t show_fan_div(struct device *dev, | 
|  | 321 | struct device_attribute *devattr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 322 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 323 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 324 | struct adm9240_data *data = adm9240_update_device(dev); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 325 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 326 | } | 
|  | 327 |  | 
|  | 328 | /* write new fan div, callers must hold data->update_lock */ | 
|  | 329 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, | 
|  | 330 | u8 fan_div) | 
|  | 331 | { | 
|  | 332 | u8 reg, old, shift = (nr + 2) * 2; | 
|  | 333 |  | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 334 | reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 335 | old = (reg >> shift) & 3; | 
|  | 336 | reg &= ~(3 << shift); | 
|  | 337 | reg |= (fan_div << shift); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 338 | i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 339 | dev_dbg(&client->dev, "fan%d clock divider changed from %u " | 
|  | 340 | "to %u\n", nr + 1, 1 << old, 1 << fan_div); | 
|  | 341 | } | 
|  | 342 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 343 | /* | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 344 | * set fan speed low limit: | 
|  | 345 | * | 
|  | 346 | * - value is zero: disable fan speed low limit alarm | 
|  | 347 | * | 
|  | 348 | * - value is below fan speed measurement range: enable fan speed low | 
|  | 349 | *   limit alarm to be asserted while fan speed too slow to measure | 
|  | 350 | * | 
|  | 351 | * - otherwise: select fan clock divider to suit fan speed low limit, | 
|  | 352 | *   measurement code may adjust registers to ensure fan speed reading | 
|  | 353 | */ | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 354 | static ssize_t set_fan_min(struct device *dev, | 
|  | 355 | struct device_attribute *devattr, | 
|  | 356 | const char *buf, size_t count) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 357 | { | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 358 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 359 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 360 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 361 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 362 | int nr = attr->index; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 363 | u8 new_div; | 
|  | 364 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 365 | mutex_lock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 366 |  | 
|  | 367 | if (!val) { | 
|  | 368 | data->fan_min[nr] = 255; | 
|  | 369 | new_div = data->fan_div[nr]; | 
|  | 370 |  | 
|  | 371 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", | 
|  | 372 | nr + 1); | 
|  | 373 |  | 
|  | 374 | } else if (val < 1350000 / (8 * 254)) { | 
|  | 375 | new_div = 3; | 
|  | 376 | data->fan_min[nr] = 254; | 
|  | 377 |  | 
|  | 378 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", | 
|  | 379 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); | 
|  | 380 |  | 
|  | 381 | } else { | 
|  | 382 | unsigned int new_min = 1350000 / val; | 
|  | 383 |  | 
|  | 384 | new_div = 0; | 
|  | 385 | while (new_min > 192 && new_div < 3) { | 
|  | 386 | new_div++; | 
|  | 387 | new_min /= 2; | 
|  | 388 | } | 
|  | 389 | if (!new_min) /* keep > 0 */ | 
|  | 390 | new_min++; | 
|  | 391 |  | 
|  | 392 | data->fan_min[nr] = new_min; | 
|  | 393 |  | 
|  | 394 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", | 
|  | 395 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | if (new_div != data->fan_div[nr]) { | 
|  | 399 | data->fan_div[nr] = new_div; | 
|  | 400 | adm9240_write_fan_div(client, nr, new_div); | 
|  | 401 | } | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 402 | i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr), | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 403 | data->fan_min[nr]); | 
|  | 404 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 405 | mutex_unlock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 406 | return count; | 
|  | 407 | } | 
|  | 408 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 409 | #define fan(nr)							\ | 
|  | 410 | static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO,		\ | 
|  | 411 | show_fan, NULL, nr - 1);			\ | 
|  | 412 | static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO,		\ | 
|  | 413 | show_fan_div, NULL, nr - 1);			\ | 
|  | 414 | static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR,	\ | 
|  | 415 | show_fan_min, set_fan_min, nr - 1); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 416 |  | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 417 | fan(1); | 
|  | 418 | fan(2); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 419 |  | 
|  | 420 | /* alarms */ | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 421 | static ssize_t show_alarms(struct device *dev, | 
|  | 422 | struct device_attribute *attr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 423 | { | 
|  | 424 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 425 | return sprintf(buf, "%u\n", data->alarms); | 
|  | 426 | } | 
|  | 427 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
|  | 428 |  | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 429 | static ssize_t show_alarm(struct device *dev, | 
|  | 430 | struct device_attribute *attr, char *buf) | 
|  | 431 | { | 
|  | 432 | int bitnr = to_sensor_dev_attr(attr)->index; | 
|  | 433 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 434 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | 
|  | 435 | } | 
|  | 436 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | 
|  | 437 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | 
|  | 438 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | 
|  | 439 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | 
|  | 440 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | 
|  | 441 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); | 
|  | 442 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | 
|  | 443 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | 
|  | 444 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | 
|  | 445 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 446 | /* vid */ | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 447 | static ssize_t show_vid(struct device *dev, | 
|  | 448 | struct device_attribute *attr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 449 | { | 
|  | 450 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 451 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); | 
|  | 452 | } | 
|  | 453 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | 
|  | 454 |  | 
|  | 455 | /* analog output */ | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 456 | static ssize_t show_aout(struct device *dev, | 
|  | 457 | struct device_attribute *attr, char *buf) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 458 | { | 
|  | 459 | struct adm9240_data *data = adm9240_update_device(dev); | 
|  | 460 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); | 
|  | 461 | } | 
|  | 462 |  | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 463 | static ssize_t set_aout(struct device *dev, | 
|  | 464 | struct device_attribute *attr, | 
|  | 465 | const char *buf, size_t count) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 466 | { | 
|  | 467 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 468 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 469 | unsigned long val = simple_strtol(buf, NULL, 10); | 
|  | 470 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 471 | mutex_lock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 472 | data->aout = AOUT_TO_REG(val); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 473 | i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 474 | mutex_unlock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 475 | return count; | 
|  | 476 | } | 
|  | 477 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); | 
|  | 478 |  | 
|  | 479 | /* chassis_clear */ | 
| Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 480 | static ssize_t chassis_clear(struct device *dev, | 
|  | 481 | struct device_attribute *attr, | 
|  | 482 | const char *buf, size_t count) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 483 | { | 
|  | 484 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 485 | unsigned long val = simple_strtol(buf, NULL, 10); | 
|  | 486 |  | 
|  | 487 | if (val == 1) { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 488 | i2c_smbus_write_byte_data(client, | 
|  | 489 | ADM9240_REG_CHASSIS_CLEAR, 0x80); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 490 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); | 
|  | 491 | } | 
|  | 492 | return count; | 
|  | 493 | } | 
|  | 494 | static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear); | 
|  | 495 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 496 | static struct attribute *adm9240_attributes[] = { | 
|  | 497 | &sensor_dev_attr_in0_input.dev_attr.attr, | 
|  | 498 | &sensor_dev_attr_in0_min.dev_attr.attr, | 
|  | 499 | &sensor_dev_attr_in0_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 500 | &sensor_dev_attr_in0_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 501 | &sensor_dev_attr_in1_input.dev_attr.attr, | 
|  | 502 | &sensor_dev_attr_in1_min.dev_attr.attr, | 
|  | 503 | &sensor_dev_attr_in1_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 504 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 505 | &sensor_dev_attr_in2_input.dev_attr.attr, | 
|  | 506 | &sensor_dev_attr_in2_min.dev_attr.attr, | 
|  | 507 | &sensor_dev_attr_in2_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 508 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 509 | &sensor_dev_attr_in3_input.dev_attr.attr, | 
|  | 510 | &sensor_dev_attr_in3_min.dev_attr.attr, | 
|  | 511 | &sensor_dev_attr_in3_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 512 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 513 | &sensor_dev_attr_in4_input.dev_attr.attr, | 
|  | 514 | &sensor_dev_attr_in4_min.dev_attr.attr, | 
|  | 515 | &sensor_dev_attr_in4_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 516 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 517 | &sensor_dev_attr_in5_input.dev_attr.attr, | 
|  | 518 | &sensor_dev_attr_in5_min.dev_attr.attr, | 
|  | 519 | &sensor_dev_attr_in5_max.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 520 | &sensor_dev_attr_in5_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 521 | &dev_attr_temp1_input.attr, | 
|  | 522 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 
|  | 523 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 524 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 525 | &sensor_dev_attr_fan1_input.dev_attr.attr, | 
|  | 526 | &sensor_dev_attr_fan1_div.dev_attr.attr, | 
|  | 527 | &sensor_dev_attr_fan1_min.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 528 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 529 | &sensor_dev_attr_fan2_input.dev_attr.attr, | 
|  | 530 | &sensor_dev_attr_fan2_div.dev_attr.attr, | 
|  | 531 | &sensor_dev_attr_fan2_min.dev_attr.attr, | 
| Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 532 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 533 | &dev_attr_alarms.attr, | 
|  | 534 | &dev_attr_aout_output.attr, | 
|  | 535 | &dev_attr_chassis_clear.attr, | 
|  | 536 | &dev_attr_cpu0_vid.attr, | 
|  | 537 | NULL | 
|  | 538 | }; | 
|  | 539 |  | 
|  | 540 | static const struct attribute_group adm9240_group = { | 
|  | 541 | .attrs = adm9240_attributes, | 
|  | 542 | }; | 
|  | 543 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 544 |  | 
|  | 545 | /*** sensor chip detect and driver install ***/ | 
|  | 546 |  | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 547 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
|  | 548 | static int adm9240_detect(struct i2c_client *new_client, int kind, | 
|  | 549 | struct i2c_board_info *info) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 550 | { | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 551 | struct i2c_adapter *adapter = new_client->adapter; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 552 | const char *name = ""; | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 553 | int address = new_client->addr; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 554 | u8 man_id, die_rev; | 
|  | 555 |  | 
|  | 556 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 557 | return -ENODEV; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 558 |  | 
|  | 559 | if (kind == 0) { | 
|  | 560 | kind = adm9240; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | if (kind < 0) { | 
|  | 564 |  | 
|  | 565 | /* verify chip: reg address should match i2c address */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 566 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 567 | != address) { | 
|  | 568 | dev_err(&adapter->dev, "detect fail: address match, " | 
|  | 569 | "0x%02x\n", address); | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 570 | return -ENODEV; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 571 | } | 
|  | 572 |  | 
|  | 573 | /* check known chip manufacturer */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 574 | man_id = i2c_smbus_read_byte_data(new_client, | 
|  | 575 | ADM9240_REG_MAN_ID); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 576 | if (man_id == 0x23) { | 
|  | 577 | kind = adm9240; | 
|  | 578 | } else if (man_id == 0xda) { | 
|  | 579 | kind = ds1780; | 
|  | 580 | } else if (man_id == 0x01) { | 
|  | 581 | kind = lm81; | 
|  | 582 | } else { | 
|  | 583 | dev_err(&adapter->dev, "detect fail: unknown manuf, " | 
|  | 584 | "0x%02x\n", man_id); | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 585 | return -ENODEV; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 586 | } | 
|  | 587 |  | 
|  | 588 | /* successful detect, print chip info */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 589 | die_rev = i2c_smbus_read_byte_data(new_client, | 
|  | 590 | ADM9240_REG_DIE_REV); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 591 | dev_info(&adapter->dev, "found %s revision %u\n", | 
|  | 592 | man_id == 0x23 ? "ADM9240" : | 
|  | 593 | man_id == 0xda ? "DS1780" : "LM81", die_rev); | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | /* either forced or detected chip kind */ | 
|  | 597 | if (kind == adm9240) { | 
|  | 598 | name = "adm9240"; | 
|  | 599 | } else if (kind == ds1780) { | 
|  | 600 | name = "ds1780"; | 
|  | 601 | } else if (kind == lm81) { | 
|  | 602 | name = "lm81"; | 
|  | 603 | } | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 604 | strlcpy(info->type, name, I2C_NAME_SIZE); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 605 |  | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 606 | return 0; | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | static int adm9240_probe(struct i2c_client *new_client, | 
|  | 610 | const struct i2c_device_id *id) | 
|  | 611 | { | 
|  | 612 | struct adm9240_data *data; | 
|  | 613 | int err; | 
|  | 614 |  | 
|  | 615 | data = kzalloc(sizeof(*data), GFP_KERNEL); | 
|  | 616 | if (!data) { | 
|  | 617 | err = -ENOMEM; | 
|  | 618 | goto exit; | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | i2c_set_clientdata(new_client, data); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 622 | mutex_init(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 623 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 624 | adm9240_init_client(new_client); | 
|  | 625 |  | 
|  | 626 | /* populate sysfs filesystem */ | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 627 | if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group))) | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 628 | goto exit_free; | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 629 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 630 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
|  | 631 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 632 | err = PTR_ERR(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 633 | goto exit_remove; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 634 | } | 
|  | 635 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 636 | return 0; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 637 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 638 | exit_remove: | 
|  | 639 | sysfs_remove_group(&new_client->dev.kobj, &adm9240_group); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 640 | exit_free: | 
| Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 641 | kfree(data); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 642 | exit: | 
|  | 643 | return err; | 
|  | 644 | } | 
|  | 645 |  | 
| Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 646 | static int adm9240_remove(struct i2c_client *client) | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 647 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 648 | struct adm9240_data *data = i2c_get_clientdata(client); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 649 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 650 | hwmon_device_unregister(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 651 | sysfs_remove_group(&client->dev.kobj, &adm9240_group); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 652 |  | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 653 | kfree(data); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 654 | return 0; | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | static void adm9240_init_client(struct i2c_client *client) | 
|  | 658 | { | 
|  | 659 | struct adm9240_data *data = i2c_get_clientdata(client); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 660 | u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG); | 
|  | 661 | u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3; | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 662 |  | 
| Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 663 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 664 |  | 
| Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 665 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, | 
|  | 666 | data->vrm % 10); | 
|  | 667 |  | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 668 | if (conf & 1) { /* measurement cycle running: report state */ | 
|  | 669 |  | 
|  | 670 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", | 
|  | 671 | conf, mode); | 
|  | 672 |  | 
|  | 673 | } else { /* cold start: open limits before starting chip */ | 
|  | 674 | int i; | 
|  | 675 |  | 
|  | 676 | for (i = 0; i < 6; i++) | 
|  | 677 | { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 678 | i2c_smbus_write_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 679 | ADM9240_REG_IN_MIN(i), 0); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 680 | i2c_smbus_write_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 681 | ADM9240_REG_IN_MAX(i), 255); | 
|  | 682 | } | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 683 | i2c_smbus_write_byte_data(client, | 
|  | 684 | ADM9240_REG_FAN_MIN(0), 255); | 
|  | 685 | i2c_smbus_write_byte_data(client, | 
|  | 686 | ADM9240_REG_FAN_MIN(1), 255); | 
|  | 687 | i2c_smbus_write_byte_data(client, | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 688 | ADM9240_REG_TEMP_MAX(0), 127); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 689 | i2c_smbus_write_byte_data(client, | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 690 | ADM9240_REG_TEMP_MAX(1), 127); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 691 |  | 
|  | 692 | /* start measurement cycle */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 693 | i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 694 |  | 
|  | 695 | dev_info(&client->dev, "cold start: config was 0x%02x " | 
|  | 696 | "mode %u\n", conf, mode); | 
|  | 697 | } | 
|  | 698 | } | 
|  | 699 |  | 
|  | 700 | static struct adm9240_data *adm9240_update_device(struct device *dev) | 
|  | 701 | { | 
|  | 702 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 703 | struct adm9240_data *data = i2c_get_clientdata(client); | 
|  | 704 | int i; | 
|  | 705 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 706 | mutex_lock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 707 |  | 
|  | 708 | /* minimum measurement cycle: 1.75 seconds */ | 
|  | 709 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) | 
|  | 710 | || !data->valid) { | 
|  | 711 |  | 
|  | 712 | for (i = 0; i < 6; i++) /* read voltages */ | 
|  | 713 | { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 714 | data->in[i] = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 715 | ADM9240_REG_IN(i)); | 
|  | 716 | } | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 717 | data->alarms = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 718 | ADM9240_REG_INT(0)) | | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 719 | i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 720 | ADM9240_REG_INT(1)) << 8; | 
|  | 721 |  | 
|  | 722 | /* read temperature: assume temperature changes less than | 
|  | 723 | * 0.5'C per two measurement cycles thus ignore possible | 
|  | 724 | * but unlikely aliasing error on lsb reading. --Grant */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 725 | data->temp = ((i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 726 | ADM9240_REG_TEMP) << 8) | | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 727 | i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 728 | ADM9240_REG_TEMP_CONF)) / 128; | 
|  | 729 |  | 
|  | 730 | for (i = 0; i < 2; i++) /* read fans */ | 
|  | 731 | { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 732 | data->fan[i] = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 733 | ADM9240_REG_FAN(i)); | 
|  | 734 |  | 
|  | 735 | /* adjust fan clock divider on overflow */ | 
|  | 736 | if (data->valid && data->fan[i] == 255 && | 
|  | 737 | data->fan_div[i] < 3) { | 
|  | 738 |  | 
|  | 739 | adm9240_write_fan_div(client, i, | 
|  | 740 | ++data->fan_div[i]); | 
|  | 741 |  | 
|  | 742 | /* adjust fan_min if active, but not to 0 */ | 
|  | 743 | if (data->fan_min[i] < 255 && | 
|  | 744 | data->fan_min[i] >= 2) | 
|  | 745 | data->fan_min[i] /= 2; | 
|  | 746 | } | 
|  | 747 | } | 
|  | 748 | data->last_updated_measure = jiffies; | 
|  | 749 | } | 
|  | 750 |  | 
|  | 751 | /* minimum config reading cycle: 300 seconds */ | 
|  | 752 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) | 
|  | 753 | || !data->valid) { | 
|  | 754 |  | 
|  | 755 | for (i = 0; i < 6; i++) | 
|  | 756 | { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 757 | data->in_min[i] = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 758 | ADM9240_REG_IN_MIN(i)); | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 759 | data->in_max[i] = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 760 | ADM9240_REG_IN_MAX(i)); | 
|  | 761 | } | 
|  | 762 | for (i = 0; i < 2; i++) | 
|  | 763 | { | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 764 | data->fan_min[i] = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 765 | ADM9240_REG_FAN_MIN(i)); | 
|  | 766 | } | 
| Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 767 | data->temp_max[0] = i2c_smbus_read_byte_data(client, | 
|  | 768 | ADM9240_REG_TEMP_MAX(0)); | 
|  | 769 | data->temp_max[1] = i2c_smbus_read_byte_data(client, | 
|  | 770 | ADM9240_REG_TEMP_MAX(1)); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 771 |  | 
|  | 772 | /* read fan divs and 5-bit VID */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 773 | i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 774 | data->fan_div[0] = (i >> 4) & 3; | 
|  | 775 | data->fan_div[1] = (i >> 6) & 3; | 
|  | 776 | data->vid = i & 0x0f; | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 777 | data->vid |= (i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 778 | ADM9240_REG_VID4) & 1) << 4; | 
|  | 779 | /* read analog out */ | 
| Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 780 | data->aout = i2c_smbus_read_byte_data(client, | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 781 | ADM9240_REG_ANALOG_OUT); | 
|  | 782 |  | 
|  | 783 | data->last_updated_config = jiffies; | 
|  | 784 | data->valid = 1; | 
|  | 785 | } | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 786 | mutex_unlock(&data->update_lock); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 787 | return data; | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | static int __init sensors_adm9240_init(void) | 
|  | 791 | { | 
|  | 792 | return i2c_add_driver(&adm9240_driver); | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | static void __exit sensors_adm9240_exit(void) | 
|  | 796 | { | 
|  | 797 | i2c_del_driver(&adm9240_driver); | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " | 
| Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 801 | "Grant Coady <gcoady.lk@gmail.com> and others"); | 
| Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 802 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); | 
|  | 803 | MODULE_LICENSE("GPL"); | 
|  | 804 |  | 
|  | 805 | module_init(sensors_adm9240_init); | 
|  | 806 | module_exit(sensors_adm9240_exit); | 
|  | 807 |  |