Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware |
| 3 | * monitoring features |
| 4 | * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com> |
| 5 | * |
| 6 | * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker |
| 7 | * and its port to kernel 2.6 by Lars Ekman. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/hwmon.h> |
| 32 | #include <linux/hwmon-sysfs.h> |
| 33 | #include <linux/hwmon-vid.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/mutex.h> |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 36 | #include <linux/ioport.h> |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 37 | #include <linux/acpi.h> |
H Hartley Sweeten | 6055fae | 2009-09-15 17:18:13 +0200 | [diff] [blame] | 38 | #include <linux/io.h> |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 39 | |
| 40 | static int uch_config = -1; |
| 41 | module_param(uch_config, int, 0); |
| 42 | MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration"); |
| 43 | |
| 44 | static int int_mode = -1; |
| 45 | module_param(int_mode, int, 0); |
| 46 | MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode"); |
| 47 | |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 48 | static unsigned short force_id; |
| 49 | module_param(force_id, ushort, 0); |
| 50 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 51 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 52 | static struct platform_device *pdev; |
| 53 | |
| 54 | #define DRVNAME "vt1211" |
| 55 | |
| 56 | /* --------------------------------------------------------------------- |
| 57 | * Registers |
| 58 | * |
| 59 | * The sensors are defined as follows. |
| 60 | * |
| 61 | * Sensor Voltage Mode Temp Mode Notes (from the datasheet) |
| 62 | * -------- ------------ --------- -------------------------- |
| 63 | * Reading 1 temp1 Intel thermal diode |
| 64 | * Reading 3 temp2 Internal thermal diode |
| 65 | * UCH1/Reading2 in0 temp3 NTC type thermistor |
| 66 | * UCH2 in1 temp4 +2.5V |
| 67 | * UCH3 in2 temp5 VccP |
| 68 | * UCH4 in3 temp6 +5V |
| 69 | * UCH5 in4 temp7 +12V |
| 70 | * 3.3V in5 Internal VDD (+3.3V) |
| 71 | * |
| 72 | * --------------------------------------------------------------------- */ |
| 73 | |
| 74 | /* Voltages (in) numbered 0-5 (ix) */ |
| 75 | #define VT1211_REG_IN(ix) (0x21 + (ix)) |
| 76 | #define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix)) |
| 77 | #define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix)) |
| 78 | |
| 79 | /* Temperatures (temp) numbered 0-6 (ix) */ |
| 80 | static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25}; |
| 81 | static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31}; |
| 82 | static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32}; |
| 83 | |
| 84 | /* Fans numbered 0-1 (ix) */ |
| 85 | #define VT1211_REG_FAN(ix) (0x29 + (ix)) |
| 86 | #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix)) |
| 87 | #define VT1211_REG_FAN_DIV 0x47 |
| 88 | |
| 89 | /* PWMs numbered 0-1 (ix) */ |
| 90 | /* Auto points numbered 0-3 (ap) */ |
| 91 | #define VT1211_REG_PWM(ix) (0x60 + (ix)) |
| 92 | #define VT1211_REG_PWM_CLK 0x50 |
| 93 | #define VT1211_REG_PWM_CTL 0x51 |
| 94 | #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap)) |
| 95 | #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap)) |
| 96 | |
| 97 | /* Miscellaneous registers */ |
| 98 | #define VT1211_REG_CONFIG 0x40 |
| 99 | #define VT1211_REG_ALARM1 0x41 |
| 100 | #define VT1211_REG_ALARM2 0x42 |
| 101 | #define VT1211_REG_VID 0x45 |
| 102 | #define VT1211_REG_UCH_CONFIG 0x4a |
| 103 | #define VT1211_REG_TEMP1_CONFIG 0x4b |
| 104 | #define VT1211_REG_TEMP2_CONFIG 0x4c |
| 105 | |
| 106 | /* In, temp & fan alarm bits */ |
| 107 | static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9}; |
| 108 | static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8}; |
| 109 | static const u8 bitalarmfan[] = {6, 7}; |
| 110 | |
| 111 | /* --------------------------------------------------------------------- |
| 112 | * Data structures and manipulation thereof |
| 113 | * --------------------------------------------------------------------- */ |
| 114 | |
| 115 | struct vt1211_data { |
| 116 | unsigned short addr; |
| 117 | const char *name; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 118 | struct device *hwmon_dev; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 119 | |
| 120 | struct mutex update_lock; |
| 121 | char valid; /* !=0 if following fields are valid */ |
| 122 | unsigned long last_updated; /* In jiffies */ |
| 123 | |
| 124 | /* Register values */ |
| 125 | u8 in[6]; |
| 126 | u8 in_max[6]; |
| 127 | u8 in_min[6]; |
| 128 | u8 temp[7]; |
| 129 | u8 temp_max[7]; |
| 130 | u8 temp_hyst[7]; |
| 131 | u8 fan[2]; |
| 132 | u8 fan_min[2]; |
| 133 | u8 fan_div[2]; |
| 134 | u8 fan_ctl; |
| 135 | u8 pwm[2]; |
| 136 | u8 pwm_ctl[2]; |
| 137 | u8 pwm_clk; |
| 138 | u8 pwm_auto_temp[4]; |
| 139 | u8 pwm_auto_pwm[2][4]; |
| 140 | u8 vid; /* Read once at init time */ |
| 141 | u8 vrm; |
| 142 | u8 uch_config; /* Read once at init time */ |
| 143 | u16 alarms; |
| 144 | }; |
| 145 | |
| 146 | /* ix = [0-5] */ |
| 147 | #define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \ |
| 148 | !(((uch_config) >> ((ix) + 2)) & 1)) |
| 149 | |
| 150 | /* ix = [0-6] */ |
| 151 | #define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \ |
| 152 | ((uch_config) >> (ix)) & 1) |
| 153 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 154 | /* |
| 155 | * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the |
| 156 | * driver according to the VT1211 BIOS porting guide |
| 157 | */ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 158 | #define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \ |
| 159 | (((reg) - 3) * 15882 + 479) / 958 : \ |
| 160 | (((reg) - 3) * 10000 + 479) / 958) |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 161 | #define IN_TO_REG(ix, val) (clamp_val((ix) == 5 ? \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 162 | ((val) * 958 + 7941) / 15882 + 3 : \ |
| 163 | ((val) * 958 + 5000) / 10000 + 3, 0, 255)) |
| 164 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 165 | /* |
| 166 | * temp1 (ix = 0) is an intel thermal diode which is scaled in user space. |
| 167 | * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver |
| 168 | * according to some measurements that I took on an EPIA M10000. |
| 169 | * temp3-7 are thermistor based so the driver returns the voltage measured at |
| 170 | * the pin (range 0V - 2.2V). |
| 171 | */ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 172 | #define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \ |
| 173 | (ix) == 1 ? (reg) < 51 ? 0 : \ |
| 174 | ((reg) - 51) * 1000 : \ |
| 175 | ((253 - (reg)) * 2200 + 105) / 210) |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 176 | #define TEMP_TO_REG(ix, val) clamp_val( \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 177 | ((ix) == 0 ? ((val) + 500) / 1000 : \ |
| 178 | (ix) == 1 ? ((val) + 500) / 1000 + 51 : \ |
| 179 | 253 - ((val) * 210 + 1100) / 2200), 0, 255) |
| 180 | |
| 181 | #define DIV_FROM_REG(reg) (1 << (reg)) |
| 182 | |
| 183 | #define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \ |
| 184 | 1310720 / (reg) / DIV_FROM_REG(div)) |
| 185 | #define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \ |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 186 | clamp_val((1310720 / (val) / \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 187 | DIV_FROM_REG(div)), 1, 254)) |
| 188 | |
| 189 | /* --------------------------------------------------------------------- |
| 190 | * Super-I/O constants and functions |
| 191 | * --------------------------------------------------------------------- */ |
| 192 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 193 | /* |
| 194 | * Configuration index port registers |
| 195 | * The vt1211 can live at 2 different addresses so we need to probe both |
| 196 | */ |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 197 | #define SIO_REG_CIP1 0x2e |
| 198 | #define SIO_REG_CIP2 0x4e |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 199 | |
| 200 | /* Configuration registers */ |
| 201 | #define SIO_VT1211_LDN 0x07 /* logical device number */ |
| 202 | #define SIO_VT1211_DEVID 0x20 /* device ID */ |
| 203 | #define SIO_VT1211_DEVREV 0x21 /* device revision */ |
| 204 | #define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */ |
| 205 | #define SIO_VT1211_BADDR 0x60 /* base I/O address */ |
| 206 | #define SIO_VT1211_ID 0x3c /* VT1211 device ID */ |
| 207 | |
| 208 | /* VT1211 logical device numbers */ |
| 209 | #define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */ |
| 210 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 211 | static inline void superio_outb(int sio_cip, int reg, int val) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 212 | { |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 213 | outb(reg, sio_cip); |
| 214 | outb(val, sio_cip + 1); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 217 | static inline int superio_inb(int sio_cip, int reg) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 218 | { |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 219 | outb(reg, sio_cip); |
| 220 | return inb(sio_cip + 1); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 223 | static inline void superio_select(int sio_cip, int ldn) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 224 | { |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 225 | outb(SIO_VT1211_LDN, sio_cip); |
| 226 | outb(ldn, sio_cip + 1); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 229 | static inline void superio_enter(int sio_cip) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 230 | { |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 231 | outb(0x87, sio_cip); |
| 232 | outb(0x87, sio_cip); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 235 | static inline void superio_exit(int sio_cip) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 236 | { |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 237 | outb(0xaa, sio_cip); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* --------------------------------------------------------------------- |
| 241 | * Device I/O access |
| 242 | * --------------------------------------------------------------------- */ |
| 243 | |
| 244 | static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg) |
| 245 | { |
| 246 | return inb(data->addr + reg); |
| 247 | } |
| 248 | |
| 249 | static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val) |
| 250 | { |
| 251 | outb(val, data->addr + reg); |
| 252 | } |
| 253 | |
| 254 | static struct vt1211_data *vt1211_update_device(struct device *dev) |
| 255 | { |
| 256 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 257 | int ix, val; |
| 258 | |
| 259 | mutex_lock(&data->update_lock); |
| 260 | |
| 261 | /* registers cache is refreshed after 1 second */ |
| 262 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 263 | /* read VID */ |
| 264 | data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f; |
| 265 | |
| 266 | /* voltage (in) registers */ |
| 267 | for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { |
| 268 | if (ISVOLT(ix, data->uch_config)) { |
| 269 | data->in[ix] = vt1211_read8(data, |
| 270 | VT1211_REG_IN(ix)); |
| 271 | data->in_min[ix] = vt1211_read8(data, |
| 272 | VT1211_REG_IN_MIN(ix)); |
| 273 | data->in_max[ix] = vt1211_read8(data, |
| 274 | VT1211_REG_IN_MAX(ix)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* temp registers */ |
| 279 | for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { |
| 280 | if (ISTEMP(ix, data->uch_config)) { |
| 281 | data->temp[ix] = vt1211_read8(data, |
| 282 | regtemp[ix]); |
| 283 | data->temp_max[ix] = vt1211_read8(data, |
| 284 | regtempmax[ix]); |
| 285 | data->temp_hyst[ix] = vt1211_read8(data, |
| 286 | regtemphyst[ix]); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* fan & pwm registers */ |
| 291 | for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) { |
| 292 | data->fan[ix] = vt1211_read8(data, |
| 293 | VT1211_REG_FAN(ix)); |
| 294 | data->fan_min[ix] = vt1211_read8(data, |
| 295 | VT1211_REG_FAN_MIN(ix)); |
| 296 | data->pwm[ix] = vt1211_read8(data, |
| 297 | VT1211_REG_PWM(ix)); |
| 298 | } |
| 299 | val = vt1211_read8(data, VT1211_REG_FAN_DIV); |
| 300 | data->fan_div[0] = (val >> 4) & 3; |
| 301 | data->fan_div[1] = (val >> 6) & 3; |
| 302 | data->fan_ctl = val & 0xf; |
| 303 | |
| 304 | val = vt1211_read8(data, VT1211_REG_PWM_CTL); |
| 305 | data->pwm_ctl[0] = val & 0xf; |
| 306 | data->pwm_ctl[1] = (val >> 4) & 0xf; |
| 307 | |
| 308 | data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK); |
| 309 | |
| 310 | /* pwm & temp auto point registers */ |
| 311 | data->pwm_auto_pwm[0][1] = vt1211_read8(data, |
| 312 | VT1211_REG_PWM_AUTO_PWM(0, 1)); |
| 313 | data->pwm_auto_pwm[0][2] = vt1211_read8(data, |
| 314 | VT1211_REG_PWM_AUTO_PWM(0, 2)); |
| 315 | data->pwm_auto_pwm[1][1] = vt1211_read8(data, |
| 316 | VT1211_REG_PWM_AUTO_PWM(1, 1)); |
| 317 | data->pwm_auto_pwm[1][2] = vt1211_read8(data, |
| 318 | VT1211_REG_PWM_AUTO_PWM(1, 2)); |
| 319 | for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) { |
| 320 | data->pwm_auto_temp[ix] = vt1211_read8(data, |
| 321 | VT1211_REG_PWM_AUTO_TEMP(ix)); |
| 322 | } |
| 323 | |
| 324 | /* alarm registers */ |
| 325 | data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) | |
| 326 | vt1211_read8(data, VT1211_REG_ALARM1); |
| 327 | |
| 328 | data->last_updated = jiffies; |
| 329 | data->valid = 1; |
| 330 | } |
| 331 | |
| 332 | mutex_unlock(&data->update_lock); |
| 333 | |
| 334 | return data; |
| 335 | } |
| 336 | |
| 337 | /* --------------------------------------------------------------------- |
| 338 | * Voltage sysfs interfaces |
| 339 | * ix = [0-5] |
| 340 | * --------------------------------------------------------------------- */ |
| 341 | |
| 342 | #define SHOW_IN_INPUT 0 |
| 343 | #define SHOW_SET_IN_MIN 1 |
| 344 | #define SHOW_SET_IN_MAX 2 |
| 345 | #define SHOW_IN_ALARM 3 |
| 346 | |
| 347 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 348 | char *buf) |
| 349 | { |
| 350 | struct vt1211_data *data = vt1211_update_device(dev); |
| 351 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 352 | to_sensor_dev_attr_2(attr); |
| 353 | int ix = sensor_attr_2->index; |
| 354 | int fn = sensor_attr_2->nr; |
| 355 | int res; |
| 356 | |
| 357 | switch (fn) { |
| 358 | case SHOW_IN_INPUT: |
| 359 | res = IN_FROM_REG(ix, data->in[ix]); |
| 360 | break; |
| 361 | case SHOW_SET_IN_MIN: |
| 362 | res = IN_FROM_REG(ix, data->in_min[ix]); |
| 363 | break; |
| 364 | case SHOW_SET_IN_MAX: |
| 365 | res = IN_FROM_REG(ix, data->in_max[ix]); |
| 366 | break; |
| 367 | case SHOW_IN_ALARM: |
| 368 | res = (data->alarms >> bitalarmin[ix]) & 1; |
| 369 | break; |
| 370 | default: |
| 371 | res = 0; |
| 372 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 373 | } |
| 374 | |
| 375 | return sprintf(buf, "%d\n", res); |
| 376 | } |
| 377 | |
| 378 | static ssize_t set_in(struct device *dev, struct device_attribute *attr, |
| 379 | const char *buf, size_t count) |
| 380 | { |
| 381 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 382 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 383 | to_sensor_dev_attr_2(attr); |
| 384 | int ix = sensor_attr_2->index; |
| 385 | int fn = sensor_attr_2->nr; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 386 | long val; |
| 387 | int err; |
| 388 | |
| 389 | err = kstrtol(buf, 10, &val); |
| 390 | if (err) |
| 391 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 392 | |
| 393 | mutex_lock(&data->update_lock); |
| 394 | switch (fn) { |
| 395 | case SHOW_SET_IN_MIN: |
| 396 | data->in_min[ix] = IN_TO_REG(ix, val); |
| 397 | vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]); |
| 398 | break; |
| 399 | case SHOW_SET_IN_MAX: |
| 400 | data->in_max[ix] = IN_TO_REG(ix, val); |
| 401 | vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]); |
| 402 | break; |
| 403 | default: |
| 404 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 405 | } |
| 406 | mutex_unlock(&data->update_lock); |
| 407 | |
| 408 | return count; |
| 409 | } |
| 410 | |
| 411 | /* --------------------------------------------------------------------- |
| 412 | * Temperature sysfs interfaces |
| 413 | * ix = [0-6] |
| 414 | * --------------------------------------------------------------------- */ |
| 415 | |
| 416 | #define SHOW_TEMP_INPUT 0 |
| 417 | #define SHOW_SET_TEMP_MAX 1 |
| 418 | #define SHOW_SET_TEMP_MAX_HYST 2 |
| 419 | #define SHOW_TEMP_ALARM 3 |
| 420 | |
| 421 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 422 | char *buf) |
| 423 | { |
| 424 | struct vt1211_data *data = vt1211_update_device(dev); |
| 425 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 426 | to_sensor_dev_attr_2(attr); |
| 427 | int ix = sensor_attr_2->index; |
| 428 | int fn = sensor_attr_2->nr; |
| 429 | int res; |
| 430 | |
| 431 | switch (fn) { |
| 432 | case SHOW_TEMP_INPUT: |
| 433 | res = TEMP_FROM_REG(ix, data->temp[ix]); |
| 434 | break; |
| 435 | case SHOW_SET_TEMP_MAX: |
| 436 | res = TEMP_FROM_REG(ix, data->temp_max[ix]); |
| 437 | break; |
| 438 | case SHOW_SET_TEMP_MAX_HYST: |
| 439 | res = TEMP_FROM_REG(ix, data->temp_hyst[ix]); |
| 440 | break; |
| 441 | case SHOW_TEMP_ALARM: |
| 442 | res = (data->alarms >> bitalarmtemp[ix]) & 1; |
| 443 | break; |
| 444 | default: |
| 445 | res = 0; |
| 446 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 447 | } |
| 448 | |
| 449 | return sprintf(buf, "%d\n", res); |
| 450 | } |
| 451 | |
| 452 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, |
| 453 | const char *buf, size_t count) |
| 454 | { |
| 455 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 456 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 457 | to_sensor_dev_attr_2(attr); |
| 458 | int ix = sensor_attr_2->index; |
| 459 | int fn = sensor_attr_2->nr; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 460 | long val; |
| 461 | int err; |
| 462 | |
| 463 | err = kstrtol(buf, 10, &val); |
| 464 | if (err) |
| 465 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 466 | |
| 467 | mutex_lock(&data->update_lock); |
| 468 | switch (fn) { |
| 469 | case SHOW_SET_TEMP_MAX: |
| 470 | data->temp_max[ix] = TEMP_TO_REG(ix, val); |
| 471 | vt1211_write8(data, regtempmax[ix], |
| 472 | data->temp_max[ix]); |
| 473 | break; |
| 474 | case SHOW_SET_TEMP_MAX_HYST: |
| 475 | data->temp_hyst[ix] = TEMP_TO_REG(ix, val); |
| 476 | vt1211_write8(data, regtemphyst[ix], |
| 477 | data->temp_hyst[ix]); |
| 478 | break; |
| 479 | default: |
| 480 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 481 | } |
| 482 | mutex_unlock(&data->update_lock); |
| 483 | |
| 484 | return count; |
| 485 | } |
| 486 | |
| 487 | /* --------------------------------------------------------------------- |
| 488 | * Fan sysfs interfaces |
| 489 | * ix = [0-1] |
| 490 | * --------------------------------------------------------------------- */ |
| 491 | |
| 492 | #define SHOW_FAN_INPUT 0 |
| 493 | #define SHOW_SET_FAN_MIN 1 |
| 494 | #define SHOW_SET_FAN_DIV 2 |
| 495 | #define SHOW_FAN_ALARM 3 |
| 496 | |
| 497 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 498 | char *buf) |
| 499 | { |
| 500 | struct vt1211_data *data = vt1211_update_device(dev); |
| 501 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 502 | to_sensor_dev_attr_2(attr); |
| 503 | int ix = sensor_attr_2->index; |
| 504 | int fn = sensor_attr_2->nr; |
| 505 | int res; |
| 506 | |
| 507 | switch (fn) { |
| 508 | case SHOW_FAN_INPUT: |
| 509 | res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]); |
| 510 | break; |
| 511 | case SHOW_SET_FAN_MIN: |
| 512 | res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]); |
| 513 | break; |
| 514 | case SHOW_SET_FAN_DIV: |
| 515 | res = DIV_FROM_REG(data->fan_div[ix]); |
| 516 | break; |
| 517 | case SHOW_FAN_ALARM: |
| 518 | res = (data->alarms >> bitalarmfan[ix]) & 1; |
| 519 | break; |
| 520 | default: |
| 521 | res = 0; |
| 522 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 523 | } |
| 524 | |
| 525 | return sprintf(buf, "%d\n", res); |
| 526 | } |
| 527 | |
| 528 | static ssize_t set_fan(struct device *dev, struct device_attribute *attr, |
| 529 | const char *buf, size_t count) |
| 530 | { |
| 531 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 532 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 533 | to_sensor_dev_attr_2(attr); |
| 534 | int ix = sensor_attr_2->index; |
| 535 | int fn = sensor_attr_2->nr; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 536 | int reg; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 537 | unsigned long val; |
| 538 | int err; |
| 539 | |
| 540 | err = kstrtoul(buf, 10, &val); |
| 541 | if (err) |
| 542 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 543 | |
| 544 | mutex_lock(&data->update_lock); |
| 545 | |
| 546 | /* sync the data cache */ |
| 547 | reg = vt1211_read8(data, VT1211_REG_FAN_DIV); |
| 548 | data->fan_div[0] = (reg >> 4) & 3; |
| 549 | data->fan_div[1] = (reg >> 6) & 3; |
| 550 | data->fan_ctl = reg & 0xf; |
| 551 | |
| 552 | switch (fn) { |
| 553 | case SHOW_SET_FAN_MIN: |
| 554 | data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]); |
| 555 | vt1211_write8(data, VT1211_REG_FAN_MIN(ix), |
| 556 | data->fan_min[ix]); |
| 557 | break; |
| 558 | case SHOW_SET_FAN_DIV: |
| 559 | switch (val) { |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 560 | case 1: |
| 561 | data->fan_div[ix] = 0; |
| 562 | break; |
| 563 | case 2: |
| 564 | data->fan_div[ix] = 1; |
| 565 | break; |
| 566 | case 4: |
| 567 | data->fan_div[ix] = 2; |
| 568 | break; |
| 569 | case 8: |
| 570 | data->fan_div[ix] = 3; |
| 571 | break; |
| 572 | default: |
| 573 | count = -EINVAL; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 574 | dev_warn(dev, |
| 575 | "fan div value %ld not supported. Choose one of 1, 2, 4, or 8.\n", |
| 576 | val); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 577 | goto EXIT; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 578 | } |
| 579 | vt1211_write8(data, VT1211_REG_FAN_DIV, |
| 580 | ((data->fan_div[1] << 6) | |
| 581 | (data->fan_div[0] << 4) | |
| 582 | data->fan_ctl)); |
| 583 | break; |
| 584 | default: |
| 585 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 586 | } |
| 587 | |
| 588 | EXIT: |
| 589 | mutex_unlock(&data->update_lock); |
| 590 | return count; |
| 591 | } |
| 592 | |
| 593 | /* --------------------------------------------------------------------- |
| 594 | * PWM sysfs interfaces |
| 595 | * ix = [0-1] |
| 596 | * --------------------------------------------------------------------- */ |
| 597 | |
| 598 | #define SHOW_PWM 0 |
| 599 | #define SHOW_SET_PWM_ENABLE 1 |
| 600 | #define SHOW_SET_PWM_FREQ 2 |
| 601 | #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3 |
| 602 | |
| 603 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 604 | char *buf) |
| 605 | { |
| 606 | struct vt1211_data *data = vt1211_update_device(dev); |
| 607 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 608 | to_sensor_dev_attr_2(attr); |
| 609 | int ix = sensor_attr_2->index; |
| 610 | int fn = sensor_attr_2->nr; |
| 611 | int res; |
| 612 | |
| 613 | switch (fn) { |
| 614 | case SHOW_PWM: |
| 615 | res = data->pwm[ix]; |
| 616 | break; |
| 617 | case SHOW_SET_PWM_ENABLE: |
| 618 | res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0; |
| 619 | break; |
| 620 | case SHOW_SET_PWM_FREQ: |
| 621 | res = 90000 >> (data->pwm_clk & 7); |
| 622 | break; |
| 623 | case SHOW_SET_PWM_AUTO_CHANNELS_TEMP: |
| 624 | res = (data->pwm_ctl[ix] & 7) + 1; |
| 625 | break; |
| 626 | default: |
| 627 | res = 0; |
| 628 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 629 | } |
| 630 | |
| 631 | return sprintf(buf, "%d\n", res); |
| 632 | } |
| 633 | |
| 634 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 635 | const char *buf, size_t count) |
| 636 | { |
| 637 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 638 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 639 | to_sensor_dev_attr_2(attr); |
| 640 | int ix = sensor_attr_2->index; |
| 641 | int fn = sensor_attr_2->nr; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 642 | int tmp, reg; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 643 | unsigned long val; |
| 644 | int err; |
| 645 | |
| 646 | err = kstrtoul(buf, 10, &val); |
| 647 | if (err) |
| 648 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 649 | |
| 650 | mutex_lock(&data->update_lock); |
| 651 | |
| 652 | switch (fn) { |
| 653 | case SHOW_SET_PWM_ENABLE: |
| 654 | /* sync the data cache */ |
| 655 | reg = vt1211_read8(data, VT1211_REG_FAN_DIV); |
| 656 | data->fan_div[0] = (reg >> 4) & 3; |
| 657 | data->fan_div[1] = (reg >> 6) & 3; |
| 658 | data->fan_ctl = reg & 0xf; |
| 659 | reg = vt1211_read8(data, VT1211_REG_PWM_CTL); |
| 660 | data->pwm_ctl[0] = reg & 0xf; |
| 661 | data->pwm_ctl[1] = (reg >> 4) & 0xf; |
| 662 | switch (val) { |
| 663 | case 0: |
| 664 | data->pwm_ctl[ix] &= 7; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 665 | /* |
| 666 | * disable SmartGuardian if both PWM outputs are |
| 667 | * disabled |
| 668 | */ |
| 669 | if ((data->pwm_ctl[ix ^ 1] & 1) == 0) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 670 | data->fan_ctl &= 0xe; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 671 | break; |
| 672 | case 2: |
| 673 | data->pwm_ctl[ix] |= 8; |
| 674 | data->fan_ctl |= 1; |
| 675 | break; |
| 676 | default: |
| 677 | count = -EINVAL; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 678 | dev_warn(dev, |
| 679 | "pwm mode %ld not supported. Choose one of 0 or 2.\n", |
| 680 | val); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 681 | goto EXIT; |
| 682 | } |
| 683 | vt1211_write8(data, VT1211_REG_PWM_CTL, |
| 684 | ((data->pwm_ctl[1] << 4) | |
| 685 | data->pwm_ctl[0])); |
| 686 | vt1211_write8(data, VT1211_REG_FAN_DIV, |
| 687 | ((data->fan_div[1] << 6) | |
| 688 | (data->fan_div[0] << 4) | |
| 689 | data->fan_ctl)); |
| 690 | break; |
| 691 | case SHOW_SET_PWM_FREQ: |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 692 | val = 135000 / clamp_val(val, 135000 >> 7, 135000); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 693 | /* calculate tmp = log2(val) */ |
| 694 | tmp = 0; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 695 | for (val >>= 1; val > 0; val >>= 1) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 696 | tmp++; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 697 | /* sync the data cache */ |
| 698 | reg = vt1211_read8(data, VT1211_REG_PWM_CLK); |
| 699 | data->pwm_clk = (reg & 0xf8) | tmp; |
| 700 | vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk); |
| 701 | break; |
| 702 | case SHOW_SET_PWM_AUTO_CHANNELS_TEMP: |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 703 | if (val < 1 || val > 7) { |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 704 | count = -EINVAL; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 705 | dev_warn(dev, |
| 706 | "temp channel %ld not supported. Choose a value between 1 and 7.\n", |
| 707 | val); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 708 | goto EXIT; |
| 709 | } |
| 710 | if (!ISTEMP(val - 1, data->uch_config)) { |
| 711 | count = -EINVAL; |
| 712 | dev_warn(dev, "temp channel %ld is not available.\n", |
| 713 | val); |
| 714 | goto EXIT; |
| 715 | } |
| 716 | /* sync the data cache */ |
| 717 | reg = vt1211_read8(data, VT1211_REG_PWM_CTL); |
| 718 | data->pwm_ctl[0] = reg & 0xf; |
| 719 | data->pwm_ctl[1] = (reg >> 4) & 0xf; |
| 720 | data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1); |
| 721 | vt1211_write8(data, VT1211_REG_PWM_CTL, |
| 722 | ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0])); |
| 723 | break; |
| 724 | default: |
| 725 | dev_dbg(dev, "Unknown attr fetch (%d)\n", fn); |
| 726 | } |
| 727 | |
| 728 | EXIT: |
| 729 | mutex_unlock(&data->update_lock); |
| 730 | return count; |
| 731 | } |
| 732 | |
| 733 | /* --------------------------------------------------------------------- |
| 734 | * PWM auto point definitions |
| 735 | * ix = [0-1] |
| 736 | * ap = [0-3] |
| 737 | * --------------------------------------------------------------------- */ |
| 738 | |
| 739 | /* |
| 740 | * pwm[ix+1]_auto_point[ap+1]_temp mapping table: |
| 741 | * Note that there is only a single set of temp auto points that controls both |
| 742 | * PWM controllers. We still create 2 sets of sysfs files to make it look |
| 743 | * more consistent even though they map to the same registers. |
| 744 | * |
| 745 | * ix ap : description |
| 746 | * ------------------- |
| 747 | * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0]) |
| 748 | * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1]) |
| 749 | * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2]) |
| 750 | * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3]) |
| 751 | * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0]) |
| 752 | * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1]) |
| 753 | * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2]) |
| 754 | * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3]) |
| 755 | */ |
| 756 | |
| 757 | static ssize_t show_pwm_auto_point_temp(struct device *dev, |
| 758 | struct device_attribute *attr, |
| 759 | char *buf) |
| 760 | { |
| 761 | struct vt1211_data *data = vt1211_update_device(dev); |
| 762 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 763 | to_sensor_dev_attr_2(attr); |
| 764 | int ix = sensor_attr_2->index; |
| 765 | int ap = sensor_attr_2->nr; |
| 766 | |
| 767 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7, |
| 768 | data->pwm_auto_temp[ap])); |
| 769 | } |
| 770 | |
| 771 | static ssize_t set_pwm_auto_point_temp(struct device *dev, |
| 772 | struct device_attribute *attr, |
| 773 | const char *buf, size_t count) |
| 774 | { |
| 775 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 776 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 777 | to_sensor_dev_attr_2(attr); |
| 778 | int ix = sensor_attr_2->index; |
| 779 | int ap = sensor_attr_2->nr; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 780 | int reg; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 781 | long val; |
| 782 | int err; |
| 783 | |
| 784 | err = kstrtol(buf, 10, &val); |
| 785 | if (err) |
| 786 | return err; |
| 787 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 788 | |
| 789 | mutex_lock(&data->update_lock); |
| 790 | |
| 791 | /* sync the data cache */ |
| 792 | reg = vt1211_read8(data, VT1211_REG_PWM_CTL); |
| 793 | data->pwm_ctl[0] = reg & 0xf; |
| 794 | data->pwm_ctl[1] = (reg >> 4) & 0xf; |
| 795 | |
| 796 | data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val); |
| 797 | vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap), |
| 798 | data->pwm_auto_temp[ap]); |
| 799 | mutex_unlock(&data->update_lock); |
| 800 | |
| 801 | return count; |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * pwm[ix+1]_auto_point[ap+1]_pwm mapping table: |
| 806 | * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't |
| 807 | * be changed. |
| 808 | * |
| 809 | * ix ap : description |
| 810 | * ------------------- |
| 811 | * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0) |
| 812 | * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1]) |
| 813 | * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2]) |
| 814 | * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255) |
| 815 | * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0) |
| 816 | * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1]) |
| 817 | * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2]) |
| 818 | * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255) |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 819 | */ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 820 | |
| 821 | static ssize_t show_pwm_auto_point_pwm(struct device *dev, |
| 822 | struct device_attribute *attr, |
| 823 | char *buf) |
| 824 | { |
| 825 | struct vt1211_data *data = vt1211_update_device(dev); |
| 826 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 827 | to_sensor_dev_attr_2(attr); |
| 828 | int ix = sensor_attr_2->index; |
| 829 | int ap = sensor_attr_2->nr; |
| 830 | |
| 831 | return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]); |
| 832 | } |
| 833 | |
| 834 | static ssize_t set_pwm_auto_point_pwm(struct device *dev, |
| 835 | struct device_attribute *attr, |
| 836 | const char *buf, size_t count) |
| 837 | { |
| 838 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 839 | struct sensor_device_attribute_2 *sensor_attr_2 = |
| 840 | to_sensor_dev_attr_2(attr); |
| 841 | int ix = sensor_attr_2->index; |
| 842 | int ap = sensor_attr_2->nr; |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 843 | unsigned long val; |
| 844 | int err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 845 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 846 | err = kstrtoul(buf, 10, &val); |
| 847 | if (err) |
| 848 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 849 | |
| 850 | mutex_lock(&data->update_lock); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 851 | data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 852 | vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap), |
| 853 | data->pwm_auto_pwm[ix][ap]); |
| 854 | mutex_unlock(&data->update_lock); |
| 855 | |
| 856 | return count; |
| 857 | } |
| 858 | |
| 859 | /* --------------------------------------------------------------------- |
| 860 | * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms) |
| 861 | * --------------------------------------------------------------------- */ |
| 862 | |
| 863 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 864 | char *buf) |
| 865 | { |
| 866 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 867 | |
| 868 | return sprintf(buf, "%d\n", data->vrm); |
| 869 | } |
| 870 | |
| 871 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 872 | const char *buf, size_t count) |
| 873 | { |
| 874 | struct vt1211_data *data = dev_get_drvdata(dev); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 875 | unsigned long val; |
| 876 | int err; |
| 877 | |
| 878 | err = kstrtoul(buf, 10, &val); |
| 879 | if (err) |
| 880 | return err; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 881 | |
| 882 | data->vrm = val; |
| 883 | |
| 884 | return count; |
| 885 | } |
| 886 | |
| 887 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 888 | char *buf) |
| 889 | { |
| 890 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 891 | |
| 892 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 893 | } |
| 894 | |
| 895 | static ssize_t show_name(struct device *dev, |
| 896 | struct device_attribute *attr, char *buf) |
| 897 | { |
| 898 | struct vt1211_data *data = dev_get_drvdata(dev); |
| 899 | |
| 900 | return sprintf(buf, "%s\n", data->name); |
| 901 | } |
| 902 | |
| 903 | static ssize_t show_alarms(struct device *dev, |
| 904 | struct device_attribute *attr, char *buf) |
| 905 | { |
| 906 | struct vt1211_data *data = vt1211_update_device(dev); |
| 907 | |
| 908 | return sprintf(buf, "%d\n", data->alarms); |
| 909 | } |
| 910 | |
| 911 | /* --------------------------------------------------------------------- |
| 912 | * Device attribute structs |
| 913 | * --------------------------------------------------------------------- */ |
| 914 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 915 | #define SENSOR_ATTR_IN(ix) \ |
| 916 | { SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \ |
| 917 | show_in, NULL, SHOW_IN_INPUT, ix), \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 918 | SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 919 | show_in, set_in, SHOW_SET_IN_MIN, ix), \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 920 | SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 921 | show_in, set_in, SHOW_SET_IN_MAX, ix), \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 922 | SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 923 | show_in, NULL, SHOW_IN_ALARM, ix) \ |
| 924 | } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 925 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 926 | static struct sensor_device_attribute_2 vt1211_sysfs_in[][4] = { |
| 927 | SENSOR_ATTR_IN(0), |
| 928 | SENSOR_ATTR_IN(1), |
| 929 | SENSOR_ATTR_IN(2), |
| 930 | SENSOR_ATTR_IN(3), |
| 931 | SENSOR_ATTR_IN(4), |
| 932 | SENSOR_ATTR_IN(5) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 933 | }; |
| 934 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 935 | #define IN_UNIT_ATTRS(X) \ |
| 936 | { &vt1211_sysfs_in[X][0].dev_attr.attr, \ |
| 937 | &vt1211_sysfs_in[X][1].dev_attr.attr, \ |
| 938 | &vt1211_sysfs_in[X][2].dev_attr.attr, \ |
| 939 | &vt1211_sysfs_in[X][3].dev_attr.attr, \ |
| 940 | NULL \ |
| 941 | } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 942 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 943 | static struct attribute *vt1211_in_attr[][5] = { |
| 944 | IN_UNIT_ATTRS(0), |
| 945 | IN_UNIT_ATTRS(1), |
| 946 | IN_UNIT_ATTRS(2), |
| 947 | IN_UNIT_ATTRS(3), |
| 948 | IN_UNIT_ATTRS(4), |
| 949 | IN_UNIT_ATTRS(5) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 950 | }; |
| 951 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 952 | static const struct attribute_group vt1211_in_attr_group[] = { |
| 953 | { .attrs = vt1211_in_attr[0] }, |
| 954 | { .attrs = vt1211_in_attr[1] }, |
| 955 | { .attrs = vt1211_in_attr[2] }, |
| 956 | { .attrs = vt1211_in_attr[3] }, |
| 957 | { .attrs = vt1211_in_attr[4] }, |
| 958 | { .attrs = vt1211_in_attr[5] } |
| 959 | }; |
| 960 | |
| 961 | #define SENSOR_ATTR_TEMP(ix) \ |
| 962 | { SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \ |
| 963 | show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 964 | SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 965 | show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 966 | SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 967 | show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \ |
| 968 | SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \ |
| 969 | show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \ |
| 970 | } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 971 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 972 | static struct sensor_device_attribute_2 vt1211_sysfs_temp[][4] = { |
| 973 | SENSOR_ATTR_TEMP(1), |
| 974 | SENSOR_ATTR_TEMP(2), |
| 975 | SENSOR_ATTR_TEMP(3), |
| 976 | SENSOR_ATTR_TEMP(4), |
| 977 | SENSOR_ATTR_TEMP(5), |
| 978 | SENSOR_ATTR_TEMP(6), |
| 979 | SENSOR_ATTR_TEMP(7), |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 980 | }; |
| 981 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 982 | #define TEMP_UNIT_ATTRS(X) \ |
| 983 | { &vt1211_sysfs_temp[X][0].dev_attr.attr, \ |
| 984 | &vt1211_sysfs_temp[X][1].dev_attr.attr, \ |
| 985 | &vt1211_sysfs_temp[X][2].dev_attr.attr, \ |
| 986 | &vt1211_sysfs_temp[X][3].dev_attr.attr, \ |
| 987 | NULL \ |
| 988 | } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 989 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 990 | static struct attribute *vt1211_temp_attr[][5] = { |
| 991 | TEMP_UNIT_ATTRS(0), |
| 992 | TEMP_UNIT_ATTRS(1), |
| 993 | TEMP_UNIT_ATTRS(2), |
| 994 | TEMP_UNIT_ATTRS(3), |
| 995 | TEMP_UNIT_ATTRS(4), |
| 996 | TEMP_UNIT_ATTRS(5), |
| 997 | TEMP_UNIT_ATTRS(6) |
| 998 | }; |
| 999 | |
| 1000 | static const struct attribute_group vt1211_temp_attr_group[] = { |
| 1001 | { .attrs = vt1211_temp_attr[0] }, |
| 1002 | { .attrs = vt1211_temp_attr[1] }, |
| 1003 | { .attrs = vt1211_temp_attr[2] }, |
| 1004 | { .attrs = vt1211_temp_attr[3] }, |
| 1005 | { .attrs = vt1211_temp_attr[4] }, |
| 1006 | { .attrs = vt1211_temp_attr[5] }, |
| 1007 | { .attrs = vt1211_temp_attr[6] } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1008 | }; |
| 1009 | |
| 1010 | #define SENSOR_ATTR_FAN(ix) \ |
| 1011 | SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \ |
| 1012 | show_fan, NULL, SHOW_FAN_INPUT, ix-1), \ |
| 1013 | SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \ |
| 1014 | show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \ |
| 1015 | SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \ |
| 1016 | show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \ |
| 1017 | SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \ |
| 1018 | show_fan, NULL, SHOW_FAN_ALARM, ix-1) |
| 1019 | |
| 1020 | #define SENSOR_ATTR_PWM(ix) \ |
| 1021 | SENSOR_ATTR_2(pwm##ix, S_IRUGO, \ |
| 1022 | show_pwm, NULL, SHOW_PWM, ix-1), \ |
| 1023 | SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \ |
| 1024 | show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \ |
| 1025 | SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \ |
| 1026 | show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1) |
| 1027 | |
| 1028 | #define SENSOR_ATTR_PWM_FREQ(ix) \ |
| 1029 | SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \ |
| 1030 | show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1) |
| 1031 | |
| 1032 | #define SENSOR_ATTR_PWM_FREQ_RO(ix) \ |
| 1033 | SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \ |
| 1034 | show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1) |
| 1035 | |
| 1036 | #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \ |
| 1037 | SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \ |
| 1038 | show_pwm_auto_point_temp, set_pwm_auto_point_temp, \ |
| 1039 | ap-1, ix-1) |
| 1040 | |
| 1041 | #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \ |
| 1042 | SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \ |
| 1043 | show_pwm_auto_point_temp, NULL, \ |
| 1044 | ap-1, ix-1) |
| 1045 | |
| 1046 | #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \ |
| 1047 | SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \ |
| 1048 | show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \ |
| 1049 | ap-1, ix-1) |
| 1050 | |
| 1051 | #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \ |
| 1052 | SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \ |
| 1053 | show_pwm_auto_point_pwm, NULL, \ |
| 1054 | ap-1, ix-1) |
| 1055 | |
| 1056 | static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = { |
| 1057 | SENSOR_ATTR_FAN(1), |
| 1058 | SENSOR_ATTR_FAN(2), |
| 1059 | SENSOR_ATTR_PWM(1), |
| 1060 | SENSOR_ATTR_PWM(2), |
| 1061 | SENSOR_ATTR_PWM_FREQ(1), |
| 1062 | SENSOR_ATTR_PWM_FREQ_RO(2), |
| 1063 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1), |
| 1064 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2), |
| 1065 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3), |
| 1066 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4), |
| 1067 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1), |
| 1068 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2), |
| 1069 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3), |
| 1070 | SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4), |
| 1071 | SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1), |
| 1072 | SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2), |
| 1073 | SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3), |
| 1074 | SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4), |
| 1075 | SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1), |
| 1076 | SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2), |
| 1077 | SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3), |
| 1078 | SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4), |
| 1079 | }; |
| 1080 | |
| 1081 | static struct device_attribute vt1211_sysfs_misc[] = { |
| 1082 | __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm), |
| 1083 | __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL), |
| 1084 | __ATTR(name, S_IRUGO, show_name, NULL), |
| 1085 | __ATTR(alarms, S_IRUGO, show_alarms, NULL), |
| 1086 | }; |
| 1087 | |
| 1088 | /* --------------------------------------------------------------------- |
| 1089 | * Device registration and initialization |
| 1090 | * --------------------------------------------------------------------- */ |
| 1091 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 1092 | static void vt1211_init_device(struct vt1211_data *data) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1093 | { |
| 1094 | /* set VRM */ |
| 1095 | data->vrm = vid_which_vrm(); |
| 1096 | |
| 1097 | /* Read (and initialize) UCH config */ |
| 1098 | data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG); |
| 1099 | if (uch_config > -1) { |
| 1100 | data->uch_config = (data->uch_config & 0x83) | |
| 1101 | (uch_config << 2); |
| 1102 | vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config); |
| 1103 | } |
| 1104 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1105 | /* |
| 1106 | * Initialize the interrupt mode (if request at module load time). |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1107 | * The VT1211 implements 3 different modes for clearing interrupts: |
| 1108 | * 0: Clear INT when status register is read. Regenerate INT as long |
| 1109 | * as temp stays above hysteresis limit. |
| 1110 | * 1: Clear INT when status register is read. DON'T regenerate INT |
| 1111 | * until temp falls below hysteresis limit and exceeds hot limit |
| 1112 | * again. |
| 1113 | * 2: Clear INT when temp falls below max limit. |
| 1114 | * |
| 1115 | * The driver only allows to force mode 0 since that's the only one |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1116 | * that makes sense for 'sensors' |
| 1117 | */ |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1118 | if (int_mode == 0) { |
| 1119 | vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0); |
| 1120 | vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0); |
| 1121 | } |
| 1122 | |
| 1123 | /* Fill in some hard wired values into our data struct */ |
| 1124 | data->pwm_auto_pwm[0][3] = 255; |
| 1125 | data->pwm_auto_pwm[1][3] = 255; |
| 1126 | } |
| 1127 | |
| 1128 | static void vt1211_remove_sysfs(struct platform_device *pdev) |
| 1129 | { |
| 1130 | struct device *dev = &pdev->dev; |
| 1131 | int i; |
| 1132 | |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 1133 | for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) |
| 1134 | sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]); |
| 1135 | |
| 1136 | for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) |
| 1137 | sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]); |
| 1138 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1139 | for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) { |
| 1140 | device_remove_file(dev, |
| 1141 | &vt1211_sysfs_fan_pwm[i].dev_attr); |
| 1142 | } |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1143 | for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1144 | device_remove_file(dev, &vt1211_sysfs_misc[i]); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 1147 | static int vt1211_probe(struct platform_device *pdev) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1148 | { |
| 1149 | struct device *dev = &pdev->dev; |
| 1150 | struct vt1211_data *data; |
| 1151 | struct resource *res; |
| 1152 | int i, err; |
| 1153 | |
Guenter Roeck | 84269ed | 2012-06-02 11:35:54 -0700 | [diff] [blame] | 1154 | data = devm_kzalloc(dev, sizeof(struct vt1211_data), GFP_KERNEL); |
Jingoo Han | 5b20995 | 2014-04-29 17:13:50 +0900 | [diff] [blame] | 1155 | if (!data) |
Guenter Roeck | 84269ed | 2012-06-02 11:35:54 -0700 | [diff] [blame] | 1156 | return -ENOMEM; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1157 | |
| 1158 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
Guenter Roeck | 84269ed | 2012-06-02 11:35:54 -0700 | [diff] [blame] | 1159 | if (!devm_request_region(dev, res->start, resource_size(res), |
| 1160 | DRVNAME)) { |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 1161 | dev_err(dev, "Failed to request region 0x%lx-0x%lx\n", |
| 1162 | (unsigned long)res->start, (unsigned long)res->end); |
Guenter Roeck | 84269ed | 2012-06-02 11:35:54 -0700 | [diff] [blame] | 1163 | return -EBUSY; |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 1164 | } |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1165 | data->addr = res->start; |
| 1166 | data->name = DRVNAME; |
| 1167 | mutex_init(&data->update_lock); |
| 1168 | |
| 1169 | platform_set_drvdata(pdev, data); |
| 1170 | |
| 1171 | /* Initialize the VT1211 chip */ |
| 1172 | vt1211_init_device(data); |
| 1173 | |
| 1174 | /* Create sysfs interface files */ |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 1175 | for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) { |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1176 | if (ISVOLT(i, data->uch_config)) { |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 1177 | err = sysfs_create_group(&dev->kobj, |
| 1178 | &vt1211_in_attr_group[i]); |
| 1179 | if (err) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1180 | goto EXIT_DEV_REMOVE; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1181 | } |
| 1182 | } |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 1183 | for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) { |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1184 | if (ISTEMP(i, data->uch_config)) { |
Guenter Roeck | 2185696 | 2012-01-19 21:49:20 -0800 | [diff] [blame] | 1185 | err = sysfs_create_group(&dev->kobj, |
| 1186 | &vt1211_temp_attr_group[i]); |
| 1187 | if (err) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1188 | goto EXIT_DEV_REMOVE; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) { |
| 1192 | err = device_create_file(dev, |
| 1193 | &vt1211_sysfs_fan_pwm[i].dev_attr); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1194 | if (err) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1195 | goto EXIT_DEV_REMOVE; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1196 | } |
| 1197 | for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) { |
| 1198 | err = device_create_file(dev, |
| 1199 | &vt1211_sysfs_misc[i]); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1200 | if (err) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1201 | goto EXIT_DEV_REMOVE; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* Register device */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1205 | data->hwmon_dev = hwmon_device_register(dev); |
| 1206 | if (IS_ERR(data->hwmon_dev)) { |
| 1207 | err = PTR_ERR(data->hwmon_dev); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1208 | dev_err(dev, "Class registration failed (%d)\n", err); |
| 1209 | goto EXIT_DEV_REMOVE_SILENT; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | |
| 1214 | EXIT_DEV_REMOVE: |
| 1215 | dev_err(dev, "Sysfs interface creation failed (%d)\n", err); |
| 1216 | EXIT_DEV_REMOVE_SILENT: |
| 1217 | vt1211_remove_sysfs(pdev); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1218 | return err; |
| 1219 | } |
| 1220 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 1221 | static int vt1211_remove(struct platform_device *pdev) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1222 | { |
| 1223 | struct vt1211_data *data = platform_get_drvdata(pdev); |
| 1224 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1225 | hwmon_device_unregister(data->hwmon_dev); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1226 | vt1211_remove_sysfs(pdev); |
Jean Delvare | ce7ee4e | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 1227 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static struct platform_driver vt1211_driver = { |
| 1232 | .driver = { |
| 1233 | .owner = THIS_MODULE, |
| 1234 | .name = DRVNAME, |
| 1235 | }, |
| 1236 | .probe = vt1211_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 1237 | .remove = vt1211_remove, |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1238 | }; |
| 1239 | |
| 1240 | static int __init vt1211_device_add(unsigned short address) |
| 1241 | { |
| 1242 | struct resource res = { |
| 1243 | .start = address, |
| 1244 | .end = address + 0x7f, |
| 1245 | .flags = IORESOURCE_IO, |
| 1246 | }; |
| 1247 | int err; |
| 1248 | |
| 1249 | pdev = platform_device_alloc(DRVNAME, address); |
| 1250 | if (!pdev) { |
| 1251 | err = -ENOMEM; |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1252 | pr_err("Device allocation failed (%d)\n", err); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1253 | goto EXIT; |
| 1254 | } |
| 1255 | |
| 1256 | res.name = pdev->name; |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 1257 | err = acpi_check_resource_conflict(&res); |
| 1258 | if (err) |
Hans de Goede | 18632f8 | 2009-02-17 19:59:54 +0100 | [diff] [blame] | 1259 | goto EXIT_DEV_PUT; |
Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 1260 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1261 | err = platform_device_add_resources(pdev, &res, 1); |
| 1262 | if (err) { |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1263 | pr_err("Device resource addition failed (%d)\n", err); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1264 | goto EXIT_DEV_PUT; |
| 1265 | } |
| 1266 | |
| 1267 | err = platform_device_add(pdev); |
| 1268 | if (err) { |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1269 | pr_err("Device addition failed (%d)\n", err); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1270 | goto EXIT_DEV_PUT; |
| 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | |
| 1275 | EXIT_DEV_PUT: |
| 1276 | platform_device_put(pdev); |
| 1277 | EXIT: |
| 1278 | return err; |
| 1279 | } |
| 1280 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1281 | static int __init vt1211_find(int sio_cip, unsigned short *address) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1282 | { |
| 1283 | int err = -ENODEV; |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 1284 | int devid; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1285 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1286 | superio_enter(sio_cip); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1287 | |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 1288 | devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1289 | if (devid != SIO_VT1211_ID) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1290 | goto EXIT; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1291 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1292 | superio_select(sio_cip, SIO_VT1211_LDN_HWMON); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1293 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1294 | if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) { |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1295 | pr_warn("HW monitor is disabled, skipping\n"); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1296 | goto EXIT; |
| 1297 | } |
| 1298 | |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1299 | *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) | |
| 1300 | (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1301 | if (*address == 0) { |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1302 | pr_warn("Base address is not set, skipping\n"); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1303 | goto EXIT; |
| 1304 | } |
| 1305 | |
| 1306 | err = 0; |
Joe Perches | 5ed9ba6 | 2010-10-20 06:51:52 +0000 | [diff] [blame] | 1307 | pr_info("Found VT1211 chip at 0x%04x, revision %u\n", |
| 1308 | *address, superio_inb(sio_cip, SIO_VT1211_DEVREV)); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1309 | |
| 1310 | EXIT: |
Juerg Haefliger | 2219cd8 | 2007-02-14 21:15:05 +0100 | [diff] [blame] | 1311 | superio_exit(sio_cip); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1312 | return err; |
| 1313 | } |
| 1314 | |
| 1315 | static int __init vt1211_init(void) |
| 1316 | { |
| 1317 | int err; |
| 1318 | unsigned short address = 0; |
| 1319 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1320 | err = vt1211_find(SIO_REG_CIP1, &address); |
| 1321 | if (err) { |
| 1322 | err = vt1211_find(SIO_REG_CIP2, &address); |
| 1323 | if (err) |
| 1324 | goto EXIT; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | if ((uch_config < -1) || (uch_config > 31)) { |
| 1328 | err = -EINVAL; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 1329 | pr_warn("Invalid UCH configuration %d. Choose a value between 0 and 31.\n", |
| 1330 | uch_config); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1331 | goto EXIT; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | if ((int_mode < -1) || (int_mode > 0)) { |
| 1335 | err = -EINVAL; |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 1336 | pr_warn("Invalid interrupt mode %d. Only mode 0 is supported.\n", |
| 1337 | int_mode); |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1338 | goto EXIT; |
| 1339 | } |
| 1340 | |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1341 | err = platform_driver_register(&vt1211_driver); |
| 1342 | if (err) |
| 1343 | goto EXIT; |
| 1344 | |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1345 | /* Sets global pdev as a side effect */ |
| 1346 | err = vt1211_device_add(address); |
Guenter Roeck | b162c03 | 2012-01-15 06:52:33 -0800 | [diff] [blame] | 1347 | if (err) |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1348 | goto EXIT_DRV_UNREGISTER; |
Juerg Haefliger | ab41319 | 2006-09-24 20:54:04 +0200 | [diff] [blame] | 1349 | |
| 1350 | return 0; |
| 1351 | |
| 1352 | EXIT_DRV_UNREGISTER: |
| 1353 | platform_driver_unregister(&vt1211_driver); |
| 1354 | EXIT: |
| 1355 | return err; |
| 1356 | } |
| 1357 | |
| 1358 | static void __exit vt1211_exit(void) |
| 1359 | { |
| 1360 | platform_device_unregister(pdev); |
| 1361 | platform_driver_unregister(&vt1211_driver); |
| 1362 | } |
| 1363 | |
| 1364 | MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>"); |
| 1365 | MODULE_DESCRIPTION("VT1211 sensors"); |
| 1366 | MODULE_LICENSE("GPL"); |
| 1367 | |
| 1368 | module_init(vt1211_init); |
| 1369 | module_exit(vt1211_exit); |