Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 2 | /* |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 3 | * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware |
| 4 | * monitoring |
| 5 | * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl> |
| 6 | * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and |
| 7 | * Philip Edelbrock <phil@netroedge.com> |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 8 | */ |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/hwmon.h> |
| 15 | #include <linux/hwmon-sysfs.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/mutex.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 19 | |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | |
| 22 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 23 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 24 | |
| 25 | /* Insmod parameters */ |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 26 | enum chips { thmc50, adm1022 }; |
Jean Delvare | f95f0b4 | 2009-12-09 20:35:59 +0100 | [diff] [blame] | 27 | |
| 28 | static unsigned short adm1022_temp3[16]; |
| 29 | static unsigned int adm1022_temp3_num; |
| 30 | module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0); |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 31 | MODULE_PARM_DESC(adm1022_temp3, |
| 32 | "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)"); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 33 | |
| 34 | /* Many THMC50 constants specified below */ |
| 35 | |
| 36 | /* The THMC50 registers */ |
| 37 | #define THMC50_REG_CONF 0x40 |
| 38 | #define THMC50_REG_COMPANY_ID 0x3E |
| 39 | #define THMC50_REG_DIE_CODE 0x3F |
| 40 | #define THMC50_REG_ANALOG_OUT 0x19 |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 41 | /* |
Krzysztof Helt | bba891c | 2007-09-09 17:35:34 +0200 | [diff] [blame] | 42 | * The mirror status register cannot be used as |
| 43 | * reading it does not clear alarms. |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 44 | */ |
Krzysztof Helt | bba891c | 2007-09-09 17:35:34 +0200 | [diff] [blame] | 45 | #define THMC50_REG_INTR 0x41 |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 46 | |
Tobias Klauser | 5910a9b | 2008-02-17 15:39:24 +0100 | [diff] [blame] | 47 | static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 }; |
| 48 | static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C }; |
| 49 | static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B }; |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 50 | static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 }; |
| 51 | static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 }; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 52 | |
| 53 | #define THMC50_REG_CONF_nFANOFF 0x20 |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 54 | #define THMC50_REG_CONF_PROGRAMMED 0x08 |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 55 | |
| 56 | /* Each client has this additional data */ |
| 57 | struct thmc50_data { |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 58 | struct i2c_client *client; |
| 59 | const struct attribute_group *groups[3]; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 60 | |
| 61 | struct mutex update_lock; |
| 62 | enum chips type; |
| 63 | unsigned long last_updated; /* In jiffies */ |
| 64 | char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */ |
| 65 | char valid; /* !=0 if following fields are valid */ |
| 66 | |
| 67 | /* Register values */ |
| 68 | s8 temp_input[3]; |
| 69 | s8 temp_max[3]; |
| 70 | s8 temp_min[3]; |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 71 | s8 temp_critical[3]; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 72 | u8 analog_out; |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 73 | u8 alarms; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 76 | static struct thmc50_data *thmc50_update_device(struct device *dev) |
| 77 | { |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 78 | struct thmc50_data *data = dev_get_drvdata(dev); |
| 79 | struct i2c_client *client = data->client; |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 80 | int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 81 | |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 82 | mutex_lock(&data->update_lock); |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 83 | |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 84 | if (time_after(jiffies, data->last_updated + timeout) |
| 85 | || !data->valid) { |
| 86 | |
| 87 | int temps = data->has_temp3 ? 3 : 2; |
| 88 | int i; |
| 89 | int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 90 | |
| 91 | prog &= THMC50_REG_CONF_PROGRAMMED; |
| 92 | |
| 93 | for (i = 0; i < temps; i++) { |
| 94 | data->temp_input[i] = i2c_smbus_read_byte_data(client, |
| 95 | THMC50_REG_TEMP[i]); |
| 96 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 97 | THMC50_REG_TEMP_MAX[i]); |
| 98 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 99 | THMC50_REG_TEMP_MIN[i]); |
| 100 | data->temp_critical[i] = |
| 101 | i2c_smbus_read_byte_data(client, |
| 102 | prog ? THMC50_REG_TEMP_CRITICAL[i] |
| 103 | : THMC50_REG_TEMP_DEFAULT[i]); |
| 104 | } |
| 105 | data->analog_out = |
| 106 | i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT); |
| 107 | data->alarms = |
| 108 | i2c_smbus_read_byte_data(client, THMC50_REG_INTR); |
| 109 | data->last_updated = jiffies; |
| 110 | data->valid = 1; |
| 111 | } |
| 112 | |
| 113 | mutex_unlock(&data->update_lock); |
| 114 | |
| 115 | return data; |
| 116 | } |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 117 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 118 | static ssize_t analog_out_show(struct device *dev, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 119 | struct device_attribute *attr, char *buf) |
| 120 | { |
| 121 | struct thmc50_data *data = thmc50_update_device(dev); |
| 122 | return sprintf(buf, "%d\n", data->analog_out); |
| 123 | } |
| 124 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 125 | static ssize_t analog_out_store(struct device *dev, |
| 126 | struct device_attribute *attr, |
| 127 | const char *buf, size_t count) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 128 | { |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 129 | struct thmc50_data *data = dev_get_drvdata(dev); |
| 130 | struct i2c_client *client = data->client; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 131 | int config; |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 132 | unsigned long tmp; |
| 133 | int err; |
| 134 | |
| 135 | err = kstrtoul(buf, 10, &tmp); |
| 136 | if (err) |
| 137 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 138 | |
| 139 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 140 | data->analog_out = clamp_val(tmp, 0, 255); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 141 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, |
| 142 | data->analog_out); |
| 143 | |
| 144 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 145 | if (data->analog_out == 0) |
| 146 | config &= ~THMC50_REG_CONF_nFANOFF; |
| 147 | else |
| 148 | config |= THMC50_REG_CONF_nFANOFF; |
| 149 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); |
| 150 | |
| 151 | mutex_unlock(&data->update_lock); |
| 152 | return count; |
| 153 | } |
| 154 | |
| 155 | /* There is only one PWM mode = DC */ |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 156 | static ssize_t pwm_mode_show(struct device *dev, |
| 157 | struct device_attribute *attr, char *buf) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 158 | { |
| 159 | return sprintf(buf, "0\n"); |
| 160 | } |
| 161 | |
| 162 | /* Temperatures */ |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 163 | static ssize_t temp_show(struct device *dev, struct device_attribute *attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 164 | char *buf) |
| 165 | { |
| 166 | int nr = to_sensor_dev_attr(attr)->index; |
| 167 | struct thmc50_data *data = thmc50_update_device(dev); |
| 168 | return sprintf(buf, "%d\n", data->temp_input[nr] * 1000); |
| 169 | } |
| 170 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 171 | static ssize_t temp_min_show(struct device *dev, |
| 172 | struct device_attribute *attr, char *buf) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 173 | { |
| 174 | int nr = to_sensor_dev_attr(attr)->index; |
| 175 | struct thmc50_data *data = thmc50_update_device(dev); |
| 176 | return sprintf(buf, "%d\n", data->temp_min[nr] * 1000); |
| 177 | } |
| 178 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 179 | static ssize_t temp_min_store(struct device *dev, |
| 180 | struct device_attribute *attr, const char *buf, |
| 181 | size_t count) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 182 | { |
| 183 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 184 | struct thmc50_data *data = dev_get_drvdata(dev); |
| 185 | struct i2c_client *client = data->client; |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 186 | long val; |
| 187 | int err; |
| 188 | |
| 189 | err = kstrtol(buf, 10, &val); |
| 190 | if (err) |
| 191 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 192 | |
| 193 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 194 | data->temp_min[nr] = clamp_val(val / 1000, -128, 127); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 195 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr], |
| 196 | data->temp_min[nr]); |
| 197 | mutex_unlock(&data->update_lock); |
| 198 | return count; |
| 199 | } |
| 200 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 201 | static ssize_t temp_max_show(struct device *dev, |
| 202 | struct device_attribute *attr, char *buf) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 203 | { |
| 204 | int nr = to_sensor_dev_attr(attr)->index; |
| 205 | struct thmc50_data *data = thmc50_update_device(dev); |
| 206 | return sprintf(buf, "%d\n", data->temp_max[nr] * 1000); |
| 207 | } |
| 208 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 209 | static ssize_t temp_max_store(struct device *dev, |
| 210 | struct device_attribute *attr, const char *buf, |
| 211 | size_t count) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 212 | { |
| 213 | int nr = to_sensor_dev_attr(attr)->index; |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 214 | struct thmc50_data *data = dev_get_drvdata(dev); |
| 215 | struct i2c_client *client = data->client; |
Guenter Roeck | 4d387df | 2012-01-14 22:33:01 -0800 | [diff] [blame] | 216 | long val; |
| 217 | int err; |
| 218 | |
| 219 | err = kstrtol(buf, 10, &val); |
| 220 | if (err) |
| 221 | return err; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 222 | |
| 223 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 224 | data->temp_max[nr] = clamp_val(val / 1000, -128, 127); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 225 | i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr], |
| 226 | data->temp_max[nr]); |
| 227 | mutex_unlock(&data->update_lock); |
| 228 | return count; |
| 229 | } |
| 230 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 231 | static ssize_t temp_critical_show(struct device *dev, |
| 232 | struct device_attribute *attr, char *buf) |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 233 | { |
| 234 | int nr = to_sensor_dev_attr(attr)->index; |
| 235 | struct thmc50_data *data = thmc50_update_device(dev); |
| 236 | return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000); |
| 237 | } |
| 238 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 239 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 240 | char *buf) |
| 241 | { |
| 242 | int index = to_sensor_dev_attr(attr)->index; |
| 243 | struct thmc50_data *data = thmc50_update_device(dev); |
| 244 | |
| 245 | return sprintf(buf, "%u\n", (data->alarms >> index) & 1); |
| 246 | } |
| 247 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 248 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); |
| 249 | static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0); |
| 250 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); |
| 251 | static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_critical, 0); |
| 252 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); |
| 253 | static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1); |
| 254 | static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); |
| 255 | static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_critical, 1); |
| 256 | static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2); |
| 257 | static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2); |
| 258 | static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2); |
| 259 | static SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_critical, 2); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 260 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 261 | static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0); |
| 262 | static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5); |
| 263 | static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 1); |
| 264 | static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 7); |
| 265 | static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 266 | |
Guenter Roeck | 29168f3 | 2019-01-21 20:26:58 -0800 | [diff] [blame] | 267 | static SENSOR_DEVICE_ATTR_RW(pwm1, analog_out, 0); |
| 268 | static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 269 | |
| 270 | static struct attribute *thmc50_attributes[] = { |
| 271 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 272 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 273 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 274 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 275 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 276 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 277 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 278 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 279 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 280 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 281 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 282 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 283 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, |
| 284 | NULL |
| 285 | }; |
| 286 | |
| 287 | static const struct attribute_group thmc50_group = { |
| 288 | .attrs = thmc50_attributes, |
| 289 | }; |
| 290 | |
| 291 | /* for ADM1022 3rd temperature mode */ |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 292 | static struct attribute *temp3_attributes[] = { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 293 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 294 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 295 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Krzysztof Helt | 84f768c | 2008-08-06 22:41:05 +0200 | [diff] [blame] | 296 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
Krzysztof Helt | dcf3b5f | 2007-08-22 14:05:22 -0400 | [diff] [blame] | 297 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 298 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 299 | NULL |
| 300 | }; |
| 301 | |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 302 | static const struct attribute_group temp3_group = { |
| 303 | .attrs = temp3_attributes, |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 304 | }; |
| 305 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 306 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 307 | static int thmc50_detect(struct i2c_client *client, |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 308 | struct i2c_board_info *info) |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 309 | { |
| 310 | unsigned company; |
| 311 | unsigned revision; |
| 312 | unsigned config; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 313 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 314 | const char *type_name; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 315 | |
| 316 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 317 | pr_debug("thmc50: detect failed, smbus byte data not supported!\n"); |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 318 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 321 | pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", |
| 322 | client->addr, i2c_adapter_id(client->adapter)); |
| 323 | |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 324 | company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID); |
| 325 | revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE); |
| 326 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 327 | if (revision < 0xc0 || (config & 0x10)) |
| 328 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 329 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 330 | if (company == 0x41) { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 331 | int id = i2c_adapter_id(client->adapter); |
| 332 | int i; |
| 333 | |
| 334 | type_name = "adm1022"; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 335 | for (i = 0; i + 1 < adm1022_temp3_num; i += 2) |
| 336 | if (adm1022_temp3[i] == id && |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 337 | adm1022_temp3[i + 1] == client->addr) { |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 338 | /* enable 2nd remote temp */ |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 339 | config |= (1 << 7); |
| 340 | i2c_smbus_write_byte_data(client, |
| 341 | THMC50_REG_CONF, |
| 342 | config); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 343 | break; |
| 344 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 345 | } else if (company == 0x49) { |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 346 | type_name = "thmc50"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 347 | } else { |
| 348 | pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); |
| 349 | return -ENODEV; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 350 | } |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 351 | |
Jean Delvare | cc28a61 | 2007-09-18 18:48:16 +0200 | [diff] [blame] | 352 | pr_debug("thmc50: Detected %s (version %x, revision %x)\n", |
| 353 | type_name, (revision >> 4) - 0xc, revision & 0xf); |
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 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 356 | |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 360 | static void thmc50_init_client(struct thmc50_data *data) |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 361 | { |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 362 | struct i2c_client *client = data->client; |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 363 | int config; |
| 364 | |
| 365 | data->analog_out = i2c_smbus_read_byte_data(client, |
| 366 | THMC50_REG_ANALOG_OUT); |
| 367 | /* set up to at least 1 */ |
| 368 | if (data->analog_out == 0) { |
| 369 | data->analog_out = 1; |
| 370 | i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, |
| 371 | data->analog_out); |
| 372 | } |
| 373 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
| 374 | config |= 0x1; /* start the chip if it is in standby mode */ |
| 375 | if (data->type == adm1022 && (config & (1 << 7))) |
| 376 | data->has_temp3 = 1; |
| 377 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); |
| 378 | } |
| 379 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 380 | static const struct i2c_device_id thmc50_id[]; |
| 381 | |
| 382 | static int thmc50_probe(struct i2c_client *client) |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 383 | { |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 384 | struct device *dev = &client->dev; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 385 | struct thmc50_data *data; |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 386 | struct device *hwmon_dev; |
| 387 | int idx = 0; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 388 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 389 | data = devm_kzalloc(dev, sizeof(struct thmc50_data), GFP_KERNEL); |
Guenter Roeck | bd91d3b | 2012-06-02 11:20:23 -0700 | [diff] [blame] | 390 | if (!data) |
| 391 | return -ENOMEM; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 392 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 393 | data->client = client; |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 394 | data->type = i2c_match_id(thmc50_id, client)->driver_data; |
Jean Delvare | ccf3748 | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 395 | mutex_init(&data->update_lock); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 396 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 397 | thmc50_init_client(data); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 398 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 399 | /* sysfs hooks */ |
| 400 | data->groups[idx++] = &thmc50_group; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 401 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 402 | /* Register additional ADM1022 sysfs hooks */ |
Jean Delvare | 894c00c | 2007-09-18 18:44:42 +0200 | [diff] [blame] | 403 | if (data->has_temp3) |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 404 | data->groups[idx++] = &temp3_group; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 405 | |
Axel Lin | 86fd260 | 2014-07-17 19:06:08 +0800 | [diff] [blame] | 406 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 407 | data, data->groups); |
| 408 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 411 | static const struct i2c_device_id thmc50_id[] = { |
| 412 | { "adm1022", adm1022 }, |
| 413 | { "thmc50", thmc50 }, |
| 414 | { } |
| 415 | }; |
| 416 | MODULE_DEVICE_TABLE(i2c, thmc50_id); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 417 | |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 418 | static struct i2c_driver thmc50_driver = { |
| 419 | .class = I2C_CLASS_HWMON, |
| 420 | .driver = { |
| 421 | .name = "thmc50", |
| 422 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 423 | .probe_new = thmc50_probe, |
Axel Lin | 9b38a66 | 2014-07-17 19:05:23 +0800 | [diff] [blame] | 424 | .id_table = thmc50_id, |
| 425 | .detect = thmc50_detect, |
| 426 | .address_list = normal_i2c, |
| 427 | }; |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 428 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 429 | module_i2c_driver(thmc50_driver); |
Krzysztof Helt | add77c6 | 2007-07-08 22:43:00 +0200 | [diff] [blame] | 430 | |
| 431 | MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>"); |
| 432 | MODULE_DESCRIPTION("THMC50 driver"); |