blob: c803e3c5fcd41af9602823a753051811408ecb02 [file] [log] [blame]
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
Jean Delvare7c81c602014-01-29 20:40:08 +01006 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
Jean Delvare3d849982009-12-09 20:36:05 +01007 *
Jordan Crouse1c301fc2009-01-15 22:27:47 +01008 * Derived from the lm83 driver by Jean Delvare
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -030016#include <linux/of_device.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010017#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/i2c.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
Jean Delvare54fe4672009-12-09 20:36:08 +010022#include <linux/hwmon-vid.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010023#include <linux/err.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020024#include <linux/jiffies.h>
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
Chris Packham4abdf382017-04-22 07:08:09 +120062#define REG_CONFIG1 0x40
63
Jordan Crouse1c301fc2009-01-15 22:27:47 +010064#define REG_STATUS1 0x41
65#define REG_STATUS2 0x42
66
Jean Delvared8d2ee02009-12-09 20:36:08 +010067#define REG_VID 0x43 /* ADT7476 only */
68
Jean Delvarecffb9dd2009-12-09 20:36:03 +010069#define REG_VOLTAGE_MIN_BASE 0x44
70#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010071
72#define REG_TEMP_MIN_BASE 0x4E
73#define REG_TEMP_MAX_BASE 0x4F
74
75#define REG_TACH_MIN_BASE 0x54
76
77#define REG_PWM_CONFIG_BASE 0x5C
78
79#define REG_TEMP_TRANGE_BASE 0x5F
80
81#define REG_PWM_MIN_BASE 0x64
82
83#define REG_TEMP_TMIN_BASE 0x67
84#define REG_TEMP_THERM_BASE 0x6A
85
86#define REG_REMOTE1_HYSTERSIS 0x6D
87#define REG_REMOTE2_HYSTERSIS 0x6E
88
89#define REG_TEMP_OFFSET_BASE 0x70
90
Jean Delvareebfaf1f2009-12-09 20:36:07 +010091#define REG_CONFIG2 0x73
92
Jordan Crouse1c301fc2009-01-15 22:27:47 +010093#define REG_EXTEND1 0x76
94#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010095
96#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +010097#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010098#define REG_CONFIG4 0x7D
99
Jean Delvare3d849982009-12-09 20:36:05 +0100100#define REG_STATUS4 0x81 /* ADT7490 only */
101
102#define REG_VTT_MIN 0x84 /* ADT7490 only */
103#define REG_VTT_MAX 0x86 /* ADT7490 only */
104
Jean Delvared8d2ee02009-12-09 20:36:08 +0100105#define VID_VIDSEL 0x80 /* ADT7476 only */
106
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100107#define CONFIG2_ATTN 0x20
108
Jean Delvare378933c2009-12-09 20:36:06 +0100109#define CONFIG3_SMBALERT 0x01
110#define CONFIG3_THERM 0x02
111
112#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100113#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100114#define CONFIG4_ATTN_IN10 0x30
115#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100116
117#define CONFIG5_TWOSCOMP 0x01
118#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100119#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100120
121/* ADT7475 Settings */
122
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100123#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100124#define ADT7475_TEMP_COUNT 3
125#define ADT7475_TACH_COUNT 4
126#define ADT7475_PWM_COUNT 3
127
128/* Macro to read the registers */
129
130#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
131
132/* Macros to easily index the registers */
133
134#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
135#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
136
137#define PWM_REG(idx) (REG_PWM_BASE + (idx))
138#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
139#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
140#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
141
142#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
143#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
144#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
145
146#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
147#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
148#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
149#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
150#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
151#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
152#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
153
Jean Delvare918ee912010-10-28 20:31:50 +0200154static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100155
Jean Delvaree5e9f442009-12-14 21:17:27 +0100156enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100157
158static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100159 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100160 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100161 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100162 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100163 { }
164};
165MODULE_DEVICE_TABLE(i2c, adt7475_id);
166
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300167static const struct of_device_id adt7475_of_match[] = {
168 {
169 .compatible = "adi,adt7473",
170 .data = (void *)adt7473
171 },
172 {
173 .compatible = "adi,adt7475",
174 .data = (void *)adt7475
175 },
176 {
177 .compatible = "adi,adt7476",
178 .data = (void *)adt7476
179 },
180 {
181 .compatible = "adi,adt7490",
182 .data = (void *)adt7490
183 },
184 { },
185};
186MODULE_DEVICE_TABLE(of, adt7475_of_match);
187
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100188struct adt7475_data {
189 struct device *hwmon_dev;
190 struct mutex lock;
191
192 unsigned long measure_updated;
193 unsigned long limits_updated;
194 char valid;
195
Jean Delvaref99318b2009-12-09 20:36:03 +0100196 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100197 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100198 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100199 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100200 u8 has_pwm2:1;
201 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100202 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100203 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100204 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100205 u16 temp[7][3];
206 u16 tach[2][4];
207 u8 pwm[4][3];
208 u8 range[3];
209 u8 pwmctl[3];
210 u8 pwmchan[3];
Jean Delvare54fe4672009-12-09 20:36:08 +0100211
212 u8 vid;
213 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100214};
215
216static struct i2c_driver adt7475_driver;
217static struct adt7475_data *adt7475_update_device(struct device *dev);
218static void adt7475_read_hystersis(struct i2c_client *client);
219static void adt7475_read_pwm(struct i2c_client *client, int index);
220
221/* Given a temp value, convert it to register value */
222
223static inline u16 temp2reg(struct adt7475_data *data, long val)
224{
225 u16 ret;
226
227 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800228 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100229 ret = (val + 64500) / 1000;
230 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800231 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100232 if (val < -500)
233 ret = (256500 + val) / 1000;
234 else
235 ret = (val + 500) / 1000;
236 }
237
238 return ret << 2;
239}
240
241/* Given a register value, convert it to a real temp value */
242
243static inline int reg2temp(struct adt7475_data *data, u16 reg)
244{
245 if (data->config5 & CONFIG5_TWOSCOMP) {
246 if (reg >= 512)
247 return (reg - 1024) * 250;
248 else
249 return reg * 250;
250 } else
251 return (reg - 256) * 250;
252}
253
254static inline int tach2rpm(u16 tach)
255{
256 if (tach == 0 || tach == 0xFFFF)
257 return 0;
258
259 return (90000 * 60) / tach;
260}
261
262static inline u16 rpm2tach(unsigned long rpm)
263{
264 if (rpm == 0)
265 return 0;
266
Guenter Roeck2a844c12013-01-09 08:09:34 -0800267 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100268}
269
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100270/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
271static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
272 { 45, 94 }, /* +2.5V */
273 { 175, 525 }, /* Vccp */
274 { 68, 71 }, /* Vcc */
275 { 93, 47 }, /* +5V */
276 { 120, 20 }, /* +12V */
277 { 45, 45 }, /* Vtt */
278};
279
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100280static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100281{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100282 const int *r = adt7473_in_scaling[channel];
283
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100284 if (bypass_attn & (1 << channel))
285 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100286 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100287}
288
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100289static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100290{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100291 const int *r = adt7473_in_scaling[channel];
292 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100293
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100294 if (bypass_attn & (1 << channel))
295 reg = (volt * 1024) / 2250;
296 else
297 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800298 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100299}
300
301static u16 adt7475_read_word(struct i2c_client *client, int reg)
302{
303 u16 val;
304
305 val = i2c_smbus_read_byte_data(client, reg);
306 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
307
308 return val;
309}
310
311static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
312{
313 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
314 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
315}
316
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800317/*
318 * Find the nearest value in a table - used for pwm frequency and
319 * auto temp range
320 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100321static int find_nearest(long val, const int *array, int size)
322{
323 int i;
324
325 if (val < array[0])
326 return 0;
327
328 if (val > array[size - 1])
329 return size - 1;
330
331 for (i = 0; i < size - 1; i++) {
332 int a, b;
333
334 if (val > array[i + 1])
335 continue;
336
337 a = val - array[i];
338 b = array[i + 1] - val;
339
340 return (a <= b) ? i : i + 1;
341 }
342
343 return 0;
344}
345
346static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
347 char *buf)
348{
349 struct adt7475_data *data = adt7475_update_device(dev);
350 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
351 unsigned short val;
352
353 switch (sattr->nr) {
354 case ALARM:
355 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100356 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100357 default:
358 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100359 return sprintf(buf, "%d\n",
360 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100361 }
362}
363
364static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
366{
367
368 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
369 struct i2c_client *client = to_i2c_client(dev);
370 struct adt7475_data *data = i2c_get_clientdata(client);
371 unsigned char reg;
372 long val;
373
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100374 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100375 return -EINVAL;
376
377 mutex_lock(&data->lock);
378
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100379 data->voltage[sattr->nr][sattr->index] =
380 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100381
Jean Delvare3d849982009-12-09 20:36:05 +0100382 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
383 if (sattr->nr == MIN)
384 reg = VOLTAGE_MIN_REG(sattr->index);
385 else
386 reg = VOLTAGE_MAX_REG(sattr->index);
387 } else {
388 if (sattr->nr == MIN)
389 reg = REG_VTT_MIN;
390 else
391 reg = REG_VTT_MAX;
392 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100393
394 i2c_smbus_write_byte_data(client, reg,
395 data->voltage[sattr->nr][sattr->index] >> 2);
396 mutex_unlock(&data->lock);
397
398 return count;
399}
400
401static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
402 char *buf)
403{
404 struct adt7475_data *data = adt7475_update_device(dev);
405 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
406 int out;
407
408 switch (sattr->nr) {
409 case HYSTERSIS:
410 mutex_lock(&data->lock);
411 out = data->temp[sattr->nr][sattr->index];
412 if (sattr->index != 1)
413 out = (out >> 4) & 0xF;
414 else
415 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800416 /*
417 * Show the value as an absolute number tied to
418 * THERM
419 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100420 out = reg2temp(data, data->temp[THERM][sattr->index]) -
421 out * 1000;
422 mutex_unlock(&data->lock);
423 break;
424
425 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800426 /*
427 * Offset is always 2's complement, regardless of the
428 * setting in CONFIG5
429 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100430 mutex_lock(&data->lock);
431 out = (s8)data->temp[sattr->nr][sattr->index];
432 if (data->config5 & CONFIG5_TEMPOFFSET)
433 out *= 1000;
434 else
435 out *= 500;
436 mutex_unlock(&data->lock);
437 break;
438
439 case ALARM:
440 out = (data->alarms >> (sattr->index + 4)) & 1;
441 break;
442
443 case FAULT:
444 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100445 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100446 break;
447
448 default:
449 /* All other temp values are in the configured format */
450 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
451 }
452
453 return sprintf(buf, "%d\n", out);
454}
455
456static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
457 const char *buf, size_t count)
458{
459 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
460 struct i2c_client *client = to_i2c_client(dev);
461 struct adt7475_data *data = i2c_get_clientdata(client);
462 unsigned char reg = 0;
463 u8 out;
464 int temp;
465 long val;
466
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100467 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100468 return -EINVAL;
469
470 mutex_lock(&data->lock);
471
472 /* We need the config register in all cases for temp <-> reg conv. */
473 data->config5 = adt7475_read(REG_CONFIG5);
474
475 switch (sattr->nr) {
476 case OFFSET:
477 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800478 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100479 out = data->temp[OFFSET][sattr->index] = val / 1000;
480 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800481 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100482 out = data->temp[OFFSET][sattr->index] = val / 500;
483 }
484 break;
485
486 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800487 /*
488 * The value will be given as an absolute value, turn it
489 * into an offset based on THERM
490 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100491
492 /* Read fresh THERM and HYSTERSIS values from the chip */
493 data->temp[THERM][sattr->index] =
494 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
495 adt7475_read_hystersis(client);
496
497 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800498 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100499 val = (temp - val) / 1000;
500
501 if (sattr->index != 1) {
502 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
503 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
504 } else {
505 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
506 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
507 }
508
509 out = data->temp[HYSTERSIS][sattr->index];
510 break;
511
512 default:
513 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
514
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800515 /*
516 * We maintain an extra 2 digits of precision for simplicity
517 * - shift those back off before writing the value
518 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100519 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
520 }
521
522 switch (sattr->nr) {
523 case MIN:
524 reg = TEMP_MIN_REG(sattr->index);
525 break;
526 case MAX:
527 reg = TEMP_MAX_REG(sattr->index);
528 break;
529 case OFFSET:
530 reg = TEMP_OFFSET_REG(sattr->index);
531 break;
532 case AUTOMIN:
533 reg = TEMP_TMIN_REG(sattr->index);
534 break;
535 case THERM:
536 reg = TEMP_THERM_REG(sattr->index);
537 break;
538 case HYSTERSIS:
539 if (sattr->index != 2)
540 reg = REG_REMOTE1_HYSTERSIS;
541 else
542 reg = REG_REMOTE2_HYSTERSIS;
543
544 break;
545 }
546
547 i2c_smbus_write_byte_data(client, reg, out);
548
549 mutex_unlock(&data->lock);
550 return count;
551}
552
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800553/*
554 * Table of autorange values - the user will write the value in millidegrees,
555 * and we'll convert it
556 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100557static const int autorange_table[] = {
558 2000, 2500, 3330, 4000, 5000, 6670, 8000,
559 10000, 13330, 16000, 20000, 26670, 32000, 40000,
560 53330, 80000
561};
562
563static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
564 char *buf)
565{
566 struct adt7475_data *data = adt7475_update_device(dev);
567 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
568 int out, val;
569
570 mutex_lock(&data->lock);
571 out = (data->range[sattr->index] >> 4) & 0x0F;
572 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
573 mutex_unlock(&data->lock);
574
575 return sprintf(buf, "%d\n", val + autorange_table[out]);
576}
577
578static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
579 const char *buf, size_t count)
580{
581 struct i2c_client *client = to_i2c_client(dev);
582 struct adt7475_data *data = i2c_get_clientdata(client);
583 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
584 int temp;
585 long val;
586
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100587 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100588 return -EINVAL;
589
590 mutex_lock(&data->lock);
591
592 /* Get a fresh copy of the needed registers */
593 data->config5 = adt7475_read(REG_CONFIG5);
594 data->temp[AUTOMIN][sattr->index] =
595 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
596 data->range[sattr->index] =
597 adt7475_read(TEMP_TRANGE_REG(sattr->index));
598
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800599 /*
600 * The user will write an absolute value, so subtract the start point
601 * to figure the range
602 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100603 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800604 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100605 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
606 val -= temp;
607
608 /* Find the nearest table entry to what the user wrote */
609 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
610
611 data->range[sattr->index] &= ~0xF0;
612 data->range[sattr->index] |= val << 4;
613
614 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
615 data->range[sattr->index]);
616
617 mutex_unlock(&data->lock);
618 return count;
619}
620
621static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
622 char *buf)
623{
624 struct adt7475_data *data = adt7475_update_device(dev);
625 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
626 int out;
627
628 if (sattr->nr == ALARM)
629 out = (data->alarms >> (sattr->index + 10)) & 1;
630 else
631 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
632
633 return sprintf(buf, "%d\n", out);
634}
635
636static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
637 const char *buf, size_t count)
638{
639
640 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
641 struct i2c_client *client = to_i2c_client(dev);
642 struct adt7475_data *data = i2c_get_clientdata(client);
643 unsigned long val;
644
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100645 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100646 return -EINVAL;
647
648 mutex_lock(&data->lock);
649
650 data->tach[MIN][sattr->index] = rpm2tach(val);
651
652 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
653 data->tach[MIN][sattr->index]);
654
655 mutex_unlock(&data->lock);
656 return count;
657}
658
659static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
660 char *buf)
661{
662 struct adt7475_data *data = adt7475_update_device(dev);
663 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
664
665 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
666}
667
668static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
669 char *buf)
670{
671 struct adt7475_data *data = adt7475_update_device(dev);
672 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
673
674 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
675}
676
677static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
678 char *buf)
679{
680 struct adt7475_data *data = adt7475_update_device(dev);
681 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
682
683 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
684}
685
686static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
687 const char *buf, size_t count)
688{
689
690 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
691 struct i2c_client *client = to_i2c_client(dev);
692 struct adt7475_data *data = i2c_get_clientdata(client);
693 unsigned char reg = 0;
694 long val;
695
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100696 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100697 return -EINVAL;
698
699 mutex_lock(&data->lock);
700
701 switch (sattr->nr) {
702 case INPUT:
703 /* Get a fresh value for CONTROL */
704 data->pwm[CONTROL][sattr->index] =
705 adt7475_read(PWM_CONFIG_REG(sattr->index));
706
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800707 /*
708 * If we are not in manual mode, then we shouldn't allow
709 * the user to set the pwm speed
710 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100711 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
712 mutex_unlock(&data->lock);
713 return count;
714 }
715
716 reg = PWM_REG(sattr->index);
717 break;
718
719 case MIN:
720 reg = PWM_MIN_REG(sattr->index);
721 break;
722
723 case MAX:
724 reg = PWM_MAX_REG(sattr->index);
725 break;
726 }
727
Guenter Roeck2a844c12013-01-09 08:09:34 -0800728 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100729 i2c_smbus_write_byte_data(client, reg,
730 data->pwm[sattr->nr][sattr->index]);
731
732 mutex_unlock(&data->lock);
733
734 return count;
735}
736
737/* Called by set_pwmctrl and set_pwmchan */
738
739static int hw_set_pwm(struct i2c_client *client, int index,
740 unsigned int pwmctl, unsigned int pwmchan)
741{
742 struct adt7475_data *data = i2c_get_clientdata(client);
743 long val = 0;
744
745 switch (pwmctl) {
746 case 0:
747 val = 0x03; /* Run at full speed */
748 break;
749 case 1:
750 val = 0x07; /* Manual mode */
751 break;
752 case 2:
753 switch (pwmchan) {
754 case 1:
755 /* Remote1 controls PWM */
756 val = 0x00;
757 break;
758 case 2:
759 /* local controls PWM */
760 val = 0x01;
761 break;
762 case 4:
763 /* remote2 controls PWM */
764 val = 0x02;
765 break;
766 case 6:
767 /* local/remote2 control PWM */
768 val = 0x05;
769 break;
770 case 7:
771 /* All three control PWM */
772 val = 0x06;
773 break;
774 default:
775 return -EINVAL;
776 }
777 break;
778 default:
779 return -EINVAL;
780 }
781
782 data->pwmctl[index] = pwmctl;
783 data->pwmchan[index] = pwmchan;
784
785 data->pwm[CONTROL][index] &= ~0xE0;
786 data->pwm[CONTROL][index] |= (val & 7) << 5;
787
788 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
789 data->pwm[CONTROL][index]);
790
791 return 0;
792}
793
794static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
795 const char *buf, size_t count)
796{
797 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
798 struct i2c_client *client = to_i2c_client(dev);
799 struct adt7475_data *data = i2c_get_clientdata(client);
800 int r;
801 long val;
802
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100803 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100804 return -EINVAL;
805
806 mutex_lock(&data->lock);
807 /* Read Modify Write PWM values */
808 adt7475_read_pwm(client, sattr->index);
809 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
810 if (r)
811 count = r;
812 mutex_unlock(&data->lock);
813
814 return count;
815}
816
817static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
818 const char *buf, size_t count)
819{
820 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
821 struct i2c_client *client = to_i2c_client(dev);
822 struct adt7475_data *data = i2c_get_clientdata(client);
823 int r;
824 long val;
825
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100826 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100827 return -EINVAL;
828
829 mutex_lock(&data->lock);
830 /* Read Modify Write PWM values */
831 adt7475_read_pwm(client, sattr->index);
832 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
833 if (r)
834 count = r;
835 mutex_unlock(&data->lock);
836
837 return count;
838}
839
840/* List of frequencies for the PWM */
841static const int pwmfreq_table[] = {
842 11, 14, 22, 29, 35, 44, 58, 88
843};
844
845static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
846 char *buf)
847{
848 struct adt7475_data *data = adt7475_update_device(dev);
849 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
850
851 return sprintf(buf, "%d\n",
852 pwmfreq_table[data->range[sattr->index] & 7]);
853}
854
855static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
856 const char *buf, size_t count)
857{
858 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
859 struct i2c_client *client = to_i2c_client(dev);
860 struct adt7475_data *data = i2c_get_clientdata(client);
861 int out;
862 long val;
863
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100864 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100865 return -EINVAL;
866
867 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
868
869 mutex_lock(&data->lock);
870
871 data->range[sattr->index] =
872 adt7475_read(TEMP_TRANGE_REG(sattr->index));
873 data->range[sattr->index] &= ~7;
874 data->range[sattr->index] |= out;
875
876 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
877 data->range[sattr->index]);
878
879 mutex_unlock(&data->lock);
880 return count;
881}
882
Julia Lawall1d05303c2016-12-22 13:05:33 +0100883static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
884 struct device_attribute *devattr,
885 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +0100886{
887 struct adt7475_data *data = adt7475_update_device(dev);
888 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
889}
890
Julia Lawall1d05303c2016-12-22 13:05:33 +0100891static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
892 struct device_attribute *devattr,
893 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +0100894{
895 struct i2c_client *client = to_i2c_client(dev);
896 struct adt7475_data *data = i2c_get_clientdata(client);
897 long val;
898
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100899 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +0100900 return -EINVAL;
901 if (val != 0 && val != 1)
902 return -EINVAL;
903
904 mutex_lock(&data->lock);
905 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
906 if (val)
907 data->config4 |= CONFIG4_MAXDUTY;
908 else
909 data->config4 &= ~CONFIG4_MAXDUTY;
910 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
911 mutex_unlock(&data->lock);
912
913 return count;
914}
915
Julia Lawall1d05303c2016-12-22 13:05:33 +0100916static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +0100917 char *buf)
918{
919 struct adt7475_data *data = dev_get_drvdata(dev);
920 return sprintf(buf, "%d\n", (int)data->vrm);
921}
922
Julia Lawall1d05303c2016-12-22 13:05:33 +0100923static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
924 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +0100925{
926 struct adt7475_data *data = dev_get_drvdata(dev);
927 long val;
928
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100929 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +0100930 return -EINVAL;
931 if (val < 0 || val > 255)
932 return -EINVAL;
933 data->vrm = val;
934
935 return count;
936}
937
Julia Lawall1d05303c2016-12-22 13:05:33 +0100938static ssize_t cpu0_vid_show(struct device *dev,
939 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +0100940{
941 struct adt7475_data *data = adt7475_update_device(dev);
942 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
943}
944
Jean Delvare3d849982009-12-09 20:36:05 +0100945static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
946static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
947 set_voltage, MAX, 0);
948static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
949 set_voltage, MIN, 0);
950static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100951static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100952static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100953 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100954static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100955 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100956static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
957static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
958static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
959 set_voltage, MAX, 2);
960static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
961 set_voltage, MIN, 2);
962static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jean Delvare3d849982009-12-09 20:36:05 +0100963static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
964static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
965 set_voltage, MAX, 3);
966static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
967 set_voltage, MIN, 3);
968static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
969static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
970static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
971 set_voltage, MAX, 4);
972static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
973 set_voltage, MIN, 4);
974static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
975static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
976static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
977 set_voltage, MAX, 5);
978static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
979 set_voltage, MIN, 5);
980static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100981static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
982static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
983static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
984static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 MAX, 0);
986static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
987 MIN, 0);
988static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
989 set_temp, OFFSET, 0);
990static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
991 show_temp, set_temp, AUTOMIN, 0);
992static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
993 show_point2, set_point2, 0, 0);
994static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
995 THERM, 0);
996static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
997 set_temp, HYSTERSIS, 0);
998static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
999static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
1000static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1001 MAX, 1);
1002static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1003 MIN, 1);
1004static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1005 set_temp, OFFSET, 1);
1006static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
1007 show_temp, set_temp, AUTOMIN, 1);
1008static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
1009 show_point2, set_point2, 0, 1);
1010static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1011 THERM, 1);
1012static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1013 set_temp, HYSTERSIS, 1);
1014static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
1015static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
1016static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
1017static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1018 MAX, 2);
1019static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1020 MIN, 2);
1021static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1022 set_temp, OFFSET, 2);
1023static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1024 show_temp, set_temp, AUTOMIN, 2);
1025static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1026 show_point2, set_point2, 0, 2);
1027static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1028 THERM, 2);
1029static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1030 set_temp, HYSTERSIS, 2);
1031static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1032static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1033 MIN, 0);
1034static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1035static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1036static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1037 MIN, 1);
1038static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1039static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1040static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1041 MIN, 2);
1042static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1043static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1044static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1045 MIN, 3);
1046static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1047static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1048 0);
1049static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1050 set_pwmfreq, INPUT, 0);
1051static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1052 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +01001053static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001054 show_pwmchan, set_pwmchan, INPUT, 0);
1055static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1056 set_pwm, MIN, 0);
1057static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1058 set_pwm, MAX, 0);
1059static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1060 1);
1061static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1062 set_pwmfreq, INPUT, 1);
1063static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1064 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +01001065static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001066 show_pwmchan, set_pwmchan, INPUT, 1);
1067static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1068 set_pwm, MIN, 1);
1069static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1070 set_pwm, MAX, 1);
1071static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1072 2);
1073static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1074 set_pwmfreq, INPUT, 2);
1075static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1076 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +01001077static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001078 show_pwmchan, set_pwmchan, INPUT, 2);
1079static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1080 set_pwm, MIN, 2);
1081static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1082 set_pwm, MAX, 2);
1083
Jean Delvaref99318b2009-12-09 20:36:03 +01001084/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001085static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001086
Julia Lawall1d05303c2016-12-22 13:05:33 +01001087static DEVICE_ATTR_RW(vrm);
1088static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001089
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001090static struct attribute *adt7475_attrs[] = {
1091 &sensor_dev_attr_in1_input.dev_attr.attr,
1092 &sensor_dev_attr_in1_max.dev_attr.attr,
1093 &sensor_dev_attr_in1_min.dev_attr.attr,
1094 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1095 &sensor_dev_attr_in2_input.dev_attr.attr,
1096 &sensor_dev_attr_in2_max.dev_attr.attr,
1097 &sensor_dev_attr_in2_min.dev_attr.attr,
1098 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1099 &sensor_dev_attr_temp1_input.dev_attr.attr,
1100 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1101 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1102 &sensor_dev_attr_temp1_max.dev_attr.attr,
1103 &sensor_dev_attr_temp1_min.dev_attr.attr,
1104 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1105 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1106 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1107 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1108 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1109 &sensor_dev_attr_temp2_input.dev_attr.attr,
1110 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1111 &sensor_dev_attr_temp2_max.dev_attr.attr,
1112 &sensor_dev_attr_temp2_min.dev_attr.attr,
1113 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1114 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1115 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1116 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1117 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1118 &sensor_dev_attr_temp3_input.dev_attr.attr,
1119 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1120 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1121 &sensor_dev_attr_temp3_max.dev_attr.attr,
1122 &sensor_dev_attr_temp3_min.dev_attr.attr,
1123 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1124 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1125 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1126 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1127 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1128 &sensor_dev_attr_fan1_input.dev_attr.attr,
1129 &sensor_dev_attr_fan1_min.dev_attr.attr,
1130 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1131 &sensor_dev_attr_fan2_input.dev_attr.attr,
1132 &sensor_dev_attr_fan2_min.dev_attr.attr,
1133 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1134 &sensor_dev_attr_fan3_input.dev_attr.attr,
1135 &sensor_dev_attr_fan3_min.dev_attr.attr,
1136 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001137 &sensor_dev_attr_pwm1.dev_attr.attr,
1138 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1139 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001140 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001141 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1142 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001143 &sensor_dev_attr_pwm3.dev_attr.attr,
1144 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1145 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001146 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001147 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1148 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001149 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001150 NULL,
1151};
1152
Jean Delvare378933c2009-12-09 20:36:06 +01001153static struct attribute *fan4_attrs[] = {
1154 &sensor_dev_attr_fan4_input.dev_attr.attr,
1155 &sensor_dev_attr_fan4_min.dev_attr.attr,
1156 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1157 NULL
1158};
1159
1160static struct attribute *pwm2_attrs[] = {
1161 &sensor_dev_attr_pwm2.dev_attr.attr,
1162 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1163 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1164 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1165 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1166 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1167 NULL
1168};
1169
Jean Delvare378933c2009-12-09 20:36:06 +01001170static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001171 &sensor_dev_attr_in0_input.dev_attr.attr,
1172 &sensor_dev_attr_in0_max.dev_attr.attr,
1173 &sensor_dev_attr_in0_min.dev_attr.attr,
1174 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001175 NULL
1176};
1177
Jean Delvared8d2ee02009-12-09 20:36:08 +01001178static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001179 &sensor_dev_attr_in3_input.dev_attr.attr,
1180 &sensor_dev_attr_in3_max.dev_attr.attr,
1181 &sensor_dev_attr_in3_min.dev_attr.attr,
1182 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001183 NULL
1184};
1185
1186static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001187 &sensor_dev_attr_in4_input.dev_attr.attr,
1188 &sensor_dev_attr_in4_max.dev_attr.attr,
1189 &sensor_dev_attr_in4_min.dev_attr.attr,
1190 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001191 NULL
1192};
1193
1194static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001195 &sensor_dev_attr_in5_input.dev_attr.attr,
1196 &sensor_dev_attr_in5_max.dev_attr.attr,
1197 &sensor_dev_attr_in5_min.dev_attr.attr,
1198 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1199 NULL
1200};
1201
Jean Delvare54fe4672009-12-09 20:36:08 +01001202static struct attribute *vid_attrs[] = {
1203 &dev_attr_cpu0_vid.attr,
1204 &dev_attr_vrm.attr,
1205 NULL
1206};
1207
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001208static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jean Delvare378933c2009-12-09 20:36:06 +01001209static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1210static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1211static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
Jean Delvared8d2ee02009-12-09 20:36:08 +01001212static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1213static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1214static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
Jean Delvare54fe4672009-12-09 20:36:08 +01001215static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001216
Jean Delvare310ec792009-12-14 21:17:23 +01001217static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001218 struct i2c_board_info *info)
1219{
1220 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001221 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001222 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001223
1224 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1225 return -ENODEV;
1226
Jean Delvareb180d052009-12-09 20:36:02 +01001227 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001228 devid2 = adt7475_read(REG_DEVID2);
1229 if (vendid != 0x41 || /* Analog Devices */
1230 (devid2 & 0xf8) != 0x68)
1231 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001232
Jean Delvared656b6f2009-12-09 20:36:04 +01001233 devid = adt7475_read(REG_DEVID);
1234 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001235 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001236 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001237 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001238 else if (devid == 0x76)
1239 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001240 else if ((devid2 & 0xfc) == 0x6c)
1241 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001242 else {
1243 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001244 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001245 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001246 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001247 }
1248
Jean Delvareb180d052009-12-09 20:36:02 +01001249 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001250
1251 return 0;
1252}
1253
Jean Delvare0f144802009-12-09 20:36:06 +01001254static void adt7475_remove_files(struct i2c_client *client,
1255 struct adt7475_data *data)
1256{
1257 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001258 if (data->has_fan4)
1259 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1260 if (data->has_pwm2)
1261 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1262 if (data->has_voltage & (1 << 0))
1263 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001264 if (data->has_voltage & (1 << 3))
1265 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1266 if (data->has_voltage & (1 << 4))
1267 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1268 if (data->has_voltage & (1 << 5))
1269 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001270 if (data->has_vid)
1271 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001272}
1273
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001274static int adt7475_probe(struct i2c_client *client,
1275 const struct i2c_device_id *id)
1276{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001277 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001278 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001279 [adt7473] = "ADT7473",
1280 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001281 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001282 [adt7490] = "ADT7490",
1283 };
1284
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001285 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001286 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001287 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001288
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001289 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001290 if (data == NULL)
1291 return -ENOMEM;
1292
1293 mutex_init(&data->lock);
1294 i2c_set_clientdata(client, data);
1295
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001296 if (client->dev.of_node)
1297 chip = (enum chips)of_device_get_match_data(&client->dev);
1298 else
1299 chip = id->driver_data;
1300
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001301 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001302 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001303 case adt7476:
1304 data->has_voltage = 0x0e; /* in1 to in3 */
1305 revision = adt7475_read(REG_DEVID2) & 0x07;
1306 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001307 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001308 data->has_voltage = 0x3e; /* in1 to in5 */
1309 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001310 if (revision == 0x03)
1311 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001312 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001313 default:
1314 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001315 revision = adt7475_read(REG_DEVID2) & 0x07;
1316 }
1317
1318 config3 = adt7475_read(REG_CONFIG3);
1319 /* Pin PWM2 may alternatively be used for ALERT output */
1320 if (!(config3 & CONFIG3_SMBALERT))
1321 data->has_pwm2 = 1;
1322 /* Meaning of this bit is inverted for the ADT7473-1 */
1323 if (id->driver_data == adt7473 && revision >= 1)
1324 data->has_pwm2 = !data->has_pwm2;
1325
1326 data->config4 = adt7475_read(REG_CONFIG4);
1327 /* Pin TACH4 may alternatively be used for THERM */
1328 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1329 data->has_fan4 = 1;
1330
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001331 /*
1332 * THERM configuration is more complex on the ADT7476 and ADT7490,
1333 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1334 * this function
1335 */
Jean Delvare378933c2009-12-09 20:36:06 +01001336 if (id->driver_data == adt7490) {
1337 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1338 !(config3 & CONFIG3_THERM))
1339 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001340 }
1341 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001342 if (!(config3 & CONFIG3_THERM) ||
1343 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1344 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001345 }
1346
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001347 /*
1348 * On the ADT7476, the +12V input pin may instead be used as VID5,
1349 * and VID pins may alternatively be used as GPIO
1350 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001351 if (id->driver_data == adt7476) {
1352 u8 vid = adt7475_read(REG_VID);
1353 if (!(vid & VID_VIDSEL))
1354 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001355
1356 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001357 }
1358
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001359 /* Voltage attenuators can be bypassed, globally or individually */
1360 config2 = adt7475_read(REG_CONFIG2);
1361 if (config2 & CONFIG2_ATTN) {
1362 data->bypass_attn = (0x3 << 3) | 0x3;
1363 } else {
1364 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1365 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1366 }
1367 data->bypass_attn &= data->has_voltage;
1368
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001369 /*
1370 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1371 * pwm's which are disabled to manual mode with 0% duty cycle
1372 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001373 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1374 adt7475_read_pwm(client, i);
1375
Chris Packham4abdf382017-04-22 07:08:09 +12001376 /* Start monitoring */
1377 switch (chip) {
1378 case adt7475:
1379 case adt7476:
1380 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1381 adt7475_read(REG_CONFIG1) | 0x01);
1382 break;
1383 default:
1384 break;
1385 }
1386
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001387 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1388 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001389 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001390
Jean Delvare378933c2009-12-09 20:36:06 +01001391 /* Features that can be disabled individually */
1392 if (data->has_fan4) {
1393 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1394 if (ret)
1395 goto eremove;
1396 }
1397 if (data->has_pwm2) {
1398 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1399 if (ret)
1400 goto eremove;
1401 }
1402 if (data->has_voltage & (1 << 0)) {
1403 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1404 if (ret)
1405 goto eremove;
1406 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001407 if (data->has_voltage & (1 << 3)) {
1408 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1409 if (ret)
1410 goto eremove;
1411 }
1412 if (data->has_voltage & (1 << 4)) {
1413 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1414 if (ret)
1415 goto eremove;
1416 }
1417 if (data->has_voltage & (1 << 5)) {
1418 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1419 if (ret)
1420 goto eremove;
1421 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001422 if (data->has_vid) {
1423 data->vrm = vid_which_vrm();
1424 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1425 if (ret)
1426 goto eremove;
1427 }
Jean Delvare378933c2009-12-09 20:36:06 +01001428
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001429 data->hwmon_dev = hwmon_device_register(&client->dev);
1430 if (IS_ERR(data->hwmon_dev)) {
1431 ret = PTR_ERR(data->hwmon_dev);
1432 goto eremove;
1433 }
1434
Jean Delvared07ca4a2009-12-09 20:36:07 +01001435 dev_info(&client->dev, "%s device, revision %d\n",
1436 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001437 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001438 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001439 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001440 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001441 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001442 data->has_pwm2 ? " pwm2" : "",
1443 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001444 if (data->bypass_attn)
1445 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1446 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1447 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1448 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1449 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001450
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001451 return 0;
1452
1453eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001454 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001455 return ret;
1456}
1457
1458static int adt7475_remove(struct i2c_client *client)
1459{
1460 struct adt7475_data *data = i2c_get_clientdata(client);
1461
1462 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001463 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001464
1465 return 0;
1466}
1467
1468static struct i2c_driver adt7475_driver = {
1469 .class = I2C_CLASS_HWMON,
1470 .driver = {
1471 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001472 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001473 },
1474 .probe = adt7475_probe,
1475 .remove = adt7475_remove,
1476 .id_table = adt7475_id,
1477 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001478 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001479};
1480
1481static void adt7475_read_hystersis(struct i2c_client *client)
1482{
1483 struct adt7475_data *data = i2c_get_clientdata(client);
1484
1485 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1486 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1487 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1488}
1489
1490static void adt7475_read_pwm(struct i2c_client *client, int index)
1491{
1492 struct adt7475_data *data = i2c_get_clientdata(client);
1493 unsigned int v;
1494
1495 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1496
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001497 /*
1498 * Figure out the internal value for pwmctrl and pwmchan
1499 * based on the current settings
1500 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001501 v = (data->pwm[CONTROL][index] >> 5) & 7;
1502
1503 if (v == 3)
1504 data->pwmctl[index] = 0;
1505 else if (v == 7)
1506 data->pwmctl[index] = 1;
1507 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001508 /*
1509 * The fan is disabled - we don't want to
1510 * support that, so change to manual mode and
1511 * set the duty cycle to 0 instead
1512 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001513 data->pwm[INPUT][index] = 0;
1514 data->pwm[CONTROL][index] &= ~0xE0;
1515 data->pwm[CONTROL][index] |= (7 << 5);
1516
1517 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1518 data->pwm[INPUT][index]);
1519
1520 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1521 data->pwm[CONTROL][index]);
1522
1523 data->pwmctl[index] = 1;
1524 } else {
1525 data->pwmctl[index] = 2;
1526
1527 switch (v) {
1528 case 0:
1529 data->pwmchan[index] = 1;
1530 break;
1531 case 1:
1532 data->pwmchan[index] = 2;
1533 break;
1534 case 2:
1535 data->pwmchan[index] = 4;
1536 break;
1537 case 5:
1538 data->pwmchan[index] = 6;
1539 break;
1540 case 6:
1541 data->pwmchan[index] = 7;
1542 break;
1543 }
1544 }
1545}
1546
1547static struct adt7475_data *adt7475_update_device(struct device *dev)
1548{
1549 struct i2c_client *client = to_i2c_client(dev);
1550 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001551 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001552 int i;
1553
1554 mutex_lock(&data->lock);
1555
1556 /* Measurement values update every 2 seconds */
1557 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1558 !data->valid) {
1559 data->alarms = adt7475_read(REG_STATUS2) << 8;
1560 data->alarms |= adt7475_read(REG_STATUS1);
1561
Jean Delvare3d849982009-12-09 20:36:05 +01001562 ext = (adt7475_read(REG_EXTEND2) << 8) |
1563 adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001564 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1565 if (!(data->has_voltage & (1 << i)))
1566 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001567 data->voltage[INPUT][i] =
1568 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001569 ((ext >> (i * 2)) & 3);
1570 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001571
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001572 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1573 data->temp[INPUT][i] =
1574 (adt7475_read(TEMP_REG(i)) << 2) |
Jean Delvare3d849982009-12-09 20:36:05 +01001575 ((ext >> ((i + 5) * 2)) & 3);
1576
1577 if (data->has_voltage & (1 << 5)) {
1578 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1579 ext = adt7475_read(REG_EXTEND3);
1580 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1581 ((ext >> 4) & 3);
1582 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001583
Jean Delvare378933c2009-12-09 20:36:06 +01001584 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1585 if (i == 3 && !data->has_fan4)
1586 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001587 data->tach[INPUT][i] =
1588 adt7475_read_word(client, TACH_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001589 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001590
1591 /* Updated by hw when in auto mode */
Jean Delvare378933c2009-12-09 20:36:06 +01001592 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1593 if (i == 1 && !data->has_pwm2)
1594 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001595 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001596 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001597
Jean Delvare54fe4672009-12-09 20:36:08 +01001598 if (data->has_vid)
1599 data->vid = adt7475_read(REG_VID) & 0x3f;
1600
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001601 data->measure_updated = jiffies;
1602 }
1603
1604 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001605 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001606 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001607 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001608 data->config5 = adt7475_read(REG_CONFIG5);
1609
1610 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001611 if (!(data->has_voltage & (1 << i)))
1612 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001613 /* Adjust values so they match the input precision */
1614 data->voltage[MIN][i] =
1615 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1616 data->voltage[MAX][i] =
1617 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1618 }
1619
Jean Delvare3d849982009-12-09 20:36:05 +01001620 if (data->has_voltage & (1 << 5)) {
1621 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1622 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1623 }
1624
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001625 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1626 /* Adjust values so they match the input precision */
1627 data->temp[MIN][i] =
1628 adt7475_read(TEMP_MIN_REG(i)) << 2;
1629 data->temp[MAX][i] =
1630 adt7475_read(TEMP_MAX_REG(i)) << 2;
1631 data->temp[AUTOMIN][i] =
1632 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1633 data->temp[THERM][i] =
1634 adt7475_read(TEMP_THERM_REG(i)) << 2;
1635 data->temp[OFFSET][i] =
1636 adt7475_read(TEMP_OFFSET_REG(i));
1637 }
1638 adt7475_read_hystersis(client);
1639
Jean Delvare378933c2009-12-09 20:36:06 +01001640 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1641 if (i == 3 && !data->has_fan4)
1642 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001643 data->tach[MIN][i] =
1644 adt7475_read_word(client, TACH_MIN_REG(i));
Jean Delvare378933c2009-12-09 20:36:06 +01001645 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001646
1647 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
Jean Delvare378933c2009-12-09 20:36:06 +01001648 if (i == 1 && !data->has_pwm2)
1649 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001650 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1651 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1652 /* Set the channel and control information */
1653 adt7475_read_pwm(client, i);
1654 }
1655
1656 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1657 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1658 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1659
1660 data->limits_updated = jiffies;
1661 data->valid = 1;
1662 }
1663
1664 mutex_unlock(&data->lock);
1665
1666 return data;
1667}
1668
Axel Linf0967ee2012-01-20 15:38:18 +08001669module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001670
1671MODULE_AUTHOR("Advanced Micro Devices, Inc");
1672MODULE_DESCRIPTION("adt7475 driver");
1673MODULE_LICENSE("GPL");