Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * pc87360.c - Part of lm_sensors, Linux kernel modules |
| 4 | * for hardware monitoring |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 5 | * Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Copied from smsc47m1.c: |
| 8 | * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com> |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Supports the following chips: |
| 11 | * |
| 12 | * Chip #vin #fan #pwm #temp devid |
| 13 | * PC87360 - 2 2 - 0xE1 |
| 14 | * PC87363 - 2 2 - 0xE8 |
| 15 | * PC87364 - 3 3 - 0xE4 |
| 16 | * PC87365 11 3 3 2 0xE5 |
| 17 | * PC87366 11 3 3 3-4 0xE9 |
| 18 | * |
| 19 | * This driver assumes that no more than one chip is present, and one of |
| 20 | * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F). |
| 21 | */ |
| 22 | |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/jiffies.h> |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 30 | #include <linux/hwmon.h> |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 31 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 32 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 33 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 35 | #include <linux/acpi.h> |
H Hartley Sweeten | 6055fae | 2009-09-15 17:18:13 +0200 | [diff] [blame] | 36 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Uwe Kleine-König | 02e0500 | 2022-09-19 12:31:54 +0200 | [diff] [blame] | 38 | #define DRIVER_NAME "pc87360" |
| 39 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 40 | /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */ |
| 41 | #define CHAN_CNVRTD 0x80 /* new data ready */ |
| 42 | #define CHAN_ENA 0x01 /* enabled channel (temp or vin) */ |
| 43 | #define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */ |
| 44 | #define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */ |
| 45 | |
| 46 | #define TEMP_OTS_OE 0x20 /* OTS Output Enable */ |
| 47 | #define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */ |
| 48 | #define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */ |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static u8 devid; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 51 | static struct platform_device *pdev; |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 52 | static unsigned short extra_isa[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static u8 confreg[4]; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static int init = 1; |
| 56 | module_param(init, int, 0); |
| 57 | MODULE_PARM_DESC(init, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 58 | "Chip initialization level:\n" |
| 59 | " 0: None\n" |
| 60 | "*1: Forcibly enable internal voltage and temperature channels, except in9\n" |
| 61 | " 2: Forcibly enable all voltage and temperature channels, except in9\n" |
| 62 | " 3: Forcibly enable all voltage and temperature channels, including in9"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 64 | static unsigned short force_id; |
| 65 | module_param(force_id, ushort, 0); |
| 66 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* |
| 69 | * Super-I/O registers and operations |
| 70 | */ |
| 71 | |
| 72 | #define DEV 0x07 /* Register: Logical device select */ |
| 73 | #define DEVID 0x20 /* Register: Device ID */ |
| 74 | #define ACT 0x30 /* Register: Device activation */ |
| 75 | #define BASE 0x60 /* Register: Base address */ |
| 76 | |
| 77 | #define FSCM 0x09 /* Logical device: fans */ |
| 78 | #define VLM 0x0d /* Logical device: voltages */ |
| 79 | #define TMS 0x0e /* Logical device: temperatures */ |
Jim Cromie | 2a32ec2 | 2008-10-18 20:27:33 -0700 | [diff] [blame] | 80 | #define LDNI_MAX 3 |
| 81 | static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | #define LD_FAN 0 |
| 84 | #define LD_IN 1 |
| 85 | #define LD_TEMP 2 |
| 86 | |
| 87 | static inline void superio_outb(int sioaddr, int reg, int val) |
| 88 | { |
| 89 | outb(reg, sioaddr); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 90 | outb(val, sioaddr + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static inline int superio_inb(int sioaddr, int reg) |
| 94 | { |
| 95 | outb(reg, sioaddr); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 96 | return inb(sioaddr + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline void superio_exit(int sioaddr) |
| 100 | { |
| 101 | outb(0x02, sioaddr); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 102 | outb(0x02, sioaddr + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Logical devices |
| 107 | */ |
| 108 | |
| 109 | #define PC87360_EXTENT 0x10 |
| 110 | #define PC87365_REG_BANK 0x09 |
| 111 | #define NO_BANK 0xff |
| 112 | |
| 113 | /* |
| 114 | * Fan registers and conversions |
| 115 | */ |
| 116 | |
| 117 | /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */ |
| 118 | #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr)) |
| 119 | #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr)) |
| 120 | #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr)) |
| 121 | #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr)) |
| 122 | #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr)) |
| 123 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 124 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : \ |
| 125 | 480000 / ((val) * (div))) |
| 126 | #define FAN_TO_REG(val, div) ((val) <= 100 ? 0 : \ |
| 127 | 480000 / ((val) * (div))) |
| 128 | #define FAN_DIV_FROM_REG(val) (1 << (((val) >> 5) & 0x03)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | #define FAN_STATUS_FROM_REG(val) ((val) & 0x07) |
| 130 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 131 | #define FAN_CONFIG_MONITOR(val, nr) (((val) >> (2 + (nr) * 3)) & 1) |
| 132 | #define FAN_CONFIG_CONTROL(val, nr) (((val) >> (3 + (nr) * 3)) & 1) |
| 133 | #define FAN_CONFIG_INVERT(val, nr) (((val) >> (4 + (nr) * 3)) & 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 135 | #define PWM_FROM_REG(val, inv) ((inv) ? 255 - (val) : (val)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | static inline u8 PWM_TO_REG(int val, int inv) |
| 137 | { |
| 138 | if (inv) |
| 139 | val = 255 - val; |
| 140 | if (val < 0) |
| 141 | return 0; |
| 142 | if (val > 255) |
| 143 | return 255; |
| 144 | return val; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Voltage registers and conversions |
| 149 | */ |
| 150 | |
| 151 | #define PC87365_REG_IN_CONVRATE 0x07 |
| 152 | #define PC87365_REG_IN_CONFIG 0x08 |
| 153 | #define PC87365_REG_IN 0x0B |
| 154 | #define PC87365_REG_IN_MIN 0x0D |
| 155 | #define PC87365_REG_IN_MAX 0x0C |
| 156 | #define PC87365_REG_IN_STATUS 0x0A |
| 157 | #define PC87365_REG_IN_ALARMS1 0x00 |
| 158 | #define PC87365_REG_IN_ALARMS2 0x01 |
| 159 | #define PC87365_REG_VID 0x06 |
| 160 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 161 | #define IN_FROM_REG(val, ref) (((val) * (ref) + 128) / 256) |
| 162 | #define IN_TO_REG(val, ref) ((val) < 0 ? 0 : \ |
| 163 | (val) * 256 >= (ref) * 255 ? 255 : \ |
| 164 | ((val) * 256 + (ref) / 2) / (ref)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Temperature registers and conversions |
| 168 | */ |
| 169 | |
| 170 | #define PC87365_REG_TEMP_CONFIG 0x08 |
| 171 | #define PC87365_REG_TEMP 0x0B |
| 172 | #define PC87365_REG_TEMP_MIN 0x0D |
| 173 | #define PC87365_REG_TEMP_MAX 0x0C |
| 174 | #define PC87365_REG_TEMP_CRIT 0x0E |
| 175 | #define PC87365_REG_TEMP_STATUS 0x0A |
| 176 | #define PC87365_REG_TEMP_ALARMS 0x00 |
| 177 | |
| 178 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 179 | #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \ |
| 180 | (val) > 127000 ? 127 : \ |
| 181 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 182 | ((val) + 500) / 1000) |
| 183 | |
| 184 | /* |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 185 | * Device data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | */ |
| 187 | |
| 188 | struct pc87360_data { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 189 | const char *name; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 190 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 191 | struct mutex lock; |
| 192 | struct mutex update_lock; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 193 | bool valid; /* true if following fields are valid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | unsigned long last_updated; /* In jiffies */ |
| 195 | |
| 196 | int address[3]; |
| 197 | |
| 198 | u8 fannr, innr, tempnr; |
| 199 | |
| 200 | u8 fan[3]; /* Register value */ |
| 201 | u8 fan_min[3]; /* Register value */ |
| 202 | u8 fan_status[3]; /* Register value */ |
| 203 | u8 pwm[3]; /* Register value */ |
| 204 | u16 fan_conf; /* Configuration register values, combined */ |
| 205 | |
| 206 | u16 in_vref; /* 1 mV/bit */ |
| 207 | u8 in[14]; /* Register value */ |
| 208 | u8 in_min[14]; /* Register value */ |
| 209 | u8 in_max[14]; /* Register value */ |
| 210 | u8 in_crit[3]; /* Register value */ |
| 211 | u8 in_status[14]; /* Register value */ |
| 212 | u16 in_alarms; /* Register values, combined, masked */ |
| 213 | u8 vid_conf; /* Configuration register value */ |
| 214 | u8 vrm; |
| 215 | u8 vid; /* Register value */ |
| 216 | |
| 217 | s8 temp[3]; /* Register value */ |
| 218 | s8 temp_min[3]; /* Register value */ |
| 219 | s8 temp_max[3]; /* Register value */ |
| 220 | s8 temp_crit[3]; /* Register value */ |
| 221 | u8 temp_status[3]; /* Register value */ |
| 222 | u8 temp_alarms; /* Register value, masked */ |
| 223 | }; |
| 224 | |
| 225 | /* |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 226 | * ldi is the logical device index |
| 227 | * bank is for voltages and temperatures only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank, |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 230 | u8 reg) |
| 231 | { |
| 232 | int res; |
| 233 | |
| 234 | mutex_lock(&(data->lock)); |
| 235 | if (bank != NO_BANK) |
| 236 | outb_p(bank, data->address[ldi] + PC87365_REG_BANK); |
| 237 | res = inb_p(data->address[ldi] + reg); |
| 238 | mutex_unlock(&(data->lock)); |
| 239 | |
| 240 | return res; |
| 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank, |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 244 | u8 reg, u8 value) |
| 245 | { |
| 246 | mutex_lock(&(data->lock)); |
| 247 | if (bank != NO_BANK) |
| 248 | outb_p(bank, data->address[ldi] + PC87365_REG_BANK); |
| 249 | outb_p(value, data->address[ldi] + reg); |
| 250 | mutex_unlock(&(data->lock)); |
| 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 253 | static void pc87360_autodiv(struct device *dev, int nr) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 254 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 255 | struct pc87360_data *data = dev_get_drvdata(dev); |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 256 | u8 old_min = data->fan_min[nr]; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 257 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 258 | /* Increase clock divider if needed and possible */ |
| 259 | if ((data->fan_status[nr] & 0x04) /* overflow flag */ |
| 260 | || (data->fan[nr] >= 224)) { /* next to overflow */ |
| 261 | if ((data->fan_status[nr] & 0x60) != 0x60) { |
| 262 | data->fan_status[nr] += 0x20; |
| 263 | data->fan_min[nr] >>= 1; |
| 264 | data->fan[nr] >>= 1; |
| 265 | dev_dbg(dev, |
| 266 | "Increasing clock divider to %d for fan %d\n", |
| 267 | FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1); |
| 268 | } |
| 269 | } else { |
| 270 | /* Decrease clock divider if possible */ |
| 271 | while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */ |
| 272 | && data->fan[nr] < 85 /* bad accuracy */ |
| 273 | && (data->fan_status[nr] & 0x60) != 0x00) { |
| 274 | data->fan_status[nr] -= 0x20; |
| 275 | data->fan_min[nr] <<= 1; |
| 276 | data->fan[nr] <<= 1; |
| 277 | dev_dbg(dev, |
| 278 | "Decreasing clock divider to %d for fan %d\n", |
| 279 | FAN_DIV_FROM_REG(data->fan_status[nr]), |
| 280 | nr + 1); |
| 281 | } |
Jim Cromie | 11be27e | 2005-09-02 23:05:07 +0200 | [diff] [blame] | 282 | } |
Jim Cromie | 11be27e | 2005-09-02 23:05:07 +0200 | [diff] [blame] | 283 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 284 | /* Write new fan min if it changed */ |
| 285 | if (old_min != data->fan_min[nr]) { |
| 286 | pc87360_write_value(data, LD_FAN, NO_BANK, |
| 287 | PC87360_REG_FAN_MIN(nr), |
| 288 | data->fan_min[nr]); |
| 289 | } |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 292 | static struct pc87360_data *pc87360_update_device(struct device *dev) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 293 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 294 | struct pc87360_data *data = dev_get_drvdata(dev); |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 295 | u8 i; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 296 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 297 | mutex_lock(&data->update_lock); |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 298 | |
| 299 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
| 300 | dev_dbg(dev, "Data update\n"); |
| 301 | |
| 302 | /* Fans */ |
| 303 | for (i = 0; i < data->fannr; i++) { |
| 304 | if (FAN_CONFIG_MONITOR(data->fan_conf, i)) { |
| 305 | data->fan_status[i] = |
| 306 | pc87360_read_value(data, LD_FAN, |
| 307 | NO_BANK, PC87360_REG_FAN_STATUS(i)); |
| 308 | data->fan[i] = pc87360_read_value(data, LD_FAN, |
| 309 | NO_BANK, PC87360_REG_FAN(i)); |
| 310 | data->fan_min[i] = pc87360_read_value(data, |
| 311 | LD_FAN, NO_BANK, |
| 312 | PC87360_REG_FAN_MIN(i)); |
| 313 | /* Change clock divider if needed */ |
| 314 | pc87360_autodiv(dev, i); |
| 315 | /* Clear bits and write new divider */ |
| 316 | pc87360_write_value(data, LD_FAN, NO_BANK, |
| 317 | PC87360_REG_FAN_STATUS(i), |
| 318 | data->fan_status[i]); |
| 319 | } |
| 320 | if (FAN_CONFIG_CONTROL(data->fan_conf, i)) |
| 321 | data->pwm[i] = pc87360_read_value(data, LD_FAN, |
| 322 | NO_BANK, PC87360_REG_PWM(i)); |
| 323 | } |
| 324 | |
| 325 | /* Voltages */ |
Kees Cook | 4265eb0 | 2023-11-30 12:02:07 -0800 | [diff] [blame] | 326 | /* |
| 327 | * The min() below does not have any practical meaning and is |
| 328 | * only needed to silence a warning observed with gcc 12+. |
| 329 | */ |
| 330 | for (i = 0; i < min(data->innr, ARRAY_SIZE(data->in)); i++) { |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 331 | data->in_status[i] = pc87360_read_value(data, LD_IN, i, |
| 332 | PC87365_REG_IN_STATUS); |
| 333 | /* Clear bits */ |
| 334 | pc87360_write_value(data, LD_IN, i, |
| 335 | PC87365_REG_IN_STATUS, |
| 336 | data->in_status[i]); |
| 337 | if ((data->in_status[i] & CHAN_READY) == CHAN_READY) { |
| 338 | data->in[i] = pc87360_read_value(data, LD_IN, |
| 339 | i, PC87365_REG_IN); |
| 340 | } |
| 341 | if (data->in_status[i] & CHAN_ENA) { |
| 342 | data->in_min[i] = pc87360_read_value(data, |
| 343 | LD_IN, i, |
| 344 | PC87365_REG_IN_MIN); |
| 345 | data->in_max[i] = pc87360_read_value(data, |
| 346 | LD_IN, i, |
| 347 | PC87365_REG_IN_MAX); |
| 348 | if (i >= 11) |
| 349 | data->in_crit[i-11] = |
| 350 | pc87360_read_value(data, LD_IN, |
| 351 | i, PC87365_REG_TEMP_CRIT); |
| 352 | } |
| 353 | } |
| 354 | if (data->innr) { |
| 355 | data->in_alarms = pc87360_read_value(data, LD_IN, |
| 356 | NO_BANK, PC87365_REG_IN_ALARMS1) |
| 357 | | ((pc87360_read_value(data, LD_IN, |
| 358 | NO_BANK, PC87365_REG_IN_ALARMS2) |
| 359 | & 0x07) << 8); |
| 360 | data->vid = (data->vid_conf & 0xE0) ? |
| 361 | pc87360_read_value(data, LD_IN, |
| 362 | NO_BANK, PC87365_REG_VID) : 0x1F; |
| 363 | } |
| 364 | |
| 365 | /* Temperatures */ |
| 366 | for (i = 0; i < data->tempnr; i++) { |
| 367 | data->temp_status[i] = pc87360_read_value(data, |
| 368 | LD_TEMP, i, |
| 369 | PC87365_REG_TEMP_STATUS); |
| 370 | /* Clear bits */ |
| 371 | pc87360_write_value(data, LD_TEMP, i, |
| 372 | PC87365_REG_TEMP_STATUS, |
| 373 | data->temp_status[i]); |
| 374 | if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) { |
| 375 | data->temp[i] = pc87360_read_value(data, |
| 376 | LD_TEMP, i, |
| 377 | PC87365_REG_TEMP); |
| 378 | } |
| 379 | if (data->temp_status[i] & CHAN_ENA) { |
| 380 | data->temp_min[i] = pc87360_read_value(data, |
| 381 | LD_TEMP, i, |
| 382 | PC87365_REG_TEMP_MIN); |
| 383 | data->temp_max[i] = pc87360_read_value(data, |
| 384 | LD_TEMP, i, |
| 385 | PC87365_REG_TEMP_MAX); |
| 386 | data->temp_crit[i] = pc87360_read_value(data, |
| 387 | LD_TEMP, i, |
| 388 | PC87365_REG_TEMP_CRIT); |
| 389 | } |
| 390 | } |
| 391 | if (data->tempnr) { |
| 392 | data->temp_alarms = pc87360_read_value(data, LD_TEMP, |
| 393 | NO_BANK, PC87365_REG_TEMP_ALARMS) |
| 394 | & 0x3F; |
| 395 | } |
| 396 | |
| 397 | data->last_updated = jiffies; |
| 398 | data->valid = true; |
| 399 | } |
| 400 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 401 | mutex_unlock(&data->update_lock); |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 402 | |
| 403 | return data; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 406 | static ssize_t in_input_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 407 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 408 | { |
| 409 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 410 | struct pc87360_data *data = pc87360_update_device(dev); |
| 411 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index], |
| 412 | data->in_vref)); |
| 413 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 414 | |
| 415 | static struct sensor_device_attribute in_input[] = { |
| 416 | SENSOR_ATTR_RO(in0_input, in_input, 0), |
| 417 | SENSOR_ATTR_RO(in1_input, in_input, 1), |
| 418 | SENSOR_ATTR_RO(in2_input, in_input, 2), |
| 419 | SENSOR_ATTR_RO(in3_input, in_input, 3), |
| 420 | SENSOR_ATTR_RO(in4_input, in_input, 4), |
| 421 | SENSOR_ATTR_RO(in5_input, in_input, 5), |
| 422 | SENSOR_ATTR_RO(in6_input, in_input, 6), |
| 423 | SENSOR_ATTR_RO(in7_input, in_input, 7), |
| 424 | SENSOR_ATTR_RO(in8_input, in_input, 8), |
| 425 | SENSOR_ATTR_RO(in9_input, in_input, 9), |
| 426 | SENSOR_ATTR_RO(in10_input, in_input, 10), |
| 427 | }; |
| 428 | |
| 429 | static ssize_t in_status_show(struct device *dev, |
| 430 | struct device_attribute *devattr, char *buf) |
| 431 | { |
| 432 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 433 | struct pc87360_data *data = pc87360_update_device(dev); |
| 434 | return sprintf(buf, "%u\n", data->in_status[attr->index]); |
| 435 | } |
| 436 | |
| 437 | static struct sensor_device_attribute in_status[] = { |
| 438 | SENSOR_ATTR_RO(in0_status, in_status, 0), |
| 439 | SENSOR_ATTR_RO(in1_status, in_status, 1), |
| 440 | SENSOR_ATTR_RO(in2_status, in_status, 2), |
| 441 | SENSOR_ATTR_RO(in3_status, in_status, 3), |
| 442 | SENSOR_ATTR_RO(in4_status, in_status, 4), |
| 443 | SENSOR_ATTR_RO(in5_status, in_status, 5), |
| 444 | SENSOR_ATTR_RO(in6_status, in_status, 6), |
| 445 | SENSOR_ATTR_RO(in7_status, in_status, 7), |
| 446 | SENSOR_ATTR_RO(in8_status, in_status, 8), |
| 447 | SENSOR_ATTR_RO(in9_status, in_status, 9), |
| 448 | SENSOR_ATTR_RO(in10_status, in_status, 10), |
| 449 | }; |
| 450 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 451 | static ssize_t in_min_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 452 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 453 | { |
| 454 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 455 | struct pc87360_data *data = pc87360_update_device(dev); |
| 456 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index], |
| 457 | data->in_vref)); |
| 458 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 459 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 460 | static ssize_t in_min_store(struct device *dev, |
| 461 | struct device_attribute *devattr, const char *buf, |
| 462 | size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 463 | { |
| 464 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 465 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 466 | long val; |
| 467 | int err; |
| 468 | |
| 469 | err = kstrtol(buf, 10, &val); |
| 470 | if (err) |
| 471 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 472 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 473 | mutex_lock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 474 | data->in_min[attr->index] = IN_TO_REG(val, data->in_vref); |
| 475 | pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN, |
| 476 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 477 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 478 | return count; |
| 479 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 480 | |
| 481 | static struct sensor_device_attribute in_min[] = { |
| 482 | SENSOR_ATTR_RW(in0_min, in_min, 0), |
| 483 | SENSOR_ATTR_RW(in1_min, in_min, 1), |
| 484 | SENSOR_ATTR_RW(in2_min, in_min, 2), |
| 485 | SENSOR_ATTR_RW(in3_min, in_min, 3), |
| 486 | SENSOR_ATTR_RW(in4_min, in_min, 4), |
| 487 | SENSOR_ATTR_RW(in5_min, in_min, 5), |
| 488 | SENSOR_ATTR_RW(in6_min, in_min, 6), |
| 489 | SENSOR_ATTR_RW(in7_min, in_min, 7), |
| 490 | SENSOR_ATTR_RW(in8_min, in_min, 8), |
| 491 | SENSOR_ATTR_RW(in9_min, in_min, 9), |
| 492 | SENSOR_ATTR_RW(in10_min, in_min, 10), |
| 493 | }; |
| 494 | |
| 495 | static ssize_t in_max_show(struct device *dev, |
| 496 | struct device_attribute *devattr, char *buf) |
| 497 | { |
| 498 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 499 | struct pc87360_data *data = pc87360_update_device(dev); |
| 500 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index], |
| 501 | data->in_vref)); |
| 502 | } |
| 503 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 504 | static ssize_t in_max_store(struct device *dev, |
| 505 | struct device_attribute *devattr, const char *buf, |
| 506 | size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 507 | { |
| 508 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 509 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 510 | long val; |
| 511 | int err; |
| 512 | |
| 513 | err = kstrtol(buf, 10, &val); |
| 514 | if (err) |
| 515 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 516 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 517 | mutex_lock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 518 | data->in_max[attr->index] = IN_TO_REG(val, |
| 519 | data->in_vref); |
| 520 | pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX, |
| 521 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 522 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 523 | return count; |
| 524 | } |
| 525 | |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 526 | static struct sensor_device_attribute in_max[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 527 | SENSOR_ATTR_RW(in0_max, in_max, 0), |
| 528 | SENSOR_ATTR_RW(in1_max, in_max, 1), |
| 529 | SENSOR_ATTR_RW(in2_max, in_max, 2), |
| 530 | SENSOR_ATTR_RW(in3_max, in_max, 3), |
| 531 | SENSOR_ATTR_RW(in4_max, in_max, 4), |
| 532 | SENSOR_ATTR_RW(in5_max, in_max, 5), |
| 533 | SENSOR_ATTR_RW(in6_max, in_max, 6), |
| 534 | SENSOR_ATTR_RW(in7_max, in_max, 7), |
| 535 | SENSOR_ATTR_RW(in8_max, in_max, 8), |
| 536 | SENSOR_ATTR_RW(in9_max, in_max, 9), |
| 537 | SENSOR_ATTR_RW(in10_max, in_max, 10), |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 538 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Jim Cromie | 28f74e7 | 2008-10-18 20:27:30 -0700 | [diff] [blame] | 540 | /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */ |
| 541 | #define CHAN_ALM_MIN 0x02 /* min limit crossed */ |
| 542 | #define CHAN_ALM_MAX 0x04 /* max limit exceeded */ |
| 543 | #define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */ |
| 544 | |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 545 | /* |
| 546 | * show_in_min/max_alarm() reads data from the per-channel status |
| 547 | * register (sec 11.5.12), not the vin event status registers (sec |
| 548 | * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms) |
| 549 | */ |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 550 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 551 | static ssize_t in_min_alarm_show(struct device *dev, |
| 552 | struct device_attribute *devattr, char *buf) |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 553 | { |
| 554 | struct pc87360_data *data = pc87360_update_device(dev); |
| 555 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 556 | |
| 557 | return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN)); |
| 558 | } |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 559 | |
| 560 | static struct sensor_device_attribute in_min_alarm[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 561 | SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0), |
| 562 | SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1), |
| 563 | SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2), |
| 564 | SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3), |
| 565 | SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4), |
| 566 | SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5), |
| 567 | SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6), |
| 568 | SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7), |
| 569 | SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8), |
| 570 | SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9), |
| 571 | SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10), |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 572 | }; |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 573 | |
| 574 | static ssize_t in_max_alarm_show(struct device *dev, |
| 575 | struct device_attribute *devattr, char *buf) |
| 576 | { |
| 577 | struct pc87360_data *data = pc87360_update_device(dev); |
| 578 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 579 | |
| 580 | return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX)); |
| 581 | } |
| 582 | |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 583 | static struct sensor_device_attribute in_max_alarm[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 584 | SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0), |
| 585 | SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1), |
| 586 | SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2), |
| 587 | SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3), |
| 588 | SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4), |
| 589 | SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5), |
| 590 | SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6), |
| 591 | SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7), |
| 592 | SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8), |
| 593 | SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9), |
| 594 | SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10), |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 595 | }; |
| 596 | |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 597 | #define VIN_UNIT_ATTRS(X) \ |
| 598 | &in_input[X].dev_attr.attr, \ |
| 599 | &in_status[X].dev_attr.attr, \ |
| 600 | &in_min[X].dev_attr.attr, \ |
Jim Cromie | 492e965 | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 601 | &in_max[X].dev_attr.attr, \ |
| 602 | &in_min_alarm[X].dev_attr.attr, \ |
| 603 | &in_max_alarm[X].dev_attr.attr |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 604 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 605 | static ssize_t cpu0_vid_show(struct device *dev, |
| 606 | struct device_attribute *attr, char *buf) |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 607 | { |
| 608 | struct pc87360_data *data = pc87360_update_device(dev); |
| 609 | return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm)); |
| 610 | } |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 611 | static DEVICE_ATTR_RO(cpu0_vid); |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 612 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 613 | static ssize_t vrm_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 614 | char *buf) |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 615 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 616 | struct pc87360_data *data = dev_get_drvdata(dev); |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 617 | return sprintf(buf, "%u\n", data->vrm); |
| 618 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 619 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 620 | static ssize_t vrm_store(struct device *dev, struct device_attribute *attr, |
| 621 | const char *buf, size_t count) |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 622 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 623 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 624 | unsigned long val; |
| 625 | int err; |
| 626 | |
| 627 | err = kstrtoul(buf, 10, &val); |
| 628 | if (err) |
| 629 | return err; |
| 630 | |
Axel Lin | 5e3b561 | 2014-08-06 08:24:54 +0800 | [diff] [blame] | 631 | if (val > 255) |
| 632 | return -EINVAL; |
| 633 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 634 | data->vrm = val; |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 635 | return count; |
| 636 | } |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 637 | static DEVICE_ATTR_RW(vrm); |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 638 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 639 | static ssize_t alarms_in_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 640 | struct device_attribute *attr, char *buf) |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 641 | { |
| 642 | struct pc87360_data *data = pc87360_update_device(dev); |
| 643 | return sprintf(buf, "%u\n", data->in_alarms); |
| 644 | } |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 645 | static DEVICE_ATTR_RO(alarms_in); |
Jim Cromie | 44646c1 | 2006-09-24 21:01:56 +0200 | [diff] [blame] | 646 | |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 647 | static struct attribute *pc8736x_vin_attr_array[] = { |
| 648 | VIN_UNIT_ATTRS(0), |
| 649 | VIN_UNIT_ATTRS(1), |
| 650 | VIN_UNIT_ATTRS(2), |
| 651 | VIN_UNIT_ATTRS(3), |
| 652 | VIN_UNIT_ATTRS(4), |
| 653 | VIN_UNIT_ATTRS(5), |
| 654 | VIN_UNIT_ATTRS(6), |
| 655 | VIN_UNIT_ATTRS(7), |
| 656 | VIN_UNIT_ATTRS(8), |
| 657 | VIN_UNIT_ATTRS(9), |
| 658 | VIN_UNIT_ATTRS(10), |
| 659 | &dev_attr_cpu0_vid.attr, |
| 660 | &dev_attr_vrm.attr, |
| 661 | &dev_attr_alarms_in.attr, |
| 662 | NULL |
| 663 | }; |
| 664 | static const struct attribute_group pc8736x_vin_group = { |
| 665 | .attrs = pc8736x_vin_attr_array, |
| 666 | }; |
| 667 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 668 | static ssize_t therm_input_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 669 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 670 | { |
| 671 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 672 | struct pc87360_data *data = pc87360_update_device(dev); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 673 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index], |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 674 | data->in_vref)); |
| 675 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | * the +11 term below reflects the fact that VLM units 11,12,13 are |
| 679 | * used in the chip to measure voltage across the thermistors |
| 680 | */ |
| 681 | static struct sensor_device_attribute therm_input[] = { |
| 682 | SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11), |
| 683 | SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11), |
| 684 | SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11), |
| 685 | }; |
| 686 | |
| 687 | static ssize_t therm_status_show(struct device *dev, |
| 688 | struct device_attribute *devattr, char *buf) |
| 689 | { |
| 690 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 691 | struct pc87360_data *data = pc87360_update_device(dev); |
| 692 | return sprintf(buf, "%u\n", data->in_status[attr->index]); |
| 693 | } |
| 694 | |
| 695 | static struct sensor_device_attribute therm_status[] = { |
| 696 | SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11), |
| 697 | SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11), |
| 698 | SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11), |
| 699 | }; |
| 700 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 701 | static ssize_t therm_min_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 702 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 703 | { |
| 704 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 705 | struct pc87360_data *data = pc87360_update_device(dev); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 706 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index], |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 707 | data->in_vref)); |
| 708 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 709 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 710 | static ssize_t therm_min_store(struct device *dev, |
| 711 | struct device_attribute *devattr, |
| 712 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 713 | { |
| 714 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 715 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 716 | long val; |
| 717 | int err; |
| 718 | |
| 719 | err = kstrtol(buf, 10, &val); |
| 720 | if (err) |
| 721 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 722 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 723 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 724 | data->in_min[attr->index] = IN_TO_REG(val, data->in_vref); |
| 725 | pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN, |
| 726 | data->in_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 727 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 728 | return count; |
| 729 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 730 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 731 | static struct sensor_device_attribute therm_min[] = { |
| 732 | SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11), |
| 733 | SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11), |
| 734 | SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11), |
| 735 | }; |
| 736 | |
| 737 | static ssize_t therm_max_show(struct device *dev, |
| 738 | struct device_attribute *devattr, char *buf) |
| 739 | { |
| 740 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 741 | struct pc87360_data *data = pc87360_update_device(dev); |
| 742 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index], |
| 743 | data->in_vref)); |
| 744 | } |
| 745 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 746 | static ssize_t therm_max_store(struct device *dev, |
| 747 | struct device_attribute *devattr, |
| 748 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 749 | { |
| 750 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 751 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 752 | long val; |
| 753 | int err; |
| 754 | |
| 755 | err = kstrtol(buf, 10, &val); |
| 756 | if (err) |
| 757 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 758 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 759 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 760 | data->in_max[attr->index] = IN_TO_REG(val, data->in_vref); |
| 761 | pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX, |
| 762 | data->in_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 763 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 764 | return count; |
| 765 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 766 | |
| 767 | static struct sensor_device_attribute therm_max[] = { |
| 768 | SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11), |
| 769 | SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11), |
| 770 | SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11), |
| 771 | }; |
| 772 | |
| 773 | static ssize_t therm_crit_show(struct device *dev, |
| 774 | struct device_attribute *devattr, char *buf) |
| 775 | { |
| 776 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 777 | struct pc87360_data *data = pc87360_update_device(dev); |
| 778 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11], |
| 779 | data->in_vref)); |
| 780 | } |
| 781 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 782 | static ssize_t therm_crit_store(struct device *dev, |
| 783 | struct device_attribute *devattr, |
| 784 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 785 | { |
| 786 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 787 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 788 | long val; |
| 789 | int err; |
| 790 | |
| 791 | err = kstrtol(buf, 10, &val); |
| 792 | if (err) |
| 793 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 794 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 795 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 796 | data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref); |
| 797 | pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT, |
| 798 | data->in_crit[attr->index-11]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 799 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 800 | return count; |
| 801 | } |
| 802 | |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 803 | static struct sensor_device_attribute therm_crit[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 804 | SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11), |
| 805 | SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11), |
| 806 | SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11), |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 807 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 809 | /* |
| 810 | * show_therm_min/max_alarm() reads data from the per-channel voltage |
| 811 | * status register (sec 11.5.12) |
| 812 | */ |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 813 | static ssize_t therm_min_alarm_show(struct device *dev, |
| 814 | struct device_attribute *devattr, |
| 815 | char *buf) |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 816 | { |
| 817 | struct pc87360_data *data = pc87360_update_device(dev); |
| 818 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 819 | |
| 820 | return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN)); |
| 821 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 822 | |
| 823 | static struct sensor_device_attribute therm_min_alarm[] = { |
| 824 | SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11), |
| 825 | SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11), |
| 826 | SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11), |
| 827 | }; |
| 828 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 829 | static ssize_t therm_max_alarm_show(struct device *dev, |
| 830 | struct device_attribute *devattr, |
| 831 | char *buf) |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 832 | { |
| 833 | struct pc87360_data *data = pc87360_update_device(dev); |
| 834 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 835 | |
| 836 | return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX)); |
| 837 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 838 | |
| 839 | static struct sensor_device_attribute therm_max_alarm[] = { |
| 840 | SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11), |
| 841 | SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11), |
| 842 | SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11), |
| 843 | }; |
| 844 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 845 | static ssize_t therm_crit_alarm_show(struct device *dev, |
| 846 | struct device_attribute *devattr, |
| 847 | char *buf) |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 848 | { |
| 849 | struct pc87360_data *data = pc87360_update_device(dev); |
| 850 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 851 | |
| 852 | return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT)); |
| 853 | } |
| 854 | |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 855 | static struct sensor_device_attribute therm_crit_alarm[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 856 | SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11), |
| 857 | SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11), |
| 858 | SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11), |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 859 | }; |
| 860 | |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 861 | #define THERM_UNIT_ATTRS(X) \ |
| 862 | &therm_input[X].dev_attr.attr, \ |
| 863 | &therm_status[X].dev_attr.attr, \ |
| 864 | &therm_min[X].dev_attr.attr, \ |
| 865 | &therm_max[X].dev_attr.attr, \ |
Jim Cromie | 865c295 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 866 | &therm_crit[X].dev_attr.attr, \ |
| 867 | &therm_min_alarm[X].dev_attr.attr, \ |
| 868 | &therm_max_alarm[X].dev_attr.attr, \ |
| 869 | &therm_crit_alarm[X].dev_attr.attr |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 870 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 871 | static struct attribute *pc8736x_therm_attr_array[] = { |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 872 | THERM_UNIT_ATTRS(0), |
| 873 | THERM_UNIT_ATTRS(1), |
| 874 | THERM_UNIT_ATTRS(2), |
| 875 | NULL |
| 876 | }; |
| 877 | static const struct attribute_group pc8736x_therm_group = { |
| 878 | .attrs = pc8736x_therm_attr_array, |
| 879 | }; |
| 880 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 881 | static ssize_t temp_input_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 882 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 883 | { |
| 884 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 885 | struct pc87360_data *data = pc87360_update_device(dev); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 886 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 887 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 888 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 889 | static struct sensor_device_attribute temp_input[] = { |
| 890 | SENSOR_ATTR_RO(temp1_input, temp_input, 0), |
| 891 | SENSOR_ATTR_RO(temp2_input, temp_input, 1), |
| 892 | SENSOR_ATTR_RO(temp3_input, temp_input, 2), |
| 893 | }; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 894 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 895 | static ssize_t temp_status_show(struct device *dev, |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 896 | struct device_attribute *devattr, char *buf) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 897 | { |
| 898 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 899 | struct pc87360_data *data = pc87360_update_device(dev); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 900 | return sprintf(buf, "%d\n", data->temp_status[attr->index]); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 901 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 902 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 903 | static struct sensor_device_attribute temp_status[] = { |
| 904 | SENSOR_ATTR_RO(temp1_status, temp_status, 0), |
| 905 | SENSOR_ATTR_RO(temp2_status, temp_status, 1), |
| 906 | SENSOR_ATTR_RO(temp3_status, temp_status, 2), |
| 907 | }; |
| 908 | |
| 909 | static ssize_t temp_min_show(struct device *dev, |
| 910 | struct device_attribute *devattr, char *buf) |
| 911 | { |
| 912 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 913 | struct pc87360_data *data = pc87360_update_device(dev); |
| 914 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index])); |
| 915 | } |
| 916 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 917 | static ssize_t temp_min_store(struct device *dev, |
| 918 | struct device_attribute *devattr, |
| 919 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 920 | { |
| 921 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 922 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 923 | long val; |
| 924 | int err; |
| 925 | |
| 926 | err = kstrtol(buf, 10, &val); |
| 927 | if (err) |
| 928 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 929 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 930 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 931 | data->temp_min[attr->index] = TEMP_TO_REG(val); |
| 932 | pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN, |
| 933 | data->temp_min[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 934 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 935 | return count; |
| 936 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 937 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 938 | static struct sensor_device_attribute temp_min[] = { |
| 939 | SENSOR_ATTR_RW(temp1_min, temp_min, 0), |
| 940 | SENSOR_ATTR_RW(temp2_min, temp_min, 1), |
| 941 | SENSOR_ATTR_RW(temp3_min, temp_min, 2), |
| 942 | }; |
| 943 | |
| 944 | static ssize_t temp_max_show(struct device *dev, |
| 945 | struct device_attribute *devattr, char *buf) |
| 946 | { |
| 947 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 948 | struct pc87360_data *data = pc87360_update_device(dev); |
| 949 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index])); |
| 950 | } |
| 951 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 952 | static ssize_t temp_max_store(struct device *dev, |
| 953 | struct device_attribute *devattr, |
| 954 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 955 | { |
| 956 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 957 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 958 | long val; |
| 959 | int err; |
| 960 | |
| 961 | err = kstrtol(buf, 10, &val); |
| 962 | if (err) |
| 963 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 964 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 965 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 966 | data->temp_max[attr->index] = TEMP_TO_REG(val); |
| 967 | pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX, |
| 968 | data->temp_max[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 969 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 970 | return count; |
| 971 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 972 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 973 | static struct sensor_device_attribute temp_max[] = { |
| 974 | SENSOR_ATTR_RW(temp1_max, temp_max, 0), |
| 975 | SENSOR_ATTR_RW(temp2_max, temp_max, 1), |
| 976 | SENSOR_ATTR_RW(temp3_max, temp_max, 2), |
| 977 | }; |
| 978 | |
| 979 | static ssize_t temp_crit_show(struct device *dev, |
| 980 | struct device_attribute *devattr, char *buf) |
| 981 | { |
| 982 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 983 | struct pc87360_data *data = pc87360_update_device(dev); |
| 984 | return sprintf(buf, "%d\n", |
| 985 | TEMP_FROM_REG(data->temp_crit[attr->index])); |
| 986 | } |
| 987 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 988 | static ssize_t temp_crit_store(struct device *dev, |
| 989 | struct device_attribute *devattr, |
| 990 | const char *buf, size_t count) |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 991 | { |
| 992 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 993 | struct pc87360_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 994 | long val; |
| 995 | int err; |
| 996 | |
| 997 | err = kstrtol(buf, 10, &val); |
| 998 | if (err) |
| 999 | return err; |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 1000 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1001 | mutex_lock(&data->update_lock); |
Jim Cromie | 694fa05 | 2005-09-02 22:57:52 +0200 | [diff] [blame] | 1002 | data->temp_crit[attr->index] = TEMP_TO_REG(val); |
| 1003 | pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT, |
| 1004 | data->temp_crit[attr->index]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1005 | mutex_unlock(&data->update_lock); |
Jim Cromie | f0986bd | 2005-09-02 22:52:43 +0200 | [diff] [blame] | 1006 | return count; |
| 1007 | } |
| 1008 | |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 1009 | static struct sensor_device_attribute temp_crit[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1010 | SENSOR_ATTR_RW(temp1_crit, temp_crit, 0), |
| 1011 | SENSOR_ATTR_RW(temp2_crit, temp_crit, 1), |
| 1012 | SENSOR_ATTR_RW(temp3_crit, temp_crit, 2), |
Jim Cromie | dedc6a7 | 2006-01-09 23:24:41 +0100 | [diff] [blame] | 1013 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1015 | /* |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1016 | * temp_min/max_alarm_show() reads data from the per-channel status |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1017 | * register (sec 12.3.7), not the temp event status registers (sec |
| 1018 | * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms) |
| 1019 | */ |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1020 | static ssize_t temp_min_alarm_show(struct device *dev, |
| 1021 | struct device_attribute *devattr, |
| 1022 | char *buf) |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1023 | { |
| 1024 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1025 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 1026 | |
| 1027 | return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN)); |
| 1028 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1029 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1030 | static struct sensor_device_attribute temp_min_alarm[] = { |
| 1031 | SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0), |
| 1032 | SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1), |
| 1033 | SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2), |
| 1034 | }; |
| 1035 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1036 | static ssize_t temp_max_alarm_show(struct device *dev, |
| 1037 | struct device_attribute *devattr, |
| 1038 | char *buf) |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1039 | { |
| 1040 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1041 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 1042 | |
| 1043 | return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX)); |
| 1044 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1045 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1046 | static struct sensor_device_attribute temp_max_alarm[] = { |
| 1047 | SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0), |
| 1048 | SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1), |
| 1049 | SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2), |
| 1050 | }; |
| 1051 | |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1052 | static ssize_t temp_crit_alarm_show(struct device *dev, |
| 1053 | struct device_attribute *devattr, |
| 1054 | char *buf) |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1055 | { |
| 1056 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1057 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 1058 | |
| 1059 | return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT)); |
| 1060 | } |
| 1061 | |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1062 | static struct sensor_device_attribute temp_crit_alarm[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1063 | SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0), |
| 1064 | SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1), |
| 1065 | SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2), |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1066 | }; |
| 1067 | |
| 1068 | #define TEMP_FAULT 0x40 /* open diode */ |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1069 | static ssize_t temp_fault_show(struct device *dev, |
| 1070 | struct device_attribute *devattr, char *buf) |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1071 | { |
| 1072 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1073 | unsigned nr = to_sensor_dev_attr(devattr)->index; |
| 1074 | |
| 1075 | return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT)); |
| 1076 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1077 | |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1078 | static struct sensor_device_attribute temp_fault[] = { |
Guenter Roeck | eba42d3 | 2018-12-10 14:02:18 -0800 | [diff] [blame] | 1079 | SENSOR_ATTR_RO(temp1_fault, temp_fault, 0), |
| 1080 | SENSOR_ATTR_RO(temp2_fault, temp_fault, 1), |
| 1081 | SENSOR_ATTR_RO(temp3_fault, temp_fault, 2), |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1082 | }; |
| 1083 | |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1084 | #define TEMP_UNIT_ATTRS(X) \ |
| 1085 | { &temp_input[X].dev_attr.attr, \ |
| 1086 | &temp_status[X].dev_attr.attr, \ |
| 1087 | &temp_min[X].dev_attr.attr, \ |
| 1088 | &temp_max[X].dev_attr.attr, \ |
| 1089 | &temp_crit[X].dev_attr.attr, \ |
| 1090 | &temp_min_alarm[X].dev_attr.attr, \ |
| 1091 | &temp_max_alarm[X].dev_attr.attr, \ |
| 1092 | &temp_crit_alarm[X].dev_attr.attr, \ |
| 1093 | &temp_fault[X].dev_attr.attr, \ |
| 1094 | NULL \ |
| 1095 | } |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 1096 | |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1097 | static struct attribute *pc8736x_temp_attr[][10] = { |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 1098 | TEMP_UNIT_ATTRS(0), |
| 1099 | TEMP_UNIT_ATTRS(1), |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1100 | TEMP_UNIT_ATTRS(2) |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 1101 | }; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1102 | |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1103 | static const struct attribute_group pc8736x_temp_attr_group[] = { |
| 1104 | { .attrs = pc8736x_temp_attr[0] }, |
| 1105 | { .attrs = pc8736x_temp_attr[1] }, |
| 1106 | { .attrs = pc8736x_temp_attr[2] } |
Jim Cromie | 941c5c0 | 2006-09-24 21:02:44 +0200 | [diff] [blame] | 1107 | }; |
| 1108 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1109 | static ssize_t alarms_temp_show(struct device *dev, |
| 1110 | struct device_attribute *attr, char *buf) |
| 1111 | { |
| 1112 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1113 | return sprintf(buf, "%u\n", data->temp_alarms); |
| 1114 | } |
| 1115 | |
| 1116 | static DEVICE_ATTR_RO(alarms_temp); |
| 1117 | |
| 1118 | static ssize_t fan_input_show(struct device *dev, |
| 1119 | struct device_attribute *devattr, char *buf) |
| 1120 | { |
| 1121 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1122 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1123 | return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index], |
| 1124 | FAN_DIV_FROM_REG(data->fan_status[attr->index]))); |
| 1125 | } |
| 1126 | |
| 1127 | static struct sensor_device_attribute fan_input[] = { |
| 1128 | SENSOR_ATTR_RO(fan1_input, fan_input, 0), |
| 1129 | SENSOR_ATTR_RO(fan2_input, fan_input, 1), |
| 1130 | SENSOR_ATTR_RO(fan3_input, fan_input, 2), |
| 1131 | }; |
| 1132 | |
| 1133 | static ssize_t fan_status_show(struct device *dev, |
| 1134 | struct device_attribute *devattr, char *buf) |
| 1135 | { |
| 1136 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1137 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1138 | return sprintf(buf, "%u\n", |
| 1139 | FAN_STATUS_FROM_REG(data->fan_status[attr->index])); |
| 1140 | } |
| 1141 | |
| 1142 | static struct sensor_device_attribute fan_status[] = { |
| 1143 | SENSOR_ATTR_RO(fan1_status, fan_status, 0), |
| 1144 | SENSOR_ATTR_RO(fan2_status, fan_status, 1), |
| 1145 | SENSOR_ATTR_RO(fan3_status, fan_status, 2), |
| 1146 | }; |
| 1147 | |
| 1148 | static ssize_t fan_div_show(struct device *dev, |
| 1149 | struct device_attribute *devattr, char *buf) |
| 1150 | { |
| 1151 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1152 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1153 | return sprintf(buf, "%u\n", |
| 1154 | FAN_DIV_FROM_REG(data->fan_status[attr->index])); |
| 1155 | } |
| 1156 | |
| 1157 | static struct sensor_device_attribute fan_div[] = { |
| 1158 | SENSOR_ATTR_RO(fan1_div, fan_div, 0), |
| 1159 | SENSOR_ATTR_RO(fan2_div, fan_div, 1), |
| 1160 | SENSOR_ATTR_RO(fan3_div, fan_div, 2), |
| 1161 | }; |
| 1162 | |
| 1163 | static ssize_t fan_min_show(struct device *dev, |
| 1164 | struct device_attribute *devattr, char *buf) |
| 1165 | { |
| 1166 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1167 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1168 | return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index], |
| 1169 | FAN_DIV_FROM_REG(data->fan_status[attr->index]))); |
| 1170 | } |
| 1171 | |
| 1172 | static ssize_t fan_min_store(struct device *dev, |
| 1173 | struct device_attribute *devattr, |
| 1174 | const char *buf, size_t count) |
| 1175 | { |
| 1176 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1177 | struct pc87360_data *data = dev_get_drvdata(dev); |
| 1178 | long fan_min; |
| 1179 | int err; |
| 1180 | |
| 1181 | err = kstrtol(buf, 10, &fan_min); |
| 1182 | if (err) |
| 1183 | return err; |
| 1184 | |
| 1185 | mutex_lock(&data->update_lock); |
| 1186 | fan_min = FAN_TO_REG(fan_min, |
| 1187 | FAN_DIV_FROM_REG(data->fan_status[attr->index])); |
| 1188 | |
| 1189 | /* If it wouldn't fit, change clock divisor */ |
| 1190 | while (fan_min > 255 |
| 1191 | && (data->fan_status[attr->index] & 0x60) != 0x60) { |
| 1192 | fan_min >>= 1; |
| 1193 | data->fan[attr->index] >>= 1; |
| 1194 | data->fan_status[attr->index] += 0x20; |
| 1195 | } |
| 1196 | data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min; |
| 1197 | pc87360_write_value(data, LD_FAN, NO_BANK, |
| 1198 | PC87360_REG_FAN_MIN(attr->index), |
| 1199 | data->fan_min[attr->index]); |
| 1200 | |
| 1201 | /* Write new divider, preserve alarm bits */ |
| 1202 | pc87360_write_value(data, LD_FAN, NO_BANK, |
| 1203 | PC87360_REG_FAN_STATUS(attr->index), |
| 1204 | data->fan_status[attr->index] & 0xF9); |
| 1205 | mutex_unlock(&data->update_lock); |
| 1206 | |
| 1207 | return count; |
| 1208 | } |
| 1209 | |
| 1210 | static struct sensor_device_attribute fan_min[] = { |
| 1211 | SENSOR_ATTR_RW(fan1_min, fan_min, 0), |
| 1212 | SENSOR_ATTR_RW(fan2_min, fan_min, 1), |
| 1213 | SENSOR_ATTR_RW(fan3_min, fan_min, 2), |
| 1214 | }; |
| 1215 | |
| 1216 | #define FAN_UNIT_ATTRS(X) \ |
| 1217 | { &fan_input[X].dev_attr.attr, \ |
| 1218 | &fan_status[X].dev_attr.attr, \ |
| 1219 | &fan_div[X].dev_attr.attr, \ |
| 1220 | &fan_min[X].dev_attr.attr, \ |
| 1221 | NULL \ |
| 1222 | } |
| 1223 | |
| 1224 | static struct attribute *pc8736x_fan_attr[][5] = { |
| 1225 | FAN_UNIT_ATTRS(0), |
| 1226 | FAN_UNIT_ATTRS(1), |
| 1227 | FAN_UNIT_ATTRS(2) |
| 1228 | }; |
| 1229 | |
| 1230 | static const struct attribute_group pc8736x_fan_attr_group[] = { |
| 1231 | { .attrs = pc8736x_fan_attr[0], }, |
| 1232 | { .attrs = pc8736x_fan_attr[1], }, |
| 1233 | { .attrs = pc8736x_fan_attr[2], }, |
| 1234 | }; |
| 1235 | |
| 1236 | static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr, |
| 1237 | char *buf) |
| 1238 | { |
| 1239 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1240 | struct pc87360_data *data = pc87360_update_device(dev); |
| 1241 | return sprintf(buf, "%u\n", |
| 1242 | PWM_FROM_REG(data->pwm[attr->index], |
| 1243 | FAN_CONFIG_INVERT(data->fan_conf, |
| 1244 | attr->index))); |
| 1245 | } |
| 1246 | |
| 1247 | static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr, |
| 1248 | const char *buf, size_t count) |
| 1249 | { |
| 1250 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1251 | struct pc87360_data *data = dev_get_drvdata(dev); |
| 1252 | long val; |
| 1253 | int err; |
| 1254 | |
| 1255 | err = kstrtol(buf, 10, &val); |
| 1256 | if (err) |
| 1257 | return err; |
| 1258 | |
| 1259 | mutex_lock(&data->update_lock); |
| 1260 | data->pwm[attr->index] = PWM_TO_REG(val, |
| 1261 | FAN_CONFIG_INVERT(data->fan_conf, attr->index)); |
| 1262 | pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index), |
| 1263 | data->pwm[attr->index]); |
| 1264 | mutex_unlock(&data->update_lock); |
| 1265 | return count; |
| 1266 | } |
| 1267 | |
| 1268 | static struct sensor_device_attribute pwm[] = { |
| 1269 | SENSOR_ATTR_RW(pwm1, pwm, 0), |
| 1270 | SENSOR_ATTR_RW(pwm2, pwm, 1), |
| 1271 | SENSOR_ATTR_RW(pwm3, pwm, 2), |
| 1272 | }; |
| 1273 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 1274 | static ssize_t name_show(struct device *dev, |
Jim Cromie | b267e8c | 2008-10-18 20:27:32 -0700 | [diff] [blame] | 1275 | struct device_attribute *devattr, char *buf) |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1276 | { |
| 1277 | struct pc87360_data *data = dev_get_drvdata(dev); |
| 1278 | return sprintf(buf, "%s\n", data->name); |
| 1279 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1280 | |
Julia Lawall | e33110d | 2016-12-22 13:05:24 +0100 | [diff] [blame] | 1281 | static DEVICE_ATTR_RO(name); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1282 | |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1283 | static void pc87360_remove_files(struct device *dev) |
| 1284 | { |
| 1285 | int i; |
| 1286 | |
| 1287 | device_remove_file(dev, &dev_attr_name); |
| 1288 | device_remove_file(dev, &dev_attr_alarms_temp); |
| 1289 | for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++) |
| 1290 | sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]); |
| 1291 | for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) { |
| 1292 | sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]); |
| 1293 | device_remove_file(dev, &pwm[i].dev_attr); |
| 1294 | } |
| 1295 | sysfs_remove_group(&dev->kobj, &pc8736x_therm_group); |
| 1296 | sysfs_remove_group(&dev->kobj, &pc8736x_vin_group); |
| 1297 | } |
| 1298 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1299 | static void pc87360_init_device(struct platform_device *pdev, |
| 1300 | int use_thermistors) |
| 1301 | { |
| 1302 | struct pc87360_data *data = platform_get_drvdata(pdev); |
| 1303 | int i, nr; |
| 1304 | const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 }; |
| 1305 | const u8 init_temp[3] = { 2, 2, 1 }; |
| 1306 | u8 reg; |
| 1307 | |
| 1308 | if (init >= 2 && data->innr) { |
| 1309 | reg = pc87360_read_value(data, LD_IN, NO_BANK, |
| 1310 | PC87365_REG_IN_CONVRATE); |
| 1311 | dev_info(&pdev->dev, |
| 1312 | "VLM conversion set to 1s period, 160us delay\n"); |
| 1313 | pc87360_write_value(data, LD_IN, NO_BANK, |
| 1314 | PC87365_REG_IN_CONVRATE, |
| 1315 | (reg & 0xC0) | 0x11); |
| 1316 | } |
| 1317 | |
| 1318 | nr = data->innr < 11 ? data->innr : 11; |
| 1319 | for (i = 0; i < nr; i++) { |
| 1320 | reg = pc87360_read_value(data, LD_IN, i, |
| 1321 | PC87365_REG_IN_STATUS); |
| 1322 | dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg); |
| 1323 | if (init >= init_in[i]) { |
| 1324 | /* Forcibly enable voltage channel */ |
| 1325 | if (!(reg & CHAN_ENA)) { |
| 1326 | dev_dbg(&pdev->dev, "Forcibly enabling in%d\n", |
| 1327 | i); |
| 1328 | pc87360_write_value(data, LD_IN, i, |
| 1329 | PC87365_REG_IN_STATUS, |
| 1330 | (reg & 0x68) | 0x87); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * We can't blindly trust the Super-I/O space configuration bit, |
| 1337 | * most BIOS won't set it properly |
| 1338 | */ |
| 1339 | dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors); |
| 1340 | for (i = 11; i < data->innr; i++) { |
| 1341 | reg = pc87360_read_value(data, LD_IN, i, |
| 1342 | PC87365_REG_TEMP_STATUS); |
| 1343 | use_thermistors = use_thermistors || (reg & CHAN_ENA); |
| 1344 | /* thermistors are temp[4-6], measured on vin[11-14] */ |
| 1345 | dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg); |
| 1346 | } |
| 1347 | dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors); |
| 1348 | |
| 1349 | i = use_thermistors ? 2 : 0; |
| 1350 | for (; i < data->tempnr; i++) { |
| 1351 | reg = pc87360_read_value(data, LD_TEMP, i, |
| 1352 | PC87365_REG_TEMP_STATUS); |
| 1353 | dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg); |
| 1354 | if (init >= init_temp[i]) { |
| 1355 | /* Forcibly enable temperature channel */ |
| 1356 | if (!(reg & CHAN_ENA)) { |
| 1357 | dev_dbg(&pdev->dev, |
| 1358 | "Forcibly enabling temp%d\n", i + 1); |
| 1359 | pc87360_write_value(data, LD_TEMP, i, |
| 1360 | PC87365_REG_TEMP_STATUS, |
| 1361 | 0xCF); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (use_thermistors) { |
| 1367 | for (i = 11; i < data->innr; i++) { |
| 1368 | if (init >= init_in[i]) { |
| 1369 | /* |
| 1370 | * The pin may already be used by thermal |
| 1371 | * diodes |
| 1372 | */ |
| 1373 | reg = pc87360_read_value(data, LD_TEMP, |
| 1374 | (i - 11) / 2, PC87365_REG_TEMP_STATUS); |
| 1375 | if (reg & CHAN_ENA) { |
| 1376 | dev_dbg(&pdev->dev, |
| 1377 | "Skipping temp%d, pin already in use by temp%d\n", |
| 1378 | i - 7, (i - 11) / 2); |
| 1379 | continue; |
| 1380 | } |
| 1381 | |
| 1382 | /* Forcibly enable thermistor channel */ |
| 1383 | reg = pc87360_read_value(data, LD_IN, i, |
| 1384 | PC87365_REG_IN_STATUS); |
| 1385 | if (!(reg & CHAN_ENA)) { |
| 1386 | dev_dbg(&pdev->dev, |
| 1387 | "Forcibly enabling temp%d\n", |
| 1388 | i - 7); |
| 1389 | pc87360_write_value(data, LD_IN, i, |
| 1390 | PC87365_REG_TEMP_STATUS, |
| 1391 | (reg & 0x60) | 0x8F); |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | if (data->innr) { |
| 1398 | reg = pc87360_read_value(data, LD_IN, NO_BANK, |
| 1399 | PC87365_REG_IN_CONFIG); |
| 1400 | dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg); |
| 1401 | if (reg & CHAN_ENA) { |
| 1402 | dev_dbg(&pdev->dev, |
| 1403 | "Forcibly enabling monitoring (VLM)\n"); |
| 1404 | pc87360_write_value(data, LD_IN, NO_BANK, |
| 1405 | PC87365_REG_IN_CONFIG, |
| 1406 | reg & 0xFE); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | if (data->tempnr) { |
| 1411 | reg = pc87360_read_value(data, LD_TEMP, NO_BANK, |
| 1412 | PC87365_REG_TEMP_CONFIG); |
| 1413 | dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg); |
| 1414 | if (reg & CHAN_ENA) { |
| 1415 | dev_dbg(&pdev->dev, |
| 1416 | "Forcibly enabling monitoring (TMS)\n"); |
| 1417 | pc87360_write_value(data, LD_TEMP, NO_BANK, |
| 1418 | PC87365_REG_TEMP_CONFIG, |
| 1419 | reg & 0xFE); |
| 1420 | } |
| 1421 | |
| 1422 | if (init >= 2) { |
| 1423 | /* Chip config as documented by National Semi. */ |
| 1424 | pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08); |
| 1425 | /* |
| 1426 | * We voluntarily omit the bank here, in case the |
| 1427 | * sequence itself matters. It shouldn't be a problem, |
| 1428 | * since nobody else is supposed to access the |
| 1429 | * device at that point. |
| 1430 | */ |
| 1431 | pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04); |
| 1432 | pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35); |
| 1433 | pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05); |
| 1434 | pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 1439 | static int pc87360_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | { |
| 1441 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | struct pc87360_data *data; |
| 1443 | int err = 0; |
Jean Delvare | 5b58157 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 1444 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | int use_thermistors = 0; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1446 | struct device *dev = &pdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1448 | data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL); |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1449 | if (!data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | switch (devid) { |
Jean Delvare | 5b58157 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 1453 | default: |
| 1454 | name = "pc87360"; |
| 1455 | data->fannr = 2; |
| 1456 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | case 0xe8: |
| 1458 | name = "pc87363"; |
Jean Delvare | 5b58157 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 1459 | data->fannr = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | break; |
| 1461 | case 0xe4: |
| 1462 | name = "pc87364"; |
| 1463 | data->fannr = 3; |
| 1464 | break; |
| 1465 | case 0xe5: |
| 1466 | name = "pc87365"; |
| 1467 | data->fannr = extra_isa[0] ? 3 : 0; |
| 1468 | data->innr = extra_isa[1] ? 11 : 0; |
| 1469 | data->tempnr = extra_isa[2] ? 2 : 0; |
| 1470 | break; |
| 1471 | case 0xe9: |
| 1472 | name = "pc87366"; |
| 1473 | data->fannr = extra_isa[0] ? 3 : 0; |
| 1474 | data->innr = extra_isa[1] ? 14 : 0; |
| 1475 | data->tempnr = extra_isa[2] ? 3 : 0; |
| 1476 | break; |
| 1477 | } |
| 1478 | |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1479 | data->name = name; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1480 | mutex_init(&data->lock); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1481 | mutex_init(&data->update_lock); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1482 | platform_set_drvdata(pdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Jim Cromie | 2a32ec2 | 2008-10-18 20:27:33 -0700 | [diff] [blame] | 1484 | for (i = 0; i < LDNI_MAX; i++) { |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1485 | data->address[i] = extra_isa[i]; |
| 1486 | if (data->address[i] |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1487 | && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT, |
Uwe Kleine-König | 02e0500 | 2022-09-19 12:31:54 +0200 | [diff] [blame] | 1488 | DRIVER_NAME)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 1489 | dev_err(dev, |
| 1490 | "Region 0x%x-0x%x already in use!\n", |
| 1491 | extra_isa[i], extra_isa[i]+PC87360_EXTENT-1); |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1492 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* Retrieve the fans configuration from Super-I/O space */ |
| 1497 | if (data->fannr) |
| 1498 | data->fan_conf = confreg[0] | (confreg[1] << 8); |
| 1499 | |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1500 | /* |
| 1501 | * Use the correct reference voltage |
| 1502 | * Unless both the VLM and the TMS logical devices agree to |
| 1503 | * use an external Vref, the internal one is used. |
| 1504 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | if (data->innr) { |
| 1506 | i = pc87360_read_value(data, LD_IN, NO_BANK, |
| 1507 | PC87365_REG_IN_CONFIG); |
| 1508 | if (data->tempnr) { |
| 1509 | i &= pc87360_read_value(data, LD_TEMP, NO_BANK, |
| 1510 | PC87365_REG_TEMP_CONFIG); |
| 1511 | } |
| 1512 | data->in_vref = (i&0x02) ? 3025 : 2966; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1513 | dev_dbg(dev, "Using %s reference voltage\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | (i&0x02) ? "external" : "internal"); |
| 1515 | |
| 1516 | data->vid_conf = confreg[3]; |
Jim Cromie | 3d7f9a8 | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1517 | data->vrm = vid_which_vrm(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* Fan clock dividers may be needed before any data is read */ |
| 1521 | for (i = 0; i < data->fannr; i++) { |
| 1522 | if (FAN_CONFIG_MONITOR(data->fan_conf, i)) |
| 1523 | data->fan_status[i] = pc87360_read_value(data, |
| 1524 | LD_FAN, NO_BANK, |
| 1525 | PC87360_REG_FAN_STATUS(i)); |
| 1526 | } |
| 1527 | |
| 1528 | if (init > 0) { |
| 1529 | if (devid == 0xe9 && data->address[1]) /* PC87366 */ |
| 1530 | use_thermistors = confreg[2] & 0x40; |
| 1531 | |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1532 | pc87360_init_device(pdev, use_thermistors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1535 | /* Register all-or-nothing sysfs groups */ |
| 1536 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1537 | if (data->innr) { |
| 1538 | err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group); |
| 1539 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1540 | goto error; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1541 | } |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1542 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1543 | if (data->innr == 14) { |
| 1544 | err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group); |
| 1545 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1546 | goto error; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1547 | } |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1548 | |
| 1549 | /* create device attr-files for varying sysfs groups */ |
| 1550 | |
| 1551 | if (data->tempnr) { |
| 1552 | for (i = 0; i < data->tempnr; i++) { |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1553 | err = sysfs_create_group(&dev->kobj, |
| 1554 | &pc8736x_temp_attr_group[i]); |
| 1555 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1556 | goto error; |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1557 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1558 | err = device_create_file(dev, &dev_attr_alarms_temp); |
| 1559 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1560 | goto error; |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | for (i = 0; i < data->fannr; i++) { |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1564 | if (FAN_CONFIG_MONITOR(data->fan_conf, i)) { |
| 1565 | err = sysfs_create_group(&dev->kobj, |
| 1566 | &pc8736x_fan_attr_group[i]); |
| 1567 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1568 | goto error; |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1569 | } |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1570 | if (FAN_CONFIG_CONTROL(data->fan_conf, i)) { |
| 1571 | err = device_create_file(dev, &pwm[i].dev_attr); |
| 1572 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1573 | goto error; |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1574 | } |
Jim Cromie | f3722d5b | 2006-09-24 21:03:25 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Guenter Roeck | 449a7a0 | 2012-01-14 21:49:53 -0800 | [diff] [blame] | 1577 | err = device_create_file(dev, &dev_attr_name); |
| 1578 | if (err) |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1579 | goto error; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1580 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1581 | data->hwmon_dev = hwmon_device_register(dev); |
| 1582 | if (IS_ERR(data->hwmon_dev)) { |
| 1583 | err = PTR_ERR(data->hwmon_dev); |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1584 | goto error; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
Guenter Roeck | 61d4d17 | 2012-06-02 11:20:19 -0700 | [diff] [blame] | 1588 | error: |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1589 | pc87360_remove_files(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | return err; |
| 1591 | } |
| 1592 | |
Uwe Kleine-König | c45af5d | 2023-09-18 10:59:39 +0200 | [diff] [blame] | 1593 | static void pc87360_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1595 | struct pc87360_data *data = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1597 | hwmon_device_unregister(data->hwmon_dev); |
Guenter Roeck | bce2778 | 2012-01-19 20:50:57 -0800 | [diff] [blame] | 1598 | pc87360_remove_files(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1601 | /* |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1602 | * Driver data |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1603 | */ |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1604 | static struct platform_driver pc87360_driver = { |
| 1605 | .driver = { |
| 1606 | .name = DRIVER_NAME, |
| 1607 | }, |
| 1608 | .probe = pc87360_probe, |
Uwe Kleine-König | c45af5d | 2023-09-18 10:59:39 +0200 | [diff] [blame] | 1609 | .remove_new = pc87360_remove, |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1610 | }; |
| 1611 | |
| 1612 | /* |
| 1613 | * Device detection, registration and update |
| 1614 | */ |
| 1615 | |
| 1616 | static int __init pc87360_find(int sioaddr, u8 *devid, |
| 1617 | unsigned short *addresses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | { |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1619 | u16 val; |
| 1620 | int i; |
| 1621 | int nrdev; /* logical device count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1623 | /* No superio_enter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1625 | /* Identify device */ |
| 1626 | val = force_id ? force_id : superio_inb(sioaddr, DEVID); |
| 1627 | switch (val) { |
| 1628 | case 0xE1: /* PC87360 */ |
| 1629 | case 0xE8: /* PC87363 */ |
| 1630 | case 0xE4: /* PC87364 */ |
| 1631 | nrdev = 1; |
| 1632 | break; |
| 1633 | case 0xE5: /* PC87365 */ |
| 1634 | case 0xE9: /* PC87366 */ |
| 1635 | nrdev = 3; |
| 1636 | break; |
| 1637 | default: |
| 1638 | superio_exit(sioaddr); |
| 1639 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | } |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1641 | /* Remember the device id */ |
| 1642 | *devid = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1644 | for (i = 0; i < nrdev; i++) { |
| 1645 | /* select logical device */ |
| 1646 | superio_outb(sioaddr, DEV, logdev[i]); |
| 1647 | |
| 1648 | val = superio_inb(sioaddr, ACT); |
| 1649 | if (!(val & 0x01)) { |
| 1650 | pr_info("Device 0x%02x not activated\n", logdev[i]); |
| 1651 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1654 | val = (superio_inb(sioaddr, BASE) << 8) |
| 1655 | | superio_inb(sioaddr, BASE + 1); |
| 1656 | if (!val) { |
| 1657 | pr_info("Base address not set for device 0x%02x\n", |
| 1658 | logdev[i]); |
| 1659 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1662 | addresses[i] = val; |
| 1663 | |
| 1664 | if (i == 0) { /* Fans */ |
| 1665 | confreg[0] = superio_inb(sioaddr, 0xF0); |
| 1666 | confreg[1] = superio_inb(sioaddr, 0xF1); |
| 1667 | |
| 1668 | pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1, |
| 1669 | (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1, |
| 1670 | (confreg[0] >> 4) & 1); |
| 1671 | pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2, |
| 1672 | (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1, |
| 1673 | (confreg[0] >> 7) & 1); |
| 1674 | pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3, |
| 1675 | confreg[1] & 1, (confreg[1] >> 1) & 1, |
| 1676 | (confreg[1] >> 2) & 1); |
| 1677 | } else if (i == 1) { /* Voltages */ |
| 1678 | /* Are we using thermistors? */ |
| 1679 | if (*devid == 0xE9) { /* PC87366 */ |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1680 | /* |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1681 | * These registers are not logical-device |
| 1682 | * specific, just that we won't need them if |
| 1683 | * we don't use the VLM device |
Guenter Roeck | 3af2861 | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 1684 | */ |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1685 | confreg[2] = superio_inb(sioaddr, 0x2B); |
| 1686 | confreg[3] = superio_inb(sioaddr, 0x25); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1688 | if (confreg[2] & 0x40) { |
| 1689 | pr_info("Using thermistors for temperature monitoring\n"); |
| 1690 | } |
| 1691 | if (confreg[3] & 0xE0) { |
| 1692 | pr_info("VID inputs routed (mode %u)\n", |
| 1693 | confreg[3] >> 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
Uwe Kleine-König | 070affa | 2022-09-19 12:31:55 +0200 | [diff] [blame] | 1699 | superio_exit(sioaddr); |
| 1700 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1703 | static int __init pc87360_device_add(unsigned short address) |
| 1704 | { |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1705 | struct resource res[3]; |
| 1706 | int err, i, res_count; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1707 | |
| 1708 | pdev = platform_device_alloc("pc87360", address); |
| 1709 | if (!pdev) { |
| 1710 | err = -ENOMEM; |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1711 | pr_err("Device allocation failed\n"); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1712 | goto exit; |
| 1713 | } |
| 1714 | |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1715 | memset(res, 0, 3 * sizeof(struct resource)); |
| 1716 | res_count = 0; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1717 | for (i = 0; i < 3; i++) { |
| 1718 | if (!extra_isa[i]) |
| 1719 | continue; |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1720 | res[res_count].start = extra_isa[i]; |
| 1721 | res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1; |
Zheng Yongjun | 94c08e0 | 2020-12-14 21:31:14 +0800 | [diff] [blame] | 1722 | res[res_count].name = "pc87360"; |
| 1723 | res[res_count].flags = IORESOURCE_IO; |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 1724 | |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1725 | err = acpi_check_resource_conflict(&res[res_count]); |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 1726 | if (err) |
| 1727 | goto exit_device_put; |
| 1728 | |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1729 | res_count++; |
| 1730 | } |
| 1731 | |
| 1732 | err = platform_device_add_resources(pdev, res, res_count); |
| 1733 | if (err) { |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1734 | pr_err("Device resources addition failed (%d)\n", err); |
Jean Delvare | b9783dc | 2010-08-14 21:08:48 +0200 | [diff] [blame] | 1735 | goto exit_device_put; |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | err = platform_device_add(pdev); |
| 1739 | if (err) { |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1740 | pr_err("Device addition failed (%d)\n", err); |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1741 | goto exit_device_put; |
| 1742 | } |
| 1743 | |
| 1744 | return 0; |
| 1745 | |
| 1746 | exit_device_put: |
| 1747 | platform_device_put(pdev); |
| 1748 | exit: |
| 1749 | return err; |
| 1750 | } |
| 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | static int __init pc87360_init(void) |
| 1753 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1754 | int err, i; |
| 1755 | unsigned short address = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
| 1757 | if (pc87360_find(0x2e, &devid, extra_isa) |
| 1758 | && pc87360_find(0x4e, &devid, extra_isa)) { |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1759 | pr_warn("PC8736x not detected, module not inserted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | return -ENODEV; |
| 1761 | } |
| 1762 | |
| 1763 | /* Arbitrarily pick one of the addresses */ |
| 1764 | for (i = 0; i < 3; i++) { |
| 1765 | if (extra_isa[i] != 0x0000) { |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 1766 | address = extra_isa[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | break; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 1771 | if (address == 0x0000) { |
Joe Perches | 9e991c6 | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1772 | pr_warn("No active logical device, module not inserted\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | return -ENODEV; |
| 1774 | } |
| 1775 | |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1776 | err = platform_driver_register(&pc87360_driver); |
| 1777 | if (err) |
| 1778 | goto exit; |
| 1779 | |
| 1780 | /* Sets global pdev as a side effect */ |
| 1781 | err = pc87360_device_add(address); |
| 1782 | if (err) |
| 1783 | goto exit_driver; |
| 1784 | |
| 1785 | return 0; |
| 1786 | |
| 1787 | exit_driver: |
| 1788 | platform_driver_unregister(&pc87360_driver); |
| 1789 | exit: |
| 1790 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static void __exit pc87360_exit(void) |
| 1794 | { |
Jean Delvare | f641b58 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1795 | platform_device_unregister(pdev); |
| 1796 | platform_driver_unregister(&pc87360_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 1799 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | MODULE_DESCRIPTION("PC8736x hardware monitor"); |
| 1801 | MODULE_LICENSE("GPL"); |
Uwe Kleine-König | 02e0500 | 2022-09-19 12:31:54 +0200 | [diff] [blame] | 1802 | MODULE_ALIAS("platform:" DRIVER_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
| 1804 | module_init(pc87360_init); |
| 1805 | module_exit(pc87360_exit); |