blob: fcfa4822214580901346ebb8ca7f0e150162470e [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>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010025
26/* Indexes for the sysfs hooks */
27
28#define INPUT 0
29#define MIN 1
30#define MAX 2
31#define CONTROL 3
32#define OFFSET 3
33#define AUTOMIN 4
34#define THERM 5
35#define HYSTERSIS 6
36
Guenter Roeck9ed5bc22012-01-19 11:02:15 -080037/*
38 * These are unique identifiers for the sysfs functions - unlike the
39 * numbers above, these are not also indexes into an array
40 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010041
42#define ALARM 9
43#define FAULT 10
44
45/* 7475 Common Registers */
46
Jean Delvared07ca4a2009-12-09 20:36:07 +010047#define REG_DEVREV2 0x12 /* ADT7490 only */
48
Jean Delvare3d849982009-12-09 20:36:05 +010049#define REG_VTT 0x1E /* ADT7490 only */
50#define REG_EXTEND3 0x1F /* ADT7490 only */
51
Jean Delvarecffb9dd2009-12-09 20:36:03 +010052#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010053#define REG_TEMP_BASE 0x25
54#define REG_TACH_BASE 0x28
55#define REG_PWM_BASE 0x30
56#define REG_PWM_MAX_BASE 0x38
57
58#define REG_DEVID 0x3D
59#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010060#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010061
62#define REG_STATUS1 0x41
63#define REG_STATUS2 0x42
64
Jean Delvared8d2ee02009-12-09 20:36:08 +010065#define REG_VID 0x43 /* ADT7476 only */
66
Jean Delvarecffb9dd2009-12-09 20:36:03 +010067#define REG_VOLTAGE_MIN_BASE 0x44
68#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010069
70#define REG_TEMP_MIN_BASE 0x4E
71#define REG_TEMP_MAX_BASE 0x4F
72
73#define REG_TACH_MIN_BASE 0x54
74
75#define REG_PWM_CONFIG_BASE 0x5C
76
77#define REG_TEMP_TRANGE_BASE 0x5F
78
79#define REG_PWM_MIN_BASE 0x64
80
81#define REG_TEMP_TMIN_BASE 0x67
82#define REG_TEMP_THERM_BASE 0x6A
83
84#define REG_REMOTE1_HYSTERSIS 0x6D
85#define REG_REMOTE2_HYSTERSIS 0x6E
86
87#define REG_TEMP_OFFSET_BASE 0x70
88
Jean Delvareebfaf1f2009-12-09 20:36:07 +010089#define REG_CONFIG2 0x73
90
Jordan Crouse1c301fc2009-01-15 22:27:47 +010091#define REG_EXTEND1 0x76
92#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010093
94#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010095#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010096#define REG_CONFIG4 0x7D
97
Jean Delvare3d849982009-12-09 20:36:05 +010098#define REG_STATUS4 0x81 /* ADT7490 only */
99
100#define REG_VTT_MIN 0x84 /* ADT7490 only */
101#define REG_VTT_MAX 0x86 /* ADT7490 only */
102
Jean Delvared8d2ee02009-12-09 20:36:08 +0100103#define VID_VIDSEL 0x80 /* ADT7476 only */
104
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100105#define CONFIG2_ATTN 0x20
106
Jean Delvare378933c2009-12-09 20:36:06 +0100107#define CONFIG3_SMBALERT 0x01
108#define CONFIG3_THERM 0x02
109
110#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100111#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100112#define CONFIG4_ATTN_IN10 0x30
113#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100114
115#define CONFIG5_TWOSCOMP 0x01
116#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100117#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100118
119/* ADT7475 Settings */
120
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100121#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100122#define ADT7475_TEMP_COUNT 3
123#define ADT7475_TACH_COUNT 4
124#define ADT7475_PWM_COUNT 3
125
126/* Macro to read the registers */
127
128#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
129
130/* Macros to easily index the registers */
131
132#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
133#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
134
135#define PWM_REG(idx) (REG_PWM_BASE + (idx))
136#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
137#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
138#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
139
140#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
141#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
142#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
143
144#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
145#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
146#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
147#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
148#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
149#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
150#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
151
Jean Delvare918ee912010-10-28 20:31:50 +0200152static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100153
Jean Delvaree5e9f442009-12-14 21:17:27 +0100154enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100155
156static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100157 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100158 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100159 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100160 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100161 { }
162};
163MODULE_DEVICE_TABLE(i2c, adt7475_id);
164
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300165static const struct of_device_id adt7475_of_match[] = {
166 {
167 .compatible = "adi,adt7473",
168 .data = (void *)adt7473
169 },
170 {
171 .compatible = "adi,adt7475",
172 .data = (void *)adt7475
173 },
174 {
175 .compatible = "adi,adt7476",
176 .data = (void *)adt7476
177 },
178 {
179 .compatible = "adi,adt7490",
180 .data = (void *)adt7490
181 },
182 { },
183};
184MODULE_DEVICE_TABLE(of, adt7475_of_match);
185
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100186struct adt7475_data {
187 struct device *hwmon_dev;
188 struct mutex lock;
189
190 unsigned long measure_updated;
191 unsigned long limits_updated;
192 char valid;
193
Jean Delvaref99318b2009-12-09 20:36:03 +0100194 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100195 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100196 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100197 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100198 u8 has_pwm2:1;
199 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100200 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100201 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100202 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100203 u16 temp[7][3];
204 u16 tach[2][4];
205 u8 pwm[4][3];
206 u8 range[3];
207 u8 pwmctl[3];
208 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100209
210 u8 vid;
211 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100212};
213
214static struct i2c_driver adt7475_driver;
215static struct adt7475_data *adt7475_update_device(struct device *dev);
216static void adt7475_read_hystersis(struct i2c_client *client);
217static void adt7475_read_pwm(struct i2c_client *client, int index);
218
219/* Given a temp value, convert it to register value */
220
221static inline u16 temp2reg(struct adt7475_data *data, long val)
222{
223 u16 ret;
224
225 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800226 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100227 ret = (val + 64500) / 1000;
228 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800229 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100230 if (val < -500)
231 ret = (256500 + val) / 1000;
232 else
233 ret = (val + 500) / 1000;
234 }
235
236 return ret << 2;
237}
238
239/* Given a register value, convert it to a real temp value */
240
241static inline int reg2temp(struct adt7475_data *data, u16 reg)
242{
243 if (data->config5 & CONFIG5_TWOSCOMP) {
244 if (reg >= 512)
245 return (reg - 1024) * 250;
246 else
247 return reg * 250;
248 } else
249 return (reg - 256) * 250;
250}
251
252static inline int tach2rpm(u16 tach)
253{
254 if (tach == 0 || tach == 0xFFFF)
255 return 0;
256
257 return (90000 * 60) / tach;
258}
259
260static inline u16 rpm2tach(unsigned long rpm)
261{
262 if (rpm == 0)
263 return 0;
264
Guenter Roeck2a844c12013-01-09 08:09:34 -0800265 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100266}
267
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100268/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
269static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
270 { 45, 94 }, /* +2.5V */
271 { 175, 525 }, /* Vccp */
272 { 68, 71 }, /* Vcc */
273 { 93, 47 }, /* +5V */
274 { 120, 20 }, /* +12V */
275 { 45, 45 }, /* Vtt */
276};
277
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100278static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100279{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100280 const int *r = adt7473_in_scaling[channel];
281
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100282 if (bypass_attn & (1 << channel))
283 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100284 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100285}
286
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100287static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100288{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100289 const int *r = adt7473_in_scaling[channel];
290 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100291
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100292 if (bypass_attn & (1 << channel))
293 reg = (volt * 1024) / 2250;
294 else
295 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800296 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100297}
298
299static u16 adt7475_read_word(struct i2c_client *client, int reg)
300{
301 u16 val;
302
303 val = i2c_smbus_read_byte_data(client, reg);
304 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
305
306 return val;
307}
308
309static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
310{
311 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
312 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
313}
314
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800315/*
316 * Find the nearest value in a table - used for pwm frequency and
317 * auto temp range
318 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100319static int find_nearest(long val, const int *array, int size)
320{
321 int i;
322
323 if (val < array[0])
324 return 0;
325
326 if (val > array[size - 1])
327 return size - 1;
328
329 for (i = 0; i < size - 1; i++) {
330 int a, b;
331
332 if (val > array[i + 1])
333 continue;
334
335 a = val - array[i];
336 b = array[i + 1] - val;
337
338 return (a <= b) ? i : i + 1;
339 }
340
341 return 0;
342}
343
344static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
345 char *buf)
346{
347 struct adt7475_data *data = adt7475_update_device(dev);
348 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
349 unsigned short val;
350
351 switch (sattr->nr) {
352 case ALARM:
353 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100354 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100355 default:
356 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100357 return sprintf(buf, "%d\n",
358 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100359 }
360}
361
362static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
363 const char *buf, size_t count)
364{
365
366 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
367 struct i2c_client *client = to_i2c_client(dev);
368 struct adt7475_data *data = i2c_get_clientdata(client);
369 unsigned char reg;
370 long val;
371
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100372 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100373 return -EINVAL;
374
375 mutex_lock(&data->lock);
376
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100377 data->voltage[sattr->nr][sattr->index] =
378 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100379
Jean Delvare3d849982009-12-09 20:36:05 +0100380 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
381 if (sattr->nr == MIN)
382 reg = VOLTAGE_MIN_REG(sattr->index);
383 else
384 reg = VOLTAGE_MAX_REG(sattr->index);
385 } else {
386 if (sattr->nr == MIN)
387 reg = REG_VTT_MIN;
388 else
389 reg = REG_VTT_MAX;
390 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100391
392 i2c_smbus_write_byte_data(client, reg,
393 data->voltage[sattr->nr][sattr->index] >> 2);
394 mutex_unlock(&data->lock);
395
396 return count;
397}
398
399static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
400 char *buf)
401{
402 struct adt7475_data *data = adt7475_update_device(dev);
403 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
404 int out;
405
406 switch (sattr->nr) {
407 case HYSTERSIS:
408 mutex_lock(&data->lock);
409 out = data->temp[sattr->nr][sattr->index];
410 if (sattr->index != 1)
411 out = (out >> 4) & 0xF;
412 else
413 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800414 /*
415 * Show the value as an absolute number tied to
416 * THERM
417 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100418 out = reg2temp(data, data->temp[THERM][sattr->index]) -
419 out * 1000;
420 mutex_unlock(&data->lock);
421 break;
422
423 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800424 /*
425 * Offset is always 2's complement, regardless of the
426 * setting in CONFIG5
427 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100428 mutex_lock(&data->lock);
429 out = (s8)data->temp[sattr->nr][sattr->index];
430 if (data->config5 & CONFIG5_TEMPOFFSET)
431 out *= 1000;
432 else
433 out *= 500;
434 mutex_unlock(&data->lock);
435 break;
436
437 case ALARM:
438 out = (data->alarms >> (sattr->index + 4)) & 1;
439 break;
440
441 case FAULT:
442 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100443 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100444 break;
445
446 default:
447 /* All other temp values are in the configured format */
448 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
449 }
450
451 return sprintf(buf, "%d\n", out);
452}
453
454static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
456{
457 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
458 struct i2c_client *client = to_i2c_client(dev);
459 struct adt7475_data *data = i2c_get_clientdata(client);
460 unsigned char reg = 0;
461 u8 out;
462 int temp;
463 long val;
464
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100465 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100466 return -EINVAL;
467
468 mutex_lock(&data->lock);
469
470 /* We need the config register in all cases for temp <-> reg conv. */
471 data->config5 = adt7475_read(REG_CONFIG5);
472
473 switch (sattr->nr) {
474 case OFFSET:
475 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800476 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100477 out = data->temp[OFFSET][sattr->index] = val / 1000;
478 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800479 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100480 out = data->temp[OFFSET][sattr->index] = val / 500;
481 }
482 break;
483
484 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800485 /*
486 * The value will be given as an absolute value, turn it
487 * into an offset based on THERM
488 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100489
490 /* Read fresh THERM and HYSTERSIS values from the chip */
491 data->temp[THERM][sattr->index] =
492 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
493 adt7475_read_hystersis(client);
494
495 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800496 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100497 val = (temp - val) / 1000;
498
499 if (sattr->index != 1) {
500 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
501 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
502 } else {
503 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
504 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
505 }
506
507 out = data->temp[HYSTERSIS][sattr->index];
508 break;
509
510 default:
511 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
512
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800513 /*
514 * We maintain an extra 2 digits of precision for simplicity
515 * - shift those back off before writing the value
516 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100517 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
518 }
519
520 switch (sattr->nr) {
521 case MIN:
522 reg = TEMP_MIN_REG(sattr->index);
523 break;
524 case MAX:
525 reg = TEMP_MAX_REG(sattr->index);
526 break;
527 case OFFSET:
528 reg = TEMP_OFFSET_REG(sattr->index);
529 break;
530 case AUTOMIN:
531 reg = TEMP_TMIN_REG(sattr->index);
532 break;
533 case THERM:
534 reg = TEMP_THERM_REG(sattr->index);
535 break;
536 case HYSTERSIS:
537 if (sattr->index != 2)
538 reg = REG_REMOTE1_HYSTERSIS;
539 else
540 reg = REG_REMOTE2_HYSTERSIS;
541
542 break;
543 }
544
545 i2c_smbus_write_byte_data(client, reg, out);
546
547 mutex_unlock(&data->lock);
548 return count;
549}
550
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800551/*
552 * Table of autorange values - the user will write the value in millidegrees,
553 * and we'll convert it
554 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100555static const int autorange_table[] = {
556 2000, 2500, 3330, 4000, 5000, 6670, 8000,
557 10000, 13330, 16000, 20000, 26670, 32000, 40000,
558 53330, 80000
559};
560
561static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
562 char *buf)
563{
564 struct adt7475_data *data = adt7475_update_device(dev);
565 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
566 int out, val;
567
568 mutex_lock(&data->lock);
569 out = (data->range[sattr->index] >> 4) & 0x0F;
570 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
571 mutex_unlock(&data->lock);
572
573 return sprintf(buf, "%d\n", val + autorange_table[out]);
574}
575
576static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
578{
579 struct i2c_client *client = to_i2c_client(dev);
580 struct adt7475_data *data = i2c_get_clientdata(client);
581 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
582 int temp;
583 long val;
584
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100585 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100586 return -EINVAL;
587
588 mutex_lock(&data->lock);
589
590 /* Get a fresh copy of the needed registers */
591 data->config5 = adt7475_read(REG_CONFIG5);
592 data->temp[AUTOMIN][sattr->index] =
593 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
594 data->range[sattr->index] =
595 adt7475_read(TEMP_TRANGE_REG(sattr->index));
596
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800597 /*
598 * The user will write an absolute value, so subtract the start point
599 * to figure the range
600 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100601 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800602 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100603 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
604 val -= temp;
605
606 /* Find the nearest table entry to what the user wrote */
607 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
608
609 data->range[sattr->index] &= ~0xF0;
610 data->range[sattr->index] |= val << 4;
611
612 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
613 data->range[sattr->index]);
614
615 mutex_unlock(&data->lock);
616 return count;
617}
618
619static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
620 char *buf)
621{
622 struct adt7475_data *data = adt7475_update_device(dev);
623 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
624 int out;
625
626 if (sattr->nr == ALARM)
627 out = (data->alarms >> (sattr->index + 10)) & 1;
628 else
629 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
630
631 return sprintf(buf, "%d\n", out);
632}
633
634static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
636{
637
638 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
639 struct i2c_client *client = to_i2c_client(dev);
640 struct adt7475_data *data = i2c_get_clientdata(client);
641 unsigned long val;
642
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100643 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100644 return -EINVAL;
645
646 mutex_lock(&data->lock);
647
648 data->tach[MIN][sattr->index] = rpm2tach(val);
649
650 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
651 data->tach[MIN][sattr->index]);
652
653 mutex_unlock(&data->lock);
654 return count;
655}
656
657static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
658 char *buf)
659{
660 struct adt7475_data *data = adt7475_update_device(dev);
661 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
662
663 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
664}
665
666static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
667 char *buf)
668{
669 struct adt7475_data *data = adt7475_update_device(dev);
670 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
671
672 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
673}
674
675static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
676 char *buf)
677{
678 struct adt7475_data *data = adt7475_update_device(dev);
679 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
680
681 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
682}
683
684static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
686{
687
688 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
689 struct i2c_client *client = to_i2c_client(dev);
690 struct adt7475_data *data = i2c_get_clientdata(client);
691 unsigned char reg = 0;
692 long val;
693
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100694 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100695 return -EINVAL;
696
697 mutex_lock(&data->lock);
698
699 switch (sattr->nr) {
700 case INPUT:
701 /* Get a fresh value for CONTROL */
702 data->pwm[CONTROL][sattr->index] =
703 adt7475_read(PWM_CONFIG_REG(sattr->index));
704
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800705 /*
706 * If we are not in manual mode, then we shouldn't allow
707 * the user to set the pwm speed
708 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100709 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
710 mutex_unlock(&data->lock);
711 return count;
712 }
713
714 reg = PWM_REG(sattr->index);
715 break;
716
717 case MIN:
718 reg = PWM_MIN_REG(sattr->index);
719 break;
720
721 case MAX:
722 reg = PWM_MAX_REG(sattr->index);
723 break;
724 }
725
Guenter Roeck2a844c12013-01-09 08:09:34 -0800726 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100727 i2c_smbus_write_byte_data(client, reg,
728 data->pwm[sattr->nr][sattr->index]);
729
730 mutex_unlock(&data->lock);
731
732 return count;
733}
734
735/* Called by set_pwmctrl and set_pwmchan */
736
737static int hw_set_pwm(struct i2c_client *client, int index,
738 unsigned int pwmctl, unsigned int pwmchan)
739{
740 struct adt7475_data *data = i2c_get_clientdata(client);
741 long val = 0;
742
743 switch (pwmctl) {
744 case 0:
745 val = 0x03; /* Run at full speed */
746 break;
747 case 1:
748 val = 0x07; /* Manual mode */
749 break;
750 case 2:
751 switch (pwmchan) {
752 case 1:
753 /* Remote1 controls PWM */
754 val = 0x00;
755 break;
756 case 2:
757 /* local controls PWM */
758 val = 0x01;
759 break;
760 case 4:
761 /* remote2 controls PWM */
762 val = 0x02;
763 break;
764 case 6:
765 /* local/remote2 control PWM */
766 val = 0x05;
767 break;
768 case 7:
769 /* All three control PWM */
770 val = 0x06;
771 break;
772 default:
773 return -EINVAL;
774 }
775 break;
776 default:
777 return -EINVAL;
778 }
779
780 data->pwmctl[index] = pwmctl;
781 data->pwmchan[index] = pwmchan;
782
783 data->pwm[CONTROL][index] &= ~0xE0;
784 data->pwm[CONTROL][index] |= (val & 7) << 5;
785
786 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
787 data->pwm[CONTROL][index]);
788
789 return 0;
790}
791
792static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
793 const char *buf, size_t count)
794{
795 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
796 struct i2c_client *client = to_i2c_client(dev);
797 struct adt7475_data *data = i2c_get_clientdata(client);
798 int r;
799 long val;
800
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100801 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100802 return -EINVAL;
803
804 mutex_lock(&data->lock);
805 /* Read Modify Write PWM values */
806 adt7475_read_pwm(client, sattr->index);
807 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
808 if (r)
809 count = r;
810 mutex_unlock(&data->lock);
811
812 return count;
813}
814
815static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
816 const char *buf, size_t count)
817{
818 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
819 struct i2c_client *client = to_i2c_client(dev);
820 struct adt7475_data *data = i2c_get_clientdata(client);
821 int r;
822 long val;
823
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100824 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100825 return -EINVAL;
826
827 mutex_lock(&data->lock);
828 /* Read Modify Write PWM values */
829 adt7475_read_pwm(client, sattr->index);
830 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
831 if (r)
832 count = r;
833 mutex_unlock(&data->lock);
834
835 return count;
836}
837
838/* List of frequencies for the PWM */
839static const int pwmfreq_table[] = {
840 11, 14, 22, 29, 35, 44, 58, 88
841};
842
843static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
844 char *buf)
845{
846 struct adt7475_data *data = adt7475_update_device(dev);
847 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
848
849 return sprintf(buf, "%d\n",
850 pwmfreq_table[data->range[sattr->index] & 7]);
851}
852
853static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
854 const char *buf, size_t count)
855{
856 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
857 struct i2c_client *client = to_i2c_client(dev);
858 struct adt7475_data *data = i2c_get_clientdata(client);
859 int out;
860 long val;
861
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100862 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100863 return -EINVAL;
864
865 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
866
867 mutex_lock(&data->lock);
868
869 data->range[sattr->index] =
870 adt7475_read(TEMP_TRANGE_REG(sattr->index));
871 data->range[sattr->index] &= ~7;
872 data->range[sattr->index] |= out;
873
874 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
875 data->range[sattr->index]);
876
877 mutex_unlock(&data->lock);
878 return count;
879}
880
Julia Lawall1d05303c2016-12-22 13:05:33 +0100881static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
882 struct device_attribute *devattr,
883 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +0100884{
885 struct adt7475_data *data = adt7475_update_device(dev);
886 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
887}
888
Julia Lawall1d05303c2016-12-22 13:05:33 +0100889static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
890 struct device_attribute *devattr,
891 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +0100892{
893 struct i2c_client *client = to_i2c_client(dev);
894 struct adt7475_data *data = i2c_get_clientdata(client);
895 long val;
896
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100897 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100898 return -EINVAL;
899 if (val != 0 && val != 1)
900 return -EINVAL;
901
902 mutex_lock(&data->lock);
903 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
904 if (val)
905 data->config4 |= CONFIG4_MAXDUTY;
906 else
907 data->config4 &= ~CONFIG4_MAXDUTY;
908 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
909 mutex_unlock(&data->lock);
910
911 return count;
912}
913
Julia Lawall1d05303c2016-12-22 13:05:33 +0100914static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +0100915 char *buf)
916{
917 struct adt7475_data *data = dev_get_drvdata(dev);
918 return sprintf(buf, "%d\n", (int)data->vrm);
919}
920
Julia Lawall1d05303c2016-12-22 13:05:33 +0100921static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
922 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +0100923{
924 struct adt7475_data *data = dev_get_drvdata(dev);
925 long val;
926
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100927 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100928 return -EINVAL;
929 if (val < 0 || val > 255)
930 return -EINVAL;
931 data->vrm = val;
932
933 return count;
934}
935
Julia Lawall1d05303c2016-12-22 13:05:33 +0100936static ssize_t cpu0_vid_show(struct device *dev,
937 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +0100938{
939 struct adt7475_data *data = adt7475_update_device(dev);
940 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
941}
942
Jean Delvare3d849982009-12-09 20:36:05 +0100943static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
944static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
945 set_voltage, MAX, 0);
946static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
947 set_voltage, MIN, 0);
948static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100949static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100950static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100951 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100952static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100953 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100954static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
955static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
956static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
957 set_voltage, MAX, 2);
958static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
959 set_voltage, MIN, 2);
960static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100961static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
962static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
963 set_voltage, MAX, 3);
964static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
965 set_voltage, MIN, 3);
966static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
967static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
968static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
969 set_voltage, MAX, 4);
970static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
971 set_voltage, MIN, 4);
972static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
973static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
974static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
975 set_voltage, MAX, 5);
976static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
977 set_voltage, MIN, 5);
978static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100979static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
980static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
981static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
982static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
983 MAX, 0);
984static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 MIN, 0);
986static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
987 set_temp, OFFSET, 0);
988static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
989 show_temp, set_temp, AUTOMIN, 0);
990static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
991 show_point2, set_point2, 0, 0);
992static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
993 THERM, 0);
994static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
995 set_temp, HYSTERSIS, 0);
996static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
997static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
998static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
999 MAX, 1);
1000static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1001 MIN, 1);
1002static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1003 set_temp, OFFSET, 1);
1004static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
1005 show_temp, set_temp, AUTOMIN, 1);
1006static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
1007 show_point2, set_point2, 0, 1);
1008static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1009 THERM, 1);
1010static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1011 set_temp, HYSTERSIS, 1);
1012static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
1013static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
1014static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
1015static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1016 MAX, 2);
1017static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1018 MIN, 2);
1019static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1020 set_temp, OFFSET, 2);
1021static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1022 show_temp, set_temp, AUTOMIN, 2);
1023static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1024 show_point2, set_point2, 0, 2);
1025static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1026 THERM, 2);
1027static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1028 set_temp, HYSTERSIS, 2);
1029static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1030static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1031 MIN, 0);
1032static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1033static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1034static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1035 MIN, 1);
1036static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1037static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1038static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1039 MIN, 2);
1040static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1041static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1042static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1043 MIN, 3);
1044static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1045static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1046 0);
1047static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1048 set_pwmfreq, INPUT, 0);
1049static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1050 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001051static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001052 show_pwmchan, set_pwmchan, INPUT, 0);
1053static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1054 set_pwm, MIN, 0);
1055static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1056 set_pwm, MAX, 0);
1057static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1058 1);
1059static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1060 set_pwmfreq, INPUT, 1);
1061static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1062 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001063static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001064 show_pwmchan, set_pwmchan, INPUT, 1);
1065static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1066 set_pwm, MIN, 1);
1067static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1068 set_pwm, MAX, 1);
1069static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1070 2);
1071static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1072 set_pwmfreq, INPUT, 2);
1073static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1074 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001075static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001076 show_pwmchan, set_pwmchan, INPUT, 2);
1077static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1078 set_pwm, MIN, 2);
1079static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1080 set_pwm, MAX, 2);
1081
Jean Delvaref99318b2009-12-09 20:36:03 +01001082/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001083static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001084
Julia Lawall1d05303c2016-12-22 13:05:33 +01001085static DEVICE_ATTR_RW(vrm);
1086static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001087
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001088static struct attribute *adt7475_attrs[] = {
1089 &sensor_dev_attr_in1_input.dev_attr.attr,
1090 &sensor_dev_attr_in1_max.dev_attr.attr,
1091 &sensor_dev_attr_in1_min.dev_attr.attr,
1092 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1093 &sensor_dev_attr_in2_input.dev_attr.attr,
1094 &sensor_dev_attr_in2_max.dev_attr.attr,
1095 &sensor_dev_attr_in2_min.dev_attr.attr,
1096 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1097 &sensor_dev_attr_temp1_input.dev_attr.attr,
1098 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1099 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1100 &sensor_dev_attr_temp1_max.dev_attr.attr,
1101 &sensor_dev_attr_temp1_min.dev_attr.attr,
1102 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1103 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1104 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1105 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1106 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1107 &sensor_dev_attr_temp2_input.dev_attr.attr,
1108 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1109 &sensor_dev_attr_temp2_max.dev_attr.attr,
1110 &sensor_dev_attr_temp2_min.dev_attr.attr,
1111 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1112 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1113 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1114 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1115 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1116 &sensor_dev_attr_temp3_input.dev_attr.attr,
1117 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1118 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1119 &sensor_dev_attr_temp3_max.dev_attr.attr,
1120 &sensor_dev_attr_temp3_min.dev_attr.attr,
1121 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1122 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1123 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1124 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1125 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1126 &sensor_dev_attr_fan1_input.dev_attr.attr,
1127 &sensor_dev_attr_fan1_min.dev_attr.attr,
1128 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1129 &sensor_dev_attr_fan2_input.dev_attr.attr,
1130 &sensor_dev_attr_fan2_min.dev_attr.attr,
1131 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1132 &sensor_dev_attr_fan3_input.dev_attr.attr,
1133 &sensor_dev_attr_fan3_min.dev_attr.attr,
1134 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001135 &sensor_dev_attr_pwm1.dev_attr.attr,
1136 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1137 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001138 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001139 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1140 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001141 &sensor_dev_attr_pwm3.dev_attr.attr,
1142 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1143 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001144 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001145 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1146 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001147 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001148 NULL,
1149};
1150
Jean Delvare378933c2009-12-09 20:36:06 +01001151static struct attribute *fan4_attrs[] = {
1152 &sensor_dev_attr_fan4_input.dev_attr.attr,
1153 &sensor_dev_attr_fan4_min.dev_attr.attr,
1154 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1155 NULL
1156};
1157
1158static struct attribute *pwm2_attrs[] = {
1159 &sensor_dev_attr_pwm2.dev_attr.attr,
1160 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1161 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1162 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1163 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1164 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1165 NULL
1166};
1167
Jean Delvare378933c2009-12-09 20:36:06 +01001168static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001169 &sensor_dev_attr_in0_input.dev_attr.attr,
1170 &sensor_dev_attr_in0_max.dev_attr.attr,
1171 &sensor_dev_attr_in0_min.dev_attr.attr,
1172 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001173 NULL
1174};
1175
Jean Delvared8d2ee02009-12-09 20:36:08 +01001176static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001177 &sensor_dev_attr_in3_input.dev_attr.attr,
1178 &sensor_dev_attr_in3_max.dev_attr.attr,
1179 &sensor_dev_attr_in3_min.dev_attr.attr,
1180 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001181 NULL
1182};
1183
1184static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001185 &sensor_dev_attr_in4_input.dev_attr.attr,
1186 &sensor_dev_attr_in4_max.dev_attr.attr,
1187 &sensor_dev_attr_in4_min.dev_attr.attr,
1188 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001189 NULL
1190};
1191
1192static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001193 &sensor_dev_attr_in5_input.dev_attr.attr,
1194 &sensor_dev_attr_in5_max.dev_attr.attr,
1195 &sensor_dev_attr_in5_min.dev_attr.attr,
1196 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1197 NULL
1198};
1199
Jean Delvare54fe4672009-12-09 20:36:08 +01001200static struct attribute *vid_attrs[] = {
1201 &dev_attr_cpu0_vid.attr,
1202 &dev_attr_vrm.attr,
1203 NULL
1204};
1205
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001206static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001207static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1208static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1209static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001210static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1211static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1212static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001213static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001214
Jean Delvare310ec792009-12-14 21:17:23 +01001215static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001216 struct i2c_board_info *info)
1217{
1218 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001219 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001220 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001221
1222 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1223 return -ENODEV;
1224
Jean Delvareb180d052009-12-09 20:36:02 +01001225 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001226 devid2 = adt7475_read(REG_DEVID2);
1227 if (vendid != 0x41 || /* Analog Devices */
1228 (devid2 & 0xf8) != 0x68)
1229 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001230
Jean Delvared656b6f2009-12-09 20:36:04 +01001231 devid = adt7475_read(REG_DEVID);
1232 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001233 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001234 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001235 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001236 else if (devid == 0x76)
1237 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001238 else if ((devid2 & 0xfc) == 0x6c)
1239 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001240 else {
1241 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001242 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001243 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001244 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001245 }
1246
Jean Delvareb180d052009-12-09 20:36:02 +01001247 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001248
1249 return 0;
1250}
1251
Jean Delvare0f144802009-12-09 20:36:06 +01001252static void adt7475_remove_files(struct i2c_client *client,
1253 struct adt7475_data *data)
1254{
1255 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001256 if (data->has_fan4)
1257 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1258 if (data->has_pwm2)
1259 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1260 if (data->has_voltage & (1 << 0))
1261 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001262 if (data->has_voltage & (1 << 3))
1263 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1264 if (data->has_voltage & (1 << 4))
1265 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1266 if (data->has_voltage & (1 << 5))
1267 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001268 if (data->has_vid)
1269 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001270}
1271
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001272static int adt7475_probe(struct i2c_client *client,
1273 const struct i2c_device_id *id)
1274{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001275 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001276 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001277 [adt7473] = "ADT7473",
1278 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001279 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001280 [adt7490] = "ADT7490",
1281 };
1282
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001283 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001284 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001285 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001286
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001287 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001288 if (data == NULL)
1289 return -ENOMEM;
1290
1291 mutex_init(&data->lock);
1292 i2c_set_clientdata(client, data);
1293
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001294 if (client->dev.of_node)
1295 chip = (enum chips)of_device_get_match_data(&client->dev);
1296 else
1297 chip = id->driver_data;
1298
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001299 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001300 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001301 case adt7476:
1302 data->has_voltage = 0x0e; /* in1 to in3 */
1303 revision = adt7475_read(REG_DEVID2) & 0x07;
1304 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001305 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001306 data->has_voltage = 0x3e; /* in1 to in5 */
1307 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001308 if (revision == 0x03)
1309 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001310 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001311 default:
1312 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001313 revision = adt7475_read(REG_DEVID2) & 0x07;
1314 }
1315
1316 config3 = adt7475_read(REG_CONFIG3);
1317 /* Pin PWM2 may alternatively be used for ALERT output */
1318 if (!(config3 & CONFIG3_SMBALERT))
1319 data->has_pwm2 = 1;
1320 /* Meaning of this bit is inverted for the ADT7473-1 */
1321 if (id->driver_data == adt7473 && revision >= 1)
1322 data->has_pwm2 = !data->has_pwm2;
1323
1324 data->config4 = adt7475_read(REG_CONFIG4);
1325 /* Pin TACH4 may alternatively be used for THERM */
1326 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1327 data->has_fan4 = 1;
1328
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001329 /*
1330 * THERM configuration is more complex on the ADT7476 and ADT7490,
1331 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1332 * this function
1333 */
Jean Delvare378933c2009-12-09 20:36:06 +01001334 if (id->driver_data == adt7490) {
1335 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1336 !(config3 & CONFIG3_THERM))
1337 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001338 }
1339 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001340 if (!(config3 & CONFIG3_THERM) ||
1341 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1342 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001343 }
1344
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001345 /*
1346 * On the ADT7476, the +12V input pin may instead be used as VID5,
1347 * and VID pins may alternatively be used as GPIO
1348 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001349 if (id->driver_data == adt7476) {
1350 u8 vid = adt7475_read(REG_VID);
1351 if (!(vid & VID_VIDSEL))
1352 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001353
1354 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001355 }
1356
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001357 /* Voltage attenuators can be bypassed, globally or individually */
1358 config2 = adt7475_read(REG_CONFIG2);
1359 if (config2 & CONFIG2_ATTN) {
1360 data->bypass_attn = (0x3 << 3) | 0x3;
1361 } else {
1362 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1363 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1364 }
1365 data->bypass_attn &= data->has_voltage;
1366
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001367 /*
1368 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1369 * pwm's which are disabled to manual mode with 0% duty cycle
1370 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001371 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1372 adt7475_read_pwm(client, i);
1373
1374 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1375 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001376 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001377
Jean Delvare378933c2009-12-09 20:36:06 +01001378 /* Features that can be disabled individually */
1379 if (data->has_fan4) {
1380 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1381 if (ret)
1382 goto eremove;
1383 }
1384 if (data->has_pwm2) {
1385 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1386 if (ret)
1387 goto eremove;
1388 }
1389 if (data->has_voltage & (1 << 0)) {
1390 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1391 if (ret)
1392 goto eremove;
1393 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001394 if (data->has_voltage & (1 << 3)) {
1395 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1396 if (ret)
1397 goto eremove;
1398 }
1399 if (data->has_voltage & (1 << 4)) {
1400 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1401 if (ret)
1402 goto eremove;
1403 }
1404 if (data->has_voltage & (1 << 5)) {
1405 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1406 if (ret)
1407 goto eremove;
1408 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001409 if (data->has_vid) {
1410 data->vrm = vid_which_vrm();
1411 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1412 if (ret)
1413 goto eremove;
1414 }
Jean Delvare378933c2009-12-09 20:36:06 +01001415
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001416 data->hwmon_dev = hwmon_device_register(&client->dev);
1417 if (IS_ERR(data->hwmon_dev)) {
1418 ret = PTR_ERR(data->hwmon_dev);
1419 goto eremove;
1420 }
1421
Jean Delvared07ca4a2009-12-09 20:36:07 +01001422 dev_info(&client->dev, "%s device, revision %d\n",
1423 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001424 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001425 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001426 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001427 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001428 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001429 data->has_pwm2 ? " pwm2" : "",
1430 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001431 if (data->bypass_attn)
1432 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1433 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1434 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1435 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1436 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001437
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001438 return 0;
1439
1440eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001441 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001442 return ret;
1443}
1444
1445static int adt7475_remove(struct i2c_client *client)
1446{
1447 struct adt7475_data *data = i2c_get_clientdata(client);
1448
1449 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001450 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001451
1452 return 0;
1453}
1454
1455static struct i2c_driver adt7475_driver = {
1456 .class = I2C_CLASS_HWMON,
1457 .driver = {
1458 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001459 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001460 },
1461 .probe = adt7475_probe,
1462 .remove = adt7475_remove,
1463 .id_table = adt7475_id,
1464 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001465 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001466};
1467
1468static void adt7475_read_hystersis(struct i2c_client *client)
1469{
1470 struct adt7475_data *data = i2c_get_clientdata(client);
1471
1472 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1473 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1474 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1475}
1476
1477static void adt7475_read_pwm(struct i2c_client *client, int index)
1478{
1479 struct adt7475_data *data = i2c_get_clientdata(client);
1480 unsigned int v;
1481
1482 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1483
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001484 /*
1485 * Figure out the internal value for pwmctrl and pwmchan
1486 * based on the current settings
1487 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001488 v = (data->pwm[CONTROL][index] >> 5) & 7;
1489
1490 if (v == 3)
1491 data->pwmctl[index] = 0;
1492 else if (v == 7)
1493 data->pwmctl[index] = 1;
1494 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001495 /*
1496 * The fan is disabled - we don't want to
1497 * support that, so change to manual mode and
1498 * set the duty cycle to 0 instead
1499 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001500 data->pwm[INPUT][index] = 0;
1501 data->pwm[CONTROL][index] &= ~0xE0;
1502 data->pwm[CONTROL][index] |= (7 << 5);
1503
1504 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1505 data->pwm[INPUT][index]);
1506
1507 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1508 data->pwm[CONTROL][index]);
1509
1510 data->pwmctl[index] = 1;
1511 } else {
1512 data->pwmctl[index] = 2;
1513
1514 switch (v) {
1515 case 0:
1516 data->pwmchan[index] = 1;
1517 break;
1518 case 1:
1519 data->pwmchan[index] = 2;
1520 break;
1521 case 2:
1522 data->pwmchan[index] = 4;
1523 break;
1524 case 5:
1525 data->pwmchan[index] = 6;
1526 break;
1527 case 6:
1528 data->pwmchan[index] = 7;
1529 break;
1530 }
1531 }
1532}
1533
1534static struct adt7475_data *adt7475_update_device(struct device *dev)
1535{
1536 struct i2c_client *client = to_i2c_client(dev);
1537 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001538 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001539 int i;
1540
1541 mutex_lock(&data->lock);
1542
1543 /* Measurement values update every 2 seconds */
1544 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1545 !data->valid) {
1546 data->alarms = adt7475_read(REG_STATUS2) << 8;
1547 data->alarms |= adt7475_read(REG_STATUS1);
1548
Jean Delvare3d849982009-12-09 20:36:05 +01001549 ext = (adt7475_read(REG_EXTEND2) << 8) |
1550 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001551 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1552 if (!(data->has_voltage & (1 << i)))
1553 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001554 data->voltage[INPUT][i] =
1555 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001556 ((ext >> (i * 2)) & 3);
1557 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001558
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001559 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1560 data->temp[INPUT][i] =
1561 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001562 ((ext >> ((i + 5) * 2)) & 3);
1563
1564 if (data->has_voltage & (1 << 5)) {
1565 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1566 ext = adt7475_read(REG_EXTEND3);
1567 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1568 ((ext >> 4) & 3);
1569 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001570
Jean Delvare378933c2009-12-09 20:36:06 +01001571 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1572 if (i == 3 && !data->has_fan4)
1573 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001574 data->tach[INPUT][i] =
1575 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001576 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001577
1578 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001579 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1580 if (i == 1 && !data->has_pwm2)
1581 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001582 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001583 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001584
Jean Delvare54fe4672009-12-09 20:36:08 +01001585 if (data->has_vid)
1586 data->vid = adt7475_read(REG_VID) & 0x3f;
1587
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001588 data->measure_updated = jiffies;
1589 }
1590
1591 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001592 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001593 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001594 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001595 data->config5 = adt7475_read(REG_CONFIG5);
1596
1597 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001598 if (!(data->has_voltage & (1 << i)))
1599 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001600 /* Adjust values so they match the input precision */
1601 data->voltage[MIN][i] =
1602 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1603 data->voltage[MAX][i] =
1604 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1605 }
1606
Jean Delvare3d849982009-12-09 20:36:05 +01001607 if (data->has_voltage & (1 << 5)) {
1608 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1609 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1610 }
1611
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001612 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1613 /* Adjust values so they match the input precision */
1614 data->temp[MIN][i] =
1615 adt7475_read(TEMP_MIN_REG(i)) << 2;
1616 data->temp[MAX][i] =
1617 adt7475_read(TEMP_MAX_REG(i)) << 2;
1618 data->temp[AUTOMIN][i] =
1619 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1620 data->temp[THERM][i] =
1621 adt7475_read(TEMP_THERM_REG(i)) << 2;
1622 data->temp[OFFSET][i] =
1623 adt7475_read(TEMP_OFFSET_REG(i));
1624 }
1625 adt7475_read_hystersis(client);
1626
Jean Delvare378933c2009-12-09 20:36:06 +01001627 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1628 if (i == 3 && !data->has_fan4)
1629 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001630 data->tach[MIN][i] =
1631 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001632 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001633
1634 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001635 if (i == 1 && !data->has_pwm2)
1636 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001637 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1638 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1639 /* Set the channel and control information */
1640 adt7475_read_pwm(client, i);
1641 }
1642
1643 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1644 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1645 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1646
1647 data->limits_updated = jiffies;
1648 data->valid = 1;
1649 }
1650
1651 mutex_unlock(&data->lock);
1652
1653 return data;
1654}
1655
Axel Linf0967ee2012-01-20 15:38:18 +08001656module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001657
1658MODULE_AUTHOR("Advanced Micro Devices, Inc");
1659MODULE_DESCRIPTION("adt7475 driver");
1660MODULE_LICENSE("GPL");