blob: 80db367b4c5430e6d6ae72aff5e33b71bc07ef9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck09770b22012-01-14 20:47:36 -08002 * lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 * Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 * Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
Jean Delvare590e8532014-06-11 18:35:56 +02008 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de>
Guenter Roeck09770b22012-01-14 20:47:36 -08009 *
10 * Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -030028#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040033#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020034#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020035#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010037#include <linux/mutex.h>
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -070038#include <linux/util_macros.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050041static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvaree5e9f442009-12-14 21:17:27 +010043enum chips {
Jeremy Gebben11650cf2019-02-04 13:19:05 -070044 lm85, lm96000,
Jean Delvaree5e9f442009-12-14 21:17:27 +010045 adm1027, adt7463, adt7468,
Guenter Roeck06923f82011-02-19 08:27:47 -080046 emc6d100, emc6d102, emc6d103, emc6d103s
Jean Delvaree5e9f442009-12-14 21:17:27 +010047};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* The LM85 registers */
50
Guenter Roeck09770b22012-01-14 20:47:36 -080051#define LM85_REG_IN(nr) (0x20 + (nr))
52#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
53#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Guenter Roeck09770b22012-01-14 20:47:36 -080055#define LM85_REG_TEMP(nr) (0x25 + (nr))
56#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
57#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Fan speeds are LSB, MSB (2 bytes) */
Guenter Roeck09770b22012-01-14 20:47:36 -080060#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
61#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Guenter Roeck09770b22012-01-14 20:47:36 -080063#define LM85_REG_PWM(nr) (0x30 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Guenter Roeck09770b22012-01-14 20:47:36 -080065#define LM85_REG_COMPANY 0x3e
66#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080067
Guenter Roeck09770b22012-01-14 20:47:36 -080068#define ADT7468_REG_CFG5 0x7c
69#define ADT7468_OFF64 (1 << 0)
70#define ADT7468_HFPWM (1 << 1)
71#define IS_ADT7468_OFF64(data) \
Darrick J. Wong79b92f22008-11-12 13:26:59 -080072 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
Guenter Roeck09770b22012-01-14 20:47:36 -080073#define IS_ADT7468_HFPWM(data) \
Jean Delvaref6c61cf2010-10-28 20:31:50 +020074 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
Darrick J. Wong79b92f22008-11-12 13:26:59 -080075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* These are the recognized values for the above regs */
Guenter Roeck09770b22012-01-14 20:47:36 -080077#define LM85_COMPANY_NATIONAL 0x01
78#define LM85_COMPANY_ANALOG_DEV 0x41
79#define LM85_COMPANY_SMSC 0x5c
Guenter Roeck09770b22012-01-14 20:47:36 -080080#define LM85_VERSTEP_LM85C 0x60
81#define LM85_VERSTEP_LM85B 0x62
82#define LM85_VERSTEP_LM96000_1 0x68
83#define LM85_VERSTEP_LM96000_2 0x69
84#define LM85_VERSTEP_ADM1027 0x60
85#define LM85_VERSTEP_ADT7463 0x62
86#define LM85_VERSTEP_ADT7463C 0x6A
87#define LM85_VERSTEP_ADT7468_1 0x71
88#define LM85_VERSTEP_ADT7468_2 0x72
89#define LM85_VERSTEP_EMC6D100_A0 0x60
90#define LM85_VERSTEP_EMC6D100_A1 0x61
91#define LM85_VERSTEP_EMC6D102 0x65
92#define LM85_VERSTEP_EMC6D103_A0 0x68
93#define LM85_VERSTEP_EMC6D103_A1 0x69
94#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Guenter Roeck09770b22012-01-14 20:47:36 -080096#define LM85_REG_CONFIG 0x40
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Guenter Roeck09770b22012-01-14 20:47:36 -080098#define LM85_REG_ALARM1 0x41
99#define LM85_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Guenter Roeck09770b22012-01-14 20:47:36 -0800101#define LM85_REG_VID 0x43
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* Automated FAN control */
Guenter Roeck09770b22012-01-14 20:47:36 -0800104#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
105#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
106#define LM85_REG_AFAN_SPIKE1 0x62
107#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
108#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
109#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
110#define LM85_REG_AFAN_HYST1 0x6d
111#define LM85_REG_AFAN_HYST2 0x6e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Guenter Roeck09770b22012-01-14 20:47:36 -0800113#define ADM1027_REG_EXTEND_ADC1 0x76
114#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define EMC6D100_REG_ALARM3 0x7d
117/* IN5, IN6 and IN7 */
Guenter Roeck09770b22012-01-14 20:47:36 -0800118#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
119#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
120#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
121#define EMC6D102_REG_EXTEND_ADC1 0x85
122#define EMC6D102_REG_EXTEND_ADC2 0x86
123#define EMC6D102_REG_EXTEND_ADC3 0x87
124#define EMC6D102_REG_EXTEND_ADC4 0x88
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Guenter Roeck09770b22012-01-14 20:47:36 -0800126/*
127 * Conversions. Rounding and limit checking is only done on the TO_REG
128 * variants. Note that you should be a bit careful with which arguments
129 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300132/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400133static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
136};
137#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jean Delvare1f448092008-04-29 14:03:37 +0200139#define INS_TO_REG(n, val) \
Guenter Roeck67b20032016-12-04 18:16:48 -0800140 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
141 lm85_scaling[n], 192)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvare1f448092008-04-29 14:03:37 +0200143#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jean Delvare1f448092008-04-29 14:03:37 +0200146#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200149static inline u16 FAN_TO_REG(unsigned long val)
150{
151 if (!val)
152 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800153 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200154}
Jean Delvare1f448092008-04-29 14:03:37 +0200155#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* Temperature is reported in .001 degC increments */
159#define TEMP_TO_REG(val) \
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700160 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
Jean Delvare1f448092008-04-29 14:03:37 +0200161#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200162 SCALE(((val) << 4) + (ext), 16, 1000)
163#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Guenter Roeck2a844c12013-01-09 08:09:34 -0800165#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define PWM_FROM_REG(val) (val)
167
Guenter Roeck09770b22012-01-14 20:47:36 -0800168/*
169 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Limit (low) temp, 1. degC
171 * Hysteresis (below limit), 1. degC (0-15)
172 * Range of speed control, .1 degC (2-80)
173 * Critical (high) temp, 1. degC
174 *
175 * FAN PWMs have the following parameters:
176 * Reference Zone, 1, 2, 3, etc.
177 * Spinup time, .05 sec
178 * PWM value at limit/low temp, 1 count
179 * PWM Frequency, 1. Hz
180 * PWM is Min or OFF below limit, flag
181 * Invert PWM output, flag
182 *
183 * Some chips filter the temp, others the fan.
184 * Filter constant (or disabled) .1 seconds
185 */
186
187/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400188static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200189 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
190 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
191};
192
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700193static int RANGE_TO_REG(long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700195 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Jean Delvare1f448092008-04-29 14:03:37 +0200197#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* These are the PWM frequency encodings */
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700200static const int lm85_freq_map[] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200201 10, 15, 23, 30, 38, 47, 61, 94
202};
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700203
Jeremy Gebbene9b95482019-02-04 13:19:06 -0700204static const int lm96000_freq_map[] = { /* 1 Hz */
205 10, 15, 23, 30, 38, 47, 61, 94,
206 22500, 24000, 25700, 25700, 27700, 27700, 30000, 30000
207};
208
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700209static const int adm1027_freq_map[] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200210 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200211};
212
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700213static int FREQ_TO_REG(const int *map,
214 unsigned int map_size, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700216 return find_closest(freq, map, map_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200218
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700219static int FREQ_FROM_REG(const int *map, unsigned int map_size, u8 reg)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200220{
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700221 return map[reg % map_size];
Jean Delvare8a0795d2008-10-17 17:51:14 +0200222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Guenter Roeck09770b22012-01-14 20:47:36 -0800224/*
225 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * to stand in for the following meanings:
227 * 1 -- PWM responds to Zone 1
228 * 2 -- PWM responds to Zone 2
229 * 3 -- PWM responds to Zone 3
230 * 23 -- PWM responds to the higher temp of Zone 2 or 3
231 * 123 -- PWM responds to highest of Zone 1, 2, or 3
232 * 0 -- PWM is always at 0% (ie, off)
233 * -1 -- PWM is always at 100%
234 * -2 -- PWM responds to manual control
235 */
236
Jean Delvaree89e22b2008-06-25 08:47:35 -0400237static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
238#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Jean Delvare1f448092008-04-29 14:03:37 +0200240static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i;
243
Jean Delvare1f448092008-04-29 14:03:37 +0200244 for (i = 0; i <= 7; ++i)
245 if (zone == lm85_zone_map[i])
246 break;
247 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400249 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Guenter Roeck2a844c12013-01-09 08:09:34 -0800252#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200253#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Guenter Roeck09770b22012-01-14 20:47:36 -0800255/*
256 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * Some sensors are not updated more frequently than once per second
259 * so it doesn't make sense to read them more often than that.
260 * We cache the results and return the saved data if the driver
261 * is called again before a second has elapsed.
262 *
263 * Also, there is significant configuration data for this chip
264 * given the automatic PWM fan control that is possible. There
265 * are about 47 bytes of config data to only 22 bytes of actual
266 * readings. So, we keep the config data up to date in the cache
267 * when it is written and only sample it once every 1 *minute*
268 */
269#define LM85_DATA_INTERVAL (HZ + HZ / 2)
270#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
271
Guenter Roeck09770b22012-01-14 20:47:36 -0800272/*
273 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * This structure encapsulates an entire Zone config. There are
275 * three zones (one for each temperature input) on the lm85
276 */
277struct lm85_zone {
278 s8 limit; /* Low temp limit */
279 u8 hyst; /* Low limit hysteresis. (0-15) */
280 u8 range; /* Temp range, encoded */
281 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b22012-01-14 20:47:36 -0800282 u8 max_desired; /*
283 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * to prevent "drift" as other autofan control
285 * values change.
286 */
287};
288
289struct lm85_autofan {
290 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u8 min_pwm; /* Minimum PWM value, encoded */
292 u8 min_off; /* Min PWM or OFF below "limit", flag */
293};
294
Guenter Roeck09770b22012-01-14 20:47:36 -0800295/*
296 * For each registered chip, we need to keep some data in memory.
297 * The structure is dynamically allocated.
298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299struct lm85_data {
Axel Lin746f6882014-07-23 12:29:11 +0800300 struct i2c_client *client;
301 const struct attribute_group *groups[6];
Jean Delvare8a0795d2008-10-17 17:51:14 +0200302 const int *freq_map;
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700303 unsigned int freq_map_size;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 enum chips type;
306
Guenter Roeckde248802011-02-25 08:19:42 -0800307 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
308
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int valid; /* !=0 if following fields are valid */
311 unsigned long last_reading; /* In jiffies */
312 unsigned long last_config; /* In jiffies */
313
314 u8 in[8]; /* Register value */
315 u8 in_max[8]; /* Register value */
316 u8 in_min[8]; /* Register value */
317 s8 temp[3]; /* Register value */
318 s8 temp_min[3]; /* Register value */
319 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u16 fan[4]; /* Register value */
321 u16 fan_min[4]; /* Register value */
322 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200323 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u8 temp_ext[3]; /* Decoded values */
325 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u8 vid; /* Register value */
327 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800329 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct lm85_autofan autofan[3];
331 struct lm85_zone zone[3];
332};
333
Axel Lin6fd5dd52014-07-23 12:27:39 +0800334static int lm85_read_value(struct i2c_client *client, u8 reg)
335{
336 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Axel Lin6fd5dd52014-07-23 12:27:39 +0800338 /* What size location is it? */
339 switch (reg) {
340 case LM85_REG_FAN(0): /* Read WORD data */
341 case LM85_REG_FAN(1):
342 case LM85_REG_FAN(2):
343 case LM85_REG_FAN(3):
344 case LM85_REG_FAN_MIN(0):
345 case LM85_REG_FAN_MIN(1):
346 case LM85_REG_FAN_MIN(2):
347 case LM85_REG_FAN_MIN(3):
348 case LM85_REG_ALARM1: /* Read both bytes at once */
349 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
350 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
351 break;
352 default: /* Read BYTE data */
353 res = i2c_smbus_read_byte_data(client, reg);
354 break;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Axel Lin6fd5dd52014-07-23 12:27:39 +0800357 return res;
358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Axel Lin6fd5dd52014-07-23 12:27:39 +0800360static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
361{
362 switch (reg) {
363 case LM85_REG_FAN(0): /* Write WORD data */
364 case LM85_REG_FAN(1):
365 case LM85_REG_FAN(2):
366 case LM85_REG_FAN(3):
367 case LM85_REG_FAN_MIN(0):
368 case LM85_REG_FAN_MIN(1):
369 case LM85_REG_FAN_MIN(2):
370 case LM85_REG_FAN_MIN(3):
371 /* NOTE: ALARM is read only, so not included here */
372 i2c_smbus_write_byte_data(client, reg, value & 0xff);
373 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
374 break;
375 default: /* Write BYTE data */
376 i2c_smbus_write_byte_data(client, reg, value);
377 break;
378 }
379}
Jean Delvare67712d02008-10-17 17:51:14 +0200380
Axel Lin6fd5dd52014-07-23 12:27:39 +0800381static struct lm85_data *lm85_update_device(struct device *dev)
382{
Axel Lin746f6882014-07-23 12:29:11 +0800383 struct lm85_data *data = dev_get_drvdata(dev);
384 struct i2c_client *client = data->client;
Axel Lin6fd5dd52014-07-23 12:27:39 +0800385 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Axel Lin6fd5dd52014-07-23 12:27:39 +0800387 mutex_lock(&data->update_lock);
388
389 if (!data->valid ||
390 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
391 /* Things that change quickly */
392 dev_dbg(&client->dev, "Reading sensor values\n");
393
394 /*
395 * Have to read extended bits first to "freeze" the
396 * more significant bits that are read later.
397 * There are 2 additional resolution bits per channel and we
398 * have room for 4, so we shift them to the left.
399 */
400 if (data->type == adm1027 || data->type == adt7463 ||
401 data->type == adt7468) {
402 int ext1 = lm85_read_value(client,
403 ADM1027_REG_EXTEND_ADC1);
404 int ext2 = lm85_read_value(client,
405 ADM1027_REG_EXTEND_ADC2);
406 int val = (ext1 << 8) + ext2;
407
408 for (i = 0; i <= 4; i++)
409 data->in_ext[i] =
410 ((val >> (i * 2)) & 0x03) << 2;
411
412 for (i = 0; i <= 2; i++)
413 data->temp_ext[i] =
414 (val >> ((i + 4) * 2)) & 0x0c;
415 }
416
417 data->vid = lm85_read_value(client, LM85_REG_VID);
418
419 for (i = 0; i <= 3; ++i) {
420 data->in[i] =
421 lm85_read_value(client, LM85_REG_IN(i));
422 data->fan[i] =
423 lm85_read_value(client, LM85_REG_FAN(i));
424 }
425
426 if (!data->has_vid5)
427 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
428
429 if (data->type == adt7468)
430 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
431
432 for (i = 0; i <= 2; ++i) {
433 data->temp[i] =
434 lm85_read_value(client, LM85_REG_TEMP(i));
435 data->pwm[i] =
436 lm85_read_value(client, LM85_REG_PWM(i));
437
438 if (IS_ADT7468_OFF64(data))
439 data->temp[i] -= 64;
440 }
441
442 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
443
444 if (data->type == emc6d100) {
445 /* Three more voltage sensors */
446 for (i = 5; i <= 7; ++i) {
447 data->in[i] = lm85_read_value(client,
448 EMC6D100_REG_IN(i));
449 }
450 /* More alarm bits */
451 data->alarms |= lm85_read_value(client,
452 EMC6D100_REG_ALARM3) << 16;
453 } else if (data->type == emc6d102 || data->type == emc6d103 ||
454 data->type == emc6d103s) {
455 /*
456 * Have to read LSB bits after the MSB ones because
457 * the reading of the MSB bits has frozen the
458 * LSBs (backward from the ADM1027).
459 */
460 int ext1 = lm85_read_value(client,
461 EMC6D102_REG_EXTEND_ADC1);
462 int ext2 = lm85_read_value(client,
463 EMC6D102_REG_EXTEND_ADC2);
464 int ext3 = lm85_read_value(client,
465 EMC6D102_REG_EXTEND_ADC3);
466 int ext4 = lm85_read_value(client,
467 EMC6D102_REG_EXTEND_ADC4);
468 data->in_ext[0] = ext3 & 0x0f;
469 data->in_ext[1] = ext4 & 0x0f;
470 data->in_ext[2] = ext4 >> 4;
471 data->in_ext[3] = ext3 >> 4;
472 data->in_ext[4] = ext2 >> 4;
473
474 data->temp_ext[0] = ext1 & 0x0f;
475 data->temp_ext[1] = ext2 & 0x0f;
476 data->temp_ext[2] = ext1 >> 4;
477 }
478
479 data->last_reading = jiffies;
480 } /* last_reading */
481
482 if (!data->valid ||
483 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
484 /* Things that don't change often */
485 dev_dbg(&client->dev, "Reading config values\n");
486
487 for (i = 0; i <= 3; ++i) {
488 data->in_min[i] =
489 lm85_read_value(client, LM85_REG_IN_MIN(i));
490 data->in_max[i] =
491 lm85_read_value(client, LM85_REG_IN_MAX(i));
492 data->fan_min[i] =
493 lm85_read_value(client, LM85_REG_FAN_MIN(i));
494 }
495
496 if (!data->has_vid5) {
497 data->in_min[4] = lm85_read_value(client,
498 LM85_REG_IN_MIN(4));
499 data->in_max[4] = lm85_read_value(client,
500 LM85_REG_IN_MAX(4));
501 }
502
503 if (data->type == emc6d100) {
504 for (i = 5; i <= 7; ++i) {
505 data->in_min[i] = lm85_read_value(client,
506 EMC6D100_REG_IN_MIN(i));
507 data->in_max[i] = lm85_read_value(client,
508 EMC6D100_REG_IN_MAX(i));
509 }
510 }
511
512 for (i = 0; i <= 2; ++i) {
513 int val;
514
515 data->temp_min[i] =
516 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
517 data->temp_max[i] =
518 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
519
520 data->autofan[i].config =
521 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
522 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700523 data->pwm_freq[i] = val % data->freq_map_size;
Axel Lin6fd5dd52014-07-23 12:27:39 +0800524 data->zone[i].range = val >> 4;
525 data->autofan[i].min_pwm =
526 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
527 data->zone[i].limit =
528 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
529 data->zone[i].critical =
530 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
531
532 if (IS_ADT7468_OFF64(data)) {
533 data->temp_min[i] -= 64;
534 data->temp_max[i] -= 64;
535 data->zone[i].limit -= 64;
536 data->zone[i].critical -= 64;
537 }
538 }
539
540 if (data->type != emc6d103s) {
541 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
542 data->autofan[0].min_off = (i & 0x20) != 0;
543 data->autofan[1].min_off = (i & 0x40) != 0;
544 data->autofan[2].min_off = (i & 0x80) != 0;
545
546 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
547 data->zone[0].hyst = i >> 4;
548 data->zone[1].hyst = i & 0x0f;
549
550 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
551 data->zone[2].hyst = i >> 4;
552 }
553
554 data->last_config = jiffies;
555 } /* last_config */
556
557 data->valid = 1;
558
559 mutex_unlock(&data->update_lock);
560
561 return data;
562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564/* 4 Fans */
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800565static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
566 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Jean Delvareb353a482007-07-05 20:36:12 +0200568 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200570 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
Jean Delvareb353a482007-07-05 20:36:12 +0200572
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800573static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
574 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jean Delvareb353a482007-07-05 20:36:12 +0200576 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200578 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
Jean Delvareb353a482007-07-05 20:36:12 +0200580
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800581static ssize_t fan_min_store(struct device *dev,
582 struct device_attribute *attr, const char *buf,
583 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Jean Delvareb353a482007-07-05 20:36:12 +0200585 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800586 struct lm85_data *data = dev_get_drvdata(dev);
587 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800588 unsigned long val;
589 int err;
590
591 err = kstrtoul(buf, 10, &val);
592 if (err)
593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100595 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 data->fan_min[nr] = FAN_TO_REG(val);
597 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return count;
600}
601
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800602static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
603static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
604static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
605static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
606static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
607static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
608static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
609static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* vid, vrm, alarms */
612
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100613static ssize_t cpu0_vid_show(struct device *dev,
614 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100617 int vid;
618
Guenter Roeckde248802011-02-25 08:19:42 -0800619 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100620 /* 6-pin VID (VRM 10) */
621 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
622 } else {
623 /* 5-pin VID (VRM 9) */
624 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
625 }
626
627 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100630static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100632static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
633 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jean Delvare90d66192007-10-08 18:24:35 +0200635 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return sprintf(buf, "%ld\n", (long) data->vrm);
637}
638
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100639static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
640 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100642 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800643 unsigned long val;
644 int err;
645
646 err = kstrtoul(buf, 10, &val);
647 if (err)
648 return err;
649
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700650 if (val > 255)
651 return -EINVAL;
652
Guenter Roeck09770b22012-01-14 20:47:36 -0800653 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return count;
655}
656
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100657static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100659static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
660 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200663 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100666static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800668static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
669 char *buf)
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200670{
671 int nr = to_sensor_dev_attr(attr)->index;
672 struct lm85_data *data = lm85_update_device(dev);
673 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
674}
675
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800676static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
677static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
678static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
679static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
680static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
681static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 18);
682static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 16);
683static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 17);
684static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
685static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14);
686static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
687static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 6);
688static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 15);
689static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 10);
690static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 11);
691static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 12);
692static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, 13);
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/* pwm */
695
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800696static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
697 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Jean Delvareb353a482007-07-05 20:36:12 +0200699 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200701 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Jean Delvareb353a482007-07-05 20:36:12 +0200703
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800704static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
705 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Jean Delvareb353a482007-07-05 20:36:12 +0200707 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800708 struct lm85_data *data = dev_get_drvdata(dev);
709 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800710 unsigned long val;
711 int err;
712
713 err = kstrtoul(buf, 10, &val);
714 if (err)
715 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100717 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 data->pwm[nr] = PWM_TO_REG(val);
719 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100720 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return count;
722}
Jean Delvareb353a482007-07-05 20:36:12 +0200723
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800724static ssize_t pwm_enable_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jean Delvareb353a482007-07-05 20:36:12 +0200727 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100729 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100732 switch (pwm_zone) {
733 case -1: /* PWM is always at 100% */
734 enable = 0;
735 break;
736 case 0: /* PWM is always at 0% */
737 case -2: /* PWM responds to manual control */
738 enable = 1;
739 break;
740 default: /* PWM in automatic mode */
741 enable = 2;
742 }
743 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800746static ssize_t pwm_enable_store(struct device *dev,
747 struct device_attribute *attr,
748 const char *buf, size_t count)
Jean Delvare455f7912007-12-03 23:28:42 +0100749{
750 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800751 struct lm85_data *data = dev_get_drvdata(dev);
752 struct i2c_client *client = data->client;
Jean Delvare455f7912007-12-03 23:28:42 +0100753 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800754 unsigned long val;
755 int err;
756
757 err = kstrtoul(buf, 10, &val);
758 if (err)
759 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100760
761 switch (val) {
762 case 0:
763 config = 3;
764 break;
765 case 1:
766 config = 7;
767 break;
768 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800769 /*
770 * Here we have to choose arbitrarily one of the 5 possible
771 * configurations; I go for the safest
772 */
Jean Delvare455f7912007-12-03 23:28:42 +0100773 config = 6;
774 break;
775 default:
776 return -EINVAL;
777 }
778
779 mutex_lock(&data->update_lock);
780 data->autofan[nr].config = lm85_read_value(client,
781 LM85_REG_AFAN_CONFIG(nr));
782 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
783 | (config << 5);
784 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
785 data->autofan[nr].config);
786 mutex_unlock(&data->update_lock);
787 return count;
788}
789
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800790static ssize_t pwm_freq_show(struct device *dev,
791 struct device_attribute *attr, char *buf)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200792{
793 int nr = to_sensor_dev_attr(attr)->index;
794 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200795 int freq;
796
797 if (IS_ADT7468_HFPWM(data))
798 freq = 22500;
799 else
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700800 freq = FREQ_FROM_REG(data->freq_map, data->freq_map_size,
801 data->pwm_freq[nr]);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200802
803 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200804}
805
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800806static ssize_t pwm_freq_store(struct device *dev,
807 struct device_attribute *attr, const char *buf,
808 size_t count)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200809{
810 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800811 struct lm85_data *data = dev_get_drvdata(dev);
812 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800813 unsigned long val;
814 int err;
815
816 err = kstrtoul(buf, 10, &val);
817 if (err)
818 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200819
820 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800821 /*
822 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200823 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800824 * This might confuse the user, but there's not much we can do.
825 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200826 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
827 data->cfg5 &= ~ADT7468_HFPWM;
828 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
829 } else { /* Low freq. mode */
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700830 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
Jeremy Gebben57bc3012019-02-04 13:19:03 -0700831 data->freq_map_size, val);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200832 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
833 (data->zone[nr].range << 4)
834 | data->pwm_freq[nr]);
835 if (data->type == adt7468) {
836 data->cfg5 |= ADT7468_HFPWM;
837 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
838 }
839 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200840 mutex_unlock(&data->update_lock);
841 return count;
842}
843
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800844static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
845static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
846static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
847static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
848static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
849static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
850static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
851static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
852static SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854/* Voltages */
855
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800856static ssize_t in_show(struct device *dev, struct device_attribute *attr,
857 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Jean Delvareb353a482007-07-05 20:36:12 +0200859 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200861 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
862 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
Jean Delvareb353a482007-07-05 20:36:12 +0200864
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800865static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
866 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Jean Delvareb353a482007-07-05 20:36:12 +0200868 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200870 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Jean Delvareb353a482007-07-05 20:36:12 +0200872
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800873static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
874 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jean Delvareb353a482007-07-05 20:36:12 +0200876 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800877 struct lm85_data *data = dev_get_drvdata(dev);
878 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800879 long val;
880 int err;
881
882 err = kstrtol(buf, 10, &val);
883 if (err)
884 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100886 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 data->in_min[nr] = INS_TO_REG(nr, val);
888 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100889 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return count;
891}
Jean Delvareb353a482007-07-05 20:36:12 +0200892
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800893static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
894 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvareb353a482007-07-05 20:36:12 +0200896 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200898 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
Jean Delvareb353a482007-07-05 20:36:12 +0200900
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800901static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
902 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jean Delvareb353a482007-07-05 20:36:12 +0200904 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800905 struct lm85_data *data = dev_get_drvdata(dev);
906 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800907 long val;
908 int err;
909
910 err = kstrtol(buf, 10, &val);
911 if (err)
912 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100914 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 data->in_max[nr] = INS_TO_REG(nr, val);
916 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100917 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return count;
919}
Jean Delvareb353a482007-07-05 20:36:12 +0200920
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800921static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
922static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
923static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
924static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
925static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
926static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
927static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
928static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
929static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
930static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
931static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
932static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
933static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
934static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
935static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
936static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
937static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
938static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
939static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
940static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
941static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
942static SENSOR_DEVICE_ATTR_RO(in7_input, in, 7);
943static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
944static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946/* Temps */
947
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800948static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
949 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Jean Delvareb353a482007-07-05 20:36:12 +0200951 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200953 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
954 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
Jean Delvareb353a482007-07-05 20:36:12 +0200956
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800957static ssize_t temp_min_show(struct device *dev,
958 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Jean Delvareb353a482007-07-05 20:36:12 +0200960 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200962 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Jean Delvareb353a482007-07-05 20:36:12 +0200964
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800965static ssize_t temp_min_store(struct device *dev,
966 struct device_attribute *attr, const char *buf,
967 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Jean Delvareb353a482007-07-05 20:36:12 +0200969 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800970 struct lm85_data *data = dev_get_drvdata(dev);
971 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800972 long val;
973 int err;
974
975 err = kstrtol(buf, 10, &val);
976 if (err)
977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800979 if (IS_ADT7468_OFF64(data))
980 val += 64;
981
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100982 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 data->temp_min[nr] = TEMP_TO_REG(val);
984 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100985 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return count;
987}
Jean Delvareb353a482007-07-05 20:36:12 +0200988
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800989static ssize_t temp_max_show(struct device *dev,
990 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jean Delvareb353a482007-07-05 20:36:12 +0200992 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200994 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
Jean Delvareb353a482007-07-05 20:36:12 +0200996
Guenter Roeck7bc85e42019-01-22 10:40:33 -0800997static ssize_t temp_max_store(struct device *dev,
998 struct device_attribute *attr, const char *buf,
999 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jean Delvareb353a482007-07-05 20:36:12 +02001001 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001002 struct lm85_data *data = dev_get_drvdata(dev);
1003 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001004 long val;
1005 int err;
1006
1007 err = kstrtol(buf, 10, &val);
1008 if (err)
1009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001011 if (IS_ADT7468_OFF64(data))
1012 val += 64;
1013
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001014 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 data->temp_max[nr] = TEMP_TO_REG(val);
1016 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001017 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return count;
1019}
Jean Delvareb353a482007-07-05 20:36:12 +02001020
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001021static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1022static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1023static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1024static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1025static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1026static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1027static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1028static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1029static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031/* Automatic PWM control */
1032
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001033static ssize_t pwm_auto_channels_show(struct device *dev,
1034 struct device_attribute *attr,
1035 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jean Delvareb353a482007-07-05 20:36:12 +02001037 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001039 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
Jean Delvareb353a482007-07-05 20:36:12 +02001041
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001042static ssize_t pwm_auto_channels_store(struct device *dev,
1043 struct device_attribute *attr,
1044 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Jean Delvareb353a482007-07-05 20:36:12 +02001046 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001047 struct lm85_data *data = dev_get_drvdata(dev);
1048 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001049 long val;
1050 int err;
1051
1052 err = kstrtol(buf, 10, &val);
1053 if (err)
1054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +02001058 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1060 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001061 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return count;
1063}
Jean Delvareb353a482007-07-05 20:36:12 +02001064
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001065static ssize_t pwm_auto_pwm_min_show(struct device *dev,
1066 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jean Delvareb353a482007-07-05 20:36:12 +02001068 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001070 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
Jean Delvareb353a482007-07-05 20:36:12 +02001072
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001073static ssize_t pwm_auto_pwm_min_store(struct device *dev,
1074 struct device_attribute *attr,
1075 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Jean Delvareb353a482007-07-05 20:36:12 +02001077 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001078 struct lm85_data *data = dev_get_drvdata(dev);
1079 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001080 unsigned long val;
1081 int err;
1082
1083 err = kstrtoul(buf, 10, &val);
1084 if (err)
1085 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001087 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1089 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1090 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001091 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return count;
1093}
Jean Delvareb353a482007-07-05 20:36:12 +02001094
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001095static ssize_t pwm_auto_pwm_minctl_show(struct device *dev,
1096 struct device_attribute *attr,
1097 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Jean Delvareb353a482007-07-05 20:36:12 +02001099 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001101 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
Jean Delvareb353a482007-07-05 20:36:12 +02001103
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001104static ssize_t pwm_auto_pwm_minctl_store(struct device *dev,
1105 struct device_attribute *attr,
1106 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Jean Delvareb353a482007-07-05 20:36:12 +02001108 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001109 struct lm85_data *data = dev_get_drvdata(dev);
1110 struct i2c_client *client = data->client;
Jean Delvare7133e562008-04-12 19:56:35 +02001111 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -08001112 long val;
1113 int err;
1114
1115 err = kstrtol(buf, 10, &val);
1116 if (err)
1117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001119 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +02001121 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1122 tmp &= ~(0x20 << nr);
1123 if (data->autofan[nr].min_off)
1124 tmp |= 0x20 << nr;
1125 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001126 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return count;
1128}
Jean Delvareb353a482007-07-05 20:36:12 +02001129
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001130static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels, pwm_auto_channels, 0);
1131static SENSOR_DEVICE_ATTR_RW(pwm1_auto_pwm_min, pwm_auto_pwm_min, 0);
1132static SENSOR_DEVICE_ATTR_RW(pwm1_auto_pwm_minctl, pwm_auto_pwm_minctl, 0);
1133static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels, pwm_auto_channels, 1);
1134static SENSOR_DEVICE_ATTR_RW(pwm2_auto_pwm_min, pwm_auto_pwm_min, 1);
1135static SENSOR_DEVICE_ATTR_RW(pwm2_auto_pwm_minctl, pwm_auto_pwm_minctl, 1);
1136static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels, pwm_auto_channels, 2);
1137static SENSOR_DEVICE_ATTR_RW(pwm3_auto_pwm_min, pwm_auto_pwm_min, 2);
1138static SENSOR_DEVICE_ATTR_RW(pwm3_auto_pwm_minctl, pwm_auto_pwm_minctl, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/* Temperature settings for automatic PWM control */
1141
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001142static ssize_t temp_auto_temp_off_show(struct device *dev,
1143 struct device_attribute *attr,
1144 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Jean Delvareb353a482007-07-05 20:36:12 +02001146 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001148 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 HYST_FROM_REG(data->zone[nr].hyst));
1150}
Jean Delvareb353a482007-07-05 20:36:12 +02001151
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001152static ssize_t temp_auto_temp_off_store(struct device *dev,
1153 struct device_attribute *attr,
1154 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Jean Delvareb353a482007-07-05 20:36:12 +02001156 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001157 struct lm85_data *data = dev_get_drvdata(dev);
1158 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001160 long val;
1161 int err;
1162
1163 err = kstrtol(buf, 10, &val);
1164 if (err)
1165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001167 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +02001170 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1172 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +02001173 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 } else {
1175 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +02001176 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001178 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return count;
1180}
Jean Delvareb353a482007-07-05 20:36:12 +02001181
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001182static ssize_t temp_auto_temp_min_show(struct device *dev,
1183 struct device_attribute *attr,
1184 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jean Delvareb353a482007-07-05 20:36:12 +02001186 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001188 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
Jean Delvareb353a482007-07-05 20:36:12 +02001190
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001191static ssize_t temp_auto_temp_min_store(struct device *dev,
1192 struct device_attribute *attr,
1193 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Jean Delvareb353a482007-07-05 20:36:12 +02001195 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001196 struct lm85_data *data = dev_get_drvdata(dev);
1197 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001198 long val;
1199 int err;
1200
1201 err = kstrtol(buf, 10, &val);
1202 if (err)
1203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001205 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 data->zone[nr].limit = TEMP_TO_REG(val);
1207 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1208 data->zone[nr].limit);
1209
1210/* Update temp_auto_max and temp_auto_range */
1211 data->zone[nr].range = RANGE_TO_REG(
1212 TEMP_FROM_REG(data->zone[nr].max_desired) -
1213 TEMP_FROM_REG(data->zone[nr].limit));
1214 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1215 ((data->zone[nr].range & 0x0f) << 4)
Jeremy Gebben57bc3012019-02-04 13:19:03 -07001216 | data->pwm_freq[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001218 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return count;
1220}
Jean Delvareb353a482007-07-05 20:36:12 +02001221
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001222static ssize_t temp_auto_temp_max_show(struct device *dev,
1223 struct device_attribute *attr,
1224 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Jean Delvareb353a482007-07-05 20:36:12 +02001226 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001228 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 RANGE_FROM_REG(data->zone[nr].range));
1230}
Jean Delvareb353a482007-07-05 20:36:12 +02001231
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001232static ssize_t temp_auto_temp_max_store(struct device *dev,
1233 struct device_attribute *attr,
1234 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jean Delvareb353a482007-07-05 20:36:12 +02001236 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001237 struct lm85_data *data = dev_get_drvdata(dev);
1238 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001240 long val;
1241 int err;
1242
1243 err = kstrtol(buf, 10, &val);
1244 if (err)
1245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001247 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 min = TEMP_FROM_REG(data->zone[nr].limit);
1249 data->zone[nr].max_desired = TEMP_TO_REG(val);
1250 data->zone[nr].range = RANGE_TO_REG(
1251 val - min);
1252 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1253 ((data->zone[nr].range & 0x0f) << 4)
Jeremy Gebben57bc3012019-02-04 13:19:03 -07001254 | data->pwm_freq[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001255 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return count;
1257}
Jean Delvareb353a482007-07-05 20:36:12 +02001258
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001259static ssize_t temp_auto_temp_crit_show(struct device *dev,
1260 struct device_attribute *attr,
1261 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Jean Delvareb353a482007-07-05 20:36:12 +02001263 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001265 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
Jean Delvareb353a482007-07-05 20:36:12 +02001267
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001268static ssize_t temp_auto_temp_crit_store(struct device *dev,
1269 struct device_attribute *attr,
1270 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Jean Delvareb353a482007-07-05 20:36:12 +02001272 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001273 struct lm85_data *data = dev_get_drvdata(dev);
1274 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001275 long val;
1276 int err;
1277
1278 err = kstrtol(buf, 10, &val);
1279 if (err)
1280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001282 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 data->zone[nr].critical = TEMP_TO_REG(val);
1284 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1285 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001286 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return count;
1288}
Jean Delvareb353a482007-07-05 20:36:12 +02001289
Guenter Roeck7bc85e42019-01-22 10:40:33 -08001290static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_off, temp_auto_temp_off, 0);
1291static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_min, temp_auto_temp_min, 0);
1292static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_max, temp_auto_temp_max, 0);
1293static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_crit, temp_auto_temp_crit, 0);
1294static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_off, temp_auto_temp_off, 1);
1295static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_min, temp_auto_temp_min, 1);
1296static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_max, temp_auto_temp_max, 1);
1297static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_crit, temp_auto_temp_crit, 1);
1298static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_off, temp_auto_temp_off, 2);
1299static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_min, temp_auto_temp_min, 2);
1300static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_max, temp_auto_temp_max, 2);
1301static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_crit, temp_auto_temp_crit, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001303static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001304 &sensor_dev_attr_fan1_input.dev_attr.attr,
1305 &sensor_dev_attr_fan2_input.dev_attr.attr,
1306 &sensor_dev_attr_fan3_input.dev_attr.attr,
1307 &sensor_dev_attr_fan4_input.dev_attr.attr,
1308 &sensor_dev_attr_fan1_min.dev_attr.attr,
1309 &sensor_dev_attr_fan2_min.dev_attr.attr,
1310 &sensor_dev_attr_fan3_min.dev_attr.attr,
1311 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001312 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1313 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1314 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1315 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001316
1317 &sensor_dev_attr_pwm1.dev_attr.attr,
1318 &sensor_dev_attr_pwm2.dev_attr.attr,
1319 &sensor_dev_attr_pwm3.dev_attr.attr,
1320 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1321 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1322 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001323 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1324 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1325 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001326
1327 &sensor_dev_attr_in0_input.dev_attr.attr,
1328 &sensor_dev_attr_in1_input.dev_attr.attr,
1329 &sensor_dev_attr_in2_input.dev_attr.attr,
1330 &sensor_dev_attr_in3_input.dev_attr.attr,
1331 &sensor_dev_attr_in0_min.dev_attr.attr,
1332 &sensor_dev_attr_in1_min.dev_attr.attr,
1333 &sensor_dev_attr_in2_min.dev_attr.attr,
1334 &sensor_dev_attr_in3_min.dev_attr.attr,
1335 &sensor_dev_attr_in0_max.dev_attr.attr,
1336 &sensor_dev_attr_in1_max.dev_attr.attr,
1337 &sensor_dev_attr_in2_max.dev_attr.attr,
1338 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001339 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1340 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1341 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1342 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001343
1344 &sensor_dev_attr_temp1_input.dev_attr.attr,
1345 &sensor_dev_attr_temp2_input.dev_attr.attr,
1346 &sensor_dev_attr_temp3_input.dev_attr.attr,
1347 &sensor_dev_attr_temp1_min.dev_attr.attr,
1348 &sensor_dev_attr_temp2_min.dev_attr.attr,
1349 &sensor_dev_attr_temp3_min.dev_attr.attr,
1350 &sensor_dev_attr_temp1_max.dev_attr.attr,
1351 &sensor_dev_attr_temp2_max.dev_attr.attr,
1352 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001353 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1354 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1355 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1356 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1357 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001358
1359 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1360 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1361 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1362 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1363 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1364 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001365
Jean Delvareb353a482007-07-05 20:36:12 +02001366 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1367 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1368 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1369 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1370 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1371 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1372 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1373 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1374 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1375
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001376 &dev_attr_vrm.attr,
1377 &dev_attr_cpu0_vid.attr,
1378 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001379 NULL
1380};
1381
1382static const struct attribute_group lm85_group = {
1383 .attrs = lm85_attributes,
1384};
1385
Guenter Roeck06923f82011-02-19 08:27:47 -08001386static struct attribute *lm85_attributes_minctl[] = {
1387 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1388 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1389 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001390 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001391};
1392
1393static const struct attribute_group lm85_group_minctl = {
1394 .attrs = lm85_attributes_minctl,
1395};
1396
1397static struct attribute *lm85_attributes_temp_off[] = {
1398 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1399 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1400 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001401 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001402};
1403
1404static const struct attribute_group lm85_group_temp_off = {
1405 .attrs = lm85_attributes_temp_off,
1406};
1407
Jean Delvare6b9aad22007-07-05 20:37:21 +02001408static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001409 &sensor_dev_attr_in4_input.dev_attr.attr,
1410 &sensor_dev_attr_in4_min.dev_attr.attr,
1411 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001412 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001413 NULL
1414};
1415
Jean Delvare6b9aad22007-07-05 20:37:21 +02001416static const struct attribute_group lm85_group_in4 = {
1417 .attrs = lm85_attributes_in4,
1418};
1419
1420static struct attribute *lm85_attributes_in567[] = {
1421 &sensor_dev_attr_in5_input.dev_attr.attr,
1422 &sensor_dev_attr_in6_input.dev_attr.attr,
1423 &sensor_dev_attr_in7_input.dev_attr.attr,
1424 &sensor_dev_attr_in5_min.dev_attr.attr,
1425 &sensor_dev_attr_in6_min.dev_attr.attr,
1426 &sensor_dev_attr_in7_min.dev_attr.attr,
1427 &sensor_dev_attr_in5_max.dev_attr.attr,
1428 &sensor_dev_attr_in6_max.dev_attr.attr,
1429 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001430 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1431 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1432 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001433 NULL
1434};
1435
1436static const struct attribute_group lm85_group_in567 = {
1437 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001438};
1439
Jean Delvare5f44759472008-06-25 09:10:30 -04001440static void lm85_init_client(struct i2c_client *client)
1441{
1442 int value;
1443
1444 /* Start monitoring if needed */
1445 value = lm85_read_value(client, LM85_REG_CONFIG);
1446 if (!(value & 0x01)) {
1447 dev_info(&client->dev, "Starting monitoring\n");
1448 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1449 }
1450
1451 /* Warn about unusual configuration bits */
1452 if (value & 0x02)
1453 dev_warn(&client->dev, "Device configuration is locked\n");
1454 if (!(value & 0x04))
1455 dev_warn(&client->dev, "Device is not ready\n");
1456}
1457
Jean Delvare5cfaf332009-09-15 17:18:14 +02001458static int lm85_is_fake(struct i2c_client *client)
1459{
1460 /*
1461 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1462 * emulate the former except that it has no hardware monitoring function
1463 * so the readings are always 0.
1464 */
1465 int i;
1466 u8 in_temp, fan;
1467
1468 for (i = 0; i < 8; i++) {
1469 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1470 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1471 if (in_temp != 0x00 || fan != 0xff)
1472 return 0;
1473 }
1474
1475 return 1;
1476}
1477
Jean Delvare67712d02008-10-17 17:51:14 +02001478/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001479static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Jean Delvare67712d02008-10-17 17:51:14 +02001481 struct i2c_adapter *adapter = client->adapter;
1482 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001483 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001484 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Jean Delvaree89e22b2008-06-25 08:47:35 -04001486 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001488 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jean Delvared42a2eb2009-12-09 20:35:53 +01001491 /* Determine the chip type */
1492 company = lm85_read_value(client, LM85_REG_COMPANY);
1493 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Guenter Roeckb55f3752013-01-10 10:01:24 -08001495 dev_dbg(&adapter->dev,
1496 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001497 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Jean Delvared42a2eb2009-12-09 20:35:53 +01001499 if (company == LM85_COMPANY_NATIONAL) {
1500 switch (verstep) {
1501 case LM85_VERSTEP_LM85C:
1502 type_name = "lm85c";
1503 break;
1504 case LM85_VERSTEP_LM85B:
1505 type_name = "lm85b";
1506 break;
1507 case LM85_VERSTEP_LM96000_1:
1508 case LM85_VERSTEP_LM96000_2:
1509 /* Check for Winbond WPCD377I */
1510 if (lm85_is_fake(client)) {
1511 dev_dbg(&adapter->dev,
1512 "Found Winbond WPCD377I, ignoring\n");
1513 return -ENODEV;
1514 }
Jeremy Gebben11650cf2019-02-04 13:19:05 -07001515 type_name = "lm96000";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001516 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001517 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001518 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1519 switch (verstep) {
1520 case LM85_VERSTEP_ADM1027:
1521 type_name = "adm1027";
1522 break;
1523 case LM85_VERSTEP_ADT7463:
1524 case LM85_VERSTEP_ADT7463C:
1525 type_name = "adt7463";
1526 break;
1527 case LM85_VERSTEP_ADT7468_1:
1528 case LM85_VERSTEP_ADT7468_2:
1529 type_name = "adt7468";
1530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001532 } else if (company == LM85_COMPANY_SMSC) {
1533 switch (verstep) {
1534 case LM85_VERSTEP_EMC6D100_A0:
1535 case LM85_VERSTEP_EMC6D100_A1:
1536 /* Note: we can't tell a '100 from a '101 */
1537 type_name = "emc6d100";
1538 break;
1539 case LM85_VERSTEP_EMC6D102:
1540 type_name = "emc6d102";
1541 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001542 case LM85_VERSTEP_EMC6D103_A0:
1543 case LM85_VERSTEP_EMC6D103_A1:
1544 type_name = "emc6d103";
1545 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001546 case LM85_VERSTEP_EMC6D103S:
1547 type_name = "emc6d103s";
1548 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
Jean Delvare590e8532014-06-11 18:35:56 +02001552 if (!type_name)
1553 return -ENODEV;
1554
Jean Delvare67712d02008-10-17 17:51:14 +02001555 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jean Delvare67712d02008-10-17 17:51:14 +02001557 return 0;
1558}
1559
Axel Lin746f6882014-07-23 12:29:11 +08001560static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001561{
Axel Lin746f6882014-07-23 12:29:11 +08001562 struct device *dev = &client->dev;
1563 struct device *hwmon_dev;
Jean Delvare67712d02008-10-17 17:51:14 +02001564 struct lm85_data *data;
Axel Lin746f6882014-07-23 12:29:11 +08001565 int idx = 0;
Jean Delvare67712d02008-10-17 17:51:14 +02001566
Axel Lin746f6882014-07-23 12:29:11 +08001567 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001568 if (!data)
1569 return -ENOMEM;
1570
Axel Lin746f6882014-07-23 12:29:11 +08001571 data->client = client;
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001572 if (client->dev.of_node)
1573 data->type = (enum chips)of_device_get_match_data(&client->dev);
1574 else
1575 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001576 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jean Delvare67712d02008-10-17 17:51:14 +02001578 /* Fill in the chip specific driver values */
1579 switch (data->type) {
1580 case adm1027:
1581 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001582 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001583 case emc6d100:
1584 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001585 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001586 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001587 data->freq_map = adm1027_freq_map;
Jeremy Gebben57bc3012019-02-04 13:19:03 -07001588 data->freq_map_size = ARRAY_SIZE(adm1027_freq_map);
Jean Delvare67712d02008-10-17 17:51:14 +02001589 break;
Jeremy Gebbene9b95482019-02-04 13:19:06 -07001590 case lm96000:
1591 data->freq_map = lm96000_freq_map;
1592 data->freq_map_size = ARRAY_SIZE(lm96000_freq_map);
1593 break;
Jean Delvare67712d02008-10-17 17:51:14 +02001594 default:
1595 data->freq_map = lm85_freq_map;
Jeremy Gebben57bc3012019-02-04 13:19:03 -07001596 data->freq_map_size = ARRAY_SIZE(lm85_freq_map);
Jean Delvare67712d02008-10-17 17:51:14 +02001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001600 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001603 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Axel Lin746f6882014-07-23 12:29:11 +08001605 /* sysfs hooks */
1606 data->groups[idx++] = &lm85_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Guenter Roeck06923f82011-02-19 08:27:47 -08001608 /* minctl and temp_off exist on all chips except emc6d103s */
1609 if (data->type != emc6d103s) {
Axel Lin746f6882014-07-23 12:29:11 +08001610 data->groups[idx++] = &lm85_group_minctl;
1611 data->groups[idx++] = &lm85_group_temp_off;
Guenter Roeck06923f82011-02-19 08:27:47 -08001612 }
1613
Guenter Roeck09770b22012-01-14 20:47:36 -08001614 /*
1615 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1616 * as a sixth digital VID input rather than an analog input.
1617 */
Guenter Roeckde248802011-02-25 08:19:42 -08001618 if (data->type == adt7463 || data->type == adt7468) {
1619 u8 vid = lm85_read_value(client, LM85_REG_VID);
1620 if (vid & 0x80)
1621 data->has_vid5 = true;
1622 }
1623
Axel Lin746f6882014-07-23 12:29:11 +08001624 if (!data->has_vid5)
1625 data->groups[idx++] = &lm85_group_in4;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001626
1627 /* The EMC6D100 has 3 additional voltage inputs */
Axel Lin746f6882014-07-23 12:29:11 +08001628 if (data->type == emc6d100)
1629 data->groups[idx++] = &lm85_group_in567;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001630
Axel Lin746f6882014-07-23 12:29:11 +08001631 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1632 data, data->groups);
1633 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Axel Lin6fd5dd52014-07-23 12:27:39 +08001636static const struct i2c_device_id lm85_id[] = {
1637 { "adm1027", adm1027 },
1638 { "adt7463", adt7463 },
1639 { "adt7468", adt7468 },
1640 { "lm85", lm85 },
1641 { "lm85b", lm85 },
1642 { "lm85c", lm85 },
Jeremy Gebben11650cf2019-02-04 13:19:05 -07001643 { "lm96000", lm96000 },
Axel Lin6fd5dd52014-07-23 12:27:39 +08001644 { "emc6d100", emc6d100 },
1645 { "emc6d101", emc6d100 },
1646 { "emc6d102", emc6d102 },
1647 { "emc6d103", emc6d103 },
1648 { "emc6d103s", emc6d103s },
1649 { }
1650};
1651MODULE_DEVICE_TABLE(i2c, lm85_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Guenter Roeck20b497a2019-04-04 07:44:40 -07001653static const struct of_device_id __maybe_unused lm85_of_match[] = {
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001654 {
1655 .compatible = "adi,adm1027",
1656 .data = (void *)adm1027
1657 },
1658 {
1659 .compatible = "adi,adt7463",
1660 .data = (void *)adt7463
1661 },
1662 {
1663 .compatible = "adi,adt7468",
1664 .data = (void *)adt7468
1665 },
1666 {
1667 .compatible = "national,lm85",
1668 .data = (void *)lm85
1669 },
1670 {
1671 .compatible = "national,lm85b",
1672 .data = (void *)lm85
1673 },
1674 {
1675 .compatible = "national,lm85c",
1676 .data = (void *)lm85
1677 },
1678 {
Jeremy Gebben11650cf2019-02-04 13:19:05 -07001679 .compatible = "ti,lm96000",
1680 .data = (void *)lm96000
1681 },
1682 {
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001683 .compatible = "smsc,emc6d100",
1684 .data = (void *)emc6d100
1685 },
1686 {
1687 .compatible = "smsc,emc6d101",
1688 .data = (void *)emc6d100
1689 },
1690 {
1691 .compatible = "smsc,emc6d102",
1692 .data = (void *)emc6d102
1693 },
1694 {
1695 .compatible = "smsc,emc6d103",
1696 .data = (void *)emc6d103
1697 },
1698 {
1699 .compatible = "smsc,emc6d103s",
1700 .data = (void *)emc6d103s
1701 },
1702 { },
1703};
1704MODULE_DEVICE_TABLE(of, lm85_of_match);
1705
Axel Lin6fd5dd52014-07-23 12:27:39 +08001706static struct i2c_driver lm85_driver = {
1707 .class = I2C_CLASS_HWMON,
1708 .driver = {
1709 .name = "lm85",
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001710 .of_match_table = of_match_ptr(lm85_of_match),
Axel Lin6fd5dd52014-07-23 12:27:39 +08001711 },
1712 .probe = lm85_probe,
Axel Lin6fd5dd52014-07-23 12:27:39 +08001713 .id_table = lm85_id,
1714 .detect = lm85_detect,
1715 .address_list = normal_i2c,
1716};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Axel Linf0967ee2012-01-20 15:38:18 +08001718module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001721MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1722 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001723 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724MODULE_DESCRIPTION("LM85-B, LM85-C driver");