Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * pc87427.c - hardware monitoring driver for the |
| 3 | * National Semiconductor PC87427 Super-I/O chip |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006, 2008, 2010 Jean Delvare <khali@linux-fr.org> |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * Supports the following chips: |
| 16 | * |
| 17 | * Chip #vin #fan #pwm #temp devid |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 18 | * PC87427 - 8 4 6 0xF2 |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 19 | * |
| 20 | * This driver assumes that no more than one chip is present. |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 21 | * Only fans are fully supported so far. Temperatures are in read-only |
| 22 | * mode, and voltages aren't supported at all. |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 23 | */ |
| 24 | |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 25 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 26 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/hwmon.h> |
| 33 | #include <linux/hwmon-sysfs.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/mutex.h> |
| 36 | #include <linux/sysfs.h> |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 37 | #include <linux/ioport.h> |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 38 | #include <linux/acpi.h> |
H Hartley Sweeten | 6055fae | 2009-09-15 17:18:13 +0200 | [diff] [blame] | 39 | #include <linux/io.h> |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 40 | |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 41 | static unsigned short force_id; |
| 42 | module_param(force_id, ushort, 0); |
| 43 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 44 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 45 | static struct platform_device *pdev; |
| 46 | |
| 47 | #define DRVNAME "pc87427" |
| 48 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 49 | /* |
| 50 | * The lock mutex protects both the I/O accesses (needed because the |
| 51 | * device is using banked registers) and the register cache (needed to keep |
| 52 | * the data in the registers and the cache in sync at any time). |
| 53 | */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 54 | struct pc87427_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 55 | struct device *hwmon_dev; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 56 | struct mutex lock; |
| 57 | int address[2]; |
| 58 | const char *name; |
| 59 | |
| 60 | unsigned long last_updated; /* in jiffies */ |
| 61 | u8 fan_enabled; /* bit vector */ |
| 62 | u16 fan[8]; /* register values */ |
| 63 | u16 fan_min[8]; /* register values */ |
| 64 | u8 fan_status[8]; /* register values */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 65 | |
| 66 | u8 pwm_enabled; /* bit vector */ |
| 67 | u8 pwm_auto_ok; /* bit vector */ |
| 68 | u8 pwm_enable[4]; /* register values */ |
| 69 | u8 pwm[4]; /* register values */ |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 70 | |
| 71 | u8 temp_enabled; /* bit vector */ |
| 72 | s16 temp[6]; /* register values */ |
| 73 | s8 temp_min[6]; /* register values */ |
| 74 | s8 temp_max[6]; /* register values */ |
| 75 | s8 temp_crit[6]; /* register values */ |
| 76 | u8 temp_status[6]; /* register values */ |
| 77 | u8 temp_type[6]; /* register values */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 80 | struct pc87427_sio_data { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 81 | unsigned short address[2]; |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 82 | u8 has_fanin; |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 83 | u8 has_fanout; |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 86 | /* |
| 87 | * Super-I/O registers and operations |
| 88 | */ |
| 89 | |
| 90 | #define SIOREG_LDSEL 0x07 /* Logical device select */ |
| 91 | #define SIOREG_DEVID 0x20 /* Device ID */ |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 92 | #define SIOREG_CF2 0x22 /* Configuration 2 */ |
| 93 | #define SIOREG_CF3 0x23 /* Configuration 3 */ |
| 94 | #define SIOREG_CF4 0x24 /* Configuration 4 */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 95 | #define SIOREG_CF5 0x25 /* Configuration 5 */ |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 96 | #define SIOREG_CFB 0x2B /* Configuration B */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 97 | #define SIOREG_CFC 0x2C /* Configuration C */ |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 98 | #define SIOREG_CFD 0x2D /* Configuration D */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 99 | #define SIOREG_ACT 0x30 /* Device activation */ |
| 100 | #define SIOREG_MAP 0x50 /* I/O or memory mapping */ |
| 101 | #define SIOREG_IOBASE 0x60 /* I/O base address */ |
| 102 | |
| 103 | static const u8 logdev[2] = { 0x09, 0x14 }; |
| 104 | static const char *logdev_str[2] = { DRVNAME " FMC", DRVNAME " HMC" }; |
| 105 | #define LD_FAN 0 |
| 106 | #define LD_IN 1 |
| 107 | #define LD_TEMP 1 |
| 108 | |
| 109 | static inline void superio_outb(int sioaddr, int reg, int val) |
| 110 | { |
| 111 | outb(reg, sioaddr); |
| 112 | outb(val, sioaddr + 1); |
| 113 | } |
| 114 | |
| 115 | static inline int superio_inb(int sioaddr, int reg) |
| 116 | { |
| 117 | outb(reg, sioaddr); |
| 118 | return inb(sioaddr + 1); |
| 119 | } |
| 120 | |
| 121 | static inline void superio_exit(int sioaddr) |
| 122 | { |
| 123 | outb(0x02, sioaddr); |
| 124 | outb(0x02, sioaddr + 1); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Logical devices |
| 129 | */ |
| 130 | |
| 131 | #define REGION_LENGTH 32 |
| 132 | #define PC87427_REG_BANK 0x0f |
| 133 | #define BANK_FM(nr) (nr) |
| 134 | #define BANK_FT(nr) (0x08 + (nr)) |
| 135 | #define BANK_FC(nr) (0x10 + (nr) * 2) |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 136 | #define BANK_TM(nr) (nr) |
| 137 | #define BANK_VM(nr) (0x08 + (nr)) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * I/O access functions |
| 141 | */ |
| 142 | |
| 143 | /* ldi is the logical device index */ |
| 144 | static inline int pc87427_read8(struct pc87427_data *data, u8 ldi, u8 reg) |
| 145 | { |
| 146 | return inb(data->address[ldi] + reg); |
| 147 | } |
| 148 | |
| 149 | /* Must be called with data->lock held, except during init */ |
| 150 | static inline int pc87427_read8_bank(struct pc87427_data *data, u8 ldi, |
| 151 | u8 bank, u8 reg) |
| 152 | { |
| 153 | outb(bank, data->address[ldi] + PC87427_REG_BANK); |
| 154 | return inb(data->address[ldi] + reg); |
| 155 | } |
| 156 | |
| 157 | /* Must be called with data->lock held, except during init */ |
| 158 | static inline void pc87427_write8_bank(struct pc87427_data *data, u8 ldi, |
| 159 | u8 bank, u8 reg, u8 value) |
| 160 | { |
| 161 | outb(bank, data->address[ldi] + PC87427_REG_BANK); |
| 162 | outb(value, data->address[ldi] + reg); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Fan registers and conversions |
| 167 | */ |
| 168 | |
| 169 | /* fan data registers are 16-bit wide */ |
| 170 | #define PC87427_REG_FAN 0x12 |
| 171 | #define PC87427_REG_FAN_MIN 0x14 |
| 172 | #define PC87427_REG_FAN_STATUS 0x10 |
| 173 | |
| 174 | #define FAN_STATUS_STALL (1 << 3) |
| 175 | #define FAN_STATUS_LOSPD (1 << 1) |
| 176 | #define FAN_STATUS_MONEN (1 << 0) |
| 177 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 178 | /* |
| 179 | * Dedicated function to read all registers related to a given fan input. |
| 180 | * This saves us quite a few locks and bank selections. |
| 181 | * Must be called with data->lock held. |
| 182 | * nr is from 0 to 7 |
| 183 | */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 184 | static void pc87427_readall_fan(struct pc87427_data *data, u8 nr) |
| 185 | { |
| 186 | int iobase = data->address[LD_FAN]; |
| 187 | |
| 188 | outb(BANK_FM(nr), iobase + PC87427_REG_BANK); |
| 189 | data->fan[nr] = inw(iobase + PC87427_REG_FAN); |
| 190 | data->fan_min[nr] = inw(iobase + PC87427_REG_FAN_MIN); |
| 191 | data->fan_status[nr] = inb(iobase + PC87427_REG_FAN_STATUS); |
| 192 | /* Clear fan alarm bits */ |
| 193 | outb(data->fan_status[nr], iobase + PC87427_REG_FAN_STATUS); |
| 194 | } |
| 195 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 196 | /* |
| 197 | * The 2 LSB of fan speed registers are used for something different. |
| 198 | * The actual 2 LSB of the measurements are not available. |
| 199 | */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 200 | static inline unsigned long fan_from_reg(u16 reg) |
| 201 | { |
| 202 | reg &= 0xfffc; |
| 203 | if (reg == 0x0000 || reg == 0xfffc) |
| 204 | return 0; |
| 205 | return 5400000UL / reg; |
| 206 | } |
| 207 | |
| 208 | /* The 2 LSB of the fan speed limit registers are not significant. */ |
| 209 | static inline u16 fan_to_reg(unsigned long val) |
| 210 | { |
| 211 | if (val < 83UL) |
| 212 | return 0xffff; |
| 213 | if (val >= 1350000UL) |
| 214 | return 0x0004; |
| 215 | return ((1350000UL + val / 2) / val) << 2; |
| 216 | } |
| 217 | |
| 218 | /* |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 219 | * PWM registers and conversions |
| 220 | */ |
| 221 | |
| 222 | #define PC87427_REG_PWM_ENABLE 0x10 |
| 223 | #define PC87427_REG_PWM_DUTY 0x12 |
| 224 | |
| 225 | #define PWM_ENABLE_MODE_MASK (7 << 4) |
| 226 | #define PWM_ENABLE_CTLEN (1 << 0) |
| 227 | |
| 228 | #define PWM_MODE_MANUAL (0 << 4) |
| 229 | #define PWM_MODE_AUTO (1 << 4) |
| 230 | #define PWM_MODE_OFF (2 << 4) |
| 231 | #define PWM_MODE_ON (7 << 4) |
| 232 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 233 | /* |
| 234 | * Dedicated function to read all registers related to a given PWM output. |
| 235 | * This saves us quite a few locks and bank selections. |
| 236 | * Must be called with data->lock held. |
| 237 | * nr is from 0 to 3 |
| 238 | */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 239 | static void pc87427_readall_pwm(struct pc87427_data *data, u8 nr) |
| 240 | { |
| 241 | int iobase = data->address[LD_FAN]; |
| 242 | |
| 243 | outb(BANK_FC(nr), iobase + PC87427_REG_BANK); |
| 244 | data->pwm_enable[nr] = inb(iobase + PC87427_REG_PWM_ENABLE); |
| 245 | data->pwm[nr] = inb(iobase + PC87427_REG_PWM_DUTY); |
| 246 | } |
| 247 | |
| 248 | static inline int pwm_enable_from_reg(u8 reg) |
| 249 | { |
| 250 | switch (reg & PWM_ENABLE_MODE_MASK) { |
| 251 | case PWM_MODE_ON: |
| 252 | return 0; |
| 253 | case PWM_MODE_MANUAL: |
| 254 | case PWM_MODE_OFF: |
| 255 | return 1; |
| 256 | case PWM_MODE_AUTO: |
| 257 | return 2; |
| 258 | default: |
| 259 | return -EPROTO; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static inline u8 pwm_enable_to_reg(unsigned long val, u8 pwmval) |
| 264 | { |
| 265 | switch (val) { |
| 266 | default: |
| 267 | return PWM_MODE_ON; |
| 268 | case 1: |
| 269 | return pwmval ? PWM_MODE_MANUAL : PWM_MODE_OFF; |
| 270 | case 2: |
| 271 | return PWM_MODE_AUTO; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 276 | * Temperature registers and conversions |
| 277 | */ |
| 278 | |
| 279 | #define PC87427_REG_TEMP_STATUS 0x10 |
| 280 | #define PC87427_REG_TEMP 0x14 |
| 281 | #define PC87427_REG_TEMP_MAX 0x18 |
| 282 | #define PC87427_REG_TEMP_MIN 0x19 |
| 283 | #define PC87427_REG_TEMP_CRIT 0x1a |
| 284 | #define PC87427_REG_TEMP_TYPE 0x1d |
| 285 | |
| 286 | #define TEMP_STATUS_CHANEN (1 << 0) |
| 287 | #define TEMP_STATUS_LOWFLG (1 << 1) |
| 288 | #define TEMP_STATUS_HIGHFLG (1 << 2) |
| 289 | #define TEMP_STATUS_CRITFLG (1 << 3) |
| 290 | #define TEMP_STATUS_SENSERR (1 << 5) |
| 291 | #define TEMP_TYPE_MASK (3 << 5) |
| 292 | |
| 293 | #define TEMP_TYPE_THERMISTOR (1 << 5) |
| 294 | #define TEMP_TYPE_REMOTE_DIODE (2 << 5) |
| 295 | #define TEMP_TYPE_LOCAL_DIODE (3 << 5) |
| 296 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 297 | /* |
| 298 | * Dedicated function to read all registers related to a given temperature |
| 299 | * input. This saves us quite a few locks and bank selections. |
| 300 | * Must be called with data->lock held. |
| 301 | * nr is from 0 to 5 |
| 302 | */ |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 303 | static void pc87427_readall_temp(struct pc87427_data *data, u8 nr) |
| 304 | { |
| 305 | int iobase = data->address[LD_TEMP]; |
| 306 | |
| 307 | outb(BANK_TM(nr), iobase + PC87427_REG_BANK); |
| 308 | data->temp[nr] = le16_to_cpu(inw(iobase + PC87427_REG_TEMP)); |
| 309 | data->temp_max[nr] = inb(iobase + PC87427_REG_TEMP_MAX); |
| 310 | data->temp_min[nr] = inb(iobase + PC87427_REG_TEMP_MIN); |
| 311 | data->temp_crit[nr] = inb(iobase + PC87427_REG_TEMP_CRIT); |
| 312 | data->temp_type[nr] = inb(iobase + PC87427_REG_TEMP_TYPE); |
| 313 | data->temp_status[nr] = inb(iobase + PC87427_REG_TEMP_STATUS); |
| 314 | /* Clear fan alarm bits */ |
| 315 | outb(data->temp_status[nr], iobase + PC87427_REG_TEMP_STATUS); |
| 316 | } |
| 317 | |
| 318 | static inline unsigned int temp_type_from_reg(u8 reg) |
| 319 | { |
| 320 | switch (reg & TEMP_TYPE_MASK) { |
| 321 | case TEMP_TYPE_THERMISTOR: |
| 322 | return 4; |
| 323 | case TEMP_TYPE_REMOTE_DIODE: |
| 324 | case TEMP_TYPE_LOCAL_DIODE: |
| 325 | return 3; |
| 326 | default: |
| 327 | return 0; |
| 328 | } |
| 329 | } |
| 330 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 331 | /* |
| 332 | * We assume 8-bit thermal sensors; 9-bit thermal sensors are possible |
| 333 | * too, but I have no idea how to figure out when they are used. |
| 334 | */ |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 335 | static inline long temp_from_reg(s16 reg) |
| 336 | { |
| 337 | return reg * 1000 / 256; |
| 338 | } |
| 339 | |
| 340 | static inline long temp_from_reg8(s8 reg) |
| 341 | { |
| 342 | return reg * 1000; |
| 343 | } |
| 344 | |
| 345 | /* |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 346 | * Data interface |
| 347 | */ |
| 348 | |
| 349 | static struct pc87427_data *pc87427_update_device(struct device *dev) |
| 350 | { |
| 351 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 352 | int i; |
| 353 | |
| 354 | mutex_lock(&data->lock); |
| 355 | if (!time_after(jiffies, data->last_updated + HZ) |
| 356 | && data->last_updated) |
| 357 | goto done; |
| 358 | |
| 359 | /* Fans */ |
| 360 | for (i = 0; i < 8; i++) { |
| 361 | if (!(data->fan_enabled & (1 << i))) |
| 362 | continue; |
| 363 | pc87427_readall_fan(data, i); |
| 364 | } |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 365 | |
| 366 | /* PWM outputs */ |
| 367 | for (i = 0; i < 4; i++) { |
| 368 | if (!(data->pwm_enabled & (1 << i))) |
| 369 | continue; |
| 370 | pc87427_readall_pwm(data, i); |
| 371 | } |
| 372 | |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 373 | /* Temperature channels */ |
| 374 | for (i = 0; i < 6; i++) { |
| 375 | if (!(data->temp_enabled & (1 << i))) |
| 376 | continue; |
| 377 | pc87427_readall_temp(data, i); |
| 378 | } |
| 379 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 380 | data->last_updated = jiffies; |
| 381 | |
| 382 | done: |
| 383 | mutex_unlock(&data->lock); |
| 384 | return data; |
| 385 | } |
| 386 | |
| 387 | static ssize_t show_fan_input(struct device *dev, struct device_attribute |
| 388 | *devattr, char *buf) |
| 389 | { |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 390 | struct pc87427_data *data = pc87427_update_device(dev); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 391 | int nr = to_sensor_dev_attr(devattr)->index; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 392 | |
| 393 | return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr])); |
| 394 | } |
| 395 | |
| 396 | static ssize_t show_fan_min(struct device *dev, struct device_attribute |
| 397 | *devattr, char *buf) |
| 398 | { |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 399 | struct pc87427_data *data = pc87427_update_device(dev); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 400 | int nr = to_sensor_dev_attr(devattr)->index; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 401 | |
| 402 | return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr])); |
| 403 | } |
| 404 | |
| 405 | static ssize_t show_fan_alarm(struct device *dev, struct device_attribute |
| 406 | *devattr, char *buf) |
| 407 | { |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 408 | struct pc87427_data *data = pc87427_update_device(dev); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 409 | int nr = to_sensor_dev_attr(devattr)->index; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 410 | |
| 411 | return sprintf(buf, "%d\n", !!(data->fan_status[nr] |
| 412 | & FAN_STATUS_LOSPD)); |
| 413 | } |
| 414 | |
| 415 | static ssize_t show_fan_fault(struct device *dev, struct device_attribute |
| 416 | *devattr, char *buf) |
| 417 | { |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 418 | struct pc87427_data *data = pc87427_update_device(dev); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 419 | int nr = to_sensor_dev_attr(devattr)->index; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 420 | |
| 421 | return sprintf(buf, "%d\n", !!(data->fan_status[nr] |
| 422 | & FAN_STATUS_STALL)); |
| 423 | } |
| 424 | |
| 425 | static ssize_t set_fan_min(struct device *dev, struct device_attribute |
| 426 | *devattr, const char *buf, size_t count) |
| 427 | { |
| 428 | struct pc87427_data *data = dev_get_drvdata(dev); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 429 | int nr = to_sensor_dev_attr(devattr)->index; |
| 430 | unsigned long val; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 431 | int iobase = data->address[LD_FAN]; |
| 432 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 433 | if (kstrtoul(buf, 10, &val) < 0) |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 434 | return -EINVAL; |
| 435 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 436 | mutex_lock(&data->lock); |
| 437 | outb(BANK_FM(nr), iobase + PC87427_REG_BANK); |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 438 | /* |
| 439 | * The low speed limit registers are read-only while monitoring |
| 440 | * is enabled, so we have to disable monitoring, then change the |
| 441 | * limit, and finally enable monitoring again. |
| 442 | */ |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 443 | outb(0, iobase + PC87427_REG_FAN_STATUS); |
| 444 | data->fan_min[nr] = fan_to_reg(val); |
| 445 | outw(data->fan_min[nr], iobase + PC87427_REG_FAN_MIN); |
| 446 | outb(FAN_STATUS_MONEN, iobase + PC87427_REG_FAN_STATUS); |
| 447 | mutex_unlock(&data->lock); |
| 448 | |
| 449 | return count; |
| 450 | } |
| 451 | |
| 452 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); |
| 453 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); |
| 454 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2); |
| 455 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3); |
| 456 | static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_input, NULL, 4); |
| 457 | static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_input, NULL, 5); |
| 458 | static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_input, NULL, 6); |
| 459 | static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_input, NULL, 7); |
| 460 | |
| 461 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, |
| 462 | show_fan_min, set_fan_min, 0); |
| 463 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, |
| 464 | show_fan_min, set_fan_min, 1); |
| 465 | static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, |
| 466 | show_fan_min, set_fan_min, 2); |
| 467 | static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, |
| 468 | show_fan_min, set_fan_min, 3); |
| 469 | static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO, |
| 470 | show_fan_min, set_fan_min, 4); |
| 471 | static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO, |
| 472 | show_fan_min, set_fan_min, 5); |
| 473 | static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO, |
| 474 | show_fan_min, set_fan_min, 6); |
| 475 | static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO, |
| 476 | show_fan_min, set_fan_min, 7); |
| 477 | |
| 478 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0); |
| 479 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1); |
| 480 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2); |
| 481 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3); |
| 482 | static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_fan_alarm, NULL, 4); |
| 483 | static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_fan_alarm, NULL, 5); |
| 484 | static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_fan_alarm, NULL, 6); |
| 485 | static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_fan_alarm, NULL, 7); |
| 486 | |
| 487 | static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0); |
| 488 | static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1); |
| 489 | static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2); |
| 490 | static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3); |
| 491 | static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, show_fan_fault, NULL, 4); |
| 492 | static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, show_fan_fault, NULL, 5); |
| 493 | static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, show_fan_fault, NULL, 6); |
| 494 | static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, show_fan_fault, NULL, 7); |
| 495 | |
| 496 | static struct attribute *pc87427_attributes_fan[8][5] = { |
| 497 | { |
| 498 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 499 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 500 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 501 | &sensor_dev_attr_fan1_fault.dev_attr.attr, |
| 502 | NULL |
| 503 | }, { |
| 504 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 505 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 506 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 507 | &sensor_dev_attr_fan2_fault.dev_attr.attr, |
| 508 | NULL |
| 509 | }, { |
| 510 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 511 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 512 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 513 | &sensor_dev_attr_fan3_fault.dev_attr.attr, |
| 514 | NULL |
| 515 | }, { |
| 516 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 517 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 518 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 519 | &sensor_dev_attr_fan4_fault.dev_attr.attr, |
| 520 | NULL |
| 521 | }, { |
| 522 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
| 523 | &sensor_dev_attr_fan5_min.dev_attr.attr, |
| 524 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, |
| 525 | &sensor_dev_attr_fan5_fault.dev_attr.attr, |
| 526 | NULL |
| 527 | }, { |
| 528 | &sensor_dev_attr_fan6_input.dev_attr.attr, |
| 529 | &sensor_dev_attr_fan6_min.dev_attr.attr, |
| 530 | &sensor_dev_attr_fan6_alarm.dev_attr.attr, |
| 531 | &sensor_dev_attr_fan6_fault.dev_attr.attr, |
| 532 | NULL |
| 533 | }, { |
| 534 | &sensor_dev_attr_fan7_input.dev_attr.attr, |
| 535 | &sensor_dev_attr_fan7_min.dev_attr.attr, |
| 536 | &sensor_dev_attr_fan7_alarm.dev_attr.attr, |
| 537 | &sensor_dev_attr_fan7_fault.dev_attr.attr, |
| 538 | NULL |
| 539 | }, { |
| 540 | &sensor_dev_attr_fan8_input.dev_attr.attr, |
| 541 | &sensor_dev_attr_fan8_min.dev_attr.attr, |
| 542 | &sensor_dev_attr_fan8_alarm.dev_attr.attr, |
| 543 | &sensor_dev_attr_fan8_fault.dev_attr.attr, |
| 544 | NULL |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | static const struct attribute_group pc87427_group_fan[8] = { |
| 549 | { .attrs = pc87427_attributes_fan[0] }, |
| 550 | { .attrs = pc87427_attributes_fan[1] }, |
| 551 | { .attrs = pc87427_attributes_fan[2] }, |
| 552 | { .attrs = pc87427_attributes_fan[3] }, |
| 553 | { .attrs = pc87427_attributes_fan[4] }, |
| 554 | { .attrs = pc87427_attributes_fan[5] }, |
| 555 | { .attrs = pc87427_attributes_fan[6] }, |
| 556 | { .attrs = pc87427_attributes_fan[7] }, |
| 557 | }; |
| 558 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 559 | /* |
| 560 | * Must be called with data->lock held and pc87427_readall_pwm() freshly |
| 561 | * called |
| 562 | */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 563 | static void update_pwm_enable(struct pc87427_data *data, int nr, u8 mode) |
| 564 | { |
| 565 | int iobase = data->address[LD_FAN]; |
| 566 | data->pwm_enable[nr] &= ~PWM_ENABLE_MODE_MASK; |
| 567 | data->pwm_enable[nr] |= mode; |
| 568 | outb(data->pwm_enable[nr], iobase + PC87427_REG_PWM_ENABLE); |
| 569 | } |
| 570 | |
| 571 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute |
| 572 | *devattr, char *buf) |
| 573 | { |
| 574 | struct pc87427_data *data = pc87427_update_device(dev); |
| 575 | int nr = to_sensor_dev_attr(devattr)->index; |
| 576 | int pwm_enable; |
| 577 | |
| 578 | pwm_enable = pwm_enable_from_reg(data->pwm_enable[nr]); |
| 579 | if (pwm_enable < 0) |
| 580 | return pwm_enable; |
| 581 | return sprintf(buf, "%d\n", pwm_enable); |
| 582 | } |
| 583 | |
| 584 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute |
| 585 | *devattr, const char *buf, size_t count) |
| 586 | { |
| 587 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 588 | int nr = to_sensor_dev_attr(devattr)->index; |
| 589 | unsigned long val; |
| 590 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 591 | if (kstrtoul(buf, 10, &val) < 0 || val > 2) |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 592 | return -EINVAL; |
| 593 | /* Can't go to automatic mode if it isn't configured */ |
| 594 | if (val == 2 && !(data->pwm_auto_ok & (1 << nr))) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | mutex_lock(&data->lock); |
| 598 | pc87427_readall_pwm(data, nr); |
| 599 | update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->pwm[nr])); |
| 600 | mutex_unlock(&data->lock); |
| 601 | |
| 602 | return count; |
| 603 | } |
| 604 | |
| 605 | static ssize_t show_pwm(struct device *dev, struct device_attribute |
| 606 | *devattr, char *buf) |
| 607 | { |
| 608 | struct pc87427_data *data = pc87427_update_device(dev); |
| 609 | int nr = to_sensor_dev_attr(devattr)->index; |
| 610 | |
| 611 | return sprintf(buf, "%d\n", (int)data->pwm[nr]); |
| 612 | } |
| 613 | |
| 614 | static ssize_t set_pwm(struct device *dev, struct device_attribute |
| 615 | *devattr, const char *buf, size_t count) |
| 616 | { |
| 617 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 618 | int nr = to_sensor_dev_attr(devattr)->index; |
| 619 | unsigned long val; |
| 620 | int iobase = data->address[LD_FAN]; |
| 621 | u8 mode; |
| 622 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 623 | if (kstrtoul(buf, 10, &val) < 0 || val > 0xff) |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 624 | return -EINVAL; |
| 625 | |
| 626 | mutex_lock(&data->lock); |
| 627 | pc87427_readall_pwm(data, nr); |
| 628 | mode = data->pwm_enable[nr] & PWM_ENABLE_MODE_MASK; |
| 629 | if (mode != PWM_MODE_MANUAL && mode != PWM_MODE_OFF) { |
| 630 | dev_notice(dev, "Can't set PWM%d duty cycle while not in " |
| 631 | "manual mode\n", nr + 1); |
| 632 | mutex_unlock(&data->lock); |
| 633 | return -EPERM; |
| 634 | } |
| 635 | |
| 636 | /* We may have to change the mode */ |
| 637 | if (mode == PWM_MODE_MANUAL && val == 0) { |
| 638 | /* Transition from Manual to Off */ |
| 639 | update_pwm_enable(data, nr, PWM_MODE_OFF); |
| 640 | mode = PWM_MODE_OFF; |
| 641 | dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1, |
| 642 | "manual", "off"); |
| 643 | } else if (mode == PWM_MODE_OFF && val != 0) { |
| 644 | /* Transition from Off to Manual */ |
| 645 | update_pwm_enable(data, nr, PWM_MODE_MANUAL); |
| 646 | mode = PWM_MODE_MANUAL; |
| 647 | dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1, |
| 648 | "off", "manual"); |
| 649 | } |
| 650 | |
| 651 | data->pwm[nr] = val; |
| 652 | if (mode == PWM_MODE_MANUAL) |
| 653 | outb(val, iobase + PC87427_REG_PWM_DUTY); |
| 654 | mutex_unlock(&data->lock); |
| 655 | |
| 656 | return count; |
| 657 | } |
| 658 | |
| 659 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, |
| 660 | show_pwm_enable, set_pwm_enable, 0); |
| 661 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, |
| 662 | show_pwm_enable, set_pwm_enable, 1); |
| 663 | static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, |
| 664 | show_pwm_enable, set_pwm_enable, 2); |
| 665 | static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, |
| 666 | show_pwm_enable, set_pwm_enable, 3); |
| 667 | |
| 668 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0); |
| 669 | static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1); |
| 670 | static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2); |
| 671 | static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3); |
| 672 | |
| 673 | static struct attribute *pc87427_attributes_pwm[4][3] = { |
| 674 | { |
| 675 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 676 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 677 | NULL |
| 678 | }, { |
| 679 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 680 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 681 | NULL |
| 682 | }, { |
| 683 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 684 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 685 | NULL |
| 686 | }, { |
| 687 | &sensor_dev_attr_pwm4_enable.dev_attr.attr, |
| 688 | &sensor_dev_attr_pwm4.dev_attr.attr, |
| 689 | NULL |
| 690 | } |
| 691 | }; |
| 692 | |
| 693 | static const struct attribute_group pc87427_group_pwm[4] = { |
| 694 | { .attrs = pc87427_attributes_pwm[0] }, |
| 695 | { .attrs = pc87427_attributes_pwm[1] }, |
| 696 | { .attrs = pc87427_attributes_pwm[2] }, |
| 697 | { .attrs = pc87427_attributes_pwm[3] }, |
| 698 | }; |
| 699 | |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 700 | static ssize_t show_temp_input(struct device *dev, struct device_attribute |
| 701 | *devattr, char *buf) |
| 702 | { |
| 703 | struct pc87427_data *data = pc87427_update_device(dev); |
| 704 | int nr = to_sensor_dev_attr(devattr)->index; |
| 705 | |
| 706 | return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr])); |
| 707 | } |
| 708 | |
| 709 | static ssize_t show_temp_min(struct device *dev, struct device_attribute |
| 710 | *devattr, char *buf) |
| 711 | { |
| 712 | struct pc87427_data *data = pc87427_update_device(dev); |
| 713 | int nr = to_sensor_dev_attr(devattr)->index; |
| 714 | |
| 715 | return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_min[nr])); |
| 716 | } |
| 717 | |
| 718 | static ssize_t show_temp_max(struct device *dev, struct device_attribute |
| 719 | *devattr, char *buf) |
| 720 | { |
| 721 | struct pc87427_data *data = pc87427_update_device(dev); |
| 722 | int nr = to_sensor_dev_attr(devattr)->index; |
| 723 | |
| 724 | return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_max[nr])); |
| 725 | } |
| 726 | |
| 727 | static ssize_t show_temp_crit(struct device *dev, struct device_attribute |
| 728 | *devattr, char *buf) |
| 729 | { |
| 730 | struct pc87427_data *data = pc87427_update_device(dev); |
| 731 | int nr = to_sensor_dev_attr(devattr)->index; |
| 732 | |
| 733 | return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_crit[nr])); |
| 734 | } |
| 735 | |
| 736 | static ssize_t show_temp_type(struct device *dev, struct device_attribute |
| 737 | *devattr, char *buf) |
| 738 | { |
| 739 | struct pc87427_data *data = pc87427_update_device(dev); |
| 740 | int nr = to_sensor_dev_attr(devattr)->index; |
| 741 | |
| 742 | return sprintf(buf, "%u\n", temp_type_from_reg(data->temp_type[nr])); |
| 743 | } |
| 744 | |
| 745 | static ssize_t show_temp_min_alarm(struct device *dev, struct device_attribute |
| 746 | *devattr, char *buf) |
| 747 | { |
| 748 | struct pc87427_data *data = pc87427_update_device(dev); |
| 749 | int nr = to_sensor_dev_attr(devattr)->index; |
| 750 | |
| 751 | return sprintf(buf, "%d\n", !!(data->temp_status[nr] |
| 752 | & TEMP_STATUS_LOWFLG)); |
| 753 | } |
| 754 | |
| 755 | static ssize_t show_temp_max_alarm(struct device *dev, struct device_attribute |
| 756 | *devattr, char *buf) |
| 757 | { |
| 758 | struct pc87427_data *data = pc87427_update_device(dev); |
| 759 | int nr = to_sensor_dev_attr(devattr)->index; |
| 760 | |
| 761 | return sprintf(buf, "%d\n", !!(data->temp_status[nr] |
| 762 | & TEMP_STATUS_HIGHFLG)); |
| 763 | } |
| 764 | |
| 765 | static ssize_t show_temp_crit_alarm(struct device *dev, struct device_attribute |
| 766 | *devattr, char *buf) |
| 767 | { |
| 768 | struct pc87427_data *data = pc87427_update_device(dev); |
| 769 | int nr = to_sensor_dev_attr(devattr)->index; |
| 770 | |
| 771 | return sprintf(buf, "%d\n", !!(data->temp_status[nr] |
| 772 | & TEMP_STATUS_CRITFLG)); |
| 773 | } |
| 774 | |
| 775 | static ssize_t show_temp_fault(struct device *dev, struct device_attribute |
| 776 | *devattr, char *buf) |
| 777 | { |
| 778 | struct pc87427_data *data = pc87427_update_device(dev); |
| 779 | int nr = to_sensor_dev_attr(devattr)->index; |
| 780 | |
| 781 | return sprintf(buf, "%d\n", !!(data->temp_status[nr] |
| 782 | & TEMP_STATUS_SENSERR)); |
| 783 | } |
| 784 | |
| 785 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0); |
| 786 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1); |
| 787 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2); |
| 788 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3); |
| 789 | static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4); |
| 790 | static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5); |
| 791 | |
| 792 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, NULL, 0); |
| 793 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, NULL, 1); |
| 794 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, NULL, 2); |
| 795 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, NULL, 3); |
| 796 | static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, NULL, 4); |
| 797 | static SENSOR_DEVICE_ATTR(temp6_min, S_IRUGO, show_temp_min, NULL, 5); |
| 798 | |
| 799 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0); |
| 800 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1); |
| 801 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2); |
| 802 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3); |
| 803 | static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4); |
| 804 | static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5); |
| 805 | |
| 806 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0); |
| 807 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1); |
| 808 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2); |
| 809 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3); |
| 810 | static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4); |
| 811 | static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5); |
| 812 | |
| 813 | static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0); |
| 814 | static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1); |
| 815 | static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2); |
| 816 | static SENSOR_DEVICE_ATTR(temp4_type, S_IRUGO, show_temp_type, NULL, 3); |
| 817 | static SENSOR_DEVICE_ATTR(temp5_type, S_IRUGO, show_temp_type, NULL, 4); |
| 818 | static SENSOR_DEVICE_ATTR(temp6_type, S_IRUGO, show_temp_type, NULL, 5); |
| 819 | |
| 820 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, |
| 821 | show_temp_min_alarm, NULL, 0); |
| 822 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, |
| 823 | show_temp_min_alarm, NULL, 1); |
| 824 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, |
| 825 | show_temp_min_alarm, NULL, 2); |
| 826 | static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, |
| 827 | show_temp_min_alarm, NULL, 3); |
| 828 | static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, |
| 829 | show_temp_min_alarm, NULL, 4); |
| 830 | static SENSOR_DEVICE_ATTR(temp6_min_alarm, S_IRUGO, |
| 831 | show_temp_min_alarm, NULL, 5); |
| 832 | |
| 833 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, |
| 834 | show_temp_max_alarm, NULL, 0); |
| 835 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, |
| 836 | show_temp_max_alarm, NULL, 1); |
| 837 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, |
| 838 | show_temp_max_alarm, NULL, 2); |
| 839 | static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, |
| 840 | show_temp_max_alarm, NULL, 3); |
| 841 | static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, |
| 842 | show_temp_max_alarm, NULL, 4); |
| 843 | static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO, |
| 844 | show_temp_max_alarm, NULL, 5); |
| 845 | |
| 846 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, |
| 847 | show_temp_crit_alarm, NULL, 0); |
| 848 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, |
| 849 | show_temp_crit_alarm, NULL, 1); |
| 850 | static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, |
| 851 | show_temp_crit_alarm, NULL, 2); |
| 852 | static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, |
| 853 | show_temp_crit_alarm, NULL, 3); |
| 854 | static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO, |
| 855 | show_temp_crit_alarm, NULL, 4); |
| 856 | static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO, |
| 857 | show_temp_crit_alarm, NULL, 5); |
| 858 | |
| 859 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0); |
| 860 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1); |
| 861 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2); |
| 862 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3); |
| 863 | static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4); |
| 864 | static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5); |
| 865 | |
| 866 | static struct attribute *pc87427_attributes_temp[6][10] = { |
| 867 | { |
| 868 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 869 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 870 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 871 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 872 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
| 873 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 874 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 875 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 876 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 877 | NULL |
| 878 | }, { |
| 879 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 880 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 881 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 882 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 883 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 884 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 885 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 886 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 887 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 888 | NULL |
| 889 | }, { |
| 890 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 891 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 892 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 893 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 894 | &sensor_dev_attr_temp3_type.dev_attr.attr, |
| 895 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 896 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 897 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 898 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 899 | NULL |
| 900 | }, { |
| 901 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 902 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 903 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 904 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
| 905 | &sensor_dev_attr_temp4_type.dev_attr.attr, |
| 906 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, |
| 907 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 908 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
| 909 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
| 910 | NULL |
| 911 | }, { |
| 912 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 913 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
| 914 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 915 | &sensor_dev_attr_temp5_crit.dev_attr.attr, |
| 916 | &sensor_dev_attr_temp5_type.dev_attr.attr, |
| 917 | &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, |
| 918 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, |
| 919 | &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, |
| 920 | &sensor_dev_attr_temp5_fault.dev_attr.attr, |
| 921 | NULL |
| 922 | }, { |
| 923 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 924 | &sensor_dev_attr_temp6_min.dev_attr.attr, |
| 925 | &sensor_dev_attr_temp6_max.dev_attr.attr, |
| 926 | &sensor_dev_attr_temp6_crit.dev_attr.attr, |
| 927 | &sensor_dev_attr_temp6_type.dev_attr.attr, |
| 928 | &sensor_dev_attr_temp6_min_alarm.dev_attr.attr, |
| 929 | &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, |
| 930 | &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, |
| 931 | &sensor_dev_attr_temp6_fault.dev_attr.attr, |
| 932 | NULL |
| 933 | } |
| 934 | }; |
| 935 | |
| 936 | static const struct attribute_group pc87427_group_temp[6] = { |
| 937 | { .attrs = pc87427_attributes_temp[0] }, |
| 938 | { .attrs = pc87427_attributes_temp[1] }, |
| 939 | { .attrs = pc87427_attributes_temp[2] }, |
| 940 | { .attrs = pc87427_attributes_temp[3] }, |
| 941 | { .attrs = pc87427_attributes_temp[4] }, |
| 942 | { .attrs = pc87427_attributes_temp[5] }, |
| 943 | }; |
| 944 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 945 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 946 | *devattr, char *buf) |
| 947 | { |
| 948 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 949 | |
| 950 | return sprintf(buf, "%s\n", data->name); |
| 951 | } |
| 952 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 953 | |
| 954 | |
| 955 | /* |
| 956 | * Device detection, attach and detach |
| 957 | */ |
| 958 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 959 | static int pc87427_request_regions(struct platform_device *pdev, |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 960 | int count) |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 961 | { |
| 962 | struct resource *res; |
| 963 | int i; |
| 964 | |
| 965 | for (i = 0; i < count; i++) { |
| 966 | res = platform_get_resource(pdev, IORESOURCE_IO, i); |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 967 | if (!res) { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 968 | dev_err(&pdev->dev, "Missing resource #%d\n", i); |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 969 | return -ENOENT; |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 970 | } |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 971 | if (!devm_request_region(&pdev->dev, res->start, |
| 972 | resource_size(res), DRVNAME)) { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 973 | dev_err(&pdev->dev, |
| 974 | "Failed to request region 0x%lx-0x%lx\n", |
| 975 | (unsigned long)res->start, |
| 976 | (unsigned long)res->end); |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 977 | return -EBUSY; |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 978 | } |
| 979 | } |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 980 | return 0; |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 983 | static void pc87427_init_device(struct device *dev) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 984 | { |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 985 | struct pc87427_sio_data *sio_data = dev->platform_data; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 986 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 987 | int i; |
| 988 | u8 reg; |
| 989 | |
| 990 | /* The FMC module should be ready */ |
| 991 | reg = pc87427_read8(data, LD_FAN, PC87427_REG_BANK); |
| 992 | if (!(reg & 0x80)) |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 993 | dev_warn(dev, "%s module not ready!\n", "FMC"); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 994 | |
| 995 | /* Check which fans are enabled */ |
| 996 | for (i = 0; i < 8; i++) { |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 997 | if (!(sio_data->has_fanin & (1 << i))) /* Not wired */ |
| 998 | continue; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 999 | reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i), |
| 1000 | PC87427_REG_FAN_STATUS); |
| 1001 | if (reg & FAN_STATUS_MONEN) |
| 1002 | data->fan_enabled |= (1 << i); |
| 1003 | } |
| 1004 | |
| 1005 | if (!data->fan_enabled) { |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1006 | dev_dbg(dev, "Enabling monitoring of all fans\n"); |
| 1007 | for (i = 0; i < 8; i++) { |
| 1008 | if (!(sio_data->has_fanin & (1 << i))) /* Not wired */ |
| 1009 | continue; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1010 | pc87427_write8_bank(data, LD_FAN, BANK_FM(i), |
| 1011 | PC87427_REG_FAN_STATUS, |
| 1012 | FAN_STATUS_MONEN); |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1013 | } |
| 1014 | data->fan_enabled = sio_data->has_fanin; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1015 | } |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1016 | |
| 1017 | /* Check which PWM outputs are enabled */ |
| 1018 | for (i = 0; i < 4; i++) { |
| 1019 | if (!(sio_data->has_fanout & (1 << i))) /* Not wired */ |
| 1020 | continue; |
| 1021 | reg = pc87427_read8_bank(data, LD_FAN, BANK_FC(i), |
| 1022 | PC87427_REG_PWM_ENABLE); |
| 1023 | if (reg & PWM_ENABLE_CTLEN) |
| 1024 | data->pwm_enabled |= (1 << i); |
| 1025 | |
Guenter Roeck | 36564ef | 2012-01-19 11:02:23 -0800 | [diff] [blame] | 1026 | /* |
| 1027 | * We don't expose an interface to reconfigure the automatic |
| 1028 | * fan control mode, so only allow to return to this mode if |
| 1029 | * it was originally set. |
| 1030 | */ |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1031 | if ((reg & PWM_ENABLE_MODE_MASK) == PWM_MODE_AUTO) { |
| 1032 | dev_dbg(dev, "PWM%d is in automatic control mode\n", |
| 1033 | i + 1); |
| 1034 | data->pwm_auto_ok |= (1 << i); |
| 1035 | } |
| 1036 | } |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* The HMC module should be ready */ |
| 1039 | reg = pc87427_read8(data, LD_TEMP, PC87427_REG_BANK); |
| 1040 | if (!(reg & 0x80)) |
| 1041 | dev_warn(dev, "%s module not ready!\n", "HMC"); |
| 1042 | |
| 1043 | /* Check which temperature channels are enabled */ |
| 1044 | for (i = 0; i < 6; i++) { |
| 1045 | reg = pc87427_read8_bank(data, LD_TEMP, BANK_TM(i), |
| 1046 | PC87427_REG_TEMP_STATUS); |
| 1047 | if (reg & TEMP_STATUS_CHANEN) |
| 1048 | data->temp_enabled |= (1 << i); |
| 1049 | } |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Jean Delvare | c6b8724 | 2010-08-14 21:09:00 +0200 | [diff] [blame] | 1052 | static void pc87427_remove_files(struct device *dev) |
| 1053 | { |
| 1054 | struct pc87427_data *data = dev_get_drvdata(dev); |
| 1055 | int i; |
| 1056 | |
| 1057 | device_remove_file(dev, &dev_attr_name); |
| 1058 | for (i = 0; i < 8; i++) { |
| 1059 | if (!(data->fan_enabled & (1 << i))) |
| 1060 | continue; |
| 1061 | sysfs_remove_group(&dev->kobj, &pc87427_group_fan[i]); |
| 1062 | } |
| 1063 | for (i = 0; i < 4; i++) { |
| 1064 | if (!(data->pwm_enabled & (1 << i))) |
| 1065 | continue; |
| 1066 | sysfs_remove_group(&dev->kobj, &pc87427_group_pwm[i]); |
| 1067 | } |
| 1068 | for (i = 0; i < 6; i++) { |
| 1069 | if (!(data->temp_enabled & (1 << i))) |
| 1070 | continue; |
| 1071 | sysfs_remove_group(&dev->kobj, &pc87427_group_temp[i]); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 1075 | static int pc87427_probe(struct platform_device *pdev) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1076 | { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1077 | struct pc87427_sio_data *sio_data = pdev->dev.platform_data; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1078 | struct pc87427_data *data; |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1079 | int i, err, res_count; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1080 | |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 1081 | data = devm_kzalloc(&pdev->dev, sizeof(struct pc87427_data), |
| 1082 | GFP_KERNEL); |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 1083 | if (!data) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1084 | pr_err("Out of memory\n"); |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 1085 | return -ENOMEM; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1088 | data->address[0] = sio_data->address[0]; |
| 1089 | data->address[1] = sio_data->address[1]; |
| 1090 | res_count = (data->address[0] != 0) + (data->address[1] != 0); |
| 1091 | |
| 1092 | err = pc87427_request_regions(pdev, res_count); |
| 1093 | if (err) |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 1094 | return err; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1095 | |
| 1096 | mutex_init(&data->lock); |
| 1097 | data->name = "pc87427"; |
| 1098 | platform_set_drvdata(pdev, data); |
| 1099 | pc87427_init_device(&pdev->dev); |
| 1100 | |
| 1101 | /* Register sysfs hooks */ |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 1102 | err = device_create_file(&pdev->dev, &dev_attr_name); |
| 1103 | if (err) |
Guenter Roeck | 5e0c876 | 2012-06-02 11:20:20 -0700 | [diff] [blame] | 1104 | return err; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1105 | for (i = 0; i < 8; i++) { |
| 1106 | if (!(data->fan_enabled & (1 << i))) |
| 1107 | continue; |
Jean Delvare | 0d22d58 | 2010-08-14 21:08:57 +0200 | [diff] [blame] | 1108 | err = sysfs_create_group(&pdev->dev.kobj, |
| 1109 | &pc87427_group_fan[i]); |
| 1110 | if (err) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1111 | goto exit_remove_files; |
| 1112 | } |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1113 | for (i = 0; i < 4; i++) { |
| 1114 | if (!(data->pwm_enabled & (1 << i))) |
| 1115 | continue; |
| 1116 | err = sysfs_create_group(&pdev->dev.kobj, |
| 1117 | &pc87427_group_pwm[i]); |
| 1118 | if (err) |
| 1119 | goto exit_remove_files; |
| 1120 | } |
Jean Delvare | 008e5f3 | 2010-08-14 21:08:59 +0200 | [diff] [blame] | 1121 | for (i = 0; i < 6; i++) { |
| 1122 | if (!(data->temp_enabled & (1 << i))) |
| 1123 | continue; |
| 1124 | err = sysfs_create_group(&pdev->dev.kobj, |
| 1125 | &pc87427_group_temp[i]); |
| 1126 | if (err) |
| 1127 | goto exit_remove_files; |
| 1128 | } |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1129 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1130 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 1131 | if (IS_ERR(data->hwmon_dev)) { |
| 1132 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1133 | dev_err(&pdev->dev, "Class registration failed (%d)\n", err); |
| 1134 | goto exit_remove_files; |
| 1135 | } |
| 1136 | |
| 1137 | return 0; |
| 1138 | |
| 1139 | exit_remove_files: |
Jean Delvare | c6b8724 | 2010-08-14 21:09:00 +0200 | [diff] [blame] | 1140 | pc87427_remove_files(&pdev->dev); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1141 | return err; |
| 1142 | } |
| 1143 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 1144 | static int pc87427_remove(struct platform_device *pdev) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1145 | { |
| 1146 | struct pc87427_data *data = platform_get_drvdata(pdev); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1147 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1148 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | c6b8724 | 2010-08-14 21:09:00 +0200 | [diff] [blame] | 1149 | pc87427_remove_files(&pdev->dev); |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 1150 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | static struct platform_driver pc87427_driver = { |
| 1156 | .driver = { |
| 1157 | .owner = THIS_MODULE, |
| 1158 | .name = DRVNAME, |
| 1159 | }, |
| 1160 | .probe = pc87427_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 1161 | .remove = pc87427_remove, |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1162 | }; |
| 1163 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1164 | static int __init pc87427_device_add(const struct pc87427_sio_data *sio_data) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1165 | { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1166 | struct resource res[2] = { |
| 1167 | { .flags = IORESOURCE_IO }, |
| 1168 | { .flags = IORESOURCE_IO }, |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1169 | }; |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1170 | int err, i, res_count; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1171 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1172 | res_count = 0; |
| 1173 | for (i = 0; i < 2; i++) { |
| 1174 | if (!sio_data->address[i]) |
| 1175 | continue; |
| 1176 | res[res_count].start = sio_data->address[i]; |
| 1177 | res[res_count].end = sio_data->address[i] + REGION_LENGTH - 1; |
| 1178 | res[res_count].name = logdev_str[i]; |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 1179 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1180 | err = acpi_check_resource_conflict(&res[res_count]); |
| 1181 | if (err) |
| 1182 | goto exit; |
| 1183 | |
| 1184 | res_count++; |
| 1185 | } |
| 1186 | |
| 1187 | pdev = platform_device_alloc(DRVNAME, res[0].start); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1188 | if (!pdev) { |
| 1189 | err = -ENOMEM; |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1190 | pr_err("Device allocation failed\n"); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1191 | goto exit; |
| 1192 | } |
| 1193 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1194 | err = platform_device_add_resources(pdev, res, res_count); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1195 | if (err) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1196 | pr_err("Device resource addition failed (%d)\n", err); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1197 | goto exit_device_put; |
| 1198 | } |
| 1199 | |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1200 | err = platform_device_add_data(pdev, sio_data, |
| 1201 | sizeof(struct pc87427_sio_data)); |
| 1202 | if (err) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1203 | pr_err("Platform data allocation failed\n"); |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1204 | goto exit_device_put; |
| 1205 | } |
| 1206 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1207 | err = platform_device_add(pdev); |
| 1208 | if (err) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1209 | pr_err("Device addition failed (%d)\n", err); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1210 | goto exit_device_put; |
| 1211 | } |
| 1212 | |
| 1213 | return 0; |
| 1214 | |
| 1215 | exit_device_put: |
| 1216 | platform_device_put(pdev); |
| 1217 | exit: |
| 1218 | return err; |
| 1219 | } |
| 1220 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1221 | static int __init pc87427_find(int sioaddr, struct pc87427_sio_data *sio_data) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1222 | { |
| 1223 | u16 val; |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1224 | u8 cfg, cfg_b; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1225 | int i, err = 0; |
| 1226 | |
| 1227 | /* Identify device */ |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 1228 | val = force_id ? force_id : superio_inb(sioaddr, SIOREG_DEVID); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1229 | if (val != 0xf2) { /* PC87427 */ |
| 1230 | err = -ENODEV; |
| 1231 | goto exit; |
| 1232 | } |
| 1233 | |
| 1234 | for (i = 0; i < 2; i++) { |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1235 | sio_data->address[i] = 0; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1236 | /* Select logical device */ |
| 1237 | superio_outb(sioaddr, SIOREG_LDSEL, logdev[i]); |
| 1238 | |
| 1239 | val = superio_inb(sioaddr, SIOREG_ACT); |
| 1240 | if (!(val & 0x01)) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1241 | pr_info("Logical device 0x%02x not activated\n", |
| 1242 | logdev[i]); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1243 | continue; |
| 1244 | } |
| 1245 | |
| 1246 | val = superio_inb(sioaddr, SIOREG_MAP); |
| 1247 | if (val & 0x01) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1248 | pr_warn("Logical device 0x%02x is memory-mapped, " |
| 1249 | "can't use\n", logdev[i]); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1250 | continue; |
| 1251 | } |
| 1252 | |
| 1253 | val = (superio_inb(sioaddr, SIOREG_IOBASE) << 8) |
| 1254 | | superio_inb(sioaddr, SIOREG_IOBASE + 1); |
| 1255 | if (!val) { |
Joe Perches | 5e24e0c | 2011-01-12 21:55:11 +0100 | [diff] [blame] | 1256 | pr_info("I/O base address not set for logical device " |
| 1257 | "0x%02x\n", logdev[i]); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1258 | continue; |
| 1259 | } |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1260 | sio_data->address[i] = val; |
| 1261 | } |
| 1262 | |
| 1263 | /* No point in loading the driver if everything is disabled */ |
| 1264 | if (!sio_data->address[0] && !sio_data->address[1]) { |
| 1265 | err = -ENODEV; |
| 1266 | goto exit; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1267 | } |
| 1268 | |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1269 | /* Check which fan inputs are wired */ |
| 1270 | sio_data->has_fanin = (1 << 2) | (1 << 3); /* FANIN2, FANIN3 */ |
| 1271 | |
| 1272 | cfg = superio_inb(sioaddr, SIOREG_CF2); |
| 1273 | if (!(cfg & (1 << 3))) |
| 1274 | sio_data->has_fanin |= (1 << 0); /* FANIN0 */ |
| 1275 | if (!(cfg & (1 << 2))) |
| 1276 | sio_data->has_fanin |= (1 << 4); /* FANIN4 */ |
| 1277 | |
| 1278 | cfg = superio_inb(sioaddr, SIOREG_CFD); |
| 1279 | if (!(cfg & (1 << 0))) |
| 1280 | sio_data->has_fanin |= (1 << 1); /* FANIN1 */ |
| 1281 | |
| 1282 | cfg = superio_inb(sioaddr, SIOREG_CF4); |
| 1283 | if (!(cfg & (1 << 0))) |
| 1284 | sio_data->has_fanin |= (1 << 7); /* FANIN7 */ |
| 1285 | cfg_b = superio_inb(sioaddr, SIOREG_CFB); |
| 1286 | if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3))) |
| 1287 | sio_data->has_fanin |= (1 << 5); /* FANIN5 */ |
| 1288 | cfg = superio_inb(sioaddr, SIOREG_CF3); |
| 1289 | if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5))) |
| 1290 | sio_data->has_fanin |= (1 << 6); /* FANIN6 */ |
| 1291 | |
Jean Delvare | 328716b | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1292 | /* Check which fan outputs are wired */ |
| 1293 | sio_data->has_fanout = (1 << 0); /* FANOUT0 */ |
| 1294 | if (cfg_b & (1 << 0)) |
| 1295 | sio_data->has_fanout |= (1 << 3); /* FANOUT3 */ |
| 1296 | |
| 1297 | cfg = superio_inb(sioaddr, SIOREG_CFC); |
| 1298 | if (!(cfg & (1 << 4))) { |
| 1299 | if (cfg_b & (1 << 1)) |
| 1300 | sio_data->has_fanout |= (1 << 1); /* FANOUT1 */ |
| 1301 | if (cfg_b & (1 << 2)) |
| 1302 | sio_data->has_fanout |= (1 << 2); /* FANOUT2 */ |
| 1303 | } |
| 1304 | |
| 1305 | /* FANOUT1 and FANOUT2 can each be routed to 2 different pins */ |
| 1306 | cfg = superio_inb(sioaddr, SIOREG_CF5); |
| 1307 | if (cfg & (1 << 6)) |
| 1308 | sio_data->has_fanout |= (1 << 1); /* FANOUT1 */ |
| 1309 | if (cfg & (1 << 5)) |
| 1310 | sio_data->has_fanout |= (1 << 2); /* FANOUT2 */ |
| 1311 | |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1312 | exit: |
| 1313 | superio_exit(sioaddr); |
| 1314 | return err; |
| 1315 | } |
| 1316 | |
| 1317 | static int __init pc87427_init(void) |
| 1318 | { |
| 1319 | int err; |
Jean Delvare | 4e7d99e | 2010-08-14 21:08:56 +0200 | [diff] [blame] | 1320 | struct pc87427_sio_data sio_data; |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1321 | |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1322 | if (pc87427_find(0x2e, &sio_data) |
| 1323 | && pc87427_find(0x4e, &sio_data)) |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1324 | return -ENODEV; |
| 1325 | |
| 1326 | err = platform_driver_register(&pc87427_driver); |
| 1327 | if (err) |
| 1328 | goto exit; |
| 1329 | |
| 1330 | /* Sets global pdev as a side effect */ |
Jean Delvare | 9d32df1 | 2010-08-14 21:08:58 +0200 | [diff] [blame] | 1331 | err = pc87427_device_add(&sio_data); |
Jean Delvare | ba224e2 | 2006-12-12 18:18:29 +0100 | [diff] [blame] | 1332 | if (err) |
| 1333 | goto exit_driver; |
| 1334 | |
| 1335 | return 0; |
| 1336 | |
| 1337 | exit_driver: |
| 1338 | platform_driver_unregister(&pc87427_driver); |
| 1339 | exit: |
| 1340 | return err; |
| 1341 | } |
| 1342 | |
| 1343 | static void __exit pc87427_exit(void) |
| 1344 | { |
| 1345 | platform_device_unregister(pdev); |
| 1346 | platform_driver_unregister(&pc87427_driver); |
| 1347 | } |
| 1348 | |
| 1349 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 1350 | MODULE_DESCRIPTION("PC87427 hardware monitoring driver"); |
| 1351 | MODULE_LICENSE("GPL"); |
| 1352 | |
| 1353 | module_init(pc87427_init); |
| 1354 | module_exit(pc87427_exit); |