Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 2 | /* |
| 3 | * max6639.c - Support for Maxim MAX6639 |
| 4 | * |
| 5 | * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller |
| 6 | * |
| 7 | * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> |
| 8 | * |
| 9 | * based on the initial MAX6639 support from semptian.net |
| 10 | * by He Changqing <hechangqing@semptian.com> |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/hwmon.h> |
| 19 | #include <linux/hwmon-sysfs.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/mutex.h> |
Wolfram Sang | 0c9fe16 | 2017-05-21 22:34:42 +0200 | [diff] [blame] | 22 | #include <linux/platform_data/max6639.h> |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 23 | |
| 24 | /* Addresses to scan */ |
Axel Lin | 7edc8cc | 2014-07-18 15:56:59 +0800 | [diff] [blame] | 25 | static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 26 | |
| 27 | /* The MAX6639 registers, valid channel numbers: 0, 1 */ |
| 28 | #define MAX6639_REG_TEMP(ch) (0x00 + (ch)) |
| 29 | #define MAX6639_REG_STATUS 0x02 |
| 30 | #define MAX6639_REG_OUTPUT_MASK 0x03 |
| 31 | #define MAX6639_REG_GCONFIG 0x04 |
| 32 | #define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch)) |
| 33 | #define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch)) |
| 34 | #define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch)) |
| 35 | #define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch)) |
| 36 | #define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4) |
| 37 | #define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4) |
| 38 | #define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4) |
| 39 | #define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4) |
| 40 | #define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch)) |
| 41 | #define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch)) |
| 42 | #define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch)) |
| 43 | #define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch)) |
| 44 | #define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch)) |
| 45 | #define MAX6639_REG_DEVID 0x3D |
| 46 | #define MAX6639_REG_MANUID 0x3E |
| 47 | #define MAX6639_REG_DEVREV 0x3F |
| 48 | |
| 49 | /* Register bits */ |
| 50 | #define MAX6639_GCONFIG_STANDBY 0x80 |
| 51 | #define MAX6639_GCONFIG_POR 0x40 |
| 52 | #define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20 |
| 53 | #define MAX6639_GCONFIG_CH2_LOCAL 0x10 |
stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 54 | #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08 |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 55 | |
| 56 | #define MAX6639_FAN_CONFIG1_PWM 0x80 |
| 57 | |
stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 58 | #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 |
| 59 | |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 60 | static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; |
| 61 | |
Chris D Schimp | b63d97a | 2012-02-20 16:59:24 -0500 | [diff] [blame] | 62 | #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \ |
| 63 | 0 : (rpm_ranges[rpm_range] * 30) / (val)) |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 64 | #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Client data (each client gets its own) |
| 68 | */ |
| 69 | struct max6639_data { |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 70 | struct i2c_client *client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 71 | struct mutex update_lock; |
| 72 | char valid; /* !=0 if following fields are valid */ |
| 73 | unsigned long last_updated; /* In jiffies */ |
| 74 | |
| 75 | /* Register values sampled regularly */ |
| 76 | u16 temp[2]; /* Temperature, in 1/8 C, 0..255 C */ |
| 77 | bool temp_fault[2]; /* Detected temperature diode failure */ |
| 78 | u8 fan[2]; /* Register value: TACH count for fans >=30 */ |
| 79 | u8 status; /* Detected channel alarms and fan failures */ |
| 80 | |
| 81 | /* Register values only written to */ |
| 82 | u8 pwm[2]; /* Register value: Duty cycle 0..120 */ |
| 83 | u8 temp_therm[2]; /* THERM Temperature, 0..255 C (->_max) */ |
| 84 | u8 temp_alert[2]; /* ALERT Temperature, 0..255 C (->_crit) */ |
| 85 | u8 temp_ot[2]; /* OT Temperature, 0..255 C (->_emergency) */ |
| 86 | |
| 87 | /* Register values initialized only once */ |
| 88 | u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */ |
| 89 | u8 rpm_range; /* Index in above rpm_ranges table */ |
| 90 | }; |
| 91 | |
| 92 | static struct max6639_data *max6639_update_device(struct device *dev) |
| 93 | { |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 94 | struct max6639_data *data = dev_get_drvdata(dev); |
| 95 | struct i2c_client *client = data->client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 96 | struct max6639_data *ret = data; |
| 97 | int i; |
| 98 | int status_reg; |
| 99 | |
| 100 | mutex_lock(&data->update_lock); |
| 101 | |
| 102 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
| 103 | int res; |
| 104 | |
| 105 | dev_dbg(&client->dev, "Starting max6639 update\n"); |
| 106 | |
| 107 | status_reg = i2c_smbus_read_byte_data(client, |
| 108 | MAX6639_REG_STATUS); |
| 109 | if (status_reg < 0) { |
| 110 | ret = ERR_PTR(status_reg); |
| 111 | goto abort; |
| 112 | } |
| 113 | |
| 114 | data->status = status_reg; |
| 115 | |
| 116 | for (i = 0; i < 2; i++) { |
| 117 | res = i2c_smbus_read_byte_data(client, |
| 118 | MAX6639_REG_FAN_CNT(i)); |
| 119 | if (res < 0) { |
| 120 | ret = ERR_PTR(res); |
| 121 | goto abort; |
| 122 | } |
| 123 | data->fan[i] = res; |
| 124 | |
| 125 | res = i2c_smbus_read_byte_data(client, |
| 126 | MAX6639_REG_TEMP_EXT(i)); |
| 127 | if (res < 0) { |
| 128 | ret = ERR_PTR(res); |
| 129 | goto abort; |
| 130 | } |
| 131 | data->temp[i] = res >> 5; |
| 132 | data->temp_fault[i] = res & 0x01; |
| 133 | |
| 134 | res = i2c_smbus_read_byte_data(client, |
| 135 | MAX6639_REG_TEMP(i)); |
| 136 | if (res < 0) { |
| 137 | ret = ERR_PTR(res); |
| 138 | goto abort; |
| 139 | } |
| 140 | data->temp[i] |= res << 3; |
| 141 | } |
| 142 | |
| 143 | data->last_updated = jiffies; |
| 144 | data->valid = 1; |
| 145 | } |
| 146 | abort: |
| 147 | mutex_unlock(&data->update_lock); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 152 | static ssize_t temp_input_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 153 | struct device_attribute *dev_attr, char *buf) |
| 154 | { |
| 155 | long temp; |
| 156 | struct max6639_data *data = max6639_update_device(dev); |
| 157 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 158 | |
| 159 | if (IS_ERR(data)) |
| 160 | return PTR_ERR(data); |
| 161 | |
| 162 | temp = data->temp[attr->index] * 125; |
| 163 | return sprintf(buf, "%ld\n", temp); |
| 164 | } |
| 165 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 166 | static ssize_t temp_fault_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 167 | struct device_attribute *dev_attr, char *buf) |
| 168 | { |
| 169 | struct max6639_data *data = max6639_update_device(dev); |
| 170 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 171 | |
| 172 | if (IS_ERR(data)) |
| 173 | return PTR_ERR(data); |
| 174 | |
| 175 | return sprintf(buf, "%d\n", data->temp_fault[attr->index]); |
| 176 | } |
| 177 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 178 | static ssize_t temp_max_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 179 | struct device_attribute *dev_attr, char *buf) |
| 180 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 181 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 182 | struct max6639_data *data = dev_get_drvdata(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 183 | |
| 184 | return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); |
| 185 | } |
| 186 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 187 | static ssize_t temp_max_store(struct device *dev, |
| 188 | struct device_attribute *dev_attr, |
| 189 | const char *buf, size_t count) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 190 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 191 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 192 | struct max6639_data *data = dev_get_drvdata(dev); |
| 193 | struct i2c_client *client = data->client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 194 | unsigned long val; |
| 195 | int res; |
| 196 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 197 | res = kstrtoul(buf, 10, &val); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 198 | if (res) |
| 199 | return res; |
| 200 | |
| 201 | mutex_lock(&data->update_lock); |
| 202 | data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val); |
| 203 | i2c_smbus_write_byte_data(client, |
| 204 | MAX6639_REG_THERM_LIMIT(attr->index), |
| 205 | data->temp_therm[attr->index]); |
| 206 | mutex_unlock(&data->update_lock); |
| 207 | return count; |
| 208 | } |
| 209 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 210 | static ssize_t temp_crit_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 211 | struct device_attribute *dev_attr, char *buf) |
| 212 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 213 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 214 | struct max6639_data *data = dev_get_drvdata(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 215 | |
| 216 | return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); |
| 217 | } |
| 218 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 219 | static ssize_t temp_crit_store(struct device *dev, |
| 220 | struct device_attribute *dev_attr, |
| 221 | const char *buf, size_t count) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 222 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 223 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 224 | struct max6639_data *data = dev_get_drvdata(dev); |
| 225 | struct i2c_client *client = data->client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 226 | unsigned long val; |
| 227 | int res; |
| 228 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 229 | res = kstrtoul(buf, 10, &val); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 230 | if (res) |
| 231 | return res; |
| 232 | |
| 233 | mutex_lock(&data->update_lock); |
| 234 | data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val); |
| 235 | i2c_smbus_write_byte_data(client, |
| 236 | MAX6639_REG_ALERT_LIMIT(attr->index), |
| 237 | data->temp_alert[attr->index]); |
| 238 | mutex_unlock(&data->update_lock); |
| 239 | return count; |
| 240 | } |
| 241 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 242 | static ssize_t temp_emergency_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 243 | struct device_attribute *dev_attr, |
| 244 | char *buf) |
| 245 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 246 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 247 | struct max6639_data *data = dev_get_drvdata(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 248 | |
| 249 | return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); |
| 250 | } |
| 251 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 252 | static ssize_t temp_emergency_store(struct device *dev, |
| 253 | struct device_attribute *dev_attr, |
| 254 | const char *buf, size_t count) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 255 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 256 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 257 | struct max6639_data *data = dev_get_drvdata(dev); |
| 258 | struct i2c_client *client = data->client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 259 | unsigned long val; |
| 260 | int res; |
| 261 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 262 | res = kstrtoul(buf, 10, &val); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 263 | if (res) |
| 264 | return res; |
| 265 | |
| 266 | mutex_lock(&data->update_lock); |
| 267 | data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val); |
| 268 | i2c_smbus_write_byte_data(client, |
| 269 | MAX6639_REG_OT_LIMIT(attr->index), |
| 270 | data->temp_ot[attr->index]); |
| 271 | mutex_unlock(&data->update_lock); |
| 272 | return count; |
| 273 | } |
| 274 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 275 | static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr, |
| 276 | char *buf) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 277 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 278 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 279 | struct max6639_data *data = dev_get_drvdata(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 280 | |
| 281 | return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); |
| 282 | } |
| 283 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 284 | static ssize_t pwm_store(struct device *dev, |
| 285 | struct device_attribute *dev_attr, const char *buf, |
| 286 | size_t count) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 287 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 288 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 289 | struct max6639_data *data = dev_get_drvdata(dev); |
| 290 | struct i2c_client *client = data->client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 291 | unsigned long val; |
| 292 | int res; |
| 293 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 294 | res = kstrtoul(buf, 10, &val); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 295 | if (res) |
| 296 | return res; |
| 297 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 298 | val = clamp_val(val, 0, 255); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 299 | |
| 300 | mutex_lock(&data->update_lock); |
| 301 | data->pwm[attr->index] = (u8)(val * 120 / 255); |
| 302 | i2c_smbus_write_byte_data(client, |
| 303 | MAX6639_REG_TARGTDUTY(attr->index), |
| 304 | data->pwm[attr->index]); |
| 305 | mutex_unlock(&data->update_lock); |
| 306 | return count; |
| 307 | } |
| 308 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 309 | static ssize_t fan_input_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 310 | struct device_attribute *dev_attr, char *buf) |
| 311 | { |
| 312 | struct max6639_data *data = max6639_update_device(dev); |
| 313 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 314 | |
| 315 | if (IS_ERR(data)) |
| 316 | return PTR_ERR(data); |
| 317 | |
| 318 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], |
Chris D Schimp | b63d97a | 2012-02-20 16:59:24 -0500 | [diff] [blame] | 319 | data->rpm_range)); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 322 | static ssize_t alarm_show(struct device *dev, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 323 | struct device_attribute *dev_attr, char *buf) |
| 324 | { |
| 325 | struct max6639_data *data = max6639_update_device(dev); |
| 326 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 327 | |
| 328 | if (IS_ERR(data)) |
| 329 | return PTR_ERR(data); |
| 330 | |
| 331 | return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index))); |
| 332 | } |
| 333 | |
Guenter Roeck | 0a0ab22 | 2018-12-10 14:02:16 -0800 | [diff] [blame] | 334 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); |
| 335 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); |
| 336 | static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0); |
| 337 | static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1); |
| 338 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); |
| 339 | static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); |
| 340 | static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0); |
| 341 | static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1); |
| 342 | static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0); |
| 343 | static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1); |
| 344 | static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); |
| 345 | static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); |
| 346 | static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); |
| 347 | static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); |
| 348 | static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1); |
| 349 | static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0); |
| 350 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3); |
| 351 | static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2); |
| 352 | static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7); |
| 353 | static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6); |
| 354 | static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5); |
| 355 | static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 356 | |
| 357 | |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 358 | static struct attribute *max6639_attrs[] = { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 359 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 360 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 361 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 362 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 363 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 364 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 365 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 366 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 367 | &sensor_dev_attr_temp1_emergency.dev_attr.attr, |
| 368 | &sensor_dev_attr_temp2_emergency.dev_attr.attr, |
| 369 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 370 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 371 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 372 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 373 | &sensor_dev_attr_fan1_fault.dev_attr.attr, |
| 374 | &sensor_dev_attr_fan2_fault.dev_attr.attr, |
| 375 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 376 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 377 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 378 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 379 | &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, |
| 380 | &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, |
| 381 | NULL |
| 382 | }; |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 383 | ATTRIBUTE_GROUPS(max6639); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * returns respective index in rpm_ranges table |
| 387 | * 1 by default on invalid range |
| 388 | */ |
| 389 | static int rpm_range_to_reg(int range) |
| 390 | { |
| 391 | int i; |
| 392 | |
| 393 | for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) { |
| 394 | if (rpm_ranges[i] == range) |
| 395 | return i; |
| 396 | } |
| 397 | |
| 398 | return 1; /* default: 4000 RPM */ |
| 399 | } |
| 400 | |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 401 | static int max6639_init_client(struct i2c_client *client, |
| 402 | struct max6639_data *data) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 403 | { |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 404 | struct max6639_platform_data *max6639_info = |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 405 | dev_get_platdata(&client->dev); |
Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 406 | int i; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 407 | int rpm_range = 1; /* default: 4000 RPM */ |
Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 408 | int err; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 409 | |
stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 410 | /* Reset chip to default values, see below for GCONFIG setup */ |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 411 | err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, |
| 412 | MAX6639_GCONFIG_POR); |
| 413 | if (err) |
| 414 | goto exit; |
| 415 | |
| 416 | /* Fans pulse per revolution is 2 by default */ |
| 417 | if (max6639_info && max6639_info->ppr > 0 && |
| 418 | max6639_info->ppr < 5) |
| 419 | data->ppr = max6639_info->ppr; |
| 420 | else |
| 421 | data->ppr = 2; |
| 422 | data->ppr -= 1; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 423 | |
| 424 | if (max6639_info) |
| 425 | rpm_range = rpm_range_to_reg(max6639_info->rpm_range); |
| 426 | data->rpm_range = rpm_range; |
| 427 | |
| 428 | for (i = 0; i < 2; i++) { |
| 429 | |
Chris D Schimp | 2f2da1a | 2012-02-20 17:44:59 -0500 | [diff] [blame] | 430 | /* Set Fan pulse per revolution */ |
| 431 | err = i2c_smbus_write_byte_data(client, |
| 432 | MAX6639_REG_FAN_PPR(i), |
| 433 | data->ppr << 6); |
| 434 | if (err) |
| 435 | goto exit; |
| 436 | |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 437 | /* Fans config PWM, RPM */ |
| 438 | err = i2c_smbus_write_byte_data(client, |
| 439 | MAX6639_REG_FAN_CONFIG1(i), |
| 440 | MAX6639_FAN_CONFIG1_PWM | rpm_range); |
| 441 | if (err) |
| 442 | goto exit; |
| 443 | |
| 444 | /* Fans PWM polarity high by default */ |
| 445 | if (max6639_info && max6639_info->pwm_polarity == 0) |
| 446 | err = i2c_smbus_write_byte_data(client, |
| 447 | MAX6639_REG_FAN_CONFIG2a(i), 0x00); |
| 448 | else |
| 449 | err = i2c_smbus_write_byte_data(client, |
| 450 | MAX6639_REG_FAN_CONFIG2a(i), 0x02); |
| 451 | if (err) |
| 452 | goto exit; |
| 453 | |
stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 454 | /* |
| 455 | * /THERM full speed enable, |
| 456 | * PWM frequency 25kHz, see also GCONFIG below |
| 457 | */ |
| 458 | err = i2c_smbus_write_byte_data(client, |
| 459 | MAX6639_REG_FAN_CONFIG3(i), |
| 460 | MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); |
| 461 | if (err) |
| 462 | goto exit; |
| 463 | |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 464 | /* Max. temp. 80C/90C/100C */ |
| 465 | data->temp_therm[i] = 80; |
| 466 | data->temp_alert[i] = 90; |
| 467 | data->temp_ot[i] = 100; |
| 468 | err = i2c_smbus_write_byte_data(client, |
| 469 | MAX6639_REG_THERM_LIMIT(i), |
| 470 | data->temp_therm[i]); |
| 471 | if (err) |
| 472 | goto exit; |
| 473 | err = i2c_smbus_write_byte_data(client, |
| 474 | MAX6639_REG_ALERT_LIMIT(i), |
| 475 | data->temp_alert[i]); |
| 476 | if (err) |
| 477 | goto exit; |
| 478 | err = i2c_smbus_write_byte_data(client, |
| 479 | MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]); |
| 480 | if (err) |
| 481 | goto exit; |
| 482 | |
| 483 | /* PWM 120/120 (i.e. 100%) */ |
| 484 | data->pwm[i] = 120; |
| 485 | err = i2c_smbus_write_byte_data(client, |
| 486 | MAX6639_REG_TARGTDUTY(i), data->pwm[i]); |
| 487 | if (err) |
| 488 | goto exit; |
| 489 | } |
| 490 | /* Start monitoring */ |
| 491 | err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, |
stigge@antcom.de | 177f3b9 | 2011-01-29 17:04:01 +0100 | [diff] [blame] | 492 | MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | |
| 493 | MAX6639_GCONFIG_PWM_FREQ_HI); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 494 | exit: |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 499 | static int max6639_detect(struct i2c_client *client, |
| 500 | struct i2c_board_info *info) |
| 501 | { |
| 502 | struct i2c_adapter *adapter = client->adapter; |
| 503 | int dev_id, manu_id; |
| 504 | |
| 505 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 506 | return -ENODEV; |
| 507 | |
| 508 | /* Actual detection via device and manufacturer ID */ |
| 509 | dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID); |
| 510 | manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID); |
| 511 | if (dev_id != 0x58 || manu_id != 0x4D) |
| 512 | return -ENODEV; |
| 513 | |
| 514 | strlcpy(info->type, "max6639", I2C_NAME_SIZE); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 519 | static int max6639_probe(struct i2c_client *client) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 520 | { |
Guenter Roeck | c1ea0a0 | 2014-02-17 10:21:26 -0800 | [diff] [blame] | 521 | struct device *dev = &client->dev; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 522 | struct max6639_data *data; |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 523 | struct device *hwmon_dev; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 524 | int err; |
| 525 | |
Guenter Roeck | c1ea0a0 | 2014-02-17 10:21:26 -0800 | [diff] [blame] | 526 | data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); |
Guenter Roeck | b07405f | 2012-06-02 09:58:13 -0700 | [diff] [blame] | 527 | if (!data) |
| 528 | return -ENOMEM; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 529 | |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 530 | data->client = client; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 531 | mutex_init(&data->update_lock); |
| 532 | |
| 533 | /* Initialize the max6639 chip */ |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 534 | err = max6639_init_client(client, data); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 535 | if (err < 0) |
Guenter Roeck | b07405f | 2012-06-02 09:58:13 -0700 | [diff] [blame] | 536 | return err; |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 537 | |
Guenter Roeck | 7981c58 | 2014-02-17 10:34:29 -0800 | [diff] [blame] | 538 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 539 | data, |
| 540 | max6639_groups); |
| 541 | return PTR_ERR_OR_ZERO(hwmon_dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 544 | #ifdef CONFIG_PM_SLEEP |
| 545 | static int max6639_suspend(struct device *dev) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 546 | { |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 547 | struct i2c_client *client = to_i2c_client(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 548 | int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); |
| 549 | if (data < 0) |
| 550 | return data; |
| 551 | |
| 552 | return i2c_smbus_write_byte_data(client, |
| 553 | MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY); |
| 554 | } |
| 555 | |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 556 | static int max6639_resume(struct device *dev) |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 557 | { |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 558 | struct i2c_client *client = to_i2c_client(dev); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 559 | int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); |
| 560 | if (data < 0) |
| 561 | return data; |
| 562 | |
| 563 | return i2c_smbus_write_byte_data(client, |
| 564 | MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY); |
| 565 | } |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 566 | #endif /* CONFIG_PM_SLEEP */ |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 567 | |
| 568 | static const struct i2c_device_id max6639_id[] = { |
| 569 | {"max6639", 0}, |
| 570 | { } |
| 571 | }; |
| 572 | |
| 573 | MODULE_DEVICE_TABLE(i2c, max6639_id); |
| 574 | |
Jingoo Han | 768821a | 2014-02-27 20:38:20 +0900 | [diff] [blame] | 575 | static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume); |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 576 | |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 577 | static struct i2c_driver max6639_driver = { |
| 578 | .class = I2C_CLASS_HWMON, |
| 579 | .driver = { |
| 580 | .name = "max6639", |
Mark Brown | 52f30f7 | 2012-03-22 16:23:58 -0400 | [diff] [blame] | 581 | .pm = &max6639_pm_ops, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 582 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 583 | .probe_new = max6639_probe, |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 584 | .id_table = max6639_id, |
| 585 | .detect = max6639_detect, |
| 586 | .address_list = normal_i2c, |
| 587 | }; |
| 588 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 589 | module_i2c_driver(max6639_driver); |
stigge@antcom.de | a5b79d6 | 2011-01-20 18:42:55 +0100 | [diff] [blame] | 590 | |
| 591 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); |
| 592 | MODULE_DESCRIPTION("max6639 driver"); |
| 593 | MODULE_LICENSE("GPL"); |