blob: 9d5b019651f2d715189caaf7ca447e5c9e0321c5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jordan Crouse1c301fc2009-01-15 22:27:47 +01002/*
3 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
4 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
5 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01007 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
Jean Delvare3d849982009-12-09 20:36:05 +01008 *
Jordan Crouse1c301fc2009-01-15 22:27:47 +01009 * Derived from the lm83 driver by Jean Delvare
Jordan Crouse1c301fc2009-01-15 22:27:47 +010010 */
11
12#include <linux/module.h>
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -030013#include <linux/of_device.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010014#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/i2c.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
Jean Delvare54fe4672009-12-09 20:36:08 +010019#include <linux/hwmon-vid.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010020#include <linux/err.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020021#include <linux/jiffies.h>
Logan Shaw2ecff392020-02-27 21:46:41 +130022#include <linux/of.h>
Chris Packhame4651642017-05-11 15:45:21 +120023#include <linux/util_macros.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010024
25/* Indexes for the sysfs hooks */
26
27#define INPUT 0
28#define MIN 1
29#define MAX 2
30#define CONTROL 3
31#define OFFSET 3
32#define AUTOMIN 4
33#define THERM 5
34#define HYSTERSIS 6
35
Guenter Roeck9ed5bc22012-01-19 11:02:15 -080036/*
37 * These are unique identifiers for the sysfs functions - unlike the
38 * numbers above, these are not also indexes into an array
39 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010040
41#define ALARM 9
42#define FAULT 10
43
44/* 7475 Common Registers */
45
Jean Delvared07ca4a2009-12-09 20:36:07 +010046#define REG_DEVREV2 0x12 /* ADT7490 only */
47
Jean Delvare3d849982009-12-09 20:36:05 +010048#define REG_VTT 0x1E /* ADT7490 only */
49#define REG_EXTEND3 0x1F /* ADT7490 only */
50
Jean Delvarecffb9dd2009-12-09 20:36:03 +010051#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010052#define REG_TEMP_BASE 0x25
53#define REG_TACH_BASE 0x28
54#define REG_PWM_BASE 0x30
55#define REG_PWM_MAX_BASE 0x38
56
57#define REG_DEVID 0x3D
58#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010059#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010060
Chris Packham4abdf382017-04-22 07:08:09 +120061#define REG_CONFIG1 0x40
62
Jordan Crouse1c301fc2009-01-15 22:27:47 +010063#define REG_STATUS1 0x41
64#define REG_STATUS2 0x42
65
Jean Delvared8d2ee02009-12-09 20:36:08 +010066#define REG_VID 0x43 /* ADT7476 only */
67
Jean Delvarecffb9dd2009-12-09 20:36:03 +010068#define REG_VOLTAGE_MIN_BASE 0x44
69#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010070
71#define REG_TEMP_MIN_BASE 0x4E
72#define REG_TEMP_MAX_BASE 0x4F
73
74#define REG_TACH_MIN_BASE 0x54
75
76#define REG_PWM_CONFIG_BASE 0x5C
77
78#define REG_TEMP_TRANGE_BASE 0x5F
79
Chris Packham1d58f5e2017-05-15 13:30:27 +120080#define REG_ENHANCE_ACOUSTICS1 0x62
81#define REG_ENHANCE_ACOUSTICS2 0x63
82
Jordan Crouse1c301fc2009-01-15 22:27:47 +010083#define REG_PWM_MIN_BASE 0x64
84
85#define REG_TEMP_TMIN_BASE 0x67
86#define REG_TEMP_THERM_BASE 0x6A
87
88#define REG_REMOTE1_HYSTERSIS 0x6D
89#define REG_REMOTE2_HYSTERSIS 0x6E
90
91#define REG_TEMP_OFFSET_BASE 0x70
92
Jean Delvareebfaf1f2009-12-09 20:36:07 +010093#define REG_CONFIG2 0x73
94
Jordan Crouse1c301fc2009-01-15 22:27:47 +010095#define REG_EXTEND1 0x76
96#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010097
98#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010099#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +0100100#define REG_CONFIG4 0x7D
101
Jean Delvare3d849982009-12-09 20:36:05 +0100102#define REG_STATUS4 0x81 /* ADT7490 only */
103
104#define REG_VTT_MIN 0x84 /* ADT7490 only */
105#define REG_VTT_MAX 0x86 /* ADT7490 only */
106
Jean Delvared8d2ee02009-12-09 20:36:08 +0100107#define VID_VIDSEL 0x80 /* ADT7476 only */
108
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100109#define CONFIG2_ATTN 0x20
110
Jean Delvare378933c2009-12-09 20:36:06 +0100111#define CONFIG3_SMBALERT 0x01
112#define CONFIG3_THERM 0x02
113
114#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100115#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100116#define CONFIG4_ATTN_IN10 0x30
117#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100118
119#define CONFIG5_TWOSCOMP 0x01
120#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100121#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100122
123/* ADT7475 Settings */
124
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100125#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100126#define ADT7475_TEMP_COUNT 3
127#define ADT7475_TACH_COUNT 4
128#define ADT7475_PWM_COUNT 3
129
130/* Macro to read the registers */
131
132#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
133
134/* Macros to easily index the registers */
135
136#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
137#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
138
139#define PWM_REG(idx) (REG_PWM_BASE + (idx))
140#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
141#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
142#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
143
144#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
145#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
146#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
147
148#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
149#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
150#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
151#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
152#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
153#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
154#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
155
Jean Delvare918ee912010-10-28 20:31:50 +0200156static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100157
Jean Delvaree5e9f442009-12-14 21:17:27 +0100158enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100159
160static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100161 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100162 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100163 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100164 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100165 { }
166};
167MODULE_DEVICE_TABLE(i2c, adt7475_id);
168
Guenter Roeckfe339db2019-04-04 06:33:42 -0700169static const struct of_device_id __maybe_unused adt7475_of_match[] = {
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300170 {
171 .compatible = "adi,adt7473",
172 .data = (void *)adt7473
173 },
174 {
175 .compatible = "adi,adt7475",
176 .data = (void *)adt7475
177 },
178 {
179 .compatible = "adi,adt7476",
180 .data = (void *)adt7476
181 },
182 {
183 .compatible = "adi,adt7490",
184 .data = (void *)adt7490
185 },
186 { },
187};
188MODULE_DEVICE_TABLE(of, adt7475_of_match);
189
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100190struct adt7475_data {
Grant McEwanc64fce72019-07-22 10:55:30 +1200191 struct i2c_client *client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100192 struct mutex lock;
193
194 unsigned long measure_updated;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +0900195 bool valid;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100196
Logan Shaw2ecff392020-02-27 21:46:41 +1300197 u8 config2;
Jean Delvaref99318b2009-12-09 20:36:03 +0100198 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100199 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100200 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100201 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100202 u8 has_pwm2:1;
203 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100204 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100205 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100206 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100207 u16 temp[7][3];
208 u16 tach[2][4];
209 u8 pwm[4][3];
210 u8 range[3];
211 u8 pwmctl[3];
212 u8 pwmchan[3];
Chris Packham1d58f5e2017-05-15 13:30:27 +1200213 u8 enh_acoustics[2];
Jean Delvare54fe4672009-12-09 20:36:08 +0100214
215 u8 vid;
216 u8 vrm;
Grant McEwanc64fce72019-07-22 10:55:30 +1200217 const struct attribute_group *groups[9];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100218};
219
220static struct i2c_driver adt7475_driver;
221static struct adt7475_data *adt7475_update_device(struct device *dev);
222static void adt7475_read_hystersis(struct i2c_client *client);
223static void adt7475_read_pwm(struct i2c_client *client, int index);
224
225/* Given a temp value, convert it to register value */
226
227static inline u16 temp2reg(struct adt7475_data *data, long val)
228{
229 u16 ret;
230
231 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800232 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100233 ret = (val + 64500) / 1000;
234 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800235 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100236 if (val < -500)
237 ret = (256500 + val) / 1000;
238 else
239 ret = (val + 500) / 1000;
240 }
241
242 return ret << 2;
243}
244
245/* Given a register value, convert it to a real temp value */
246
247static inline int reg2temp(struct adt7475_data *data, u16 reg)
248{
249 if (data->config5 & CONFIG5_TWOSCOMP) {
250 if (reg >= 512)
251 return (reg - 1024) * 250;
252 else
253 return reg * 250;
254 } else
255 return (reg - 256) * 250;
256}
257
258static inline int tach2rpm(u16 tach)
259{
260 if (tach == 0 || tach == 0xFFFF)
261 return 0;
262
263 return (90000 * 60) / tach;
264}
265
266static inline u16 rpm2tach(unsigned long rpm)
267{
268 if (rpm == 0)
269 return 0;
270
Guenter Roeck2a844c12013-01-09 08:09:34 -0800271 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100272}
273
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100274/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
275static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
276 { 45, 94 }, /* +2.5V */
277 { 175, 525 }, /* Vccp */
278 { 68, 71 }, /* Vcc */
279 { 93, 47 }, /* +5V */
280 { 120, 20 }, /* +12V */
281 { 45, 45 }, /* Vtt */
282};
283
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100284static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100285{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100286 const int *r = adt7473_in_scaling[channel];
287
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100288 if (bypass_attn & (1 << channel))
289 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100290 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100291}
292
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100293static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100294{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100295 const int *r = adt7473_in_scaling[channel];
296 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100297
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100298 if (bypass_attn & (1 << channel))
Luuk Paulussencf3ca182019-12-06 12:16:59 +1300299 reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100300 else
Luuk Paulussencf3ca182019-12-06 12:16:59 +1300301 reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
302 (r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800303 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100304}
305
Dan Carpenterf196dec2018-08-14 13:07:47 +0300306static int adt7475_read_word(struct i2c_client *client, int reg)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100307{
Dan Carpenterf196dec2018-08-14 13:07:47 +0300308 int val1, val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100309
Dan Carpenterf196dec2018-08-14 13:07:47 +0300310 val1 = i2c_smbus_read_byte_data(client, reg);
311 if (val1 < 0)
312 return val1;
313 val2 = i2c_smbus_read_byte_data(client, reg + 1);
314 if (val2 < 0)
315 return val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100316
Dan Carpenterf196dec2018-08-14 13:07:47 +0300317 return val1 | (val2 << 8);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100318}
319
320static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
321{
322 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
323 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
324}
325
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800326static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100327 char *buf)
328{
329 struct adt7475_data *data = adt7475_update_device(dev);
330 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
331 unsigned short val;
332
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900333 if (IS_ERR(data))
334 return PTR_ERR(data);
335
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100336 switch (sattr->nr) {
337 case ALARM:
338 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100339 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100340 default:
341 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100342 return sprintf(buf, "%d\n",
343 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100344 }
345}
346
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800347static ssize_t voltage_store(struct device *dev,
348 struct device_attribute *attr, const char *buf,
349 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100350{
351
352 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200353 struct adt7475_data *data = dev_get_drvdata(dev);
354 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100355 unsigned char reg;
356 long val;
357
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100358 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100359 return -EINVAL;
360
361 mutex_lock(&data->lock);
362
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100363 data->voltage[sattr->nr][sattr->index] =
364 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100365
Jean Delvare3d849982009-12-09 20:36:05 +0100366 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
367 if (sattr->nr == MIN)
368 reg = VOLTAGE_MIN_REG(sattr->index);
369 else
370 reg = VOLTAGE_MAX_REG(sattr->index);
371 } else {
372 if (sattr->nr == MIN)
373 reg = REG_VTT_MIN;
374 else
375 reg = REG_VTT_MAX;
376 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100377
378 i2c_smbus_write_byte_data(client, reg,
379 data->voltage[sattr->nr][sattr->index] >> 2);
380 mutex_unlock(&data->lock);
381
382 return count;
383}
384
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800385static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100386 char *buf)
387{
388 struct adt7475_data *data = adt7475_update_device(dev);
389 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
390 int out;
391
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900392 if (IS_ERR(data))
393 return PTR_ERR(data);
394
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100395 switch (sattr->nr) {
396 case HYSTERSIS:
397 mutex_lock(&data->lock);
398 out = data->temp[sattr->nr][sattr->index];
399 if (sattr->index != 1)
400 out = (out >> 4) & 0xF;
401 else
402 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800403 /*
404 * Show the value as an absolute number tied to
405 * THERM
406 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100407 out = reg2temp(data, data->temp[THERM][sattr->index]) -
408 out * 1000;
409 mutex_unlock(&data->lock);
410 break;
411
412 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800413 /*
414 * Offset is always 2's complement, regardless of the
415 * setting in CONFIG5
416 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100417 mutex_lock(&data->lock);
418 out = (s8)data->temp[sattr->nr][sattr->index];
419 if (data->config5 & CONFIG5_TEMPOFFSET)
420 out *= 1000;
421 else
422 out *= 500;
423 mutex_unlock(&data->lock);
424 break;
425
426 case ALARM:
427 out = (data->alarms >> (sattr->index + 4)) & 1;
428 break;
429
430 case FAULT:
431 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100432 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100433 break;
434
435 default:
436 /* All other temp values are in the configured format */
437 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
438 }
439
440 return sprintf(buf, "%d\n", out);
441}
442
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800443static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
444 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100445{
446 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200447 struct adt7475_data *data = dev_get_drvdata(dev);
448 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100449 unsigned char reg = 0;
450 u8 out;
451 int temp;
452 long val;
453
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100454 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100455 return -EINVAL;
456
457 mutex_lock(&data->lock);
458
459 /* We need the config register in all cases for temp <-> reg conv. */
460 data->config5 = adt7475_read(REG_CONFIG5);
461
462 switch (sattr->nr) {
463 case OFFSET:
464 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800465 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100466 out = data->temp[OFFSET][sattr->index] = val / 1000;
467 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800468 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100469 out = data->temp[OFFSET][sattr->index] = val / 500;
470 }
471 break;
472
473 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800474 /*
475 * The value will be given as an absolute value, turn it
476 * into an offset based on THERM
477 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100478
479 /* Read fresh THERM and HYSTERSIS values from the chip */
480 data->temp[THERM][sattr->index] =
481 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
482 adt7475_read_hystersis(client);
483
484 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800485 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100486 val = (temp - val) / 1000;
487
488 if (sattr->index != 1) {
489 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
490 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
491 } else {
492 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
493 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
494 }
495
496 out = data->temp[HYSTERSIS][sattr->index];
497 break;
498
499 default:
500 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
501
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800502 /*
503 * We maintain an extra 2 digits of precision for simplicity
504 * - shift those back off before writing the value
505 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100506 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
507 }
508
509 switch (sattr->nr) {
510 case MIN:
511 reg = TEMP_MIN_REG(sattr->index);
512 break;
513 case MAX:
514 reg = TEMP_MAX_REG(sattr->index);
515 break;
516 case OFFSET:
517 reg = TEMP_OFFSET_REG(sattr->index);
518 break;
519 case AUTOMIN:
520 reg = TEMP_TMIN_REG(sattr->index);
521 break;
522 case THERM:
523 reg = TEMP_THERM_REG(sattr->index);
524 break;
525 case HYSTERSIS:
526 if (sattr->index != 2)
527 reg = REG_REMOTE1_HYSTERSIS;
528 else
529 reg = REG_REMOTE2_HYSTERSIS;
530
531 break;
532 }
533
534 i2c_smbus_write_byte_data(client, reg, out);
535
536 mutex_unlock(&data->lock);
537 return count;
538}
539
Chris Packham8f05bcc2017-05-15 13:30:28 +1200540/* Assuming CONFIG6[SLOW] is 0 */
541static const int ad7475_st_map[] = {
542 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
543};
544
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800545static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
Chris Packham8f05bcc2017-05-15 13:30:28 +1200547{
548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200549 struct adt7475_data *data = dev_get_drvdata(dev);
Chris Packham8f05bcc2017-05-15 13:30:28 +1200550 long val;
551
552 switch (sattr->index) {
553 case 0:
554 val = data->enh_acoustics[0] & 0xf;
555 break;
556 case 1:
557 val = (data->enh_acoustics[1] >> 4) & 0xf;
558 break;
559 case 2:
560 default:
561 val = data->enh_acoustics[1] & 0xf;
562 break;
563 }
564
565 if (val & 0x8)
566 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
567 else
568 return sprintf(buf, "0\n");
569}
570
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800571static ssize_t temp_st_store(struct device *dev,
572 struct device_attribute *attr, const char *buf,
573 size_t count)
Chris Packham8f05bcc2017-05-15 13:30:28 +1200574{
575 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200576 struct adt7475_data *data = dev_get_drvdata(dev);
577 struct i2c_client *client = data->client;
Chris Packham8f05bcc2017-05-15 13:30:28 +1200578 unsigned char reg;
579 int shift, idx;
580 ulong val;
581
582 if (kstrtoul(buf, 10, &val))
583 return -EINVAL;
584
585 switch (sattr->index) {
586 case 0:
587 reg = REG_ENHANCE_ACOUSTICS1;
588 shift = 0;
589 idx = 0;
590 break;
591 case 1:
592 reg = REG_ENHANCE_ACOUSTICS2;
593 shift = 0;
594 idx = 1;
595 break;
596 case 2:
597 default:
598 reg = REG_ENHANCE_ACOUSTICS2;
599 shift = 4;
600 idx = 1;
601 break;
602 }
603
604 if (val > 0) {
605 val = find_closest_descending(val, ad7475_st_map,
606 ARRAY_SIZE(ad7475_st_map));
607 val |= 0x8;
608 }
609
610 mutex_lock(&data->lock);
611
612 data->enh_acoustics[idx] &= ~(0xf << shift);
613 data->enh_acoustics[idx] |= (val << shift);
614
615 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
616
617 mutex_unlock(&data->lock);
618
619 return count;
620}
621
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800622/*
623 * Table of autorange values - the user will write the value in millidegrees,
624 * and we'll convert it
625 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100626static const int autorange_table[] = {
627 2000, 2500, 3330, 4000, 5000, 6670, 8000,
628 10000, 13330, 16000, 20000, 26670, 32000, 40000,
629 53330, 80000
630};
631
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800632static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100633 char *buf)
634{
635 struct adt7475_data *data = adt7475_update_device(dev);
636 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
637 int out, val;
638
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900639 if (IS_ERR(data))
640 return PTR_ERR(data);
641
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100642 mutex_lock(&data->lock);
643 out = (data->range[sattr->index] >> 4) & 0x0F;
644 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
645 mutex_unlock(&data->lock);
646
647 return sprintf(buf, "%d\n", val + autorange_table[out]);
648}
649
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800650static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100652{
Grant McEwanc64fce72019-07-22 10:55:30 +1200653 struct adt7475_data *data = dev_get_drvdata(dev);
654 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100655 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
656 int temp;
657 long val;
658
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100659 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100660 return -EINVAL;
661
662 mutex_lock(&data->lock);
663
664 /* Get a fresh copy of the needed registers */
665 data->config5 = adt7475_read(REG_CONFIG5);
666 data->temp[AUTOMIN][sattr->index] =
667 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
668 data->range[sattr->index] =
669 adt7475_read(TEMP_TRANGE_REG(sattr->index));
670
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800671 /*
672 * The user will write an absolute value, so subtract the start point
673 * to figure the range
674 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100675 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800676 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100677 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
678 val -= temp;
679
680 /* Find the nearest table entry to what the user wrote */
Chris Packhame4651642017-05-11 15:45:21 +1200681 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100682
683 data->range[sattr->index] &= ~0xF0;
684 data->range[sattr->index] |= val << 4;
685
686 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
687 data->range[sattr->index]);
688
689 mutex_unlock(&data->lock);
690 return count;
691}
692
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800693static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100694 char *buf)
695{
696 struct adt7475_data *data = adt7475_update_device(dev);
697 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
698 int out;
699
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900700 if (IS_ERR(data))
701 return PTR_ERR(data);
702
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100703 if (sattr->nr == ALARM)
704 out = (data->alarms >> (sattr->index + 10)) & 1;
705 else
706 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
707
708 return sprintf(buf, "%d\n", out);
709}
710
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800711static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100713{
714
715 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200716 struct adt7475_data *data = dev_get_drvdata(dev);
717 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100718 unsigned long val;
719
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100720 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100721 return -EINVAL;
722
723 mutex_lock(&data->lock);
724
725 data->tach[MIN][sattr->index] = rpm2tach(val);
726
727 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
728 data->tach[MIN][sattr->index]);
729
730 mutex_unlock(&data->lock);
731 return count;
732}
733
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800734static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100735 char *buf)
736{
737 struct adt7475_data *data = adt7475_update_device(dev);
738 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
739
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900740 if (IS_ERR(data))
741 return PTR_ERR(data);
742
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100743 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
744}
745
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800746static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100747 char *buf)
748{
749 struct adt7475_data *data = adt7475_update_device(dev);
750 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
751
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900752 if (IS_ERR(data))
753 return PTR_ERR(data);
754
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100755 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
756}
757
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800758static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100759 char *buf)
760{
761 struct adt7475_data *data = adt7475_update_device(dev);
762 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
763
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900764 if (IS_ERR(data))
765 return PTR_ERR(data);
766
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100767 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
768}
769
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800770static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100772{
773
774 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200775 struct adt7475_data *data = dev_get_drvdata(dev);
776 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100777 unsigned char reg = 0;
778 long val;
779
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100780 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100781 return -EINVAL;
782
783 mutex_lock(&data->lock);
784
785 switch (sattr->nr) {
786 case INPUT:
787 /* Get a fresh value for CONTROL */
788 data->pwm[CONTROL][sattr->index] =
789 adt7475_read(PWM_CONFIG_REG(sattr->index));
790
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800791 /*
792 * If we are not in manual mode, then we shouldn't allow
793 * the user to set the pwm speed
794 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100795 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
796 mutex_unlock(&data->lock);
797 return count;
798 }
799
800 reg = PWM_REG(sattr->index);
801 break;
802
803 case MIN:
804 reg = PWM_MIN_REG(sattr->index);
805 break;
806
807 case MAX:
808 reg = PWM_MAX_REG(sattr->index);
809 break;
810 }
811
Guenter Roeck2a844c12013-01-09 08:09:34 -0800812 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100813 i2c_smbus_write_byte_data(client, reg,
814 data->pwm[sattr->nr][sattr->index]);
Chris Packham1d58f5e2017-05-15 13:30:27 +1200815 mutex_unlock(&data->lock);
816
817 return count;
818}
819
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800820static ssize_t stall_disable_show(struct device *dev,
Chris Packham1d58f5e2017-05-15 13:30:27 +1200821 struct device_attribute *attr, char *buf)
822{
823 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200824 struct adt7475_data *data = dev_get_drvdata(dev);
825
Chris Packham1d58f5e2017-05-15 13:30:27 +1200826 u8 mask = BIT(5 + sattr->index);
827
828 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
829}
830
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800831static ssize_t stall_disable_store(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf, size_t count)
Chris Packham1d58f5e2017-05-15 13:30:27 +1200834{
835 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200836 struct adt7475_data *data = dev_get_drvdata(dev);
837 struct i2c_client *client = data->client;
Chris Packham1d58f5e2017-05-15 13:30:27 +1200838 long val;
839 u8 mask = BIT(5 + sattr->index);
840
841 if (kstrtol(buf, 10, &val))
842 return -EINVAL;
843
844 mutex_lock(&data->lock);
845
846 data->enh_acoustics[0] &= ~mask;
847 if (val)
848 data->enh_acoustics[0] |= mask;
849
850 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
851 data->enh_acoustics[0]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100852
853 mutex_unlock(&data->lock);
854
855 return count;
856}
857
858/* Called by set_pwmctrl and set_pwmchan */
859
860static int hw_set_pwm(struct i2c_client *client, int index,
861 unsigned int pwmctl, unsigned int pwmchan)
862{
863 struct adt7475_data *data = i2c_get_clientdata(client);
864 long val = 0;
865
866 switch (pwmctl) {
867 case 0:
868 val = 0x03; /* Run at full speed */
869 break;
870 case 1:
871 val = 0x07; /* Manual mode */
872 break;
873 case 2:
874 switch (pwmchan) {
875 case 1:
876 /* Remote1 controls PWM */
877 val = 0x00;
878 break;
879 case 2:
880 /* local controls PWM */
881 val = 0x01;
882 break;
883 case 4:
884 /* remote2 controls PWM */
885 val = 0x02;
886 break;
887 case 6:
888 /* local/remote2 control PWM */
889 val = 0x05;
890 break;
891 case 7:
892 /* All three control PWM */
893 val = 0x06;
894 break;
895 default:
896 return -EINVAL;
897 }
898 break;
899 default:
900 return -EINVAL;
901 }
902
903 data->pwmctl[index] = pwmctl;
904 data->pwmchan[index] = pwmchan;
905
906 data->pwm[CONTROL][index] &= ~0xE0;
907 data->pwm[CONTROL][index] |= (val & 7) << 5;
908
909 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
910 data->pwm[CONTROL][index]);
911
912 return 0;
913}
914
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800915static ssize_t pwmchan_store(struct device *dev,
916 struct device_attribute *attr, const char *buf,
917 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100918{
919 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200920 struct adt7475_data *data = dev_get_drvdata(dev);
921 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100922 int r;
923 long val;
924
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100925 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100926 return -EINVAL;
927
928 mutex_lock(&data->lock);
929 /* Read Modify Write PWM values */
930 adt7475_read_pwm(client, sattr->index);
931 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
932 if (r)
933 count = r;
934 mutex_unlock(&data->lock);
935
936 return count;
937}
938
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800939static ssize_t pwmctrl_store(struct device *dev,
940 struct device_attribute *attr, const char *buf,
941 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100942{
943 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200944 struct adt7475_data *data = dev_get_drvdata(dev);
945 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100946 int r;
947 long val;
948
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100949 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100950 return -EINVAL;
951
952 mutex_lock(&data->lock);
953 /* Read Modify Write PWM values */
954 adt7475_read_pwm(client, sattr->index);
955 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
956 if (r)
957 count = r;
958 mutex_unlock(&data->lock);
959
960 return count;
961}
962
963/* List of frequencies for the PWM */
964static const int pwmfreq_table[] = {
Chris Packham3490c922017-05-15 13:30:29 +1200965 11, 14, 22, 29, 35, 44, 58, 88, 22500
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100966};
967
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800968static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100969 char *buf)
970{
971 struct adt7475_data *data = adt7475_update_device(dev);
972 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Dan Carpenter9d193712018-08-14 12:12:36 +0300973 int idx;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100974
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900975 if (IS_ERR(data))
976 return PTR_ERR(data);
Dan Carpenter9d193712018-08-14 12:12:36 +0300977 idx = clamp_val(data->range[sattr->index] & 0xf, 0,
978 ARRAY_SIZE(pwmfreq_table) - 1);
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900979
Dan Carpenter9d193712018-08-14 12:12:36 +0300980 return sprintf(buf, "%d\n", pwmfreq_table[idx]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100981}
982
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800983static ssize_t pwmfreq_store(struct device *dev,
984 struct device_attribute *attr, const char *buf,
985 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100986{
987 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Grant McEwanc64fce72019-07-22 10:55:30 +1200988 struct adt7475_data *data = dev_get_drvdata(dev);
989 struct i2c_client *client = data->client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100990 int out;
991 long val;
992
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100993 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100994 return -EINVAL;
995
Chris Packhame4651642017-05-11 15:45:21 +1200996 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100997
998 mutex_lock(&data->lock);
999
1000 data->range[sattr->index] =
1001 adt7475_read(TEMP_TRANGE_REG(sattr->index));
Chris Packham3490c922017-05-15 13:30:29 +12001002 data->range[sattr->index] &= ~0xf;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001003 data->range[sattr->index] |= out;
1004
1005 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
1006 data->range[sattr->index]);
1007
1008 mutex_unlock(&data->lock);
1009 return count;
1010}
1011
Julia Lawall1d05303c2016-12-22 13:05:33 +01001012static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1013 struct device_attribute *devattr,
1014 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +01001015{
1016 struct adt7475_data *data = adt7475_update_device(dev);
Dan Carpenter9d193712018-08-14 12:12:36 +03001017
1018 if (IS_ERR(data))
1019 return PTR_ERR(data);
1020
Jean Delvaref99318b2009-12-09 20:36:03 +01001021 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1022}
1023
Julia Lawall1d05303c2016-12-22 13:05:33 +01001024static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1025 struct device_attribute *devattr,
1026 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +01001027{
Grant McEwanc64fce72019-07-22 10:55:30 +12001028 struct adt7475_data *data = dev_get_drvdata(dev);
1029 struct i2c_client *client = data->client;
Jean Delvaref99318b2009-12-09 20:36:03 +01001030 long val;
1031
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001032 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +01001033 return -EINVAL;
1034 if (val != 0 && val != 1)
1035 return -EINVAL;
1036
1037 mutex_lock(&data->lock);
1038 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1039 if (val)
1040 data->config4 |= CONFIG4_MAXDUTY;
1041 else
1042 data->config4 &= ~CONFIG4_MAXDUTY;
1043 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1044 mutex_unlock(&data->lock);
1045
1046 return count;
1047}
1048
Julia Lawall1d05303c2016-12-22 13:05:33 +01001049static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +01001050 char *buf)
1051{
1052 struct adt7475_data *data = dev_get_drvdata(dev);
1053 return sprintf(buf, "%d\n", (int)data->vrm);
1054}
1055
Julia Lawall1d05303c2016-12-22 13:05:33 +01001056static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1057 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +01001058{
1059 struct adt7475_data *data = dev_get_drvdata(dev);
1060 long val;
1061
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001062 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +01001063 return -EINVAL;
1064 if (val < 0 || val > 255)
1065 return -EINVAL;
1066 data->vrm = val;
1067
1068 return count;
1069}
1070
Julia Lawall1d05303c2016-12-22 13:05:33 +01001071static ssize_t cpu0_vid_show(struct device *dev,
1072 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +01001073{
1074 struct adt7475_data *data = adt7475_update_device(dev);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001075
1076 if (IS_ERR(data))
1077 return PTR_ERR(data);
1078
Jean Delvare54fe4672009-12-09 20:36:08 +01001079 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1080}
1081
Guenter Roeckc24f9ba2018-12-10 14:02:02 -08001082static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1083static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1084static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1085static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1086static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1087static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1088static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1089static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1090static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1091static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1092static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1093static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1094static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1095static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1096static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1097static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1098static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1099static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1100static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1101static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1102static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1103static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1104static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1105static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
1106static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1107static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1108static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1109static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1110static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1111static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1112static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1113static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1114static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1115static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1116static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1117static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1118static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1119static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1120static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1121static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1122static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1123static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1124static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1125static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1126static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1127static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1128static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1129static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1130static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1131static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1132static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1133static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1134static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1135static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1136static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1137static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1138static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1139static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1140static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1141static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1142static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1143static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1144static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1145static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1146static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1147static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1148static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1149static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1150static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1151static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1152static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1153static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1154static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1155static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1156static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1157static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1158static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1159static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1160static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1161static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1162static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1163static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1164static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1165static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1166static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1167static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1168static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1169static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1170static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001171
Jean Delvaref99318b2009-12-09 20:36:03 +01001172/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001173static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001174
Julia Lawall1d05303c2016-12-22 13:05:33 +01001175static DEVICE_ATTR_RW(vrm);
1176static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001177
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001178static struct attribute *adt7475_attrs[] = {
1179 &sensor_dev_attr_in1_input.dev_attr.attr,
1180 &sensor_dev_attr_in1_max.dev_attr.attr,
1181 &sensor_dev_attr_in1_min.dev_attr.attr,
1182 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1183 &sensor_dev_attr_in2_input.dev_attr.attr,
1184 &sensor_dev_attr_in2_max.dev_attr.attr,
1185 &sensor_dev_attr_in2_min.dev_attr.attr,
1186 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1187 &sensor_dev_attr_temp1_input.dev_attr.attr,
1188 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1189 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1190 &sensor_dev_attr_temp1_max.dev_attr.attr,
1191 &sensor_dev_attr_temp1_min.dev_attr.attr,
1192 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1193 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1194 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1195 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1196 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001197 &sensor_dev_attr_temp1_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001198 &sensor_dev_attr_temp2_input.dev_attr.attr,
1199 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1200 &sensor_dev_attr_temp2_max.dev_attr.attr,
1201 &sensor_dev_attr_temp2_min.dev_attr.attr,
1202 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1203 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1204 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1205 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1206 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001207 &sensor_dev_attr_temp2_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001208 &sensor_dev_attr_temp3_input.dev_attr.attr,
1209 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1210 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1211 &sensor_dev_attr_temp3_max.dev_attr.attr,
1212 &sensor_dev_attr_temp3_min.dev_attr.attr,
1213 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1214 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1215 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1216 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1217 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001218 &sensor_dev_attr_temp3_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001219 &sensor_dev_attr_fan1_input.dev_attr.attr,
1220 &sensor_dev_attr_fan1_min.dev_attr.attr,
1221 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1222 &sensor_dev_attr_fan2_input.dev_attr.attr,
1223 &sensor_dev_attr_fan2_min.dev_attr.attr,
1224 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1225 &sensor_dev_attr_fan3_input.dev_attr.attr,
1226 &sensor_dev_attr_fan3_min.dev_attr.attr,
1227 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001228 &sensor_dev_attr_pwm1.dev_attr.attr,
1229 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1230 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001231 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001232 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1233 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001234 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001235 &sensor_dev_attr_pwm3.dev_attr.attr,
1236 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1237 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001238 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001239 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1240 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001241 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001242 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001243 NULL,
1244};
1245
Jean Delvare378933c2009-12-09 20:36:06 +01001246static struct attribute *fan4_attrs[] = {
1247 &sensor_dev_attr_fan4_input.dev_attr.attr,
1248 &sensor_dev_attr_fan4_min.dev_attr.attr,
1249 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1250 NULL
1251};
1252
1253static struct attribute *pwm2_attrs[] = {
1254 &sensor_dev_attr_pwm2.dev_attr.attr,
1255 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1256 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1257 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1258 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1259 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001260 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001261 NULL
1262};
1263
Jean Delvare378933c2009-12-09 20:36:06 +01001264static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001265 &sensor_dev_attr_in0_input.dev_attr.attr,
1266 &sensor_dev_attr_in0_max.dev_attr.attr,
1267 &sensor_dev_attr_in0_min.dev_attr.attr,
1268 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001269 NULL
1270};
1271
Jean Delvared8d2ee02009-12-09 20:36:08 +01001272static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001273 &sensor_dev_attr_in3_input.dev_attr.attr,
1274 &sensor_dev_attr_in3_max.dev_attr.attr,
1275 &sensor_dev_attr_in3_min.dev_attr.attr,
1276 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001277 NULL
1278};
1279
1280static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001281 &sensor_dev_attr_in4_input.dev_attr.attr,
1282 &sensor_dev_attr_in4_max.dev_attr.attr,
1283 &sensor_dev_attr_in4_min.dev_attr.attr,
1284 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001285 NULL
1286};
1287
1288static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001289 &sensor_dev_attr_in5_input.dev_attr.attr,
1290 &sensor_dev_attr_in5_max.dev_attr.attr,
1291 &sensor_dev_attr_in5_min.dev_attr.attr,
1292 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1293 NULL
1294};
1295
Jean Delvare54fe4672009-12-09 20:36:08 +01001296static struct attribute *vid_attrs[] = {
1297 &dev_attr_cpu0_vid.attr,
1298 &dev_attr_vrm.attr,
1299 NULL
1300};
1301
Arvind Yadavf5397be2017-07-05 11:04:04 +05301302static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1303static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1304static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1305static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1306static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1307static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1308static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1309static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001310
Jean Delvare310ec792009-12-14 21:17:23 +01001311static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001312 struct i2c_board_info *info)
1313{
1314 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001315 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001316 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001317
1318 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1319 return -ENODEV;
1320
Jean Delvareb180d052009-12-09 20:36:02 +01001321 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001322 devid2 = adt7475_read(REG_DEVID2);
1323 if (vendid != 0x41 || /* Analog Devices */
1324 (devid2 & 0xf8) != 0x68)
1325 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001326
Jean Delvared656b6f2009-12-09 20:36:04 +01001327 devid = adt7475_read(REG_DEVID);
1328 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001329 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001330 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001331 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001332 else if (devid == 0x76)
1333 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001334 else if ((devid2 & 0xfc) == 0x6c)
1335 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001336 else {
1337 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001338 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001339 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001340 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001341 }
1342
Jean Delvareb180d052009-12-09 20:36:02 +01001343 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001344
1345 return 0;
1346}
1347
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001348static int adt7475_update_limits(struct i2c_client *client)
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001349{
1350 struct adt7475_data *data = i2c_get_clientdata(client);
1351 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001352 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001353
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001354 ret = adt7475_read(REG_CONFIG4);
1355 if (ret < 0)
1356 return ret;
1357 data->config4 = ret;
1358
1359 ret = adt7475_read(REG_CONFIG5);
1360 if (ret < 0)
1361 return ret;
1362 data->config5 = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001363
1364 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1365 if (!(data->has_voltage & (1 << i)))
1366 continue;
1367 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001368 ret = adt7475_read(VOLTAGE_MIN_REG(i));
1369 if (ret < 0)
1370 return ret;
1371 data->voltage[MIN][i] = ret << 2;
1372
1373 ret = adt7475_read(VOLTAGE_MAX_REG(i));
1374 if (ret < 0)
1375 return ret;
1376 data->voltage[MAX][i] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001377 }
1378
1379 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001380 ret = adt7475_read(REG_VTT_MIN);
1381 if (ret < 0)
1382 return ret;
1383 data->voltage[MIN][5] = ret << 2;
1384
1385 ret = adt7475_read(REG_VTT_MAX);
1386 if (ret < 0)
1387 return ret;
1388 data->voltage[MAX][5] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001389 }
1390
1391 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1392 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001393 ret = adt7475_read(TEMP_MIN_REG(i));
1394 if (ret < 0)
1395 return ret;
1396 data->temp[MIN][i] = ret << 2;
1397
1398 ret = adt7475_read(TEMP_MAX_REG(i));
1399 if (ret < 0)
1400 return ret;
1401 data->temp[MAX][i] = ret << 2;
1402
1403 ret = adt7475_read(TEMP_TMIN_REG(i));
1404 if (ret < 0)
1405 return ret;
1406 data->temp[AUTOMIN][i] = ret << 2;
1407
1408 ret = adt7475_read(TEMP_THERM_REG(i));
1409 if (ret < 0)
1410 return ret;
1411 data->temp[THERM][i] = ret << 2;
1412
1413 ret = adt7475_read(TEMP_OFFSET_REG(i));
1414 if (ret < 0)
1415 return ret;
1416 data->temp[OFFSET][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001417 }
1418 adt7475_read_hystersis(client);
1419
1420 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1421 if (i == 3 && !data->has_fan4)
1422 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001423 ret = adt7475_read_word(client, TACH_MIN_REG(i));
1424 if (ret < 0)
1425 return ret;
1426 data->tach[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001427 }
1428
1429 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1430 if (i == 1 && !data->has_pwm2)
1431 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001432 ret = adt7475_read(PWM_MAX_REG(i));
1433 if (ret < 0)
1434 return ret;
1435 data->pwm[MAX][i] = ret;
1436
1437 ret = adt7475_read(PWM_MIN_REG(i));
1438 if (ret < 0)
1439 return ret;
1440 data->pwm[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001441 /* Set the channel and control information */
1442 adt7475_read_pwm(client, i);
1443 }
1444
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001445 ret = adt7475_read(TEMP_TRANGE_REG(0));
1446 if (ret < 0)
1447 return ret;
1448 data->range[0] = ret;
1449
1450 ret = adt7475_read(TEMP_TRANGE_REG(1));
1451 if (ret < 0)
1452 return ret;
1453 data->range[1] = ret;
1454
1455 ret = adt7475_read(TEMP_TRANGE_REG(2));
1456 if (ret < 0)
1457 return ret;
1458 data->range[2] = ret;
1459
1460 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001461}
1462
Logan Shaw2ecff392020-02-27 21:46:41 +13001463static int set_property_bit(const struct i2c_client *client, char *property,
1464 u8 *config, u8 bit_index)
1465{
1466 u32 prop_value = 0;
1467 int ret = of_property_read_u32(client->dev.of_node, property,
1468 &prop_value);
1469
1470 if (!ret) {
1471 if (prop_value)
1472 *config |= (1 << bit_index);
1473 else
1474 *config &= ~(1 << bit_index);
1475 }
1476
1477 return ret;
1478}
1479
1480static int load_attenuators(const struct i2c_client *client, int chip,
1481 struct adt7475_data *data)
1482{
1483 int ret;
1484
1485 if (chip == adt7476 || chip == adt7490) {
1486 set_property_bit(client, "adi,bypass-attenuator-in0",
1487 &data->config4, 4);
1488 set_property_bit(client, "adi,bypass-attenuator-in1",
1489 &data->config4, 5);
1490 set_property_bit(client, "adi,bypass-attenuator-in3",
1491 &data->config4, 6);
1492 set_property_bit(client, "adi,bypass-attenuator-in4",
1493 &data->config4, 7);
1494
1495 ret = i2c_smbus_write_byte_data(client, REG_CONFIG4,
1496 data->config4);
1497 if (ret < 0)
1498 return ret;
1499 } else if (chip == adt7473 || chip == adt7475) {
1500 set_property_bit(client, "adi,bypass-attenuator-in1",
1501 &data->config2, 5);
1502
1503 ret = i2c_smbus_write_byte_data(client, REG_CONFIG2,
1504 data->config2);
1505 if (ret < 0)
1506 return ret;
1507 }
1508
1509 return 0;
1510}
1511
Chris Packham86da28e2020-02-27 21:46:42 +13001512static int adt7475_set_pwm_polarity(struct i2c_client *client)
1513{
1514 u32 states[ADT7475_PWM_COUNT];
1515 int ret, i;
1516 u8 val;
1517
1518 ret = of_property_read_u32_array(client->dev.of_node,
1519 "adi,pwm-active-state", states,
1520 ARRAY_SIZE(states));
1521 if (ret)
1522 return ret;
1523
1524 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1525 ret = adt7475_read(PWM_CONFIG_REG(i));
1526 if (ret < 0)
1527 return ret;
1528 val = ret;
1529 if (states[i])
1530 val &= ~BIT(4);
1531 else
1532 val |= BIT(4);
1533
1534 ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(i), val);
1535 if (ret)
1536 return ret;
1537 }
1538
1539 return 0;
1540}
1541
Stephen Kitt67487032020-08-13 18:02:22 +02001542static int adt7475_probe(struct i2c_client *client)
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001543{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001544 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001545 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001546 [adt7473] = "ADT7473",
1547 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001548 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001549 [adt7490] = "ADT7490",
1550 };
1551
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001552 struct adt7475_data *data;
Grant McEwanc64fce72019-07-22 10:55:30 +12001553 struct device *hwmon_dev;
1554 int i, ret = 0, revision, group_num = 0;
Logan Shaw2ecff392020-02-27 21:46:41 +13001555 u8 config3;
Stephen Kitt67487032020-08-13 18:02:22 +02001556 const struct i2c_device_id *id = i2c_match_id(adt7475_id, client);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001557
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001558 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559 if (data == NULL)
1560 return -ENOMEM;
1561
1562 mutex_init(&data->lock);
Grant McEwanc64fce72019-07-22 10:55:30 +12001563 data->client = client;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001564 i2c_set_clientdata(client, data);
1565
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001566 if (client->dev.of_node)
1567 chip = (enum chips)of_device_get_match_data(&client->dev);
1568 else
1569 chip = id->driver_data;
1570
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001571 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001572 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001573 case adt7476:
1574 data->has_voltage = 0x0e; /* in1 to in3 */
1575 revision = adt7475_read(REG_DEVID2) & 0x07;
1576 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001577 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001578 data->has_voltage = 0x3e; /* in1 to in5 */
1579 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001580 if (revision == 0x03)
1581 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001582 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001583 default:
1584 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001585 revision = adt7475_read(REG_DEVID2) & 0x07;
1586 }
1587
1588 config3 = adt7475_read(REG_CONFIG3);
1589 /* Pin PWM2 may alternatively be used for ALERT output */
1590 if (!(config3 & CONFIG3_SMBALERT))
1591 data->has_pwm2 = 1;
1592 /* Meaning of this bit is inverted for the ADT7473-1 */
1593 if (id->driver_data == adt7473 && revision >= 1)
1594 data->has_pwm2 = !data->has_pwm2;
1595
1596 data->config4 = adt7475_read(REG_CONFIG4);
1597 /* Pin TACH4 may alternatively be used for THERM */
1598 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1599 data->has_fan4 = 1;
1600
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001601 /*
1602 * THERM configuration is more complex on the ADT7476 and ADT7490,
1603 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1604 * this function
1605 */
Jean Delvare378933c2009-12-09 20:36:06 +01001606 if (id->driver_data == adt7490) {
1607 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1608 !(config3 & CONFIG3_THERM))
1609 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001610 }
1611 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001612 if (!(config3 & CONFIG3_THERM) ||
1613 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1614 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001615 }
1616
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001617 /*
1618 * On the ADT7476, the +12V input pin may instead be used as VID5,
1619 * and VID pins may alternatively be used as GPIO
1620 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001621 if (id->driver_data == adt7476) {
1622 u8 vid = adt7475_read(REG_VID);
1623 if (!(vid & VID_VIDSEL))
1624 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001625
1626 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001627 }
1628
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001629 /* Voltage attenuators can be bypassed, globally or individually */
Logan Shaw2ecff392020-02-27 21:46:41 +13001630 data->config2 = adt7475_read(REG_CONFIG2);
1631 ret = load_attenuators(client, chip, data);
1632 if (ret)
1633 dev_warn(&client->dev, "Error configuring attenuator bypass\n");
1634
1635 if (data->config2 & CONFIG2_ATTN) {
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001636 data->bypass_attn = (0x3 << 3) | 0x3;
1637 } else {
1638 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1639 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1640 }
1641 data->bypass_attn &= data->has_voltage;
1642
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001643 /*
1644 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1645 * pwm's which are disabled to manual mode with 0% duty cycle
1646 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001647 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1648 adt7475_read_pwm(client, i);
1649
Chris Packham86da28e2020-02-27 21:46:42 +13001650 ret = adt7475_set_pwm_polarity(client);
1651 if (ret && ret != -EINVAL)
1652 dev_warn(&client->dev, "Error configuring pwm polarity\n");
1653
Chris Packham4abdf382017-04-22 07:08:09 +12001654 /* Start monitoring */
1655 switch (chip) {
1656 case adt7475:
1657 case adt7476:
1658 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1659 adt7475_read(REG_CONFIG1) | 0x01);
1660 break;
1661 default:
1662 break;
1663 }
1664
Grant McEwanc64fce72019-07-22 10:55:30 +12001665 data->groups[group_num++] = &adt7475_attr_group;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001666
Jean Delvare378933c2009-12-09 20:36:06 +01001667 /* Features that can be disabled individually */
1668 if (data->has_fan4) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001669 data->groups[group_num++] = &fan4_attr_group;
Jean Delvare378933c2009-12-09 20:36:06 +01001670 }
1671 if (data->has_pwm2) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001672 data->groups[group_num++] = &pwm2_attr_group;
Jean Delvare378933c2009-12-09 20:36:06 +01001673 }
1674 if (data->has_voltage & (1 << 0)) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001675 data->groups[group_num++] = &in0_attr_group;
Jean Delvare378933c2009-12-09 20:36:06 +01001676 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001677 if (data->has_voltage & (1 << 3)) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001678 data->groups[group_num++] = &in3_attr_group;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001679 }
1680 if (data->has_voltage & (1 << 4)) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001681 data->groups[group_num++] = &in4_attr_group;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001682 }
1683 if (data->has_voltage & (1 << 5)) {
Grant McEwanc64fce72019-07-22 10:55:30 +12001684 data->groups[group_num++] = &in5_attr_group;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001685 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001686 if (data->has_vid) {
1687 data->vrm = vid_which_vrm();
Grant McEwanc64fce72019-07-22 10:55:30 +12001688 data->groups[group_num] = &vid_attr_group;
Jean Delvare54fe4672009-12-09 20:36:08 +01001689 }
Jean Delvare378933c2009-12-09 20:36:06 +01001690
Grant McEwanc64fce72019-07-22 10:55:30 +12001691 /* register device with all the acquired attributes */
1692 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
1693 client->name, data,
1694 data->groups);
1695
1696 if (IS_ERR(hwmon_dev)) {
1697 ret = PTR_ERR(hwmon_dev);
1698 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001699 }
1700
Jean Delvared07ca4a2009-12-09 20:36:07 +01001701 dev_info(&client->dev, "%s device, revision %d\n",
1702 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001703 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001704 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001705 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001706 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001707 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001708 data->has_pwm2 ? " pwm2" : "",
1709 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001710 if (data->bypass_attn)
1711 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1712 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1713 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1714 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1715 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001716
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001717 /* Limits and settings, should never change update more than once */
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001718 ret = adt7475_update_limits(client);
1719 if (ret)
Grant McEwanc64fce72019-07-22 10:55:30 +12001720 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001721
1722 return 0;
1723}
1724
1725static struct i2c_driver adt7475_driver = {
1726 .class = I2C_CLASS_HWMON,
1727 .driver = {
1728 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001729 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001730 },
Stephen Kitt67487032020-08-13 18:02:22 +02001731 .probe_new = adt7475_probe,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001732 .id_table = adt7475_id,
1733 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001734 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001735};
1736
1737static void adt7475_read_hystersis(struct i2c_client *client)
1738{
1739 struct adt7475_data *data = i2c_get_clientdata(client);
1740
1741 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1742 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1743 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1744}
1745
1746static void adt7475_read_pwm(struct i2c_client *client, int index)
1747{
1748 struct adt7475_data *data = i2c_get_clientdata(client);
1749 unsigned int v;
1750
1751 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1752
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001753 /*
1754 * Figure out the internal value for pwmctrl and pwmchan
1755 * based on the current settings
1756 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001757 v = (data->pwm[CONTROL][index] >> 5) & 7;
1758
1759 if (v == 3)
1760 data->pwmctl[index] = 0;
1761 else if (v == 7)
1762 data->pwmctl[index] = 1;
1763 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001764 /*
1765 * The fan is disabled - we don't want to
1766 * support that, so change to manual mode and
1767 * set the duty cycle to 0 instead
1768 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001769 data->pwm[INPUT][index] = 0;
1770 data->pwm[CONTROL][index] &= ~0xE0;
1771 data->pwm[CONTROL][index] |= (7 << 5);
1772
1773 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1774 data->pwm[INPUT][index]);
1775
1776 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1777 data->pwm[CONTROL][index]);
1778
1779 data->pwmctl[index] = 1;
1780 } else {
1781 data->pwmctl[index] = 2;
1782
1783 switch (v) {
1784 case 0:
1785 data->pwmchan[index] = 1;
1786 break;
1787 case 1:
1788 data->pwmchan[index] = 2;
1789 break;
1790 case 2:
1791 data->pwmchan[index] = 4;
1792 break;
1793 case 5:
1794 data->pwmchan[index] = 6;
1795 break;
1796 case 6:
1797 data->pwmchan[index] = 7;
1798 break;
1799 }
1800 }
1801}
1802
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001803static int adt7475_update_measure(struct device *dev)
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001804{
Grant McEwanc64fce72019-07-22 10:55:30 +12001805 struct adt7475_data *data = dev_get_drvdata(dev);
1806 struct i2c_client *client = data->client;
Jean Delvare3d849982009-12-09 20:36:05 +01001807 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001808 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001809 int ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001810
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001811 ret = adt7475_read(REG_STATUS2);
1812 if (ret < 0)
1813 return ret;
1814 data->alarms = ret << 8;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001815
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001816 ret = adt7475_read(REG_STATUS1);
1817 if (ret < 0)
1818 return ret;
1819 data->alarms |= ret;
1820
1821 ret = adt7475_read(REG_EXTEND2);
1822 if (ret < 0)
1823 return ret;
1824
1825 ext = (ret << 8);
1826
1827 ret = adt7475_read(REG_EXTEND1);
1828 if (ret < 0)
1829 return ret;
1830
1831 ext |= ret;
1832
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001833 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1834 if (!(data->has_voltage & (1 << i)))
1835 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001836 ret = adt7475_read(VOLTAGE_REG(i));
1837 if (ret < 0)
1838 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001839 data->voltage[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001840 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001841 ((ext >> (i * 2)) & 3);
1842 }
1843
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001844 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1845 ret = adt7475_read(TEMP_REG(i));
1846 if (ret < 0)
1847 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001848 data->temp[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001849 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001850 ((ext >> ((i + 5) * 2)) & 3);
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001851 }
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001852
1853 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001854 ret = adt7475_read(REG_STATUS4);
1855 if (ret < 0)
1856 return ret;
1857 data->alarms |= ret << 24;
1858
1859 ret = adt7475_read(REG_EXTEND3);
1860 if (ret < 0)
1861 return ret;
1862 ext = ret;
1863
1864 ret = adt7475_read(REG_VTT);
1865 if (ret < 0)
1866 return ret;
1867 data->voltage[INPUT][5] = ret << 2 |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001868 ((ext >> 4) & 3);
1869 }
1870
1871 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1872 if (i == 3 && !data->has_fan4)
1873 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001874 ret = adt7475_read_word(client, TACH_REG(i));
1875 if (ret < 0)
1876 return ret;
1877 data->tach[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001878 }
1879
1880 /* Updated by hw when in auto mode */
1881 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1882 if (i == 1 && !data->has_pwm2)
1883 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001884 ret = adt7475_read(PWM_REG(i));
1885 if (ret < 0)
1886 return ret;
1887 data->pwm[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001888 }
1889
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001890 if (data->has_vid) {
1891 ret = adt7475_read(REG_VID);
1892 if (ret < 0)
1893 return ret;
1894 data->vid = ret & 0x3f;
1895 }
1896
1897 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001898}
1899
1900static struct adt7475_data *adt7475_update_device(struct device *dev)
1901{
Grant McEwanc64fce72019-07-22 10:55:30 +12001902 struct adt7475_data *data = dev_get_drvdata(dev);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001903 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001904
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001905 mutex_lock(&data->lock);
1906
1907 /* Measurement values update every 2 seconds */
1908 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1909 !data->valid) {
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001910 ret = adt7475_update_measure(dev);
1911 if (ret) {
1912 data->valid = false;
1913 mutex_unlock(&data->lock);
1914 return ERR_PTR(ret);
1915 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001916 data->measure_updated = jiffies;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +09001917 data->valid = true;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001918 }
1919
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001920 mutex_unlock(&data->lock);
1921
1922 return data;
1923}
1924
Axel Linf0967ee2012-01-20 15:38:18 +08001925module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001926
1927MODULE_AUTHOR("Advanced Micro Devices, Inc");
1928MODULE_DESCRIPTION("adt7475 driver");
1929MODULE_LICENSE("GPL");