Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 1 | /* |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 2 | * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl> |
| 5 | * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and |
| 6 | * Philip Edelbrock <phil@netroedge.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/mutex.h> |
| 31 | |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 35 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 36 | |
| 37 | /* Insmod parameters */ |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 38 | enum chips { thmc50, adm1022 }; |
Jean Delvare | f95f0b4 | 2009-12-09 20:35:59 +0100 | [diff] [blame] | 39 | |
| 40 | static unsigned short adm1022_temp3[16]; |
| 41 | static unsigned int adm1022_temp3_num; |
| 42 | module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0); |
| 43 | MODULE_PARM_DESC(adm1022_temp3, "List of adapter,address pairs " |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 44 | "to enable 3rd temperature (ADM1022 only)"); |
| 45 | |
| 46 | /* Many THMC50 constants specified below */ |
| 47 | |
| 48 | /* The THMC50 registers */ |
| 49 | #define THMC50_REG_CONF 0x40 |
| 50 | #define THMC50_REG_COMPANY_ID 0x3E |
| 51 | #define THMC50_REG_DIE_CODE 0x3F |
| 52 | #define THMC50_REG_ANALOG_OUT 0x19 |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 53 | /* |
Krzysztof Helt | bba891c | 2007-09-09 17:35:34 +0200 | [diff] [blame] | 54 | * The mirror status register cannot be used as |
| 55 | * reading it does not clear alarms. |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 56 | */ |
Krzysztof Helt | bba891c | 2007-09-09 17:35:34 +0200 | [diff] [blame] | 57 | #define THMC50_REG_INTR 0x41 |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 58 | |
Tobias Klauser | 5910a9b | 2008-02-17 15:39:24 +0100 | [diff] [blame] | 59 | static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 }; |
| 60 | static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C }; |
| 61 | static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B }; |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 62 | static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 }; |
| 63 | static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 }; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 64 | |
| 65 | #define THMC50_REG_CONF_nFANOFF 0x20 |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 66 | #define THMC50_REG_CONF_PROGRAMMED 0x08 |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 67 | |
| 68 | /* Each client has this additional data */ |
| 69 | struct thmc50_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 70 | struct device *hwmon_dev; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 71 | |
| 72 | struct mutex update_lock; |
| 73 | enum chips type; |
| 74 | unsigned long last_updated; /* In jiffies */ |
| 75 | char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */ |
| 76 | char valid; /* !=0 if following fields are valid */ |
| 77 | |
| 78 | /* Register values */ |
| 79 | s8 temp_input[3]; |
| 80 | s8 temp_max[3]; |
| 81 | s8 temp_min[3]; |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 82 | s8 temp_critical[3]; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 83 | u8 analog_out; |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 84 | u8 alarms; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 87 | static int thmc50_detect(struct i2c_client *client, |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 88 | struct i2c_board_info *info); |
| 89 | static int thmc50_probe(struct i2c_client *client, |
| 90 | const struct i2c_device_id *id); |
| 91 | static int thmc50_remove(struct i2c_client *client); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 92 | static void thmc50_init_client(struct i2c_client *client); |
| 93 | static struct thmc50_data *thmc50_update_device(struct device *dev); |
| 94 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 95 | static const struct i2c_device_id thmc50_id[] = { |
| 96 | { "adm1022", adm1022 }, |
| 97 | { "thmc50", thmc50 }, |
| 98 | { } |
| 99 | }; |
| 100 | MODULE_DEVICE_TABLE(i2c, thmc50_id); |
| 101 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 102 | static struct i2c_driver thmc50_driver = { |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 103 | .class = I2C_CLASS_HWMON, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 104 | .driver = { |
| 105 | .name = "thmc50", |
| 106 | }, |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 107 | .probe = thmc50_probe, |
| 108 | .remove = thmc50_remove, |
| 109 | .id_table = thmc50_id, |
| 110 | .detect = thmc50_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 111 | .address_list = normal_i2c, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static ssize_t show_analog_out(struct device *dev, |
| 115 | struct device_attribute *attr, char *buf) |
| 116 | { |
| 117 | struct thmc50_data *data = thmc50_update_device(dev); |
| 118 | return sprintf(buf, "%d\n", data->analog_out); |
| 119 | } |
| 120 | |
| 121 | static ssize_t set_analog_out(struct device *dev, |
| 122 | struct device_attribute *attr, |
| 123 | const char *buf, size_t count) |
| 124 | { |
| 125 | struct i2c_client *client = to_i2c_client(dev); |
| 126 | struct thmc50_data *data = i2c_get_clientdata(client); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 127 | int config; |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 128 | unsigned long tmp; |
| 129 | int err; |
| 130 | |
| 131 | err = kstrtoul(buf, 10, &tmp); |
| 132 | if (err) |
| 133 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 134 | |
| 135 | mutex_lock(&data->update_lock); |
| 136 | data->analog_out = SENSORS_LIMIT(tmp, 0, 255); |
| 137 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, |
| 138 | data->analog_out); |
| 139 | |
| 140 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 141 | if (data->analog_out == 0) |
| 142 | config &= ~THMC50_REG_CONF_nFANOFF; |
| 143 | else |
| 144 | config |= THMC50_REG_CONF_nFANOFF; |
| 145 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); |
| 146 | |
| 147 | mutex_unlock(&data->update_lock); |
| 148 | return count; |
| 149 | } |
| 150 | |
| 151 | /* There is only one PWM mode = DC */ |
| 152 | static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr, |
| 153 | char *buf) |
| 154 | { |
| 155 | return sprintf(buf, "0\n"); |
| 156 | } |
| 157 | |
| 158 | /* Temperatures */ |
| 159 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 160 | char *buf) |
| 161 | { |
| 162 | int nr = to_sensor_dev_attr(attr)->index; |
| 163 | struct thmc50_data *data = thmc50_update_device(dev); |
| 164 | return sprintf(buf, "%d\n", data->temp_input[nr] * 1000); |
| 165 | } |
| 166 | |
| 167 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
| 168 | char *buf) |
| 169 | { |
| 170 | int nr = to_sensor_dev_attr(attr)->index; |
| 171 | struct thmc50_data *data = thmc50_update_device(dev); |
| 172 | return sprintf(buf, "%d\n", data->temp_min[nr] * 1000); |
| 173 | } |
| 174 | |
| 175 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
| 176 | const char *buf, size_t count) |
| 177 | { |
| 178 | int nr = to_sensor_dev_attr(attr)->index; |
| 179 | struct i2c_client *client = to_i2c_client(dev); |
| 180 | struct thmc50_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 181 | long val; |
| 182 | int err; |
| 183 | |
| 184 | err = kstrtol(buf, 10, &val); |
| 185 | if (err) |
| 186 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 187 | |
| 188 | mutex_lock(&data->update_lock); |
| 189 | data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127); |
| 190 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr], |
| 191 | data->temp_min[nr]); |
| 192 | mutex_unlock(&data->update_lock); |
| 193 | return count; |
| 194 | } |
| 195 | |
| 196 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 197 | char *buf) |
| 198 | { |
| 199 | int nr = to_sensor_dev_attr(attr)->index; |
| 200 | struct thmc50_data *data = thmc50_update_device(dev); |
| 201 | return sprintf(buf, "%d\n", data->temp_max[nr] * 1000); |
| 202 | } |
| 203 | |
| 204 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 205 | const char *buf, size_t count) |
| 206 | { |
| 207 | int nr = to_sensor_dev_attr(attr)->index; |
| 208 | struct i2c_client *client = to_i2c_client(dev); |
| 209 | struct thmc50_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 210 | long val; |
| 211 | int err; |
| 212 | |
| 213 | err = kstrtol(buf, 10, &val); |
| 214 | if (err) |
| 215 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 216 | |
| 217 | mutex_lock(&data->update_lock); |
| 218 | data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127); |
| 219 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr], |
| 220 | data->temp_max[nr]); |
| 221 | mutex_unlock(&data->update_lock); |
| 222 | return count; |
| 223 | } |
| 224 | |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 225 | static ssize_t show_temp_critical(struct device *dev, |
| 226 | struct device_attribute *attr, |
| 227 | char *buf) |
| 228 | { |
| 229 | int nr = to_sensor_dev_attr(attr)->index; |
| 230 | struct thmc50_data *data = thmc50_update_device(dev); |
| 231 | return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000); |
| 232 | } |
| 233 | |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 234 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 235 | char *buf) |
| 236 | { |
| 237 | int index = to_sensor_dev_attr(attr)->index; |
| 238 | struct thmc50_data *data = thmc50_update_device(dev); |
| 239 | |
| 240 | return sprintf(buf, "%u\n", (data->alarms >> index) & 1); |
| 241 | } |
| 242 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 243 | #define temp_reg(offset) \ |
| 244 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \ |
| 245 | NULL, offset - 1); \ |
| 246 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 247 | show_temp_min, set_temp_min, offset - 1); \ |
| 248 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 249 | show_temp_max, set_temp_max, offset - 1); \ |
| 250 | static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \ |
| 251 | show_temp_critical, NULL, offset - 1); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 252 | |
| 253 | temp_reg(1); |
| 254 | temp_reg(2); |
| 255 | temp_reg(3); |
| 256 | |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 257 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 258 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 259 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 260 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7); |
| 261 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); |
| 262 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 263 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out, |
| 264 | set_analog_out, 0); |
| 265 | static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0); |
| 266 | |
| 267 | static struct attribute *thmc50_attributes[] = { |
| 268 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 269 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 270 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 271 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 272 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 273 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 274 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 275 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 276 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 277 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 278 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 279 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 280 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, |
| 281 | NULL |
| 282 | }; |
| 283 | |
| 284 | static const struct attribute_group thmc50_group = { |
| 285 | .attrs = thmc50_attributes, |
| 286 | }; |
| 287 | |
| 288 | /* for ADM1022 3rd temperature mode */ |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 289 | static struct attribute *temp3_attributes[] = { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 290 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 291 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 292 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 293 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 294 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 295 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 296 | NULL |
| 297 | }; |
| 298 | |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 299 | static const struct attribute_group temp3_group = { |
| 300 | .attrs = temp3_attributes, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 301 | }; |
| 302 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 303 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 304 | static int thmc50_detect(struct i2c_client *client, |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 305 | struct i2c_board_info *info) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 306 | { |
| 307 | unsigned company; |
| 308 | unsigned revision; |
| 309 | unsigned config; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 310 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 311 | const char *type_name; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 312 | |
| 313 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 314 | pr_debug("thmc50: detect failed, " |
| 315 | "smbus byte data not supported!\n"); |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 316 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 319 | pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", |
| 320 | client->addr, i2c_adapter_id(client->adapter)); |
| 321 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 322 | company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID); |
| 323 | revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE); |
| 324 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 325 | if (revision < 0xc0 || (config & 0x10)) |
| 326 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 327 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 328 | if (company == 0x41) { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 329 | int id = i2c_adapter_id(client->adapter); |
| 330 | int i; |
| 331 | |
| 332 | type_name = "adm1022"; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 333 | for (i = 0; i + 1 < adm1022_temp3_num; i += 2) |
| 334 | if (adm1022_temp3[i] == id && |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 335 | adm1022_temp3[i + 1] == client->addr) { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 336 | /* enable 2nd remote temp */ |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 337 | config |= (1 << 7); |
| 338 | i2c_smbus_write_byte_data(client, |
| 339 | THMC50_REG_CONF, |
| 340 | config); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 341 | break; |
| 342 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 343 | } else if (company == 0x49) { |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 344 | type_name = "thmc50"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 345 | } else { |
| 346 | pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); |
| 347 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 348 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 349 | |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 350 | pr_debug("thmc50: Detected %s (version %x, revision %x)\n", |
| 351 | type_name, (revision >> 4) - 0xc, revision & 0xf); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 352 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 353 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 354 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int thmc50_probe(struct i2c_client *client, |
| 359 | const struct i2c_device_id *id) |
| 360 | { |
| 361 | struct thmc50_data *data; |
| 362 | int err; |
| 363 | |
| 364 | data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL); |
| 365 | if (!data) { |
| 366 | pr_debug("thmc50: detect failed, kzalloc failed!\n"); |
| 367 | err = -ENOMEM; |
| 368 | goto exit; |
| 369 | } |
| 370 | |
| 371 | i2c_set_clientdata(client, data); |
| 372 | data->type = id->driver_data; |
| 373 | mutex_init(&data->update_lock); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 374 | |
| 375 | thmc50_init_client(client); |
| 376 | |
| 377 | /* Register sysfs hooks */ |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 378 | err = sysfs_create_group(&client->dev.kobj, &thmc50_group); |
| 379 | if (err) |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 380 | goto exit_free; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 381 | |
| 382 | /* Register ADM1022 sysfs hooks */ |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 383 | if (data->has_temp3) { |
| 384 | err = sysfs_create_group(&client->dev.kobj, &temp3_group); |
| 385 | if (err) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 386 | goto exit_remove_sysfs_thmc50; |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 387 | } |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 388 | |
| 389 | /* Register a new directory entry with module sensors */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 390 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 391 | if (IS_ERR(data->hwmon_dev)) { |
| 392 | err = PTR_ERR(data->hwmon_dev); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 393 | goto exit_remove_sysfs; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | |
| 398 | exit_remove_sysfs: |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 399 | if (data->has_temp3) |
| 400 | sysfs_remove_group(&client->dev.kobj, &temp3_group); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 401 | exit_remove_sysfs_thmc50: |
| 402 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 403 | exit_free: |
| 404 | kfree(data); |
| 405 | exit: |
| 406 | return err; |
| 407 | } |
| 408 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 409 | static int thmc50_remove(struct i2c_client *client) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 410 | { |
| 411 | struct thmc50_data *data = i2c_get_clientdata(client); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 412 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 413 | hwmon_device_unregister(data->hwmon_dev); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 414 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 415 | if (data->has_temp3) |
| 416 | sysfs_remove_group(&client->dev.kobj, &temp3_group); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 417 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 418 | kfree(data); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static void thmc50_init_client(struct i2c_client *client) |
| 424 | { |
| 425 | struct thmc50_data *data = i2c_get_clientdata(client); |
| 426 | int config; |
| 427 | |
| 428 | data->analog_out = i2c_smbus_read_byte_data(client, |
| 429 | THMC50_REG_ANALOG_OUT); |
| 430 | /* set up to at least 1 */ |
| 431 | if (data->analog_out == 0) { |
| 432 | data->analog_out = 1; |
| 433 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, |
| 434 | data->analog_out); |
| 435 | } |
| 436 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 437 | config |= 0x1; /* start the chip if it is in standby mode */ |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 438 | if (data->type == adm1022 && (config & (1 << 7))) |
| 439 | data->has_temp3 = 1; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 440 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); |
| 441 | } |
| 442 | |
| 443 | static struct thmc50_data *thmc50_update_device(struct device *dev) |
| 444 | { |
| 445 | struct i2c_client *client = to_i2c_client(dev); |
| 446 | struct thmc50_data *data = i2c_get_clientdata(client); |
| 447 | int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0); |
| 448 | |
| 449 | mutex_lock(&data->update_lock); |
| 450 | |
| 451 | if (time_after(jiffies, data->last_updated + timeout) |
| 452 | || !data->valid) { |
| 453 | |
| 454 | int temps = data->has_temp3 ? 3 : 2; |
| 455 | int i; |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 456 | int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 457 | |
| 458 | prog &= THMC50_REG_CONF_PROGRAMMED; |
| 459 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 460 | for (i = 0; i < temps; i++) { |
| 461 | data->temp_input[i] = i2c_smbus_read_byte_data(client, |
| 462 | THMC50_REG_TEMP[i]); |
| 463 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 464 | THMC50_REG_TEMP_MAX[i]); |
| 465 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 466 | THMC50_REG_TEMP_MIN[i]); |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 467 | data->temp_critical[i] = |
| 468 | i2c_smbus_read_byte_data(client, |
| 469 | prog ? THMC50_REG_TEMP_CRITICAL[i] |
| 470 | : THMC50_REG_TEMP_DEFAULT[i]); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 471 | } |
| 472 | data->analog_out = |
| 473 | i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT); |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 474 | data->alarms = |
Krzysztof Helt | bba891c | 2007-09-09 17:35:34 +0200 | [diff] [blame] | 475 | i2c_smbus_read_byte_data(client, THMC50_REG_INTR); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 476 | data->last_updated = jiffies; |
| 477 | data->valid = 1; |
| 478 | } |
| 479 | |
| 480 | mutex_unlock(&data->update_lock); |
| 481 | |
| 482 | return data; |
| 483 | } |
| 484 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 485 | module_i2c_driver(thmc50_driver); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 486 | |
| 487 | MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>"); |
| 488 | MODULE_DESCRIPTION("THMC50 driver"); |