blob: ec0c43fbcdcee7f2804fd2514f6b4752f04f6415 [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 Delvare7c81c60f2014-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
82#define REG_PWM_MIN_BASE 0x64
83
84#define REG_TEMP_TMIN_BASE 0x67
85#define REG_TEMP_THERM_BASE 0x6A
86
87#define REG_REMOTE1_HYSTERSIS 0x6D
88#define REG_REMOTE2_HYSTERSIS 0x6E
89
90#define REG_TEMP_OFFSET_BASE 0x70
91
Jean Delvareebfaf1f2009-12-09 20:36:07 +010092#define REG_CONFIG2 0x73
93
Jordan Crouse1c301fc2009-01-15 22:27:47 +010094#define REG_EXTEND1 0x76
95#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010096
97#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010098#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010099#define REG_CONFIG4 0x7D
100
Jean Delvare3d849982009-12-09 20:36:05 +0100101#define REG_STATUS4 0x81 /* ADT7490 only */
102
103#define REG_VTT_MIN 0x84 /* ADT7490 only */
104#define REG_VTT_MAX 0x86 /* ADT7490 only */
105
Jean Delvared8d2ee02009-12-09 20:36:08 +0100106#define VID_VIDSEL 0x80 /* ADT7476 only */
107
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100108#define CONFIG2_ATTN 0x20
109
Jean Delvare378933c2009-12-09 20:36:06 +0100110#define CONFIG3_SMBALERT 0x01
111#define CONFIG3_THERM 0x02
112
113#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100114#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100115#define CONFIG4_ATTN_IN10 0x30
116#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100117
118#define CONFIG5_TWOSCOMP 0x01
119#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100120#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100121
122/* ADT7475 Settings */
123
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100124#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100125#define ADT7475_TEMP_COUNT 3
126#define ADT7475_TACH_COUNT 4
127#define ADT7475_PWM_COUNT 3
128
129/* Macro to read the registers */
130
131#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
132
133/* Macros to easily index the registers */
134
135#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
136#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
137
138#define PWM_REG(idx) (REG_PWM_BASE + (idx))
139#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
140#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
141#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
142
143#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
144#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
145#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
146
147#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
148#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
149#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
150#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
151#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
152#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
153#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
154
Jean Delvare918ee912010-10-28 20:31:50 +0200155static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100156
Jean Delvaree5e9f442009-12-14 21:17:27 +0100157enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100158
159static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100160 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100161 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100162 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100163 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100164 { }
165};
166MODULE_DEVICE_TABLE(i2c, adt7475_id);
167
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300168static const struct of_device_id adt7475_of_match[] = {
169 {
170 .compatible = "adi,adt7473",
171 .data = (void *)adt7473
172 },
173 {
174 .compatible = "adi,adt7475",
175 .data = (void *)adt7475
176 },
177 {
178 .compatible = "adi,adt7476",
179 .data = (void *)adt7476
180 },
181 {
182 .compatible = "adi,adt7490",
183 .data = (void *)adt7490
184 },
185 { },
186};
187MODULE_DEVICE_TABLE(of, adt7475_of_match);
188
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100189struct adt7475_data {
190 struct device *hwmon_dev;
191 struct mutex lock;
192
193 unsigned long measure_updated;
194 unsigned long limits_updated;
195 char valid;
196
Jean Delvaref99318b2009-12-09 20:36:03 +0100197 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100198 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100199 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100200 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100201 u8 has_pwm2:1;
202 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100203 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100204 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100205 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100206 u16 temp[7][3];
207 u16 tach[2][4];
208 u8 pwm[4][3];
209 u8 range[3];
210 u8 pwmctl[3];
211 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100212
213 u8 vid;
214 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100215};
216
217static struct i2c_driver adt7475_driver;
218static struct adt7475_data *adt7475_update_device(struct device *dev);
219static void adt7475_read_hystersis(struct i2c_client *client);
220static void adt7475_read_pwm(struct i2c_client *client, int index);
221
222/* Given a temp value, convert it to register value */
223
224static inline u16 temp2reg(struct adt7475_data *data, long val)
225{
226 u16 ret;
227
228 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800229 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100230 ret = (val + 64500) / 1000;
231 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800232 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100233 if (val < -500)
234 ret = (256500 + val) / 1000;
235 else
236 ret = (val + 500) / 1000;
237 }
238
239 return ret << 2;
240}
241
242/* Given a register value, convert it to a real temp value */
243
244static inline int reg2temp(struct adt7475_data *data, u16 reg)
245{
246 if (data->config5 & CONFIG5_TWOSCOMP) {
247 if (reg >= 512)
248 return (reg - 1024) * 250;
249 else
250 return reg * 250;
251 } else
252 return (reg - 256) * 250;
253}
254
255static inline int tach2rpm(u16 tach)
256{
257 if (tach == 0 || tach == 0xFFFF)
258 return 0;
259
260 return (90000 * 60) / tach;
261}
262
263static inline u16 rpm2tach(unsigned long rpm)
264{
265 if (rpm == 0)
266 return 0;
267
Guenter Roeck2a844c12013-01-09 08:09:34 -0800268 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100269}
270
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100271/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
272static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
273 { 45, 94 }, /* +2.5V */
274 { 175, 525 }, /* Vccp */
275 { 68, 71 }, /* Vcc */
276 { 93, 47 }, /* +5V */
277 { 120, 20 }, /* +12V */
278 { 45, 45 }, /* Vtt */
279};
280
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100281static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100282{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100283 const int *r = adt7473_in_scaling[channel];
284
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100285 if (bypass_attn & (1 << channel))
286 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100287 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100288}
289
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100290static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100291{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100292 const int *r = adt7473_in_scaling[channel];
293 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100294
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100295 if (bypass_attn & (1 << channel))
296 reg = (volt * 1024) / 2250;
297 else
298 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800299 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100300}
301
302static u16 adt7475_read_word(struct i2c_client *client, int reg)
303{
304 u16 val;
305
306 val = i2c_smbus_read_byte_data(client, reg);
307 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
308
309 return val;
310}
311
312static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
313{
314 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
315 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
316}
317
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100318static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
319 char *buf)
320{
321 struct adt7475_data *data = adt7475_update_device(dev);
322 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
323 unsigned short val;
324
325 switch (sattr->nr) {
326 case ALARM:
327 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100328 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100329 default:
330 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100331 return sprintf(buf, "%d\n",
332 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100333 }
334}
335
336static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
338{
339
340 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
341 struct i2c_client *client = to_i2c_client(dev);
342 struct adt7475_data *data = i2c_get_clientdata(client);
343 unsigned char reg;
344 long val;
345
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100346 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100347 return -EINVAL;
348
349 mutex_lock(&data->lock);
350
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100351 data->voltage[sattr->nr][sattr->index] =
352 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100353
Jean Delvare3d849982009-12-09 20:36:05 +0100354 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
355 if (sattr->nr == MIN)
356 reg = VOLTAGE_MIN_REG(sattr->index);
357 else
358 reg = VOLTAGE_MAX_REG(sattr->index);
359 } else {
360 if (sattr->nr == MIN)
361 reg = REG_VTT_MIN;
362 else
363 reg = REG_VTT_MAX;
364 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100365
366 i2c_smbus_write_byte_data(client, reg,
367 data->voltage[sattr->nr][sattr->index] >> 2);
368 mutex_unlock(&data->lock);
369
370 return count;
371}
372
373static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
374 char *buf)
375{
376 struct adt7475_data *data = adt7475_update_device(dev);
377 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
378 int out;
379
380 switch (sattr->nr) {
381 case HYSTERSIS:
382 mutex_lock(&data->lock);
383 out = data->temp[sattr->nr][sattr->index];
384 if (sattr->index != 1)
385 out = (out >> 4) & 0xF;
386 else
387 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800388 /*
389 * Show the value as an absolute number tied to
390 * THERM
391 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100392 out = reg2temp(data, data->temp[THERM][sattr->index]) -
393 out * 1000;
394 mutex_unlock(&data->lock);
395 break;
396
397 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800398 /*
399 * Offset is always 2's complement, regardless of the
400 * setting in CONFIG5
401 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100402 mutex_lock(&data->lock);
403 out = (s8)data->temp[sattr->nr][sattr->index];
404 if (data->config5 & CONFIG5_TEMPOFFSET)
405 out *= 1000;
406 else
407 out *= 500;
408 mutex_unlock(&data->lock);
409 break;
410
411 case ALARM:
412 out = (data->alarms >> (sattr->index + 4)) & 1;
413 break;
414
415 case FAULT:
416 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100417 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100418 break;
419
420 default:
421 /* All other temp values are in the configured format */
422 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
423 }
424
425 return sprintf(buf, "%d\n", out);
426}
427
428static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
429 const char *buf, size_t count)
430{
431 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
432 struct i2c_client *client = to_i2c_client(dev);
433 struct adt7475_data *data = i2c_get_clientdata(client);
434 unsigned char reg = 0;
435 u8 out;
436 int temp;
437 long val;
438
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100439 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100440 return -EINVAL;
441
442 mutex_lock(&data->lock);
443
444 /* We need the config register in all cases for temp <-> reg conv. */
445 data->config5 = adt7475_read(REG_CONFIG5);
446
447 switch (sattr->nr) {
448 case OFFSET:
449 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800450 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100451 out = data->temp[OFFSET][sattr->index] = val / 1000;
452 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800453 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100454 out = data->temp[OFFSET][sattr->index] = val / 500;
455 }
456 break;
457
458 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800459 /*
460 * The value will be given as an absolute value, turn it
461 * into an offset based on THERM
462 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100463
464 /* Read fresh THERM and HYSTERSIS values from the chip */
465 data->temp[THERM][sattr->index] =
466 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
467 adt7475_read_hystersis(client);
468
469 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800470 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100471 val = (temp - val) / 1000;
472
473 if (sattr->index != 1) {
474 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
475 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
476 } else {
477 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
478 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
479 }
480
481 out = data->temp[HYSTERSIS][sattr->index];
482 break;
483
484 default:
485 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
486
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800487 /*
488 * We maintain an extra 2 digits of precision for simplicity
489 * - shift those back off before writing the value
490 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100491 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
492 }
493
494 switch (sattr->nr) {
495 case MIN:
496 reg = TEMP_MIN_REG(sattr->index);
497 break;
498 case MAX:
499 reg = TEMP_MAX_REG(sattr->index);
500 break;
501 case OFFSET:
502 reg = TEMP_OFFSET_REG(sattr->index);
503 break;
504 case AUTOMIN:
505 reg = TEMP_TMIN_REG(sattr->index);
506 break;
507 case THERM:
508 reg = TEMP_THERM_REG(sattr->index);
509 break;
510 case HYSTERSIS:
511 if (sattr->index != 2)
512 reg = REG_REMOTE1_HYSTERSIS;
513 else
514 reg = REG_REMOTE2_HYSTERSIS;
515
516 break;
517 }
518
519 i2c_smbus_write_byte_data(client, reg, out);
520
521 mutex_unlock(&data->lock);
522 return count;
523}
524
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800525/*
526 * Table of autorange values - the user will write the value in millidegrees,
527 * and we'll convert it
528 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100529static const int autorange_table[] = {
530 2000, 2500, 3330, 4000, 5000, 6670, 8000,
531 10000, 13330, 16000, 20000, 26670, 32000, 40000,
532 53330, 80000
533};
534
535static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
536 char *buf)
537{
538 struct adt7475_data *data = adt7475_update_device(dev);
539 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
540 int out, val;
541
542 mutex_lock(&data->lock);
543 out = (data->range[sattr->index] >> 4) & 0x0F;
544 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
545 mutex_unlock(&data->lock);
546
547 return sprintf(buf, "%d\n", val + autorange_table[out]);
548}
549
550static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
551 const char *buf, size_t count)
552{
553 struct i2c_client *client = to_i2c_client(dev);
554 struct adt7475_data *data = i2c_get_clientdata(client);
555 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
556 int temp;
557 long val;
558
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100559 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100560 return -EINVAL;
561
562 mutex_lock(&data->lock);
563
564 /* Get a fresh copy of the needed registers */
565 data->config5 = adt7475_read(REG_CONFIG5);
566 data->temp[AUTOMIN][sattr->index] =
567 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
568 data->range[sattr->index] =
569 adt7475_read(TEMP_TRANGE_REG(sattr->index));
570
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800571 /*
572 * The user will write an absolute value, so subtract the start point
573 * to figure the range
574 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100575 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800576 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100577 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
578 val -= temp;
579
580 /* Find the nearest table entry to what the user wrote */
Chris Packhame4651642017-05-11 15:45:21 +1200581 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100582
583 data->range[sattr->index] &= ~0xF0;
584 data->range[sattr->index] |= val << 4;
585
586 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
587 data->range[sattr->index]);
588
589 mutex_unlock(&data->lock);
590 return count;
591}
592
593static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
594 char *buf)
595{
596 struct adt7475_data *data = adt7475_update_device(dev);
597 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
598 int out;
599
600 if (sattr->nr == ALARM)
601 out = (data->alarms >> (sattr->index + 10)) & 1;
602 else
603 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
604
605 return sprintf(buf, "%d\n", out);
606}
607
608static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
610{
611
612 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
613 struct i2c_client *client = to_i2c_client(dev);
614 struct adt7475_data *data = i2c_get_clientdata(client);
615 unsigned long val;
616
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100617 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100618 return -EINVAL;
619
620 mutex_lock(&data->lock);
621
622 data->tach[MIN][sattr->index] = rpm2tach(val);
623
624 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
625 data->tach[MIN][sattr->index]);
626
627 mutex_unlock(&data->lock);
628 return count;
629}
630
631static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
632 char *buf)
633{
634 struct adt7475_data *data = adt7475_update_device(dev);
635 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
636
637 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
638}
639
640static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
641 char *buf)
642{
643 struct adt7475_data *data = adt7475_update_device(dev);
644 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
645
646 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
647}
648
649static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
650 char *buf)
651{
652 struct adt7475_data *data = adt7475_update_device(dev);
653 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
654
655 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
656}
657
658static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
660{
661
662 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
663 struct i2c_client *client = to_i2c_client(dev);
664 struct adt7475_data *data = i2c_get_clientdata(client);
665 unsigned char reg = 0;
666 long val;
667
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100668 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100669 return -EINVAL;
670
671 mutex_lock(&data->lock);
672
673 switch (sattr->nr) {
674 case INPUT:
675 /* Get a fresh value for CONTROL */
676 data->pwm[CONTROL][sattr->index] =
677 adt7475_read(PWM_CONFIG_REG(sattr->index));
678
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800679 /*
680 * If we are not in manual mode, then we shouldn't allow
681 * the user to set the pwm speed
682 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100683 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
684 mutex_unlock(&data->lock);
685 return count;
686 }
687
688 reg = PWM_REG(sattr->index);
689 break;
690
691 case MIN:
692 reg = PWM_MIN_REG(sattr->index);
693 break;
694
695 case MAX:
696 reg = PWM_MAX_REG(sattr->index);
697 break;
698 }
699
Guenter Roeck2a844c12013-01-09 08:09:34 -0800700 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100701 i2c_smbus_write_byte_data(client, reg,
702 data->pwm[sattr->nr][sattr->index]);
703
704 mutex_unlock(&data->lock);
705
706 return count;
707}
708
709/* Called by set_pwmctrl and set_pwmchan */
710
711static int hw_set_pwm(struct i2c_client *client, int index,
712 unsigned int pwmctl, unsigned int pwmchan)
713{
714 struct adt7475_data *data = i2c_get_clientdata(client);
715 long val = 0;
716
717 switch (pwmctl) {
718 case 0:
719 val = 0x03; /* Run at full speed */
720 break;
721 case 1:
722 val = 0x07; /* Manual mode */
723 break;
724 case 2:
725 switch (pwmchan) {
726 case 1:
727 /* Remote1 controls PWM */
728 val = 0x00;
729 break;
730 case 2:
731 /* local controls PWM */
732 val = 0x01;
733 break;
734 case 4:
735 /* remote2 controls PWM */
736 val = 0x02;
737 break;
738 case 6:
739 /* local/remote2 control PWM */
740 val = 0x05;
741 break;
742 case 7:
743 /* All three control PWM */
744 val = 0x06;
745 break;
746 default:
747 return -EINVAL;
748 }
749 break;
750 default:
751 return -EINVAL;
752 }
753
754 data->pwmctl[index] = pwmctl;
755 data->pwmchan[index] = pwmchan;
756
757 data->pwm[CONTROL][index] &= ~0xE0;
758 data->pwm[CONTROL][index] |= (val & 7) << 5;
759
760 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
761 data->pwm[CONTROL][index]);
762
763 return 0;
764}
765
766static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
768{
769 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
770 struct i2c_client *client = to_i2c_client(dev);
771 struct adt7475_data *data = i2c_get_clientdata(client);
772 int r;
773 long val;
774
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100775 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100776 return -EINVAL;
777
778 mutex_lock(&data->lock);
779 /* Read Modify Write PWM values */
780 adt7475_read_pwm(client, sattr->index);
781 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
782 if (r)
783 count = r;
784 mutex_unlock(&data->lock);
785
786 return count;
787}
788
789static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
790 const char *buf, size_t count)
791{
792 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
793 struct i2c_client *client = to_i2c_client(dev);
794 struct adt7475_data *data = i2c_get_clientdata(client);
795 int r;
796 long val;
797
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100798 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100799 return -EINVAL;
800
801 mutex_lock(&data->lock);
802 /* Read Modify Write PWM values */
803 adt7475_read_pwm(client, sattr->index);
804 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
805 if (r)
806 count = r;
807 mutex_unlock(&data->lock);
808
809 return count;
810}
811
812/* List of frequencies for the PWM */
813static const int pwmfreq_table[] = {
814 11, 14, 22, 29, 35, 44, 58, 88
815};
816
817static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
818 char *buf)
819{
820 struct adt7475_data *data = adt7475_update_device(dev);
821 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
822
823 return sprintf(buf, "%d\n",
824 pwmfreq_table[data->range[sattr->index] & 7]);
825}
826
827static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
828 const char *buf, size_t count)
829{
830 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
831 struct i2c_client *client = to_i2c_client(dev);
832 struct adt7475_data *data = i2c_get_clientdata(client);
833 int out;
834 long val;
835
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100836 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100837 return -EINVAL;
838
Chris Packhame4651642017-05-11 15:45:21 +1200839 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100840
841 mutex_lock(&data->lock);
842
843 data->range[sattr->index] =
844 adt7475_read(TEMP_TRANGE_REG(sattr->index));
845 data->range[sattr->index] &= ~7;
846 data->range[sattr->index] |= out;
847
848 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
849 data->range[sattr->index]);
850
851 mutex_unlock(&data->lock);
852 return count;
853}
854
Julia Lawall1d05303c2016-12-22 13:05:33 +0100855static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
856 struct device_attribute *devattr,
857 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +0100858{
859 struct adt7475_data *data = adt7475_update_device(dev);
860 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
861}
862
Julia Lawall1d05303c2016-12-22 13:05:33 +0100863static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
864 struct device_attribute *devattr,
865 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +0100866{
867 struct i2c_client *client = to_i2c_client(dev);
868 struct adt7475_data *data = i2c_get_clientdata(client);
869 long val;
870
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100871 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100872 return -EINVAL;
873 if (val != 0 && val != 1)
874 return -EINVAL;
875
876 mutex_lock(&data->lock);
877 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
878 if (val)
879 data->config4 |= CONFIG4_MAXDUTY;
880 else
881 data->config4 &= ~CONFIG4_MAXDUTY;
882 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
883 mutex_unlock(&data->lock);
884
885 return count;
886}
887
Julia Lawall1d05303c2016-12-22 13:05:33 +0100888static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +0100889 char *buf)
890{
891 struct adt7475_data *data = dev_get_drvdata(dev);
892 return sprintf(buf, "%d\n", (int)data->vrm);
893}
894
Julia Lawall1d05303c2016-12-22 13:05:33 +0100895static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
896 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +0100897{
898 struct adt7475_data *data = dev_get_drvdata(dev);
899 long val;
900
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100901 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100902 return -EINVAL;
903 if (val < 0 || val > 255)
904 return -EINVAL;
905 data->vrm = val;
906
907 return count;
908}
909
Julia Lawall1d05303c2016-12-22 13:05:33 +0100910static ssize_t cpu0_vid_show(struct device *dev,
911 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +0100912{
913 struct adt7475_data *data = adt7475_update_device(dev);
914 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
915}
916
Jean Delvare3d849982009-12-09 20:36:05 +0100917static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
918static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
919 set_voltage, MAX, 0);
920static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
921 set_voltage, MIN, 0);
922static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100923static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100924static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100925 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100926static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100927 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100928static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
929static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
930static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
931 set_voltage, MAX, 2);
932static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
933 set_voltage, MIN, 2);
934static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100935static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
936static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
937 set_voltage, MAX, 3);
938static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
939 set_voltage, MIN, 3);
940static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
941static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
942static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
943 set_voltage, MAX, 4);
944static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
945 set_voltage, MIN, 4);
946static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
947static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
948static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
949 set_voltage, MAX, 5);
950static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
951 set_voltage, MIN, 5);
952static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100953static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
954static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
955static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
956static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
957 MAX, 0);
958static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
959 MIN, 0);
960static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
961 set_temp, OFFSET, 0);
962static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
963 show_temp, set_temp, AUTOMIN, 0);
964static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
965 show_point2, set_point2, 0, 0);
966static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
967 THERM, 0);
968static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
969 set_temp, HYSTERSIS, 0);
970static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
971static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
972static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
973 MAX, 1);
974static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 MIN, 1);
976static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
977 set_temp, OFFSET, 1);
978static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
979 show_temp, set_temp, AUTOMIN, 1);
980static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
981 show_point2, set_point2, 0, 1);
982static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
983 THERM, 1);
984static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
985 set_temp, HYSTERSIS, 1);
986static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
987static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
988static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
989static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
990 MAX, 2);
991static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
992 MIN, 2);
993static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
994 set_temp, OFFSET, 2);
995static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
996 show_temp, set_temp, AUTOMIN, 2);
997static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
998 show_point2, set_point2, 0, 2);
999static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1000 THERM, 2);
1001static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1002 set_temp, HYSTERSIS, 2);
1003static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1004static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1005 MIN, 0);
1006static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1007static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1008static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1009 MIN, 1);
1010static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1011static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1012static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1013 MIN, 2);
1014static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1015static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1016static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1017 MIN, 3);
1018static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1019static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1020 0);
1021static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1022 set_pwmfreq, INPUT, 0);
1023static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1024 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001025static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001026 show_pwmchan, set_pwmchan, INPUT, 0);
1027static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1028 set_pwm, MIN, 0);
1029static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1030 set_pwm, MAX, 0);
1031static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1032 1);
1033static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1034 set_pwmfreq, INPUT, 1);
1035static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1036 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001037static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001038 show_pwmchan, set_pwmchan, INPUT, 1);
1039static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1040 set_pwm, MIN, 1);
1041static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1042 set_pwm, MAX, 1);
1043static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1044 2);
1045static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1046 set_pwmfreq, INPUT, 2);
1047static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1048 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001049static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001050 show_pwmchan, set_pwmchan, INPUT, 2);
1051static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1052 set_pwm, MIN, 2);
1053static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1054 set_pwm, MAX, 2);
1055
Jean Delvaref99318b2009-12-09 20:36:03 +01001056/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001057static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001058
Julia Lawall1d05303c2016-12-22 13:05:33 +01001059static DEVICE_ATTR_RW(vrm);
1060static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001061
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001062static struct attribute *adt7475_attrs[] = {
1063 &sensor_dev_attr_in1_input.dev_attr.attr,
1064 &sensor_dev_attr_in1_max.dev_attr.attr,
1065 &sensor_dev_attr_in1_min.dev_attr.attr,
1066 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1067 &sensor_dev_attr_in2_input.dev_attr.attr,
1068 &sensor_dev_attr_in2_max.dev_attr.attr,
1069 &sensor_dev_attr_in2_min.dev_attr.attr,
1070 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1071 &sensor_dev_attr_temp1_input.dev_attr.attr,
1072 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1073 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1074 &sensor_dev_attr_temp1_max.dev_attr.attr,
1075 &sensor_dev_attr_temp1_min.dev_attr.attr,
1076 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1077 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1078 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1079 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1080 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1081 &sensor_dev_attr_temp2_input.dev_attr.attr,
1082 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1083 &sensor_dev_attr_temp2_max.dev_attr.attr,
1084 &sensor_dev_attr_temp2_min.dev_attr.attr,
1085 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1086 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1087 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1088 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1089 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1090 &sensor_dev_attr_temp3_input.dev_attr.attr,
1091 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1092 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1093 &sensor_dev_attr_temp3_max.dev_attr.attr,
1094 &sensor_dev_attr_temp3_min.dev_attr.attr,
1095 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1096 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1097 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1098 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1099 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1100 &sensor_dev_attr_fan1_input.dev_attr.attr,
1101 &sensor_dev_attr_fan1_min.dev_attr.attr,
1102 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1103 &sensor_dev_attr_fan2_input.dev_attr.attr,
1104 &sensor_dev_attr_fan2_min.dev_attr.attr,
1105 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1106 &sensor_dev_attr_fan3_input.dev_attr.attr,
1107 &sensor_dev_attr_fan3_min.dev_attr.attr,
1108 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001109 &sensor_dev_attr_pwm1.dev_attr.attr,
1110 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1111 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001112 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001113 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1114 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001115 &sensor_dev_attr_pwm3.dev_attr.attr,
1116 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1117 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001118 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001119 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001121 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001122 NULL,
1123};
1124
Jean Delvare378933c2009-12-09 20:36:06 +01001125static struct attribute *fan4_attrs[] = {
1126 &sensor_dev_attr_fan4_input.dev_attr.attr,
1127 &sensor_dev_attr_fan4_min.dev_attr.attr,
1128 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1129 NULL
1130};
1131
1132static struct attribute *pwm2_attrs[] = {
1133 &sensor_dev_attr_pwm2.dev_attr.attr,
1134 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1135 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1136 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1137 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1138 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1139 NULL
1140};
1141
Jean Delvare378933c2009-12-09 20:36:06 +01001142static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001143 &sensor_dev_attr_in0_input.dev_attr.attr,
1144 &sensor_dev_attr_in0_max.dev_attr.attr,
1145 &sensor_dev_attr_in0_min.dev_attr.attr,
1146 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001147 NULL
1148};
1149
Jean Delvared8d2ee02009-12-09 20:36:08 +01001150static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001151 &sensor_dev_attr_in3_input.dev_attr.attr,
1152 &sensor_dev_attr_in3_max.dev_attr.attr,
1153 &sensor_dev_attr_in3_min.dev_attr.attr,
1154 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001155 NULL
1156};
1157
1158static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001159 &sensor_dev_attr_in4_input.dev_attr.attr,
1160 &sensor_dev_attr_in4_max.dev_attr.attr,
1161 &sensor_dev_attr_in4_min.dev_attr.attr,
1162 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001163 NULL
1164};
1165
1166static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001167 &sensor_dev_attr_in5_input.dev_attr.attr,
1168 &sensor_dev_attr_in5_max.dev_attr.attr,
1169 &sensor_dev_attr_in5_min.dev_attr.attr,
1170 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1171 NULL
1172};
1173
Jean Delvare54fe4672009-12-09 20:36:08 +01001174static struct attribute *vid_attrs[] = {
1175 &dev_attr_cpu0_vid.attr,
1176 &dev_attr_vrm.attr,
1177 NULL
1178};
1179
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001180static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001181static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1182static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1183static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001184static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1185static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1186static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001187static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001188
Jean Delvare310ec792009-12-14 21:17:23 +01001189static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001190 struct i2c_board_info *info)
1191{
1192 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001193 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001194 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001195
1196 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1197 return -ENODEV;
1198
Jean Delvareb180d052009-12-09 20:36:02 +01001199 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001200 devid2 = adt7475_read(REG_DEVID2);
1201 if (vendid != 0x41 || /* Analog Devices */
1202 (devid2 & 0xf8) != 0x68)
1203 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001204
Jean Delvared656b6f2009-12-09 20:36:04 +01001205 devid = adt7475_read(REG_DEVID);
1206 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001207 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001208 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001209 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001210 else if (devid == 0x76)
1211 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001212 else if ((devid2 & 0xfc) == 0x6c)
1213 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001214 else {
1215 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001216 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001217 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001218 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001219 }
1220
Jean Delvareb180d052009-12-09 20:36:02 +01001221 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001222
1223 return 0;
1224}
1225
Jean Delvare0f144802009-12-09 20:36:06 +01001226static void adt7475_remove_files(struct i2c_client *client,
1227 struct adt7475_data *data)
1228{
1229 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001230 if (data->has_fan4)
1231 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1232 if (data->has_pwm2)
1233 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1234 if (data->has_voltage & (1 << 0))
1235 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001236 if (data->has_voltage & (1 << 3))
1237 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1238 if (data->has_voltage & (1 << 4))
1239 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1240 if (data->has_voltage & (1 << 5))
1241 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001242 if (data->has_vid)
1243 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001244}
1245
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001246static int adt7475_probe(struct i2c_client *client,
1247 const struct i2c_device_id *id)
1248{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001249 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001250 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001251 [adt7473] = "ADT7473",
1252 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001253 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001254 [adt7490] = "ADT7490",
1255 };
1256
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001257 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001258 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001259 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001260
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001261 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001262 if (data == NULL)
1263 return -ENOMEM;
1264
1265 mutex_init(&data->lock);
1266 i2c_set_clientdata(client, data);
1267
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001268 if (client->dev.of_node)
1269 chip = (enum chips)of_device_get_match_data(&client->dev);
1270 else
1271 chip = id->driver_data;
1272
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001273 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001274 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001275 case adt7476:
1276 data->has_voltage = 0x0e; /* in1 to in3 */
1277 revision = adt7475_read(REG_DEVID2) & 0x07;
1278 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001279 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001280 data->has_voltage = 0x3e; /* in1 to in5 */
1281 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001282 if (revision == 0x03)
1283 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001284 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001285 default:
1286 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001287 revision = adt7475_read(REG_DEVID2) & 0x07;
1288 }
1289
1290 config3 = adt7475_read(REG_CONFIG3);
1291 /* Pin PWM2 may alternatively be used for ALERT output */
1292 if (!(config3 & CONFIG3_SMBALERT))
1293 data->has_pwm2 = 1;
1294 /* Meaning of this bit is inverted for the ADT7473-1 */
1295 if (id->driver_data == adt7473 && revision >= 1)
1296 data->has_pwm2 = !data->has_pwm2;
1297
1298 data->config4 = adt7475_read(REG_CONFIG4);
1299 /* Pin TACH4 may alternatively be used for THERM */
1300 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1301 data->has_fan4 = 1;
1302
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001303 /*
1304 * THERM configuration is more complex on the ADT7476 and ADT7490,
1305 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1306 * this function
1307 */
Jean Delvare378933c2009-12-09 20:36:06 +01001308 if (id->driver_data == adt7490) {
1309 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1310 !(config3 & CONFIG3_THERM))
1311 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001312 }
1313 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001314 if (!(config3 & CONFIG3_THERM) ||
1315 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1316 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001317 }
1318
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001319 /*
1320 * On the ADT7476, the +12V input pin may instead be used as VID5,
1321 * and VID pins may alternatively be used as GPIO
1322 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001323 if (id->driver_data == adt7476) {
1324 u8 vid = adt7475_read(REG_VID);
1325 if (!(vid & VID_VIDSEL))
1326 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001327
1328 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001329 }
1330
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001331 /* Voltage attenuators can be bypassed, globally or individually */
1332 config2 = adt7475_read(REG_CONFIG2);
1333 if (config2 & CONFIG2_ATTN) {
1334 data->bypass_attn = (0x3 << 3) | 0x3;
1335 } else {
1336 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1337 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1338 }
1339 data->bypass_attn &= data->has_voltage;
1340
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001341 /*
1342 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1343 * pwm's which are disabled to manual mode with 0% duty cycle
1344 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001345 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1346 adt7475_read_pwm(client, i);
1347
Chris Packham4abdf382017-04-22 07:08:09 +12001348 /* Start monitoring */
1349 switch (chip) {
1350 case adt7475:
1351 case adt7476:
1352 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1353 adt7475_read(REG_CONFIG1) | 0x01);
1354 break;
1355 default:
1356 break;
1357 }
1358
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001359 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1360 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001361 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001362
Jean Delvare378933c2009-12-09 20:36:06 +01001363 /* Features that can be disabled individually */
1364 if (data->has_fan4) {
1365 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1366 if (ret)
1367 goto eremove;
1368 }
1369 if (data->has_pwm2) {
1370 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1371 if (ret)
1372 goto eremove;
1373 }
1374 if (data->has_voltage & (1 << 0)) {
1375 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1376 if (ret)
1377 goto eremove;
1378 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001379 if (data->has_voltage & (1 << 3)) {
1380 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1381 if (ret)
1382 goto eremove;
1383 }
1384 if (data->has_voltage & (1 << 4)) {
1385 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1386 if (ret)
1387 goto eremove;
1388 }
1389 if (data->has_voltage & (1 << 5)) {
1390 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1391 if (ret)
1392 goto eremove;
1393 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001394 if (data->has_vid) {
1395 data->vrm = vid_which_vrm();
1396 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1397 if (ret)
1398 goto eremove;
1399 }
Jean Delvare378933c2009-12-09 20:36:06 +01001400
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001401 data->hwmon_dev = hwmon_device_register(&client->dev);
1402 if (IS_ERR(data->hwmon_dev)) {
1403 ret = PTR_ERR(data->hwmon_dev);
1404 goto eremove;
1405 }
1406
Jean Delvared07ca4a2009-12-09 20:36:07 +01001407 dev_info(&client->dev, "%s device, revision %d\n",
1408 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001409 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001410 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001411 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001412 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001413 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001414 data->has_pwm2 ? " pwm2" : "",
1415 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001416 if (data->bypass_attn)
1417 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1418 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1419 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1420 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1421 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001422
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001423 return 0;
1424
1425eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001426 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001427 return ret;
1428}
1429
1430static int adt7475_remove(struct i2c_client *client)
1431{
1432 struct adt7475_data *data = i2c_get_clientdata(client);
1433
1434 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001435 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001436
1437 return 0;
1438}
1439
1440static struct i2c_driver adt7475_driver = {
1441 .class = I2C_CLASS_HWMON,
1442 .driver = {
1443 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001444 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001445 },
1446 .probe = adt7475_probe,
1447 .remove = adt7475_remove,
1448 .id_table = adt7475_id,
1449 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001450 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001451};
1452
1453static void adt7475_read_hystersis(struct i2c_client *client)
1454{
1455 struct adt7475_data *data = i2c_get_clientdata(client);
1456
1457 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1458 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1459 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1460}
1461
1462static void adt7475_read_pwm(struct i2c_client *client, int index)
1463{
1464 struct adt7475_data *data = i2c_get_clientdata(client);
1465 unsigned int v;
1466
1467 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1468
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001469 /*
1470 * Figure out the internal value for pwmctrl and pwmchan
1471 * based on the current settings
1472 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001473 v = (data->pwm[CONTROL][index] >> 5) & 7;
1474
1475 if (v == 3)
1476 data->pwmctl[index] = 0;
1477 else if (v == 7)
1478 data->pwmctl[index] = 1;
1479 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001480 /*
1481 * The fan is disabled - we don't want to
1482 * support that, so change to manual mode and
1483 * set the duty cycle to 0 instead
1484 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001485 data->pwm[INPUT][index] = 0;
1486 data->pwm[CONTROL][index] &= ~0xE0;
1487 data->pwm[CONTROL][index] |= (7 << 5);
1488
1489 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1490 data->pwm[INPUT][index]);
1491
1492 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1493 data->pwm[CONTROL][index]);
1494
1495 data->pwmctl[index] = 1;
1496 } else {
1497 data->pwmctl[index] = 2;
1498
1499 switch (v) {
1500 case 0:
1501 data->pwmchan[index] = 1;
1502 break;
1503 case 1:
1504 data->pwmchan[index] = 2;
1505 break;
1506 case 2:
1507 data->pwmchan[index] = 4;
1508 break;
1509 case 5:
1510 data->pwmchan[index] = 6;
1511 break;
1512 case 6:
1513 data->pwmchan[index] = 7;
1514 break;
1515 }
1516 }
1517}
1518
1519static struct adt7475_data *adt7475_update_device(struct device *dev)
1520{
1521 struct i2c_client *client = to_i2c_client(dev);
1522 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001523 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001524 int i;
1525
1526 mutex_lock(&data->lock);
1527
1528 /* Measurement values update every 2 seconds */
1529 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1530 !data->valid) {
1531 data->alarms = adt7475_read(REG_STATUS2) << 8;
1532 data->alarms |= adt7475_read(REG_STATUS1);
1533
Jean Delvare3d849982009-12-09 20:36:05 +01001534 ext = (adt7475_read(REG_EXTEND2) << 8) |
1535 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001536 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1537 if (!(data->has_voltage & (1 << i)))
1538 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001539 data->voltage[INPUT][i] =
1540 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001541 ((ext >> (i * 2)) & 3);
1542 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001543
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001544 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1545 data->temp[INPUT][i] =
1546 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001547 ((ext >> ((i + 5) * 2)) & 3);
1548
1549 if (data->has_voltage & (1 << 5)) {
1550 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1551 ext = adt7475_read(REG_EXTEND3);
1552 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1553 ((ext >> 4) & 3);
1554 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001555
Jean Delvare378933c2009-12-09 20:36:06 +01001556 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1557 if (i == 3 && !data->has_fan4)
1558 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559 data->tach[INPUT][i] =
1560 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001561 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001562
1563 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001564 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1565 if (i == 1 && !data->has_pwm2)
1566 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001567 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001568 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001569
Jean Delvare54fe4672009-12-09 20:36:08 +01001570 if (data->has_vid)
1571 data->vid = adt7475_read(REG_VID) & 0x3f;
1572
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001573 data->measure_updated = jiffies;
1574 }
1575
1576 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001577 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001578 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001579 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001580 data->config5 = adt7475_read(REG_CONFIG5);
1581
1582 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001583 if (!(data->has_voltage & (1 << i)))
1584 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001585 /* Adjust values so they match the input precision */
1586 data->voltage[MIN][i] =
1587 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1588 data->voltage[MAX][i] =
1589 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1590 }
1591
Jean Delvare3d849982009-12-09 20:36:05 +01001592 if (data->has_voltage & (1 << 5)) {
1593 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1594 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1595 }
1596
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001597 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1598 /* Adjust values so they match the input precision */
1599 data->temp[MIN][i] =
1600 adt7475_read(TEMP_MIN_REG(i)) << 2;
1601 data->temp[MAX][i] =
1602 adt7475_read(TEMP_MAX_REG(i)) << 2;
1603 data->temp[AUTOMIN][i] =
1604 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1605 data->temp[THERM][i] =
1606 adt7475_read(TEMP_THERM_REG(i)) << 2;
1607 data->temp[OFFSET][i] =
1608 adt7475_read(TEMP_OFFSET_REG(i));
1609 }
1610 adt7475_read_hystersis(client);
1611
Jean Delvare378933c2009-12-09 20:36:06 +01001612 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1613 if (i == 3 && !data->has_fan4)
1614 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001615 data->tach[MIN][i] =
1616 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001617 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001618
1619 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001620 if (i == 1 && !data->has_pwm2)
1621 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001622 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1623 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1624 /* Set the channel and control information */
1625 adt7475_read_pwm(client, i);
1626 }
1627
1628 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1629 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1630 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1631
1632 data->limits_updated = jiffies;
1633 data->valid = 1;
1634 }
1635
1636 mutex_unlock(&data->lock);
1637
1638 return data;
1639}
1640
Axel Linf0967ee2012-01-20 15:38:18 +08001641module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001642
1643MODULE_AUTHOR("Advanced Micro Devices, Inc");
1644MODULE_DESCRIPTION("adt7475 driver");
1645MODULE_LICENSE("GPL");