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