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 |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 3 | * monitoring |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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 |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 9 | * guidance from Jean Delvare |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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 | * |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 23 | * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm' |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 53 | #include <linux/jiffies.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 54 | |
| 55 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 56 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 57 | I2C_CLIENT_END }; |
| 58 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 59 | enum chips { 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 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 101 | return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* temperature range: -40..125, 127 disables temperature alarm */ |
| 105 | static inline s8 TEMP_TO_REG(long val) |
| 106 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 107 | return clamp_val(SCALE(val, 1, 1000), -40, 127); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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 | { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 125 | return clamp_val(SCALE(val, 255, 1250), 0, 255); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 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); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 135 | static int adm9240_detect(struct i2c_client *client, |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 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, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 159 | .address_list = normal_i2c, |
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); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 208 | long val; |
| 209 | int err; |
| 210 | |
| 211 | err = kstrtol(buf, 10, &val); |
| 212 | if (err) |
| 213 | return err; |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 214 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 215 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 216 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
| 217 | i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index), |
| 218 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 219 | mutex_unlock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 220 | return count; |
| 221 | } |
| 222 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 223 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 224 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 225 | show_max, set_max, 0); |
| 226 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 227 | show_max, set_max, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 228 | |
| 229 | /* voltage */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 230 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, |
| 231 | char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 232 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 233 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 234 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 235 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index], |
| 236 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 237 | } |
| 238 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 239 | static ssize_t show_in_min(struct device *dev, |
| 240 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 241 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 242 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 243 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 244 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index], |
| 245 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 246 | } |
| 247 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 248 | static ssize_t show_in_max(struct device *dev, |
| 249 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 250 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 251 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 252 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 253 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index], |
| 254 | attr->index)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 255 | } |
| 256 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 257 | static ssize_t set_in_min(struct device *dev, |
| 258 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 259 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 260 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 261 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 262 | struct i2c_client *client = to_i2c_client(dev); |
| 263 | struct adm9240_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 264 | unsigned long val; |
| 265 | int err; |
| 266 | |
| 267 | err = kstrtoul(buf, 10, &val); |
| 268 | if (err) |
| 269 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 270 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 271 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 272 | data->in_min[attr->index] = IN_TO_REG(val, attr->index); |
| 273 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index), |
| 274 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 275 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 276 | return count; |
| 277 | } |
| 278 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 279 | static ssize_t set_in_max(struct device *dev, |
| 280 | struct device_attribute *devattr, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 281 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 282 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 283 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 284 | struct i2c_client *client = to_i2c_client(dev); |
| 285 | struct adm9240_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 286 | unsigned long val; |
| 287 | int err; |
| 288 | |
| 289 | err = kstrtoul(buf, 10, &val); |
| 290 | if (err) |
| 291 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 292 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 293 | mutex_lock(&data->update_lock); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 294 | data->in_max[attr->index] = IN_TO_REG(val, attr->index); |
| 295 | i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index), |
| 296 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 297 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 298 | return count; |
| 299 | } |
| 300 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 301 | #define vin(nr) \ |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 302 | static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 303 | show_in, NULL, nr); \ |
| 304 | static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \ |
| 305 | show_in_min, set_in_min, nr); \ |
| 306 | static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \ |
| 307 | show_in_max, set_in_max, nr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 308 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 309 | vin(0); |
| 310 | vin(1); |
| 311 | vin(2); |
| 312 | vin(3); |
| 313 | vin(4); |
| 314 | vin(5); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 315 | |
| 316 | /* fans */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 317 | static ssize_t show_fan(struct device *dev, |
| 318 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 319 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 320 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 321 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 322 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
| 323 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 324 | } |
| 325 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 326 | static ssize_t show_fan_min(struct device *dev, |
| 327 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 328 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 329 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 330 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 331 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 332 | 1 << data->fan_div[attr->index])); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 333 | } |
| 334 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 335 | static ssize_t show_fan_div(struct device *dev, |
| 336 | struct device_attribute *devattr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 337 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 338 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 339 | struct adm9240_data *data = adm9240_update_device(dev); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 340 | return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* write new fan div, callers must hold data->update_lock */ |
| 344 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, |
| 345 | u8 fan_div) |
| 346 | { |
| 347 | u8 reg, old, shift = (nr + 2) * 2; |
| 348 | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 349 | reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 350 | old = (reg >> shift) & 3; |
| 351 | reg &= ~(3 << shift); |
| 352 | reg |= (fan_div << shift); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 353 | i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg); |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 354 | dev_dbg(&client->dev, |
| 355 | "fan%d clock divider changed from %u to %u\n", |
| 356 | nr + 1, 1 << old, 1 << fan_div); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 357 | } |
| 358 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 359 | /* |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 360 | * set fan speed low limit: |
| 361 | * |
| 362 | * - value is zero: disable fan speed low limit alarm |
| 363 | * |
| 364 | * - value is below fan speed measurement range: enable fan speed low |
| 365 | * limit alarm to be asserted while fan speed too slow to measure |
| 366 | * |
| 367 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 368 | * measurement code may adjust registers to ensure fan speed reading |
| 369 | */ |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 370 | static ssize_t set_fan_min(struct device *dev, |
| 371 | struct device_attribute *devattr, |
| 372 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 373 | { |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 374 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 375 | struct i2c_client *client = to_i2c_client(dev); |
| 376 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 377 | int nr = attr->index; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 378 | u8 new_div; |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 379 | unsigned long val; |
| 380 | int err; |
| 381 | |
| 382 | err = kstrtoul(buf, 10, &val); |
| 383 | if (err) |
| 384 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 385 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 386 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 387 | |
| 388 | if (!val) { |
| 389 | data->fan_min[nr] = 255; |
| 390 | new_div = data->fan_div[nr]; |
| 391 | |
| 392 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 393 | nr + 1); |
| 394 | |
| 395 | } else if (val < 1350000 / (8 * 254)) { |
| 396 | new_div = 3; |
| 397 | data->fan_min[nr] = 254; |
| 398 | |
| 399 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 400 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 401 | |
| 402 | } else { |
| 403 | unsigned int new_min = 1350000 / val; |
| 404 | |
| 405 | new_div = 0; |
| 406 | while (new_min > 192 && new_div < 3) { |
| 407 | new_div++; |
| 408 | new_min /= 2; |
| 409 | } |
| 410 | if (!new_min) /* keep > 0 */ |
| 411 | new_min++; |
| 412 | |
| 413 | data->fan_min[nr] = new_min; |
| 414 | |
| 415 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 416 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 417 | } |
| 418 | |
| 419 | if (new_div != data->fan_div[nr]) { |
| 420 | data->fan_div[nr] = new_div; |
| 421 | adm9240_write_fan_div(client, nr, new_div); |
| 422 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 423 | i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr), |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 424 | data->fan_min[nr]); |
| 425 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 426 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 427 | return count; |
| 428 | } |
| 429 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 430 | #define fan(nr) \ |
| 431 | static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \ |
| 432 | show_fan, NULL, nr - 1); \ |
| 433 | static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \ |
| 434 | show_fan_div, NULL, nr - 1); \ |
| 435 | static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \ |
| 436 | show_fan_min, set_fan_min, nr - 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 437 | |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 438 | fan(1); |
| 439 | fan(2); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 440 | |
| 441 | /* alarms */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 442 | static ssize_t show_alarms(struct device *dev, |
| 443 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 444 | { |
| 445 | struct adm9240_data *data = adm9240_update_device(dev); |
| 446 | return sprintf(buf, "%u\n", data->alarms); |
| 447 | } |
| 448 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 449 | |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 450 | static ssize_t show_alarm(struct device *dev, |
| 451 | struct device_attribute *attr, char *buf) |
| 452 | { |
| 453 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 454 | struct adm9240_data *data = adm9240_update_device(dev); |
| 455 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 456 | } |
| 457 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 458 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 459 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 460 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 461 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 462 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 463 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 464 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 465 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 466 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 467 | /* vid */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 468 | static ssize_t show_vid(struct device *dev, |
| 469 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 470 | { |
| 471 | struct adm9240_data *data = adm9240_update_device(dev); |
| 472 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 473 | } |
| 474 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 475 | |
| 476 | /* analog output */ |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 477 | static ssize_t show_aout(struct device *dev, |
| 478 | struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 479 | { |
| 480 | struct adm9240_data *data = adm9240_update_device(dev); |
| 481 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 482 | } |
| 483 | |
Jean Delvare | e415e48 | 2005-09-25 16:14:18 +0200 | [diff] [blame] | 484 | static ssize_t set_aout(struct device *dev, |
| 485 | struct device_attribute *attr, |
| 486 | const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 487 | { |
| 488 | struct i2c_client *client = to_i2c_client(dev); |
| 489 | struct adm9240_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 490 | long val; |
| 491 | int err; |
| 492 | |
| 493 | err = kstrtol(buf, 10, &val); |
| 494 | if (err) |
| 495 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 496 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 497 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 498 | data->aout = AOUT_TO_REG(val); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 499 | i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 500 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 501 | return count; |
| 502 | } |
| 503 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 504 | |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 505 | static ssize_t chassis_clear(struct device *dev, |
| 506 | struct device_attribute *attr, |
| 507 | const char *buf, size_t count) |
| 508 | { |
| 509 | struct i2c_client *client = to_i2c_client(dev); |
| 510 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 511 | unsigned long val; |
| 512 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 513 | if (kstrtoul(buf, 10, &val) || val != 0) |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 514 | return -EINVAL; |
| 515 | |
| 516 | mutex_lock(&data->update_lock); |
| 517 | i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80); |
| 518 | data->valid = 0; /* Force cache refresh */ |
| 519 | mutex_unlock(&data->update_lock); |
| 520 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); |
| 521 | |
| 522 | return count; |
| 523 | } |
| 524 | static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm, |
| 525 | chassis_clear, 12); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 526 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 527 | static struct attribute *adm9240_attributes[] = { |
| 528 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 529 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 530 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 531 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 532 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 533 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 534 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 535 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 536 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 537 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 538 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 539 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 540 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 541 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 542 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 543 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 544 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 545 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 546 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 547 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 548 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 549 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 550 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 551 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 552 | &dev_attr_temp1_input.attr, |
| 553 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 554 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 555 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 556 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 557 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 558 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 559 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 560 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 561 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 562 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
Jean Delvare | 360f945 | 2008-01-06 15:49:19 +0100 | [diff] [blame] | 563 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 564 | &dev_attr_alarms.attr, |
| 565 | &dev_attr_aout_output.attr, |
Jean Delvare | 0de2b24 | 2011-01-12 21:55:09 +0100 | [diff] [blame] | 566 | &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 567 | &dev_attr_cpu0_vid.attr, |
| 568 | NULL |
| 569 | }; |
| 570 | |
| 571 | static const struct attribute_group adm9240_group = { |
| 572 | .attrs = adm9240_attributes, |
| 573 | }; |
| 574 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 575 | |
| 576 | /*** sensor chip detect and driver install ***/ |
| 577 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 578 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 579 | static int adm9240_detect(struct i2c_client *new_client, |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 580 | struct i2c_board_info *info) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 581 | { |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 582 | struct i2c_adapter *adapter = new_client->adapter; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 583 | const char *name = ""; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 584 | int address = new_client->addr; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 585 | u8 man_id, die_rev; |
| 586 | |
| 587 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 588 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 589 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 590 | /* verify chip: reg address should match i2c address */ |
| 591 | if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR) |
| 592 | != address) { |
| 593 | dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n", |
| 594 | address); |
| 595 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 596 | } |
| 597 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 598 | /* check known chip manufacturer */ |
| 599 | man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID); |
| 600 | if (man_id == 0x23) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 601 | name = "adm9240"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 602 | } else if (man_id == 0xda) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 603 | name = "ds1780"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 604 | } else if (man_id == 0x01) { |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 605 | name = "lm81"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 606 | } else { |
| 607 | dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n", |
| 608 | man_id); |
| 609 | return -ENODEV; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 610 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 611 | |
| 612 | /* successful detect, print chip info */ |
| 613 | die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV); |
| 614 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 615 | man_id == 0x23 ? "ADM9240" : |
| 616 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 617 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 618 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 619 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static int adm9240_probe(struct i2c_client *new_client, |
| 624 | const struct i2c_device_id *id) |
| 625 | { |
| 626 | struct adm9240_data *data; |
| 627 | int err; |
| 628 | |
Guenter Roeck | e5ebb77 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 629 | data = devm_kzalloc(&new_client->dev, sizeof(*data), GFP_KERNEL); |
| 630 | if (!data) |
| 631 | return -ENOMEM; |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 632 | |
| 633 | i2c_set_clientdata(new_client, data); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 634 | mutex_init(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 635 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 636 | adm9240_init_client(new_client); |
| 637 | |
| 638 | /* populate sysfs filesystem */ |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 639 | err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group); |
| 640 | if (err) |
Guenter Roeck | e5ebb77 | 2012-06-02 09:58:00 -0700 | [diff] [blame] | 641 | return err; |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 642 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 643 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 644 | if (IS_ERR(data->hwmon_dev)) { |
| 645 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 646 | goto exit_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 647 | } |
| 648 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 649 | return 0; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 650 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 651 | exit_remove: |
| 652 | sysfs_remove_group(&new_client->dev.kobj, &adm9240_group); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 653 | return err; |
| 654 | } |
| 655 | |
Jean Delvare | 7fae828 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 656 | static int adm9240_remove(struct i2c_client *client) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 657 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 658 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 659 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 660 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 661 | sysfs_remove_group(&client->dev.kobj, &adm9240_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 662 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static void adm9240_init_client(struct i2c_client *client) |
| 667 | { |
| 668 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 669 | u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG); |
| 670 | 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] | 671 | |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 672 | data->vrm = vid_which_vrm(); /* need this to report vid as mV */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 673 | |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 674 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 675 | data->vrm % 10); |
| 676 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 677 | if (conf & 1) { /* measurement cycle running: report state */ |
| 678 | |
| 679 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 680 | conf, mode); |
| 681 | |
| 682 | } else { /* cold start: open limits before starting chip */ |
| 683 | int i; |
| 684 | |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 685 | for (i = 0; i < 6; i++) { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 686 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 687 | ADM9240_REG_IN_MIN(i), 0); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 688 | i2c_smbus_write_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 689 | ADM9240_REG_IN_MAX(i), 255); |
| 690 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 691 | i2c_smbus_write_byte_data(client, |
| 692 | ADM9240_REG_FAN_MIN(0), 255); |
| 693 | i2c_smbus_write_byte_data(client, |
| 694 | ADM9240_REG_FAN_MIN(1), 255); |
| 695 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 696 | ADM9240_REG_TEMP_MAX(0), 127); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 697 | i2c_smbus_write_byte_data(client, |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 698 | ADM9240_REG_TEMP_MAX(1), 127); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 699 | |
| 700 | /* start measurement cycle */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 701 | i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 702 | |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 703 | dev_info(&client->dev, |
| 704 | "cold start: config was 0x%02x mode %u\n", conf, mode); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 709 | { |
| 710 | struct i2c_client *client = to_i2c_client(dev); |
| 711 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 712 | int i; |
| 713 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 714 | mutex_lock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 715 | |
| 716 | /* minimum measurement cycle: 1.75 seconds */ |
| 717 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 718 | || !data->valid) { |
| 719 | |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 720 | for (i = 0; i < 6; i++) { /* read voltages */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 721 | data->in[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 722 | ADM9240_REG_IN(i)); |
| 723 | } |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 724 | data->alarms = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 725 | ADM9240_REG_INT(0)) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 726 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 727 | ADM9240_REG_INT(1)) << 8; |
| 728 | |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 729 | /* |
| 730 | * read temperature: assume temperature changes less than |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 731 | * 0.5'C per two measurement cycles thus ignore possible |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 732 | * but unlikely aliasing error on lsb reading. --Grant |
| 733 | */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 734 | data->temp = ((i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 735 | ADM9240_REG_TEMP) << 8) | |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 736 | i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 737 | ADM9240_REG_TEMP_CONF)) / 128; |
| 738 | |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 739 | for (i = 0; i < 2; i++) { /* read fans */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 740 | data->fan[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 741 | ADM9240_REG_FAN(i)); |
| 742 | |
| 743 | /* adjust fan clock divider on overflow */ |
| 744 | if (data->valid && data->fan[i] == 255 && |
| 745 | data->fan_div[i] < 3) { |
| 746 | |
| 747 | adm9240_write_fan_div(client, i, |
| 748 | ++data->fan_div[i]); |
| 749 | |
| 750 | /* adjust fan_min if active, but not to 0 */ |
| 751 | if (data->fan_min[i] < 255 && |
| 752 | data->fan_min[i] >= 2) |
| 753 | data->fan_min[i] /= 2; |
| 754 | } |
| 755 | } |
| 756 | data->last_updated_measure = jiffies; |
| 757 | } |
| 758 | |
| 759 | /* minimum config reading cycle: 300 seconds */ |
| 760 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 761 | || !data->valid) { |
| 762 | |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 763 | for (i = 0; i < 6; i++) { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 764 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 765 | ADM9240_REG_IN_MIN(i)); |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 766 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 767 | ADM9240_REG_IN_MAX(i)); |
| 768 | } |
Guenter Roeck | c387e4e | 2012-01-14 13:12:38 -0800 | [diff] [blame] | 769 | for (i = 0; i < 2; i++) { |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 770 | data->fan_min[i] = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 771 | ADM9240_REG_FAN_MIN(i)); |
| 772 | } |
Grant Coady | c7461a6 | 2005-09-17 05:32:57 +1000 | [diff] [blame] | 773 | data->temp_max[0] = i2c_smbus_read_byte_data(client, |
| 774 | ADM9240_REG_TEMP_MAX(0)); |
| 775 | data->temp_max[1] = i2c_smbus_read_byte_data(client, |
| 776 | ADM9240_REG_TEMP_MAX(1)); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 777 | |
| 778 | /* read fan divs and 5-bit VID */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 779 | i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 780 | data->fan_div[0] = (i >> 4) & 3; |
| 781 | data->fan_div[1] = (i >> 6) & 3; |
| 782 | data->vid = i & 0x0f; |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 783 | data->vid |= (i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 784 | ADM9240_REG_VID4) & 1) << 4; |
| 785 | /* read analog out */ |
Grant Coady | 205cf13 | 2005-09-17 05:32:55 +1000 | [diff] [blame] | 786 | data->aout = i2c_smbus_read_byte_data(client, |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 787 | ADM9240_REG_ANALOG_OUT); |
| 788 | |
| 789 | data->last_updated_config = jiffies; |
| 790 | data->valid = 1; |
| 791 | } |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 792 | mutex_unlock(&data->update_lock); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 793 | return data; |
| 794 | } |
| 795 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 796 | module_i2c_driver(adm9240_driver); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 797 | |
| 798 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
Grant Coady | 2ca7b96 | 2006-10-08 21:57:41 +0200 | [diff] [blame] | 799 | "Grant Coady <gcoady.lk@gmail.com> and others"); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 800 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 801 | MODULE_LICENSE("GPL"); |