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