Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 1 | /* |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 2 | * f75375s.c - driver for the Fintek F75375/SP, F75373 and |
| 3 | * F75387SG/RG hardware monitoring features |
Riku Voipio | b26e0ed | 2009-03-03 21:37:17 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2007 Riku Voipio |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 5 | * |
| 6 | * Datasheets available at: |
| 7 | * |
| 8 | * f75375: |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 9 | * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 10 | * |
| 11 | * f75373: |
Justin P. Mattock | 631dd1a | 2010-10-18 11:03:14 +0200 | [diff] [blame] | 12 | * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 13 | * |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 14 | * f75387: |
| 15 | * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf |
| 16 | * |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/hwmon.h> |
| 36 | #include <linux/hwmon-sysfs.h> |
| 37 | #include <linux/i2c.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/mutex.h> |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 40 | #include <linux/f75375s.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 42 | |
| 43 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 44 | static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END }; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 45 | |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 46 | enum chips { f75373, f75375, f75387 }; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 47 | |
| 48 | /* Fintek F75375 registers */ |
| 49 | #define F75375_REG_CONFIG0 0x0 |
| 50 | #define F75375_REG_CONFIG1 0x1 |
| 51 | #define F75375_REG_CONFIG2 0x2 |
| 52 | #define F75375_REG_CONFIG3 0x3 |
| 53 | #define F75375_REG_ADDR 0x4 |
| 54 | #define F75375_REG_INTR 0x31 |
| 55 | #define F75375_CHIP_ID 0x5A |
| 56 | #define F75375_REG_VERSION 0x5C |
| 57 | #define F75375_REG_VENDOR 0x5D |
| 58 | #define F75375_REG_FAN_TIMER 0x60 |
| 59 | |
| 60 | #define F75375_REG_VOLT(nr) (0x10 + (nr)) |
| 61 | #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2) |
| 62 | #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2) |
| 63 | |
| 64 | #define F75375_REG_TEMP(nr) (0x14 + (nr)) |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 65 | #define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr)) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 66 | #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2) |
| 67 | #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2) |
| 68 | |
| 69 | #define F75375_REG_FAN(nr) (0x16 + (nr) * 2) |
| 70 | #define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2) |
| 71 | #define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10) |
| 72 | #define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10) |
| 73 | #define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10) |
| 74 | |
| 75 | #define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10) |
| 76 | #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step)) |
| 77 | #define F75375_REG_FAN_B_SPEED(nr, step) \ |
| 78 | ((0xA5 + (nr) * 0x10) + (step) * 2) |
| 79 | |
| 80 | #define F75375_REG_PWM1_RAISE_DUTY 0x69 |
| 81 | #define F75375_REG_PWM2_RAISE_DUTY 0x6A |
| 82 | #define F75375_REG_PWM1_DROP_DUTY 0x6B |
| 83 | #define F75375_REG_PWM2_DROP_DUTY 0x6C |
| 84 | |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 85 | #define F75375_FAN_CTRL_LINEAR(nr) (4 + nr) |
| 86 | #define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4)) |
Guillem Jover | 96f3640 | 2010-09-17 17:24:11 +0200 | [diff] [blame] | 87 | #define FAN_CTRL_MODE(nr) (4 + ((nr) * 2)) |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 88 | #define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4)) |
| 89 | #define F75387_FAN_MANU_MODE(nr) ((nr) * 4) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Data structures and manipulation thereof |
| 93 | */ |
| 94 | |
| 95 | struct f75375_data { |
| 96 | unsigned short addr; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 97 | struct device *hwmon_dev; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 98 | |
| 99 | const char *name; |
| 100 | int kind; |
| 101 | struct mutex update_lock; /* protect register access */ |
| 102 | char valid; |
| 103 | unsigned long last_updated; /* In jiffies */ |
| 104 | unsigned long last_limits; /* In jiffies */ |
| 105 | |
| 106 | /* Register values */ |
| 107 | u8 in[4]; |
| 108 | u8 in_max[4]; |
| 109 | u8 in_min[4]; |
| 110 | u16 fan[2]; |
| 111 | u16 fan_min[2]; |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 112 | u16 fan_max[2]; |
| 113 | u16 fan_target[2]; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 114 | u8 fan_timer; |
| 115 | u8 pwm[2]; |
| 116 | u8 pwm_mode[2]; |
| 117 | u8 pwm_enable[2]; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 118 | /* |
| 119 | * f75387: For remote temperature reading, it uses signed 11-bit |
| 120 | * values with LSB = 0.125 degree Celsius, left-justified in 16-bit |
| 121 | * registers. For original 8-bit temp readings, the LSB just is 0. |
| 122 | */ |
| 123 | s16 temp11[2]; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 124 | s8 temp_high[2]; |
| 125 | s8 temp_max_hyst[2]; |
| 126 | }; |
| 127 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 128 | static int f75375_detect(struct i2c_client *client, |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 129 | struct i2c_board_info *info); |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 130 | static int f75375_probe(struct i2c_client *client, |
| 131 | const struct i2c_device_id *id); |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 132 | static int f75375_remove(struct i2c_client *client); |
| 133 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 134 | static const struct i2c_device_id f75375_id[] = { |
| 135 | { "f75373", f75373 }, |
| 136 | { "f75375", f75375 }, |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 137 | { "f75387", f75387 }, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 138 | { } |
| 139 | }; |
| 140 | MODULE_DEVICE_TABLE(i2c, f75375_id); |
| 141 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 142 | static struct i2c_driver f75375_driver = { |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 143 | .class = I2C_CLASS_HWMON, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 144 | .driver = { |
| 145 | .name = "f75375", |
| 146 | }, |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 147 | .probe = f75375_probe, |
| 148 | .remove = f75375_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 149 | .id_table = f75375_id, |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 150 | .detect = f75375_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 151 | .address_list = normal_i2c, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static inline int f75375_read8(struct i2c_client *client, u8 reg) |
| 155 | { |
| 156 | return i2c_smbus_read_byte_data(client, reg); |
| 157 | } |
| 158 | |
| 159 | /* in most cases, should be called while holding update_lock */ |
| 160 | static inline u16 f75375_read16(struct i2c_client *client, u8 reg) |
| 161 | { |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 162 | return (i2c_smbus_read_byte_data(client, reg) << 8) |
| 163 | | i2c_smbus_read_byte_data(client, reg + 1); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static inline void f75375_write8(struct i2c_client *client, u8 reg, |
| 167 | u8 value) |
| 168 | { |
| 169 | i2c_smbus_write_byte_data(client, reg, value); |
| 170 | } |
| 171 | |
| 172 | static inline void f75375_write16(struct i2c_client *client, u8 reg, |
| 173 | u16 value) |
| 174 | { |
Nikolaus Schulz | eb2f255 | 2012-02-08 18:56:10 +0100 | [diff] [blame] | 175 | int err = i2c_smbus_write_byte_data(client, reg, (value >> 8)); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 176 | if (err) |
| 177 | return; |
| 178 | i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF)); |
| 179 | } |
| 180 | |
Nikolaus Schulz | 331255d | 2012-02-28 22:15:51 +0100 | [diff] [blame] | 181 | static void f75375_write_pwm(struct i2c_client *client, int nr) |
| 182 | { |
| 183 | struct f75375_data *data = i2c_get_clientdata(client); |
| 184 | if (data->kind == f75387) |
| 185 | f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]); |
| 186 | else |
| 187 | f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), |
| 188 | data->pwm[nr]); |
| 189 | } |
| 190 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 191 | static struct f75375_data *f75375_update_device(struct device *dev) |
| 192 | { |
| 193 | struct i2c_client *client = to_i2c_client(dev); |
| 194 | struct f75375_data *data = i2c_get_clientdata(client); |
| 195 | int nr; |
| 196 | |
| 197 | mutex_lock(&data->update_lock); |
| 198 | |
| 199 | /* Limit registers cache is refreshed after 60 seconds */ |
| 200 | if (time_after(jiffies, data->last_limits + 60 * HZ) |
| 201 | || !data->valid) { |
| 202 | for (nr = 0; nr < 2; nr++) { |
| 203 | data->temp_high[nr] = |
| 204 | f75375_read8(client, F75375_REG_TEMP_HIGH(nr)); |
| 205 | data->temp_max_hyst[nr] = |
| 206 | f75375_read8(client, F75375_REG_TEMP_HYST(nr)); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 207 | data->fan_max[nr] = |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 208 | f75375_read16(client, F75375_REG_FAN_FULL(nr)); |
| 209 | data->fan_min[nr] = |
| 210 | f75375_read16(client, F75375_REG_FAN_MIN(nr)); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 211 | data->fan_target[nr] = |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 212 | f75375_read16(client, F75375_REG_FAN_EXP(nr)); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 213 | } |
| 214 | for (nr = 0; nr < 4; nr++) { |
| 215 | data->in_max[nr] = |
| 216 | f75375_read8(client, F75375_REG_VOLT_HIGH(nr)); |
| 217 | data->in_min[nr] = |
| 218 | f75375_read8(client, F75375_REG_VOLT_LOW(nr)); |
| 219 | } |
| 220 | data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER); |
| 221 | data->last_limits = jiffies; |
| 222 | } |
| 223 | |
| 224 | /* Measurement registers cache is refreshed after 2 second */ |
| 225 | if (time_after(jiffies, data->last_updated + 2 * HZ) |
| 226 | || !data->valid) { |
| 227 | for (nr = 0; nr < 2; nr++) { |
Nikolaus Schulz | a1c1baf | 2012-02-08 18:56:11 +0100 | [diff] [blame] | 228 | data->pwm[nr] = f75375_read8(client, |
| 229 | F75375_REG_FAN_PWM_DUTY(nr)); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 230 | /* assign MSB, therefore shift it by 8 bits */ |
| 231 | data->temp11[nr] = |
| 232 | f75375_read8(client, F75375_REG_TEMP(nr)) << 8; |
| 233 | if (data->kind == f75387) |
| 234 | /* merge F75387's temperature LSB (11-bit) */ |
| 235 | data->temp11[nr] |= |
| 236 | f75375_read8(client, |
| 237 | F75387_REG_TEMP11_LSB(nr)); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 238 | data->fan[nr] = |
| 239 | f75375_read16(client, F75375_REG_FAN(nr)); |
| 240 | } |
| 241 | for (nr = 0; nr < 4; nr++) |
| 242 | data->in[nr] = |
| 243 | f75375_read8(client, F75375_REG_VOLT(nr)); |
| 244 | |
| 245 | data->last_updated = jiffies; |
| 246 | data->valid = 1; |
| 247 | } |
| 248 | |
| 249 | mutex_unlock(&data->update_lock); |
| 250 | return data; |
| 251 | } |
| 252 | |
| 253 | static inline u16 rpm_from_reg(u16 reg) |
| 254 | { |
| 255 | if (reg == 0 || reg == 0xffff) |
| 256 | return 0; |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 257 | return 1500000 / reg; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static inline u16 rpm_to_reg(int rpm) |
| 261 | { |
| 262 | if (rpm < 367 || rpm > 0xffff) |
| 263 | return 0xffff; |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 264 | return 1500000 / rpm; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 265 | } |
| 266 | |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 267 | static bool duty_mode_enabled(u8 pwm_enable) |
| 268 | { |
| 269 | switch (pwm_enable) { |
| 270 | case 0: /* Manual, duty mode (full speed) */ |
| 271 | case 1: /* Manual, duty mode */ |
| 272 | case 4: /* Auto, duty mode */ |
| 273 | return true; |
| 274 | case 2: /* Auto, speed mode */ |
| 275 | case 3: /* Manual, speed mode */ |
| 276 | return false; |
| 277 | default: |
| 278 | BUG(); |
| 279 | } |
| 280 | } |
| 281 | |
Nikolaus Schulz | 15d1ad0 | 2012-02-28 16:15:54 -0500 | [diff] [blame] | 282 | static bool auto_mode_enabled(u8 pwm_enable) |
| 283 | { |
| 284 | switch (pwm_enable) { |
| 285 | case 0: /* Manual, duty mode (full speed) */ |
| 286 | case 1: /* Manual, duty mode */ |
| 287 | case 3: /* Manual, speed mode */ |
| 288 | return false; |
| 289 | case 2: /* Auto, speed mode */ |
| 290 | case 4: /* Auto, duty mode */ |
| 291 | return true; |
| 292 | default: |
| 293 | BUG(); |
| 294 | } |
| 295 | } |
| 296 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 297 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 298 | const char *buf, size_t count) |
| 299 | { |
| 300 | int nr = to_sensor_dev_attr(attr)->index; |
| 301 | struct i2c_client *client = to_i2c_client(dev); |
| 302 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 303 | unsigned long val; |
| 304 | int err; |
| 305 | |
| 306 | err = kstrtoul(buf, 10, &val); |
| 307 | if (err < 0) |
| 308 | return err; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 309 | |
| 310 | mutex_lock(&data->update_lock); |
| 311 | data->fan_min[nr] = rpm_to_reg(val); |
| 312 | f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]); |
| 313 | mutex_unlock(&data->update_lock); |
| 314 | return count; |
| 315 | } |
| 316 | |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 317 | static ssize_t set_fan_target(struct device *dev, struct device_attribute *attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 318 | const char *buf, size_t count) |
| 319 | { |
| 320 | int nr = to_sensor_dev_attr(attr)->index; |
| 321 | struct i2c_client *client = to_i2c_client(dev); |
| 322 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 323 | unsigned long val; |
| 324 | int err; |
| 325 | |
| 326 | err = kstrtoul(buf, 10, &val); |
| 327 | if (err < 0) |
| 328 | return err; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 329 | |
Nikolaus Schulz | 15d1ad0 | 2012-02-28 16:15:54 -0500 | [diff] [blame] | 330 | if (auto_mode_enabled(data->pwm_enable[nr])) |
| 331 | return -EINVAL; |
| 332 | if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr])) |
| 333 | return -EINVAL; |
| 334 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 335 | mutex_lock(&data->update_lock); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 336 | data->fan_target[nr] = rpm_to_reg(val); |
| 337 | f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 338 | mutex_unlock(&data->update_lock); |
| 339 | return count; |
| 340 | } |
| 341 | |
| 342 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 343 | const char *buf, size_t count) |
| 344 | { |
| 345 | int nr = to_sensor_dev_attr(attr)->index; |
| 346 | struct i2c_client *client = to_i2c_client(dev); |
| 347 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 348 | unsigned long val; |
| 349 | int err; |
| 350 | |
| 351 | err = kstrtoul(buf, 10, &val); |
| 352 | if (err < 0) |
| 353 | return err; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 354 | |
Nikolaus Schulz | 15d1ad0 | 2012-02-28 16:15:54 -0500 | [diff] [blame] | 355 | if (auto_mode_enabled(data->pwm_enable[nr]) || |
| 356 | !duty_mode_enabled(data->pwm_enable[nr])) |
| 357 | return -EINVAL; |
| 358 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 359 | mutex_lock(&data->update_lock); |
| 360 | data->pwm[nr] = SENSORS_LIMIT(val, 0, 255); |
Nikolaus Schulz | 331255d | 2012-02-28 22:15:51 +0100 | [diff] [blame] | 361 | f75375_write_pwm(client, nr); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 362 | mutex_unlock(&data->update_lock); |
| 363 | return count; |
| 364 | } |
| 365 | |
| 366 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute |
| 367 | *attr, char *buf) |
| 368 | { |
| 369 | int nr = to_sensor_dev_attr(attr)->index; |
| 370 | struct f75375_data *data = f75375_update_device(dev); |
| 371 | return sprintf(buf, "%d\n", data->pwm_enable[nr]); |
| 372 | } |
| 373 | |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 374 | static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 375 | { |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 376 | struct f75375_data *data = i2c_get_clientdata(client); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 377 | u8 fanmode; |
| 378 | |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 379 | if (val < 0 || val > 4) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 382 | fanmode = f75375_read8(client, F75375_REG_FAN_TIMER); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 383 | if (data->kind == f75387) { |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 384 | /* For now, deny dangerous toggling of duty mode */ |
| 385 | if (duty_mode_enabled(data->pwm_enable[nr]) != |
| 386 | duty_mode_enabled(val)) |
| 387 | return -EOPNOTSUPP; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 388 | /* clear each fanX_mode bit before setting them properly */ |
| 389 | fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr)); |
| 390 | fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr)); |
| 391 | switch (val) { |
| 392 | case 0: /* full speed */ |
| 393 | fanmode |= (1 << F75387_FAN_MANU_MODE(nr)); |
| 394 | fanmode |= (1 << F75387_FAN_DUTY_MODE(nr)); |
| 395 | data->pwm[nr] = 255; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 396 | break; |
| 397 | case 1: /* PWM */ |
| 398 | fanmode |= (1 << F75387_FAN_MANU_MODE(nr)); |
| 399 | fanmode |= (1 << F75387_FAN_DUTY_MODE(nr)); |
| 400 | break; |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 401 | case 2: /* Automatic, speed mode */ |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 402 | break; |
| 403 | case 3: /* fan speed */ |
| 404 | fanmode |= (1 << F75387_FAN_MANU_MODE(nr)); |
| 405 | break; |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 406 | case 4: /* Automatic, pwm */ |
| 407 | fanmode |= (1 << F75387_FAN_DUTY_MODE(nr)); |
| 408 | break; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 409 | } |
| 410 | } else { |
| 411 | /* clear each fanX_mode bit before setting them properly */ |
| 412 | fanmode &= ~(3 << FAN_CTRL_MODE(nr)); |
| 413 | switch (val) { |
| 414 | case 0: /* full speed */ |
| 415 | fanmode |= (3 << FAN_CTRL_MODE(nr)); |
| 416 | data->pwm[nr] = 255; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 417 | break; |
| 418 | case 1: /* PWM */ |
| 419 | fanmode |= (3 << FAN_CTRL_MODE(nr)); |
| 420 | break; |
| 421 | case 2: /* AUTOMATIC*/ |
Nikolaus Schulz | 09e87e5 | 2012-02-08 18:56:08 +0100 | [diff] [blame] | 422 | fanmode |= (1 << FAN_CTRL_MODE(nr)); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 423 | break; |
| 424 | case 3: /* fan speed */ |
| 425 | break; |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 426 | case 4: /* Automatic pwm */ |
| 427 | return -EINVAL; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 428 | } |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 429 | } |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 430 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 431 | f75375_write8(client, F75375_REG_FAN_TIMER, fanmode); |
| 432 | data->pwm_enable[nr] = val; |
Nikolaus Schulz | c1c1a3d | 2012-02-22 23:18:44 +0100 | [diff] [blame] | 433 | if (val == 0) |
Nikolaus Schulz | 331255d | 2012-02-28 22:15:51 +0100 | [diff] [blame] | 434 | f75375_write_pwm(client, nr); |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 439 | const char *buf, size_t count) |
| 440 | { |
| 441 | int nr = to_sensor_dev_attr(attr)->index; |
| 442 | struct i2c_client *client = to_i2c_client(dev); |
| 443 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 444 | unsigned long val; |
| 445 | int err; |
| 446 | |
| 447 | err = kstrtoul(buf, 10, &val); |
| 448 | if (err < 0) |
| 449 | return err; |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 450 | |
| 451 | mutex_lock(&data->update_lock); |
| 452 | err = set_pwm_enable_direct(client, nr, val); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 453 | mutex_unlock(&data->update_lock); |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 454 | return err ? err : count; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr, |
| 458 | const char *buf, size_t count) |
| 459 | { |
| 460 | int nr = to_sensor_dev_attr(attr)->index; |
| 461 | struct i2c_client *client = to_i2c_client(dev); |
| 462 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 463 | unsigned long val; |
| 464 | int err; |
| 465 | u8 conf; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 466 | char reg, ctrl; |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 467 | |
| 468 | err = kstrtoul(buf, 10, &val); |
| 469 | if (err < 0) |
| 470 | return err; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 471 | |
Riku Voipio | 76e83be | 2007-10-26 14:14:23 +0300 | [diff] [blame] | 472 | if (!(val == 0 || val == 1)) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 473 | return -EINVAL; |
| 474 | |
Guenter Roeck | 5cd3222 | 2011-12-08 10:55:16 -0800 | [diff] [blame] | 475 | /* F75373 does not support DC (linear voltage) fan control mode */ |
| 476 | if (data->kind == f75373 && val == 0) |
| 477 | return -EINVAL; |
| 478 | |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 479 | /* take care for different registers */ |
| 480 | if (data->kind == f75387) { |
| 481 | reg = F75375_REG_FAN_TIMER; |
| 482 | ctrl = F75387_FAN_CTRL_LINEAR(nr); |
| 483 | } else { |
| 484 | reg = F75375_REG_CONFIG1; |
| 485 | ctrl = F75375_FAN_CTRL_LINEAR(nr); |
| 486 | } |
| 487 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 488 | mutex_lock(&data->update_lock); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 489 | conf = f75375_read8(client, reg); |
| 490 | conf &= ~(1 << ctrl); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 491 | |
| 492 | if (val == 0) |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 493 | conf |= (1 << ctrl); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 494 | |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 495 | f75375_write8(client, reg, conf); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 496 | data->pwm_mode[nr] = val; |
| 497 | mutex_unlock(&data->update_lock); |
| 498 | return count; |
| 499 | } |
| 500 | |
| 501 | static ssize_t show_pwm(struct device *dev, struct device_attribute |
| 502 | *attr, char *buf) |
| 503 | { |
| 504 | int nr = to_sensor_dev_attr(attr)->index; |
| 505 | struct f75375_data *data = f75375_update_device(dev); |
| 506 | return sprintf(buf, "%d\n", data->pwm[nr]); |
| 507 | } |
| 508 | |
| 509 | static ssize_t show_pwm_mode(struct device *dev, struct device_attribute |
| 510 | *attr, char *buf) |
| 511 | { |
| 512 | int nr = to_sensor_dev_attr(attr)->index; |
| 513 | struct f75375_data *data = f75375_update_device(dev); |
| 514 | return sprintf(buf, "%d\n", data->pwm_mode[nr]); |
| 515 | } |
| 516 | |
| 517 | #define VOLT_FROM_REG(val) ((val) * 8) |
| 518 | #define VOLT_TO_REG(val) ((val) / 8) |
| 519 | |
| 520 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 521 | char *buf) |
| 522 | { |
| 523 | int nr = to_sensor_dev_attr(attr)->index; |
| 524 | struct f75375_data *data = f75375_update_device(dev); |
| 525 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr])); |
| 526 | } |
| 527 | |
| 528 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
| 529 | char *buf) |
| 530 | { |
| 531 | int nr = to_sensor_dev_attr(attr)->index; |
| 532 | struct f75375_data *data = f75375_update_device(dev); |
| 533 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr])); |
| 534 | } |
| 535 | |
| 536 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
| 537 | char *buf) |
| 538 | { |
| 539 | int nr = to_sensor_dev_attr(attr)->index; |
| 540 | struct f75375_data *data = f75375_update_device(dev); |
| 541 | return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr])); |
| 542 | } |
| 543 | |
| 544 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 545 | const char *buf, size_t count) |
| 546 | { |
| 547 | int nr = to_sensor_dev_attr(attr)->index; |
| 548 | struct i2c_client *client = to_i2c_client(dev); |
| 549 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 550 | unsigned long val; |
| 551 | int err; |
| 552 | |
| 553 | err = kstrtoul(buf, 10, &val); |
| 554 | if (err < 0) |
| 555 | return err; |
| 556 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 557 | val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff); |
| 558 | mutex_lock(&data->update_lock); |
| 559 | data->in_max[nr] = val; |
| 560 | f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]); |
| 561 | mutex_unlock(&data->update_lock); |
| 562 | return count; |
| 563 | } |
| 564 | |
| 565 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 566 | const char *buf, size_t count) |
| 567 | { |
| 568 | int nr = to_sensor_dev_attr(attr)->index; |
| 569 | struct i2c_client *client = to_i2c_client(dev); |
| 570 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 571 | unsigned long val; |
| 572 | int err; |
| 573 | |
| 574 | err = kstrtoul(buf, 10, &val); |
| 575 | if (err < 0) |
| 576 | return err; |
| 577 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 578 | val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff); |
| 579 | mutex_lock(&data->update_lock); |
| 580 | data->in_min[nr] = val; |
| 581 | f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]); |
| 582 | mutex_unlock(&data->update_lock); |
| 583 | return count; |
| 584 | } |
| 585 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 586 | #define TEMP_TO_REG(val) ((val) / 1000) |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 587 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 588 | |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 589 | static ssize_t show_temp11(struct device *dev, struct device_attribute *attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 590 | char *buf) |
| 591 | { |
| 592 | int nr = to_sensor_dev_attr(attr)->index; |
| 593 | struct f75375_data *data = f75375_update_device(dev); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 594 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr])); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 598 | char *buf) |
| 599 | { |
| 600 | int nr = to_sensor_dev_attr(attr)->index; |
| 601 | struct f75375_data *data = f75375_update_device(dev); |
| 602 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])); |
| 603 | } |
| 604 | |
| 605 | static ssize_t show_temp_max_hyst(struct device *dev, |
| 606 | struct device_attribute *attr, char *buf) |
| 607 | { |
| 608 | int nr = to_sensor_dev_attr(attr)->index; |
| 609 | struct f75375_data *data = f75375_update_device(dev); |
| 610 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr])); |
| 611 | } |
| 612 | |
| 613 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 614 | const char *buf, size_t count) |
| 615 | { |
| 616 | int nr = to_sensor_dev_attr(attr)->index; |
| 617 | struct i2c_client *client = to_i2c_client(dev); |
| 618 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 619 | unsigned long val; |
| 620 | int err; |
| 621 | |
| 622 | err = kstrtoul(buf, 10, &val); |
| 623 | if (err < 0) |
| 624 | return err; |
| 625 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 626 | val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127); |
| 627 | mutex_lock(&data->update_lock); |
| 628 | data->temp_high[nr] = val; |
| 629 | f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]); |
| 630 | mutex_unlock(&data->update_lock); |
| 631 | return count; |
| 632 | } |
| 633 | |
| 634 | static ssize_t set_temp_max_hyst(struct device *dev, |
| 635 | struct device_attribute *attr, const char *buf, size_t count) |
| 636 | { |
| 637 | int nr = to_sensor_dev_attr(attr)->index; |
| 638 | struct i2c_client *client = to_i2c_client(dev); |
| 639 | struct f75375_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 640 | unsigned long val; |
| 641 | int err; |
| 642 | |
| 643 | err = kstrtoul(buf, 10, &val); |
| 644 | if (err < 0) |
| 645 | return err; |
| 646 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 647 | val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127); |
| 648 | mutex_lock(&data->update_lock); |
| 649 | data->temp_max_hyst[nr] = val; |
| 650 | f75375_write8(client, F75375_REG_TEMP_HYST(nr), |
| 651 | data->temp_max_hyst[nr]); |
| 652 | mutex_unlock(&data->update_lock); |
| 653 | return count; |
| 654 | } |
| 655 | |
| 656 | #define show_fan(thing) \ |
| 657 | static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \ |
| 658 | char *buf)\ |
| 659 | {\ |
| 660 | int nr = to_sensor_dev_attr(attr)->index;\ |
| 661 | struct f75375_data *data = f75375_update_device(dev); \ |
| 662 | return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \ |
| 663 | } |
| 664 | |
| 665 | show_fan(fan); |
| 666 | show_fan(fan_min); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 667 | show_fan(fan_max); |
| 668 | show_fan(fan_target); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 669 | |
| 670 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0); |
| 671 | static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR, |
| 672 | show_in_max, set_in_max, 0); |
| 673 | static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR, |
| 674 | show_in_min, set_in_min, 0); |
| 675 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1); |
| 676 | static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR, |
| 677 | show_in_max, set_in_max, 1); |
| 678 | static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR, |
| 679 | show_in_min, set_in_min, 1); |
| 680 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2); |
| 681 | static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR, |
| 682 | show_in_max, set_in_max, 2); |
| 683 | static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR, |
| 684 | show_in_min, set_in_min, 2); |
| 685 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3); |
| 686 | static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR, |
| 687 | show_in_max, set_in_max, 3); |
| 688 | static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR, |
| 689 | show_in_min, set_in_min, 3); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 690 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 691 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR, |
| 692 | show_temp_max_hyst, set_temp_max_hyst, 0); |
| 693 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR, |
| 694 | show_temp_max, set_temp_max, 0); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 695 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 696 | static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR, |
| 697 | show_temp_max_hyst, set_temp_max_hyst, 1); |
| 698 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR, |
| 699 | show_temp_max, set_temp_max, 1); |
| 700 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 701 | static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 702 | static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR, |
| 703 | show_fan_min, set_fan_min, 0); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 704 | static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR, |
| 705 | show_fan_target, set_fan_target, 0); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 706 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 707 | static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 708 | static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR, |
| 709 | show_fan_min, set_fan_min, 1); |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 710 | static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR, |
| 711 | show_fan_target, set_fan_target, 1); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 712 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, |
| 713 | show_pwm, set_pwm, 0); |
| 714 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, |
| 715 | show_pwm_enable, set_pwm_enable, 0); |
Riku Voipio | 76e83be | 2007-10-26 14:14:23 +0300 | [diff] [blame] | 716 | static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 717 | show_pwm_mode, set_pwm_mode, 0); |
| 718 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, |
| 719 | show_pwm, set_pwm, 1); |
| 720 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, |
| 721 | show_pwm_enable, set_pwm_enable, 1); |
Riku Voipio | 76e83be | 2007-10-26 14:14:23 +0300 | [diff] [blame] | 722 | static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 723 | show_pwm_mode, set_pwm_mode, 1); |
| 724 | |
| 725 | static struct attribute *f75375_attributes[] = { |
| 726 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 727 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 728 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 729 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 730 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 731 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, |
| 732 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 733 | &sensor_dev_attr_fan1_max.dev_attr.attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 734 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 735 | &sensor_dev_attr_fan1_target.dev_attr.attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 736 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 737 | &sensor_dev_attr_fan2_max.dev_attr.attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 738 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
Guenter Roeck | 740f6be3 | 2011-12-08 09:59:25 -0800 | [diff] [blame] | 739 | &sensor_dev_attr_fan2_target.dev_attr.attr, |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 740 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 741 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 742 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, |
| 743 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 744 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 745 | &sensor_dev_attr_pwm2_mode.dev_attr.attr, |
| 746 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 747 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 748 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 749 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 750 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 751 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 752 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 753 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 754 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 755 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 756 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 757 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 758 | NULL |
| 759 | }; |
| 760 | |
| 761 | static const struct attribute_group f75375_group = { |
| 762 | .attrs = f75375_attributes, |
| 763 | }; |
| 764 | |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 765 | static void f75375_init(struct i2c_client *client, struct f75375_data *data, |
| 766 | struct f75375s_platform_data *f75375s_pdata) |
| 767 | { |
| 768 | int nr; |
Guenter Roeck | b1b561a | 2011-12-08 10:15:51 -0800 | [diff] [blame] | 769 | |
| 770 | if (!f75375s_pdata) { |
| 771 | u8 conf, mode; |
| 772 | int nr; |
| 773 | |
| 774 | conf = f75375_read8(client, F75375_REG_CONFIG1); |
| 775 | mode = f75375_read8(client, F75375_REG_FAN_TIMER); |
| 776 | for (nr = 0; nr < 2; nr++) { |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 777 | if (data->kind == f75387) { |
| 778 | bool manu, duty; |
| 779 | |
Nikolaus Schulz | a367a1e | 2012-02-08 18:56:09 +0100 | [diff] [blame] | 780 | if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr)))) |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 781 | data->pwm_mode[nr] = 1; |
| 782 | |
| 783 | manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1); |
| 784 | duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1); |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 785 | if (!manu && duty) |
| 786 | /* auto, pwm */ |
| 787 | data->pwm_enable[nr] = 4; |
| 788 | else if (manu && !duty) |
| 789 | /* manual, speed */ |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 790 | data->pwm_enable[nr] = 3; |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 791 | else if (!manu && !duty) |
| 792 | /* automatic, speed */ |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 793 | data->pwm_enable[nr] = 2; |
| 794 | else |
Nikolaus Schulz | b17d656 | 2012-02-28 16:15:53 -0500 | [diff] [blame] | 795 | /* manual, pwm */ |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 796 | data->pwm_enable[nr] = 1; |
| 797 | } else { |
| 798 | if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr)))) |
| 799 | data->pwm_mode[nr] = 1; |
| 800 | |
| 801 | switch ((mode >> FAN_CTRL_MODE(nr)) & 3) { |
| 802 | case 0: /* speed */ |
| 803 | data->pwm_enable[nr] = 3; |
| 804 | break; |
| 805 | case 1: /* automatic */ |
| 806 | data->pwm_enable[nr] = 2; |
| 807 | break; |
| 808 | default: /* manual */ |
| 809 | data->pwm_enable[nr] = 1; |
| 810 | break; |
| 811 | } |
Guenter Roeck | b1b561a | 2011-12-08 10:15:51 -0800 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | return; |
| 815 | } |
| 816 | |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 817 | set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]); |
| 818 | set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]); |
| 819 | for (nr = 0; nr < 2; nr++) { |
Nikolaus Schulz | 15d1ad0 | 2012-02-28 16:15:54 -0500 | [diff] [blame] | 820 | if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) || |
| 821 | !duty_mode_enabled(f75375s_pdata->pwm_enable[nr])) |
| 822 | continue; |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 823 | data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255); |
Nikolaus Schulz | 331255d | 2012-02-28 22:15:51 +0100 | [diff] [blame] | 824 | f75375_write_pwm(client, nr); |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | } |
| 828 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 829 | static int f75375_probe(struct i2c_client *client, |
| 830 | const struct i2c_device_id *id) |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 831 | { |
Andrew Klossner | 51b3e27 | 2009-03-12 13:36:39 +0100 | [diff] [blame] | 832 | struct f75375_data *data; |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 833 | struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data; |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 834 | int err; |
| 835 | |
| 836 | if (!i2c_check_functionality(client->adapter, |
| 837 | I2C_FUNC_SMBUS_BYTE_DATA)) |
| 838 | return -EIO; |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 839 | data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL); |
| 840 | if (!data) |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 841 | return -ENOMEM; |
| 842 | |
| 843 | i2c_set_clientdata(client, data); |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 844 | mutex_init(&data->update_lock); |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 845 | data->kind = id->driver_data; |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 846 | |
Guenter Roeck | 4fd826e | 2011-12-08 09:54:18 -0800 | [diff] [blame] | 847 | err = sysfs_create_group(&client->dev.kobj, &f75375_group); |
| 848 | if (err) |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 849 | goto exit_free; |
| 850 | |
Nikolaus Schulz | edeea10 | 2012-02-28 22:15:52 +0100 | [diff] [blame] | 851 | if (data->kind != f75373) { |
Riku Voipio | 76e83be | 2007-10-26 14:14:23 +0300 | [diff] [blame] | 852 | err = sysfs_chmod_file(&client->dev.kobj, |
| 853 | &sensor_dev_attr_pwm1_mode.dev_attr.attr, |
| 854 | S_IRUGO | S_IWUSR); |
| 855 | if (err) |
| 856 | goto exit_remove; |
| 857 | err = sysfs_chmod_file(&client->dev.kobj, |
| 858 | &sensor_dev_attr_pwm2_mode.dev_attr.attr, |
| 859 | S_IRUGO | S_IWUSR); |
| 860 | if (err) |
| 861 | goto exit_remove; |
| 862 | } |
| 863 | |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 864 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 865 | if (IS_ERR(data->hwmon_dev)) { |
| 866 | err = PTR_ERR(data->hwmon_dev); |
| 867 | goto exit_remove; |
| 868 | } |
| 869 | |
Guenter Roeck | b1b561a | 2011-12-08 10:15:51 -0800 | [diff] [blame] | 870 | f75375_init(client, data, f75375s_pdata); |
Riku Voipio | ff312d0 | 2007-09-26 13:14:40 +0300 | [diff] [blame] | 871 | |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 872 | return 0; |
| 873 | |
| 874 | exit_remove: |
| 875 | sysfs_remove_group(&client->dev.kobj, &f75375_group); |
| 876 | exit_free: |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 877 | kfree(data); |
Riku Voipio | 620c142 | 2007-10-16 12:46:49 +0300 | [diff] [blame] | 878 | return err; |
| 879 | } |
| 880 | |
| 881 | static int f75375_remove(struct i2c_client *client) |
| 882 | { |
| 883 | struct f75375_data *data = i2c_get_clientdata(client); |
| 884 | hwmon_device_unregister(data->hwmon_dev); |
| 885 | sysfs_remove_group(&client->dev.kobj, &f75375_group); |
| 886 | kfree(data); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 890 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 891 | static int f75375_detect(struct i2c_client *client, |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 892 | struct i2c_board_info *info) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 893 | { |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 894 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 895 | u16 vendid, chipid; |
| 896 | u8 version; |
| 897 | const char *name; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 898 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 899 | vendid = f75375_read16(client, F75375_REG_VENDOR); |
| 900 | chipid = f75375_read16(client, F75375_CHIP_ID); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 901 | if (vendid != 0x1934) |
| 902 | return -ENODEV; |
| 903 | |
| 904 | if (chipid == 0x0306) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 905 | name = "f75375"; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 906 | else if (chipid == 0x0204) |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 907 | name = "f75373"; |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 908 | else if (chipid == 0x0410) |
| 909 | name = "f75387"; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 910 | else |
| 911 | return -ENODEV; |
| 912 | |
| 913 | version = f75375_read8(client, F75375_REG_VERSION); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 914 | dev_info(&adapter->dev, "found %s version: %02X\n", name, version); |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 915 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 916 | |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 917 | return 0; |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static int __init sensors_f75375_init(void) |
| 921 | { |
Jean Delvare | 935ada8 | 2008-07-16 19:30:11 +0200 | [diff] [blame] | 922 | return i2c_add_driver(&f75375_driver); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | static void __exit sensors_f75375_exit(void) |
| 926 | { |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 927 | i2c_del_driver(&f75375_driver); |
| 928 | } |
| 929 | |
Riku Voipio | b26e0ed | 2009-03-03 21:37:17 +0200 | [diff] [blame] | 930 | MODULE_AUTHOR("Riku Voipio"); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 931 | MODULE_LICENSE("GPL"); |
Björn Gerhart | f58c44e | 2011-12-13 13:19:58 -0500 | [diff] [blame] | 932 | MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver"); |
Riku Voipio | 84f1e44 | 2007-08-24 13:03:09 +0300 | [diff] [blame] | 933 | |
| 934 | module_init(sensors_f75375_init); |
| 935 | module_exit(sensors_f75375_exit); |