blob: b538ace2d2924da6e77d51484fbbded84af16666 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Guenter Roeckfbb66702012-01-19 11:02:14 -08003 * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Based on lm75.c and lm85.c
6 * Supports adm1030 / adm1031
7 * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01008 * Reworked by Jean Delvare <jdelvare@suse.de>
Guenter Roeckfbb66702012-01-19 11:02:14 -08009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/jiffies.h>
15#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040016#include <linux/hwmon.h>
Jean Delvarec8010822007-12-02 23:39:38 +010017#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040018#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/* Following macros takes channel parameter starting from 0 to 2 */
22#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010023#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define ADM1031_REG_PWM (0x22)
25#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
Jean Delvare87c33da2010-05-27 19:58:46 +020026#define ADM1031_REG_FAN_FILTER (0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Ira Snyder49dc9ef2009-09-23 22:59:41 +020028#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010029#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
30#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
31#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jean Delvare6d6006b2007-12-02 23:33:57 +010033#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
35
36#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
37
Jean Delvare6d6006b2007-12-02 23:33:57 +010038#define ADM1031_REG_CONF1 0x00
39#define ADM1031_REG_CONF2 0x01
40#define ADM1031_REG_EXT_TEMP 0x06
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
43#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
44#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
45
46#define ADM1031_CONF2_PWM1_ENABLE 0x01
47#define ADM1031_CONF2_PWM2_ENABLE 0x02
48#define ADM1031_CONF2_TACH1_ENABLE 0x04
49#define ADM1031_CONF2_TACH2_ENABLE 0x08
50#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
51
Jean Delvare87c33da2010-05-27 19:58:46 +020052#define ADM1031_UPDATE_RATE_MASK 0x1c
53#define ADM1031_UPDATE_RATE_SHIFT 2
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050056static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jean Delvaree5e9f442009-12-14 21:17:27 +010058enum chips { adm1030, adm1031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60typedef u8 auto_chan_table_t[8][2];
61
62/* Each client has this additional data */
63struct adm1031_data {
Axel Linb060f3c2014-07-03 21:57:44 +080064 struct i2c_client *client;
65 const struct attribute_group *groups[3];
Ingo Molnar9a61bf62006-01-18 23:19:26 +010066 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int chip_type;
68 char valid; /* !=0 if following fields are valid */
69 unsigned long last_updated; /* In jiffies */
Guenter Roecka51b9942010-09-17 17:24:14 +020070 unsigned int update_interval; /* In milliseconds */
Guenter Roeckfbb66702012-01-19 11:02:14 -080071 /*
72 * The chan_select_table contains the possible configurations for
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * auto fan control.
74 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010075 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u16 alarm;
77 u8 conf1;
78 u8 conf2;
79 u8 fan[2];
80 u8 fan_div[2];
81 u8 fan_min[2];
82 u8 pwm[2];
83 u8 old_pwm[2];
84 s8 temp[3];
85 u8 ext_temp[3];
86 u8 auto_temp[3];
87 u8 auto_temp_min[3];
88 u8 auto_temp_off[3];
89 u8 auto_temp_max[3];
Ira Snyder49dc9ef2009-09-23 22:59:41 +020090 s8 temp_offset[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 s8 temp_min[3];
92 s8 temp_max[3];
93 s8 temp_crit[3];
94};
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
97{
98 return i2c_smbus_read_byte_data(client, reg);
99}
100
101static inline int
102adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
103{
104 return i2c_smbus_write_byte_data(client, reg, value);
105}
106
Axel Lin278ee1c2014-07-03 21:56:33 +0800107static struct adm1031_data *adm1031_update_device(struct device *dev)
108{
Axel Linb060f3c2014-07-03 21:57:44 +0800109 struct adm1031_data *data = dev_get_drvdata(dev);
110 struct i2c_client *client = data->client;
Axel Lin278ee1c2014-07-03 21:56:33 +0800111 unsigned long next_update;
112 int chan;
113
114 mutex_lock(&data->update_lock);
115
116 next_update = data->last_updated
117 + msecs_to_jiffies(data->update_interval);
118 if (time_after(jiffies, next_update) || !data->valid) {
119
120 dev_dbg(&client->dev, "Starting adm1031 update\n");
121 for (chan = 0;
122 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
123 u8 oldh, newh;
124
125 oldh =
126 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
127 data->ext_temp[chan] =
128 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
129 newh =
130 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
131 if (newh != oldh) {
132 data->ext_temp[chan] =
133 adm1031_read_value(client,
134 ADM1031_REG_EXT_TEMP);
135#ifdef DEBUG
136 oldh =
137 adm1031_read_value(client,
138 ADM1031_REG_TEMP(chan));
139
140 /* oldh is actually newer */
141 if (newh != oldh)
142 dev_warn(&client->dev,
143 "Remote temperature may be wrong.\n");
144#endif
145 }
146 data->temp[chan] = newh;
147
148 data->temp_offset[chan] =
149 adm1031_read_value(client,
150 ADM1031_REG_TEMP_OFFSET(chan));
151 data->temp_min[chan] =
152 adm1031_read_value(client,
153 ADM1031_REG_TEMP_MIN(chan));
154 data->temp_max[chan] =
155 adm1031_read_value(client,
156 ADM1031_REG_TEMP_MAX(chan));
157 data->temp_crit[chan] =
158 adm1031_read_value(client,
159 ADM1031_REG_TEMP_CRIT(chan));
160 data->auto_temp[chan] =
161 adm1031_read_value(client,
162 ADM1031_REG_AUTO_TEMP(chan));
163
164 }
165
166 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
167 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
168
169 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
170 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
171 if (data->chip_type == adm1030)
172 data->alarm &= 0xc0ff;
173
174 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
175 chan++) {
176 data->fan_div[chan] =
177 adm1031_read_value(client,
178 ADM1031_REG_FAN_DIV(chan));
179 data->fan_min[chan] =
180 adm1031_read_value(client,
181 ADM1031_REG_FAN_MIN(chan));
182 data->fan[chan] =
183 adm1031_read_value(client,
184 ADM1031_REG_FAN_SPEED(chan));
185 data->pwm[chan] =
186 (adm1031_read_value(client,
187 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
188 }
189 data->last_updated = jiffies;
190 data->valid = 1;
191 }
192
193 mutex_unlock(&data->update_lock);
194
195 return data;
196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
199 ((val + 500) / 1000)))
200
201#define TEMP_FROM_REG(val) ((val) * 1000)
202
203#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
204
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200205#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
206#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
207 (val) | 0x70 : (val))
208
Guenter Roeck1c720092012-01-16 22:51:48 +0100209#define FAN_FROM_REG(reg, div) ((reg) ? \
210 (11250 * 60) / ((reg) * (div)) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212static int FAN_TO_REG(int reg, int div)
213{
214 int tmp;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800215 tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return tmp > 255 ? 255 : tmp;
217}
218
219#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
220
Guenter Roeck2a844c12013-01-09 08:09:34 -0800221#define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#define PWM_FROM_REG(val) ((val) << 4)
223
224#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
225#define FAN_CHAN_TO_REG(val, reg) \
226 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
227
228#define AUTO_TEMP_MIN_TO_REG(val, reg) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100229 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
230#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
232
233#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
234
235#define AUTO_TEMP_OFF_FROM_REG(reg) \
236 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
237
238#define AUTO_TEMP_MAX_FROM_REG(reg) \
239 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
240 AUTO_TEMP_MIN_FROM_REG(reg))
241
242static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
243{
244 int ret;
245 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
246
247 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
248 ret = ((reg & 0xf8) |
249 (range < 10000 ? 0 :
250 range < 20000 ? 1 :
251 range < 40000 ? 2 : range < 80000 ? 3 : 4));
252 return ret;
253}
254
255/* FAN auto control */
256#define GET_FAN_AUTO_BITFIELD(data, idx) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100257 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Guenter Roeckfbb66702012-01-19 11:02:14 -0800259/*
260 * The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * control bitfields. the index in the table is the register value.
262 * MSb is the auto fan control enable bit, so the four first entries
263 * in the table disables auto fan control when both bitfields are zero.
264 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100265static const auto_chan_table_t auto_channel_select_table_adm1031 = {
266 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
267 { 2 /* 0b010 */ , 4 /* 0b100 */ },
268 { 2 /* 0b010 */ , 2 /* 0b010 */ },
269 { 4 /* 0b100 */ , 4 /* 0b100 */ },
270 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Jean Delvare6d6006b2007-12-02 23:33:57 +0100273static const auto_chan_table_t auto_channel_select_table_adm1030 = {
274 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
275 { 2 /* 0b10 */ , 0 },
276 { 0xff /* invalid */ , 0 },
277 { 0xff /* invalid */ , 0 },
278 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Guenter Roeckfbb66702012-01-19 11:02:14 -0800281/*
282 * That function checks if a bitfield is valid and returns the other bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * nearest match if no exact match where found.
284 */
285static int
Guenter Roeckce15a812012-03-24 08:51:05 -0700286get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int i;
289 int first_match = -1, exact_match = -1;
290 u8 other_reg_val =
291 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
292
Guenter Roeckce15a812012-03-24 08:51:05 -0700293 if (val == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 for (i = 0; i < 8; i++) {
297 if ((val == (*data->chan_select_table)[i][chan]) &&
298 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
299 other_reg_val)) {
300 /* We found an exact match */
301 exact_match = i;
302 break;
303 } else if (val == (*data->chan_select_table)[i][chan] &&
304 first_match == -1) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800305 /*
306 * Save the first match in case of an exact match has
Jean Delvare6d6006b2007-12-02 23:33:57 +0100307 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
309 first_match = i;
310 }
311 }
312
Guenter Roeck1c720092012-01-16 22:51:48 +0100313 if (exact_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700314 return exact_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100315 else if (first_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700316 return first_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100317
Guenter Roeckce15a812012-03-24 08:51:05 -0700318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Guenter Roeck8e757e12019-01-21 20:37:22 -0800321static ssize_t fan_auto_channel_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100322 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Jean Delvarec8010822007-12-02 23:39:38 +0100324 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct adm1031_data *data = adm1031_update_device(dev);
326 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
327}
328
329static ssize_t
Guenter Roeck8e757e12019-01-21 20:37:22 -0800330fan_auto_channel_store(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Axel Linb060f3c2014-07-03 21:57:44 +0800333 struct adm1031_data *data = dev_get_drvdata(dev);
334 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100335 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100336 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 u8 reg;
338 int ret;
339 u8 old_fan_mode;
340
Guenter Roeck1c720092012-01-16 22:51:48 +0100341 ret = kstrtol(buf, 10, &val);
342 if (ret)
343 return ret;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 old_fan_mode = data->conf1;
346
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100348
Guenter Roeckce15a812012-03-24 08:51:05 -0700349 ret = get_fan_auto_nearest(data, nr, val, data->conf1);
350 if (ret < 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100351 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
353 }
Guenter Roeckce15a812012-03-24 08:51:05 -0700354 reg = ret;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100355 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
356 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100358 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800359 /*
360 * Switch to Auto Fan Mode
Jean Delvare6d6006b2007-12-02 23:33:57 +0100361 * Save PWM registers
Guenter Roeckfbb66702012-01-19 11:02:14 -0800362 * Set PWM registers to 33% Both
363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 data->old_pwm[0] = data->pwm[0];
365 data->old_pwm[1] = data->pwm[1];
366 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
367 } else {
368 /* Switch to Manual Mode */
369 data->pwm[0] = data->old_pwm[0];
370 data->pwm[1] = data->old_pwm[1];
371 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100372 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 data->pwm[0] | (data->pwm[1] << 4));
374 }
375 }
376 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
377 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100378 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return count;
380}
381
Guenter Roeck8e757e12019-01-21 20:37:22 -0800382static SENSOR_DEVICE_ATTR_RW(auto_fan1_channel, fan_auto_channel, 0);
383static SENSOR_DEVICE_ATTR_RW(auto_fan2_channel, fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385/* Auto Temps */
Guenter Roeck8e757e12019-01-21 20:37:22 -0800386static ssize_t auto_temp_off_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100387 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Jean Delvarec8010822007-12-02 23:39:38 +0100389 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100391 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
393}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800394static ssize_t auto_temp_min_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100395 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jean Delvarec8010822007-12-02 23:39:38 +0100397 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct adm1031_data *data = adm1031_update_device(dev);
399 return sprintf(buf, "%d\n",
400 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
401}
402static ssize_t
Guenter Roeck8e757e12019-01-21 20:37:22 -0800403auto_temp_min_store(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Axel Linb060f3c2014-07-03 21:57:44 +0800406 struct adm1031_data *data = dev_get_drvdata(dev);
407 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100408 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100409 long val;
410 int ret;
411
412 ret = kstrtol(buf, 10, &val);
413 if (ret)
414 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Guenter Roeck145e74a42014-07-03 13:44:23 -0700416 val = clamp_val(val, 0, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100417 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
419 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
420 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return count;
423}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800424static ssize_t auto_temp_max_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100425 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jean Delvarec8010822007-12-02 23:39:38 +0100427 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct adm1031_data *data = adm1031_update_device(dev);
429 return sprintf(buf, "%d\n",
430 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
431}
432static ssize_t
Guenter Roeck8e757e12019-01-21 20:37:22 -0800433auto_temp_max_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Axel Linb060f3c2014-07-03 21:57:44 +0800436 struct adm1031_data *data = dev_get_drvdata(dev);
437 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100438 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100439 long val;
440 int ret;
441
442 ret = kstrtol(buf, 10, &val);
443 if (ret)
444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Guenter Roeck145e74a42014-07-03 13:44:23 -0700446 val = clamp_val(val, 0, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100447 mutex_lock(&data->update_lock);
Guenter Roeck1c720092012-01-16 22:51:48 +0100448 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
449 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
451 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100452 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return count;
454}
455
Guenter Roeck8e757e12019-01-21 20:37:22 -0800456static SENSOR_DEVICE_ATTR_RO(auto_temp1_off, auto_temp_off, 0);
457static SENSOR_DEVICE_ATTR_RW(auto_temp1_min, auto_temp_min, 0);
458static SENSOR_DEVICE_ATTR_RW(auto_temp1_max, auto_temp_max, 0);
459static SENSOR_DEVICE_ATTR_RO(auto_temp2_off, auto_temp_off, 1);
460static SENSOR_DEVICE_ATTR_RW(auto_temp2_min, auto_temp_min, 1);
461static SENSOR_DEVICE_ATTR_RW(auto_temp2_max, auto_temp_max, 1);
462static SENSOR_DEVICE_ATTR_RO(auto_temp3_off, auto_temp_off, 2);
463static SENSOR_DEVICE_ATTR_RW(auto_temp3_min, auto_temp_min, 2);
464static SENSOR_DEVICE_ATTR_RW(auto_temp3_max, auto_temp_max, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/* pwm */
Guenter Roeck8e757e12019-01-21 20:37:22 -0800467static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
468 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Jean Delvarec8010822007-12-02 23:39:38 +0100470 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct adm1031_data *data = adm1031_update_device(dev);
472 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
473}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800474static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
475 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Axel Linb060f3c2014-07-03 21:57:44 +0800477 struct adm1031_data *data = dev_get_drvdata(dev);
478 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100479 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100480 long val;
481 int ret, reg;
482
483 ret = kstrtol(buf, 10, &val);
484 if (ret)
485 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100487 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100488 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 (((val>>4) & 0xf) != 5)) {
490 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100491 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493 }
494 data->pwm[nr] = PWM_TO_REG(val);
495 reg = adm1031_read_value(client, ADM1031_REG_PWM);
496 adm1031_write_value(client, ADM1031_REG_PWM,
497 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
498 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100499 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return count;
501}
502
Guenter Roeck8e757e12019-01-21 20:37:22 -0800503static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
504static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
505static SENSOR_DEVICE_ATTR_RW(auto_fan1_min_pwm, pwm, 0);
506static SENSOR_DEVICE_ATTR_RW(auto_fan2_min_pwm, pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508/* Fans */
509
510/*
511 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600512 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * not supposed to run
514 */
515static int trust_fan_readings(struct adm1031_data *data, int chan)
516{
517 int res = 0;
518
519 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
520 switch (data->conf1 & 0x60) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100521 case 0x00:
522 /*
523 * remote temp1 controls fan1,
524 * remote temp2 controls fan2
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 res = data->temp[chan+1] >=
Guenter Roeck1c720092012-01-16 22:51:48 +0100527 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 case 0x20: /* remote temp1 controls both fans */
530 res =
531 data->temp[1] >=
532 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
533 break;
534 case 0x40: /* remote temp2 controls both fans */
535 res =
536 data->temp[2] >=
537 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
538 break;
539 case 0x60: /* max controls both fans */
540 res =
541 data->temp[0] >=
542 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
543 || data->temp[1] >=
544 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100545 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 && data->temp[2] >=
547 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
548 break;
549 }
550 } else {
551 res = data->pwm[chan] > 0;
552 }
553 return res;
554}
555
Guenter Roeck8e757e12019-01-21 20:37:22 -0800556static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
557 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jean Delvarec8010822007-12-02 23:39:38 +0100559 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct adm1031_data *data = adm1031_update_device(dev);
561 int value;
562
563 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
564 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
565 return sprintf(buf, "%d\n", value);
566}
567
Guenter Roeck8e757e12019-01-21 20:37:22 -0800568static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvarec8010822007-12-02 23:39:38 +0100571 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct adm1031_data *data = adm1031_update_device(dev);
573 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
574}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800575static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
576 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Jean Delvarec8010822007-12-02 23:39:38 +0100578 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct adm1031_data *data = adm1031_update_device(dev);
580 return sprintf(buf, "%d\n",
581 FAN_FROM_REG(data->fan_min[nr],
582 FAN_DIV_FROM_REG(data->fan_div[nr])));
583}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800584static ssize_t fan_min_store(struct device *dev,
585 struct device_attribute *attr, const char *buf,
586 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Axel Linb060f3c2014-07-03 21:57:44 +0800588 struct adm1031_data *data = dev_get_drvdata(dev);
589 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100590 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100591 long val;
592 int ret;
593
594 ret = kstrtol(buf, 10, &val);
595 if (ret)
596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100600 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
602 } else {
603 data->fan_min[nr] = 0xff;
604 }
605 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100606 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return count;
608}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800609static ssize_t fan_div_store(struct device *dev,
610 struct device_attribute *attr, const char *buf,
611 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Axel Linb060f3c2014-07-03 21:57:44 +0800613 struct adm1031_data *data = dev_get_drvdata(dev);
614 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100615 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100616 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 u8 tmp;
618 int old_div;
619 int new_min;
Guenter Roeck1c720092012-01-16 22:51:48 +0100620 int ret;
621
622 ret = kstrtol(buf, 10, &val);
623 if (ret)
624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 tmp = val == 8 ? 0xc0 :
627 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100628 val == 2 ? 0x40 :
629 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 0xff;
631 if (tmp == 0xff)
632 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100633
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100634 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100635 /* Get fresh readings */
636 data->fan_div[nr] = adm1031_read_value(client,
637 ADM1031_REG_FAN_DIV(nr));
638 data->fan_min[nr] = adm1031_read_value(client,
639 ADM1031_REG_FAN_MIN(nr));
640
641 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100643 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
644 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jean Delvare6d6006b2007-12-02 23:33:57 +0100647 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100649 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100651
652 /* Invalidate the cache: fan speed is no longer valid */
653 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100654 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return count;
656}
657
Guenter Roeck8e757e12019-01-21 20:37:22 -0800658static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
659static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
660static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
661static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
662static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
663static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665/* Temps */
Guenter Roeck8e757e12019-01-21 20:37:22 -0800666static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
667 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jean Delvarec8010822007-12-02 23:39:38 +0100669 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct adm1031_data *data = adm1031_update_device(dev);
671 int ext;
672 ext = nr == 0 ?
673 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
674 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
675 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
676}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800677static ssize_t temp_offset_show(struct device *dev,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200678 struct device_attribute *attr, char *buf)
679{
680 int nr = to_sensor_dev_attr(attr)->index;
681 struct adm1031_data *data = adm1031_update_device(dev);
682 return sprintf(buf, "%d\n",
683 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
684}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800685static ssize_t temp_min_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100686 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jean Delvarec8010822007-12-02 23:39:38 +0100688 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct adm1031_data *data = adm1031_update_device(dev);
690 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
691}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800692static ssize_t temp_max_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100693 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jean Delvarec8010822007-12-02 23:39:38 +0100695 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct adm1031_data *data = adm1031_update_device(dev);
697 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
698}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800699static ssize_t temp_crit_show(struct device *dev,
Jean Delvarec8010822007-12-02 23:39:38 +0100700 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jean Delvarec8010822007-12-02 23:39:38 +0100702 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct adm1031_data *data = adm1031_update_device(dev);
704 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
705}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800706static ssize_t temp_offset_store(struct device *dev,
707 struct device_attribute *attr,
708 const char *buf, size_t count)
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200709{
Axel Linb060f3c2014-07-03 21:57:44 +0800710 struct adm1031_data *data = dev_get_drvdata(dev);
711 struct i2c_client *client = data->client;
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200712 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100713 long val;
714 int ret;
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200715
Guenter Roeck1c720092012-01-16 22:51:48 +0100716 ret = kstrtol(buf, 10, &val);
717 if (ret)
718 return ret;
719
Guenter Roeck2a844c12013-01-09 08:09:34 -0800720 val = clamp_val(val, -15000, 15000);
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200721 mutex_lock(&data->update_lock);
722 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
723 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
724 data->temp_offset[nr]);
725 mutex_unlock(&data->update_lock);
726 return count;
727}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800728static ssize_t temp_min_store(struct device *dev,
729 struct device_attribute *attr, const char *buf,
730 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Axel Linb060f3c2014-07-03 21:57:44 +0800732 struct adm1031_data *data = dev_get_drvdata(dev);
733 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100734 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100735 long val;
736 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Guenter Roeck1c720092012-01-16 22:51:48 +0100738 ret = kstrtol(buf, 10, &val);
739 if (ret)
740 return ret;
741
Guenter Roeck145e74a42014-07-03 13:44:23 -0700742 val = clamp_val(val, -55000, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100743 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 data->temp_min[nr] = TEMP_TO_REG(val);
745 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
746 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100747 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return count;
749}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800750static ssize_t temp_max_store(struct device *dev,
751 struct device_attribute *attr, const char *buf,
752 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Axel Linb060f3c2014-07-03 21:57:44 +0800754 struct adm1031_data *data = dev_get_drvdata(dev);
755 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100756 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100757 long val;
758 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Guenter Roeck1c720092012-01-16 22:51:48 +0100760 ret = kstrtol(buf, 10, &val);
761 if (ret)
762 return ret;
763
Guenter Roeck145e74a42014-07-03 13:44:23 -0700764 val = clamp_val(val, -55000, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100765 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 data->temp_max[nr] = TEMP_TO_REG(val);
767 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
768 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100769 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return count;
771}
Guenter Roeck8e757e12019-01-21 20:37:22 -0800772static ssize_t temp_crit_store(struct device *dev,
773 struct device_attribute *attr, const char *buf,
774 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Axel Linb060f3c2014-07-03 21:57:44 +0800776 struct adm1031_data *data = dev_get_drvdata(dev);
777 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100778 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100779 long val;
780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Guenter Roeck1c720092012-01-16 22:51:48 +0100782 ret = kstrtol(buf, 10, &val);
783 if (ret)
784 return ret;
785
Guenter Roeck145e74a42014-07-03 13:44:23 -0700786 val = clamp_val(val, -55000, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 data->temp_crit[nr] = TEMP_TO_REG(val);
789 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
790 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100791 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return count;
793}
794
Guenter Roeck8e757e12019-01-21 20:37:22 -0800795static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
796static SENSOR_DEVICE_ATTR_RW(temp1_offset, temp_offset, 0);
797static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
798static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
799static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
800static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
801static SENSOR_DEVICE_ATTR_RW(temp2_offset, temp_offset, 1);
802static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
803static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
804static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
805static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
806static SENSOR_DEVICE_ATTR_RW(temp3_offset, temp_offset, 2);
807static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
808static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
809static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp_crit, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811/* Alarms */
Julia Lawallbfb6b172016-12-22 13:04:32 +0100812static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck1c720092012-01-16 22:51:48 +0100813 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct adm1031_data *data = adm1031_update_device(dev);
816 return sprintf(buf, "%d\n", data->alarm);
817}
818
Julia Lawallbfb6b172016-12-22 13:04:32 +0100819static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Guenter Roeck8e757e12019-01-21 20:37:22 -0800821static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
822 char *buf)
Jean Delvare050ab872007-12-02 23:42:24 +0100823{
824 int bitnr = to_sensor_dev_attr(attr)->index;
825 struct adm1031_data *data = adm1031_update_device(dev);
826 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
827}
828
Guenter Roeck8e757e12019-01-21 20:37:22 -0800829static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 0);
830static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
831static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
832static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, alarm, 3);
833static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 4);
834static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 5);
835static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 6);
836static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 7);
837static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 8);
838static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 9);
839static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 10);
840static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, alarm, 11);
841static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 12);
842static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 13);
843static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Guenter Roecka51b9942010-09-17 17:24:14 +0200845/* Update Interval */
846static const unsigned int update_intervals[] = {
Jean Delvare87c33da2010-05-27 19:58:46 +0200847 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
848};
849
Julia Lawallbfb6b172016-12-22 13:04:32 +0100850static ssize_t update_interval_show(struct device *dev,
Guenter Roecka51b9942010-09-17 17:24:14 +0200851 struct device_attribute *attr, char *buf)
Jean Delvare87c33da2010-05-27 19:58:46 +0200852{
Axel Linb060f3c2014-07-03 21:57:44 +0800853 struct adm1031_data *data = dev_get_drvdata(dev);
Jean Delvare87c33da2010-05-27 19:58:46 +0200854
Guenter Roecka51b9942010-09-17 17:24:14 +0200855 return sprintf(buf, "%u\n", data->update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200856}
857
Julia Lawallbfb6b172016-12-22 13:04:32 +0100858static ssize_t update_interval_store(struct device *dev,
859 struct device_attribute *attr,
860 const char *buf, size_t count)
Jean Delvare87c33da2010-05-27 19:58:46 +0200861{
Axel Linb060f3c2014-07-03 21:57:44 +0800862 struct adm1031_data *data = dev_get_drvdata(dev);
863 struct i2c_client *client = data->client;
Jean Delvare87c33da2010-05-27 19:58:46 +0200864 unsigned long val;
865 int i, err;
866 u8 reg;
867
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100868 err = kstrtoul(buf, 10, &val);
Jean Delvare87c33da2010-05-27 19:58:46 +0200869 if (err)
870 return err;
871
Guenter Roecka51b9942010-09-17 17:24:14 +0200872 /*
873 * Find the nearest update interval from the table.
874 * Use it to determine the matching update rate.
875 */
876 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
877 if (val >= update_intervals[i])
Jean Delvare87c33da2010-05-27 19:58:46 +0200878 break;
879 }
Guenter Roecka51b9942010-09-17 17:24:14 +0200880 /* if not found, we point to the last entry (lowest update interval) */
Jean Delvare87c33da2010-05-27 19:58:46 +0200881
882 /* set the new update rate while preserving other settings */
883 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
884 reg &= ~ADM1031_UPDATE_RATE_MASK;
885 reg |= i << ADM1031_UPDATE_RATE_SHIFT;
886 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
887
888 mutex_lock(&data->update_lock);
Guenter Roecka51b9942010-09-17 17:24:14 +0200889 data->update_interval = update_intervals[i];
Jean Delvare87c33da2010-05-27 19:58:46 +0200890 mutex_unlock(&data->update_lock);
891
892 return count;
893}
894
Julia Lawallbfb6b172016-12-22 13:04:32 +0100895static DEVICE_ATTR_RW(update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200896
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200897static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100898 &sensor_dev_attr_fan1_input.dev_attr.attr,
899 &sensor_dev_attr_fan1_div.dev_attr.attr,
900 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100901 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
902 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100903 &sensor_dev_attr_pwm1.dev_attr.attr,
904 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
905 &sensor_dev_attr_temp1_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200906 &sensor_dev_attr_temp1_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100907 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100908 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100909 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100910 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100911 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100912 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100913 &sensor_dev_attr_temp2_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200914 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100915 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100916 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100917 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100918 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100919 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100920 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
921 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200922
Jean Delvarec8010822007-12-02 23:39:38 +0100923 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
924 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
925 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200926
Jean Delvarec8010822007-12-02 23:39:38 +0100927 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
928 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
929 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200930
Jean Delvarec8010822007-12-02 23:39:38 +0100931 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200932
Guenter Roecka51b9942010-09-17 17:24:14 +0200933 &dev_attr_update_interval.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200934 &dev_attr_alarms.attr,
935
936 NULL
937};
938
939static const struct attribute_group adm1031_group = {
940 .attrs = adm1031_attributes,
941};
942
943static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100944 &sensor_dev_attr_fan2_input.dev_attr.attr,
945 &sensor_dev_attr_fan2_div.dev_attr.attr,
946 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100947 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
948 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100949 &sensor_dev_attr_pwm2.dev_attr.attr,
950 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
951 &sensor_dev_attr_temp3_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200952 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100953 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100954 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100955 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100956 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100957 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100958 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
959 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100960 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
961 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
962 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
963 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200964 NULL
965};
966
967static const struct attribute_group adm1031_group_opt = {
968 .attrs = adm1031_attributes_opt,
969};
970
Jean Delvareaf200f82008-07-16 19:30:09 +0200971/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100972static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200973 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Jean Delvareaf200f82008-07-16 19:30:09 +0200975 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100976 const char *name;
977 int id, co;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareaf200f82008-07-16 19:30:09 +0200980 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Jean Delvare52df6442009-12-09 20:35:57 +0100982 id = i2c_smbus_read_byte_data(client, 0x3d);
983 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jean Delvare52df6442009-12-09 20:35:57 +0100985 if (!((id == 0x31 || id == 0x30) && co == 0x41))
986 return -ENODEV;
987 name = (id == 0x30) ? "adm1030" : "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Jean Delvareaf200f82008-07-16 19:30:09 +0200989 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Jean Delvareaf200f82008-07-16 19:30:09 +0200991 return 0;
992}
993
Axel Lin278ee1c2014-07-03 21:56:33 +0800994static void adm1031_init_client(struct i2c_client *client)
995{
996 unsigned int read_val;
997 unsigned int mask;
998 int i;
999 struct adm1031_data *data = i2c_get_clientdata(client);
1000
1001 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
1002 if (data->chip_type == adm1031) {
1003 mask |= (ADM1031_CONF2_PWM2_ENABLE |
1004 ADM1031_CONF2_TACH2_ENABLE);
1005 }
1006 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1007 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1008 if ((read_val | mask) != read_val)
1009 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
1010
1011 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
1012 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1013 adm1031_write_value(client, ADM1031_REG_CONF1,
1014 read_val | ADM1031_CONF1_MONITOR_ENABLE);
1015 }
1016
1017 /* Read the chip's update rate */
1018 mask = ADM1031_UPDATE_RATE_MASK;
1019 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
1020 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
1021 /* Save it as update interval */
1022 data->update_interval = update_intervals[i];
1023}
1024
Stephen Kitt67487032020-08-13 18:02:22 +02001025static const struct i2c_device_id adm1031_id[];
1026
1027static int adm1031_probe(struct i2c_client *client)
Jean Delvareaf200f82008-07-16 19:30:09 +02001028{
Axel Linb060f3c2014-07-03 21:57:44 +08001029 struct device *dev = &client->dev;
1030 struct device *hwmon_dev;
Jean Delvareaf200f82008-07-16 19:30:09 +02001031 struct adm1031_data *data;
Jean Delvareaf200f82008-07-16 19:30:09 +02001032
Axel Linb060f3c2014-07-03 21:57:44 +08001033 data = devm_kzalloc(dev, sizeof(struct adm1031_data), GFP_KERNEL);
Guenter Roeckdc2fd662012-06-02 09:57:59 -07001034 if (!data)
1035 return -ENOMEM;
Jean Delvareaf200f82008-07-16 19:30:09 +02001036
1037 i2c_set_clientdata(client, data);
Axel Linb060f3c2014-07-03 21:57:44 +08001038 data->client = client;
Stephen Kitt67487032020-08-13 18:02:22 +02001039 data->chip_type = i2c_match_id(adm1031_id, client)->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001040 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jean Delvareaf200f82008-07-16 19:30:09 +02001042 if (data->chip_type == adm1030)
1043 data->chan_select_table = &auto_channel_select_table_adm1030;
1044 else
1045 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +01001048 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Axel Linb060f3c2014-07-03 21:57:44 +08001050 /* sysfs hooks */
1051 data->groups[0] = &adm1031_group;
1052 if (data->chip_type == adm1031)
1053 data->groups[1] = &adm1031_group_opt;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001054
Axel Linb060f3c2014-07-03 21:57:44 +08001055 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1056 data, data->groups);
1057 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Axel Lin278ee1c2014-07-03 21:56:33 +08001060static const struct i2c_device_id adm1031_id[] = {
1061 { "adm1030", adm1030 },
1062 { "adm1031", adm1031 },
1063 { }
1064};
1065MODULE_DEVICE_TABLE(i2c, adm1031_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Axel Lin278ee1c2014-07-03 21:56:33 +08001067static struct i2c_driver adm1031_driver = {
1068 .class = I2C_CLASS_HWMON,
1069 .driver = {
1070 .name = "adm1031",
1071 },
Stephen Kitt67487032020-08-13 18:02:22 +02001072 .probe_new = adm1031_probe,
Axel Lin278ee1c2014-07-03 21:56:33 +08001073 .id_table = adm1031_id,
1074 .detect = adm1031_detect,
1075 .address_list = normal_i2c,
1076};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Axel Linf0967ee2012-01-20 15:38:18 +08001078module_i2c_driver(adm1031_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1081MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1082MODULE_LICENSE("GPL");