blob: 0dbb8df74e44a702b82a179b19fc0234fe1f14db [file] [log] [blame]
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
Jean Delvare7c81c602014-01-29 20:40:08 +01006 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
Jean Delvare3d849982009-12-09 20:36:05 +01007 *
Jordan Crouse1c301fc2009-01-15 22:27:47 +01008 * Derived from the lm83 driver by Jean Delvare
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -030016#include <linux/of_device.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010017#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/i2c.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
Jean Delvare54fe4672009-12-09 20:36:08 +010022#include <linux/hwmon-vid.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010023#include <linux/err.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020024#include <linux/jiffies.h>
Chris Packhame4651642017-05-11 15:45:21 +120025#include <linux/util_macros.h>
Jordan Crouse1c301fc2009-01-15 22:27:47 +010026
27/* Indexes for the sysfs hooks */
28
29#define INPUT 0
30#define MIN 1
31#define MAX 2
32#define CONTROL 3
33#define OFFSET 3
34#define AUTOMIN 4
35#define THERM 5
36#define HYSTERSIS 6
37
Guenter Roeck9ed5bc22012-01-19 11:02:15 -080038/*
39 * These are unique identifiers for the sysfs functions - unlike the
40 * numbers above, these are not also indexes into an array
41 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010042
43#define ALARM 9
44#define FAULT 10
45
46/* 7475 Common Registers */
47
Jean Delvared07ca4a2009-12-09 20:36:07 +010048#define REG_DEVREV2 0x12 /* ADT7490 only */
49
Jean Delvare3d849982009-12-09 20:36:05 +010050#define REG_VTT 0x1E /* ADT7490 only */
51#define REG_EXTEND3 0x1F /* ADT7490 only */
52
Jean Delvarecffb9dd2009-12-09 20:36:03 +010053#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010054#define REG_TEMP_BASE 0x25
55#define REG_TACH_BASE 0x28
56#define REG_PWM_BASE 0x30
57#define REG_PWM_MAX_BASE 0x38
58
59#define REG_DEVID 0x3D
60#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010061#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010062
Chris Packham4abdf382017-04-22 07:08:09 +120063#define REG_CONFIG1 0x40
64
Jordan Crouse1c301fc2009-01-15 22:27:47 +010065#define REG_STATUS1 0x41
66#define REG_STATUS2 0x42
67
Jean Delvared8d2ee02009-12-09 20:36:08 +010068#define REG_VID 0x43 /* ADT7476 only */
69
Jean Delvarecffb9dd2009-12-09 20:36:03 +010070#define REG_VOLTAGE_MIN_BASE 0x44
71#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010072
73#define REG_TEMP_MIN_BASE 0x4E
74#define REG_TEMP_MAX_BASE 0x4F
75
76#define REG_TACH_MIN_BASE 0x54
77
78#define REG_PWM_CONFIG_BASE 0x5C
79
80#define REG_TEMP_TRANGE_BASE 0x5F
81
Chris Packham1d58f5e2017-05-15 13:30:27 +120082#define REG_ENHANCE_ACOUSTICS1 0x62
83#define REG_ENHANCE_ACOUSTICS2 0x63
84
Jordan Crouse1c301fc2009-01-15 22:27:47 +010085#define REG_PWM_MIN_BASE 0x64
86
87#define REG_TEMP_TMIN_BASE 0x67
88#define REG_TEMP_THERM_BASE 0x6A
89
90#define REG_REMOTE1_HYSTERSIS 0x6D
91#define REG_REMOTE2_HYSTERSIS 0x6E
92
93#define REG_TEMP_OFFSET_BASE 0x70
94
Jean Delvareebfaf1f2009-12-09 20:36:07 +010095#define REG_CONFIG2 0x73
96
Jordan Crouse1c301fc2009-01-15 22:27:47 +010097#define REG_EXTEND1 0x76
98#define REG_EXTEND2 0x77
Jean Delvare378933c2009-12-09 20:36:06 +010099
100#define REG_CONFIG3 0x78
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100101#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +0100102#define REG_CONFIG4 0x7D
103
Jean Delvare3d849982009-12-09 20:36:05 +0100104#define REG_STATUS4 0x81 /* ADT7490 only */
105
106#define REG_VTT_MIN 0x84 /* ADT7490 only */
107#define REG_VTT_MAX 0x86 /* ADT7490 only */
108
Jean Delvared8d2ee02009-12-09 20:36:08 +0100109#define VID_VIDSEL 0x80 /* ADT7476 only */
110
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100111#define CONFIG2_ATTN 0x20
112
Jean Delvare378933c2009-12-09 20:36:06 +0100113#define CONFIG3_SMBALERT 0x01
114#define CONFIG3_THERM 0x02
115
116#define CONFIG4_PINFUNC 0x03
Jean Delvaref99318b2009-12-09 20:36:03 +0100117#define CONFIG4_MAXDUTY 0x08
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100118#define CONFIG4_ATTN_IN10 0x30
119#define CONFIG4_ATTN_IN43 0xC0
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100120
121#define CONFIG5_TWOSCOMP 0x01
122#define CONFIG5_TEMPOFFSET 0x02
Jean Delvare54fe4672009-12-09 20:36:08 +0100123#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100124
125/* ADT7475 Settings */
126
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100127#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100128#define ADT7475_TEMP_COUNT 3
129#define ADT7475_TACH_COUNT 4
130#define ADT7475_PWM_COUNT 3
131
132/* Macro to read the registers */
133
134#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
135
136/* Macros to easily index the registers */
137
138#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
139#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
140
141#define PWM_REG(idx) (REG_PWM_BASE + (idx))
142#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
143#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
144#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
145
146#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
147#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
148#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
149
150#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
151#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
152#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
153#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
154#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
155#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
156#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
157
Jean Delvare918ee912010-10-28 20:31:50 +0200158static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100159
Jean Delvaree5e9f442009-12-14 21:17:27 +0100160enum chips { adt7473, adt7475, adt7476, adt7490 };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100161
162static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100163 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100164 { "adt7475", adt7475 },
Jean Delvared8d2ee02009-12-09 20:36:08 +0100165 { "adt7476", adt7476 },
Jean Delvare3d849982009-12-09 20:36:05 +0100166 { "adt7490", adt7490 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100167 { }
168};
169MODULE_DEVICE_TABLE(i2c, adt7475_id);
170
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -0300171static const struct of_device_id adt7475_of_match[] = {
172 {
173 .compatible = "adi,adt7473",
174 .data = (void *)adt7473
175 },
176 {
177 .compatible = "adi,adt7475",
178 .data = (void *)adt7475
179 },
180 {
181 .compatible = "adi,adt7476",
182 .data = (void *)adt7476
183 },
184 {
185 .compatible = "adi,adt7490",
186 .data = (void *)adt7490
187 },
188 { },
189};
190MODULE_DEVICE_TABLE(of, adt7475_of_match);
191
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100192struct adt7475_data {
193 struct device *hwmon_dev;
194 struct mutex lock;
195
196 unsigned long measure_updated;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +0900197 bool valid;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100198
Jean Delvaref99318b2009-12-09 20:36:03 +0100199 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100200 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100201 u8 has_voltage;
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100202 u8 bypass_attn; /* Bypass voltage attenuator */
Jean Delvare378933c2009-12-09 20:36:06 +0100203 u8 has_pwm2:1;
204 u8 has_fan4:1;
Jean Delvare54fe4672009-12-09 20:36:08 +0100205 u8 has_vid:1;
Jean Delvare3d849982009-12-09 20:36:05 +0100206 u32 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100207 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100208 u16 temp[7][3];
209 u16 tach[2][4];
210 u8 pwm[4][3];
211 u8 range[3];
212 u8 pwmctl[3];
213 u8 pwmchan[3];
Chris Packham1d58f5e2017-05-15 13:30:27 +1200214 u8 enh_acoustics[2];
Jean Delvare54fe4672009-12-09 20:36:08 +0100215
216 u8 vid;
217 u8 vrm;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100218};
219
220static struct i2c_driver adt7475_driver;
221static struct adt7475_data *adt7475_update_device(struct device *dev);
222static void adt7475_read_hystersis(struct i2c_client *client);
223static void adt7475_read_pwm(struct i2c_client *client, int index);
224
225/* Given a temp value, convert it to register value */
226
227static inline u16 temp2reg(struct adt7475_data *data, long val)
228{
229 u16 ret;
230
231 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800232 val = clamp_val(val, -64000, 191000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100233 ret = (val + 64500) / 1000;
234 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800235 val = clamp_val(val, -128000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100236 if (val < -500)
237 ret = (256500 + val) / 1000;
238 else
239 ret = (val + 500) / 1000;
240 }
241
242 return ret << 2;
243}
244
245/* Given a register value, convert it to a real temp value */
246
247static inline int reg2temp(struct adt7475_data *data, u16 reg)
248{
249 if (data->config5 & CONFIG5_TWOSCOMP) {
250 if (reg >= 512)
251 return (reg - 1024) * 250;
252 else
253 return reg * 250;
254 } else
255 return (reg - 256) * 250;
256}
257
258static inline int tach2rpm(u16 tach)
259{
260 if (tach == 0 || tach == 0xFFFF)
261 return 0;
262
263 return (90000 * 60) / tach;
264}
265
266static inline u16 rpm2tach(unsigned long rpm)
267{
268 if (rpm == 0)
269 return 0;
270
Guenter Roeck2a844c12013-01-09 08:09:34 -0800271 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100272}
273
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100274/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
275static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
276 { 45, 94 }, /* +2.5V */
277 { 175, 525 }, /* Vccp */
278 { 68, 71 }, /* Vcc */
279 { 93, 47 }, /* +5V */
280 { 120, 20 }, /* +12V */
281 { 45, 45 }, /* Vtt */
282};
283
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100284static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100285{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100286 const int *r = adt7473_in_scaling[channel];
287
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100288 if (bypass_attn & (1 << channel))
289 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100290 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100291}
292
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100293static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100294{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100295 const int *r = adt7473_in_scaling[channel];
296 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100297
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100298 if (bypass_attn & (1 << channel))
299 reg = (volt * 1024) / 2250;
300 else
301 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800302 return clamp_val(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100303}
304
Dan Carpenterf196dec2018-08-14 13:07:47 +0300305static int adt7475_read_word(struct i2c_client *client, int reg)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100306{
Dan Carpenterf196dec2018-08-14 13:07:47 +0300307 int val1, val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100308
Dan Carpenterf196dec2018-08-14 13:07:47 +0300309 val1 = i2c_smbus_read_byte_data(client, reg);
310 if (val1 < 0)
311 return val1;
312 val2 = i2c_smbus_read_byte_data(client, reg + 1);
313 if (val2 < 0)
314 return val2;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100315
Dan Carpenterf196dec2018-08-14 13:07:47 +0300316 return val1 | (val2 << 8);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100317}
318
319static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
320{
321 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
322 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
323}
324
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800325static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100326 char *buf)
327{
328 struct adt7475_data *data = adt7475_update_device(dev);
329 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
330 unsigned short val;
331
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900332 if (IS_ERR(data))
333 return PTR_ERR(data);
334
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100335 switch (sattr->nr) {
336 case ALARM:
337 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100338 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100339 default:
340 val = data->voltage[sattr->nr][sattr->index];
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100341 return sprintf(buf, "%d\n",
342 reg2volt(sattr->index, val, data->bypass_attn));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100343 }
344}
345
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800346static ssize_t voltage_store(struct device *dev,
347 struct device_attribute *attr, const char *buf,
348 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100349{
350
351 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
352 struct i2c_client *client = to_i2c_client(dev);
353 struct adt7475_data *data = i2c_get_clientdata(client);
354 unsigned char reg;
355 long val;
356
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100357 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100358 return -EINVAL;
359
360 mutex_lock(&data->lock);
361
Jean Delvareebfaf1f2009-12-09 20:36:07 +0100362 data->voltage[sattr->nr][sattr->index] =
363 volt2reg(sattr->index, val, data->bypass_attn);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100364
Jean Delvare3d849982009-12-09 20:36:05 +0100365 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
366 if (sattr->nr == MIN)
367 reg = VOLTAGE_MIN_REG(sattr->index);
368 else
369 reg = VOLTAGE_MAX_REG(sattr->index);
370 } else {
371 if (sattr->nr == MIN)
372 reg = REG_VTT_MIN;
373 else
374 reg = REG_VTT_MAX;
375 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100376
377 i2c_smbus_write_byte_data(client, reg,
378 data->voltage[sattr->nr][sattr->index] >> 2);
379 mutex_unlock(&data->lock);
380
381 return count;
382}
383
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800384static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100385 char *buf)
386{
387 struct adt7475_data *data = adt7475_update_device(dev);
388 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
389 int out;
390
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900391 if (IS_ERR(data))
392 return PTR_ERR(data);
393
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100394 switch (sattr->nr) {
395 case HYSTERSIS:
396 mutex_lock(&data->lock);
397 out = data->temp[sattr->nr][sattr->index];
398 if (sattr->index != 1)
399 out = (out >> 4) & 0xF;
400 else
401 out = (out & 0xF);
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800402 /*
403 * Show the value as an absolute number tied to
404 * THERM
405 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100406 out = reg2temp(data, data->temp[THERM][sattr->index]) -
407 out * 1000;
408 mutex_unlock(&data->lock);
409 break;
410
411 case OFFSET:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800412 /*
413 * Offset is always 2's complement, regardless of the
414 * setting in CONFIG5
415 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100416 mutex_lock(&data->lock);
417 out = (s8)data->temp[sattr->nr][sattr->index];
418 if (data->config5 & CONFIG5_TEMPOFFSET)
419 out *= 1000;
420 else
421 out *= 500;
422 mutex_unlock(&data->lock);
423 break;
424
425 case ALARM:
426 out = (data->alarms >> (sattr->index + 4)) & 1;
427 break;
428
429 case FAULT:
430 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100431 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100432 break;
433
434 default:
435 /* All other temp values are in the configured format */
436 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
437 }
438
439 return sprintf(buf, "%d\n", out);
440}
441
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800442static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
443 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100444{
445 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
446 struct i2c_client *client = to_i2c_client(dev);
447 struct adt7475_data *data = i2c_get_clientdata(client);
448 unsigned char reg = 0;
449 u8 out;
450 int temp;
451 long val;
452
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100453 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100454 return -EINVAL;
455
456 mutex_lock(&data->lock);
457
458 /* We need the config register in all cases for temp <-> reg conv. */
459 data->config5 = adt7475_read(REG_CONFIG5);
460
461 switch (sattr->nr) {
462 case OFFSET:
463 if (data->config5 & CONFIG5_TEMPOFFSET) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800464 val = clamp_val(val, -63000, 127000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100465 out = data->temp[OFFSET][sattr->index] = val / 1000;
466 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800467 val = clamp_val(val, -63000, 64000);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100468 out = data->temp[OFFSET][sattr->index] = val / 500;
469 }
470 break;
471
472 case HYSTERSIS:
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800473 /*
474 * The value will be given as an absolute value, turn it
475 * into an offset based on THERM
476 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100477
478 /* Read fresh THERM and HYSTERSIS values from the chip */
479 data->temp[THERM][sattr->index] =
480 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
481 adt7475_read_hystersis(client);
482
483 temp = reg2temp(data, data->temp[THERM][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800484 val = clamp_val(val, temp - 15000, temp);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100485 val = (temp - val) / 1000;
486
487 if (sattr->index != 1) {
488 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
489 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
490 } else {
491 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
492 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
493 }
494
495 out = data->temp[HYSTERSIS][sattr->index];
496 break;
497
498 default:
499 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
500
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800501 /*
502 * We maintain an extra 2 digits of precision for simplicity
503 * - shift those back off before writing the value
504 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100505 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
506 }
507
508 switch (sattr->nr) {
509 case MIN:
510 reg = TEMP_MIN_REG(sattr->index);
511 break;
512 case MAX:
513 reg = TEMP_MAX_REG(sattr->index);
514 break;
515 case OFFSET:
516 reg = TEMP_OFFSET_REG(sattr->index);
517 break;
518 case AUTOMIN:
519 reg = TEMP_TMIN_REG(sattr->index);
520 break;
521 case THERM:
522 reg = TEMP_THERM_REG(sattr->index);
523 break;
524 case HYSTERSIS:
525 if (sattr->index != 2)
526 reg = REG_REMOTE1_HYSTERSIS;
527 else
528 reg = REG_REMOTE2_HYSTERSIS;
529
530 break;
531 }
532
533 i2c_smbus_write_byte_data(client, reg, out);
534
535 mutex_unlock(&data->lock);
536 return count;
537}
538
Chris Packham8f05bcc2017-05-15 13:30:28 +1200539/* Assuming CONFIG6[SLOW] is 0 */
540static const int ad7475_st_map[] = {
541 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
542};
543
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800544static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
545 char *buf)
Chris Packham8f05bcc2017-05-15 13:30:28 +1200546{
547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 struct i2c_client *client = to_i2c_client(dev);
549 struct adt7475_data *data = i2c_get_clientdata(client);
550 long val;
551
552 switch (sattr->index) {
553 case 0:
554 val = data->enh_acoustics[0] & 0xf;
555 break;
556 case 1:
557 val = (data->enh_acoustics[1] >> 4) & 0xf;
558 break;
559 case 2:
560 default:
561 val = data->enh_acoustics[1] & 0xf;
562 break;
563 }
564
565 if (val & 0x8)
566 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
567 else
568 return sprintf(buf, "0\n");
569}
570
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800571static ssize_t temp_st_store(struct device *dev,
572 struct device_attribute *attr, const char *buf,
573 size_t count)
Chris Packham8f05bcc2017-05-15 13:30:28 +1200574{
575 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
576 struct i2c_client *client = to_i2c_client(dev);
577 struct adt7475_data *data = i2c_get_clientdata(client);
578 unsigned char reg;
579 int shift, idx;
580 ulong val;
581
582 if (kstrtoul(buf, 10, &val))
583 return -EINVAL;
584
585 switch (sattr->index) {
586 case 0:
587 reg = REG_ENHANCE_ACOUSTICS1;
588 shift = 0;
589 idx = 0;
590 break;
591 case 1:
592 reg = REG_ENHANCE_ACOUSTICS2;
593 shift = 0;
594 idx = 1;
595 break;
596 case 2:
597 default:
598 reg = REG_ENHANCE_ACOUSTICS2;
599 shift = 4;
600 idx = 1;
601 break;
602 }
603
604 if (val > 0) {
605 val = find_closest_descending(val, ad7475_st_map,
606 ARRAY_SIZE(ad7475_st_map));
607 val |= 0x8;
608 }
609
610 mutex_lock(&data->lock);
611
612 data->enh_acoustics[idx] &= ~(0xf << shift);
613 data->enh_acoustics[idx] |= (val << shift);
614
615 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
616
617 mutex_unlock(&data->lock);
618
619 return count;
620}
621
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800622/*
623 * Table of autorange values - the user will write the value in millidegrees,
624 * and we'll convert it
625 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100626static const int autorange_table[] = {
627 2000, 2500, 3330, 4000, 5000, 6670, 8000,
628 10000, 13330, 16000, 20000, 26670, 32000, 40000,
629 53330, 80000
630};
631
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800632static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100633 char *buf)
634{
635 struct adt7475_data *data = adt7475_update_device(dev);
636 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
637 int out, val;
638
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900639 if (IS_ERR(data))
640 return PTR_ERR(data);
641
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100642 mutex_lock(&data->lock);
643 out = (data->range[sattr->index] >> 4) & 0x0F;
644 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
645 mutex_unlock(&data->lock);
646
647 return sprintf(buf, "%d\n", val + autorange_table[out]);
648}
649
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800650static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100652{
653 struct i2c_client *client = to_i2c_client(dev);
654 struct adt7475_data *data = i2c_get_clientdata(client);
655 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
656 int temp;
657 long val;
658
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100659 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100660 return -EINVAL;
661
662 mutex_lock(&data->lock);
663
664 /* Get a fresh copy of the needed registers */
665 data->config5 = adt7475_read(REG_CONFIG5);
666 data->temp[AUTOMIN][sattr->index] =
667 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
668 data->range[sattr->index] =
669 adt7475_read(TEMP_TRANGE_REG(sattr->index));
670
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800671 /*
672 * The user will write an absolute value, so subtract the start point
673 * to figure the range
674 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100675 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800676 val = clamp_val(val, temp + autorange_table[0],
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100677 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
678 val -= temp;
679
680 /* Find the nearest table entry to what the user wrote */
Chris Packhame4651642017-05-11 15:45:21 +1200681 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100682
683 data->range[sattr->index] &= ~0xF0;
684 data->range[sattr->index] |= val << 4;
685
686 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
687 data->range[sattr->index]);
688
689 mutex_unlock(&data->lock);
690 return count;
691}
692
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800693static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100694 char *buf)
695{
696 struct adt7475_data *data = adt7475_update_device(dev);
697 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
698 int out;
699
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900700 if (IS_ERR(data))
701 return PTR_ERR(data);
702
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100703 if (sattr->nr == ALARM)
704 out = (data->alarms >> (sattr->index + 10)) & 1;
705 else
706 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
707
708 return sprintf(buf, "%d\n", out);
709}
710
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800711static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100713{
714
715 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
716 struct i2c_client *client = to_i2c_client(dev);
717 struct adt7475_data *data = i2c_get_clientdata(client);
718 unsigned long val;
719
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100720 if (kstrtoul(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100721 return -EINVAL;
722
723 mutex_lock(&data->lock);
724
725 data->tach[MIN][sattr->index] = rpm2tach(val);
726
727 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
728 data->tach[MIN][sattr->index]);
729
730 mutex_unlock(&data->lock);
731 return count;
732}
733
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800734static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100735 char *buf)
736{
737 struct adt7475_data *data = adt7475_update_device(dev);
738 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
739
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900740 if (IS_ERR(data))
741 return PTR_ERR(data);
742
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100743 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
744}
745
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800746static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100747 char *buf)
748{
749 struct adt7475_data *data = adt7475_update_device(dev);
750 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
751
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900752 if (IS_ERR(data))
753 return PTR_ERR(data);
754
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100755 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
756}
757
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800758static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100759 char *buf)
760{
761 struct adt7475_data *data = adt7475_update_device(dev);
762 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
763
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900764 if (IS_ERR(data))
765 return PTR_ERR(data);
766
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100767 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
768}
769
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800770static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100772{
773
774 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
775 struct i2c_client *client = to_i2c_client(dev);
776 struct adt7475_data *data = i2c_get_clientdata(client);
777 unsigned char reg = 0;
778 long val;
779
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100780 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100781 return -EINVAL;
782
783 mutex_lock(&data->lock);
784
785 switch (sattr->nr) {
786 case INPUT:
787 /* Get a fresh value for CONTROL */
788 data->pwm[CONTROL][sattr->index] =
789 adt7475_read(PWM_CONFIG_REG(sattr->index));
790
Guenter Roeck9ed5bc22012-01-19 11:02:15 -0800791 /*
792 * If we are not in manual mode, then we shouldn't allow
793 * the user to set the pwm speed
794 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100795 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
796 mutex_unlock(&data->lock);
797 return count;
798 }
799
800 reg = PWM_REG(sattr->index);
801 break;
802
803 case MIN:
804 reg = PWM_MIN_REG(sattr->index);
805 break;
806
807 case MAX:
808 reg = PWM_MAX_REG(sattr->index);
809 break;
810 }
811
Guenter Roeck2a844c12013-01-09 08:09:34 -0800812 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100813 i2c_smbus_write_byte_data(client, reg,
814 data->pwm[sattr->nr][sattr->index]);
Chris Packham1d58f5e2017-05-15 13:30:27 +1200815 mutex_unlock(&data->lock);
816
817 return count;
818}
819
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800820static ssize_t stall_disable_show(struct device *dev,
Chris Packham1d58f5e2017-05-15 13:30:27 +1200821 struct device_attribute *attr, char *buf)
822{
823 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
824 struct i2c_client *client = to_i2c_client(dev);
825 struct adt7475_data *data = i2c_get_clientdata(client);
826 u8 mask = BIT(5 + sattr->index);
827
828 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
829}
830
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800831static ssize_t stall_disable_store(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf, size_t count)
Chris Packham1d58f5e2017-05-15 13:30:27 +1200834{
835 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
836 struct i2c_client *client = to_i2c_client(dev);
837 struct adt7475_data *data = i2c_get_clientdata(client);
838 long val;
839 u8 mask = BIT(5 + sattr->index);
840
841 if (kstrtol(buf, 10, &val))
842 return -EINVAL;
843
844 mutex_lock(&data->lock);
845
846 data->enh_acoustics[0] &= ~mask;
847 if (val)
848 data->enh_acoustics[0] |= mask;
849
850 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
851 data->enh_acoustics[0]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100852
853 mutex_unlock(&data->lock);
854
855 return count;
856}
857
858/* Called by set_pwmctrl and set_pwmchan */
859
860static int hw_set_pwm(struct i2c_client *client, int index,
861 unsigned int pwmctl, unsigned int pwmchan)
862{
863 struct adt7475_data *data = i2c_get_clientdata(client);
864 long val = 0;
865
866 switch (pwmctl) {
867 case 0:
868 val = 0x03; /* Run at full speed */
869 break;
870 case 1:
871 val = 0x07; /* Manual mode */
872 break;
873 case 2:
874 switch (pwmchan) {
875 case 1:
876 /* Remote1 controls PWM */
877 val = 0x00;
878 break;
879 case 2:
880 /* local controls PWM */
881 val = 0x01;
882 break;
883 case 4:
884 /* remote2 controls PWM */
885 val = 0x02;
886 break;
887 case 6:
888 /* local/remote2 control PWM */
889 val = 0x05;
890 break;
891 case 7:
892 /* All three control PWM */
893 val = 0x06;
894 break;
895 default:
896 return -EINVAL;
897 }
898 break;
899 default:
900 return -EINVAL;
901 }
902
903 data->pwmctl[index] = pwmctl;
904 data->pwmchan[index] = pwmchan;
905
906 data->pwm[CONTROL][index] &= ~0xE0;
907 data->pwm[CONTROL][index] |= (val & 7) << 5;
908
909 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
910 data->pwm[CONTROL][index]);
911
912 return 0;
913}
914
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800915static ssize_t pwmchan_store(struct device *dev,
916 struct device_attribute *attr, const char *buf,
917 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100918{
919 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
920 struct i2c_client *client = to_i2c_client(dev);
921 struct adt7475_data *data = i2c_get_clientdata(client);
922 int r;
923 long val;
924
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100925 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100926 return -EINVAL;
927
928 mutex_lock(&data->lock);
929 /* Read Modify Write PWM values */
930 adt7475_read_pwm(client, sattr->index);
931 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
932 if (r)
933 count = r;
934 mutex_unlock(&data->lock);
935
936 return count;
937}
938
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800939static ssize_t pwmctrl_store(struct device *dev,
940 struct device_attribute *attr, const char *buf,
941 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100942{
943 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
944 struct i2c_client *client = to_i2c_client(dev);
945 struct adt7475_data *data = i2c_get_clientdata(client);
946 int r;
947 long val;
948
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100949 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100950 return -EINVAL;
951
952 mutex_lock(&data->lock);
953 /* Read Modify Write PWM values */
954 adt7475_read_pwm(client, sattr->index);
955 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
956 if (r)
957 count = r;
958 mutex_unlock(&data->lock);
959
960 return count;
961}
962
963/* List of frequencies for the PWM */
964static const int pwmfreq_table[] = {
Chris Packham3490c922017-05-15 13:30:29 +1200965 11, 14, 22, 29, 35, 44, 58, 88, 22500
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100966};
967
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800968static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100969 char *buf)
970{
971 struct adt7475_data *data = adt7475_update_device(dev);
972 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
Dan Carpenter9d193712018-08-14 12:12:36 +0300973 int idx;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100974
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900975 if (IS_ERR(data))
976 return PTR_ERR(data);
Dan Carpenter9d193712018-08-14 12:12:36 +0300977 idx = clamp_val(data->range[sattr->index] & 0xf, 0,
978 ARRAY_SIZE(pwmfreq_table) - 1);
Tokunori Ikegami4afec792018-08-08 10:32:19 +0900979
Dan Carpenter9d193712018-08-14 12:12:36 +0300980 return sprintf(buf, "%d\n", pwmfreq_table[idx]);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100981}
982
Guenter Roeckc24f9ba2018-12-10 14:02:02 -0800983static ssize_t pwmfreq_store(struct device *dev,
984 struct device_attribute *attr, const char *buf,
985 size_t count)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100986{
987 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
988 struct i2c_client *client = to_i2c_client(dev);
989 struct adt7475_data *data = i2c_get_clientdata(client);
990 int out;
991 long val;
992
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100993 if (kstrtol(buf, 10, &val))
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100994 return -EINVAL;
995
Chris Packhame4651642017-05-11 15:45:21 +1200996 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100997
998 mutex_lock(&data->lock);
999
1000 data->range[sattr->index] =
1001 adt7475_read(TEMP_TRANGE_REG(sattr->index));
Chris Packham3490c922017-05-15 13:30:29 +12001002 data->range[sattr->index] &= ~0xf;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001003 data->range[sattr->index] |= out;
1004
1005 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
1006 data->range[sattr->index]);
1007
1008 mutex_unlock(&data->lock);
1009 return count;
1010}
1011
Julia Lawall1d05303c2016-12-22 13:05:33 +01001012static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1013 struct device_attribute *devattr,
1014 char *buf)
Jean Delvaref99318b2009-12-09 20:36:03 +01001015{
1016 struct adt7475_data *data = adt7475_update_device(dev);
Dan Carpenter9d193712018-08-14 12:12:36 +03001017
1018 if (IS_ERR(data))
1019 return PTR_ERR(data);
1020
Jean Delvaref99318b2009-12-09 20:36:03 +01001021 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1022}
1023
Julia Lawall1d05303c2016-12-22 13:05:33 +01001024static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1025 struct device_attribute *devattr,
1026 const char *buf, size_t count)
Jean Delvaref99318b2009-12-09 20:36:03 +01001027{
1028 struct i2c_client *client = to_i2c_client(dev);
1029 struct adt7475_data *data = i2c_get_clientdata(client);
1030 long val;
1031
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001032 if (kstrtol(buf, 10, &val))
Jean Delvaref99318b2009-12-09 20:36:03 +01001033 return -EINVAL;
1034 if (val != 0 && val != 1)
1035 return -EINVAL;
1036
1037 mutex_lock(&data->lock);
1038 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1039 if (val)
1040 data->config4 |= CONFIG4_MAXDUTY;
1041 else
1042 data->config4 &= ~CONFIG4_MAXDUTY;
1043 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1044 mutex_unlock(&data->lock);
1045
1046 return count;
1047}
1048
Julia Lawall1d05303c2016-12-22 13:05:33 +01001049static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
Jean Delvare54fe4672009-12-09 20:36:08 +01001050 char *buf)
1051{
1052 struct adt7475_data *data = dev_get_drvdata(dev);
1053 return sprintf(buf, "%d\n", (int)data->vrm);
1054}
1055
Julia Lawall1d05303c2016-12-22 13:05:33 +01001056static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1057 const char *buf, size_t count)
Jean Delvare54fe4672009-12-09 20:36:08 +01001058{
1059 struct adt7475_data *data = dev_get_drvdata(dev);
1060 long val;
1061
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001062 if (kstrtol(buf, 10, &val))
Jean Delvare54fe4672009-12-09 20:36:08 +01001063 return -EINVAL;
1064 if (val < 0 || val > 255)
1065 return -EINVAL;
1066 data->vrm = val;
1067
1068 return count;
1069}
1070
Julia Lawall1d05303c2016-12-22 13:05:33 +01001071static ssize_t cpu0_vid_show(struct device *dev,
1072 struct device_attribute *devattr, char *buf)
Jean Delvare54fe4672009-12-09 20:36:08 +01001073{
1074 struct adt7475_data *data = adt7475_update_device(dev);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001075
1076 if (IS_ERR(data))
1077 return PTR_ERR(data);
1078
Jean Delvare54fe4672009-12-09 20:36:08 +01001079 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1080}
1081
Guenter Roeckc24f9ba2018-12-10 14:02:02 -08001082static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1083static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1084static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1085static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1086static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1087static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1088static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1089static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1090static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1091static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1092static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1093static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1094static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1095static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1096static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1097static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1098static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1099static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1100static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1101static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1102static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1103static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1104static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1105static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
1106static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1107static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1108static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1109static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1110static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1111static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1112static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1113static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1114static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1115static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1116static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1117static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1118static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1119static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1120static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1121static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1122static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1123static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1124static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1125static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1126static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1127static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1128static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1129static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1130static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1131static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1132static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1133static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1134static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1135static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1136static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1137static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1138static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1139static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1140static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1141static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1142static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1143static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1144static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1145static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1146static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1147static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1148static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1149static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1150static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1151static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1152static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1153static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1154static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1155static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1156static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1157static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1158static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1159static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1160static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1161static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1162static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1163static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1164static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1165static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1166static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1167static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1168static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1169static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1170static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001171
Jean Delvaref99318b2009-12-09 20:36:03 +01001172/* Non-standard name, might need revisiting */
Julia Lawall1d05303c2016-12-22 13:05:33 +01001173static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
Jean Delvaref99318b2009-12-09 20:36:03 +01001174
Julia Lawall1d05303c2016-12-22 13:05:33 +01001175static DEVICE_ATTR_RW(vrm);
1176static DEVICE_ATTR_RO(cpu0_vid);
Jean Delvare54fe4672009-12-09 20:36:08 +01001177
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001178static struct attribute *adt7475_attrs[] = {
1179 &sensor_dev_attr_in1_input.dev_attr.attr,
1180 &sensor_dev_attr_in1_max.dev_attr.attr,
1181 &sensor_dev_attr_in1_min.dev_attr.attr,
1182 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1183 &sensor_dev_attr_in2_input.dev_attr.attr,
1184 &sensor_dev_attr_in2_max.dev_attr.attr,
1185 &sensor_dev_attr_in2_min.dev_attr.attr,
1186 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1187 &sensor_dev_attr_temp1_input.dev_attr.attr,
1188 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1189 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1190 &sensor_dev_attr_temp1_max.dev_attr.attr,
1191 &sensor_dev_attr_temp1_min.dev_attr.attr,
1192 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1193 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1194 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1195 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1196 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001197 &sensor_dev_attr_temp1_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001198 &sensor_dev_attr_temp2_input.dev_attr.attr,
1199 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1200 &sensor_dev_attr_temp2_max.dev_attr.attr,
1201 &sensor_dev_attr_temp2_min.dev_attr.attr,
1202 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1203 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1204 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1205 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1206 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001207 &sensor_dev_attr_temp2_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001208 &sensor_dev_attr_temp3_input.dev_attr.attr,
1209 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1210 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1211 &sensor_dev_attr_temp3_max.dev_attr.attr,
1212 &sensor_dev_attr_temp3_min.dev_attr.attr,
1213 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1214 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1215 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1216 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1217 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
Chris Packham8f05bcc2017-05-15 13:30:28 +12001218 &sensor_dev_attr_temp3_smoothing.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001219 &sensor_dev_attr_fan1_input.dev_attr.attr,
1220 &sensor_dev_attr_fan1_min.dev_attr.attr,
1221 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1222 &sensor_dev_attr_fan2_input.dev_attr.attr,
1223 &sensor_dev_attr_fan2_min.dev_attr.attr,
1224 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1225 &sensor_dev_attr_fan3_input.dev_attr.attr,
1226 &sensor_dev_attr_fan3_min.dev_attr.attr,
1227 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001228 &sensor_dev_attr_pwm1.dev_attr.attr,
1229 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1230 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001231 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001232 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1233 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001234 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001235 &sensor_dev_attr_pwm3.dev_attr.attr,
1236 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1237 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001238 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001239 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1240 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001241 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001242 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001243 NULL,
1244};
1245
Jean Delvare378933c2009-12-09 20:36:06 +01001246static struct attribute *fan4_attrs[] = {
1247 &sensor_dev_attr_fan4_input.dev_attr.attr,
1248 &sensor_dev_attr_fan4_min.dev_attr.attr,
1249 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1250 NULL
1251};
1252
1253static struct attribute *pwm2_attrs[] = {
1254 &sensor_dev_attr_pwm2.dev_attr.attr,
1255 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1256 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1257 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1258 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1259 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Chris Packham1d58f5e2017-05-15 13:30:27 +12001260 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001261 NULL
1262};
1263
Jean Delvare378933c2009-12-09 20:36:06 +01001264static struct attribute *in0_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001265 &sensor_dev_attr_in0_input.dev_attr.attr,
1266 &sensor_dev_attr_in0_max.dev_attr.attr,
1267 &sensor_dev_attr_in0_min.dev_attr.attr,
1268 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare378933c2009-12-09 20:36:06 +01001269 NULL
1270};
1271
Jean Delvared8d2ee02009-12-09 20:36:08 +01001272static struct attribute *in3_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001273 &sensor_dev_attr_in3_input.dev_attr.attr,
1274 &sensor_dev_attr_in3_max.dev_attr.attr,
1275 &sensor_dev_attr_in3_min.dev_attr.attr,
1276 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001277 NULL
1278};
1279
1280static struct attribute *in4_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001281 &sensor_dev_attr_in4_input.dev_attr.attr,
1282 &sensor_dev_attr_in4_max.dev_attr.attr,
1283 &sensor_dev_attr_in4_min.dev_attr.attr,
1284 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001285 NULL
1286};
1287
1288static struct attribute *in5_attrs[] = {
Jean Delvare3d849982009-12-09 20:36:05 +01001289 &sensor_dev_attr_in5_input.dev_attr.attr,
1290 &sensor_dev_attr_in5_max.dev_attr.attr,
1291 &sensor_dev_attr_in5_min.dev_attr.attr,
1292 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1293 NULL
1294};
1295
Jean Delvare54fe4672009-12-09 20:36:08 +01001296static struct attribute *vid_attrs[] = {
1297 &dev_attr_cpu0_vid.attr,
1298 &dev_attr_vrm.attr,
1299 NULL
1300};
1301
Arvind Yadavf5397be2017-07-05 11:04:04 +05301302static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1303static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1304static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1305static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1306static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1307static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1308static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1309static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001310
Jean Delvare310ec792009-12-14 21:17:23 +01001311static int adt7475_detect(struct i2c_client *client,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001312 struct i2c_board_info *info)
1313{
1314 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001315 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001316 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001317
1318 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1319 return -ENODEV;
1320
Jean Delvareb180d052009-12-09 20:36:02 +01001321 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001322 devid2 = adt7475_read(REG_DEVID2);
1323 if (vendid != 0x41 || /* Analog Devices */
1324 (devid2 & 0xf8) != 0x68)
1325 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001326
Jean Delvared656b6f2009-12-09 20:36:04 +01001327 devid = adt7475_read(REG_DEVID);
1328 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001329 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001330 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001331 name = "adt7475";
Jean Delvared8d2ee02009-12-09 20:36:08 +01001332 else if (devid == 0x76)
1333 name = "adt7476";
Jean Delvare3d849982009-12-09 20:36:05 +01001334 else if ((devid2 & 0xfc) == 0x6c)
1335 name = "adt7490";
Jean Delvareb180d052009-12-09 20:36:02 +01001336 else {
1337 dev_dbg(&adapter->dev,
Jean Delvared8d2ee02009-12-09 20:36:08 +01001338 "Couldn't detect an ADT7473/75/76/90 part at "
Jean Delvareb180d052009-12-09 20:36:02 +01001339 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001340 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001341 }
1342
Jean Delvareb180d052009-12-09 20:36:02 +01001343 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001344
1345 return 0;
1346}
1347
Jean Delvare0f144802009-12-09 20:36:06 +01001348static void adt7475_remove_files(struct i2c_client *client,
1349 struct adt7475_data *data)
1350{
1351 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
Jean Delvare378933c2009-12-09 20:36:06 +01001352 if (data->has_fan4)
1353 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1354 if (data->has_pwm2)
1355 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1356 if (data->has_voltage & (1 << 0))
1357 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001358 if (data->has_voltage & (1 << 3))
1359 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1360 if (data->has_voltage & (1 << 4))
1361 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1362 if (data->has_voltage & (1 << 5))
1363 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
Jean Delvare54fe4672009-12-09 20:36:08 +01001364 if (data->has_vid)
1365 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
Jean Delvare0f144802009-12-09 20:36:06 +01001366}
1367
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001368static int adt7475_update_limits(struct i2c_client *client)
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001369{
1370 struct adt7475_data *data = i2c_get_clientdata(client);
1371 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001372 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001373
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001374 ret = adt7475_read(REG_CONFIG4);
1375 if (ret < 0)
1376 return ret;
1377 data->config4 = ret;
1378
1379 ret = adt7475_read(REG_CONFIG5);
1380 if (ret < 0)
1381 return ret;
1382 data->config5 = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001383
1384 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1385 if (!(data->has_voltage & (1 << i)))
1386 continue;
1387 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001388 ret = adt7475_read(VOLTAGE_MIN_REG(i));
1389 if (ret < 0)
1390 return ret;
1391 data->voltage[MIN][i] = ret << 2;
1392
1393 ret = adt7475_read(VOLTAGE_MAX_REG(i));
1394 if (ret < 0)
1395 return ret;
1396 data->voltage[MAX][i] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001397 }
1398
1399 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001400 ret = adt7475_read(REG_VTT_MIN);
1401 if (ret < 0)
1402 return ret;
1403 data->voltage[MIN][5] = ret << 2;
1404
1405 ret = adt7475_read(REG_VTT_MAX);
1406 if (ret < 0)
1407 return ret;
1408 data->voltage[MAX][5] = ret << 2;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001409 }
1410
1411 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1412 /* Adjust values so they match the input precision */
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001413 ret = adt7475_read(TEMP_MIN_REG(i));
1414 if (ret < 0)
1415 return ret;
1416 data->temp[MIN][i] = ret << 2;
1417
1418 ret = adt7475_read(TEMP_MAX_REG(i));
1419 if (ret < 0)
1420 return ret;
1421 data->temp[MAX][i] = ret << 2;
1422
1423 ret = adt7475_read(TEMP_TMIN_REG(i));
1424 if (ret < 0)
1425 return ret;
1426 data->temp[AUTOMIN][i] = ret << 2;
1427
1428 ret = adt7475_read(TEMP_THERM_REG(i));
1429 if (ret < 0)
1430 return ret;
1431 data->temp[THERM][i] = ret << 2;
1432
1433 ret = adt7475_read(TEMP_OFFSET_REG(i));
1434 if (ret < 0)
1435 return ret;
1436 data->temp[OFFSET][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001437 }
1438 adt7475_read_hystersis(client);
1439
1440 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1441 if (i == 3 && !data->has_fan4)
1442 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001443 ret = adt7475_read_word(client, TACH_MIN_REG(i));
1444 if (ret < 0)
1445 return ret;
1446 data->tach[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001447 }
1448
1449 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1450 if (i == 1 && !data->has_pwm2)
1451 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001452 ret = adt7475_read(PWM_MAX_REG(i));
1453 if (ret < 0)
1454 return ret;
1455 data->pwm[MAX][i] = ret;
1456
1457 ret = adt7475_read(PWM_MIN_REG(i));
1458 if (ret < 0)
1459 return ret;
1460 data->pwm[MIN][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001461 /* Set the channel and control information */
1462 adt7475_read_pwm(client, i);
1463 }
1464
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001465 ret = adt7475_read(TEMP_TRANGE_REG(0));
1466 if (ret < 0)
1467 return ret;
1468 data->range[0] = ret;
1469
1470 ret = adt7475_read(TEMP_TRANGE_REG(1));
1471 if (ret < 0)
1472 return ret;
1473 data->range[1] = ret;
1474
1475 ret = adt7475_read(TEMP_TRANGE_REG(2));
1476 if (ret < 0)
1477 return ret;
1478 data->range[2] = ret;
1479
1480 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001481}
1482
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001483static int adt7475_probe(struct i2c_client *client,
1484 const struct i2c_device_id *id)
1485{
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001486 enum chips chip;
Frans Meulenbroeks99b8c832012-01-08 19:34:08 +01001487 static const char * const names[] = {
Jean Delvared07ca4a2009-12-09 20:36:07 +01001488 [adt7473] = "ADT7473",
1489 [adt7475] = "ADT7475",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001490 [adt7476] = "ADT7476",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001491 [adt7490] = "ADT7490",
1492 };
1493
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001494 struct adt7475_data *data;
Jean Delvare378933c2009-12-09 20:36:06 +01001495 int i, ret = 0, revision;
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001496 u8 config2, config3;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001497
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001498 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001499 if (data == NULL)
1500 return -ENOMEM;
1501
1502 mutex_init(&data->lock);
1503 i2c_set_clientdata(client, data);
1504
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001505 if (client->dev.of_node)
1506 chip = (enum chips)of_device_get_match_data(&client->dev);
1507 else
1508 chip = id->driver_data;
1509
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001510 /* Initialize device-specific values */
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001511 switch (chip) {
Jean Delvared8d2ee02009-12-09 20:36:08 +01001512 case adt7476:
1513 data->has_voltage = 0x0e; /* in1 to in3 */
1514 revision = adt7475_read(REG_DEVID2) & 0x07;
1515 break;
Jean Delvare3d849982009-12-09 20:36:05 +01001516 case adt7490:
Jean Delvare378933c2009-12-09 20:36:06 +01001517 data->has_voltage = 0x3e; /* in1 to in5 */
1518 revision = adt7475_read(REG_DEVID2) & 0x03;
Jean Delvared07ca4a2009-12-09 20:36:07 +01001519 if (revision == 0x03)
1520 revision += adt7475_read(REG_DEVREV2);
Jean Delvare3d849982009-12-09 20:36:05 +01001521 break;
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001522 default:
1523 data->has_voltage = 0x06; /* in1, in2 */
Jean Delvare378933c2009-12-09 20:36:06 +01001524 revision = adt7475_read(REG_DEVID2) & 0x07;
1525 }
1526
1527 config3 = adt7475_read(REG_CONFIG3);
1528 /* Pin PWM2 may alternatively be used for ALERT output */
1529 if (!(config3 & CONFIG3_SMBALERT))
1530 data->has_pwm2 = 1;
1531 /* Meaning of this bit is inverted for the ADT7473-1 */
1532 if (id->driver_data == adt7473 && revision >= 1)
1533 data->has_pwm2 = !data->has_pwm2;
1534
1535 data->config4 = adt7475_read(REG_CONFIG4);
1536 /* Pin TACH4 may alternatively be used for THERM */
1537 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1538 data->has_fan4 = 1;
1539
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001540 /*
1541 * THERM configuration is more complex on the ADT7476 and ADT7490,
1542 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1543 * this function
1544 */
Jean Delvare378933c2009-12-09 20:36:06 +01001545 if (id->driver_data == adt7490) {
1546 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1547 !(config3 & CONFIG3_THERM))
1548 data->has_fan4 = 1;
Jean Delvared8d2ee02009-12-09 20:36:08 +01001549 }
1550 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
Jean Delvare378933c2009-12-09 20:36:06 +01001551 if (!(config3 & CONFIG3_THERM) ||
1552 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1553 data->has_voltage |= (1 << 0); /* in0 */
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001554 }
1555
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001556 /*
1557 * On the ADT7476, the +12V input pin may instead be used as VID5,
1558 * and VID pins may alternatively be used as GPIO
1559 */
Jean Delvared8d2ee02009-12-09 20:36:08 +01001560 if (id->driver_data == adt7476) {
1561 u8 vid = adt7475_read(REG_VID);
1562 if (!(vid & VID_VIDSEL))
1563 data->has_voltage |= (1 << 4); /* in4 */
Jean Delvare54fe4672009-12-09 20:36:08 +01001564
1565 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001566 }
1567
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001568 /* Voltage attenuators can be bypassed, globally or individually */
1569 config2 = adt7475_read(REG_CONFIG2);
1570 if (config2 & CONFIG2_ATTN) {
1571 data->bypass_attn = (0x3 << 3) | 0x3;
1572 } else {
1573 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1574 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1575 }
1576 data->bypass_attn &= data->has_voltage;
1577
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001578 /*
1579 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1580 * pwm's which are disabled to manual mode with 0% duty cycle
1581 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001582 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1583 adt7475_read_pwm(client, i);
1584
Chris Packham4abdf382017-04-22 07:08:09 +12001585 /* Start monitoring */
1586 switch (chip) {
1587 case adt7475:
1588 case adt7476:
1589 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1590 adt7475_read(REG_CONFIG1) | 0x01);
1591 break;
1592 default:
1593 break;
1594 }
1595
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001596 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1597 if (ret)
Guenter Roecke3ecb2e2012-06-02 09:58:01 -07001598 return ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001599
Jean Delvare378933c2009-12-09 20:36:06 +01001600 /* Features that can be disabled individually */
1601 if (data->has_fan4) {
1602 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1603 if (ret)
1604 goto eremove;
1605 }
1606 if (data->has_pwm2) {
1607 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1608 if (ret)
1609 goto eremove;
1610 }
1611 if (data->has_voltage & (1 << 0)) {
1612 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1613 if (ret)
1614 goto eremove;
1615 }
Jean Delvared8d2ee02009-12-09 20:36:08 +01001616 if (data->has_voltage & (1 << 3)) {
1617 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1618 if (ret)
1619 goto eremove;
1620 }
1621 if (data->has_voltage & (1 << 4)) {
1622 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1623 if (ret)
1624 goto eremove;
1625 }
1626 if (data->has_voltage & (1 << 5)) {
1627 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1628 if (ret)
1629 goto eremove;
1630 }
Jean Delvare54fe4672009-12-09 20:36:08 +01001631 if (data->has_vid) {
1632 data->vrm = vid_which_vrm();
1633 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1634 if (ret)
1635 goto eremove;
1636 }
Jean Delvare378933c2009-12-09 20:36:06 +01001637
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001638 data->hwmon_dev = hwmon_device_register(&client->dev);
1639 if (IS_ERR(data->hwmon_dev)) {
1640 ret = PTR_ERR(data->hwmon_dev);
1641 goto eremove;
1642 }
1643
Jean Delvared07ca4a2009-12-09 20:36:07 +01001644 dev_info(&client->dev, "%s device, revision %d\n",
1645 names[id->driver_data], revision);
Jean Delvared8d2ee02009-12-09 20:36:08 +01001646 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
Jean Delvare54fe4672009-12-09 20:36:08 +01001647 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001648 (data->has_voltage & (1 << 0)) ? " in0" : "",
Jean Delvared8d2ee02009-12-09 20:36:08 +01001649 (data->has_voltage & (1 << 4)) ? " in4" : "",
Jean Delvared07ca4a2009-12-09 20:36:07 +01001650 data->has_fan4 ? " fan4" : "",
Jean Delvare54fe4672009-12-09 20:36:08 +01001651 data->has_pwm2 ? " pwm2" : "",
1652 data->has_vid ? " vid" : "");
Jean Delvareebfaf1f2009-12-09 20:36:07 +01001653 if (data->bypass_attn)
1654 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1655 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1656 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1657 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1658 (data->bypass_attn & (1 << 4)) ? " in4" : "");
Jean Delvared07ca4a2009-12-09 20:36:07 +01001659
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001660 /* Limits and settings, should never change update more than once */
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001661 ret = adt7475_update_limits(client);
1662 if (ret)
1663 goto eremove;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001664
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001665 return 0;
1666
1667eremove:
Jean Delvare0f144802009-12-09 20:36:06 +01001668 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001669 return ret;
1670}
1671
1672static int adt7475_remove(struct i2c_client *client)
1673{
1674 struct adt7475_data *data = i2c_get_clientdata(client);
1675
1676 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0f144802009-12-09 20:36:06 +01001677 adt7475_remove_files(client, data);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001678
1679 return 0;
1680}
1681
1682static struct i2c_driver adt7475_driver = {
1683 .class = I2C_CLASS_HWMON,
1684 .driver = {
1685 .name = "adt7475",
Javier Martinez Canillas4e2496e2017-02-24 10:12:58 -03001686 .of_match_table = of_match_ptr(adt7475_of_match),
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001687 },
1688 .probe = adt7475_probe,
1689 .remove = adt7475_remove,
1690 .id_table = adt7475_id,
1691 .detect = adt7475_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001692 .address_list = normal_i2c,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001693};
1694
1695static void adt7475_read_hystersis(struct i2c_client *client)
1696{
1697 struct adt7475_data *data = i2c_get_clientdata(client);
1698
1699 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1700 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1701 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1702}
1703
1704static void adt7475_read_pwm(struct i2c_client *client, int index)
1705{
1706 struct adt7475_data *data = i2c_get_clientdata(client);
1707 unsigned int v;
1708
1709 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1710
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001711 /*
1712 * Figure out the internal value for pwmctrl and pwmchan
1713 * based on the current settings
1714 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001715 v = (data->pwm[CONTROL][index] >> 5) & 7;
1716
1717 if (v == 3)
1718 data->pwmctl[index] = 0;
1719 else if (v == 7)
1720 data->pwmctl[index] = 1;
1721 else if (v == 4) {
Guenter Roeck9ed5bc22012-01-19 11:02:15 -08001722 /*
1723 * The fan is disabled - we don't want to
1724 * support that, so change to manual mode and
1725 * set the duty cycle to 0 instead
1726 */
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001727 data->pwm[INPUT][index] = 0;
1728 data->pwm[CONTROL][index] &= ~0xE0;
1729 data->pwm[CONTROL][index] |= (7 << 5);
1730
1731 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1732 data->pwm[INPUT][index]);
1733
1734 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1735 data->pwm[CONTROL][index]);
1736
1737 data->pwmctl[index] = 1;
1738 } else {
1739 data->pwmctl[index] = 2;
1740
1741 switch (v) {
1742 case 0:
1743 data->pwmchan[index] = 1;
1744 break;
1745 case 1:
1746 data->pwmchan[index] = 2;
1747 break;
1748 case 2:
1749 data->pwmchan[index] = 4;
1750 break;
1751 case 5:
1752 data->pwmchan[index] = 6;
1753 break;
1754 case 6:
1755 data->pwmchan[index] = 7;
1756 break;
1757 }
1758 }
1759}
1760
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001761static int adt7475_update_measure(struct device *dev)
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001762{
1763 struct i2c_client *client = to_i2c_client(dev);
1764 struct adt7475_data *data = i2c_get_clientdata(client);
Jean Delvare3d849982009-12-09 20:36:05 +01001765 u16 ext;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001766 int i;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001767 int ret;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001768
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001769 ret = adt7475_read(REG_STATUS2);
1770 if (ret < 0)
1771 return ret;
1772 data->alarms = ret << 8;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001773
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001774 ret = adt7475_read(REG_STATUS1);
1775 if (ret < 0)
1776 return ret;
1777 data->alarms |= ret;
1778
1779 ret = adt7475_read(REG_EXTEND2);
1780 if (ret < 0)
1781 return ret;
1782
1783 ext = (ret << 8);
1784
1785 ret = adt7475_read(REG_EXTEND1);
1786 if (ret < 0)
1787 return ret;
1788
1789 ext |= ret;
1790
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001791 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1792 if (!(data->has_voltage & (1 << i)))
1793 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001794 ret = adt7475_read(VOLTAGE_REG(i));
1795 if (ret < 0)
1796 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001797 data->voltage[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001798 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001799 ((ext >> (i * 2)) & 3);
1800 }
1801
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001802 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1803 ret = adt7475_read(TEMP_REG(i));
1804 if (ret < 0)
1805 return ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001806 data->temp[INPUT][i] =
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001807 (ret << 2) |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001808 ((ext >> ((i + 5) * 2)) & 3);
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001809 }
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001810
1811 if (data->has_voltage & (1 << 5)) {
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001812 ret = adt7475_read(REG_STATUS4);
1813 if (ret < 0)
1814 return ret;
1815 data->alarms |= ret << 24;
1816
1817 ret = adt7475_read(REG_EXTEND3);
1818 if (ret < 0)
1819 return ret;
1820 ext = ret;
1821
1822 ret = adt7475_read(REG_VTT);
1823 if (ret < 0)
1824 return ret;
1825 data->voltage[INPUT][5] = ret << 2 |
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001826 ((ext >> 4) & 3);
1827 }
1828
1829 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1830 if (i == 3 && !data->has_fan4)
1831 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001832 ret = adt7475_read_word(client, TACH_REG(i));
1833 if (ret < 0)
1834 return ret;
1835 data->tach[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001836 }
1837
1838 /* Updated by hw when in auto mode */
1839 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1840 if (i == 1 && !data->has_pwm2)
1841 continue;
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001842 ret = adt7475_read(PWM_REG(i));
1843 if (ret < 0)
1844 return ret;
1845 data->pwm[INPUT][i] = ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001846 }
1847
Tokunori Ikegami702afea2018-08-08 10:32:18 +09001848 if (data->has_vid) {
1849 ret = adt7475_read(REG_VID);
1850 if (ret < 0)
1851 return ret;
1852 data->vid = ret & 0x3f;
1853 }
1854
1855 return 0;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001856}
1857
1858static struct adt7475_data *adt7475_update_device(struct device *dev)
1859{
1860 struct i2c_client *client = to_i2c_client(dev);
1861 struct adt7475_data *data = i2c_get_clientdata(client);
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001862 int ret;
Tokunori Ikegami5cf943e2018-08-08 10:32:16 +09001863
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001864 mutex_lock(&data->lock);
1865
1866 /* Measurement values update every 2 seconds */
1867 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1868 !data->valid) {
Tokunori Ikegami4afec792018-08-08 10:32:19 +09001869 ret = adt7475_update_measure(dev);
1870 if (ret) {
1871 data->valid = false;
1872 mutex_unlock(&data->lock);
1873 return ERR_PTR(ret);
1874 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001875 data->measure_updated = jiffies;
Tokunori Ikegamib36fb172018-08-08 10:32:17 +09001876 data->valid = true;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001877 }
1878
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001879 mutex_unlock(&data->lock);
1880
1881 return data;
1882}
1883
Axel Linf0967ee2012-01-20 15:38:18 +08001884module_i2c_driver(adt7475_driver);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001885
1886MODULE_AUTHOR("Advanced Micro Devices, Inc");
1887MODULE_DESCRIPTION("adt7475 driver");
1888MODULE_LICENSE("GPL");