Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 2 | /*************************************************************************** |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 3 | * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com> * |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 4 | * * |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 5 | ***************************************************************************/ |
| 6 | |
| 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/jiffies.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/hwmon.h> |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/mutex.h> |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 17 | #include "sch56xx-common.h" |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 18 | |
| 19 | #define DRVNAME "sch5627" |
| 20 | #define DEVNAME DRVNAME /* We only support one model */ |
| 21 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 22 | #define SCH5627_HWMON_ID 0xa5 |
| 23 | #define SCH5627_COMPANY_ID 0x5c |
| 24 | #define SCH5627_PRIMARY_ID 0xa0 |
| 25 | |
| 26 | #define SCH5627_REG_BUILD_CODE 0x39 |
| 27 | #define SCH5627_REG_BUILD_ID 0x3a |
| 28 | #define SCH5627_REG_HWMON_ID 0x3c |
| 29 | #define SCH5627_REG_HWMON_REV 0x3d |
| 30 | #define SCH5627_REG_COMPANY_ID 0x3e |
| 31 | #define SCH5627_REG_PRIMARY_ID 0x3f |
| 32 | #define SCH5627_REG_CTRL 0x40 |
| 33 | |
| 34 | #define SCH5627_NO_TEMPS 8 |
| 35 | #define SCH5627_NO_FANS 4 |
| 36 | #define SCH5627_NO_IN 5 |
| 37 | |
| 38 | static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = { |
| 39 | 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 }; |
| 40 | static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = { |
| 41 | 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 }; |
| 42 | static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = { |
| 43 | 0, 0, 1, 1, 0, 0, 0, 1 }; |
| 44 | static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = { |
| 45 | 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 }; |
| 46 | static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = { |
| 47 | 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 }; |
| 48 | |
| 49 | static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = { |
| 50 | 0x2C, 0x2E, 0x30, 0x32 }; |
| 51 | static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = { |
| 52 | 0x62, 0x64, 0x66, 0x68 }; |
| 53 | |
| 54 | static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = { |
| 55 | 0x22, 0x23, 0x24, 0x25, 0x189 }; |
| 56 | static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = { |
| 57 | 0xE4, 0xE4, 0xE3, 0xE3, 0x18A }; |
| 58 | static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = { |
| 59 | 1, 0, 1, 0, 1 }; |
| 60 | static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = { |
| 61 | 10745, 3660, 9765, 10745, 3660 }; |
| 62 | static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = { |
| 63 | "VCC", "VTT", "VBAT", "VTR", "V_IN" }; |
| 64 | |
| 65 | struct sch5627_data { |
| 66 | unsigned short addr; |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 67 | u8 control; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 68 | u8 temp_max[SCH5627_NO_TEMPS]; |
| 69 | u8 temp_crit[SCH5627_NO_TEMPS]; |
| 70 | u16 fan_min[SCH5627_NO_FANS]; |
| 71 | |
| 72 | struct mutex update_lock; |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 73 | unsigned long last_battery; /* In jiffies */ |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 74 | char temp_valid; /* !=0 if following fields are valid */ |
| 75 | char fan_valid; |
| 76 | char in_valid; |
| 77 | unsigned long temp_last_updated; /* In jiffies */ |
| 78 | unsigned long fan_last_updated; |
| 79 | unsigned long in_last_updated; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 80 | u16 temp[SCH5627_NO_TEMPS]; |
| 81 | u16 fan[SCH5627_NO_FANS]; |
| 82 | u16 in[SCH5627_NO_IN]; |
| 83 | }; |
| 84 | |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 85 | static int sch5627_update_temp(struct sch5627_data *data) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 86 | { |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 87 | int ret = 0; |
| 88 | int i, val; |
| 89 | |
| 90 | mutex_lock(&data->update_lock); |
| 91 | |
| 92 | /* Cache the values for 1 second */ |
| 93 | if (time_after(jiffies, data->temp_last_updated + HZ) || !data->temp_valid) { |
| 94 | for (i = 0; i < SCH5627_NO_TEMPS; i++) { |
| 95 | val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_TEMP_MSB[i], |
| 96 | SCH5627_REG_TEMP_LSN[i], |
| 97 | SCH5627_REG_TEMP_HIGH_NIBBLE[i]); |
| 98 | if (unlikely(val < 0)) { |
| 99 | ret = val; |
| 100 | goto abort; |
| 101 | } |
| 102 | data->temp[i] = val; |
| 103 | } |
| 104 | data->temp_last_updated = jiffies; |
| 105 | data->temp_valid = 1; |
| 106 | } |
| 107 | abort: |
| 108 | mutex_unlock(&data->update_lock); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static int sch5627_update_fan(struct sch5627_data *data) |
| 113 | { |
| 114 | int ret = 0; |
| 115 | int i, val; |
| 116 | |
| 117 | mutex_lock(&data->update_lock); |
| 118 | |
| 119 | /* Cache the values for 1 second */ |
| 120 | if (time_after(jiffies, data->fan_last_updated + HZ) || !data->fan_valid) { |
| 121 | for (i = 0; i < SCH5627_NO_FANS; i++) { |
| 122 | val = sch56xx_read_virtual_reg16(data->addr, SCH5627_REG_FAN[i]); |
| 123 | if (unlikely(val < 0)) { |
| 124 | ret = val; |
| 125 | goto abort; |
| 126 | } |
| 127 | data->fan[i] = val; |
| 128 | } |
| 129 | data->fan_last_updated = jiffies; |
| 130 | data->fan_valid = 1; |
| 131 | } |
| 132 | abort: |
| 133 | mutex_unlock(&data->update_lock); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static int sch5627_update_in(struct sch5627_data *data) |
| 138 | { |
| 139 | int ret = 0; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 140 | int i, val; |
| 141 | |
| 142 | mutex_lock(&data->update_lock); |
| 143 | |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 144 | /* Trigger a Vbat voltage measurement every 5 minutes */ |
| 145 | if (time_after(jiffies, data->last_battery + 300 * HZ)) { |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 146 | sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, data->control | 0x10); |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 147 | data->last_battery = jiffies; |
| 148 | } |
| 149 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 150 | /* Cache the values for 1 second */ |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 151 | if (time_after(jiffies, data->in_last_updated + HZ) || !data->in_valid) { |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 152 | for (i = 0; i < SCH5627_NO_IN; i++) { |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 153 | val = sch56xx_read_virtual_reg12(data->addr, SCH5627_REG_IN_MSB[i], |
| 154 | SCH5627_REG_IN_LSN[i], |
| 155 | SCH5627_REG_IN_HIGH_NIBBLE[i]); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 156 | if (unlikely(val < 0)) { |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 157 | ret = val; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 158 | goto abort; |
| 159 | } |
| 160 | data->in[i] = val; |
| 161 | } |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 162 | data->in_last_updated = jiffies; |
| 163 | data->in_valid = 1; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 164 | } |
| 165 | abort: |
| 166 | mutex_unlock(&data->update_lock); |
| 167 | return ret; |
| 168 | } |
| 169 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 170 | static int sch5627_read_limits(struct sch5627_data *data) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 171 | { |
| 172 | int i, val; |
| 173 | |
| 174 | for (i = 0; i < SCH5627_NO_TEMPS; i++) { |
| 175 | /* |
| 176 | * Note what SMSC calls ABS, is what lm_sensors calls max |
| 177 | * (aka high), and HIGH is what lm_sensors calls crit. |
| 178 | */ |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 179 | val = sch56xx_read_virtual_reg(data->addr, |
| 180 | SCH5627_REG_TEMP_ABS[i]); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 181 | if (val < 0) |
| 182 | return val; |
| 183 | data->temp_max[i] = val; |
| 184 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 185 | val = sch56xx_read_virtual_reg(data->addr, |
| 186 | SCH5627_REG_TEMP_HIGH[i]); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 187 | if (val < 0) |
| 188 | return val; |
| 189 | data->temp_crit[i] = val; |
| 190 | } |
| 191 | for (i = 0; i < SCH5627_NO_FANS; i++) { |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 192 | val = sch56xx_read_virtual_reg16(data->addr, |
| 193 | SCH5627_REG_FAN_MIN[i]); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 194 | if (val < 0) |
| 195 | return val; |
| 196 | data->fan_min[i] = val; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int reg_to_temp(u16 reg) |
| 203 | { |
| 204 | return (reg * 625) / 10 - 64000; |
| 205 | } |
| 206 | |
| 207 | static int reg_to_temp_limit(u8 reg) |
| 208 | { |
| 209 | return (reg - 64) * 1000; |
| 210 | } |
| 211 | |
| 212 | static int reg_to_rpm(u16 reg) |
| 213 | { |
| 214 | if (reg == 0) |
| 215 | return -EIO; |
| 216 | if (reg == 0xffff) |
| 217 | return 0; |
| 218 | |
| 219 | return 5400540 / reg; |
| 220 | } |
| 221 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 222 | static umode_t sch5627_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, |
| 223 | int channel) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 224 | { |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 225 | return 0444; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 228 | static int sch5627_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, |
| 229 | long *val) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 230 | { |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 231 | struct sch5627_data *data = dev_get_drvdata(dev); |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 232 | int ret; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 233 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 234 | switch (type) { |
| 235 | case hwmon_temp: |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 236 | ret = sch5627_update_temp(data); |
| 237 | if (ret < 0) |
| 238 | return ret; |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 239 | switch (attr) { |
| 240 | case hwmon_temp_input: |
| 241 | *val = reg_to_temp(data->temp[channel]); |
| 242 | return 0; |
| 243 | case hwmon_temp_max: |
| 244 | *val = reg_to_temp_limit(data->temp_max[channel]); |
| 245 | return 0; |
| 246 | case hwmon_temp_crit: |
| 247 | *val = reg_to_temp_limit(data->temp_crit[channel]); |
| 248 | return 0; |
| 249 | case hwmon_temp_fault: |
| 250 | *val = (data->temp[channel] == 0); |
| 251 | return 0; |
| 252 | default: |
| 253 | break; |
| 254 | } |
| 255 | break; |
| 256 | case hwmon_fan: |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 257 | ret = sch5627_update_fan(data); |
| 258 | if (ret < 0) |
| 259 | return ret; |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 260 | switch (attr) { |
| 261 | case hwmon_fan_input: |
| 262 | ret = reg_to_rpm(data->fan[channel]); |
| 263 | if (ret < 0) |
| 264 | return ret; |
| 265 | *val = ret; |
| 266 | return 0; |
| 267 | case hwmon_fan_min: |
| 268 | ret = reg_to_rpm(data->fan_min[channel]); |
| 269 | if (ret < 0) |
| 270 | return ret; |
| 271 | *val = ret; |
| 272 | return 0; |
| 273 | case hwmon_fan_fault: |
| 274 | *val = (data->fan[channel] == 0xffff); |
| 275 | return 0; |
| 276 | default: |
| 277 | break; |
| 278 | } |
| 279 | break; |
| 280 | case hwmon_in: |
Armin Wolf | 790ac8f | 2021-04-11 18:42:25 +0200 | [diff] [blame] | 281 | ret = sch5627_update_in(data); |
| 282 | if (ret < 0) |
| 283 | return ret; |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 284 | switch (attr) { |
| 285 | case hwmon_in_input: |
| 286 | *val = DIV_ROUND_CLOSEST(data->in[channel] * SCH5627_REG_IN_FACTOR[channel], |
| 287 | 10000); |
| 288 | return 0; |
| 289 | default: |
| 290 | break; |
| 291 | } |
| 292 | break; |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | return -EOPNOTSUPP; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 300 | static int sch5627_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, |
| 301 | int channel, const char **str) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 302 | { |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 303 | switch (type) { |
| 304 | case hwmon_in: |
| 305 | switch (attr) { |
| 306 | case hwmon_in_label: |
| 307 | *str = SCH5627_IN_LABELS[channel]; |
| 308 | return 0; |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | break; |
| 313 | default: |
| 314 | break; |
| 315 | } |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 316 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 317 | return -EOPNOTSUPP; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 320 | static const struct hwmon_ops sch5627_ops = { |
| 321 | .is_visible = sch5627_is_visible, |
| 322 | .read = sch5627_read, |
| 323 | .read_string = sch5627_read_string, |
| 324 | }; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 325 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 326 | static const struct hwmon_channel_info *sch5627_info[] = { |
| 327 | HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), |
| 328 | HWMON_CHANNEL_INFO(temp, |
| 329 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 330 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 331 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 332 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 333 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 334 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 335 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT, |
| 336 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_FAULT |
| 337 | ), |
| 338 | HWMON_CHANNEL_INFO(fan, |
| 339 | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, |
| 340 | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, |
| 341 | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT, |
| 342 | HWMON_F_INPUT | HWMON_F_MIN | HWMON_F_FAULT |
| 343 | ), |
| 344 | HWMON_CHANNEL_INFO(in, |
| 345 | HWMON_I_INPUT | HWMON_I_LABEL, |
| 346 | HWMON_I_INPUT | HWMON_I_LABEL, |
| 347 | HWMON_I_INPUT | HWMON_I_LABEL, |
| 348 | HWMON_I_INPUT | HWMON_I_LABEL, |
| 349 | HWMON_I_INPUT |
| 350 | ), |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 351 | NULL |
| 352 | }; |
| 353 | |
Armin Wolf | e7e0b46 | 2021-04-11 18:42:24 +0200 | [diff] [blame] | 354 | static const struct hwmon_chip_info sch5627_chip_info = { |
| 355 | .ops = &sch5627_ops, |
| 356 | .info = sch5627_info, |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 357 | }; |
| 358 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 359 | static int sch5627_probe(struct platform_device *pdev) |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 360 | { |
| 361 | struct sch5627_data *data; |
Armin Wolf | 93a6fb2 | 2021-04-17 23:09:19 +0200 | [diff] [blame] | 362 | struct device *hwmon_dev; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 363 | int err, build_code, build_id, hwmon_rev, val; |
| 364 | |
Guenter Roeck | bd8d8e0 | 2012-06-02 11:20:21 -0700 | [diff] [blame] | 365 | data = devm_kzalloc(&pdev->dev, sizeof(struct sch5627_data), |
| 366 | GFP_KERNEL); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 367 | if (!data) |
| 368 | return -ENOMEM; |
| 369 | |
| 370 | data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start; |
| 371 | mutex_init(&data->update_lock); |
| 372 | platform_set_drvdata(pdev, data); |
| 373 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 374 | val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 375 | if (val < 0) |
| 376 | return val; |
| 377 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 378 | if (val != SCH5627_HWMON_ID) { |
| 379 | pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon", |
| 380 | val, SCH5627_HWMON_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 381 | return -ENODEV; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 384 | val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 385 | if (val < 0) |
| 386 | return val; |
| 387 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 388 | if (val != SCH5627_COMPANY_ID) { |
| 389 | pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company", |
| 390 | val, SCH5627_COMPANY_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 391 | return -ENODEV; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 394 | val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 395 | if (val < 0) |
| 396 | return val; |
| 397 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 398 | if (val != SCH5627_PRIMARY_ID) { |
| 399 | pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary", |
| 400 | val, SCH5627_PRIMARY_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 401 | return -ENODEV; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 404 | build_code = sch56xx_read_virtual_reg(data->addr, |
| 405 | SCH5627_REG_BUILD_CODE); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 406 | if (build_code < 0) |
| 407 | return build_code; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 408 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 409 | build_id = sch56xx_read_virtual_reg16(data->addr, |
| 410 | SCH5627_REG_BUILD_ID); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 411 | if (build_id < 0) |
| 412 | return build_id; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 413 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 414 | hwmon_rev = sch56xx_read_virtual_reg(data->addr, |
| 415 | SCH5627_REG_HWMON_REV); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 416 | if (hwmon_rev < 0) |
| 417 | return hwmon_rev; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 418 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 419 | val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 420 | if (val < 0) |
| 421 | return val; |
| 422 | |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 423 | data->control = val; |
| 424 | if (!(data->control & 0x01)) { |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 425 | pr_err("hardware monitoring not enabled\n"); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 426 | return -ENODEV; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 427 | } |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 428 | /* Trigger a Vbat voltage measurement, so that we get a valid reading |
| 429 | the first time we read Vbat */ |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 430 | sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL, |
Hans de Goede | 357b9dc | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 431 | data->control | 0x10); |
| 432 | data->last_battery = jiffies; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * Read limits, we do this only once as reading a register on |
| 436 | * the sch5627 is quite expensive (and they don't change). |
| 437 | */ |
| 438 | err = sch5627_read_limits(data); |
| 439 | if (err) |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 440 | return err; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 441 | |
Hans de Goede | 28ff2f7 | 2011-07-25 21:46:09 +0200 | [diff] [blame] | 442 | pr_info("found %s chip at %#hx\n", DEVNAME, data->addr); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 443 | pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n", |
| 444 | build_code, build_id, hwmon_rev); |
| 445 | |
Armin Wolf | 93a6fb2 | 2021-04-17 23:09:19 +0200 | [diff] [blame] | 446 | hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, DEVNAME, data, |
| 447 | &sch5627_chip_info, NULL); |
Armin Wolf | bab10bf | 2021-04-17 23:09:20 +0200 | [diff] [blame] | 448 | if (IS_ERR(hwmon_dev)) |
| 449 | return PTR_ERR(hwmon_dev); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 450 | |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 451 | /* Note failing to register the watchdog is not a fatal error */ |
Armin Wolf | 2be5f0d | 2021-05-08 15:14:54 +0200 | [diff] [blame] | 452 | sch56xx_watchdog_register(&pdev->dev, data->addr, |
| 453 | (build_code << 24) | (build_id << 8) | hwmon_rev, |
| 454 | &data->update_lock, 1); |
Hans de Goede | 312869e | 2012-03-18 13:05:08 +0100 | [diff] [blame] | 455 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 456 | return 0; |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 459 | static struct platform_driver sch5627_driver = { |
| 460 | .driver = { |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 461 | .name = DRVNAME, |
| 462 | }, |
| 463 | .probe = sch5627_probe, |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 464 | }; |
| 465 | |
Axel Lin | 25a236a | 2011-11-25 02:31:00 -0500 | [diff] [blame] | 466 | module_platform_driver(sch5627_driver); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 467 | |
| 468 | MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver"); |
Hans de Goede | 7958e3b | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 469 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
Hans de Goede | a98d506 | 2011-03-21 17:59:36 +0100 | [diff] [blame] | 470 | MODULE_LICENSE("GPL"); |