blob: c1e4cfb40c3def02d7e464661c6fb4ee4cffc6b4 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Juerg Haefliger94319962007-06-09 10:11:16 -04002/*
Juerg Haefligerea694432010-05-27 19:59:01 +02003 * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x, SCH5027,
4 * and SCH5127 Super-I/O chips integrated hardware monitoring
5 * features.
6 * Copyright (c) 2007, 2008, 2009, 2010 Juerg Haefliger <juergh@gmail.com>
Juerg Haefliger94319962007-06-09 10:11:16 -04007 *
Juerg Haefligere95c2372007-10-07 21:27:35 -07008 * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
Juerg Haefliger549edb82008-08-06 22:41:03 +02009 * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
Juerg Haefligerea694432010-05-27 19:59:01 +020010 * if a SCH311x or SCH5127 chip is found. Both types of chips have very
11 * similar hardware monitoring capabilities but differ in the way they can be
12 * accessed.
Juerg Haefliger94319962007-06-09 10:11:16 -040013 */
14
Joe Perches9c6e13b2010-10-20 06:51:32 +000015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Juerg Haefliger94319962007-06-09 10:11:16 -040017#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/jiffies.h>
21#include <linux/i2c.h>
Juerg Haefligere95c2372007-10-07 21:27:35 -070022#include <linux/platform_device.h>
Juerg Haefliger94319962007-06-09 10:11:16 -040023#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/hwmon-vid.h>
26#include <linux/err.h>
27#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010028#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020029#include <linux/io.h>
Juerg Haefliger94319962007-06-09 10:11:16 -040030
Juerg Haefligere95c2372007-10-07 21:27:35 -070031/* ISA device, if found */
32static struct platform_device *pdev;
33
Juerg Haefliger94319962007-06-09 10:11:16 -040034/* Module load parameters */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103035static bool force_start;
Juerg Haefliger94319962007-06-09 10:11:16 -040036module_param(force_start, bool, 0);
37MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
38
Jean Delvare67b671b2007-12-06 23:13:42 +010039static unsigned short force_id;
40module_param(force_id, ushort, 0);
41MODULE_PARM_DESC(force_id, "Override the detected device ID");
42
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool probe_all_addr;
Juerg Haefliger92430b62008-04-03 21:34:19 -070044module_param(probe_all_addr, bool, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080045MODULE_PARM_DESC(probe_all_addr,
46 "Include probing of non-standard LPC addresses");
Juerg Haefliger92430b62008-04-03 21:34:19 -070047
Juerg Haefliger94319962007-06-09 10:11:16 -040048/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050049static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
Juerg Haefliger94319962007-06-09 10:11:16 -040050
Juerg Haefligerea694432010-05-27 19:59:01 +020051enum chips { dme1737, sch5027, sch311x, sch5127 };
Juerg Haefliger94319962007-06-09 10:11:16 -040052
Guenter Roeckb55f3752013-01-10 10:01:24 -080053#define DO_REPORT "Please report to the driver maintainer."
54
Juerg Haefliger94319962007-06-09 10:11:16 -040055/* ---------------------------------------------------------------------
56 * Registers
57 *
58 * The sensors are defined as follows:
59 *
60 * Voltages Temperatures
61 * -------- ------------
62 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
63 * in1 Vccp (proc core) temp2 Internal temp
64 * in2 VCC (internal +3.3V) temp3 Remote diode 2
65 * in3 +5V
66 * in4 +12V
67 * in5 VTR (+3.3V stby)
68 * in6 Vbat
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010069 * in7 Vtrip (sch5127 only)
Juerg Haefliger94319962007-06-09 10:11:16 -040070 *
71 * --------------------------------------------------------------------- */
72
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010073/* Voltages (in) numbered 0-7 (ix) */
Guenter Roeckc8de8362012-01-14 13:25:01 -080074#define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) : \
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010075 (ix) < 7 ? 0x94 + (ix) : \
76 0x1f)
Guenter Roeckc8de8362012-01-14 13:25:01 -080077#define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
Juerg Haefliger94319962007-06-09 10:11:16 -040078 : 0x91 + (ix) * 2)
Guenter Roeckc8de8362012-01-14 13:25:01 -080079#define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
Juerg Haefliger94319962007-06-09 10:11:16 -040080 : 0x92 + (ix) * 2)
81
82/* Temperatures (temp) numbered 0-2 (ix) */
83#define DME1737_REG_TEMP(ix) (0x25 + (ix))
84#define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
85#define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
86#define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
87 : 0x1c + (ix))
88
Guenter Roeckc8de8362012-01-14 13:25:01 -080089/*
90 * Voltage and temperature LSBs
Juerg Haefliger94319962007-06-09 10:11:16 -040091 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
92 * IN_TEMP_LSB(0) = [in5, in6]
93 * IN_TEMP_LSB(1) = [temp3, temp1]
94 * IN_TEMP_LSB(2) = [in4, temp2]
95 * IN_TEMP_LSB(3) = [in3, in0]
Juerg Haefligerd4b94e12011-01-12 21:55:13 +010096 * IN_TEMP_LSB(4) = [in2, in1]
Guenter Roeckc8de8362012-01-14 13:25:01 -080097 * IN_TEMP_LSB(5) = [res, in7]
98 */
Juerg Haefliger94319962007-06-09 10:11:16 -040099#define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100100static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0, 5};
101static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4, 4};
Juerg Haefliger94319962007-06-09 10:11:16 -0400102static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
103static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
104
105/* Fans numbered 0-5 (ix) */
106#define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
107 : 0xa1 + (ix) * 2)
108#define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
109 : 0xa5 + (ix) * 2)
110#define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
111 : 0xb2 + (ix))
112#define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
113
114/* PWMs numbered 0-2, 4-5 (ix) */
115#define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
116 : 0xa1 + (ix))
117#define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
118#define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
119#define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
120 : 0xa3 + (ix))
Guenter Roeckc8de8362012-01-14 13:25:01 -0800121/*
122 * The layout of the ramp rate registers is different from the other pwm
Juerg Haefliger94319962007-06-09 10:11:16 -0400123 * registers. The bits for the 3 PWMs are stored in 2 registers:
124 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
Guenter Roeckc8de8362012-01-14 13:25:01 -0800125 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0]
126 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400127#define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
128
129/* Thermal zones 0-2 */
130#define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
131#define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
Guenter Roeckc8de8362012-01-14 13:25:01 -0800132/*
133 * The layout of the hysteresis registers is different from the other zone
Juerg Haefliger94319962007-06-09 10:11:16 -0400134 * registers. The bits for the 3 zones are stored in 2 registers:
135 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
Guenter Roeckc8de8362012-01-14 13:25:01 -0800136 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES]
137 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400138#define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
139
Guenter Roeckc8de8362012-01-14 13:25:01 -0800140/*
141 * Alarm registers and bit mapping
Juerg Haefliger94319962007-06-09 10:11:16 -0400142 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
Guenter Roeckc8de8362012-01-14 13:25:01 -0800143 * alarm value [0, ALARM3, ALARM2, ALARM1].
144 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400145#define DME1737_REG_ALARM1 0x41
146#define DME1737_REG_ALARM2 0x42
147#define DME1737_REG_ALARM3 0x83
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100148static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17, 18};
Juerg Haefliger94319962007-06-09 10:11:16 -0400149static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
150static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
151
152/* Miscellaneous registers */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700153#define DME1737_REG_DEVICE 0x3d
Juerg Haefliger94319962007-06-09 10:11:16 -0400154#define DME1737_REG_COMPANY 0x3e
155#define DME1737_REG_VERSTEP 0x3f
156#define DME1737_REG_CONFIG 0x40
157#define DME1737_REG_CONFIG2 0x7f
158#define DME1737_REG_VID 0x43
159#define DME1737_REG_TACH_PWM 0x81
160
161/* ---------------------------------------------------------------------
162 * Misc defines
163 * --------------------------------------------------------------------- */
164
165/* Chip identification */
166#define DME1737_COMPANY_SMSC 0x5c
167#define DME1737_VERSTEP 0x88
168#define DME1737_VERSTEP_MASK 0xf8
Juerg Haefligere95c2372007-10-07 21:27:35 -0700169#define SCH311X_DEVICE 0x8c
Juerg Haefliger549edb82008-08-06 22:41:03 +0200170#define SCH5027_VERSTEP 0x69
Juerg Haefligerea694432010-05-27 19:59:01 +0200171#define SCH5127_DEVICE 0x8e
172
173/* Device ID values (global configuration register index 0x20) */
174#define DME1737_ID_1 0x77
175#define DME1737_ID_2 0x78
176#define SCH3112_ID 0x7c
177#define SCH3114_ID 0x7d
178#define SCH3116_ID 0x7f
179#define SCH5027_ID 0x89
180#define SCH5127_ID 0x86
Juerg Haefligere95c2372007-10-07 21:27:35 -0700181
182/* Length of ISA address segment */
183#define DME1737_EXTENT 2
Juerg Haefliger94319962007-06-09 10:11:16 -0400184
Juerg Haefligerea694432010-05-27 19:59:01 +0200185/* chip-dependent features */
186#define HAS_TEMP_OFFSET (1 << 0) /* bit 0 */
187#define HAS_VID (1 << 1) /* bit 1 */
188#define HAS_ZONE3 (1 << 2) /* bit 2 */
189#define HAS_ZONE_HYST (1 << 3) /* bit 3 */
190#define HAS_PWM_MIN (1 << 4) /* bit 4 */
191#define HAS_FAN(ix) (1 << ((ix) + 5)) /* bits 5-10 */
192#define HAS_PWM(ix) (1 << ((ix) + 11)) /* bits 11-16 */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100193#define HAS_IN7 (1 << 17) /* bit 17 */
Juerg Haefligerea694432010-05-27 19:59:01 +0200194
Juerg Haefliger94319962007-06-09 10:11:16 -0400195/* ---------------------------------------------------------------------
196 * Data structures and manipulation thereof
197 * --------------------------------------------------------------------- */
198
199struct dme1737_data {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200200 struct i2c_client *client; /* for I2C devices only */
Tony Jones1beeffe2007-08-20 13:46:20 -0700201 struct device *hwmon_dev;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200202 const char *name;
203 unsigned int addr; /* for ISA devices only */
Juerg Haefliger94319962007-06-09 10:11:16 -0400204
205 struct mutex update_lock;
206 int valid; /* !=0 if following fields are valid */
207 unsigned long last_update; /* in jiffies */
208 unsigned long last_vbat; /* in jiffies */
Juerg Haefligerf994fb22008-03-25 21:49:15 -0700209 enum chips type;
Juerg Haefliger549edb82008-08-06 22:41:03 +0200210 const int *in_nominal; /* pointer to IN_NOMINAL array */
Juerg Haefliger94319962007-06-09 10:11:16 -0400211
212 u8 vid;
213 u8 pwm_rr_en;
Juerg Haefligerea694432010-05-27 19:59:01 +0200214 u32 has_features;
Juerg Haefliger94319962007-06-09 10:11:16 -0400215
216 /* Register values */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100217 u16 in[8];
218 u8 in_min[8];
219 u8 in_max[8];
Juerg Haefliger94319962007-06-09 10:11:16 -0400220 s16 temp[3];
221 s8 temp_min[3];
222 s8 temp_max[3];
223 s8 temp_offset[3];
224 u8 config;
225 u8 config2;
226 u8 vrm;
227 u16 fan[6];
228 u16 fan_min[6];
229 u8 fan_max[2];
230 u8 fan_opt[6];
231 u8 pwm[6];
232 u8 pwm_min[3];
233 u8 pwm_config[3];
234 u8 pwm_acz[3];
235 u8 pwm_freq[6];
236 u8 pwm_rr[2];
Axel Lind58e47d2014-08-06 08:02:44 +0800237 s8 zone_low[3];
238 s8 zone_abs[3];
Juerg Haefliger94319962007-06-09 10:11:16 -0400239 u8 zone_hyst[2];
240 u32 alarms;
241};
242
243/* Nominal voltage values */
Juerg Haefligerf994fb22008-03-25 21:49:15 -0700244static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
245 3300};
246static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
247 3300};
Juerg Haefliger549edb82008-08-06 22:41:03 +0200248static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
249 3300};
Juerg Haefligerea694432010-05-27 19:59:01 +0200250static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100251 3300, 1500};
Juerg Haefliger549edb82008-08-06 22:41:03 +0200252#define IN_NOMINAL(type) ((type) == sch311x ? IN_NOMINAL_SCH311x : \
253 (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
Juerg Haefligerea694432010-05-27 19:59:01 +0200254 (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
Juerg Haefliger549edb82008-08-06 22:41:03 +0200255 IN_NOMINAL_DME1737)
Juerg Haefliger94319962007-06-09 10:11:16 -0400256
Guenter Roeckc8de8362012-01-14 13:25:01 -0800257/*
258 * Voltage input
Juerg Haefliger94319962007-06-09 10:11:16 -0400259 * Voltage inputs have 16 bits resolution, limit values have 8 bits
Guenter Roeckc8de8362012-01-14 13:25:01 -0800260 * resolution.
261 */
Juerg Haefliger549edb82008-08-06 22:41:03 +0200262static inline int IN_FROM_REG(int reg, int nominal, int res)
Juerg Haefliger94319962007-06-09 10:11:16 -0400263{
Juerg Haefliger549edb82008-08-06 22:41:03 +0200264 return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
Juerg Haefliger94319962007-06-09 10:11:16 -0400265}
266
Axel Lind58e47d2014-08-06 08:02:44 +0800267static inline int IN_TO_REG(long val, int nominal)
Juerg Haefliger94319962007-06-09 10:11:16 -0400268{
Guenter Roeck07cc1892016-12-27 14:15:05 -0800269 val = clamp_val(val, 0, 255 * nominal / 192);
270 return DIV_ROUND_CLOSEST(val * 192, nominal);
Juerg Haefliger94319962007-06-09 10:11:16 -0400271}
272
Guenter Roeckc8de8362012-01-14 13:25:01 -0800273/*
274 * Temperature input
Juerg Haefliger94319962007-06-09 10:11:16 -0400275 * The register values represent temperatures in 2's complement notation from
276 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
Guenter Roeckc8de8362012-01-14 13:25:01 -0800277 * values have 8 bits resolution.
278 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400279static inline int TEMP_FROM_REG(int reg, int res)
280{
281 return (reg * 1000) >> (res - 8);
282}
283
Axel Lind58e47d2014-08-06 08:02:44 +0800284static inline int TEMP_TO_REG(long val)
Juerg Haefliger94319962007-06-09 10:11:16 -0400285{
Guenter Roeck07cc1892016-12-27 14:15:05 -0800286 val = clamp_val(val, -128000, 127000);
287 return DIV_ROUND_CLOSEST(val, 1000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400288}
289
290/* Temperature range */
291static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
292 10000, 13333, 16000, 20000, 26666, 32000,
293 40000, 53333, 80000};
294
295static inline int TEMP_RANGE_FROM_REG(int reg)
296{
297 return TEMP_RANGE[(reg >> 4) & 0x0f];
298}
299
Axel Lind58e47d2014-08-06 08:02:44 +0800300static int TEMP_RANGE_TO_REG(long val, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400301{
302 int i;
303
304 for (i = 15; i > 0; i--) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800305 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2)
Juerg Haefliger94319962007-06-09 10:11:16 -0400306 break;
Juerg Haefliger94319962007-06-09 10:11:16 -0400307 }
308
309 return (reg & 0x0f) | (i << 4);
310}
311
Guenter Roeckc8de8362012-01-14 13:25:01 -0800312/*
313 * Temperature hysteresis
Juerg Haefliger94319962007-06-09 10:11:16 -0400314 * Register layout:
315 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
Guenter Roeckc8de8362012-01-14 13:25:01 -0800316 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx]
317 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400318static inline int TEMP_HYST_FROM_REG(int reg, int ix)
319{
320 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
321}
322
Guenter Roeck07cc1892016-12-27 14:15:05 -0800323static inline int TEMP_HYST_TO_REG(int temp, long hyst, int ix, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400324{
Guenter Roeck07cc1892016-12-27 14:15:05 -0800325 hyst = clamp_val(hyst, temp - 15000, temp);
326 hyst = DIV_ROUND_CLOSEST(temp - hyst, 1000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400327
328 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
329}
330
331/* Fan input RPM */
332static inline int FAN_FROM_REG(int reg, int tpc)
333{
Guenter Roeckc8de8362012-01-14 13:25:01 -0800334 if (tpc)
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800335 return tpc * reg;
Guenter Roeckc8de8362012-01-14 13:25:01 -0800336 else
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800337 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
Juerg Haefliger94319962007-06-09 10:11:16 -0400338}
339
Axel Lind58e47d2014-08-06 08:02:44 +0800340static inline int FAN_TO_REG(long val, int tpc)
Juerg Haefliger94319962007-06-09 10:11:16 -0400341{
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800342 if (tpc) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800343 return clamp_val(val / tpc, 0, 0xffff);
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800344 } else {
345 return (val <= 0) ? 0xffff :
Guenter Roeck2a844c12013-01-09 08:09:34 -0800346 clamp_val(90000 * 60 / val, 0, 0xfffe);
Juerg Haefligerff8421f2008-01-27 16:39:46 -0800347 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400348}
349
Guenter Roeckc8de8362012-01-14 13:25:01 -0800350/*
351 * Fan TPC (tach pulse count)
Juerg Haefliger94319962007-06-09 10:11:16 -0400352 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
Guenter Roeckc8de8362012-01-14 13:25:01 -0800353 * is configured in legacy (non-tpc) mode
354 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400355static inline int FAN_TPC_FROM_REG(int reg)
356{
357 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
358}
359
Guenter Roeckc8de8362012-01-14 13:25:01 -0800360/*
361 * Fan type
Juerg Haefliger94319962007-06-09 10:11:16 -0400362 * The type of a fan is expressed in number of pulses-per-revolution that it
Guenter Roeckc8de8362012-01-14 13:25:01 -0800363 * emits
364 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400365static inline int FAN_TYPE_FROM_REG(int reg)
366{
367 int edge = (reg >> 1) & 0x03;
368
369 return (edge > 0) ? 1 << (edge - 1) : 0;
370}
371
Axel Lind58e47d2014-08-06 08:02:44 +0800372static inline int FAN_TYPE_TO_REG(long val, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400373{
374 int edge = (val == 4) ? 3 : val;
375
376 return (reg & 0xf9) | (edge << 1);
377}
378
379/* Fan max RPM */
380static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
381 0x11, 0x0f, 0x0e};
382
383static int FAN_MAX_FROM_REG(int reg)
384{
385 int i;
386
387 for (i = 10; i > 0; i--) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800388 if (reg == FAN_MAX[i])
Juerg Haefliger94319962007-06-09 10:11:16 -0400389 break;
Juerg Haefliger94319962007-06-09 10:11:16 -0400390 }
391
392 return 1000 + i * 500;
393}
394
Axel Lind58e47d2014-08-06 08:02:44 +0800395static int FAN_MAX_TO_REG(long val)
Juerg Haefliger94319962007-06-09 10:11:16 -0400396{
397 int i;
398
399 for (i = 10; i > 0; i--) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800400 if (val > (1000 + (i - 1) * 500))
Juerg Haefliger94319962007-06-09 10:11:16 -0400401 break;
Juerg Haefliger94319962007-06-09 10:11:16 -0400402 }
403
404 return FAN_MAX[i];
405}
406
Guenter Roeckc8de8362012-01-14 13:25:01 -0800407/*
408 * PWM enable
Juerg Haefliger94319962007-06-09 10:11:16 -0400409 * Register to enable mapping:
410 * 000: 2 fan on zone 1 auto
411 * 001: 2 fan on zone 2 auto
412 * 010: 2 fan on zone 3 auto
413 * 011: 0 fan full on
414 * 100: -1 fan disabled
415 * 101: 2 fan on hottest of zones 2,3 auto
416 * 110: 2 fan on hottest of zones 1,2,3 auto
Guenter Roeckc8de8362012-01-14 13:25:01 -0800417 * 111: 1 fan in manual mode
418 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400419static inline int PWM_EN_FROM_REG(int reg)
420{
421 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
422
423 return en[(reg >> 5) & 0x07];
424}
425
426static inline int PWM_EN_TO_REG(int val, int reg)
427{
428 int en = (val == 1) ? 7 : 3;
429
430 return (reg & 0x1f) | ((en & 0x07) << 5);
431}
432
Guenter Roeckc8de8362012-01-14 13:25:01 -0800433/*
434 * PWM auto channels zone
Juerg Haefliger94319962007-06-09 10:11:16 -0400435 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
436 * corresponding to zone x+1):
437 * 000: 001 fan on zone 1 auto
438 * 001: 010 fan on zone 2 auto
439 * 010: 100 fan on zone 3 auto
440 * 011: 000 fan full on
441 * 100: 000 fan disabled
442 * 101: 110 fan on hottest of zones 2,3 auto
443 * 110: 111 fan on hottest of zones 1,2,3 auto
Guenter Roeckc8de8362012-01-14 13:25:01 -0800444 * 111: 000 fan in manual mode
445 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400446static inline int PWM_ACZ_FROM_REG(int reg)
447{
448 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
449
450 return acz[(reg >> 5) & 0x07];
451}
452
Axel Lind58e47d2014-08-06 08:02:44 +0800453static inline int PWM_ACZ_TO_REG(long val, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400454{
455 int acz = (val == 4) ? 2 : val - 1;
456
457 return (reg & 0x1f) | ((acz & 0x07) << 5);
458}
459
460/* PWM frequency */
461static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
462 15000, 20000, 30000, 25000, 0, 0, 0, 0};
463
464static inline int PWM_FREQ_FROM_REG(int reg)
465{
466 return PWM_FREQ[reg & 0x0f];
467}
468
Axel Lind58e47d2014-08-06 08:02:44 +0800469static int PWM_FREQ_TO_REG(long val, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400470{
471 int i;
472
473 /* the first two cases are special - stupid chip design! */
474 if (val > 27500) {
475 i = 10;
476 } else if (val > 22500) {
477 i = 11;
478 } else {
479 for (i = 9; i > 0; i--) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800480 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2)
Juerg Haefliger94319962007-06-09 10:11:16 -0400481 break;
Juerg Haefliger94319962007-06-09 10:11:16 -0400482 }
483 }
484
485 return (reg & 0xf0) | i;
486}
487
Guenter Roeckc8de8362012-01-14 13:25:01 -0800488/*
489 * PWM ramp rate
Juerg Haefliger94319962007-06-09 10:11:16 -0400490 * Register layout:
491 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
Guenter Roeckc8de8362012-01-14 13:25:01 -0800492 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0]
493 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400494static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
495
496static inline int PWM_RR_FROM_REG(int reg, int ix)
497{
498 int rr = (ix == 1) ? reg >> 4 : reg;
499
500 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
501}
502
Axel Lind58e47d2014-08-06 08:02:44 +0800503static int PWM_RR_TO_REG(long val, int ix, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400504{
505 int i;
506
507 for (i = 0; i < 7; i++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800508 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2)
Juerg Haefliger94319962007-06-09 10:11:16 -0400509 break;
Juerg Haefliger94319962007-06-09 10:11:16 -0400510 }
511
512 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
513}
514
515/* PWM ramp rate enable */
516static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
517{
518 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
519}
520
Axel Lind58e47d2014-08-06 08:02:44 +0800521static inline int PWM_RR_EN_TO_REG(long val, int ix, int reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400522{
523 int en = (ix == 1) ? 0x80 : 0x08;
524
525 return val ? reg | en : reg & ~en;
526}
527
Guenter Roeckc8de8362012-01-14 13:25:01 -0800528/*
529 * PWM min/off
Juerg Haefliger94319962007-06-09 10:11:16 -0400530 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
Guenter Roeckc8de8362012-01-14 13:25:01 -0800531 * the register layout).
532 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400533static inline int PWM_OFF_FROM_REG(int reg, int ix)
534{
535 return (reg >> (ix + 5)) & 0x01;
536}
537
538static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
539{
540 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
541}
542
543/* ---------------------------------------------------------------------
544 * Device I/O access
Juerg Haefligere95c2372007-10-07 21:27:35 -0700545 *
546 * ISA access is performed through an index/data register pair and needs to
547 * be protected by a mutex during runtime (not required for initialization).
548 * We use data->update_lock for this and need to ensure that we acquire it
549 * before calling dme1737_read or dme1737_write.
Juerg Haefliger94319962007-06-09 10:11:16 -0400550 * --------------------------------------------------------------------- */
551
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200552static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
Juerg Haefliger94319962007-06-09 10:11:16 -0400553{
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200554 struct i2c_client *client = data->client;
Juerg Haefligere95c2372007-10-07 21:27:35 -0700555 s32 val;
Juerg Haefliger94319962007-06-09 10:11:16 -0400556
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200557 if (client) { /* I2C device */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700558 val = i2c_smbus_read_byte_data(client, reg);
559
560 if (val < 0) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800561 dev_warn(&client->dev,
562 "Read from register 0x%02x failed! %s\n",
563 reg, DO_REPORT);
Juerg Haefligere95c2372007-10-07 21:27:35 -0700564 }
565 } else { /* ISA device */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200566 outb(reg, data->addr);
567 val = inb(data->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400568 }
569
570 return val;
571}
572
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200573static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
Juerg Haefliger94319962007-06-09 10:11:16 -0400574{
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200575 struct i2c_client *client = data->client;
Juerg Haefligere95c2372007-10-07 21:27:35 -0700576 s32 res = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -0400577
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200578 if (client) { /* I2C device */
Juerg Haefligere95c2372007-10-07 21:27:35 -0700579 res = i2c_smbus_write_byte_data(client, reg, val);
580
581 if (res < 0) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800582 dev_warn(&client->dev,
583 "Write to register 0x%02x failed! %s\n",
584 reg, DO_REPORT);
Juerg Haefligere95c2372007-10-07 21:27:35 -0700585 }
586 } else { /* ISA device */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200587 outb(reg, data->addr);
588 outb(val, data->addr + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -0400589 }
590
591 return res;
592}
593
594static struct dme1737_data *dme1737_update_device(struct device *dev)
595{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700596 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400597 int ix;
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100598 u8 lsb[6];
Juerg Haefliger94319962007-06-09 10:11:16 -0400599
600 mutex_lock(&data->update_lock);
601
602 /* Enable a Vbat monitoring cycle every 10 mins */
603 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200604 dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400605 DME1737_REG_CONFIG) | 0x10);
606 data->last_vbat = jiffies;
607 }
608
609 /* Sample register contents every 1 sec */
610 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
Juerg Haefligerea694432010-05-27 19:59:01 +0200611 if (data->has_features & HAS_VID) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200612 data->vid = dme1737_read(data, DME1737_REG_VID) &
Juerg Haefliger549edb82008-08-06 22:41:03 +0200613 0x3f;
614 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400615
616 /* In (voltage) registers */
617 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800618 /*
619 * Voltage inputs are stored as 16 bit values even
Juerg Haefliger94319962007-06-09 10:11:16 -0400620 * though they have only 12 bits resolution. This is
Guenter Roeckc8de8362012-01-14 13:25:01 -0800621 * to make it consistent with the temp inputs.
622 */
623 if (ix == 7 && !(data->has_features & HAS_IN7))
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100624 continue;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200625 data->in[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400626 DME1737_REG_IN(ix)) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200627 data->in_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400628 DME1737_REG_IN_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200629 data->in_max[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400630 DME1737_REG_IN_MAX(ix));
631 }
632
633 /* Temp registers */
634 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800635 /*
636 * Temp inputs are stored as 16 bit values even
Juerg Haefliger94319962007-06-09 10:11:16 -0400637 * though they have only 12 bits resolution. This is
638 * to take advantage of implicit conversions between
639 * register values (2's complement) and temp values
Guenter Roeckc8de8362012-01-14 13:25:01 -0800640 * (signed decimal).
641 */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200642 data->temp[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400643 DME1737_REG_TEMP(ix)) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200644 data->temp_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400645 DME1737_REG_TEMP_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200646 data->temp_max[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400647 DME1737_REG_TEMP_MAX(ix));
Juerg Haefligerea694432010-05-27 19:59:01 +0200648 if (data->has_features & HAS_TEMP_OFFSET) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200649 data->temp_offset[ix] = dme1737_read(data,
Juerg Haefliger549edb82008-08-06 22:41:03 +0200650 DME1737_REG_TEMP_OFFSET(ix));
651 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400652 }
653
Guenter Roeckc8de8362012-01-14 13:25:01 -0800654 /*
655 * In and temp LSB registers
Juerg Haefliger94319962007-06-09 10:11:16 -0400656 * The LSBs are latched when the MSBs are read, so the order in
657 * which the registers are read (MSB first, then LSB) is
Guenter Roeckc8de8362012-01-14 13:25:01 -0800658 * important!
659 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400660 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800661 if (ix == 5 && !(data->has_features & HAS_IN7))
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100662 continue;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200663 lsb[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400664 DME1737_REG_IN_TEMP_LSB(ix));
665 }
666 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800667 if (ix == 7 && !(data->has_features & HAS_IN7))
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100668 continue;
Juerg Haefliger94319962007-06-09 10:11:16 -0400669 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
670 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
671 }
672 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
673 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
674 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
675 }
676
677 /* Fan registers */
678 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800679 /*
680 * Skip reading registers if optional fans are not
681 * present
682 */
683 if (!(data->has_features & HAS_FAN(ix)))
Juerg Haefliger94319962007-06-09 10:11:16 -0400684 continue;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200685 data->fan[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400686 DME1737_REG_FAN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200687 data->fan[ix] |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400688 DME1737_REG_FAN(ix) + 1) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200689 data->fan_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400690 DME1737_REG_FAN_MIN(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200691 data->fan_min[ix] |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400692 DME1737_REG_FAN_MIN(ix) + 1) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200693 data->fan_opt[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400694 DME1737_REG_FAN_OPT(ix));
695 /* fan_max exists only for fan[5-6] */
696 if (ix > 3) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200697 data->fan_max[ix - 4] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400698 DME1737_REG_FAN_MAX(ix));
699 }
700 }
701
702 /* PWM registers */
703 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800704 /*
705 * Skip reading registers if optional PWMs are not
706 * present
707 */
708 if (!(data->has_features & HAS_PWM(ix)))
Juerg Haefliger94319962007-06-09 10:11:16 -0400709 continue;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200710 data->pwm[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400711 DME1737_REG_PWM(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200712 data->pwm_freq[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400713 DME1737_REG_PWM_FREQ(ix));
714 /* pwm_config and pwm_min exist only for pwm[1-3] */
715 if (ix < 3) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200716 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400717 DME1737_REG_PWM_CONFIG(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200718 data->pwm_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400719 DME1737_REG_PWM_MIN(ix));
720 }
721 }
722 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200723 data->pwm_rr[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400724 DME1737_REG_PWM_RR(ix));
725 }
726
727 /* Thermal zone registers */
728 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +0200729 /* Skip reading registers if zone3 is not present */
Guenter Roeckc8de8362012-01-14 13:25:01 -0800730 if ((ix == 2) && !(data->has_features & HAS_ZONE3))
Juerg Haefligerea694432010-05-27 19:59:01 +0200731 continue;
Juerg Haefligerea694432010-05-27 19:59:01 +0200732 /* sch5127 zone2 registers are special */
733 if ((ix == 1) && (data->type == sch5127)) {
734 data->zone_low[1] = dme1737_read(data,
735 DME1737_REG_ZONE_LOW(2));
736 data->zone_abs[1] = dme1737_read(data,
737 DME1737_REG_ZONE_ABS(2));
738 } else {
739 data->zone_low[ix] = dme1737_read(data,
740 DME1737_REG_ZONE_LOW(ix));
741 data->zone_abs[ix] = dme1737_read(data,
742 DME1737_REG_ZONE_ABS(ix));
743 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400744 }
Juerg Haefligerea694432010-05-27 19:59:01 +0200745 if (data->has_features & HAS_ZONE_HYST) {
Juerg Haefliger549edb82008-08-06 22:41:03 +0200746 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200747 data->zone_hyst[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400748 DME1737_REG_ZONE_HYST(ix));
Juerg Haefliger549edb82008-08-06 22:41:03 +0200749 }
Juerg Haefliger94319962007-06-09 10:11:16 -0400750 }
751
752 /* Alarm registers */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200753 data->alarms = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400754 DME1737_REG_ALARM1);
Guenter Roeckc8de8362012-01-14 13:25:01 -0800755 /*
756 * Bit 7 tells us if the other alarm registers are non-zero and
757 * therefore also need to be read
758 */
Juerg Haefliger94319962007-06-09 10:11:16 -0400759 if (data->alarms & 0x80) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200760 data->alarms |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400761 DME1737_REG_ALARM2) << 8;
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200762 data->alarms |= dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -0400763 DME1737_REG_ALARM3) << 16;
764 }
765
Guenter Roeckc8de8362012-01-14 13:25:01 -0800766 /*
767 * The ISA chips require explicit clearing of alarm bits.
Juerg Haefligere95c2372007-10-07 21:27:35 -0700768 * Don't worry, an alarm will come back if the condition
Guenter Roeckc8de8362012-01-14 13:25:01 -0800769 * that causes it still exists
770 */
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200771 if (!data->client) {
Guenter Roeckc8de8362012-01-14 13:25:01 -0800772 if (data->alarms & 0xff0000)
773 dme1737_write(data, DME1737_REG_ALARM3, 0xff);
774 if (data->alarms & 0xff00)
775 dme1737_write(data, DME1737_REG_ALARM2, 0xff);
776 if (data->alarms & 0xff)
777 dme1737_write(data, DME1737_REG_ALARM1, 0xff);
Juerg Haefligere95c2372007-10-07 21:27:35 -0700778 }
779
Juerg Haefliger94319962007-06-09 10:11:16 -0400780 data->last_update = jiffies;
781 data->valid = 1;
782 }
783
784 mutex_unlock(&data->update_lock);
785
786 return data;
787}
788
789/* ---------------------------------------------------------------------
790 * Voltage sysfs attributes
Juerg Haefligerd4b94e12011-01-12 21:55:13 +0100791 * ix = [0-7]
Juerg Haefliger94319962007-06-09 10:11:16 -0400792 * --------------------------------------------------------------------- */
793
794#define SYS_IN_INPUT 0
795#define SYS_IN_MIN 1
796#define SYS_IN_MAX 2
797#define SYS_IN_ALARM 3
798
799static ssize_t show_in(struct device *dev, struct device_attribute *attr,
800 char *buf)
801{
802 struct dme1737_data *data = dme1737_update_device(dev);
803 struct sensor_device_attribute_2
804 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
805 int ix = sensor_attr_2->index;
806 int fn = sensor_attr_2->nr;
807 int res;
808
809 switch (fn) {
810 case SYS_IN_INPUT:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200811 res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
Juerg Haefliger94319962007-06-09 10:11:16 -0400812 break;
813 case SYS_IN_MIN:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200814 res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
Juerg Haefliger94319962007-06-09 10:11:16 -0400815 break;
816 case SYS_IN_MAX:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200817 res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
Juerg Haefliger94319962007-06-09 10:11:16 -0400818 break;
819 case SYS_IN_ALARM:
820 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
821 break;
822 default:
823 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700824 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400825 }
826
827 return sprintf(buf, "%d\n", res);
828}
829
830static ssize_t set_in(struct device *dev, struct device_attribute *attr,
831 const char *buf, size_t count)
832{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700833 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400834 struct sensor_device_attribute_2
835 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
836 int ix = sensor_attr_2->index;
837 int fn = sensor_attr_2->nr;
Guenter Roeckc8de8362012-01-14 13:25:01 -0800838 long val;
839 int err;
840
841 err = kstrtol(buf, 10, &val);
842 if (err)
843 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -0400844
845 mutex_lock(&data->update_lock);
846 switch (fn) {
847 case SYS_IN_MIN:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200848 data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200849 dme1737_write(data, DME1737_REG_IN_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400850 data->in_min[ix]);
851 break;
852 case SYS_IN_MAX:
Juerg Haefliger549edb82008-08-06 22:41:03 +0200853 data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200854 dme1737_write(data, DME1737_REG_IN_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400855 data->in_max[ix]);
856 break;
857 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700858 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400859 }
860 mutex_unlock(&data->update_lock);
861
862 return count;
863}
864
865/* ---------------------------------------------------------------------
866 * Temperature sysfs attributes
867 * ix = [0-2]
868 * --------------------------------------------------------------------- */
869
870#define SYS_TEMP_INPUT 0
871#define SYS_TEMP_MIN 1
872#define SYS_TEMP_MAX 2
873#define SYS_TEMP_OFFSET 3
874#define SYS_TEMP_ALARM 4
875#define SYS_TEMP_FAULT 5
876
877static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
878 char *buf)
879{
880 struct dme1737_data *data = dme1737_update_device(dev);
881 struct sensor_device_attribute_2
882 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
883 int ix = sensor_attr_2->index;
884 int fn = sensor_attr_2->nr;
885 int res;
886
887 switch (fn) {
888 case SYS_TEMP_INPUT:
889 res = TEMP_FROM_REG(data->temp[ix], 16);
890 break;
891 case SYS_TEMP_MIN:
892 res = TEMP_FROM_REG(data->temp_min[ix], 8);
893 break;
894 case SYS_TEMP_MAX:
895 res = TEMP_FROM_REG(data->temp_max[ix], 8);
896 break;
897 case SYS_TEMP_OFFSET:
898 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
899 break;
900 case SYS_TEMP_ALARM:
901 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
902 break;
903 case SYS_TEMP_FAULT:
Juerg Haefligerc0f31402007-07-20 14:16:47 -0700904 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
Juerg Haefliger94319962007-06-09 10:11:16 -0400905 break;
906 default:
907 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700908 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400909 }
910
911 return sprintf(buf, "%d\n", res);
912}
913
914static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
915 const char *buf, size_t count)
916{
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700917 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -0400918 struct sensor_device_attribute_2
919 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
920 int ix = sensor_attr_2->index;
921 int fn = sensor_attr_2->nr;
Guenter Roeckc8de8362012-01-14 13:25:01 -0800922 long val;
923 int err;
924
925 err = kstrtol(buf, 10, &val);
926 if (err)
927 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -0400928
929 mutex_lock(&data->update_lock);
930 switch (fn) {
931 case SYS_TEMP_MIN:
932 data->temp_min[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200933 dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400934 data->temp_min[ix]);
935 break;
936 case SYS_TEMP_MAX:
937 data->temp_max[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200938 dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400939 data->temp_max[ix]);
940 break;
941 case SYS_TEMP_OFFSET:
942 data->temp_offset[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +0200943 dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -0400944 data->temp_offset[ix]);
945 break;
946 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -0700947 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -0400948 }
949 mutex_unlock(&data->update_lock);
950
951 return count;
952}
953
954/* ---------------------------------------------------------------------
955 * Zone sysfs attributes
956 * ix = [0-2]
957 * --------------------------------------------------------------------- */
958
959#define SYS_ZONE_AUTO_CHANNELS_TEMP 0
960#define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
961#define SYS_ZONE_AUTO_POINT1_TEMP 2
962#define SYS_ZONE_AUTO_POINT2_TEMP 3
963#define SYS_ZONE_AUTO_POINT3_TEMP 4
964
965static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
966 char *buf)
967{
968 struct dme1737_data *data = dme1737_update_device(dev);
969 struct sensor_device_attribute_2
970 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
971 int ix = sensor_attr_2->index;
972 int fn = sensor_attr_2->nr;
973 int res;
974
975 switch (fn) {
976 case SYS_ZONE_AUTO_CHANNELS_TEMP:
977 /* check config2 for non-standard temp-to-zone mapping */
Guenter Roeckc8de8362012-01-14 13:25:01 -0800978 if ((ix == 1) && (data->config2 & 0x02))
Juerg Haefliger94319962007-06-09 10:11:16 -0400979 res = 4;
Guenter Roeckc8de8362012-01-14 13:25:01 -0800980 else
Juerg Haefliger94319962007-06-09 10:11:16 -0400981 res = 1 << ix;
Juerg Haefliger94319962007-06-09 10:11:16 -0400982 break;
983 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
984 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
985 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
986 break;
987 case SYS_ZONE_AUTO_POINT1_TEMP:
988 res = TEMP_FROM_REG(data->zone_low[ix], 8);
989 break;
990 case SYS_ZONE_AUTO_POINT2_TEMP:
991 /* pwm_freq holds the temp range bits in the upper nibble */
992 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
993 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
994 break;
995 case SYS_ZONE_AUTO_POINT3_TEMP:
996 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
997 break;
998 default:
999 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001000 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001001 }
1002
1003 return sprintf(buf, "%d\n", res);
1004}
1005
1006static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
1007 const char *buf, size_t count)
1008{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001009 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001010 struct sensor_device_attribute_2
1011 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1012 int ix = sensor_attr_2->index;
1013 int fn = sensor_attr_2->nr;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001014 long val;
Guenter Roeck07cc1892016-12-27 14:15:05 -08001015 int temp;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001016 int err;
Guenter Roeck07cc1892016-12-27 14:15:05 -08001017 u8 reg;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001018
1019 err = kstrtol(buf, 10, &val);
1020 if (err)
1021 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04001022
1023 mutex_lock(&data->update_lock);
1024 switch (fn) {
1025 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
1026 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001027 data->zone_low[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001028 DME1737_REG_ZONE_LOW(ix));
1029 /* Modify the temp hyst value */
Guenter Roeck07cc1892016-12-27 14:15:05 -08001030 temp = TEMP_FROM_REG(data->zone_low[ix], 8);
1031 reg = dme1737_read(data, DME1737_REG_ZONE_HYST(ix == 2));
1032 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(temp, val, ix, reg);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001033 dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
Juerg Haefliger94319962007-06-09 10:11:16 -04001034 data->zone_hyst[ix == 2]);
1035 break;
1036 case SYS_ZONE_AUTO_POINT1_TEMP:
1037 data->zone_low[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001038 dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001039 data->zone_low[ix]);
1040 break;
1041 case SYS_ZONE_AUTO_POINT2_TEMP:
1042 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001043 data->zone_low[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001044 DME1737_REG_ZONE_LOW(ix));
Guenter Roeckc8de8362012-01-14 13:25:01 -08001045 /*
1046 * Modify the temp range value (which is stored in the upper
1047 * nibble of the pwm_freq register)
1048 */
Guenter Roeck07cc1892016-12-27 14:15:05 -08001049 temp = TEMP_FROM_REG(data->zone_low[ix], 8);
1050 val = clamp_val(val, temp, temp + 80000);
1051 reg = dme1737_read(data, DME1737_REG_PWM_FREQ(ix));
1052 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val - temp, reg);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001053 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001054 data->pwm_freq[ix]);
1055 break;
1056 case SYS_ZONE_AUTO_POINT3_TEMP:
1057 data->zone_abs[ix] = TEMP_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001058 dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001059 data->zone_abs[ix]);
1060 break;
1061 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001062 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001063 }
1064 mutex_unlock(&data->update_lock);
1065
1066 return count;
1067}
1068
1069/* ---------------------------------------------------------------------
1070 * Fan sysfs attributes
1071 * ix = [0-5]
1072 * --------------------------------------------------------------------- */
1073
1074#define SYS_FAN_INPUT 0
1075#define SYS_FAN_MIN 1
1076#define SYS_FAN_MAX 2
1077#define SYS_FAN_ALARM 3
1078#define SYS_FAN_TYPE 4
1079
1080static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1081 char *buf)
1082{
1083 struct dme1737_data *data = dme1737_update_device(dev);
1084 struct sensor_device_attribute_2
1085 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1086 int ix = sensor_attr_2->index;
1087 int fn = sensor_attr_2->nr;
1088 int res;
1089
1090 switch (fn) {
1091 case SYS_FAN_INPUT:
1092 res = FAN_FROM_REG(data->fan[ix],
1093 ix < 4 ? 0 :
1094 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1095 break;
1096 case SYS_FAN_MIN:
1097 res = FAN_FROM_REG(data->fan_min[ix],
1098 ix < 4 ? 0 :
1099 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1100 break;
1101 case SYS_FAN_MAX:
1102 /* only valid for fan[5-6] */
1103 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1104 break;
1105 case SYS_FAN_ALARM:
1106 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1107 break;
1108 case SYS_FAN_TYPE:
1109 /* only valid for fan[1-4] */
1110 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1111 break;
1112 default:
1113 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001114 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001115 }
1116
1117 return sprintf(buf, "%d\n", res);
1118}
1119
1120static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1121 const char *buf, size_t count)
1122{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001123 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001124 struct sensor_device_attribute_2
1125 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1126 int ix = sensor_attr_2->index;
1127 int fn = sensor_attr_2->nr;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001128 long val;
1129 int err;
1130
1131 err = kstrtol(buf, 10, &val);
1132 if (err)
1133 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04001134
1135 mutex_lock(&data->update_lock);
1136 switch (fn) {
1137 case SYS_FAN_MIN:
1138 if (ix < 4) {
1139 data->fan_min[ix] = FAN_TO_REG(val, 0);
1140 } else {
1141 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001142 data->fan_opt[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001143 DME1737_REG_FAN_OPT(ix));
1144 /* Modify the fan min value */
1145 data->fan_min[ix] = FAN_TO_REG(val,
1146 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1147 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001148 dme1737_write(data, DME1737_REG_FAN_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001149 data->fan_min[ix] & 0xff);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001150 dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
Juerg Haefliger94319962007-06-09 10:11:16 -04001151 data->fan_min[ix] >> 8);
1152 break;
1153 case SYS_FAN_MAX:
1154 /* Only valid for fan[5-6] */
1155 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001156 dme1737_write(data, DME1737_REG_FAN_MAX(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001157 data->fan_max[ix - 4]);
1158 break;
1159 case SYS_FAN_TYPE:
1160 /* Only valid for fan[1-4] */
1161 if (!(val == 1 || val == 2 || val == 4)) {
1162 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001163 dev_warn(dev,
1164 "Fan type value %ld not supported. Choose one of 1, 2, or 4.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04001165 val);
1166 goto exit;
1167 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001168 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001169 DME1737_REG_FAN_OPT(ix)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001170 dme1737_write(data, DME1737_REG_FAN_OPT(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001171 data->fan_opt[ix]);
1172 break;
1173 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001174 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001175 }
1176exit:
1177 mutex_unlock(&data->update_lock);
1178
1179 return count;
1180}
1181
1182/* ---------------------------------------------------------------------
1183 * PWM sysfs attributes
1184 * ix = [0-4]
1185 * --------------------------------------------------------------------- */
1186
1187#define SYS_PWM 0
1188#define SYS_PWM_FREQ 1
1189#define SYS_PWM_ENABLE 2
1190#define SYS_PWM_RAMP_RATE 3
1191#define SYS_PWM_AUTO_CHANNELS_ZONE 4
1192#define SYS_PWM_AUTO_PWM_MIN 5
1193#define SYS_PWM_AUTO_POINT1_PWM 6
1194#define SYS_PWM_AUTO_POINT2_PWM 7
1195
1196static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1197 char *buf)
1198{
1199 struct dme1737_data *data = dme1737_update_device(dev);
1200 struct sensor_device_attribute_2
1201 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1202 int ix = sensor_attr_2->index;
1203 int fn = sensor_attr_2->nr;
1204 int res;
1205
1206 switch (fn) {
1207 case SYS_PWM:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001208 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0)
Juerg Haefliger94319962007-06-09 10:11:16 -04001209 res = 255;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001210 else
Juerg Haefliger94319962007-06-09 10:11:16 -04001211 res = data->pwm[ix];
Juerg Haefliger94319962007-06-09 10:11:16 -04001212 break;
1213 case SYS_PWM_FREQ:
1214 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1215 break;
1216 case SYS_PWM_ENABLE:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001217 if (ix >= 3)
Juerg Haefliger94319962007-06-09 10:11:16 -04001218 res = 1; /* pwm[5-6] hard-wired to manual mode */
Guenter Roeckc8de8362012-01-14 13:25:01 -08001219 else
Juerg Haefliger94319962007-06-09 10:11:16 -04001220 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
Juerg Haefliger94319962007-06-09 10:11:16 -04001221 break;
1222 case SYS_PWM_RAMP_RATE:
1223 /* Only valid for pwm[1-3] */
1224 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1225 break;
1226 case SYS_PWM_AUTO_CHANNELS_ZONE:
1227 /* Only valid for pwm[1-3] */
Guenter Roeckc8de8362012-01-14 13:25:01 -08001228 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2)
Juerg Haefliger94319962007-06-09 10:11:16 -04001229 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
Guenter Roeckc8de8362012-01-14 13:25:01 -08001230 else
Juerg Haefliger94319962007-06-09 10:11:16 -04001231 res = data->pwm_acz[ix];
Juerg Haefliger94319962007-06-09 10:11:16 -04001232 break;
1233 case SYS_PWM_AUTO_PWM_MIN:
1234 /* Only valid for pwm[1-3] */
Guenter Roeckc8de8362012-01-14 13:25:01 -08001235 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix))
Juerg Haefliger94319962007-06-09 10:11:16 -04001236 res = data->pwm_min[ix];
Guenter Roeckc8de8362012-01-14 13:25:01 -08001237 else
Juerg Haefliger94319962007-06-09 10:11:16 -04001238 res = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -04001239 break;
1240 case SYS_PWM_AUTO_POINT1_PWM:
1241 /* Only valid for pwm[1-3] */
1242 res = data->pwm_min[ix];
1243 break;
1244 case SYS_PWM_AUTO_POINT2_PWM:
1245 /* Only valid for pwm[1-3] */
1246 res = 255; /* hard-wired */
1247 break;
1248 default:
1249 res = 0;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001250 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001251 }
1252
1253 return sprintf(buf, "%d\n", res);
1254}
1255
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001256static struct attribute *dme1737_pwm_chmod_attr[];
Al Viro48176a92011-07-24 03:40:40 -04001257static void dme1737_chmod_file(struct device*, struct attribute*, umode_t);
Juerg Haefliger94319962007-06-09 10:11:16 -04001258
1259static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1260 const char *buf, size_t count)
1261{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001262 struct dme1737_data *data = dev_get_drvdata(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04001263 struct sensor_device_attribute_2
1264 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1265 int ix = sensor_attr_2->index;
1266 int fn = sensor_attr_2->nr;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001267 long val;
1268 int err;
1269
1270 err = kstrtol(buf, 10, &val);
1271 if (err)
1272 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04001273
1274 mutex_lock(&data->update_lock);
1275 switch (fn) {
1276 case SYS_PWM:
Guenter Roeck2a844c12013-01-09 08:09:34 -08001277 data->pwm[ix] = clamp_val(val, 0, 255);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001278 dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
Juerg Haefliger94319962007-06-09 10:11:16 -04001279 break;
1280 case SYS_PWM_FREQ:
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001281 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001282 DME1737_REG_PWM_FREQ(ix)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001283 dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001284 data->pwm_freq[ix]);
1285 break;
1286 case SYS_PWM_ENABLE:
1287 /* Only valid for pwm[1-3] */
1288 if (val < 0 || val > 2) {
1289 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001290 dev_warn(dev,
1291 "PWM enable %ld not supported. Choose one of 0, 1, or 2.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04001292 val);
1293 goto exit;
1294 }
1295 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001296 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001297 DME1737_REG_PWM_CONFIG(ix));
1298 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1299 /* Bail out if no change */
1300 goto exit;
1301 }
1302 /* Do some housekeeping if we are currently in auto mode */
1303 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1304 /* Save the current zone channel assignment */
1305 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1306 data->pwm_config[ix]);
1307 /* Save the current ramp rate state and disable it */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001308 data->pwm_rr[ix > 0] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001309 DME1737_REG_PWM_RR(ix > 0));
1310 data->pwm_rr_en &= ~(1 << ix);
1311 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1312 data->pwm_rr_en |= (1 << ix);
1313 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1314 data->pwm_rr[ix > 0]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001315 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001316 DME1737_REG_PWM_RR(ix > 0),
1317 data->pwm_rr[ix > 0]);
1318 }
1319 }
1320 /* Set the new PWM mode */
1321 switch (val) {
1322 case 0:
1323 /* Change permissions of pwm[ix] to read-only */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001324 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001325 S_IRUGO);
1326 /* Turn fan fully on */
1327 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1328 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001329 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001330 data->pwm_config[ix]);
1331 break;
1332 case 1:
1333 /* Turn on manual mode */
1334 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1335 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001336 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001337 data->pwm_config[ix]);
1338 /* Change permissions of pwm[ix] to read-writeable */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001339 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001340 S_IRUGO | S_IWUSR);
1341 break;
1342 case 2:
1343 /* Change permissions of pwm[ix] to read-only */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001344 dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
Juerg Haefliger94319962007-06-09 10:11:16 -04001345 S_IRUGO);
Guenter Roeckc8de8362012-01-14 13:25:01 -08001346 /*
1347 * Turn on auto mode using the saved zone channel
1348 * assignment
1349 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001350 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1351 data->pwm_acz[ix],
1352 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001353 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001354 data->pwm_config[ix]);
1355 /* Enable PWM ramp rate if previously enabled */
1356 if (data->pwm_rr_en & (1 << ix)) {
1357 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001358 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001359 DME1737_REG_PWM_RR(ix > 0)));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001360 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001361 DME1737_REG_PWM_RR(ix > 0),
1362 data->pwm_rr[ix > 0]);
1363 }
1364 break;
1365 }
1366 break;
1367 case SYS_PWM_RAMP_RATE:
1368 /* Only valid for pwm[1-3] */
1369 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001370 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001371 DME1737_REG_PWM_CONFIG(ix));
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001372 data->pwm_rr[ix > 0] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001373 DME1737_REG_PWM_RR(ix > 0));
1374 /* Set the ramp rate value */
1375 if (val > 0) {
1376 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1377 data->pwm_rr[ix > 0]);
1378 }
Guenter Roeckc8de8362012-01-14 13:25:01 -08001379 /*
1380 * Enable/disable the feature only if the associated PWM
1381 * output is in automatic mode.
1382 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001383 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1384 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1385 data->pwm_rr[ix > 0]);
1386 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001387 dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
Juerg Haefliger94319962007-06-09 10:11:16 -04001388 data->pwm_rr[ix > 0]);
1389 break;
1390 case SYS_PWM_AUTO_CHANNELS_ZONE:
1391 /* Only valid for pwm[1-3] */
1392 if (!(val == 1 || val == 2 || val == 4 ||
1393 val == 6 || val == 7)) {
1394 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001395 dev_warn(dev,
1396 "PWM auto channels zone %ld not supported. Choose one of 1, 2, 4, 6, "
Juerg Haefliger94319962007-06-09 10:11:16 -04001397 "or 7.\n", val);
1398 goto exit;
1399 }
1400 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001401 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001402 DME1737_REG_PWM_CONFIG(ix));
1403 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
Guenter Roeckc8de8362012-01-14 13:25:01 -08001404 /*
1405 * PWM is already in auto mode so update the temp
1406 * channel assignment
1407 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001408 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1409 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001410 dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001411 data->pwm_config[ix]);
1412 } else {
Guenter Roeckc8de8362012-01-14 13:25:01 -08001413 /*
1414 * PWM is not in auto mode so we save the temp
1415 * channel assignment for later use
1416 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001417 data->pwm_acz[ix] = val;
1418 }
1419 break;
1420 case SYS_PWM_AUTO_PWM_MIN:
1421 /* Only valid for pwm[1-3] */
1422 /* Refresh the cache */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001423 data->pwm_min[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001424 DME1737_REG_PWM_MIN(ix));
Guenter Roeckc8de8362012-01-14 13:25:01 -08001425 /*
1426 * There are only 2 values supported for the auto_pwm_min
Juerg Haefliger94319962007-06-09 10:11:16 -04001427 * value: 0 or auto_point1_pwm. So if the temperature drops
1428 * below the auto_point1_temp_hyst value, the fan either turns
Guenter Roeckc8de8362012-01-14 13:25:01 -08001429 * off or runs at auto_point1_pwm duty-cycle.
1430 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001431 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1432 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001433 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001434 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001435 } else {
1436 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001437 dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04001438 DME1737_REG_PWM_RR(0)));
Juerg Haefliger94319962007-06-09 10:11:16 -04001439 }
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001440 dme1737_write(data, DME1737_REG_PWM_RR(0),
Juerg Haefliger94319962007-06-09 10:11:16 -04001441 data->pwm_rr[0]);
1442 break;
1443 case SYS_PWM_AUTO_POINT1_PWM:
1444 /* Only valid for pwm[1-3] */
Guenter Roeck2a844c12013-01-09 08:09:34 -08001445 data->pwm_min[ix] = clamp_val(val, 0, 255);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001446 dme1737_write(data, DME1737_REG_PWM_MIN(ix),
Juerg Haefliger94319962007-06-09 10:11:16 -04001447 data->pwm_min[ix]);
1448 break;
1449 default:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001450 dev_dbg(dev, "Unknown function %d.\n", fn);
Juerg Haefliger94319962007-06-09 10:11:16 -04001451 }
1452exit:
1453 mutex_unlock(&data->update_lock);
1454
1455 return count;
1456}
1457
1458/* ---------------------------------------------------------------------
1459 * Miscellaneous sysfs attributes
1460 * --------------------------------------------------------------------- */
1461
Julia Lawallfd5ddb82016-12-22 13:05:20 +01001462static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001463 char *buf)
1464{
1465 struct i2c_client *client = to_i2c_client(dev);
1466 struct dme1737_data *data = i2c_get_clientdata(client);
1467
1468 return sprintf(buf, "%d\n", data->vrm);
1469}
1470
Julia Lawallfd5ddb82016-12-22 13:05:20 +01001471static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
1472 const char *buf, size_t count)
Juerg Haefliger94319962007-06-09 10:11:16 -04001473{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001474 struct dme1737_data *data = dev_get_drvdata(dev);
Axel Lind58e47d2014-08-06 08:02:44 +08001475 unsigned long val;
Guenter Roeckc8de8362012-01-14 13:25:01 -08001476 int err;
1477
Axel Lind58e47d2014-08-06 08:02:44 +08001478 err = kstrtoul(buf, 10, &val);
Guenter Roeckc8de8362012-01-14 13:25:01 -08001479 if (err)
1480 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04001481
Axel Lind58e47d2014-08-06 08:02:44 +08001482 if (val > 255)
1483 return -EINVAL;
1484
Juerg Haefliger94319962007-06-09 10:11:16 -04001485 data->vrm = val;
1486 return count;
1487}
1488
Julia Lawallfd5ddb82016-12-22 13:05:20 +01001489static ssize_t cpu0_vid_show(struct device *dev,
1490 struct device_attribute *attr, char *buf)
Juerg Haefliger94319962007-06-09 10:11:16 -04001491{
1492 struct dme1737_data *data = dme1737_update_device(dev);
1493
1494 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1495}
1496
Julia Lawallfd5ddb82016-12-22 13:05:20 +01001497static ssize_t name_show(struct device *dev, struct device_attribute *attr,
Juerg Haefligere95c2372007-10-07 21:27:35 -07001498 char *buf)
1499{
1500 struct dme1737_data *data = dev_get_drvdata(dev);
1501
Jean Delvaredbc2bc22008-10-14 17:30:04 +02001502 return sprintf(buf, "%s\n", data->name);
Juerg Haefligere95c2372007-10-07 21:27:35 -07001503}
1504
Juerg Haefliger94319962007-06-09 10:11:16 -04001505/* ---------------------------------------------------------------------
1506 * Sysfs device attribute defines and structs
1507 * --------------------------------------------------------------------- */
1508
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001509/* Voltages 0-7 */
Juerg Haefliger94319962007-06-09 10:11:16 -04001510
1511#define SENSOR_DEVICE_ATTR_IN(ix) \
1512static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001513 show_in, NULL, SYS_IN_INPUT, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001514static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001515 show_in, set_in, SYS_IN_MIN, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001516static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001517 show_in, set_in, SYS_IN_MAX, ix); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001518static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001519 show_in, NULL, SYS_IN_ALARM, ix)
Juerg Haefliger94319962007-06-09 10:11:16 -04001520
1521SENSOR_DEVICE_ATTR_IN(0);
1522SENSOR_DEVICE_ATTR_IN(1);
1523SENSOR_DEVICE_ATTR_IN(2);
1524SENSOR_DEVICE_ATTR_IN(3);
1525SENSOR_DEVICE_ATTR_IN(4);
1526SENSOR_DEVICE_ATTR_IN(5);
1527SENSOR_DEVICE_ATTR_IN(6);
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001528SENSOR_DEVICE_ATTR_IN(7);
Juerg Haefliger94319962007-06-09 10:11:16 -04001529
1530/* Temperatures 1-3 */
1531
1532#define SENSOR_DEVICE_ATTR_TEMP(ix) \
1533static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001534 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001535static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001536 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001537static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001538 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001539static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001540 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001541static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001542 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001543static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001544 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001545
1546SENSOR_DEVICE_ATTR_TEMP(1);
1547SENSOR_DEVICE_ATTR_TEMP(2);
1548SENSOR_DEVICE_ATTR_TEMP(3);
1549
1550/* Zones 1-3 */
1551
1552#define SENSOR_DEVICE_ATTR_ZONE(ix) \
1553static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001554 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001555static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001556 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001557static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001558 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001559static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001560 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001561static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001562 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001563
1564SENSOR_DEVICE_ATTR_ZONE(1);
1565SENSOR_DEVICE_ATTR_ZONE(2);
1566SENSOR_DEVICE_ATTR_ZONE(3);
1567
1568/* Fans 1-4 */
1569
1570#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1571static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001572 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001573static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001574 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001575static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001576 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001577static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001578 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001579
1580SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1581SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1582SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1583SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1584
1585/* Fans 5-6 */
1586
1587#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1588static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001589 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001590static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001591 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001592static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001593 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001594static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001595 show_fan, set_fan, SYS_FAN_MAX, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001596
1597SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1598SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1599
1600/* PWMs 1-3 */
1601
1602#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1603static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001604 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001605static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001606 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001607static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001608 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001609static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001610 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001611static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001612 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001613static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001614 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001615static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001616 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001617static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001618 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001619
1620SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1621SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1622SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1623
1624/* PWMs 5-6 */
1625
1626#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001627static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001628 show_pwm, set_pwm, SYS_PWM, ix-1); \
Juerg Haefliger9b257712008-03-25 21:49:02 -07001629static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001630 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
Juerg Haefliger94319962007-06-09 10:11:16 -04001631static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001632 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
Juerg Haefliger94319962007-06-09 10:11:16 -04001633
1634SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1635SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1636
1637/* Misc */
1638
Julia Lawallfd5ddb82016-12-22 13:05:20 +01001639static DEVICE_ATTR_RW(vrm);
1640static DEVICE_ATTR_RO(cpu0_vid);
1641static DEVICE_ATTR_RO(name); /* for ISA devices */
Juerg Haefliger94319962007-06-09 10:11:16 -04001642
Guenter Roeckc8de8362012-01-14 13:25:01 -08001643/*
1644 * This struct holds all the attributes that are always present and need to be
Juerg Haefliger94319962007-06-09 10:11:16 -04001645 * created unconditionally. The attributes that need modification of their
1646 * permissions are created read-only and write permissions are added or removed
Guenter Roeckc8de8362012-01-14 13:25:01 -08001647 * on the fly when required
1648 */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01001649static struct attribute *dme1737_attr[] = {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001650 /* Voltages */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001651 &sensor_dev_attr_in0_input.dev_attr.attr,
1652 &sensor_dev_attr_in0_min.dev_attr.attr,
1653 &sensor_dev_attr_in0_max.dev_attr.attr,
1654 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1655 &sensor_dev_attr_in1_input.dev_attr.attr,
1656 &sensor_dev_attr_in1_min.dev_attr.attr,
1657 &sensor_dev_attr_in1_max.dev_attr.attr,
1658 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1659 &sensor_dev_attr_in2_input.dev_attr.attr,
1660 &sensor_dev_attr_in2_min.dev_attr.attr,
1661 &sensor_dev_attr_in2_max.dev_attr.attr,
1662 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1663 &sensor_dev_attr_in3_input.dev_attr.attr,
1664 &sensor_dev_attr_in3_min.dev_attr.attr,
1665 &sensor_dev_attr_in3_max.dev_attr.attr,
1666 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1667 &sensor_dev_attr_in4_input.dev_attr.attr,
1668 &sensor_dev_attr_in4_min.dev_attr.attr,
1669 &sensor_dev_attr_in4_max.dev_attr.attr,
1670 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1671 &sensor_dev_attr_in5_input.dev_attr.attr,
1672 &sensor_dev_attr_in5_min.dev_attr.attr,
1673 &sensor_dev_attr_in5_max.dev_attr.attr,
1674 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1675 &sensor_dev_attr_in6_input.dev_attr.attr,
1676 &sensor_dev_attr_in6_min.dev_attr.attr,
1677 &sensor_dev_attr_in6_max.dev_attr.attr,
1678 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001679 /* Temperatures */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001680 &sensor_dev_attr_temp1_input.dev_attr.attr,
1681 &sensor_dev_attr_temp1_min.dev_attr.attr,
1682 &sensor_dev_attr_temp1_max.dev_attr.attr,
1683 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1684 &sensor_dev_attr_temp1_fault.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001685 &sensor_dev_attr_temp2_input.dev_attr.attr,
1686 &sensor_dev_attr_temp2_min.dev_attr.attr,
1687 &sensor_dev_attr_temp2_max.dev_attr.attr,
1688 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1689 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001690 &sensor_dev_attr_temp3_input.dev_attr.attr,
1691 &sensor_dev_attr_temp3_min.dev_attr.attr,
1692 &sensor_dev_attr_temp3_max.dev_attr.attr,
1693 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1694 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07001695 /* Zones */
Juerg Haefliger9b257712008-03-25 21:49:02 -07001696 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1697 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1698 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1699 &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001700 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1701 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1702 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1703 &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
Juerg Haefliger549edb82008-08-06 22:41:03 +02001704 NULL
1705};
1706
1707static const struct attribute_group dme1737_group = {
1708 .attrs = dme1737_attr,
1709};
1710
Guenter Roeckc8de8362012-01-14 13:25:01 -08001711/*
1712 * The following struct holds temp offset attributes, which are not available
Juerg Haefligerea694432010-05-27 19:59:01 +02001713 * in all chips. The following chips support them:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001714 * DME1737, SCH311x
1715 */
Juerg Haefligerea694432010-05-27 19:59:01 +02001716static struct attribute *dme1737_temp_offset_attr[] = {
Juerg Haefliger549edb82008-08-06 22:41:03 +02001717 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1718 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1719 &sensor_dev_attr_temp3_offset.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001720 NULL
1721};
1722
Juerg Haefligerea694432010-05-27 19:59:01 +02001723static const struct attribute_group dme1737_temp_offset_group = {
1724 .attrs = dme1737_temp_offset_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001725};
1726
Guenter Roeckc8de8362012-01-14 13:25:01 -08001727/*
1728 * The following struct holds VID related attributes, which are not available
Juerg Haefligerea694432010-05-27 19:59:01 +02001729 * in all chips. The following chips support them:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001730 * DME1737
1731 */
Jean Delvare9d091442009-10-24 13:28:46 +02001732static struct attribute *dme1737_vid_attr[] = {
1733 &dev_attr_vrm.attr,
1734 &dev_attr_cpu0_vid.attr,
1735 NULL
1736};
1737
1738static const struct attribute_group dme1737_vid_group = {
1739 .attrs = dme1737_vid_attr,
1740};
1741
Guenter Roeckc8de8362012-01-14 13:25:01 -08001742/*
1743 * The following struct holds temp zone 3 related attributes, which are not
Juerg Haefligerea694432010-05-27 19:59:01 +02001744 * available in all chips. The following chips support them:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001745 * DME1737, SCH311x, SCH5027
1746 */
Juerg Haefligerea694432010-05-27 19:59:01 +02001747static struct attribute *dme1737_zone3_attr[] = {
1748 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1749 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1750 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1751 &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1752 NULL
1753};
1754
1755static const struct attribute_group dme1737_zone3_group = {
1756 .attrs = dme1737_zone3_attr,
1757};
1758
1759
Guenter Roeckc8de8362012-01-14 13:25:01 -08001760/*
1761 * The following struct holds temp zone hysteresis related attributes, which
Juerg Haefligerea694432010-05-27 19:59:01 +02001762 * are not available in all chips. The following chips support them:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001763 * DME1737, SCH311x
1764 */
Juerg Haefligerea694432010-05-27 19:59:01 +02001765static struct attribute *dme1737_zone_hyst_attr[] = {
1766 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1767 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1768 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1769 NULL
1770};
1771
1772static const struct attribute_group dme1737_zone_hyst_group = {
1773 .attrs = dme1737_zone_hyst_attr,
1774};
1775
Guenter Roeckc8de8362012-01-14 13:25:01 -08001776/*
1777 * The following struct holds voltage in7 related attributes, which
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001778 * are not available in all chips. The following chips support them:
Guenter Roeckc8de8362012-01-14 13:25:01 -08001779 * SCH5127
1780 */
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01001781static struct attribute *dme1737_in7_attr[] = {
1782 &sensor_dev_attr_in7_input.dev_attr.attr,
1783 &sensor_dev_attr_in7_min.dev_attr.attr,
1784 &sensor_dev_attr_in7_max.dev_attr.attr,
1785 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1786 NULL
1787};
1788
1789static const struct attribute_group dme1737_in7_group = {
1790 .attrs = dme1737_in7_attr,
1791};
1792
Guenter Roeckc8de8362012-01-14 13:25:01 -08001793/*
1794 * The following structs hold the PWM attributes, some of which are optional.
Juerg Haefliger94319962007-06-09 10:11:16 -04001795 * Their creation depends on the chip configuration which is determined during
Guenter Roeckc8de8362012-01-14 13:25:01 -08001796 * module load.
1797 */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001798static struct attribute *dme1737_pwm1_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001799 &sensor_dev_attr_pwm1.dev_attr.attr,
1800 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1801 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1802 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1803 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001804 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1805 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001806 NULL
1807};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001808static struct attribute *dme1737_pwm2_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001809 &sensor_dev_attr_pwm2.dev_attr.attr,
1810 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1811 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1812 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1813 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001814 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1815 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001816 NULL
1817};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001818static struct attribute *dme1737_pwm3_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001819 &sensor_dev_attr_pwm3.dev_attr.attr,
1820 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1821 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1822 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1823 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001824 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1825 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001826 NULL
1827};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001828static struct attribute *dme1737_pwm5_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001829 &sensor_dev_attr_pwm5.dev_attr.attr,
1830 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1831 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001832 NULL
1833};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001834static struct attribute *dme1737_pwm6_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001835 &sensor_dev_attr_pwm6.dev_attr.attr,
1836 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1837 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001838 NULL
1839};
1840
1841static const struct attribute_group dme1737_pwm_group[] = {
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001842 { .attrs = dme1737_pwm1_attr },
1843 { .attrs = dme1737_pwm2_attr },
1844 { .attrs = dme1737_pwm3_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001845 { .attrs = NULL },
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001846 { .attrs = dme1737_pwm5_attr },
1847 { .attrs = dme1737_pwm6_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001848};
1849
Guenter Roeckc8de8362012-01-14 13:25:01 -08001850/*
1851 * The following struct holds auto PWM min attributes, which are not available
Juerg Haefligerea694432010-05-27 19:59:01 +02001852 * in all chips. Their creation depends on the chip type which is determined
Guenter Roeckc8de8362012-01-14 13:25:01 -08001853 * during module load.
1854 */
Juerg Haefligerea694432010-05-27 19:59:01 +02001855static struct attribute *dme1737_auto_pwm_min_attr[] = {
Juerg Haefliger549edb82008-08-06 22:41:03 +02001856 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1857 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1858 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1859};
1860
Guenter Roeckc8de8362012-01-14 13:25:01 -08001861/*
1862 * The following structs hold the fan attributes, some of which are optional.
Juerg Haefliger94319962007-06-09 10:11:16 -04001863 * Their creation depends on the chip configuration which is determined during
Guenter Roeckc8de8362012-01-14 13:25:01 -08001864 * module load.
1865 */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001866static struct attribute *dme1737_fan1_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001867 &sensor_dev_attr_fan1_input.dev_attr.attr,
1868 &sensor_dev_attr_fan1_min.dev_attr.attr,
1869 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1870 &sensor_dev_attr_fan1_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001871 NULL
1872};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001873static struct attribute *dme1737_fan2_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001874 &sensor_dev_attr_fan2_input.dev_attr.attr,
1875 &sensor_dev_attr_fan2_min.dev_attr.attr,
1876 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1877 &sensor_dev_attr_fan2_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001878 NULL
1879};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001880static struct attribute *dme1737_fan3_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001881 &sensor_dev_attr_fan3_input.dev_attr.attr,
1882 &sensor_dev_attr_fan3_min.dev_attr.attr,
1883 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1884 &sensor_dev_attr_fan3_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001885 NULL
1886};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001887static struct attribute *dme1737_fan4_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001888 &sensor_dev_attr_fan4_input.dev_attr.attr,
1889 &sensor_dev_attr_fan4_min.dev_attr.attr,
1890 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1891 &sensor_dev_attr_fan4_type.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001892 NULL
1893};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001894static struct attribute *dme1737_fan5_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001895 &sensor_dev_attr_fan5_input.dev_attr.attr,
1896 &sensor_dev_attr_fan5_min.dev_attr.attr,
1897 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1898 &sensor_dev_attr_fan5_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001899 NULL
1900};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001901static struct attribute *dme1737_fan6_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001902 &sensor_dev_attr_fan6_input.dev_attr.attr,
1903 &sensor_dev_attr_fan6_min.dev_attr.attr,
1904 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1905 &sensor_dev_attr_fan6_max.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001906 NULL
1907};
1908
1909static const struct attribute_group dme1737_fan_group[] = {
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001910 { .attrs = dme1737_fan1_attr },
1911 { .attrs = dme1737_fan2_attr },
1912 { .attrs = dme1737_fan3_attr },
1913 { .attrs = dme1737_fan4_attr },
1914 { .attrs = dme1737_fan5_attr },
1915 { .attrs = dme1737_fan6_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001916};
1917
Guenter Roeckc8de8362012-01-14 13:25:01 -08001918/*
1919 * The permissions of the following zone attributes are changed to read-
1920 * writeable if the chip is *not* locked. Otherwise they stay read-only.
1921 */
Juerg Haefliger549edb82008-08-06 22:41:03 +02001922static struct attribute *dme1737_zone_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001923 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1924 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1925 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001926 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1927 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1928 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
Juerg Haefligerea694432010-05-27 19:59:01 +02001929 NULL
1930};
1931
1932static const struct attribute_group dme1737_zone_chmod_group = {
1933 .attrs = dme1737_zone_chmod_attr,
1934};
1935
1936
Guenter Roeckc8de8362012-01-14 13:25:01 -08001937/*
1938 * The permissions of the following zone 3 attributes are changed to read-
1939 * writeable if the chip is *not* locked. Otherwise they stay read-only.
1940 */
Juerg Haefligerea694432010-05-27 19:59:01 +02001941static struct attribute *dme1737_zone3_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001942 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1943 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1944 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001945 NULL
1946};
1947
Juerg Haefligerea694432010-05-27 19:59:01 +02001948static const struct attribute_group dme1737_zone3_chmod_group = {
1949 .attrs = dme1737_zone3_chmod_attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001950};
1951
Guenter Roeckc8de8362012-01-14 13:25:01 -08001952/*
1953 * The permissions of the following PWM attributes are changed to read-
Juerg Haefliger94319962007-06-09 10:11:16 -04001954 * writeable if the chip is *not* locked and the respective PWM is available.
Guenter Roeckc8de8362012-01-14 13:25:01 -08001955 * Otherwise they stay read-only.
1956 */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001957static struct attribute *dme1737_pwm1_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001958 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1959 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1960 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1961 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001962 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001963 NULL
1964};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001965static struct attribute *dme1737_pwm2_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001966 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1967 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1968 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1969 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001970 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001971 NULL
1972};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001973static struct attribute *dme1737_pwm3_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001974 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1975 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1976 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1977 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
Juerg Haefliger9b257712008-03-25 21:49:02 -07001978 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001979 NULL
1980};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001981static struct attribute *dme1737_pwm5_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001982 &sensor_dev_attr_pwm5.dev_attr.attr,
1983 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001984 NULL
1985};
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001986static struct attribute *dme1737_pwm6_chmod_attr[] = {
Juerg Haefliger9b257712008-03-25 21:49:02 -07001987 &sensor_dev_attr_pwm6.dev_attr.attr,
1988 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
Juerg Haefliger94319962007-06-09 10:11:16 -04001989 NULL
1990};
1991
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001992static const struct attribute_group dme1737_pwm_chmod_group[] = {
1993 { .attrs = dme1737_pwm1_chmod_attr },
1994 { .attrs = dme1737_pwm2_chmod_attr },
1995 { .attrs = dme1737_pwm3_chmod_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001996 { .attrs = NULL },
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02001997 { .attrs = dme1737_pwm5_chmod_attr },
1998 { .attrs = dme1737_pwm6_chmod_attr },
Juerg Haefliger94319962007-06-09 10:11:16 -04001999};
2000
Guenter Roeckc8de8362012-01-14 13:25:01 -08002001/*
2002 * Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
2003 * chip is not locked. Otherwise they are read-only.
2004 */
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002005static struct attribute *dme1737_pwm_chmod_attr[] = {
Juerg Haefliger94319962007-06-09 10:11:16 -04002006 &sensor_dev_attr_pwm1.dev_attr.attr,
2007 &sensor_dev_attr_pwm2.dev_attr.attr,
2008 &sensor_dev_attr_pwm3.dev_attr.attr,
2009};
2010
2011/* ---------------------------------------------------------------------
2012 * Super-IO functions
2013 * --------------------------------------------------------------------- */
2014
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002015static inline void dme1737_sio_enter(int sio_cip)
2016{
2017 outb(0x55, sio_cip);
2018}
2019
2020static inline void dme1737_sio_exit(int sio_cip)
2021{
2022 outb(0xaa, sio_cip);
2023}
2024
Juerg Haefliger94319962007-06-09 10:11:16 -04002025static inline int dme1737_sio_inb(int sio_cip, int reg)
2026{
2027 outb(reg, sio_cip);
2028 return inb(sio_cip + 1);
2029}
2030
2031static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
2032{
2033 outb(reg, sio_cip);
2034 outb(val, sio_cip + 1);
2035}
2036
Juerg Haefliger94319962007-06-09 10:11:16 -04002037/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07002038 * Device initialization
Juerg Haefliger94319962007-06-09 10:11:16 -04002039 * --------------------------------------------------------------------- */
2040
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002041static int dme1737_i2c_get_features(int, struct dme1737_data*);
Juerg Haefliger94319962007-06-09 10:11:16 -04002042
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002043static void dme1737_chmod_file(struct device *dev,
Al Viro48176a92011-07-24 03:40:40 -04002044 struct attribute *attr, umode_t mode)
Juerg Haefliger94319962007-06-09 10:11:16 -04002045{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002046 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
2047 dev_warn(dev, "Failed to change permissions of %s.\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04002048 attr->name);
2049 }
2050}
2051
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002052static void dme1737_chmod_group(struct device *dev,
Juerg Haefliger94319962007-06-09 10:11:16 -04002053 const struct attribute_group *group,
Al Viro48176a92011-07-24 03:40:40 -04002054 umode_t mode)
Juerg Haefliger94319962007-06-09 10:11:16 -04002055{
2056 struct attribute **attr;
2057
Guenter Roeckc8de8362012-01-14 13:25:01 -08002058 for (attr = group->attrs; *attr; attr++)
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002059 dme1737_chmod_file(dev, *attr, mode);
Juerg Haefliger94319962007-06-09 10:11:16 -04002060}
2061
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002062static void dme1737_remove_files(struct device *dev)
Juerg Haefliger94319962007-06-09 10:11:16 -04002063{
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002064 struct dme1737_data *data = dev_get_drvdata(dev);
2065 int ix;
2066
2067 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002068 if (data->has_features & HAS_FAN(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002069 sysfs_remove_group(&dev->kobj,
2070 &dme1737_fan_group[ix]);
2071 }
2072 }
2073
2074 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002075 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002076 sysfs_remove_group(&dev->kobj,
2077 &dme1737_pwm_group[ix]);
Juerg Haefligerea694432010-05-27 19:59:01 +02002078 if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002079 sysfs_remove_file(&dev->kobj,
Juerg Haefligerea694432010-05-27 19:59:01 +02002080 dme1737_auto_pwm_min_attr[ix]);
Juerg Haefliger549edb82008-08-06 22:41:03 +02002081 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002082 }
2083 }
2084
Guenter Roeckc8de8362012-01-14 13:25:01 -08002085 if (data->has_features & HAS_TEMP_OFFSET)
Juerg Haefligerea694432010-05-27 19:59:01 +02002086 sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002087 if (data->has_features & HAS_VID)
Jean Delvare9d091442009-10-24 13:28:46 +02002088 sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002089 if (data->has_features & HAS_ZONE3)
Juerg Haefligerea694432010-05-27 19:59:01 +02002090 sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002091 if (data->has_features & HAS_ZONE_HYST)
Juerg Haefligerea694432010-05-27 19:59:01 +02002092 sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002093 if (data->has_features & HAS_IN7)
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002094 sysfs_remove_group(&dev->kobj, &dme1737_in7_group);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002095 sysfs_remove_group(&dev->kobj, &dme1737_group);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002096
Guenter Roeckc8de8362012-01-14 13:25:01 -08002097 if (!data->client)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002098 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002099}
2100
2101static int dme1737_create_files(struct device *dev)
2102{
2103 struct dme1737_data *data = dev_get_drvdata(dev);
2104 int err, ix;
2105
Juerg Haefligere95c2372007-10-07 21:27:35 -07002106 /* Create a name attribute for ISA devices */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002107 if (!data->client) {
2108 err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002109 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002110 goto exit;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002111 }
2112
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002113 /* Create standard sysfs attributes */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002114 err = sysfs_create_group(&dev->kobj, &dme1737_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002115 if (err)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002116 goto exit_remove;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002117
Juerg Haefligerea694432010-05-27 19:59:01 +02002118 /* Create chip-dependent sysfs attributes */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002119 if (data->has_features & HAS_TEMP_OFFSET) {
2120 err = sysfs_create_group(&dev->kobj,
2121 &dme1737_temp_offset_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002122 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002123 goto exit_remove;
Juerg Haefliger549edb82008-08-06 22:41:03 +02002124 }
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002125 if (data->has_features & HAS_VID) {
2126 err = sysfs_create_group(&dev->kobj, &dme1737_vid_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002127 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002128 goto exit_remove;
Jean Delvare9d091442009-10-24 13:28:46 +02002129 }
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002130 if (data->has_features & HAS_ZONE3) {
2131 err = sysfs_create_group(&dev->kobj, &dme1737_zone3_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002132 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002133 goto exit_remove;
Juerg Haefligerea694432010-05-27 19:59:01 +02002134 }
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002135 if (data->has_features & HAS_ZONE_HYST) {
2136 err = sysfs_create_group(&dev->kobj, &dme1737_zone_hyst_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002137 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002138 goto exit_remove;
Juerg Haefligerea694432010-05-27 19:59:01 +02002139 }
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002140 if (data->has_features & HAS_IN7) {
2141 err = sysfs_create_group(&dev->kobj, &dme1737_in7_group);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002142 if (err)
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002143 goto exit_remove;
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002144 }
Jean Delvare9d091442009-10-24 13:28:46 +02002145
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002146 /* Create fan sysfs attributes */
2147 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002148 if (data->has_features & HAS_FAN(ix)) {
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002149 err = sysfs_create_group(&dev->kobj,
2150 &dme1737_fan_group[ix]);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002151 if (err)
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002152 goto exit_remove;
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002153 }
2154 }
2155
2156 /* Create PWM sysfs attributes */
2157 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002158 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002159 err = sysfs_create_group(&dev->kobj,
2160 &dme1737_pwm_group[ix]);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002161 if (err)
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002162 goto exit_remove;
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002163 if ((data->has_features & HAS_PWM_MIN) && (ix < 3)) {
2164 err = sysfs_create_file(&dev->kobj,
2165 dme1737_auto_pwm_min_attr[ix]);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002166 if (err)
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002167 goto exit_remove;
Juerg Haefliger549edb82008-08-06 22:41:03 +02002168 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002169 }
2170 }
2171
Guenter Roeckc8de8362012-01-14 13:25:01 -08002172 /*
2173 * Inform if the device is locked. Otherwise change the permissions of
2174 * selected attributes from read-only to read-writeable.
2175 */
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002176 if (data->config & 0x02) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08002177 dev_info(dev,
2178 "Device is locked. Some attributes will be read-only.\n");
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002179 } else {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002180 /* Change permissions of zone sysfs attributes */
2181 dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002182 S_IRUGO | S_IWUSR);
2183
Juerg Haefligerea694432010-05-27 19:59:01 +02002184 /* Change permissions of chip-dependent sysfs attributes */
2185 if (data->has_features & HAS_TEMP_OFFSET) {
2186 dme1737_chmod_group(dev, &dme1737_temp_offset_group,
2187 S_IRUGO | S_IWUSR);
2188 }
2189 if (data->has_features & HAS_ZONE3) {
2190 dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
2191 S_IRUGO | S_IWUSR);
2192 }
2193 if (data->has_features & HAS_ZONE_HYST) {
2194 dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
Juerg Haefliger549edb82008-08-06 22:41:03 +02002195 S_IRUGO | S_IWUSR);
2196 }
2197
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002198 /* Change permissions of PWM sysfs attributes */
2199 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002200 if (data->has_features & HAS_PWM(ix)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002201 dme1737_chmod_group(dev,
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002202 &dme1737_pwm_chmod_group[ix],
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002203 S_IRUGO | S_IWUSR);
Juerg Haefligerea694432010-05-27 19:59:01 +02002204 if ((data->has_features & HAS_PWM_MIN) &&
2205 ix < 3) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002206 dme1737_chmod_file(dev,
Juerg Haefligerea694432010-05-27 19:59:01 +02002207 dme1737_auto_pwm_min_attr[ix],
Juerg Haefliger549edb82008-08-06 22:41:03 +02002208 S_IRUGO | S_IWUSR);
2209 }
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002210 }
2211 }
2212
2213 /* Change permissions of pwm[1-3] if in manual mode */
2214 for (ix = 0; ix < 3; ix++) {
Juerg Haefligerea694432010-05-27 19:59:01 +02002215 if ((data->has_features & HAS_PWM(ix)) &&
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002216 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
2217 dme1737_chmod_file(dev,
Juerg Haefliger73ce48f2008-08-06 22:41:03 +02002218 dme1737_pwm_chmod_attr[ix],
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002219 S_IRUGO | S_IWUSR);
2220 }
2221 }
2222 }
2223
2224 return 0;
2225
2226exit_remove:
2227 dme1737_remove_files(dev);
2228exit:
2229 return err;
2230}
2231
2232static int dme1737_init_device(struct device *dev)
2233{
2234 struct dme1737_data *data = dev_get_drvdata(dev);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002235 struct i2c_client *client = data->client;
Juerg Haefliger94319962007-06-09 10:11:16 -04002236 int ix;
2237 u8 reg;
2238
Juerg Haefliger549edb82008-08-06 22:41:03 +02002239 /* Point to the right nominal voltages array */
2240 data->in_nominal = IN_NOMINAL(data->type);
2241
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002242 data->config = dme1737_read(data, DME1737_REG_CONFIG);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002243 /* Inform if part is not monitoring/started */
2244 if (!(data->config & 0x01)) {
2245 if (!force_start) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08002246 dev_err(dev,
2247 "Device is not monitoring. Use the force_start load parameter to override.\n");
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002248 return -EFAULT;
2249 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002250
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002251 /* Force monitoring */
2252 data->config |= 0x01;
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002253 dme1737_write(data, DME1737_REG_CONFIG, data->config);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002254 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002255 /* Inform if part is not ready */
2256 if (!(data->config & 0x04)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002257 dev_err(dev, "Device is not ready.\n");
Juerg Haefliger94319962007-06-09 10:11:16 -04002258 return -EFAULT;
2259 }
2260
Guenter Roeckc8de8362012-01-14 13:25:01 -08002261 /*
2262 * Determine which optional fan and pwm features are enabled (only
2263 * valid for I2C devices)
2264 */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002265 if (client) { /* I2C chip */
2266 data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002267 /* Check if optional fan3 input is enabled */
Guenter Roeckc8de8362012-01-14 13:25:01 -08002268 if (data->config2 & 0x04)
Juerg Haefligerea694432010-05-27 19:59:01 +02002269 data->has_features |= HAS_FAN(2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002270
Guenter Roeckc8de8362012-01-14 13:25:01 -08002271 /*
2272 * Fan4 and pwm3 are only available if the client's I2C address
Juerg Haefligere95c2372007-10-07 21:27:35 -07002273 * is the default 0x2e. Otherwise the I/Os associated with
Guenter Roeckc8de8362012-01-14 13:25:01 -08002274 * these functions are used for addr enable/select.
2275 */
2276 if (client->addr == 0x2e)
Juerg Haefligerea694432010-05-27 19:59:01 +02002277 data->has_features |= HAS_FAN(3) | HAS_PWM(2);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002278
Guenter Roeckc8de8362012-01-14 13:25:01 -08002279 /*
2280 * Determine which of the optional fan[5-6] and pwm[5-6]
Juerg Haefligere95c2372007-10-07 21:27:35 -07002281 * features are enabled. For this, we need to query the runtime
2282 * registers through the Super-IO LPC interface. Try both
Guenter Roeckc8de8362012-01-14 13:25:01 -08002283 * config ports 0x2e and 0x4e.
2284 */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002285 if (dme1737_i2c_get_features(0x2e, data) &&
2286 dme1737_i2c_get_features(0x4e, data)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08002287 dev_warn(dev,
2288 "Failed to query Super-IO for optional features.\n");
Juerg Haefligere95c2372007-10-07 21:27:35 -07002289 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002290 }
2291
Juerg Haefligerea694432010-05-27 19:59:01 +02002292 /* Fan[1-2] and pwm[1-2] are present in all chips */
2293 data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);
2294
2295 /* Chip-dependent features */
2296 switch (data->type) {
2297 case dme1737:
2298 data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
2299 HAS_ZONE_HYST | HAS_PWM_MIN;
2300 break;
2301 case sch311x:
2302 data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
2303 HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
2304 break;
2305 case sch5027:
2306 data->has_features |= HAS_ZONE3;
2307 break;
2308 case sch5127:
Juerg Haefligerd4b94e12011-01-12 21:55:13 +01002309 data->has_features |= HAS_FAN(2) | HAS_PWM(2) | HAS_IN7;
Juerg Haefligerea694432010-05-27 19:59:01 +02002310 break;
2311 default:
2312 break;
2313 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002314
Guenter Roeckb55f3752013-01-10 10:01:24 -08002315 dev_info(dev,
2316 "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
Juerg Haefligerea694432010-05-27 19:59:01 +02002317 (data->has_features & HAS_PWM(2)) ? "yes" : "no",
2318 (data->has_features & HAS_PWM(4)) ? "yes" : "no",
2319 (data->has_features & HAS_PWM(5)) ? "yes" : "no",
2320 (data->has_features & HAS_FAN(2)) ? "yes" : "no",
2321 (data->has_features & HAS_FAN(3)) ? "yes" : "no",
2322 (data->has_features & HAS_FAN(4)) ? "yes" : "no",
2323 (data->has_features & HAS_FAN(5)) ? "yes" : "no");
Juerg Haefliger94319962007-06-09 10:11:16 -04002324
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002325 reg = dme1737_read(data, DME1737_REG_TACH_PWM);
Juerg Haefliger94319962007-06-09 10:11:16 -04002326 /* Inform if fan-to-pwm mapping differs from the default */
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002327 if (client && reg != 0xa4) { /* I2C chip */
Guenter Roeckb55f3752013-01-10 10:01:24 -08002328 dev_warn(dev,
2329 "Non-standard fan to pwm mapping: fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, fan4->pwm%d. %s\n",
Juerg Haefliger94319962007-06-09 10:11:16 -04002330 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
Guenter Roeckb55f3752013-01-10 10:01:24 -08002331 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1,
2332 DO_REPORT);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002333 } else if (!client && reg != 0x24) { /* ISA chip */
Guenter Roeckb55f3752013-01-10 10:01:24 -08002334 dev_warn(dev,
2335 "Non-standard fan to pwm mapping: fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. %s\n",
Juerg Haefligere95c2372007-10-07 21:27:35 -07002336 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
Guenter Roeckb55f3752013-01-10 10:01:24 -08002337 ((reg >> 4) & 0x03) + 1, DO_REPORT);
Juerg Haefliger94319962007-06-09 10:11:16 -04002338 }
2339
Guenter Roeckc8de8362012-01-14 13:25:01 -08002340 /*
2341 * Switch pwm[1-3] to manual mode if they are currently disabled and
Juerg Haefliger94319962007-06-09 10:11:16 -04002342 * set the duty-cycles to 0% (which is identical to the PWMs being
Guenter Roeckc8de8362012-01-14 13:25:01 -08002343 * disabled).
2344 */
Juerg Haefliger94319962007-06-09 10:11:16 -04002345 if (!(data->config & 0x02)) {
2346 for (ix = 0; ix < 3; ix++) {
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002347 data->pwm_config[ix] = dme1737_read(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04002348 DME1737_REG_PWM_CONFIG(ix));
Juerg Haefligerea694432010-05-27 19:59:01 +02002349 if ((data->has_features & HAS_PWM(ix)) &&
Juerg Haefliger94319962007-06-09 10:11:16 -04002350 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08002351 dev_info(dev,
2352 "Switching pwm%d to manual mode.\n",
2353 ix + 1);
Juerg Haefliger94319962007-06-09 10:11:16 -04002354 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2355 data->pwm_config[ix]);
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002356 dme1737_write(data, DME1737_REG_PWM(ix), 0);
2357 dme1737_write(data,
Juerg Haefliger94319962007-06-09 10:11:16 -04002358 DME1737_REG_PWM_CONFIG(ix),
2359 data->pwm_config[ix]);
2360 }
2361 }
2362 }
2363
2364 /* Initialize the default PWM auto channels zone (acz) assignments */
2365 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
2366 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
2367 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
2368
2369 /* Set VRM */
Guenter Roeckc8de8362012-01-14 13:25:01 -08002370 if (data->has_features & HAS_VID)
Juerg Haefliger549edb82008-08-06 22:41:03 +02002371 data->vrm = vid_which_vrm();
Juerg Haefliger94319962007-06-09 10:11:16 -04002372
2373 return 0;
2374}
2375
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002376/* ---------------------------------------------------------------------
2377 * I2C device detection and registration
2378 * --------------------------------------------------------------------- */
2379
2380static struct i2c_driver dme1737_i2c_driver;
2381
2382static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2383{
Juerg Haefliger94319962007-06-09 10:11:16 -04002384 int err = 0, reg;
2385 u16 addr;
2386
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002387 dme1737_sio_enter(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002388
Guenter Roeckc8de8362012-01-14 13:25:01 -08002389 /*
2390 * Check device ID
2391 * We currently know about two kinds of DME1737 and SCH5027.
2392 */
Juerg Haefliger345a2222008-01-26 08:54:24 -08002393 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefligerea694432010-05-27 19:59:01 +02002394 if (!(reg == DME1737_ID_1 || reg == DME1737_ID_2 ||
2395 reg == SCH5027_ID)) {
Juerg Haefliger94319962007-06-09 10:11:16 -04002396 err = -ENODEV;
2397 goto exit;
2398 }
2399
2400 /* Select logical device A (runtime registers) */
2401 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2402
2403 /* Get the base address of the runtime registers */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002404 addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2405 dme1737_sio_inb(sio_cip, 0x61);
2406 if (!addr) {
Juerg Haefliger94319962007-06-09 10:11:16 -04002407 err = -ENODEV;
2408 goto exit;
2409 }
2410
Guenter Roeckc8de8362012-01-14 13:25:01 -08002411 /*
2412 * Read the runtime registers to determine which optional features
Juerg Haefliger94319962007-06-09 10:11:16 -04002413 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
Guenter Roeckc8de8362012-01-14 13:25:01 -08002414 * to '10' if the respective feature is enabled.
2415 */
2416 if ((inb(addr + 0x43) & 0x0c) == 0x08) /* fan6 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002417 data->has_features |= HAS_FAN(5);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002418 if ((inb(addr + 0x44) & 0x0c) == 0x08) /* pwm6 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002419 data->has_features |= HAS_PWM(5);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002420 if ((inb(addr + 0x45) & 0x0c) == 0x08) /* fan5 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002421 data->has_features |= HAS_FAN(4);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002422 if ((inb(addr + 0x46) & 0x0c) == 0x08) /* pwm5 */
Juerg Haefligerea694432010-05-27 19:59:01 +02002423 data->has_features |= HAS_PWM(4);
Juerg Haefliger94319962007-06-09 10:11:16 -04002424
2425exit:
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002426 dme1737_sio_exit(sio_cip);
Juerg Haefliger94319962007-06-09 10:11:16 -04002427
2428 return err;
2429}
2430
Jean Delvare67a37302008-10-14 17:30:04 +02002431/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01002432static int dme1737_i2c_detect(struct i2c_client *client,
Jean Delvare67a37302008-10-14 17:30:04 +02002433 struct i2c_board_info *info)
Juerg Haefliger94319962007-06-09 10:11:16 -04002434{
Jean Delvare67a37302008-10-14 17:30:04 +02002435 struct i2c_adapter *adapter = client->adapter;
2436 struct device *dev = &adapter->dev;
Juerg Haefliger94319962007-06-09 10:11:16 -04002437 u8 company, verstep = 0;
Juerg Haefliger94319962007-06-09 10:11:16 -04002438 const char *name;
2439
Guenter Roeckc8de8362012-01-14 13:25:01 -08002440 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare67a37302008-10-14 17:30:04 +02002441 return -ENODEV;
Juerg Haefliger94319962007-06-09 10:11:16 -04002442
Jean Delvare52df6442009-12-09 20:35:57 +01002443 company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
2444 verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);
Juerg Haefliger94319962007-06-09 10:11:16 -04002445
Jean Delvare52df6442009-12-09 20:35:57 +01002446 if (company == DME1737_COMPANY_SMSC &&
2447 verstep == SCH5027_VERSTEP) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002448 name = "sch5027";
Jean Delvare52df6442009-12-09 20:35:57 +01002449 } else if (company == DME1737_COMPANY_SMSC &&
2450 (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
Juerg Haefliger549edb82008-08-06 22:41:03 +02002451 name = "dme1737";
Jean Delvare52df6442009-12-09 20:35:57 +01002452 } else {
2453 return -ENODEV;
Juerg Haefliger549edb82008-08-06 22:41:03 +02002454 }
Juerg Haefliger94319962007-06-09 10:11:16 -04002455
Juerg Haefliger549edb82008-08-06 22:41:03 +02002456 dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
Jean Delvare52df6442009-12-09 20:35:57 +01002457 verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737",
2458 client->addr, verstep);
Jean Delvare67a37302008-10-14 17:30:04 +02002459 strlcpy(info->type, name, I2C_NAME_SIZE);
2460
2461 return 0;
2462}
2463
Stephen Kitt4e1b4d22020-08-21 18:00:35 +02002464static const struct i2c_device_id dme1737_id[];
2465
2466static int dme1737_i2c_probe(struct i2c_client *client)
Jean Delvare67a37302008-10-14 17:30:04 +02002467{
2468 struct dme1737_data *data;
2469 struct device *dev = &client->dev;
2470 int err;
2471
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002472 data = devm_kzalloc(dev, sizeof(struct dme1737_data), GFP_KERNEL);
2473 if (!data)
2474 return -ENOMEM;
Jean Delvare67a37302008-10-14 17:30:04 +02002475
2476 i2c_set_clientdata(client, data);
Stephen Kitt4e1b4d22020-08-21 18:00:35 +02002477 data->type = i2c_match_id(dme1737_id, client)->driver_data;
Jean Delvare67a37302008-10-14 17:30:04 +02002478 data->client = client;
2479 data->name = client->name;
2480 mutex_init(&data->update_lock);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002481
Juerg Haefliger94319962007-06-09 10:11:16 -04002482 /* Initialize the DME1737 chip */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002483 err = dme1737_init_device(dev);
2484 if (err) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002485 dev_err(dev, "Failed to initialize device.\n");
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002486 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002487 }
2488
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002489 /* Create sysfs files */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002490 err = dme1737_create_files(dev);
2491 if (err) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002492 dev_err(dev, "Failed to create sysfs files.\n");
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002493 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002494 }
2495
2496 /* Register device */
Mark M. Hoffman62ee3e12007-10-10 22:32:50 -04002497 data->hwmon_dev = hwmon_device_register(dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07002498 if (IS_ERR(data->hwmon_dev)) {
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002499 dev_err(dev, "Failed to register device.\n");
Tony Jones1beeffe2007-08-20 13:46:20 -07002500 err = PTR_ERR(data->hwmon_dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002501 goto exit_remove;
2502 }
2503
Juerg Haefliger94319962007-06-09 10:11:16 -04002504 return 0;
2505
2506exit_remove:
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002507 dme1737_remove_files(dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002508 return err;
2509}
2510
Jean Delvare67a37302008-10-14 17:30:04 +02002511static int dme1737_i2c_remove(struct i2c_client *client)
Juerg Haefliger94319962007-06-09 10:11:16 -04002512{
2513 struct dme1737_data *data = i2c_get_clientdata(client);
Juerg Haefliger94319962007-06-09 10:11:16 -04002514
Tony Jones1beeffe2007-08-20 13:46:20 -07002515 hwmon_device_unregister(data->hwmon_dev);
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002516 dme1737_remove_files(&client->dev);
Juerg Haefliger94319962007-06-09 10:11:16 -04002517
Juerg Haefliger94319962007-06-09 10:11:16 -04002518 return 0;
2519}
2520
Jean Delvare67a37302008-10-14 17:30:04 +02002521static const struct i2c_device_id dme1737_id[] = {
2522 { "dme1737", dme1737 },
2523 { "sch5027", sch5027 },
2524 { }
2525};
2526MODULE_DEVICE_TABLE(i2c, dme1737_id);
2527
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002528static struct i2c_driver dme1737_i2c_driver = {
Jean Delvare67a37302008-10-14 17:30:04 +02002529 .class = I2C_CLASS_HWMON,
Juerg Haefliger94319962007-06-09 10:11:16 -04002530 .driver = {
2531 .name = "dme1737",
2532 },
Stephen Kitt4e1b4d22020-08-21 18:00:35 +02002533 .probe_new = dme1737_i2c_probe,
Jean Delvare67a37302008-10-14 17:30:04 +02002534 .remove = dme1737_i2c_remove,
2535 .id_table = dme1737_id,
2536 .detect = dme1737_i2c_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01002537 .address_list = normal_i2c,
Juerg Haefliger94319962007-06-09 10:11:16 -04002538};
2539
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002540/* ---------------------------------------------------------------------
Juerg Haefligere95c2372007-10-07 21:27:35 -07002541 * ISA device detection and registration
2542 * --------------------------------------------------------------------- */
2543
2544static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2545{
2546 int err = 0, reg;
2547 unsigned short base_addr;
2548
2549 dme1737_sio_enter(sio_cip);
2550
Guenter Roeckc8de8362012-01-14 13:25:01 -08002551 /*
2552 * Check device ID
2553 * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127
2554 */
Jean Delvare67b671b2007-12-06 23:13:42 +01002555 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
Juerg Haefligerea694432010-05-27 19:59:01 +02002556 if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
2557 reg == SCH5127_ID)) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002558 err = -ENODEV;
2559 goto exit;
2560 }
2561
2562 /* Select logical device A (runtime registers) */
2563 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2564
2565 /* Get the base address of the runtime registers */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002566 base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2567 dme1737_sio_inb(sio_cip, 0x61);
2568 if (!base_addr) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002569 pr_err("Base address not set\n");
Juerg Haefligere95c2372007-10-07 21:27:35 -07002570 err = -ENODEV;
2571 goto exit;
2572 }
2573
Guenter Roeckc8de8362012-01-14 13:25:01 -08002574 /*
2575 * Access to the hwmon registers is through an index/data register
2576 * pair located at offset 0x70/0x71.
2577 */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002578 *addr = base_addr + 0x70;
2579
2580exit:
2581 dme1737_sio_exit(sio_cip);
2582 return err;
2583}
2584
2585static int __init dme1737_isa_device_add(unsigned short addr)
2586{
2587 struct resource res = {
2588 .start = addr,
2589 .end = addr + DME1737_EXTENT - 1,
2590 .name = "dme1737",
2591 .flags = IORESOURCE_IO,
2592 };
2593 int err;
2594
Jean Delvareb9acb642009-01-07 16:37:35 +01002595 err = acpi_check_resource_conflict(&res);
2596 if (err)
2597 goto exit;
2598
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002599 pdev = platform_device_alloc("dme1737", addr);
2600 if (!pdev) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002601 pr_err("Failed to allocate device\n");
Juerg Haefligere95c2372007-10-07 21:27:35 -07002602 err = -ENOMEM;
2603 goto exit;
2604 }
2605
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002606 err = platform_device_add_resources(pdev, &res, 1);
2607 if (err) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002608 pr_err("Failed to add device resource (err = %d)\n", err);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002609 goto exit_device_put;
2610 }
2611
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002612 err = platform_device_add(pdev);
2613 if (err) {
Joe Perches9c6e13b2010-10-20 06:51:32 +00002614 pr_err("Failed to add device (err = %d)\n", err);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002615 goto exit_device_put;
2616 }
2617
2618 return 0;
2619
2620exit_device_put:
2621 platform_device_put(pdev);
2622 pdev = NULL;
2623exit:
2624 return err;
2625}
2626
Bill Pemberton6c931ae2012-11-19 13:22:35 -05002627static int dme1737_isa_probe(struct platform_device *pdev)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002628{
2629 u8 company, device;
2630 struct resource *res;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002631 struct dme1737_data *data;
2632 struct device *dev = &pdev->dev;
2633 int err;
2634
2635 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002636 if (!devm_request_region(dev, res->start, DME1737_EXTENT, "dme1737")) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002637 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2638 (unsigned short)res->start,
2639 (unsigned short)res->start + DME1737_EXTENT - 1);
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002640 return -EBUSY;
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002641 }
Juerg Haefligere95c2372007-10-07 21:27:35 -07002642
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002643 data = devm_kzalloc(dev, sizeof(struct dme1737_data), GFP_KERNEL);
2644 if (!data)
2645 return -ENOMEM;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002646
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002647 data->addr = res->start;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002648 platform_set_drvdata(pdev, data);
2649
Juerg Haefliger55d68d72008-08-06 22:41:03 +02002650 /* Skip chip detection if module is loaded with force_id parameter */
Juerg Haefligerea694432010-05-27 19:59:01 +02002651 switch (force_id) {
2652 case SCH3112_ID:
2653 case SCH3114_ID:
2654 case SCH3116_ID:
2655 data->type = sch311x;
2656 break;
2657 case SCH5127_ID:
2658 data->type = sch5127;
2659 break;
2660 default:
Jean Delvaredbc2bc22008-10-14 17:30:04 +02002661 company = dme1737_read(data, DME1737_REG_COMPANY);
2662 device = dme1737_read(data, DME1737_REG_DEVICE);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002663
Juerg Haefligerea694432010-05-27 19:59:01 +02002664 if ((company == DME1737_COMPANY_SMSC) &&
2665 (device == SCH311X_DEVICE)) {
2666 data->type = sch311x;
2667 } else if ((company == DME1737_COMPANY_SMSC) &&
2668 (device == SCH5127_DEVICE)) {
2669 data->type = sch5127;
2670 } else {
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002671 return -ENODEV;
Juerg Haefliger55d68d72008-08-06 22:41:03 +02002672 }
Juerg Haefligere95c2372007-10-07 21:27:35 -07002673 }
2674
Guenter Roeckc8de8362012-01-14 13:25:01 -08002675 if (data->type == sch5127)
Juerg Haefligerea694432010-05-27 19:59:01 +02002676 data->name = "sch5127";
Guenter Roeckc8de8362012-01-14 13:25:01 -08002677 else
Juerg Haefligerea694432010-05-27 19:59:01 +02002678 data->name = "sch311x";
Juerg Haefligerea694432010-05-27 19:59:01 +02002679
2680 /* Initialize the mutex */
Juerg Haefligere95c2372007-10-07 21:27:35 -07002681 mutex_init(&data->update_lock);
2682
Juerg Haefligerea694432010-05-27 19:59:01 +02002683 dev_info(dev, "Found a %s chip at 0x%04x\n",
2684 data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002685
2686 /* Initialize the chip */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002687 err = dme1737_init_device(dev);
2688 if (err) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002689 dev_err(dev, "Failed to initialize device.\n");
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002690 return err;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002691 }
2692
2693 /* Create sysfs files */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002694 err = dme1737_create_files(dev);
2695 if (err) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002696 dev_err(dev, "Failed to create sysfs files.\n");
Guenter Roeck805fd8c2012-06-02 09:58:03 -07002697 return err;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002698 }
2699
2700 /* Register device */
2701 data->hwmon_dev = hwmon_device_register(dev);
2702 if (IS_ERR(data->hwmon_dev)) {
2703 dev_err(dev, "Failed to register device.\n");
2704 err = PTR_ERR(data->hwmon_dev);
2705 goto exit_remove_files;
2706 }
2707
2708 return 0;
2709
2710exit_remove_files:
2711 dme1737_remove_files(dev);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002712 return err;
2713}
2714
Bill Pemberton281dfd02012-11-19 13:25:51 -05002715static int dme1737_isa_remove(struct platform_device *pdev)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002716{
2717 struct dme1737_data *data = platform_get_drvdata(pdev);
2718
2719 hwmon_device_unregister(data->hwmon_dev);
2720 dme1737_remove_files(&pdev->dev);
Juerg Haefligere95c2372007-10-07 21:27:35 -07002721
2722 return 0;
2723}
2724
2725static struct platform_driver dme1737_isa_driver = {
2726 .driver = {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002727 .name = "dme1737",
2728 },
2729 .probe = dme1737_isa_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05002730 .remove = dme1737_isa_remove,
Juerg Haefligere95c2372007-10-07 21:27:35 -07002731};
2732
2733/* ---------------------------------------------------------------------
Juerg Haefliger67e2f322007-10-01 21:20:28 -07002734 * Module initialization and cleanup
2735 * --------------------------------------------------------------------- */
2736
Juerg Haefliger94319962007-06-09 10:11:16 -04002737static int __init dme1737_init(void)
2738{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002739 int err;
2740 unsigned short addr;
2741
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002742 err = i2c_add_driver(&dme1737_i2c_driver);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002743 if (err)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002744 goto exit;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002745
2746 if (dme1737_isa_detect(0x2e, &addr) &&
Juerg Haefliger92430b62008-04-03 21:34:19 -07002747 dme1737_isa_detect(0x4e, &addr) &&
2748 (!probe_all_addr ||
2749 (dme1737_isa_detect(0x162e, &addr) &&
2750 dme1737_isa_detect(0x164e, &addr)))) {
Juerg Haefligere95c2372007-10-07 21:27:35 -07002751 /* Return 0 if we didn't find an ISA device */
2752 return 0;
2753 }
2754
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002755 err = platform_driver_register(&dme1737_isa_driver);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002756 if (err)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002757 goto exit_del_i2c_driver;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002758
2759 /* Sets global pdev as a side effect */
Juerg Haefliger06f3d9f2011-01-12 21:55:13 +01002760 err = dme1737_isa_device_add(addr);
Guenter Roeckc8de8362012-01-14 13:25:01 -08002761 if (err)
Juerg Haefligere95c2372007-10-07 21:27:35 -07002762 goto exit_del_isa_driver;
Juerg Haefligere95c2372007-10-07 21:27:35 -07002763
2764 return 0;
2765
2766exit_del_isa_driver:
2767 platform_driver_unregister(&dme1737_isa_driver);
2768exit_del_i2c_driver:
2769 i2c_del_driver(&dme1737_i2c_driver);
2770exit:
2771 return err;
Juerg Haefliger94319962007-06-09 10:11:16 -04002772}
2773
2774static void __exit dme1737_exit(void)
2775{
Juerg Haefligere95c2372007-10-07 21:27:35 -07002776 if (pdev) {
2777 platform_device_unregister(pdev);
2778 platform_driver_unregister(&dme1737_isa_driver);
2779 }
2780
Juerg Haefligerb237eb22007-10-01 21:19:04 -07002781 i2c_del_driver(&dme1737_i2c_driver);
Juerg Haefliger94319962007-06-09 10:11:16 -04002782}
2783
2784MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2785MODULE_DESCRIPTION("DME1737 sensors");
2786MODULE_LICENSE("GPL");
2787
2788module_init(dme1737_init);
2789module_exit(dme1737_exit);