blob: 16045149f3db9aec6ab67c224da5f8bb0a834c30 [file] [log] [blame]
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001/*
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 Delvare7c81c602014-01-29 20:40:08 +01006 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
Jean Delvare3d849982009-12-09 20:36:05 +01007 *
Jordan Crouse1c301fc2009-01-15 22:27:47 +01008 * 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 Canillas4e2496e2017-02-24 10:12:58 -030016#include <linux/of_device.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010017#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 Delvare54fe4672009-12-09 20:36:08 +010022#include <linux/hwmon-vid.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010023#include <linux/err.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020024#include <linux/jiffies.h>
Chris Packhame4651642017-05-11 15:45:21 +120025#include <linux/util_macros.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010026
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 Roeck9ed5bc22012-01-19 11:02:15 -080038/*
39 * These are unique identifiers for the sysfs functions - unlike the
40 * numbers above, these are not also indexes into an array
41 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010042
43#define ALARM 9
44#define FAULT 10
45
46/* 7475 Common Registers */
47
Jean Delvared07ca4a2009-12-09 20:36:07 +010048#define REG_DEVREV2 0x12 /* ADT7490 only */
49
Jean Delvare3d849982009-12-09 20:36:05 +010050#define REG_VTT 0x1E /* ADT7490 only */
51#define REG_EXTEND3 0x1F /* ADT7490 only */
52
Jean Delvarecffb9dd2009-12-09 20:36:03 +010053#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010054#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 Delvared656b6f2009-12-09 20:36:04 +010061#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010062
Chris Packham4abdf382017-04-22 07:08:09 +120063#define REG_CONFIG1 0x40
64
Jordan Crouse1c301fc2009-01-15 22:27:47 +010065#define REG_STATUS1 0x41
66#define REG_STATUS2 0x42
67
Jean Delvared8d2ee02009-12-09 20:36:08 +010068#define REG_VID 0x43 /* ADT7476 only */
69
Jean Delvarecffb9dd2009-12-09 20:36:03 +010070#define REG_VOLTAGE_MIN_BASE 0x44
71#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010072
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
Chris Packham1d58f5e2017-05-15 13:30:27 +120082#define REG_ENHANCE_ACOUSTICS1 0x62
83#define REG_ENHANCE_ACOUSTICS2 0x63
84
Jordan Crouse1c301fc2009-01-15 22:27:47 +010085#define REG_PWM_MIN_BASE 0x64
86
87#define REG_TEMP_TMIN_BASE 0x67
88#define REG_TEMP_THERM_BASE 0x6A
89
90#define REG_REMOTE1_HYSTERSIS 0x6D
91#define REG_REMOTE2_HYSTERSIS 0x6E
92
93#define REG_TEMP_OFFSET_BASE 0x70
94
Jean Delvareebfaf1f2009-12-09 20:36:07 +010095#define REG_CONFIG2 0x73
96
Jordan Crouse1c301fc2009-01-15 22:27:47 +010097#define REG_EXTEND1 0x76
98#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010099
100#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100101#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +0100102#define REG_CONFIG4 0x7D
103
Jean Delvare3d849982009-12-09 20:36:05 +0100104#define REG_STATUS4 0x81 /* ADT7490 only */
105
106#define REG_VTT_MIN 0x84 /* ADT7490 only */
107#define REG_VTT_MAX 0x86 /* ADT7490 only */
108
Jean Delvared8d2ee02009-12-09 20:36:08 +0100109#define VID_VIDSEL 0x80 /* ADT7476 only */
110
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100111#define CONFIG2_ATTN 0x20
112
Jean Delvare378933c2009-12-09 20:36:06 +0100113#define CONFIG3_SMBALERT 0x01
114#define CONFIG3_THERM 0x02
115
116#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100117#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100118#define CONFIG4_ATTN_IN10 0x30
119#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100120
121#define CONFIG5_TWOSCOMP 0x01
122#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100123#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100124
125/* ADT7475 Settings */
126
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100127#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100128#define ADT7475_TEMP_COUNT 3
129#define ADT7475_TACH_COUNT 4
130#define ADT7475_PWM_COUNT 3
131
132/* Macro to read the registers */
133
134#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
135
136/* Macros to easily index the registers */
137
138#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
139#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
140
141#define PWM_REG(idx) (REG_PWM_BASE + (idx))
142#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
143#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
144#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
145
146#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
147#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
148#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
149
150#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
151#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
152#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
153#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
154#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
155#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
156#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
157
Jean Delvare918ee912010-10-28 20:31:50 +0200158static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100159
Jean Delvaree5e9f442009-12-14 21:17:27 +0100160enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100161
162static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100163 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100164 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100165 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100166 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100167 { }
168};
169MODULE_DEVICE_TABLE(i2c, adt7475_id);
170
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300171static const struct of_device_id adt7475_of_match[] = {
172 {
173 .compatible = "adi,adt7473",
174 .data = (void *)adt7473
175 },
176 {
177 .compatible = "adi,adt7475",
178 .data = (void *)adt7475
179 },
180 {
181 .compatible = "adi,adt7476",
182 .data = (void *)adt7476
183 },
184 {
185 .compatible = "adi,adt7490",
186 .data = (void *)adt7490
187 },
188 { },
189};
190MODULE_DEVICE_TABLE(of, adt7475_of_match);
191
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100192struct adt7475_data {
193 struct device *hwmon_dev;
194 struct mutex lock;
195
196 unsigned long measure_updated;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +0900197 bool valid;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100198
Jean Delvaref99318b2009-12-09 20:36:03 +0100199 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100200 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100201 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100202 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100203 u8 has_pwm2:1;
204 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100205 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100206 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100207 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100208 u16 temp[7][3];
209 u16 tach[2][4];
210 u8 pwm[4][3];
211 u8 range[3];
212 u8 pwmctl[3];
213 u8 pwmchan[3];
Chris Packham1d58f5e2017-05-15 13:30:27 +1200214 u8 enh_acoustics[2];
Jean Delvare54fe4672009-12-09 20:36:08 +0100215
216 u8 vid;
217 u8 vrm;
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))
299 reg = (volt * 1024) / 2250;
300 else
301 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800302 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100303}
304
305static u16 adt7475_read_word(struct i2c_client *client, int reg)
306{
307 u16 val;
308
309 val = i2c_smbus_read_byte_data(client, reg);
310 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
311
312 return val;
313}
314
315static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
316{
317 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
318 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
319}
320
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100321static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct adt7475_data *data = adt7475_update_device(dev);
325 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
326 unsigned short val;
327
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900328 if (IS_ERR(data))
329 return PTR_ERR(data);
330
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100331 switch (sattr->nr) {
332 case ALARM:
333 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100334 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100335 default:
336 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100337 return sprintf(buf, "%d\n",
338 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100339 }
340}
341
342static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
344{
345
346 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
347 struct i2c_client *client = to_i2c_client(dev);
348 struct adt7475_data *data = i2c_get_clientdata(client);
349 unsigned char reg;
350 long val;
351
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100352 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100353 return -EINVAL;
354
355 mutex_lock(&data->lock);
356
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100357 data->voltage[sattr->nr][sattr->index] =
358 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100359
Jean Delvare3d849982009-12-09 20:36:05 +0100360 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
361 if (sattr->nr == MIN)
362 reg = VOLTAGE_MIN_REG(sattr->index);
363 else
364 reg = VOLTAGE_MAX_REG(sattr->index);
365 } else {
366 if (sattr->nr == MIN)
367 reg = REG_VTT_MIN;
368 else
369 reg = REG_VTT_MAX;
370 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100371
372 i2c_smbus_write_byte_data(client, reg,
373 data->voltage[sattr->nr][sattr->index] >> 2);
374 mutex_unlock(&data->lock);
375
376 return count;
377}
378
379static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
380 char *buf)
381{
382 struct adt7475_data *data = adt7475_update_device(dev);
383 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
384 int out;
385
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900386 if (IS_ERR(data))
387 return PTR_ERR(data);
388
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100389 switch (sattr->nr) {
390 case HYSTERSIS:
391 mutex_lock(&data->lock);
392 out = data->temp[sattr->nr][sattr->index];
393 if (sattr->index != 1)
394 out = (out >> 4) & 0xF;
395 else
396 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800397 /*
398 * Show the value as an absolute number tied to
399 * THERM
400 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100401 out = reg2temp(data, data->temp[THERM][sattr->index]) -
402 out * 1000;
403 mutex_unlock(&data->lock);
404 break;
405
406 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800407 /*
408 * Offset is always 2's complement, regardless of the
409 * setting in CONFIG5
410 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100411 mutex_lock(&data->lock);
412 out = (s8)data->temp[sattr->nr][sattr->index];
413 if (data->config5 & CONFIG5_TEMPOFFSET)
414 out *= 1000;
415 else
416 out *= 500;
417 mutex_unlock(&data->lock);
418 break;
419
420 case ALARM:
421 out = (data->alarms >> (sattr->index + 4)) & 1;
422 break;
423
424 case FAULT:
425 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100426 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100427 break;
428
429 default:
430 /* All other temp values are in the configured format */
431 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
432 }
433
434 return sprintf(buf, "%d\n", out);
435}
436
437static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
439{
440 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
441 struct i2c_client *client = to_i2c_client(dev);
442 struct adt7475_data *data = i2c_get_clientdata(client);
443 unsigned char reg = 0;
444 u8 out;
445 int temp;
446 long val;
447
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100448 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100449 return -EINVAL;
450
451 mutex_lock(&data->lock);
452
453 /* We need the config register in all cases for temp <-> reg conv. */
454 data->config5 = adt7475_read(REG_CONFIG5);
455
456 switch (sattr->nr) {
457 case OFFSET:
458 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800459 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100460 out = data->temp[OFFSET][sattr->index] = val / 1000;
461 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800462 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100463 out = data->temp[OFFSET][sattr->index] = val / 500;
464 }
465 break;
466
467 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800468 /*
469 * The value will be given as an absolute value, turn it
470 * into an offset based on THERM
471 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100472
473 /* Read fresh THERM and HYSTERSIS values from the chip */
474 data->temp[THERM][sattr->index] =
475 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
476 adt7475_read_hystersis(client);
477
478 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800479 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100480 val = (temp - val) / 1000;
481
482 if (sattr->index != 1) {
483 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
484 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
485 } else {
486 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
487 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
488 }
489
490 out = data->temp[HYSTERSIS][sattr->index];
491 break;
492
493 default:
494 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
495
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800496 /*
497 * We maintain an extra 2 digits of precision for simplicity
498 * - shift those back off before writing the value
499 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100500 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
501 }
502
503 switch (sattr->nr) {
504 case MIN:
505 reg = TEMP_MIN_REG(sattr->index);
506 break;
507 case MAX:
508 reg = TEMP_MAX_REG(sattr->index);
509 break;
510 case OFFSET:
511 reg = TEMP_OFFSET_REG(sattr->index);
512 break;
513 case AUTOMIN:
514 reg = TEMP_TMIN_REG(sattr->index);
515 break;
516 case THERM:
517 reg = TEMP_THERM_REG(sattr->index);
518 break;
519 case HYSTERSIS:
520 if (sattr->index != 2)
521 reg = REG_REMOTE1_HYSTERSIS;
522 else
523 reg = REG_REMOTE2_HYSTERSIS;
524
525 break;
526 }
527
528 i2c_smbus_write_byte_data(client, reg, out);
529
530 mutex_unlock(&data->lock);
531 return count;
532}
533
Chris Packham8f05bcc2017-05-15 13:30:28 +1200534/* Assuming CONFIG6[SLOW] is 0 */
535static const int ad7475_st_map[] = {
536 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
537};
538
539static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr,
540 char *buf)
541{
542 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
543 struct i2c_client *client = to_i2c_client(dev);
544 struct adt7475_data *data = i2c_get_clientdata(client);
545 long val;
546
547 switch (sattr->index) {
548 case 0:
549 val = data->enh_acoustics[0] & 0xf;
550 break;
551 case 1:
552 val = (data->enh_acoustics[1] >> 4) & 0xf;
553 break;
554 case 2:
555 default:
556 val = data->enh_acoustics[1] & 0xf;
557 break;
558 }
559
560 if (val & 0x8)
561 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
562 else
563 return sprintf(buf, "0\n");
564}
565
566static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
568{
569 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
570 struct i2c_client *client = to_i2c_client(dev);
571 struct adt7475_data *data = i2c_get_clientdata(client);
572 unsigned char reg;
573 int shift, idx;
574 ulong val;
575
576 if (kstrtoul(buf, 10, &val))
577 return -EINVAL;
578
579 switch (sattr->index) {
580 case 0:
581 reg = REG_ENHANCE_ACOUSTICS1;
582 shift = 0;
583 idx = 0;
584 break;
585 case 1:
586 reg = REG_ENHANCE_ACOUSTICS2;
587 shift = 0;
588 idx = 1;
589 break;
590 case 2:
591 default:
592 reg = REG_ENHANCE_ACOUSTICS2;
593 shift = 4;
594 idx = 1;
595 break;
596 }
597
598 if (val > 0) {
599 val = find_closest_descending(val, ad7475_st_map,
600 ARRAY_SIZE(ad7475_st_map));
601 val |= 0x8;
602 }
603
604 mutex_lock(&data->lock);
605
606 data->enh_acoustics[idx] &= ~(0xf << shift);
607 data->enh_acoustics[idx] |= (val << shift);
608
609 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
610
611 mutex_unlock(&data->lock);
612
613 return count;
614}
615
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800616/*
617 * Table of autorange values - the user will write the value in millidegrees,
618 * and we'll convert it
619 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100620static const int autorange_table[] = {
621 2000, 2500, 3330, 4000, 5000, 6670, 8000,
622 10000, 13330, 16000, 20000, 26670, 32000, 40000,
623 53330, 80000
624};
625
626static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
627 char *buf)
628{
629 struct adt7475_data *data = adt7475_update_device(dev);
630 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
631 int out, val;
632
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900633 if (IS_ERR(data))
634 return PTR_ERR(data);
635
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100636 mutex_lock(&data->lock);
637 out = (data->range[sattr->index] >> 4) & 0x0F;
638 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
639 mutex_unlock(&data->lock);
640
641 return sprintf(buf, "%d\n", val + autorange_table[out]);
642}
643
644static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
645 const char *buf, size_t count)
646{
647 struct i2c_client *client = to_i2c_client(dev);
648 struct adt7475_data *data = i2c_get_clientdata(client);
649 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
650 int temp;
651 long val;
652
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100653 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100654 return -EINVAL;
655
656 mutex_lock(&data->lock);
657
658 /* Get a fresh copy of the needed registers */
659 data->config5 = adt7475_read(REG_CONFIG5);
660 data->temp[AUTOMIN][sattr->index] =
661 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
662 data->range[sattr->index] =
663 adt7475_read(TEMP_TRANGE_REG(sattr->index));
664
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800665 /*
666 * The user will write an absolute value, so subtract the start point
667 * to figure the range
668 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100669 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800670 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100671 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
672 val -= temp;
673
674 /* Find the nearest table entry to what the user wrote */
Chris Packhame4651642017-05-11 15:45:21 +1200675 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100676
677 data->range[sattr->index] &= ~0xF0;
678 data->range[sattr->index] |= val << 4;
679
680 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
681 data->range[sattr->index]);
682
683 mutex_unlock(&data->lock);
684 return count;
685}
686
687static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
688 char *buf)
689{
690 struct adt7475_data *data = adt7475_update_device(dev);
691 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
692 int out;
693
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900694 if (IS_ERR(data))
695 return PTR_ERR(data);
696
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100697 if (sattr->nr == ALARM)
698 out = (data->alarms >> (sattr->index + 10)) & 1;
699 else
700 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
701
702 return sprintf(buf, "%d\n", out);
703}
704
705static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
706 const char *buf, size_t count)
707{
708
709 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adt7475_data *data = i2c_get_clientdata(client);
712 unsigned long val;
713
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100714 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100715 return -EINVAL;
716
717 mutex_lock(&data->lock);
718
719 data->tach[MIN][sattr->index] = rpm2tach(val);
720
721 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
722 data->tach[MIN][sattr->index]);
723
724 mutex_unlock(&data->lock);
725 return count;
726}
727
728static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
729 char *buf)
730{
731 struct adt7475_data *data = adt7475_update_device(dev);
732 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
733
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900734 if (IS_ERR(data))
735 return PTR_ERR(data);
736
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100737 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
738}
739
740static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
741 char *buf)
742{
743 struct adt7475_data *data = adt7475_update_device(dev);
744 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
745
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900746 if (IS_ERR(data))
747 return PTR_ERR(data);
748
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100749 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
750}
751
752static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
753 char *buf)
754{
755 struct adt7475_data *data = adt7475_update_device(dev);
756 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
757
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900758 if (IS_ERR(data))
759 return PTR_ERR(data);
760
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100761 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
762}
763
764static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t count)
766{
767
768 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
769 struct i2c_client *client = to_i2c_client(dev);
770 struct adt7475_data *data = i2c_get_clientdata(client);
771 unsigned char reg = 0;
772 long val;
773
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100774 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100775 return -EINVAL;
776
777 mutex_lock(&data->lock);
778
779 switch (sattr->nr) {
780 case INPUT:
781 /* Get a fresh value for CONTROL */
782 data->pwm[CONTROL][sattr->index] =
783 adt7475_read(PWM_CONFIG_REG(sattr->index));
784
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800785 /*
786 * If we are not in manual mode, then we shouldn't allow
787 * the user to set the pwm speed
788 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100789 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
790 mutex_unlock(&data->lock);
791 return count;
792 }
793
794 reg = PWM_REG(sattr->index);
795 break;
796
797 case MIN:
798 reg = PWM_MIN_REG(sattr->index);
799 break;
800
801 case MAX:
802 reg = PWM_MAX_REG(sattr->index);
803 break;
804 }
805
Guenter Roeck2a844c12013-01-09 08:09:34 -0800806 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100807 i2c_smbus_write_byte_data(client, reg,
808 data->pwm[sattr->nr][sattr->index]);
Chris Packham1d58f5e2017-05-15 13:30:27 +1200809 mutex_unlock(&data->lock);
810
811 return count;
812}
813
814static ssize_t show_stall_disable(struct device *dev,
815 struct device_attribute *attr, char *buf)
816{
817 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
818 struct i2c_client *client = to_i2c_client(dev);
819 struct adt7475_data *data = i2c_get_clientdata(client);
820 u8 mask = BIT(5 + sattr->index);
821
822 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
823}
824
825static ssize_t set_stall_disable(struct device *dev,
826 struct device_attribute *attr, const char *buf,
827 size_t count)
828{
829 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
830 struct i2c_client *client = to_i2c_client(dev);
831 struct adt7475_data *data = i2c_get_clientdata(client);
832 long val;
833 u8 mask = BIT(5 + sattr->index);
834
835 if (kstrtol(buf, 10, &val))
836 return -EINVAL;
837
838 mutex_lock(&data->lock);
839
840 data->enh_acoustics[0] &= ~mask;
841 if (val)
842 data->enh_acoustics[0] |= mask;
843
844 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
845 data->enh_acoustics[0]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100846
847 mutex_unlock(&data->lock);
848
849 return count;
850}
851
852/* Called by set_pwmctrl and set_pwmchan */
853
854static int hw_set_pwm(struct i2c_client *client, int index,
855 unsigned int pwmctl, unsigned int pwmchan)
856{
857 struct adt7475_data *data = i2c_get_clientdata(client);
858 long val = 0;
859
860 switch (pwmctl) {
861 case 0:
862 val = 0x03; /* Run at full speed */
863 break;
864 case 1:
865 val = 0x07; /* Manual mode */
866 break;
867 case 2:
868 switch (pwmchan) {
869 case 1:
870 /* Remote1 controls PWM */
871 val = 0x00;
872 break;
873 case 2:
874 /* local controls PWM */
875 val = 0x01;
876 break;
877 case 4:
878 /* remote2 controls PWM */
879 val = 0x02;
880 break;
881 case 6:
882 /* local/remote2 control PWM */
883 val = 0x05;
884 break;
885 case 7:
886 /* All three control PWM */
887 val = 0x06;
888 break;
889 default:
890 return -EINVAL;
891 }
892 break;
893 default:
894 return -EINVAL;
895 }
896
897 data->pwmctl[index] = pwmctl;
898 data->pwmchan[index] = pwmchan;
899
900 data->pwm[CONTROL][index] &= ~0xE0;
901 data->pwm[CONTROL][index] |= (val & 7) << 5;
902
903 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
904 data->pwm[CONTROL][index]);
905
906 return 0;
907}
908
909static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
910 const char *buf, size_t count)
911{
912 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
913 struct i2c_client *client = to_i2c_client(dev);
914 struct adt7475_data *data = i2c_get_clientdata(client);
915 int r;
916 long val;
917
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100918 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100919 return -EINVAL;
920
921 mutex_lock(&data->lock);
922 /* Read Modify Write PWM values */
923 adt7475_read_pwm(client, sattr->index);
924 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
925 if (r)
926 count = r;
927 mutex_unlock(&data->lock);
928
929 return count;
930}
931
932static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
933 const char *buf, size_t count)
934{
935 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
936 struct i2c_client *client = to_i2c_client(dev);
937 struct adt7475_data *data = i2c_get_clientdata(client);
938 int r;
939 long val;
940
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100941 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100942 return -EINVAL;
943
944 mutex_lock(&data->lock);
945 /* Read Modify Write PWM values */
946 adt7475_read_pwm(client, sattr->index);
947 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
948 if (r)
949 count = r;
950 mutex_unlock(&data->lock);
951
952 return count;
953}
954
955/* List of frequencies for the PWM */
956static const int pwmfreq_table[] = {
Chris Packham3490c922017-05-15 13:30:29 +1200957 11, 14, 22, 29, 35, 44, 58, 88, 22500
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100958};
959
960static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
961 char *buf)
962{
963 struct adt7475_data *data = adt7475_update_device(dev);
964 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Dan Carpenter9d193712018-08-14 12:12:36 +0300965 int idx;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100966
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900967 if (IS_ERR(data))
968 return PTR_ERR(data);
Dan Carpenter9d193712018-08-14 12:12:36 +0300969 idx = clamp_val(data->range[sattr->index] & 0xf, 0,
970 ARRAY_SIZE(pwmfreq_table) - 1);
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900971
Dan Carpenter9d193712018-08-14 12:12:36 +0300972 return sprintf(buf, "%d\n", pwmfreq_table[idx]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100973}
974
975static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
976 const char *buf, size_t count)
977{
978 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
979 struct i2c_client *client = to_i2c_client(dev);
980 struct adt7475_data *data = i2c_get_clientdata(client);
981 int out;
982 long val;
983
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100984 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100985 return -EINVAL;
986
Chris Packhame4651642017-05-11 15:45:21 +1200987 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100988
989 mutex_lock(&data->lock);
990
991 data->range[sattr->index] =
992 adt7475_read(TEMP_TRANGE_REG(sattr->index));
Chris Packham3490c922017-05-15 13:30:29 +1200993 data->range[sattr->index] &= ~0xf;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100994 data->range[sattr->index] |= out;
995
996 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
997 data->range[sattr->index]);
998
999 mutex_unlock(&data->lock);
1000 return count;
1001}
1002
Julia Lawall1d05303c2016-12-22 13:05:33 +01001003static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1004 struct device_attribute *devattr,
1005 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +01001006{
1007 struct adt7475_data *data = adt7475_update_device(dev);
Dan Carpenter9d193712018-08-14 12:12:36 +03001008
1009 if (IS_ERR(data))
1010 return PTR_ERR(data);
1011
Jean Delvaref99318b2009-12-09 20:36:03 +01001012 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1013}
1014
Julia Lawall1d05303c2016-12-22 13:05:33 +01001015static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1016 struct device_attribute *devattr,
1017 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +01001018{
1019 struct i2c_client *client = to_i2c_client(dev);
1020 struct adt7475_data *data = i2c_get_clientdata(client);
1021 long val;
1022
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001023 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +01001024 return -EINVAL;
1025 if (val != 0 && val != 1)
1026 return -EINVAL;
1027
1028 mutex_lock(&data->lock);
1029 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1030 if (val)
1031 data->config4 |= CONFIG4_MAXDUTY;
1032 else
1033 data->config4 &= ~CONFIG4_MAXDUTY;
1034 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1035 mutex_unlock(&data->lock);
1036
1037 return count;
1038}
1039
Julia Lawall1d05303c2016-12-22 13:05:33 +01001040static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +01001041 char *buf)
1042{
1043 struct adt7475_data *data = dev_get_drvdata(dev);
1044 return sprintf(buf, "%d\n", (int)data->vrm);
1045}
1046
Julia Lawall1d05303c2016-12-22 13:05:33 +01001047static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1048 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +01001049{
1050 struct adt7475_data *data = dev_get_drvdata(dev);
1051 long val;
1052
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001053 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +01001054 return -EINVAL;
1055 if (val < 0 || val > 255)
1056 return -EINVAL;
1057 data->vrm = val;
1058
1059 return count;
1060}
1061
Julia Lawall1d05303c2016-12-22 13:05:33 +01001062static ssize_t cpu0_vid_show(struct device *dev,
1063 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +01001064{
1065 struct adt7475_data *data = adt7475_update_device(dev);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001066
1067 if (IS_ERR(data))
1068 return PTR_ERR(data);
1069
Jean Delvare54fe4672009-12-09 20:36:08 +01001070 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1071}
1072
Jean Delvare3d849982009-12-09 20:36:05 +01001073static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
1074static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
1075 set_voltage, MAX, 0);
1076static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
1077 set_voltage, MIN, 0);
1078static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001079static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001080static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001081 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001082static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001083 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001084static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
1085static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
1086static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
1087 set_voltage, MAX, 2);
1088static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
1089 set_voltage, MIN, 2);
1090static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +01001091static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
1092static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
1093 set_voltage, MAX, 3);
1094static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
1095 set_voltage, MIN, 3);
1096static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
1097static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
1098static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
1099 set_voltage, MAX, 4);
1100static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
1101 set_voltage, MIN, 4);
1102static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
1103static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
1104static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
1105 set_voltage, MAX, 5);
1106static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
1107 set_voltage, MIN, 5);
1108static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001109static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
1110static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
1111static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
1112static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1113 MAX, 0);
1114static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1115 MIN, 0);
1116static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1117 set_temp, OFFSET, 0);
1118static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
1119 show_temp, set_temp, AUTOMIN, 0);
1120static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
1121 show_point2, set_point2, 0, 0);
1122static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1123 THERM, 0);
1124static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1125 set_temp, HYSTERSIS, 0);
Chris Packham8f05bcc2017-05-15 13:30:28 +12001126static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1127 set_temp_st, 0, 0);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001128static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
1129static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
1130static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1131 MAX, 1);
1132static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1133 MIN, 1);
1134static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1135 set_temp, OFFSET, 1);
1136static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
1137 show_temp, set_temp, AUTOMIN, 1);
1138static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
1139 show_point2, set_point2, 0, 1);
1140static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1141 THERM, 1);
1142static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1143 set_temp, HYSTERSIS, 1);
Chris Packham8f05bcc2017-05-15 13:30:28 +12001144static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1145 set_temp_st, 0, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001146static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
1147static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
1148static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
1149static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1150 MAX, 2);
1151static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1152 MIN, 2);
1153static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1154 set_temp, OFFSET, 2);
1155static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1156 show_temp, set_temp, AUTOMIN, 2);
1157static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1158 show_point2, set_point2, 0, 2);
1159static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1160 THERM, 2);
1161static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1162 set_temp, HYSTERSIS, 2);
Chris Packham8f05bcc2017-05-15 13:30:28 +12001163static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1164 set_temp_st, 0, 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001165static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1166static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1167 MIN, 0);
1168static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1169static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1170static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1171 MIN, 1);
1172static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1173static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1174static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1175 MIN, 2);
1176static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1177static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1178static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1179 MIN, 3);
1180static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1181static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1182 0);
1183static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1184 set_pwmfreq, INPUT, 0);
1185static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1186 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001187static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001188 show_pwmchan, set_pwmchan, INPUT, 0);
1189static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1190 set_pwm, MIN, 0);
1191static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1192 set_pwm, MAX, 0);
Chris Packham1d58f5e2017-05-15 13:30:27 +12001193static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR,
1194 show_stall_disable, set_stall_disable, 0, 0);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001195static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1196 1);
1197static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1198 set_pwmfreq, INPUT, 1);
1199static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1200 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001201static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001202 show_pwmchan, set_pwmchan, INPUT, 1);
1203static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1204 set_pwm, MIN, 1);
1205static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1206 set_pwm, MAX, 1);
Chris Packham1d58f5e2017-05-15 13:30:27 +12001207static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR,
1208 show_stall_disable, set_stall_disable, 0, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001209static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1210 2);
1211static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1212 set_pwmfreq, INPUT, 2);
1213static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1214 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001215static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001216 show_pwmchan, set_pwmchan, INPUT, 2);
1217static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1218 set_pwm, MIN, 2);
1219static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1220 set_pwm, MAX, 2);
Chris Packham1d58f5e2017-05-15 13:30:27 +12001221static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR,
1222 show_stall_disable, set_stall_disable, 0, 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001223
Jean Delvaref99318b2009-12-09 20:36:03 +01001224/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001225static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001226
Julia Lawall1d05303c2016-12-22 13:05:33 +01001227static DEVICE_ATTR_RW(vrm);
1228static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001229
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001230static struct attribute *adt7475_attrs[] = {
1231 &sensor_dev_attr_in1_input.dev_attr.attr,
1232 &sensor_dev_attr_in1_max.dev_attr.attr,
1233 &sensor_dev_attr_in1_min.dev_attr.attr,
1234 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1235 &sensor_dev_attr_in2_input.dev_attr.attr,
1236 &sensor_dev_attr_in2_max.dev_attr.attr,
1237 &sensor_dev_attr_in2_min.dev_attr.attr,
1238 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1239 &sensor_dev_attr_temp1_input.dev_attr.attr,
1240 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1241 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1242 &sensor_dev_attr_temp1_max.dev_attr.attr,
1243 &sensor_dev_attr_temp1_min.dev_attr.attr,
1244 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1245 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1246 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1247 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1248 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001249 &sensor_dev_attr_temp1_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001250 &sensor_dev_attr_temp2_input.dev_attr.attr,
1251 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1252 &sensor_dev_attr_temp2_max.dev_attr.attr,
1253 &sensor_dev_attr_temp2_min.dev_attr.attr,
1254 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1255 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1256 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1257 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1258 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001259 &sensor_dev_attr_temp2_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001260 &sensor_dev_attr_temp3_input.dev_attr.attr,
1261 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1262 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1263 &sensor_dev_attr_temp3_max.dev_attr.attr,
1264 &sensor_dev_attr_temp3_min.dev_attr.attr,
1265 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1266 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1267 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1268 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1269 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001270 &sensor_dev_attr_temp3_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001271 &sensor_dev_attr_fan1_input.dev_attr.attr,
1272 &sensor_dev_attr_fan1_min.dev_attr.attr,
1273 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1274 &sensor_dev_attr_fan2_input.dev_attr.attr,
1275 &sensor_dev_attr_fan2_min.dev_attr.attr,
1276 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1277 &sensor_dev_attr_fan3_input.dev_attr.attr,
1278 &sensor_dev_attr_fan3_min.dev_attr.attr,
1279 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001280 &sensor_dev_attr_pwm1.dev_attr.attr,
1281 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1282 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001283 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001284 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1285 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001286 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001287 &sensor_dev_attr_pwm3.dev_attr.attr,
1288 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1289 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001290 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001291 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1292 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001293 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001294 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001295 NULL,
1296};
1297
Jean Delvare378933c2009-12-09 20:36:06 +01001298static struct attribute *fan4_attrs[] = {
1299 &sensor_dev_attr_fan4_input.dev_attr.attr,
1300 &sensor_dev_attr_fan4_min.dev_attr.attr,
1301 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1302 NULL
1303};
1304
1305static struct attribute *pwm2_attrs[] = {
1306 &sensor_dev_attr_pwm2.dev_attr.attr,
1307 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1308 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1309 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1310 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1311 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001312 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001313 NULL
1314};
1315
Jean Delvare378933c2009-12-09 20:36:06 +01001316static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001317 &sensor_dev_attr_in0_input.dev_attr.attr,
1318 &sensor_dev_attr_in0_max.dev_attr.attr,
1319 &sensor_dev_attr_in0_min.dev_attr.attr,
1320 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001321 NULL
1322};
1323
Jean Delvared8d2ee02009-12-09 20:36:08 +01001324static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001325 &sensor_dev_attr_in3_input.dev_attr.attr,
1326 &sensor_dev_attr_in3_max.dev_attr.attr,
1327 &sensor_dev_attr_in3_min.dev_attr.attr,
1328 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001329 NULL
1330};
1331
1332static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001333 &sensor_dev_attr_in4_input.dev_attr.attr,
1334 &sensor_dev_attr_in4_max.dev_attr.attr,
1335 &sensor_dev_attr_in4_min.dev_attr.attr,
1336 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001337 NULL
1338};
1339
1340static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001341 &sensor_dev_attr_in5_input.dev_attr.attr,
1342 &sensor_dev_attr_in5_max.dev_attr.attr,
1343 &sensor_dev_attr_in5_min.dev_attr.attr,
1344 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1345 NULL
1346};
1347
Jean Delvare54fe4672009-12-09 20:36:08 +01001348static struct attribute *vid_attrs[] = {
1349 &dev_attr_cpu0_vid.attr,
1350 &dev_attr_vrm.attr,
1351 NULL
1352};
1353
Arvind Yadavf5397be2017-07-05 11:04:04 +05301354static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1355static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1356static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1357static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1358static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1359static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1360static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1361static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001362
Jean Delvare310ec792009-12-14 21:17:23 +01001363static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001364 struct i2c_board_info *info)
1365{
1366 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001367 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001368 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001369
1370 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1371 return -ENODEV;
1372
Jean Delvareb180d052009-12-09 20:36:02 +01001373 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001374 devid2 = adt7475_read(REG_DEVID2);
1375 if (vendid != 0x41 || /* Analog Devices */
1376 (devid2 & 0xf8) != 0x68)
1377 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001378
Jean Delvared656b6f2009-12-09 20:36:04 +01001379 devid = adt7475_read(REG_DEVID);
1380 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001381 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001382 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001383 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001384 else if (devid == 0x76)
1385 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001386 else if ((devid2 & 0xfc) == 0x6c)
1387 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001388 else {
1389 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001390 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001391 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001392 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001393 }
1394
Jean Delvareb180d052009-12-09 20:36:02 +01001395 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001396
1397 return 0;
1398}
1399
Jean Delvare0f144802009-12-09 20:36:06 +01001400static void adt7475_remove_files(struct i2c_client *client,
1401 struct adt7475_data *data)
1402{
1403 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001404 if (data->has_fan4)
1405 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1406 if (data->has_pwm2)
1407 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1408 if (data->has_voltage & (1 << 0))
1409 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001410 if (data->has_voltage & (1 << 3))
1411 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1412 if (data->has_voltage & (1 << 4))
1413 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1414 if (data->has_voltage & (1 << 5))
1415 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001416 if (data->has_vid)
1417 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001418}
1419
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001420static int adt7475_update_limits(struct i2c_client *client)
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001421{
1422 struct adt7475_data *data = i2c_get_clientdata(client);
1423 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001424 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001425
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001426 ret = adt7475_read(REG_CONFIG4);
1427 if (ret < 0)
1428 return ret;
1429 data->config4 = ret;
1430
1431 ret = adt7475_read(REG_CONFIG5);
1432 if (ret < 0)
1433 return ret;
1434 data->config5 = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001435
1436 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1437 if (!(data->has_voltage & (1 << i)))
1438 continue;
1439 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001440 ret = adt7475_read(VOLTAGE_MIN_REG(i));
1441 if (ret < 0)
1442 return ret;
1443 data->voltage[MIN][i] = ret << 2;
1444
1445 ret = adt7475_read(VOLTAGE_MAX_REG(i));
1446 if (ret < 0)
1447 return ret;
1448 data->voltage[MAX][i] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001449 }
1450
1451 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001452 ret = adt7475_read(REG_VTT_MIN);
1453 if (ret < 0)
1454 return ret;
1455 data->voltage[MIN][5] = ret << 2;
1456
1457 ret = adt7475_read(REG_VTT_MAX);
1458 if (ret < 0)
1459 return ret;
1460 data->voltage[MAX][5] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001461 }
1462
1463 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1464 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001465 ret = adt7475_read(TEMP_MIN_REG(i));
1466 if (ret < 0)
1467 return ret;
1468 data->temp[MIN][i] = ret << 2;
1469
1470 ret = adt7475_read(TEMP_MAX_REG(i));
1471 if (ret < 0)
1472 return ret;
1473 data->temp[MAX][i] = ret << 2;
1474
1475 ret = adt7475_read(TEMP_TMIN_REG(i));
1476 if (ret < 0)
1477 return ret;
1478 data->temp[AUTOMIN][i] = ret << 2;
1479
1480 ret = adt7475_read(TEMP_THERM_REG(i));
1481 if (ret < 0)
1482 return ret;
1483 data->temp[THERM][i] = ret << 2;
1484
1485 ret = adt7475_read(TEMP_OFFSET_REG(i));
1486 if (ret < 0)
1487 return ret;
1488 data->temp[OFFSET][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001489 }
1490 adt7475_read_hystersis(client);
1491
1492 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1493 if (i == 3 && !data->has_fan4)
1494 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001495 ret = adt7475_read_word(client, TACH_MIN_REG(i));
1496 if (ret < 0)
1497 return ret;
1498 data->tach[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001499 }
1500
1501 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1502 if (i == 1 && !data->has_pwm2)
1503 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001504 ret = adt7475_read(PWM_MAX_REG(i));
1505 if (ret < 0)
1506 return ret;
1507 data->pwm[MAX][i] = ret;
1508
1509 ret = adt7475_read(PWM_MIN_REG(i));
1510 if (ret < 0)
1511 return ret;
1512 data->pwm[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001513 /* Set the channel and control information */
1514 adt7475_read_pwm(client, i);
1515 }
1516
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001517 ret = adt7475_read(TEMP_TRANGE_REG(0));
1518 if (ret < 0)
1519 return ret;
1520 data->range[0] = ret;
1521
1522 ret = adt7475_read(TEMP_TRANGE_REG(1));
1523 if (ret < 0)
1524 return ret;
1525 data->range[1] = ret;
1526
1527 ret = adt7475_read(TEMP_TRANGE_REG(2));
1528 if (ret < 0)
1529 return ret;
1530 data->range[2] = ret;
1531
1532 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001533}
1534
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001535static int adt7475_probe(struct i2c_client *client,
1536 const struct i2c_device_id *id)
1537{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001538 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001539 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001540 [adt7473] = "ADT7473",
1541 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001542 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001543 [adt7490] = "ADT7490",
1544 };
1545
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001546 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001547 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001548 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001549
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001550 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001551 if (data == NULL)
1552 return -ENOMEM;
1553
1554 mutex_init(&data->lock);
1555 i2c_set_clientdata(client, data);
1556
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001557 if (client->dev.of_node)
1558 chip = (enum chips)of_device_get_match_data(&client->dev);
1559 else
1560 chip = id->driver_data;
1561
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001562 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001563 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001564 case adt7476:
1565 data->has_voltage = 0x0e; /* in1 to in3 */
1566 revision = adt7475_read(REG_DEVID2) & 0x07;
1567 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001568 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001569 data->has_voltage = 0x3e; /* in1 to in5 */
1570 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001571 if (revision == 0x03)
1572 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001573 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001574 default:
1575 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001576 revision = adt7475_read(REG_DEVID2) & 0x07;
1577 }
1578
1579 config3 = adt7475_read(REG_CONFIG3);
1580 /* Pin PWM2 may alternatively be used for ALERT output */
1581 if (!(config3 & CONFIG3_SMBALERT))
1582 data->has_pwm2 = 1;
1583 /* Meaning of this bit is inverted for the ADT7473-1 */
1584 if (id->driver_data == adt7473 && revision >= 1)
1585 data->has_pwm2 = !data->has_pwm2;
1586
1587 data->config4 = adt7475_read(REG_CONFIG4);
1588 /* Pin TACH4 may alternatively be used for THERM */
1589 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1590 data->has_fan4 = 1;
1591
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001592 /*
1593 * THERM configuration is more complex on the ADT7476 and ADT7490,
1594 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1595 * this function
1596 */
Jean Delvare378933c2009-12-09 20:36:06 +01001597 if (id->driver_data == adt7490) {
1598 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1599 !(config3 & CONFIG3_THERM))
1600 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001601 }
1602 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001603 if (!(config3 & CONFIG3_THERM) ||
1604 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1605 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001606 }
1607
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001608 /*
1609 * On the ADT7476, the +12V input pin may instead be used as VID5,
1610 * and VID pins may alternatively be used as GPIO
1611 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001612 if (id->driver_data == adt7476) {
1613 u8 vid = adt7475_read(REG_VID);
1614 if (!(vid & VID_VIDSEL))
1615 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001616
1617 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001618 }
1619
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001620 /* Voltage attenuators can be bypassed, globally or individually */
1621 config2 = adt7475_read(REG_CONFIG2);
1622 if (config2 & CONFIG2_ATTN) {
1623 data->bypass_attn = (0x3 << 3) | 0x3;
1624 } else {
1625 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1626 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1627 }
1628 data->bypass_attn &= data->has_voltage;
1629
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001630 /*
1631 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1632 * pwm's which are disabled to manual mode with 0% duty cycle
1633 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001634 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1635 adt7475_read_pwm(client, i);
1636
Chris Packham4abdf382017-04-22 07:08:09 +12001637 /* Start monitoring */
1638 switch (chip) {
1639 case adt7475:
1640 case adt7476:
1641 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1642 adt7475_read(REG_CONFIG1) | 0x01);
1643 break;
1644 default:
1645 break;
1646 }
1647
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001648 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1649 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001650 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001651
Jean Delvare378933c2009-12-09 20:36:06 +01001652 /* Features that can be disabled individually */
1653 if (data->has_fan4) {
1654 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1655 if (ret)
1656 goto eremove;
1657 }
1658 if (data->has_pwm2) {
1659 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1660 if (ret)
1661 goto eremove;
1662 }
1663 if (data->has_voltage & (1 << 0)) {
1664 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1665 if (ret)
1666 goto eremove;
1667 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001668 if (data->has_voltage & (1 << 3)) {
1669 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1670 if (ret)
1671 goto eremove;
1672 }
1673 if (data->has_voltage & (1 << 4)) {
1674 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1675 if (ret)
1676 goto eremove;
1677 }
1678 if (data->has_voltage & (1 << 5)) {
1679 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1680 if (ret)
1681 goto eremove;
1682 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001683 if (data->has_vid) {
1684 data->vrm = vid_which_vrm();
1685 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1686 if (ret)
1687 goto eremove;
1688 }
Jean Delvare378933c2009-12-09 20:36:06 +01001689
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001690 data->hwmon_dev = hwmon_device_register(&client->dev);
1691 if (IS_ERR(data->hwmon_dev)) {
1692 ret = PTR_ERR(data->hwmon_dev);
1693 goto eremove;
1694 }
1695
Jean Delvared07ca4a2009-12-09 20:36:07 +01001696 dev_info(&client->dev, "%s device, revision %d\n",
1697 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001698 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001699 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001700 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001701 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001702 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001703 data->has_pwm2 ? " pwm2" : "",
1704 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001705 if (data->bypass_attn)
1706 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1707 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1708 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1709 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1710 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001711
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001712 /* Limits and settings, should never change update more than once */
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001713 ret = adt7475_update_limits(client);
1714 if (ret)
1715 goto eremove;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001716
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001717 return 0;
1718
1719eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001720 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001721 return ret;
1722}
1723
1724static int adt7475_remove(struct i2c_client *client)
1725{
1726 struct adt7475_data *data = i2c_get_clientdata(client);
1727
1728 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001729 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001730
1731 return 0;
1732}
1733
1734static struct i2c_driver adt7475_driver = {
1735 .class = I2C_CLASS_HWMON,
1736 .driver = {
1737 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001738 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001739 },
1740 .probe = adt7475_probe,
1741 .remove = adt7475_remove,
1742 .id_table = adt7475_id,
1743 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001744 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001745};
1746
1747static void adt7475_read_hystersis(struct i2c_client *client)
1748{
1749 struct adt7475_data *data = i2c_get_clientdata(client);
1750
1751 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1752 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1753 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1754}
1755
1756static void adt7475_read_pwm(struct i2c_client *client, int index)
1757{
1758 struct adt7475_data *data = i2c_get_clientdata(client);
1759 unsigned int v;
1760
1761 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1762
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001763 /*
1764 * Figure out the internal value for pwmctrl and pwmchan
1765 * based on the current settings
1766 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001767 v = (data->pwm[CONTROL][index] >> 5) & 7;
1768
1769 if (v == 3)
1770 data->pwmctl[index] = 0;
1771 else if (v == 7)
1772 data->pwmctl[index] = 1;
1773 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001774 /*
1775 * The fan is disabled - we don't want to
1776 * support that, so change to manual mode and
1777 * set the duty cycle to 0 instead
1778 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001779 data->pwm[INPUT][index] = 0;
1780 data->pwm[CONTROL][index] &= ~0xE0;
1781 data->pwm[CONTROL][index] |= (7 << 5);
1782
1783 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1784 data->pwm[INPUT][index]);
1785
1786 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1787 data->pwm[CONTROL][index]);
1788
1789 data->pwmctl[index] = 1;
1790 } else {
1791 data->pwmctl[index] = 2;
1792
1793 switch (v) {
1794 case 0:
1795 data->pwmchan[index] = 1;
1796 break;
1797 case 1:
1798 data->pwmchan[index] = 2;
1799 break;
1800 case 2:
1801 data->pwmchan[index] = 4;
1802 break;
1803 case 5:
1804 data->pwmchan[index] = 6;
1805 break;
1806 case 6:
1807 data->pwmchan[index] = 7;
1808 break;
1809 }
1810 }
1811}
1812
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001813static int adt7475_update_measure(struct device *dev)
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001814{
1815 struct i2c_client *client = to_i2c_client(dev);
1816 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001817 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001818 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001819 int ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001820
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001821 ret = adt7475_read(REG_STATUS2);
1822 if (ret < 0)
1823 return ret;
1824 data->alarms = ret << 8;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001825
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001826 ret = adt7475_read(REG_STATUS1);
1827 if (ret < 0)
1828 return ret;
1829 data->alarms |= ret;
1830
1831 ret = adt7475_read(REG_EXTEND2);
1832 if (ret < 0)
1833 return ret;
1834
1835 ext = (ret << 8);
1836
1837 ret = adt7475_read(REG_EXTEND1);
1838 if (ret < 0)
1839 return ret;
1840
1841 ext |= ret;
1842
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001843 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1844 if (!(data->has_voltage & (1 << i)))
1845 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001846 ret = adt7475_read(VOLTAGE_REG(i));
1847 if (ret < 0)
1848 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001849 data->voltage[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001850 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001851 ((ext >> (i * 2)) & 3);
1852 }
1853
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001854 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1855 ret = adt7475_read(TEMP_REG(i));
1856 if (ret < 0)
1857 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001858 data->temp[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001859 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001860 ((ext >> ((i + 5) * 2)) & 3);
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001861 }
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001862
1863 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001864 ret = adt7475_read(REG_STATUS4);
1865 if (ret < 0)
1866 return ret;
1867 data->alarms |= ret << 24;
1868
1869 ret = adt7475_read(REG_EXTEND3);
1870 if (ret < 0)
1871 return ret;
1872 ext = ret;
1873
1874 ret = adt7475_read(REG_VTT);
1875 if (ret < 0)
1876 return ret;
1877 data->voltage[INPUT][5] = ret << 2 |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001878 ((ext >> 4) & 3);
1879 }
1880
1881 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1882 if (i == 3 && !data->has_fan4)
1883 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001884 ret = adt7475_read_word(client, TACH_REG(i));
1885 if (ret < 0)
1886 return ret;
1887 data->tach[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001888 }
1889
1890 /* Updated by hw when in auto mode */
1891 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1892 if (i == 1 && !data->has_pwm2)
1893 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001894 ret = adt7475_read(PWM_REG(i));
1895 if (ret < 0)
1896 return ret;
1897 data->pwm[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001898 }
1899
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001900 if (data->has_vid) {
1901 ret = adt7475_read(REG_VID);
1902 if (ret < 0)
1903 return ret;
1904 data->vid = ret & 0x3f;
1905 }
1906
1907 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001908}
1909
1910static struct adt7475_data *adt7475_update_device(struct device *dev)
1911{
1912 struct i2c_client *client = to_i2c_client(dev);
1913 struct adt7475_data *data = i2c_get_clientdata(client);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001914 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001915
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001916 mutex_lock(&data->lock);
1917
1918 /* Measurement values update every 2 seconds */
1919 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1920 !data->valid) {
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001921 ret = adt7475_update_measure(dev);
1922 if (ret) {
1923 data->valid = false;
1924 mutex_unlock(&data->lock);
1925 return ERR_PTR(ret);
1926 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001927 data->measure_updated = jiffies;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +09001928 data->valid = true;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001929 }
1930
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001931 mutex_unlock(&data->lock);
1932
1933 return data;
1934}
1935
Axel Linf0967ee2012-01-20 15:38:18 +08001936module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001937
1938MODULE_AUTHOR("Advanced Micro Devices, Inc");
1939MODULE_DESCRIPTION("adt7475 driver");
1940MODULE_LICENSE("GPL");