Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives |
| 3 | * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
| 5 | * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 6 | * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de> |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 7 | * |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 8 | * Derived from the lm83 driver by Jean Delvare |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/hwmon.h> |
| 20 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 21 | #include <linux/hwmon-vid.h> |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 22 | #include <linux/err.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 24 | |
| 25 | /* Indexes for the sysfs hooks */ |
| 26 | |
| 27 | #define INPUT 0 |
| 28 | #define MIN 1 |
| 29 | #define MAX 2 |
| 30 | #define CONTROL 3 |
| 31 | #define OFFSET 3 |
| 32 | #define AUTOMIN 4 |
| 33 | #define THERM 5 |
| 34 | #define HYSTERSIS 6 |
| 35 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 36 | /* |
| 37 | * These are unique identifiers for the sysfs functions - unlike the |
| 38 | * numbers above, these are not also indexes into an array |
| 39 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 40 | |
| 41 | #define ALARM 9 |
| 42 | #define FAULT 10 |
| 43 | |
| 44 | /* 7475 Common Registers */ |
| 45 | |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 46 | #define REG_DEVREV2 0x12 /* ADT7490 only */ |
| 47 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 48 | #define REG_VTT 0x1E /* ADT7490 only */ |
| 49 | #define REG_EXTEND3 0x1F /* ADT7490 only */ |
| 50 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 51 | #define REG_VOLTAGE_BASE 0x20 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 52 | #define REG_TEMP_BASE 0x25 |
| 53 | #define REG_TACH_BASE 0x28 |
| 54 | #define REG_PWM_BASE 0x30 |
| 55 | #define REG_PWM_MAX_BASE 0x38 |
| 56 | |
| 57 | #define REG_DEVID 0x3D |
| 58 | #define REG_VENDID 0x3E |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 59 | #define REG_DEVID2 0x3F |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 60 | |
| 61 | #define REG_STATUS1 0x41 |
| 62 | #define REG_STATUS2 0x42 |
| 63 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 64 | #define REG_VID 0x43 /* ADT7476 only */ |
| 65 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 66 | #define REG_VOLTAGE_MIN_BASE 0x44 |
| 67 | #define REG_VOLTAGE_MAX_BASE 0x45 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 68 | |
| 69 | #define REG_TEMP_MIN_BASE 0x4E |
| 70 | #define REG_TEMP_MAX_BASE 0x4F |
| 71 | |
| 72 | #define REG_TACH_MIN_BASE 0x54 |
| 73 | |
| 74 | #define REG_PWM_CONFIG_BASE 0x5C |
| 75 | |
| 76 | #define REG_TEMP_TRANGE_BASE 0x5F |
| 77 | |
| 78 | #define REG_PWM_MIN_BASE 0x64 |
| 79 | |
| 80 | #define REG_TEMP_TMIN_BASE 0x67 |
| 81 | #define REG_TEMP_THERM_BASE 0x6A |
| 82 | |
| 83 | #define REG_REMOTE1_HYSTERSIS 0x6D |
| 84 | #define REG_REMOTE2_HYSTERSIS 0x6E |
| 85 | |
| 86 | #define REG_TEMP_OFFSET_BASE 0x70 |
| 87 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 88 | #define REG_CONFIG2 0x73 |
| 89 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 90 | #define REG_EXTEND1 0x76 |
| 91 | #define REG_EXTEND2 0x77 |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 92 | |
| 93 | #define REG_CONFIG3 0x78 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 94 | #define REG_CONFIG5 0x7C |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 95 | #define REG_CONFIG4 0x7D |
| 96 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 97 | #define REG_STATUS4 0x81 /* ADT7490 only */ |
| 98 | |
| 99 | #define REG_VTT_MIN 0x84 /* ADT7490 only */ |
| 100 | #define REG_VTT_MAX 0x86 /* ADT7490 only */ |
| 101 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 102 | #define VID_VIDSEL 0x80 /* ADT7476 only */ |
| 103 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 104 | #define CONFIG2_ATTN 0x20 |
| 105 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 106 | #define CONFIG3_SMBALERT 0x01 |
| 107 | #define CONFIG3_THERM 0x02 |
| 108 | |
| 109 | #define CONFIG4_PINFUNC 0x03 |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 110 | #define CONFIG4_MAXDUTY 0x08 |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 111 | #define CONFIG4_ATTN_IN10 0x30 |
| 112 | #define CONFIG4_ATTN_IN43 0xC0 |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 113 | |
| 114 | #define CONFIG5_TWOSCOMP 0x01 |
| 115 | #define CONFIG5_TEMPOFFSET 0x02 |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 116 | #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 117 | |
| 118 | /* ADT7475 Settings */ |
| 119 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 120 | #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 121 | #define ADT7475_TEMP_COUNT 3 |
| 122 | #define ADT7475_TACH_COUNT 4 |
| 123 | #define ADT7475_PWM_COUNT 3 |
| 124 | |
| 125 | /* Macro to read the registers */ |
| 126 | |
| 127 | #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) |
| 128 | |
| 129 | /* Macros to easily index the registers */ |
| 130 | |
| 131 | #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) |
| 132 | #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) |
| 133 | |
| 134 | #define PWM_REG(idx) (REG_PWM_BASE + (idx)) |
| 135 | #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) |
| 136 | #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) |
| 137 | #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) |
| 138 | |
| 139 | #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) |
| 140 | #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) |
| 141 | #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) |
| 142 | |
| 143 | #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) |
| 144 | #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) |
| 145 | #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) |
| 146 | #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) |
| 147 | #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) |
| 148 | #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) |
| 149 | #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) |
| 150 | |
Jean Delvare | 918ee91 | 2010-10-28 20:31:50 +0200 | [diff] [blame] | 151 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 152 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 153 | enum chips { adt7473, adt7475, adt7476, adt7490 }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 154 | |
| 155 | static const struct i2c_device_id adt7475_id[] = { |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 156 | { "adt7473", adt7473 }, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 157 | { "adt7475", adt7475 }, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 158 | { "adt7476", adt7476 }, |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 159 | { "adt7490", adt7490 }, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 160 | { } |
| 161 | }; |
| 162 | MODULE_DEVICE_TABLE(i2c, adt7475_id); |
| 163 | |
| 164 | struct adt7475_data { |
| 165 | struct device *hwmon_dev; |
| 166 | struct mutex lock; |
| 167 | |
| 168 | unsigned long measure_updated; |
| 169 | unsigned long limits_updated; |
| 170 | char valid; |
| 171 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 172 | u8 config4; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 173 | u8 config5; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 174 | u8 has_voltage; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 175 | u8 bypass_attn; /* Bypass voltage attenuator */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 176 | u8 has_pwm2:1; |
| 177 | u8 has_fan4:1; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 178 | u8 has_vid:1; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 179 | u32 alarms; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 180 | u16 voltage[3][6]; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 181 | u16 temp[7][3]; |
| 182 | u16 tach[2][4]; |
| 183 | u8 pwm[4][3]; |
| 184 | u8 range[3]; |
| 185 | u8 pwmctl[3]; |
| 186 | u8 pwmchan[3]; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 187 | |
| 188 | u8 vid; |
| 189 | u8 vrm; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static struct i2c_driver adt7475_driver; |
| 193 | static struct adt7475_data *adt7475_update_device(struct device *dev); |
| 194 | static void adt7475_read_hystersis(struct i2c_client *client); |
| 195 | static void adt7475_read_pwm(struct i2c_client *client, int index); |
| 196 | |
| 197 | /* Given a temp value, convert it to register value */ |
| 198 | |
| 199 | static inline u16 temp2reg(struct adt7475_data *data, long val) |
| 200 | { |
| 201 | u16 ret; |
| 202 | |
| 203 | if (!(data->config5 & CONFIG5_TWOSCOMP)) { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 204 | val = clamp_val(val, -64000, 191000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 205 | ret = (val + 64500) / 1000; |
| 206 | } else { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 207 | val = clamp_val(val, -128000, 127000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 208 | if (val < -500) |
| 209 | ret = (256500 + val) / 1000; |
| 210 | else |
| 211 | ret = (val + 500) / 1000; |
| 212 | } |
| 213 | |
| 214 | return ret << 2; |
| 215 | } |
| 216 | |
| 217 | /* Given a register value, convert it to a real temp value */ |
| 218 | |
| 219 | static inline int reg2temp(struct adt7475_data *data, u16 reg) |
| 220 | { |
| 221 | if (data->config5 & CONFIG5_TWOSCOMP) { |
| 222 | if (reg >= 512) |
| 223 | return (reg - 1024) * 250; |
| 224 | else |
| 225 | return reg * 250; |
| 226 | } else |
| 227 | return (reg - 256) * 250; |
| 228 | } |
| 229 | |
| 230 | static inline int tach2rpm(u16 tach) |
| 231 | { |
| 232 | if (tach == 0 || tach == 0xFFFF) |
| 233 | return 0; |
| 234 | |
| 235 | return (90000 * 60) / tach; |
| 236 | } |
| 237 | |
| 238 | static inline u16 rpm2tach(unsigned long rpm) |
| 239 | { |
| 240 | if (rpm == 0) |
| 241 | return 0; |
| 242 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 243 | return clamp_val((90000 * 60) / rpm, 1, 0xFFFF); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 246 | /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */ |
| 247 | static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = { |
| 248 | { 45, 94 }, /* +2.5V */ |
| 249 | { 175, 525 }, /* Vccp */ |
| 250 | { 68, 71 }, /* Vcc */ |
| 251 | { 93, 47 }, /* +5V */ |
| 252 | { 120, 20 }, /* +12V */ |
| 253 | { 45, 45 }, /* Vtt */ |
| 254 | }; |
| 255 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 256 | static inline int reg2volt(int channel, u16 reg, u8 bypass_attn) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 257 | { |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 258 | const int *r = adt7473_in_scaling[channel]; |
| 259 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 260 | if (bypass_attn & (1 << channel)) |
| 261 | return DIV_ROUND_CLOSEST(reg * 2250, 1024); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 262 | return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 265 | static inline u16 volt2reg(int channel, long volt, u8 bypass_attn) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 266 | { |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 267 | const int *r = adt7473_in_scaling[channel]; |
| 268 | long reg; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 269 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 270 | if (bypass_attn & (1 << channel)) |
Luuk Paulussen | c5e1aae | 2019-12-06 12:16:59 +1300 | [diff] [blame] | 271 | reg = DIV_ROUND_CLOSEST(volt * 1024, 2250); |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 272 | else |
Luuk Paulussen | c5e1aae | 2019-12-06 12:16:59 +1300 | [diff] [blame] | 273 | reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024, |
| 274 | (r[0] + r[1]) * 2250); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 275 | return clamp_val(reg, 0, 1023) & (0xff << 2); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Dan Carpenter | 8ccd9e4 | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 278 | static int adt7475_read_word(struct i2c_client *client, int reg) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 279 | { |
Dan Carpenter | 8ccd9e4 | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 280 | int val1, val2; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 281 | |
Dan Carpenter | 8ccd9e4 | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 282 | val1 = i2c_smbus_read_byte_data(client, reg); |
| 283 | if (val1 < 0) |
| 284 | return val1; |
| 285 | val2 = i2c_smbus_read_byte_data(client, reg + 1); |
| 286 | if (val2 < 0) |
| 287 | return val2; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 288 | |
Dan Carpenter | 8ccd9e4 | 2018-08-14 13:07:47 +0300 | [diff] [blame] | 289 | return val1 | (val2 << 8); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) |
| 293 | { |
| 294 | i2c_smbus_write_byte_data(client, reg + 1, val >> 8); |
| 295 | i2c_smbus_write_byte_data(client, reg, val & 0xFF); |
| 296 | } |
| 297 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 298 | /* |
| 299 | * Find the nearest value in a table - used for pwm frequency and |
| 300 | * auto temp range |
| 301 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 302 | static int find_nearest(long val, const int *array, int size) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | if (val < array[0]) |
| 307 | return 0; |
| 308 | |
| 309 | if (val > array[size - 1]) |
| 310 | return size - 1; |
| 311 | |
| 312 | for (i = 0; i < size - 1; i++) { |
| 313 | int a, b; |
| 314 | |
| 315 | if (val > array[i + 1]) |
| 316 | continue; |
| 317 | |
| 318 | a = val - array[i]; |
| 319 | b = array[i + 1] - val; |
| 320 | |
| 321 | return (a <= b) ? i : i + 1; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, |
| 328 | char *buf) |
| 329 | { |
| 330 | struct adt7475_data *data = adt7475_update_device(dev); |
| 331 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 332 | unsigned short val; |
| 333 | |
| 334 | switch (sattr->nr) { |
| 335 | case ALARM: |
| 336 | return sprintf(buf, "%d\n", |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 337 | (data->alarms >> sattr->index) & 1); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 338 | default: |
| 339 | val = data->voltage[sattr->nr][sattr->index]; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 340 | return sprintf(buf, "%d\n", |
| 341 | reg2volt(sattr->index, val, data->bypass_attn)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, |
| 346 | const char *buf, size_t count) |
| 347 | { |
| 348 | |
| 349 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 350 | struct i2c_client *client = to_i2c_client(dev); |
| 351 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 352 | unsigned char reg; |
| 353 | long val; |
| 354 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 355 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | |
| 358 | mutex_lock(&data->lock); |
| 359 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 360 | data->voltage[sattr->nr][sattr->index] = |
| 361 | volt2reg(sattr->index, val, data->bypass_attn); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 362 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 363 | if (sattr->index < ADT7475_VOLTAGE_COUNT) { |
| 364 | if (sattr->nr == MIN) |
| 365 | reg = VOLTAGE_MIN_REG(sattr->index); |
| 366 | else |
| 367 | reg = VOLTAGE_MAX_REG(sattr->index); |
| 368 | } else { |
| 369 | if (sattr->nr == MIN) |
| 370 | reg = REG_VTT_MIN; |
| 371 | else |
| 372 | reg = REG_VTT_MAX; |
| 373 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 374 | |
| 375 | i2c_smbus_write_byte_data(client, reg, |
| 376 | data->voltage[sattr->nr][sattr->index] >> 2); |
| 377 | mutex_unlock(&data->lock); |
| 378 | |
| 379 | return count; |
| 380 | } |
| 381 | |
| 382 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 383 | char *buf) |
| 384 | { |
| 385 | struct adt7475_data *data = adt7475_update_device(dev); |
| 386 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 387 | int out; |
| 388 | |
| 389 | switch (sattr->nr) { |
| 390 | case HYSTERSIS: |
| 391 | mutex_lock(&data->lock); |
| 392 | out = data->temp[sattr->nr][sattr->index]; |
| 393 | if (sattr->index != 1) |
| 394 | out = (out >> 4) & 0xF; |
| 395 | else |
| 396 | out = (out & 0xF); |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 397 | /* |
| 398 | * Show the value as an absolute number tied to |
| 399 | * THERM |
| 400 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 401 | out = reg2temp(data, data->temp[THERM][sattr->index]) - |
| 402 | out * 1000; |
| 403 | mutex_unlock(&data->lock); |
| 404 | break; |
| 405 | |
| 406 | case OFFSET: |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 407 | /* |
| 408 | * Offset is always 2's complement, regardless of the |
| 409 | * setting in CONFIG5 |
| 410 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 411 | mutex_lock(&data->lock); |
| 412 | out = (s8)data->temp[sattr->nr][sattr->index]; |
| 413 | if (data->config5 & CONFIG5_TEMPOFFSET) |
| 414 | out *= 1000; |
| 415 | else |
| 416 | out *= 500; |
| 417 | mutex_unlock(&data->lock); |
| 418 | break; |
| 419 | |
| 420 | case ALARM: |
| 421 | out = (data->alarms >> (sattr->index + 4)) & 1; |
| 422 | break; |
| 423 | |
| 424 | case FAULT: |
| 425 | /* Note - only for remote1 and remote2 */ |
Jean Delvare | cf312e0 | 2009-11-16 12:45:39 +0100 | [diff] [blame] | 426 | out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | default: |
| 430 | /* All other temp values are in the configured format */ |
| 431 | out = reg2temp(data, data->temp[sattr->nr][sattr->index]); |
| 432 | } |
| 433 | |
| 434 | return sprintf(buf, "%d\n", out); |
| 435 | } |
| 436 | |
| 437 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, |
| 438 | const char *buf, size_t count) |
| 439 | { |
| 440 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 441 | struct i2c_client *client = to_i2c_client(dev); |
| 442 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 443 | unsigned char reg = 0; |
| 444 | u8 out; |
| 445 | int temp; |
| 446 | long val; |
| 447 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 448 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 449 | return -EINVAL; |
| 450 | |
| 451 | mutex_lock(&data->lock); |
| 452 | |
| 453 | /* We need the config register in all cases for temp <-> reg conv. */ |
| 454 | data->config5 = adt7475_read(REG_CONFIG5); |
| 455 | |
| 456 | switch (sattr->nr) { |
| 457 | case OFFSET: |
| 458 | if (data->config5 & CONFIG5_TEMPOFFSET) { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 459 | val = clamp_val(val, -63000, 127000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 460 | out = data->temp[OFFSET][sattr->index] = val / 1000; |
| 461 | } else { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 462 | val = clamp_val(val, -63000, 64000); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 463 | out = data->temp[OFFSET][sattr->index] = val / 500; |
| 464 | } |
| 465 | break; |
| 466 | |
| 467 | case HYSTERSIS: |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 468 | /* |
| 469 | * The value will be given as an absolute value, turn it |
| 470 | * into an offset based on THERM |
| 471 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 472 | |
| 473 | /* Read fresh THERM and HYSTERSIS values from the chip */ |
| 474 | data->temp[THERM][sattr->index] = |
| 475 | adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; |
| 476 | adt7475_read_hystersis(client); |
| 477 | |
| 478 | temp = reg2temp(data, data->temp[THERM][sattr->index]); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 479 | val = clamp_val(val, temp - 15000, temp); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 480 | val = (temp - val) / 1000; |
| 481 | |
| 482 | if (sattr->index != 1) { |
| 483 | data->temp[HYSTERSIS][sattr->index] &= 0xF0; |
| 484 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; |
| 485 | } else { |
| 486 | data->temp[HYSTERSIS][sattr->index] &= 0x0F; |
| 487 | data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); |
| 488 | } |
| 489 | |
| 490 | out = data->temp[HYSTERSIS][sattr->index]; |
| 491 | break; |
| 492 | |
| 493 | default: |
| 494 | data->temp[sattr->nr][sattr->index] = temp2reg(data, val); |
| 495 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 496 | /* |
| 497 | * We maintain an extra 2 digits of precision for simplicity |
| 498 | * - shift those back off before writing the value |
| 499 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 500 | out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); |
| 501 | } |
| 502 | |
| 503 | switch (sattr->nr) { |
| 504 | case MIN: |
| 505 | reg = TEMP_MIN_REG(sattr->index); |
| 506 | break; |
| 507 | case MAX: |
| 508 | reg = TEMP_MAX_REG(sattr->index); |
| 509 | break; |
| 510 | case OFFSET: |
| 511 | reg = TEMP_OFFSET_REG(sattr->index); |
| 512 | break; |
| 513 | case AUTOMIN: |
| 514 | reg = TEMP_TMIN_REG(sattr->index); |
| 515 | break; |
| 516 | case THERM: |
| 517 | reg = TEMP_THERM_REG(sattr->index); |
| 518 | break; |
| 519 | case HYSTERSIS: |
| 520 | if (sattr->index != 2) |
| 521 | reg = REG_REMOTE1_HYSTERSIS; |
| 522 | else |
| 523 | reg = REG_REMOTE2_HYSTERSIS; |
| 524 | |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | i2c_smbus_write_byte_data(client, reg, out); |
| 529 | |
| 530 | mutex_unlock(&data->lock); |
| 531 | return count; |
| 532 | } |
| 533 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 534 | /* |
| 535 | * Table of autorange values - the user will write the value in millidegrees, |
| 536 | * and we'll convert it |
| 537 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 538 | static const int autorange_table[] = { |
| 539 | 2000, 2500, 3330, 4000, 5000, 6670, 8000, |
| 540 | 10000, 13330, 16000, 20000, 26670, 32000, 40000, |
| 541 | 53330, 80000 |
| 542 | }; |
| 543 | |
| 544 | static ssize_t show_point2(struct device *dev, struct device_attribute *attr, |
| 545 | char *buf) |
| 546 | { |
| 547 | struct adt7475_data *data = adt7475_update_device(dev); |
| 548 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 549 | int out, val; |
| 550 | |
| 551 | mutex_lock(&data->lock); |
| 552 | out = (data->range[sattr->index] >> 4) & 0x0F; |
| 553 | val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
| 554 | mutex_unlock(&data->lock); |
| 555 | |
| 556 | return sprintf(buf, "%d\n", val + autorange_table[out]); |
| 557 | } |
| 558 | |
| 559 | static ssize_t set_point2(struct device *dev, struct device_attribute *attr, |
| 560 | const char *buf, size_t count) |
| 561 | { |
| 562 | struct i2c_client *client = to_i2c_client(dev); |
| 563 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 564 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 565 | int temp; |
| 566 | long val; |
| 567 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 568 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 569 | return -EINVAL; |
| 570 | |
| 571 | mutex_lock(&data->lock); |
| 572 | |
| 573 | /* Get a fresh copy of the needed registers */ |
| 574 | data->config5 = adt7475_read(REG_CONFIG5); |
| 575 | data->temp[AUTOMIN][sattr->index] = |
| 576 | adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; |
| 577 | data->range[sattr->index] = |
| 578 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 579 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 580 | /* |
| 581 | * The user will write an absolute value, so subtract the start point |
| 582 | * to figure the range |
| 583 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 584 | temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 585 | val = clamp_val(val, temp + autorange_table[0], |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 586 | temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); |
| 587 | val -= temp; |
| 588 | |
| 589 | /* Find the nearest table entry to what the user wrote */ |
| 590 | val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table)); |
| 591 | |
| 592 | data->range[sattr->index] &= ~0xF0; |
| 593 | data->range[sattr->index] |= val << 4; |
| 594 | |
| 595 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 596 | data->range[sattr->index]); |
| 597 | |
| 598 | mutex_unlock(&data->lock); |
| 599 | return count; |
| 600 | } |
| 601 | |
| 602 | static ssize_t show_tach(struct device *dev, struct device_attribute *attr, |
| 603 | char *buf) |
| 604 | { |
| 605 | struct adt7475_data *data = adt7475_update_device(dev); |
| 606 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 607 | int out; |
| 608 | |
| 609 | if (sattr->nr == ALARM) |
| 610 | out = (data->alarms >> (sattr->index + 10)) & 1; |
| 611 | else |
| 612 | out = tach2rpm(data->tach[sattr->nr][sattr->index]); |
| 613 | |
| 614 | return sprintf(buf, "%d\n", out); |
| 615 | } |
| 616 | |
| 617 | static ssize_t set_tach(struct device *dev, struct device_attribute *attr, |
| 618 | const char *buf, size_t count) |
| 619 | { |
| 620 | |
| 621 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 622 | struct i2c_client *client = to_i2c_client(dev); |
| 623 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 624 | unsigned long val; |
| 625 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 626 | if (kstrtoul(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 627 | return -EINVAL; |
| 628 | |
| 629 | mutex_lock(&data->lock); |
| 630 | |
| 631 | data->tach[MIN][sattr->index] = rpm2tach(val); |
| 632 | |
| 633 | adt7475_write_word(client, TACH_MIN_REG(sattr->index), |
| 634 | data->tach[MIN][sattr->index]); |
| 635 | |
| 636 | mutex_unlock(&data->lock); |
| 637 | return count; |
| 638 | } |
| 639 | |
| 640 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 641 | char *buf) |
| 642 | { |
| 643 | struct adt7475_data *data = adt7475_update_device(dev); |
| 644 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 645 | |
| 646 | return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); |
| 647 | } |
| 648 | |
| 649 | static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, |
| 650 | char *buf) |
| 651 | { |
| 652 | struct adt7475_data *data = adt7475_update_device(dev); |
| 653 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 654 | |
| 655 | return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); |
| 656 | } |
| 657 | |
| 658 | static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 659 | char *buf) |
| 660 | { |
| 661 | struct adt7475_data *data = adt7475_update_device(dev); |
| 662 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 663 | |
| 664 | return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); |
| 665 | } |
| 666 | |
| 667 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 668 | const char *buf, size_t count) |
| 669 | { |
| 670 | |
| 671 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 672 | struct i2c_client *client = to_i2c_client(dev); |
| 673 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 674 | unsigned char reg = 0; |
| 675 | long val; |
| 676 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 677 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 678 | return -EINVAL; |
| 679 | |
| 680 | mutex_lock(&data->lock); |
| 681 | |
| 682 | switch (sattr->nr) { |
| 683 | case INPUT: |
| 684 | /* Get a fresh value for CONTROL */ |
| 685 | data->pwm[CONTROL][sattr->index] = |
| 686 | adt7475_read(PWM_CONFIG_REG(sattr->index)); |
| 687 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 688 | /* |
| 689 | * If we are not in manual mode, then we shouldn't allow |
| 690 | * the user to set the pwm speed |
| 691 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 692 | if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { |
| 693 | mutex_unlock(&data->lock); |
| 694 | return count; |
| 695 | } |
| 696 | |
| 697 | reg = PWM_REG(sattr->index); |
| 698 | break; |
| 699 | |
| 700 | case MIN: |
| 701 | reg = PWM_MIN_REG(sattr->index); |
| 702 | break; |
| 703 | |
| 704 | case MAX: |
| 705 | reg = PWM_MAX_REG(sattr->index); |
| 706 | break; |
| 707 | } |
| 708 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 709 | data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 710 | i2c_smbus_write_byte_data(client, reg, |
| 711 | data->pwm[sattr->nr][sattr->index]); |
| 712 | |
| 713 | mutex_unlock(&data->lock); |
| 714 | |
| 715 | return count; |
| 716 | } |
| 717 | |
| 718 | /* Called by set_pwmctrl and set_pwmchan */ |
| 719 | |
| 720 | static int hw_set_pwm(struct i2c_client *client, int index, |
| 721 | unsigned int pwmctl, unsigned int pwmchan) |
| 722 | { |
| 723 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 724 | long val = 0; |
| 725 | |
| 726 | switch (pwmctl) { |
| 727 | case 0: |
| 728 | val = 0x03; /* Run at full speed */ |
| 729 | break; |
| 730 | case 1: |
| 731 | val = 0x07; /* Manual mode */ |
| 732 | break; |
| 733 | case 2: |
| 734 | switch (pwmchan) { |
| 735 | case 1: |
| 736 | /* Remote1 controls PWM */ |
| 737 | val = 0x00; |
| 738 | break; |
| 739 | case 2: |
| 740 | /* local controls PWM */ |
| 741 | val = 0x01; |
| 742 | break; |
| 743 | case 4: |
| 744 | /* remote2 controls PWM */ |
| 745 | val = 0x02; |
| 746 | break; |
| 747 | case 6: |
| 748 | /* local/remote2 control PWM */ |
| 749 | val = 0x05; |
| 750 | break; |
| 751 | case 7: |
| 752 | /* All three control PWM */ |
| 753 | val = 0x06; |
| 754 | break; |
| 755 | default: |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | break; |
| 759 | default: |
| 760 | return -EINVAL; |
| 761 | } |
| 762 | |
| 763 | data->pwmctl[index] = pwmctl; |
| 764 | data->pwmchan[index] = pwmchan; |
| 765 | |
| 766 | data->pwm[CONTROL][index] &= ~0xE0; |
| 767 | data->pwm[CONTROL][index] |= (val & 7) << 5; |
| 768 | |
| 769 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 770 | data->pwm[CONTROL][index]); |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, |
| 776 | const char *buf, size_t count) |
| 777 | { |
| 778 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 779 | struct i2c_client *client = to_i2c_client(dev); |
| 780 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 781 | int r; |
| 782 | long val; |
| 783 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 784 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | |
| 787 | mutex_lock(&data->lock); |
| 788 | /* Read Modify Write PWM values */ |
| 789 | adt7475_read_pwm(client, sattr->index); |
| 790 | r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); |
| 791 | if (r) |
| 792 | count = r; |
| 793 | mutex_unlock(&data->lock); |
| 794 | |
| 795 | return count; |
| 796 | } |
| 797 | |
| 798 | static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, |
| 799 | const char *buf, size_t count) |
| 800 | { |
| 801 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 802 | struct i2c_client *client = to_i2c_client(dev); |
| 803 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 804 | int r; |
| 805 | long val; |
| 806 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 807 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 808 | return -EINVAL; |
| 809 | |
| 810 | mutex_lock(&data->lock); |
| 811 | /* Read Modify Write PWM values */ |
| 812 | adt7475_read_pwm(client, sattr->index); |
| 813 | r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); |
| 814 | if (r) |
| 815 | count = r; |
| 816 | mutex_unlock(&data->lock); |
| 817 | |
| 818 | return count; |
| 819 | } |
| 820 | |
| 821 | /* List of frequencies for the PWM */ |
| 822 | static const int pwmfreq_table[] = { |
| 823 | 11, 14, 22, 29, 35, 44, 58, 88 |
| 824 | }; |
| 825 | |
| 826 | static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 827 | char *buf) |
| 828 | { |
| 829 | struct adt7475_data *data = adt7475_update_device(dev); |
| 830 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 831 | |
| 832 | return sprintf(buf, "%d\n", |
| 833 | pwmfreq_table[data->range[sattr->index] & 7]); |
| 834 | } |
| 835 | |
| 836 | static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, |
| 837 | const char *buf, size_t count) |
| 838 | { |
| 839 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 840 | struct i2c_client *client = to_i2c_client(dev); |
| 841 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 842 | int out; |
| 843 | long val; |
| 844 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 845 | if (kstrtol(buf, 10, &val)) |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 846 | return -EINVAL; |
| 847 | |
| 848 | out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); |
| 849 | |
| 850 | mutex_lock(&data->lock); |
| 851 | |
| 852 | data->range[sattr->index] = |
| 853 | adt7475_read(TEMP_TRANGE_REG(sattr->index)); |
| 854 | data->range[sattr->index] &= ~7; |
| 855 | data->range[sattr->index] |= out; |
| 856 | |
| 857 | i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), |
| 858 | data->range[sattr->index]); |
| 859 | |
| 860 | mutex_unlock(&data->lock); |
| 861 | return count; |
| 862 | } |
| 863 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 864 | static ssize_t show_pwm_at_crit(struct device *dev, |
| 865 | struct device_attribute *devattr, char *buf) |
| 866 | { |
| 867 | struct adt7475_data *data = adt7475_update_device(dev); |
| 868 | return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); |
| 869 | } |
| 870 | |
| 871 | static ssize_t set_pwm_at_crit(struct device *dev, |
| 872 | struct device_attribute *devattr, |
| 873 | const char *buf, size_t count) |
| 874 | { |
| 875 | struct i2c_client *client = to_i2c_client(dev); |
| 876 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 877 | long val; |
| 878 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 879 | if (kstrtol(buf, 10, &val)) |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 880 | return -EINVAL; |
| 881 | if (val != 0 && val != 1) |
| 882 | return -EINVAL; |
| 883 | |
| 884 | mutex_lock(&data->lock); |
| 885 | data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); |
| 886 | if (val) |
| 887 | data->config4 |= CONFIG4_MAXDUTY; |
| 888 | else |
| 889 | data->config4 &= ~CONFIG4_MAXDUTY; |
| 890 | i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); |
| 891 | mutex_unlock(&data->lock); |
| 892 | |
| 893 | return count; |
| 894 | } |
| 895 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 896 | static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr, |
| 897 | char *buf) |
| 898 | { |
| 899 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 900 | return sprintf(buf, "%d\n", (int)data->vrm); |
| 901 | } |
| 902 | |
| 903 | static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr, |
| 904 | const char *buf, size_t count) |
| 905 | { |
| 906 | struct adt7475_data *data = dev_get_drvdata(dev); |
| 907 | long val; |
| 908 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 909 | if (kstrtol(buf, 10, &val)) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 910 | return -EINVAL; |
| 911 | if (val < 0 || val > 255) |
| 912 | return -EINVAL; |
| 913 | data->vrm = val; |
| 914 | |
| 915 | return count; |
| 916 | } |
| 917 | |
| 918 | static ssize_t show_vid(struct device *dev, struct device_attribute *devattr, |
| 919 | char *buf) |
| 920 | { |
| 921 | struct adt7475_data *data = adt7475_update_device(dev); |
| 922 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 923 | } |
| 924 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 925 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0); |
| 926 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage, |
| 927 | set_voltage, MAX, 0); |
| 928 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage, |
| 929 | set_voltage, MIN, 0); |
| 930 | static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 931 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 932 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 933 | set_voltage, MAX, 1); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 934 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 935 | set_voltage, MIN, 1); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 936 | static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); |
| 937 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2); |
| 938 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, |
| 939 | set_voltage, MAX, 2); |
| 940 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, |
| 941 | set_voltage, MIN, 2); |
| 942 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2); |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 943 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3); |
| 944 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage, |
| 945 | set_voltage, MAX, 3); |
| 946 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage, |
| 947 | set_voltage, MIN, 3); |
| 948 | static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3); |
| 949 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4); |
| 950 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage, |
| 951 | set_voltage, MAX, 4); |
| 952 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage, |
| 953 | set_voltage, MIN, 4); |
| 954 | static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8); |
| 955 | static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5); |
| 956 | static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage, |
| 957 | set_voltage, MAX, 5); |
| 958 | static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage, |
| 959 | set_voltage, MIN, 5); |
| 960 | static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 961 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); |
| 962 | static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); |
| 963 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); |
| 964 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 965 | MAX, 0); |
| 966 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 967 | MIN, 0); |
| 968 | static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, |
| 969 | set_temp, OFFSET, 0); |
| 970 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 971 | show_temp, set_temp, AUTOMIN, 0); |
| 972 | static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 973 | show_point2, set_point2, 0, 0); |
| 974 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 975 | THERM, 0); |
| 976 | static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 977 | set_temp, HYSTERSIS, 0); |
| 978 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); |
| 979 | static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); |
| 980 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 981 | MAX, 1); |
| 982 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 983 | MIN, 1); |
| 984 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, |
| 985 | set_temp, OFFSET, 1); |
| 986 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 987 | show_temp, set_temp, AUTOMIN, 1); |
| 988 | static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 989 | show_point2, set_point2, 0, 1); |
| 990 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 991 | THERM, 1); |
| 992 | static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 993 | set_temp, HYSTERSIS, 1); |
| 994 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); |
| 995 | static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); |
| 996 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); |
| 997 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 998 | MAX, 2); |
| 999 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1000 | MIN, 2); |
| 1001 | static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, |
| 1002 | set_temp, OFFSET, 2); |
| 1003 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, |
| 1004 | show_temp, set_temp, AUTOMIN, 2); |
| 1005 | static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, |
| 1006 | show_point2, set_point2, 0, 2); |
| 1007 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, |
| 1008 | THERM, 2); |
| 1009 | static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, |
| 1010 | set_temp, HYSTERSIS, 2); |
| 1011 | static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); |
| 1012 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1013 | MIN, 0); |
| 1014 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); |
| 1015 | static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); |
| 1016 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1017 | MIN, 1); |
| 1018 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); |
| 1019 | static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); |
| 1020 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1021 | MIN, 2); |
| 1022 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); |
| 1023 | static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); |
| 1024 | static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, |
| 1025 | MIN, 3); |
| 1026 | static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); |
| 1027 | static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1028 | 0); |
| 1029 | static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1030 | set_pwmfreq, INPUT, 0); |
| 1031 | static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1032 | set_pwmctrl, INPUT, 0); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1033 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1034 | show_pwmchan, set_pwmchan, INPUT, 0); |
| 1035 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1036 | set_pwm, MIN, 0); |
| 1037 | static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1038 | set_pwm, MAX, 0); |
| 1039 | static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1040 | 1); |
| 1041 | static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1042 | set_pwmfreq, INPUT, 1); |
| 1043 | static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1044 | set_pwmctrl, INPUT, 1); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1045 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1046 | show_pwmchan, set_pwmchan, INPUT, 1); |
| 1047 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1048 | set_pwm, MIN, 1); |
| 1049 | static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1050 | set_pwm, MAX, 1); |
| 1051 | static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, |
| 1052 | 2); |
| 1053 | static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, |
| 1054 | set_pwmfreq, INPUT, 2); |
| 1055 | static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, |
| 1056 | set_pwmctrl, INPUT, 2); |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1057 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1058 | show_pwmchan, set_pwmchan, INPUT, 2); |
| 1059 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1060 | set_pwm, MIN, 2); |
| 1061 | static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, |
| 1062 | set_pwm, MAX, 2); |
| 1063 | |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1064 | /* Non-standard name, might need revisiting */ |
| 1065 | static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO, |
| 1066 | show_pwm_at_crit, set_pwm_at_crit); |
| 1067 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1068 | static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm); |
| 1069 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 1070 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1071 | static struct attribute *adt7475_attrs[] = { |
| 1072 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 1073 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 1074 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 1075 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 1076 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 1077 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 1078 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 1079 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 1080 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1081 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1082 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 1083 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1084 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1085 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 1086 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, |
| 1087 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, |
| 1088 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 1089 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 1090 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1091 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1092 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1093 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1094 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 1095 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, |
| 1096 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, |
| 1097 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 1098 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 1099 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1100 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 1101 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1102 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1103 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1104 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
| 1105 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, |
| 1106 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, |
| 1107 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 1108 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 1109 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1110 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1111 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1112 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1113 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1114 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1115 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1116 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1117 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1118 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1119 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1120 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1121 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1122 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1123 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1124 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1125 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1126 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
Jean Delvare | 84d2a31 | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1127 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1128 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1129 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1130 | &dev_attr_pwm_use_point2_pwm_at_crit.attr, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1131 | NULL, |
| 1132 | }; |
| 1133 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1134 | static struct attribute *fan4_attrs[] = { |
| 1135 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 1136 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1137 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 1138 | NULL |
| 1139 | }; |
| 1140 | |
| 1141 | static struct attribute *pwm2_attrs[] = { |
| 1142 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1143 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1144 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1145 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
| 1146 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1147 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
| 1148 | NULL |
| 1149 | }; |
| 1150 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1151 | static struct attribute *in0_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1152 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 1153 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 1154 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 1155 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1156 | NULL |
| 1157 | }; |
| 1158 | |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1159 | static struct attribute *in3_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1160 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 1161 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 1162 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 1163 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1164 | NULL |
| 1165 | }; |
| 1166 | |
| 1167 | static struct attribute *in4_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1168 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 1169 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 1170 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 1171 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1172 | NULL |
| 1173 | }; |
| 1174 | |
| 1175 | static struct attribute *in5_attrs[] = { |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1176 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 1177 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 1178 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 1179 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 1180 | NULL |
| 1181 | }; |
| 1182 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1183 | static struct attribute *vid_attrs[] = { |
| 1184 | &dev_attr_cpu0_vid.attr, |
| 1185 | &dev_attr_vrm.attr, |
| 1186 | NULL |
| 1187 | }; |
| 1188 | |
Jean Delvare | 54ecb9e | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1189 | static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1190 | static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs }; |
| 1191 | static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs }; |
| 1192 | static struct attribute_group in0_attr_group = { .attrs = in0_attrs }; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1193 | static struct attribute_group in3_attr_group = { .attrs = in3_attrs }; |
| 1194 | static struct attribute_group in4_attr_group = { .attrs = in4_attrs }; |
| 1195 | static struct attribute_group in5_attr_group = { .attrs = in5_attrs }; |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1196 | static struct attribute_group vid_attr_group = { .attrs = vid_attrs }; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1197 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1198 | static int adt7475_detect(struct i2c_client *client, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1199 | struct i2c_board_info *info) |
| 1200 | { |
| 1201 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1202 | int vendid, devid, devid2; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1203 | const char *name; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1204 | |
| 1205 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1206 | return -ENODEV; |
| 1207 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1208 | vendid = adt7475_read(REG_VENDID); |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1209 | devid2 = adt7475_read(REG_DEVID2); |
| 1210 | if (vendid != 0x41 || /* Analog Devices */ |
| 1211 | (devid2 & 0xf8) != 0x68) |
| 1212 | return -ENODEV; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1213 | |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1214 | devid = adt7475_read(REG_DEVID); |
| 1215 | if (devid == 0x73) |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1216 | name = "adt7473"; |
Jean Delvare | d656b6f | 2009-12-09 20:36:04 +0100 | [diff] [blame] | 1217 | else if (devid == 0x75 && client->addr == 0x2e) |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1218 | name = "adt7475"; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1219 | else if (devid == 0x76) |
| 1220 | name = "adt7476"; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1221 | else if ((devid2 & 0xfc) == 0x6c) |
| 1222 | name = "adt7490"; |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1223 | else { |
| 1224 | dev_dbg(&adapter->dev, |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1225 | "Couldn't detect an ADT7473/75/76/90 part at " |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1226 | "0x%02x\n", (unsigned int)client->addr); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 1227 | return -ENODEV; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Jean Delvare | b180d05 | 2009-12-09 20:36:02 +0100 | [diff] [blame] | 1230 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Jean Delvare | 0f14480 | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1235 | static void adt7475_remove_files(struct i2c_client *client, |
| 1236 | struct adt7475_data *data) |
| 1237 | { |
| 1238 | sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1239 | if (data->has_fan4) |
| 1240 | sysfs_remove_group(&client->dev.kobj, &fan4_attr_group); |
| 1241 | if (data->has_pwm2) |
| 1242 | sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group); |
| 1243 | if (data->has_voltage & (1 << 0)) |
| 1244 | sysfs_remove_group(&client->dev.kobj, &in0_attr_group); |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1245 | if (data->has_voltage & (1 << 3)) |
| 1246 | sysfs_remove_group(&client->dev.kobj, &in3_attr_group); |
| 1247 | if (data->has_voltage & (1 << 4)) |
| 1248 | sysfs_remove_group(&client->dev.kobj, &in4_attr_group); |
| 1249 | if (data->has_voltage & (1 << 5)) |
| 1250 | sysfs_remove_group(&client->dev.kobj, &in5_attr_group); |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1251 | if (data->has_vid) |
| 1252 | sysfs_remove_group(&client->dev.kobj, &vid_attr_group); |
Jean Delvare | 0f14480 | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1255 | static int adt7475_probe(struct i2c_client *client, |
| 1256 | const struct i2c_device_id *id) |
| 1257 | { |
Frans Meulenbroeks | 99b8c83 | 2012-01-08 19:34:08 +0100 | [diff] [blame] | 1258 | static const char * const names[] = { |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1259 | [adt7473] = "ADT7473", |
| 1260 | [adt7475] = "ADT7475", |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1261 | [adt7476] = "ADT7476", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1262 | [adt7490] = "ADT7490", |
| 1263 | }; |
| 1264 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1265 | struct adt7475_data *data; |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1266 | int i, ret = 0, revision; |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1267 | u8 config2, config3; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1268 | |
Guenter Roeck | e3ecb2e | 2012-06-02 09:58:01 -0700 | [diff] [blame] | 1269 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1270 | if (data == NULL) |
| 1271 | return -ENOMEM; |
| 1272 | |
| 1273 | mutex_init(&data->lock); |
| 1274 | i2c_set_clientdata(client, data); |
| 1275 | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1276 | /* Initialize device-specific values */ |
| 1277 | switch (id->driver_data) { |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1278 | case adt7476: |
| 1279 | data->has_voltage = 0x0e; /* in1 to in3 */ |
| 1280 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1281 | break; |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1282 | case adt7490: |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1283 | data->has_voltage = 0x3e; /* in1 to in5 */ |
| 1284 | revision = adt7475_read(REG_DEVID2) & 0x03; |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1285 | if (revision == 0x03) |
| 1286 | revision += adt7475_read(REG_DEVREV2); |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1287 | break; |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1288 | default: |
| 1289 | data->has_voltage = 0x06; /* in1, in2 */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1290 | revision = adt7475_read(REG_DEVID2) & 0x07; |
| 1291 | } |
| 1292 | |
| 1293 | config3 = adt7475_read(REG_CONFIG3); |
| 1294 | /* Pin PWM2 may alternatively be used for ALERT output */ |
| 1295 | if (!(config3 & CONFIG3_SMBALERT)) |
| 1296 | data->has_pwm2 = 1; |
| 1297 | /* Meaning of this bit is inverted for the ADT7473-1 */ |
| 1298 | if (id->driver_data == adt7473 && revision >= 1) |
| 1299 | data->has_pwm2 = !data->has_pwm2; |
| 1300 | |
| 1301 | data->config4 = adt7475_read(REG_CONFIG4); |
| 1302 | /* Pin TACH4 may alternatively be used for THERM */ |
| 1303 | if ((data->config4 & CONFIG4_PINFUNC) == 0x0) |
| 1304 | data->has_fan4 = 1; |
| 1305 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1306 | /* |
| 1307 | * THERM configuration is more complex on the ADT7476 and ADT7490, |
| 1308 | * because 2 different pins (TACH4 and +2.5 Vin) can be used for |
| 1309 | * this function |
| 1310 | */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1311 | if (id->driver_data == adt7490) { |
| 1312 | if ((data->config4 & CONFIG4_PINFUNC) == 0x1 && |
| 1313 | !(config3 & CONFIG3_THERM)) |
| 1314 | data->has_fan4 = 1; |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1315 | } |
| 1316 | if (id->driver_data == adt7476 || id->driver_data == adt7490) { |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1317 | if (!(config3 & CONFIG3_THERM) || |
| 1318 | (data->config4 & CONFIG4_PINFUNC) == 0x1) |
| 1319 | data->has_voltage |= (1 << 0); /* in0 */ |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1322 | /* |
| 1323 | * On the ADT7476, the +12V input pin may instead be used as VID5, |
| 1324 | * and VID pins may alternatively be used as GPIO |
| 1325 | */ |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1326 | if (id->driver_data == adt7476) { |
| 1327 | u8 vid = adt7475_read(REG_VID); |
| 1328 | if (!(vid & VID_VIDSEL)) |
| 1329 | data->has_voltage |= (1 << 4); /* in4 */ |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1330 | |
| 1331 | data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO); |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1334 | /* Voltage attenuators can be bypassed, globally or individually */ |
| 1335 | config2 = adt7475_read(REG_CONFIG2); |
| 1336 | if (config2 & CONFIG2_ATTN) { |
| 1337 | data->bypass_attn = (0x3 << 3) | 0x3; |
| 1338 | } else { |
| 1339 | data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) | |
| 1340 | ((data->config4 & CONFIG4_ATTN_IN43) >> 3); |
| 1341 | } |
| 1342 | data->bypass_attn &= data->has_voltage; |
| 1343 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1344 | /* |
| 1345 | * Call adt7475_read_pwm for all pwm's as this will reprogram any |
| 1346 | * pwm's which are disabled to manual mode with 0% duty cycle |
| 1347 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1348 | for (i = 0; i < ADT7475_PWM_COUNT; i++) |
| 1349 | adt7475_read_pwm(client, i); |
| 1350 | |
| 1351 | ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); |
| 1352 | if (ret) |
Guenter Roeck | e3ecb2e | 2012-06-02 09:58:01 -0700 | [diff] [blame] | 1353 | return ret; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1354 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1355 | /* Features that can be disabled individually */ |
| 1356 | if (data->has_fan4) { |
| 1357 | ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group); |
| 1358 | if (ret) |
| 1359 | goto eremove; |
| 1360 | } |
| 1361 | if (data->has_pwm2) { |
| 1362 | ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group); |
| 1363 | if (ret) |
| 1364 | goto eremove; |
| 1365 | } |
| 1366 | if (data->has_voltage & (1 << 0)) { |
| 1367 | ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group); |
| 1368 | if (ret) |
| 1369 | goto eremove; |
| 1370 | } |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1371 | if (data->has_voltage & (1 << 3)) { |
| 1372 | ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group); |
| 1373 | if (ret) |
| 1374 | goto eremove; |
| 1375 | } |
| 1376 | if (data->has_voltage & (1 << 4)) { |
| 1377 | ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group); |
| 1378 | if (ret) |
| 1379 | goto eremove; |
| 1380 | } |
| 1381 | if (data->has_voltage & (1 << 5)) { |
| 1382 | ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group); |
| 1383 | if (ret) |
| 1384 | goto eremove; |
| 1385 | } |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1386 | if (data->has_vid) { |
| 1387 | data->vrm = vid_which_vrm(); |
| 1388 | ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group); |
| 1389 | if (ret) |
| 1390 | goto eremove; |
| 1391 | } |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1392 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1393 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 1394 | if (IS_ERR(data->hwmon_dev)) { |
| 1395 | ret = PTR_ERR(data->hwmon_dev); |
| 1396 | goto eremove; |
| 1397 | } |
| 1398 | |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1399 | dev_info(&client->dev, "%s device, revision %d\n", |
| 1400 | names[id->driver_data], revision); |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1401 | if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2) |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1402 | dev_info(&client->dev, "Optional features:%s%s%s%s%s\n", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1403 | (data->has_voltage & (1 << 0)) ? " in0" : "", |
Jean Delvare | d8d2ee0 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1404 | (data->has_voltage & (1 << 4)) ? " in4" : "", |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1405 | data->has_fan4 ? " fan4" : "", |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1406 | data->has_pwm2 ? " pwm2" : "", |
| 1407 | data->has_vid ? " vid" : ""); |
Jean Delvare | ebfaf1f | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1408 | if (data->bypass_attn) |
| 1409 | dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n", |
| 1410 | (data->bypass_attn & (1 << 0)) ? " in0" : "", |
| 1411 | (data->bypass_attn & (1 << 1)) ? " in1" : "", |
| 1412 | (data->bypass_attn & (1 << 3)) ? " in3" : "", |
| 1413 | (data->bypass_attn & (1 << 4)) ? " in4" : ""); |
Jean Delvare | d07ca4a | 2009-12-09 20:36:07 +0100 | [diff] [blame] | 1414 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1415 | return 0; |
| 1416 | |
| 1417 | eremove: |
Jean Delvare | 0f14480 | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1418 | adt7475_remove_files(client, data); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1419 | return ret; |
| 1420 | } |
| 1421 | |
| 1422 | static int adt7475_remove(struct i2c_client *client) |
| 1423 | { |
| 1424 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1425 | |
| 1426 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 0f14480 | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1427 | adt7475_remove_files(client, data); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | static struct i2c_driver adt7475_driver = { |
| 1433 | .class = I2C_CLASS_HWMON, |
| 1434 | .driver = { |
| 1435 | .name = "adt7475", |
| 1436 | }, |
| 1437 | .probe = adt7475_probe, |
| 1438 | .remove = adt7475_remove, |
| 1439 | .id_table = adt7475_id, |
| 1440 | .detect = adt7475_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 1441 | .address_list = normal_i2c, |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1442 | }; |
| 1443 | |
| 1444 | static void adt7475_read_hystersis(struct i2c_client *client) |
| 1445 | { |
| 1446 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1447 | |
| 1448 | data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); |
| 1449 | data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; |
| 1450 | data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); |
| 1451 | } |
| 1452 | |
| 1453 | static void adt7475_read_pwm(struct i2c_client *client, int index) |
| 1454 | { |
| 1455 | struct adt7475_data *data = i2c_get_clientdata(client); |
| 1456 | unsigned int v; |
| 1457 | |
| 1458 | data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); |
| 1459 | |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1460 | /* |
| 1461 | * Figure out the internal value for pwmctrl and pwmchan |
| 1462 | * based on the current settings |
| 1463 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1464 | v = (data->pwm[CONTROL][index] >> 5) & 7; |
| 1465 | |
| 1466 | if (v == 3) |
| 1467 | data->pwmctl[index] = 0; |
| 1468 | else if (v == 7) |
| 1469 | data->pwmctl[index] = 1; |
| 1470 | else if (v == 4) { |
Guenter Roeck | 9ed5bc2 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1471 | /* |
| 1472 | * The fan is disabled - we don't want to |
| 1473 | * support that, so change to manual mode and |
| 1474 | * set the duty cycle to 0 instead |
| 1475 | */ |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1476 | data->pwm[INPUT][index] = 0; |
| 1477 | data->pwm[CONTROL][index] &= ~0xE0; |
| 1478 | data->pwm[CONTROL][index] |= (7 << 5); |
| 1479 | |
| 1480 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1481 | data->pwm[INPUT][index]); |
| 1482 | |
| 1483 | i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), |
| 1484 | data->pwm[CONTROL][index]); |
| 1485 | |
| 1486 | data->pwmctl[index] = 1; |
| 1487 | } else { |
| 1488 | data->pwmctl[index] = 2; |
| 1489 | |
| 1490 | switch (v) { |
| 1491 | case 0: |
| 1492 | data->pwmchan[index] = 1; |
| 1493 | break; |
| 1494 | case 1: |
| 1495 | data->pwmchan[index] = 2; |
| 1496 | break; |
| 1497 | case 2: |
| 1498 | data->pwmchan[index] = 4; |
| 1499 | break; |
| 1500 | case 5: |
| 1501 | data->pwmchan[index] = 6; |
| 1502 | break; |
| 1503 | case 6: |
| 1504 | data->pwmchan[index] = 7; |
| 1505 | break; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | static struct adt7475_data *adt7475_update_device(struct device *dev) |
| 1511 | { |
| 1512 | struct i2c_client *client = to_i2c_client(dev); |
| 1513 | struct adt7475_data *data = i2c_get_clientdata(client); |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1514 | u16 ext; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1515 | int i; |
| 1516 | |
| 1517 | mutex_lock(&data->lock); |
| 1518 | |
| 1519 | /* Measurement values update every 2 seconds */ |
| 1520 | if (time_after(jiffies, data->measure_updated + HZ * 2) || |
| 1521 | !data->valid) { |
| 1522 | data->alarms = adt7475_read(REG_STATUS2) << 8; |
| 1523 | data->alarms |= adt7475_read(REG_STATUS1); |
| 1524 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1525 | ext = (adt7475_read(REG_EXTEND2) << 8) | |
| 1526 | adt7475_read(REG_EXTEND1); |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1527 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
| 1528 | if (!(data->has_voltage & (1 << i))) |
| 1529 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1530 | data->voltage[INPUT][i] = |
| 1531 | (adt7475_read(VOLTAGE_REG(i)) << 2) | |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1532 | ((ext >> (i * 2)) & 3); |
| 1533 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1534 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1535 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) |
| 1536 | data->temp[INPUT][i] = |
| 1537 | (adt7475_read(TEMP_REG(i)) << 2) | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1538 | ((ext >> ((i + 5) * 2)) & 3); |
| 1539 | |
| 1540 | if (data->has_voltage & (1 << 5)) { |
| 1541 | data->alarms |= adt7475_read(REG_STATUS4) << 24; |
| 1542 | ext = adt7475_read(REG_EXTEND3); |
| 1543 | data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 | |
| 1544 | ((ext >> 4) & 3); |
| 1545 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1546 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1547 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1548 | if (i == 3 && !data->has_fan4) |
| 1549 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1550 | data->tach[INPUT][i] = |
| 1551 | adt7475_read_word(client, TACH_REG(i)); |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1552 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1553 | |
| 1554 | /* Updated by hw when in auto mode */ |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1555 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
| 1556 | if (i == 1 && !data->has_pwm2) |
| 1557 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1558 | data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1559 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1560 | |
Jean Delvare | 54fe467 | 2009-12-09 20:36:08 +0100 | [diff] [blame] | 1561 | if (data->has_vid) |
| 1562 | data->vid = adt7475_read(REG_VID) & 0x3f; |
| 1563 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1564 | data->measure_updated = jiffies; |
| 1565 | } |
| 1566 | |
| 1567 | /* Limits and settings, should never change update every 60 seconds */ |
Jean Delvare | 56e35ee | 2009-11-16 12:45:40 +0100 | [diff] [blame] | 1568 | if (time_after(jiffies, data->limits_updated + HZ * 60) || |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1569 | !data->valid) { |
Jean Delvare | f99318b | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1570 | data->config4 = adt7475_read(REG_CONFIG4); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1571 | data->config5 = adt7475_read(REG_CONFIG5); |
| 1572 | |
| 1573 | for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { |
Jean Delvare | cffb9dd | 2009-12-09 20:36:03 +0100 | [diff] [blame] | 1574 | if (!(data->has_voltage & (1 << i))) |
| 1575 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1576 | /* Adjust values so they match the input precision */ |
| 1577 | data->voltage[MIN][i] = |
| 1578 | adt7475_read(VOLTAGE_MIN_REG(i)) << 2; |
| 1579 | data->voltage[MAX][i] = |
| 1580 | adt7475_read(VOLTAGE_MAX_REG(i)) << 2; |
| 1581 | } |
| 1582 | |
Jean Delvare | 3d84998 | 2009-12-09 20:36:05 +0100 | [diff] [blame] | 1583 | if (data->has_voltage & (1 << 5)) { |
| 1584 | data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2; |
| 1585 | data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2; |
| 1586 | } |
| 1587 | |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1588 | for (i = 0; i < ADT7475_TEMP_COUNT; i++) { |
| 1589 | /* Adjust values so they match the input precision */ |
| 1590 | data->temp[MIN][i] = |
| 1591 | adt7475_read(TEMP_MIN_REG(i)) << 2; |
| 1592 | data->temp[MAX][i] = |
| 1593 | adt7475_read(TEMP_MAX_REG(i)) << 2; |
| 1594 | data->temp[AUTOMIN][i] = |
| 1595 | adt7475_read(TEMP_TMIN_REG(i)) << 2; |
| 1596 | data->temp[THERM][i] = |
| 1597 | adt7475_read(TEMP_THERM_REG(i)) << 2; |
| 1598 | data->temp[OFFSET][i] = |
| 1599 | adt7475_read(TEMP_OFFSET_REG(i)); |
| 1600 | } |
| 1601 | adt7475_read_hystersis(client); |
| 1602 | |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1603 | for (i = 0; i < ADT7475_TACH_COUNT; i++) { |
| 1604 | if (i == 3 && !data->has_fan4) |
| 1605 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1606 | data->tach[MIN][i] = |
| 1607 | adt7475_read_word(client, TACH_MIN_REG(i)); |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1608 | } |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1609 | |
| 1610 | for (i = 0; i < ADT7475_PWM_COUNT; i++) { |
Jean Delvare | 378933c | 2009-12-09 20:36:06 +0100 | [diff] [blame] | 1611 | if (i == 1 && !data->has_pwm2) |
| 1612 | continue; |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1613 | data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); |
| 1614 | data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); |
| 1615 | /* Set the channel and control information */ |
| 1616 | adt7475_read_pwm(client, i); |
| 1617 | } |
| 1618 | |
| 1619 | data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); |
| 1620 | data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); |
| 1621 | data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); |
| 1622 | |
| 1623 | data->limits_updated = jiffies; |
| 1624 | data->valid = 1; |
| 1625 | } |
| 1626 | |
| 1627 | mutex_unlock(&data->lock); |
| 1628 | |
| 1629 | return data; |
| 1630 | } |
| 1631 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 1632 | module_i2c_driver(adt7475_driver); |
Jordan Crouse | 1c301fc | 2009-01-15 22:27:47 +0100 | [diff] [blame] | 1633 | |
| 1634 | MODULE_AUTHOR("Advanced Micro Devices, Inc"); |
| 1635 | MODULE_DESCRIPTION("adt7475 driver"); |
| 1636 | MODULE_LICENSE("GPL"); |