blob: bcf508269fd63bdb11b5fb10360e1b752ebafdd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeckfbb66702012-01-19 11:02:14 -08002 * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Based on lm75.c and lm85.c
5 * Supports adm1030 / adm1031
6 * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01007 * Reworked by Jean Delvare <jdelvare@suse.de>
Guenter Roeckfbb66702012-01-19 11:02:14 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvarec8010822007-12-02 23:39:38 +010030#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Following macros takes channel parameter starting from 0 to 2 */
35#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010036#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ADM1031_REG_PWM (0x22)
38#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
Jean Delvare87c33da2010-05-27 19:58:46 +020039#define ADM1031_REG_FAN_FILTER (0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ira Snyder49dc9ef2009-09-23 22:59:41 +020041#define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
Jean Delvare6d6006b2007-12-02 23:33:57 +010042#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jean Delvare6d6006b2007-12-02 23:33:57 +010046#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48
49#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50
Jean Delvare6d6006b2007-12-02 23:33:57 +010051#define ADM1031_REG_CONF1 0x00
52#define ADM1031_REG_CONF2 0x01
53#define ADM1031_REG_EXT_TEMP 0x06
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
56#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
57#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
58
59#define ADM1031_CONF2_PWM1_ENABLE 0x01
60#define ADM1031_CONF2_PWM2_ENABLE 0x02
61#define ADM1031_CONF2_TACH1_ENABLE 0x04
62#define ADM1031_CONF2_TACH2_ENABLE 0x08
63#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64
Jean Delvare87c33da2010-05-27 19:58:46 +020065#define ADM1031_UPDATE_RATE_MASK 0x1c
66#define ADM1031_UPDATE_RATE_SHIFT 2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050069static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jean Delvaree5e9f442009-12-14 21:17:27 +010071enum chips { adm1030, adm1031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73typedef u8 auto_chan_table_t[8][2];
74
75/* Each client has this additional data */
76struct adm1031_data {
Axel Linb060f3c2014-07-03 21:57:44 +080077 struct i2c_client *client;
78 const struct attribute_group *groups[3];
Ingo Molnar9a61bf62006-01-18 23:19:26 +010079 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int chip_type;
81 char valid; /* !=0 if following fields are valid */
82 unsigned long last_updated; /* In jiffies */
Guenter Roecka51b9942010-09-17 17:24:14 +020083 unsigned int update_interval; /* In milliseconds */
Guenter Roeckfbb66702012-01-19 11:02:14 -080084 /*
85 * The chan_select_table contains the possible configurations for
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * auto fan control.
87 */
Jean Delvare6d6006b2007-12-02 23:33:57 +010088 const auto_chan_table_t *chan_select_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 u16 alarm;
90 u8 conf1;
91 u8 conf2;
92 u8 fan[2];
93 u8 fan_div[2];
94 u8 fan_min[2];
95 u8 pwm[2];
96 u8 old_pwm[2];
97 s8 temp[3];
98 u8 ext_temp[3];
99 u8 auto_temp[3];
100 u8 auto_temp_min[3];
101 u8 auto_temp_off[3];
102 u8 auto_temp_max[3];
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200103 s8 temp_offset[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 s8 temp_min[3];
105 s8 temp_max[3];
106 s8 temp_crit[3];
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
110{
111 return i2c_smbus_read_byte_data(client, reg);
112}
113
114static inline int
115adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
116{
117 return i2c_smbus_write_byte_data(client, reg, value);
118}
119
Axel Lin278ee1c2014-07-03 21:56:33 +0800120static struct adm1031_data *adm1031_update_device(struct device *dev)
121{
Axel Linb060f3c2014-07-03 21:57:44 +0800122 struct adm1031_data *data = dev_get_drvdata(dev);
123 struct i2c_client *client = data->client;
Axel Lin278ee1c2014-07-03 21:56:33 +0800124 unsigned long next_update;
125 int chan;
126
127 mutex_lock(&data->update_lock);
128
129 next_update = data->last_updated
130 + msecs_to_jiffies(data->update_interval);
131 if (time_after(jiffies, next_update) || !data->valid) {
132
133 dev_dbg(&client->dev, "Starting adm1031 update\n");
134 for (chan = 0;
135 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
136 u8 oldh, newh;
137
138 oldh =
139 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
140 data->ext_temp[chan] =
141 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
142 newh =
143 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
144 if (newh != oldh) {
145 data->ext_temp[chan] =
146 adm1031_read_value(client,
147 ADM1031_REG_EXT_TEMP);
148#ifdef DEBUG
149 oldh =
150 adm1031_read_value(client,
151 ADM1031_REG_TEMP(chan));
152
153 /* oldh is actually newer */
154 if (newh != oldh)
155 dev_warn(&client->dev,
156 "Remote temperature may be wrong.\n");
157#endif
158 }
159 data->temp[chan] = newh;
160
161 data->temp_offset[chan] =
162 adm1031_read_value(client,
163 ADM1031_REG_TEMP_OFFSET(chan));
164 data->temp_min[chan] =
165 adm1031_read_value(client,
166 ADM1031_REG_TEMP_MIN(chan));
167 data->temp_max[chan] =
168 adm1031_read_value(client,
169 ADM1031_REG_TEMP_MAX(chan));
170 data->temp_crit[chan] =
171 adm1031_read_value(client,
172 ADM1031_REG_TEMP_CRIT(chan));
173 data->auto_temp[chan] =
174 adm1031_read_value(client,
175 ADM1031_REG_AUTO_TEMP(chan));
176
177 }
178
179 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
180 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
181
182 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
183 | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
184 if (data->chip_type == adm1030)
185 data->alarm &= 0xc0ff;
186
187 for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
188 chan++) {
189 data->fan_div[chan] =
190 adm1031_read_value(client,
191 ADM1031_REG_FAN_DIV(chan));
192 data->fan_min[chan] =
193 adm1031_read_value(client,
194 ADM1031_REG_FAN_MIN(chan));
195 data->fan[chan] =
196 adm1031_read_value(client,
197 ADM1031_REG_FAN_SPEED(chan));
198 data->pwm[chan] =
199 (adm1031_read_value(client,
200 ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
201 }
202 data->last_updated = jiffies;
203 data->valid = 1;
204 }
205
206 mutex_unlock(&data->update_lock);
207
208 return data;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
212 ((val + 500) / 1000)))
213
214#define TEMP_FROM_REG(val) ((val) * 1000)
215
216#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
217
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200218#define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
219#define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
220 (val) | 0x70 : (val))
221
Guenter Roeck1c720092012-01-16 22:51:48 +0100222#define FAN_FROM_REG(reg, div) ((reg) ? \
223 (11250 * 60) / ((reg) * (div)) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static int FAN_TO_REG(int reg, int div)
226{
227 int tmp;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800228 tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return tmp > 255 ? 255 : tmp;
230}
231
232#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
233
Guenter Roeck2a844c12013-01-09 08:09:34 -0800234#define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define PWM_FROM_REG(val) ((val) << 4)
236
237#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
238#define FAN_CHAN_TO_REG(val, reg) \
239 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
240
241#define AUTO_TEMP_MIN_TO_REG(val, reg) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100242 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
243#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
245
246#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
247
248#define AUTO_TEMP_OFF_FROM_REG(reg) \
249 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
250
251#define AUTO_TEMP_MAX_FROM_REG(reg) \
252 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
253 AUTO_TEMP_MIN_FROM_REG(reg))
254
255static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
256{
257 int ret;
258 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
259
260 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
261 ret = ((reg & 0xf8) |
262 (range < 10000 ? 0 :
263 range < 20000 ? 1 :
264 range < 40000 ? 2 : range < 80000 ? 3 : 4));
265 return ret;
266}
267
268/* FAN auto control */
269#define GET_FAN_AUTO_BITFIELD(data, idx) \
Guenter Roeck1c720092012-01-16 22:51:48 +0100270 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Guenter Roeckfbb66702012-01-19 11:02:14 -0800272/*
273 * The tables below contains the possible values for the auto fan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * control bitfields. the index in the table is the register value.
275 * MSb is the auto fan control enable bit, so the four first entries
276 * in the table disables auto fan control when both bitfields are zero.
277 */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100278static const auto_chan_table_t auto_channel_select_table_adm1031 = {
279 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
280 { 2 /* 0b010 */ , 4 /* 0b100 */ },
281 { 2 /* 0b010 */ , 2 /* 0b010 */ },
282 { 4 /* 0b100 */ , 4 /* 0b100 */ },
283 { 7 /* 0b111 */ , 7 /* 0b111 */ },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
285
Jean Delvare6d6006b2007-12-02 23:33:57 +0100286static const auto_chan_table_t auto_channel_select_table_adm1030 = {
287 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
288 { 2 /* 0b10 */ , 0 },
289 { 0xff /* invalid */ , 0 },
290 { 0xff /* invalid */ , 0 },
291 { 3 /* 0b11 */ , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Guenter Roeckfbb66702012-01-19 11:02:14 -0800294/*
295 * That function checks if a bitfield is valid and returns the other bitfield
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * nearest match if no exact match where found.
297 */
298static int
Guenter Roeckce15a812012-03-24 08:51:05 -0700299get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 int i;
302 int first_match = -1, exact_match = -1;
303 u8 other_reg_val =
304 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
305
Guenter Roeckce15a812012-03-24 08:51:05 -0700306 if (val == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 for (i = 0; i < 8; i++) {
310 if ((val == (*data->chan_select_table)[i][chan]) &&
311 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
312 other_reg_val)) {
313 /* We found an exact match */
314 exact_match = i;
315 break;
316 } else if (val == (*data->chan_select_table)[i][chan] &&
317 first_match == -1) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800318 /*
319 * Save the first match in case of an exact match has
Jean Delvare6d6006b2007-12-02 23:33:57 +0100320 * not been found
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322 first_match = i;
323 }
324 }
325
Guenter Roeck1c720092012-01-16 22:51:48 +0100326 if (exact_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700327 return exact_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100328 else if (first_match >= 0)
Guenter Roeckce15a812012-03-24 08:51:05 -0700329 return first_match;
Guenter Roeck1c720092012-01-16 22:51:48 +0100330
Guenter Roeckce15a812012-03-24 08:51:05 -0700331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Jean Delvarec8010822007-12-02 23:39:38 +0100334static ssize_t show_fan_auto_channel(struct device *dev,
335 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Jean Delvarec8010822007-12-02 23:39:38 +0100337 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct adm1031_data *data = adm1031_update_device(dev);
339 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
340}
341
342static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100343set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Axel Linb060f3c2014-07-03 21:57:44 +0800346 struct adm1031_data *data = dev_get_drvdata(dev);
347 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100348 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100349 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 u8 reg;
351 int ret;
352 u8 old_fan_mode;
353
Guenter Roeck1c720092012-01-16 22:51:48 +0100354 ret = kstrtol(buf, 10, &val);
355 if (ret)
356 return ret;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 old_fan_mode = data->conf1;
359
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100360 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100361
Guenter Roeckce15a812012-03-24 08:51:05 -0700362 ret = get_fan_auto_nearest(data, nr, val, data->conf1);
363 if (ret < 0) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100364 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366 }
Guenter Roeckce15a812012-03-24 08:51:05 -0700367 reg = ret;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100368 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
369 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100371 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
Guenter Roeckfbb66702012-01-19 11:02:14 -0800372 /*
373 * Switch to Auto Fan Mode
Jean Delvare6d6006b2007-12-02 23:33:57 +0100374 * Save PWM registers
Guenter Roeckfbb66702012-01-19 11:02:14 -0800375 * Set PWM registers to 33% Both
376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 data->old_pwm[0] = data->pwm[0];
378 data->old_pwm[1] = data->pwm[1];
379 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
380 } else {
381 /* Switch to Manual Mode */
382 data->pwm[0] = data->old_pwm[0];
383 data->pwm[1] = data->old_pwm[1];
384 /* Restore PWM registers */
Jean Delvare6d6006b2007-12-02 23:33:57 +0100385 adm1031_write_value(client, ADM1031_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 data->pwm[0] | (data->pwm[1] << 4));
387 }
388 }
389 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
390 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100391 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return count;
393}
394
Jean Delvarec8010822007-12-02 23:39:38 +0100395static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
396 show_fan_auto_channel, set_fan_auto_channel, 0);
397static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
398 show_fan_auto_channel, set_fan_auto_channel, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/* Auto Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100401static ssize_t show_auto_temp_off(struct device *dev,
402 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Jean Delvarec8010822007-12-02 23:39:38 +0100404 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct adm1031_data *data = adm1031_update_device(dev);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100406 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
408}
Jean Delvarec8010822007-12-02 23:39:38 +0100409static ssize_t show_auto_temp_min(struct device *dev,
410 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jean Delvarec8010822007-12-02 23:39:38 +0100412 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct adm1031_data *data = adm1031_update_device(dev);
414 return sprintf(buf, "%d\n",
415 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
416}
417static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100418set_auto_temp_min(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Axel Linb060f3c2014-07-03 21:57:44 +0800421 struct adm1031_data *data = dev_get_drvdata(dev);
422 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100423 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100424 long val;
425 int ret;
426
427 ret = kstrtol(buf, 10, &val);
428 if (ret)
429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Guenter Roeck145e74a42014-07-03 13:44:23 -0700431 val = clamp_val(val, 0, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100432 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
434 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
435 data->auto_temp[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100436 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return count;
438}
Jean Delvarec8010822007-12-02 23:39:38 +0100439static ssize_t show_auto_temp_max(struct device *dev,
440 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Jean Delvarec8010822007-12-02 23:39:38 +0100442 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct adm1031_data *data = adm1031_update_device(dev);
444 return sprintf(buf, "%d\n",
445 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
446}
447static ssize_t
Jean Delvarec8010822007-12-02 23:39:38 +0100448set_auto_temp_max(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Axel Linb060f3c2014-07-03 21:57:44 +0800451 struct adm1031_data *data = dev_get_drvdata(dev);
452 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100453 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100454 long val;
455 int ret;
456
457 ret = kstrtol(buf, 10, &val);
458 if (ret)
459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Guenter Roeck145e74a42014-07-03 13:44:23 -0700461 val = clamp_val(val, 0, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100462 mutex_lock(&data->update_lock);
Guenter Roeck1c720092012-01-16 22:51:48 +0100463 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
464 data->pwm[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
466 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return count;
469}
470
Jean Delvarec8010822007-12-02 23:39:38 +0100471#define auto_temp_reg(offset) \
472static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
473 show_auto_temp_off, NULL, offset - 1); \
474static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
475 show_auto_temp_min, set_auto_temp_min, offset - 1); \
476static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
477 show_auto_temp_max, set_auto_temp_max, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479auto_temp_reg(1);
480auto_temp_reg(2);
481auto_temp_reg(3);
482
483/* pwm */
Jean Delvarec8010822007-12-02 23:39:38 +0100484static ssize_t show_pwm(struct device *dev,
485 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jean Delvarec8010822007-12-02 23:39:38 +0100487 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct adm1031_data *data = adm1031_update_device(dev);
489 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
490}
Jean Delvarec8010822007-12-02 23:39:38 +0100491static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Axel Linb060f3c2014-07-03 21:57:44 +0800494 struct adm1031_data *data = dev_get_drvdata(dev);
495 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100496 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100497 long val;
498 int ret, reg;
499
500 ret = kstrtol(buf, 10, &val);
501 if (ret)
502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100504 mutex_lock(&data->update_lock);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100505 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 (((val>>4) & 0xf) != 5)) {
507 /* In automatic mode, the only PWM accepted is 33% */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -EINVAL;
510 }
511 data->pwm[nr] = PWM_TO_REG(val);
512 reg = adm1031_read_value(client, ADM1031_REG_PWM);
513 adm1031_write_value(client, ADM1031_REG_PWM,
514 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
515 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100516 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return count;
518}
519
Jean Delvarec8010822007-12-02 23:39:38 +0100520static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
521static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
522static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
523 show_pwm, set_pwm, 0);
524static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
525 show_pwm, set_pwm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/* Fans */
528
529/*
530 * That function checks the cases where the fan reading is not
Steven Cole44bbe872005-05-03 18:21:25 -0600531 * relevant. It is used to provide 0 as fan reading when the fan is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * not supposed to run
533 */
534static int trust_fan_readings(struct adm1031_data *data, int chan)
535{
536 int res = 0;
537
538 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
539 switch (data->conf1 & 0x60) {
Guenter Roeck1c720092012-01-16 22:51:48 +0100540 case 0x00:
541 /*
542 * remote temp1 controls fan1,
543 * remote temp2 controls fan2
544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 res = data->temp[chan+1] >=
Guenter Roeck1c720092012-01-16 22:51:48 +0100546 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 case 0x20: /* remote temp1 controls both fans */
549 res =
550 data->temp[1] >=
551 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
552 break;
553 case 0x40: /* remote temp2 controls both fans */
554 res =
555 data->temp[2] >=
556 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
557 break;
558 case 0x60: /* max controls both fans */
559 res =
560 data->temp[0] >=
561 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
562 || data->temp[1] >=
563 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
Jean Delvare6d6006b2007-12-02 23:33:57 +0100564 || (data->chip_type == adm1031
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 && data->temp[2] >=
566 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
567 break;
568 }
569 } else {
570 res = data->pwm[chan] > 0;
571 }
572 return res;
573}
574
575
Jean Delvarec8010822007-12-02 23:39:38 +0100576static ssize_t show_fan(struct device *dev,
577 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvarec8010822007-12-02 23:39:38 +0100579 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct adm1031_data *data = adm1031_update_device(dev);
581 int value;
582
583 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
584 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
585 return sprintf(buf, "%d\n", value);
586}
587
Jean Delvarec8010822007-12-02 23:39:38 +0100588static ssize_t show_fan_div(struct device *dev,
589 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Jean Delvarec8010822007-12-02 23:39:38 +0100591 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct adm1031_data *data = adm1031_update_device(dev);
593 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
594}
Jean Delvarec8010822007-12-02 23:39:38 +0100595static ssize_t show_fan_min(struct device *dev,
596 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jean Delvarec8010822007-12-02 23:39:38 +0100598 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct adm1031_data *data = adm1031_update_device(dev);
600 return sprintf(buf, "%d\n",
601 FAN_FROM_REG(data->fan_min[nr],
602 FAN_DIV_FROM_REG(data->fan_div[nr])));
603}
Jean Delvarec8010822007-12-02 23:39:38 +0100604static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
605 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Axel Linb060f3c2014-07-03 21:57:44 +0800607 struct adm1031_data *data = dev_get_drvdata(dev);
608 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100609 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100610 long val;
611 int ret;
612
613 ret = kstrtol(buf, 10, &val);
614 if (ret)
615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100617 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (val) {
Jean Delvare6d6006b2007-12-02 23:33:57 +0100619 data->fan_min[nr] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
621 } else {
622 data->fan_min[nr] = 0xff;
623 }
624 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100625 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return count;
627}
Jean Delvarec8010822007-12-02 23:39:38 +0100628static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
629 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Axel Linb060f3c2014-07-03 21:57:44 +0800631 struct adm1031_data *data = dev_get_drvdata(dev);
632 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100633 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100634 long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 u8 tmp;
636 int old_div;
637 int new_min;
Guenter Roeck1c720092012-01-16 22:51:48 +0100638 int ret;
639
640 ret = kstrtol(buf, 10, &val);
641 if (ret)
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 tmp = val == 8 ? 0xc0 :
645 val == 4 ? 0x80 :
Jean Delvare6d6006b2007-12-02 23:33:57 +0100646 val == 2 ? 0x40 :
647 val == 1 ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 0xff;
649 if (tmp == 0xff)
650 return -EINVAL;
Jean Delvare6d6006b2007-12-02 23:33:57 +0100651
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_lock(&data->update_lock);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100653 /* Get fresh readings */
654 data->fan_div[nr] = adm1031_read_value(client,
655 ADM1031_REG_FAN_DIV(nr));
656 data->fan_min[nr] = adm1031_read_value(client,
657 ADM1031_REG_FAN_MIN(nr));
658
659 /* Write the new clock divider and fan min */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100661 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
662 new_min = data->fan_min[nr] * old_div / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Jean Delvare6d6006b2007-12-02 23:33:57 +0100665 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 data->fan_div[nr]);
Jean Delvare6d6006b2007-12-02 23:33:57 +0100667 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 data->fan_min[nr]);
Jean Delvare38a1f0e2007-12-02 23:32:42 +0100669
670 /* Invalidate the cache: fan speed is no longer valid */
671 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100672 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return count;
674}
675
676#define fan_offset(offset) \
Jean Delvarec8010822007-12-02 23:39:38 +0100677static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
678 show_fan, NULL, offset - 1); \
679static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
680 show_fan_min, set_fan_min, offset - 1); \
681static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
682 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684fan_offset(1);
685fan_offset(2);
686
687
688/* Temps */
Jean Delvarec8010822007-12-02 23:39:38 +0100689static ssize_t show_temp(struct device *dev,
690 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jean Delvarec8010822007-12-02 23:39:38 +0100692 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct adm1031_data *data = adm1031_update_device(dev);
694 int ext;
695 ext = nr == 0 ?
696 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
697 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
698 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
699}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200700static ssize_t show_temp_offset(struct device *dev,
701 struct device_attribute *attr, char *buf)
702{
703 int nr = to_sensor_dev_attr(attr)->index;
704 struct adm1031_data *data = adm1031_update_device(dev);
705 return sprintf(buf, "%d\n",
706 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
707}
Jean Delvarec8010822007-12-02 23:39:38 +0100708static ssize_t show_temp_min(struct device *dev,
709 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvarec8010822007-12-02 23:39:38 +0100711 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct adm1031_data *data = adm1031_update_device(dev);
713 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
714}
Jean Delvarec8010822007-12-02 23:39:38 +0100715static ssize_t show_temp_max(struct device *dev,
716 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Jean Delvarec8010822007-12-02 23:39:38 +0100718 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct adm1031_data *data = adm1031_update_device(dev);
720 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
721}
Jean Delvarec8010822007-12-02 23:39:38 +0100722static ssize_t show_temp_crit(struct device *dev,
723 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Jean Delvarec8010822007-12-02 23:39:38 +0100725 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct adm1031_data *data = adm1031_update_device(dev);
727 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
728}
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200729static ssize_t set_temp_offset(struct device *dev,
730 struct device_attribute *attr, const char *buf,
731 size_t count)
732{
Axel Linb060f3c2014-07-03 21:57:44 +0800733 struct adm1031_data *data = dev_get_drvdata(dev);
734 struct i2c_client *client = data->client;
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200735 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100736 long val;
737 int ret;
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200738
Guenter Roeck1c720092012-01-16 22:51:48 +0100739 ret = kstrtol(buf, 10, &val);
740 if (ret)
741 return ret;
742
Guenter Roeck2a844c12013-01-09 08:09:34 -0800743 val = clamp_val(val, -15000, 15000);
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200744 mutex_lock(&data->update_lock);
745 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
746 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
747 data->temp_offset[nr]);
748 mutex_unlock(&data->update_lock);
749 return count;
750}
Jean Delvarec8010822007-12-02 23:39:38 +0100751static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
752 const char *buf, 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_min[nr] = TEMP_TO_REG(val);
767 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
768 data->temp_min[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}
Jean Delvarec8010822007-12-02 23:39:38 +0100772static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Axel Linb060f3c2014-07-03 21:57:44 +0800775 struct adm1031_data *data = dev_get_drvdata(dev);
776 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100777 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100778 long val;
779 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Guenter Roeck1c720092012-01-16 22:51:48 +0100781 ret = kstrtol(buf, 10, &val);
782 if (ret)
783 return ret;
784
Guenter Roeck145e74a42014-07-03 13:44:23 -0700785 val = clamp_val(val, -55000, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100786 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 data->temp_max[nr] = TEMP_TO_REG(val);
788 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
789 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return count;
792}
Jean Delvarec8010822007-12-02 23:39:38 +0100793static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Axel Linb060f3c2014-07-03 21:57:44 +0800796 struct adm1031_data *data = dev_get_drvdata(dev);
797 struct i2c_client *client = data->client;
Jean Delvarec8010822007-12-02 23:39:38 +0100798 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck1c720092012-01-16 22:51:48 +0100799 long val;
800 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Guenter Roeck1c720092012-01-16 22:51:48 +0100802 ret = kstrtol(buf, 10, &val);
803 if (ret)
804 return ret;
805
Guenter Roeck145e74a42014-07-03 13:44:23 -0700806 val = clamp_val(val, -55000, 127000);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100807 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 data->temp_crit[nr] = TEMP_TO_REG(val);
809 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
810 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100811 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return count;
813}
814
Jean Delvarec8010822007-12-02 23:39:38 +0100815#define temp_reg(offset) \
816static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
817 show_temp, NULL, offset - 1); \
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200818static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
819 show_temp_offset, set_temp_offset, offset - 1); \
Jean Delvarec8010822007-12-02 23:39:38 +0100820static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
821 show_temp_min, set_temp_min, offset - 1); \
822static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
823 show_temp_max, set_temp_max, offset - 1); \
824static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
825 show_temp_crit, set_temp_crit, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827temp_reg(1);
828temp_reg(2);
829temp_reg(3);
830
831/* Alarms */
Julia Lawallbfb6b172016-12-22 13:04:32 +0100832static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck1c720092012-01-16 22:51:48 +0100833 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct adm1031_data *data = adm1031_update_device(dev);
836 return sprintf(buf, "%d\n", data->alarm);
837}
838
Julia Lawallbfb6b172016-12-22 13:04:32 +0100839static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jean Delvare050ab872007-12-02 23:42:24 +0100841static ssize_t show_alarm(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 int bitnr = to_sensor_dev_attr(attr)->index;
845 struct adm1031_data *data = adm1031_update_device(dev);
846 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
847}
848
849static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
850static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
851static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
852static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
853static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
854static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
855static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
856static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
857static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
858static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
859static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
860static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
861static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
862static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
863static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Guenter Roecka51b9942010-09-17 17:24:14 +0200865/* Update Interval */
866static const unsigned int update_intervals[] = {
Jean Delvare87c33da2010-05-27 19:58:46 +0200867 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
868};
869
Julia Lawallbfb6b172016-12-22 13:04:32 +0100870static ssize_t update_interval_show(struct device *dev,
Guenter Roecka51b9942010-09-17 17:24:14 +0200871 struct device_attribute *attr, char *buf)
Jean Delvare87c33da2010-05-27 19:58:46 +0200872{
Axel Linb060f3c2014-07-03 21:57:44 +0800873 struct adm1031_data *data = dev_get_drvdata(dev);
Jean Delvare87c33da2010-05-27 19:58:46 +0200874
Guenter Roecka51b9942010-09-17 17:24:14 +0200875 return sprintf(buf, "%u\n", data->update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200876}
877
Julia Lawallbfb6b172016-12-22 13:04:32 +0100878static ssize_t update_interval_store(struct device *dev,
879 struct device_attribute *attr,
880 const char *buf, size_t count)
Jean Delvare87c33da2010-05-27 19:58:46 +0200881{
Axel Linb060f3c2014-07-03 21:57:44 +0800882 struct adm1031_data *data = dev_get_drvdata(dev);
883 struct i2c_client *client = data->client;
Jean Delvare87c33da2010-05-27 19:58:46 +0200884 unsigned long val;
885 int i, err;
886 u8 reg;
887
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100888 err = kstrtoul(buf, 10, &val);
Jean Delvare87c33da2010-05-27 19:58:46 +0200889 if (err)
890 return err;
891
Guenter Roecka51b9942010-09-17 17:24:14 +0200892 /*
893 * Find the nearest update interval from the table.
894 * Use it to determine the matching update rate.
895 */
896 for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
897 if (val >= update_intervals[i])
Jean Delvare87c33da2010-05-27 19:58:46 +0200898 break;
899 }
Guenter Roecka51b9942010-09-17 17:24:14 +0200900 /* if not found, we point to the last entry (lowest update interval) */
Jean Delvare87c33da2010-05-27 19:58:46 +0200901
902 /* set the new update rate while preserving other settings */
903 reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
904 reg &= ~ADM1031_UPDATE_RATE_MASK;
905 reg |= i << ADM1031_UPDATE_RATE_SHIFT;
906 adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
907
908 mutex_lock(&data->update_lock);
Guenter Roecka51b9942010-09-17 17:24:14 +0200909 data->update_interval = update_intervals[i];
Jean Delvare87c33da2010-05-27 19:58:46 +0200910 mutex_unlock(&data->update_lock);
911
912 return count;
913}
914
Julia Lawallbfb6b172016-12-22 13:04:32 +0100915static DEVICE_ATTR_RW(update_interval);
Jean Delvare87c33da2010-05-27 19:58:46 +0200916
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200917static struct attribute *adm1031_attributes[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100918 &sensor_dev_attr_fan1_input.dev_attr.attr,
919 &sensor_dev_attr_fan1_div.dev_attr.attr,
920 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100921 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
922 &sensor_dev_attr_fan1_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100923 &sensor_dev_attr_pwm1.dev_attr.attr,
924 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
925 &sensor_dev_attr_temp1_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200926 &sensor_dev_attr_temp1_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100927 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100928 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100929 &sensor_dev_attr_temp1_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100930 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100931 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100932 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100933 &sensor_dev_attr_temp2_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200934 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100935 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100936 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100937 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100938 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100939 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100940 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
941 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200942
Jean Delvarec8010822007-12-02 23:39:38 +0100943 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
944 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
945 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200946
Jean Delvarec8010822007-12-02 23:39:38 +0100947 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
948 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
949 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200950
Jean Delvarec8010822007-12-02 23:39:38 +0100951 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200952
Guenter Roecka51b9942010-09-17 17:24:14 +0200953 &dev_attr_update_interval.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200954 &dev_attr_alarms.attr,
955
956 NULL
957};
958
959static const struct attribute_group adm1031_group = {
960 .attrs = adm1031_attributes,
961};
962
963static struct attribute *adm1031_attributes_opt[] = {
Jean Delvarec8010822007-12-02 23:39:38 +0100964 &sensor_dev_attr_fan2_input.dev_attr.attr,
965 &sensor_dev_attr_fan2_div.dev_attr.attr,
966 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100967 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
968 &sensor_dev_attr_fan2_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100969 &sensor_dev_attr_pwm2.dev_attr.attr,
970 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
971 &sensor_dev_attr_temp3_input.dev_attr.attr,
Ira Snyder49dc9ef2009-09-23 22:59:41 +0200972 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100973 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100974 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100975 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100976 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100977 &sensor_dev_attr_temp3_crit.dev_attr.attr,
Jean Delvare050ab872007-12-02 23:42:24 +0100978 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
979 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvarec8010822007-12-02 23:39:38 +0100980 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
981 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
982 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
983 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200984 NULL
985};
986
987static const struct attribute_group adm1031_group_opt = {
988 .attrs = adm1031_attributes_opt,
989};
990
Jean Delvareaf200f82008-07-16 19:30:09 +0200991/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100992static int adm1031_detect(struct i2c_client *client,
Jean Delvareaf200f82008-07-16 19:30:09 +0200993 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Jean Delvareaf200f82008-07-16 19:30:09 +0200995 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100996 const char *name;
997 int id, co;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareaf200f82008-07-16 19:30:09 +02001000 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jean Delvare52df6442009-12-09 20:35:57 +01001002 id = i2c_smbus_read_byte_data(client, 0x3d);
1003 co = i2c_smbus_read_byte_data(client, 0x3e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Jean Delvare52df6442009-12-09 20:35:57 +01001005 if (!((id == 0x31 || id == 0x30) && co == 0x41))
1006 return -ENODEV;
1007 name = (id == 0x30) ? "adm1030" : "adm1031";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Jean Delvareaf200f82008-07-16 19:30:09 +02001009 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jean Delvareaf200f82008-07-16 19:30:09 +02001011 return 0;
1012}
1013
Axel Lin278ee1c2014-07-03 21:56:33 +08001014static void adm1031_init_client(struct i2c_client *client)
1015{
1016 unsigned int read_val;
1017 unsigned int mask;
1018 int i;
1019 struct adm1031_data *data = i2c_get_clientdata(client);
1020
1021 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
1022 if (data->chip_type == adm1031) {
1023 mask |= (ADM1031_CONF2_PWM2_ENABLE |
1024 ADM1031_CONF2_TACH2_ENABLE);
1025 }
1026 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1027 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1028 if ((read_val | mask) != read_val)
1029 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
1030
1031 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
1032 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1033 adm1031_write_value(client, ADM1031_REG_CONF1,
1034 read_val | ADM1031_CONF1_MONITOR_ENABLE);
1035 }
1036
1037 /* Read the chip's update rate */
1038 mask = ADM1031_UPDATE_RATE_MASK;
1039 read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
1040 i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
1041 /* Save it as update interval */
1042 data->update_interval = update_intervals[i];
1043}
1044
Jean Delvareaf200f82008-07-16 19:30:09 +02001045static int adm1031_probe(struct i2c_client *client,
1046 const struct i2c_device_id *id)
1047{
Axel Linb060f3c2014-07-03 21:57:44 +08001048 struct device *dev = &client->dev;
1049 struct device *hwmon_dev;
Jean Delvareaf200f82008-07-16 19:30:09 +02001050 struct adm1031_data *data;
Jean Delvareaf200f82008-07-16 19:30:09 +02001051
Axel Linb060f3c2014-07-03 21:57:44 +08001052 data = devm_kzalloc(dev, sizeof(struct adm1031_data), GFP_KERNEL);
Guenter Roeckdc2fd662012-06-02 09:57:59 -07001053 if (!data)
1054 return -ENOMEM;
Jean Delvareaf200f82008-07-16 19:30:09 +02001055
1056 i2c_set_clientdata(client, data);
Axel Linb060f3c2014-07-03 21:57:44 +08001057 data->client = client;
Jean Delvareaf200f82008-07-16 19:30:09 +02001058 data->chip_type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001059 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jean Delvareaf200f82008-07-16 19:30:09 +02001061 if (data->chip_type == adm1030)
1062 data->chan_select_table = &auto_channel_select_table_adm1030;
1063 else
1064 data->chan_select_table = &auto_channel_select_table_adm1031;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* Initialize the ADM1031 chip */
Jean Delvare6d6006b2007-12-02 23:33:57 +01001067 adm1031_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Axel Linb060f3c2014-07-03 21:57:44 +08001069 /* sysfs hooks */
1070 data->groups[0] = &adm1031_group;
1071 if (data->chip_type == adm1031)
1072 data->groups[1] = &adm1031_group_opt;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001073
Axel Linb060f3c2014-07-03 21:57:44 +08001074 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1075 data, data->groups);
1076 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Axel Lin278ee1c2014-07-03 21:56:33 +08001079static const struct i2c_device_id adm1031_id[] = {
1080 { "adm1030", adm1030 },
1081 { "adm1031", adm1031 },
1082 { }
1083};
1084MODULE_DEVICE_TABLE(i2c, adm1031_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Axel Lin278ee1c2014-07-03 21:56:33 +08001086static struct i2c_driver adm1031_driver = {
1087 .class = I2C_CLASS_HWMON,
1088 .driver = {
1089 .name = "adm1031",
1090 },
1091 .probe = adm1031_probe,
Axel Lin278ee1c2014-07-03 21:56:33 +08001092 .id_table = adm1031_id,
1093 .detect = adm1031_detect,
1094 .address_list = normal_i2c,
1095};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Axel Linf0967ee2012-01-20 15:38:18 +08001097module_i2c_driver(adm1031_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1100MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1101MODULE_LICENSE("GPL");