Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * nct6775 - Driver for the hardware monitoring functionality of |
| 3 | * Nuvoton NCT677x Super-I/O chips |
| 4 | * |
| 5 | * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net> |
| 6 | * |
| 7 | * Derived from w83627ehf driver |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 8 | * Copyright (C) 2005-2012 Jean Delvare <jdelvare@suse.de> |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 9 | * Copyright (C) 2006 Yuan Mu (Winbond), |
| 10 | * Rudolf Marek <r.marek@assembler.cz> |
| 11 | * David Hubbard <david.c.hubbard@gmail.com> |
| 12 | * Daniel J Blueman <daniel.blueman@gmail.com> |
| 13 | * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00) |
| 14 | * |
| 15 | * Shamelessly ripped from the w83627hf driver |
| 16 | * Copyright (C) 2003 Mark Studebaker |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation; either version 2 of the License, or |
| 21 | * (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | * GNU General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program; if not, write to the Free Software |
| 30 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 31 | * |
| 32 | * |
| 33 | * Supports the following chips: |
| 34 | * |
| 35 | * Chip #vin #fan #pwm #temp chip IDs man ID |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 36 | * nct6106d 9 3 3 6+3 0xc450 0xc1 0x5ca3 |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 37 | * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3 |
| 38 | * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3 |
| 39 | * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3 |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 40 | * nct6791d 15 6 6 2+6 0xc800 0xc1 0x5ca3 |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 41 | * nct6792d 15 6 6 2+6 0xc910 0xc1 0x5ca3 |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 42 | * nct6793d 15 6 6 2+6 0xd120 0xc1 0x5ca3 |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 43 | * |
| 44 | * #temp lists the number of monitored temperature sources (first value) plus |
| 45 | * the number of directly connectable temperature sensors (second value). |
| 46 | */ |
| 47 | |
| 48 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 49 | |
| 50 | #include <linux/module.h> |
| 51 | #include <linux/init.h> |
| 52 | #include <linux/slab.h> |
| 53 | #include <linux/jiffies.h> |
| 54 | #include <linux/platform_device.h> |
| 55 | #include <linux/hwmon.h> |
| 56 | #include <linux/hwmon-sysfs.h> |
| 57 | #include <linux/hwmon-vid.h> |
| 58 | #include <linux/err.h> |
| 59 | #include <linux/mutex.h> |
| 60 | #include <linux/acpi.h> |
Guenter Roeck | 25cdd99 | 2015-02-06 18:55:36 -0800 | [diff] [blame] | 61 | #include <linux/dmi.h> |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 62 | #include <linux/io.h> |
| 63 | #include "lm75.h" |
| 64 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 65 | #define USE_ALTERNATE |
| 66 | |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 67 | enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791, nct6792, nct6793 }; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 68 | |
| 69 | /* used to set data->name = nct6775_device_names[data->sio_kind] */ |
| 70 | static const char * const nct6775_device_names[] = { |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 71 | "nct6106", |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 72 | "nct6775", |
| 73 | "nct6776", |
| 74 | "nct6779", |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 75 | "nct6791", |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 76 | "nct6792", |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 77 | "nct6793", |
| 78 | }; |
| 79 | |
| 80 | static const char * const nct6775_sio_names[] __initconst = { |
| 81 | "NCT6106D", |
| 82 | "NCT6775F", |
| 83 | "NCT6776D/F", |
| 84 | "NCT6779D", |
| 85 | "NCT6791D", |
| 86 | "NCT6792D", |
| 87 | "NCT6793D", |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static unsigned short force_id; |
| 91 | module_param(force_id, ushort, 0); |
| 92 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 93 | |
Guenter Roeck | 47ece96 | 2012-12-04 07:59:32 -0800 | [diff] [blame] | 94 | static unsigned short fan_debounce; |
| 95 | module_param(fan_debounce, ushort, 0); |
| 96 | MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal"); |
| 97 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 98 | #define DRVNAME "nct6775" |
| 99 | |
| 100 | /* |
| 101 | * Super-I/O constants and functions |
| 102 | */ |
| 103 | |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 104 | #define NCT6775_LD_ACPI 0x0a |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 105 | #define NCT6775_LD_HWM 0x0b |
| 106 | #define NCT6775_LD_VID 0x0d |
| 107 | |
| 108 | #define SIO_REG_LDSEL 0x07 /* Logical device select */ |
| 109 | #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ |
| 110 | #define SIO_REG_ENABLE 0x30 /* Logical device enable */ |
| 111 | #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ |
| 112 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 113 | #define SIO_NCT6106_ID 0xc450 |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 114 | #define SIO_NCT6775_ID 0xb470 |
| 115 | #define SIO_NCT6776_ID 0xc330 |
| 116 | #define SIO_NCT6779_ID 0xc560 |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 117 | #define SIO_NCT6791_ID 0xc800 |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 118 | #define SIO_NCT6792_ID 0xc910 |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 119 | #define SIO_NCT6793_ID 0xd120 |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 120 | #define SIO_ID_MASK 0xFFF0 |
| 121 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 122 | enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 }; |
| 123 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 124 | static inline void |
| 125 | superio_outb(int ioreg, int reg, int val) |
| 126 | { |
| 127 | outb(reg, ioreg); |
| 128 | outb(val, ioreg + 1); |
| 129 | } |
| 130 | |
| 131 | static inline int |
| 132 | superio_inb(int ioreg, int reg) |
| 133 | { |
| 134 | outb(reg, ioreg); |
| 135 | return inb(ioreg + 1); |
| 136 | } |
| 137 | |
| 138 | static inline void |
| 139 | superio_select(int ioreg, int ld) |
| 140 | { |
| 141 | outb(SIO_REG_LDSEL, ioreg); |
| 142 | outb(ld, ioreg + 1); |
| 143 | } |
| 144 | |
| 145 | static inline int |
| 146 | superio_enter(int ioreg) |
| 147 | { |
| 148 | /* |
| 149 | * Try to reserve <ioreg> and <ioreg + 1> for exclusive access. |
| 150 | */ |
| 151 | if (!request_muxed_region(ioreg, 2, DRVNAME)) |
| 152 | return -EBUSY; |
| 153 | |
| 154 | outb(0x87, ioreg); |
| 155 | outb(0x87, ioreg); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static inline void |
| 161 | superio_exit(int ioreg) |
| 162 | { |
| 163 | outb(0xaa, ioreg); |
| 164 | outb(0x02, ioreg); |
| 165 | outb(0x02, ioreg + 1); |
| 166 | release_region(ioreg, 2); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * ISA constants |
| 171 | */ |
| 172 | |
| 173 | #define IOREGION_ALIGNMENT (~7) |
| 174 | #define IOREGION_OFFSET 5 |
| 175 | #define IOREGION_LENGTH 2 |
| 176 | #define ADDR_REG_OFFSET 0 |
| 177 | #define DATA_REG_OFFSET 1 |
| 178 | |
| 179 | #define NCT6775_REG_BANK 0x4E |
| 180 | #define NCT6775_REG_CONFIG 0x40 |
| 181 | |
| 182 | /* |
| 183 | * Not currently used: |
| 184 | * REG_MAN_ID has the value 0x5ca3 for all supported chips. |
| 185 | * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model. |
| 186 | * REG_MAN_ID is at port 0x4f |
| 187 | * REG_CHIP_ID is at port 0x58 |
| 188 | */ |
| 189 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 190 | #define NUM_TEMP 10 /* Max number of temp attribute sets w/ limits*/ |
| 191 | #define NUM_TEMP_FIXED 6 /* Max number of fixed temp attribute sets */ |
| 192 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 193 | #define NUM_REG_ALARM 7 /* Max number of alarm registers */ |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 194 | #define NUM_REG_BEEP 5 /* Max number of beep registers */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 195 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 196 | #define NUM_FAN 6 |
| 197 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 198 | /* Common and NCT6775 specific data */ |
| 199 | |
| 200 | /* Voltage min/max registers for nr=7..14 are in bank 5 */ |
| 201 | |
| 202 | static const u16 NCT6775_REG_IN_MAX[] = { |
| 203 | 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a, |
| 204 | 0x55c, 0x55e, 0x560, 0x562 }; |
| 205 | static const u16 NCT6775_REG_IN_MIN[] = { |
| 206 | 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b, |
| 207 | 0x55d, 0x55f, 0x561, 0x563 }; |
| 208 | static const u16 NCT6775_REG_IN[] = { |
| 209 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552 |
| 210 | }; |
| 211 | |
| 212 | #define NCT6775_REG_VBAT 0x5D |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 213 | #define NCT6775_REG_DIODE 0x5E |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 214 | #define NCT6775_DIODE_MASK 0x02 |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 215 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 216 | #define NCT6775_REG_FANDIV1 0x506 |
| 217 | #define NCT6775_REG_FANDIV2 0x507 |
| 218 | |
Guenter Roeck | 47ece96 | 2012-12-04 07:59:32 -0800 | [diff] [blame] | 219 | #define NCT6775_REG_CR_FAN_DEBOUNCE 0xf0 |
| 220 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 221 | static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B }; |
| 222 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 223 | /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 224 | |
| 225 | static const s8 NCT6775_ALARM_BITS[] = { |
| 226 | 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */ |
| 227 | 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */ |
| 228 | -1, /* unused */ |
Guenter Roeck | 41fa9a9 | 2013-06-23 13:04:04 -0700 | [diff] [blame] | 229 | 6, 7, 11, -1, -1, /* fan1..fan5 */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 230 | -1, -1, -1, /* unused */ |
| 231 | 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ |
| 232 | 12, -1 }; /* intrusion0, intrusion1 */ |
| 233 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 234 | #define FAN_ALARM_BASE 16 |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 235 | #define TEMP_ALARM_BASE 24 |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 236 | #define INTRUSION_ALARM_BASE 30 |
| 237 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 238 | static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e }; |
| 239 | |
| 240 | /* |
| 241 | * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures, |
| 242 | * 30..31 intrusion |
| 243 | */ |
| 244 | static const s8 NCT6775_BEEP_BITS[] = { |
| 245 | 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */ |
| 246 | 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */ |
| 247 | 21, /* global beep enable */ |
| 248 | 6, 7, 11, 28, -1, /* fan1..fan5 */ |
| 249 | -1, -1, -1, /* unused */ |
| 250 | 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ |
| 251 | 12, -1 }; /* intrusion0, intrusion1 */ |
| 252 | |
| 253 | #define BEEP_ENABLE_BASE 15 |
| 254 | |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 255 | static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee }; |
| 256 | static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 }; |
| 257 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 258 | /* DC or PWM output fan configuration */ |
| 259 | static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 }; |
| 260 | static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 }; |
| 261 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 262 | /* Advanced Fan control, some values are common for all fans */ |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 263 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 264 | static const u16 NCT6775_REG_TARGET[] = { |
| 265 | 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01 }; |
| 266 | static const u16 NCT6775_REG_FAN_MODE[] = { |
| 267 | 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 268 | static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 269 | 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 270 | static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 271 | 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 272 | static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 273 | 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05 }; |
| 274 | static const u16 NCT6775_REG_FAN_START_OUTPUT[] = { |
| 275 | 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 276 | static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a }; |
| 277 | static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b }; |
| 278 | |
| 279 | static const u16 NCT6775_REG_FAN_STOP_TIME[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 280 | 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07 }; |
| 281 | static const u16 NCT6775_REG_PWM[] = { |
| 282 | 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09 }; |
| 283 | static const u16 NCT6775_REG_PWM_READ[] = { |
| 284 | 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09 }; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 285 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 286 | static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 }; |
| 287 | static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d }; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 288 | static const u16 NCT6775_REG_FAN_PULSES[] = { 0x641, 0x642, 0x643, 0x644, 0 }; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 289 | static const u16 NCT6775_FAN_PULSE_SHIFT[] = { 0, 0, 0, 0, 0, 0 }; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 290 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 291 | static const u16 NCT6775_REG_TEMP[] = { |
| 292 | 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d }; |
| 293 | |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 294 | static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 }; |
| 295 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 296 | static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = { |
| 297 | 0, 0x152, 0x252, 0x628, 0x629, 0x62A }; |
| 298 | static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = { |
| 299 | 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D }; |
| 300 | static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = { |
| 301 | 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C }; |
| 302 | |
| 303 | static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = { |
| 304 | 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 }; |
| 305 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 306 | static const u16 NCT6775_REG_TEMP_SEL[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 307 | 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 308 | |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 309 | static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 310 | 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 311 | static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 312 | 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 313 | static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 314 | 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 315 | static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 316 | 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 317 | static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 318 | 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 319 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 320 | static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 }; |
| 321 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 322 | static const u16 NCT6775_REG_AUTO_TEMP[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 323 | 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 324 | static const u16 NCT6775_REG_AUTO_PWM[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 325 | 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 326 | |
| 327 | #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p)) |
| 328 | #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p)) |
| 329 | |
| 330 | static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 }; |
| 331 | |
| 332 | static const u16 NCT6775_REG_CRITICAL_TEMP[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 333 | 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 334 | static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 335 | 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 336 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 337 | static const char *const nct6775_temp_label[] = { |
| 338 | "", |
| 339 | "SYSTIN", |
| 340 | "CPUTIN", |
| 341 | "AUXTIN", |
| 342 | "AMD SB-TSI", |
| 343 | "PECI Agent 0", |
| 344 | "PECI Agent 1", |
| 345 | "PECI Agent 2", |
| 346 | "PECI Agent 3", |
| 347 | "PECI Agent 4", |
| 348 | "PECI Agent 5", |
| 349 | "PECI Agent 6", |
| 350 | "PECI Agent 7", |
| 351 | "PCH_CHIP_CPU_MAX_TEMP", |
| 352 | "PCH_CHIP_TEMP", |
| 353 | "PCH_CPU_TEMP", |
| 354 | "PCH_MCH_TEMP", |
| 355 | "PCH_DIM0_TEMP", |
| 356 | "PCH_DIM1_TEMP", |
| 357 | "PCH_DIM2_TEMP", |
| 358 | "PCH_DIM3_TEMP" |
| 359 | }; |
| 360 | |
| 361 | static const u16 NCT6775_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6775_temp_label) - 1] |
| 362 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x661, 0x662, 0x664 }; |
| 363 | |
| 364 | static const u16 NCT6775_REG_TEMP_CRIT[ARRAY_SIZE(nct6775_temp_label) - 1] |
| 365 | = { 0, 0, 0, 0, 0xa00, 0xa01, 0xa02, 0xa03, 0xa04, 0xa05, 0xa06, |
| 366 | 0xa07 }; |
| 367 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 368 | /* NCT6776 specific data */ |
| 369 | |
Guenter Roeck | 728d294 | 2015-08-31 16:13:47 -0700 | [diff] [blame] | 370 | /* STEP_UP_TIME and STEP_DOWN_TIME regs are swapped for all chips but NCT6775 */ |
| 371 | #define NCT6776_REG_FAN_STEP_UP_TIME NCT6775_REG_FAN_STEP_DOWN_TIME |
| 372 | #define NCT6776_REG_FAN_STEP_DOWN_TIME NCT6775_REG_FAN_STEP_UP_TIME |
| 373 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 374 | static const s8 NCT6776_ALARM_BITS[] = { |
| 375 | 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */ |
| 376 | 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */ |
| 377 | -1, /* unused */ |
| 378 | 6, 7, 11, 10, 23, /* fan1..fan5 */ |
| 379 | -1, -1, -1, /* unused */ |
| 380 | 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ |
| 381 | 12, 9 }; /* intrusion0, intrusion1 */ |
| 382 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 383 | static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 }; |
| 384 | |
| 385 | static const s8 NCT6776_BEEP_BITS[] = { |
| 386 | 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */ |
| 387 | 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */ |
| 388 | 24, /* global beep enable */ |
| 389 | 25, 26, 27, 28, 29, /* fan1..fan5 */ |
| 390 | -1, -1, -1, /* unused */ |
| 391 | 16, 17, 18, 19, 20, 21, /* temp1..temp6 */ |
| 392 | 30, 31 }; /* intrusion0, intrusion1 */ |
| 393 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 394 | static const u16 NCT6776_REG_TOLERANCE_H[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 395 | 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 396 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 397 | static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 }; |
| 398 | static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 }; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 399 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 400 | static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642 }; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 401 | static const u16 NCT6776_REG_FAN_PULSES[] = { 0x644, 0x645, 0x646, 0, 0 }; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 402 | |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 403 | static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 404 | 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e }; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 405 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 406 | static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = { |
| 407 | 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A }; |
| 408 | |
| 409 | static const char *const nct6776_temp_label[] = { |
| 410 | "", |
| 411 | "SYSTIN", |
| 412 | "CPUTIN", |
| 413 | "AUXTIN", |
| 414 | "SMBUSMASTER 0", |
| 415 | "SMBUSMASTER 1", |
| 416 | "SMBUSMASTER 2", |
| 417 | "SMBUSMASTER 3", |
| 418 | "SMBUSMASTER 4", |
| 419 | "SMBUSMASTER 5", |
| 420 | "SMBUSMASTER 6", |
| 421 | "SMBUSMASTER 7", |
| 422 | "PECI Agent 0", |
| 423 | "PECI Agent 1", |
| 424 | "PCH_CHIP_CPU_MAX_TEMP", |
| 425 | "PCH_CHIP_TEMP", |
| 426 | "PCH_CPU_TEMP", |
| 427 | "PCH_MCH_TEMP", |
| 428 | "PCH_DIM0_TEMP", |
| 429 | "PCH_DIM1_TEMP", |
| 430 | "PCH_DIM2_TEMP", |
| 431 | "PCH_DIM3_TEMP", |
| 432 | "BYTE_TEMP" |
| 433 | }; |
| 434 | |
| 435 | static const u16 NCT6776_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1] |
| 436 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x401, 0x402, 0x404 }; |
| 437 | |
| 438 | static const u16 NCT6776_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1] |
| 439 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a }; |
| 440 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 441 | /* NCT6779 specific data */ |
| 442 | |
| 443 | static const u16 NCT6779_REG_IN[] = { |
| 444 | 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487, |
| 445 | 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e }; |
| 446 | |
| 447 | static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = { |
| 448 | 0x459, 0x45A, 0x45B, 0x568 }; |
| 449 | |
| 450 | static const s8 NCT6779_ALARM_BITS[] = { |
| 451 | 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */ |
| 452 | 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */ |
| 453 | -1, /* unused */ |
| 454 | 6, 7, 11, 10, 23, /* fan1..fan5 */ |
| 455 | -1, -1, -1, /* unused */ |
| 456 | 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ |
| 457 | 12, 9 }; /* intrusion0, intrusion1 */ |
| 458 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 459 | static const s8 NCT6779_BEEP_BITS[] = { |
| 460 | 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */ |
| 461 | 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */ |
| 462 | 24, /* global beep enable */ |
| 463 | 25, 26, 27, 28, 29, /* fan1..fan5 */ |
| 464 | -1, -1, -1, /* unused */ |
| 465 | 16, 17, -1, -1, -1, -1, /* temp1..temp6 */ |
| 466 | 30, 31 }; /* intrusion0, intrusion1 */ |
| 467 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 468 | static const u16 NCT6779_REG_FAN[] = { |
| 469 | 0x4b0, 0x4b2, 0x4b4, 0x4b6, 0x4b8, 0x4ba }; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 470 | static const u16 NCT6779_REG_FAN_PULSES[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 471 | 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 }; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 472 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 473 | static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 474 | 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36 }; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 475 | #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01 |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 476 | static const u16 NCT6779_REG_CRITICAL_PWM[] = { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 477 | 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37 }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 478 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 479 | static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 }; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 480 | static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b }; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 481 | static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = { |
| 482 | 0x18, 0x152 }; |
| 483 | static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = { |
| 484 | 0x3a, 0x153 }; |
| 485 | static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = { |
| 486 | 0x39, 0x155 }; |
| 487 | |
| 488 | static const u16 NCT6779_REG_TEMP_OFFSET[] = { |
| 489 | 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c }; |
| 490 | |
| 491 | static const char *const nct6779_temp_label[] = { |
| 492 | "", |
| 493 | "SYSTIN", |
| 494 | "CPUTIN", |
| 495 | "AUXTIN0", |
| 496 | "AUXTIN1", |
| 497 | "AUXTIN2", |
| 498 | "AUXTIN3", |
| 499 | "", |
| 500 | "SMBUSMASTER 0", |
| 501 | "SMBUSMASTER 1", |
| 502 | "SMBUSMASTER 2", |
| 503 | "SMBUSMASTER 3", |
| 504 | "SMBUSMASTER 4", |
| 505 | "SMBUSMASTER 5", |
| 506 | "SMBUSMASTER 6", |
| 507 | "SMBUSMASTER 7", |
| 508 | "PECI Agent 0", |
| 509 | "PECI Agent 1", |
| 510 | "PCH_CHIP_CPU_MAX_TEMP", |
| 511 | "PCH_CHIP_TEMP", |
| 512 | "PCH_CPU_TEMP", |
| 513 | "PCH_MCH_TEMP", |
| 514 | "PCH_DIM0_TEMP", |
| 515 | "PCH_DIM1_TEMP", |
| 516 | "PCH_DIM2_TEMP", |
| 517 | "PCH_DIM3_TEMP", |
Guenter Roeck | 9a38371 | 2015-08-29 15:29:25 -0700 | [diff] [blame^] | 518 | "BYTE_TEMP", |
| 519 | "", |
| 520 | "", |
| 521 | "", |
| 522 | "", |
| 523 | "Virtual_TEMP" |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 524 | }; |
| 525 | |
Guenter Roeck | 9a38371 | 2015-08-29 15:29:25 -0700 | [diff] [blame^] | 526 | #define NCT6779_NUM_LABELS (ARRAY_SIZE(nct6779_temp_label) - 5) |
| 527 | #define NCT6791_NUM_LABELS ARRAY_SIZE(nct6779_temp_label) |
| 528 | |
| 529 | static const u16 NCT6779_REG_TEMP_ALTERNATE[NCT6791_NUM_LABELS - 1] |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 530 | = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0, |
| 531 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 532 | 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407, |
| 533 | 0x408, 0 }; |
| 534 | |
Guenter Roeck | 9a38371 | 2015-08-29 15:29:25 -0700 | [diff] [blame^] | 535 | static const u16 NCT6779_REG_TEMP_CRIT[NCT6791_NUM_LABELS - 1] |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 536 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x709, 0x70a }; |
| 537 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 538 | /* NCT6791 specific data */ |
| 539 | |
| 540 | #define NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE 0x28 |
| 541 | |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 542 | static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[6] = { 0, 0x239 }; |
| 543 | static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[6] = { 0, 0x23a }; |
| 544 | static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[6] = { 0, 0x23b }; |
| 545 | static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[6] = { 0, 0x23c }; |
| 546 | static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[6] = { 0, 0x23d }; |
| 547 | static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[6] = { 0, 0x23e }; |
| 548 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 549 | static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = { |
| 550 | 0x459, 0x45A, 0x45B, 0x568, 0x45D }; |
| 551 | |
| 552 | static const s8 NCT6791_ALARM_BITS[] = { |
| 553 | 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */ |
| 554 | 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */ |
| 555 | -1, /* unused */ |
| 556 | 6, 7, 11, 10, 23, 33, /* fan1..fan6 */ |
| 557 | -1, -1, /* unused */ |
| 558 | 4, 5, 13, -1, -1, -1, /* temp1..temp6 */ |
| 559 | 12, 9 }; /* intrusion0, intrusion1 */ |
| 560 | |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 561 | /* NCT6792/NCT6793 specific data */ |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 562 | |
| 563 | static const u16 NCT6792_REG_TEMP_MON[] = { |
| 564 | 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d }; |
| 565 | static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = { |
| 566 | 0xb2, 0xb3, 0xb4, 0xb5, 0xbf }; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 567 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 568 | /* NCT6102D/NCT6106D specific data */ |
| 569 | |
| 570 | #define NCT6106_REG_VBAT 0x318 |
| 571 | #define NCT6106_REG_DIODE 0x319 |
| 572 | #define NCT6106_DIODE_MASK 0x01 |
| 573 | |
| 574 | static const u16 NCT6106_REG_IN_MAX[] = { |
| 575 | 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 }; |
| 576 | static const u16 NCT6106_REG_IN_MIN[] = { |
| 577 | 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 }; |
| 578 | static const u16 NCT6106_REG_IN[] = { |
| 579 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 }; |
| 580 | |
| 581 | static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 }; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 582 | static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a }; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 583 | static const u16 NCT6106_REG_TEMP_HYST[] = { |
| 584 | 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 }; |
| 585 | static const u16 NCT6106_REG_TEMP_OVER[] = { |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 586 | 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 }; |
| 587 | static const u16 NCT6106_REG_TEMP_CRIT_L[] = { |
| 588 | 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 }; |
| 589 | static const u16 NCT6106_REG_TEMP_CRIT_H[] = { |
| 590 | 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 }; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 591 | static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 }; |
| 592 | static const u16 NCT6106_REG_TEMP_CONFIG[] = { |
| 593 | 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc }; |
| 594 | |
| 595 | static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 }; |
| 596 | static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 }; |
| 597 | static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0, 0 }; |
| 598 | static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4, 0, 0 }; |
| 599 | |
| 600 | static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 }; |
| 601 | static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 }; |
| 602 | static const u16 NCT6106_REG_PWM[] = { 0x119, 0x129, 0x139 }; |
| 603 | static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c }; |
| 604 | static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 }; |
| 605 | static const u16 NCT6106_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130 }; |
| 606 | static const u16 NCT6106_REG_TEMP_SOURCE[] = { |
| 607 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 }; |
| 608 | |
| 609 | static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a }; |
| 610 | static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = { |
| 611 | 0x11b, 0x12b, 0x13b }; |
| 612 | |
| 613 | static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c }; |
| 614 | #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10 |
| 615 | static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d }; |
| 616 | |
| 617 | static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 }; |
| 618 | static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 }; |
| 619 | static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 }; |
| 620 | static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 }; |
| 621 | static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 }; |
| 622 | static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 }; |
| 623 | |
| 624 | static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 }; |
| 625 | |
| 626 | static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 }; |
| 627 | static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 }; |
| 628 | static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a }; |
| 629 | static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x17c }; |
| 630 | static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c }; |
| 631 | static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d }; |
| 632 | |
| 633 | static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 }; |
| 634 | static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 }; |
| 635 | |
| 636 | static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = { |
| 637 | 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d }; |
| 638 | |
| 639 | static const s8 NCT6106_ALARM_BITS[] = { |
| 640 | 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */ |
| 641 | 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */ |
| 642 | -1, /* unused */ |
| 643 | 32, 33, 34, -1, -1, /* fan1..fan5 */ |
| 644 | -1, -1, -1, /* unused */ |
| 645 | 16, 17, 18, 19, 20, 21, /* temp1..temp6 */ |
| 646 | 48, -1 /* intrusion0, intrusion1 */ |
| 647 | }; |
| 648 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 649 | static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = { |
| 650 | 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 }; |
| 651 | |
| 652 | static const s8 NCT6106_BEEP_BITS[] = { |
| 653 | 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */ |
| 654 | 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */ |
| 655 | 32, /* global beep enable */ |
| 656 | 24, 25, 26, 27, 28, /* fan1..fan5 */ |
| 657 | -1, -1, -1, /* unused */ |
| 658 | 16, 17, 18, 19, 20, 21, /* temp1..temp6 */ |
| 659 | 34, -1 /* intrusion0, intrusion1 */ |
| 660 | }; |
| 661 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 662 | static const u16 NCT6106_REG_TEMP_ALTERNATE[ARRAY_SIZE(nct6776_temp_label) - 1] |
| 663 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x51, 0x52, 0x54 }; |
| 664 | |
| 665 | static const u16 NCT6106_REG_TEMP_CRIT[ARRAY_SIZE(nct6776_temp_label) - 1] |
| 666 | = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x204, 0x205 }; |
| 667 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 668 | static enum pwm_enable reg_to_pwm_enable(int pwm, int mode) |
| 669 | { |
| 670 | if (mode == 0 && pwm == 255) |
| 671 | return off; |
| 672 | return mode + 1; |
| 673 | } |
| 674 | |
| 675 | static int pwm_enable_to_reg(enum pwm_enable mode) |
| 676 | { |
| 677 | if (mode == off) |
| 678 | return 0; |
| 679 | return mode - 1; |
| 680 | } |
| 681 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 682 | /* |
| 683 | * Conversions |
| 684 | */ |
| 685 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 686 | /* 1 is DC mode, output in ms */ |
| 687 | static unsigned int step_time_from_reg(u8 reg, u8 mode) |
| 688 | { |
| 689 | return mode ? 400 * reg : 100 * reg; |
| 690 | } |
| 691 | |
| 692 | static u8 step_time_to_reg(unsigned int msec, u8 mode) |
| 693 | { |
| 694 | return clamp_val((mode ? (msec + 200) / 400 : |
| 695 | (msec + 50) / 100), 1, 255); |
| 696 | } |
| 697 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 698 | static unsigned int fan_from_reg8(u16 reg, unsigned int divreg) |
| 699 | { |
| 700 | if (reg == 0 || reg == 255) |
| 701 | return 0; |
| 702 | return 1350000U / (reg << divreg); |
| 703 | } |
| 704 | |
| 705 | static unsigned int fan_from_reg13(u16 reg, unsigned int divreg) |
| 706 | { |
| 707 | if ((reg & 0xff1f) == 0xff1f) |
| 708 | return 0; |
| 709 | |
| 710 | reg = (reg & 0x1f) | ((reg & 0xff00) >> 3); |
| 711 | |
| 712 | if (reg == 0) |
| 713 | return 0; |
| 714 | |
| 715 | return 1350000U / reg; |
| 716 | } |
| 717 | |
| 718 | static unsigned int fan_from_reg16(u16 reg, unsigned int divreg) |
| 719 | { |
| 720 | if (reg == 0 || reg == 0xffff) |
| 721 | return 0; |
| 722 | |
| 723 | /* |
| 724 | * Even though the registers are 16 bit wide, the fan divisor |
| 725 | * still applies. |
| 726 | */ |
| 727 | return 1350000U / (reg << divreg); |
| 728 | } |
| 729 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 730 | static u16 fan_to_reg(u32 fan, unsigned int divreg) |
| 731 | { |
| 732 | if (!fan) |
| 733 | return 0; |
| 734 | |
| 735 | return (1350000U / fan) >> divreg; |
| 736 | } |
| 737 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 738 | static inline unsigned int |
| 739 | div_from_reg(u8 reg) |
| 740 | { |
| 741 | return 1 << reg; |
| 742 | } |
| 743 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 744 | /* |
| 745 | * Some of the voltage inputs have internal scaling, the tables below |
| 746 | * contain 8 (the ADC LSB in mV) * scaling factor * 100 |
| 747 | */ |
| 748 | static const u16 scale_in[15] = { |
| 749 | 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800, |
| 750 | 800, 800 |
| 751 | }; |
| 752 | |
| 753 | static inline long in_from_reg(u8 reg, u8 nr) |
| 754 | { |
| 755 | return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100); |
| 756 | } |
| 757 | |
| 758 | static inline u8 in_to_reg(u32 val, u8 nr) |
| 759 | { |
| 760 | return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255); |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Data structures and manipulation thereof |
| 765 | */ |
| 766 | |
| 767 | struct nct6775_data { |
| 768 | int addr; /* IO base of hw monitor block */ |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 769 | int sioreg; /* SIO register address */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 770 | enum kinds kind; |
| 771 | const char *name; |
| 772 | |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 773 | const struct attribute_group *groups[6]; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 774 | |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 775 | u16 reg_temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, |
| 776 | * 3=temp_crit, 4=temp_lcrit |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 777 | */ |
| 778 | u8 temp_src[NUM_TEMP]; |
| 779 | u16 reg_temp_config[NUM_TEMP]; |
| 780 | const char * const *temp_label; |
| 781 | int temp_label_num; |
| 782 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 783 | u16 REG_CONFIG; |
| 784 | u16 REG_VBAT; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 785 | u16 REG_DIODE; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 786 | u8 DIODE_MASK; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 787 | |
| 788 | const s8 *ALARM_BITS; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 789 | const s8 *BEEP_BITS; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 790 | |
| 791 | const u16 *REG_VIN; |
| 792 | const u16 *REG_IN_MINMAX[2]; |
| 793 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 794 | const u16 *REG_TARGET; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 795 | const u16 *REG_FAN; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 796 | const u16 *REG_FAN_MODE; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 797 | const u16 *REG_FAN_MIN; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 798 | const u16 *REG_FAN_PULSES; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 799 | const u16 *FAN_PULSE_SHIFT; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 800 | const u16 *REG_FAN_TIME[3]; |
| 801 | |
| 802 | const u16 *REG_TOLERANCE_H; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 803 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 804 | const u8 *REG_PWM_MODE; |
| 805 | const u8 *PWM_MODE_MASK; |
| 806 | |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 807 | const u16 *REG_PWM[7]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, |
| 808 | * [3]=pwm_max, [4]=pwm_step, |
| 809 | * [5]=weight_duty_step, [6]=weight_duty_base |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 810 | */ |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 811 | const u16 *REG_PWM_READ; |
| 812 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 813 | const u16 *REG_CRITICAL_PWM_ENABLE; |
| 814 | u8 CRITICAL_PWM_ENABLE_MASK; |
| 815 | const u16 *REG_CRITICAL_PWM; |
| 816 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 817 | const u16 *REG_AUTO_TEMP; |
| 818 | const u16 *REG_AUTO_PWM; |
| 819 | |
| 820 | const u16 *REG_CRITICAL_TEMP; |
| 821 | const u16 *REG_CRITICAL_TEMP_TOLERANCE; |
| 822 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 823 | const u16 *REG_TEMP_SOURCE; /* temp register sources */ |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 824 | const u16 *REG_TEMP_SEL; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 825 | const u16 *REG_WEIGHT_TEMP_SEL; |
| 826 | const u16 *REG_WEIGHT_TEMP[3]; /* 0=base, 1=tolerance, 2=step */ |
| 827 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 828 | const u16 *REG_TEMP_OFFSET; |
| 829 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 830 | const u16 *REG_ALARM; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 831 | const u16 *REG_BEEP; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 832 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 833 | unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg); |
| 834 | unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg); |
| 835 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 836 | struct mutex update_lock; |
| 837 | bool valid; /* true if following fields are valid */ |
| 838 | unsigned long last_updated; /* In jiffies */ |
| 839 | |
| 840 | /* Register values */ |
| 841 | u8 bank; /* current register bank */ |
| 842 | u8 in_num; /* number of in inputs we have */ |
| 843 | u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */ |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 844 | unsigned int rpm[NUM_FAN]; |
| 845 | u16 fan_min[NUM_FAN]; |
| 846 | u8 fan_pulses[NUM_FAN]; |
| 847 | u8 fan_div[NUM_FAN]; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 848 | u8 has_pwm; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 849 | u8 has_fan; /* some fan inputs can be disabled */ |
| 850 | u8 has_fan_min; /* some fans don't have min register */ |
| 851 | bool has_fan_div; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 852 | |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 853 | u8 num_temp_alarms; /* 2, 3, or 6 */ |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 854 | u8 num_temp_beeps; /* 2, 3, or 6 */ |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 855 | u8 temp_fixed_num; /* 3 or 6 */ |
| 856 | u8 temp_type[NUM_TEMP_FIXED]; |
| 857 | s8 temp_offset[NUM_TEMP_FIXED]; |
Dan Carpenter | f58876a | 2013-07-18 18:01:11 +0300 | [diff] [blame] | 858 | s16 temp[5][NUM_TEMP]; /* 0=temp, 1=temp_over, 2=temp_hyst, |
| 859 | * 3=temp_crit, 4=temp_lcrit */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 860 | u64 alarms; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 861 | u64 beeps; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 862 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 863 | u8 pwm_num; /* number of pwm */ |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 864 | u8 pwm_mode[NUM_FAN]; /* 1->DC variable voltage, |
| 865 | * 0->PWM variable duty cycle |
| 866 | */ |
| 867 | enum pwm_enable pwm_enable[NUM_FAN]; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 868 | /* 0->off |
| 869 | * 1->manual |
| 870 | * 2->thermal cruise mode (also called SmartFan I) |
| 871 | * 3->fan speed cruise mode |
| 872 | * 4->SmartFan III |
| 873 | * 5->enhanced variable thermal cruise (SmartFan IV) |
| 874 | */ |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 875 | u8 pwm[7][NUM_FAN]; /* [0]=pwm, [1]=pwm_start, [2]=pwm_floor, |
| 876 | * [3]=pwm_max, [4]=pwm_step, |
| 877 | * [5]=weight_duty_step, [6]=weight_duty_base |
| 878 | */ |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 879 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 880 | u8 target_temp[NUM_FAN]; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 881 | u8 target_temp_mask; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 882 | u32 target_speed[NUM_FAN]; |
| 883 | u32 target_speed_tolerance[NUM_FAN]; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 884 | u8 speed_tolerance_limit; |
| 885 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 886 | u8 temp_tolerance[2][NUM_FAN]; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 887 | u8 tolerance_mask; |
| 888 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 889 | u8 fan_time[3][NUM_FAN]; /* 0 = stop_time, 1 = step_up, 2 = step_down */ |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 890 | |
| 891 | /* Automatic fan speed control registers */ |
| 892 | int auto_pwm_num; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 893 | u8 auto_pwm[NUM_FAN][7]; |
| 894 | u8 auto_temp[NUM_FAN][7]; |
| 895 | u8 pwm_temp_sel[NUM_FAN]; |
| 896 | u8 pwm_weight_temp_sel[NUM_FAN]; |
| 897 | u8 weight_temp[3][NUM_FAN]; /* 0->temp_step, 1->temp_step_tol, |
| 898 | * 2->temp_base |
| 899 | */ |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 900 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 901 | u8 vid; |
| 902 | u8 vrm; |
| 903 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 904 | bool have_vid; |
| 905 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 906 | u16 have_temp; |
| 907 | u16 have_temp_fixed; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 908 | u16 have_in; |
Guenter Roeck | 48e9318 | 2015-02-07 08:48:49 -0800 | [diff] [blame] | 909 | |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 910 | /* Remember extra register values over suspend/resume */ |
| 911 | u8 vbat; |
| 912 | u8 fandiv1; |
| 913 | u8 fandiv2; |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 914 | u8 sio_reg_enable; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 915 | }; |
| 916 | |
| 917 | struct nct6775_sio_data { |
| 918 | int sioreg; |
| 919 | enum kinds kind; |
| 920 | }; |
| 921 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 922 | struct sensor_device_template { |
| 923 | struct device_attribute dev_attr; |
| 924 | union { |
| 925 | struct { |
| 926 | u8 nr; |
| 927 | u8 index; |
| 928 | } s; |
| 929 | int index; |
| 930 | } u; |
| 931 | bool s2; /* true if both index and nr are used */ |
| 932 | }; |
| 933 | |
| 934 | struct sensor_device_attr_u { |
| 935 | union { |
| 936 | struct sensor_device_attribute a1; |
| 937 | struct sensor_device_attribute_2 a2; |
| 938 | } u; |
| 939 | char name[32]; |
| 940 | }; |
| 941 | |
| 942 | #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \ |
| 943 | .attr = {.name = _template, .mode = _mode }, \ |
| 944 | .show = _show, \ |
| 945 | .store = _store, \ |
| 946 | } |
| 947 | |
| 948 | #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \ |
| 949 | { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \ |
| 950 | .u.index = _index, \ |
| 951 | .s2 = false } |
| 952 | |
| 953 | #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \ |
| 954 | _nr, _index) \ |
| 955 | { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \ |
| 956 | .u.s.index = _index, \ |
| 957 | .u.s.nr = _nr, \ |
| 958 | .s2 = true } |
| 959 | |
| 960 | #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \ |
| 961 | static struct sensor_device_template sensor_dev_template_##_name \ |
| 962 | = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \ |
| 963 | _index) |
| 964 | |
| 965 | #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \ |
| 966 | _nr, _index) \ |
| 967 | static struct sensor_device_template sensor_dev_template_##_name \ |
| 968 | = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \ |
| 969 | _nr, _index) |
| 970 | |
| 971 | struct sensor_template_group { |
| 972 | struct sensor_device_template **templates; |
| 973 | umode_t (*is_visible)(struct kobject *, struct attribute *, int); |
| 974 | int base; |
| 975 | }; |
| 976 | |
| 977 | static struct attribute_group * |
| 978 | nct6775_create_attr_group(struct device *dev, struct sensor_template_group *tg, |
| 979 | int repeat) |
| 980 | { |
| 981 | struct attribute_group *group; |
| 982 | struct sensor_device_attr_u *su; |
| 983 | struct sensor_device_attribute *a; |
| 984 | struct sensor_device_attribute_2 *a2; |
| 985 | struct attribute **attrs; |
| 986 | struct sensor_device_template **t; |
Dan Carpenter | 1e687e8 | 2013-10-19 11:55:15 +0300 | [diff] [blame] | 987 | int i, count; |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 988 | |
| 989 | if (repeat <= 0) |
| 990 | return ERR_PTR(-EINVAL); |
| 991 | |
| 992 | t = tg->templates; |
| 993 | for (count = 0; *t; t++, count++) |
| 994 | ; |
| 995 | |
| 996 | if (count == 0) |
| 997 | return ERR_PTR(-EINVAL); |
| 998 | |
| 999 | group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL); |
| 1000 | if (group == NULL) |
| 1001 | return ERR_PTR(-ENOMEM); |
| 1002 | |
| 1003 | attrs = devm_kzalloc(dev, sizeof(*attrs) * (repeat * count + 1), |
| 1004 | GFP_KERNEL); |
| 1005 | if (attrs == NULL) |
| 1006 | return ERR_PTR(-ENOMEM); |
| 1007 | |
| 1008 | su = devm_kzalloc(dev, sizeof(*su) * repeat * count, |
| 1009 | GFP_KERNEL); |
| 1010 | if (su == NULL) |
| 1011 | return ERR_PTR(-ENOMEM); |
| 1012 | |
| 1013 | group->attrs = attrs; |
| 1014 | group->is_visible = tg->is_visible; |
| 1015 | |
| 1016 | for (i = 0; i < repeat; i++) { |
| 1017 | t = tg->templates; |
Dan Carpenter | 1e687e8 | 2013-10-19 11:55:15 +0300 | [diff] [blame] | 1018 | while (*t != NULL) { |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1019 | snprintf(su->name, sizeof(su->name), |
| 1020 | (*t)->dev_attr.attr.name, tg->base + i); |
| 1021 | if ((*t)->s2) { |
| 1022 | a2 = &su->u.a2; |
Guenter Roeck | 1b63bf6 | 2015-05-28 09:08:09 -0700 | [diff] [blame] | 1023 | sysfs_attr_init(&a2->dev_attr.attr); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1024 | a2->dev_attr.attr.name = su->name; |
| 1025 | a2->nr = (*t)->u.s.nr + i; |
| 1026 | a2->index = (*t)->u.s.index; |
| 1027 | a2->dev_attr.attr.mode = |
| 1028 | (*t)->dev_attr.attr.mode; |
| 1029 | a2->dev_attr.show = (*t)->dev_attr.show; |
| 1030 | a2->dev_attr.store = (*t)->dev_attr.store; |
| 1031 | *attrs = &a2->dev_attr.attr; |
| 1032 | } else { |
| 1033 | a = &su->u.a1; |
Guenter Roeck | 1b63bf6 | 2015-05-28 09:08:09 -0700 | [diff] [blame] | 1034 | sysfs_attr_init(&a->dev_attr.attr); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1035 | a->dev_attr.attr.name = su->name; |
| 1036 | a->index = (*t)->u.index + i; |
| 1037 | a->dev_attr.attr.mode = |
| 1038 | (*t)->dev_attr.attr.mode; |
| 1039 | a->dev_attr.show = (*t)->dev_attr.show; |
| 1040 | a->dev_attr.store = (*t)->dev_attr.store; |
| 1041 | *attrs = &a->dev_attr.attr; |
| 1042 | } |
| 1043 | attrs++; |
| 1044 | su++; |
| 1045 | t++; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1049 | return group; |
| 1050 | } |
| 1051 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1052 | static bool is_word_sized(struct nct6775_data *data, u16 reg) |
| 1053 | { |
| 1054 | switch (data->kind) { |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1055 | case nct6106: |
| 1056 | return reg == 0x20 || reg == 0x22 || reg == 0x24 || |
| 1057 | reg == 0xe0 || reg == 0xe2 || reg == 0xe4 || |
| 1058 | reg == 0x111 || reg == 0x121 || reg == 0x131; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1059 | case nct6775: |
| 1060 | return (((reg & 0xff00) == 0x100 || |
| 1061 | (reg & 0xff00) == 0x200) && |
| 1062 | ((reg & 0x00ff) == 0x50 || |
| 1063 | (reg & 0x00ff) == 0x53 || |
| 1064 | (reg & 0x00ff) == 0x55)) || |
| 1065 | (reg & 0xfff0) == 0x630 || |
| 1066 | reg == 0x640 || reg == 0x642 || |
| 1067 | reg == 0x662 || |
| 1068 | ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) || |
| 1069 | reg == 0x73 || reg == 0x75 || reg == 0x77; |
| 1070 | case nct6776: |
| 1071 | return (((reg & 0xff00) == 0x100 || |
| 1072 | (reg & 0xff00) == 0x200) && |
| 1073 | ((reg & 0x00ff) == 0x50 || |
| 1074 | (reg & 0x00ff) == 0x53 || |
| 1075 | (reg & 0x00ff) == 0x55)) || |
| 1076 | (reg & 0xfff0) == 0x630 || |
| 1077 | reg == 0x402 || |
| 1078 | reg == 0x640 || reg == 0x642 || |
| 1079 | ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) || |
| 1080 | reg == 0x73 || reg == 0x75 || reg == 0x77; |
| 1081 | case nct6779: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 1082 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 1083 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 1084 | case nct6793: |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1085 | return reg == 0x150 || reg == 0x153 || reg == 0x155 || |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 1086 | ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x0b) || |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1087 | reg == 0x402 || |
| 1088 | reg == 0x63a || reg == 0x63c || reg == 0x63e || |
| 1089 | reg == 0x640 || reg == 0x642 || |
| 1090 | reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 || |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 1091 | reg == 0x7b || reg == 0x7d; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1092 | } |
| 1093 | return false; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * On older chips, only registers 0x50-0x5f are banked. |
| 1098 | * On more recent chips, all registers are banked. |
| 1099 | * Assume that is the case and set the bank number for each access. |
| 1100 | * Cache the bank number so it only needs to be set if it changes. |
| 1101 | */ |
| 1102 | static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg) |
| 1103 | { |
| 1104 | u8 bank = reg >> 8; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1105 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1106 | if (data->bank != bank) { |
| 1107 | outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET); |
| 1108 | outb_p(bank, data->addr + DATA_REG_OFFSET); |
| 1109 | data->bank = bank; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | static u16 nct6775_read_value(struct nct6775_data *data, u16 reg) |
| 1114 | { |
| 1115 | int res, word_sized = is_word_sized(data, reg); |
| 1116 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1117 | nct6775_set_bank(data, reg); |
| 1118 | outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET); |
| 1119 | res = inb_p(data->addr + DATA_REG_OFFSET); |
| 1120 | if (word_sized) { |
| 1121 | outb_p((reg & 0xff) + 1, |
| 1122 | data->addr + ADDR_REG_OFFSET); |
| 1123 | res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET); |
| 1124 | } |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1125 | return res; |
| 1126 | } |
| 1127 | |
| 1128 | static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value) |
| 1129 | { |
| 1130 | int word_sized = is_word_sized(data, reg); |
| 1131 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1132 | nct6775_set_bank(data, reg); |
| 1133 | outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET); |
| 1134 | if (word_sized) { |
| 1135 | outb_p(value >> 8, data->addr + DATA_REG_OFFSET); |
| 1136 | outb_p((reg & 0xff) + 1, |
| 1137 | data->addr + ADDR_REG_OFFSET); |
| 1138 | } |
| 1139 | outb_p(value & 0xff, data->addr + DATA_REG_OFFSET); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1143 | /* We left-align 8-bit temperature values to make the code simpler */ |
| 1144 | static u16 nct6775_read_temp(struct nct6775_data *data, u16 reg) |
| 1145 | { |
| 1146 | u16 res; |
| 1147 | |
| 1148 | res = nct6775_read_value(data, reg); |
| 1149 | if (!is_word_sized(data, reg)) |
| 1150 | res <<= 8; |
| 1151 | |
| 1152 | return res; |
| 1153 | } |
| 1154 | |
| 1155 | static int nct6775_write_temp(struct nct6775_data *data, u16 reg, u16 value) |
| 1156 | { |
| 1157 | if (!is_word_sized(data, reg)) |
| 1158 | value >>= 8; |
| 1159 | return nct6775_write_value(data, reg, value); |
| 1160 | } |
| 1161 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1162 | /* This function assumes that the caller holds data->update_lock */ |
| 1163 | static void nct6775_write_fan_div(struct nct6775_data *data, int nr) |
| 1164 | { |
| 1165 | u8 reg; |
| 1166 | |
| 1167 | switch (nr) { |
| 1168 | case 0: |
| 1169 | reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x70) |
| 1170 | | (data->fan_div[0] & 0x7); |
| 1171 | nct6775_write_value(data, NCT6775_REG_FANDIV1, reg); |
| 1172 | break; |
| 1173 | case 1: |
| 1174 | reg = (nct6775_read_value(data, NCT6775_REG_FANDIV1) & 0x7) |
| 1175 | | ((data->fan_div[1] << 4) & 0x70); |
| 1176 | nct6775_write_value(data, NCT6775_REG_FANDIV1, reg); |
| 1177 | break; |
| 1178 | case 2: |
| 1179 | reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x70) |
| 1180 | | (data->fan_div[2] & 0x7); |
| 1181 | nct6775_write_value(data, NCT6775_REG_FANDIV2, reg); |
| 1182 | break; |
| 1183 | case 3: |
| 1184 | reg = (nct6775_read_value(data, NCT6775_REG_FANDIV2) & 0x7) |
| 1185 | | ((data->fan_div[3] << 4) & 0x70); |
| 1186 | nct6775_write_value(data, NCT6775_REG_FANDIV2, reg); |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | static void nct6775_write_fan_div_common(struct nct6775_data *data, int nr) |
| 1192 | { |
| 1193 | if (data->kind == nct6775) |
| 1194 | nct6775_write_fan_div(data, nr); |
| 1195 | } |
| 1196 | |
| 1197 | static void nct6775_update_fan_div(struct nct6775_data *data) |
| 1198 | { |
| 1199 | u8 i; |
| 1200 | |
| 1201 | i = nct6775_read_value(data, NCT6775_REG_FANDIV1); |
| 1202 | data->fan_div[0] = i & 0x7; |
| 1203 | data->fan_div[1] = (i & 0x70) >> 4; |
| 1204 | i = nct6775_read_value(data, NCT6775_REG_FANDIV2); |
| 1205 | data->fan_div[2] = i & 0x7; |
Guenter Roeck | 6445e66 | 2013-04-21 09:13:28 -0700 | [diff] [blame] | 1206 | if (data->has_fan & (1 << 3)) |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1207 | data->fan_div[3] = (i & 0x70) >> 4; |
| 1208 | } |
| 1209 | |
| 1210 | static void nct6775_update_fan_div_common(struct nct6775_data *data) |
| 1211 | { |
| 1212 | if (data->kind == nct6775) |
| 1213 | nct6775_update_fan_div(data); |
| 1214 | } |
| 1215 | |
| 1216 | static void nct6775_init_fan_div(struct nct6775_data *data) |
| 1217 | { |
| 1218 | int i; |
| 1219 | |
| 1220 | nct6775_update_fan_div_common(data); |
| 1221 | /* |
| 1222 | * For all fans, start with highest divider value if the divider |
| 1223 | * register is not initialized. This ensures that we get a |
| 1224 | * reading from the fan count register, even if it is not optimal. |
| 1225 | * We'll compute a better divider later on. |
| 1226 | */ |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1227 | for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) { |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1228 | if (!(data->has_fan & (1 << i))) |
| 1229 | continue; |
| 1230 | if (data->fan_div[i] == 0) { |
| 1231 | data->fan_div[i] = 7; |
| 1232 | nct6775_write_fan_div_common(data, i); |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static void nct6775_init_fan_common(struct device *dev, |
| 1238 | struct nct6775_data *data) |
| 1239 | { |
| 1240 | int i; |
| 1241 | u8 reg; |
| 1242 | |
| 1243 | if (data->has_fan_div) |
| 1244 | nct6775_init_fan_div(data); |
| 1245 | |
| 1246 | /* |
| 1247 | * If fan_min is not set (0), set it to 0xff to disable it. This |
| 1248 | * prevents the unnecessary warning when fanX_min is reported as 0. |
| 1249 | */ |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1250 | for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) { |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1251 | if (data->has_fan_min & (1 << i)) { |
| 1252 | reg = nct6775_read_value(data, data->REG_FAN_MIN[i]); |
| 1253 | if (!reg) |
| 1254 | nct6775_write_value(data, data->REG_FAN_MIN[i], |
| 1255 | data->has_fan_div ? 0xff |
| 1256 | : 0xff1f); |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | static void nct6775_select_fan_div(struct device *dev, |
| 1262 | struct nct6775_data *data, int nr, u16 reg) |
| 1263 | { |
| 1264 | u8 fan_div = data->fan_div[nr]; |
| 1265 | u16 fan_min; |
| 1266 | |
| 1267 | if (!data->has_fan_div) |
| 1268 | return; |
| 1269 | |
| 1270 | /* |
| 1271 | * If we failed to measure the fan speed, or the reported value is not |
| 1272 | * in the optimal range, and the clock divider can be modified, |
| 1273 | * let's try that for next time. |
| 1274 | */ |
| 1275 | if (reg == 0x00 && fan_div < 0x07) |
| 1276 | fan_div++; |
| 1277 | else if (reg != 0x00 && reg < 0x30 && fan_div > 0) |
| 1278 | fan_div--; |
| 1279 | |
| 1280 | if (fan_div != data->fan_div[nr]) { |
| 1281 | dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n", |
| 1282 | nr + 1, div_from_reg(data->fan_div[nr]), |
| 1283 | div_from_reg(fan_div)); |
| 1284 | |
| 1285 | /* Preserve min limit if possible */ |
| 1286 | if (data->has_fan_min & (1 << nr)) { |
| 1287 | fan_min = data->fan_min[nr]; |
| 1288 | if (fan_div > data->fan_div[nr]) { |
| 1289 | if (fan_min != 255 && fan_min > 1) |
| 1290 | fan_min >>= 1; |
| 1291 | } else { |
| 1292 | if (fan_min != 255) { |
| 1293 | fan_min <<= 1; |
| 1294 | if (fan_min > 254) |
| 1295 | fan_min = 254; |
| 1296 | } |
| 1297 | } |
| 1298 | if (fan_min != data->fan_min[nr]) { |
| 1299 | data->fan_min[nr] = fan_min; |
| 1300 | nct6775_write_value(data, data->REG_FAN_MIN[nr], |
| 1301 | fan_min); |
| 1302 | } |
| 1303 | } |
| 1304 | data->fan_div[nr] = fan_div; |
| 1305 | nct6775_write_fan_div_common(data, nr); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 1309 | static void nct6775_update_pwm(struct device *dev) |
| 1310 | { |
| 1311 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1312 | int i, j; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1313 | int fanmodecfg, reg; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 1314 | bool duty_is_dc; |
| 1315 | |
| 1316 | for (i = 0; i < data->pwm_num; i++) { |
| 1317 | if (!(data->has_pwm & (1 << i))) |
| 1318 | continue; |
| 1319 | |
| 1320 | duty_is_dc = data->REG_PWM_MODE[i] && |
| 1321 | (nct6775_read_value(data, data->REG_PWM_MODE[i]) |
| 1322 | & data->PWM_MODE_MASK[i]); |
| 1323 | data->pwm_mode[i] = duty_is_dc; |
| 1324 | |
| 1325 | fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]); |
| 1326 | for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) { |
| 1327 | if (data->REG_PWM[j] && data->REG_PWM[j][i]) { |
| 1328 | data->pwm[j][i] |
| 1329 | = nct6775_read_value(data, |
| 1330 | data->REG_PWM[j][i]); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i], |
| 1335 | (fanmodecfg >> 4) & 7); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1336 | |
| 1337 | if (!data->temp_tolerance[0][i] || |
| 1338 | data->pwm_enable[i] != speed_cruise) |
| 1339 | data->temp_tolerance[0][i] = fanmodecfg & 0x0f; |
| 1340 | if (!data->target_speed_tolerance[i] || |
| 1341 | data->pwm_enable[i] == speed_cruise) { |
| 1342 | u8 t = fanmodecfg & 0x0f; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1343 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1344 | if (data->REG_TOLERANCE_H) { |
| 1345 | t |= (nct6775_read_value(data, |
| 1346 | data->REG_TOLERANCE_H[i]) & 0x70) >> 1; |
| 1347 | } |
| 1348 | data->target_speed_tolerance[i] = t; |
| 1349 | } |
| 1350 | |
| 1351 | data->temp_tolerance[1][i] = |
| 1352 | nct6775_read_value(data, |
| 1353 | data->REG_CRITICAL_TEMP_TOLERANCE[i]); |
| 1354 | |
| 1355 | reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]); |
| 1356 | data->pwm_temp_sel[i] = reg & 0x1f; |
| 1357 | /* If fan can stop, report floor as 0 */ |
| 1358 | if (reg & 0x80) |
| 1359 | data->pwm[2][i] = 0; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 1360 | |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 1361 | if (!data->REG_WEIGHT_TEMP_SEL[i]) |
| 1362 | continue; |
| 1363 | |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 1364 | reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]); |
| 1365 | data->pwm_weight_temp_sel[i] = reg & 0x1f; |
| 1366 | /* If weight is disabled, report weight source as 0 */ |
| 1367 | if (j == 1 && !(reg & 0x80)) |
| 1368 | data->pwm_weight_temp_sel[i] = 0; |
| 1369 | |
| 1370 | /* Weight temp data */ |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1371 | for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) { |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 1372 | data->weight_temp[j][i] |
| 1373 | = nct6775_read_value(data, |
| 1374 | data->REG_WEIGHT_TEMP[j][i]); |
| 1375 | } |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | static void nct6775_update_pwm_limits(struct device *dev) |
| 1380 | { |
| 1381 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1382 | int i, j; |
| 1383 | u8 reg; |
| 1384 | u16 reg_t; |
| 1385 | |
| 1386 | for (i = 0; i < data->pwm_num; i++) { |
| 1387 | if (!(data->has_pwm & (1 << i))) |
| 1388 | continue; |
| 1389 | |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1390 | for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) { |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1391 | data->fan_time[j][i] = |
| 1392 | nct6775_read_value(data, data->REG_FAN_TIME[j][i]); |
| 1393 | } |
| 1394 | |
| 1395 | reg_t = nct6775_read_value(data, data->REG_TARGET[i]); |
| 1396 | /* Update only in matching mode or if never updated */ |
| 1397 | if (!data->target_temp[i] || |
| 1398 | data->pwm_enable[i] == thermal_cruise) |
| 1399 | data->target_temp[i] = reg_t & data->target_temp_mask; |
| 1400 | if (!data->target_speed[i] || |
| 1401 | data->pwm_enable[i] == speed_cruise) { |
| 1402 | if (data->REG_TOLERANCE_H) { |
| 1403 | reg_t |= (nct6775_read_value(data, |
| 1404 | data->REG_TOLERANCE_H[i]) & 0x0f) << 8; |
| 1405 | } |
| 1406 | data->target_speed[i] = reg_t; |
| 1407 | } |
| 1408 | |
| 1409 | for (j = 0; j < data->auto_pwm_num; j++) { |
| 1410 | data->auto_pwm[i][j] = |
| 1411 | nct6775_read_value(data, |
| 1412 | NCT6775_AUTO_PWM(data, i, j)); |
| 1413 | data->auto_temp[i][j] = |
| 1414 | nct6775_read_value(data, |
| 1415 | NCT6775_AUTO_TEMP(data, i, j)); |
| 1416 | } |
| 1417 | |
| 1418 | /* critical auto_pwm temperature data */ |
| 1419 | data->auto_temp[i][data->auto_pwm_num] = |
| 1420 | nct6775_read_value(data, data->REG_CRITICAL_TEMP[i]); |
| 1421 | |
| 1422 | switch (data->kind) { |
| 1423 | case nct6775: |
| 1424 | reg = nct6775_read_value(data, |
| 1425 | NCT6775_REG_CRITICAL_ENAB[i]); |
| 1426 | data->auto_pwm[i][data->auto_pwm_num] = |
| 1427 | (reg & 0x02) ? 0xff : 0x00; |
| 1428 | break; |
| 1429 | case nct6776: |
| 1430 | data->auto_pwm[i][data->auto_pwm_num] = 0xff; |
| 1431 | break; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1432 | case nct6106: |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1433 | case nct6779: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 1434 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 1435 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 1436 | case nct6793: |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1437 | reg = nct6775_read_value(data, |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1438 | data->REG_CRITICAL_PWM_ENABLE[i]); |
| 1439 | if (reg & data->CRITICAL_PWM_ENABLE_MASK) |
| 1440 | reg = nct6775_read_value(data, |
| 1441 | data->REG_CRITICAL_PWM[i]); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1442 | else |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1443 | reg = 0xff; |
| 1444 | data->auto_pwm[i][data->auto_pwm_num] = reg; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1445 | break; |
| 1446 | } |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1450 | static struct nct6775_data *nct6775_update_device(struct device *dev) |
| 1451 | { |
| 1452 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1453 | int i, j; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1454 | |
| 1455 | mutex_lock(&data->update_lock); |
| 1456 | |
Guenter Roeck | 6445e66 | 2013-04-21 09:13:28 -0700 | [diff] [blame] | 1457 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1458 | || !data->valid) { |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1459 | /* Fan clock dividers */ |
| 1460 | nct6775_update_fan_div_common(data); |
| 1461 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1462 | /* Measured voltages and limits */ |
| 1463 | for (i = 0; i < data->in_num; i++) { |
| 1464 | if (!(data->have_in & (1 << i))) |
| 1465 | continue; |
| 1466 | |
| 1467 | data->in[i][0] = nct6775_read_value(data, |
| 1468 | data->REG_VIN[i]); |
| 1469 | data->in[i][1] = nct6775_read_value(data, |
| 1470 | data->REG_IN_MINMAX[0][i]); |
| 1471 | data->in[i][2] = nct6775_read_value(data, |
| 1472 | data->REG_IN_MINMAX[1][i]); |
| 1473 | } |
| 1474 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1475 | /* Measured fan speeds and limits */ |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1476 | for (i = 0; i < ARRAY_SIZE(data->rpm); i++) { |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1477 | u16 reg; |
| 1478 | |
| 1479 | if (!(data->has_fan & (1 << i))) |
| 1480 | continue; |
| 1481 | |
| 1482 | reg = nct6775_read_value(data, data->REG_FAN[i]); |
| 1483 | data->rpm[i] = data->fan_from_reg(reg, |
| 1484 | data->fan_div[i]); |
| 1485 | |
| 1486 | if (data->has_fan_min & (1 << i)) |
| 1487 | data->fan_min[i] = nct6775_read_value(data, |
| 1488 | data->REG_FAN_MIN[i]); |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 1489 | data->fan_pulses[i] = |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1490 | (nct6775_read_value(data, data->REG_FAN_PULSES[i]) |
| 1491 | >> data->FAN_PULSE_SHIFT[i]) & 0x03; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1492 | |
| 1493 | nct6775_select_fan_div(dev, data, i, reg); |
| 1494 | } |
| 1495 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 1496 | nct6775_update_pwm(dev); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 1497 | nct6775_update_pwm_limits(dev); |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 1498 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1499 | /* Measured temperatures and limits */ |
| 1500 | for (i = 0; i < NUM_TEMP; i++) { |
| 1501 | if (!(data->have_temp & (1 << i))) |
| 1502 | continue; |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 1503 | for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) { |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1504 | if (data->reg_temp[j][i]) |
| 1505 | data->temp[j][i] |
| 1506 | = nct6775_read_temp(data, |
| 1507 | data->reg_temp[j][i]); |
| 1508 | } |
Guenter Roeck | 45a5b3a | 2013-09-11 10:35:47 -0700 | [diff] [blame] | 1509 | if (i >= NUM_TEMP_FIXED || |
| 1510 | !(data->have_temp_fixed & (1 << i))) |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1511 | continue; |
| 1512 | data->temp_offset[i] |
| 1513 | = nct6775_read_value(data, data->REG_TEMP_OFFSET[i]); |
| 1514 | } |
| 1515 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1516 | data->alarms = 0; |
| 1517 | for (i = 0; i < NUM_REG_ALARM; i++) { |
| 1518 | u8 alarm; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1519 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1520 | if (!data->REG_ALARM[i]) |
| 1521 | continue; |
| 1522 | alarm = nct6775_read_value(data, data->REG_ALARM[i]); |
| 1523 | data->alarms |= ((u64)alarm) << (i << 3); |
| 1524 | } |
| 1525 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1526 | data->beeps = 0; |
| 1527 | for (i = 0; i < NUM_REG_BEEP; i++) { |
| 1528 | u8 beep; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1529 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1530 | if (!data->REG_BEEP[i]) |
| 1531 | continue; |
| 1532 | beep = nct6775_read_value(data, data->REG_BEEP[i]); |
| 1533 | data->beeps |= ((u64)beep) << (i << 3); |
| 1534 | } |
| 1535 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1536 | data->last_updated = jiffies; |
| 1537 | data->valid = true; |
| 1538 | } |
| 1539 | |
| 1540 | mutex_unlock(&data->update_lock); |
| 1541 | return data; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * Sysfs callback functions |
| 1546 | */ |
| 1547 | static ssize_t |
| 1548 | show_in_reg(struct device *dev, struct device_attribute *attr, char *buf) |
| 1549 | { |
| 1550 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1551 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1552 | int index = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1553 | int nr = sattr->nr; |
| 1554 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1555 | return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr)); |
| 1556 | } |
| 1557 | |
| 1558 | static ssize_t |
| 1559 | store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf, |
| 1560 | size_t count) |
| 1561 | { |
| 1562 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1563 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1564 | int index = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1565 | int nr = sattr->nr; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1566 | unsigned long val; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1567 | int err; |
| 1568 | |
| 1569 | err = kstrtoul(buf, 10, &val); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1570 | if (err < 0) |
| 1571 | return err; |
| 1572 | mutex_lock(&data->update_lock); |
| 1573 | data->in[nr][index] = in_to_reg(val, nr); |
Guenter Roeck | 6445e66 | 2013-04-21 09:13:28 -0700 | [diff] [blame] | 1574 | nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr], |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1575 | data->in[nr][index]); |
| 1576 | mutex_unlock(&data->update_lock); |
| 1577 | return count; |
| 1578 | } |
| 1579 | |
| 1580 | static ssize_t |
| 1581 | show_alarm(struct device *dev, struct device_attribute *attr, char *buf) |
| 1582 | { |
| 1583 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1584 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1585 | int nr = data->ALARM_BITS[sattr->index]; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1586 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1587 | return sprintf(buf, "%u\n", |
| 1588 | (unsigned int)((data->alarms >> nr) & 0x01)); |
| 1589 | } |
| 1590 | |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 1591 | static int find_temp_source(struct nct6775_data *data, int index, int count) |
| 1592 | { |
| 1593 | int source = data->temp_src[index]; |
| 1594 | int nr; |
| 1595 | |
| 1596 | for (nr = 0; nr < count; nr++) { |
| 1597 | int src; |
| 1598 | |
| 1599 | src = nct6775_read_value(data, |
| 1600 | data->REG_TEMP_SOURCE[nr]) & 0x1f; |
| 1601 | if (src == source) |
| 1602 | return nr; |
| 1603 | } |
Guenter Roeck | e8ab508 | 2013-09-11 10:32:18 -0700 | [diff] [blame] | 1604 | return -ENODEV; |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | static ssize_t |
| 1608 | show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf) |
| 1609 | { |
| 1610 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1611 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1612 | unsigned int alarm = 0; |
| 1613 | int nr; |
| 1614 | |
| 1615 | /* |
| 1616 | * For temperatures, there is no fixed mapping from registers to alarm |
| 1617 | * bits. Alarm bits are determined by the temperature source mapping. |
| 1618 | */ |
| 1619 | nr = find_temp_source(data, sattr->index, data->num_temp_alarms); |
| 1620 | if (nr >= 0) { |
| 1621 | int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE]; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1622 | |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 1623 | alarm = (data->alarms >> bit) & 0x01; |
| 1624 | } |
| 1625 | return sprintf(buf, "%u\n", alarm); |
| 1626 | } |
| 1627 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1628 | static ssize_t |
| 1629 | show_beep(struct device *dev, struct device_attribute *attr, char *buf) |
| 1630 | { |
| 1631 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1632 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1633 | int nr = data->BEEP_BITS[sattr->index]; |
| 1634 | |
| 1635 | return sprintf(buf, "%u\n", |
| 1636 | (unsigned int)((data->beeps >> nr) & 0x01)); |
| 1637 | } |
| 1638 | |
| 1639 | static ssize_t |
| 1640 | store_beep(struct device *dev, struct device_attribute *attr, const char *buf, |
| 1641 | size_t count) |
| 1642 | { |
| 1643 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 1644 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1645 | int nr = data->BEEP_BITS[sattr->index]; |
| 1646 | int regindex = nr >> 3; |
| 1647 | unsigned long val; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1648 | int err; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1649 | |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1650 | err = kstrtoul(buf, 10, &val); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1651 | if (err < 0) |
| 1652 | return err; |
| 1653 | if (val > 1) |
| 1654 | return -EINVAL; |
| 1655 | |
| 1656 | mutex_lock(&data->update_lock); |
| 1657 | if (val) |
| 1658 | data->beeps |= (1ULL << nr); |
| 1659 | else |
| 1660 | data->beeps &= ~(1ULL << nr); |
| 1661 | nct6775_write_value(data, data->REG_BEEP[regindex], |
| 1662 | (data->beeps >> (regindex << 3)) & 0xff); |
| 1663 | mutex_unlock(&data->update_lock); |
| 1664 | return count; |
| 1665 | } |
| 1666 | |
| 1667 | static ssize_t |
| 1668 | show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf) |
| 1669 | { |
| 1670 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1671 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1672 | unsigned int beep = 0; |
| 1673 | int nr; |
| 1674 | |
| 1675 | /* |
| 1676 | * For temperatures, there is no fixed mapping from registers to beep |
| 1677 | * enable bits. Beep enable bits are determined by the temperature |
| 1678 | * source mapping. |
| 1679 | */ |
| 1680 | nr = find_temp_source(data, sattr->index, data->num_temp_beeps); |
| 1681 | if (nr >= 0) { |
| 1682 | int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE]; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1683 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1684 | beep = (data->beeps >> bit) & 0x01; |
| 1685 | } |
| 1686 | return sprintf(buf, "%u\n", beep); |
| 1687 | } |
| 1688 | |
| 1689 | static ssize_t |
| 1690 | store_temp_beep(struct device *dev, struct device_attribute *attr, |
| 1691 | const char *buf, size_t count) |
| 1692 | { |
| 1693 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 1694 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1695 | int nr, bit, regindex; |
| 1696 | unsigned long val; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1697 | int err; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1698 | |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1699 | err = kstrtoul(buf, 10, &val); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1700 | if (err < 0) |
| 1701 | return err; |
| 1702 | if (val > 1) |
| 1703 | return -EINVAL; |
| 1704 | |
| 1705 | nr = find_temp_source(data, sattr->index, data->num_temp_beeps); |
| 1706 | if (nr < 0) |
Guenter Roeck | e8ab508 | 2013-09-11 10:32:18 -0700 | [diff] [blame] | 1707 | return nr; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1708 | |
| 1709 | bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE]; |
| 1710 | regindex = bit >> 3; |
| 1711 | |
| 1712 | mutex_lock(&data->update_lock); |
| 1713 | if (val) |
| 1714 | data->beeps |= (1ULL << bit); |
| 1715 | else |
| 1716 | data->beeps &= ~(1ULL << bit); |
| 1717 | nct6775_write_value(data, data->REG_BEEP[regindex], |
| 1718 | (data->beeps >> (regindex << 3)) & 0xff); |
| 1719 | mutex_unlock(&data->update_lock); |
| 1720 | |
| 1721 | return count; |
| 1722 | } |
| 1723 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1724 | static umode_t nct6775_in_is_visible(struct kobject *kobj, |
| 1725 | struct attribute *attr, int index) |
| 1726 | { |
| 1727 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1728 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1729 | int in = index / 5; /* voltage index */ |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1730 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1731 | if (!(data->have_in & (1 << in))) |
| 1732 | return 0; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1733 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1734 | return attr->mode; |
| 1735 | } |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1736 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1737 | SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0); |
| 1738 | SENSOR_TEMPLATE(in_alarm, "in%d_alarm", S_IRUGO, show_alarm, NULL, 0); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1739 | SENSOR_TEMPLATE(in_beep, "in%d_beep", S_IWUSR | S_IRUGO, show_beep, store_beep, |
| 1740 | 0); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1741 | SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IWUSR | S_IRUGO, show_in_reg, |
| 1742 | store_in_reg, 0, 1); |
| 1743 | SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IWUSR | S_IRUGO, show_in_reg, |
| 1744 | store_in_reg, 0, 2); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1745 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1746 | /* |
| 1747 | * nct6775_in_is_visible uses the index into the following array |
| 1748 | * to determine if attributes should be created or not. |
| 1749 | * Any change in order or content must be matched. |
| 1750 | */ |
| 1751 | static struct sensor_device_template *nct6775_attributes_in_template[] = { |
| 1752 | &sensor_dev_template_in_input, |
| 1753 | &sensor_dev_template_in_alarm, |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1754 | &sensor_dev_template_in_beep, |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1755 | &sensor_dev_template_in_min, |
| 1756 | &sensor_dev_template_in_max, |
| 1757 | NULL |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1758 | }; |
| 1759 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1760 | static struct sensor_template_group nct6775_in_template_group = { |
| 1761 | .templates = nct6775_attributes_in_template, |
| 1762 | .is_visible = nct6775_in_is_visible, |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 1763 | }; |
| 1764 | |
| 1765 | static ssize_t |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1766 | show_fan(struct device *dev, struct device_attribute *attr, char *buf) |
| 1767 | { |
| 1768 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1769 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1770 | int nr = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1771 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1772 | return sprintf(buf, "%d\n", data->rpm[nr]); |
| 1773 | } |
| 1774 | |
| 1775 | static ssize_t |
| 1776 | show_fan_min(struct device *dev, struct device_attribute *attr, char *buf) |
| 1777 | { |
| 1778 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1779 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1780 | int nr = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1781 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1782 | return sprintf(buf, "%d\n", |
| 1783 | data->fan_from_reg_min(data->fan_min[nr], |
| 1784 | data->fan_div[nr])); |
| 1785 | } |
| 1786 | |
| 1787 | static ssize_t |
| 1788 | show_fan_div(struct device *dev, struct device_attribute *attr, char *buf) |
| 1789 | { |
| 1790 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1791 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1792 | int nr = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1793 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1794 | return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr])); |
| 1795 | } |
| 1796 | |
| 1797 | static ssize_t |
| 1798 | store_fan_min(struct device *dev, struct device_attribute *attr, |
| 1799 | const char *buf, size_t count) |
| 1800 | { |
| 1801 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1802 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1803 | int nr = sattr->index; |
| 1804 | unsigned long val; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1805 | unsigned int reg; |
| 1806 | u8 new_div; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1807 | int err; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1808 | |
| 1809 | err = kstrtoul(buf, 10, &val); |
| 1810 | if (err < 0) |
| 1811 | return err; |
| 1812 | |
| 1813 | mutex_lock(&data->update_lock); |
| 1814 | if (!data->has_fan_div) { |
| 1815 | /* NCT6776F or NCT6779D; we know this is a 13 bit register */ |
| 1816 | if (!val) { |
| 1817 | val = 0xff1f; |
| 1818 | } else { |
| 1819 | if (val > 1350000U) |
| 1820 | val = 135000U; |
| 1821 | val = 1350000U / val; |
| 1822 | val = (val & 0x1f) | ((val << 3) & 0xff00); |
| 1823 | } |
| 1824 | data->fan_min[nr] = val; |
| 1825 | goto write_min; /* Leave fan divider alone */ |
| 1826 | } |
| 1827 | if (!val) { |
| 1828 | /* No min limit, alarm disabled */ |
| 1829 | data->fan_min[nr] = 255; |
| 1830 | new_div = data->fan_div[nr]; /* No change */ |
| 1831 | dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1); |
| 1832 | goto write_div; |
| 1833 | } |
| 1834 | reg = 1350000U / val; |
| 1835 | if (reg >= 128 * 255) { |
| 1836 | /* |
| 1837 | * Speed below this value cannot possibly be represented, |
| 1838 | * even with the highest divider (128) |
| 1839 | */ |
| 1840 | data->fan_min[nr] = 254; |
| 1841 | new_div = 7; /* 128 == (1 << 7) */ |
| 1842 | dev_warn(dev, |
| 1843 | "fan%u low limit %lu below minimum %u, set to minimum\n", |
| 1844 | nr + 1, val, data->fan_from_reg_min(254, 7)); |
| 1845 | } else if (!reg) { |
| 1846 | /* |
| 1847 | * Speed above this value cannot possibly be represented, |
| 1848 | * even with the lowest divider (1) |
| 1849 | */ |
| 1850 | data->fan_min[nr] = 1; |
| 1851 | new_div = 0; /* 1 == (1 << 0) */ |
| 1852 | dev_warn(dev, |
| 1853 | "fan%u low limit %lu above maximum %u, set to maximum\n", |
| 1854 | nr + 1, val, data->fan_from_reg_min(1, 0)); |
| 1855 | } else { |
| 1856 | /* |
| 1857 | * Automatically pick the best divider, i.e. the one such |
| 1858 | * that the min limit will correspond to a register value |
| 1859 | * in the 96..192 range |
| 1860 | */ |
| 1861 | new_div = 0; |
| 1862 | while (reg > 192 && new_div < 7) { |
| 1863 | reg >>= 1; |
| 1864 | new_div++; |
| 1865 | } |
| 1866 | data->fan_min[nr] = reg; |
| 1867 | } |
| 1868 | |
| 1869 | write_div: |
| 1870 | /* |
| 1871 | * Write both the fan clock divider (if it changed) and the new |
| 1872 | * fan min (unconditionally) |
| 1873 | */ |
| 1874 | if (new_div != data->fan_div[nr]) { |
| 1875 | dev_dbg(dev, "fan%u clock divider changed from %u to %u\n", |
| 1876 | nr + 1, div_from_reg(data->fan_div[nr]), |
| 1877 | div_from_reg(new_div)); |
| 1878 | data->fan_div[nr] = new_div; |
| 1879 | nct6775_write_fan_div_common(data, nr); |
| 1880 | /* Give the chip time to sample a new speed value */ |
| 1881 | data->last_updated = jiffies; |
| 1882 | } |
| 1883 | |
| 1884 | write_min: |
| 1885 | nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]); |
| 1886 | mutex_unlock(&data->update_lock); |
| 1887 | |
| 1888 | return count; |
| 1889 | } |
| 1890 | |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 1891 | static ssize_t |
| 1892 | show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf) |
| 1893 | { |
| 1894 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1895 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1896 | int p = data->fan_pulses[sattr->index]; |
| 1897 | |
| 1898 | return sprintf(buf, "%d\n", p ? : 4); |
| 1899 | } |
| 1900 | |
| 1901 | static ssize_t |
| 1902 | store_fan_pulses(struct device *dev, struct device_attribute *attr, |
| 1903 | const char *buf, size_t count) |
| 1904 | { |
| 1905 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 1906 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1907 | int nr = sattr->index; |
| 1908 | unsigned long val; |
| 1909 | int err; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1910 | u8 reg; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 1911 | |
| 1912 | err = kstrtoul(buf, 10, &val); |
| 1913 | if (err < 0) |
| 1914 | return err; |
| 1915 | |
| 1916 | if (val > 4) |
| 1917 | return -EINVAL; |
| 1918 | |
| 1919 | mutex_lock(&data->update_lock); |
| 1920 | data->fan_pulses[nr] = val & 3; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 1921 | reg = nct6775_read_value(data, data->REG_FAN_PULSES[nr]); |
| 1922 | reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]); |
| 1923 | reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr]; |
| 1924 | nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg); |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 1925 | mutex_unlock(&data->update_lock); |
| 1926 | |
| 1927 | return count; |
| 1928 | } |
| 1929 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1930 | static umode_t nct6775_fan_is_visible(struct kobject *kobj, |
| 1931 | struct attribute *attr, int index) |
| 1932 | { |
| 1933 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1934 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1935 | int fan = index / 6; /* fan index */ |
| 1936 | int nr = index % 6; /* attribute index */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1937 | |
| 1938 | if (!(data->has_fan & (1 << fan))) |
| 1939 | return 0; |
| 1940 | |
| 1941 | if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1) |
| 1942 | return 0; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1943 | if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1) |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1944 | return 0; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1945 | if (nr == 4 && !(data->has_fan_min & (1 << fan))) |
| 1946 | return 0; |
| 1947 | if (nr == 5 && data->kind != nct6775) |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1948 | return 0; |
| 1949 | |
| 1950 | return attr->mode; |
| 1951 | } |
| 1952 | |
| 1953 | SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0); |
| 1954 | SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", S_IRUGO, show_alarm, NULL, |
| 1955 | FAN_ALARM_BASE); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1956 | SENSOR_TEMPLATE(fan_beep, "fan%d_beep", S_IWUSR | S_IRUGO, show_beep, |
| 1957 | store_beep, FAN_ALARM_BASE); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1958 | SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IWUSR | S_IRUGO, show_fan_pulses, |
| 1959 | store_fan_pulses, 0); |
| 1960 | SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IWUSR | S_IRUGO, show_fan_min, |
| 1961 | store_fan_min, 0); |
| 1962 | SENSOR_TEMPLATE(fan_div, "fan%d_div", S_IRUGO, show_fan_div, NULL, 0); |
| 1963 | |
| 1964 | /* |
| 1965 | * nct6775_fan_is_visible uses the index into the following array |
| 1966 | * to determine if attributes should be created or not. |
| 1967 | * Any change in order or content must be matched. |
| 1968 | */ |
| 1969 | static struct sensor_device_template *nct6775_attributes_fan_template[] = { |
| 1970 | &sensor_dev_template_fan_input, |
| 1971 | &sensor_dev_template_fan_alarm, /* 1 */ |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1972 | &sensor_dev_template_fan_beep, /* 2 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1973 | &sensor_dev_template_fan_pulses, |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 1974 | &sensor_dev_template_fan_min, /* 4 */ |
| 1975 | &sensor_dev_template_fan_div, /* 5 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1976 | NULL |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1977 | }; |
| 1978 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 1979 | static struct sensor_template_group nct6775_fan_template_group = { |
| 1980 | .templates = nct6775_attributes_fan_template, |
| 1981 | .is_visible = nct6775_fan_is_visible, |
| 1982 | .base = 1, |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 1983 | }; |
| 1984 | |
| 1985 | static ssize_t |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1986 | show_temp_label(struct device *dev, struct device_attribute *attr, char *buf) |
| 1987 | { |
| 1988 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1989 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 1990 | int nr = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 1991 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 1992 | return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]); |
| 1993 | } |
| 1994 | |
| 1995 | static ssize_t |
| 1996 | show_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 1997 | { |
| 1998 | struct nct6775_data *data = nct6775_update_device(dev); |
| 1999 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2000 | int nr = sattr->nr; |
| 2001 | int index = sattr->index; |
| 2002 | |
| 2003 | return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr])); |
| 2004 | } |
| 2005 | |
| 2006 | static ssize_t |
| 2007 | store_temp(struct device *dev, struct device_attribute *attr, const char *buf, |
| 2008 | size_t count) |
| 2009 | { |
| 2010 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2011 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2012 | int nr = sattr->nr; |
| 2013 | int index = sattr->index; |
| 2014 | int err; |
| 2015 | long val; |
| 2016 | |
| 2017 | err = kstrtol(buf, 10, &val); |
| 2018 | if (err < 0) |
| 2019 | return err; |
| 2020 | |
| 2021 | mutex_lock(&data->update_lock); |
| 2022 | data->temp[index][nr] = LM75_TEMP_TO_REG(val); |
| 2023 | nct6775_write_temp(data, data->reg_temp[index][nr], |
| 2024 | data->temp[index][nr]); |
| 2025 | mutex_unlock(&data->update_lock); |
| 2026 | return count; |
| 2027 | } |
| 2028 | |
| 2029 | static ssize_t |
| 2030 | show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf) |
| 2031 | { |
| 2032 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2033 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2034 | |
| 2035 | return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000); |
| 2036 | } |
| 2037 | |
| 2038 | static ssize_t |
| 2039 | store_temp_offset(struct device *dev, struct device_attribute *attr, |
| 2040 | const char *buf, size_t count) |
| 2041 | { |
| 2042 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2043 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2044 | int nr = sattr->index; |
| 2045 | long val; |
| 2046 | int err; |
| 2047 | |
| 2048 | err = kstrtol(buf, 10, &val); |
| 2049 | if (err < 0) |
| 2050 | return err; |
| 2051 | |
| 2052 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); |
| 2053 | |
| 2054 | mutex_lock(&data->update_lock); |
| 2055 | data->temp_offset[nr] = val; |
| 2056 | nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val); |
| 2057 | mutex_unlock(&data->update_lock); |
| 2058 | |
| 2059 | return count; |
| 2060 | } |
| 2061 | |
| 2062 | static ssize_t |
| 2063 | show_temp_type(struct device *dev, struct device_attribute *attr, char *buf) |
| 2064 | { |
| 2065 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2066 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2067 | int nr = sattr->index; |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 2068 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2069 | return sprintf(buf, "%d\n", (int)data->temp_type[nr]); |
| 2070 | } |
| 2071 | |
| 2072 | static ssize_t |
| 2073 | store_temp_type(struct device *dev, struct device_attribute *attr, |
| 2074 | const char *buf, size_t count) |
| 2075 | { |
| 2076 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2077 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2078 | int nr = sattr->index; |
| 2079 | unsigned long val; |
| 2080 | int err; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2081 | u8 vbat, diode, vbit, dbit; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2082 | |
| 2083 | err = kstrtoul(buf, 10, &val); |
| 2084 | if (err < 0) |
| 2085 | return err; |
| 2086 | |
| 2087 | if (val != 1 && val != 3 && val != 4) |
| 2088 | return -EINVAL; |
| 2089 | |
| 2090 | mutex_lock(&data->update_lock); |
| 2091 | |
| 2092 | data->temp_type[nr] = val; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2093 | vbit = 0x02 << nr; |
| 2094 | dbit = data->DIODE_MASK << nr; |
| 2095 | vbat = nct6775_read_value(data, data->REG_VBAT) & ~vbit; |
| 2096 | diode = nct6775_read_value(data, data->REG_DIODE) & ~dbit; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2097 | switch (val) { |
| 2098 | case 1: /* CPU diode (diode, current mode) */ |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2099 | vbat |= vbit; |
| 2100 | diode |= dbit; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2101 | break; |
| 2102 | case 3: /* diode, voltage mode */ |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2103 | vbat |= dbit; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2104 | break; |
| 2105 | case 4: /* thermistor */ |
| 2106 | break; |
| 2107 | } |
| 2108 | nct6775_write_value(data, data->REG_VBAT, vbat); |
| 2109 | nct6775_write_value(data, data->REG_DIODE, diode); |
| 2110 | |
| 2111 | mutex_unlock(&data->update_lock); |
| 2112 | return count; |
| 2113 | } |
| 2114 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2115 | static umode_t nct6775_temp_is_visible(struct kobject *kobj, |
| 2116 | struct attribute *attr, int index) |
| 2117 | { |
| 2118 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2119 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2120 | int temp = index / 10; /* temp index */ |
| 2121 | int nr = index % 10; /* attribute index */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2122 | |
| 2123 | if (!(data->have_temp & (1 << temp))) |
| 2124 | return 0; |
| 2125 | |
| 2126 | if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0) |
| 2127 | return 0; /* alarm */ |
| 2128 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2129 | if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0) |
| 2130 | return 0; /* beep */ |
| 2131 | |
| 2132 | if (nr == 4 && !data->reg_temp[1][temp]) /* max */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2133 | return 0; |
| 2134 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2135 | if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2136 | return 0; |
| 2137 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2138 | if (nr == 6 && !data->reg_temp[3][temp]) /* crit */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2139 | return 0; |
| 2140 | |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2141 | if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */ |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 2142 | return 0; |
| 2143 | |
| 2144 | /* offset and type only apply to fixed sensors */ |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2145 | if (nr > 7 && !(data->have_temp_fixed & (1 << temp))) |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2146 | return 0; |
| 2147 | |
| 2148 | return attr->mode; |
| 2149 | } |
| 2150 | |
| 2151 | SENSOR_TEMPLATE_2(temp_input, "temp%d_input", S_IRUGO, show_temp, NULL, 0, 0); |
| 2152 | SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0); |
| 2153 | SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO | S_IWUSR, show_temp, |
| 2154 | store_temp, 0, 1); |
| 2155 | SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", S_IRUGO | S_IWUSR, |
| 2156 | show_temp, store_temp, 0, 2); |
| 2157 | SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO | S_IWUSR, show_temp, |
| 2158 | store_temp, 0, 3); |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 2159 | SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", S_IRUGO | S_IWUSR, show_temp, |
| 2160 | store_temp, 0, 4); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2161 | SENSOR_TEMPLATE(temp_offset, "temp%d_offset", S_IRUGO | S_IWUSR, |
| 2162 | show_temp_offset, store_temp_offset, 0); |
| 2163 | SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO | S_IWUSR, show_temp_type, |
| 2164 | store_temp_type, 0); |
| 2165 | SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", S_IRUGO, show_temp_alarm, NULL, 0); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2166 | SENSOR_TEMPLATE(temp_beep, "temp%d_beep", S_IRUGO | S_IWUSR, show_temp_beep, |
| 2167 | store_temp_beep, 0); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2168 | |
| 2169 | /* |
| 2170 | * nct6775_temp_is_visible uses the index into the following array |
| 2171 | * to determine if attributes should be created or not. |
| 2172 | * Any change in order or content must be matched. |
| 2173 | */ |
| 2174 | static struct sensor_device_template *nct6775_attributes_temp_template[] = { |
| 2175 | &sensor_dev_template_temp_input, |
| 2176 | &sensor_dev_template_temp_label, |
| 2177 | &sensor_dev_template_temp_alarm, /* 2 */ |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 2178 | &sensor_dev_template_temp_beep, /* 3 */ |
| 2179 | &sensor_dev_template_temp_max, /* 4 */ |
| 2180 | &sensor_dev_template_temp_max_hyst, /* 5 */ |
| 2181 | &sensor_dev_template_temp_crit, /* 6 */ |
| 2182 | &sensor_dev_template_temp_lcrit, /* 7 */ |
| 2183 | &sensor_dev_template_temp_offset, /* 8 */ |
| 2184 | &sensor_dev_template_temp_type, /* 9 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2185 | NULL |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2186 | }; |
| 2187 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2188 | static struct sensor_template_group nct6775_temp_template_group = { |
| 2189 | .templates = nct6775_attributes_temp_template, |
| 2190 | .is_visible = nct6775_temp_is_visible, |
| 2191 | .base = 1, |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2192 | }; |
| 2193 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 2194 | static ssize_t |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2195 | show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf) |
| 2196 | { |
| 2197 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2198 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2199 | |
| 2200 | return sprintf(buf, "%d\n", !data->pwm_mode[sattr->index]); |
| 2201 | } |
| 2202 | |
| 2203 | static ssize_t |
| 2204 | store_pwm_mode(struct device *dev, struct device_attribute *attr, |
| 2205 | const char *buf, size_t count) |
| 2206 | { |
| 2207 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2208 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2209 | int nr = sattr->index; |
| 2210 | unsigned long val; |
| 2211 | int err; |
| 2212 | u8 reg; |
| 2213 | |
| 2214 | err = kstrtoul(buf, 10, &val); |
| 2215 | if (err < 0) |
| 2216 | return err; |
| 2217 | |
| 2218 | if (val > 1) |
| 2219 | return -EINVAL; |
| 2220 | |
| 2221 | /* Setting DC mode is not supported for all chips/channels */ |
| 2222 | if (data->REG_PWM_MODE[nr] == 0) { |
| 2223 | if (val) |
| 2224 | return -EINVAL; |
| 2225 | return count; |
| 2226 | } |
| 2227 | |
| 2228 | mutex_lock(&data->update_lock); |
| 2229 | data->pwm_mode[nr] = val; |
| 2230 | reg = nct6775_read_value(data, data->REG_PWM_MODE[nr]); |
| 2231 | reg &= ~data->PWM_MODE_MASK[nr]; |
| 2232 | if (val) |
| 2233 | reg |= data->PWM_MODE_MASK[nr]; |
| 2234 | nct6775_write_value(data, data->REG_PWM_MODE[nr], reg); |
| 2235 | mutex_unlock(&data->update_lock); |
| 2236 | return count; |
| 2237 | } |
| 2238 | |
| 2239 | static ssize_t |
| 2240 | show_pwm(struct device *dev, struct device_attribute *attr, char *buf) |
| 2241 | { |
| 2242 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2243 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2244 | int nr = sattr->nr; |
| 2245 | int index = sattr->index; |
| 2246 | int pwm; |
| 2247 | |
| 2248 | /* |
| 2249 | * For automatic fan control modes, show current pwm readings. |
| 2250 | * Otherwise, show the configured value. |
| 2251 | */ |
| 2252 | if (index == 0 && data->pwm_enable[nr] > manual) |
| 2253 | pwm = nct6775_read_value(data, data->REG_PWM_READ[nr]); |
| 2254 | else |
| 2255 | pwm = data->pwm[index][nr]; |
| 2256 | |
| 2257 | return sprintf(buf, "%d\n", pwm); |
| 2258 | } |
| 2259 | |
| 2260 | static ssize_t |
| 2261 | store_pwm(struct device *dev, struct device_attribute *attr, const char *buf, |
| 2262 | size_t count) |
| 2263 | { |
| 2264 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2265 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2266 | int nr = sattr->nr; |
| 2267 | int index = sattr->index; |
| 2268 | unsigned long val; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2269 | int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 }; |
| 2270 | int maxval[7] |
| 2271 | = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 }; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2272 | int err; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2273 | u8 reg; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2274 | |
| 2275 | err = kstrtoul(buf, 10, &val); |
| 2276 | if (err < 0) |
| 2277 | return err; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2278 | val = clamp_val(val, minval[index], maxval[index]); |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2279 | |
| 2280 | mutex_lock(&data->update_lock); |
| 2281 | data->pwm[index][nr] = val; |
| 2282 | nct6775_write_value(data, data->REG_PWM[index][nr], val); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2283 | if (index == 2) { /* floor: disable if val == 0 */ |
| 2284 | reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]); |
| 2285 | reg &= 0x7f; |
| 2286 | if (val) |
| 2287 | reg |= 0x80; |
| 2288 | nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg); |
| 2289 | } |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2290 | mutex_unlock(&data->update_lock); |
| 2291 | return count; |
| 2292 | } |
| 2293 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2294 | /* Returns 0 if OK, -EINVAL otherwise */ |
| 2295 | static int check_trip_points(struct nct6775_data *data, int nr) |
| 2296 | { |
| 2297 | int i; |
| 2298 | |
| 2299 | for (i = 0; i < data->auto_pwm_num - 1; i++) { |
| 2300 | if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1]) |
| 2301 | return -EINVAL; |
| 2302 | } |
| 2303 | for (i = 0; i < data->auto_pwm_num - 1; i++) { |
| 2304 | if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1]) |
| 2305 | return -EINVAL; |
| 2306 | } |
| 2307 | /* validate critical temperature and pwm if enabled (pwm > 0) */ |
| 2308 | if (data->auto_pwm[nr][data->auto_pwm_num]) { |
| 2309 | if (data->auto_temp[nr][data->auto_pwm_num - 1] > |
| 2310 | data->auto_temp[nr][data->auto_pwm_num] || |
| 2311 | data->auto_pwm[nr][data->auto_pwm_num - 1] > |
| 2312 | data->auto_pwm[nr][data->auto_pwm_num]) |
| 2313 | return -EINVAL; |
| 2314 | } |
| 2315 | return 0; |
| 2316 | } |
| 2317 | |
| 2318 | static void pwm_update_registers(struct nct6775_data *data, int nr) |
| 2319 | { |
| 2320 | u8 reg; |
| 2321 | |
| 2322 | switch (data->pwm_enable[nr]) { |
| 2323 | case off: |
| 2324 | case manual: |
| 2325 | break; |
| 2326 | case speed_cruise: |
| 2327 | reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]); |
| 2328 | reg = (reg & ~data->tolerance_mask) | |
| 2329 | (data->target_speed_tolerance[nr] & data->tolerance_mask); |
| 2330 | nct6775_write_value(data, data->REG_FAN_MODE[nr], reg); |
| 2331 | nct6775_write_value(data, data->REG_TARGET[nr], |
| 2332 | data->target_speed[nr] & 0xff); |
| 2333 | if (data->REG_TOLERANCE_H) { |
| 2334 | reg = (data->target_speed[nr] >> 8) & 0x0f; |
| 2335 | reg |= (data->target_speed_tolerance[nr] & 0x38) << 1; |
| 2336 | nct6775_write_value(data, |
| 2337 | data->REG_TOLERANCE_H[nr], |
| 2338 | reg); |
| 2339 | } |
| 2340 | break; |
| 2341 | case thermal_cruise: |
| 2342 | nct6775_write_value(data, data->REG_TARGET[nr], |
| 2343 | data->target_temp[nr]); |
| 2344 | /* intentional */ |
| 2345 | default: |
| 2346 | reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]); |
| 2347 | reg = (reg & ~data->tolerance_mask) | |
| 2348 | data->temp_tolerance[0][nr]; |
| 2349 | nct6775_write_value(data, data->REG_FAN_MODE[nr], reg); |
| 2350 | break; |
| 2351 | } |
| 2352 | } |
| 2353 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2354 | static ssize_t |
| 2355 | show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf) |
| 2356 | { |
| 2357 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2358 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2359 | |
| 2360 | return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]); |
| 2361 | } |
| 2362 | |
| 2363 | static ssize_t |
| 2364 | store_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 2365 | const char *buf, size_t count) |
| 2366 | { |
| 2367 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2368 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2369 | int nr = sattr->index; |
| 2370 | unsigned long val; |
| 2371 | int err; |
| 2372 | u16 reg; |
| 2373 | |
| 2374 | err = kstrtoul(buf, 10, &val); |
| 2375 | if (err < 0) |
| 2376 | return err; |
| 2377 | |
| 2378 | if (val > sf4) |
| 2379 | return -EINVAL; |
| 2380 | |
| 2381 | if (val == sf3 && data->kind != nct6775) |
| 2382 | return -EINVAL; |
| 2383 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2384 | if (val == sf4 && check_trip_points(data, nr)) { |
| 2385 | dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n"); |
| 2386 | dev_err(dev, "Adjust trip points and try again\n"); |
| 2387 | return -EINVAL; |
| 2388 | } |
| 2389 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2390 | mutex_lock(&data->update_lock); |
| 2391 | data->pwm_enable[nr] = val; |
| 2392 | if (val == off) { |
| 2393 | /* |
| 2394 | * turn off pwm control: select manual mode, set pwm to maximum |
| 2395 | */ |
| 2396 | data->pwm[0][nr] = 255; |
| 2397 | nct6775_write_value(data, data->REG_PWM[0][nr], 255); |
| 2398 | } |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2399 | pwm_update_registers(data, nr); |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2400 | reg = nct6775_read_value(data, data->REG_FAN_MODE[nr]); |
| 2401 | reg &= 0x0f; |
| 2402 | reg |= pwm_enable_to_reg(val) << 4; |
| 2403 | nct6775_write_value(data, data->REG_FAN_MODE[nr], reg); |
| 2404 | mutex_unlock(&data->update_lock); |
| 2405 | return count; |
| 2406 | } |
| 2407 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2408 | static ssize_t |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2409 | show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src) |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2410 | { |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2411 | int i, sel = 0; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2412 | |
| 2413 | for (i = 0; i < NUM_TEMP; i++) { |
| 2414 | if (!(data->have_temp & (1 << i))) |
| 2415 | continue; |
| 2416 | if (src == data->temp_src[i]) { |
| 2417 | sel = i + 1; |
| 2418 | break; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | return sprintf(buf, "%d\n", sel); |
| 2423 | } |
| 2424 | |
| 2425 | static ssize_t |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2426 | show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf) |
| 2427 | { |
| 2428 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2429 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2430 | int index = sattr->index; |
| 2431 | |
| 2432 | return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]); |
| 2433 | } |
| 2434 | |
| 2435 | static ssize_t |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2436 | store_pwm_temp_sel(struct device *dev, struct device_attribute *attr, |
| 2437 | const char *buf, size_t count) |
| 2438 | { |
| 2439 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2440 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2441 | int nr = sattr->index; |
| 2442 | unsigned long val; |
| 2443 | int err, reg, src; |
| 2444 | |
| 2445 | err = kstrtoul(buf, 10, &val); |
| 2446 | if (err < 0) |
| 2447 | return err; |
| 2448 | if (val == 0 || val > NUM_TEMP) |
| 2449 | return -EINVAL; |
| 2450 | if (!(data->have_temp & (1 << (val - 1))) || !data->temp_src[val - 1]) |
| 2451 | return -EINVAL; |
| 2452 | |
| 2453 | mutex_lock(&data->update_lock); |
| 2454 | src = data->temp_src[val - 1]; |
| 2455 | data->pwm_temp_sel[nr] = src; |
| 2456 | reg = nct6775_read_value(data, data->REG_TEMP_SEL[nr]); |
| 2457 | reg &= 0xe0; |
| 2458 | reg |= src; |
| 2459 | nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg); |
| 2460 | mutex_unlock(&data->update_lock); |
| 2461 | |
| 2462 | return count; |
| 2463 | } |
| 2464 | |
| 2465 | static ssize_t |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2466 | show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr, |
| 2467 | char *buf) |
| 2468 | { |
| 2469 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2470 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2471 | int index = sattr->index; |
| 2472 | |
| 2473 | return show_pwm_temp_sel_common(data, buf, |
| 2474 | data->pwm_weight_temp_sel[index]); |
| 2475 | } |
| 2476 | |
| 2477 | static ssize_t |
| 2478 | store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr, |
| 2479 | const char *buf, size_t count) |
| 2480 | { |
| 2481 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2482 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2483 | int nr = sattr->index; |
| 2484 | unsigned long val; |
| 2485 | int err, reg, src; |
| 2486 | |
| 2487 | err = kstrtoul(buf, 10, &val); |
| 2488 | if (err < 0) |
| 2489 | return err; |
| 2490 | if (val > NUM_TEMP) |
| 2491 | return -EINVAL; |
| 2492 | if (val && (!(data->have_temp & (1 << (val - 1))) || |
| 2493 | !data->temp_src[val - 1])) |
| 2494 | return -EINVAL; |
| 2495 | |
| 2496 | mutex_lock(&data->update_lock); |
| 2497 | if (val) { |
| 2498 | src = data->temp_src[val - 1]; |
| 2499 | data->pwm_weight_temp_sel[nr] = src; |
| 2500 | reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]); |
| 2501 | reg &= 0xe0; |
| 2502 | reg |= (src | 0x80); |
| 2503 | nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg); |
| 2504 | } else { |
| 2505 | data->pwm_weight_temp_sel[nr] = 0; |
| 2506 | reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr]); |
| 2507 | reg &= 0x7f; |
| 2508 | nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg); |
| 2509 | } |
| 2510 | mutex_unlock(&data->update_lock); |
| 2511 | |
| 2512 | return count; |
| 2513 | } |
| 2514 | |
| 2515 | static ssize_t |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2516 | show_target_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 2517 | { |
| 2518 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2519 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2520 | |
| 2521 | return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000); |
| 2522 | } |
| 2523 | |
| 2524 | static ssize_t |
| 2525 | store_target_temp(struct device *dev, struct device_attribute *attr, |
| 2526 | const char *buf, size_t count) |
| 2527 | { |
| 2528 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2529 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2530 | int nr = sattr->index; |
| 2531 | unsigned long val; |
| 2532 | int err; |
| 2533 | |
| 2534 | err = kstrtoul(buf, 10, &val); |
| 2535 | if (err < 0) |
| 2536 | return err; |
| 2537 | |
| 2538 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, |
| 2539 | data->target_temp_mask); |
| 2540 | |
| 2541 | mutex_lock(&data->update_lock); |
| 2542 | data->target_temp[nr] = val; |
| 2543 | pwm_update_registers(data, nr); |
| 2544 | mutex_unlock(&data->update_lock); |
| 2545 | return count; |
| 2546 | } |
| 2547 | |
| 2548 | static ssize_t |
| 2549 | show_target_speed(struct device *dev, struct device_attribute *attr, char *buf) |
| 2550 | { |
| 2551 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2552 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2553 | int nr = sattr->index; |
| 2554 | |
| 2555 | return sprintf(buf, "%d\n", |
| 2556 | fan_from_reg16(data->target_speed[nr], |
| 2557 | data->fan_div[nr])); |
| 2558 | } |
| 2559 | |
| 2560 | static ssize_t |
| 2561 | store_target_speed(struct device *dev, struct device_attribute *attr, |
| 2562 | const char *buf, size_t count) |
| 2563 | { |
| 2564 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2565 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2566 | int nr = sattr->index; |
| 2567 | unsigned long val; |
| 2568 | int err; |
| 2569 | u16 speed; |
| 2570 | |
| 2571 | err = kstrtoul(buf, 10, &val); |
| 2572 | if (err < 0) |
| 2573 | return err; |
| 2574 | |
| 2575 | val = clamp_val(val, 0, 1350000U); |
| 2576 | speed = fan_to_reg(val, data->fan_div[nr]); |
| 2577 | |
| 2578 | mutex_lock(&data->update_lock); |
| 2579 | data->target_speed[nr] = speed; |
| 2580 | pwm_update_registers(data, nr); |
| 2581 | mutex_unlock(&data->update_lock); |
| 2582 | return count; |
| 2583 | } |
| 2584 | |
| 2585 | static ssize_t |
| 2586 | show_temp_tolerance(struct device *dev, struct device_attribute *attr, |
| 2587 | char *buf) |
| 2588 | { |
| 2589 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2590 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2591 | int nr = sattr->nr; |
| 2592 | int index = sattr->index; |
| 2593 | |
| 2594 | return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000); |
| 2595 | } |
| 2596 | |
| 2597 | static ssize_t |
| 2598 | store_temp_tolerance(struct device *dev, struct device_attribute *attr, |
| 2599 | const char *buf, size_t count) |
| 2600 | { |
| 2601 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2602 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2603 | int nr = sattr->nr; |
| 2604 | int index = sattr->index; |
| 2605 | unsigned long val; |
| 2606 | int err; |
| 2607 | |
| 2608 | err = kstrtoul(buf, 10, &val); |
| 2609 | if (err < 0) |
| 2610 | return err; |
| 2611 | |
| 2612 | /* Limit tolerance as needed */ |
| 2613 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask); |
| 2614 | |
| 2615 | mutex_lock(&data->update_lock); |
| 2616 | data->temp_tolerance[index][nr] = val; |
| 2617 | if (index) |
| 2618 | pwm_update_registers(data, nr); |
| 2619 | else |
| 2620 | nct6775_write_value(data, |
| 2621 | data->REG_CRITICAL_TEMP_TOLERANCE[nr], |
| 2622 | val); |
| 2623 | mutex_unlock(&data->update_lock); |
| 2624 | return count; |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Fan speed tolerance is a tricky beast, since the associated register is |
| 2629 | * a tick counter, but the value is reported and configured as rpm. |
| 2630 | * Compute resulting low and high rpm values and report the difference. |
| 2631 | */ |
| 2632 | static ssize_t |
| 2633 | show_speed_tolerance(struct device *dev, struct device_attribute *attr, |
| 2634 | char *buf) |
| 2635 | { |
| 2636 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2637 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2638 | int nr = sattr->index; |
| 2639 | int low = data->target_speed[nr] - data->target_speed_tolerance[nr]; |
| 2640 | int high = data->target_speed[nr] + data->target_speed_tolerance[nr]; |
| 2641 | int tolerance; |
| 2642 | |
| 2643 | if (low <= 0) |
| 2644 | low = 1; |
| 2645 | if (high > 0xffff) |
| 2646 | high = 0xffff; |
| 2647 | if (high < low) |
| 2648 | high = low; |
| 2649 | |
| 2650 | tolerance = (fan_from_reg16(low, data->fan_div[nr]) |
| 2651 | - fan_from_reg16(high, data->fan_div[nr])) / 2; |
| 2652 | |
| 2653 | return sprintf(buf, "%d\n", tolerance); |
| 2654 | } |
| 2655 | |
| 2656 | static ssize_t |
| 2657 | store_speed_tolerance(struct device *dev, struct device_attribute *attr, |
| 2658 | const char *buf, size_t count) |
| 2659 | { |
| 2660 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2661 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 2662 | int nr = sattr->index; |
| 2663 | unsigned long val; |
| 2664 | int err; |
| 2665 | int low, high; |
| 2666 | |
| 2667 | err = kstrtoul(buf, 10, &val); |
| 2668 | if (err < 0) |
| 2669 | return err; |
| 2670 | |
| 2671 | high = fan_from_reg16(data->target_speed[nr], |
| 2672 | data->fan_div[nr]) + val; |
| 2673 | low = fan_from_reg16(data->target_speed[nr], |
| 2674 | data->fan_div[nr]) - val; |
| 2675 | if (low <= 0) |
| 2676 | low = 1; |
| 2677 | if (high < low) |
| 2678 | high = low; |
| 2679 | |
| 2680 | val = (fan_to_reg(low, data->fan_div[nr]) - |
| 2681 | fan_to_reg(high, data->fan_div[nr])) / 2; |
| 2682 | |
| 2683 | /* Limit tolerance as needed */ |
| 2684 | val = clamp_val(val, 0, data->speed_tolerance_limit); |
| 2685 | |
| 2686 | mutex_lock(&data->update_lock); |
| 2687 | data->target_speed_tolerance[nr] = val; |
| 2688 | pwm_update_registers(data, nr); |
| 2689 | mutex_unlock(&data->update_lock); |
| 2690 | return count; |
| 2691 | } |
| 2692 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2693 | SENSOR_TEMPLATE_2(pwm, "pwm%d", S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 0); |
| 2694 | SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", S_IWUSR | S_IRUGO, show_pwm_mode, |
| 2695 | store_pwm_mode, 0); |
| 2696 | SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", S_IWUSR | S_IRUGO, show_pwm_enable, |
| 2697 | store_pwm_enable, 0); |
| 2698 | SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", S_IWUSR | S_IRUGO, |
| 2699 | show_pwm_temp_sel, store_pwm_temp_sel, 0); |
| 2700 | SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", S_IWUSR | S_IRUGO, |
| 2701 | show_target_temp, store_target_temp, 0); |
| 2702 | SENSOR_TEMPLATE(fan_target, "fan%d_target", S_IWUSR | S_IRUGO, |
| 2703 | show_target_speed, store_target_speed, 0); |
| 2704 | SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", S_IWUSR | S_IRUGO, |
| 2705 | show_speed_tolerance, store_speed_tolerance, 0); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2706 | |
| 2707 | /* Smart Fan registers */ |
| 2708 | |
| 2709 | static ssize_t |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2710 | show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 2711 | { |
| 2712 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2713 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2714 | int nr = sattr->nr; |
| 2715 | int index = sattr->index; |
| 2716 | |
| 2717 | return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000); |
| 2718 | } |
| 2719 | |
| 2720 | static ssize_t |
| 2721 | store_weight_temp(struct device *dev, struct device_attribute *attr, |
| 2722 | const char *buf, size_t count) |
| 2723 | { |
| 2724 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2725 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2726 | int nr = sattr->nr; |
| 2727 | int index = sattr->index; |
| 2728 | unsigned long val; |
| 2729 | int err; |
| 2730 | |
| 2731 | err = kstrtoul(buf, 10, &val); |
| 2732 | if (err < 0) |
| 2733 | return err; |
| 2734 | |
| 2735 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255); |
| 2736 | |
| 2737 | mutex_lock(&data->update_lock); |
| 2738 | data->weight_temp[index][nr] = val; |
| 2739 | nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val); |
| 2740 | mutex_unlock(&data->update_lock); |
| 2741 | return count; |
| 2742 | } |
| 2743 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2744 | SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", S_IWUSR | S_IRUGO, |
| 2745 | show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0); |
| 2746 | SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step", |
| 2747 | S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 0); |
| 2748 | SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol", |
| 2749 | S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 1); |
| 2750 | SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base", |
| 2751 | S_IWUSR | S_IRUGO, show_weight_temp, store_weight_temp, 0, 2); |
| 2752 | SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step", |
| 2753 | S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 5); |
| 2754 | SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base", |
| 2755 | S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0, 6); |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 2756 | |
| 2757 | static ssize_t |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2758 | show_fan_time(struct device *dev, struct device_attribute *attr, char *buf) |
| 2759 | { |
| 2760 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2761 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2762 | int nr = sattr->nr; |
| 2763 | int index = sattr->index; |
| 2764 | |
| 2765 | return sprintf(buf, "%d\n", |
| 2766 | step_time_from_reg(data->fan_time[index][nr], |
| 2767 | data->pwm_mode[nr])); |
| 2768 | } |
| 2769 | |
| 2770 | static ssize_t |
| 2771 | store_fan_time(struct device *dev, struct device_attribute *attr, |
| 2772 | const char *buf, size_t count) |
| 2773 | { |
| 2774 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2775 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2776 | int nr = sattr->nr; |
| 2777 | int index = sattr->index; |
| 2778 | unsigned long val; |
| 2779 | int err; |
| 2780 | |
| 2781 | err = kstrtoul(buf, 10, &val); |
| 2782 | if (err < 0) |
| 2783 | return err; |
| 2784 | |
| 2785 | val = step_time_to_reg(val, data->pwm_mode[nr]); |
| 2786 | mutex_lock(&data->update_lock); |
| 2787 | data->fan_time[index][nr] = val; |
| 2788 | nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val); |
| 2789 | mutex_unlock(&data->update_lock); |
| 2790 | return count; |
| 2791 | } |
| 2792 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 2793 | static ssize_t |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2794 | show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf) |
| 2795 | { |
| 2796 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2797 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2798 | |
| 2799 | return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]); |
| 2800 | } |
| 2801 | |
| 2802 | static ssize_t |
| 2803 | store_auto_pwm(struct device *dev, struct device_attribute *attr, |
| 2804 | const char *buf, size_t count) |
| 2805 | { |
| 2806 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2807 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2808 | int nr = sattr->nr; |
| 2809 | int point = sattr->index; |
| 2810 | unsigned long val; |
| 2811 | int err; |
| 2812 | u8 reg; |
| 2813 | |
| 2814 | err = kstrtoul(buf, 10, &val); |
| 2815 | if (err < 0) |
| 2816 | return err; |
| 2817 | if (val > 255) |
| 2818 | return -EINVAL; |
| 2819 | |
| 2820 | if (point == data->auto_pwm_num) { |
| 2821 | if (data->kind != nct6775 && !val) |
| 2822 | return -EINVAL; |
| 2823 | if (data->kind != nct6779 && val) |
| 2824 | val = 0xff; |
| 2825 | } |
| 2826 | |
| 2827 | mutex_lock(&data->update_lock); |
| 2828 | data->auto_pwm[nr][point] = val; |
| 2829 | if (point < data->auto_pwm_num) { |
| 2830 | nct6775_write_value(data, |
| 2831 | NCT6775_AUTO_PWM(data, nr, point), |
| 2832 | data->auto_pwm[nr][point]); |
| 2833 | } else { |
| 2834 | switch (data->kind) { |
| 2835 | case nct6775: |
| 2836 | /* disable if needed (pwm == 0) */ |
| 2837 | reg = nct6775_read_value(data, |
| 2838 | NCT6775_REG_CRITICAL_ENAB[nr]); |
| 2839 | if (val) |
| 2840 | reg |= 0x02; |
| 2841 | else |
| 2842 | reg &= ~0x02; |
| 2843 | nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr], |
| 2844 | reg); |
| 2845 | break; |
| 2846 | case nct6776: |
| 2847 | break; /* always enabled, nothing to do */ |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2848 | case nct6106: |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2849 | case nct6779: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 2850 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 2851 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 2852 | case nct6793: |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2853 | nct6775_write_value(data, data->REG_CRITICAL_PWM[nr], |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2854 | val); |
| 2855 | reg = nct6775_read_value(data, |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2856 | data->REG_CRITICAL_PWM_ENABLE[nr]); |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2857 | if (val == 255) |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2858 | reg &= ~data->CRITICAL_PWM_ENABLE_MASK; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2859 | else |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2860 | reg |= data->CRITICAL_PWM_ENABLE_MASK; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2861 | nct6775_write_value(data, |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 2862 | data->REG_CRITICAL_PWM_ENABLE[nr], |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 2863 | reg); |
| 2864 | break; |
| 2865 | } |
| 2866 | } |
| 2867 | mutex_unlock(&data->update_lock); |
| 2868 | return count; |
| 2869 | } |
| 2870 | |
| 2871 | static ssize_t |
| 2872 | show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 2873 | { |
| 2874 | struct nct6775_data *data = nct6775_update_device(dev); |
| 2875 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2876 | int nr = sattr->nr; |
| 2877 | int point = sattr->index; |
| 2878 | |
| 2879 | /* |
| 2880 | * We don't know for sure if the temperature is signed or unsigned. |
| 2881 | * Assume it is unsigned. |
| 2882 | */ |
| 2883 | return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000); |
| 2884 | } |
| 2885 | |
| 2886 | static ssize_t |
| 2887 | store_auto_temp(struct device *dev, struct device_attribute *attr, |
| 2888 | const char *buf, size_t count) |
| 2889 | { |
| 2890 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2891 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 2892 | int nr = sattr->nr; |
| 2893 | int point = sattr->index; |
| 2894 | unsigned long val; |
| 2895 | int err; |
| 2896 | |
| 2897 | err = kstrtoul(buf, 10, &val); |
| 2898 | if (err) |
| 2899 | return err; |
| 2900 | if (val > 255000) |
| 2901 | return -EINVAL; |
| 2902 | |
| 2903 | mutex_lock(&data->update_lock); |
| 2904 | data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000); |
| 2905 | if (point < data->auto_pwm_num) { |
| 2906 | nct6775_write_value(data, |
| 2907 | NCT6775_AUTO_TEMP(data, nr, point), |
| 2908 | data->auto_temp[nr][point]); |
| 2909 | } else { |
| 2910 | nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr], |
| 2911 | data->auto_temp[nr][point]); |
| 2912 | } |
| 2913 | mutex_unlock(&data->update_lock); |
| 2914 | return count; |
| 2915 | } |
| 2916 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2917 | static umode_t nct6775_pwm_is_visible(struct kobject *kobj, |
| 2918 | struct attribute *attr, int index) |
| 2919 | { |
| 2920 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2921 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 2922 | int pwm = index / 36; /* pwm index */ |
| 2923 | int nr = index % 36; /* attribute index */ |
| 2924 | |
| 2925 | if (!(data->has_pwm & (1 << pwm))) |
| 2926 | return 0; |
| 2927 | |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 2928 | if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */ |
| 2929 | if (!data->REG_WEIGHT_TEMP_SEL[pwm]) |
| 2930 | return 0; |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 2931 | if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */ |
| 2932 | return 0; |
| 2933 | if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */ |
| 2934 | return 0; |
| 2935 | if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */ |
| 2936 | return 0; |
| 2937 | |
| 2938 | if (nr >= 22 && nr <= 35) { /* auto point */ |
| 2939 | int api = (nr - 22) / 2; /* auto point index */ |
| 2940 | |
| 2941 | if (api > data->auto_pwm_num) |
| 2942 | return 0; |
| 2943 | } |
| 2944 | return attr->mode; |
| 2945 | } |
| 2946 | |
| 2947 | SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", S_IWUSR | S_IRUGO, |
| 2948 | show_fan_time, store_fan_time, 0, 0); |
| 2949 | SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", S_IWUSR | S_IRUGO, |
| 2950 | show_fan_time, store_fan_time, 0, 1); |
| 2951 | SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", S_IWUSR | S_IRUGO, |
| 2952 | show_fan_time, store_fan_time, 0, 2); |
| 2953 | SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", S_IWUSR | S_IRUGO, show_pwm, |
| 2954 | store_pwm, 0, 1); |
| 2955 | SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", S_IWUSR | S_IRUGO, show_pwm, |
| 2956 | store_pwm, 0, 2); |
| 2957 | SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", S_IWUSR | S_IRUGO, |
| 2958 | show_temp_tolerance, store_temp_tolerance, 0, 0); |
| 2959 | SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance", |
| 2960 | S_IWUSR | S_IRUGO, show_temp_tolerance, store_temp_tolerance, |
| 2961 | 0, 1); |
| 2962 | |
| 2963 | SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", S_IWUSR | S_IRUGO, show_pwm, store_pwm, |
| 2964 | 0, 3); |
| 2965 | |
| 2966 | SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", S_IWUSR | S_IRUGO, show_pwm, |
| 2967 | store_pwm, 0, 4); |
| 2968 | |
| 2969 | SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm", |
| 2970 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 0); |
| 2971 | SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp", |
| 2972 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 0); |
| 2973 | |
| 2974 | SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm", |
| 2975 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 1); |
| 2976 | SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp", |
| 2977 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 1); |
| 2978 | |
| 2979 | SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm", |
| 2980 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 2); |
| 2981 | SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp", |
| 2982 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 2); |
| 2983 | |
| 2984 | SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm", |
| 2985 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 3); |
| 2986 | SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp", |
| 2987 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 3); |
| 2988 | |
| 2989 | SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm", |
| 2990 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 4); |
| 2991 | SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp", |
| 2992 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 4); |
| 2993 | |
| 2994 | SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm", |
| 2995 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 5); |
| 2996 | SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp", |
| 2997 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 5); |
| 2998 | |
| 2999 | SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm", |
| 3000 | S_IWUSR | S_IRUGO, show_auto_pwm, store_auto_pwm, 0, 6); |
| 3001 | SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp", |
| 3002 | S_IWUSR | S_IRUGO, show_auto_temp, store_auto_temp, 0, 6); |
| 3003 | |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3004 | /* |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3005 | * nct6775_pwm_is_visible uses the index into the following array |
| 3006 | * to determine if attributes should be created or not. |
| 3007 | * Any change in order or content must be matched. |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3008 | */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3009 | static struct sensor_device_template *nct6775_attributes_pwm_template[] = { |
| 3010 | &sensor_dev_template_pwm, |
| 3011 | &sensor_dev_template_pwm_mode, |
| 3012 | &sensor_dev_template_pwm_enable, |
| 3013 | &sensor_dev_template_pwm_temp_sel, |
| 3014 | &sensor_dev_template_pwm_temp_tolerance, |
| 3015 | &sensor_dev_template_pwm_crit_temp_tolerance, |
| 3016 | &sensor_dev_template_pwm_target_temp, |
| 3017 | &sensor_dev_template_fan_target, |
| 3018 | &sensor_dev_template_fan_tolerance, |
| 3019 | &sensor_dev_template_pwm_stop_time, |
| 3020 | &sensor_dev_template_pwm_step_up_time, |
| 3021 | &sensor_dev_template_pwm_step_down_time, |
| 3022 | &sensor_dev_template_pwm_start, |
| 3023 | &sensor_dev_template_pwm_floor, |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 3024 | &sensor_dev_template_pwm_weight_temp_sel, /* 14 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3025 | &sensor_dev_template_pwm_weight_temp_step, |
| 3026 | &sensor_dev_template_pwm_weight_temp_step_tol, |
| 3027 | &sensor_dev_template_pwm_weight_temp_step_base, |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 3028 | &sensor_dev_template_pwm_weight_duty_step, /* 18 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3029 | &sensor_dev_template_pwm_max, /* 19 */ |
| 3030 | &sensor_dev_template_pwm_step, /* 20 */ |
| 3031 | &sensor_dev_template_pwm_weight_duty_base, /* 21 */ |
| 3032 | &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */ |
| 3033 | &sensor_dev_template_pwm_auto_point1_temp, |
| 3034 | &sensor_dev_template_pwm_auto_point2_pwm, |
| 3035 | &sensor_dev_template_pwm_auto_point2_temp, |
| 3036 | &sensor_dev_template_pwm_auto_point3_pwm, |
| 3037 | &sensor_dev_template_pwm_auto_point3_temp, |
| 3038 | &sensor_dev_template_pwm_auto_point4_pwm, |
| 3039 | &sensor_dev_template_pwm_auto_point4_temp, |
| 3040 | &sensor_dev_template_pwm_auto_point5_pwm, |
| 3041 | &sensor_dev_template_pwm_auto_point5_temp, |
| 3042 | &sensor_dev_template_pwm_auto_point6_pwm, |
| 3043 | &sensor_dev_template_pwm_auto_point6_temp, |
| 3044 | &sensor_dev_template_pwm_auto_point7_pwm, |
| 3045 | &sensor_dev_template_pwm_auto_point7_temp, /* 35 */ |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3046 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3047 | NULL |
| 3048 | }; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3049 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3050 | static struct sensor_template_group nct6775_pwm_template_group = { |
| 3051 | .templates = nct6775_attributes_pwm_template, |
| 3052 | .is_visible = nct6775_pwm_is_visible, |
| 3053 | .base = 1, |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3054 | }; |
| 3055 | |
| 3056 | static ssize_t |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3057 | show_vid(struct device *dev, struct device_attribute *attr, char *buf) |
| 3058 | { |
| 3059 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 9cd892b | 2014-11-16 10:00:06 -0800 | [diff] [blame] | 3060 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3061 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 3062 | } |
| 3063 | |
| 3064 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 3065 | |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3066 | /* Case open detection */ |
| 3067 | |
| 3068 | static ssize_t |
| 3069 | clear_caseopen(struct device *dev, struct device_attribute *attr, |
| 3070 | const char *buf, size_t count) |
| 3071 | { |
| 3072 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3073 | int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE; |
| 3074 | unsigned long val; |
| 3075 | u8 reg; |
| 3076 | int ret; |
| 3077 | |
| 3078 | if (kstrtoul(buf, 10, &val) || val != 0) |
| 3079 | return -EINVAL; |
| 3080 | |
| 3081 | mutex_lock(&data->update_lock); |
| 3082 | |
| 3083 | /* |
| 3084 | * Use CR registers to clear caseopen status. |
| 3085 | * The CR registers are the same for all chips, and not all chips |
| 3086 | * support clearing the caseopen status through "regular" registers. |
| 3087 | */ |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3088 | ret = superio_enter(data->sioreg); |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3089 | if (ret) { |
| 3090 | count = ret; |
| 3091 | goto error; |
| 3092 | } |
| 3093 | |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3094 | superio_select(data->sioreg, NCT6775_LD_ACPI); |
| 3095 | reg = superio_inb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]); |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3096 | reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr]; |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3097 | superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg); |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3098 | reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr]; |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3099 | superio_outb(data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg); |
| 3100 | superio_exit(data->sioreg); |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3101 | |
| 3102 | data->valid = false; /* Force cache refresh */ |
| 3103 | error: |
| 3104 | mutex_unlock(&data->update_lock); |
| 3105 | return count; |
| 3106 | } |
| 3107 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3108 | static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm, |
| 3109 | clear_caseopen, INTRUSION_ALARM_BASE); |
| 3110 | static SENSOR_DEVICE_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm, |
| 3111 | clear_caseopen, INTRUSION_ALARM_BASE + 1); |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3112 | static SENSOR_DEVICE_ATTR(intrusion0_beep, S_IWUSR | S_IRUGO, show_beep, |
| 3113 | store_beep, INTRUSION_ALARM_BASE); |
| 3114 | static SENSOR_DEVICE_ATTR(intrusion1_beep, S_IWUSR | S_IRUGO, show_beep, |
| 3115 | store_beep, INTRUSION_ALARM_BASE + 1); |
| 3116 | static SENSOR_DEVICE_ATTR(beep_enable, S_IWUSR | S_IRUGO, show_beep, |
| 3117 | store_beep, BEEP_ENABLE_BASE); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3118 | |
| 3119 | static umode_t nct6775_other_is_visible(struct kobject *kobj, |
| 3120 | struct attribute *attr, int index) |
| 3121 | { |
| 3122 | struct device *dev = container_of(kobj, struct device, kobj); |
| 3123 | struct nct6775_data *data = dev_get_drvdata(dev); |
| 3124 | |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 3125 | if (index == 0 && !data->have_vid) |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3126 | return 0; |
| 3127 | |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 3128 | if (index == 1 || index == 2) { |
| 3129 | if (data->ALARM_BITS[INTRUSION_ALARM_BASE + index - 1] < 0) |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3130 | return 0; |
| 3131 | } |
| 3132 | |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 3133 | if (index == 3 || index == 4) { |
| 3134 | if (data->BEEP_BITS[INTRUSION_ALARM_BASE + index - 3] < 0) |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3135 | return 0; |
| 3136 | } |
| 3137 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3138 | return attr->mode; |
| 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * nct6775_other_is_visible uses the index into the following array |
| 3143 | * to determine if attributes should be created or not. |
| 3144 | * Any change in order or content must be matched. |
| 3145 | */ |
| 3146 | static struct attribute *nct6775_attributes_other[] = { |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 3147 | &dev_attr_cpu0_vid.attr, /* 0 */ |
| 3148 | &sensor_dev_attr_intrusion0_alarm.dev_attr.attr, /* 1 */ |
| 3149 | &sensor_dev_attr_intrusion1_alarm.dev_attr.attr, /* 2 */ |
| 3150 | &sensor_dev_attr_intrusion0_beep.dev_attr.attr, /* 3 */ |
| 3151 | &sensor_dev_attr_intrusion1_beep.dev_attr.attr, /* 4 */ |
| 3152 | &sensor_dev_attr_beep_enable.dev_attr.attr, /* 5 */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3153 | |
| 3154 | NULL |
| 3155 | }; |
| 3156 | |
| 3157 | static const struct attribute_group nct6775_group_other = { |
| 3158 | .attrs = nct6775_attributes_other, |
| 3159 | .is_visible = nct6775_other_is_visible, |
Guenter Roeck | a6bd587 | 2012-12-04 03:13:34 -0800 | [diff] [blame] | 3160 | }; |
| 3161 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3162 | static inline void nct6775_init_device(struct nct6775_data *data) |
| 3163 | { |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3164 | int i; |
| 3165 | u8 tmp, diode; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3166 | |
| 3167 | /* Start monitoring if needed */ |
| 3168 | if (data->REG_CONFIG) { |
| 3169 | tmp = nct6775_read_value(data, data->REG_CONFIG); |
| 3170 | if (!(tmp & 0x01)) |
| 3171 | nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01); |
| 3172 | } |
| 3173 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3174 | /* Enable temperature sensors if needed */ |
| 3175 | for (i = 0; i < NUM_TEMP; i++) { |
| 3176 | if (!(data->have_temp & (1 << i))) |
| 3177 | continue; |
| 3178 | if (!data->reg_temp_config[i]) |
| 3179 | continue; |
| 3180 | tmp = nct6775_read_value(data, data->reg_temp_config[i]); |
| 3181 | if (tmp & 0x01) |
| 3182 | nct6775_write_value(data, data->reg_temp_config[i], |
| 3183 | tmp & 0xfe); |
| 3184 | } |
| 3185 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3186 | /* Enable VBAT monitoring if needed */ |
| 3187 | tmp = nct6775_read_value(data, data->REG_VBAT); |
| 3188 | if (!(tmp & 0x01)) |
| 3189 | nct6775_write_value(data, data->REG_VBAT, tmp | 0x01); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3190 | |
| 3191 | diode = nct6775_read_value(data, data->REG_DIODE); |
| 3192 | |
| 3193 | for (i = 0; i < data->temp_fixed_num; i++) { |
| 3194 | if (!(data->have_temp_fixed & (1 << i))) |
| 3195 | continue; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3196 | if ((tmp & (data->DIODE_MASK << i))) /* diode */ |
| 3197 | data->temp_type[i] |
| 3198 | = 3 - ((diode >> i) & data->DIODE_MASK); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3199 | else /* thermistor */ |
| 3200 | data->temp_type[i] = 4; |
| 3201 | } |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3204 | static void |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3205 | nct6775_check_fan_inputs(struct nct6775_data *data) |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3206 | { |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3207 | bool fan3pin, fan4pin, fan4min, fan5pin, fan6pin; |
| 3208 | bool pwm3pin, pwm4pin, pwm5pin, pwm6pin; |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3209 | int sioreg = data->sioreg; |
| 3210 | int regval; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3211 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 3212 | /* Store SIO_REG_ENABLE for use during resume */ |
| 3213 | superio_select(sioreg, NCT6775_LD_HWM); |
| 3214 | data->sio_reg_enable = superio_inb(sioreg, SIO_REG_ENABLE); |
| 3215 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3216 | /* fan4 and fan5 share some pins with the GPIO and serial flash */ |
| 3217 | if (data->kind == nct6775) { |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3218 | regval = superio_inb(sioreg, 0x2c); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3219 | |
| 3220 | fan3pin = regval & (1 << 6); |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3221 | pwm3pin = regval & (1 << 7); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3222 | |
| 3223 | /* On NCT6775, fan4 shares pins with the fdc interface */ |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3224 | fan4pin = !(superio_inb(sioreg, 0x2A) & 0x80); |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3225 | fan4min = false; |
| 3226 | fan5pin = false; |
| 3227 | fan6pin = false; |
| 3228 | pwm4pin = false; |
| 3229 | pwm5pin = false; |
| 3230 | pwm6pin = false; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3231 | } else if (data->kind == nct6776) { |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3232 | bool gpok = superio_inb(sioreg, 0x27) & 0x80; |
Guenter Roeck | 25cdd99 | 2015-02-06 18:55:36 -0800 | [diff] [blame] | 3233 | const char *board_vendor, *board_name; |
| 3234 | |
| 3235 | board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); |
| 3236 | board_name = dmi_get_system_info(DMI_BOARD_NAME); |
| 3237 | |
| 3238 | if (board_name && board_vendor && |
| 3239 | !strcmp(board_vendor, "ASRock")) { |
| 3240 | /* |
| 3241 | * Auxiliary fan monitoring is not enabled on ASRock |
| 3242 | * Z77 Pro4-M if booted in UEFI Ultra-FastBoot mode. |
| 3243 | * Observed with BIOS version 2.00. |
| 3244 | */ |
| 3245 | if (!strcmp(board_name, "Z77 Pro4-M")) { |
| 3246 | if ((data->sio_reg_enable & 0xe0) != 0xe0) { |
| 3247 | data->sio_reg_enable |= 0xe0; |
| 3248 | superio_outb(sioreg, SIO_REG_ENABLE, |
| 3249 | data->sio_reg_enable); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3253 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 3254 | if (data->sio_reg_enable & 0x80) |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3255 | fan3pin = gpok; |
| 3256 | else |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3257 | fan3pin = !(superio_inb(sioreg, 0x24) & 0x40); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3258 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 3259 | if (data->sio_reg_enable & 0x40) |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3260 | fan4pin = gpok; |
| 3261 | else |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3262 | fan4pin = superio_inb(sioreg, 0x1C) & 0x01; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3263 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 3264 | if (data->sio_reg_enable & 0x20) |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3265 | fan5pin = gpok; |
| 3266 | else |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3267 | fan5pin = superio_inb(sioreg, 0x1C) & 0x02; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3268 | |
| 3269 | fan4min = fan4pin; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3270 | fan6pin = false; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3271 | pwm3pin = fan3pin; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3272 | pwm4pin = false; |
| 3273 | pwm5pin = false; |
| 3274 | pwm6pin = false; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3275 | } else if (data->kind == nct6106) { |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3276 | regval = superio_inb(sioreg, 0x24); |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3277 | fan3pin = !(regval & 0x80); |
| 3278 | pwm3pin = regval & 0x08; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3279 | |
| 3280 | fan4pin = false; |
| 3281 | fan4min = false; |
| 3282 | fan5pin = false; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3283 | fan6pin = false; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3284 | pwm4pin = false; |
| 3285 | pwm5pin = false; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3286 | pwm6pin = false; |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 3287 | } else { /* NCT6779D, NCT6791D, NCT6792D, or NCT6793D */ |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3288 | regval = superio_inb(sioreg, 0x1c); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3289 | |
| 3290 | fan3pin = !(regval & (1 << 5)); |
| 3291 | fan4pin = !(regval & (1 << 6)); |
| 3292 | fan5pin = !(regval & (1 << 7)); |
| 3293 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3294 | pwm3pin = !(regval & (1 << 0)); |
| 3295 | pwm4pin = !(regval & (1 << 1)); |
| 3296 | pwm5pin = !(regval & (1 << 2)); |
| 3297 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3298 | fan4min = fan4pin; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3299 | |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 3300 | if (data->kind == nct6791 || data->kind == nct6792 || |
| 3301 | data->kind == nct6793) { |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3302 | regval = superio_inb(sioreg, 0x2d); |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3303 | fan6pin = (regval & (1 << 1)); |
| 3304 | pwm6pin = (regval & (1 << 0)); |
| 3305 | } else { /* NCT6779D */ |
| 3306 | fan6pin = false; |
| 3307 | pwm6pin = false; |
| 3308 | } |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3309 | } |
| 3310 | |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3311 | /* fan 1 and 2 (0x03) are always present */ |
| 3312 | data->has_fan = 0x03 | (fan3pin << 2) | (fan4pin << 3) | |
| 3313 | (fan5pin << 4) | (fan6pin << 5); |
| 3314 | data->has_fan_min = 0x03 | (fan3pin << 2) | (fan4min << 3) | |
| 3315 | (fan5pin << 4); |
| 3316 | data->has_pwm = 0x03 | (pwm3pin << 2) | (pwm4pin << 3) | |
| 3317 | (pwm5pin << 4) | (pwm6pin << 5); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3318 | } |
| 3319 | |
Guenter Roeck | 8e9285b | 2012-12-04 08:03:37 -0800 | [diff] [blame] | 3320 | static void add_temp_sensors(struct nct6775_data *data, const u16 *regp, |
| 3321 | int *available, int *mask) |
| 3322 | { |
| 3323 | int i; |
| 3324 | u8 src; |
| 3325 | |
| 3326 | for (i = 0; i < data->pwm_num && *available; i++) { |
| 3327 | int index; |
| 3328 | |
| 3329 | if (!regp[i]) |
| 3330 | continue; |
| 3331 | src = nct6775_read_value(data, regp[i]); |
| 3332 | src &= 0x1f; |
| 3333 | if (!src || (*mask & (1 << src))) |
| 3334 | continue; |
| 3335 | if (src >= data->temp_label_num || |
| 3336 | !strlen(data->temp_label[src])) |
| 3337 | continue; |
| 3338 | |
| 3339 | index = __ffs(*available); |
| 3340 | nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src); |
| 3341 | *available &= ~(1 << index); |
| 3342 | *mask |= 1 << src; |
| 3343 | } |
| 3344 | } |
| 3345 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3346 | static int nct6775_probe(struct platform_device *pdev) |
| 3347 | { |
| 3348 | struct device *dev = &pdev->dev; |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 3349 | struct nct6775_sio_data *sio_data = dev_get_platdata(dev); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3350 | struct nct6775_data *data; |
| 3351 | struct resource *res; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3352 | int i, s, err = 0; |
| 3353 | int src, mask, available; |
| 3354 | const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3355 | const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit; |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 3356 | const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3357 | int num_reg_temp, num_reg_temp_mon; |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3358 | u8 cr2a; |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3359 | struct attribute_group *group; |
Guenter Roeck | a150d95 | 2013-07-11 22:55:22 -0700 | [diff] [blame] | 3360 | struct device *hwmon_dev; |
Axel Lin | 55bdee6 | 2014-07-24 08:59:34 +0800 | [diff] [blame] | 3361 | int num_attr_groups = 0; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3362 | |
| 3363 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 3364 | if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH, |
| 3365 | DRVNAME)) |
| 3366 | return -EBUSY; |
| 3367 | |
| 3368 | data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data), |
| 3369 | GFP_KERNEL); |
| 3370 | if (!data) |
| 3371 | return -ENOMEM; |
| 3372 | |
| 3373 | data->kind = sio_data->kind; |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3374 | data->sioreg = sio_data->sioreg; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3375 | data->addr = res->start; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3376 | mutex_init(&data->update_lock); |
| 3377 | data->name = nct6775_device_names[data->kind]; |
| 3378 | data->bank = 0xff; /* Force initial bank selection */ |
| 3379 | platform_set_drvdata(pdev, data); |
| 3380 | |
| 3381 | switch (data->kind) { |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3382 | case nct6106: |
| 3383 | data->in_num = 9; |
| 3384 | data->pwm_num = 3; |
| 3385 | data->auto_pwm_num = 4; |
| 3386 | data->temp_fixed_num = 3; |
| 3387 | data->num_temp_alarms = 6; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3388 | data->num_temp_beeps = 6; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3389 | |
| 3390 | data->fan_from_reg = fan_from_reg13; |
| 3391 | data->fan_from_reg_min = fan_from_reg13; |
| 3392 | |
| 3393 | data->temp_label = nct6776_temp_label; |
| 3394 | data->temp_label_num = ARRAY_SIZE(nct6776_temp_label); |
| 3395 | |
| 3396 | data->REG_VBAT = NCT6106_REG_VBAT; |
| 3397 | data->REG_DIODE = NCT6106_REG_DIODE; |
| 3398 | data->DIODE_MASK = NCT6106_DIODE_MASK; |
| 3399 | data->REG_VIN = NCT6106_REG_IN; |
| 3400 | data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN; |
| 3401 | data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX; |
| 3402 | data->REG_TARGET = NCT6106_REG_TARGET; |
| 3403 | data->REG_FAN = NCT6106_REG_FAN; |
| 3404 | data->REG_FAN_MODE = NCT6106_REG_FAN_MODE; |
| 3405 | data->REG_FAN_MIN = NCT6106_REG_FAN_MIN; |
| 3406 | data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES; |
| 3407 | data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT; |
| 3408 | data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME; |
| 3409 | data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME; |
| 3410 | data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME; |
| 3411 | data->REG_PWM[0] = NCT6106_REG_PWM; |
| 3412 | data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT; |
| 3413 | data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT; |
| 3414 | data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP; |
| 3415 | data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE; |
| 3416 | data->REG_PWM_READ = NCT6106_REG_PWM_READ; |
| 3417 | data->REG_PWM_MODE = NCT6106_REG_PWM_MODE; |
| 3418 | data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK; |
| 3419 | data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP; |
| 3420 | data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM; |
| 3421 | data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP; |
| 3422 | data->REG_CRITICAL_TEMP_TOLERANCE |
| 3423 | = NCT6106_REG_CRITICAL_TEMP_TOLERANCE; |
| 3424 | data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE; |
| 3425 | data->CRITICAL_PWM_ENABLE_MASK |
| 3426 | = NCT6106_CRITICAL_PWM_ENABLE_MASK; |
| 3427 | data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM; |
| 3428 | data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET; |
| 3429 | data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE; |
| 3430 | data->REG_TEMP_SEL = NCT6106_REG_TEMP_SEL; |
| 3431 | data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL; |
| 3432 | data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP; |
| 3433 | data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL; |
| 3434 | data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE; |
| 3435 | data->REG_ALARM = NCT6106_REG_ALARM; |
| 3436 | data->ALARM_BITS = NCT6106_ALARM_BITS; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3437 | data->REG_BEEP = NCT6106_REG_BEEP; |
| 3438 | data->BEEP_BITS = NCT6106_BEEP_BITS; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3439 | |
| 3440 | reg_temp = NCT6106_REG_TEMP; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3441 | reg_temp_mon = NCT6106_REG_TEMP_MON; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3442 | num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP); |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3443 | num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON); |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3444 | reg_temp_over = NCT6106_REG_TEMP_OVER; |
| 3445 | reg_temp_hyst = NCT6106_REG_TEMP_HYST; |
| 3446 | reg_temp_config = NCT6106_REG_TEMP_CONFIG; |
| 3447 | reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE; |
| 3448 | reg_temp_crit = NCT6106_REG_TEMP_CRIT; |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 3449 | reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L; |
| 3450 | reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3451 | |
| 3452 | break; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3453 | case nct6775: |
| 3454 | data->in_num = 9; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3455 | data->pwm_num = 3; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3456 | data->auto_pwm_num = 6; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3457 | data->has_fan_div = true; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3458 | data->temp_fixed_num = 3; |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 3459 | data->num_temp_alarms = 3; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3460 | data->num_temp_beeps = 3; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3461 | |
| 3462 | data->ALARM_BITS = NCT6775_ALARM_BITS; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3463 | data->BEEP_BITS = NCT6775_BEEP_BITS; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3464 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3465 | data->fan_from_reg = fan_from_reg16; |
| 3466 | data->fan_from_reg_min = fan_from_reg8; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3467 | data->target_temp_mask = 0x7f; |
| 3468 | data->tolerance_mask = 0x0f; |
| 3469 | data->speed_tolerance_limit = 15; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3470 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3471 | data->temp_label = nct6775_temp_label; |
| 3472 | data->temp_label_num = ARRAY_SIZE(nct6775_temp_label); |
| 3473 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3474 | data->REG_CONFIG = NCT6775_REG_CONFIG; |
| 3475 | data->REG_VBAT = NCT6775_REG_VBAT; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3476 | data->REG_DIODE = NCT6775_REG_DIODE; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3477 | data->DIODE_MASK = NCT6775_DIODE_MASK; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3478 | data->REG_VIN = NCT6775_REG_IN; |
| 3479 | data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN; |
| 3480 | data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3481 | data->REG_TARGET = NCT6775_REG_TARGET; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3482 | data->REG_FAN = NCT6775_REG_FAN; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3483 | data->REG_FAN_MODE = NCT6775_REG_FAN_MODE; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3484 | data->REG_FAN_MIN = NCT6775_REG_FAN_MIN; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 3485 | data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3486 | data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3487 | data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME; |
| 3488 | data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME; |
| 3489 | data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3490 | data->REG_PWM[0] = NCT6775_REG_PWM; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3491 | data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT; |
| 3492 | data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT; |
| 3493 | data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT; |
| 3494 | data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3495 | data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3496 | data->REG_PWM_READ = NCT6775_REG_PWM_READ; |
| 3497 | data->REG_PWM_MODE = NCT6775_REG_PWM_MODE; |
| 3498 | data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3499 | data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP; |
| 3500 | data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM; |
| 3501 | data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP; |
| 3502 | data->REG_CRITICAL_TEMP_TOLERANCE |
| 3503 | = NCT6775_REG_CRITICAL_TEMP_TOLERANCE; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3504 | data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET; |
| 3505 | data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3506 | data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3507 | data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL; |
| 3508 | data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP; |
| 3509 | data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL; |
| 3510 | data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3511 | data->REG_ALARM = NCT6775_REG_ALARM; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3512 | data->REG_BEEP = NCT6775_REG_BEEP; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3513 | |
| 3514 | reg_temp = NCT6775_REG_TEMP; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3515 | reg_temp_mon = NCT6775_REG_TEMP_MON; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3516 | num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP); |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3517 | num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3518 | reg_temp_over = NCT6775_REG_TEMP_OVER; |
| 3519 | reg_temp_hyst = NCT6775_REG_TEMP_HYST; |
| 3520 | reg_temp_config = NCT6775_REG_TEMP_CONFIG; |
| 3521 | reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE; |
| 3522 | reg_temp_crit = NCT6775_REG_TEMP_CRIT; |
| 3523 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3524 | break; |
| 3525 | case nct6776: |
| 3526 | data->in_num = 9; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3527 | data->pwm_num = 3; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3528 | data->auto_pwm_num = 4; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3529 | data->has_fan_div = false; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3530 | data->temp_fixed_num = 3; |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 3531 | data->num_temp_alarms = 3; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3532 | data->num_temp_beeps = 6; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3533 | |
| 3534 | data->ALARM_BITS = NCT6776_ALARM_BITS; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3535 | data->BEEP_BITS = NCT6776_BEEP_BITS; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3536 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3537 | data->fan_from_reg = fan_from_reg13; |
| 3538 | data->fan_from_reg_min = fan_from_reg13; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3539 | data->target_temp_mask = 0xff; |
| 3540 | data->tolerance_mask = 0x07; |
| 3541 | data->speed_tolerance_limit = 63; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3542 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3543 | data->temp_label = nct6776_temp_label; |
| 3544 | data->temp_label_num = ARRAY_SIZE(nct6776_temp_label); |
| 3545 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3546 | data->REG_CONFIG = NCT6775_REG_CONFIG; |
| 3547 | data->REG_VBAT = NCT6775_REG_VBAT; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3548 | data->REG_DIODE = NCT6775_REG_DIODE; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3549 | data->DIODE_MASK = NCT6775_DIODE_MASK; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3550 | data->REG_VIN = NCT6775_REG_IN; |
| 3551 | data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN; |
| 3552 | data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3553 | data->REG_TARGET = NCT6775_REG_TARGET; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3554 | data->REG_FAN = NCT6775_REG_FAN; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3555 | data->REG_FAN_MODE = NCT6775_REG_FAN_MODE; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3556 | data->REG_FAN_MIN = NCT6776_REG_FAN_MIN; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 3557 | data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3558 | data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3559 | data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME; |
Guenter Roeck | 728d294 | 2015-08-31 16:13:47 -0700 | [diff] [blame] | 3560 | data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME; |
| 3561 | data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3562 | data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3563 | data->REG_PWM[0] = NCT6775_REG_PWM; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3564 | data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT; |
| 3565 | data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3566 | data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP; |
| 3567 | data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3568 | data->REG_PWM_READ = NCT6775_REG_PWM_READ; |
| 3569 | data->REG_PWM_MODE = NCT6776_REG_PWM_MODE; |
| 3570 | data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3571 | data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP; |
| 3572 | data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM; |
| 3573 | data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP; |
| 3574 | data->REG_CRITICAL_TEMP_TOLERANCE |
| 3575 | = NCT6775_REG_CRITICAL_TEMP_TOLERANCE; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3576 | data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET; |
| 3577 | data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3578 | data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3579 | data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL; |
| 3580 | data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP; |
| 3581 | data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL; |
| 3582 | data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3583 | data->REG_ALARM = NCT6775_REG_ALARM; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3584 | data->REG_BEEP = NCT6776_REG_BEEP; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3585 | |
| 3586 | reg_temp = NCT6775_REG_TEMP; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3587 | reg_temp_mon = NCT6775_REG_TEMP_MON; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3588 | num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP); |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3589 | num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3590 | reg_temp_over = NCT6775_REG_TEMP_OVER; |
| 3591 | reg_temp_hyst = NCT6775_REG_TEMP_HYST; |
| 3592 | reg_temp_config = NCT6776_REG_TEMP_CONFIG; |
| 3593 | reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE; |
| 3594 | reg_temp_crit = NCT6776_REG_TEMP_CRIT; |
| 3595 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3596 | break; |
| 3597 | case nct6779: |
| 3598 | data->in_num = 15; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3599 | data->pwm_num = 5; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3600 | data->auto_pwm_num = 4; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3601 | data->has_fan_div = false; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3602 | data->temp_fixed_num = 6; |
Guenter Roeck | b1d2bff | 2013-06-22 16:15:31 -0700 | [diff] [blame] | 3603 | data->num_temp_alarms = 2; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3604 | data->num_temp_beeps = 2; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3605 | |
| 3606 | data->ALARM_BITS = NCT6779_ALARM_BITS; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3607 | data->BEEP_BITS = NCT6779_BEEP_BITS; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3608 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3609 | data->fan_from_reg = fan_from_reg13; |
| 3610 | data->fan_from_reg_min = fan_from_reg13; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3611 | data->target_temp_mask = 0xff; |
| 3612 | data->tolerance_mask = 0x07; |
| 3613 | data->speed_tolerance_limit = 63; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3614 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3615 | data->temp_label = nct6779_temp_label; |
Guenter Roeck | 9a38371 | 2015-08-29 15:29:25 -0700 | [diff] [blame^] | 3616 | data->temp_label_num = NCT6779_NUM_LABELS; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3617 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3618 | data->REG_CONFIG = NCT6775_REG_CONFIG; |
| 3619 | data->REG_VBAT = NCT6775_REG_VBAT; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3620 | data->REG_DIODE = NCT6775_REG_DIODE; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3621 | data->DIODE_MASK = NCT6775_DIODE_MASK; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3622 | data->REG_VIN = NCT6779_REG_IN; |
| 3623 | data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN; |
| 3624 | data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3625 | data->REG_TARGET = NCT6775_REG_TARGET; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3626 | data->REG_FAN = NCT6779_REG_FAN; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3627 | data->REG_FAN_MODE = NCT6775_REG_FAN_MODE; |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3628 | data->REG_FAN_MIN = NCT6776_REG_FAN_MIN; |
Guenter Roeck | 5c25d95 | 2012-12-11 07:29:06 -0800 | [diff] [blame] | 3629 | data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3630 | data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3631 | data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME; |
Guenter Roeck | 728d294 | 2015-08-31 16:13:47 -0700 | [diff] [blame] | 3632 | data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME; |
| 3633 | data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3634 | data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3635 | data->REG_PWM[0] = NCT6775_REG_PWM; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3636 | data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT; |
| 3637 | data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3638 | data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP; |
| 3639 | data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 3640 | data->REG_PWM_READ = NCT6775_REG_PWM_READ; |
| 3641 | data->REG_PWM_MODE = NCT6776_REG_PWM_MODE; |
| 3642 | data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3643 | data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP; |
| 3644 | data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM; |
| 3645 | data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP; |
| 3646 | data->REG_CRITICAL_TEMP_TOLERANCE |
| 3647 | = NCT6775_REG_CRITICAL_TEMP_TOLERANCE; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3648 | data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE; |
| 3649 | data->CRITICAL_PWM_ENABLE_MASK |
| 3650 | = NCT6779_CRITICAL_PWM_ENABLE_MASK; |
| 3651 | data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3652 | data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET; |
| 3653 | data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 3654 | data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL; |
Guenter Roeck | bbd8dec | 2012-12-04 09:08:29 -0800 | [diff] [blame] | 3655 | data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL; |
| 3656 | data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP; |
| 3657 | data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL; |
| 3658 | data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3659 | data->REG_ALARM = NCT6779_REG_ALARM; |
Guenter Roeck | 3084699 | 2013-06-24 22:21:59 -0700 | [diff] [blame] | 3660 | data->REG_BEEP = NCT6776_REG_BEEP; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3661 | |
| 3662 | reg_temp = NCT6779_REG_TEMP; |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3663 | reg_temp_mon = NCT6779_REG_TEMP_MON; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3664 | num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP); |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3665 | num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON); |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3666 | reg_temp_over = NCT6779_REG_TEMP_OVER; |
| 3667 | reg_temp_hyst = NCT6779_REG_TEMP_HYST; |
| 3668 | reg_temp_config = NCT6779_REG_TEMP_CONFIG; |
| 3669 | reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE; |
| 3670 | reg_temp_crit = NCT6779_REG_TEMP_CRIT; |
| 3671 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3672 | break; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3673 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 3674 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 3675 | case nct6793: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3676 | data->in_num = 15; |
| 3677 | data->pwm_num = 6; |
| 3678 | data->auto_pwm_num = 4; |
| 3679 | data->has_fan_div = false; |
| 3680 | data->temp_fixed_num = 6; |
| 3681 | data->num_temp_alarms = 2; |
| 3682 | data->num_temp_beeps = 2; |
| 3683 | |
| 3684 | data->ALARM_BITS = NCT6791_ALARM_BITS; |
| 3685 | data->BEEP_BITS = NCT6779_BEEP_BITS; |
| 3686 | |
| 3687 | data->fan_from_reg = fan_from_reg13; |
| 3688 | data->fan_from_reg_min = fan_from_reg13; |
| 3689 | data->target_temp_mask = 0xff; |
| 3690 | data->tolerance_mask = 0x07; |
| 3691 | data->speed_tolerance_limit = 63; |
| 3692 | |
| 3693 | data->temp_label = nct6779_temp_label; |
Guenter Roeck | 9a38371 | 2015-08-29 15:29:25 -0700 | [diff] [blame^] | 3694 | data->temp_label_num = NCT6791_NUM_LABELS; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3695 | |
| 3696 | data->REG_CONFIG = NCT6775_REG_CONFIG; |
| 3697 | data->REG_VBAT = NCT6775_REG_VBAT; |
| 3698 | data->REG_DIODE = NCT6775_REG_DIODE; |
| 3699 | data->DIODE_MASK = NCT6775_DIODE_MASK; |
| 3700 | data->REG_VIN = NCT6779_REG_IN; |
| 3701 | data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN; |
| 3702 | data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX; |
| 3703 | data->REG_TARGET = NCT6775_REG_TARGET; |
| 3704 | data->REG_FAN = NCT6779_REG_FAN; |
| 3705 | data->REG_FAN_MODE = NCT6775_REG_FAN_MODE; |
| 3706 | data->REG_FAN_MIN = NCT6776_REG_FAN_MIN; |
| 3707 | data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES; |
| 3708 | data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT; |
| 3709 | data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME; |
Guenter Roeck | 728d294 | 2015-08-31 16:13:47 -0700 | [diff] [blame] | 3710 | data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME; |
| 3711 | data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3712 | data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H; |
| 3713 | data->REG_PWM[0] = NCT6775_REG_PWM; |
| 3714 | data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT; |
| 3715 | data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT; |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 3716 | data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP; |
| 3717 | data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3718 | data->REG_PWM_READ = NCT6775_REG_PWM_READ; |
| 3719 | data->REG_PWM_MODE = NCT6776_REG_PWM_MODE; |
| 3720 | data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK; |
| 3721 | data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP; |
| 3722 | data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM; |
| 3723 | data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP; |
| 3724 | data->REG_CRITICAL_TEMP_TOLERANCE |
| 3725 | = NCT6775_REG_CRITICAL_TEMP_TOLERANCE; |
| 3726 | data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE; |
| 3727 | data->CRITICAL_PWM_ENABLE_MASK |
| 3728 | = NCT6779_CRITICAL_PWM_ENABLE_MASK; |
| 3729 | data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM; |
| 3730 | data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET; |
| 3731 | data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE; |
| 3732 | data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL; |
Guenter Roeck | cc76dee | 2013-11-13 12:47:17 -0800 | [diff] [blame] | 3733 | data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL; |
| 3734 | data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP; |
| 3735 | data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL; |
| 3736 | data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3737 | data->REG_ALARM = NCT6791_REG_ALARM; |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 3738 | if (data->kind == nct6791) |
| 3739 | data->REG_BEEP = NCT6776_REG_BEEP; |
| 3740 | else |
| 3741 | data->REG_BEEP = NCT6792_REG_BEEP; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3742 | |
| 3743 | reg_temp = NCT6779_REG_TEMP; |
| 3744 | num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP); |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 3745 | if (data->kind == nct6791) { |
| 3746 | reg_temp_mon = NCT6779_REG_TEMP_MON; |
| 3747 | num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON); |
| 3748 | } else { |
| 3749 | reg_temp_mon = NCT6792_REG_TEMP_MON; |
| 3750 | num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON); |
| 3751 | } |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3752 | reg_temp_over = NCT6779_REG_TEMP_OVER; |
| 3753 | reg_temp_hyst = NCT6779_REG_TEMP_HYST; |
| 3754 | reg_temp_config = NCT6779_REG_TEMP_CONFIG; |
| 3755 | reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE; |
| 3756 | reg_temp_crit = NCT6779_REG_TEMP_CRIT; |
| 3757 | |
| 3758 | break; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3759 | default: |
| 3760 | return -ENODEV; |
| 3761 | } |
| 3762 | data->have_in = (1 << data->in_num) - 1; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3763 | data->have_temp = 0; |
| 3764 | |
| 3765 | /* |
| 3766 | * On some boards, not all available temperature sources are monitored, |
| 3767 | * even though some of the monitoring registers are unused. |
| 3768 | * Get list of unused monitoring registers, then detect if any fan |
| 3769 | * controls are configured to use unmonitored temperature sources. |
| 3770 | * If so, assign the unmonitored temperature sources to available |
| 3771 | * monitoring registers. |
| 3772 | */ |
| 3773 | mask = 0; |
| 3774 | available = 0; |
| 3775 | for (i = 0; i < num_reg_temp; i++) { |
| 3776 | if (reg_temp[i] == 0) |
| 3777 | continue; |
| 3778 | |
| 3779 | src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f; |
| 3780 | if (!src || (mask & (1 << src))) |
| 3781 | available |= 1 << i; |
| 3782 | |
| 3783 | mask |= 1 << src; |
| 3784 | } |
| 3785 | |
Guenter Roeck | 8e9285b | 2012-12-04 08:03:37 -0800 | [diff] [blame] | 3786 | /* |
| 3787 | * Now find unmonitored temperature registers and enable monitoring |
| 3788 | * if additional monitoring registers are available. |
| 3789 | */ |
| 3790 | add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask); |
| 3791 | add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask); |
| 3792 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3793 | mask = 0; |
| 3794 | s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */ |
| 3795 | for (i = 0; i < num_reg_temp; i++) { |
| 3796 | if (reg_temp[i] == 0) |
| 3797 | continue; |
| 3798 | |
| 3799 | src = nct6775_read_value(data, data->REG_TEMP_SOURCE[i]) & 0x1f; |
| 3800 | if (!src || (mask & (1 << src))) |
| 3801 | continue; |
| 3802 | |
| 3803 | if (src >= data->temp_label_num || |
| 3804 | !strlen(data->temp_label[src])) { |
| 3805 | dev_info(dev, |
| 3806 | "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n", |
| 3807 | src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]); |
| 3808 | continue; |
| 3809 | } |
| 3810 | |
| 3811 | mask |= 1 << src; |
| 3812 | |
| 3813 | /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */ |
| 3814 | if (src <= data->temp_fixed_num) { |
| 3815 | data->have_temp |= 1 << (src - 1); |
| 3816 | data->have_temp_fixed |= 1 << (src - 1); |
| 3817 | data->reg_temp[0][src - 1] = reg_temp[i]; |
| 3818 | data->reg_temp[1][src - 1] = reg_temp_over[i]; |
| 3819 | data->reg_temp[2][src - 1] = reg_temp_hyst[i]; |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 3820 | if (reg_temp_crit_h && reg_temp_crit_h[i]) |
| 3821 | data->reg_temp[3][src - 1] = reg_temp_crit_h[i]; |
| 3822 | else if (reg_temp_crit[src - 1]) |
| 3823 | data->reg_temp[3][src - 1] |
| 3824 | = reg_temp_crit[src - 1]; |
| 3825 | if (reg_temp_crit_l && reg_temp_crit_l[i]) |
| 3826 | data->reg_temp[4][src - 1] = reg_temp_crit_l[i]; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3827 | data->reg_temp_config[src - 1] = reg_temp_config[i]; |
| 3828 | data->temp_src[src - 1] = src; |
| 3829 | continue; |
| 3830 | } |
| 3831 | |
| 3832 | if (s >= NUM_TEMP) |
| 3833 | continue; |
| 3834 | |
| 3835 | /* Use dynamic index for other sources */ |
| 3836 | data->have_temp |= 1 << s; |
| 3837 | data->reg_temp[0][s] = reg_temp[i]; |
| 3838 | data->reg_temp[1][s] = reg_temp_over[i]; |
| 3839 | data->reg_temp[2][s] = reg_temp_hyst[i]; |
| 3840 | data->reg_temp_config[s] = reg_temp_config[i]; |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 3841 | if (reg_temp_crit_h && reg_temp_crit_h[i]) |
| 3842 | data->reg_temp[3][s] = reg_temp_crit_h[i]; |
| 3843 | else if (reg_temp_crit[src - 1]) |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3844 | data->reg_temp[3][s] = reg_temp_crit[src - 1]; |
Guenter Roeck | b7a6135 | 2013-04-02 22:14:06 -0700 | [diff] [blame] | 3845 | if (reg_temp_crit_l && reg_temp_crit_l[i]) |
| 3846 | data->reg_temp[4][s] = reg_temp_crit_l[i]; |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3847 | |
| 3848 | data->temp_src[s] = src; |
| 3849 | s++; |
| 3850 | } |
| 3851 | |
Guenter Roeck | d1a284b | 2013-11-13 12:46:20 -0800 | [diff] [blame] | 3852 | /* |
| 3853 | * Repeat with temperatures used for fan control. |
| 3854 | * This set of registers does not support limits. |
| 3855 | */ |
| 3856 | for (i = 0; i < num_reg_temp_mon; i++) { |
| 3857 | if (reg_temp_mon[i] == 0) |
| 3858 | continue; |
| 3859 | |
| 3860 | src = nct6775_read_value(data, data->REG_TEMP_SEL[i]) & 0x1f; |
| 3861 | if (!src || (mask & (1 << src))) |
| 3862 | continue; |
| 3863 | |
| 3864 | if (src >= data->temp_label_num || |
| 3865 | !strlen(data->temp_label[src])) { |
| 3866 | dev_info(dev, |
| 3867 | "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n", |
| 3868 | src, i, data->REG_TEMP_SEL[i], |
| 3869 | reg_temp_mon[i]); |
| 3870 | continue; |
| 3871 | } |
| 3872 | |
| 3873 | mask |= 1 << src; |
| 3874 | |
| 3875 | /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */ |
| 3876 | if (src <= data->temp_fixed_num) { |
| 3877 | if (data->have_temp & (1 << (src - 1))) |
| 3878 | continue; |
| 3879 | data->have_temp |= 1 << (src - 1); |
| 3880 | data->have_temp_fixed |= 1 << (src - 1); |
| 3881 | data->reg_temp[0][src - 1] = reg_temp_mon[i]; |
| 3882 | data->temp_src[src - 1] = src; |
| 3883 | continue; |
| 3884 | } |
| 3885 | |
| 3886 | if (s >= NUM_TEMP) |
| 3887 | continue; |
| 3888 | |
| 3889 | /* Use dynamic index for other sources */ |
| 3890 | data->have_temp |= 1 << s; |
| 3891 | data->reg_temp[0][s] = reg_temp_mon[i]; |
| 3892 | data->temp_src[s] = src; |
| 3893 | s++; |
| 3894 | } |
| 3895 | |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3896 | #ifdef USE_ALTERNATE |
| 3897 | /* |
| 3898 | * Go through the list of alternate temp registers and enable |
| 3899 | * if possible. |
| 3900 | * The temperature is already monitored if the respective bit in <mask> |
| 3901 | * is set. |
| 3902 | */ |
| 3903 | for (i = 0; i < data->temp_label_num - 1; i++) { |
| 3904 | if (!reg_temp_alternate[i]) |
| 3905 | continue; |
| 3906 | if (mask & (1 << (i + 1))) |
| 3907 | continue; |
| 3908 | if (i < data->temp_fixed_num) { |
| 3909 | if (data->have_temp & (1 << i)) |
| 3910 | continue; |
| 3911 | data->have_temp |= 1 << i; |
| 3912 | data->have_temp_fixed |= 1 << i; |
| 3913 | data->reg_temp[0][i] = reg_temp_alternate[i]; |
Guenter Roeck | 169c05c | 2013-05-09 10:40:01 -0700 | [diff] [blame] | 3914 | if (i < num_reg_temp) { |
| 3915 | data->reg_temp[1][i] = reg_temp_over[i]; |
| 3916 | data->reg_temp[2][i] = reg_temp_hyst[i]; |
| 3917 | } |
Guenter Roeck | aa136e5 | 2012-12-04 03:26:05 -0800 | [diff] [blame] | 3918 | data->temp_src[i] = i + 1; |
| 3919 | continue; |
| 3920 | } |
| 3921 | |
| 3922 | if (s >= NUM_TEMP) /* Abort if no more space */ |
| 3923 | break; |
| 3924 | |
| 3925 | data->have_temp |= 1 << s; |
| 3926 | data->reg_temp[0][s] = reg_temp_alternate[i]; |
| 3927 | data->temp_src[s] = i + 1; |
| 3928 | s++; |
| 3929 | } |
| 3930 | #endif /* USE_ALTERNATE */ |
| 3931 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3932 | /* Initialize the chip */ |
| 3933 | nct6775_init_device(data); |
| 3934 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3935 | err = superio_enter(sio_data->sioreg); |
| 3936 | if (err) |
| 3937 | return err; |
| 3938 | |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3939 | cr2a = superio_inb(sio_data->sioreg, 0x2a); |
| 3940 | switch (data->kind) { |
| 3941 | case nct6775: |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3942 | data->have_vid = (cr2a & 0x40); |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3943 | break; |
| 3944 | case nct6776: |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3945 | data->have_vid = (cr2a & 0x60) == 0x40; |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3946 | break; |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3947 | case nct6106: |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3948 | case nct6779: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3949 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 3950 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 3951 | case nct6793: |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3952 | break; |
| 3953 | } |
| 3954 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3955 | /* |
| 3956 | * Read VID value |
| 3957 | * We can get the VID input values directly at logical device D 0xe3. |
| 3958 | */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3959 | if (data->have_vid) { |
Guenter Roeck | 0fc1f8f | 2013-02-26 09:43:50 -0800 | [diff] [blame] | 3960 | superio_select(sio_data->sioreg, NCT6775_LD_VID); |
| 3961 | data->vid = superio_inb(sio_data->sioreg, 0xe3); |
| 3962 | data->vrm = vid_which_vrm(); |
| 3963 | } |
Guenter Roeck | 47ece96 | 2012-12-04 07:59:32 -0800 | [diff] [blame] | 3964 | |
| 3965 | if (fan_debounce) { |
| 3966 | u8 tmp; |
| 3967 | |
| 3968 | superio_select(sio_data->sioreg, NCT6775_LD_HWM); |
| 3969 | tmp = superio_inb(sio_data->sioreg, |
| 3970 | NCT6775_REG_CR_FAN_DEBOUNCE); |
| 3971 | switch (data->kind) { |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 3972 | case nct6106: |
| 3973 | tmp |= 0xe0; |
| 3974 | break; |
Guenter Roeck | 47ece96 | 2012-12-04 07:59:32 -0800 | [diff] [blame] | 3975 | case nct6775: |
| 3976 | tmp |= 0x1e; |
| 3977 | break; |
| 3978 | case nct6776: |
| 3979 | case nct6779: |
| 3980 | tmp |= 0x3e; |
| 3981 | break; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3982 | case nct6791: |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 3983 | case nct6792: |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 3984 | case nct6793: |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 3985 | tmp |= 0x7e; |
| 3986 | break; |
Guenter Roeck | 47ece96 | 2012-12-04 07:59:32 -0800 | [diff] [blame] | 3987 | } |
| 3988 | superio_outb(sio_data->sioreg, NCT6775_REG_CR_FAN_DEBOUNCE, |
| 3989 | tmp); |
| 3990 | dev_info(&pdev->dev, "Enabled fan debounce for chip %s\n", |
| 3991 | data->name); |
| 3992 | } |
| 3993 | |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 3994 | nct6775_check_fan_inputs(data); |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 3995 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 3996 | superio_exit(sio_data->sioreg); |
| 3997 | |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 3998 | /* Read fan clock dividers immediately */ |
| 3999 | nct6775_init_fan_common(dev, data); |
| 4000 | |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 4001 | /* Register sysfs hooks */ |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 4002 | group = nct6775_create_attr_group(dev, &nct6775_pwm_template_group, |
| 4003 | data->pwm_num); |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 4004 | if (IS_ERR(group)) |
| 4005 | return PTR_ERR(group); |
| 4006 | |
Axel Lin | 55bdee6 | 2014-07-24 08:59:34 +0800 | [diff] [blame] | 4007 | data->groups[num_attr_groups++] = group; |
Guenter Roeck | cdcaece | 2012-12-04 09:04:52 -0800 | [diff] [blame] | 4008 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 4009 | group = nct6775_create_attr_group(dev, &nct6775_in_template_group, |
| 4010 | fls(data->have_in)); |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 4011 | if (IS_ERR(group)) |
| 4012 | return PTR_ERR(group); |
| 4013 | |
Axel Lin | 55bdee6 | 2014-07-24 08:59:34 +0800 | [diff] [blame] | 4014 | data->groups[num_attr_groups++] = group; |
Guenter Roeck | 77eb5b3 | 2012-12-04 08:30:54 -0800 | [diff] [blame] | 4015 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 4016 | group = nct6775_create_attr_group(dev, &nct6775_fan_template_group, |
| 4017 | fls(data->has_fan)); |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 4018 | if (IS_ERR(group)) |
| 4019 | return PTR_ERR(group); |
| 4020 | |
Axel Lin | 55bdee6 | 2014-07-24 08:59:34 +0800 | [diff] [blame] | 4021 | data->groups[num_attr_groups++] = group; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4022 | |
Guenter Roeck | f73cf63 | 2013-03-18 09:22:50 -0700 | [diff] [blame] | 4023 | group = nct6775_create_attr_group(dev, &nct6775_temp_template_group, |
| 4024 | fls(data->have_temp)); |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 4025 | if (IS_ERR(group)) |
| 4026 | return PTR_ERR(group); |
Guenter Roeck | 1c65dc3 | 2012-12-04 07:56:24 -0800 | [diff] [blame] | 4027 | |
Axel Lin | 55bdee6 | 2014-07-24 08:59:34 +0800 | [diff] [blame] | 4028 | data->groups[num_attr_groups++] = group; |
| 4029 | data->groups[num_attr_groups++] = &nct6775_group_other; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4030 | |
Guenter Roeck | a150d95 | 2013-07-11 22:55:22 -0700 | [diff] [blame] | 4031 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name, |
| 4032 | data, data->groups); |
Fengguang Wu | 9c09bd8 | 2013-09-17 06:43:42 -0700 | [diff] [blame] | 4033 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4034 | } |
| 4035 | |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4036 | static void nct6791_enable_io_mapping(int sioaddr) |
| 4037 | { |
| 4038 | int val; |
| 4039 | |
| 4040 | val = superio_inb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE); |
| 4041 | if (val & 0x10) { |
| 4042 | pr_info("Enabling hardware monitor logical device mappings.\n"); |
| 4043 | superio_outb(sioaddr, NCT6791_REG_HM_IO_SPACE_LOCK_ENABLE, |
| 4044 | val & ~0x10); |
| 4045 | } |
| 4046 | } |
| 4047 | |
Guenter Roeck | 48e9318 | 2015-02-07 08:48:49 -0800 | [diff] [blame] | 4048 | static int __maybe_unused nct6775_suspend(struct device *dev) |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4049 | { |
| 4050 | struct nct6775_data *data = nct6775_update_device(dev); |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4051 | |
| 4052 | mutex_lock(&data->update_lock); |
| 4053 | data->vbat = nct6775_read_value(data, data->REG_VBAT); |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 4054 | if (data->kind == nct6775) { |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4055 | data->fandiv1 = nct6775_read_value(data, NCT6775_REG_FANDIV1); |
| 4056 | data->fandiv2 = nct6775_read_value(data, NCT6775_REG_FANDIV2); |
| 4057 | } |
| 4058 | mutex_unlock(&data->update_lock); |
| 4059 | |
| 4060 | return 0; |
| 4061 | } |
| 4062 | |
Guenter Roeck | 48e9318 | 2015-02-07 08:48:49 -0800 | [diff] [blame] | 4063 | static int __maybe_unused nct6775_resume(struct device *dev) |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4064 | { |
| 4065 | struct nct6775_data *data = dev_get_drvdata(dev); |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 4066 | int sioreg = data->sioreg; |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4067 | int i, j, err = 0; |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 4068 | u8 reg; |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4069 | |
| 4070 | mutex_lock(&data->update_lock); |
| 4071 | data->bank = 0xff; /* Force initial bank selection */ |
| 4072 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 4073 | err = superio_enter(sioreg); |
| 4074 | if (err) |
| 4075 | goto abort; |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4076 | |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 4077 | superio_select(sioreg, NCT6775_LD_HWM); |
| 4078 | reg = superio_inb(sioreg, SIO_REG_ENABLE); |
| 4079 | if (reg != data->sio_reg_enable) |
| 4080 | superio_outb(sioreg, SIO_REG_ENABLE, data->sio_reg_enable); |
| 4081 | |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 4082 | if (data->kind == nct6791 || data->kind == nct6792 || |
| 4083 | data->kind == nct6793) |
Guenter Roeck | d2a14ea | 2015-02-06 18:53:21 -0800 | [diff] [blame] | 4084 | nct6791_enable_io_mapping(sioreg); |
| 4085 | |
| 4086 | superio_exit(sioreg); |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4087 | |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4088 | /* Restore limits */ |
| 4089 | for (i = 0; i < data->in_num; i++) { |
| 4090 | if (!(data->have_in & (1 << i))) |
| 4091 | continue; |
| 4092 | |
| 4093 | nct6775_write_value(data, data->REG_IN_MINMAX[0][i], |
| 4094 | data->in[i][1]); |
| 4095 | nct6775_write_value(data, data->REG_IN_MINMAX[1][i], |
| 4096 | data->in[i][2]); |
| 4097 | } |
| 4098 | |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 4099 | for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) { |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4100 | if (!(data->has_fan_min & (1 << i))) |
| 4101 | continue; |
| 4102 | |
| 4103 | nct6775_write_value(data, data->REG_FAN_MIN[i], |
| 4104 | data->fan_min[i]); |
| 4105 | } |
| 4106 | |
| 4107 | for (i = 0; i < NUM_TEMP; i++) { |
| 4108 | if (!(data->have_temp & (1 << i))) |
| 4109 | continue; |
| 4110 | |
Guenter Roeck | c409fd4 | 2013-04-09 05:04:00 -0700 | [diff] [blame] | 4111 | for (j = 1; j < ARRAY_SIZE(data->reg_temp); j++) |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4112 | if (data->reg_temp[j][i]) |
| 4113 | nct6775_write_temp(data, data->reg_temp[j][i], |
| 4114 | data->temp[j][i]); |
| 4115 | } |
| 4116 | |
| 4117 | /* Restore other settings */ |
| 4118 | nct6775_write_value(data, data->REG_VBAT, data->vbat); |
Guenter Roeck | df612d5 | 2013-07-08 13:15:04 -0700 | [diff] [blame] | 4119 | if (data->kind == nct6775) { |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4120 | nct6775_write_value(data, NCT6775_REG_FANDIV1, data->fandiv1); |
| 4121 | nct6775_write_value(data, NCT6775_REG_FANDIV2, data->fandiv2); |
| 4122 | } |
| 4123 | |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4124 | abort: |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4125 | /* Force re-reading all values */ |
| 4126 | data->valid = false; |
| 4127 | mutex_unlock(&data->update_lock); |
| 4128 | |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4129 | return err; |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4130 | } |
| 4131 | |
Guenter Roeck | 48e9318 | 2015-02-07 08:48:49 -0800 | [diff] [blame] | 4132 | static SIMPLE_DEV_PM_OPS(nct6775_dev_pm_ops, nct6775_suspend, nct6775_resume); |
Guenter Roeck | 84d19d9 | 2012-12-04 08:01:39 -0800 | [diff] [blame] | 4133 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4134 | static struct platform_driver nct6775_driver = { |
| 4135 | .driver = { |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4136 | .name = DRVNAME, |
Guenter Roeck | 48e9318 | 2015-02-07 08:48:49 -0800 | [diff] [blame] | 4137 | .pm = &nct6775_dev_pm_ops, |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4138 | }, |
| 4139 | .probe = nct6775_probe, |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4140 | }; |
| 4141 | |
| 4142 | /* nct6775_find() looks for a '627 in the Super-I/O config space */ |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4143 | static int __init nct6775_find(int sioaddr, struct nct6775_sio_data *sio_data) |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4144 | { |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4145 | u16 val; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4146 | int err; |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4147 | int addr; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4148 | |
| 4149 | err = superio_enter(sioaddr); |
| 4150 | if (err) |
| 4151 | return err; |
| 4152 | |
| 4153 | if (force_id) |
| 4154 | val = force_id; |
| 4155 | else |
| 4156 | val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8) |
| 4157 | | superio_inb(sioaddr, SIO_REG_DEVID + 1); |
| 4158 | switch (val & SIO_ID_MASK) { |
Guenter Roeck | 6c00950 | 2012-07-01 08:23:15 -0700 | [diff] [blame] | 4159 | case SIO_NCT6106_ID: |
| 4160 | sio_data->kind = nct6106; |
| 4161 | break; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4162 | case SIO_NCT6775_ID: |
| 4163 | sio_data->kind = nct6775; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4164 | break; |
| 4165 | case SIO_NCT6776_ID: |
| 4166 | sio_data->kind = nct6776; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4167 | break; |
| 4168 | case SIO_NCT6779_ID: |
| 4169 | sio_data->kind = nct6779; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4170 | break; |
David Bartley | 578ab5f | 2013-06-24 22:28:28 -0700 | [diff] [blame] | 4171 | case SIO_NCT6791_ID: |
| 4172 | sio_data->kind = nct6791; |
| 4173 | break; |
Guenter Roeck | 8aefb93 | 2014-11-16 09:50:04 -0800 | [diff] [blame] | 4174 | case SIO_NCT6792_ID: |
| 4175 | sio_data->kind = nct6792; |
| 4176 | break; |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 4177 | case SIO_NCT6793_ID: |
| 4178 | sio_data->kind = nct6793; |
| 4179 | break; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4180 | default: |
| 4181 | if (val != 0xffff) |
| 4182 | pr_debug("unsupported chip ID: 0x%04x\n", val); |
| 4183 | superio_exit(sioaddr); |
| 4184 | return -ENODEV; |
| 4185 | } |
| 4186 | |
| 4187 | /* We have a known chip, find the HWM I/O address */ |
| 4188 | superio_select(sioaddr, NCT6775_LD_HWM); |
| 4189 | val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8) |
| 4190 | | superio_inb(sioaddr, SIO_REG_ADDR + 1); |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4191 | addr = val & IOREGION_ALIGNMENT; |
| 4192 | if (addr == 0) { |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4193 | pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n"); |
| 4194 | superio_exit(sioaddr); |
| 4195 | return -ENODEV; |
| 4196 | } |
| 4197 | |
| 4198 | /* Activate logical device if needed */ |
| 4199 | val = superio_inb(sioaddr, SIO_REG_ENABLE); |
| 4200 | if (!(val & 0x01)) { |
| 4201 | pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n"); |
| 4202 | superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01); |
| 4203 | } |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4204 | |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 4205 | if (sio_data->kind == nct6791 || sio_data->kind == nct6792 || |
| 4206 | sio_data->kind == nct6793) |
Guenter Roeck | f5776cc | 2013-12-25 07:25:59 -0800 | [diff] [blame] | 4207 | nct6791_enable_io_mapping(sioaddr); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4208 | |
| 4209 | superio_exit(sioaddr); |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4210 | pr_info("Found %s or compatible chip at %#x:%#x\n", |
| 4211 | nct6775_sio_names[sio_data->kind], sioaddr, addr); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4212 | sio_data->sioreg = sioaddr; |
| 4213 | |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4214 | return addr; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* |
| 4218 | * when Super-I/O functions move to a separate file, the Super-I/O |
| 4219 | * bus will manage the lifetime of the device and this module will only keep |
Guenter Roeck | 615fc8c | 2013-07-06 09:43:30 -0700 | [diff] [blame] | 4220 | * track of the nct6775 driver. But since we use platform_device_alloc(), we |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4221 | * must keep track of the device |
| 4222 | */ |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4223 | static struct platform_device *pdev[2]; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4224 | |
| 4225 | static int __init sensors_nct6775_init(void) |
| 4226 | { |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4227 | int i, err; |
| 4228 | bool found = false; |
| 4229 | int address; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4230 | struct resource res; |
| 4231 | struct nct6775_sio_data sio_data; |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4232 | int sioaddr[2] = { 0x2e, 0x4e }; |
| 4233 | |
| 4234 | err = platform_driver_register(&nct6775_driver); |
| 4235 | if (err) |
| 4236 | return err; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4237 | |
| 4238 | /* |
| 4239 | * initialize sio_data->kind and sio_data->sioreg. |
| 4240 | * |
| 4241 | * when Super-I/O functions move to a separate file, the Super-I/O |
| 4242 | * driver will probe 0x2e and 0x4e and auto-detect the presence of a |
| 4243 | * nct6775 hardware monitor, and call probe() |
| 4244 | */ |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4245 | for (i = 0; i < ARRAY_SIZE(pdev); i++) { |
| 4246 | address = nct6775_find(sioaddr[i], &sio_data); |
| 4247 | if (address <= 0) |
| 4248 | continue; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4249 | |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4250 | found = true; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4251 | |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4252 | pdev[i] = platform_device_alloc(DRVNAME, address); |
| 4253 | if (!pdev[i]) { |
| 4254 | err = -ENOMEM; |
Axel Lin | 9d311ed | 2014-05-24 23:21:23 +0800 | [diff] [blame] | 4255 | goto exit_device_unregister; |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | err = platform_device_add_data(pdev[i], &sio_data, |
| 4259 | sizeof(struct nct6775_sio_data)); |
| 4260 | if (err) |
| 4261 | goto exit_device_put; |
| 4262 | |
| 4263 | memset(&res, 0, sizeof(res)); |
| 4264 | res.name = DRVNAME; |
| 4265 | res.start = address + IOREGION_OFFSET; |
| 4266 | res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1; |
| 4267 | res.flags = IORESOURCE_IO; |
| 4268 | |
| 4269 | err = acpi_check_resource_conflict(&res); |
| 4270 | if (err) { |
| 4271 | platform_device_put(pdev[i]); |
| 4272 | pdev[i] = NULL; |
| 4273 | continue; |
| 4274 | } |
| 4275 | |
| 4276 | err = platform_device_add_resources(pdev[i], &res, 1); |
| 4277 | if (err) |
| 4278 | goto exit_device_put; |
| 4279 | |
| 4280 | /* platform_device_add calls probe() */ |
| 4281 | err = platform_device_add(pdev[i]); |
| 4282 | if (err) |
| 4283 | goto exit_device_put; |
| 4284 | } |
| 4285 | if (!found) { |
| 4286 | err = -ENODEV; |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4287 | goto exit_unregister; |
| 4288 | } |
| 4289 | |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4290 | return 0; |
| 4291 | |
| 4292 | exit_device_put: |
Axel Lin | 9d311ed | 2014-05-24 23:21:23 +0800 | [diff] [blame] | 4293 | platform_device_put(pdev[i]); |
| 4294 | exit_device_unregister: |
| 4295 | while (--i >= 0) { |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4296 | if (pdev[i]) |
Axel Lin | 9d311ed | 2014-05-24 23:21:23 +0800 | [diff] [blame] | 4297 | platform_device_unregister(pdev[i]); |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4298 | } |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4299 | exit_unregister: |
| 4300 | platform_driver_unregister(&nct6775_driver); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4301 | return err; |
| 4302 | } |
| 4303 | |
| 4304 | static void __exit sensors_nct6775_exit(void) |
| 4305 | { |
Guenter Roeck | 698a7c2 | 2013-04-05 07:35:25 -0700 | [diff] [blame] | 4306 | int i; |
| 4307 | |
| 4308 | for (i = 0; i < ARRAY_SIZE(pdev); i++) { |
| 4309 | if (pdev[i]) |
| 4310 | platform_device_unregister(pdev[i]); |
| 4311 | } |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4312 | platform_driver_unregister(&nct6775_driver); |
| 4313 | } |
| 4314 | |
| 4315 | MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); |
Guenter Roeck | cd1faefa | 2015-08-30 19:45:19 -0700 | [diff] [blame] | 4316 | MODULE_DESCRIPTION("Driver for NCT6775F and compatible chips"); |
Guenter Roeck | 9de2e2e | 2012-05-20 19:29:48 -0700 | [diff] [blame] | 4317 | MODULE_LICENSE("GPL"); |
| 4318 | |
| 4319 | module_init(sensors_nct6775_init); |
| 4320 | module_exit(sensors_nct6775_exit); |