Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | smsc47m1.c - Part of lm_sensors, Linux kernel modules |
| 3 | for hardware monitoring |
| 4 | |
Jean Delvare | 6091780 | 2006-10-08 22:00:44 +0200 | [diff] [blame] | 5 | Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 6 | LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997 |
| 7 | Super-I/O chips. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com> |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 10 | Copyright (C) 2004-2007 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com> |
| 12 | and Jean Delvare |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/ioport.h> |
| 32 | #include <linux/jiffies.h> |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 34 | #include <linux/hwmon.h> |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 35 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 36 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 39 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/io.h> |
| 41 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 42 | static struct platform_device *pdev; |
| 43 | |
| 44 | #define DRVNAME "smsc47m1" |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 45 | enum chips { smsc47m1, smsc47m2 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* Super-I/0 registers and commands */ |
| 48 | |
| 49 | #define REG 0x2e /* The register to read/write */ |
| 50 | #define VAL 0x2f /* The value to read/write */ |
| 51 | |
| 52 | static inline void |
| 53 | superio_outb(int reg, int val) |
| 54 | { |
| 55 | outb(reg, REG); |
| 56 | outb(val, VAL); |
| 57 | } |
| 58 | |
| 59 | static inline int |
| 60 | superio_inb(int reg) |
| 61 | { |
| 62 | outb(reg, REG); |
| 63 | return inb(VAL); |
| 64 | } |
| 65 | |
| 66 | /* logical device for fans is 0x0A */ |
| 67 | #define superio_select() superio_outb(0x07, 0x0A) |
| 68 | |
| 69 | static inline void |
| 70 | superio_enter(void) |
| 71 | { |
| 72 | outb(0x55, REG); |
| 73 | } |
| 74 | |
| 75 | static inline void |
| 76 | superio_exit(void) |
| 77 | { |
| 78 | outb(0xAA, REG); |
| 79 | } |
| 80 | |
| 81 | #define SUPERIO_REG_ACT 0x30 |
| 82 | #define SUPERIO_REG_BASE 0x60 |
| 83 | #define SUPERIO_REG_DEVID 0x20 |
| 84 | |
| 85 | /* Logical device registers */ |
| 86 | |
| 87 | #define SMSC_EXTENT 0x80 |
| 88 | |
| 89 | /* nr is 0 or 1 in the macros below */ |
| 90 | #define SMSC47M1_REG_ALARM 0x04 |
| 91 | #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr)) |
| 92 | #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #define SMSC47M1_REG_FANDIV 0x58 |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 94 | |
| 95 | static const u8 SMSC47M1_REG_FAN[3] = { 0x59, 0x5a, 0x6b }; |
| 96 | static const u8 SMSC47M1_REG_FAN_PRELOAD[3] = { 0x5b, 0x5c, 0x6c }; |
| 97 | static const u8 SMSC47M1_REG_PWM[3] = { 0x56, 0x57, 0x69 }; |
| 98 | |
| 99 | #define SMSC47M2_REG_ALARM6 0x09 |
| 100 | #define SMSC47M2_REG_TPIN1 0x38 |
| 101 | #define SMSC47M2_REG_TPIN2 0x37 |
| 102 | #define SMSC47M2_REG_TPIN3 0x2d |
| 103 | #define SMSC47M2_REG_PPIN3 0x2c |
| 104 | #define SMSC47M2_REG_FANDIV3 0x6a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | #define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \ |
| 107 | 983040/((192-(reg))*(div))) |
| 108 | #define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \ |
| 109 | 983040/(((reg)-(preload))*(div))) |
| 110 | #define DIV_FROM_REG(reg) (1 << (reg)) |
| 111 | #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1) |
| 112 | #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01) |
| 113 | #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E) |
| 114 | |
| 115 | struct smsc47m1_data { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 116 | unsigned short addr; |
| 117 | const char *name; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 118 | enum chips type; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 119 | struct class_device *class_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 121 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | unsigned long last_updated; /* In jiffies */ |
| 123 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 124 | u8 fan[3]; /* Register value */ |
| 125 | u8 fan_preload[3]; /* Register value */ |
| 126 | u8 fan_div[3]; /* Register encoding, shifted right */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | u8 alarms; /* Register encoding */ |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 128 | u8 pwm[3]; /* Register value (bit 0 is disable) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 131 | struct smsc47m1_sio_data { |
| 132 | enum chips type; |
| 133 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 135 | |
| 136 | static int smsc47m1_probe(struct platform_device *pdev); |
Jean Delvare | d054612 | 2007-07-22 12:09:48 +0200 | [diff] [blame] | 137 | static int __devexit smsc47m1_remove(struct platform_device *pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, |
| 139 | int init); |
| 140 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 141 | static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg) |
Jean Delvare | 94e183f | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 142 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 143 | return inb_p(data->addr + reg); |
Jean Delvare | 94e183f | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 146 | static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg, |
Jean Delvare | 94e183f | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 147 | u8 value) |
| 148 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 149 | outb_p(value, data->addr + reg); |
Jean Delvare | 94e183f | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 152 | static struct platform_driver smsc47m1_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 153 | .driver = { |
Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 154 | .owner = THIS_MODULE, |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 155 | .name = DRVNAME, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 156 | }, |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 157 | .probe = smsc47m1_probe, |
| 158 | .remove = __devexit_p(smsc47m1_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 161 | static ssize_t get_fan(struct device *dev, struct device_attribute |
| 162 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 164 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 166 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* This chip (stupidly) stops monitoring fan speed if PWM is |
| 168 | enabled and duty cycle is 0%. This is fine if the monitoring |
| 169 | and control concern the same fan, but troublesome if they are |
| 170 | not (which could as well happen). */ |
| 171 | int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 : |
| 172 | FAN_FROM_REG(data->fan[nr], |
| 173 | DIV_FROM_REG(data->fan_div[nr]), |
| 174 | data->fan_preload[nr]); |
| 175 | return sprintf(buf, "%d\n", rpm); |
| 176 | } |
| 177 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 178 | static ssize_t get_fan_min(struct device *dev, struct device_attribute |
| 179 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 181 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 183 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int rpm = MIN_FROM_REG(data->fan_preload[nr], |
| 185 | DIV_FROM_REG(data->fan_div[nr])); |
| 186 | return sprintf(buf, "%d\n", rpm); |
| 187 | } |
| 188 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 189 | static ssize_t get_fan_div(struct device *dev, struct device_attribute |
| 190 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 192 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 194 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 197 | static ssize_t get_pwm(struct device *dev, struct device_attribute |
| 198 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 200 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 202 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 205 | static ssize_t get_pwm_en(struct device *dev, struct device_attribute |
| 206 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 208 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 210 | return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 213 | static ssize_t get_alarms(struct device *dev, struct device_attribute |
| 214 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 217 | return sprintf(buf, "%d\n", data->alarms); |
| 218 | } |
| 219 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 220 | static ssize_t set_fan_min(struct device *dev, struct device_attribute |
| 221 | *devattr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 223 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 224 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 225 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | long rpmdiv, val = simple_strtol(buf, NULL, 10); |
| 227 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 228 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]); |
| 230 | |
| 231 | if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) { |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 232 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 237 | smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | data->fan_preload[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 239 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | return count; |
| 242 | } |
| 243 | |
| 244 | /* Note: we save and restore the fan minimum here, because its value is |
| 245 | determined in part by the fan clock divider. This follows the principle |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 246 | of least surprise; the user doesn't expect the fan minimum to change just |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | because the divider changed. */ |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 248 | static ssize_t set_fan_div(struct device *dev, struct device_attribute |
| 249 | *devattr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 251 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 252 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 253 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | long new_div = simple_strtol(buf, NULL, 10), tmp; |
| 255 | u8 old_div = DIV_FROM_REG(data->fan_div[nr]); |
| 256 | |
| 257 | if (new_div == old_div) /* No change */ |
| 258 | return count; |
| 259 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 260 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | switch (new_div) { |
| 262 | case 1: data->fan_div[nr] = 0; break; |
| 263 | case 2: data->fan_div[nr] = 1; break; |
| 264 | case 4: data->fan_div[nr] = 2; break; |
| 265 | case 8: data->fan_div[nr] = 3; break; |
| 266 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 267 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
| 270 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 271 | switch (nr) { |
| 272 | case 0: |
| 273 | case 1: |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 274 | tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 275 | & ~(0x03 << (4 + 2 * nr)); |
| 276 | tmp |= data->fan_div[nr] << (4 + 2 * nr); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 277 | smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 278 | break; |
| 279 | case 2: |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 280 | tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 281 | tmp |= data->fan_div[2] << 4; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 282 | smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 283 | break; |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* Preserve fan min */ |
| 287 | tmp = 192 - (old_div * (192 - data->fan_preload[nr]) |
| 288 | + new_div / 2) / new_div; |
| 289 | data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 290 | smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | data->fan_preload[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 292 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | return count; |
| 295 | } |
| 296 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 297 | static ssize_t set_pwm(struct device *dev, struct device_attribute |
| 298 | *devattr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 300 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 301 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 302 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | long val = simple_strtol(buf, NULL, 10); |
| 304 | |
| 305 | if (val < 0 || val > 255) |
| 306 | return -EINVAL; |
| 307 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 308 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | data->pwm[nr] &= 0x81; /* Preserve additional bits */ |
| 310 | data->pwm[nr] |= PWM_TO_REG(val); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 311 | smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | data->pwm[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 313 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | return count; |
| 316 | } |
| 317 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 318 | static ssize_t set_pwm_en(struct device *dev, struct device_attribute |
| 319 | *devattr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 321 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 322 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 323 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | long val = simple_strtol(buf, NULL, 10); |
| 325 | |
| 326 | if (val != 0 && val != 1) |
| 327 | return -EINVAL; |
| 328 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 329 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | data->pwm[nr] &= 0xFE; /* preserve the other bits */ |
| 331 | data->pwm[nr] |= !val; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 332 | smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | data->pwm[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 334 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | return count; |
| 337 | } |
| 338 | |
| 339 | #define fan_present(offset) \ |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 340 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan, \ |
| 341 | NULL, offset - 1); \ |
| 342 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 343 | get_fan_min, set_fan_min, offset - 1); \ |
| 344 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 345 | get_fan_div, set_fan_div, offset - 1); \ |
| 346 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
| 347 | get_pwm, set_pwm, offset - 1); \ |
| 348 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
| 349 | get_pwm_en, set_pwm_en, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | fan_present(1); |
| 352 | fan_present(2); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 353 | fan_present(3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL); |
| 356 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 357 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 358 | *devattr, char *buf) |
| 359 | { |
| 360 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
| 361 | |
| 362 | return sprintf(buf, "%s\n", data->name); |
| 363 | } |
| 364 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 365 | |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 366 | /* Almost all sysfs files may or may not be created depending on the chip |
| 367 | setup so we create them individually. It is still convenient to define a |
| 368 | group to remove them all at once. */ |
| 369 | static struct attribute *smsc47m1_attributes[] = { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 370 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 371 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 372 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 373 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 374 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 375 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 376 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 377 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 378 | &sensor_dev_attr_fan3_div.dev_attr.attr, |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 379 | |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 380 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 381 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 382 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 383 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 384 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 385 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 386 | |
| 387 | &dev_attr_alarms.attr, |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 388 | &dev_attr_name.attr, |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 389 | NULL |
| 390 | }; |
| 391 | |
| 392 | static const struct attribute_group smsc47m1_group = { |
| 393 | .attrs = smsc47m1_attributes, |
| 394 | }; |
| 395 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 396 | static int __init smsc47m1_find(unsigned short *addr, |
| 397 | struct smsc47m1_sio_data *sio_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | u8 val; |
| 400 | |
| 401 | superio_enter(); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 402 | val = superio_inb(SUPERIO_REG_DEVID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | /* |
Jean Delvare | 6091780 | 2006-10-08 22:00:44 +0200 | [diff] [blame] | 405 | * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x |
| 406 | * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * The LPC47M15x and LPC47M192 chips "with hardware monitoring block" |
Jean Delvare | ec5ce55 | 2005-04-26 22:09:43 +0200 | [diff] [blame] | 408 | * can do much more besides (device id 0x60). |
Jean Delvare | b890a07 | 2005-10-26 22:21:24 +0200 | [diff] [blame] | 409 | * The LPC47M997 is undocumented, but seems to be compatible with |
| 410 | * the LPC47M192, and has the same device id. |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 411 | * The LPC47M292 (device id 0x6B) is somewhat compatible, but it |
| 412 | * supports a 3rd fan, and the pin configuration registers are |
| 413 | * unfortunately different. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | */ |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 415 | switch (val) { |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 416 | case 0x51: |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 417 | pr_info(DRVNAME ": Found SMSC LPC47B27x\n"); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 418 | sio_data->type = smsc47m1; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 419 | break; |
| 420 | case 0x59: |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 421 | pr_info(DRVNAME ": Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n"); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 422 | sio_data->type = smsc47m1; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 423 | break; |
| 424 | case 0x5F: |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 425 | pr_info(DRVNAME ": Found SMSC LPC47M14x\n"); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 426 | sio_data->type = smsc47m1; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 427 | break; |
| 428 | case 0x60: |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 429 | pr_info(DRVNAME ": Found SMSC LPC47M15x/LPC47M192/LPC47M997\n"); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 430 | sio_data->type = smsc47m1; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 431 | break; |
| 432 | case 0x6B: |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 433 | pr_info(DRVNAME ": Found SMSC LPC47M292\n"); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 434 | sio_data->type = smsc47m2; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 435 | break; |
| 436 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | superio_exit(); |
| 438 | return -ENODEV; |
| 439 | } |
| 440 | |
| 441 | superio_select(); |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 442 | *addr = (superio_inb(SUPERIO_REG_BASE) << 8) |
| 443 | | superio_inb(SUPERIO_REG_BASE + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | val = superio_inb(SUPERIO_REG_ACT); |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 445 | if (*addr == 0 || (val & 0x01) == 0) { |
Jean Delvare | 620100c | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 446 | pr_info(DRVNAME ": Device is disabled, will not use\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | superio_exit(); |
| 448 | return -ENODEV; |
| 449 | } |
| 450 | |
| 451 | superio_exit(); |
| 452 | return 0; |
| 453 | } |
| 454 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 455 | static int __devinit smsc47m1_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 457 | struct device *dev = &pdev->dev; |
| 458 | struct smsc47m1_sio_data *sio_data = dev->platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | struct smsc47m1_data *data; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 460 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | int err = 0; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 462 | int fan1, fan2, fan3, pwm1, pwm2, pwm3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 464 | static const char *names[] = { |
| 465 | "smsc47m1", |
| 466 | "smsc47m2", |
| 467 | }; |
| 468 | |
| 469 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 470 | if (!request_region(res->start, SMSC_EXTENT, DRVNAME)) { |
| 471 | dev_err(dev, "Region 0x%lx-0x%lx already in use!\n", |
| 472 | (unsigned long)res->start, |
| 473 | (unsigned long)res->end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return -EBUSY; |
| 475 | } |
| 476 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 477 | if (!(data = kzalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | err = -ENOMEM; |
| 479 | goto error_release; |
| 480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 482 | data->addr = res->start; |
| 483 | data->type = sio_data->type; |
| 484 | data->name = names[sio_data->type]; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 485 | mutex_init(&data->update_lock); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 486 | platform_set_drvdata(pdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* If no function is properly configured, there's no point in |
| 489 | actually registering the chip. */ |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 490 | pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | == 0x04; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 492 | pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | == 0x04; |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 494 | if (data->type == smsc47m2) { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 495 | fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 496 | & 0x0d) == 0x09; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 497 | fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 498 | & 0x0d) == 0x09; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 499 | fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 500 | & 0x0d) == 0x0d; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 501 | pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 502 | & 0x0d) == 0x08; |
| 503 | } else { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 504 | fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0)) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 505 | & 0x05) == 0x05; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 506 | fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1)) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 507 | & 0x05) == 0x05; |
| 508 | fan3 = 0; |
| 509 | pwm3 = 0; |
| 510 | } |
| 511 | if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 512 | dev_warn(dev, "Device not configured, will not use\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | err = -ENODEV; |
| 514 | goto error_free; |
| 515 | } |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* Some values (fan min, clock dividers, pwm registers) may be |
| 518 | needed before any update is triggered, so we better read them |
| 519 | at least once here. We don't usually do it that way, but in |
| 520 | this particular case, manually reading 5 registers out of 8 |
| 521 | doesn't make much sense and we're better using the existing |
| 522 | function. */ |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 523 | smsc47m1_update_device(dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 525 | /* Register sysfs hooks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (fan1) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 527 | if ((err = device_create_file(dev, |
| 528 | &sensor_dev_attr_fan1_input.dev_attr)) |
| 529 | || (err = device_create_file(dev, |
| 530 | &sensor_dev_attr_fan1_min.dev_attr)) |
| 531 | || (err = device_create_file(dev, |
| 532 | &sensor_dev_attr_fan1_div.dev_attr))) |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 533 | goto error_remove_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 535 | dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | if (fan2) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 538 | if ((err = device_create_file(dev, |
| 539 | &sensor_dev_attr_fan2_input.dev_attr)) |
| 540 | || (err = device_create_file(dev, |
| 541 | &sensor_dev_attr_fan2_min.dev_attr)) |
| 542 | || (err = device_create_file(dev, |
| 543 | &sensor_dev_attr_fan2_div.dev_attr))) |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 544 | goto error_remove_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 546 | dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 548 | if (fan3) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 549 | if ((err = device_create_file(dev, |
| 550 | &sensor_dev_attr_fan3_input.dev_attr)) |
| 551 | || (err = device_create_file(dev, |
| 552 | &sensor_dev_attr_fan3_min.dev_attr)) |
| 553 | || (err = device_create_file(dev, |
| 554 | &sensor_dev_attr_fan3_div.dev_attr))) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 555 | goto error_remove_files; |
| 556 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 557 | dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n"); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | if (pwm1) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 560 | if ((err = device_create_file(dev, |
| 561 | &sensor_dev_attr_pwm1.dev_attr)) |
| 562 | || (err = device_create_file(dev, |
| 563 | &sensor_dev_attr_pwm1_enable.dev_attr))) |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 564 | goto error_remove_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 566 | dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n"); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (pwm2) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 569 | if ((err = device_create_file(dev, |
| 570 | &sensor_dev_attr_pwm2.dev_attr)) |
| 571 | || (err = device_create_file(dev, |
| 572 | &sensor_dev_attr_pwm2_enable.dev_attr))) |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 573 | goto error_remove_files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 575 | dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 577 | if (pwm3) { |
Jean Delvare | e84cfbc | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 578 | if ((err = device_create_file(dev, |
| 579 | &sensor_dev_attr_pwm3.dev_attr)) |
| 580 | || (err = device_create_file(dev, |
| 581 | &sensor_dev_attr_pwm3_enable.dev_attr))) |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 582 | goto error_remove_files; |
| 583 | } else |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 584 | dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n"); |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 585 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 586 | if ((err = device_create_file(dev, &dev_attr_alarms))) |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 587 | goto error_remove_files; |
Jean Delvare | 68a50b5 | 2007-08-12 13:58:50 +0200 | [diff] [blame^] | 588 | if ((err = device_create_file(dev, &dev_attr_name))) |
| 589 | goto error_remove_files; |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 590 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 591 | data->class_dev = hwmon_device_register(dev); |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 592 | if (IS_ERR(data->class_dev)) { |
| 593 | err = PTR_ERR(data->class_dev); |
| 594 | goto error_remove_files; |
| 595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 599 | error_remove_files: |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 600 | sysfs_remove_group(&dev->kobj, &smsc47m1_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | error_free: |
Jean Delvare | 04a6217 | 2007-06-12 13:57:19 +0200 | [diff] [blame] | 602 | platform_set_drvdata(pdev, NULL); |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 603 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | error_release: |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 605 | release_region(res->start, SMSC_EXTENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return err; |
| 607 | } |
| 608 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 609 | static int __devexit smsc47m1_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 611 | struct smsc47m1_data *data = platform_get_drvdata(pdev); |
| 612 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 614 | hwmon_device_unregister(data->class_dev); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 615 | sysfs_remove_group(&pdev->dev.kobj, &smsc47m1_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 616 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 617 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 618 | release_region(res->start, SMSC_EXTENT); |
Jean Delvare | 04a6217 | 2007-06-12 13:57:19 +0200 | [diff] [blame] | 619 | platform_set_drvdata(pdev, NULL); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 620 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, |
| 626 | int init) |
| 627 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 628 | struct smsc47m1_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 630 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) { |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 633 | int i, fan_nr; |
| 634 | fan_nr = data->type == smsc47m2 ? 3 : 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 636 | for (i = 0; i < fan_nr; i++) { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 637 | data->fan[i] = smsc47m1_read_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 638 | SMSC47M1_REG_FAN[i]); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 639 | data->fan_preload[i] = smsc47m1_read_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 640 | SMSC47M1_REG_FAN_PRELOAD[i]); |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 641 | data->pwm[i] = smsc47m1_read_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 642 | SMSC47M1_REG_PWM[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 645 | i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | data->fan_div[0] = (i >> 4) & 0x03; |
| 647 | data->fan_div[1] = i >> 6; |
| 648 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 649 | data->alarms = smsc47m1_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | SMSC47M1_REG_ALARM) >> 6; |
| 651 | /* Clear alarms if needed */ |
| 652 | if (data->alarms) |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 653 | smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 655 | if (fan_nr >= 3) { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 656 | data->fan_div[2] = (smsc47m1_read_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 657 | SMSC47M2_REG_FANDIV3) >> 4) & 0x03; |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 658 | data->alarms |= (smsc47m1_read_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 659 | SMSC47M2_REG_ALARM6) & 0x40) >> 4; |
| 660 | /* Clear alarm if needed */ |
| 661 | if (data->alarms & 0x04) |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 662 | smsc47m1_write_value(data, |
Jean Delvare | 8eccbb6 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 663 | SMSC47M2_REG_ALARM6, |
| 664 | 0x40); |
| 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | data->last_updated = jiffies; |
| 668 | } |
| 669 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 670 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | return data; |
| 672 | } |
| 673 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 674 | static int __init smsc47m1_device_add(unsigned short address, |
| 675 | const struct smsc47m1_sio_data *sio_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 677 | struct resource res = { |
| 678 | .start = address, |
| 679 | .end = address + SMSC_EXTENT - 1, |
| 680 | .name = DRVNAME, |
| 681 | .flags = IORESOURCE_IO, |
| 682 | }; |
| 683 | int err; |
| 684 | |
| 685 | pdev = platform_device_alloc(DRVNAME, address); |
| 686 | if (!pdev) { |
| 687 | err = -ENOMEM; |
| 688 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); |
| 689 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 692 | err = platform_device_add_resources(pdev, &res, 1); |
| 693 | if (err) { |
| 694 | printk(KERN_ERR DRVNAME ": Device resource addition failed " |
| 695 | "(%d)\n", err); |
| 696 | goto exit_device_put; |
| 697 | } |
| 698 | |
Jean Delvare | 2df6d81 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 699 | err = platform_device_add_data(pdev, sio_data, |
| 700 | sizeof(struct smsc47m1_sio_data)); |
| 701 | if (err) { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 702 | printk(KERN_ERR DRVNAME ": Platform data allocation failed\n"); |
| 703 | goto exit_device_put; |
| 704 | } |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 705 | |
| 706 | err = platform_device_add(pdev); |
| 707 | if (err) { |
| 708 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", |
| 709 | err); |
| 710 | goto exit_device_put; |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | |
| 715 | exit_device_put: |
| 716 | platform_device_put(pdev); |
| 717 | exit: |
| 718 | return err; |
| 719 | } |
| 720 | |
| 721 | static int __init sm_smsc47m1_init(void) |
| 722 | { |
| 723 | int err; |
| 724 | unsigned short address; |
| 725 | struct smsc47m1_sio_data sio_data; |
| 726 | |
| 727 | if (smsc47m1_find(&address, &sio_data)) |
| 728 | return -ENODEV; |
| 729 | |
| 730 | err = platform_driver_register(&smsc47m1_driver); |
| 731 | if (err) |
| 732 | goto exit; |
| 733 | |
| 734 | /* Sets global pdev as a side effect */ |
| 735 | err = smsc47m1_device_add(address, &sio_data); |
| 736 | if (err) |
| 737 | goto exit_driver; |
| 738 | |
| 739 | return 0; |
| 740 | |
| 741 | exit_driver: |
| 742 | platform_driver_unregister(&smsc47m1_driver); |
| 743 | exit: |
| 744 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void __exit sm_smsc47m1_exit(void) |
| 748 | { |
Jean Delvare | 51f2cca | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 749 | platform_device_unregister(pdev); |
| 750 | platform_driver_unregister(&smsc47m1_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>"); |
| 754 | MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver"); |
| 755 | MODULE_LICENSE("GPL"); |
| 756 | |
| 757 | module_init(sm_smsc47m1_init); |
| 758 | module_exit(sm_smsc47m1_exit); |