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