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