xref: /linux/drivers/hwmon/dme1737.c (revision 2a844c148e1f714ebf42cb96e1b172ce394c36c9)
19431996fSJuerg Haefliger /*
2ea694431SJuerg Haefliger  * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x, SCH5027,
3ea694431SJuerg Haefliger  *             and SCH5127 Super-I/O chips integrated hardware monitoring
4ea694431SJuerg Haefliger  *             features.
5ea694431SJuerg Haefliger  * Copyright (c) 2007, 2008, 2009, 2010 Juerg Haefliger <juergh@gmail.com>
69431996fSJuerg Haefliger  *
7e95c237dSJuerg Haefliger  * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
8549edb83SJuerg Haefliger  * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
9ea694431SJuerg Haefliger  * if a SCH311x or SCH5127 chip is found. Both types of chips have very
10ea694431SJuerg Haefliger  * similar hardware monitoring capabilities but differ in the way they can be
11ea694431SJuerg Haefliger  * accessed.
129431996fSJuerg Haefliger  *
139431996fSJuerg Haefliger  * This program is free software; you can redistribute it and/or modify
149431996fSJuerg Haefliger  * it under the terms of the GNU General Public License as published by
159431996fSJuerg Haefliger  * the Free Software Foundation; either version 2 of the License, or
169431996fSJuerg Haefliger  * (at your option) any later version.
179431996fSJuerg Haefliger  *
189431996fSJuerg Haefliger  * This program is distributed in the hope that it will be useful,
199431996fSJuerg Haefliger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
209431996fSJuerg Haefliger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
219431996fSJuerg Haefliger  * GNU General Public License for more details.
229431996fSJuerg Haefliger  *
239431996fSJuerg Haefliger  * You should have received a copy of the GNU General Public License
249431996fSJuerg Haefliger  * along with this program; if not, write to the Free Software
259431996fSJuerg Haefliger  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
269431996fSJuerg Haefliger  */
279431996fSJuerg Haefliger 
289c6e13b4SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
299c6e13b4SJoe Perches 
309431996fSJuerg Haefliger #include <linux/module.h>
319431996fSJuerg Haefliger #include <linux/init.h>
329431996fSJuerg Haefliger #include <linux/slab.h>
339431996fSJuerg Haefliger #include <linux/jiffies.h>
349431996fSJuerg Haefliger #include <linux/i2c.h>
35e95c237dSJuerg Haefliger #include <linux/platform_device.h>
369431996fSJuerg Haefliger #include <linux/hwmon.h>
379431996fSJuerg Haefliger #include <linux/hwmon-sysfs.h>
389431996fSJuerg Haefliger #include <linux/hwmon-vid.h>
399431996fSJuerg Haefliger #include <linux/err.h>
409431996fSJuerg Haefliger #include <linux/mutex.h>
41b9acb64aSJean Delvare #include <linux/acpi.h>
426055fae8SH Hartley Sweeten #include <linux/io.h>
439431996fSJuerg Haefliger 
44e95c237dSJuerg Haefliger /* ISA device, if found */
45e95c237dSJuerg Haefliger static struct platform_device *pdev;
46e95c237dSJuerg Haefliger 
479431996fSJuerg Haefliger /* Module load parameters */
4890ab5ee9SRusty Russell static bool force_start;
499431996fSJuerg Haefliger module_param(force_start, bool, 0);
509431996fSJuerg Haefliger MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
519431996fSJuerg Haefliger 
5267b671bcSJean Delvare static unsigned short force_id;
5367b671bcSJean Delvare module_param(force_id, ushort, 0);
5467b671bcSJean Delvare MODULE_PARM_DESC(force_id, "Override the detected device ID");
5567b671bcSJean Delvare 
5690ab5ee9SRusty Russell static bool probe_all_addr;
5792430b6fSJuerg Haefliger module_param(probe_all_addr, bool, 0);
5892430b6fSJuerg Haefliger MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
5992430b6fSJuerg Haefliger 		 "addresses");
6092430b6fSJuerg Haefliger 
619431996fSJuerg Haefliger /* Addresses to scan */
6225e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
639431996fSJuerg Haefliger 
64ea694431SJuerg Haefliger enum chips { dme1737, sch5027, sch311x, sch5127 };
659431996fSJuerg Haefliger 
669431996fSJuerg Haefliger /* ---------------------------------------------------------------------
679431996fSJuerg Haefliger  * Registers
689431996fSJuerg Haefliger  *
699431996fSJuerg Haefliger  * The sensors are defined as follows:
709431996fSJuerg Haefliger  *
719431996fSJuerg Haefliger  * Voltages                          Temperatures
729431996fSJuerg Haefliger  * --------                          ------------
739431996fSJuerg Haefliger  * in0   +5VTR (+5V stdby)           temp1   Remote diode 1
749431996fSJuerg Haefliger  * in1   Vccp  (proc core)           temp2   Internal temp
759431996fSJuerg Haefliger  * in2   VCC   (internal +3.3V)      temp3   Remote diode 2
769431996fSJuerg Haefliger  * in3   +5V
779431996fSJuerg Haefliger  * in4   +12V
789431996fSJuerg Haefliger  * in5   VTR   (+3.3V stby)
799431996fSJuerg Haefliger  * in6   Vbat
80d4b94e1fSJuerg Haefliger  * in7   Vtrip (sch5127 only)
819431996fSJuerg Haefliger  *
829431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
839431996fSJuerg Haefliger 
84d4b94e1fSJuerg Haefliger /* Voltages (in) numbered 0-7 (ix) */
85d4b94e1fSJuerg Haefliger #define DME1737_REG_IN(ix)		((ix) < 5 ? 0x20 + (ix) : \
86d4b94e1fSJuerg Haefliger 					 (ix) < 7 ? 0x94 + (ix) : \
87d4b94e1fSJuerg Haefliger 						    0x1f)
889431996fSJuerg Haefliger #define DME1737_REG_IN_MIN(ix)		((ix) < 5 ? 0x44 + (ix) * 2 \
899431996fSJuerg Haefliger 						  : 0x91 + (ix) * 2)
909431996fSJuerg Haefliger #define DME1737_REG_IN_MAX(ix)		((ix) < 5 ? 0x45 + (ix) * 2 \
919431996fSJuerg Haefliger 						  : 0x92 + (ix) * 2)
929431996fSJuerg Haefliger 
939431996fSJuerg Haefliger /* Temperatures (temp) numbered 0-2 (ix) */
949431996fSJuerg Haefliger #define DME1737_REG_TEMP(ix)		(0x25 + (ix))
959431996fSJuerg Haefliger #define DME1737_REG_TEMP_MIN(ix)	(0x4e + (ix) * 2)
969431996fSJuerg Haefliger #define DME1737_REG_TEMP_MAX(ix)	(0x4f + (ix) * 2)
979431996fSJuerg Haefliger #define DME1737_REG_TEMP_OFFSET(ix)	((ix) == 0 ? 0x1f \
989431996fSJuerg Haefliger 						   : 0x1c + (ix))
999431996fSJuerg Haefliger 
100c8de8362SGuenter Roeck /*
101c8de8362SGuenter Roeck  * Voltage and temperature LSBs
1029431996fSJuerg Haefliger  * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
1039431996fSJuerg Haefliger  *    IN_TEMP_LSB(0) = [in5, in6]
1049431996fSJuerg Haefliger  *    IN_TEMP_LSB(1) = [temp3, temp1]
1059431996fSJuerg Haefliger  *    IN_TEMP_LSB(2) = [in4, temp2]
1069431996fSJuerg Haefliger  *    IN_TEMP_LSB(3) = [in3, in0]
107d4b94e1fSJuerg Haefliger  *    IN_TEMP_LSB(4) = [in2, in1]
108c8de8362SGuenter Roeck  *    IN_TEMP_LSB(5) = [res, in7]
109c8de8362SGuenter Roeck  */
1109431996fSJuerg Haefliger #define DME1737_REG_IN_TEMP_LSB(ix)	(0x84 + (ix))
111d4b94e1fSJuerg Haefliger static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0, 5};
112d4b94e1fSJuerg Haefliger static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4, 4};
1139431996fSJuerg Haefliger static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
1149431996fSJuerg Haefliger static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
1159431996fSJuerg Haefliger 
1169431996fSJuerg Haefliger /* Fans numbered 0-5 (ix) */
1179431996fSJuerg Haefliger #define DME1737_REG_FAN(ix)		((ix) < 4 ? 0x28 + (ix) * 2 \
1189431996fSJuerg Haefliger 						  : 0xa1 + (ix) * 2)
1199431996fSJuerg Haefliger #define DME1737_REG_FAN_MIN(ix)		((ix) < 4 ? 0x54 + (ix) * 2 \
1209431996fSJuerg Haefliger 						  : 0xa5 + (ix) * 2)
1219431996fSJuerg Haefliger #define DME1737_REG_FAN_OPT(ix)		((ix) < 4 ? 0x90 + (ix) \
1229431996fSJuerg Haefliger 						  : 0xb2 + (ix))
1239431996fSJuerg Haefliger #define DME1737_REG_FAN_MAX(ix)		(0xb4 + (ix)) /* only for fan[4-5] */
1249431996fSJuerg Haefliger 
1259431996fSJuerg Haefliger /* PWMs numbered 0-2, 4-5 (ix) */
1269431996fSJuerg Haefliger #define DME1737_REG_PWM(ix)		((ix) < 3 ? 0x30 + (ix) \
1279431996fSJuerg Haefliger 						  : 0xa1 + (ix))
1289431996fSJuerg Haefliger #define DME1737_REG_PWM_CONFIG(ix)	(0x5c + (ix)) /* only for pwm[0-2] */
1299431996fSJuerg Haefliger #define DME1737_REG_PWM_MIN(ix)		(0x64 + (ix)) /* only for pwm[0-2] */
1309431996fSJuerg Haefliger #define DME1737_REG_PWM_FREQ(ix)	((ix) < 3 ? 0x5f + (ix) \
1319431996fSJuerg Haefliger 						  : 0xa3 + (ix))
132c8de8362SGuenter Roeck /*
133c8de8362SGuenter Roeck  * The layout of the ramp rate registers is different from the other pwm
1349431996fSJuerg Haefliger  * registers. The bits for the 3 PWMs are stored in 2 registers:
1359431996fSJuerg Haefliger  *    PWM_RR(0) = [OFF3, OFF2,  OFF1,  RES,   RR1E, RR1-2, RR1-1, RR1-0]
136c8de8362SGuenter Roeck  *    PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0]
137c8de8362SGuenter Roeck  */
1389431996fSJuerg Haefliger #define DME1737_REG_PWM_RR(ix)		(0x62 + (ix)) /* only for pwm[0-2] */
1399431996fSJuerg Haefliger 
1409431996fSJuerg Haefliger /* Thermal zones 0-2 */
1419431996fSJuerg Haefliger #define DME1737_REG_ZONE_LOW(ix)	(0x67 + (ix))
1429431996fSJuerg Haefliger #define DME1737_REG_ZONE_ABS(ix)	(0x6a + (ix))
143c8de8362SGuenter Roeck /*
144c8de8362SGuenter Roeck  * The layout of the hysteresis registers is different from the other zone
1459431996fSJuerg Haefliger  * registers. The bits for the 3 zones are stored in 2 registers:
1469431996fSJuerg Haefliger  *    ZONE_HYST(0) = [H1-3,  H1-2,  H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
147c8de8362SGuenter Roeck  *    ZONE_HYST(1) = [H3-3,  H3-2,  H3-1, H3-0, RES,  RES,  RES,  RES]
148c8de8362SGuenter Roeck  */
1499431996fSJuerg Haefliger #define DME1737_REG_ZONE_HYST(ix)	(0x6d + (ix))
1509431996fSJuerg Haefliger 
151c8de8362SGuenter Roeck /*
152c8de8362SGuenter Roeck  * Alarm registers and bit mapping
1539431996fSJuerg Haefliger  * The 3 8-bit alarm registers will be concatenated to a single 32-bit
154c8de8362SGuenter Roeck  * alarm value [0, ALARM3, ALARM2, ALARM1].
155c8de8362SGuenter Roeck  */
1569431996fSJuerg Haefliger #define DME1737_REG_ALARM1		0x41
1579431996fSJuerg Haefliger #define DME1737_REG_ALARM2		0x42
1589431996fSJuerg Haefliger #define DME1737_REG_ALARM3		0x83
159d4b94e1fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17, 18};
1609431996fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
1619431996fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
1629431996fSJuerg Haefliger 
1639431996fSJuerg Haefliger /* Miscellaneous registers */
164e95c237dSJuerg Haefliger #define DME1737_REG_DEVICE		0x3d
1659431996fSJuerg Haefliger #define DME1737_REG_COMPANY		0x3e
1669431996fSJuerg Haefliger #define DME1737_REG_VERSTEP		0x3f
1679431996fSJuerg Haefliger #define DME1737_REG_CONFIG		0x40
1689431996fSJuerg Haefliger #define DME1737_REG_CONFIG2		0x7f
1699431996fSJuerg Haefliger #define DME1737_REG_VID			0x43
1709431996fSJuerg Haefliger #define DME1737_REG_TACH_PWM		0x81
1719431996fSJuerg Haefliger 
1729431996fSJuerg Haefliger /* ---------------------------------------------------------------------
1739431996fSJuerg Haefliger  * Misc defines
1749431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
1759431996fSJuerg Haefliger 
1769431996fSJuerg Haefliger /* Chip identification */
1779431996fSJuerg Haefliger #define DME1737_COMPANY_SMSC	0x5c
1789431996fSJuerg Haefliger #define DME1737_VERSTEP		0x88
1799431996fSJuerg Haefliger #define DME1737_VERSTEP_MASK	0xf8
180e95c237dSJuerg Haefliger #define SCH311X_DEVICE		0x8c
181549edb83SJuerg Haefliger #define SCH5027_VERSTEP		0x69
182ea694431SJuerg Haefliger #define SCH5127_DEVICE		0x8e
183ea694431SJuerg Haefliger 
184ea694431SJuerg Haefliger /* Device ID values (global configuration register index 0x20) */
185ea694431SJuerg Haefliger #define DME1737_ID_1	0x77
186ea694431SJuerg Haefliger #define DME1737_ID_2	0x78
187ea694431SJuerg Haefliger #define SCH3112_ID	0x7c
188ea694431SJuerg Haefliger #define SCH3114_ID	0x7d
189ea694431SJuerg Haefliger #define SCH3116_ID	0x7f
190ea694431SJuerg Haefliger #define SCH5027_ID	0x89
191ea694431SJuerg Haefliger #define SCH5127_ID	0x86
192e95c237dSJuerg Haefliger 
193e95c237dSJuerg Haefliger /* Length of ISA address segment */
194e95c237dSJuerg Haefliger #define DME1737_EXTENT	2
1959431996fSJuerg Haefliger 
196ea694431SJuerg Haefliger /* chip-dependent features */
197ea694431SJuerg Haefliger #define HAS_TEMP_OFFSET		(1 << 0)		/* bit 0 */
198ea694431SJuerg Haefliger #define HAS_VID			(1 << 1)		/* bit 1 */
199ea694431SJuerg Haefliger #define HAS_ZONE3		(1 << 2)		/* bit 2 */
200ea694431SJuerg Haefliger #define HAS_ZONE_HYST		(1 << 3)		/* bit 3 */
201ea694431SJuerg Haefliger #define HAS_PWM_MIN		(1 << 4)		/* bit 4 */
202ea694431SJuerg Haefliger #define HAS_FAN(ix)		(1 << ((ix) + 5))	/* bits 5-10 */
203ea694431SJuerg Haefliger #define HAS_PWM(ix)		(1 << ((ix) + 11))	/* bits 11-16 */
204d4b94e1fSJuerg Haefliger #define HAS_IN7			(1 << 17)		/* bit 17 */
205ea694431SJuerg Haefliger 
2069431996fSJuerg Haefliger /* ---------------------------------------------------------------------
2079431996fSJuerg Haefliger  * Data structures and manipulation thereof
2089431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
2099431996fSJuerg Haefliger 
2109431996fSJuerg Haefliger struct dme1737_data {
211dbc2bc25SJean Delvare 	struct i2c_client *client;	/* for I2C devices only */
2121beeffe4STony Jones 	struct device *hwmon_dev;
213dbc2bc25SJean Delvare 	const char *name;
214dbc2bc25SJean Delvare 	unsigned int addr;		/* for ISA devices only */
2159431996fSJuerg Haefliger 
2169431996fSJuerg Haefliger 	struct mutex update_lock;
2179431996fSJuerg Haefliger 	int valid;			/* !=0 if following fields are valid */
2189431996fSJuerg Haefliger 	unsigned long last_update;	/* in jiffies */
2199431996fSJuerg Haefliger 	unsigned long last_vbat;	/* in jiffies */
220f994fb23SJuerg Haefliger 	enum chips type;
221549edb83SJuerg Haefliger 	const int *in_nominal;		/* pointer to IN_NOMINAL array */
2229431996fSJuerg Haefliger 
2239431996fSJuerg Haefliger 	u8 vid;
2249431996fSJuerg Haefliger 	u8 pwm_rr_en;
225ea694431SJuerg Haefliger 	u32 has_features;
2269431996fSJuerg Haefliger 
2279431996fSJuerg Haefliger 	/* Register values */
228d4b94e1fSJuerg Haefliger 	u16 in[8];
229d4b94e1fSJuerg Haefliger 	u8  in_min[8];
230d4b94e1fSJuerg Haefliger 	u8  in_max[8];
2319431996fSJuerg Haefliger 	s16 temp[3];
2329431996fSJuerg Haefliger 	s8  temp_min[3];
2339431996fSJuerg Haefliger 	s8  temp_max[3];
2349431996fSJuerg Haefliger 	s8  temp_offset[3];
2359431996fSJuerg Haefliger 	u8  config;
2369431996fSJuerg Haefliger 	u8  config2;
2379431996fSJuerg Haefliger 	u8  vrm;
2389431996fSJuerg Haefliger 	u16 fan[6];
2399431996fSJuerg Haefliger 	u16 fan_min[6];
2409431996fSJuerg Haefliger 	u8  fan_max[2];
2419431996fSJuerg Haefliger 	u8  fan_opt[6];
2429431996fSJuerg Haefliger 	u8  pwm[6];
2439431996fSJuerg Haefliger 	u8  pwm_min[3];
2449431996fSJuerg Haefliger 	u8  pwm_config[3];
2459431996fSJuerg Haefliger 	u8  pwm_acz[3];
2469431996fSJuerg Haefliger 	u8  pwm_freq[6];
2479431996fSJuerg Haefliger 	u8  pwm_rr[2];
2489431996fSJuerg Haefliger 	u8  zone_low[3];
2499431996fSJuerg Haefliger 	u8  zone_abs[3];
2509431996fSJuerg Haefliger 	u8  zone_hyst[2];
2519431996fSJuerg Haefliger 	u32 alarms;
2529431996fSJuerg Haefliger };
2539431996fSJuerg Haefliger 
2549431996fSJuerg Haefliger /* Nominal voltage values */
255f994fb23SJuerg Haefliger static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
256f994fb23SJuerg Haefliger 					 3300};
257f994fb23SJuerg Haefliger static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
258f994fb23SJuerg Haefliger 					 3300};
259549edb83SJuerg Haefliger static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
260549edb83SJuerg Haefliger 					 3300};
261ea694431SJuerg Haefliger static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
262d4b94e1fSJuerg Haefliger 					 3300, 1500};
263549edb83SJuerg Haefliger #define IN_NOMINAL(type)	((type) == sch311x ? IN_NOMINAL_SCH311x : \
264549edb83SJuerg Haefliger 				 (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
265ea694431SJuerg Haefliger 				 (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
266549edb83SJuerg Haefliger 				 IN_NOMINAL_DME1737)
2679431996fSJuerg Haefliger 
268c8de8362SGuenter Roeck /*
269c8de8362SGuenter Roeck  * Voltage input
2709431996fSJuerg Haefliger  * Voltage inputs have 16 bits resolution, limit values have 8 bits
271c8de8362SGuenter Roeck  * resolution.
272c8de8362SGuenter Roeck  */
273549edb83SJuerg Haefliger static inline int IN_FROM_REG(int reg, int nominal, int res)
2749431996fSJuerg Haefliger {
275549edb83SJuerg Haefliger 	return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
2769431996fSJuerg Haefliger }
2779431996fSJuerg Haefliger 
278549edb83SJuerg Haefliger static inline int IN_TO_REG(int val, int nominal)
2799431996fSJuerg Haefliger {
280*2a844c14SGuenter Roeck 	return clamp_val((val * 192 + nominal / 2) / nominal, 0, 255);
2819431996fSJuerg Haefliger }
2829431996fSJuerg Haefliger 
283c8de8362SGuenter Roeck /*
284c8de8362SGuenter Roeck  * Temperature input
2859431996fSJuerg Haefliger  * The register values represent temperatures in 2's complement notation from
2869431996fSJuerg Haefliger  * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
287c8de8362SGuenter Roeck  * values have 8 bits resolution.
288c8de8362SGuenter Roeck  */
2899431996fSJuerg Haefliger static inline int TEMP_FROM_REG(int reg, int res)
2909431996fSJuerg Haefliger {
2919431996fSJuerg Haefliger 	return (reg * 1000) >> (res - 8);
2929431996fSJuerg Haefliger }
2939431996fSJuerg Haefliger 
2949431996fSJuerg Haefliger static inline int TEMP_TO_REG(int val)
2959431996fSJuerg Haefliger {
296*2a844c14SGuenter Roeck 	return clamp_val((val < 0 ? val - 500 : val + 500) / 1000, -128, 127);
2979431996fSJuerg Haefliger }
2989431996fSJuerg Haefliger 
2999431996fSJuerg Haefliger /* Temperature range */
3009431996fSJuerg Haefliger static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
3019431996fSJuerg Haefliger 				 10000, 13333, 16000, 20000, 26666, 32000,
3029431996fSJuerg Haefliger 				 40000, 53333, 80000};
3039431996fSJuerg Haefliger 
3049431996fSJuerg Haefliger static inline int TEMP_RANGE_FROM_REG(int reg)
3059431996fSJuerg Haefliger {
3069431996fSJuerg Haefliger 	return TEMP_RANGE[(reg >> 4) & 0x0f];
3079431996fSJuerg Haefliger }
3089431996fSJuerg Haefliger 
3099431996fSJuerg Haefliger static int TEMP_RANGE_TO_REG(int val, int reg)
3109431996fSJuerg Haefliger {
3119431996fSJuerg Haefliger 	int i;
3129431996fSJuerg Haefliger 
3139431996fSJuerg Haefliger 	for (i = 15; i > 0; i--) {
314c8de8362SGuenter Roeck 		if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2)
3159431996fSJuerg Haefliger 			break;
3169431996fSJuerg Haefliger 	}
3179431996fSJuerg Haefliger 
3189431996fSJuerg Haefliger 	return (reg & 0x0f) | (i << 4);
3199431996fSJuerg Haefliger }
3209431996fSJuerg Haefliger 
321c8de8362SGuenter Roeck /*
322c8de8362SGuenter Roeck  * Temperature hysteresis
3239431996fSJuerg Haefliger  * Register layout:
3249431996fSJuerg Haefliger  *    reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
325c8de8362SGuenter Roeck  *    reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx]
326c8de8362SGuenter Roeck  */
3279431996fSJuerg Haefliger static inline int TEMP_HYST_FROM_REG(int reg, int ix)
3289431996fSJuerg Haefliger {
3299431996fSJuerg Haefliger 	return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
3309431996fSJuerg Haefliger }
3319431996fSJuerg Haefliger 
3329431996fSJuerg Haefliger static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
3339431996fSJuerg Haefliger {
334*2a844c14SGuenter Roeck 	int hyst = clamp_val((val + 500) / 1000, 0, 15);
3359431996fSJuerg Haefliger 
3369431996fSJuerg Haefliger 	return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
3379431996fSJuerg Haefliger }
3389431996fSJuerg Haefliger 
3399431996fSJuerg Haefliger /* Fan input RPM */
3409431996fSJuerg Haefliger static inline int FAN_FROM_REG(int reg, int tpc)
3419431996fSJuerg Haefliger {
342c8de8362SGuenter Roeck 	if (tpc)
343ff8421f7SJuerg Haefliger 		return tpc * reg;
344c8de8362SGuenter Roeck 	else
345ff8421f7SJuerg Haefliger 		return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
346ff8421f7SJuerg Haefliger }
3479431996fSJuerg Haefliger 
3489431996fSJuerg Haefliger static inline int FAN_TO_REG(int val, int tpc)
3499431996fSJuerg Haefliger {
350ff8421f7SJuerg Haefliger 	if (tpc) {
351*2a844c14SGuenter Roeck 		return clamp_val(val / tpc, 0, 0xffff);
352ff8421f7SJuerg Haefliger 	} else {
353ff8421f7SJuerg Haefliger 		return (val <= 0) ? 0xffff :
354*2a844c14SGuenter Roeck 			clamp_val(90000 * 60 / val, 0, 0xfffe);
355ff8421f7SJuerg Haefliger 	}
3569431996fSJuerg Haefliger }
3579431996fSJuerg Haefliger 
358c8de8362SGuenter Roeck /*
359c8de8362SGuenter Roeck  * Fan TPC (tach pulse count)
3609431996fSJuerg Haefliger  * Converts a register value to a TPC multiplier or returns 0 if the tachometer
361c8de8362SGuenter Roeck  * is configured in legacy (non-tpc) mode
362c8de8362SGuenter Roeck  */
3639431996fSJuerg Haefliger static inline int FAN_TPC_FROM_REG(int reg)
3649431996fSJuerg Haefliger {
3659431996fSJuerg Haefliger 	return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
3669431996fSJuerg Haefliger }
3679431996fSJuerg Haefliger 
368c8de8362SGuenter Roeck /*
369c8de8362SGuenter Roeck  * Fan type
3709431996fSJuerg Haefliger  * The type of a fan is expressed in number of pulses-per-revolution that it
371c8de8362SGuenter Roeck  * emits
372c8de8362SGuenter Roeck  */
3739431996fSJuerg Haefliger static inline int FAN_TYPE_FROM_REG(int reg)
3749431996fSJuerg Haefliger {
3759431996fSJuerg Haefliger 	int edge = (reg >> 1) & 0x03;
3769431996fSJuerg Haefliger 
3779431996fSJuerg Haefliger 	return (edge > 0) ? 1 << (edge - 1) : 0;
3789431996fSJuerg Haefliger }
3799431996fSJuerg Haefliger 
3809431996fSJuerg Haefliger static inline int FAN_TYPE_TO_REG(int val, int reg)
3819431996fSJuerg Haefliger {
3829431996fSJuerg Haefliger 	int edge = (val == 4) ? 3 : val;
3839431996fSJuerg Haefliger 
3849431996fSJuerg Haefliger 	return (reg & 0xf9) | (edge << 1);
3859431996fSJuerg Haefliger }
3869431996fSJuerg Haefliger 
3879431996fSJuerg Haefliger /* Fan max RPM */
3889431996fSJuerg Haefliger static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
3899431996fSJuerg Haefliger 			      0x11, 0x0f, 0x0e};
3909431996fSJuerg Haefliger 
3919431996fSJuerg Haefliger static int FAN_MAX_FROM_REG(int reg)
3929431996fSJuerg Haefliger {
3939431996fSJuerg Haefliger 	int i;
3949431996fSJuerg Haefliger 
3959431996fSJuerg Haefliger 	for (i = 10; i > 0; i--) {
396c8de8362SGuenter Roeck 		if (reg == FAN_MAX[i])
3979431996fSJuerg Haefliger 			break;
3989431996fSJuerg Haefliger 	}
3999431996fSJuerg Haefliger 
4009431996fSJuerg Haefliger 	return 1000 + i * 500;
4019431996fSJuerg Haefliger }
4029431996fSJuerg Haefliger 
4039431996fSJuerg Haefliger static int FAN_MAX_TO_REG(int val)
4049431996fSJuerg Haefliger {
4059431996fSJuerg Haefliger 	int i;
4069431996fSJuerg Haefliger 
4079431996fSJuerg Haefliger 	for (i = 10; i > 0; i--) {
408c8de8362SGuenter Roeck 		if (val > (1000 + (i - 1) * 500))
4099431996fSJuerg Haefliger 			break;
4109431996fSJuerg Haefliger 	}
4119431996fSJuerg Haefliger 
4129431996fSJuerg Haefliger 	return FAN_MAX[i];
4139431996fSJuerg Haefliger }
4149431996fSJuerg Haefliger 
415c8de8362SGuenter Roeck /*
416c8de8362SGuenter Roeck  * PWM enable
4179431996fSJuerg Haefliger  * Register to enable mapping:
4189431996fSJuerg Haefliger  * 000:  2  fan on zone 1 auto
4199431996fSJuerg Haefliger  * 001:  2  fan on zone 2 auto
4209431996fSJuerg Haefliger  * 010:  2  fan on zone 3 auto
4219431996fSJuerg Haefliger  * 011:  0  fan full on
4229431996fSJuerg Haefliger  * 100: -1  fan disabled
4239431996fSJuerg Haefliger  * 101:  2  fan on hottest of zones 2,3 auto
4249431996fSJuerg Haefliger  * 110:  2  fan on hottest of zones 1,2,3 auto
425c8de8362SGuenter Roeck  * 111:  1  fan in manual mode
426c8de8362SGuenter Roeck  */
4279431996fSJuerg Haefliger static inline int PWM_EN_FROM_REG(int reg)
4289431996fSJuerg Haefliger {
4299431996fSJuerg Haefliger 	static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
4309431996fSJuerg Haefliger 
4319431996fSJuerg Haefliger 	return en[(reg >> 5) & 0x07];
4329431996fSJuerg Haefliger }
4339431996fSJuerg Haefliger 
4349431996fSJuerg Haefliger static inline int PWM_EN_TO_REG(int val, int reg)
4359431996fSJuerg Haefliger {
4369431996fSJuerg Haefliger 	int en = (val == 1) ? 7 : 3;
4379431996fSJuerg Haefliger 
4389431996fSJuerg Haefliger 	return (reg & 0x1f) | ((en & 0x07) << 5);
4399431996fSJuerg Haefliger }
4409431996fSJuerg Haefliger 
441c8de8362SGuenter Roeck /*
442c8de8362SGuenter Roeck  * PWM auto channels zone
4439431996fSJuerg Haefliger  * Register to auto channels zone mapping (ACZ is a bitfield with bit x
4449431996fSJuerg Haefliger  * corresponding to zone x+1):
4459431996fSJuerg Haefliger  * 000: 001  fan on zone 1 auto
4469431996fSJuerg Haefliger  * 001: 010  fan on zone 2 auto
4479431996fSJuerg Haefliger  * 010: 100  fan on zone 3 auto
4489431996fSJuerg Haefliger  * 011: 000  fan full on
4499431996fSJuerg Haefliger  * 100: 000  fan disabled
4509431996fSJuerg Haefliger  * 101: 110  fan on hottest of zones 2,3 auto
4519431996fSJuerg Haefliger  * 110: 111  fan on hottest of zones 1,2,3 auto
452c8de8362SGuenter Roeck  * 111: 000  fan in manual mode
453c8de8362SGuenter Roeck  */
4549431996fSJuerg Haefliger static inline int PWM_ACZ_FROM_REG(int reg)
4559431996fSJuerg Haefliger {
4569431996fSJuerg Haefliger 	static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
4579431996fSJuerg Haefliger 
4589431996fSJuerg Haefliger 	return acz[(reg >> 5) & 0x07];
4599431996fSJuerg Haefliger }
4609431996fSJuerg Haefliger 
4619431996fSJuerg Haefliger static inline int PWM_ACZ_TO_REG(int val, int reg)
4629431996fSJuerg Haefliger {
4639431996fSJuerg Haefliger 	int acz = (val == 4) ? 2 : val - 1;
4649431996fSJuerg Haefliger 
4659431996fSJuerg Haefliger 	return (reg & 0x1f) | ((acz & 0x07) << 5);
4669431996fSJuerg Haefliger }
4679431996fSJuerg Haefliger 
4689431996fSJuerg Haefliger /* PWM frequency */
4699431996fSJuerg Haefliger static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
4709431996fSJuerg Haefliger 			       15000, 20000, 30000, 25000, 0, 0, 0, 0};
4719431996fSJuerg Haefliger 
4729431996fSJuerg Haefliger static inline int PWM_FREQ_FROM_REG(int reg)
4739431996fSJuerg Haefliger {
4749431996fSJuerg Haefliger 	return PWM_FREQ[reg & 0x0f];
4759431996fSJuerg Haefliger }
4769431996fSJuerg Haefliger 
4779431996fSJuerg Haefliger static int PWM_FREQ_TO_REG(int val, int reg)
4789431996fSJuerg Haefliger {
4799431996fSJuerg Haefliger 	int i;
4809431996fSJuerg Haefliger 
4819431996fSJuerg Haefliger 	/* the first two cases are special - stupid chip design! */
4829431996fSJuerg Haefliger 	if (val > 27500) {
4839431996fSJuerg Haefliger 		i = 10;
4849431996fSJuerg Haefliger 	} else if (val > 22500) {
4859431996fSJuerg Haefliger 		i = 11;
4869431996fSJuerg Haefliger 	} else {
4879431996fSJuerg Haefliger 		for (i = 9; i > 0; i--) {
488c8de8362SGuenter Roeck 			if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2)
4899431996fSJuerg Haefliger 				break;
4909431996fSJuerg Haefliger 		}
4919431996fSJuerg Haefliger 	}
4929431996fSJuerg Haefliger 
4939431996fSJuerg Haefliger 	return (reg & 0xf0) | i;
4949431996fSJuerg Haefliger }
4959431996fSJuerg Haefliger 
496c8de8362SGuenter Roeck /*
497c8de8362SGuenter Roeck  * PWM ramp rate
4989431996fSJuerg Haefliger  * Register layout:
4999431996fSJuerg Haefliger  *    reg[0] = [OFF3,  OFF2,  OFF1,  RES,   RR1-E, RR1-2, RR1-1, RR1-0]
500c8de8362SGuenter Roeck  *    reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0]
501c8de8362SGuenter Roeck  */
5029431996fSJuerg Haefliger static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
5039431996fSJuerg Haefliger 
5049431996fSJuerg Haefliger static inline int PWM_RR_FROM_REG(int reg, int ix)
5059431996fSJuerg Haefliger {
5069431996fSJuerg Haefliger 	int rr = (ix == 1) ? reg >> 4 : reg;
5079431996fSJuerg Haefliger 
5089431996fSJuerg Haefliger 	return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
5099431996fSJuerg Haefliger }
5109431996fSJuerg Haefliger 
5119431996fSJuerg Haefliger static int PWM_RR_TO_REG(int val, int ix, int reg)
5129431996fSJuerg Haefliger {
5139431996fSJuerg Haefliger 	int i;
5149431996fSJuerg Haefliger 
5159431996fSJuerg Haefliger 	for (i = 0; i < 7; i++) {
516c8de8362SGuenter Roeck 		if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2)
5179431996fSJuerg Haefliger 			break;
5189431996fSJuerg Haefliger 	}
5199431996fSJuerg Haefliger 
5209431996fSJuerg Haefliger 	return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
5219431996fSJuerg Haefliger }
5229431996fSJuerg Haefliger 
5239431996fSJuerg Haefliger /* PWM ramp rate enable */
5249431996fSJuerg Haefliger static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
5259431996fSJuerg Haefliger {
5269431996fSJuerg Haefliger 	return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
5279431996fSJuerg Haefliger }
5289431996fSJuerg Haefliger 
5299431996fSJuerg Haefliger static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
5309431996fSJuerg Haefliger {
5319431996fSJuerg Haefliger 	int en = (ix == 1) ? 0x80 : 0x08;
5329431996fSJuerg Haefliger 
5339431996fSJuerg Haefliger 	return val ? reg | en : reg & ~en;
5349431996fSJuerg Haefliger }
5359431996fSJuerg Haefliger 
536c8de8362SGuenter Roeck /*
537c8de8362SGuenter Roeck  * PWM min/off
5389431996fSJuerg Haefliger  * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
539c8de8362SGuenter Roeck  * the register layout).
540c8de8362SGuenter Roeck  */
5419431996fSJuerg Haefliger static inline int PWM_OFF_FROM_REG(int reg, int ix)
5429431996fSJuerg Haefliger {
5439431996fSJuerg Haefliger 	return (reg >> (ix + 5)) & 0x01;
5449431996fSJuerg Haefliger }
5459431996fSJuerg Haefliger 
5469431996fSJuerg Haefliger static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
5479431996fSJuerg Haefliger {
5489431996fSJuerg Haefliger 	return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
5499431996fSJuerg Haefliger }
5509431996fSJuerg Haefliger 
5519431996fSJuerg Haefliger /* ---------------------------------------------------------------------
5529431996fSJuerg Haefliger  * Device I/O access
553e95c237dSJuerg Haefliger  *
554e95c237dSJuerg Haefliger  * ISA access is performed through an index/data register pair and needs to
555e95c237dSJuerg Haefliger  * be protected by a mutex during runtime (not required for initialization).
556e95c237dSJuerg Haefliger  * We use data->update_lock for this and need to ensure that we acquire it
557e95c237dSJuerg Haefliger  * before calling dme1737_read or dme1737_write.
5589431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
5599431996fSJuerg Haefliger 
560dbc2bc25SJean Delvare static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
5619431996fSJuerg Haefliger {
562dbc2bc25SJean Delvare 	struct i2c_client *client = data->client;
563e95c237dSJuerg Haefliger 	s32 val;
564e95c237dSJuerg Haefliger 
565dbc2bc25SJean Delvare 	if (client) { /* I2C device */
566e95c237dSJuerg Haefliger 		val = i2c_smbus_read_byte_data(client, reg);
5679431996fSJuerg Haefliger 
5689431996fSJuerg Haefliger 		if (val < 0) {
569e95c237dSJuerg Haefliger 			dev_warn(&client->dev, "Read from register "
570e95c237dSJuerg Haefliger 				 "0x%02x failed! Please report to the driver "
571e95c237dSJuerg Haefliger 				 "maintainer.\n", reg);
572e95c237dSJuerg Haefliger 		}
573e95c237dSJuerg Haefliger 	} else { /* ISA device */
574dbc2bc25SJean Delvare 		outb(reg, data->addr);
575dbc2bc25SJean Delvare 		val = inb(data->addr + 1);
5769431996fSJuerg Haefliger 	}
5779431996fSJuerg Haefliger 
5789431996fSJuerg Haefliger 	return val;
5799431996fSJuerg Haefliger }
5809431996fSJuerg Haefliger 
581dbc2bc25SJean Delvare static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
5829431996fSJuerg Haefliger {
583dbc2bc25SJean Delvare 	struct i2c_client *client = data->client;
584e95c237dSJuerg Haefliger 	s32 res = 0;
585e95c237dSJuerg Haefliger 
586dbc2bc25SJean Delvare 	if (client) { /* I2C device */
587e95c237dSJuerg Haefliger 		res = i2c_smbus_write_byte_data(client, reg, val);
5889431996fSJuerg Haefliger 
5899431996fSJuerg Haefliger 		if (res < 0) {
590e95c237dSJuerg Haefliger 			dev_warn(&client->dev, "Write to register "
591e95c237dSJuerg Haefliger 				 "0x%02x failed! Please report to the driver "
592e95c237dSJuerg Haefliger 				 "maintainer.\n", reg);
593e95c237dSJuerg Haefliger 		}
594e95c237dSJuerg Haefliger 	} else { /* ISA device */
595dbc2bc25SJean Delvare 		outb(reg, data->addr);
596dbc2bc25SJean Delvare 		outb(val, data->addr + 1);
5979431996fSJuerg Haefliger 	}
5989431996fSJuerg Haefliger 
5999431996fSJuerg Haefliger 	return res;
6009431996fSJuerg Haefliger }
6019431996fSJuerg Haefliger 
6029431996fSJuerg Haefliger static struct dme1737_data *dme1737_update_device(struct device *dev)
6039431996fSJuerg Haefliger {
604b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
6059431996fSJuerg Haefliger 	int ix;
606d4b94e1fSJuerg Haefliger 	u8 lsb[6];
6079431996fSJuerg Haefliger 
6089431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
6099431996fSJuerg Haefliger 
6109431996fSJuerg Haefliger 	/* Enable a Vbat monitoring cycle every 10 mins */
6119431996fSJuerg Haefliger 	if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
612dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
6139431996fSJuerg Haefliger 						DME1737_REG_CONFIG) | 0x10);
6149431996fSJuerg Haefliger 		data->last_vbat = jiffies;
6159431996fSJuerg Haefliger 	}
6169431996fSJuerg Haefliger 
6179431996fSJuerg Haefliger 	/* Sample register contents every 1 sec */
6189431996fSJuerg Haefliger 	if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
619ea694431SJuerg Haefliger 		if (data->has_features & HAS_VID) {
620dbc2bc25SJean Delvare 			data->vid = dme1737_read(data, DME1737_REG_VID) &
621549edb83SJuerg Haefliger 				0x3f;
622549edb83SJuerg Haefliger 		}
6239431996fSJuerg Haefliger 
6249431996fSJuerg Haefliger 		/* In (voltage) registers */
6259431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
626c8de8362SGuenter Roeck 			/*
627c8de8362SGuenter Roeck 			 * Voltage inputs are stored as 16 bit values even
6289431996fSJuerg Haefliger 			 * though they have only 12 bits resolution. This is
629c8de8362SGuenter Roeck 			 * to make it consistent with the temp inputs.
630c8de8362SGuenter Roeck 			 */
631c8de8362SGuenter Roeck 			if (ix == 7 && !(data->has_features & HAS_IN7))
632d4b94e1fSJuerg Haefliger 				continue;
633dbc2bc25SJean Delvare 			data->in[ix] = dme1737_read(data,
6349431996fSJuerg Haefliger 					DME1737_REG_IN(ix)) << 8;
635dbc2bc25SJean Delvare 			data->in_min[ix] = dme1737_read(data,
6369431996fSJuerg Haefliger 					DME1737_REG_IN_MIN(ix));
637dbc2bc25SJean Delvare 			data->in_max[ix] = dme1737_read(data,
6389431996fSJuerg Haefliger 					DME1737_REG_IN_MAX(ix));
6399431996fSJuerg Haefliger 		}
6409431996fSJuerg Haefliger 
6419431996fSJuerg Haefliger 		/* Temp registers */
6429431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
643c8de8362SGuenter Roeck 			/*
644c8de8362SGuenter Roeck 			 * Temp inputs are stored as 16 bit values even
6459431996fSJuerg Haefliger 			 * though they have only 12 bits resolution. This is
6469431996fSJuerg Haefliger 			 * to take advantage of implicit conversions between
6479431996fSJuerg Haefliger 			 * register values (2's complement) and temp values
648c8de8362SGuenter Roeck 			 * (signed decimal).
649c8de8362SGuenter Roeck 			 */
650dbc2bc25SJean Delvare 			data->temp[ix] = dme1737_read(data,
6519431996fSJuerg Haefliger 					DME1737_REG_TEMP(ix)) << 8;
652dbc2bc25SJean Delvare 			data->temp_min[ix] = dme1737_read(data,
6539431996fSJuerg Haefliger 					DME1737_REG_TEMP_MIN(ix));
654dbc2bc25SJean Delvare 			data->temp_max[ix] = dme1737_read(data,
6559431996fSJuerg Haefliger 					DME1737_REG_TEMP_MAX(ix));
656ea694431SJuerg Haefliger 			if (data->has_features & HAS_TEMP_OFFSET) {
657dbc2bc25SJean Delvare 				data->temp_offset[ix] = dme1737_read(data,
6589431996fSJuerg Haefliger 						DME1737_REG_TEMP_OFFSET(ix));
6599431996fSJuerg Haefliger 			}
660549edb83SJuerg Haefliger 		}
6619431996fSJuerg Haefliger 
662c8de8362SGuenter Roeck 		/*
663c8de8362SGuenter Roeck 		 * In and temp LSB registers
6649431996fSJuerg Haefliger 		 * The LSBs are latched when the MSBs are read, so the order in
6659431996fSJuerg Haefliger 		 * which the registers are read (MSB first, then LSB) is
666c8de8362SGuenter Roeck 		 * important!
667c8de8362SGuenter Roeck 		 */
6689431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
669c8de8362SGuenter Roeck 			if (ix == 5 && !(data->has_features & HAS_IN7))
670d4b94e1fSJuerg Haefliger 				continue;
671dbc2bc25SJean Delvare 			lsb[ix] = dme1737_read(data,
6729431996fSJuerg Haefliger 					DME1737_REG_IN_TEMP_LSB(ix));
6739431996fSJuerg Haefliger 		}
6749431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
675c8de8362SGuenter Roeck 			if (ix == 7 && !(data->has_features & HAS_IN7))
676d4b94e1fSJuerg Haefliger 				continue;
6779431996fSJuerg Haefliger 			data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
6789431996fSJuerg Haefliger 					DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
6799431996fSJuerg Haefliger 		}
6809431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
6819431996fSJuerg Haefliger 			data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
6829431996fSJuerg Haefliger 					DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
6839431996fSJuerg Haefliger 		}
6849431996fSJuerg Haefliger 
6859431996fSJuerg Haefliger 		/* Fan registers */
6869431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
687c8de8362SGuenter Roeck 			/*
688c8de8362SGuenter Roeck 			 * Skip reading registers if optional fans are not
689c8de8362SGuenter Roeck 			 * present
690c8de8362SGuenter Roeck 			 */
691c8de8362SGuenter Roeck 			if (!(data->has_features & HAS_FAN(ix)))
6929431996fSJuerg Haefliger 				continue;
693dbc2bc25SJean Delvare 			data->fan[ix] = dme1737_read(data,
6949431996fSJuerg Haefliger 					DME1737_REG_FAN(ix));
695dbc2bc25SJean Delvare 			data->fan[ix] |= dme1737_read(data,
6969431996fSJuerg Haefliger 					DME1737_REG_FAN(ix) + 1) << 8;
697dbc2bc25SJean Delvare 			data->fan_min[ix] = dme1737_read(data,
6989431996fSJuerg Haefliger 					DME1737_REG_FAN_MIN(ix));
699dbc2bc25SJean Delvare 			data->fan_min[ix] |= dme1737_read(data,
7009431996fSJuerg Haefliger 					DME1737_REG_FAN_MIN(ix) + 1) << 8;
701dbc2bc25SJean Delvare 			data->fan_opt[ix] = dme1737_read(data,
7029431996fSJuerg Haefliger 					DME1737_REG_FAN_OPT(ix));
7039431996fSJuerg Haefliger 			/* fan_max exists only for fan[5-6] */
7049431996fSJuerg Haefliger 			if (ix > 3) {
705dbc2bc25SJean Delvare 				data->fan_max[ix - 4] = dme1737_read(data,
7069431996fSJuerg Haefliger 					DME1737_REG_FAN_MAX(ix));
7079431996fSJuerg Haefliger 			}
7089431996fSJuerg Haefliger 		}
7099431996fSJuerg Haefliger 
7109431996fSJuerg Haefliger 		/* PWM registers */
7119431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
712c8de8362SGuenter Roeck 			/*
713c8de8362SGuenter Roeck 			 * Skip reading registers if optional PWMs are not
714c8de8362SGuenter Roeck 			 * present
715c8de8362SGuenter Roeck 			 */
716c8de8362SGuenter Roeck 			if (!(data->has_features & HAS_PWM(ix)))
7179431996fSJuerg Haefliger 				continue;
718dbc2bc25SJean Delvare 			data->pwm[ix] = dme1737_read(data,
7199431996fSJuerg Haefliger 					DME1737_REG_PWM(ix));
720dbc2bc25SJean Delvare 			data->pwm_freq[ix] = dme1737_read(data,
7219431996fSJuerg Haefliger 					DME1737_REG_PWM_FREQ(ix));
7229431996fSJuerg Haefliger 			/* pwm_config and pwm_min exist only for pwm[1-3] */
7239431996fSJuerg Haefliger 			if (ix < 3) {
724dbc2bc25SJean Delvare 				data->pwm_config[ix] = dme1737_read(data,
7259431996fSJuerg Haefliger 						DME1737_REG_PWM_CONFIG(ix));
726dbc2bc25SJean Delvare 				data->pwm_min[ix] = dme1737_read(data,
7279431996fSJuerg Haefliger 						DME1737_REG_PWM_MIN(ix));
7289431996fSJuerg Haefliger 			}
7299431996fSJuerg Haefliger 		}
7309431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
731dbc2bc25SJean Delvare 			data->pwm_rr[ix] = dme1737_read(data,
7329431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(ix));
7339431996fSJuerg Haefliger 		}
7349431996fSJuerg Haefliger 
7359431996fSJuerg Haefliger 		/* Thermal zone registers */
7369431996fSJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
737ea694431SJuerg Haefliger 			/* Skip reading registers if zone3 is not present */
738c8de8362SGuenter Roeck 			if ((ix == 2) && !(data->has_features & HAS_ZONE3))
739ea694431SJuerg Haefliger 				continue;
740ea694431SJuerg Haefliger 			/* sch5127 zone2 registers are special */
741ea694431SJuerg Haefliger 			if ((ix == 1) && (data->type == sch5127)) {
742ea694431SJuerg Haefliger 				data->zone_low[1] = dme1737_read(data,
743ea694431SJuerg Haefliger 						DME1737_REG_ZONE_LOW(2));
744ea694431SJuerg Haefliger 				data->zone_abs[1] = dme1737_read(data,
745ea694431SJuerg Haefliger 						DME1737_REG_ZONE_ABS(2));
746ea694431SJuerg Haefliger 			} else {
747dbc2bc25SJean Delvare 				data->zone_low[ix] = dme1737_read(data,
7489431996fSJuerg Haefliger 						DME1737_REG_ZONE_LOW(ix));
749dbc2bc25SJean Delvare 				data->zone_abs[ix] = dme1737_read(data,
7509431996fSJuerg Haefliger 						DME1737_REG_ZONE_ABS(ix));
7519431996fSJuerg Haefliger 			}
752ea694431SJuerg Haefliger 		}
753ea694431SJuerg Haefliger 		if (data->has_features & HAS_ZONE_HYST) {
7549431996fSJuerg Haefliger 			for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
755dbc2bc25SJean Delvare 				data->zone_hyst[ix] = dme1737_read(data,
7569431996fSJuerg Haefliger 						DME1737_REG_ZONE_HYST(ix));
7579431996fSJuerg Haefliger 			}
758549edb83SJuerg Haefliger 		}
7599431996fSJuerg Haefliger 
7609431996fSJuerg Haefliger 		/* Alarm registers */
761dbc2bc25SJean Delvare 		data->alarms = dme1737_read(data,
7629431996fSJuerg Haefliger 						DME1737_REG_ALARM1);
763c8de8362SGuenter Roeck 		/*
764c8de8362SGuenter Roeck 		 * Bit 7 tells us if the other alarm registers are non-zero and
765c8de8362SGuenter Roeck 		 * therefore also need to be read
766c8de8362SGuenter Roeck 		 */
7679431996fSJuerg Haefliger 		if (data->alarms & 0x80) {
768dbc2bc25SJean Delvare 			data->alarms |= dme1737_read(data,
7699431996fSJuerg Haefliger 						DME1737_REG_ALARM2) << 8;
770dbc2bc25SJean Delvare 			data->alarms |= dme1737_read(data,
7719431996fSJuerg Haefliger 						DME1737_REG_ALARM3) << 16;
7729431996fSJuerg Haefliger 		}
7739431996fSJuerg Haefliger 
774c8de8362SGuenter Roeck 		/*
775c8de8362SGuenter Roeck 		 * The ISA chips require explicit clearing of alarm bits.
776e95c237dSJuerg Haefliger 		 * Don't worry, an alarm will come back if the condition
777c8de8362SGuenter Roeck 		 * that causes it still exists
778c8de8362SGuenter Roeck 		 */
779dbc2bc25SJean Delvare 		if (!data->client) {
780c8de8362SGuenter Roeck 			if (data->alarms & 0xff0000)
781c8de8362SGuenter Roeck 				dme1737_write(data, DME1737_REG_ALARM3, 0xff);
782c8de8362SGuenter Roeck 			if (data->alarms & 0xff00)
783c8de8362SGuenter Roeck 				dme1737_write(data, DME1737_REG_ALARM2, 0xff);
784c8de8362SGuenter Roeck 			if (data->alarms & 0xff)
785c8de8362SGuenter Roeck 				dme1737_write(data, DME1737_REG_ALARM1, 0xff);
786e95c237dSJuerg Haefliger 		}
787e95c237dSJuerg Haefliger 
7889431996fSJuerg Haefliger 		data->last_update = jiffies;
7899431996fSJuerg Haefliger 		data->valid = 1;
7909431996fSJuerg Haefliger 	}
7919431996fSJuerg Haefliger 
7929431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
7939431996fSJuerg Haefliger 
7949431996fSJuerg Haefliger 	return data;
7959431996fSJuerg Haefliger }
7969431996fSJuerg Haefliger 
7979431996fSJuerg Haefliger /* ---------------------------------------------------------------------
7989431996fSJuerg Haefliger  * Voltage sysfs attributes
799d4b94e1fSJuerg Haefliger  * ix = [0-7]
8009431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
8019431996fSJuerg Haefliger 
8029431996fSJuerg Haefliger #define SYS_IN_INPUT	0
8039431996fSJuerg Haefliger #define SYS_IN_MIN	1
8049431996fSJuerg Haefliger #define SYS_IN_MAX	2
8059431996fSJuerg Haefliger #define SYS_IN_ALARM	3
8069431996fSJuerg Haefliger 
8079431996fSJuerg Haefliger static ssize_t show_in(struct device *dev, struct device_attribute *attr,
8089431996fSJuerg Haefliger 		       char *buf)
8099431996fSJuerg Haefliger {
8109431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
8119431996fSJuerg Haefliger 	struct sensor_device_attribute_2
8129431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
8139431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
8149431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
8159431996fSJuerg Haefliger 	int res;
8169431996fSJuerg Haefliger 
8179431996fSJuerg Haefliger 	switch (fn) {
8189431996fSJuerg Haefliger 	case SYS_IN_INPUT:
819549edb83SJuerg Haefliger 		res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
8209431996fSJuerg Haefliger 		break;
8219431996fSJuerg Haefliger 	case SYS_IN_MIN:
822549edb83SJuerg Haefliger 		res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
8239431996fSJuerg Haefliger 		break;
8249431996fSJuerg Haefliger 	case SYS_IN_MAX:
825549edb83SJuerg Haefliger 		res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
8269431996fSJuerg Haefliger 		break;
8279431996fSJuerg Haefliger 	case SYS_IN_ALARM:
8289431996fSJuerg Haefliger 		res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
8299431996fSJuerg Haefliger 		break;
8309431996fSJuerg Haefliger 	default:
8319431996fSJuerg Haefliger 		res = 0;
832b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
8339431996fSJuerg Haefliger 	}
8349431996fSJuerg Haefliger 
8359431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", res);
8369431996fSJuerg Haefliger }
8379431996fSJuerg Haefliger 
8389431996fSJuerg Haefliger static ssize_t set_in(struct device *dev, struct device_attribute *attr,
8399431996fSJuerg Haefliger 		      const char *buf, size_t count)
8409431996fSJuerg Haefliger {
841b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
8429431996fSJuerg Haefliger 	struct sensor_device_attribute_2
8439431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
8449431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
8459431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
846c8de8362SGuenter Roeck 	long val;
847c8de8362SGuenter Roeck 	int err;
848c8de8362SGuenter Roeck 
849c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
850c8de8362SGuenter Roeck 	if (err)
851c8de8362SGuenter Roeck 		return err;
8529431996fSJuerg Haefliger 
8539431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
8549431996fSJuerg Haefliger 	switch (fn) {
8559431996fSJuerg Haefliger 	case SYS_IN_MIN:
856549edb83SJuerg Haefliger 		data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
857dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_IN_MIN(ix),
8589431996fSJuerg Haefliger 			      data->in_min[ix]);
8599431996fSJuerg Haefliger 		break;
8609431996fSJuerg Haefliger 	case SYS_IN_MAX:
861549edb83SJuerg Haefliger 		data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
862dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_IN_MAX(ix),
8639431996fSJuerg Haefliger 			      data->in_max[ix]);
8649431996fSJuerg Haefliger 		break;
8659431996fSJuerg Haefliger 	default:
866b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
8679431996fSJuerg Haefliger 	}
8689431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
8699431996fSJuerg Haefliger 
8709431996fSJuerg Haefliger 	return count;
8719431996fSJuerg Haefliger }
8729431996fSJuerg Haefliger 
8739431996fSJuerg Haefliger /* ---------------------------------------------------------------------
8749431996fSJuerg Haefliger  * Temperature sysfs attributes
8759431996fSJuerg Haefliger  * ix = [0-2]
8769431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
8779431996fSJuerg Haefliger 
8789431996fSJuerg Haefliger #define SYS_TEMP_INPUT			0
8799431996fSJuerg Haefliger #define SYS_TEMP_MIN			1
8809431996fSJuerg Haefliger #define SYS_TEMP_MAX			2
8819431996fSJuerg Haefliger #define SYS_TEMP_OFFSET			3
8829431996fSJuerg Haefliger #define SYS_TEMP_ALARM			4
8839431996fSJuerg Haefliger #define SYS_TEMP_FAULT			5
8849431996fSJuerg Haefliger 
8859431996fSJuerg Haefliger static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
8869431996fSJuerg Haefliger 			 char *buf)
8879431996fSJuerg Haefliger {
8889431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
8899431996fSJuerg Haefliger 	struct sensor_device_attribute_2
8909431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
8919431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
8929431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
8939431996fSJuerg Haefliger 	int res;
8949431996fSJuerg Haefliger 
8959431996fSJuerg Haefliger 	switch (fn) {
8969431996fSJuerg Haefliger 	case SYS_TEMP_INPUT:
8979431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->temp[ix], 16);
8989431996fSJuerg Haefliger 		break;
8999431996fSJuerg Haefliger 	case SYS_TEMP_MIN:
9009431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->temp_min[ix], 8);
9019431996fSJuerg Haefliger 		break;
9029431996fSJuerg Haefliger 	case SYS_TEMP_MAX:
9039431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->temp_max[ix], 8);
9049431996fSJuerg Haefliger 		break;
9059431996fSJuerg Haefliger 	case SYS_TEMP_OFFSET:
9069431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->temp_offset[ix], 8);
9079431996fSJuerg Haefliger 		break;
9089431996fSJuerg Haefliger 	case SYS_TEMP_ALARM:
9099431996fSJuerg Haefliger 		res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
9109431996fSJuerg Haefliger 		break;
9119431996fSJuerg Haefliger 	case SYS_TEMP_FAULT:
912c0f31403SJuerg Haefliger 		res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
9139431996fSJuerg Haefliger 		break;
9149431996fSJuerg Haefliger 	default:
9159431996fSJuerg Haefliger 		res = 0;
916b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
9179431996fSJuerg Haefliger 	}
9189431996fSJuerg Haefliger 
9199431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", res);
9209431996fSJuerg Haefliger }
9219431996fSJuerg Haefliger 
9229431996fSJuerg Haefliger static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
9239431996fSJuerg Haefliger 			const char *buf, size_t count)
9249431996fSJuerg Haefliger {
925b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
9269431996fSJuerg Haefliger 	struct sensor_device_attribute_2
9279431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
9289431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
9299431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
930c8de8362SGuenter Roeck 	long val;
931c8de8362SGuenter Roeck 	int err;
932c8de8362SGuenter Roeck 
933c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
934c8de8362SGuenter Roeck 	if (err)
935c8de8362SGuenter Roeck 		return err;
9369431996fSJuerg Haefliger 
9379431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
9389431996fSJuerg Haefliger 	switch (fn) {
9399431996fSJuerg Haefliger 	case SYS_TEMP_MIN:
9409431996fSJuerg Haefliger 		data->temp_min[ix] = TEMP_TO_REG(val);
941dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
9429431996fSJuerg Haefliger 			      data->temp_min[ix]);
9439431996fSJuerg Haefliger 		break;
9449431996fSJuerg Haefliger 	case SYS_TEMP_MAX:
9459431996fSJuerg Haefliger 		data->temp_max[ix] = TEMP_TO_REG(val);
946dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
9479431996fSJuerg Haefliger 			      data->temp_max[ix]);
9489431996fSJuerg Haefliger 		break;
9499431996fSJuerg Haefliger 	case SYS_TEMP_OFFSET:
9509431996fSJuerg Haefliger 		data->temp_offset[ix] = TEMP_TO_REG(val);
951dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
9529431996fSJuerg Haefliger 			      data->temp_offset[ix]);
9539431996fSJuerg Haefliger 		break;
9549431996fSJuerg Haefliger 	default:
955b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
9569431996fSJuerg Haefliger 	}
9579431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
9589431996fSJuerg Haefliger 
9599431996fSJuerg Haefliger 	return count;
9609431996fSJuerg Haefliger }
9619431996fSJuerg Haefliger 
9629431996fSJuerg Haefliger /* ---------------------------------------------------------------------
9639431996fSJuerg Haefliger  * Zone sysfs attributes
9649431996fSJuerg Haefliger  * ix = [0-2]
9659431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
9669431996fSJuerg Haefliger 
9679431996fSJuerg Haefliger #define SYS_ZONE_AUTO_CHANNELS_TEMP	0
9689431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT1_TEMP_HYST	1
9699431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT1_TEMP	2
9709431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT2_TEMP	3
9719431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT3_TEMP	4
9729431996fSJuerg Haefliger 
9739431996fSJuerg Haefliger static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
9749431996fSJuerg Haefliger 			 char *buf)
9759431996fSJuerg Haefliger {
9769431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
9779431996fSJuerg Haefliger 	struct sensor_device_attribute_2
9789431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
9799431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
9809431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
9819431996fSJuerg Haefliger 	int res;
9829431996fSJuerg Haefliger 
9839431996fSJuerg Haefliger 	switch (fn) {
9849431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_CHANNELS_TEMP:
9859431996fSJuerg Haefliger 		/* check config2 for non-standard temp-to-zone mapping */
986c8de8362SGuenter Roeck 		if ((ix == 1) && (data->config2 & 0x02))
9879431996fSJuerg Haefliger 			res = 4;
988c8de8362SGuenter Roeck 		else
9899431996fSJuerg Haefliger 			res = 1 << ix;
9909431996fSJuerg Haefliger 		break;
9919431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
9929431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->zone_low[ix], 8) -
9939431996fSJuerg Haefliger 		      TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
9949431996fSJuerg Haefliger 		break;
9959431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT1_TEMP:
9969431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->zone_low[ix], 8);
9979431996fSJuerg Haefliger 		break;
9989431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT2_TEMP:
9999431996fSJuerg Haefliger 		/* pwm_freq holds the temp range bits in the upper nibble */
10009431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->zone_low[ix], 8) +
10019431996fSJuerg Haefliger 		      TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
10029431996fSJuerg Haefliger 		break;
10039431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT3_TEMP:
10049431996fSJuerg Haefliger 		res = TEMP_FROM_REG(data->zone_abs[ix], 8);
10059431996fSJuerg Haefliger 		break;
10069431996fSJuerg Haefliger 	default:
10079431996fSJuerg Haefliger 		res = 0;
1008b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
10099431996fSJuerg Haefliger 	}
10109431996fSJuerg Haefliger 
10119431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", res);
10129431996fSJuerg Haefliger }
10139431996fSJuerg Haefliger 
10149431996fSJuerg Haefliger static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
10159431996fSJuerg Haefliger 			const char *buf, size_t count)
10169431996fSJuerg Haefliger {
1017b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
10189431996fSJuerg Haefliger 	struct sensor_device_attribute_2
10199431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
10209431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
10219431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
1022c8de8362SGuenter Roeck 	long val;
1023c8de8362SGuenter Roeck 	int err;
1024c8de8362SGuenter Roeck 
1025c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1026c8de8362SGuenter Roeck 	if (err)
1027c8de8362SGuenter Roeck 		return err;
10289431996fSJuerg Haefliger 
10299431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
10309431996fSJuerg Haefliger 	switch (fn) {
10319431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
10329431996fSJuerg Haefliger 		/* Refresh the cache */
1033dbc2bc25SJean Delvare 		data->zone_low[ix] = dme1737_read(data,
10349431996fSJuerg Haefliger 						  DME1737_REG_ZONE_LOW(ix));
10359431996fSJuerg Haefliger 		/* Modify the temp hyst value */
10369431996fSJuerg Haefliger 		data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
10379431996fSJuerg Haefliger 					TEMP_FROM_REG(data->zone_low[ix], 8) -
1038dbc2bc25SJean Delvare 					val, ix, dme1737_read(data,
10399431996fSJuerg Haefliger 					DME1737_REG_ZONE_HYST(ix == 2)));
1040dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
10419431996fSJuerg Haefliger 			      data->zone_hyst[ix == 2]);
10429431996fSJuerg Haefliger 		break;
10439431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT1_TEMP:
10449431996fSJuerg Haefliger 		data->zone_low[ix] = TEMP_TO_REG(val);
1045dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
10469431996fSJuerg Haefliger 			      data->zone_low[ix]);
10479431996fSJuerg Haefliger 		break;
10489431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT2_TEMP:
10499431996fSJuerg Haefliger 		/* Refresh the cache */
1050dbc2bc25SJean Delvare 		data->zone_low[ix] = dme1737_read(data,
10519431996fSJuerg Haefliger 						  DME1737_REG_ZONE_LOW(ix));
1052c8de8362SGuenter Roeck 		/*
1053c8de8362SGuenter Roeck 		 * Modify the temp range value (which is stored in the upper
1054c8de8362SGuenter Roeck 		 * nibble of the pwm_freq register)
1055c8de8362SGuenter Roeck 		 */
10569431996fSJuerg Haefliger 		data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
10579431996fSJuerg Haefliger 					TEMP_FROM_REG(data->zone_low[ix], 8),
1058dbc2bc25SJean Delvare 					dme1737_read(data,
10599431996fSJuerg Haefliger 					DME1737_REG_PWM_FREQ(ix)));
1060dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
10619431996fSJuerg Haefliger 			      data->pwm_freq[ix]);
10629431996fSJuerg Haefliger 		break;
10639431996fSJuerg Haefliger 	case SYS_ZONE_AUTO_POINT3_TEMP:
10649431996fSJuerg Haefliger 		data->zone_abs[ix] = TEMP_TO_REG(val);
1065dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
10669431996fSJuerg Haefliger 			      data->zone_abs[ix]);
10679431996fSJuerg Haefliger 		break;
10689431996fSJuerg Haefliger 	default:
1069b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
10709431996fSJuerg Haefliger 	}
10719431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
10729431996fSJuerg Haefliger 
10739431996fSJuerg Haefliger 	return count;
10749431996fSJuerg Haefliger }
10759431996fSJuerg Haefliger 
10769431996fSJuerg Haefliger /* ---------------------------------------------------------------------
10779431996fSJuerg Haefliger  * Fan sysfs attributes
10789431996fSJuerg Haefliger  * ix = [0-5]
10799431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
10809431996fSJuerg Haefliger 
10819431996fSJuerg Haefliger #define SYS_FAN_INPUT	0
10829431996fSJuerg Haefliger #define SYS_FAN_MIN	1
10839431996fSJuerg Haefliger #define SYS_FAN_MAX	2
10849431996fSJuerg Haefliger #define SYS_FAN_ALARM	3
10859431996fSJuerg Haefliger #define SYS_FAN_TYPE	4
10869431996fSJuerg Haefliger 
10879431996fSJuerg Haefliger static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
10889431996fSJuerg Haefliger 			char *buf)
10899431996fSJuerg Haefliger {
10909431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
10919431996fSJuerg Haefliger 	struct sensor_device_attribute_2
10929431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
10939431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
10949431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
10959431996fSJuerg Haefliger 	int res;
10969431996fSJuerg Haefliger 
10979431996fSJuerg Haefliger 	switch (fn) {
10989431996fSJuerg Haefliger 	case SYS_FAN_INPUT:
10999431996fSJuerg Haefliger 		res = FAN_FROM_REG(data->fan[ix],
11009431996fSJuerg Haefliger 				   ix < 4 ? 0 :
11019431996fSJuerg Haefliger 				   FAN_TPC_FROM_REG(data->fan_opt[ix]));
11029431996fSJuerg Haefliger 		break;
11039431996fSJuerg Haefliger 	case SYS_FAN_MIN:
11049431996fSJuerg Haefliger 		res = FAN_FROM_REG(data->fan_min[ix],
11059431996fSJuerg Haefliger 				   ix < 4 ? 0 :
11069431996fSJuerg Haefliger 				   FAN_TPC_FROM_REG(data->fan_opt[ix]));
11079431996fSJuerg Haefliger 		break;
11089431996fSJuerg Haefliger 	case SYS_FAN_MAX:
11099431996fSJuerg Haefliger 		/* only valid for fan[5-6] */
11109431996fSJuerg Haefliger 		res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
11119431996fSJuerg Haefliger 		break;
11129431996fSJuerg Haefliger 	case SYS_FAN_ALARM:
11139431996fSJuerg Haefliger 		res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
11149431996fSJuerg Haefliger 		break;
11159431996fSJuerg Haefliger 	case SYS_FAN_TYPE:
11169431996fSJuerg Haefliger 		/* only valid for fan[1-4] */
11179431996fSJuerg Haefliger 		res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
11189431996fSJuerg Haefliger 		break;
11199431996fSJuerg Haefliger 	default:
11209431996fSJuerg Haefliger 		res = 0;
1121b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
11229431996fSJuerg Haefliger 	}
11239431996fSJuerg Haefliger 
11249431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", res);
11259431996fSJuerg Haefliger }
11269431996fSJuerg Haefliger 
11279431996fSJuerg Haefliger static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
11289431996fSJuerg Haefliger 		       const char *buf, size_t count)
11299431996fSJuerg Haefliger {
1130b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
11319431996fSJuerg Haefliger 	struct sensor_device_attribute_2
11329431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
11339431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
11349431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
1135c8de8362SGuenter Roeck 	long val;
1136c8de8362SGuenter Roeck 	int err;
1137c8de8362SGuenter Roeck 
1138c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1139c8de8362SGuenter Roeck 	if (err)
1140c8de8362SGuenter Roeck 		return err;
11419431996fSJuerg Haefliger 
11429431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
11439431996fSJuerg Haefliger 	switch (fn) {
11449431996fSJuerg Haefliger 	case SYS_FAN_MIN:
11459431996fSJuerg Haefliger 		if (ix < 4) {
11469431996fSJuerg Haefliger 			data->fan_min[ix] = FAN_TO_REG(val, 0);
11479431996fSJuerg Haefliger 		} else {
11489431996fSJuerg Haefliger 			/* Refresh the cache */
1149dbc2bc25SJean Delvare 			data->fan_opt[ix] = dme1737_read(data,
11509431996fSJuerg Haefliger 						DME1737_REG_FAN_OPT(ix));
11519431996fSJuerg Haefliger 			/* Modify the fan min value */
11529431996fSJuerg Haefliger 			data->fan_min[ix] = FAN_TO_REG(val,
11539431996fSJuerg Haefliger 					FAN_TPC_FROM_REG(data->fan_opt[ix]));
11549431996fSJuerg Haefliger 		}
1155dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_FAN_MIN(ix),
11569431996fSJuerg Haefliger 			      data->fan_min[ix] & 0xff);
1157dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
11589431996fSJuerg Haefliger 			      data->fan_min[ix] >> 8);
11599431996fSJuerg Haefliger 		break;
11609431996fSJuerg Haefliger 	case SYS_FAN_MAX:
11619431996fSJuerg Haefliger 		/* Only valid for fan[5-6] */
11629431996fSJuerg Haefliger 		data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1163dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_FAN_MAX(ix),
11649431996fSJuerg Haefliger 			      data->fan_max[ix - 4]);
11659431996fSJuerg Haefliger 		break;
11669431996fSJuerg Haefliger 	case SYS_FAN_TYPE:
11679431996fSJuerg Haefliger 		/* Only valid for fan[1-4] */
11689431996fSJuerg Haefliger 		if (!(val == 1 || val == 2 || val == 4)) {
11699431996fSJuerg Haefliger 			count = -EINVAL;
1170e95c237dSJuerg Haefliger 			dev_warn(dev, "Fan type value %ld not "
11719431996fSJuerg Haefliger 				 "supported. Choose one of 1, 2, or 4.\n",
11729431996fSJuerg Haefliger 				 val);
11739431996fSJuerg Haefliger 			goto exit;
11749431996fSJuerg Haefliger 		}
1175dbc2bc25SJean Delvare 		data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
11769431996fSJuerg Haefliger 					DME1737_REG_FAN_OPT(ix)));
1177dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_FAN_OPT(ix),
11789431996fSJuerg Haefliger 			      data->fan_opt[ix]);
11799431996fSJuerg Haefliger 		break;
11809431996fSJuerg Haefliger 	default:
1181b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
11829431996fSJuerg Haefliger 	}
11839431996fSJuerg Haefliger exit:
11849431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
11859431996fSJuerg Haefliger 
11869431996fSJuerg Haefliger 	return count;
11879431996fSJuerg Haefliger }
11889431996fSJuerg Haefliger 
11899431996fSJuerg Haefliger /* ---------------------------------------------------------------------
11909431996fSJuerg Haefliger  * PWM sysfs attributes
11919431996fSJuerg Haefliger  * ix = [0-4]
11929431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
11939431996fSJuerg Haefliger 
11949431996fSJuerg Haefliger #define SYS_PWM				0
11959431996fSJuerg Haefliger #define SYS_PWM_FREQ			1
11969431996fSJuerg Haefliger #define SYS_PWM_ENABLE			2
11979431996fSJuerg Haefliger #define SYS_PWM_RAMP_RATE		3
11989431996fSJuerg Haefliger #define SYS_PWM_AUTO_CHANNELS_ZONE	4
11999431996fSJuerg Haefliger #define SYS_PWM_AUTO_PWM_MIN		5
12009431996fSJuerg Haefliger #define SYS_PWM_AUTO_POINT1_PWM		6
12019431996fSJuerg Haefliger #define SYS_PWM_AUTO_POINT2_PWM		7
12029431996fSJuerg Haefliger 
12039431996fSJuerg Haefliger static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
12049431996fSJuerg Haefliger 			char *buf)
12059431996fSJuerg Haefliger {
12069431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
12079431996fSJuerg Haefliger 	struct sensor_device_attribute_2
12089431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
12099431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
12109431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
12119431996fSJuerg Haefliger 	int res;
12129431996fSJuerg Haefliger 
12139431996fSJuerg Haefliger 	switch (fn) {
12149431996fSJuerg Haefliger 	case SYS_PWM:
1215c8de8362SGuenter Roeck 		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0)
12169431996fSJuerg Haefliger 			res = 255;
1217c8de8362SGuenter Roeck 		else
12189431996fSJuerg Haefliger 			res = data->pwm[ix];
12199431996fSJuerg Haefliger 		break;
12209431996fSJuerg Haefliger 	case SYS_PWM_FREQ:
12219431996fSJuerg Haefliger 		res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
12229431996fSJuerg Haefliger 		break;
12239431996fSJuerg Haefliger 	case SYS_PWM_ENABLE:
1224c8de8362SGuenter Roeck 		if (ix >= 3)
12259431996fSJuerg Haefliger 			res = 1; /* pwm[5-6] hard-wired to manual mode */
1226c8de8362SGuenter Roeck 		else
12279431996fSJuerg Haefliger 			res = PWM_EN_FROM_REG(data->pwm_config[ix]);
12289431996fSJuerg Haefliger 		break;
12299431996fSJuerg Haefliger 	case SYS_PWM_RAMP_RATE:
12309431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
12319431996fSJuerg Haefliger 		res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
12329431996fSJuerg Haefliger 		break;
12339431996fSJuerg Haefliger 	case SYS_PWM_AUTO_CHANNELS_ZONE:
12349431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
1235c8de8362SGuenter Roeck 		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2)
12369431996fSJuerg Haefliger 			res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1237c8de8362SGuenter Roeck 		else
12389431996fSJuerg Haefliger 			res = data->pwm_acz[ix];
12399431996fSJuerg Haefliger 		break;
12409431996fSJuerg Haefliger 	case SYS_PWM_AUTO_PWM_MIN:
12419431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
1242c8de8362SGuenter Roeck 		if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix))
12439431996fSJuerg Haefliger 			res = data->pwm_min[ix];
1244c8de8362SGuenter Roeck 		else
12459431996fSJuerg Haefliger 			res = 0;
12469431996fSJuerg Haefliger 		break;
12479431996fSJuerg Haefliger 	case SYS_PWM_AUTO_POINT1_PWM:
12489431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
12499431996fSJuerg Haefliger 		res = data->pwm_min[ix];
12509431996fSJuerg Haefliger 		break;
12519431996fSJuerg Haefliger 	case SYS_PWM_AUTO_POINT2_PWM:
12529431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
12539431996fSJuerg Haefliger 		res = 255; /* hard-wired */
12549431996fSJuerg Haefliger 		break;
12559431996fSJuerg Haefliger 	default:
12569431996fSJuerg Haefliger 		res = 0;
1257b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
12589431996fSJuerg Haefliger 	}
12599431996fSJuerg Haefliger 
12609431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", res);
12619431996fSJuerg Haefliger }
12629431996fSJuerg Haefliger 
126373ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm_chmod_attr[];
126448176a97SAl Viro static void dme1737_chmod_file(struct device*, struct attribute*, umode_t);
12659431996fSJuerg Haefliger 
12669431996fSJuerg Haefliger static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
12679431996fSJuerg Haefliger 		       const char *buf, size_t count)
12689431996fSJuerg Haefliger {
1269b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
12709431996fSJuerg Haefliger 	struct sensor_device_attribute_2
12719431996fSJuerg Haefliger 		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
12729431996fSJuerg Haefliger 	int ix = sensor_attr_2->index;
12739431996fSJuerg Haefliger 	int fn = sensor_attr_2->nr;
1274c8de8362SGuenter Roeck 	long val;
1275c8de8362SGuenter Roeck 	int err;
1276c8de8362SGuenter Roeck 
1277c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1278c8de8362SGuenter Roeck 	if (err)
1279c8de8362SGuenter Roeck 		return err;
12809431996fSJuerg Haefliger 
12819431996fSJuerg Haefliger 	mutex_lock(&data->update_lock);
12829431996fSJuerg Haefliger 	switch (fn) {
12839431996fSJuerg Haefliger 	case SYS_PWM:
1284*2a844c14SGuenter Roeck 		data->pwm[ix] = clamp_val(val, 0, 255);
1285dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
12869431996fSJuerg Haefliger 		break;
12879431996fSJuerg Haefliger 	case SYS_PWM_FREQ:
1288dbc2bc25SJean Delvare 		data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
12899431996fSJuerg Haefliger 						DME1737_REG_PWM_FREQ(ix)));
1290dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
12919431996fSJuerg Haefliger 			      data->pwm_freq[ix]);
12929431996fSJuerg Haefliger 		break;
12939431996fSJuerg Haefliger 	case SYS_PWM_ENABLE:
12949431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
12959431996fSJuerg Haefliger 		if (val < 0 || val > 2) {
12969431996fSJuerg Haefliger 			count = -EINVAL;
1297e95c237dSJuerg Haefliger 			dev_warn(dev, "PWM enable %ld not "
12989431996fSJuerg Haefliger 				 "supported. Choose one of 0, 1, or 2.\n",
12999431996fSJuerg Haefliger 				 val);
13009431996fSJuerg Haefliger 			goto exit;
13019431996fSJuerg Haefliger 		}
13029431996fSJuerg Haefliger 		/* Refresh the cache */
1303dbc2bc25SJean Delvare 		data->pwm_config[ix] = dme1737_read(data,
13049431996fSJuerg Haefliger 						DME1737_REG_PWM_CONFIG(ix));
13059431996fSJuerg Haefliger 		if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
13069431996fSJuerg Haefliger 			/* Bail out if no change */
13079431996fSJuerg Haefliger 			goto exit;
13089431996fSJuerg Haefliger 		}
13099431996fSJuerg Haefliger 		/* Do some housekeeping if we are currently in auto mode */
13109431996fSJuerg Haefliger 		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
13119431996fSJuerg Haefliger 			/* Save the current zone channel assignment */
13129431996fSJuerg Haefliger 			data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
13139431996fSJuerg Haefliger 							data->pwm_config[ix]);
13149431996fSJuerg Haefliger 			/* Save the current ramp rate state and disable it */
1315dbc2bc25SJean Delvare 			data->pwm_rr[ix > 0] = dme1737_read(data,
13169431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(ix > 0));
13179431996fSJuerg Haefliger 			data->pwm_rr_en &= ~(1 << ix);
13189431996fSJuerg Haefliger 			if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
13199431996fSJuerg Haefliger 				data->pwm_rr_en |= (1 << ix);
13209431996fSJuerg Haefliger 				data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
13219431996fSJuerg Haefliger 							data->pwm_rr[ix > 0]);
1322dbc2bc25SJean Delvare 				dme1737_write(data,
13239431996fSJuerg Haefliger 					      DME1737_REG_PWM_RR(ix > 0),
13249431996fSJuerg Haefliger 					      data->pwm_rr[ix > 0]);
13259431996fSJuerg Haefliger 			}
13269431996fSJuerg Haefliger 		}
13279431996fSJuerg Haefliger 		/* Set the new PWM mode */
13289431996fSJuerg Haefliger 		switch (val) {
13299431996fSJuerg Haefliger 		case 0:
13309431996fSJuerg Haefliger 			/* Change permissions of pwm[ix] to read-only */
133173ce48f6SJuerg Haefliger 			dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
13329431996fSJuerg Haefliger 					   S_IRUGO);
13339431996fSJuerg Haefliger 			/* Turn fan fully on */
13349431996fSJuerg Haefliger 			data->pwm_config[ix] = PWM_EN_TO_REG(0,
13359431996fSJuerg Haefliger 							data->pwm_config[ix]);
1336dbc2bc25SJean Delvare 			dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
13379431996fSJuerg Haefliger 				      data->pwm_config[ix]);
13389431996fSJuerg Haefliger 			break;
13399431996fSJuerg Haefliger 		case 1:
13409431996fSJuerg Haefliger 			/* Turn on manual mode */
13419431996fSJuerg Haefliger 			data->pwm_config[ix] = PWM_EN_TO_REG(1,
13429431996fSJuerg Haefliger 							data->pwm_config[ix]);
1343dbc2bc25SJean Delvare 			dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
13449431996fSJuerg Haefliger 				      data->pwm_config[ix]);
13459431996fSJuerg Haefliger 			/* Change permissions of pwm[ix] to read-writeable */
134673ce48f6SJuerg Haefliger 			dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
13479431996fSJuerg Haefliger 					   S_IRUGO | S_IWUSR);
13489431996fSJuerg Haefliger 			break;
13499431996fSJuerg Haefliger 		case 2:
13509431996fSJuerg Haefliger 			/* Change permissions of pwm[ix] to read-only */
135173ce48f6SJuerg Haefliger 			dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
13529431996fSJuerg Haefliger 					   S_IRUGO);
1353c8de8362SGuenter Roeck 			/*
1354c8de8362SGuenter Roeck 			 * Turn on auto mode using the saved zone channel
1355c8de8362SGuenter Roeck 			 * assignment
1356c8de8362SGuenter Roeck 			 */
13579431996fSJuerg Haefliger 			data->pwm_config[ix] = PWM_ACZ_TO_REG(
13589431996fSJuerg Haefliger 							data->pwm_acz[ix],
13599431996fSJuerg Haefliger 							data->pwm_config[ix]);
1360dbc2bc25SJean Delvare 			dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
13619431996fSJuerg Haefliger 				      data->pwm_config[ix]);
13629431996fSJuerg Haefliger 			/* Enable PWM ramp rate if previously enabled */
13639431996fSJuerg Haefliger 			if (data->pwm_rr_en & (1 << ix)) {
13649431996fSJuerg Haefliger 				data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1365dbc2bc25SJean Delvare 						dme1737_read(data,
13669431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(ix > 0)));
1367dbc2bc25SJean Delvare 				dme1737_write(data,
13689431996fSJuerg Haefliger 					      DME1737_REG_PWM_RR(ix > 0),
13699431996fSJuerg Haefliger 					      data->pwm_rr[ix > 0]);
13709431996fSJuerg Haefliger 			}
13719431996fSJuerg Haefliger 			break;
13729431996fSJuerg Haefliger 		}
13739431996fSJuerg Haefliger 		break;
13749431996fSJuerg Haefliger 	case SYS_PWM_RAMP_RATE:
13759431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
13769431996fSJuerg Haefliger 		/* Refresh the cache */
1377dbc2bc25SJean Delvare 		data->pwm_config[ix] = dme1737_read(data,
13789431996fSJuerg Haefliger 						DME1737_REG_PWM_CONFIG(ix));
1379dbc2bc25SJean Delvare 		data->pwm_rr[ix > 0] = dme1737_read(data,
13809431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(ix > 0));
13819431996fSJuerg Haefliger 		/* Set the ramp rate value */
13829431996fSJuerg Haefliger 		if (val > 0) {
13839431996fSJuerg Haefliger 			data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
13849431996fSJuerg Haefliger 							data->pwm_rr[ix > 0]);
13859431996fSJuerg Haefliger 		}
1386c8de8362SGuenter Roeck 		/*
1387c8de8362SGuenter Roeck 		 * Enable/disable the feature only if the associated PWM
1388c8de8362SGuenter Roeck 		 * output is in automatic mode.
1389c8de8362SGuenter Roeck 		 */
13909431996fSJuerg Haefliger 		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
13919431996fSJuerg Haefliger 			data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
13929431996fSJuerg Haefliger 							data->pwm_rr[ix > 0]);
13939431996fSJuerg Haefliger 		}
1394dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
13959431996fSJuerg Haefliger 			      data->pwm_rr[ix > 0]);
13969431996fSJuerg Haefliger 		break;
13979431996fSJuerg Haefliger 	case SYS_PWM_AUTO_CHANNELS_ZONE:
13989431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
13999431996fSJuerg Haefliger 		if (!(val == 1 || val == 2 || val == 4 ||
14009431996fSJuerg Haefliger 		      val == 6 || val == 7)) {
14019431996fSJuerg Haefliger 			count = -EINVAL;
1402e95c237dSJuerg Haefliger 			dev_warn(dev, "PWM auto channels zone %ld "
14039431996fSJuerg Haefliger 				 "not supported. Choose one of 1, 2, 4, 6, "
14049431996fSJuerg Haefliger 				 "or 7.\n", val);
14059431996fSJuerg Haefliger 			goto exit;
14069431996fSJuerg Haefliger 		}
14079431996fSJuerg Haefliger 		/* Refresh the cache */
1408dbc2bc25SJean Delvare 		data->pwm_config[ix] = dme1737_read(data,
14099431996fSJuerg Haefliger 						DME1737_REG_PWM_CONFIG(ix));
14109431996fSJuerg Haefliger 		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1411c8de8362SGuenter Roeck 			/*
1412c8de8362SGuenter Roeck 			 * PWM is already in auto mode so update the temp
1413c8de8362SGuenter Roeck 			 * channel assignment
1414c8de8362SGuenter Roeck 			 */
14159431996fSJuerg Haefliger 			data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
14169431996fSJuerg Haefliger 						data->pwm_config[ix]);
1417dbc2bc25SJean Delvare 			dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
14189431996fSJuerg Haefliger 				      data->pwm_config[ix]);
14199431996fSJuerg Haefliger 		} else {
1420c8de8362SGuenter Roeck 			/*
1421c8de8362SGuenter Roeck 			 * PWM is not in auto mode so we save the temp
1422c8de8362SGuenter Roeck 			 * channel assignment for later use
1423c8de8362SGuenter Roeck 			 */
14249431996fSJuerg Haefliger 			data->pwm_acz[ix] = val;
14259431996fSJuerg Haefliger 		}
14269431996fSJuerg Haefliger 		break;
14279431996fSJuerg Haefliger 	case SYS_PWM_AUTO_PWM_MIN:
14289431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
14299431996fSJuerg Haefliger 		/* Refresh the cache */
1430dbc2bc25SJean Delvare 		data->pwm_min[ix] = dme1737_read(data,
14319431996fSJuerg Haefliger 						DME1737_REG_PWM_MIN(ix));
1432c8de8362SGuenter Roeck 		/*
1433c8de8362SGuenter Roeck 		 * There are only 2 values supported for the auto_pwm_min
14349431996fSJuerg Haefliger 		 * value: 0 or auto_point1_pwm. So if the temperature drops
14359431996fSJuerg Haefliger 		 * below the auto_point1_temp_hyst value, the fan either turns
1436c8de8362SGuenter Roeck 		 * off or runs at auto_point1_pwm duty-cycle.
1437c8de8362SGuenter Roeck 		 */
14389431996fSJuerg Haefliger 		if (val > ((data->pwm_min[ix] + 1) / 2)) {
14399431996fSJuerg Haefliger 			data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1440dbc2bc25SJean Delvare 						dme1737_read(data,
14419431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(0)));
14429431996fSJuerg Haefliger 		} else {
14439431996fSJuerg Haefliger 			data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1444dbc2bc25SJean Delvare 						dme1737_read(data,
14459431996fSJuerg Haefliger 						DME1737_REG_PWM_RR(0)));
14469431996fSJuerg Haefliger 		}
1447dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM_RR(0),
14489431996fSJuerg Haefliger 			      data->pwm_rr[0]);
14499431996fSJuerg Haefliger 		break;
14509431996fSJuerg Haefliger 	case SYS_PWM_AUTO_POINT1_PWM:
14519431996fSJuerg Haefliger 		/* Only valid for pwm[1-3] */
1452*2a844c14SGuenter Roeck 		data->pwm_min[ix] = clamp_val(val, 0, 255);
1453dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_PWM_MIN(ix),
14549431996fSJuerg Haefliger 			      data->pwm_min[ix]);
14559431996fSJuerg Haefliger 		break;
14569431996fSJuerg Haefliger 	default:
1457b237eb25SJuerg Haefliger 		dev_dbg(dev, "Unknown function %d.\n", fn);
14589431996fSJuerg Haefliger 	}
14599431996fSJuerg Haefliger exit:
14609431996fSJuerg Haefliger 	mutex_unlock(&data->update_lock);
14619431996fSJuerg Haefliger 
14629431996fSJuerg Haefliger 	return count;
14639431996fSJuerg Haefliger }
14649431996fSJuerg Haefliger 
14659431996fSJuerg Haefliger /* ---------------------------------------------------------------------
14669431996fSJuerg Haefliger  * Miscellaneous sysfs attributes
14679431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
14689431996fSJuerg Haefliger 
14699431996fSJuerg Haefliger static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
14709431996fSJuerg Haefliger 			char *buf)
14719431996fSJuerg Haefliger {
14729431996fSJuerg Haefliger 	struct i2c_client *client = to_i2c_client(dev);
14739431996fSJuerg Haefliger 	struct dme1737_data *data = i2c_get_clientdata(client);
14749431996fSJuerg Haefliger 
14759431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", data->vrm);
14769431996fSJuerg Haefliger }
14779431996fSJuerg Haefliger 
14789431996fSJuerg Haefliger static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
14799431996fSJuerg Haefliger 		       const char *buf, size_t count)
14809431996fSJuerg Haefliger {
1481b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
1482c8de8362SGuenter Roeck 	long val;
1483c8de8362SGuenter Roeck 	int err;
1484c8de8362SGuenter Roeck 
1485c8de8362SGuenter Roeck 	err = kstrtol(buf, 10, &val);
1486c8de8362SGuenter Roeck 	if (err)
1487c8de8362SGuenter Roeck 		return err;
14889431996fSJuerg Haefliger 
14899431996fSJuerg Haefliger 	data->vrm = val;
14909431996fSJuerg Haefliger 	return count;
14919431996fSJuerg Haefliger }
14929431996fSJuerg Haefliger 
14939431996fSJuerg Haefliger static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
14949431996fSJuerg Haefliger 			char *buf)
14959431996fSJuerg Haefliger {
14969431996fSJuerg Haefliger 	struct dme1737_data *data = dme1737_update_device(dev);
14979431996fSJuerg Haefliger 
14989431996fSJuerg Haefliger 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
14999431996fSJuerg Haefliger }
15009431996fSJuerg Haefliger 
1501e95c237dSJuerg Haefliger static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1502e95c237dSJuerg Haefliger 			 char *buf)
1503e95c237dSJuerg Haefliger {
1504e95c237dSJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
1505e95c237dSJuerg Haefliger 
1506dbc2bc25SJean Delvare 	return sprintf(buf, "%s\n", data->name);
1507e95c237dSJuerg Haefliger }
1508e95c237dSJuerg Haefliger 
15099431996fSJuerg Haefliger /* ---------------------------------------------------------------------
15109431996fSJuerg Haefliger  * Sysfs device attribute defines and structs
15119431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
15129431996fSJuerg Haefliger 
1513d4b94e1fSJuerg Haefliger /* Voltages 0-7 */
15149431996fSJuerg Haefliger 
15159431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_IN(ix) \
15169431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
15179431996fSJuerg Haefliger 	show_in, NULL, SYS_IN_INPUT, ix); \
15189431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
15199431996fSJuerg Haefliger 	show_in, set_in, SYS_IN_MIN, ix); \
15209431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
15219431996fSJuerg Haefliger 	show_in, set_in, SYS_IN_MAX, ix); \
15229431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
15239431996fSJuerg Haefliger 	show_in, NULL, SYS_IN_ALARM, ix)
15249431996fSJuerg Haefliger 
15259431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(0);
15269431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(1);
15279431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(2);
15289431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(3);
15299431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(4);
15309431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(5);
15319431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(6);
1532d4b94e1fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(7);
15339431996fSJuerg Haefliger 
15349431996fSJuerg Haefliger /* Temperatures 1-3 */
15359431996fSJuerg Haefliger 
15369431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_TEMP(ix) \
15379431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
15389431996fSJuerg Haefliger 	show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
15399431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
15409431996fSJuerg Haefliger 	show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
15419431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
15429431996fSJuerg Haefliger 	show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
15439431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
15449431996fSJuerg Haefliger 	show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
15459431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
15469431996fSJuerg Haefliger 	show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
15479431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
15489431996fSJuerg Haefliger 	show_temp, NULL, SYS_TEMP_FAULT, ix-1)
15499431996fSJuerg Haefliger 
15509431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(1);
15519431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(2);
15529431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(3);
15539431996fSJuerg Haefliger 
15549431996fSJuerg Haefliger /* Zones 1-3 */
15559431996fSJuerg Haefliger 
15569431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_ZONE(ix) \
15579431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
15589431996fSJuerg Haefliger 	show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
15599431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
15609431996fSJuerg Haefliger 	show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
15619431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
15629431996fSJuerg Haefliger 	show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
15639431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
15649431996fSJuerg Haefliger 	show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
15659431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
15669431996fSJuerg Haefliger 	show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
15679431996fSJuerg Haefliger 
15689431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(1);
15699431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(2);
15709431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(3);
15719431996fSJuerg Haefliger 
15729431996fSJuerg Haefliger /* Fans 1-4 */
15739431996fSJuerg Haefliger 
15749431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
15759431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
15769431996fSJuerg Haefliger 	show_fan, NULL, SYS_FAN_INPUT, ix-1); \
15779431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
15789431996fSJuerg Haefliger 	show_fan, set_fan, SYS_FAN_MIN, ix-1); \
15799431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
15809431996fSJuerg Haefliger 	show_fan, NULL, SYS_FAN_ALARM, ix-1); \
15819431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
15829431996fSJuerg Haefliger 	show_fan, set_fan, SYS_FAN_TYPE, ix-1)
15839431996fSJuerg Haefliger 
15849431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(1);
15859431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(2);
15869431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(3);
15879431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(4);
15889431996fSJuerg Haefliger 
15899431996fSJuerg Haefliger /* Fans 5-6 */
15909431996fSJuerg Haefliger 
15919431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
15929431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
15939431996fSJuerg Haefliger 	show_fan, NULL, SYS_FAN_INPUT, ix-1); \
15949431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
15959431996fSJuerg Haefliger 	show_fan, set_fan, SYS_FAN_MIN, ix-1); \
15969431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
15979431996fSJuerg Haefliger 	show_fan, NULL, SYS_FAN_ALARM, ix-1); \
15989431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
15999431996fSJuerg Haefliger 	show_fan, set_fan, SYS_FAN_MAX, ix-1)
16009431996fSJuerg Haefliger 
16019431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_5TO6(5);
16029431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_5TO6(6);
16039431996fSJuerg Haefliger 
16049431996fSJuerg Haefliger /* PWMs 1-3 */
16059431996fSJuerg Haefliger 
16069431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
16079431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
16089431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM, ix-1); \
16099431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
16109431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
16119431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
16129431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
16139431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
16149431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
16159431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
16169431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
16179431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
16189431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
16199431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
16209431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
16219431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
16229431996fSJuerg Haefliger 	show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
16239431996fSJuerg Haefliger 
16249431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(1);
16259431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(2);
16269431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(3);
16279431996fSJuerg Haefliger 
16289431996fSJuerg Haefliger /* PWMs 5-6 */
16299431996fSJuerg Haefliger 
16309431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
16319b257714SJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
16329431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM, ix-1); \
16339b257714SJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
16349431996fSJuerg Haefliger 	show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
16359431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
16369431996fSJuerg Haefliger 	show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
16379431996fSJuerg Haefliger 
16389431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_5TO6(5);
16399431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_5TO6(6);
16409431996fSJuerg Haefliger 
16419431996fSJuerg Haefliger /* Misc */
16429431996fSJuerg Haefliger 
16439431996fSJuerg Haefliger static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
16449431996fSJuerg Haefliger static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1645e95c237dSJuerg Haefliger static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);   /* for ISA devices */
16469431996fSJuerg Haefliger 
1647c8de8362SGuenter Roeck /*
1648c8de8362SGuenter Roeck  * This struct holds all the attributes that are always present and need to be
16499431996fSJuerg Haefliger  * created unconditionally. The attributes that need modification of their
16509431996fSJuerg Haefliger  * permissions are created read-only and write permissions are added or removed
1651c8de8362SGuenter Roeck  * on the fly when required
1652c8de8362SGuenter Roeck  */
16539431996fSJuerg Haefliger static struct attribute *dme1737_attr[] = {
16549431996fSJuerg Haefliger 	/* Voltages */
16559b257714SJuerg Haefliger 	&sensor_dev_attr_in0_input.dev_attr.attr,
16569b257714SJuerg Haefliger 	&sensor_dev_attr_in0_min.dev_attr.attr,
16579b257714SJuerg Haefliger 	&sensor_dev_attr_in0_max.dev_attr.attr,
16589b257714SJuerg Haefliger 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
16599b257714SJuerg Haefliger 	&sensor_dev_attr_in1_input.dev_attr.attr,
16609b257714SJuerg Haefliger 	&sensor_dev_attr_in1_min.dev_attr.attr,
16619b257714SJuerg Haefliger 	&sensor_dev_attr_in1_max.dev_attr.attr,
16629b257714SJuerg Haefliger 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
16639b257714SJuerg Haefliger 	&sensor_dev_attr_in2_input.dev_attr.attr,
16649b257714SJuerg Haefliger 	&sensor_dev_attr_in2_min.dev_attr.attr,
16659b257714SJuerg Haefliger 	&sensor_dev_attr_in2_max.dev_attr.attr,
16669b257714SJuerg Haefliger 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
16679b257714SJuerg Haefliger 	&sensor_dev_attr_in3_input.dev_attr.attr,
16689b257714SJuerg Haefliger 	&sensor_dev_attr_in3_min.dev_attr.attr,
16699b257714SJuerg Haefliger 	&sensor_dev_attr_in3_max.dev_attr.attr,
16709b257714SJuerg Haefliger 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
16719b257714SJuerg Haefliger 	&sensor_dev_attr_in4_input.dev_attr.attr,
16729b257714SJuerg Haefliger 	&sensor_dev_attr_in4_min.dev_attr.attr,
16739b257714SJuerg Haefliger 	&sensor_dev_attr_in4_max.dev_attr.attr,
16749b257714SJuerg Haefliger 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
16759b257714SJuerg Haefliger 	&sensor_dev_attr_in5_input.dev_attr.attr,
16769b257714SJuerg Haefliger 	&sensor_dev_attr_in5_min.dev_attr.attr,
16779b257714SJuerg Haefliger 	&sensor_dev_attr_in5_max.dev_attr.attr,
16789b257714SJuerg Haefliger 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
16799b257714SJuerg Haefliger 	&sensor_dev_attr_in6_input.dev_attr.attr,
16809b257714SJuerg Haefliger 	&sensor_dev_attr_in6_min.dev_attr.attr,
16819b257714SJuerg Haefliger 	&sensor_dev_attr_in6_max.dev_attr.attr,
16829b257714SJuerg Haefliger 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
16839431996fSJuerg Haefliger 	/* Temperatures */
16849b257714SJuerg Haefliger 	&sensor_dev_attr_temp1_input.dev_attr.attr,
16859b257714SJuerg Haefliger 	&sensor_dev_attr_temp1_min.dev_attr.attr,
16869b257714SJuerg Haefliger 	&sensor_dev_attr_temp1_max.dev_attr.attr,
16879b257714SJuerg Haefliger 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
16889b257714SJuerg Haefliger 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
16899b257714SJuerg Haefliger 	&sensor_dev_attr_temp2_input.dev_attr.attr,
16909b257714SJuerg Haefliger 	&sensor_dev_attr_temp2_min.dev_attr.attr,
16919b257714SJuerg Haefliger 	&sensor_dev_attr_temp2_max.dev_attr.attr,
16929b257714SJuerg Haefliger 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
16939b257714SJuerg Haefliger 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
16949b257714SJuerg Haefliger 	&sensor_dev_attr_temp3_input.dev_attr.attr,
16959b257714SJuerg Haefliger 	&sensor_dev_attr_temp3_min.dev_attr.attr,
16969b257714SJuerg Haefliger 	&sensor_dev_attr_temp3_max.dev_attr.attr,
16979b257714SJuerg Haefliger 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
16989b257714SJuerg Haefliger 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
16999431996fSJuerg Haefliger 	/* Zones */
17009b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
17019b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
17029b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
17039b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
17049b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
17059b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
17069b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
17079b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
1708549edb83SJuerg Haefliger 	NULL
1709549edb83SJuerg Haefliger };
1710549edb83SJuerg Haefliger 
1711549edb83SJuerg Haefliger static const struct attribute_group dme1737_group = {
1712549edb83SJuerg Haefliger 	.attrs = dme1737_attr,
1713549edb83SJuerg Haefliger };
1714549edb83SJuerg Haefliger 
1715c8de8362SGuenter Roeck /*
1716c8de8362SGuenter Roeck  * The following struct holds temp offset attributes, which are not available
1717ea694431SJuerg Haefliger  * in all chips. The following chips support them:
1718c8de8362SGuenter Roeck  * DME1737, SCH311x
1719c8de8362SGuenter Roeck  */
1720ea694431SJuerg Haefliger static struct attribute *dme1737_temp_offset_attr[] = {
1721549edb83SJuerg Haefliger 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
1722549edb83SJuerg Haefliger 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
1723549edb83SJuerg Haefliger 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
17249431996fSJuerg Haefliger 	NULL
17259431996fSJuerg Haefliger };
17269431996fSJuerg Haefliger 
1727ea694431SJuerg Haefliger static const struct attribute_group dme1737_temp_offset_group = {
1728ea694431SJuerg Haefliger 	.attrs = dme1737_temp_offset_attr,
17299431996fSJuerg Haefliger };
17309431996fSJuerg Haefliger 
1731c8de8362SGuenter Roeck /*
1732c8de8362SGuenter Roeck  * The following struct holds VID related attributes, which are not available
1733ea694431SJuerg Haefliger  * in all chips. The following chips support them:
1734c8de8362SGuenter Roeck  * DME1737
1735c8de8362SGuenter Roeck  */
17369d091446SJean Delvare static struct attribute *dme1737_vid_attr[] = {
17379d091446SJean Delvare 	&dev_attr_vrm.attr,
17389d091446SJean Delvare 	&dev_attr_cpu0_vid.attr,
17399d091446SJean Delvare 	NULL
17409d091446SJean Delvare };
17419d091446SJean Delvare 
17429d091446SJean Delvare static const struct attribute_group dme1737_vid_group = {
17439d091446SJean Delvare 	.attrs = dme1737_vid_attr,
17449d091446SJean Delvare };
17459d091446SJean Delvare 
1746c8de8362SGuenter Roeck /*
1747c8de8362SGuenter Roeck  * The following struct holds temp zone 3 related attributes, which are not
1748ea694431SJuerg Haefliger  * available in all chips. The following chips support them:
1749c8de8362SGuenter Roeck  * DME1737, SCH311x, SCH5027
1750c8de8362SGuenter Roeck  */
1751ea694431SJuerg Haefliger static struct attribute *dme1737_zone3_attr[] = {
1752ea694431SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1753ea694431SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1754ea694431SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1755ea694431SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1756ea694431SJuerg Haefliger 	NULL
1757ea694431SJuerg Haefliger };
1758ea694431SJuerg Haefliger 
1759ea694431SJuerg Haefliger static const struct attribute_group dme1737_zone3_group = {
1760ea694431SJuerg Haefliger 	.attrs = dme1737_zone3_attr,
1761ea694431SJuerg Haefliger };
1762ea694431SJuerg Haefliger 
1763ea694431SJuerg Haefliger 
1764c8de8362SGuenter Roeck /*
1765c8de8362SGuenter Roeck  * The following struct holds temp zone hysteresis related attributes, which
1766ea694431SJuerg Haefliger  * are not available in all chips. The following chips support them:
1767c8de8362SGuenter Roeck  * DME1737, SCH311x
1768c8de8362SGuenter Roeck  */
1769ea694431SJuerg Haefliger static struct attribute *dme1737_zone_hyst_attr[] = {
1770ea694431SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1771ea694431SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1772ea694431SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1773ea694431SJuerg Haefliger 	NULL
1774ea694431SJuerg Haefliger };
1775ea694431SJuerg Haefliger 
1776ea694431SJuerg Haefliger static const struct attribute_group dme1737_zone_hyst_group = {
1777ea694431SJuerg Haefliger 	.attrs = dme1737_zone_hyst_attr,
1778ea694431SJuerg Haefliger };
1779ea694431SJuerg Haefliger 
1780c8de8362SGuenter Roeck /*
1781c8de8362SGuenter Roeck  * The following struct holds voltage in7 related attributes, which
1782d4b94e1fSJuerg Haefliger  * are not available in all chips. The following chips support them:
1783c8de8362SGuenter Roeck  * SCH5127
1784c8de8362SGuenter Roeck  */
1785d4b94e1fSJuerg Haefliger static struct attribute *dme1737_in7_attr[] = {
1786d4b94e1fSJuerg Haefliger 	&sensor_dev_attr_in7_input.dev_attr.attr,
1787d4b94e1fSJuerg Haefliger 	&sensor_dev_attr_in7_min.dev_attr.attr,
1788d4b94e1fSJuerg Haefliger 	&sensor_dev_attr_in7_max.dev_attr.attr,
1789d4b94e1fSJuerg Haefliger 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
1790d4b94e1fSJuerg Haefliger 	NULL
1791d4b94e1fSJuerg Haefliger };
1792d4b94e1fSJuerg Haefliger 
1793d4b94e1fSJuerg Haefliger static const struct attribute_group dme1737_in7_group = {
1794d4b94e1fSJuerg Haefliger 	.attrs = dme1737_in7_attr,
1795d4b94e1fSJuerg Haefliger };
1796d4b94e1fSJuerg Haefliger 
1797c8de8362SGuenter Roeck /*
1798c8de8362SGuenter Roeck  * The following structs hold the PWM attributes, some of which are optional.
17999431996fSJuerg Haefliger  * Their creation depends on the chip configuration which is determined during
1800c8de8362SGuenter Roeck  * module load.
1801c8de8362SGuenter Roeck  */
180273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm1_attr[] = {
18039b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1.dev_attr.attr,
18049b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
18059b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
18069b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
18079b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
18089b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
18099b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
18109431996fSJuerg Haefliger 	NULL
18119431996fSJuerg Haefliger };
181273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm2_attr[] = {
18139b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2.dev_attr.attr,
18149b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
18159b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
18169b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
18179b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
18189b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
18199b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
18209431996fSJuerg Haefliger 	NULL
18219431996fSJuerg Haefliger };
182273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm3_attr[] = {
18239b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3.dev_attr.attr,
18249b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
18259b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
18269b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
18279b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
18289b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
18299b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
18309431996fSJuerg Haefliger 	NULL
18319431996fSJuerg Haefliger };
183273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm5_attr[] = {
18339b257714SJuerg Haefliger 	&sensor_dev_attr_pwm5.dev_attr.attr,
18349b257714SJuerg Haefliger 	&sensor_dev_attr_pwm5_freq.dev_attr.attr,
18359b257714SJuerg Haefliger 	&sensor_dev_attr_pwm5_enable.dev_attr.attr,
18369431996fSJuerg Haefliger 	NULL
18379431996fSJuerg Haefliger };
183873ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm6_attr[] = {
18399b257714SJuerg Haefliger 	&sensor_dev_attr_pwm6.dev_attr.attr,
18409b257714SJuerg Haefliger 	&sensor_dev_attr_pwm6_freq.dev_attr.attr,
18419b257714SJuerg Haefliger 	&sensor_dev_attr_pwm6_enable.dev_attr.attr,
18429431996fSJuerg Haefliger 	NULL
18439431996fSJuerg Haefliger };
18449431996fSJuerg Haefliger 
18459431996fSJuerg Haefliger static const struct attribute_group dme1737_pwm_group[] = {
184673ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm1_attr },
184773ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm2_attr },
184873ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm3_attr },
18499431996fSJuerg Haefliger 	{ .attrs = NULL },
185073ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm5_attr },
185173ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm6_attr },
18529431996fSJuerg Haefliger };
18539431996fSJuerg Haefliger 
1854c8de8362SGuenter Roeck /*
1855c8de8362SGuenter Roeck  * The following struct holds auto PWM min attributes, which are not available
1856ea694431SJuerg Haefliger  * in all chips. Their creation depends on the chip type which is determined
1857c8de8362SGuenter Roeck  * during module load.
1858c8de8362SGuenter Roeck  */
1859ea694431SJuerg Haefliger static struct attribute *dme1737_auto_pwm_min_attr[] = {
1860549edb83SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1861549edb83SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1862549edb83SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1863549edb83SJuerg Haefliger };
1864549edb83SJuerg Haefliger 
1865c8de8362SGuenter Roeck /*
1866c8de8362SGuenter Roeck  * The following structs hold the fan attributes, some of which are optional.
18679431996fSJuerg Haefliger  * Their creation depends on the chip configuration which is determined during
1868c8de8362SGuenter Roeck  * module load.
1869c8de8362SGuenter Roeck  */
187073ce48f6SJuerg Haefliger static struct attribute *dme1737_fan1_attr[] = {
18719b257714SJuerg Haefliger 	&sensor_dev_attr_fan1_input.dev_attr.attr,
18729b257714SJuerg Haefliger 	&sensor_dev_attr_fan1_min.dev_attr.attr,
18739b257714SJuerg Haefliger 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
18749b257714SJuerg Haefliger 	&sensor_dev_attr_fan1_type.dev_attr.attr,
18759431996fSJuerg Haefliger 	NULL
18769431996fSJuerg Haefliger };
187773ce48f6SJuerg Haefliger static struct attribute *dme1737_fan2_attr[] = {
18789b257714SJuerg Haefliger 	&sensor_dev_attr_fan2_input.dev_attr.attr,
18799b257714SJuerg Haefliger 	&sensor_dev_attr_fan2_min.dev_attr.attr,
18809b257714SJuerg Haefliger 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
18819b257714SJuerg Haefliger 	&sensor_dev_attr_fan2_type.dev_attr.attr,
18829431996fSJuerg Haefliger 	NULL
18839431996fSJuerg Haefliger };
188473ce48f6SJuerg Haefliger static struct attribute *dme1737_fan3_attr[] = {
18859b257714SJuerg Haefliger 	&sensor_dev_attr_fan3_input.dev_attr.attr,
18869b257714SJuerg Haefliger 	&sensor_dev_attr_fan3_min.dev_attr.attr,
18879b257714SJuerg Haefliger 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
18889b257714SJuerg Haefliger 	&sensor_dev_attr_fan3_type.dev_attr.attr,
18899431996fSJuerg Haefliger 	NULL
18909431996fSJuerg Haefliger };
189173ce48f6SJuerg Haefliger static struct attribute *dme1737_fan4_attr[] = {
18929b257714SJuerg Haefliger 	&sensor_dev_attr_fan4_input.dev_attr.attr,
18939b257714SJuerg Haefliger 	&sensor_dev_attr_fan4_min.dev_attr.attr,
18949b257714SJuerg Haefliger 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
18959b257714SJuerg Haefliger 	&sensor_dev_attr_fan4_type.dev_attr.attr,
18969431996fSJuerg Haefliger 	NULL
18979431996fSJuerg Haefliger };
189873ce48f6SJuerg Haefliger static struct attribute *dme1737_fan5_attr[] = {
18999b257714SJuerg Haefliger 	&sensor_dev_attr_fan5_input.dev_attr.attr,
19009b257714SJuerg Haefliger 	&sensor_dev_attr_fan5_min.dev_attr.attr,
19019b257714SJuerg Haefliger 	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
19029b257714SJuerg Haefliger 	&sensor_dev_attr_fan5_max.dev_attr.attr,
19039431996fSJuerg Haefliger 	NULL
19049431996fSJuerg Haefliger };
190573ce48f6SJuerg Haefliger static struct attribute *dme1737_fan6_attr[] = {
19069b257714SJuerg Haefliger 	&sensor_dev_attr_fan6_input.dev_attr.attr,
19079b257714SJuerg Haefliger 	&sensor_dev_attr_fan6_min.dev_attr.attr,
19089b257714SJuerg Haefliger 	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
19099b257714SJuerg Haefliger 	&sensor_dev_attr_fan6_max.dev_attr.attr,
19109431996fSJuerg Haefliger 	NULL
19119431996fSJuerg Haefliger };
19129431996fSJuerg Haefliger 
19139431996fSJuerg Haefliger static const struct attribute_group dme1737_fan_group[] = {
191473ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan1_attr },
191573ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan2_attr },
191673ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan3_attr },
191773ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan4_attr },
191873ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan5_attr },
191973ce48f6SJuerg Haefliger 	{ .attrs = dme1737_fan6_attr },
19209431996fSJuerg Haefliger };
19219431996fSJuerg Haefliger 
1922c8de8362SGuenter Roeck /*
1923c8de8362SGuenter Roeck  * The permissions of the following zone attributes are changed to read-
1924c8de8362SGuenter Roeck  * writeable if the chip is *not* locked. Otherwise they stay read-only.
1925c8de8362SGuenter Roeck  */
1926549edb83SJuerg Haefliger static struct attribute *dme1737_zone_chmod_attr[] = {
19279b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
19289b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
19299b257714SJuerg Haefliger 	&sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
19309b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
19319b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
19329b257714SJuerg Haefliger 	&sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1933ea694431SJuerg Haefliger 	NULL
1934ea694431SJuerg Haefliger };
1935ea694431SJuerg Haefliger 
1936ea694431SJuerg Haefliger static const struct attribute_group dme1737_zone_chmod_group = {
1937ea694431SJuerg Haefliger 	.attrs = dme1737_zone_chmod_attr,
1938ea694431SJuerg Haefliger };
1939ea694431SJuerg Haefliger 
1940ea694431SJuerg Haefliger 
1941c8de8362SGuenter Roeck /*
1942c8de8362SGuenter Roeck  * The permissions of the following zone 3 attributes are changed to read-
1943c8de8362SGuenter Roeck  * writeable if the chip is *not* locked. Otherwise they stay read-only.
1944c8de8362SGuenter Roeck  */
1945ea694431SJuerg Haefliger static struct attribute *dme1737_zone3_chmod_attr[] = {
19469b257714SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
19479b257714SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
19489b257714SJuerg Haefliger 	&sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
19499431996fSJuerg Haefliger 	NULL
19509431996fSJuerg Haefliger };
19519431996fSJuerg Haefliger 
1952ea694431SJuerg Haefliger static const struct attribute_group dme1737_zone3_chmod_group = {
1953ea694431SJuerg Haefliger 	.attrs = dme1737_zone3_chmod_attr,
19549431996fSJuerg Haefliger };
19559431996fSJuerg Haefliger 
1956c8de8362SGuenter Roeck /*
1957c8de8362SGuenter Roeck  * The permissions of the following PWM attributes are changed to read-
19589431996fSJuerg Haefliger  * writeable if the chip is *not* locked and the respective PWM is available.
1959c8de8362SGuenter Roeck  * Otherwise they stay read-only.
1960c8de8362SGuenter Roeck  */
196173ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm1_chmod_attr[] = {
19629b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
19639b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
19649b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
19659b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
19669b257714SJuerg Haefliger 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
19679431996fSJuerg Haefliger 	NULL
19689431996fSJuerg Haefliger };
196973ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm2_chmod_attr[] = {
19709b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
19719b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
19729b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
19739b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
19749b257714SJuerg Haefliger 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
19759431996fSJuerg Haefliger 	NULL
19769431996fSJuerg Haefliger };
197773ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm3_chmod_attr[] = {
19789b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
19799b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
19809b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
19819b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
19829b257714SJuerg Haefliger 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
19839431996fSJuerg Haefliger 	NULL
19849431996fSJuerg Haefliger };
198573ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm5_chmod_attr[] = {
19869b257714SJuerg Haefliger 	&sensor_dev_attr_pwm5.dev_attr.attr,
19879b257714SJuerg Haefliger 	&sensor_dev_attr_pwm5_freq.dev_attr.attr,
19889431996fSJuerg Haefliger 	NULL
19899431996fSJuerg Haefliger };
199073ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm6_chmod_attr[] = {
19919b257714SJuerg Haefliger 	&sensor_dev_attr_pwm6.dev_attr.attr,
19929b257714SJuerg Haefliger 	&sensor_dev_attr_pwm6_freq.dev_attr.attr,
19939431996fSJuerg Haefliger 	NULL
19949431996fSJuerg Haefliger };
19959431996fSJuerg Haefliger 
199673ce48f6SJuerg Haefliger static const struct attribute_group dme1737_pwm_chmod_group[] = {
199773ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm1_chmod_attr },
199873ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm2_chmod_attr },
199973ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm3_chmod_attr },
20009431996fSJuerg Haefliger 	{ .attrs = NULL },
200173ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm5_chmod_attr },
200273ce48f6SJuerg Haefliger 	{ .attrs = dme1737_pwm6_chmod_attr },
20039431996fSJuerg Haefliger };
20049431996fSJuerg Haefliger 
2005c8de8362SGuenter Roeck /*
2006c8de8362SGuenter Roeck  * Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
2007c8de8362SGuenter Roeck  * chip is not locked. Otherwise they are read-only.
2008c8de8362SGuenter Roeck  */
200973ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm_chmod_attr[] = {
20109431996fSJuerg Haefliger 	&sensor_dev_attr_pwm1.dev_attr.attr,
20119431996fSJuerg Haefliger 	&sensor_dev_attr_pwm2.dev_attr.attr,
20129431996fSJuerg Haefliger 	&sensor_dev_attr_pwm3.dev_attr.attr,
20139431996fSJuerg Haefliger };
20149431996fSJuerg Haefliger 
20159431996fSJuerg Haefliger /* ---------------------------------------------------------------------
20169431996fSJuerg Haefliger  * Super-IO functions
20179431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
20189431996fSJuerg Haefliger 
2019b237eb25SJuerg Haefliger static inline void dme1737_sio_enter(int sio_cip)
2020b237eb25SJuerg Haefliger {
2021b237eb25SJuerg Haefliger 	outb(0x55, sio_cip);
2022b237eb25SJuerg Haefliger }
2023b237eb25SJuerg Haefliger 
2024b237eb25SJuerg Haefliger static inline void dme1737_sio_exit(int sio_cip)
2025b237eb25SJuerg Haefliger {
2026b237eb25SJuerg Haefliger 	outb(0xaa, sio_cip);
2027b237eb25SJuerg Haefliger }
2028b237eb25SJuerg Haefliger 
20299431996fSJuerg Haefliger static inline int dme1737_sio_inb(int sio_cip, int reg)
20309431996fSJuerg Haefliger {
20319431996fSJuerg Haefliger 	outb(reg, sio_cip);
20329431996fSJuerg Haefliger 	return inb(sio_cip + 1);
20339431996fSJuerg Haefliger }
20349431996fSJuerg Haefliger 
20359431996fSJuerg Haefliger static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
20369431996fSJuerg Haefliger {
20379431996fSJuerg Haefliger 	outb(reg, sio_cip);
20389431996fSJuerg Haefliger 	outb(val, sio_cip + 1);
20399431996fSJuerg Haefliger }
20409431996fSJuerg Haefliger 
20419431996fSJuerg Haefliger /* ---------------------------------------------------------------------
2042e95c237dSJuerg Haefliger  * Device initialization
20439431996fSJuerg Haefliger  * --------------------------------------------------------------------- */
20449431996fSJuerg Haefliger 
204567e2f328SJuerg Haefliger static int dme1737_i2c_get_features(int, struct dme1737_data*);
20469431996fSJuerg Haefliger 
2047b237eb25SJuerg Haefliger static void dme1737_chmod_file(struct device *dev,
204848176a97SAl Viro 			       struct attribute *attr, umode_t mode)
20499431996fSJuerg Haefliger {
2050b237eb25SJuerg Haefliger 	if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
2051b237eb25SJuerg Haefliger 		dev_warn(dev, "Failed to change permissions of %s.\n",
20529431996fSJuerg Haefliger 			 attr->name);
20539431996fSJuerg Haefliger 	}
20549431996fSJuerg Haefliger }
20559431996fSJuerg Haefliger 
2056b237eb25SJuerg Haefliger static void dme1737_chmod_group(struct device *dev,
20579431996fSJuerg Haefliger 				const struct attribute_group *group,
205848176a97SAl Viro 				umode_t mode)
20599431996fSJuerg Haefliger {
20609431996fSJuerg Haefliger 	struct attribute **attr;
20619431996fSJuerg Haefliger 
2062c8de8362SGuenter Roeck 	for (attr = group->attrs; *attr; attr++)
2063b237eb25SJuerg Haefliger 		dme1737_chmod_file(dev, *attr, mode);
20649431996fSJuerg Haefliger }
20659431996fSJuerg Haefliger 
2066b237eb25SJuerg Haefliger static void dme1737_remove_files(struct device *dev)
20679431996fSJuerg Haefliger {
2068b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
2069b237eb25SJuerg Haefliger 	int ix;
2070b237eb25SJuerg Haefliger 
2071b237eb25SJuerg Haefliger 	for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
2072ea694431SJuerg Haefliger 		if (data->has_features & HAS_FAN(ix)) {
2073b237eb25SJuerg Haefliger 			sysfs_remove_group(&dev->kobj,
2074b237eb25SJuerg Haefliger 					   &dme1737_fan_group[ix]);
2075b237eb25SJuerg Haefliger 		}
2076b237eb25SJuerg Haefliger 	}
2077b237eb25SJuerg Haefliger 
2078b237eb25SJuerg Haefliger 	for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
2079ea694431SJuerg Haefliger 		if (data->has_features & HAS_PWM(ix)) {
2080b237eb25SJuerg Haefliger 			sysfs_remove_group(&dev->kobj,
2081b237eb25SJuerg Haefliger 					   &dme1737_pwm_group[ix]);
2082ea694431SJuerg Haefliger 			if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
2083549edb83SJuerg Haefliger 				sysfs_remove_file(&dev->kobj,
2084ea694431SJuerg Haefliger 						dme1737_auto_pwm_min_attr[ix]);
2085b237eb25SJuerg Haefliger 			}
2086b237eb25SJuerg Haefliger 		}
2087549edb83SJuerg Haefliger 	}
2088549edb83SJuerg Haefliger 
2089c8de8362SGuenter Roeck 	if (data->has_features & HAS_TEMP_OFFSET)
2090ea694431SJuerg Haefliger 		sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
2091c8de8362SGuenter Roeck 	if (data->has_features & HAS_VID)
20929d091446SJean Delvare 		sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
2093c8de8362SGuenter Roeck 	if (data->has_features & HAS_ZONE3)
2094ea694431SJuerg Haefliger 		sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
2095c8de8362SGuenter Roeck 	if (data->has_features & HAS_ZONE_HYST)
2096ea694431SJuerg Haefliger 		sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
2097c8de8362SGuenter Roeck 	if (data->has_features & HAS_IN7)
2098d4b94e1fSJuerg Haefliger 		sysfs_remove_group(&dev->kobj, &dme1737_in7_group);
2099b237eb25SJuerg Haefliger 	sysfs_remove_group(&dev->kobj, &dme1737_group);
2100e95c237dSJuerg Haefliger 
2101c8de8362SGuenter Roeck 	if (!data->client)
2102e95c237dSJuerg Haefliger 		sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
2103e95c237dSJuerg Haefliger }
2104b237eb25SJuerg Haefliger 
2105b237eb25SJuerg Haefliger static int dme1737_create_files(struct device *dev)
2106b237eb25SJuerg Haefliger {
2107b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
2108b237eb25SJuerg Haefliger 	int err, ix;
2109b237eb25SJuerg Haefliger 
2110e95c237dSJuerg Haefliger 	/* Create a name attribute for ISA devices */
211106f3d9fbSJuerg Haefliger 	if (!data->client) {
211206f3d9fbSJuerg Haefliger 		err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr);
2113c8de8362SGuenter Roeck 		if (err)
2114e95c237dSJuerg Haefliger 			goto exit;
2115e95c237dSJuerg Haefliger 	}
2116e95c237dSJuerg Haefliger 
2117b237eb25SJuerg Haefliger 	/* Create standard sysfs attributes */
211806f3d9fbSJuerg Haefliger 	err = sysfs_create_group(&dev->kobj, &dme1737_group);
2119c8de8362SGuenter Roeck 	if (err)
2120e95c237dSJuerg Haefliger 		goto exit_remove;
2121b237eb25SJuerg Haefliger 
2122ea694431SJuerg Haefliger 	/* Create chip-dependent sysfs attributes */
212306f3d9fbSJuerg Haefliger 	if (data->has_features & HAS_TEMP_OFFSET) {
212406f3d9fbSJuerg Haefliger 		err = sysfs_create_group(&dev->kobj,
212506f3d9fbSJuerg Haefliger 					 &dme1737_temp_offset_group);
2126c8de8362SGuenter Roeck 		if (err)
2127549edb83SJuerg Haefliger 			goto exit_remove;
2128549edb83SJuerg Haefliger 	}
212906f3d9fbSJuerg Haefliger 	if (data->has_features & HAS_VID) {
213006f3d9fbSJuerg Haefliger 		err = sysfs_create_group(&dev->kobj, &dme1737_vid_group);
2131c8de8362SGuenter Roeck 		if (err)
21329d091446SJean Delvare 			goto exit_remove;
21339d091446SJean Delvare 	}
213406f3d9fbSJuerg Haefliger 	if (data->has_features & HAS_ZONE3) {
213506f3d9fbSJuerg Haefliger 		err = sysfs_create_group(&dev->kobj, &dme1737_zone3_group);
2136c8de8362SGuenter Roeck 		if (err)
2137ea694431SJuerg Haefliger 			goto exit_remove;
2138ea694431SJuerg Haefliger 	}
213906f3d9fbSJuerg Haefliger 	if (data->has_features & HAS_ZONE_HYST) {
214006f3d9fbSJuerg Haefliger 		err = sysfs_create_group(&dev->kobj, &dme1737_zone_hyst_group);
2141c8de8362SGuenter Roeck 		if (err)
2142ea694431SJuerg Haefliger 			goto exit_remove;
2143ea694431SJuerg Haefliger 	}
2144d4b94e1fSJuerg Haefliger 	if (data->has_features & HAS_IN7) {
2145d4b94e1fSJuerg Haefliger 		err = sysfs_create_group(&dev->kobj, &dme1737_in7_group);
2146c8de8362SGuenter Roeck 		if (err)
2147d4b94e1fSJuerg Haefliger 			goto exit_remove;
2148d4b94e1fSJuerg Haefliger 	}
21499d091446SJean Delvare 
2150b237eb25SJuerg Haefliger 	/* Create fan sysfs attributes */
2151b237eb25SJuerg Haefliger 	for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
2152ea694431SJuerg Haefliger 		if (data->has_features & HAS_FAN(ix)) {
215306f3d9fbSJuerg Haefliger 			err = sysfs_create_group(&dev->kobj,
215406f3d9fbSJuerg Haefliger 						 &dme1737_fan_group[ix]);
2155c8de8362SGuenter Roeck 			if (err)
2156b237eb25SJuerg Haefliger 				goto exit_remove;
2157b237eb25SJuerg Haefliger 		}
2158b237eb25SJuerg Haefliger 	}
2159b237eb25SJuerg Haefliger 
2160b237eb25SJuerg Haefliger 	/* Create PWM sysfs attributes */
2161b237eb25SJuerg Haefliger 	for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
2162ea694431SJuerg Haefliger 		if (data->has_features & HAS_PWM(ix)) {
216306f3d9fbSJuerg Haefliger 			err = sysfs_create_group(&dev->kobj,
216406f3d9fbSJuerg Haefliger 						 &dme1737_pwm_group[ix]);
2165c8de8362SGuenter Roeck 			if (err)
2166b237eb25SJuerg Haefliger 				goto exit_remove;
216706f3d9fbSJuerg Haefliger 			if ((data->has_features & HAS_PWM_MIN) && (ix < 3)) {
216806f3d9fbSJuerg Haefliger 				err = sysfs_create_file(&dev->kobj,
216906f3d9fbSJuerg Haefliger 						dme1737_auto_pwm_min_attr[ix]);
2170c8de8362SGuenter Roeck 				if (err)
2171549edb83SJuerg Haefliger 					goto exit_remove;
2172549edb83SJuerg Haefliger 			}
2173b237eb25SJuerg Haefliger 		}
2174b237eb25SJuerg Haefliger 	}
2175b237eb25SJuerg Haefliger 
2176c8de8362SGuenter Roeck 	/*
2177c8de8362SGuenter Roeck 	 * Inform if the device is locked. Otherwise change the permissions of
2178c8de8362SGuenter Roeck 	 * selected attributes from read-only to read-writeable.
2179c8de8362SGuenter Roeck 	 */
2180b237eb25SJuerg Haefliger 	if (data->config & 0x02) {
2181b237eb25SJuerg Haefliger 		dev_info(dev, "Device is locked. Some attributes "
2182b237eb25SJuerg Haefliger 			 "will be read-only.\n");
2183b237eb25SJuerg Haefliger 	} else {
2184549edb83SJuerg Haefliger 		/* Change permissions of zone sysfs attributes */
2185549edb83SJuerg Haefliger 		dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
2186b237eb25SJuerg Haefliger 				    S_IRUGO | S_IWUSR);
2187b237eb25SJuerg Haefliger 
2188ea694431SJuerg Haefliger 		/* Change permissions of chip-dependent sysfs attributes */
2189ea694431SJuerg Haefliger 		if (data->has_features & HAS_TEMP_OFFSET) {
2190ea694431SJuerg Haefliger 			dme1737_chmod_group(dev, &dme1737_temp_offset_group,
2191ea694431SJuerg Haefliger 					    S_IRUGO | S_IWUSR);
2192ea694431SJuerg Haefliger 		}
2193ea694431SJuerg Haefliger 		if (data->has_features & HAS_ZONE3) {
2194ea694431SJuerg Haefliger 			dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
2195ea694431SJuerg Haefliger 					    S_IRUGO | S_IWUSR);
2196ea694431SJuerg Haefliger 		}
2197ea694431SJuerg Haefliger 		if (data->has_features & HAS_ZONE_HYST) {
2198ea694431SJuerg Haefliger 			dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
2199549edb83SJuerg Haefliger 					    S_IRUGO | S_IWUSR);
2200549edb83SJuerg Haefliger 		}
2201549edb83SJuerg Haefliger 
220273ce48f6SJuerg Haefliger 		/* Change permissions of PWM sysfs attributes */
220373ce48f6SJuerg Haefliger 		for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
2204ea694431SJuerg Haefliger 			if (data->has_features & HAS_PWM(ix)) {
2205b237eb25SJuerg Haefliger 				dme1737_chmod_group(dev,
220673ce48f6SJuerg Haefliger 						&dme1737_pwm_chmod_group[ix],
2207b237eb25SJuerg Haefliger 						S_IRUGO | S_IWUSR);
2208ea694431SJuerg Haefliger 				if ((data->has_features & HAS_PWM_MIN) &&
2209ea694431SJuerg Haefliger 				    ix < 3) {
2210549edb83SJuerg Haefliger 					dme1737_chmod_file(dev,
2211ea694431SJuerg Haefliger 						dme1737_auto_pwm_min_attr[ix],
2212549edb83SJuerg Haefliger 						S_IRUGO | S_IWUSR);
2213549edb83SJuerg Haefliger 				}
2214b237eb25SJuerg Haefliger 			}
2215b237eb25SJuerg Haefliger 		}
2216b237eb25SJuerg Haefliger 
2217b237eb25SJuerg Haefliger 		/* Change permissions of pwm[1-3] if in manual mode */
2218b237eb25SJuerg Haefliger 		for (ix = 0; ix < 3; ix++) {
2219ea694431SJuerg Haefliger 			if ((data->has_features & HAS_PWM(ix)) &&
2220b237eb25SJuerg Haefliger 			    (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
2221b237eb25SJuerg Haefliger 				dme1737_chmod_file(dev,
222273ce48f6SJuerg Haefliger 						dme1737_pwm_chmod_attr[ix],
2223b237eb25SJuerg Haefliger 						S_IRUGO | S_IWUSR);
2224b237eb25SJuerg Haefliger 			}
2225b237eb25SJuerg Haefliger 		}
2226b237eb25SJuerg Haefliger 	}
2227b237eb25SJuerg Haefliger 
2228b237eb25SJuerg Haefliger 	return 0;
2229b237eb25SJuerg Haefliger 
2230b237eb25SJuerg Haefliger exit_remove:
2231b237eb25SJuerg Haefliger 	dme1737_remove_files(dev);
2232b237eb25SJuerg Haefliger exit:
2233b237eb25SJuerg Haefliger 	return err;
2234b237eb25SJuerg Haefliger }
2235b237eb25SJuerg Haefliger 
2236b237eb25SJuerg Haefliger static int dme1737_init_device(struct device *dev)
2237b237eb25SJuerg Haefliger {
2238b237eb25SJuerg Haefliger 	struct dme1737_data *data = dev_get_drvdata(dev);
2239dbc2bc25SJean Delvare 	struct i2c_client *client = data->client;
22409431996fSJuerg Haefliger 	int ix;
22419431996fSJuerg Haefliger 	u8 reg;
22429431996fSJuerg Haefliger 
2243549edb83SJuerg Haefliger 	/* Point to the right nominal voltages array */
2244549edb83SJuerg Haefliger 	data->in_nominal = IN_NOMINAL(data->type);
2245549edb83SJuerg Haefliger 
2246dbc2bc25SJean Delvare 	data->config = dme1737_read(data, DME1737_REG_CONFIG);
22479431996fSJuerg Haefliger 	/* Inform if part is not monitoring/started */
22489431996fSJuerg Haefliger 	if (!(data->config & 0x01)) {
22499431996fSJuerg Haefliger 		if (!force_start) {
2250b237eb25SJuerg Haefliger 			dev_err(dev, "Device is not monitoring. "
22519431996fSJuerg Haefliger 				"Use the force_start load parameter to "
22529431996fSJuerg Haefliger 				"override.\n");
22539431996fSJuerg Haefliger 			return -EFAULT;
22549431996fSJuerg Haefliger 		}
22559431996fSJuerg Haefliger 
22569431996fSJuerg Haefliger 		/* Force monitoring */
22579431996fSJuerg Haefliger 		data->config |= 0x01;
2258dbc2bc25SJean Delvare 		dme1737_write(data, DME1737_REG_CONFIG, data->config);
22599431996fSJuerg Haefliger 	}
22609431996fSJuerg Haefliger 	/* Inform if part is not ready */
22619431996fSJuerg Haefliger 	if (!(data->config & 0x04)) {
2262b237eb25SJuerg Haefliger 		dev_err(dev, "Device is not ready.\n");
22639431996fSJuerg Haefliger 		return -EFAULT;
22649431996fSJuerg Haefliger 	}
22659431996fSJuerg Haefliger 
2266c8de8362SGuenter Roeck 	/*
2267c8de8362SGuenter Roeck 	 * Determine which optional fan and pwm features are enabled (only
2268c8de8362SGuenter Roeck 	 * valid for I2C devices)
2269c8de8362SGuenter Roeck 	 */
2270dbc2bc25SJean Delvare 	if (client) {   /* I2C chip */
2271dbc2bc25SJean Delvare 		data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
22729431996fSJuerg Haefliger 		/* Check if optional fan3 input is enabled */
2273c8de8362SGuenter Roeck 		if (data->config2 & 0x04)
2274ea694431SJuerg Haefliger 			data->has_features |= HAS_FAN(2);
22759431996fSJuerg Haefliger 
2276c8de8362SGuenter Roeck 		/*
2277c8de8362SGuenter Roeck 		 * Fan4 and pwm3 are only available if the client's I2C address
2278e95c237dSJuerg Haefliger 		 * is the default 0x2e. Otherwise the I/Os associated with
2279c8de8362SGuenter Roeck 		 * these functions are used for addr enable/select.
2280c8de8362SGuenter Roeck 		 */
2281c8de8362SGuenter Roeck 		if (client->addr == 0x2e)
2282ea694431SJuerg Haefliger 			data->has_features |= HAS_FAN(3) | HAS_PWM(2);
22839431996fSJuerg Haefliger 
2284c8de8362SGuenter Roeck 		/*
2285c8de8362SGuenter Roeck 		 * Determine which of the optional fan[5-6] and pwm[5-6]
2286e95c237dSJuerg Haefliger 		 * features are enabled. For this, we need to query the runtime
2287e95c237dSJuerg Haefliger 		 * registers through the Super-IO LPC interface. Try both
2288c8de8362SGuenter Roeck 		 * config ports 0x2e and 0x4e.
2289c8de8362SGuenter Roeck 		 */
2290b237eb25SJuerg Haefliger 		if (dme1737_i2c_get_features(0x2e, data) &&
2291b237eb25SJuerg Haefliger 		    dme1737_i2c_get_features(0x4e, data)) {
2292b237eb25SJuerg Haefliger 			dev_warn(dev, "Failed to query Super-IO for optional "
22939431996fSJuerg Haefliger 				 "features.\n");
22949431996fSJuerg Haefliger 		}
2295e95c237dSJuerg Haefliger 	}
22969431996fSJuerg Haefliger 
2297ea694431SJuerg Haefliger 	/* Fan[1-2] and pwm[1-2] are present in all chips */
2298ea694431SJuerg Haefliger 	data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);
2299ea694431SJuerg Haefliger 
2300ea694431SJuerg Haefliger 	/* Chip-dependent features */
2301ea694431SJuerg Haefliger 	switch (data->type) {
2302ea694431SJuerg Haefliger 	case dme1737:
2303ea694431SJuerg Haefliger 		data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
2304ea694431SJuerg Haefliger 			HAS_ZONE_HYST | HAS_PWM_MIN;
2305ea694431SJuerg Haefliger 		break;
2306ea694431SJuerg Haefliger 	case sch311x:
2307ea694431SJuerg Haefliger 		data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
2308ea694431SJuerg Haefliger 			HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
2309ea694431SJuerg Haefliger 		break;
2310ea694431SJuerg Haefliger 	case sch5027:
2311ea694431SJuerg Haefliger 		data->has_features |= HAS_ZONE3;
2312ea694431SJuerg Haefliger 		break;
2313ea694431SJuerg Haefliger 	case sch5127:
2314d4b94e1fSJuerg Haefliger 		data->has_features |= HAS_FAN(2) | HAS_PWM(2) | HAS_IN7;
2315ea694431SJuerg Haefliger 		break;
2316ea694431SJuerg Haefliger 	default:
2317ea694431SJuerg Haefliger 		break;
2318ea694431SJuerg Haefliger 	}
23199431996fSJuerg Haefliger 
2320b237eb25SJuerg Haefliger 	dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
23219431996fSJuerg Haefliger 		 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
2322ea694431SJuerg Haefliger 		 (data->has_features & HAS_PWM(2)) ? "yes" : "no",
2323ea694431SJuerg Haefliger 		 (data->has_features & HAS_PWM(4)) ? "yes" : "no",
2324ea694431SJuerg Haefliger 		 (data->has_features & HAS_PWM(5)) ? "yes" : "no",
2325ea694431SJuerg Haefliger 		 (data->has_features & HAS_FAN(2)) ? "yes" : "no",
2326ea694431SJuerg Haefliger 		 (data->has_features & HAS_FAN(3)) ? "yes" : "no",
2327ea694431SJuerg Haefliger 		 (data->has_features & HAS_FAN(4)) ? "yes" : "no",
2328ea694431SJuerg Haefliger 		 (data->has_features & HAS_FAN(5)) ? "yes" : "no");
23299431996fSJuerg Haefliger 
2330dbc2bc25SJean Delvare 	reg = dme1737_read(data, DME1737_REG_TACH_PWM);
23319431996fSJuerg Haefliger 	/* Inform if fan-to-pwm mapping differs from the default */
2332dbc2bc25SJean Delvare 	if (client && reg != 0xa4) {   /* I2C chip */
2333b237eb25SJuerg Haefliger 		dev_warn(dev, "Non-standard fan to pwm mapping: "
23349431996fSJuerg Haefliger 			 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
23359431996fSJuerg Haefliger 			 "fan4->pwm%d. Please report to the driver "
23369431996fSJuerg Haefliger 			 "maintainer.\n",
23379431996fSJuerg Haefliger 			 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
23389431996fSJuerg Haefliger 			 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
2339dbc2bc25SJean Delvare 	} else if (!client && reg != 0x24) {   /* ISA chip */
2340e95c237dSJuerg Haefliger 		dev_warn(dev, "Non-standard fan to pwm mapping: "
2341e95c237dSJuerg Haefliger 			 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2342e95c237dSJuerg Haefliger 			 "Please report to the driver maintainer.\n",
2343e95c237dSJuerg Haefliger 			 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2344e95c237dSJuerg Haefliger 			 ((reg >> 4) & 0x03) + 1);
23459431996fSJuerg Haefliger 	}
23469431996fSJuerg Haefliger 
2347c8de8362SGuenter Roeck 	/*
2348c8de8362SGuenter Roeck 	 * Switch pwm[1-3] to manual mode if they are currently disabled and
23499431996fSJuerg Haefliger 	 * set the duty-cycles to 0% (which is identical to the PWMs being
2350c8de8362SGuenter Roeck 	 * disabled).
2351c8de8362SGuenter Roeck 	 */
23529431996fSJuerg Haefliger 	if (!(data->config & 0x02)) {
23539431996fSJuerg Haefliger 		for (ix = 0; ix < 3; ix++) {
2354dbc2bc25SJean Delvare 			data->pwm_config[ix] = dme1737_read(data,
23559431996fSJuerg Haefliger 						DME1737_REG_PWM_CONFIG(ix));
2356ea694431SJuerg Haefliger 			if ((data->has_features & HAS_PWM(ix)) &&
23579431996fSJuerg Haefliger 			    (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
2358b237eb25SJuerg Haefliger 				dev_info(dev, "Switching pwm%d to "
23599431996fSJuerg Haefliger 					 "manual mode.\n", ix + 1);
23609431996fSJuerg Haefliger 				data->pwm_config[ix] = PWM_EN_TO_REG(1,
23619431996fSJuerg Haefliger 							data->pwm_config[ix]);
2362dbc2bc25SJean Delvare 				dme1737_write(data, DME1737_REG_PWM(ix), 0);
2363dbc2bc25SJean Delvare 				dme1737_write(data,
23649431996fSJuerg Haefliger 					      DME1737_REG_PWM_CONFIG(ix),
23659431996fSJuerg Haefliger 					      data->pwm_config[ix]);
23669431996fSJuerg Haefliger 			}
23679431996fSJuerg Haefliger 		}
23689431996fSJuerg Haefliger 	}
23699431996fSJuerg Haefliger 
23709431996fSJuerg Haefliger 	/* Initialize the default PWM auto channels zone (acz) assignments */
23719431996fSJuerg Haefliger 	data->pwm_acz[0] = 1;	/* pwm1 -> zone1 */
23729431996fSJuerg Haefliger 	data->pwm_acz[1] = 2;	/* pwm2 -> zone2 */
23739431996fSJuerg Haefliger 	data->pwm_acz[2] = 4;	/* pwm3 -> zone3 */
23749431996fSJuerg Haefliger 
23759431996fSJuerg Haefliger 	/* Set VRM */
2376c8de8362SGuenter Roeck 	if (data->has_features & HAS_VID)
23779431996fSJuerg Haefliger 		data->vrm = vid_which_vrm();
23789431996fSJuerg Haefliger 
23799431996fSJuerg Haefliger 	return 0;
23809431996fSJuerg Haefliger }
23819431996fSJuerg Haefliger 
238267e2f328SJuerg Haefliger /* ---------------------------------------------------------------------
238367e2f328SJuerg Haefliger  * I2C device detection and registration
238467e2f328SJuerg Haefliger  * --------------------------------------------------------------------- */
238567e2f328SJuerg Haefliger 
238667e2f328SJuerg Haefliger static struct i2c_driver dme1737_i2c_driver;
238767e2f328SJuerg Haefliger 
238867e2f328SJuerg Haefliger static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
238967e2f328SJuerg Haefliger {
23909431996fSJuerg Haefliger 	int err = 0, reg;
23919431996fSJuerg Haefliger 	u16 addr;
23929431996fSJuerg Haefliger 
239367e2f328SJuerg Haefliger 	dme1737_sio_enter(sio_cip);
23949431996fSJuerg Haefliger 
2395c8de8362SGuenter Roeck 	/*
2396c8de8362SGuenter Roeck 	 * Check device ID
2397c8de8362SGuenter Roeck 	 * We currently know about two kinds of DME1737 and SCH5027.
2398c8de8362SGuenter Roeck 	 */
2399345a2224SJuerg Haefliger 	reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2400ea694431SJuerg Haefliger 	if (!(reg == DME1737_ID_1 || reg == DME1737_ID_2 ||
2401ea694431SJuerg Haefliger 	      reg == SCH5027_ID)) {
24029431996fSJuerg Haefliger 		err = -ENODEV;
24039431996fSJuerg Haefliger 		goto exit;
24049431996fSJuerg Haefliger 	}
24059431996fSJuerg Haefliger 
24069431996fSJuerg Haefliger 	/* Select logical device A (runtime registers) */
24079431996fSJuerg Haefliger 	dme1737_sio_outb(sio_cip, 0x07, 0x0a);
24089431996fSJuerg Haefliger 
24099431996fSJuerg Haefliger 	/* Get the base address of the runtime registers */
241006f3d9fbSJuerg Haefliger 	addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
241106f3d9fbSJuerg Haefliger 		dme1737_sio_inb(sio_cip, 0x61);
241206f3d9fbSJuerg Haefliger 	if (!addr) {
24139431996fSJuerg Haefliger 		err = -ENODEV;
24149431996fSJuerg Haefliger 		goto exit;
24159431996fSJuerg Haefliger 	}
24169431996fSJuerg Haefliger 
2417c8de8362SGuenter Roeck 	/*
2418c8de8362SGuenter Roeck 	 * Read the runtime registers to determine which optional features
24199431996fSJuerg Haefliger 	 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2420c8de8362SGuenter Roeck 	 * to '10' if the respective feature is enabled.
2421c8de8362SGuenter Roeck 	 */
2422c8de8362SGuenter Roeck 	if ((inb(addr + 0x43) & 0x0c) == 0x08) /* fan6 */
2423ea694431SJuerg Haefliger 		data->has_features |= HAS_FAN(5);
2424c8de8362SGuenter Roeck 	if ((inb(addr + 0x44) & 0x0c) == 0x08) /* pwm6 */
2425ea694431SJuerg Haefliger 		data->has_features |= HAS_PWM(5);
2426c8de8362SGuenter Roeck 	if ((inb(addr + 0x45) & 0x0c) == 0x08) /* fan5 */
2427ea694431SJuerg Haefliger 		data->has_features |= HAS_FAN(4);
2428c8de8362SGuenter Roeck 	if ((inb(addr + 0x46) & 0x0c) == 0x08) /* pwm5 */
2429ea694431SJuerg Haefliger 		data->has_features |= HAS_PWM(4);
24309431996fSJuerg Haefliger 
24319431996fSJuerg Haefliger exit:
243267e2f328SJuerg Haefliger 	dme1737_sio_exit(sio_cip);
24339431996fSJuerg Haefliger 
24349431996fSJuerg Haefliger 	return err;
24359431996fSJuerg Haefliger }
24369431996fSJuerg Haefliger 
243767a37308SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
2438310ec792SJean Delvare static int dme1737_i2c_detect(struct i2c_client *client,
243967a37308SJean Delvare 			      struct i2c_board_info *info)
24409431996fSJuerg Haefliger {
244167a37308SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
244267a37308SJean Delvare 	struct device *dev = &adapter->dev;
24439431996fSJuerg Haefliger 	u8 company, verstep = 0;
24449431996fSJuerg Haefliger 	const char *name;
24459431996fSJuerg Haefliger 
2446c8de8362SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
244767a37308SJean Delvare 		return -ENODEV;
24489431996fSJuerg Haefliger 
244967a37308SJean Delvare 	company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
245067a37308SJean Delvare 	verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);
24519431996fSJuerg Haefliger 
2452549edb83SJuerg Haefliger 	if (company == DME1737_COMPANY_SMSC &&
2453549edb83SJuerg Haefliger 	    verstep == SCH5027_VERSTEP) {
245452df6440SJean Delvare 		name = "sch5027";
245552df6440SJean Delvare 	} else if (company == DME1737_COMPANY_SMSC &&
245652df6440SJean Delvare 		   (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
245752df6440SJean Delvare 		name = "dme1737";
2458549edb83SJuerg Haefliger 	} else {
245967a37308SJean Delvare 		return -ENODEV;
24609431996fSJuerg Haefliger 	}
24619431996fSJuerg Haefliger 
2462549edb83SJuerg Haefliger 	dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
246352df6440SJean Delvare 		 verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737",
246452df6440SJean Delvare 		 client->addr, verstep);
246567a37308SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
246667a37308SJean Delvare 
246767a37308SJean Delvare 	return 0;
246867a37308SJean Delvare }
246967a37308SJean Delvare 
247067a37308SJean Delvare static int dme1737_i2c_probe(struct i2c_client *client,
247167a37308SJean Delvare 			     const struct i2c_device_id *id)
247267a37308SJean Delvare {
247367a37308SJean Delvare 	struct dme1737_data *data;
247467a37308SJean Delvare 	struct device *dev = &client->dev;
247567a37308SJean Delvare 	int err;
247667a37308SJean Delvare 
2477805fd8c5SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct dme1737_data), GFP_KERNEL);
2478805fd8c5SGuenter Roeck 	if (!data)
2479805fd8c5SGuenter Roeck 		return -ENOMEM;
248067a37308SJean Delvare 
248167a37308SJean Delvare 	i2c_set_clientdata(client, data);
248267a37308SJean Delvare 	data->type = id->driver_data;
248367a37308SJean Delvare 	data->client = client;
248467a37308SJean Delvare 	data->name = client->name;
248567a37308SJean Delvare 	mutex_init(&data->update_lock);
2486b237eb25SJuerg Haefliger 
24879431996fSJuerg Haefliger 	/* Initialize the DME1737 chip */
248806f3d9fbSJuerg Haefliger 	err = dme1737_init_device(dev);
248906f3d9fbSJuerg Haefliger 	if (err) {
2490b237eb25SJuerg Haefliger 		dev_err(dev, "Failed to initialize device.\n");
2491805fd8c5SGuenter Roeck 		return err;
24929431996fSJuerg Haefliger 	}
24939431996fSJuerg Haefliger 
2494b237eb25SJuerg Haefliger 	/* Create sysfs files */
249506f3d9fbSJuerg Haefliger 	err = dme1737_create_files(dev);
249606f3d9fbSJuerg Haefliger 	if (err) {
2497b237eb25SJuerg Haefliger 		dev_err(dev, "Failed to create sysfs files.\n");
2498805fd8c5SGuenter Roeck 		return err;
24999431996fSJuerg Haefliger 	}
25009431996fSJuerg Haefliger 
25019431996fSJuerg Haefliger 	/* Register device */
250262ee3e10SMark M. Hoffman 	data->hwmon_dev = hwmon_device_register(dev);
25031beeffe4STony Jones 	if (IS_ERR(data->hwmon_dev)) {
2504b237eb25SJuerg Haefliger 		dev_err(dev, "Failed to register device.\n");
25051beeffe4STony Jones 		err = PTR_ERR(data->hwmon_dev);
25069431996fSJuerg Haefliger 		goto exit_remove;
25079431996fSJuerg Haefliger 	}
25089431996fSJuerg Haefliger 
25099431996fSJuerg Haefliger 	return 0;
25109431996fSJuerg Haefliger 
25119431996fSJuerg Haefliger exit_remove:
2512b237eb25SJuerg Haefliger 	dme1737_remove_files(dev);
25139431996fSJuerg Haefliger 	return err;
25149431996fSJuerg Haefliger }
25159431996fSJuerg Haefliger 
251667a37308SJean Delvare static int dme1737_i2c_remove(struct i2c_client *client)
25179431996fSJuerg Haefliger {
25189431996fSJuerg Haefliger 	struct dme1737_data *data = i2c_get_clientdata(client);
25199431996fSJuerg Haefliger 
25201beeffe4STony Jones 	hwmon_device_unregister(data->hwmon_dev);
2521b237eb25SJuerg Haefliger 	dme1737_remove_files(&client->dev);
25229431996fSJuerg Haefliger 
25239431996fSJuerg Haefliger 	return 0;
25249431996fSJuerg Haefliger }
25259431996fSJuerg Haefliger 
252667a37308SJean Delvare static const struct i2c_device_id dme1737_id[] = {
252767a37308SJean Delvare 	{ "dme1737", dme1737 },
252867a37308SJean Delvare 	{ "sch5027", sch5027 },
252967a37308SJean Delvare 	{ }
253067a37308SJean Delvare };
253167a37308SJean Delvare MODULE_DEVICE_TABLE(i2c, dme1737_id);
253267a37308SJean Delvare 
2533b237eb25SJuerg Haefliger static struct i2c_driver dme1737_i2c_driver = {
253467a37308SJean Delvare 	.class = I2C_CLASS_HWMON,
25359431996fSJuerg Haefliger 	.driver = {
25369431996fSJuerg Haefliger 		.name = "dme1737",
25379431996fSJuerg Haefliger 	},
253867a37308SJean Delvare 	.probe = dme1737_i2c_probe,
253967a37308SJean Delvare 	.remove = dme1737_i2c_remove,
254067a37308SJean Delvare 	.id_table = dme1737_id,
254167a37308SJean Delvare 	.detect = dme1737_i2c_detect,
2542c3813d6aSJean Delvare 	.address_list = normal_i2c,
25439431996fSJuerg Haefliger };
25449431996fSJuerg Haefliger 
254567e2f328SJuerg Haefliger /* ---------------------------------------------------------------------
2546e95c237dSJuerg Haefliger  * ISA device detection and registration
2547e95c237dSJuerg Haefliger  * --------------------------------------------------------------------- */
2548e95c237dSJuerg Haefliger 
2549e95c237dSJuerg Haefliger static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2550e95c237dSJuerg Haefliger {
2551e95c237dSJuerg Haefliger 	int err = 0, reg;
2552e95c237dSJuerg Haefliger 	unsigned short base_addr;
2553e95c237dSJuerg Haefliger 
2554e95c237dSJuerg Haefliger 	dme1737_sio_enter(sio_cip);
2555e95c237dSJuerg Haefliger 
2556c8de8362SGuenter Roeck 	/*
2557c8de8362SGuenter Roeck 	 * Check device ID
2558c8de8362SGuenter Roeck 	 * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127
2559c8de8362SGuenter Roeck 	 */
256067b671bcSJean Delvare 	reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2561ea694431SJuerg Haefliger 	if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
2562ea694431SJuerg Haefliger 	      reg == SCH5127_ID)) {
2563e95c237dSJuerg Haefliger 		err = -ENODEV;
2564e95c237dSJuerg Haefliger 		goto exit;
2565e95c237dSJuerg Haefliger 	}
2566e95c237dSJuerg Haefliger 
2567e95c237dSJuerg Haefliger 	/* Select logical device A (runtime registers) */
2568e95c237dSJuerg Haefliger 	dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2569e95c237dSJuerg Haefliger 
2570e95c237dSJuerg Haefliger 	/* Get the base address of the runtime registers */
257106f3d9fbSJuerg Haefliger 	base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
257206f3d9fbSJuerg Haefliger 		     dme1737_sio_inb(sio_cip, 0x61);
257306f3d9fbSJuerg Haefliger 	if (!base_addr) {
25749c6e13b4SJoe Perches 		pr_err("Base address not set\n");
2575e95c237dSJuerg Haefliger 		err = -ENODEV;
2576e95c237dSJuerg Haefliger 		goto exit;
2577e95c237dSJuerg Haefliger 	}
2578e95c237dSJuerg Haefliger 
2579c8de8362SGuenter Roeck 	/*
2580c8de8362SGuenter Roeck 	 * Access to the hwmon registers is through an index/data register
2581c8de8362SGuenter Roeck 	 * pair located at offset 0x70/0x71.
2582c8de8362SGuenter Roeck 	 */
2583e95c237dSJuerg Haefliger 	*addr = base_addr + 0x70;
2584e95c237dSJuerg Haefliger 
2585e95c237dSJuerg Haefliger exit:
2586e95c237dSJuerg Haefliger 	dme1737_sio_exit(sio_cip);
2587e95c237dSJuerg Haefliger 	return err;
2588e95c237dSJuerg Haefliger }
2589e95c237dSJuerg Haefliger 
2590e95c237dSJuerg Haefliger static int __init dme1737_isa_device_add(unsigned short addr)
2591e95c237dSJuerg Haefliger {
2592e95c237dSJuerg Haefliger 	struct resource res = {
2593e95c237dSJuerg Haefliger 		.start	= addr,
2594e95c237dSJuerg Haefliger 		.end	= addr + DME1737_EXTENT - 1,
2595e95c237dSJuerg Haefliger 		.name	= "dme1737",
2596e95c237dSJuerg Haefliger 		.flags	= IORESOURCE_IO,
2597e95c237dSJuerg Haefliger 	};
2598e95c237dSJuerg Haefliger 	int err;
2599e95c237dSJuerg Haefliger 
2600b9acb64aSJean Delvare 	err = acpi_check_resource_conflict(&res);
2601b9acb64aSJean Delvare 	if (err)
2602b9acb64aSJean Delvare 		goto exit;
2603b9acb64aSJean Delvare 
260406f3d9fbSJuerg Haefliger 	pdev = platform_device_alloc("dme1737", addr);
260506f3d9fbSJuerg Haefliger 	if (!pdev) {
26069c6e13b4SJoe Perches 		pr_err("Failed to allocate device\n");
2607e95c237dSJuerg Haefliger 		err = -ENOMEM;
2608e95c237dSJuerg Haefliger 		goto exit;
2609e95c237dSJuerg Haefliger 	}
2610e95c237dSJuerg Haefliger 
261106f3d9fbSJuerg Haefliger 	err = platform_device_add_resources(pdev, &res, 1);
261206f3d9fbSJuerg Haefliger 	if (err) {
26139c6e13b4SJoe Perches 		pr_err("Failed to add device resource (err = %d)\n", err);
2614e95c237dSJuerg Haefliger 		goto exit_device_put;
2615e95c237dSJuerg Haefliger 	}
2616e95c237dSJuerg Haefliger 
261706f3d9fbSJuerg Haefliger 	err = platform_device_add(pdev);
261806f3d9fbSJuerg Haefliger 	if (err) {
26199c6e13b4SJoe Perches 		pr_err("Failed to add device (err = %d)\n", err);
2620e95c237dSJuerg Haefliger 		goto exit_device_put;
2621e95c237dSJuerg Haefliger 	}
2622e95c237dSJuerg Haefliger 
2623e95c237dSJuerg Haefliger 	return 0;
2624e95c237dSJuerg Haefliger 
2625e95c237dSJuerg Haefliger exit_device_put:
2626e95c237dSJuerg Haefliger 	platform_device_put(pdev);
2627e95c237dSJuerg Haefliger 	pdev = NULL;
2628e95c237dSJuerg Haefliger exit:
2629e95c237dSJuerg Haefliger 	return err;
2630e95c237dSJuerg Haefliger }
2631e95c237dSJuerg Haefliger 
26326c931ae1SBill Pemberton static int dme1737_isa_probe(struct platform_device *pdev)
2633e95c237dSJuerg Haefliger {
2634e95c237dSJuerg Haefliger 	u8 company, device;
2635e95c237dSJuerg Haefliger 	struct resource *res;
2636e95c237dSJuerg Haefliger 	struct dme1737_data *data;
2637e95c237dSJuerg Haefliger 	struct device *dev = &pdev->dev;
2638e95c237dSJuerg Haefliger 	int err;
2639e95c237dSJuerg Haefliger 
2640e95c237dSJuerg Haefliger 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2641805fd8c5SGuenter Roeck 	if (!devm_request_region(dev, res->start, DME1737_EXTENT, "dme1737")) {
2642e95c237dSJuerg Haefliger 		dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2643e95c237dSJuerg Haefliger 			(unsigned short)res->start,
2644e95c237dSJuerg Haefliger 			(unsigned short)res->start + DME1737_EXTENT - 1);
2645805fd8c5SGuenter Roeck 		return -EBUSY;
2646e95c237dSJuerg Haefliger 	}
2647e95c237dSJuerg Haefliger 
2648805fd8c5SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct dme1737_data), GFP_KERNEL);
2649805fd8c5SGuenter Roeck 	if (!data)
2650805fd8c5SGuenter Roeck 		return -ENOMEM;
2651e95c237dSJuerg Haefliger 
2652dbc2bc25SJean Delvare 	data->addr = res->start;
2653e95c237dSJuerg Haefliger 	platform_set_drvdata(pdev, data);
2654e95c237dSJuerg Haefliger 
265555d68d75SJuerg Haefliger 	/* Skip chip detection if module is loaded with force_id parameter */
2656ea694431SJuerg Haefliger 	switch (force_id) {
2657ea694431SJuerg Haefliger 	case SCH3112_ID:
2658ea694431SJuerg Haefliger 	case SCH3114_ID:
2659ea694431SJuerg Haefliger 	case SCH3116_ID:
2660ea694431SJuerg Haefliger 		data->type = sch311x;
2661ea694431SJuerg Haefliger 		break;
2662ea694431SJuerg Haefliger 	case SCH5127_ID:
2663ea694431SJuerg Haefliger 		data->type = sch5127;
2664ea694431SJuerg Haefliger 		break;
2665ea694431SJuerg Haefliger 	default:
2666dbc2bc25SJean Delvare 		company = dme1737_read(data, DME1737_REG_COMPANY);
2667dbc2bc25SJean Delvare 		device = dme1737_read(data, DME1737_REG_DEVICE);
2668e95c237dSJuerg Haefliger 
2669ea694431SJuerg Haefliger 		if ((company == DME1737_COMPANY_SMSC) &&
2670ea694431SJuerg Haefliger 		    (device == SCH311X_DEVICE)) {
2671ea694431SJuerg Haefliger 			data->type = sch311x;
2672ea694431SJuerg Haefliger 		} else if ((company == DME1737_COMPANY_SMSC) &&
2673ea694431SJuerg Haefliger 			   (device == SCH5127_DEVICE)) {
2674ea694431SJuerg Haefliger 			data->type = sch5127;
2675ea694431SJuerg Haefliger 		} else {
2676805fd8c5SGuenter Roeck 			return -ENODEV;
2677e95c237dSJuerg Haefliger 		}
267855d68d75SJuerg Haefliger 	}
2679e95c237dSJuerg Haefliger 
2680c8de8362SGuenter Roeck 	if (data->type == sch5127)
2681ea694431SJuerg Haefliger 		data->name = "sch5127";
2682c8de8362SGuenter Roeck 	else
2683dbc2bc25SJean Delvare 		data->name = "sch311x";
2684ea694431SJuerg Haefliger 
2685ea694431SJuerg Haefliger 	/* Initialize the mutex */
2686e95c237dSJuerg Haefliger 	mutex_init(&data->update_lock);
2687e95c237dSJuerg Haefliger 
2688ea694431SJuerg Haefliger 	dev_info(dev, "Found a %s chip at 0x%04x\n",
2689ea694431SJuerg Haefliger 		 data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);
2690e95c237dSJuerg Haefliger 
2691e95c237dSJuerg Haefliger 	/* Initialize the chip */
269206f3d9fbSJuerg Haefliger 	err = dme1737_init_device(dev);
269306f3d9fbSJuerg Haefliger 	if (err) {
2694e95c237dSJuerg Haefliger 		dev_err(dev, "Failed to initialize device.\n");
2695805fd8c5SGuenter Roeck 		return err;
2696e95c237dSJuerg Haefliger 	}
2697e95c237dSJuerg Haefliger 
2698e95c237dSJuerg Haefliger 	/* Create sysfs files */
269906f3d9fbSJuerg Haefliger 	err = dme1737_create_files(dev);
270006f3d9fbSJuerg Haefliger 	if (err) {
2701e95c237dSJuerg Haefliger 		dev_err(dev, "Failed to create sysfs files.\n");
2702805fd8c5SGuenter Roeck 		return err;
2703e95c237dSJuerg Haefliger 	}
2704e95c237dSJuerg Haefliger 
2705e95c237dSJuerg Haefliger 	/* Register device */
2706e95c237dSJuerg Haefliger 	data->hwmon_dev = hwmon_device_register(dev);
2707e95c237dSJuerg Haefliger 	if (IS_ERR(data->hwmon_dev)) {
2708e95c237dSJuerg Haefliger 		dev_err(dev, "Failed to register device.\n");
2709e95c237dSJuerg Haefliger 		err = PTR_ERR(data->hwmon_dev);
2710e95c237dSJuerg Haefliger 		goto exit_remove_files;
2711e95c237dSJuerg Haefliger 	}
2712e95c237dSJuerg Haefliger 
2713e95c237dSJuerg Haefliger 	return 0;
2714e95c237dSJuerg Haefliger 
2715e95c237dSJuerg Haefliger exit_remove_files:
2716e95c237dSJuerg Haefliger 	dme1737_remove_files(dev);
2717e95c237dSJuerg Haefliger 	return err;
2718e95c237dSJuerg Haefliger }
2719e95c237dSJuerg Haefliger 
2720281dfd0bSBill Pemberton static int dme1737_isa_remove(struct platform_device *pdev)
2721e95c237dSJuerg Haefliger {
2722e95c237dSJuerg Haefliger 	struct dme1737_data *data = platform_get_drvdata(pdev);
2723e95c237dSJuerg Haefliger 
2724e95c237dSJuerg Haefliger 	hwmon_device_unregister(data->hwmon_dev);
2725e95c237dSJuerg Haefliger 	dme1737_remove_files(&pdev->dev);
2726e95c237dSJuerg Haefliger 
2727e95c237dSJuerg Haefliger 	return 0;
2728e95c237dSJuerg Haefliger }
2729e95c237dSJuerg Haefliger 
2730e95c237dSJuerg Haefliger static struct platform_driver dme1737_isa_driver = {
2731e95c237dSJuerg Haefliger 	.driver = {
2732e95c237dSJuerg Haefliger 		.owner = THIS_MODULE,
2733e95c237dSJuerg Haefliger 		.name = "dme1737",
2734e95c237dSJuerg Haefliger 	},
2735e95c237dSJuerg Haefliger 	.probe = dme1737_isa_probe,
27369e5e9b7aSBill Pemberton 	.remove = dme1737_isa_remove,
2737e95c237dSJuerg Haefliger };
2738e95c237dSJuerg Haefliger 
2739e95c237dSJuerg Haefliger /* ---------------------------------------------------------------------
274067e2f328SJuerg Haefliger  * Module initialization and cleanup
274167e2f328SJuerg Haefliger  * --------------------------------------------------------------------- */
274267e2f328SJuerg Haefliger 
27439431996fSJuerg Haefliger static int __init dme1737_init(void)
27449431996fSJuerg Haefliger {
2745e95c237dSJuerg Haefliger 	int err;
2746e95c237dSJuerg Haefliger 	unsigned short addr;
2747e95c237dSJuerg Haefliger 
274806f3d9fbSJuerg Haefliger 	err = i2c_add_driver(&dme1737_i2c_driver);
2749c8de8362SGuenter Roeck 	if (err)
2750e95c237dSJuerg Haefliger 		goto exit;
2751e95c237dSJuerg Haefliger 
2752e95c237dSJuerg Haefliger 	if (dme1737_isa_detect(0x2e, &addr) &&
275392430b6fSJuerg Haefliger 	    dme1737_isa_detect(0x4e, &addr) &&
275492430b6fSJuerg Haefliger 	    (!probe_all_addr ||
275592430b6fSJuerg Haefliger 	     (dme1737_isa_detect(0x162e, &addr) &&
275692430b6fSJuerg Haefliger 	      dme1737_isa_detect(0x164e, &addr)))) {
2757e95c237dSJuerg Haefliger 		/* Return 0 if we didn't find an ISA device */
2758e95c237dSJuerg Haefliger 		return 0;
2759e95c237dSJuerg Haefliger 	}
2760e95c237dSJuerg Haefliger 
276106f3d9fbSJuerg Haefliger 	err = platform_driver_register(&dme1737_isa_driver);
2762c8de8362SGuenter Roeck 	if (err)
2763e95c237dSJuerg Haefliger 		goto exit_del_i2c_driver;
2764e95c237dSJuerg Haefliger 
2765e95c237dSJuerg Haefliger 	/* Sets global pdev as a side effect */
276606f3d9fbSJuerg Haefliger 	err = dme1737_isa_device_add(addr);
2767c8de8362SGuenter Roeck 	if (err)
2768e95c237dSJuerg Haefliger 		goto exit_del_isa_driver;
2769e95c237dSJuerg Haefliger 
2770e95c237dSJuerg Haefliger 	return 0;
2771e95c237dSJuerg Haefliger 
2772e95c237dSJuerg Haefliger exit_del_isa_driver:
2773e95c237dSJuerg Haefliger 	platform_driver_unregister(&dme1737_isa_driver);
2774e95c237dSJuerg Haefliger exit_del_i2c_driver:
2775e95c237dSJuerg Haefliger 	i2c_del_driver(&dme1737_i2c_driver);
2776e95c237dSJuerg Haefliger exit:
2777e95c237dSJuerg Haefliger 	return err;
27789431996fSJuerg Haefliger }
27799431996fSJuerg Haefliger 
27809431996fSJuerg Haefliger static void __exit dme1737_exit(void)
27819431996fSJuerg Haefliger {
2782e95c237dSJuerg Haefliger 	if (pdev) {
2783e95c237dSJuerg Haefliger 		platform_device_unregister(pdev);
2784e95c237dSJuerg Haefliger 		platform_driver_unregister(&dme1737_isa_driver);
2785e95c237dSJuerg Haefliger 	}
2786e95c237dSJuerg Haefliger 
2787b237eb25SJuerg Haefliger 	i2c_del_driver(&dme1737_i2c_driver);
27889431996fSJuerg Haefliger }
27899431996fSJuerg Haefliger 
27909431996fSJuerg Haefliger MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
27919431996fSJuerg Haefliger MODULE_DESCRIPTION("DME1737 sensors");
27929431996fSJuerg Haefliger MODULE_LICENSE("GPL");
27939431996fSJuerg Haefliger 
27949431996fSJuerg Haefliger module_init(dme1737_init);
27959431996fSJuerg Haefliger module_exit(dme1737_exit);
2796